Explicit locations: introduce probe locations
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
32d0add0 3 Copyright (C) 1986-2015 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
45741a9c 35#include "infrun.h"
c906108c
SS
36#include "gdbthread.h"
37#include "target.h"
38#include "language.h"
50f182aa 39#include "gdb-demangle.h"
0ba1096a 40#include "filenames.h"
c906108c
SS
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
5b7f31a4 47#include "gdb.h"
8b93c638 48#include "ui-out.h"
e1507482 49#include "cli/cli-script.h"
fe898f56 50#include "block.h"
a77053c2 51#include "solib.h"
84acb35a
JJ
52#include "solist.h"
53#include "observer.h"
765dc015 54#include "memattr.h"
f7f9143b 55#include "ada-lang.h"
d1aa2f50 56#include "top.h"
79a45b7d 57#include "valprint.h"
4efc6507 58#include "jit.h"
65d79d4b 59#include "parser-defs.h"
55aa24fb
SDJ
60#include "gdb_regex.h"
61#include "probe.h"
e9cafbcc 62#include "cli/cli-utils.h"
be34f849 63#include "continuations.h"
1bfeeb0f
JL
64#include "stack.h"
65#include "skip.h"
b775012e 66#include "ax-gdb.h"
e2e4d78b 67#include "dummy-frame.h"
5589af0e 68#include "interps.h"
d3ce09f5 69#include "format.h"
f00aae0f 70#include "location.h"
d3ce09f5 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"
6dddc817 80#include "extension.h"
104c1213 81
e7e8980f
YQ
82/* Enums for exception-handling support. */
83enum exception_event_kind
84{
85 EX_EVENT_THROW,
591f19e8 86 EX_EVENT_RETHROW,
e7e8980f
YQ
87 EX_EVENT_CATCH
88};
89
4a64f543 90/* Prototypes for local functions. */
c906108c 91
a14ed312 92static void enable_delete_command (char *, int);
c906108c 93
a14ed312 94static void enable_once_command (char *, int);
c906108c 95
816338b5
SS
96static void enable_count_command (char *, int);
97
a14ed312 98static void disable_command (char *, int);
c906108c 99
a14ed312 100static void enable_command (char *, int);
c906108c 101
95a42b64
TT
102static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
103 void *),
104 void *);
c906108c 105
a14ed312 106static void ignore_command (char *, int);
c906108c 107
4efb68b1 108static int breakpoint_re_set_one (void *);
c906108c 109
348d480f
PA
110static void breakpoint_re_set_default (struct breakpoint *);
111
f00aae0f
KS
112static void
113 create_sals_from_location_default (const struct event_location *location,
114 struct linespec_result *canonical,
115 enum bptype type_wanted);
983af33b
SDJ
116
117static void create_breakpoints_sal_default (struct gdbarch *,
118 struct linespec_result *,
e7e0cddf 119 char *, char *, enum bptype,
983af33b
SDJ
120 enum bpdisp, int, int,
121 int,
122 const struct breakpoint_ops *,
44f238bb 123 int, int, int, unsigned);
983af33b 124
f00aae0f
KS
125static void decode_location_default (struct breakpoint *b,
126 const struct event_location *location,
127 struct symtabs_and_lines *sals);
983af33b 128
a14ed312 129static void clear_command (char *, int);
c906108c 130
a14ed312 131static void catch_command (char *, int);
c906108c 132
a9634178 133static int can_use_hardware_watchpoint (struct value *);
c906108c 134
98deb0da 135static void break_command_1 (char *, int, int);
c906108c 136
a14ed312 137static void mention (struct breakpoint *);
c906108c 138
348d480f
PA
139static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
140 enum bptype,
c0a91b2b 141 const struct breakpoint_ops *);
3742cc8b
YQ
142static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
143 const struct symtab_and_line *);
144
4a64f543
MS
145/* This function is used in gdbtk sources and thus can not be made
146 static. */
63c252f8 147struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 148 struct symtab_and_line,
c0a91b2b
TT
149 enum bptype,
150 const struct breakpoint_ops *);
c906108c 151
06edf0c0
PA
152static struct breakpoint *
153 momentary_breakpoint_from_master (struct breakpoint *orig,
154 enum bptype type,
a1aa2221
LM
155 const struct breakpoint_ops *ops,
156 int loc_enabled);
06edf0c0 157
76897487
KB
158static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
159
a6d9a66e
UW
160static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
161 CORE_ADDR bpaddr,
88f7da05 162 enum bptype bptype);
76897487 163
6c95b8df
PA
164static void describe_other_breakpoints (struct gdbarch *,
165 struct program_space *, CORE_ADDR,
5af949e3 166 struct obj_section *, int);
c906108c 167
85d721b8
PA
168static int watchpoint_locations_match (struct bp_location *loc1,
169 struct bp_location *loc2);
170
f1310107
TJB
171static int breakpoint_location_address_match (struct bp_location *bl,
172 struct address_space *aspace,
173 CORE_ADDR addr);
174
a14ed312 175static void breakpoints_info (char *, int);
c906108c 176
d77f58be
SS
177static void watchpoints_info (char *, int);
178
e5a67952
MS
179static int breakpoint_1 (char *, int,
180 int (*) (const struct breakpoint *));
c906108c 181
4efb68b1 182static int breakpoint_cond_eval (void *);
c906108c 183
4efb68b1 184static void cleanup_executing_breakpoints (void *);
c906108c 185
a14ed312 186static void commands_command (char *, int);
c906108c 187
a14ed312 188static void condition_command (char *, int);
c906108c 189
c5aa993b
JM
190typedef enum
191 {
192 mark_inserted,
193 mark_uninserted
194 }
195insertion_state_t;
c906108c 196
0bde7532 197static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 198static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 199
e514a9d6 200static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 201
4efb68b1 202static int watchpoint_check (void *);
c906108c 203
a14ed312 204static void maintenance_info_breakpoints (char *, int);
c906108c 205
a14ed312 206static int hw_breakpoint_used_count (void);
c906108c 207
a1398e0c
PA
208static int hw_watchpoint_use_count (struct breakpoint *);
209
210static int hw_watchpoint_used_count_others (struct breakpoint *except,
211 enum bptype type,
212 int *other_type_used);
c906108c 213
a14ed312 214static void hbreak_command (char *, int);
c906108c 215
a14ed312 216static void thbreak_command (char *, int);
c906108c 217
816338b5
SS
218static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
219 int count);
c906108c 220
a14ed312 221static void stop_command (char *arg, int from_tty);
7a292a7a 222
a14ed312 223static void stopin_command (char *arg, int from_tty);
7a292a7a 224
a14ed312 225static void stopat_command (char *arg, int from_tty);
7a292a7a 226
a14ed312 227static void tcatch_command (char *arg, int from_tty);
7a292a7a 228
fe3f5fa8 229static void free_bp_location (struct bp_location *loc);
f431efe5
PA
230static void incref_bp_location (struct bp_location *loc);
231static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 232
39d61571 233static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 234
44702360
PA
235/* update_global_location_list's modes of operation wrt to whether to
236 insert locations now. */
237enum ugll_insert_mode
238{
239 /* Don't insert any breakpoint locations into the inferior, only
240 remove already-inserted locations that no longer should be
241 inserted. Functions that delete a breakpoint or breakpoints
242 should specify this mode, so that deleting a breakpoint doesn't
243 have the side effect of inserting the locations of other
244 breakpoints that are marked not-inserted, but should_be_inserted
245 returns true on them.
246
247 This behavior is useful is situations close to tear-down -- e.g.,
248 after an exec, while the target still has execution, but
249 breakpoint shadows of the previous executable image should *NOT*
250 be restored to the new image; or before detaching, where the
251 target still has execution and wants to delete breakpoints from
252 GDB's lists, and all breakpoints had already been removed from
253 the inferior. */
254 UGLL_DONT_INSERT,
255
a25a5a45
PA
256 /* May insert breakpoints iff breakpoints_should_be_inserted_now
257 claims breakpoints should be inserted now. */
04086b45
PA
258 UGLL_MAY_INSERT,
259
a25a5a45
PA
260 /* Insert locations now, irrespective of
261 breakpoints_should_be_inserted_now. E.g., say all threads are
262 stopped right now, and the user did "continue". We need to
263 insert breakpoints _before_ resuming the target, but
264 UGLL_MAY_INSERT wouldn't insert them, because
265 breakpoints_should_be_inserted_now returns false at that point,
266 as no thread is running yet. */
04086b45 267 UGLL_INSERT
44702360
PA
268};
269
270static void update_global_location_list (enum ugll_insert_mode);
a5606eee 271
44702360 272static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 273
d77f58be 274static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
275
276static void insert_breakpoint_locations (void);
a5606eee 277
1042e4c0
SS
278static void tracepoints_info (char *, int);
279
280static void delete_trace_command (char *, int);
281
282static void enable_trace_command (char *, int);
283
284static void disable_trace_command (char *, int);
285
286static void trace_pass_command (char *, int);
287
558a9d82
YQ
288static void set_tracepoint_count (int num);
289
9c06b0b4
TJB
290static int is_masked_watchpoint (const struct breakpoint *b);
291
b775012e
LM
292static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
293
983af33b
SDJ
294/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
295 otherwise. */
296
297static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 298
2060206e
PA
299/* The breakpoint_ops structure to be inherited by all breakpoint_ops
300 that are implemented on top of software or hardware breakpoints
301 (user breakpoints, internal and momentary breakpoints, etc.). */
302static struct breakpoint_ops bkpt_base_breakpoint_ops;
303
304/* Internal breakpoints class type. */
06edf0c0 305static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
306
307/* Momentary breakpoints class type. */
06edf0c0
PA
308static struct breakpoint_ops momentary_breakpoint_ops;
309
e2e4d78b
JK
310/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
311static struct breakpoint_ops longjmp_breakpoint_ops;
312
2060206e
PA
313/* The breakpoint_ops structure to be used in regular user created
314 breakpoints. */
315struct breakpoint_ops bkpt_breakpoint_ops;
316
55aa24fb
SDJ
317/* Breakpoints set on probes. */
318static struct breakpoint_ops bkpt_probe_breakpoint_ops;
319
e7e0cddf 320/* Dynamic printf class type. */
c5867ab6 321struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 322
d3ce09f5
SS
323/* The style in which to perform a dynamic printf. This is a user
324 option because different output options have different tradeoffs;
325 if GDB does the printing, there is better error handling if there
326 is a problem with any of the arguments, but using an inferior
327 function lets you have special-purpose printers and sending of
328 output to the same place as compiled-in print functions. */
329
330static const char dprintf_style_gdb[] = "gdb";
331static const char dprintf_style_call[] = "call";
332static const char dprintf_style_agent[] = "agent";
333static const char *const dprintf_style_enums[] = {
334 dprintf_style_gdb,
335 dprintf_style_call,
336 dprintf_style_agent,
337 NULL
338};
339static const char *dprintf_style = dprintf_style_gdb;
340
341/* The function to use for dynamic printf if the preferred style is to
342 call into the inferior. The value is simply a string that is
343 copied into the command, so it can be anything that GDB can
344 evaluate to a callable address, not necessarily a function name. */
345
346static char *dprintf_function = "";
347
348/* The channel to use for dynamic printf if the preferred style is to
349 call into the inferior; if a nonempty string, it will be passed to
350 the call as the first argument, with the format string as the
351 second. As with the dprintf function, this can be anything that
352 GDB knows how to evaluate, so in addition to common choices like
353 "stderr", this could be an app-specific expression like
354 "mystreams[curlogger]". */
355
356static char *dprintf_channel = "";
357
358/* True if dprintf commands should continue to operate even if GDB
359 has disconnected. */
360static int disconnected_dprintf = 1;
361
5cea2a26
PA
362/* A reference-counted struct command_line. This lets multiple
363 breakpoints share a single command list. */
364struct counted_command_line
365{
366 /* The reference count. */
367 int refc;
368
369 /* The command list. */
370 struct command_line *commands;
371};
372
373struct command_line *
374breakpoint_commands (struct breakpoint *b)
375{
376 return b->commands ? b->commands->commands : NULL;
377}
3daf8fe5 378
f3b1572e
PA
379/* Flag indicating that a command has proceeded the inferior past the
380 current breakpoint. */
381
382static int breakpoint_proceeded;
383
956a9fb9 384const char *
2cec12e5
AR
385bpdisp_text (enum bpdisp disp)
386{
4a64f543
MS
387 /* NOTE: the following values are a part of MI protocol and
388 represent values of 'disp' field returned when inferior stops at
389 a breakpoint. */
bc043ef3 390 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 391
2cec12e5
AR
392 return bpdisps[(int) disp];
393}
c906108c 394
4a64f543 395/* Prototypes for exported functions. */
c906108c 396/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 397 if such is available. */
c906108c
SS
398static int can_use_hw_watchpoints;
399
920d2a44
AC
400static void
401show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
402 struct cmd_list_element *c,
403 const char *value)
404{
3e43a32a
MS
405 fprintf_filtered (file,
406 _("Debugger's willingness to use "
407 "watchpoint hardware is %s.\n"),
920d2a44
AC
408 value);
409}
410
fa8d40ab
JJ
411/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
412 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 413 for unrecognized breakpoint locations.
fa8d40ab
JJ
414 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
415static enum auto_boolean pending_break_support;
920d2a44
AC
416static void
417show_pending_break_support (struct ui_file *file, int from_tty,
418 struct cmd_list_element *c,
419 const char *value)
420{
3e43a32a
MS
421 fprintf_filtered (file,
422 _("Debugger's behavior regarding "
423 "pending breakpoints is %s.\n"),
920d2a44
AC
424 value);
425}
fa8d40ab 426
765dc015 427/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 428 set with "break" but falling in read-only memory.
765dc015
VP
429 If 0, gdb will warn about such breakpoints, but won't automatically
430 use hardware breakpoints. */
431static int automatic_hardware_breakpoints;
432static void
433show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
434 struct cmd_list_element *c,
435 const char *value)
436{
3e43a32a
MS
437 fprintf_filtered (file,
438 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
439 value);
440}
441
a25a5a45
PA
442/* If on, GDB keeps breakpoints inserted even if the inferior is
443 stopped, and immediately inserts any new breakpoints as soon as
444 they're created. If off (default), GDB keeps breakpoints off of
445 the target as long as possible. That is, it delays inserting
446 breakpoints until the next resume, and removes them again when the
447 target fully stops. This is a bit safer in case GDB crashes while
448 processing user input. */
449static int always_inserted_mode = 0;
72d0e2c5 450
33e5cbd6 451static void
74960c60 452show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 453 struct cmd_list_element *c, const char *value)
74960c60 454{
a25a5a45
PA
455 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
456 value);
74960c60
VP
457}
458
b57bacec
PA
459/* See breakpoint.h. */
460
33e5cbd6 461int
a25a5a45 462breakpoints_should_be_inserted_now (void)
33e5cbd6 463{
a25a5a45
PA
464 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
465 {
466 /* If breakpoints are global, they should be inserted even if no
467 thread under gdb's control is running, or even if there are
468 no threads under GDB's control yet. */
469 return 1;
470 }
471 else if (target_has_execution)
472 {
372316f1
PA
473 struct thread_info *tp;
474
a25a5a45
PA
475 if (always_inserted_mode)
476 {
477 /* The user wants breakpoints inserted even if all threads
478 are stopped. */
479 return 1;
480 }
481
b57bacec
PA
482 if (threads_are_executing ())
483 return 1;
372316f1
PA
484
485 /* Don't remove breakpoints yet if, even though all threads are
486 stopped, we still have events to process. */
487 ALL_NON_EXITED_THREADS (tp)
488 if (tp->resumed
489 && tp->suspend.waitstatus_pending_p)
490 return 1;
a25a5a45
PA
491 }
492 return 0;
33e5cbd6 493}
765dc015 494
b775012e
LM
495static const char condition_evaluation_both[] = "host or target";
496
497/* Modes for breakpoint condition evaluation. */
498static const char condition_evaluation_auto[] = "auto";
499static const char condition_evaluation_host[] = "host";
500static const char condition_evaluation_target[] = "target";
501static const char *const condition_evaluation_enums[] = {
502 condition_evaluation_auto,
503 condition_evaluation_host,
504 condition_evaluation_target,
505 NULL
506};
507
508/* Global that holds the current mode for breakpoint condition evaluation. */
509static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
510
511/* Global that we use to display information to the user (gets its value from
512 condition_evaluation_mode_1. */
513static const char *condition_evaluation_mode = condition_evaluation_auto;
514
515/* Translate a condition evaluation mode MODE into either "host"
516 or "target". This is used mostly to translate from "auto" to the
517 real setting that is being used. It returns the translated
518 evaluation mode. */
519
520static const char *
521translate_condition_evaluation_mode (const char *mode)
522{
523 if (mode == condition_evaluation_auto)
524 {
525 if (target_supports_evaluation_of_breakpoint_conditions ())
526 return condition_evaluation_target;
527 else
528 return condition_evaluation_host;
529 }
530 else
531 return mode;
532}
533
534/* Discovers what condition_evaluation_auto translates to. */
535
536static const char *
537breakpoint_condition_evaluation_mode (void)
538{
539 return translate_condition_evaluation_mode (condition_evaluation_mode);
540}
541
542/* Return true if GDB should evaluate breakpoint conditions or false
543 otherwise. */
544
545static int
546gdb_evaluates_breakpoint_condition_p (void)
547{
548 const char *mode = breakpoint_condition_evaluation_mode ();
549
550 return (mode == condition_evaluation_host);
551}
552
a14ed312 553void _initialize_breakpoint (void);
c906108c 554
c906108c
SS
555/* Are we executing breakpoint commands? */
556static int executing_breakpoint_commands;
557
c02f5703
MS
558/* Are overlay event breakpoints enabled? */
559static int overlay_events_enabled;
560
e09342b5
TJB
561/* See description in breakpoint.h. */
562int target_exact_watchpoints = 0;
563
c906108c 564/* Walk the following statement or block through all breakpoints.
e5dd4106 565 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 566 current breakpoint. */
c906108c 567
5c44784c 568#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 569
5c44784c
JM
570#define ALL_BREAKPOINTS_SAFE(B,TMP) \
571 for (B = breakpoint_chain; \
572 B ? (TMP=B->next, 1): 0; \
573 B = TMP)
c906108c 574
4a64f543
MS
575/* Similar iterator for the low-level breakpoints. SAFE variant is
576 not provided so update_global_location_list must not be called
577 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 578
876fa593
JK
579#define ALL_BP_LOCATIONS(B,BP_TMP) \
580 for (BP_TMP = bp_location; \
581 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
582 BP_TMP++)
7cc221ef 583
b775012e
LM
584/* Iterates through locations with address ADDRESS for the currently selected
585 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
586 to where the loop should start from.
587 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
588 appropriate location to start with. */
589
590#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
591 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
592 BP_LOCP_TMP = BP_LOCP_START; \
593 BP_LOCP_START \
594 && (BP_LOCP_TMP < bp_location + bp_location_count \
595 && (*BP_LOCP_TMP)->address == ADDRESS); \
596 BP_LOCP_TMP++)
597
1042e4c0
SS
598/* Iterator for tracepoints only. */
599
600#define ALL_TRACEPOINTS(B) \
601 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 602 if (is_tracepoint (B))
1042e4c0 603
7cc221ef 604/* Chains of all breakpoints defined. */
c906108c
SS
605
606struct breakpoint *breakpoint_chain;
607
876fa593
JK
608/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
609
610static struct bp_location **bp_location;
611
612/* Number of elements of BP_LOCATION. */
613
614static unsigned bp_location_count;
615
4a64f543
MS
616/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
617 ADDRESS for the current elements of BP_LOCATION which get a valid
618 result from bp_location_has_shadow. You can use it for roughly
619 limiting the subrange of BP_LOCATION to scan for shadow bytes for
620 an address you need to read. */
876fa593
JK
621
622static CORE_ADDR bp_location_placed_address_before_address_max;
623
4a64f543
MS
624/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
625 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
626 BP_LOCATION which get a valid result from bp_location_has_shadow.
627 You can use it for roughly limiting the subrange of BP_LOCATION to
628 scan for shadow bytes for an address you need to read. */
876fa593
JK
629
630static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 631
4a64f543
MS
632/* The locations that no longer correspond to any breakpoint, unlinked
633 from bp_location array, but for which a hit may still be reported
634 by a target. */
20874c92
VP
635VEC(bp_location_p) *moribund_locations = NULL;
636
c906108c
SS
637/* Number of last breakpoint made. */
638
95a42b64
TT
639static int breakpoint_count;
640
86b17b60
PA
641/* The value of `breakpoint_count' before the last command that
642 created breakpoints. If the last (break-like) command created more
643 than one breakpoint, then the difference between BREAKPOINT_COUNT
644 and PREV_BREAKPOINT_COUNT is more than one. */
645static int prev_breakpoint_count;
c906108c 646
1042e4c0
SS
647/* Number of last tracepoint made. */
648
95a42b64 649static int tracepoint_count;
1042e4c0 650
6149aea9
PA
651static struct cmd_list_element *breakpoint_set_cmdlist;
652static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 653struct cmd_list_element *save_cmdlist;
6149aea9 654
badd37ce
SDJ
655/* See declaration at breakpoint.h. */
656
657struct breakpoint *
658breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
659 void *user_data)
660{
661 struct breakpoint *b = NULL;
662
663 ALL_BREAKPOINTS (b)
664 {
665 if (func (b, user_data) != 0)
666 break;
667 }
668
669 return b;
670}
671
468d015d
JJ
672/* Return whether a breakpoint is an active enabled breakpoint. */
673static int
674breakpoint_enabled (struct breakpoint *b)
675{
0d381245 676 return (b->enable_state == bp_enabled);
468d015d
JJ
677}
678
c906108c
SS
679/* Set breakpoint count to NUM. */
680
95a42b64 681static void
fba45db2 682set_breakpoint_count (int num)
c906108c 683{
86b17b60 684 prev_breakpoint_count = breakpoint_count;
c906108c 685 breakpoint_count = num;
4fa62494 686 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
687}
688
86b17b60
PA
689/* Used by `start_rbreak_breakpoints' below, to record the current
690 breakpoint count before "rbreak" creates any breakpoint. */
691static int rbreak_start_breakpoint_count;
692
95a42b64
TT
693/* Called at the start an "rbreak" command to record the first
694 breakpoint made. */
86b17b60 695
95a42b64
TT
696void
697start_rbreak_breakpoints (void)
698{
86b17b60 699 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
700}
701
702/* Called at the end of an "rbreak" command to record the last
703 breakpoint made. */
86b17b60 704
95a42b64
TT
705void
706end_rbreak_breakpoints (void)
707{
86b17b60 708 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
709}
710
4a64f543 711/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
712
713void
fba45db2 714clear_breakpoint_hit_counts (void)
c906108c
SS
715{
716 struct breakpoint *b;
717
718 ALL_BREAKPOINTS (b)
719 b->hit_count = 0;
720}
721
9add0f1b
TT
722/* Allocate a new counted_command_line with reference count of 1.
723 The new structure owns COMMANDS. */
724
725static struct counted_command_line *
726alloc_counted_command_line (struct command_line *commands)
727{
728 struct counted_command_line *result
729 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 730
9add0f1b
TT
731 result->refc = 1;
732 result->commands = commands;
733 return result;
734}
735
736/* Increment reference count. This does nothing if CMD is NULL. */
737
738static void
739incref_counted_command_line (struct counted_command_line *cmd)
740{
741 if (cmd)
742 ++cmd->refc;
743}
744
745/* Decrement reference count. If the reference count reaches 0,
746 destroy the counted_command_line. Sets *CMDP to NULL. This does
747 nothing if *CMDP is NULL. */
748
749static void
750decref_counted_command_line (struct counted_command_line **cmdp)
751{
752 if (*cmdp)
753 {
754 if (--(*cmdp)->refc == 0)
755 {
756 free_command_lines (&(*cmdp)->commands);
757 xfree (*cmdp);
758 }
759 *cmdp = NULL;
760 }
761}
762
763/* A cleanup function that calls decref_counted_command_line. */
764
765static void
766do_cleanup_counted_command_line (void *arg)
767{
768 decref_counted_command_line (arg);
769}
770
771/* Create a cleanup that calls decref_counted_command_line on the
772 argument. */
773
774static struct cleanup *
775make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
776{
777 return make_cleanup (do_cleanup_counted_command_line, cmdp);
778}
779
c906108c 780\f
48cb2d85
VP
781/* Return the breakpoint with the specified number, or NULL
782 if the number does not refer to an existing breakpoint. */
783
784struct breakpoint *
785get_breakpoint (int num)
786{
787 struct breakpoint *b;
788
789 ALL_BREAKPOINTS (b)
790 if (b->number == num)
791 return b;
792
793 return NULL;
794}
5c44784c 795
c906108c 796\f
adc36818 797
b775012e
LM
798/* Mark locations as "conditions have changed" in case the target supports
799 evaluating conditions on its side. */
800
801static void
802mark_breakpoint_modified (struct breakpoint *b)
803{
804 struct bp_location *loc;
805
806 /* This is only meaningful if the target is
807 evaluating conditions and if the user has
808 opted for condition evaluation on the target's
809 side. */
810 if (gdb_evaluates_breakpoint_condition_p ()
811 || !target_supports_evaluation_of_breakpoint_conditions ())
812 return;
813
814 if (!is_breakpoint (b))
815 return;
816
817 for (loc = b->loc; loc; loc = loc->next)
818 loc->condition_changed = condition_modified;
819}
820
821/* Mark location as "conditions have changed" in case the target supports
822 evaluating conditions on its side. */
823
824static void
825mark_breakpoint_location_modified (struct bp_location *loc)
826{
827 /* This is only meaningful if the target is
828 evaluating conditions and if the user has
829 opted for condition evaluation on the target's
830 side. */
831 if (gdb_evaluates_breakpoint_condition_p ()
832 || !target_supports_evaluation_of_breakpoint_conditions ())
833
834 return;
835
836 if (!is_breakpoint (loc->owner))
837 return;
838
839 loc->condition_changed = condition_modified;
840}
841
842/* Sets the condition-evaluation mode using the static global
843 condition_evaluation_mode. */
844
845static void
846set_condition_evaluation_mode (char *args, int from_tty,
847 struct cmd_list_element *c)
848{
b775012e
LM
849 const char *old_mode, *new_mode;
850
851 if ((condition_evaluation_mode_1 == condition_evaluation_target)
852 && !target_supports_evaluation_of_breakpoint_conditions ())
853 {
854 condition_evaluation_mode_1 = condition_evaluation_mode;
855 warning (_("Target does not support breakpoint condition evaluation.\n"
856 "Using host evaluation mode instead."));
857 return;
858 }
859
860 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
861 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
862
abf1152a
JK
863 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
864 settings was "auto". */
865 condition_evaluation_mode = condition_evaluation_mode_1;
866
b775012e
LM
867 /* Only update the mode if the user picked a different one. */
868 if (new_mode != old_mode)
869 {
870 struct bp_location *loc, **loc_tmp;
871 /* If the user switched to a different evaluation mode, we
872 need to synch the changes with the target as follows:
873
874 "host" -> "target": Send all (valid) conditions to the target.
875 "target" -> "host": Remove all the conditions from the target.
876 */
877
b775012e
LM
878 if (new_mode == condition_evaluation_target)
879 {
880 /* Mark everything modified and synch conditions with the
881 target. */
882 ALL_BP_LOCATIONS (loc, loc_tmp)
883 mark_breakpoint_location_modified (loc);
884 }
885 else
886 {
887 /* Manually mark non-duplicate locations to synch conditions
888 with the target. We do this to remove all the conditions the
889 target knows about. */
890 ALL_BP_LOCATIONS (loc, loc_tmp)
891 if (is_breakpoint (loc->owner) && loc->inserted)
892 loc->needs_update = 1;
893 }
894
895 /* Do the update. */
44702360 896 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
897 }
898
899 return;
900}
901
902/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
903 what "auto" is translating to. */
904
905static void
906show_condition_evaluation_mode (struct ui_file *file, int from_tty,
907 struct cmd_list_element *c, const char *value)
908{
909 if (condition_evaluation_mode == condition_evaluation_auto)
910 fprintf_filtered (file,
911 _("Breakpoint condition evaluation "
912 "mode is %s (currently %s).\n"),
913 value,
914 breakpoint_condition_evaluation_mode ());
915 else
916 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
917 value);
918}
919
920/* A comparison function for bp_location AP and BP that is used by
921 bsearch. This comparison function only cares about addresses, unlike
922 the more general bp_location_compare function. */
923
924static int
925bp_location_compare_addrs (const void *ap, const void *bp)
926{
927 struct bp_location *a = *(void **) ap;
928 struct bp_location *b = *(void **) bp;
929
930 if (a->address == b->address)
931 return 0;
932 else
933 return ((a->address > b->address) - (a->address < b->address));
934}
935
936/* Helper function to skip all bp_locations with addresses
937 less than ADDRESS. It returns the first bp_location that
938 is greater than or equal to ADDRESS. If none is found, just
939 return NULL. */
940
941static struct bp_location **
942get_first_locp_gte_addr (CORE_ADDR address)
943{
944 struct bp_location dummy_loc;
945 struct bp_location *dummy_locp = &dummy_loc;
946 struct bp_location **locp_found = NULL;
947
948 /* Initialize the dummy location's address field. */
949 memset (&dummy_loc, 0, sizeof (struct bp_location));
950 dummy_loc.address = address;
951
952 /* Find a close match to the first location at ADDRESS. */
953 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
954 sizeof (struct bp_location **),
955 bp_location_compare_addrs);
956
957 /* Nothing was found, nothing left to do. */
958 if (locp_found == NULL)
959 return NULL;
960
961 /* We may have found a location that is at ADDRESS but is not the first in the
962 location's list. Go backwards (if possible) and locate the first one. */
963 while ((locp_found - 1) >= bp_location
964 && (*(locp_found - 1))->address == address)
965 locp_found--;
966
967 return locp_found;
968}
969
adc36818 970void
7a26bd4d 971set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
972 int from_tty)
973{
3a5c3e22
PA
974 xfree (b->cond_string);
975 b->cond_string = NULL;
adc36818 976
3a5c3e22 977 if (is_watchpoint (b))
adc36818 978 {
3a5c3e22
PA
979 struct watchpoint *w = (struct watchpoint *) b;
980
981 xfree (w->cond_exp);
982 w->cond_exp = NULL;
983 }
984 else
985 {
986 struct bp_location *loc;
987
988 for (loc = b->loc; loc; loc = loc->next)
989 {
990 xfree (loc->cond);
991 loc->cond = NULL;
b775012e
LM
992
993 /* No need to free the condition agent expression
994 bytecode (if we have one). We will handle this
995 when we go through update_global_location_list. */
3a5c3e22 996 }
adc36818 997 }
adc36818
PM
998
999 if (*exp == 0)
1000 {
1001 if (from_tty)
1002 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1003 }
1004 else
1005 {
bbc13ae3 1006 const char *arg = exp;
cc59ec59 1007
adc36818
PM
1008 /* I don't know if it matters whether this is the string the user
1009 typed in or the decompiled expression. */
1010 b->cond_string = xstrdup (arg);
1011 b->condition_not_parsed = 0;
1012
1013 if (is_watchpoint (b))
1014 {
3a5c3e22
PA
1015 struct watchpoint *w = (struct watchpoint *) b;
1016
adc36818
PM
1017 innermost_block = NULL;
1018 arg = exp;
1bb9788d 1019 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
1020 if (*arg)
1021 error (_("Junk at end of expression"));
3a5c3e22 1022 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1023 }
1024 else
1025 {
3a5c3e22
PA
1026 struct bp_location *loc;
1027
adc36818
PM
1028 for (loc = b->loc; loc; loc = loc->next)
1029 {
1030 arg = exp;
1031 loc->cond =
1bb9788d
TT
1032 parse_exp_1 (&arg, loc->address,
1033 block_for_pc (loc->address), 0);
adc36818
PM
1034 if (*arg)
1035 error (_("Junk at end of expression"));
1036 }
1037 }
1038 }
b775012e
LM
1039 mark_breakpoint_modified (b);
1040
8d3788bd 1041 observer_notify_breakpoint_modified (b);
adc36818
PM
1042}
1043
d55637df
TT
1044/* Completion for the "condition" command. */
1045
1046static VEC (char_ptr) *
6f937416
PA
1047condition_completer (struct cmd_list_element *cmd,
1048 const char *text, const char *word)
d55637df 1049{
6f937416 1050 const char *space;
d55637df 1051
6f937416
PA
1052 text = skip_spaces_const (text);
1053 space = skip_to_space_const (text);
d55637df
TT
1054 if (*space == '\0')
1055 {
1056 int len;
1057 struct breakpoint *b;
1058 VEC (char_ptr) *result = NULL;
1059
1060 if (text[0] == '$')
1061 {
1062 /* We don't support completion of history indices. */
1063 if (isdigit (text[1]))
1064 return NULL;
1065 return complete_internalvar (&text[1]);
1066 }
1067
1068 /* We're completing the breakpoint number. */
1069 len = strlen (text);
1070
1071 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1072 {
1073 char number[50];
1074
1075 xsnprintf (number, sizeof (number), "%d", b->number);
1076
1077 if (strncmp (number, text, len) == 0)
1078 VEC_safe_push (char_ptr, result, xstrdup (number));
1079 }
d55637df
TT
1080
1081 return result;
1082 }
1083
1084 /* We're completing the expression part. */
6f937416 1085 text = skip_spaces_const (space);
d55637df
TT
1086 return expression_completer (cmd, text, word);
1087}
1088
c906108c
SS
1089/* condition N EXP -- set break condition of breakpoint N to EXP. */
1090
1091static void
fba45db2 1092condition_command (char *arg, int from_tty)
c906108c 1093{
52f0bd74 1094 struct breakpoint *b;
c906108c 1095 char *p;
52f0bd74 1096 int bnum;
c906108c
SS
1097
1098 if (arg == 0)
e2e0b3e5 1099 error_no_arg (_("breakpoint number"));
c906108c
SS
1100
1101 p = arg;
1102 bnum = get_number (&p);
5c44784c 1103 if (bnum == 0)
8a3fe4f8 1104 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1105
1106 ALL_BREAKPOINTS (b)
1107 if (b->number == bnum)
2f069f6f 1108 {
6dddc817
DE
1109 /* Check if this breakpoint has a "stop" method implemented in an
1110 extension language. This method and conditions entered into GDB
1111 from the CLI are mutually exclusive. */
1112 const struct extension_language_defn *extlang
1113 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1114
1115 if (extlang != NULL)
1116 {
1117 error (_("Only one stop condition allowed. There is currently"
1118 " a %s stop condition defined for this breakpoint."),
1119 ext_lang_capitalized_name (extlang));
1120 }
2566ad2d 1121 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1122
1123 if (is_breakpoint (b))
44702360 1124 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1125
2f069f6f
JB
1126 return;
1127 }
c906108c 1128
8a3fe4f8 1129 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1130}
1131
a7bdde9e
VP
1132/* Check that COMMAND do not contain commands that are suitable
1133 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1134 Throw if any such commands is found. */
1135
a7bdde9e
VP
1136static void
1137check_no_tracepoint_commands (struct command_line *commands)
1138{
1139 struct command_line *c;
cc59ec59 1140
a7bdde9e
VP
1141 for (c = commands; c; c = c->next)
1142 {
1143 int i;
1144
1145 if (c->control_type == while_stepping_control)
3e43a32a
MS
1146 error (_("The 'while-stepping' command can "
1147 "only be used for tracepoints"));
a7bdde9e
VP
1148
1149 for (i = 0; i < c->body_count; ++i)
1150 check_no_tracepoint_commands ((c->body_list)[i]);
1151
1152 /* Not that command parsing removes leading whitespace and comment
4a64f543 1153 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1154 command directly. */
1155 if (strstr (c->line, "collect ") == c->line)
1156 error (_("The 'collect' command can only be used for tracepoints"));
1157
51661e93
VP
1158 if (strstr (c->line, "teval ") == c->line)
1159 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1160 }
1161}
1162
d77f58be
SS
1163/* Encapsulate tests for different types of tracepoints. */
1164
d9b3f62e
PA
1165static int
1166is_tracepoint_type (enum bptype type)
1167{
1168 return (type == bp_tracepoint
1169 || type == bp_fast_tracepoint
1170 || type == bp_static_tracepoint);
1171}
1172
a7bdde9e 1173int
d77f58be 1174is_tracepoint (const struct breakpoint *b)
a7bdde9e 1175{
d9b3f62e 1176 return is_tracepoint_type (b->type);
a7bdde9e 1177}
d9b3f62e 1178
e5dd4106 1179/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1180 breakpoint. This function will throw an exception if a problem is
1181 found. */
48cb2d85 1182
95a42b64
TT
1183static void
1184validate_commands_for_breakpoint (struct breakpoint *b,
1185 struct command_line *commands)
48cb2d85 1186{
d77f58be 1187 if (is_tracepoint (b))
a7bdde9e 1188 {
c9a6ce02 1189 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1190 struct command_line *c;
1191 struct command_line *while_stepping = 0;
c9a6ce02
PA
1192
1193 /* Reset the while-stepping step count. The previous commands
1194 might have included a while-stepping action, while the new
1195 ones might not. */
1196 t->step_count = 0;
1197
1198 /* We need to verify that each top-level element of commands is
1199 valid for tracepoints, that there's at most one
1200 while-stepping element, and that the while-stepping's body
1201 has valid tracing commands excluding nested while-stepping.
1202 We also need to validate the tracepoint action line in the
1203 context of the tracepoint --- validate_actionline actually
1204 has side effects, like setting the tracepoint's
1205 while-stepping STEP_COUNT, in addition to checking if the
1206 collect/teval actions parse and make sense in the
1207 tracepoint's context. */
a7bdde9e
VP
1208 for (c = commands; c; c = c->next)
1209 {
a7bdde9e
VP
1210 if (c->control_type == while_stepping_control)
1211 {
1212 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1213 error (_("The 'while-stepping' command "
1214 "cannot be used for fast tracepoint"));
0fb4aa4b 1215 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1216 error (_("The 'while-stepping' command "
1217 "cannot be used for static tracepoint"));
a7bdde9e
VP
1218
1219 if (while_stepping)
3e43a32a
MS
1220 error (_("The 'while-stepping' command "
1221 "can be used only once"));
a7bdde9e
VP
1222 else
1223 while_stepping = c;
1224 }
c9a6ce02
PA
1225
1226 validate_actionline (c->line, b);
a7bdde9e
VP
1227 }
1228 if (while_stepping)
1229 {
1230 struct command_line *c2;
1231
1232 gdb_assert (while_stepping->body_count == 1);
1233 c2 = while_stepping->body_list[0];
1234 for (; c2; c2 = c2->next)
1235 {
a7bdde9e
VP
1236 if (c2->control_type == while_stepping_control)
1237 error (_("The 'while-stepping' command cannot be nested"));
1238 }
1239 }
1240 }
1241 else
1242 {
1243 check_no_tracepoint_commands (commands);
1244 }
95a42b64
TT
1245}
1246
0fb4aa4b
PA
1247/* Return a vector of all the static tracepoints set at ADDR. The
1248 caller is responsible for releasing the vector. */
1249
1250VEC(breakpoint_p) *
1251static_tracepoints_here (CORE_ADDR addr)
1252{
1253 struct breakpoint *b;
1254 VEC(breakpoint_p) *found = 0;
1255 struct bp_location *loc;
1256
1257 ALL_BREAKPOINTS (b)
1258 if (b->type == bp_static_tracepoint)
1259 {
1260 for (loc = b->loc; loc; loc = loc->next)
1261 if (loc->address == addr)
1262 VEC_safe_push(breakpoint_p, found, b);
1263 }
1264
1265 return found;
1266}
1267
95a42b64 1268/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1269 validate that only allowed commands are included. */
95a42b64
TT
1270
1271void
4a64f543
MS
1272breakpoint_set_commands (struct breakpoint *b,
1273 struct command_line *commands)
95a42b64
TT
1274{
1275 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1276
9add0f1b
TT
1277 decref_counted_command_line (&b->commands);
1278 b->commands = alloc_counted_command_line (commands);
8d3788bd 1279 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1280}
1281
45a43567
TT
1282/* Set the internal `silent' flag on the breakpoint. Note that this
1283 is not the same as the "silent" that may appear in the breakpoint's
1284 commands. */
1285
1286void
1287breakpoint_set_silent (struct breakpoint *b, int silent)
1288{
1289 int old_silent = b->silent;
1290
1291 b->silent = silent;
1292 if (old_silent != silent)
8d3788bd 1293 observer_notify_breakpoint_modified (b);
45a43567
TT
1294}
1295
1296/* Set the thread for this breakpoint. If THREAD is -1, make the
1297 breakpoint work for any thread. */
1298
1299void
1300breakpoint_set_thread (struct breakpoint *b, int thread)
1301{
1302 int old_thread = b->thread;
1303
1304 b->thread = thread;
1305 if (old_thread != thread)
8d3788bd 1306 observer_notify_breakpoint_modified (b);
45a43567
TT
1307}
1308
1309/* Set the task for this breakpoint. If TASK is 0, make the
1310 breakpoint work for any task. */
1311
1312void
1313breakpoint_set_task (struct breakpoint *b, int task)
1314{
1315 int old_task = b->task;
1316
1317 b->task = task;
1318 if (old_task != task)
8d3788bd 1319 observer_notify_breakpoint_modified (b);
45a43567
TT
1320}
1321
95a42b64
TT
1322void
1323check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1324{
1325 struct breakpoint *b = closure;
cc59ec59 1326
6f937416 1327 validate_actionline (line, b);
a7bdde9e
VP
1328}
1329
95a42b64
TT
1330/* A structure used to pass information through
1331 map_breakpoint_numbers. */
1332
1333struct commands_info
1334{
1335 /* True if the command was typed at a tty. */
1336 int from_tty;
86b17b60
PA
1337
1338 /* The breakpoint range spec. */
1339 char *arg;
1340
95a42b64
TT
1341 /* Non-NULL if the body of the commands are being read from this
1342 already-parsed command. */
1343 struct command_line *control;
86b17b60 1344
95a42b64
TT
1345 /* The command lines read from the user, or NULL if they have not
1346 yet been read. */
1347 struct counted_command_line *cmd;
1348};
1349
1350/* A callback for map_breakpoint_numbers that sets the commands for
1351 commands_command. */
1352
c906108c 1353static void
95a42b64 1354do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1355{
95a42b64 1356 struct commands_info *info = data;
c906108c 1357
95a42b64
TT
1358 if (info->cmd == NULL)
1359 {
1360 struct command_line *l;
5c44784c 1361
95a42b64
TT
1362 if (info->control != NULL)
1363 l = copy_command_lines (info->control->body_list[0]);
1364 else
86b17b60
PA
1365 {
1366 struct cleanup *old_chain;
1367 char *str;
c5aa993b 1368
3e43a32a
MS
1369 str = xstrprintf (_("Type commands for breakpoint(s) "
1370 "%s, one per line."),
86b17b60
PA
1371 info->arg);
1372
1373 old_chain = make_cleanup (xfree, str);
1374
1375 l = read_command_lines (str,
1376 info->from_tty, 1,
d77f58be 1377 (is_tracepoint (b)
86b17b60
PA
1378 ? check_tracepoint_command : 0),
1379 b);
1380
1381 do_cleanups (old_chain);
1382 }
a7bdde9e 1383
95a42b64
TT
1384 info->cmd = alloc_counted_command_line (l);
1385 }
1386
1387 /* If a breakpoint was on the list more than once, we don't need to
1388 do anything. */
1389 if (b->commands != info->cmd)
1390 {
1391 validate_commands_for_breakpoint (b, info->cmd->commands);
1392 incref_counted_command_line (info->cmd);
1393 decref_counted_command_line (&b->commands);
1394 b->commands = info->cmd;
8d3788bd 1395 observer_notify_breakpoint_modified (b);
c5aa993b 1396 }
95a42b64
TT
1397}
1398
1399static void
4a64f543
MS
1400commands_command_1 (char *arg, int from_tty,
1401 struct command_line *control)
95a42b64
TT
1402{
1403 struct cleanup *cleanups;
1404 struct commands_info info;
1405
1406 info.from_tty = from_tty;
1407 info.control = control;
1408 info.cmd = NULL;
1409 /* If we read command lines from the user, then `info' will hold an
1410 extra reference to the commands that we must clean up. */
1411 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1412
1413 if (arg == NULL || !*arg)
1414 {
86b17b60 1415 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1416 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1417 breakpoint_count);
95a42b64
TT
1418 else if (breakpoint_count > 0)
1419 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1420 else
1421 {
1422 /* So that we don't try to free the incoming non-NULL
1423 argument in the cleanup below. Mapping breakpoint
1424 numbers will fail in this case. */
1425 arg = NULL;
1426 }
95a42b64 1427 }
9766ced4
SS
1428 else
1429 /* The command loop has some static state, so we need to preserve
1430 our argument. */
1431 arg = xstrdup (arg);
86b17b60
PA
1432
1433 if (arg != NULL)
1434 make_cleanup (xfree, arg);
1435
1436 info.arg = arg;
95a42b64
TT
1437
1438 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1439
1440 if (info.cmd == NULL)
1441 error (_("No breakpoints specified."));
1442
1443 do_cleanups (cleanups);
1444}
1445
1446static void
1447commands_command (char *arg, int from_tty)
1448{
1449 commands_command_1 (arg, from_tty, NULL);
c906108c 1450}
40c03ae8
EZ
1451
1452/* Like commands_command, but instead of reading the commands from
1453 input stream, takes them from an already parsed command structure.
1454
1455 This is used by cli-script.c to DTRT with breakpoint commands
1456 that are part of if and while bodies. */
1457enum command_control_type
1458commands_from_control_command (char *arg, struct command_line *cmd)
1459{
95a42b64
TT
1460 commands_command_1 (arg, 0, cmd);
1461 return simple_control;
40c03ae8 1462}
876fa593
JK
1463
1464/* Return non-zero if BL->TARGET_INFO contains valid information. */
1465
1466static int
1467bp_location_has_shadow (struct bp_location *bl)
1468{
1469 if (bl->loc_type != bp_loc_software_breakpoint)
1470 return 0;
1471 if (!bl->inserted)
1472 return 0;
1473 if (bl->target_info.shadow_len == 0)
e5dd4106 1474 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1475 return 0;
1476 return 1;
1477}
1478
9d497a19
PA
1479/* Update BUF, which is LEN bytes read from the target address
1480 MEMADDR, by replacing a memory breakpoint with its shadowed
1481 contents.
1482
1483 If READBUF is not NULL, this buffer must not overlap with the of
1484 the breakpoint location's shadow_contents buffer. Otherwise, a
1485 failed assertion internal error will be raised. */
1486
1487static void
1488one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1489 const gdb_byte *writebuf_org,
1490 ULONGEST memaddr, LONGEST len,
1491 struct bp_target_info *target_info,
1492 struct gdbarch *gdbarch)
1493{
1494 /* Now do full processing of the found relevant range of elements. */
1495 CORE_ADDR bp_addr = 0;
1496 int bp_size = 0;
1497 int bptoffset = 0;
1498
1499 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1500 current_program_space->aspace, 0))
1501 {
1502 /* The breakpoint is inserted in a different address space. */
1503 return;
1504 }
1505
1506 /* Addresses and length of the part of the breakpoint that
1507 we need to copy. */
1508 bp_addr = target_info->placed_address;
1509 bp_size = target_info->shadow_len;
1510
1511 if (bp_addr + bp_size <= memaddr)
1512 {
1513 /* The breakpoint is entirely before the chunk of memory we are
1514 reading. */
1515 return;
1516 }
1517
1518 if (bp_addr >= memaddr + len)
1519 {
1520 /* The breakpoint is entirely after the chunk of memory we are
1521 reading. */
1522 return;
1523 }
1524
1525 /* Offset within shadow_contents. */
1526 if (bp_addr < memaddr)
1527 {
1528 /* Only copy the second part of the breakpoint. */
1529 bp_size -= memaddr - bp_addr;
1530 bptoffset = memaddr - bp_addr;
1531 bp_addr = memaddr;
1532 }
1533
1534 if (bp_addr + bp_size > memaddr + len)
1535 {
1536 /* Only copy the first part of the breakpoint. */
1537 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1538 }
1539
1540 if (readbuf != NULL)
1541 {
1542 /* Verify that the readbuf buffer does not overlap with the
1543 shadow_contents buffer. */
1544 gdb_assert (target_info->shadow_contents >= readbuf + len
1545 || readbuf >= (target_info->shadow_contents
1546 + target_info->shadow_len));
1547
1548 /* Update the read buffer with this inserted breakpoint's
1549 shadow. */
1550 memcpy (readbuf + bp_addr - memaddr,
1551 target_info->shadow_contents + bptoffset, bp_size);
1552 }
1553 else
1554 {
1555 const unsigned char *bp;
0d5ed153
MR
1556 CORE_ADDR addr = target_info->reqstd_address;
1557 int placed_size;
9d497a19
PA
1558
1559 /* Update the shadow with what we want to write to memory. */
1560 memcpy (target_info->shadow_contents + bptoffset,
1561 writebuf_org + bp_addr - memaddr, bp_size);
1562
1563 /* Determine appropriate breakpoint contents and size for this
1564 address. */
0d5ed153 1565 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1566
1567 /* Update the final write buffer with this inserted
1568 breakpoint's INSN. */
1569 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1570 }
1571}
1572
8defab1a 1573/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1574 by replacing any memory breakpoints with their shadowed contents.
1575
35c63cd8
JB
1576 If READBUF is not NULL, this buffer must not overlap with any of
1577 the breakpoint location's shadow_contents buffers. Otherwise,
1578 a failed assertion internal error will be raised.
1579
876fa593 1580 The range of shadowed area by each bp_location is:
35df4500
TJB
1581 bl->address - bp_location_placed_address_before_address_max
1582 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1583 The range we were requested to resolve shadows for is:
1584 memaddr ... memaddr + len
1585 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1586 memaddr + len <= (bl->address
1587 - bp_location_placed_address_before_address_max)
876fa593 1588 and:
35df4500 1589 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1590
8defab1a 1591void
f0ba3972
PA
1592breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1593 const gdb_byte *writebuf_org,
1594 ULONGEST memaddr, LONGEST len)
c906108c 1595{
4a64f543
MS
1596 /* Left boundary, right boundary and median element of our binary
1597 search. */
876fa593 1598 unsigned bc_l, bc_r, bc;
9d497a19 1599 size_t i;
876fa593 1600
4a64f543
MS
1601 /* Find BC_L which is a leftmost element which may affect BUF
1602 content. It is safe to report lower value but a failure to
1603 report higher one. */
876fa593
JK
1604
1605 bc_l = 0;
1606 bc_r = bp_location_count;
1607 while (bc_l + 1 < bc_r)
1608 {
35df4500 1609 struct bp_location *bl;
876fa593
JK
1610
1611 bc = (bc_l + bc_r) / 2;
35df4500 1612 bl = bp_location[bc];
876fa593 1613
4a64f543
MS
1614 /* Check first BL->ADDRESS will not overflow due to the added
1615 constant. Then advance the left boundary only if we are sure
1616 the BC element can in no way affect the BUF content (MEMADDR
1617 to MEMADDR + LEN range).
876fa593 1618
4a64f543
MS
1619 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1620 offset so that we cannot miss a breakpoint with its shadow
1621 range tail still reaching MEMADDR. */
c5aa993b 1622
35df4500
TJB
1623 if ((bl->address + bp_location_shadow_len_after_address_max
1624 >= bl->address)
1625 && (bl->address + bp_location_shadow_len_after_address_max
1626 <= memaddr))
876fa593
JK
1627 bc_l = bc;
1628 else
1629 bc_r = bc;
1630 }
1631
128070bb
PA
1632 /* Due to the binary search above, we need to make sure we pick the
1633 first location that's at BC_L's address. E.g., if there are
1634 multiple locations at the same address, BC_L may end up pointing
1635 at a duplicate location, and miss the "master"/"inserted"
1636 location. Say, given locations L1, L2 and L3 at addresses A and
1637 B:
1638
1639 L1@A, L2@A, L3@B, ...
1640
1641 BC_L could end up pointing at location L2, while the "master"
1642 location could be L1. Since the `loc->inserted' flag is only set
1643 on "master" locations, we'd forget to restore the shadow of L1
1644 and L2. */
1645 while (bc_l > 0
1646 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1647 bc_l--;
1648
876fa593
JK
1649 /* Now do full processing of the found relevant range of elements. */
1650
1651 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1652 {
35df4500 1653 struct bp_location *bl = bp_location[bc];
876fa593
JK
1654 CORE_ADDR bp_addr = 0;
1655 int bp_size = 0;
1656 int bptoffset = 0;
1657
35df4500
TJB
1658 /* bp_location array has BL->OWNER always non-NULL. */
1659 if (bl->owner->type == bp_none)
8a3fe4f8 1660 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1661 bl->owner->number);
ffce0d52 1662
e5dd4106 1663 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1664 content. */
1665
35df4500
TJB
1666 if (bl->address >= bp_location_placed_address_before_address_max
1667 && memaddr + len <= (bl->address
1668 - bp_location_placed_address_before_address_max))
876fa593
JK
1669 break;
1670
35df4500 1671 if (!bp_location_has_shadow (bl))
c5aa993b 1672 continue;
6c95b8df 1673
9d497a19
PA
1674 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1675 memaddr, len, &bl->target_info, bl->gdbarch);
1676 }
c906108c 1677}
9d497a19 1678
c906108c 1679\f
c5aa993b 1680
b775012e
LM
1681/* Return true if BPT is either a software breakpoint or a hardware
1682 breakpoint. */
1683
1684int
1685is_breakpoint (const struct breakpoint *bpt)
1686{
1687 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1688 || bpt->type == bp_hardware_breakpoint
1689 || bpt->type == bp_dprintf);
b775012e
LM
1690}
1691
60e1c644
PA
1692/* Return true if BPT is of any hardware watchpoint kind. */
1693
a5606eee 1694static int
d77f58be 1695is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1696{
1697 return (bpt->type == bp_hardware_watchpoint
1698 || bpt->type == bp_read_watchpoint
1699 || bpt->type == bp_access_watchpoint);
1700}
7270d8f2 1701
60e1c644
PA
1702/* Return true if BPT is of any watchpoint kind, hardware or
1703 software. */
1704
3a5c3e22 1705int
d77f58be 1706is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1707{
1708 return (is_hardware_watchpoint (bpt)
1709 || bpt->type == bp_watchpoint);
1710}
1711
3a5c3e22
PA
1712/* Returns true if the current thread and its running state are safe
1713 to evaluate or update watchpoint B. Watchpoints on local
1714 expressions need to be evaluated in the context of the thread that
1715 was current when the watchpoint was created, and, that thread needs
1716 to be stopped to be able to select the correct frame context.
1717 Watchpoints on global expressions can be evaluated on any thread,
1718 and in any state. It is presently left to the target allowing
1719 memory accesses when threads are running. */
f6bc2008
PA
1720
1721static int
3a5c3e22 1722watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1723{
d0d8b0c6
JK
1724 return (b->base.pspace == current_program_space
1725 && (ptid_equal (b->watchpoint_thread, null_ptid)
1726 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1727 && !is_executing (inferior_ptid))));
f6bc2008
PA
1728}
1729
d0fb5eae
JK
1730/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1731 associated bp_watchpoint_scope breakpoint. */
1732
1733static void
3a5c3e22 1734watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1735{
3a5c3e22 1736 struct breakpoint *b = &w->base;
d0fb5eae
JK
1737
1738 if (b->related_breakpoint != b)
1739 {
1740 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1741 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1742 b->related_breakpoint->disposition = disp_del_at_next_stop;
1743 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1744 b->related_breakpoint = b;
1745 }
1746 b->disposition = disp_del_at_next_stop;
1747}
1748
bb9d5f81
PP
1749/* Extract a bitfield value from value VAL using the bit parameters contained in
1750 watchpoint W. */
1751
1752static struct value *
1753extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1754{
1755 struct value *bit_val;
1756
1757 if (val == NULL)
1758 return NULL;
1759
1760 bit_val = allocate_value (value_type (val));
1761
1762 unpack_value_bitfield (bit_val,
1763 w->val_bitpos,
1764 w->val_bitsize,
1765 value_contents_for_printing (val),
1766 value_offset (val),
1767 val);
1768
1769 return bit_val;
1770}
1771
567e1b4e
JB
1772/* Assuming that B is a watchpoint:
1773 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1774 - Evaluate expression and store the result in B->val
567e1b4e
JB
1775 - Evaluate the condition if there is one, and store the result
1776 in b->loc->cond.
a5606eee
VP
1777 - Update the list of values that must be watched in B->loc.
1778
4a64f543
MS
1779 If the watchpoint disposition is disp_del_at_next_stop, then do
1780 nothing. If this is local watchpoint that is out of scope, delete
1781 it.
1782
1783 Even with `set breakpoint always-inserted on' the watchpoints are
1784 removed + inserted on each stop here. Normal breakpoints must
1785 never be removed because they might be missed by a running thread
1786 when debugging in non-stop mode. On the other hand, hardware
1787 watchpoints (is_hardware_watchpoint; processed here) are specific
1788 to each LWP since they are stored in each LWP's hardware debug
1789 registers. Therefore, such LWP must be stopped first in order to
1790 be able to modify its hardware watchpoints.
1791
1792 Hardware watchpoints must be reset exactly once after being
1793 presented to the user. It cannot be done sooner, because it would
1794 reset the data used to present the watchpoint hit to the user. And
1795 it must not be done later because it could display the same single
1796 watchpoint hit during multiple GDB stops. Note that the latter is
1797 relevant only to the hardware watchpoint types bp_read_watchpoint
1798 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1799 not user-visible - its hit is suppressed if the memory content has
1800 not changed.
1801
1802 The following constraints influence the location where we can reset
1803 hardware watchpoints:
1804
1805 * target_stopped_by_watchpoint and target_stopped_data_address are
1806 called several times when GDB stops.
1807
1808 [linux]
1809 * Multiple hardware watchpoints can be hit at the same time,
1810 causing GDB to stop. GDB only presents one hardware watchpoint
1811 hit at a time as the reason for stopping, and all the other hits
1812 are presented later, one after the other, each time the user
1813 requests the execution to be resumed. Execution is not resumed
1814 for the threads still having pending hit event stored in
1815 LWP_INFO->STATUS. While the watchpoint is already removed from
1816 the inferior on the first stop the thread hit event is kept being
1817 reported from its cached value by linux_nat_stopped_data_address
1818 until the real thread resume happens after the watchpoint gets
1819 presented and thus its LWP_INFO->STATUS gets reset.
1820
1821 Therefore the hardware watchpoint hit can get safely reset on the
1822 watchpoint removal from inferior. */
a79d3c27 1823
b40ce68a 1824static void
3a5c3e22 1825update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1826{
a5606eee 1827 int within_current_scope;
a5606eee 1828 struct frame_id saved_frame_id;
66076460 1829 int frame_saved;
a5606eee 1830
f6bc2008
PA
1831 /* If this is a local watchpoint, we only want to check if the
1832 watchpoint frame is in scope if the current thread is the thread
1833 that was used to create the watchpoint. */
1834 if (!watchpoint_in_thread_scope (b))
1835 return;
1836
3a5c3e22 1837 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1838 return;
1839
66076460 1840 frame_saved = 0;
a5606eee
VP
1841
1842 /* Determine if the watchpoint is within scope. */
1843 if (b->exp_valid_block == NULL)
1844 within_current_scope = 1;
1845 else
1846 {
b5db5dfc
UW
1847 struct frame_info *fi = get_current_frame ();
1848 struct gdbarch *frame_arch = get_frame_arch (fi);
1849 CORE_ADDR frame_pc = get_frame_pc (fi);
1850
c9cf6e20
MG
1851 /* If we're at a point where the stack has been destroyed
1852 (e.g. in a function epilogue), unwinding may not work
1853 properly. Do not attempt to recreate locations at this
b5db5dfc 1854 point. See similar comments in watchpoint_check. */
c9cf6e20 1855 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1856 return;
66076460
DJ
1857
1858 /* Save the current frame's ID so we can restore it after
1859 evaluating the watchpoint expression on its own frame. */
1860 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1861 took a frame parameter, so that we didn't have to change the
1862 selected frame. */
1863 frame_saved = 1;
1864 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1865
a5606eee
VP
1866 fi = frame_find_by_id (b->watchpoint_frame);
1867 within_current_scope = (fi != NULL);
1868 if (within_current_scope)
1869 select_frame (fi);
1870 }
1871
b5db5dfc
UW
1872 /* We don't free locations. They are stored in the bp_location array
1873 and update_global_location_list will eventually delete them and
1874 remove breakpoints if needed. */
3a5c3e22 1875 b->base.loc = NULL;
b5db5dfc 1876
a5606eee
VP
1877 if (within_current_scope && reparse)
1878 {
bbc13ae3 1879 const char *s;
d63d0675 1880
a5606eee
VP
1881 if (b->exp)
1882 {
1883 xfree (b->exp);
1884 b->exp = NULL;
1885 }
d63d0675 1886 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1887 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1888 /* If the meaning of expression itself changed, the old value is
1889 no longer relevant. We don't want to report a watchpoint hit
1890 to the user when the old value and the new value may actually
1891 be completely different objects. */
1892 value_free (b->val);
fa4727a6
DJ
1893 b->val = NULL;
1894 b->val_valid = 0;
60e1c644
PA
1895
1896 /* Note that unlike with breakpoints, the watchpoint's condition
1897 expression is stored in the breakpoint object, not in the
1898 locations (re)created below. */
3a5c3e22 1899 if (b->base.cond_string != NULL)
60e1c644
PA
1900 {
1901 if (b->cond_exp != NULL)
1902 {
1903 xfree (b->cond_exp);
1904 b->cond_exp = NULL;
1905 }
1906
3a5c3e22 1907 s = b->base.cond_string;
1bb9788d 1908 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1909 }
a5606eee 1910 }
a5606eee
VP
1911
1912 /* If we failed to parse the expression, for example because
1913 it refers to a global variable in a not-yet-loaded shared library,
1914 don't try to insert watchpoint. We don't automatically delete
1915 such watchpoint, though, since failure to parse expression
1916 is different from out-of-scope watchpoint. */
e8369a73 1917 if (!target_has_execution)
2d134ed3
PA
1918 {
1919 /* Without execution, memory can't change. No use to try and
1920 set watchpoint locations. The watchpoint will be reset when
1921 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1922 if (!can_use_hw_watchpoints)
1923 {
1924 if (b->base.ops->works_in_software_mode (&b->base))
1925 b->base.type = bp_watchpoint;
1926 else
638aa5a1
AB
1927 error (_("Can't set read/access watchpoint when "
1928 "hardware watchpoints are disabled."));
e8369a73 1929 }
2d134ed3
PA
1930 }
1931 else if (within_current_scope && b->exp)
a5606eee 1932 {
0cf6dd15 1933 int pc = 0;
fa4727a6 1934 struct value *val_chain, *v, *result, *next;
2d134ed3 1935 struct program_space *frame_pspace;
a5606eee 1936
3a1115a0 1937 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1938
a5606eee
VP
1939 /* Avoid setting b->val if it's already set. The meaning of
1940 b->val is 'the last value' user saw, and we should update
1941 it only if we reported that last value to user. As it
9c06b0b4
TJB
1942 happens, the code that reports it updates b->val directly.
1943 We don't keep track of the memory value for masked
1944 watchpoints. */
3a5c3e22 1945 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1946 {
bb9d5f81
PP
1947 if (b->val_bitsize != 0)
1948 {
1949 v = extract_bitfield_from_watchpoint_value (b, v);
1950 if (v != NULL)
1951 release_value (v);
1952 }
fa4727a6
DJ
1953 b->val = v;
1954 b->val_valid = 1;
1955 }
a5606eee 1956
2d134ed3
PA
1957 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1958
a5606eee 1959 /* Look at each value on the value chain. */
9fa40276 1960 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1961 {
1962 /* If it's a memory location, and GDB actually needed
1963 its contents to evaluate the expression, then we
fa4727a6
DJ
1964 must watch it. If the first value returned is
1965 still lazy, that means an error occurred reading it;
1966 watch it anyway in case it becomes readable. */
a5606eee 1967 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1968 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1969 {
1970 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1971
a5606eee
VP
1972 /* We only watch structs and arrays if user asked
1973 for it explicitly, never if they just happen to
1974 appear in the middle of some value chain. */
fa4727a6 1975 if (v == result
a5606eee
VP
1976 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1977 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1978 {
1979 CORE_ADDR addr;
f486487f 1980 enum target_hw_bp_type type;
a5606eee 1981 struct bp_location *loc, **tmp;
bb9d5f81
PP
1982 int bitpos = 0, bitsize = 0;
1983
1984 if (value_bitsize (v) != 0)
1985 {
1986 /* Extract the bit parameters out from the bitfield
1987 sub-expression. */
1988 bitpos = value_bitpos (v);
1989 bitsize = value_bitsize (v);
1990 }
1991 else if (v == result && b->val_bitsize != 0)
1992 {
1993 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1994 lvalue whose bit parameters are saved in the fields
1995 VAL_BITPOS and VAL_BITSIZE. */
1996 bitpos = b->val_bitpos;
1997 bitsize = b->val_bitsize;
1998 }
a5606eee 1999
42ae5230 2000 addr = value_address (v);
bb9d5f81
PP
2001 if (bitsize != 0)
2002 {
2003 /* Skip the bytes that don't contain the bitfield. */
2004 addr += bitpos / 8;
2005 }
2006
a5606eee 2007 type = hw_write;
3a5c3e22 2008 if (b->base.type == bp_read_watchpoint)
a5606eee 2009 type = hw_read;
3a5c3e22 2010 else if (b->base.type == bp_access_watchpoint)
a5606eee 2011 type = hw_access;
3a5c3e22
PA
2012
2013 loc = allocate_bp_location (&b->base);
2014 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2015 ;
2016 *tmp = loc;
a6d9a66e 2017 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2018
2019 loc->pspace = frame_pspace;
a5606eee 2020 loc->address = addr;
bb9d5f81
PP
2021
2022 if (bitsize != 0)
2023 {
2024 /* Just cover the bytes that make up the bitfield. */
2025 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2026 }
2027 else
2028 loc->length = TYPE_LENGTH (value_type (v));
2029
a5606eee
VP
2030 loc->watchpoint_type = type;
2031 }
2032 }
9fa40276
TJB
2033 }
2034
2035 /* Change the type of breakpoint between hardware assisted or
2036 an ordinary watchpoint depending on the hardware support
2037 and free hardware slots. REPARSE is set when the inferior
2038 is started. */
a9634178 2039 if (reparse)
9fa40276 2040 {
e09342b5 2041 int reg_cnt;
9fa40276
TJB
2042 enum bp_loc_type loc_type;
2043 struct bp_location *bl;
a5606eee 2044
a9634178 2045 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2046
2047 if (reg_cnt)
9fa40276
TJB
2048 {
2049 int i, target_resources_ok, other_type_used;
a1398e0c 2050 enum bptype type;
9fa40276 2051
a9634178
TJB
2052 /* Use an exact watchpoint when there's only one memory region to be
2053 watched, and only one debug register is needed to watch it. */
2054 b->exact = target_exact_watchpoints && reg_cnt == 1;
2055
9fa40276 2056 /* We need to determine how many resources are already
e09342b5
TJB
2057 used for all other hardware watchpoints plus this one
2058 to see if we still have enough resources to also fit
a1398e0c
PA
2059 this watchpoint in as well. */
2060
2061 /* If this is a software watchpoint, we try to turn it
2062 to a hardware one -- count resources as if B was of
2063 hardware watchpoint type. */
2064 type = b->base.type;
2065 if (type == bp_watchpoint)
2066 type = bp_hardware_watchpoint;
2067
2068 /* This watchpoint may or may not have been placed on
2069 the list yet at this point (it won't be in the list
2070 if we're trying to create it for the first time,
2071 through watch_command), so always account for it
2072 manually. */
2073
2074 /* Count resources used by all watchpoints except B. */
2075 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2076
2077 /* Add in the resources needed for B. */
2078 i += hw_watchpoint_use_count (&b->base);
2079
2080 target_resources_ok
2081 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2082 if (target_resources_ok <= 0)
a9634178 2083 {
3a5c3e22 2084 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2085
2086 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2087 error (_("Target does not support this type of "
2088 "hardware watchpoint."));
9c06b0b4
TJB
2089 else if (target_resources_ok < 0 && !sw_mode)
2090 error (_("There are not enough available hardware "
2091 "resources for this watchpoint."));
a1398e0c
PA
2092
2093 /* Downgrade to software watchpoint. */
2094 b->base.type = bp_watchpoint;
2095 }
2096 else
2097 {
2098 /* If this was a software watchpoint, we've just
2099 found we have enough resources to turn it to a
2100 hardware watchpoint. Otherwise, this is a
2101 nop. */
2102 b->base.type = type;
a9634178 2103 }
9fa40276 2104 }
3a5c3e22 2105 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2106 {
2107 if (!can_use_hw_watchpoints)
2108 error (_("Can't set read/access watchpoint when "
2109 "hardware watchpoints are disabled."));
2110 else
2111 error (_("Expression cannot be implemented with "
2112 "read/access watchpoint."));
2113 }
9fa40276 2114 else
3a5c3e22 2115 b->base.type = bp_watchpoint;
9fa40276 2116
3a5c3e22 2117 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2118 : bp_loc_hardware_watchpoint);
3a5c3e22 2119 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2120 bl->loc_type = loc_type;
2121 }
2122
2123 for (v = val_chain; v; v = next)
2124 {
a5606eee
VP
2125 next = value_next (v);
2126 if (v != b->val)
2127 value_free (v);
2128 }
2129
c7437ca6
PA
2130 /* If a software watchpoint is not watching any memory, then the
2131 above left it without any location set up. But,
2132 bpstat_stop_status requires a location to be able to report
2133 stops, so make sure there's at least a dummy one. */
3a5c3e22 2134 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2135 {
3a5c3e22
PA
2136 struct breakpoint *base = &b->base;
2137 base->loc = allocate_bp_location (base);
2138 base->loc->pspace = frame_pspace;
2139 base->loc->address = -1;
2140 base->loc->length = -1;
2141 base->loc->watchpoint_type = -1;
c7437ca6 2142 }
a5606eee
VP
2143 }
2144 else if (!within_current_scope)
7270d8f2 2145 {
ac74f770
MS
2146 printf_filtered (_("\
2147Watchpoint %d deleted because the program has left the block\n\
2148in which its expression is valid.\n"),
3a5c3e22 2149 b->base.number);
d0fb5eae 2150 watchpoint_del_at_next_stop (b);
7270d8f2 2151 }
a5606eee
VP
2152
2153 /* Restore the selected frame. */
66076460
DJ
2154 if (frame_saved)
2155 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2156}
2157
a5606eee 2158
74960c60 2159/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2160 inserted in the inferior. We don't differentiate the type of BL's owner
2161 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2162 breakpoint_ops is not defined, because in insert_bp_location,
2163 tracepoint's insert_location will not be called. */
74960c60 2164static int
35df4500 2165should_be_inserted (struct bp_location *bl)
74960c60 2166{
35df4500 2167 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2168 return 0;
2169
35df4500 2170 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2171 return 0;
2172
35df4500 2173 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2174 return 0;
2175
f8eba3c6
TT
2176 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2177 return 0;
2178
56710373
PA
2179 /* This is set for example, when we're attached to the parent of a
2180 vfork, and have detached from the child. The child is running
2181 free, and we expect it to do an exec or exit, at which point the
2182 OS makes the parent schedulable again (and the target reports
2183 that the vfork is done). Until the child is done with the shared
2184 memory region, do not insert breakpoints in the parent, otherwise
2185 the child could still trip on the parent's breakpoints. Since
2186 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2187 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2188 return 0;
2189
31e77af2
PA
2190 /* Don't insert a breakpoint if we're trying to step past its
2191 location. */
2192 if ((bl->loc_type == bp_loc_software_breakpoint
2193 || bl->loc_type == bp_loc_hardware_breakpoint)
2194 && stepping_past_instruction_at (bl->pspace->aspace,
2195 bl->address))
e558d7c1
PA
2196 {
2197 if (debug_infrun)
2198 {
2199 fprintf_unfiltered (gdb_stdlog,
2200 "infrun: skipping breakpoint: "
2201 "stepping past insn at: %s\n",
2202 paddress (bl->gdbarch, bl->address));
2203 }
2204 return 0;
2205 }
31e77af2 2206
963f9c80
PA
2207 /* Don't insert watchpoints if we're trying to step past the
2208 instruction that triggered one. */
2209 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2210 && stepping_past_nonsteppable_watchpoint ())
2211 {
2212 if (debug_infrun)
2213 {
2214 fprintf_unfiltered (gdb_stdlog,
2215 "infrun: stepping past non-steppable watchpoint. "
2216 "skipping watchpoint at %s:%d\n",
2217 paddress (bl->gdbarch, bl->address),
2218 bl->length);
2219 }
2220 return 0;
2221 }
2222
74960c60
VP
2223 return 1;
2224}
2225
934709f0
PW
2226/* Same as should_be_inserted but does the check assuming
2227 that the location is not duplicated. */
2228
2229static int
2230unduplicated_should_be_inserted (struct bp_location *bl)
2231{
2232 int result;
2233 const int save_duplicate = bl->duplicate;
2234
2235 bl->duplicate = 0;
2236 result = should_be_inserted (bl);
2237 bl->duplicate = save_duplicate;
2238 return result;
2239}
2240
b775012e
LM
2241/* Parses a conditional described by an expression COND into an
2242 agent expression bytecode suitable for evaluation
2243 by the bytecode interpreter. Return NULL if there was
2244 any error during parsing. */
2245
2246static struct agent_expr *
2247parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2248{
2249 struct agent_expr *aexpr = NULL;
b775012e
LM
2250
2251 if (!cond)
2252 return NULL;
2253
2254 /* We don't want to stop processing, so catch any errors
2255 that may show up. */
492d29ea 2256 TRY
b775012e
LM
2257 {
2258 aexpr = gen_eval_for_expr (scope, cond);
2259 }
2260
492d29ea 2261 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2262 {
2263 /* If we got here, it means the condition could not be parsed to a valid
2264 bytecode expression and thus can't be evaluated on the target's side.
2265 It's no use iterating through the conditions. */
2266 return NULL;
2267 }
492d29ea 2268 END_CATCH
b775012e
LM
2269
2270 /* We have a valid agent expression. */
2271 return aexpr;
2272}
2273
2274/* Based on location BL, create a list of breakpoint conditions to be
2275 passed on to the target. If we have duplicated locations with different
2276 conditions, we will add such conditions to the list. The idea is that the
2277 target will evaluate the list of conditions and will only notify GDB when
2278 one of them is true. */
2279
2280static void
2281build_target_condition_list (struct bp_location *bl)
2282{
2283 struct bp_location **locp = NULL, **loc2p;
2284 int null_condition_or_parse_error = 0;
2285 int modified = bl->needs_update;
2286 struct bp_location *loc;
2287
8b4f3082
PA
2288 /* Release conditions left over from a previous insert. */
2289 VEC_free (agent_expr_p, bl->target_info.conditions);
2290
b775012e
LM
2291 /* This is only meaningful if the target is
2292 evaluating conditions and if the user has
2293 opted for condition evaluation on the target's
2294 side. */
2295 if (gdb_evaluates_breakpoint_condition_p ()
2296 || !target_supports_evaluation_of_breakpoint_conditions ())
2297 return;
2298
2299 /* Do a first pass to check for locations with no assigned
2300 conditions or conditions that fail to parse to a valid agent expression
2301 bytecode. If any of these happen, then it's no use to send conditions
2302 to the target since this location will always trigger and generate a
2303 response back to GDB. */
2304 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2305 {
2306 loc = (*loc2p);
2307 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2308 {
2309 if (modified)
2310 {
2311 struct agent_expr *aexpr;
2312
2313 /* Re-parse the conditions since something changed. In that
2314 case we already freed the condition bytecodes (see
2315 force_breakpoint_reinsertion). We just
2316 need to parse the condition to bytecodes again. */
2317 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2318 loc->cond_bytecode = aexpr;
2319
2320 /* Check if we managed to parse the conditional expression
2321 correctly. If not, we will not send this condition
2322 to the target. */
2323 if (aexpr)
2324 continue;
2325 }
2326
2327 /* If we have a NULL bytecode expression, it means something
2328 went wrong or we have a null condition expression. */
2329 if (!loc->cond_bytecode)
2330 {
2331 null_condition_or_parse_error = 1;
2332 break;
2333 }
2334 }
2335 }
2336
2337 /* If any of these happened, it means we will have to evaluate the conditions
2338 for the location's address on gdb's side. It is no use keeping bytecodes
2339 for all the other duplicate locations, thus we free all of them here.
2340
2341 This is so we have a finer control over which locations' conditions are
2342 being evaluated by GDB or the remote stub. */
2343 if (null_condition_or_parse_error)
2344 {
2345 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2346 {
2347 loc = (*loc2p);
2348 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2349 {
2350 /* Only go as far as the first NULL bytecode is
2351 located. */
2352 if (!loc->cond_bytecode)
2353 return;
2354
2355 free_agent_expr (loc->cond_bytecode);
2356 loc->cond_bytecode = NULL;
2357 }
2358 }
2359 }
2360
2361 /* No NULL conditions or failed bytecode generation. Build a condition list
2362 for this location's address. */
2363 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2364 {
2365 loc = (*loc2p);
2366 if (loc->cond
2367 && is_breakpoint (loc->owner)
2368 && loc->pspace->num == bl->pspace->num
2369 && loc->owner->enable_state == bp_enabled
2370 && loc->enabled)
2371 /* Add the condition to the vector. This will be used later to send the
2372 conditions to the target. */
2373 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2374 loc->cond_bytecode);
2375 }
2376
2377 return;
2378}
2379
d3ce09f5
SS
2380/* Parses a command described by string CMD into an agent expression
2381 bytecode suitable for evaluation by the bytecode interpreter.
2382 Return NULL if there was any error during parsing. */
2383
2384static struct agent_expr *
2385parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2386{
2387 struct cleanup *old_cleanups = 0;
2388 struct expression *expr, **argvec;
2389 struct agent_expr *aexpr = NULL;
bbc13ae3
KS
2390 const char *cmdrest;
2391 const char *format_start, *format_end;
d3ce09f5
SS
2392 struct format_piece *fpieces;
2393 int nargs;
2394 struct gdbarch *gdbarch = get_current_arch ();
2395
2396 if (!cmd)
2397 return NULL;
2398
2399 cmdrest = cmd;
2400
2401 if (*cmdrest == ',')
2402 ++cmdrest;
bbc13ae3 2403 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2404
2405 if (*cmdrest++ != '"')
2406 error (_("No format string following the location"));
2407
2408 format_start = cmdrest;
2409
2410 fpieces = parse_format_string (&cmdrest);
2411
2412 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2413
2414 format_end = cmdrest;
2415
2416 if (*cmdrest++ != '"')
2417 error (_("Bad format string, non-terminated '\"'."));
2418
bbc13ae3 2419 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2420
2421 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2422 error (_("Invalid argument syntax"));
2423
2424 if (*cmdrest == ',')
2425 cmdrest++;
bbc13ae3 2426 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2427
2428 /* For each argument, make an expression. */
2429
2430 argvec = (struct expression **) alloca (strlen (cmd)
2431 * sizeof (struct expression *));
2432
2433 nargs = 0;
2434 while (*cmdrest != '\0')
2435 {
bbc13ae3 2436 const char *cmd1;
d3ce09f5
SS
2437
2438 cmd1 = cmdrest;
2439 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2440 argvec[nargs++] = expr;
2441 cmdrest = cmd1;
2442 if (*cmdrest == ',')
2443 ++cmdrest;
2444 }
2445
2446 /* We don't want to stop processing, so catch any errors
2447 that may show up. */
492d29ea 2448 TRY
d3ce09f5
SS
2449 {
2450 aexpr = gen_printf (scope, gdbarch, 0, 0,
2451 format_start, format_end - format_start,
2452 fpieces, nargs, argvec);
2453 }
492d29ea 2454 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2455 {
2456 /* If we got here, it means the command could not be parsed to a valid
2457 bytecode expression and thus can't be evaluated on the target's side.
2458 It's no use iterating through the other commands. */
492d29ea 2459 aexpr = NULL;
d3ce09f5 2460 }
492d29ea
PA
2461 END_CATCH
2462
2463 do_cleanups (old_cleanups);
d3ce09f5 2464
d3ce09f5
SS
2465 /* We have a valid agent expression, return it. */
2466 return aexpr;
2467}
2468
2469/* Based on location BL, create a list of breakpoint commands to be
2470 passed on to the target. If we have duplicated locations with
2471 different commands, we will add any such to the list. */
2472
2473static void
2474build_target_command_list (struct bp_location *bl)
2475{
2476 struct bp_location **locp = NULL, **loc2p;
2477 int null_command_or_parse_error = 0;
2478 int modified = bl->needs_update;
2479 struct bp_location *loc;
2480
8b4f3082
PA
2481 /* Release commands left over from a previous insert. */
2482 VEC_free (agent_expr_p, bl->target_info.tcommands);
2483
41fac0cf 2484 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2485 return;
2486
41fac0cf
PA
2487 /* For now, limit to agent-style dprintf breakpoints. */
2488 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2489 return;
2490
41fac0cf
PA
2491 /* For now, if we have any duplicate location that isn't a dprintf,
2492 don't install the target-side commands, as that would make the
2493 breakpoint not be reported to the core, and we'd lose
2494 control. */
2495 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2496 {
2497 loc = (*loc2p);
2498 if (is_breakpoint (loc->owner)
2499 && loc->pspace->num == bl->pspace->num
2500 && loc->owner->type != bp_dprintf)
2501 return;
2502 }
2503
d3ce09f5
SS
2504 /* Do a first pass to check for locations with no assigned
2505 conditions or conditions that fail to parse to a valid agent expression
2506 bytecode. If any of these happen, then it's no use to send conditions
2507 to the target since this location will always trigger and generate a
2508 response back to GDB. */
2509 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2510 {
2511 loc = (*loc2p);
2512 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2513 {
2514 if (modified)
2515 {
2516 struct agent_expr *aexpr;
2517
2518 /* Re-parse the commands since something changed. In that
2519 case we already freed the command bytecodes (see
2520 force_breakpoint_reinsertion). We just
2521 need to parse the command to bytecodes again. */
2522 aexpr = parse_cmd_to_aexpr (bl->address,
2523 loc->owner->extra_string);
2524 loc->cmd_bytecode = aexpr;
2525
2526 if (!aexpr)
2527 continue;
2528 }
2529
2530 /* If we have a NULL bytecode expression, it means something
2531 went wrong or we have a null command expression. */
2532 if (!loc->cmd_bytecode)
2533 {
2534 null_command_or_parse_error = 1;
2535 break;
2536 }
2537 }
2538 }
2539
2540 /* If anything failed, then we're not doing target-side commands,
2541 and so clean up. */
2542 if (null_command_or_parse_error)
2543 {
2544 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2545 {
2546 loc = (*loc2p);
2547 if (is_breakpoint (loc->owner)
2548 && loc->pspace->num == bl->pspace->num)
2549 {
2550 /* Only go as far as the first NULL bytecode is
2551 located. */
40fb6c5e 2552 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2553 return;
2554
40fb6c5e
HZ
2555 free_agent_expr (loc->cmd_bytecode);
2556 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2557 }
2558 }
2559 }
2560
2561 /* No NULL commands or failed bytecode generation. Build a command list
2562 for this location's address. */
2563 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2564 {
2565 loc = (*loc2p);
2566 if (loc->owner->extra_string
2567 && is_breakpoint (loc->owner)
2568 && loc->pspace->num == bl->pspace->num
2569 && loc->owner->enable_state == bp_enabled
2570 && loc->enabled)
2571 /* Add the command to the vector. This will be used later
2572 to send the commands to the target. */
2573 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2574 loc->cmd_bytecode);
2575 }
2576
2577 bl->target_info.persist = 0;
2578 /* Maybe flag this location as persistent. */
2579 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2580 bl->target_info.persist = 1;
2581}
2582
35df4500
TJB
2583/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2584 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2585 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2586 Returns 0 for success, 1 if the bp_location type is not supported or
2587 -1 for failure.
879bfdc2 2588
4a64f543
MS
2589 NOTE drow/2003-09-09: This routine could be broken down to an
2590 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2591static int
35df4500 2592insert_bp_location (struct bp_location *bl,
26bb91f3 2593 struct ui_file *tmp_error_stream,
3fbb6ffa 2594 int *disabled_breaks,
dd61ec5c
MW
2595 int *hw_breakpoint_error,
2596 int *hw_bp_error_explained_already)
879bfdc2 2597{
0000e5cc
PA
2598 enum errors bp_err = GDB_NO_ERROR;
2599 const char *bp_err_message = NULL;
879bfdc2 2600
b775012e 2601 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2602 return 0;
2603
35c63cd8
JB
2604 /* Note we don't initialize bl->target_info, as that wipes out
2605 the breakpoint location's shadow_contents if the breakpoint
2606 is still inserted at that location. This in turn breaks
2607 target_read_memory which depends on these buffers when
2608 a memory read is requested at the breakpoint location:
2609 Once the target_info has been wiped, we fail to see that
2610 we have a breakpoint inserted at that address and thus
2611 read the breakpoint instead of returning the data saved in
2612 the breakpoint location's shadow contents. */
0d5ed153 2613 bl->target_info.reqstd_address = bl->address;
35df4500 2614 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2615 bl->target_info.length = bl->length;
8181d85f 2616
b775012e
LM
2617 /* When working with target-side conditions, we must pass all the conditions
2618 for the same breakpoint address down to the target since GDB will not
2619 insert those locations. With a list of breakpoint conditions, the target
2620 can decide when to stop and notify GDB. */
2621
2622 if (is_breakpoint (bl->owner))
2623 {
2624 build_target_condition_list (bl);
d3ce09f5
SS
2625 build_target_command_list (bl);
2626 /* Reset the modification marker. */
b775012e
LM
2627 bl->needs_update = 0;
2628 }
2629
35df4500
TJB
2630 if (bl->loc_type == bp_loc_software_breakpoint
2631 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2632 {
35df4500 2633 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2634 {
2635 /* If the explicitly specified breakpoint type
2636 is not hardware breakpoint, check the memory map to see
2637 if the breakpoint address is in read only memory or not.
4a64f543 2638
765dc015
VP
2639 Two important cases are:
2640 - location type is not hardware breakpoint, memory
2641 is readonly. We change the type of the location to
2642 hardware breakpoint.
4a64f543
MS
2643 - location type is hardware breakpoint, memory is
2644 read-write. This means we've previously made the
2645 location hardware one, but then the memory map changed,
2646 so we undo.
765dc015 2647
4a64f543
MS
2648 When breakpoints are removed, remove_breakpoints will use
2649 location types we've just set here, the only possible
2650 problem is that memory map has changed during running
2651 program, but it's not going to work anyway with current
2652 gdb. */
765dc015 2653 struct mem_region *mr
0d5ed153 2654 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2655
2656 if (mr)
2657 {
2658 if (automatic_hardware_breakpoints)
2659 {
765dc015
VP
2660 enum bp_loc_type new_type;
2661
2662 if (mr->attrib.mode != MEM_RW)
2663 new_type = bp_loc_hardware_breakpoint;
2664 else
2665 new_type = bp_loc_software_breakpoint;
2666
35df4500 2667 if (new_type != bl->loc_type)
765dc015
VP
2668 {
2669 static int said = 0;
cc59ec59 2670
35df4500 2671 bl->loc_type = new_type;
765dc015
VP
2672 if (!said)
2673 {
3e43a32a
MS
2674 fprintf_filtered (gdb_stdout,
2675 _("Note: automatically using "
2676 "hardware breakpoints for "
2677 "read-only addresses.\n"));
765dc015
VP
2678 said = 1;
2679 }
2680 }
2681 }
35df4500 2682 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2683 && mr->attrib.mode != MEM_RW)
2684 {
2685 fprintf_unfiltered (tmp_error_stream,
2686 _("Cannot insert breakpoint %d.\n"
2687 "Cannot set software breakpoint "
2688 "at read-only address %s\n"),
2689 bl->owner->number,
2690 paddress (bl->gdbarch, bl->address));
2691 return 1;
2692 }
765dc015
VP
2693 }
2694 }
2695
879bfdc2
DJ
2696 /* First check to see if we have to handle an overlay. */
2697 if (overlay_debugging == ovly_off
35df4500
TJB
2698 || bl->section == NULL
2699 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2700 {
2701 /* No overlay handling: just set the breakpoint. */
492d29ea 2702 TRY
dd61ec5c 2703 {
0000e5cc
PA
2704 int val;
2705
dd61ec5c 2706 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2707 if (val)
2708 bp_err = GENERIC_ERROR;
dd61ec5c 2709 }
492d29ea 2710 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2711 {
0000e5cc
PA
2712 bp_err = e.error;
2713 bp_err_message = e.message;
dd61ec5c 2714 }
492d29ea 2715 END_CATCH
879bfdc2
DJ
2716 }
2717 else
2718 {
4a64f543 2719 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2720 Shall we set a breakpoint at the LMA? */
2721 if (!overlay_events_enabled)
2722 {
2723 /* Yes -- overlay event support is not active,
2724 so we must try to set a breakpoint at the LMA.
2725 This will not work for a hardware breakpoint. */
35df4500 2726 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2727 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2728 bl->owner->number);
879bfdc2
DJ
2729 else
2730 {
35df4500
TJB
2731 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2732 bl->section);
879bfdc2 2733 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2734 bl->overlay_target_info = bl->target_info;
0d5ed153 2735 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2736
2737 /* No overlay handling: just set the breakpoint. */
492d29ea 2738 TRY
0000e5cc
PA
2739 {
2740 int val;
2741
2742 val = target_insert_breakpoint (bl->gdbarch,
2743 &bl->overlay_target_info);
2744 if (val)
2745 bp_err = GENERIC_ERROR;
2746 }
492d29ea 2747 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2748 {
2749 bp_err = e.error;
2750 bp_err_message = e.message;
2751 }
492d29ea 2752 END_CATCH
0000e5cc
PA
2753
2754 if (bp_err != GDB_NO_ERROR)
99361f52 2755 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2756 "Overlay breakpoint %d "
2757 "failed: in ROM?\n",
35df4500 2758 bl->owner->number);
879bfdc2
DJ
2759 }
2760 }
2761 /* Shall we set a breakpoint at the VMA? */
35df4500 2762 if (section_is_mapped (bl->section))
879bfdc2
DJ
2763 {
2764 /* Yes. This overlay section is mapped into memory. */
492d29ea 2765 TRY
dd61ec5c 2766 {
0000e5cc
PA
2767 int val;
2768
dd61ec5c 2769 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2770 if (val)
2771 bp_err = GENERIC_ERROR;
dd61ec5c 2772 }
492d29ea 2773 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2774 {
0000e5cc
PA
2775 bp_err = e.error;
2776 bp_err_message = e.message;
dd61ec5c 2777 }
492d29ea 2778 END_CATCH
879bfdc2
DJ
2779 }
2780 else
2781 {
2782 /* No. This breakpoint will not be inserted.
2783 No error, but do not mark the bp as 'inserted'. */
2784 return 0;
2785 }
2786 }
2787
0000e5cc 2788 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2789 {
2790 /* Can't set the breakpoint. */
0000e5cc
PA
2791
2792 /* In some cases, we might not be able to insert a
2793 breakpoint in a shared library that has already been
2794 removed, but we have not yet processed the shlib unload
2795 event. Unfortunately, some targets that implement
076855f9
PA
2796 breakpoint insertion themselves can't tell why the
2797 breakpoint insertion failed (e.g., the remote target
2798 doesn't define error codes), so we must treat generic
2799 errors as memory errors. */
0000e5cc 2800 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2801 && bl->loc_type == bp_loc_software_breakpoint
08351840 2802 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2803 || shared_objfile_contains_address_p (bl->pspace,
2804 bl->address)))
879bfdc2 2805 {
4a64f543 2806 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2807 bl->shlib_disabled = 1;
8d3788bd 2808 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2809 if (!*disabled_breaks)
2810 {
2811 fprintf_unfiltered (tmp_error_stream,
2812 "Cannot insert breakpoint %d.\n",
2813 bl->owner->number);
2814 fprintf_unfiltered (tmp_error_stream,
2815 "Temporarily disabling shared "
2816 "library breakpoints:\n");
2817 }
2818 *disabled_breaks = 1;
879bfdc2 2819 fprintf_unfiltered (tmp_error_stream,
35df4500 2820 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2821 return 0;
879bfdc2
DJ
2822 }
2823 else
879bfdc2 2824 {
35df4500 2825 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2826 {
0000e5cc
PA
2827 *hw_breakpoint_error = 1;
2828 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2829 fprintf_unfiltered (tmp_error_stream,
2830 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2831 bl->owner->number, bp_err_message ? ":" : ".\n");
2832 if (bp_err_message != NULL)
2833 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2834 }
2835 else
2836 {
0000e5cc
PA
2837 if (bp_err_message == NULL)
2838 {
2839 char *message
2840 = memory_error_message (TARGET_XFER_E_IO,
2841 bl->gdbarch, bl->address);
2842 struct cleanup *old_chain = make_cleanup (xfree, message);
2843
2844 fprintf_unfiltered (tmp_error_stream,
2845 "Cannot insert breakpoint %d.\n"
2846 "%s\n",
2847 bl->owner->number, message);
2848 do_cleanups (old_chain);
2849 }
2850 else
2851 {
2852 fprintf_unfiltered (tmp_error_stream,
2853 "Cannot insert breakpoint %d: %s\n",
2854 bl->owner->number,
2855 bp_err_message);
2856 }
879bfdc2 2857 }
0000e5cc 2858 return 1;
879bfdc2
DJ
2859
2860 }
2861 }
2862 else
35df4500 2863 bl->inserted = 1;
879bfdc2 2864
0000e5cc 2865 return 0;
879bfdc2
DJ
2866 }
2867
35df4500 2868 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2869 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2870 watchpoints. It's not clear that it's necessary... */
35df4500 2871 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2872 {
0000e5cc
PA
2873 int val;
2874
77b06cd7
TJB
2875 gdb_assert (bl->owner->ops != NULL
2876 && bl->owner->ops->insert_location != NULL);
2877
2878 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2879
2880 /* If trying to set a read-watchpoint, and it turns out it's not
2881 supported, try emulating one with an access watchpoint. */
35df4500 2882 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2883 {
2884 struct bp_location *loc, **loc_temp;
2885
2886 /* But don't try to insert it, if there's already another
2887 hw_access location that would be considered a duplicate
2888 of this one. */
2889 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2890 if (loc != bl
85d721b8 2891 && loc->watchpoint_type == hw_access
35df4500 2892 && watchpoint_locations_match (bl, loc))
85d721b8 2893 {
35df4500
TJB
2894 bl->duplicate = 1;
2895 bl->inserted = 1;
2896 bl->target_info = loc->target_info;
2897 bl->watchpoint_type = hw_access;
85d721b8
PA
2898 val = 0;
2899 break;
2900 }
2901
2902 if (val == 1)
2903 {
77b06cd7
TJB
2904 bl->watchpoint_type = hw_access;
2905 val = bl->owner->ops->insert_location (bl);
2906
2907 if (val)
2908 /* Back to the original value. */
2909 bl->watchpoint_type = hw_read;
85d721b8
PA
2910 }
2911 }
2912
35df4500 2913 bl->inserted = (val == 0);
879bfdc2
DJ
2914 }
2915
35df4500 2916 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2917 {
0000e5cc
PA
2918 int val;
2919
77b06cd7
TJB
2920 gdb_assert (bl->owner->ops != NULL
2921 && bl->owner->ops->insert_location != NULL);
2922
2923 val = bl->owner->ops->insert_location (bl);
2924 if (val)
2925 {
2926 bl->owner->enable_state = bp_disabled;
2927
2928 if (val == 1)
2929 warning (_("\
2930Error inserting catchpoint %d: Your system does not support this type\n\
2931of catchpoint."), bl->owner->number);
2932 else
2933 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2934 }
2935
2936 bl->inserted = (val == 0);
1640b821
DJ
2937
2938 /* We've already printed an error message if there was a problem
2939 inserting this catchpoint, and we've disabled the catchpoint,
2940 so just return success. */
2941 return 0;
879bfdc2
DJ
2942 }
2943
2944 return 0;
2945}
2946
6c95b8df
PA
2947/* This function is called when program space PSPACE is about to be
2948 deleted. It takes care of updating breakpoints to not reference
2949 PSPACE anymore. */
2950
2951void
2952breakpoint_program_space_exit (struct program_space *pspace)
2953{
2954 struct breakpoint *b, *b_temp;
876fa593 2955 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2956
2957 /* Remove any breakpoint that was set through this program space. */
2958 ALL_BREAKPOINTS_SAFE (b, b_temp)
2959 {
2960 if (b->pspace == pspace)
2961 delete_breakpoint (b);
2962 }
2963
2964 /* Breakpoints set through other program spaces could have locations
2965 bound to PSPACE as well. Remove those. */
876fa593 2966 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2967 {
2968 struct bp_location *tmp;
2969
2970 if (loc->pspace == pspace)
2971 {
2bdf28a0 2972 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2973 if (loc->owner->loc == loc)
2974 loc->owner->loc = loc->next;
2975 else
2976 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2977 if (tmp->next == loc)
2978 {
2979 tmp->next = loc->next;
2980 break;
2981 }
2982 }
2983 }
2984
2985 /* Now update the global location list to permanently delete the
2986 removed locations above. */
44702360 2987 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2988}
2989
74960c60
VP
2990/* Make sure all breakpoints are inserted in inferior.
2991 Throws exception on any error.
2992 A breakpoint that is already inserted won't be inserted
2993 again, so calling this function twice is safe. */
2994void
2995insert_breakpoints (void)
2996{
2997 struct breakpoint *bpt;
2998
2999 ALL_BREAKPOINTS (bpt)
3000 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3001 {
3002 struct watchpoint *w = (struct watchpoint *) bpt;
3003
3004 update_watchpoint (w, 0 /* don't reparse. */);
3005 }
74960c60 3006
04086b45
PA
3007 /* Updating watchpoints creates new locations, so update the global
3008 location list. Explicitly tell ugll to insert locations and
3009 ignore breakpoints_always_inserted_mode. */
3010 update_global_location_list (UGLL_INSERT);
74960c60
VP
3011}
3012
20388dd6
YQ
3013/* Invoke CALLBACK for each of bp_location. */
3014
3015void
3016iterate_over_bp_locations (walk_bp_location_callback callback)
3017{
3018 struct bp_location *loc, **loc_tmp;
3019
3020 ALL_BP_LOCATIONS (loc, loc_tmp)
3021 {
3022 callback (loc, NULL);
3023 }
3024}
3025
b775012e
LM
3026/* This is used when we need to synch breakpoint conditions between GDB and the
3027 target. It is the case with deleting and disabling of breakpoints when using
3028 always-inserted mode. */
3029
3030static void
3031update_inserted_breakpoint_locations (void)
3032{
3033 struct bp_location *bl, **blp_tmp;
3034 int error_flag = 0;
3035 int val = 0;
3036 int disabled_breaks = 0;
3037 int hw_breakpoint_error = 0;
dd61ec5c 3038 int hw_bp_details_reported = 0;
b775012e
LM
3039
3040 struct ui_file *tmp_error_stream = mem_fileopen ();
3041 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3042
3043 /* Explicitly mark the warning -- this will only be printed if
3044 there was an error. */
3045 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3046
3047 save_current_space_and_thread ();
3048
3049 ALL_BP_LOCATIONS (bl, blp_tmp)
3050 {
3051 /* We only want to update software breakpoints and hardware
3052 breakpoints. */
3053 if (!is_breakpoint (bl->owner))
3054 continue;
3055
3056 /* We only want to update locations that are already inserted
3057 and need updating. This is to avoid unwanted insertion during
3058 deletion of breakpoints. */
3059 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3060 continue;
3061
3062 switch_to_program_space_and_thread (bl->pspace);
3063
3064 /* For targets that support global breakpoints, there's no need
3065 to select an inferior to insert breakpoint to. In fact, even
3066 if we aren't attached to any process yet, we should still
3067 insert breakpoints. */
f5656ead 3068 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3069 && ptid_equal (inferior_ptid, null_ptid))
3070 continue;
3071
3072 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3073 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3074 if (val)
3075 error_flag = val;
3076 }
3077
3078 if (error_flag)
3079 {
3080 target_terminal_ours_for_output ();
3081 error_stream (tmp_error_stream);
3082 }
3083
3084 do_cleanups (cleanups);
3085}
3086
c30eee59 3087/* Used when starting or continuing the program. */
c906108c 3088
74960c60
VP
3089static void
3090insert_breakpoint_locations (void)
c906108c 3091{
a5606eee 3092 struct breakpoint *bpt;
35df4500 3093 struct bp_location *bl, **blp_tmp;
eacd795a 3094 int error_flag = 0;
c906108c 3095 int val = 0;
3fbb6ffa 3096 int disabled_breaks = 0;
81d0cc19 3097 int hw_breakpoint_error = 0;
dd61ec5c 3098 int hw_bp_error_explained_already = 0;
c906108c 3099
81d0cc19 3100 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3101 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3102
81d0cc19
GS
3103 /* Explicitly mark the warning -- this will only be printed if
3104 there was an error. */
3105 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3106
3107 save_current_space_and_thread ();
3108
35df4500 3109 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3110 {
b775012e 3111 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3112 continue;
3113
4a64f543
MS
3114 /* There is no point inserting thread-specific breakpoints if
3115 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3116 has BL->OWNER always non-NULL. */
35df4500
TJB
3117 if (bl->owner->thread != -1
3118 && !valid_thread_id (bl->owner->thread))
f365de73
AS
3119 continue;
3120
35df4500 3121 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3122
3123 /* For targets that support global breakpoints, there's no need
3124 to select an inferior to insert breakpoint to. In fact, even
3125 if we aren't attached to any process yet, we should still
3126 insert breakpoints. */
f5656ead 3127 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3128 && ptid_equal (inferior_ptid, null_ptid))
3129 continue;
3130
3fbb6ffa 3131 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3132 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3133 if (val)
eacd795a 3134 error_flag = val;
879bfdc2 3135 }
c906108c 3136
4a64f543
MS
3137 /* If we failed to insert all locations of a watchpoint, remove
3138 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3139 ALL_BREAKPOINTS (bpt)
3140 {
3141 int some_failed = 0;
3142 struct bp_location *loc;
3143
3144 if (!is_hardware_watchpoint (bpt))
3145 continue;
3146
d6b74ac4 3147 if (!breakpoint_enabled (bpt))
a5606eee 3148 continue;
74960c60
VP
3149
3150 if (bpt->disposition == disp_del_at_next_stop)
3151 continue;
a5606eee
VP
3152
3153 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3154 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3155 {
3156 some_failed = 1;
3157 break;
3158 }
3159 if (some_failed)
3160 {
3161 for (loc = bpt->loc; loc; loc = loc->next)
3162 if (loc->inserted)
3163 remove_breakpoint (loc, mark_uninserted);
3164
3165 hw_breakpoint_error = 1;
3166 fprintf_unfiltered (tmp_error_stream,
3167 "Could not insert hardware watchpoint %d.\n",
3168 bpt->number);
eacd795a 3169 error_flag = -1;
a5606eee
VP
3170 }
3171 }
3172
eacd795a 3173 if (error_flag)
81d0cc19
GS
3174 {
3175 /* If a hardware breakpoint or watchpoint was inserted, add a
3176 message about possibly exhausted resources. */
dd61ec5c 3177 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3178 {
c6510018
MS
3179 fprintf_unfiltered (tmp_error_stream,
3180 "Could not insert hardware breakpoints:\n\
3181You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3182 }
81d0cc19
GS
3183 target_terminal_ours_for_output ();
3184 error_stream (tmp_error_stream);
3185 }
f7545552
TT
3186
3187 do_cleanups (cleanups);
c906108c
SS
3188}
3189
c30eee59
TJB
3190/* Used when the program stops.
3191 Returns zero if successful, or non-zero if there was a problem
3192 removing a breakpoint location. */
3193
c906108c 3194int
fba45db2 3195remove_breakpoints (void)
c906108c 3196{
35df4500 3197 struct bp_location *bl, **blp_tmp;
3a1bae8e 3198 int val = 0;
c906108c 3199
35df4500 3200 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3201 {
1e4d1764 3202 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3203 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3204 }
3a1bae8e 3205 return val;
c906108c
SS
3206}
3207
49fa26b0
PA
3208/* When a thread exits, remove breakpoints that are related to
3209 that thread. */
3210
3211static void
3212remove_threaded_breakpoints (struct thread_info *tp, int silent)
3213{
3214 struct breakpoint *b, *b_tmp;
3215
3216 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3217 {
96181529 3218 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3219 {
3220 b->disposition = disp_del_at_next_stop;
3221
3222 printf_filtered (_("\
46ecd527 3223Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3224 b->number, tp->num);
3225
3226 /* Hide it from the user. */
3227 b->number = 0;
3228 }
3229 }
3230}
3231
6c95b8df
PA
3232/* Remove breakpoints of process PID. */
3233
3234int
3235remove_breakpoints_pid (int pid)
3236{
35df4500 3237 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3238 int val;
3239 struct inferior *inf = find_inferior_pid (pid);
3240
35df4500 3241 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3242 {
35df4500 3243 if (bl->pspace != inf->pspace)
6c95b8df
PA
3244 continue;
3245
fc126975 3246 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3247 {
35df4500 3248 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3249 if (val != 0)
3250 return val;
3251 }
3252 }
3253 return 0;
3254}
3255
c906108c 3256int
fba45db2 3257reattach_breakpoints (int pid)
c906108c 3258{
6c95b8df 3259 struct cleanup *old_chain;
35df4500 3260 struct bp_location *bl, **blp_tmp;
c906108c 3261 int val;
86b887df 3262 struct ui_file *tmp_error_stream;
dd61ec5c 3263 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3264 struct inferior *inf;
3265 struct thread_info *tp;
3266
3267 tp = any_live_thread_of_process (pid);
3268 if (tp == NULL)
3269 return 1;
3270
3271 inf = find_inferior_pid (pid);
3272 old_chain = save_inferior_ptid ();
3273
3274 inferior_ptid = tp->ptid;
a4954f26 3275
86b887df 3276 tmp_error_stream = mem_fileopen ();
a4954f26 3277 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3278
35df4500 3279 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3280 {
35df4500 3281 if (bl->pspace != inf->pspace)
6c95b8df
PA
3282 continue;
3283
35df4500 3284 if (bl->inserted)
c5aa993b 3285 {
35df4500 3286 bl->inserted = 0;
dd61ec5c 3287 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3288 if (val != 0)
3289 {
ce696e05 3290 do_cleanups (old_chain);
c5aa993b
JM
3291 return val;
3292 }
3293 }
3294 }
ce696e05 3295 do_cleanups (old_chain);
c906108c
SS
3296 return 0;
3297}
3298
e58b0e63
PA
3299static int internal_breakpoint_number = -1;
3300
84f4c1fe
PM
3301/* Set the breakpoint number of B, depending on the value of INTERNAL.
3302 If INTERNAL is non-zero, the breakpoint number will be populated
3303 from internal_breakpoint_number and that variable decremented.
e5dd4106 3304 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3305 breakpoint_count and that value incremented. Internal breakpoints
3306 do not set the internal var bpnum. */
3307static void
3308set_breakpoint_number (int internal, struct breakpoint *b)
3309{
3310 if (internal)
3311 b->number = internal_breakpoint_number--;
3312 else
3313 {
3314 set_breakpoint_count (breakpoint_count + 1);
3315 b->number = breakpoint_count;
3316 }
3317}
3318
e62c965a 3319static struct breakpoint *
a6d9a66e 3320create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3321 CORE_ADDR address, enum bptype type,
c0a91b2b 3322 const struct breakpoint_ops *ops)
e62c965a 3323{
e62c965a
PP
3324 struct symtab_and_line sal;
3325 struct breakpoint *b;
3326
4a64f543 3327 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3328
3329 sal.pc = address;
3330 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3331 sal.pspace = current_program_space;
e62c965a 3332
06edf0c0 3333 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3334 b->number = internal_breakpoint_number--;
3335 b->disposition = disp_donttouch;
3336
3337 return b;
3338}
3339
17450429
PP
3340static const char *const longjmp_names[] =
3341 {
3342 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3343 };
3344#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3345
3346/* Per-objfile data private to breakpoint.c. */
3347struct breakpoint_objfile_data
3348{
3349 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3350 struct bound_minimal_symbol overlay_msym;
17450429
PP
3351
3352 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3353 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3354
28106bc2
SDJ
3355 /* True if we have looked for longjmp probes. */
3356 int longjmp_searched;
3357
3358 /* SystemTap probe points for longjmp (if any). */
3359 VEC (probe_p) *longjmp_probes;
3360
17450429 3361 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3362 struct bound_minimal_symbol terminate_msym;
17450429
PP
3363
3364 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3365 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3366
3367 /* True if we have looked for exception probes. */
3368 int exception_searched;
3369
3370 /* SystemTap probe points for unwinding (if any). */
3371 VEC (probe_p) *exception_probes;
17450429
PP
3372};
3373
3374static const struct objfile_data *breakpoint_objfile_key;
3375
3376/* Minimal symbol not found sentinel. */
3377static struct minimal_symbol msym_not_found;
3378
3379/* Returns TRUE if MSYM point to the "not found" sentinel. */
3380
3381static int
3382msym_not_found_p (const struct minimal_symbol *msym)
3383{
3384 return msym == &msym_not_found;
3385}
3386
3387/* Return per-objfile data needed by breakpoint.c.
3388 Allocate the data if necessary. */
3389
3390static struct breakpoint_objfile_data *
3391get_breakpoint_objfile_data (struct objfile *objfile)
3392{
3393 struct breakpoint_objfile_data *bp_objfile_data;
3394
3395 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3396 if (bp_objfile_data == NULL)
3397 {
3398 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3399 sizeof (*bp_objfile_data));
3400
3401 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3402 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3403 }
3404 return bp_objfile_data;
3405}
3406
28106bc2
SDJ
3407static void
3408free_breakpoint_probes (struct objfile *obj, void *data)
3409{
3410 struct breakpoint_objfile_data *bp_objfile_data = data;
3411
3412 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3413 VEC_free (probe_p, bp_objfile_data->exception_probes);
3414}
3415
e62c965a 3416static void
af02033e 3417create_overlay_event_breakpoint (void)
e62c965a 3418{
69de3c6a 3419 struct objfile *objfile;
af02033e 3420 const char *const func_name = "_ovly_debug_event";
e62c965a 3421
69de3c6a
PP
3422 ALL_OBJFILES (objfile)
3423 {
3424 struct breakpoint *b;
17450429
PP
3425 struct breakpoint_objfile_data *bp_objfile_data;
3426 CORE_ADDR addr;
f00aae0f 3427 char *p;
69de3c6a 3428
17450429
PP
3429 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3430
3b7344d5 3431 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3432 continue;
3433
3b7344d5 3434 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3435 {
3b7344d5 3436 struct bound_minimal_symbol m;
17450429
PP
3437
3438 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3439 if (m.minsym == NULL)
17450429
PP
3440 {
3441 /* Avoid future lookups in this objfile. */
3b7344d5 3442 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3443 continue;
3444 }
3445 bp_objfile_data->overlay_msym = m;
3446 }
e62c965a 3447
77e371c0 3448 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3449 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3450 bp_overlay_event,
3451 &internal_breakpoint_ops);
f00aae0f
KS
3452 p = ASTRDUP (func_name);
3453 b->location = new_linespec_location (&p);
e62c965a 3454
69de3c6a
PP
3455 if (overlay_debugging == ovly_auto)
3456 {
3457 b->enable_state = bp_enabled;
3458 overlay_events_enabled = 1;
3459 }
3460 else
3461 {
3462 b->enable_state = bp_disabled;
3463 overlay_events_enabled = 0;
3464 }
e62c965a 3465 }
44702360 3466 update_global_location_list (UGLL_MAY_INSERT);
e62c965a
PP
3467}
3468
0fd8e87f 3469static void
af02033e 3470create_longjmp_master_breakpoint (void)
0fd8e87f 3471{
6c95b8df 3472 struct program_space *pspace;
6c95b8df
PA
3473 struct cleanup *old_chain;
3474
3475 old_chain = save_current_program_space ();
0fd8e87f 3476
6c95b8df 3477 ALL_PSPACES (pspace)
af02033e
PP
3478 {
3479 struct objfile *objfile;
3480
3481 set_current_program_space (pspace);
3482
3483 ALL_OBJFILES (objfile)
0fd8e87f 3484 {
af02033e
PP
3485 int i;
3486 struct gdbarch *gdbarch;
17450429 3487 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3488
af02033e 3489 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3490
17450429
PP
3491 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3492
28106bc2
SDJ
3493 if (!bp_objfile_data->longjmp_searched)
3494 {
25f9533e
SDJ
3495 VEC (probe_p) *ret;
3496
3497 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3498 if (ret != NULL)
3499 {
3500 /* We are only interested in checking one element. */
3501 struct probe *p = VEC_index (probe_p, ret, 0);
3502
3503 if (!can_evaluate_probe_arguments (p))
3504 {
3505 /* We cannot use the probe interface here, because it does
3506 not know how to evaluate arguments. */
3507 VEC_free (probe_p, ret);
3508 ret = NULL;
3509 }
3510 }
3511 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3512 bp_objfile_data->longjmp_searched = 1;
3513 }
3514
3515 if (bp_objfile_data->longjmp_probes != NULL)
3516 {
3517 int i;
3518 struct probe *probe;
3519 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3520
3521 for (i = 0;
3522 VEC_iterate (probe_p,
3523 bp_objfile_data->longjmp_probes,
3524 i, probe);
3525 ++i)
3526 {
3527 struct breakpoint *b;
3528
729662a5
TT
3529 b = create_internal_breakpoint (gdbarch,
3530 get_probe_address (probe,
3531 objfile),
28106bc2
SDJ
3532 bp_longjmp_master,
3533 &internal_breakpoint_ops);
5b56227b
KS
3534 b->location
3535 = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3536 b->enable_state = bp_disabled;
3537 }
3538
3539 continue;
3540 }
3541
0569175e
TSD
3542 if (!gdbarch_get_longjmp_target_p (gdbarch))
3543 continue;
3544
17450429 3545 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3546 {
3547 struct breakpoint *b;
af02033e 3548 const char *func_name;
17450429 3549 CORE_ADDR addr;
f00aae0f 3550 char *p;
6c95b8df 3551
3b7344d5 3552 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3553 continue;
0fd8e87f 3554
17450429 3555 func_name = longjmp_names[i];
3b7344d5 3556 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3557 {
3b7344d5 3558 struct bound_minimal_symbol m;
17450429
PP
3559
3560 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3561 if (m.minsym == NULL)
17450429
PP
3562 {
3563 /* Prevent future lookups in this objfile. */
3b7344d5 3564 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3565 continue;
3566 }
3567 bp_objfile_data->longjmp_msym[i] = m;
3568 }
3569
77e371c0 3570 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3571 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3572 &internal_breakpoint_ops);
f00aae0f
KS
3573 p = ASTRDUP (func_name);
3574 b->location = new_linespec_location (&p);
af02033e
PP
3575 b->enable_state = bp_disabled;
3576 }
0fd8e87f 3577 }
af02033e 3578 }
44702360 3579 update_global_location_list (UGLL_MAY_INSERT);
6c95b8df
PA
3580
3581 do_cleanups (old_chain);
0fd8e87f
UW
3582}
3583
af02033e 3584/* Create a master std::terminate breakpoint. */
aa7d318d 3585static void
af02033e 3586create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3587{
3588 struct program_space *pspace;
aa7d318d 3589 struct cleanup *old_chain;
af02033e 3590 const char *const func_name = "std::terminate()";
aa7d318d
TT
3591
3592 old_chain = save_current_program_space ();
3593
3594 ALL_PSPACES (pspace)
17450429
PP
3595 {
3596 struct objfile *objfile;
3597 CORE_ADDR addr;
3598
3599 set_current_program_space (pspace);
3600
aa7d318d
TT
3601 ALL_OBJFILES (objfile)
3602 {
3603 struct breakpoint *b;
17450429 3604 struct breakpoint_objfile_data *bp_objfile_data;
f00aae0f 3605 char *p;
aa7d318d 3606
17450429 3607 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3608
3b7344d5 3609 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3610 continue;
3611
3b7344d5 3612 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3613 {
3b7344d5 3614 struct bound_minimal_symbol m;
17450429
PP
3615
3616 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3617 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3618 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3619 {
3620 /* Prevent future lookups in this objfile. */
3b7344d5 3621 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3622 continue;
3623 }
3624 bp_objfile_data->terminate_msym = m;
3625 }
aa7d318d 3626
77e371c0 3627 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3628 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3629 bp_std_terminate_master,
3630 &internal_breakpoint_ops);
f00aae0f
KS
3631 p = ASTRDUP (func_name);
3632 b->location = new_linespec_location (&p);
aa7d318d
TT
3633 b->enable_state = bp_disabled;
3634 }
17450429
PP
3635 }
3636
44702360 3637 update_global_location_list (UGLL_MAY_INSERT);
aa7d318d
TT
3638
3639 do_cleanups (old_chain);
3640}
3641
186c406b
TT
3642/* Install a master breakpoint on the unwinder's debug hook. */
3643
70221824 3644static void
186c406b
TT
3645create_exception_master_breakpoint (void)
3646{
3647 struct objfile *objfile;
17450429 3648 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3649
3650 ALL_OBJFILES (objfile)
3651 {
17450429
PP
3652 struct breakpoint *b;
3653 struct gdbarch *gdbarch;
3654 struct breakpoint_objfile_data *bp_objfile_data;
3655 CORE_ADDR addr;
f00aae0f 3656 char *p;
17450429
PP
3657
3658 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3659
28106bc2
SDJ
3660 /* We prefer the SystemTap probe point if it exists. */
3661 if (!bp_objfile_data->exception_searched)
3662 {
25f9533e
SDJ
3663 VEC (probe_p) *ret;
3664
3665 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3666
3667 if (ret != NULL)
3668 {
3669 /* We are only interested in checking one element. */
3670 struct probe *p = VEC_index (probe_p, ret, 0);
3671
3672 if (!can_evaluate_probe_arguments (p))
3673 {
3674 /* We cannot use the probe interface here, because it does
3675 not know how to evaluate arguments. */
3676 VEC_free (probe_p, ret);
3677 ret = NULL;
3678 }
3679 }
3680 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3681 bp_objfile_data->exception_searched = 1;
3682 }
3683
3684 if (bp_objfile_data->exception_probes != NULL)
3685 {
3686 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3687 int i;
3688 struct probe *probe;
3689
3690 for (i = 0;
3691 VEC_iterate (probe_p,
3692 bp_objfile_data->exception_probes,
3693 i, probe);
3694 ++i)
3695 {
3696 struct breakpoint *b;
3697
729662a5
TT
3698 b = create_internal_breakpoint (gdbarch,
3699 get_probe_address (probe,
3700 objfile),
28106bc2
SDJ
3701 bp_exception_master,
3702 &internal_breakpoint_ops);
5b56227b
KS
3703 b->location
3704 = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3705 b->enable_state = bp_disabled;
3706 }
3707
3708 continue;
3709 }
3710
3711 /* Otherwise, try the hook function. */
3712
3b7344d5 3713 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3714 continue;
3715
3716 gdbarch = get_objfile_arch (objfile);
186c406b 3717
3b7344d5 3718 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3719 {
3b7344d5 3720 struct bound_minimal_symbol debug_hook;
186c406b 3721
17450429 3722 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3723 if (debug_hook.minsym == NULL)
17450429 3724 {
3b7344d5 3725 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3726 continue;
3727 }
3728
3729 bp_objfile_data->exception_msym = debug_hook;
186c406b 3730 }
17450429 3731
77e371c0 3732 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3733 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3734 &current_target);
06edf0c0
PA
3735 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3736 &internal_breakpoint_ops);
f00aae0f
KS
3737 p = ASTRDUP (func_name);
3738 b->location = new_linespec_location (&p);
17450429 3739 b->enable_state = bp_disabled;
186c406b
TT
3740 }
3741
44702360 3742 update_global_location_list (UGLL_MAY_INSERT);
186c406b
TT
3743}
3744
c906108c 3745void
fba45db2 3746update_breakpoints_after_exec (void)
c906108c 3747{
35df4500 3748 struct breakpoint *b, *b_tmp;
876fa593 3749 struct bp_location *bploc, **bplocp_tmp;
c906108c 3750
25b22b0a
PA
3751 /* We're about to delete breakpoints from GDB's lists. If the
3752 INSERTED flag is true, GDB will try to lift the breakpoints by
3753 writing the breakpoints' "shadow contents" back into memory. The
3754 "shadow contents" are NOT valid after an exec, so GDB should not
3755 do that. Instead, the target is responsible from marking
3756 breakpoints out as soon as it detects an exec. We don't do that
3757 here instead, because there may be other attempts to delete
3758 breakpoints after detecting an exec and before reaching here. */
876fa593 3759 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3760 if (bploc->pspace == current_program_space)
3761 gdb_assert (!bploc->inserted);
c906108c 3762
35df4500 3763 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3764 {
6c95b8df
PA
3765 if (b->pspace != current_program_space)
3766 continue;
3767
4a64f543 3768 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3769 if (b->type == bp_shlib_event)
3770 {
3771 delete_breakpoint (b);
3772 continue;
3773 }
c906108c 3774
4a64f543 3775 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3776 if (b->type == bp_jit_event)
3777 {
3778 delete_breakpoint (b);
3779 continue;
3780 }
3781
1900040c 3782 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3783 as must overlay event and longjmp master breakpoints. */
3784 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3785 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3786 || b->type == bp_exception_master)
c4093a6a
JM
3787 {
3788 delete_breakpoint (b);
3789 continue;
3790 }
3791
4a64f543 3792 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3793 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3794 {
3795 delete_breakpoint (b);
3796 continue;
3797 }
3798
7c16b83e
PA
3799 /* Just like single-step breakpoints. */
3800 if (b->type == bp_single_step)
3801 {
3802 delete_breakpoint (b);
3803 continue;
3804 }
3805
611c83ae
PA
3806 /* Longjmp and longjmp-resume breakpoints are also meaningless
3807 after an exec. */
186c406b 3808 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3809 || b->type == bp_longjmp_call_dummy
186c406b 3810 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3811 {
3812 delete_breakpoint (b);
3813 continue;
3814 }
3815
ce78b96d
JB
3816 if (b->type == bp_catchpoint)
3817 {
3818 /* For now, none of the bp_catchpoint breakpoints need to
3819 do anything at this point. In the future, if some of
3820 the catchpoints need to something, we will need to add
3821 a new method, and call this method from here. */
3822 continue;
3823 }
3824
c5aa993b
JM
3825 /* bp_finish is a special case. The only way we ought to be able
3826 to see one of these when an exec() has happened, is if the user
3827 caught a vfork, and then said "finish". Ordinarily a finish just
3828 carries them to the call-site of the current callee, by setting
3829 a temporary bp there and resuming. But in this case, the finish
3830 will carry them entirely through the vfork & exec.
3831
3832 We don't want to allow a bp_finish to remain inserted now. But
3833 we can't safely delete it, 'cause finish_command has a handle to
3834 the bp on a bpstat, and will later want to delete it. There's a
3835 chance (and I've seen it happen) that if we delete the bp_finish
3836 here, that its storage will get reused by the time finish_command
3837 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3838 We really must allow finish_command to delete a bp_finish.
3839
e5dd4106 3840 In the absence of a general solution for the "how do we know
53a5351d
JM
3841 it's safe to delete something others may have handles to?"
3842 problem, what we'll do here is just uninsert the bp_finish, and
3843 let finish_command delete it.
3844
3845 (We know the bp_finish is "doomed" in the sense that it's
3846 momentary, and will be deleted as soon as finish_command sees
3847 the inferior stopped. So it doesn't matter that the bp's
3848 address is probably bogus in the new a.out, unlike e.g., the
3849 solib breakpoints.) */
c5aa993b 3850
c5aa993b
JM
3851 if (b->type == bp_finish)
3852 {
3853 continue;
3854 }
3855
3856 /* Without a symbolic address, we have little hope of the
3857 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3858 a.out. */
f00aae0f 3859 if (event_location_empty_p (b->location))
c5aa993b
JM
3860 {
3861 delete_breakpoint (b);
3862 continue;
3863 }
c5aa993b 3864 }
c906108c
SS
3865}
3866
3867int
d80ee84f 3868detach_breakpoints (ptid_t ptid)
c906108c 3869{
35df4500 3870 struct bp_location *bl, **blp_tmp;
3a1bae8e 3871 int val = 0;
ce696e05 3872 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3873 struct inferior *inf = current_inferior ();
c5aa993b 3874
dfd4cc63 3875 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3876 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3877
6c95b8df 3878 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3879 inferior_ptid = ptid;
35df4500 3880 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3881 {
35df4500 3882 if (bl->pspace != inf->pspace)
6c95b8df
PA
3883 continue;
3884
bd9673a4
PW
3885 /* This function must physically remove breakpoints locations
3886 from the specified ptid, without modifying the breakpoint
3887 package's state. Locations of type bp_loc_other are only
3888 maintained at GDB side. So, there is no need to remove
3889 these bp_loc_other locations. Moreover, removing these
3890 would modify the breakpoint package's state. */
3891 if (bl->loc_type == bp_loc_other)
3892 continue;
3893
35df4500
TJB
3894 if (bl->inserted)
3895 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3896 }
d03285ec 3897
ce696e05 3898 do_cleanups (old_chain);
3a1bae8e 3899 return val;
c906108c
SS
3900}
3901
35df4500 3902/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3903 Note that this is used to detach breakpoints from a child fork.
3904 When we get here, the child isn't in the inferior list, and neither
3905 do we have objects to represent its address space --- we should
35df4500 3906 *not* look at bl->pspace->aspace here. */
6c95b8df 3907
c906108c 3908static int
35df4500 3909remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3910{
3911 int val;
c5aa993b 3912
35df4500
TJB
3913 /* BL is never in moribund_locations by our callers. */
3914 gdb_assert (bl->owner != NULL);
2bdf28a0 3915
74960c60
VP
3916 /* The type of none suggests that owner is actually deleted.
3917 This should not ever happen. */
35df4500 3918 gdb_assert (bl->owner->type != bp_none);
0bde7532 3919
35df4500
TJB
3920 if (bl->loc_type == bp_loc_software_breakpoint
3921 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3922 {
c02f5703
MS
3923 /* "Normal" instruction breakpoint: either the standard
3924 trap-instruction bp (bp_breakpoint), or a
3925 bp_hardware_breakpoint. */
3926
3927 /* First check to see if we have to handle an overlay. */
3928 if (overlay_debugging == ovly_off
35df4500
TJB
3929 || bl->section == NULL
3930 || !(section_is_overlay (bl->section)))
c02f5703
MS
3931 {
3932 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3933
3934 /* If we're trying to uninsert a memory breakpoint that we
3935 know is set in a dynamic object that is marked
3936 shlib_disabled, then either the dynamic object was
3937 removed with "remove-symbol-file" or with
3938 "nosharedlibrary". In the former case, we don't know
3939 whether another dynamic object might have loaded over the
3940 breakpoint's address -- the user might well let us know
3941 about it next with add-symbol-file (the whole point of
d03de421 3942 add-symbol-file is letting the user manually maintain a
08351840
PA
3943 list of dynamically loaded objects). If we have the
3944 breakpoint's shadow memory, that is, this is a software
3945 breakpoint managed by GDB, check whether the breakpoint
3946 is still inserted in memory, to avoid overwriting wrong
3947 code with stale saved shadow contents. Note that HW
3948 breakpoints don't have shadow memory, as they're
3949 implemented using a mechanism that is not dependent on
3950 being able to modify the target's memory, and as such
3951 they should always be removed. */
3952 if (bl->shlib_disabled
3953 && bl->target_info.shadow_len != 0
3954 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3955 val = 0;
3956 else
3957 val = bl->owner->ops->remove_location (bl);
c02f5703 3958 }
c906108c
SS
3959 else
3960 {
4a64f543 3961 /* This breakpoint is in an overlay section.
c02f5703
MS
3962 Did we set a breakpoint at the LMA? */
3963 if (!overlay_events_enabled)
3964 {
3965 /* Yes -- overlay event support is not active, so we
3966 should have set a breakpoint at the LMA. Remove it.
3967 */
c02f5703
MS
3968 /* Ignore any failures: if the LMA is in ROM, we will
3969 have already warned when we failed to insert it. */
35df4500
TJB
3970 if (bl->loc_type == bp_loc_hardware_breakpoint)
3971 target_remove_hw_breakpoint (bl->gdbarch,
3972 &bl->overlay_target_info);
c02f5703 3973 else
35df4500
TJB
3974 target_remove_breakpoint (bl->gdbarch,
3975 &bl->overlay_target_info);
c02f5703
MS
3976 }
3977 /* Did we set a breakpoint at the VMA?
3978 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3979 if (bl->inserted)
c906108c 3980 {
c02f5703
MS
3981 /* Yes -- remove it. Previously we did not bother to
3982 remove the breakpoint if the section had been
3983 unmapped, but let's not rely on that being safe. We
3984 don't know what the overlay manager might do. */
aa67235e
UW
3985
3986 /* However, we should remove *software* breakpoints only
3987 if the section is still mapped, or else we overwrite
3988 wrong code with the saved shadow contents. */
348d480f
PA
3989 if (bl->loc_type == bp_loc_hardware_breakpoint
3990 || section_is_mapped (bl->section))
3991 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3992 else
3993 val = 0;
c906108c 3994 }
c02f5703
MS
3995 else
3996 {
3997 /* No -- not inserted, so no need to remove. No error. */
3998 val = 0;
3999 }
c906108c 4000 }
879d1e6b 4001
08351840
PA
4002 /* In some cases, we might not be able to remove a breakpoint in
4003 a shared library that has already been removed, but we have
4004 not yet processed the shlib unload event. Similarly for an
4005 unloaded add-symbol-file object - the user might not yet have
4006 had the chance to remove-symbol-file it. shlib_disabled will
4007 be set if the library/object has already been removed, but
4008 the breakpoint hasn't been uninserted yet, e.g., after
4009 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4010 always-inserted mode. */
076855f9 4011 if (val
08351840
PA
4012 && (bl->loc_type == bp_loc_software_breakpoint
4013 && (bl->shlib_disabled
4014 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4015 || shared_objfile_contains_address_p (bl->pspace,
4016 bl->address))))
879d1e6b
UW
4017 val = 0;
4018
c906108c
SS
4019 if (val)
4020 return val;
35df4500 4021 bl->inserted = (is == mark_inserted);
c906108c 4022 }
35df4500 4023 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4024 {
77b06cd7
TJB
4025 gdb_assert (bl->owner->ops != NULL
4026 && bl->owner->ops->remove_location != NULL);
4027
35df4500 4028 bl->inserted = (is == mark_inserted);
77b06cd7 4029 bl->owner->ops->remove_location (bl);
2e70b7b9 4030
c906108c 4031 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 4032 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4033 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4034 bl->owner->number);
c906108c 4035 }
35df4500
TJB
4036 else if (bl->owner->type == bp_catchpoint
4037 && breakpoint_enabled (bl->owner)
4038 && !bl->duplicate)
ce78b96d 4039 {
77b06cd7
TJB
4040 gdb_assert (bl->owner->ops != NULL
4041 && bl->owner->ops->remove_location != NULL);
ce78b96d 4042
77b06cd7 4043 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4044 if (val)
4045 return val;
77b06cd7 4046
35df4500 4047 bl->inserted = (is == mark_inserted);
ce78b96d 4048 }
c906108c
SS
4049
4050 return 0;
4051}
4052
6c95b8df 4053static int
35df4500 4054remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4055{
4056 int ret;
4057 struct cleanup *old_chain;
4058
35df4500
TJB
4059 /* BL is never in moribund_locations by our callers. */
4060 gdb_assert (bl->owner != NULL);
2bdf28a0 4061
6c95b8df
PA
4062 /* The type of none suggests that owner is actually deleted.
4063 This should not ever happen. */
35df4500 4064 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4065
4066 old_chain = save_current_space_and_thread ();
4067
35df4500 4068 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4069
35df4500 4070 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4071
4072 do_cleanups (old_chain);
4073 return ret;
4074}
4075
c906108c
SS
4076/* Clear the "inserted" flag in all breakpoints. */
4077
25b22b0a 4078void
fba45db2 4079mark_breakpoints_out (void)
c906108c 4080{
35df4500 4081 struct bp_location *bl, **blp_tmp;
c906108c 4082
35df4500 4083 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4084 if (bl->pspace == current_program_space)
35df4500 4085 bl->inserted = 0;
c906108c
SS
4086}
4087
53a5351d
JM
4088/* Clear the "inserted" flag in all breakpoints and delete any
4089 breakpoints which should go away between runs of the program.
c906108c
SS
4090
4091 Plus other such housekeeping that has to be done for breakpoints
4092 between runs.
4093
53a5351d
JM
4094 Note: this function gets called at the end of a run (by
4095 generic_mourn_inferior) and when a run begins (by
4a64f543 4096 init_wait_for_inferior). */
c906108c
SS
4097
4098
4099
4100void
fba45db2 4101breakpoint_init_inferior (enum inf_context context)
c906108c 4102{
35df4500
TJB
4103 struct breakpoint *b, *b_tmp;
4104 struct bp_location *bl, **blp_tmp;
1c5cfe86 4105 int ix;
6c95b8df 4106 struct program_space *pspace = current_program_space;
c906108c 4107
50c71eaf
PA
4108 /* If breakpoint locations are shared across processes, then there's
4109 nothing to do. */
f5656ead 4110 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4111 return;
4112
1a853c52 4113 mark_breakpoints_out ();
075f6582 4114
35df4500 4115 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4116 {
6c95b8df
PA
4117 if (b->loc && b->loc->pspace != pspace)
4118 continue;
4119
c5aa993b
JM
4120 switch (b->type)
4121 {
4122 case bp_call_dummy:
e2e4d78b 4123 case bp_longjmp_call_dummy:
c906108c 4124
c5aa993b 4125 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4126 cause problems when the inferior is rerun, so we better get
4127 rid of it. */
4128
4129 case bp_watchpoint_scope:
4130
4131 /* Also get rid of scope breakpoints. */
4132
4133 case bp_shlib_event:
4134
4135 /* Also remove solib event breakpoints. Their addresses may
4136 have changed since the last time we ran the program.
4137 Actually we may now be debugging against different target;
4138 and so the solib backend that installed this breakpoint may
4139 not be used in by the target. E.g.,
4140
4141 (gdb) file prog-linux
4142 (gdb) run # native linux target
4143 ...
4144 (gdb) kill
4145 (gdb) file prog-win.exe
4146 (gdb) tar rem :9999 # remote Windows gdbserver.
4147 */
c906108c 4148
f59f708a
PA
4149 case bp_step_resume:
4150
4151 /* Also remove step-resume breakpoints. */
4152
7c16b83e
PA
4153 case bp_single_step:
4154
4155 /* Also remove single-step breakpoints. */
4156
c5aa993b
JM
4157 delete_breakpoint (b);
4158 break;
c906108c 4159
c5aa993b
JM
4160 case bp_watchpoint:
4161 case bp_hardware_watchpoint:
4162 case bp_read_watchpoint:
4163 case bp_access_watchpoint:
3a5c3e22
PA
4164 {
4165 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4166
3a5c3e22
PA
4167 /* Likewise for watchpoints on local expressions. */
4168 if (w->exp_valid_block != NULL)
4169 delete_breakpoint (b);
4170 else if (context == inf_starting)
4171 {
4172 /* Reset val field to force reread of starting value in
4173 insert_breakpoints. */
4174 if (w->val)
4175 value_free (w->val);
4176 w->val = NULL;
4177 w->val_valid = 0;
c860120c 4178 }
3a5c3e22 4179 }
c5aa993b
JM
4180 break;
4181 default:
c5aa993b
JM
4182 break;
4183 }
4184 }
1c5cfe86
PA
4185
4186 /* Get rid of the moribund locations. */
35df4500
TJB
4187 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4188 decref_bp_location (&bl);
1c5cfe86 4189 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4190}
4191
6c95b8df
PA
4192/* These functions concern about actual breakpoints inserted in the
4193 target --- to e.g. check if we need to do decr_pc adjustment or if
4194 we need to hop over the bkpt --- so we check for address space
4195 match, not program space. */
4196
c2c6d25f
JM
4197/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4198 exists at PC. It returns ordinary_breakpoint_here if it's an
4199 ordinary breakpoint, or permanent_breakpoint_here if it's a
4200 permanent breakpoint.
4201 - When continuing from a location with an ordinary breakpoint, we
4202 actually single step once before calling insert_breakpoints.
e5dd4106 4203 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4204 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4205 the target, to advance the PC past the breakpoint. */
c906108c 4206
c2c6d25f 4207enum breakpoint_here
6c95b8df 4208breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4209{
35df4500 4210 struct bp_location *bl, **blp_tmp;
c2c6d25f 4211 int any_breakpoint_here = 0;
c906108c 4212
35df4500 4213 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4214 {
35df4500
TJB
4215 if (bl->loc_type != bp_loc_software_breakpoint
4216 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4217 continue;
4218
f1310107 4219 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4220 if ((breakpoint_enabled (bl->owner)
1a853c52 4221 || bl->permanent)
f1310107 4222 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4223 {
4224 if (overlay_debugging
35df4500
TJB
4225 && section_is_overlay (bl->section)
4226 && !section_is_mapped (bl->section))
075f6582 4227 continue; /* unmapped overlay -- can't be a match */
1a853c52 4228 else if (bl->permanent)
075f6582
DJ
4229 return permanent_breakpoint_here;
4230 else
4231 any_breakpoint_here = 1;
4232 }
4233 }
c906108c 4234
f486487f 4235 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4236}
4237
1c5cfe86
PA
4238/* Return true if there's a moribund breakpoint at PC. */
4239
4240int
6c95b8df 4241moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4242{
4243 struct bp_location *loc;
4244 int ix;
4245
4246 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4247 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4248 return 1;
4249
4250 return 0;
4251}
c2c6d25f 4252
f7ce857f
PA
4253/* Returns non-zero iff BL is inserted at PC, in address space
4254 ASPACE. */
4255
4256static int
4257bp_location_inserted_here_p (struct bp_location *bl,
4258 struct address_space *aspace, CORE_ADDR pc)
4259{
4260 if (bl->inserted
4261 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4262 aspace, pc))
4263 {
4264 if (overlay_debugging
4265 && section_is_overlay (bl->section)
4266 && !section_is_mapped (bl->section))
4267 return 0; /* unmapped overlay -- can't be a match */
4268 else
4269 return 1;
4270 }
4271 return 0;
4272}
4273
a1fd2fa5 4274/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4275
4276int
a1fd2fa5 4277breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4278{
f7ce857f
PA
4279 struct bp_location **blp, **blp_tmp = NULL;
4280 struct bp_location *bl;
c906108c 4281
f7ce857f 4282 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4283 {
f7ce857f
PA
4284 struct bp_location *bl = *blp;
4285
35df4500
TJB
4286 if (bl->loc_type != bp_loc_software_breakpoint
4287 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4288 continue;
4289
f7ce857f
PA
4290 if (bp_location_inserted_here_p (bl, aspace, pc))
4291 return 1;
c5aa993b 4292 }
c36b740a
VP
4293 return 0;
4294}
4295
a1fd2fa5
PA
4296/* This function returns non-zero iff there is a software breakpoint
4297 inserted at PC. */
c36b740a
VP
4298
4299int
a1fd2fa5
PA
4300software_breakpoint_inserted_here_p (struct address_space *aspace,
4301 CORE_ADDR pc)
4fa8626c 4302{
f7ce857f
PA
4303 struct bp_location **blp, **blp_tmp = NULL;
4304 struct bp_location *bl;
4fa8626c 4305
f7ce857f 4306 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4307 {
f7ce857f
PA
4308 struct bp_location *bl = *blp;
4309
35df4500 4310 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4311 continue;
4312
f7ce857f
PA
4313 if (bp_location_inserted_here_p (bl, aspace, pc))
4314 return 1;
4fa8626c
DJ
4315 }
4316
4317 return 0;
9c02b525
PA
4318}
4319
4320/* See breakpoint.h. */
4321
4322int
4323hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4324 CORE_ADDR pc)
4325{
4326 struct bp_location **blp, **blp_tmp = NULL;
4327 struct bp_location *bl;
4328
4329 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4330 {
4331 struct bp_location *bl = *blp;
4332
4333 if (bl->loc_type != bp_loc_hardware_breakpoint)
4334 continue;
4335
4336 if (bp_location_inserted_here_p (bl, aspace, pc))
4337 return 1;
4338 }
4339
4340 return 0;
4fa8626c
DJ
4341}
4342
9093389c
PA
4343int
4344hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4345 CORE_ADDR addr, ULONGEST len)
4346{
4347 struct breakpoint *bpt;
4348
4349 ALL_BREAKPOINTS (bpt)
4350 {
4351 struct bp_location *loc;
4352
4353 if (bpt->type != bp_hardware_watchpoint
4354 && bpt->type != bp_access_watchpoint)
4355 continue;
4356
4357 if (!breakpoint_enabled (bpt))
4358 continue;
4359
4360 for (loc = bpt->loc; loc; loc = loc->next)
4361 if (loc->pspace->aspace == aspace && loc->inserted)
4362 {
4363 CORE_ADDR l, h;
4364
4365 /* Check for intersection. */
4366 l = max (loc->address, addr);
4367 h = min (loc->address + loc->length, addr + len);
4368 if (l < h)
4369 return 1;
4370 }
4371 }
4372 return 0;
4373}
c906108c 4374\f
c5aa993b 4375
c906108c
SS
4376/* bpstat stuff. External routines' interfaces are documented
4377 in breakpoint.h. */
4378
4379int
c326b90e 4380is_catchpoint (struct breakpoint *ep)
c906108c 4381{
533be4dd 4382 return (ep->type == bp_catchpoint);
c906108c
SS
4383}
4384
f431efe5
PA
4385/* Frees any storage that is part of a bpstat. Does not walk the
4386 'next' chain. */
4387
4388static void
198757a8
VP
4389bpstat_free (bpstat bs)
4390{
4391 if (bs->old_val != NULL)
4392 value_free (bs->old_val);
9add0f1b 4393 decref_counted_command_line (&bs->commands);
f431efe5 4394 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4395 xfree (bs);
4396}
4397
c906108c
SS
4398/* Clear a bpstat so that it says we are not at any breakpoint.
4399 Also free any storage that is part of a bpstat. */
4400
4401void
fba45db2 4402bpstat_clear (bpstat *bsp)
c906108c
SS
4403{
4404 bpstat p;
4405 bpstat q;
4406
4407 if (bsp == 0)
4408 return;
4409 p = *bsp;
4410 while (p != NULL)
4411 {
4412 q = p->next;
198757a8 4413 bpstat_free (p);
c906108c
SS
4414 p = q;
4415 }
4416 *bsp = NULL;
4417}
4418
4419/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4420 is part of the bpstat is copied as well. */
4421
4422bpstat
fba45db2 4423bpstat_copy (bpstat bs)
c906108c
SS
4424{
4425 bpstat p = NULL;
4426 bpstat tmp;
4427 bpstat retval = NULL;
4428
4429 if (bs == NULL)
4430 return bs;
4431
4432 for (; bs != NULL; bs = bs->next)
4433 {
4434 tmp = (bpstat) xmalloc (sizeof (*tmp));
4435 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4436 incref_counted_command_line (tmp->commands);
f431efe5 4437 incref_bp_location (tmp->bp_location_at);
31cc81e9 4438 if (bs->old_val != NULL)
3c3185ac
JK
4439 {
4440 tmp->old_val = value_copy (bs->old_val);
4441 release_value (tmp->old_val);
4442 }
31cc81e9 4443
c906108c
SS
4444 if (p == NULL)
4445 /* This is the first thing in the chain. */
4446 retval = tmp;
4447 else
4448 p->next = tmp;
4449 p = tmp;
4450 }
4451 p->next = NULL;
4452 return retval;
4453}
4454
4a64f543 4455/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4456
4457bpstat
fba45db2 4458bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4459{
c5aa993b
JM
4460 if (bsp == NULL)
4461 return NULL;
c906108c 4462
c5aa993b
JM
4463 for (; bsp != NULL; bsp = bsp->next)
4464 {
f431efe5 4465 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4466 return bsp;
4467 }
c906108c
SS
4468 return NULL;
4469}
4470
ab04a2af
TT
4471/* See breakpoint.h. */
4472
47591c29 4473int
427cd150 4474bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4475{
ab04a2af
TT
4476 for (; bsp != NULL; bsp = bsp->next)
4477 {
427cd150
TT
4478 if (bsp->breakpoint_at == NULL)
4479 {
4480 /* A moribund location can never explain a signal other than
4481 GDB_SIGNAL_TRAP. */
4482 if (sig == GDB_SIGNAL_TRAP)
47591c29 4483 return 1;
427cd150
TT
4484 }
4485 else
47591c29
PA
4486 {
4487 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4488 sig))
4489 return 1;
4490 }
ab04a2af
TT
4491 }
4492
47591c29 4493 return 0;
ab04a2af
TT
4494}
4495
4a64f543
MS
4496/* Put in *NUM the breakpoint number of the first breakpoint we are
4497 stopped at. *BSP upon return is a bpstat which points to the
4498 remaining breakpoints stopped at (but which is not guaranteed to be
4499 good for anything but further calls to bpstat_num).
4500
8671a17b
PA
4501 Return 0 if passed a bpstat which does not indicate any breakpoints.
4502 Return -1 if stopped at a breakpoint that has been deleted since
4503 we set it.
4504 Return 1 otherwise. */
c906108c
SS
4505
4506int
8671a17b 4507bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4508{
4509 struct breakpoint *b;
4510
4511 if ((*bsp) == NULL)
4512 return 0; /* No more breakpoint values */
8671a17b 4513
4a64f543
MS
4514 /* We assume we'll never have several bpstats that correspond to a
4515 single breakpoint -- otherwise, this function might return the
4516 same number more than once and this will look ugly. */
f431efe5 4517 b = (*bsp)->breakpoint_at;
8671a17b
PA
4518 *bsp = (*bsp)->next;
4519 if (b == NULL)
4520 return -1; /* breakpoint that's been deleted since */
4521
4522 *num = b->number; /* We have its number */
4523 return 1;
c906108c
SS
4524}
4525
e93ca019 4526/* See breakpoint.h. */
c906108c
SS
4527
4528void
e93ca019 4529bpstat_clear_actions (void)
c906108c 4530{
e93ca019
JK
4531 struct thread_info *tp;
4532 bpstat bs;
4533
4534 if (ptid_equal (inferior_ptid, null_ptid))
4535 return;
4536
4537 tp = find_thread_ptid (inferior_ptid);
4538 if (tp == NULL)
4539 return;
4540
4541 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4542 {
9add0f1b 4543 decref_counted_command_line (&bs->commands);
abf85f46 4544
c906108c
SS
4545 if (bs->old_val != NULL)
4546 {
4547 value_free (bs->old_val);
4548 bs->old_val = NULL;
4549 }
4550 }
4551}
4552
f3b1572e
PA
4553/* Called when a command is about to proceed the inferior. */
4554
4555static void
4556breakpoint_about_to_proceed (void)
4557{
4558 if (!ptid_equal (inferior_ptid, null_ptid))
4559 {
4560 struct thread_info *tp = inferior_thread ();
4561
4562 /* Allow inferior function calls in breakpoint commands to not
4563 interrupt the command list. When the call finishes
4564 successfully, the inferior will be standing at the same
4565 breakpoint as if nothing happened. */
16c381f0 4566 if (tp->control.in_infcall)
f3b1572e
PA
4567 return;
4568 }
4569
4570 breakpoint_proceeded = 1;
4571}
4572
4a64f543
MS
4573/* Stub for cleaning up our state if we error-out of a breakpoint
4574 command. */
c906108c 4575static void
4efb68b1 4576cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4577{
4578 executing_breakpoint_commands = 0;
4579}
4580
abf85f46
JK
4581/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4582 or its equivalent. */
4583
4584static int
4585command_line_is_silent (struct command_line *cmd)
4586{
4f45d445 4587 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4588}
4589
4a64f543
MS
4590/* Execute all the commands associated with all the breakpoints at
4591 this location. Any of these commands could cause the process to
4592 proceed beyond this point, etc. We look out for such changes by
4593 checking the global "breakpoint_proceeded" after each command.
c906108c 4594
347bddb7
PA
4595 Returns true if a breakpoint command resumed the inferior. In that
4596 case, it is the caller's responsibility to recall it again with the
4597 bpstat of the current thread. */
4598
4599static int
4600bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4601{
4602 bpstat bs;
4603 struct cleanup *old_chain;
347bddb7 4604 int again = 0;
c906108c
SS
4605
4606 /* Avoid endless recursion if a `source' command is contained
4607 in bs->commands. */
4608 if (executing_breakpoint_commands)
347bddb7 4609 return 0;
c906108c
SS
4610
4611 executing_breakpoint_commands = 1;
4612 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4613
cf6c5ffb
TT
4614 prevent_dont_repeat ();
4615
4a64f543 4616 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4617 bs = *bsp;
4618
4619 breakpoint_proceeded = 0;
4620 for (; bs != NULL; bs = bs->next)
4621 {
9add0f1b 4622 struct counted_command_line *ccmd;
6c50ab1c
JB
4623 struct command_line *cmd;
4624 struct cleanup *this_cmd_tree_chain;
4625
4626 /* Take ownership of the BSP's command tree, if it has one.
4627
4628 The command tree could legitimately contain commands like
4629 'step' and 'next', which call clear_proceed_status, which
4630 frees stop_bpstat's command tree. To make sure this doesn't
4631 free the tree we're executing out from under us, we need to
4632 take ownership of the tree ourselves. Since a given bpstat's
4633 commands are only executed once, we don't need to copy it; we
4634 can clear the pointer in the bpstat, and make sure we free
4635 the tree when we're done. */
9add0f1b
TT
4636 ccmd = bs->commands;
4637 bs->commands = NULL;
abf85f46
JK
4638 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4639 cmd = ccmd ? ccmd->commands : NULL;
4640 if (command_line_is_silent (cmd))
4641 {
4642 /* The action has been already done by bpstat_stop_status. */
4643 cmd = cmd->next;
4644 }
6c50ab1c 4645
c906108c
SS
4646 while (cmd != NULL)
4647 {
4648 execute_control_command (cmd);
4649
4650 if (breakpoint_proceeded)
4651 break;
4652 else
4653 cmd = cmd->next;
4654 }
6c50ab1c
JB
4655
4656 /* We can free this command tree now. */
4657 do_cleanups (this_cmd_tree_chain);
4658
c906108c 4659 if (breakpoint_proceeded)
32c1e744 4660 {
5589af0e 4661 if (interpreter_async && target_can_async_p ())
347bddb7
PA
4662 /* If we are in async mode, then the target might be still
4663 running, not stopped at any breakpoint, so nothing for
4664 us to do here -- just return to the event loop. */
4665 ;
32c1e744
VP
4666 else
4667 /* In sync mode, when execute_control_command returns
4668 we're already standing on the next breakpoint.
347bddb7
PA
4669 Breakpoint commands for that stop were not run, since
4670 execute_command does not run breakpoint commands --
4671 only command_line_handler does, but that one is not
4672 involved in execution of breakpoint commands. So, we
4673 can now execute breakpoint commands. It should be
4674 noted that making execute_command do bpstat actions is
4675 not an option -- in this case we'll have recursive
4676 invocation of bpstat for each breakpoint with a
4677 command, and can easily blow up GDB stack. Instead, we
4678 return true, which will trigger the caller to recall us
4679 with the new stop_bpstat. */
4680 again = 1;
4681 break;
32c1e744 4682 }
c906108c 4683 }
c2b8ed2c 4684 do_cleanups (old_chain);
347bddb7
PA
4685 return again;
4686}
4687
4688void
4689bpstat_do_actions (void)
4690{
353d1d73
JK
4691 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4692
347bddb7
PA
4693 /* Do any commands attached to breakpoint we are stopped at. */
4694 while (!ptid_equal (inferior_ptid, null_ptid)
4695 && target_has_execution
4696 && !is_exited (inferior_ptid)
4697 && !is_executing (inferior_ptid))
4698 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4699 and only return when it is stopped at the next breakpoint, we
4700 keep doing breakpoint actions until it returns false to
4701 indicate the inferior was not resumed. */
16c381f0 4702 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4703 break;
353d1d73
JK
4704
4705 discard_cleanups (cleanup_if_error);
c906108c
SS
4706}
4707
fa4727a6
DJ
4708/* Print out the (old or new) value associated with a watchpoint. */
4709
4710static void
4711watchpoint_value_print (struct value *val, struct ui_file *stream)
4712{
4713 if (val == NULL)
4714 fprintf_unfiltered (stream, _("<unreadable>"));
4715 else
79a45b7d
TT
4716 {
4717 struct value_print_options opts;
4718 get_user_print_options (&opts);
4719 value_print (val, stream, &opts);
4720 }
fa4727a6
DJ
4721}
4722
e514a9d6 4723/* Generic routine for printing messages indicating why we
4a64f543 4724 stopped. The behavior of this function depends on the value
e514a9d6
JM
4725 'print_it' in the bpstat structure. Under some circumstances we
4726 may decide not to print anything here and delegate the task to
4a64f543 4727 normal_stop(). */
e514a9d6
JM
4728
4729static enum print_stop_action
4730print_bp_stop_message (bpstat bs)
4731{
4732 switch (bs->print_it)
4733 {
4734 case print_it_noop:
4a64f543 4735 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4736 return PRINT_UNKNOWN;
4737 break;
4738
4739 case print_it_done:
4740 /* We still want to print the frame, but we already printed the
4a64f543 4741 relevant messages. */
e514a9d6
JM
4742 return PRINT_SRC_AND_LOC;
4743 break;
4744
4745 case print_it_normal:
4f8d1dc6 4746 {
f431efe5
PA
4747 struct breakpoint *b = bs->breakpoint_at;
4748
1a6a67de
TJB
4749 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4750 which has since been deleted. */
4751 if (b == NULL)
4752 return PRINT_UNKNOWN;
4753
348d480f
PA
4754 /* Normal case. Call the breakpoint's print_it method. */
4755 return b->ops->print_it (bs);
4f8d1dc6 4756 }
348d480f 4757 break;
3086aeae 4758
e514a9d6 4759 default:
8e65ff28 4760 internal_error (__FILE__, __LINE__,
e2e0b3e5 4761 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4762 break;
c906108c 4763 }
c906108c
SS
4764}
4765
edcc5120
TT
4766/* A helper function that prints a shared library stopped event. */
4767
4768static void
4769print_solib_event (int is_catchpoint)
4770{
4771 int any_deleted
4772 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4773 int any_added
4774 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4775
4776 if (!is_catchpoint)
4777 {
4778 if (any_added || any_deleted)
4779 ui_out_text (current_uiout,
4780 _("Stopped due to shared library event:\n"));
4781 else
4782 ui_out_text (current_uiout,
4783 _("Stopped due to shared library event (no "
4784 "libraries added or removed)\n"));
4785 }
4786
4787 if (ui_out_is_mi_like_p (current_uiout))
4788 ui_out_field_string (current_uiout, "reason",
4789 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4790
4791 if (any_deleted)
4792 {
4793 struct cleanup *cleanup;
4794 char *name;
4795 int ix;
4796
4797 ui_out_text (current_uiout, _(" Inferior unloaded "));
4798 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4799 "removed");
4800 for (ix = 0;
4801 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4802 ix, name);
4803 ++ix)
4804 {
4805 if (ix > 0)
4806 ui_out_text (current_uiout, " ");
4807 ui_out_field_string (current_uiout, "library", name);
4808 ui_out_text (current_uiout, "\n");
4809 }
4810
4811 do_cleanups (cleanup);
4812 }
4813
4814 if (any_added)
4815 {
4816 struct so_list *iter;
4817 int ix;
4818 struct cleanup *cleanup;
4819
4820 ui_out_text (current_uiout, _(" Inferior loaded "));
4821 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4822 "added");
4823 for (ix = 0;
4824 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4825 ix, iter);
4826 ++ix)
4827 {
4828 if (ix > 0)
4829 ui_out_text (current_uiout, " ");
4830 ui_out_field_string (current_uiout, "library", iter->so_name);
4831 ui_out_text (current_uiout, "\n");
4832 }
4833
4834 do_cleanups (cleanup);
4835 }
4836}
4837
e514a9d6
JM
4838/* Print a message indicating what happened. This is called from
4839 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4840 list - a list of the eventpoints that caused this stop. KIND is
4841 the target_waitkind for the stopping event. This
e514a9d6
JM
4842 routine calls the generic print routine for printing a message
4843 about reasons for stopping. This will print (for example) the
4844 "Breakpoint n," part of the output. The return value of this
4845 routine is one of:
c906108c 4846
4a64f543 4847 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4848 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4849 code to print the location. An example is
c5aa993b
JM
4850 "Breakpoint 1, " which should be followed by
4851 the location.
917317f4 4852 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4853 to also print the location part of the message.
4854 An example is the catch/throw messages, which
4a64f543 4855 don't require a location appended to the end.
917317f4 4856 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4857 further info to be printed. */
c906108c 4858
917317f4 4859enum print_stop_action
36dfb11c 4860bpstat_print (bpstat bs, int kind)
c906108c 4861{
f486487f 4862 enum print_stop_action val;
c5aa993b 4863
c906108c 4864 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4865 (Currently all watchpoints go on the bpstat whether hit or not.
4866 That probably could (should) be changed, provided care is taken
c906108c 4867 with respect to bpstat_explains_signal). */
e514a9d6
JM
4868 for (; bs; bs = bs->next)
4869 {
4870 val = print_bp_stop_message (bs);
4871 if (val == PRINT_SRC_ONLY
4872 || val == PRINT_SRC_AND_LOC
4873 || val == PRINT_NOTHING)
4874 return val;
4875 }
c906108c 4876
36dfb11c
TT
4877 /* If we had hit a shared library event breakpoint,
4878 print_bp_stop_message would print out this message. If we hit an
4879 OS-level shared library event, do the same thing. */
4880 if (kind == TARGET_WAITKIND_LOADED)
4881 {
edcc5120 4882 print_solib_event (0);
36dfb11c
TT
4883 return PRINT_NOTHING;
4884 }
4885
e514a9d6 4886 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4887 with and nothing was printed. */
917317f4 4888 return PRINT_UNKNOWN;
c906108c
SS
4889}
4890
c42bd95a
DE
4891/* Evaluate the expression EXP and return 1 if value is zero.
4892 This returns the inverse of the condition because it is called
4893 from catch_errors which returns 0 if an exception happened, and if an
4894 exception happens we want execution to stop.
4a64f543 4895 The argument is a "struct expression *" that has been cast to a
c42bd95a 4896 "void *" to make it pass through catch_errors. */
c906108c
SS
4897
4898static int
4efb68b1 4899breakpoint_cond_eval (void *exp)
c906108c 4900{
278cd55f 4901 struct value *mark = value_mark ();
c5aa993b 4902 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4903
c906108c
SS
4904 value_free_to_mark (mark);
4905 return i;
4906}
4907
5760d0ab 4908/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4909
4910static bpstat
5760d0ab 4911bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4912{
4913 bpstat bs;
4914
4915 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4916 bs->next = NULL;
4917 **bs_link_pointer = bs;
4918 *bs_link_pointer = &bs->next;
f431efe5
PA
4919 bs->breakpoint_at = bl->owner;
4920 bs->bp_location_at = bl;
4921 incref_bp_location (bl);
c906108c
SS
4922 /* If the condition is false, etc., don't do the commands. */
4923 bs->commands = NULL;
4924 bs->old_val = NULL;
4925 bs->print_it = print_it_normal;
4926 return bs;
4927}
4928\f
d983da9c
DJ
4929/* The target has stopped with waitstatus WS. Check if any hardware
4930 watchpoints have triggered, according to the target. */
4931
4932int
4933watchpoints_triggered (struct target_waitstatus *ws)
4934{
d92524f1 4935 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4936 CORE_ADDR addr;
4937 struct breakpoint *b;
4938
4939 if (!stopped_by_watchpoint)
4940 {
4941 /* We were not stopped by a watchpoint. Mark all watchpoints
4942 as not triggered. */
4943 ALL_BREAKPOINTS (b)
cc60f2e3 4944 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4945 {
4946 struct watchpoint *w = (struct watchpoint *) b;
4947
4948 w->watchpoint_triggered = watch_triggered_no;
4949 }
d983da9c
DJ
4950
4951 return 0;
4952 }
4953
4954 if (!target_stopped_data_address (&current_target, &addr))
4955 {
4956 /* We were stopped by a watchpoint, but we don't know where.
4957 Mark all watchpoints as unknown. */
4958 ALL_BREAKPOINTS (b)
cc60f2e3 4959 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4960 {
4961 struct watchpoint *w = (struct watchpoint *) b;
4962
4963 w->watchpoint_triggered = watch_triggered_unknown;
4964 }
d983da9c 4965
3c4797ba 4966 return 1;
d983da9c
DJ
4967 }
4968
4969 /* The target could report the data address. Mark watchpoints
4970 affected by this data address as triggered, and all others as not
4971 triggered. */
4972
4973 ALL_BREAKPOINTS (b)
cc60f2e3 4974 if (is_hardware_watchpoint (b))
d983da9c 4975 {
3a5c3e22 4976 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4977 struct bp_location *loc;
d983da9c 4978
3a5c3e22 4979 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4980 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4981 {
3a5c3e22 4982 if (is_masked_watchpoint (b))
9c06b0b4 4983 {
3a5c3e22
PA
4984 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4985 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4986
4987 if (newaddr == start)
4988 {
3a5c3e22 4989 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4990 break;
4991 }
4992 }
4993 /* Exact match not required. Within range is sufficient. */
4994 else if (target_watchpoint_addr_within_range (&current_target,
4995 addr, loc->address,
4996 loc->length))
4997 {
3a5c3e22 4998 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4999 break;
5000 }
5001 }
d983da9c
DJ
5002 }
5003
5004 return 1;
5005}
5006
c906108c
SS
5007/* Possible return values for watchpoint_check (this can't be an enum
5008 because of check_errors). */
5009/* The watchpoint has been deleted. */
5010#define WP_DELETED 1
5011/* The value has changed. */
5012#define WP_VALUE_CHANGED 2
5013/* The value has not changed. */
5014#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5015/* Ignore this watchpoint, no matter if the value changed or not. */
5016#define WP_IGNORE 4
c906108c
SS
5017
5018#define BP_TEMPFLAG 1
5019#define BP_HARDWAREFLAG 2
5020
4a64f543
MS
5021/* Evaluate watchpoint condition expression and check if its value
5022 changed.
553e4c11
JB
5023
5024 P should be a pointer to struct bpstat, but is defined as a void *
5025 in order for this function to be usable with catch_errors. */
c906108c
SS
5026
5027static int
4efb68b1 5028watchpoint_check (void *p)
c906108c
SS
5029{
5030 bpstat bs = (bpstat) p;
3a5c3e22 5031 struct watchpoint *b;
c906108c
SS
5032 struct frame_info *fr;
5033 int within_current_scope;
5034
f431efe5 5035 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5036 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5037 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5038
f6bc2008
PA
5039 /* If this is a local watchpoint, we only want to check if the
5040 watchpoint frame is in scope if the current thread is the thread
5041 that was used to create the watchpoint. */
5042 if (!watchpoint_in_thread_scope (b))
60e1c644 5043 return WP_IGNORE;
f6bc2008 5044
c906108c
SS
5045 if (b->exp_valid_block == NULL)
5046 within_current_scope = 1;
5047 else
5048 {
edb3359d
DJ
5049 struct frame_info *frame = get_current_frame ();
5050 struct gdbarch *frame_arch = get_frame_arch (frame);
5051 CORE_ADDR frame_pc = get_frame_pc (frame);
5052
c9cf6e20 5053 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5054 still in the function but the stack frame has already been
5055 invalidated. Since we can't rely on the values of local
5056 variables after the stack has been destroyed, we are treating
5057 the watchpoint in that state as `not changed' without further
5058 checking. Don't mark watchpoints as changed if the current
5059 frame is in an epilogue - even if they are in some other
5060 frame, our view of the stack is likely to be wrong and
5061 frame_find_by_id could error out. */
c9cf6e20 5062 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5063 return WP_IGNORE;
a0f49112 5064
101dcfbe 5065 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5066 within_current_scope = (fr != NULL);
69fbadd5
DJ
5067
5068 /* If we've gotten confused in the unwinder, we might have
5069 returned a frame that can't describe this variable. */
edb3359d
DJ
5070 if (within_current_scope)
5071 {
5072 struct symbol *function;
5073
5074 function = get_frame_function (fr);
5075 if (function == NULL
5076 || !contained_in (b->exp_valid_block,
5077 SYMBOL_BLOCK_VALUE (function)))
5078 within_current_scope = 0;
5079 }
69fbadd5 5080
edb3359d 5081 if (within_current_scope)
c906108c
SS
5082 /* If we end up stopping, the current frame will get selected
5083 in normal_stop. So this call to select_frame won't affect
5084 the user. */
0f7d239c 5085 select_frame (fr);
c906108c 5086 }
c5aa993b 5087
c906108c
SS
5088 if (within_current_scope)
5089 {
4a64f543
MS
5090 /* We use value_{,free_to_}mark because it could be a *long*
5091 time before we return to the command level and call
5092 free_all_values. We can't call free_all_values because we
5093 might be in the middle of evaluating a function call. */
c906108c 5094
0cf6dd15 5095 int pc = 0;
9c06b0b4 5096 struct value *mark;
fa4727a6
DJ
5097 struct value *new_val;
5098
3a5c3e22 5099 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5100 /* Since we don't know the exact trigger address (from
5101 stopped_data_address), just tell the user we've triggered
5102 a mask watchpoint. */
5103 return WP_VALUE_CHANGED;
5104
5105 mark = value_mark ();
3a1115a0 5106 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5107
bb9d5f81
PP
5108 if (b->val_bitsize != 0)
5109 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5110
4a64f543
MS
5111 /* We use value_equal_contents instead of value_equal because
5112 the latter coerces an array to a pointer, thus comparing just
5113 the address of the array instead of its contents. This is
5114 not what we want. */
fa4727a6 5115 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5116 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5117 {
fa4727a6
DJ
5118 if (new_val != NULL)
5119 {
5120 release_value (new_val);
5121 value_free_to_mark (mark);
5122 }
c906108c
SS
5123 bs->old_val = b->val;
5124 b->val = new_val;
fa4727a6 5125 b->val_valid = 1;
c906108c
SS
5126 return WP_VALUE_CHANGED;
5127 }
5128 else
5129 {
60e1c644 5130 /* Nothing changed. */
c906108c 5131 value_free_to_mark (mark);
c906108c
SS
5132 return WP_VALUE_NOT_CHANGED;
5133 }
5134 }
5135 else
5136 {
79a45e25
PA
5137 struct ui_out *uiout = current_uiout;
5138
c906108c 5139 /* This seems like the only logical thing to do because
c5aa993b
JM
5140 if we temporarily ignored the watchpoint, then when
5141 we reenter the block in which it is valid it contains
5142 garbage (in the case of a function, it may have two
5143 garbage values, one before and one after the prologue).
5144 So we can't even detect the first assignment to it and
5145 watch after that (since the garbage may or may not equal
5146 the first value assigned). */
348d480f
PA
5147 /* We print all the stop information in
5148 breakpoint_ops->print_it, but in this case, by the time we
5149 call breakpoint_ops->print_it this bp will be deleted
5150 already. So we have no choice but print the information
5151 here. */
9dc5e2a9 5152 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5153 ui_out_field_string
5154 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5155 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5156 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5157 ui_out_text (uiout,
5158 " deleted because the program has left the block in\n\
8b93c638 5159which its expression is valid.\n");
4ce44c66 5160
cdac0397 5161 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5162 decref_counted_command_line (&b->base.commands);
d0fb5eae 5163 watchpoint_del_at_next_stop (b);
c906108c
SS
5164
5165 return WP_DELETED;
5166 }
5167}
5168
18a18393 5169/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5170 breakpoint location BL. This function does not check if we should
5171 stop, only if BL explains the stop. */
5172
18a18393 5173static int
6c95b8df 5174bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5175 struct address_space *aspace, CORE_ADDR bp_addr,
5176 const struct target_waitstatus *ws)
18a18393
VP
5177{
5178 struct breakpoint *b = bl->owner;
5179
348d480f 5180 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5181 gdb_assert (b != NULL);
5182
09ac7c10 5183 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5184}
5185
3a5c3e22
PA
5186/* Determine if the watched values have actually changed, and we
5187 should stop. If not, set BS->stop to 0. */
5188
18a18393
VP
5189static void
5190bpstat_check_watchpoint (bpstat bs)
5191{
2bdf28a0 5192 const struct bp_location *bl;
3a5c3e22 5193 struct watchpoint *b;
2bdf28a0
JK
5194
5195 /* BS is built for existing struct breakpoint. */
f431efe5 5196 bl = bs->bp_location_at;
2bdf28a0 5197 gdb_assert (bl != NULL);
3a5c3e22 5198 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5199 gdb_assert (b != NULL);
18a18393 5200
18a18393 5201 {
18a18393
VP
5202 int must_check_value = 0;
5203
3a5c3e22 5204 if (b->base.type == bp_watchpoint)
18a18393
VP
5205 /* For a software watchpoint, we must always check the
5206 watched value. */
5207 must_check_value = 1;
5208 else if (b->watchpoint_triggered == watch_triggered_yes)
5209 /* We have a hardware watchpoint (read, write, or access)
5210 and the target earlier reported an address watched by
5211 this watchpoint. */
5212 must_check_value = 1;
5213 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5214 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5215 /* We were stopped by a hardware watchpoint, but the target could
5216 not report the data address. We must check the watchpoint's
5217 value. Access and read watchpoints are out of luck; without
5218 a data address, we can't figure it out. */
5219 must_check_value = 1;
3a5c3e22 5220
18a18393
VP
5221 if (must_check_value)
5222 {
3e43a32a
MS
5223 char *message
5224 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5225 b->base.number);
18a18393
VP
5226 struct cleanup *cleanups = make_cleanup (xfree, message);
5227 int e = catch_errors (watchpoint_check, bs, message,
5228 RETURN_MASK_ALL);
5229 do_cleanups (cleanups);
5230 switch (e)
5231 {
5232 case WP_DELETED:
5233 /* We've already printed what needs to be printed. */
5234 bs->print_it = print_it_done;
5235 /* Stop. */
5236 break;
60e1c644
PA
5237 case WP_IGNORE:
5238 bs->print_it = print_it_noop;
5239 bs->stop = 0;
5240 break;
18a18393 5241 case WP_VALUE_CHANGED:
3a5c3e22 5242 if (b->base.type == bp_read_watchpoint)
18a18393 5243 {
85d721b8
PA
5244 /* There are two cases to consider here:
5245
4a64f543 5246 1. We're watching the triggered memory for reads.
85d721b8
PA
5247 In that case, trust the target, and always report
5248 the watchpoint hit to the user. Even though
5249 reads don't cause value changes, the value may
5250 have changed since the last time it was read, and
5251 since we're not trapping writes, we will not see
5252 those, and as such we should ignore our notion of
5253 old value.
5254
4a64f543 5255 2. We're watching the triggered memory for both
85d721b8
PA
5256 reads and writes. There are two ways this may
5257 happen:
5258
4a64f543 5259 2.1. This is a target that can't break on data
85d721b8
PA
5260 reads only, but can break on accesses (reads or
5261 writes), such as e.g., x86. We detect this case
5262 at the time we try to insert read watchpoints.
5263
4a64f543 5264 2.2. Otherwise, the target supports read
85d721b8
PA
5265 watchpoints, but, the user set an access or write
5266 watchpoint watching the same memory as this read
5267 watchpoint.
5268
5269 If we're watching memory writes as well as reads,
5270 ignore watchpoint hits when we find that the
5271 value hasn't changed, as reads don't cause
5272 changes. This still gives false positives when
5273 the program writes the same value to memory as
5274 what there was already in memory (we will confuse
5275 it for a read), but it's much better than
5276 nothing. */
5277
5278 int other_write_watchpoint = 0;
5279
5280 if (bl->watchpoint_type == hw_read)
5281 {
5282 struct breakpoint *other_b;
5283
5284 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5285 if (other_b->type == bp_hardware_watchpoint
5286 || other_b->type == bp_access_watchpoint)
85d721b8 5287 {
3a5c3e22
PA
5288 struct watchpoint *other_w =
5289 (struct watchpoint *) other_b;
5290
5291 if (other_w->watchpoint_triggered
5292 == watch_triggered_yes)
5293 {
5294 other_write_watchpoint = 1;
5295 break;
5296 }
85d721b8
PA
5297 }
5298 }
5299
5300 if (other_write_watchpoint
5301 || bl->watchpoint_type == hw_access)
5302 {
5303 /* We're watching the same memory for writes,
5304 and the value changed since the last time we
5305 updated it, so this trap must be for a write.
5306 Ignore it. */
5307 bs->print_it = print_it_noop;
5308 bs->stop = 0;
5309 }
18a18393
VP
5310 }
5311 break;
5312 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5313 if (b->base.type == bp_hardware_watchpoint
5314 || b->base.type == bp_watchpoint)
18a18393
VP
5315 {
5316 /* Don't stop: write watchpoints shouldn't fire if
5317 the value hasn't changed. */
5318 bs->print_it = print_it_noop;
5319 bs->stop = 0;
5320 }
5321 /* Stop. */
5322 break;
5323 default:
5324 /* Can't happen. */
5325 case 0:
5326 /* Error from catch_errors. */
3a5c3e22 5327 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5328 watchpoint_del_at_next_stop (b);
18a18393
VP
5329 /* We've already printed what needs to be printed. */
5330 bs->print_it = print_it_done;
5331 break;
5332 }
5333 }
5334 else /* must_check_value == 0 */
5335 {
5336 /* This is a case where some watchpoint(s) triggered, but
5337 not at the address of this watchpoint, or else no
5338 watchpoint triggered after all. So don't print
5339 anything for this watchpoint. */
5340 bs->print_it = print_it_noop;
5341 bs->stop = 0;
5342 }
5343 }
5344}
5345
7d4df6a4
DE
5346/* For breakpoints that are currently marked as telling gdb to stop,
5347 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5348 of breakpoint referred to by BS. If we should not stop for this
5349 breakpoint, set BS->stop to 0. */
f431efe5 5350
18a18393
VP
5351static void
5352bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5353{
2bdf28a0
JK
5354 const struct bp_location *bl;
5355 struct breakpoint *b;
7d4df6a4
DE
5356 int value_is_zero = 0;
5357 struct expression *cond;
5358
5359 gdb_assert (bs->stop);
2bdf28a0
JK
5360
5361 /* BS is built for existing struct breakpoint. */
f431efe5 5362 bl = bs->bp_location_at;
2bdf28a0 5363 gdb_assert (bl != NULL);
f431efe5 5364 b = bs->breakpoint_at;
2bdf28a0 5365 gdb_assert (b != NULL);
18a18393 5366
b775012e
LM
5367 /* Even if the target evaluated the condition on its end and notified GDB, we
5368 need to do so again since GDB does not know if we stopped due to a
5369 breakpoint or a single step breakpoint. */
5370
18a18393 5371 if (frame_id_p (b->frame_id)
edb3359d 5372 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5373 {
7d4df6a4
DE
5374 bs->stop = 0;
5375 return;
5376 }
60e1c644 5377
12ab52e9
PA
5378 /* If this is a thread/task-specific breakpoint, don't waste cpu
5379 evaluating the condition if this isn't the specified
5380 thread/task. */
5381 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5382 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5383
6c1b0f7b
DE
5384 {
5385 bs->stop = 0;
5386 return;
5387 }
5388
6dddc817
DE
5389 /* Evaluate extension language breakpoints that have a "stop" method
5390 implemented. */
5391 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5392
7d4df6a4
DE
5393 if (is_watchpoint (b))
5394 {
5395 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5396
7d4df6a4
DE
5397 cond = w->cond_exp;
5398 }
5399 else
5400 cond = bl->cond;
60e1c644 5401
7d4df6a4
DE
5402 if (cond && b->disposition != disp_del_at_next_stop)
5403 {
5404 int within_current_scope = 1;
5405 struct watchpoint * w;
60e1c644 5406
7d4df6a4
DE
5407 /* We use value_mark and value_free_to_mark because it could
5408 be a long time before we return to the command level and
5409 call free_all_values. We can't call free_all_values
5410 because we might be in the middle of evaluating a
5411 function call. */
5412 struct value *mark = value_mark ();
5413
5414 if (is_watchpoint (b))
5415 w = (struct watchpoint *) b;
5416 else
5417 w = NULL;
5418
5419 /* Need to select the frame, with all that implies so that
5420 the conditions will have the right context. Because we
5421 use the frame, we will not see an inlined function's
5422 variables when we arrive at a breakpoint at the start
5423 of the inlined function; the current frame will be the
5424 call site. */
5425 if (w == NULL || w->cond_exp_valid_block == NULL)
5426 select_frame (get_current_frame ());
5427 else
18a18393 5428 {
7d4df6a4
DE
5429 struct frame_info *frame;
5430
5431 /* For local watchpoint expressions, which particular
5432 instance of a local is being watched matters, so we
5433 keep track of the frame to evaluate the expression
5434 in. To evaluate the condition however, it doesn't
5435 really matter which instantiation of the function
5436 where the condition makes sense triggers the
5437 watchpoint. This allows an expression like "watch
5438 global if q > 10" set in `func', catch writes to
5439 global on all threads that call `func', or catch
5440 writes on all recursive calls of `func' by a single
5441 thread. We simply always evaluate the condition in
5442 the innermost frame that's executing where it makes
5443 sense to evaluate the condition. It seems
5444 intuitive. */
5445 frame = block_innermost_frame (w->cond_exp_valid_block);
5446 if (frame != NULL)
5447 select_frame (frame);
5448 else
5449 within_current_scope = 0;
18a18393 5450 }
7d4df6a4
DE
5451 if (within_current_scope)
5452 value_is_zero
5453 = catch_errors (breakpoint_cond_eval, cond,
5454 "Error in testing breakpoint condition:\n",
5455 RETURN_MASK_ALL);
5456 else
18a18393 5457 {
7d4df6a4
DE
5458 warning (_("Watchpoint condition cannot be tested "
5459 "in the current scope"));
5460 /* If we failed to set the right context for this
5461 watchpoint, unconditionally report it. */
5462 value_is_zero = 0;
18a18393 5463 }
7d4df6a4
DE
5464 /* FIXME-someday, should give breakpoint #. */
5465 value_free_to_mark (mark);
18a18393 5466 }
7d4df6a4
DE
5467
5468 if (cond && value_is_zero)
5469 {
5470 bs->stop = 0;
5471 }
7d4df6a4
DE
5472 else if (b->ignore_count > 0)
5473 {
5474 b->ignore_count--;
5475 bs->stop = 0;
5476 /* Increase the hit count even though we don't stop. */
5477 ++(b->hit_count);
5478 observer_notify_breakpoint_modified (b);
5479 }
18a18393
VP
5480}
5481
1cf4d951
PA
5482/* Returns true if we need to track moribund locations of LOC's type
5483 on the current target. */
5484
5485static int
5486need_moribund_for_location_type (struct bp_location *loc)
5487{
5488 return ((loc->loc_type == bp_loc_software_breakpoint
5489 && !target_supports_stopped_by_sw_breakpoint ())
5490 || (loc->loc_type == bp_loc_hardware_breakpoint
5491 && !target_supports_stopped_by_hw_breakpoint ()));
5492}
5493
18a18393 5494
9709f61c 5495/* Get a bpstat associated with having just stopped at address
d983da9c 5496 BP_ADDR in thread PTID.
c906108c 5497
d983da9c 5498 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5499 don't understand this stop. Result is a chain of bpstat's such
5500 that:
c906108c 5501
c5aa993b 5502 if we don't understand the stop, the result is a null pointer.
c906108c 5503
c5aa993b 5504 if we understand why we stopped, the result is not null.
c906108c 5505
c5aa993b
JM
5506 Each element of the chain refers to a particular breakpoint or
5507 watchpoint at which we have stopped. (We may have stopped for
5508 several reasons concurrently.)
c906108c 5509
c5aa993b
JM
5510 Each element of the chain has valid next, breakpoint_at,
5511 commands, FIXME??? fields. */
c906108c
SS
5512
5513bpstat
6c95b8df 5514bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5515 CORE_ADDR bp_addr, ptid_t ptid,
5516 const struct target_waitstatus *ws)
c906108c 5517{
0d381245 5518 struct breakpoint *b = NULL;
afe38095 5519 struct bp_location *bl;
20874c92 5520 struct bp_location *loc;
5760d0ab
JK
5521 /* First item of allocated bpstat's. */
5522 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5523 /* Pointer to the last thing in the chain currently. */
5760d0ab 5524 bpstat bs;
20874c92 5525 int ix;
429374b8 5526 int need_remove_insert;
f431efe5 5527 int removed_any;
c906108c 5528
f431efe5
PA
5529 /* First, build the bpstat chain with locations that explain a
5530 target stop, while being careful to not set the target running,
5531 as that may invalidate locations (in particular watchpoint
5532 locations are recreated). Resuming will happen here with
5533 breakpoint conditions or watchpoint expressions that include
5534 inferior function calls. */
c5aa993b 5535
429374b8
JK
5536 ALL_BREAKPOINTS (b)
5537 {
1a853c52 5538 if (!breakpoint_enabled (b))
429374b8 5539 continue;
a5606eee 5540
429374b8
JK
5541 for (bl = b->loc; bl != NULL; bl = bl->next)
5542 {
4a64f543
MS
5543 /* For hardware watchpoints, we look only at the first
5544 location. The watchpoint_check function will work on the
5545 entire expression, not the individual locations. For
5546 read watchpoints, the watchpoints_triggered function has
5547 checked all locations already. */
429374b8
JK
5548 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5549 break;
18a18393 5550
f6592439 5551 if (!bl->enabled || bl->shlib_disabled)
429374b8 5552 continue;
c5aa993b 5553
09ac7c10 5554 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5555 continue;
c5aa993b 5556
4a64f543
MS
5557 /* Come here if it's a watchpoint, or if the break address
5558 matches. */
c5aa993b 5559
4a64f543
MS
5560 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5561 explain stop. */
c5aa993b 5562
f431efe5
PA
5563 /* Assume we stop. Should we find a watchpoint that is not
5564 actually triggered, or if the condition of the breakpoint
5565 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5566 bs->stop = 1;
5567 bs->print = 1;
d983da9c 5568
f431efe5
PA
5569 /* If this is a scope breakpoint, mark the associated
5570 watchpoint as triggered so that we will handle the
5571 out-of-scope event. We'll get to the watchpoint next
5572 iteration. */
d0fb5eae 5573 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5574 {
5575 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5576
5577 w->watchpoint_triggered = watch_triggered_yes;
5578 }
f431efe5
PA
5579 }
5580 }
5581
7c16b83e 5582 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5583 if (!target_supports_stopped_by_sw_breakpoint ()
5584 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5585 {
1cf4d951 5586 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5587 {
1cf4d951
PA
5588 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5589 && need_moribund_for_location_type (loc))
5590 {
5591 bs = bpstat_alloc (loc, &bs_link);
5592 /* For hits of moribund locations, we should just proceed. */
5593 bs->stop = 0;
5594 bs->print = 0;
5595 bs->print_it = print_it_noop;
5596 }
f431efe5
PA
5597 }
5598 }
5599
edcc5120
TT
5600 /* A bit of special processing for shlib breakpoints. We need to
5601 process solib loading here, so that the lists of loaded and
5602 unloaded libraries are correct before we handle "catch load" and
5603 "catch unload". */
5604 for (bs = bs_head; bs != NULL; bs = bs->next)
5605 {
5d268276 5606 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5607 {
5608 handle_solib_event ();
5609 break;
5610 }
5611 }
5612
f431efe5
PA
5613 /* Now go through the locations that caused the target to stop, and
5614 check whether we're interested in reporting this stop to higher
5615 layers, or whether we should resume the target transparently. */
5616
5617 removed_any = 0;
5618
5760d0ab 5619 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5620 {
5621 if (!bs->stop)
5622 continue;
5623
f431efe5 5624 b = bs->breakpoint_at;
348d480f
PA
5625 b->ops->check_status (bs);
5626 if (bs->stop)
28010a5d 5627 {
348d480f 5628 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5629
429374b8
JK
5630 if (bs->stop)
5631 {
5632 ++(b->hit_count);
8d3788bd 5633 observer_notify_breakpoint_modified (b);
c906108c 5634
4a64f543 5635 /* We will stop here. */
429374b8
JK
5636 if (b->disposition == disp_disable)
5637 {
816338b5 5638 --(b->enable_count);
1a853c52 5639 if (b->enable_count <= 0)
429374b8 5640 b->enable_state = bp_disabled;
f431efe5 5641 removed_any = 1;
429374b8
JK
5642 }
5643 if (b->silent)
5644 bs->print = 0;
5645 bs->commands = b->commands;
9add0f1b 5646 incref_counted_command_line (bs->commands);
abf85f46
JK
5647 if (command_line_is_silent (bs->commands
5648 ? bs->commands->commands : NULL))
5649 bs->print = 0;
9d6e6e84
HZ
5650
5651 b->ops->after_condition_true (bs);
429374b8
JK
5652 }
5653
348d480f 5654 }
a9b3a50f
PA
5655
5656 /* Print nothing for this entry if we don't stop or don't
5657 print. */
5658 if (!bs->stop || !bs->print)
5659 bs->print_it = print_it_noop;
429374b8 5660 }
876fa593 5661
d983da9c
DJ
5662 /* If we aren't stopping, the value of some hardware watchpoint may
5663 not have changed, but the intermediate memory locations we are
5664 watching may have. Don't bother if we're stopping; this will get
5665 done later. */
d832cb68 5666 need_remove_insert = 0;
5760d0ab
JK
5667 if (! bpstat_causes_stop (bs_head))
5668 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5669 if (!bs->stop
f431efe5
PA
5670 && bs->breakpoint_at
5671 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5672 {
3a5c3e22
PA
5673 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5674
5675 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5676 need_remove_insert = 1;
d983da9c
DJ
5677 }
5678
d832cb68 5679 if (need_remove_insert)
44702360 5680 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5681 else if (removed_any)
44702360 5682 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5683
5760d0ab 5684 return bs_head;
c906108c 5685}
628fe4e4
JK
5686
5687static void
5688handle_jit_event (void)
5689{
5690 struct frame_info *frame;
5691 struct gdbarch *gdbarch;
5692
5693 /* Switch terminal for any messages produced by
5694 breakpoint_re_set. */
5695 target_terminal_ours_for_output ();
5696
5697 frame = get_current_frame ();
5698 gdbarch = get_frame_arch (frame);
5699
5700 jit_event_handler (gdbarch);
5701
5702 target_terminal_inferior ();
5703}
5704
5705/* Prepare WHAT final decision for infrun. */
5706
5707/* Decide what infrun needs to do with this bpstat. */
5708
c906108c 5709struct bpstat_what
0e30163f 5710bpstat_what (bpstat bs_head)
c906108c 5711{
c906108c 5712 struct bpstat_what retval;
628fe4e4 5713 int jit_event = 0;
0e30163f 5714 bpstat bs;
c906108c 5715
628fe4e4 5716 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5717 retval.call_dummy = STOP_NONE;
186c406b 5718 retval.is_longjmp = 0;
628fe4e4 5719
0e30163f 5720 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5721 {
628fe4e4
JK
5722 /* Extract this BS's action. After processing each BS, we check
5723 if its action overrides all we've seem so far. */
5724 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5725 enum bptype bptype;
5726
c906108c 5727 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5728 {
5729 /* I suspect this can happen if it was a momentary
5730 breakpoint which has since been deleted. */
5731 bptype = bp_none;
5732 }
20874c92 5733 else
f431efe5 5734 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5735
5736 switch (bptype)
c906108c
SS
5737 {
5738 case bp_none:
628fe4e4 5739 break;
c906108c
SS
5740 case bp_breakpoint:
5741 case bp_hardware_breakpoint:
7c16b83e 5742 case bp_single_step:
c906108c
SS
5743 case bp_until:
5744 case bp_finish:
a9b3a50f 5745 case bp_shlib_event:
c906108c
SS
5746 if (bs->stop)
5747 {
5748 if (bs->print)
628fe4e4 5749 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5750 else
628fe4e4 5751 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5752 }
5753 else
628fe4e4 5754 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5755 break;
5756 case bp_watchpoint:
5757 case bp_hardware_watchpoint:
5758 case bp_read_watchpoint:
5759 case bp_access_watchpoint:
5760 if (bs->stop)
5761 {
5762 if (bs->print)
628fe4e4 5763 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5764 else
628fe4e4 5765 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5766 }
5767 else
628fe4e4
JK
5768 {
5769 /* There was a watchpoint, but we're not stopping.
5770 This requires no further action. */
5771 }
c906108c
SS
5772 break;
5773 case bp_longjmp:
e2e4d78b 5774 case bp_longjmp_call_dummy:
186c406b 5775 case bp_exception:
0a39bb32
PA
5776 if (bs->stop)
5777 {
5778 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5779 retval.is_longjmp = bptype != bp_exception;
5780 }
5781 else
5782 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5783 break;
5784 case bp_longjmp_resume:
186c406b 5785 case bp_exception_resume:
0a39bb32
PA
5786 if (bs->stop)
5787 {
5788 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5789 retval.is_longjmp = bptype == bp_longjmp_resume;
5790 }
5791 else
5792 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5793 break;
5794 case bp_step_resume:
5795 if (bs->stop)
628fe4e4
JK
5796 this_action = BPSTAT_WHAT_STEP_RESUME;
5797 else
c906108c 5798 {
628fe4e4
JK
5799 /* It is for the wrong frame. */
5800 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5801 }
c906108c 5802 break;
2c03e5be
PA
5803 case bp_hp_step_resume:
5804 if (bs->stop)
5805 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5806 else
5807 {
5808 /* It is for the wrong frame. */
5809 this_action = BPSTAT_WHAT_SINGLE;
5810 }
5811 break;
c906108c 5812 case bp_watchpoint_scope:
c4093a6a 5813 case bp_thread_event:
1900040c 5814 case bp_overlay_event:
0fd8e87f 5815 case bp_longjmp_master:
aa7d318d 5816 case bp_std_terminate_master:
186c406b 5817 case bp_exception_master:
628fe4e4 5818 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5819 break;
ce78b96d 5820 case bp_catchpoint:
c5aa993b
JM
5821 if (bs->stop)
5822 {
5823 if (bs->print)
628fe4e4 5824 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5825 else
628fe4e4 5826 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5827 }
5828 else
628fe4e4
JK
5829 {
5830 /* There was a catchpoint, but we're not stopping.
5831 This requires no further action. */
5832 }
5833 break;
628fe4e4
JK
5834 case bp_jit_event:
5835 jit_event = 1;
5836 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5837 break;
c906108c 5838 case bp_call_dummy:
53a5351d
JM
5839 /* Make sure the action is stop (silent or noisy),
5840 so infrun.c pops the dummy frame. */
aa7d318d 5841 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5842 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5843 break;
5844 case bp_std_terminate:
5845 /* Make sure the action is stop (silent or noisy),
5846 so infrun.c pops the dummy frame. */
aa7d318d 5847 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5848 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5849 break;
1042e4c0 5850 case bp_tracepoint:
7a697b8d 5851 case bp_fast_tracepoint:
0fb4aa4b 5852 case bp_static_tracepoint:
1042e4c0
SS
5853 /* Tracepoint hits should not be reported back to GDB, and
5854 if one got through somehow, it should have been filtered
5855 out already. */
5856 internal_error (__FILE__, __LINE__,
7a697b8d 5857 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5858 break;
5859 case bp_gnu_ifunc_resolver:
5860 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5861 this_action = BPSTAT_WHAT_SINGLE;
5862 break;
5863 case bp_gnu_ifunc_resolver_return:
5864 /* The breakpoint will be removed, execution will restart from the
5865 PC of the former breakpoint. */
5866 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5867 break;
e7e0cddf
SS
5868
5869 case bp_dprintf:
a11cfd87
HZ
5870 if (bs->stop)
5871 this_action = BPSTAT_WHAT_STOP_SILENT;
5872 else
5873 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5874 break;
5875
628fe4e4
JK
5876 default:
5877 internal_error (__FILE__, __LINE__,
5878 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5879 }
628fe4e4
JK
5880
5881 retval.main_action = max (retval.main_action, this_action);
c906108c 5882 }
628fe4e4 5883
0e30163f
JK
5884 /* These operations may affect the bs->breakpoint_at state so they are
5885 delayed after MAIN_ACTION is decided above. */
5886
628fe4e4
JK
5887 if (jit_event)
5888 {
5889 if (debug_infrun)
5890 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5891
5892 handle_jit_event ();
5893 }
5894
0e30163f
JK
5895 for (bs = bs_head; bs != NULL; bs = bs->next)
5896 {
5897 struct breakpoint *b = bs->breakpoint_at;
5898
5899 if (b == NULL)
5900 continue;
5901 switch (b->type)
5902 {
5903 case bp_gnu_ifunc_resolver:
5904 gnu_ifunc_resolver_stop (b);
5905 break;
5906 case bp_gnu_ifunc_resolver_return:
5907 gnu_ifunc_resolver_return_stop (b);
5908 break;
5909 }
5910 }
5911
c906108c
SS
5912 return retval;
5913}
5914
5915/* Nonzero if we should step constantly (e.g. watchpoints on machines
5916 without hardware support). This isn't related to a specific bpstat,
5917 just to things like whether watchpoints are set. */
5918
c5aa993b 5919int
fba45db2 5920bpstat_should_step (void)
c906108c
SS
5921{
5922 struct breakpoint *b;
cc59ec59 5923
c906108c 5924 ALL_BREAKPOINTS (b)
717a8278 5925 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5926 return 1;
c906108c
SS
5927 return 0;
5928}
5929
67822962
PA
5930int
5931bpstat_causes_stop (bpstat bs)
5932{
5933 for (; bs != NULL; bs = bs->next)
5934 if (bs->stop)
5935 return 1;
5936
5937 return 0;
5938}
5939
c906108c 5940\f
c5aa993b 5941
170b53b2
UW
5942/* Compute a string of spaces suitable to indent the next line
5943 so it starts at the position corresponding to the table column
5944 named COL_NAME in the currently active table of UIOUT. */
5945
5946static char *
5947wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5948{
5949 static char wrap_indent[80];
5950 int i, total_width, width, align;
5951 char *text;
5952
5953 total_width = 0;
5954 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5955 {
5956 if (strcmp (text, col_name) == 0)
5957 {
5958 gdb_assert (total_width < sizeof wrap_indent);
5959 memset (wrap_indent, ' ', total_width);
5960 wrap_indent[total_width] = 0;
5961
5962 return wrap_indent;
5963 }
5964
5965 total_width += width + 1;
5966 }
5967
5968 return NULL;
5969}
5970
b775012e
LM
5971/* Determine if the locations of this breakpoint will have their conditions
5972 evaluated by the target, host or a mix of both. Returns the following:
5973
5974 "host": Host evals condition.
5975 "host or target": Host or Target evals condition.
5976 "target": Target evals condition.
5977*/
5978
5979static const char *
5980bp_condition_evaluator (struct breakpoint *b)
5981{
5982 struct bp_location *bl;
5983 char host_evals = 0;
5984 char target_evals = 0;
5985
5986 if (!b)
5987 return NULL;
5988
5989 if (!is_breakpoint (b))
5990 return NULL;
5991
5992 if (gdb_evaluates_breakpoint_condition_p ()
5993 || !target_supports_evaluation_of_breakpoint_conditions ())
5994 return condition_evaluation_host;
5995
5996 for (bl = b->loc; bl; bl = bl->next)
5997 {
5998 if (bl->cond_bytecode)
5999 target_evals++;
6000 else
6001 host_evals++;
6002 }
6003
6004 if (host_evals && target_evals)
6005 return condition_evaluation_both;
6006 else if (target_evals)
6007 return condition_evaluation_target;
6008 else
6009 return condition_evaluation_host;
6010}
6011
6012/* Determine the breakpoint location's condition evaluator. This is
6013 similar to bp_condition_evaluator, but for locations. */
6014
6015static const char *
6016bp_location_condition_evaluator (struct bp_location *bl)
6017{
6018 if (bl && !is_breakpoint (bl->owner))
6019 return NULL;
6020
6021 if (gdb_evaluates_breakpoint_condition_p ()
6022 || !target_supports_evaluation_of_breakpoint_conditions ())
6023 return condition_evaluation_host;
6024
6025 if (bl && bl->cond_bytecode)
6026 return condition_evaluation_target;
6027 else
6028 return condition_evaluation_host;
6029}
6030
859825b8
JK
6031/* Print the LOC location out of the list of B->LOC locations. */
6032
170b53b2
UW
6033static void
6034print_breakpoint_location (struct breakpoint *b,
6035 struct bp_location *loc)
0d381245 6036{
79a45e25 6037 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6038 struct cleanup *old_chain = save_current_program_space ();
6039
859825b8
JK
6040 if (loc != NULL && loc->shlib_disabled)
6041 loc = NULL;
6042
6c95b8df
PA
6043 if (loc != NULL)
6044 set_current_program_space (loc->pspace);
6045
56435ebe 6046 if (b->display_canonical)
f00aae0f
KS
6047 ui_out_field_string (uiout, "what",
6048 event_location_to_string (b->location));
2f202fde 6049 else if (loc && loc->symtab)
0d381245
VP
6050 {
6051 struct symbol *sym
6052 = find_pc_sect_function (loc->address, loc->section);
6053 if (sym)
6054 {
6055 ui_out_text (uiout, "in ");
6056 ui_out_field_string (uiout, "func",
6057 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6058 ui_out_text (uiout, " ");
6059 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6060 ui_out_text (uiout, "at ");
0d381245 6061 }
05cba821
JK
6062 ui_out_field_string (uiout, "file",
6063 symtab_to_filename_for_display (loc->symtab));
0d381245 6064 ui_out_text (uiout, ":");
05cba821 6065
0d381245 6066 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6067 ui_out_field_string (uiout, "fullname",
6068 symtab_to_fullname (loc->symtab));
0d381245 6069
f8eba3c6 6070 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6071 }
859825b8 6072 else if (loc)
0d381245 6073 {
f99d8bf4
PA
6074 struct ui_file *stb = mem_fileopen ();
6075 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6076
f99d8bf4 6077 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6078 demangle, "");
0d381245 6079 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6080
6081 do_cleanups (stb_chain);
0d381245 6082 }
859825b8 6083 else
f00aae0f
KS
6084 {
6085 ui_out_field_string (uiout, "pending",
6086 event_location_to_string (b->location));
6087 /* If extra_string is available, it could be holding a condition
6088 or dprintf arguments. In either case, make sure it is printed,
6089 too, but only for non-MI streams. */
6090 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6091 {
6092 if (b->type == bp_dprintf)
6093 ui_out_text (uiout, ",");
6094 else
6095 ui_out_text (uiout, " ");
6096 ui_out_text (uiout, b->extra_string);
6097 }
6098 }
6c95b8df 6099
b775012e
LM
6100 if (loc && is_breakpoint (b)
6101 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6102 && bp_condition_evaluator (b) == condition_evaluation_both)
6103 {
6104 ui_out_text (uiout, " (");
6105 ui_out_field_string (uiout, "evaluated-by",
6106 bp_location_condition_evaluator (loc));
6107 ui_out_text (uiout, ")");
6108 }
6109
6c95b8df 6110 do_cleanups (old_chain);
0d381245
VP
6111}
6112
269b11a2
PA
6113static const char *
6114bptype_string (enum bptype type)
c906108c 6115{
c4093a6a
JM
6116 struct ep_type_description
6117 {
6118 enum bptype type;
6119 char *description;
6120 };
6121 static struct ep_type_description bptypes[] =
c906108c 6122 {
c5aa993b
JM
6123 {bp_none, "?deleted?"},
6124 {bp_breakpoint, "breakpoint"},
c906108c 6125 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6126 {bp_single_step, "sw single-step"},
c5aa993b
JM
6127 {bp_until, "until"},
6128 {bp_finish, "finish"},
6129 {bp_watchpoint, "watchpoint"},
c906108c 6130 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6131 {bp_read_watchpoint, "read watchpoint"},
6132 {bp_access_watchpoint, "acc watchpoint"},
6133 {bp_longjmp, "longjmp"},
6134 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6135 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6136 {bp_exception, "exception"},
6137 {bp_exception_resume, "exception resume"},
c5aa993b 6138 {bp_step_resume, "step resume"},
2c03e5be 6139 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6140 {bp_watchpoint_scope, "watchpoint scope"},
6141 {bp_call_dummy, "call dummy"},
aa7d318d 6142 {bp_std_terminate, "std::terminate"},
c5aa993b 6143 {bp_shlib_event, "shlib events"},
c4093a6a 6144 {bp_thread_event, "thread events"},
1900040c 6145 {bp_overlay_event, "overlay events"},
0fd8e87f 6146 {bp_longjmp_master, "longjmp master"},
aa7d318d 6147 {bp_std_terminate_master, "std::terminate master"},
186c406b 6148 {bp_exception_master, "exception master"},
ce78b96d 6149 {bp_catchpoint, "catchpoint"},
1042e4c0 6150 {bp_tracepoint, "tracepoint"},
7a697b8d 6151 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6152 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6153 {bp_dprintf, "dprintf"},
4efc6507 6154 {bp_jit_event, "jit events"},
0e30163f
JK
6155 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6156 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6157 };
269b11a2
PA
6158
6159 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6160 || ((int) type != bptypes[(int) type].type))
6161 internal_error (__FILE__, __LINE__,
6162 _("bptypes table does not describe type #%d."),
6163 (int) type);
6164
6165 return bptypes[(int) type].description;
6166}
6167
998580f1
MK
6168/* For MI, output a field named 'thread-groups' with a list as the value.
6169 For CLI, prefix the list with the string 'inf'. */
6170
6171static void
6172output_thread_groups (struct ui_out *uiout,
6173 const char *field_name,
6174 VEC(int) *inf_num,
6175 int mi_only)
6176{
752eb8b4 6177 struct cleanup *back_to;
998580f1
MK
6178 int is_mi = ui_out_is_mi_like_p (uiout);
6179 int inf;
6180 int i;
6181
6182 /* For backward compatibility, don't display inferiors in CLI unless
6183 there are several. Always display them for MI. */
6184 if (!is_mi && mi_only)
6185 return;
6186
752eb8b4
TT
6187 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6188
998580f1
MK
6189 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6190 {
6191 if (is_mi)
6192 {
6193 char mi_group[10];
6194
6195 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6196 ui_out_field_string (uiout, NULL, mi_group);
6197 }
6198 else
6199 {
6200 if (i == 0)
6201 ui_out_text (uiout, " inf ");
6202 else
6203 ui_out_text (uiout, ", ");
6204
6205 ui_out_text (uiout, plongest (inf));
6206 }
6207 }
6208
6209 do_cleanups (back_to);
6210}
6211
269b11a2
PA
6212/* Print B to gdb_stdout. */
6213
6214static void
6215print_one_breakpoint_location (struct breakpoint *b,
6216 struct bp_location *loc,
6217 int loc_number,
6218 struct bp_location **last_loc,
269b11a2
PA
6219 int allflag)
6220{
6221 struct command_line *l;
c2c6d25f 6222 static char bpenables[] = "nynny";
c906108c 6223
79a45e25 6224 struct ui_out *uiout = current_uiout;
0d381245
VP
6225 int header_of_multiple = 0;
6226 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6227 struct value_print_options opts;
6228
6229 get_user_print_options (&opts);
0d381245
VP
6230
6231 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6232 /* See comment in print_one_breakpoint concerning treatment of
6233 breakpoints with single disabled location. */
0d381245
VP
6234 if (loc == NULL
6235 && (b->loc != NULL
6236 && (b->loc->next != NULL || !b->loc->enabled)))
6237 header_of_multiple = 1;
6238 if (loc == NULL)
6239 loc = b->loc;
6240
c4093a6a
JM
6241 annotate_record ();
6242
6243 /* 1 */
6244 annotate_field (0);
0d381245
VP
6245 if (part_of_multiple)
6246 {
6247 char *formatted;
0c6773c1 6248 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6249 ui_out_field_string (uiout, "number", formatted);
6250 xfree (formatted);
6251 }
6252 else
6253 {
6254 ui_out_field_int (uiout, "number", b->number);
6255 }
c4093a6a
JM
6256
6257 /* 2 */
6258 annotate_field (1);
0d381245
VP
6259 if (part_of_multiple)
6260 ui_out_field_skip (uiout, "type");
269b11a2
PA
6261 else
6262 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6263
6264 /* 3 */
6265 annotate_field (2);
0d381245
VP
6266 if (part_of_multiple)
6267 ui_out_field_skip (uiout, "disp");
6268 else
2cec12e5 6269 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6270
c4093a6a
JM
6271
6272 /* 4 */
6273 annotate_field (3);
0d381245 6274 if (part_of_multiple)
54e52265 6275 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6276 else
4a64f543
MS
6277 ui_out_field_fmt (uiout, "enabled", "%c",
6278 bpenables[(int) b->enable_state]);
54e52265 6279 ui_out_spaces (uiout, 2);
0d381245 6280
c4093a6a
JM
6281
6282 /* 5 and 6 */
3086aeae 6283 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6284 {
4a64f543
MS
6285 /* Although the print_one can possibly print all locations,
6286 calling it here is not likely to get any nice result. So,
6287 make sure there's just one location. */
0d381245 6288 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6289 b->ops->print_one (b, last_loc);
0d381245 6290 }
3086aeae
DJ
6291 else
6292 switch (b->type)
6293 {
6294 case bp_none:
6295 internal_error (__FILE__, __LINE__,
e2e0b3e5 6296 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6297 break;
c906108c 6298
3086aeae
DJ
6299 case bp_watchpoint:
6300 case bp_hardware_watchpoint:
6301 case bp_read_watchpoint:
6302 case bp_access_watchpoint:
3a5c3e22
PA
6303 {
6304 struct watchpoint *w = (struct watchpoint *) b;
6305
6306 /* Field 4, the address, is omitted (which makes the columns
6307 not line up too nicely with the headers, but the effect
6308 is relatively readable). */
6309 if (opts.addressprint)
6310 ui_out_field_skip (uiout, "addr");
6311 annotate_field (5);
6312 ui_out_field_string (uiout, "what", w->exp_string);
6313 }
3086aeae
DJ
6314 break;
6315
3086aeae
DJ
6316 case bp_breakpoint:
6317 case bp_hardware_breakpoint:
7c16b83e 6318 case bp_single_step:
3086aeae
DJ
6319 case bp_until:
6320 case bp_finish:
6321 case bp_longjmp:
6322 case bp_longjmp_resume:
e2e4d78b 6323 case bp_longjmp_call_dummy:
186c406b
TT
6324 case bp_exception:
6325 case bp_exception_resume:
3086aeae 6326 case bp_step_resume:
2c03e5be 6327 case bp_hp_step_resume:
3086aeae
DJ
6328 case bp_watchpoint_scope:
6329 case bp_call_dummy:
aa7d318d 6330 case bp_std_terminate:
3086aeae
DJ
6331 case bp_shlib_event:
6332 case bp_thread_event:
6333 case bp_overlay_event:
0fd8e87f 6334 case bp_longjmp_master:
aa7d318d 6335 case bp_std_terminate_master:
186c406b 6336 case bp_exception_master:
1042e4c0 6337 case bp_tracepoint:
7a697b8d 6338 case bp_fast_tracepoint:
0fb4aa4b 6339 case bp_static_tracepoint:
e7e0cddf 6340 case bp_dprintf:
4efc6507 6341 case bp_jit_event:
0e30163f
JK
6342 case bp_gnu_ifunc_resolver:
6343 case bp_gnu_ifunc_resolver_return:
79a45b7d 6344 if (opts.addressprint)
3086aeae
DJ
6345 {
6346 annotate_field (4);
54e52265 6347 if (header_of_multiple)
0d381245 6348 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6349 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6350 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6351 else
5af949e3
UW
6352 ui_out_field_core_addr (uiout, "addr",
6353 loc->gdbarch, loc->address);
3086aeae
DJ
6354 }
6355 annotate_field (5);
0d381245 6356 if (!header_of_multiple)
170b53b2 6357 print_breakpoint_location (b, loc);
0d381245 6358 if (b->loc)
a6d9a66e 6359 *last_loc = b->loc;
3086aeae
DJ
6360 break;
6361 }
c906108c 6362
6c95b8df 6363
998580f1 6364 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6365 {
6366 struct inferior *inf;
998580f1
MK
6367 VEC(int) *inf_num = NULL;
6368 int mi_only = 1;
6c95b8df 6369
998580f1 6370 ALL_INFERIORS (inf)
6c95b8df
PA
6371 {
6372 if (inf->pspace == loc->pspace)
998580f1 6373 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6374 }
998580f1
MK
6375
6376 /* For backward compatibility, don't display inferiors in CLI unless
6377 there are several. Always display for MI. */
6378 if (allflag
6379 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6380 && (number_of_program_spaces () > 1
6381 || number_of_inferiors () > 1)
6382 /* LOC is for existing B, it cannot be in
6383 moribund_locations and thus having NULL OWNER. */
6384 && loc->owner->type != bp_catchpoint))
6385 mi_only = 0;
6386 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6387 VEC_free (int, inf_num);
6c95b8df
PA
6388 }
6389
4a306c9a 6390 if (!part_of_multiple)
c4093a6a 6391 {
4a306c9a
JB
6392 if (b->thread != -1)
6393 {
6394 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6395 "stop only in" line a little further down. */
4a306c9a
JB
6396 ui_out_text (uiout, " thread ");
6397 ui_out_field_int (uiout, "thread", b->thread);
6398 }
6399 else if (b->task != 0)
6400 {
6401 ui_out_text (uiout, " task ");
6402 ui_out_field_int (uiout, "task", b->task);
6403 }
c4093a6a 6404 }
f1310107 6405
8b93c638 6406 ui_out_text (uiout, "\n");
f1310107 6407
348d480f 6408 if (!part_of_multiple)
f1310107
TJB
6409 b->ops->print_one_detail (b, uiout);
6410
0d381245 6411 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6412 {
6413 annotate_field (6);
8b93c638 6414 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6415 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6416 the frame ID. */
5af949e3
UW
6417 ui_out_field_core_addr (uiout, "frame",
6418 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6419 ui_out_text (uiout, "\n");
c4093a6a
JM
6420 }
6421
28010a5d 6422 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6423 {
6424 annotate_field (7);
d77f58be 6425 if (is_tracepoint (b))
1042e4c0
SS
6426 ui_out_text (uiout, "\ttrace only if ");
6427 else
6428 ui_out_text (uiout, "\tstop only if ");
0101ce28 6429 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6430
6431 /* Print whether the target is doing the breakpoint's condition
6432 evaluation. If GDB is doing the evaluation, don't print anything. */
6433 if (is_breakpoint (b)
6434 && breakpoint_condition_evaluation_mode ()
6435 == condition_evaluation_target)
6436 {
6437 ui_out_text (uiout, " (");
6438 ui_out_field_string (uiout, "evaluated-by",
6439 bp_condition_evaluator (b));
6440 ui_out_text (uiout, " evals)");
6441 }
0101ce28
JJ
6442 ui_out_text (uiout, "\n");
6443 }
6444
0d381245 6445 if (!part_of_multiple && b->thread != -1)
c4093a6a 6446 {
4a64f543 6447 /* FIXME should make an annotation for this. */
8b93c638
JM
6448 ui_out_text (uiout, "\tstop only in thread ");
6449 ui_out_field_int (uiout, "thread", b->thread);
6450 ui_out_text (uiout, "\n");
c4093a6a
JM
6451 }
6452
556ec64d
YQ
6453 if (!part_of_multiple)
6454 {
6455 if (b->hit_count)
31f56a27
YQ
6456 {
6457 /* FIXME should make an annotation for this. */
6458 if (is_catchpoint (b))
6459 ui_out_text (uiout, "\tcatchpoint");
6460 else if (is_tracepoint (b))
6461 ui_out_text (uiout, "\ttracepoint");
6462 else
6463 ui_out_text (uiout, "\tbreakpoint");
6464 ui_out_text (uiout, " already hit ");
6465 ui_out_field_int (uiout, "times", b->hit_count);
6466 if (b->hit_count == 1)
6467 ui_out_text (uiout, " time\n");
6468 else
6469 ui_out_text (uiout, " times\n");
6470 }
556ec64d
YQ
6471 else
6472 {
31f56a27
YQ
6473 /* Output the count also if it is zero, but only if this is mi. */
6474 if (ui_out_is_mi_like_p (uiout))
6475 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6476 }
6477 }
8b93c638 6478
0d381245 6479 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6480 {
6481 annotate_field (8);
8b93c638
JM
6482 ui_out_text (uiout, "\tignore next ");
6483 ui_out_field_int (uiout, "ignore", b->ignore_count);
6484 ui_out_text (uiout, " hits\n");
c4093a6a 6485 }
059fb39f 6486
816338b5
SS
6487 /* Note that an enable count of 1 corresponds to "enable once"
6488 behavior, which is reported by the combination of enablement and
6489 disposition, so we don't need to mention it here. */
6490 if (!part_of_multiple && b->enable_count > 1)
6491 {
6492 annotate_field (8);
6493 ui_out_text (uiout, "\tdisable after ");
6494 /* Tweak the wording to clarify that ignore and enable counts
6495 are distinct, and have additive effect. */
6496 if (b->ignore_count)
6497 ui_out_text (uiout, "additional ");
6498 else
6499 ui_out_text (uiout, "next ");
6500 ui_out_field_int (uiout, "enable", b->enable_count);
6501 ui_out_text (uiout, " hits\n");
6502 }
6503
f196051f
SS
6504 if (!part_of_multiple && is_tracepoint (b))
6505 {
6506 struct tracepoint *tp = (struct tracepoint *) b;
6507
6508 if (tp->traceframe_usage)
6509 {
6510 ui_out_text (uiout, "\ttrace buffer usage ");
6511 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6512 ui_out_text (uiout, " bytes\n");
6513 }
6514 }
d3ce09f5 6515
9add0f1b 6516 l = b->commands ? b->commands->commands : NULL;
059fb39f 6517 if (!part_of_multiple && l)
c4093a6a 6518 {
3b31d625
EZ
6519 struct cleanup *script_chain;
6520
c4093a6a 6521 annotate_field (9);
3b31d625 6522 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6523 print_command_lines (uiout, l, 4);
3b31d625 6524 do_cleanups (script_chain);
c4093a6a 6525 }
d24317b4 6526
d9b3f62e 6527 if (is_tracepoint (b))
1042e4c0 6528 {
d9b3f62e
PA
6529 struct tracepoint *t = (struct tracepoint *) b;
6530
6531 if (!part_of_multiple && t->pass_count)
6532 {
6533 annotate_field (10);
6534 ui_out_text (uiout, "\tpass count ");
6535 ui_out_field_int (uiout, "pass", t->pass_count);
6536 ui_out_text (uiout, " \n");
6537 }
f2a8bc8a
YQ
6538
6539 /* Don't display it when tracepoint or tracepoint location is
6540 pending. */
6541 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6542 {
6543 annotate_field (11);
6544
6545 if (ui_out_is_mi_like_p (uiout))
6546 ui_out_field_string (uiout, "installed",
6547 loc->inserted ? "y" : "n");
6548 else
6549 {
6550 if (loc->inserted)
6551 ui_out_text (uiout, "\t");
6552 else
6553 ui_out_text (uiout, "\tnot ");
6554 ui_out_text (uiout, "installed on target\n");
6555 }
6556 }
1042e4c0
SS
6557 }
6558
d24317b4
VP
6559 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6560 {
3a5c3e22
PA
6561 if (is_watchpoint (b))
6562 {
6563 struct watchpoint *w = (struct watchpoint *) b;
6564
6565 ui_out_field_string (uiout, "original-location", w->exp_string);
6566 }
f00aae0f
KS
6567 else if (b->location != NULL
6568 && event_location_to_string (b->location) != NULL)
6569 ui_out_field_string (uiout, "original-location",
6570 event_location_to_string (b->location));
d24317b4 6571 }
c4093a6a 6572}
c5aa993b 6573
0d381245
VP
6574static void
6575print_one_breakpoint (struct breakpoint *b,
4a64f543 6576 struct bp_location **last_loc,
6c95b8df 6577 int allflag)
0d381245 6578{
8d3788bd 6579 struct cleanup *bkpt_chain;
79a45e25 6580 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6581
6582 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6583
12c5a436 6584 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6585 do_cleanups (bkpt_chain);
0d381245
VP
6586
6587 /* If this breakpoint has custom print function,
6588 it's already printed. Otherwise, print individual
6589 locations, if any. */
6590 if (b->ops == NULL || b->ops->print_one == NULL)
6591 {
4a64f543
MS
6592 /* If breakpoint has a single location that is disabled, we
6593 print it as if it had several locations, since otherwise it's
6594 hard to represent "breakpoint enabled, location disabled"
6595 situation.
6596
6597 Note that while hardware watchpoints have several locations
a3be7890 6598 internally, that's not a property exposed to user. */
0d381245 6599 if (b->loc
a5606eee 6600 && !is_hardware_watchpoint (b)
8d3788bd 6601 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6602 {
6603 struct bp_location *loc;
6604 int n = 1;
8d3788bd 6605
0d381245 6606 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6607 {
6608 struct cleanup *inner2 =
6609 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6610 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6611 do_cleanups (inner2);
6612 }
0d381245
VP
6613 }
6614 }
6615}
6616
a6d9a66e
UW
6617static int
6618breakpoint_address_bits (struct breakpoint *b)
6619{
6620 int print_address_bits = 0;
6621 struct bp_location *loc;
6622
6623 for (loc = b->loc; loc; loc = loc->next)
6624 {
c7437ca6
PA
6625 int addr_bit;
6626
6627 /* Software watchpoints that aren't watching memory don't have
6628 an address to print. */
6629 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6630 continue;
6631
6632 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6633 if (addr_bit > print_address_bits)
6634 print_address_bits = addr_bit;
6635 }
6636
6637 return print_address_bits;
6638}
0d381245 6639
c4093a6a
JM
6640struct captured_breakpoint_query_args
6641 {
6642 int bnum;
6643 };
c5aa993b 6644
c4093a6a 6645static int
2b65245e 6646do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6647{
6648 struct captured_breakpoint_query_args *args = data;
52f0bd74 6649 struct breakpoint *b;
a6d9a66e 6650 struct bp_location *dummy_loc = NULL;
cc59ec59 6651
c4093a6a
JM
6652 ALL_BREAKPOINTS (b)
6653 {
6654 if (args->bnum == b->number)
c5aa993b 6655 {
12c5a436 6656 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6657 return GDB_RC_OK;
c5aa993b 6658 }
c4093a6a
JM
6659 }
6660 return GDB_RC_NONE;
6661}
c5aa993b 6662
c4093a6a 6663enum gdb_rc
4a64f543
MS
6664gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6665 char **error_message)
c4093a6a
JM
6666{
6667 struct captured_breakpoint_query_args args;
cc59ec59 6668
c4093a6a
JM
6669 args.bnum = bnum;
6670 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6671 an error. */
b0b13bb4
DJ
6672 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6673 error_message, RETURN_MASK_ALL) < 0)
6674 return GDB_RC_FAIL;
6675 else
6676 return GDB_RC_OK;
c4093a6a 6677}
c5aa993b 6678
09d682a4
TT
6679/* Return true if this breakpoint was set by the user, false if it is
6680 internal or momentary. */
6681
6682int
6683user_breakpoint_p (struct breakpoint *b)
6684{
46c6471b 6685 return b->number > 0;
09d682a4
TT
6686}
6687
7f3b0473 6688/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6689 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6690 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6691 FILTER is non-NULL, call it on each breakpoint and only include the
6692 ones for which it returns non-zero. Return the total number of
6693 breakpoints listed. */
c906108c 6694
d77f58be 6695static int
e5a67952 6696breakpoint_1 (char *args, int allflag,
4a64f543 6697 int (*filter) (const struct breakpoint *))
c4093a6a 6698{
52f0bd74 6699 struct breakpoint *b;
a6d9a66e 6700 struct bp_location *last_loc = NULL;
7f3b0473 6701 int nr_printable_breakpoints;
3b31d625 6702 struct cleanup *bkpttbl_chain;
79a45b7d 6703 struct value_print_options opts;
a6d9a66e 6704 int print_address_bits = 0;
269b11a2 6705 int print_type_col_width = 14;
79a45e25 6706 struct ui_out *uiout = current_uiout;
269b11a2 6707
79a45b7d
TT
6708 get_user_print_options (&opts);
6709
4a64f543
MS
6710 /* Compute the number of rows in the table, as well as the size
6711 required for address fields. */
7f3b0473
AC
6712 nr_printable_breakpoints = 0;
6713 ALL_BREAKPOINTS (b)
e5a67952
MS
6714 {
6715 /* If we have a filter, only list the breakpoints it accepts. */
6716 if (filter && !filter (b))
6717 continue;
6718
6719 /* If we have an "args" string, it is a list of breakpoints to
6720 accept. Skip the others. */
6721 if (args != NULL && *args != '\0')
6722 {
6723 if (allflag && parse_and_eval_long (args) != b->number)
6724 continue;
6725 if (!allflag && !number_is_in_list (args, b->number))
6726 continue;
6727 }
269b11a2 6728
e5a67952
MS
6729 if (allflag || user_breakpoint_p (b))
6730 {
6731 int addr_bit, type_len;
a6d9a66e 6732
e5a67952
MS
6733 addr_bit = breakpoint_address_bits (b);
6734 if (addr_bit > print_address_bits)
6735 print_address_bits = addr_bit;
269b11a2 6736
e5a67952
MS
6737 type_len = strlen (bptype_string (b->type));
6738 if (type_len > print_type_col_width)
6739 print_type_col_width = type_len;
6740
6741 nr_printable_breakpoints++;
6742 }
6743 }
7f3b0473 6744
79a45b7d 6745 if (opts.addressprint)
3b31d625 6746 bkpttbl_chain
3e43a32a
MS
6747 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6748 nr_printable_breakpoints,
3b31d625 6749 "BreakpointTable");
8b93c638 6750 else
3b31d625 6751 bkpttbl_chain
3e43a32a
MS
6752 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6753 nr_printable_breakpoints,
3b31d625 6754 "BreakpointTable");
8b93c638 6755
7f3b0473 6756 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6757 annotate_breakpoints_headers ();
6758 if (nr_printable_breakpoints > 0)
6759 annotate_field (0);
4a64f543 6760 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6761 if (nr_printable_breakpoints > 0)
6762 annotate_field (1);
269b11a2 6763 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6764 "type", "Type"); /* 2 */
d7faa9e7
AC
6765 if (nr_printable_breakpoints > 0)
6766 annotate_field (2);
4a64f543 6767 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6768 if (nr_printable_breakpoints > 0)
6769 annotate_field (3);
54e52265 6770 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6771 if (opts.addressprint)
e5a67952
MS
6772 {
6773 if (nr_printable_breakpoints > 0)
6774 annotate_field (4);
6775 if (print_address_bits <= 32)
6776 ui_out_table_header (uiout, 10, ui_left,
6777 "addr", "Address"); /* 5 */
6778 else
6779 ui_out_table_header (uiout, 18, ui_left,
6780 "addr", "Address"); /* 5 */
6781 }
d7faa9e7
AC
6782 if (nr_printable_breakpoints > 0)
6783 annotate_field (5);
6784 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6785 ui_out_table_body (uiout);
6786 if (nr_printable_breakpoints > 0)
6787 annotate_breakpoints_table ();
7f3b0473 6788
c4093a6a 6789 ALL_BREAKPOINTS (b)
e5a67952
MS
6790 {
6791 QUIT;
6792 /* If we have a filter, only list the breakpoints it accepts. */
6793 if (filter && !filter (b))
6794 continue;
6795
6796 /* If we have an "args" string, it is a list of breakpoints to
6797 accept. Skip the others. */
6798
6799 if (args != NULL && *args != '\0')
6800 {
6801 if (allflag) /* maintenance info breakpoint */
6802 {
6803 if (parse_and_eval_long (args) != b->number)
6804 continue;
6805 }
6806 else /* all others */
6807 {
6808 if (!number_is_in_list (args, b->number))
6809 continue;
6810 }
6811 }
6812 /* We only print out user settable breakpoints unless the
6813 allflag is set. */
6814 if (allflag || user_breakpoint_p (b))
12c5a436 6815 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6816 }
6817
3b31d625 6818 do_cleanups (bkpttbl_chain);
698384cd 6819
7f3b0473 6820 if (nr_printable_breakpoints == 0)
c906108c 6821 {
4a64f543
MS
6822 /* If there's a filter, let the caller decide how to report
6823 empty list. */
d77f58be
SS
6824 if (!filter)
6825 {
e5a67952 6826 if (args == NULL || *args == '\0')
d77f58be
SS
6827 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6828 else
4a64f543 6829 ui_out_message (uiout, 0,
e5a67952
MS
6830 "No breakpoint or watchpoint matching '%s'.\n",
6831 args);
d77f58be 6832 }
c906108c
SS
6833 }
6834 else
c4093a6a 6835 {
a6d9a66e
UW
6836 if (last_loc && !server_command)
6837 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6838 }
c906108c 6839
4a64f543 6840 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6841 there have been breakpoints? */
c906108c 6842 annotate_breakpoints_table_end ();
d77f58be
SS
6843
6844 return nr_printable_breakpoints;
c906108c
SS
6845}
6846
ad443146
SS
6847/* Display the value of default-collect in a way that is generally
6848 compatible with the breakpoint list. */
6849
6850static void
6851default_collect_info (void)
6852{
79a45e25
PA
6853 struct ui_out *uiout = current_uiout;
6854
ad443146
SS
6855 /* If it has no value (which is frequently the case), say nothing; a
6856 message like "No default-collect." gets in user's face when it's
6857 not wanted. */
6858 if (!*default_collect)
6859 return;
6860
6861 /* The following phrase lines up nicely with per-tracepoint collect
6862 actions. */
6863 ui_out_text (uiout, "default collect ");
6864 ui_out_field_string (uiout, "default-collect", default_collect);
6865 ui_out_text (uiout, " \n");
6866}
6867
c906108c 6868static void
e5a67952 6869breakpoints_info (char *args, int from_tty)
c906108c 6870{
e5a67952 6871 breakpoint_1 (args, 0, NULL);
ad443146
SS
6872
6873 default_collect_info ();
d77f58be
SS
6874}
6875
6876static void
e5a67952 6877watchpoints_info (char *args, int from_tty)
d77f58be 6878{
e5a67952 6879 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6880 struct ui_out *uiout = current_uiout;
d77f58be
SS
6881
6882 if (num_printed == 0)
6883 {
e5a67952 6884 if (args == NULL || *args == '\0')
d77f58be
SS
6885 ui_out_message (uiout, 0, "No watchpoints.\n");
6886 else
e5a67952 6887 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6888 }
c906108c
SS
6889}
6890
7a292a7a 6891static void
e5a67952 6892maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6893{
e5a67952 6894 breakpoint_1 (args, 1, NULL);
ad443146
SS
6895
6896 default_collect_info ();
c906108c
SS
6897}
6898
0d381245 6899static int
714835d5 6900breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6901 struct program_space *pspace,
714835d5 6902 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6903{
6904 struct bp_location *bl = b->loc;
cc59ec59 6905
0d381245
VP
6906 for (; bl; bl = bl->next)
6907 {
6c95b8df
PA
6908 if (bl->pspace == pspace
6909 && bl->address == pc
0d381245
VP
6910 && (!overlay_debugging || bl->section == section))
6911 return 1;
6912 }
6913 return 0;
6914}
6915
672f9b60 6916/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6917 concerns with logical breakpoints, so we match program spaces, not
6918 address spaces. */
c906108c
SS
6919
6920static void
6c95b8df
PA
6921describe_other_breakpoints (struct gdbarch *gdbarch,
6922 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6923 struct obj_section *section, int thread)
c906108c 6924{
52f0bd74
AC
6925 int others = 0;
6926 struct breakpoint *b;
c906108c
SS
6927
6928 ALL_BREAKPOINTS (b)
672f9b60
KP
6929 others += (user_breakpoint_p (b)
6930 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6931 if (others > 0)
6932 {
a3f17187
AC
6933 if (others == 1)
6934 printf_filtered (_("Note: breakpoint "));
6935 else /* if (others == ???) */
6936 printf_filtered (_("Note: breakpoints "));
c906108c 6937 ALL_BREAKPOINTS (b)
672f9b60 6938 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6939 {
6940 others--;
6941 printf_filtered ("%d", b->number);
6942 if (b->thread == -1 && thread != -1)
6943 printf_filtered (" (all threads)");
6944 else if (b->thread != -1)
6945 printf_filtered (" (thread %d)", b->thread);
6946 printf_filtered ("%s%s ",
059fb39f 6947 ((b->enable_state == bp_disabled
f8eba3c6 6948 || b->enable_state == bp_call_disabled)
0d381245 6949 ? " (disabled)"
0d381245
VP
6950 : ""),
6951 (others > 1) ? ","
6952 : ((others == 1) ? " and" : ""));
6953 }
a3f17187 6954 printf_filtered (_("also set at pc "));
5af949e3 6955 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6956 printf_filtered (".\n");
6957 }
6958}
6959\f
c906108c 6960
e4f237da
KB
6961/* Return true iff it is meaningful to use the address member of
6962 BPT. For some breakpoint types, the address member is irrelevant
6963 and it makes no sense to attempt to compare it to other addresses
6964 (or use it for any other purpose either).
6965
4a64f543
MS
6966 More specifically, each of the following breakpoint types will
6967 always have a zero valued address and we don't want to mark
6968 breakpoints of any of these types to be a duplicate of an actual
6969 breakpoint at address zero:
e4f237da
KB
6970
6971 bp_watchpoint
2d134ed3
PA
6972 bp_catchpoint
6973
6974*/
e4f237da
KB
6975
6976static int
6977breakpoint_address_is_meaningful (struct breakpoint *bpt)
6978{
6979 enum bptype type = bpt->type;
6980
2d134ed3
PA
6981 return (type != bp_watchpoint && type != bp_catchpoint);
6982}
6983
6984/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6985 true if LOC1 and LOC2 represent the same watchpoint location. */
6986
6987static int
4a64f543
MS
6988watchpoint_locations_match (struct bp_location *loc1,
6989 struct bp_location *loc2)
2d134ed3 6990{
3a5c3e22
PA
6991 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6992 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6993
6994 /* Both of them must exist. */
6995 gdb_assert (w1 != NULL);
6996 gdb_assert (w2 != NULL);
2bdf28a0 6997
4a64f543
MS
6998 /* If the target can evaluate the condition expression in hardware,
6999 then we we need to insert both watchpoints even if they are at
7000 the same place. Otherwise the watchpoint will only trigger when
7001 the condition of whichever watchpoint was inserted evaluates to
7002 true, not giving a chance for GDB to check the condition of the
7003 other watchpoint. */
3a5c3e22 7004 if ((w1->cond_exp
4a64f543
MS
7005 && target_can_accel_watchpoint_condition (loc1->address,
7006 loc1->length,
0cf6dd15 7007 loc1->watchpoint_type,
3a5c3e22
PA
7008 w1->cond_exp))
7009 || (w2->cond_exp
4a64f543
MS
7010 && target_can_accel_watchpoint_condition (loc2->address,
7011 loc2->length,
0cf6dd15 7012 loc2->watchpoint_type,
3a5c3e22 7013 w2->cond_exp)))
0cf6dd15
TJB
7014 return 0;
7015
85d721b8
PA
7016 /* Note that this checks the owner's type, not the location's. In
7017 case the target does not support read watchpoints, but does
7018 support access watchpoints, we'll have bp_read_watchpoint
7019 watchpoints with hw_access locations. Those should be considered
7020 duplicates of hw_read locations. The hw_read locations will
7021 become hw_access locations later. */
2d134ed3
PA
7022 return (loc1->owner->type == loc2->owner->type
7023 && loc1->pspace->aspace == loc2->pspace->aspace
7024 && loc1->address == loc2->address
7025 && loc1->length == loc2->length);
e4f237da
KB
7026}
7027
31e77af2 7028/* See breakpoint.h. */
6c95b8df 7029
31e77af2 7030int
6c95b8df
PA
7031breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7032 struct address_space *aspace2, CORE_ADDR addr2)
7033{
f5656ead 7034 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7035 || aspace1 == aspace2)
7036 && addr1 == addr2);
7037}
7038
f1310107
TJB
7039/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7040 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7041 matches ASPACE2. On targets that have global breakpoints, the address
7042 space doesn't really matter. */
7043
7044static int
7045breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7046 int len1, struct address_space *aspace2,
7047 CORE_ADDR addr2)
7048{
f5656ead 7049 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7050 || aspace1 == aspace2)
7051 && addr2 >= addr1 && addr2 < addr1 + len1);
7052}
7053
7054/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7055 a ranged breakpoint. In most targets, a match happens only if ASPACE
7056 matches the breakpoint's address space. On targets that have global
7057 breakpoints, the address space doesn't really matter. */
7058
7059static int
7060breakpoint_location_address_match (struct bp_location *bl,
7061 struct address_space *aspace,
7062 CORE_ADDR addr)
7063{
7064 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7065 aspace, addr)
7066 || (bl->length
7067 && breakpoint_address_match_range (bl->pspace->aspace,
7068 bl->address, bl->length,
7069 aspace, addr)));
7070}
7071
1e4d1764
YQ
7072/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7073 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7074 true, otherwise returns false. */
7075
7076static int
7077tracepoint_locations_match (struct bp_location *loc1,
7078 struct bp_location *loc2)
7079{
7080 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7081 /* Since tracepoint locations are never duplicated with others', tracepoint
7082 locations at the same address of different tracepoints are regarded as
7083 different locations. */
7084 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7085 else
7086 return 0;
7087}
7088
2d134ed3
PA
7089/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7090 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7091 represent the same location. */
7092
7093static int
4a64f543
MS
7094breakpoint_locations_match (struct bp_location *loc1,
7095 struct bp_location *loc2)
2d134ed3 7096{
2bdf28a0
JK
7097 int hw_point1, hw_point2;
7098
7099 /* Both of them must not be in moribund_locations. */
7100 gdb_assert (loc1->owner != NULL);
7101 gdb_assert (loc2->owner != NULL);
7102
7103 hw_point1 = is_hardware_watchpoint (loc1->owner);
7104 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7105
7106 if (hw_point1 != hw_point2)
7107 return 0;
7108 else if (hw_point1)
7109 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7110 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7111 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7112 else
f1310107
TJB
7113 /* We compare bp_location.length in order to cover ranged breakpoints. */
7114 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7115 loc2->pspace->aspace, loc2->address)
7116 && loc1->length == loc2->length);
2d134ed3
PA
7117}
7118
76897487
KB
7119static void
7120breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7121 int bnum, int have_bnum)
7122{
f63fbe86
MS
7123 /* The longest string possibly returned by hex_string_custom
7124 is 50 chars. These must be at least that big for safety. */
7125 char astr1[64];
7126 char astr2[64];
76897487 7127
bb599908
PH
7128 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7129 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7130 if (have_bnum)
8a3fe4f8 7131 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7132 bnum, astr1, astr2);
7133 else
8a3fe4f8 7134 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7135}
7136
4a64f543
MS
7137/* Adjust a breakpoint's address to account for architectural
7138 constraints on breakpoint placement. Return the adjusted address.
7139 Note: Very few targets require this kind of adjustment. For most
7140 targets, this function is simply the identity function. */
76897487
KB
7141
7142static CORE_ADDR
a6d9a66e
UW
7143adjust_breakpoint_address (struct gdbarch *gdbarch,
7144 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7145{
a6d9a66e 7146 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7147 {
7148 /* Very few targets need any kind of breakpoint adjustment. */
7149 return bpaddr;
7150 }
88f7da05
KB
7151 else if (bptype == bp_watchpoint
7152 || bptype == bp_hardware_watchpoint
7153 || bptype == bp_read_watchpoint
7154 || bptype == bp_access_watchpoint
fe798b75 7155 || bptype == bp_catchpoint)
88f7da05
KB
7156 {
7157 /* Watchpoints and the various bp_catch_* eventpoints should not
7158 have their addresses modified. */
7159 return bpaddr;
7160 }
7c16b83e
PA
7161 else if (bptype == bp_single_step)
7162 {
7163 /* Single-step breakpoints should not have their addresses
7164 modified. If there's any architectural constrain that
7165 applies to this address, then it should have already been
7166 taken into account when the breakpoint was created in the
7167 first place. If we didn't do this, stepping through e.g.,
7168 Thumb-2 IT blocks would break. */
7169 return bpaddr;
7170 }
76897487
KB
7171 else
7172 {
7173 CORE_ADDR adjusted_bpaddr;
7174
7175 /* Some targets have architectural constraints on the placement
7176 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7177 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7178
7179 /* An adjusted breakpoint address can significantly alter
7180 a user's expectations. Print a warning if an adjustment
7181 is required. */
7182 if (adjusted_bpaddr != bpaddr)
7183 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7184
7185 return adjusted_bpaddr;
7186 }
7187}
7188
28010a5d
PA
7189void
7190init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7191 struct breakpoint *owner)
7cc221ef 7192{
7cc221ef
DJ
7193 memset (loc, 0, sizeof (*loc));
7194
348d480f
PA
7195 gdb_assert (ops != NULL);
7196
28010a5d
PA
7197 loc->ops = ops;
7198 loc->owner = owner;
511a6cd4 7199 loc->cond = NULL;
b775012e 7200 loc->cond_bytecode = NULL;
0d381245
VP
7201 loc->shlib_disabled = 0;
7202 loc->enabled = 1;
e049a4b5 7203
28010a5d 7204 switch (owner->type)
e049a4b5
DJ
7205 {
7206 case bp_breakpoint:
7c16b83e 7207 case bp_single_step:
e049a4b5
DJ
7208 case bp_until:
7209 case bp_finish:
7210 case bp_longjmp:
7211 case bp_longjmp_resume:
e2e4d78b 7212 case bp_longjmp_call_dummy:
186c406b
TT
7213 case bp_exception:
7214 case bp_exception_resume:
e049a4b5 7215 case bp_step_resume:
2c03e5be 7216 case bp_hp_step_resume:
e049a4b5
DJ
7217 case bp_watchpoint_scope:
7218 case bp_call_dummy:
aa7d318d 7219 case bp_std_terminate:
e049a4b5
DJ
7220 case bp_shlib_event:
7221 case bp_thread_event:
7222 case bp_overlay_event:
4efc6507 7223 case bp_jit_event:
0fd8e87f 7224 case bp_longjmp_master:
aa7d318d 7225 case bp_std_terminate_master:
186c406b 7226 case bp_exception_master:
0e30163f
JK
7227 case bp_gnu_ifunc_resolver:
7228 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7229 case bp_dprintf:
e049a4b5 7230 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7231 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7232 break;
7233 case bp_hardware_breakpoint:
7234 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7235 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7236 break;
7237 case bp_hardware_watchpoint:
7238 case bp_read_watchpoint:
7239 case bp_access_watchpoint:
7240 loc->loc_type = bp_loc_hardware_watchpoint;
7241 break;
7242 case bp_watchpoint:
ce78b96d 7243 case bp_catchpoint:
15c3d785
PA
7244 case bp_tracepoint:
7245 case bp_fast_tracepoint:
0fb4aa4b 7246 case bp_static_tracepoint:
e049a4b5
DJ
7247 loc->loc_type = bp_loc_other;
7248 break;
7249 default:
e2e0b3e5 7250 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7251 }
7252
f431efe5 7253 loc->refc = 1;
28010a5d
PA
7254}
7255
7256/* Allocate a struct bp_location. */
7257
7258static struct bp_location *
7259allocate_bp_location (struct breakpoint *bpt)
7260{
348d480f
PA
7261 return bpt->ops->allocate_location (bpt);
7262}
7cc221ef 7263
f431efe5
PA
7264static void
7265free_bp_location (struct bp_location *loc)
fe3f5fa8 7266{
348d480f 7267 loc->ops->dtor (loc);
fe3f5fa8
VP
7268 xfree (loc);
7269}
7270
f431efe5
PA
7271/* Increment reference count. */
7272
7273static void
7274incref_bp_location (struct bp_location *bl)
7275{
7276 ++bl->refc;
7277}
7278
7279/* Decrement reference count. If the reference count reaches 0,
7280 destroy the bp_location. Sets *BLP to NULL. */
7281
7282static void
7283decref_bp_location (struct bp_location **blp)
7284{
0807b50c
PA
7285 gdb_assert ((*blp)->refc > 0);
7286
f431efe5
PA
7287 if (--(*blp)->refc == 0)
7288 free_bp_location (*blp);
7289 *blp = NULL;
7290}
7291
346774a9 7292/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7293
346774a9
PA
7294static void
7295add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7296{
346774a9 7297 struct breakpoint *b1;
c906108c 7298
346774a9
PA
7299 /* Add this breakpoint to the end of the chain so that a list of
7300 breakpoints will come out in order of increasing numbers. */
7301
7302 b1 = breakpoint_chain;
7303 if (b1 == 0)
7304 breakpoint_chain = b;
7305 else
7306 {
7307 while (b1->next)
7308 b1 = b1->next;
7309 b1->next = b;
7310 }
7311}
7312
7313/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7314
7315static void
7316init_raw_breakpoint_without_location (struct breakpoint *b,
7317 struct gdbarch *gdbarch,
28010a5d 7318 enum bptype bptype,
c0a91b2b 7319 const struct breakpoint_ops *ops)
346774a9 7320{
c906108c 7321 memset (b, 0, sizeof (*b));
2219d63c 7322
348d480f
PA
7323 gdb_assert (ops != NULL);
7324
28010a5d 7325 b->ops = ops;
4d28f7a8 7326 b->type = bptype;
a6d9a66e 7327 b->gdbarch = gdbarch;
c906108c
SS
7328 b->language = current_language->la_language;
7329 b->input_radix = input_radix;
7330 b->thread = -1;
b5de0fa7 7331 b->enable_state = bp_enabled;
c906108c
SS
7332 b->next = 0;
7333 b->silent = 0;
7334 b->ignore_count = 0;
7335 b->commands = NULL;
818dd999 7336 b->frame_id = null_frame_id;
0d381245 7337 b->condition_not_parsed = 0;
84f4c1fe 7338 b->py_bp_object = NULL;
d0fb5eae 7339 b->related_breakpoint = b;
f00aae0f 7340 b->location = NULL;
346774a9
PA
7341}
7342
7343/* Helper to set_raw_breakpoint below. Creates a breakpoint
7344 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7345
7346static struct breakpoint *
7347set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7348 enum bptype bptype,
c0a91b2b 7349 const struct breakpoint_ops *ops)
346774a9
PA
7350{
7351 struct breakpoint *b = XNEW (struct breakpoint);
7352
348d480f 7353 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7354 add_to_breakpoint_chain (b);
0d381245
VP
7355 return b;
7356}
7357
0e30163f
JK
7358/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7359 resolutions should be made as the user specified the location explicitly
7360 enough. */
7361
0d381245 7362static void
0e30163f 7363set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7364{
2bdf28a0
JK
7365 gdb_assert (loc->owner != NULL);
7366
0d381245 7367 if (loc->owner->type == bp_breakpoint
1042e4c0 7368 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7369 || is_tracepoint (loc->owner))
0d381245 7370 {
0e30163f 7371 int is_gnu_ifunc;
2c02bd72 7372 const char *function_name;
6a3a010b 7373 CORE_ADDR func_addr;
0e30163f 7374
2c02bd72 7375 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7376 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7377
7378 if (is_gnu_ifunc && !explicit_loc)
7379 {
7380 struct breakpoint *b = loc->owner;
7381
7382 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7383 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7384 &loc->requested_address))
7385 {
7386 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7387 loc->address = adjust_breakpoint_address (loc->gdbarch,
7388 loc->requested_address,
7389 b->type);
7390 }
7391 else if (b->type == bp_breakpoint && b->loc == loc
7392 && loc->next == NULL && b->related_breakpoint == b)
7393 {
7394 /* Create only the whole new breakpoint of this type but do not
7395 mess more complicated breakpoints with multiple locations. */
7396 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7397 /* Remember the resolver's address for use by the return
7398 breakpoint. */
7399 loc->related_address = func_addr;
0e30163f
JK
7400 }
7401 }
7402
2c02bd72
DE
7403 if (function_name)
7404 loc->function_name = xstrdup (function_name);
0d381245
VP
7405 }
7406}
7407
a6d9a66e 7408/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7409struct gdbarch *
a6d9a66e
UW
7410get_sal_arch (struct symtab_and_line sal)
7411{
7412 if (sal.section)
7413 return get_objfile_arch (sal.section->objfile);
7414 if (sal.symtab)
eb822aa6 7415 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7416
7417 return NULL;
7418}
7419
346774a9
PA
7420/* Low level routine for partially initializing a breakpoint of type
7421 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7422 file name, and line number are provided by SAL.
0d381245
VP
7423
7424 It is expected that the caller will complete the initialization of
7425 the newly created breakpoint struct as well as output any status
c56053d2 7426 information regarding the creation of a new breakpoint. */
0d381245 7427
346774a9
PA
7428static void
7429init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7430 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7431 const struct breakpoint_ops *ops)
0d381245 7432{
28010a5d 7433 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7434
3742cc8b 7435 add_location_to_breakpoint (b, &sal);
0d381245 7436
6c95b8df
PA
7437 if (bptype != bp_catchpoint)
7438 gdb_assert (sal.pspace != NULL);
7439
f8eba3c6
TT
7440 /* Store the program space that was used to set the breakpoint,
7441 except for ordinary breakpoints, which are independent of the
7442 program space. */
7443 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7444 b->pspace = sal.pspace;
346774a9 7445}
c906108c 7446
346774a9
PA
7447/* set_raw_breakpoint is a low level routine for allocating and
7448 partially initializing a breakpoint of type BPTYPE. The newly
7449 created breakpoint's address, section, source file name, and line
7450 number are provided by SAL. The newly created and partially
7451 initialized breakpoint is added to the breakpoint chain and
7452 is also returned as the value of this function.
7453
7454 It is expected that the caller will complete the initialization of
7455 the newly created breakpoint struct as well as output any status
7456 information regarding the creation of a new breakpoint. In
7457 particular, set_raw_breakpoint does NOT set the breakpoint
7458 number! Care should be taken to not allow an error to occur
7459 prior to completing the initialization of the breakpoint. If this
7460 should happen, a bogus breakpoint will be left on the chain. */
7461
7462struct breakpoint *
7463set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7464 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7465 const struct breakpoint_ops *ops)
346774a9
PA
7466{
7467 struct breakpoint *b = XNEW (struct breakpoint);
7468
348d480f 7469 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7470 add_to_breakpoint_chain (b);
c906108c
SS
7471 return b;
7472}
7473
53a5351d 7474/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7475 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7476 initiated the operation. */
c906108c
SS
7477
7478void
186c406b 7479set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7480{
35df4500 7481 struct breakpoint *b, *b_tmp;
186c406b 7482 int thread = tp->num;
0fd8e87f
UW
7483
7484 /* To avoid having to rescan all objfile symbols at every step,
7485 we maintain a list of continually-inserted but always disabled
7486 longjmp "master" breakpoints. Here, we simply create momentary
7487 clones of those and enable them for the requested thread. */
35df4500 7488 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7489 if (b->pspace == current_program_space
186c406b
TT
7490 && (b->type == bp_longjmp_master
7491 || b->type == bp_exception_master))
0fd8e87f 7492 {
06edf0c0
PA
7493 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7494 struct breakpoint *clone;
cc59ec59 7495
e2e4d78b
JK
7496 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7497 after their removal. */
06edf0c0 7498 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7499 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7500 clone->thread = thread;
7501 }
186c406b
TT
7502
7503 tp->initiating_frame = frame;
c906108c
SS
7504}
7505
611c83ae 7506/* Delete all longjmp breakpoints from THREAD. */
c906108c 7507void
611c83ae 7508delete_longjmp_breakpoint (int thread)
c906108c 7509{
35df4500 7510 struct breakpoint *b, *b_tmp;
c906108c 7511
35df4500 7512 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7513 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7514 {
7515 if (b->thread == thread)
7516 delete_breakpoint (b);
7517 }
c906108c
SS
7518}
7519
f59f708a
PA
7520void
7521delete_longjmp_breakpoint_at_next_stop (int thread)
7522{
7523 struct breakpoint *b, *b_tmp;
7524
7525 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7526 if (b->type == bp_longjmp || b->type == bp_exception)
7527 {
7528 if (b->thread == thread)
7529 b->disposition = disp_del_at_next_stop;
7530 }
7531}
7532
e2e4d78b
JK
7533/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7534 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7535 pointer to any of them. Return NULL if this system cannot place longjmp
7536 breakpoints. */
7537
7538struct breakpoint *
7539set_longjmp_breakpoint_for_call_dummy (void)
7540{
7541 struct breakpoint *b, *retval = NULL;
7542
7543 ALL_BREAKPOINTS (b)
7544 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7545 {
7546 struct breakpoint *new_b;
7547
7548 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7549 &momentary_breakpoint_ops,
7550 1);
e2e4d78b
JK
7551 new_b->thread = pid_to_thread_id (inferior_ptid);
7552
7553 /* Link NEW_B into the chain of RETVAL breakpoints. */
7554
7555 gdb_assert (new_b->related_breakpoint == new_b);
7556 if (retval == NULL)
7557 retval = new_b;
7558 new_b->related_breakpoint = retval;
7559 while (retval->related_breakpoint != new_b->related_breakpoint)
7560 retval = retval->related_breakpoint;
7561 retval->related_breakpoint = new_b;
7562 }
7563
7564 return retval;
7565}
7566
7567/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7568 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7569 stack.
7570
7571 You should call this function only at places where it is safe to currently
7572 unwind the whole stack. Failed stack unwind would discard live dummy
7573 frames. */
7574
7575void
b67a2c6f 7576check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7577{
7578 struct breakpoint *b, *b_tmp;
7579
7580 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7581 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7582 {
7583 struct breakpoint *dummy_b = b->related_breakpoint;
7584
7585 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7586 dummy_b = dummy_b->related_breakpoint;
7587 if (dummy_b->type != bp_call_dummy
7588 || frame_find_by_id (dummy_b->frame_id) != NULL)
7589 continue;
7590
b67a2c6f 7591 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7592
7593 while (b->related_breakpoint != b)
7594 {
7595 if (b_tmp == b->related_breakpoint)
7596 b_tmp = b->related_breakpoint->next;
7597 delete_breakpoint (b->related_breakpoint);
7598 }
7599 delete_breakpoint (b);
7600 }
7601}
7602
1900040c
MS
7603void
7604enable_overlay_breakpoints (void)
7605{
52f0bd74 7606 struct breakpoint *b;
1900040c
MS
7607
7608 ALL_BREAKPOINTS (b)
7609 if (b->type == bp_overlay_event)
7610 {
7611 b->enable_state = bp_enabled;
44702360 7612 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7613 overlay_events_enabled = 1;
1900040c
MS
7614 }
7615}
7616
7617void
7618disable_overlay_breakpoints (void)
7619{
52f0bd74 7620 struct breakpoint *b;
1900040c
MS
7621
7622 ALL_BREAKPOINTS (b)
7623 if (b->type == bp_overlay_event)
7624 {
7625 b->enable_state = bp_disabled;
44702360 7626 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7627 overlay_events_enabled = 0;
1900040c
MS
7628 }
7629}
7630
aa7d318d
TT
7631/* Set an active std::terminate breakpoint for each std::terminate
7632 master breakpoint. */
7633void
7634set_std_terminate_breakpoint (void)
7635{
35df4500 7636 struct breakpoint *b, *b_tmp;
aa7d318d 7637
35df4500 7638 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7639 if (b->pspace == current_program_space
7640 && b->type == bp_std_terminate_master)
7641 {
06edf0c0 7642 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7643 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7644 }
7645}
7646
7647/* Delete all the std::terminate breakpoints. */
7648void
7649delete_std_terminate_breakpoint (void)
7650{
35df4500 7651 struct breakpoint *b, *b_tmp;
aa7d318d 7652
35df4500 7653 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7654 if (b->type == bp_std_terminate)
7655 delete_breakpoint (b);
7656}
7657
c4093a6a 7658struct breakpoint *
a6d9a66e 7659create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7660{
7661 struct breakpoint *b;
c4093a6a 7662
06edf0c0
PA
7663 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7664 &internal_breakpoint_ops);
7665
b5de0fa7 7666 b->enable_state = bp_enabled;
f00aae0f 7667 /* location has to be used or breakpoint_re_set will delete me. */
a06efdd6 7668 b->location = new_address_location (b->loc->address);
c4093a6a 7669
44702360 7670 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7671
c4093a6a
JM
7672 return b;
7673}
7674
7675void
7676remove_thread_event_breakpoints (void)
7677{
35df4500 7678 struct breakpoint *b, *b_tmp;
c4093a6a 7679
35df4500 7680 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7681 if (b->type == bp_thread_event
7682 && b->loc->pspace == current_program_space)
c4093a6a
JM
7683 delete_breakpoint (b);
7684}
7685
0101ce28
JJ
7686struct lang_and_radix
7687 {
7688 enum language lang;
7689 int radix;
7690 };
7691
4efc6507
DE
7692/* Create a breakpoint for JIT code registration and unregistration. */
7693
7694struct breakpoint *
7695create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7696{
7697 struct breakpoint *b;
7698
06edf0c0
PA
7699 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7700 &internal_breakpoint_ops);
44702360 7701 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7702 return b;
7703}
0101ce28 7704
03673fc7
PP
7705/* Remove JIT code registration and unregistration breakpoint(s). */
7706
7707void
7708remove_jit_event_breakpoints (void)
7709{
7710 struct breakpoint *b, *b_tmp;
7711
7712 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7713 if (b->type == bp_jit_event
7714 && b->loc->pspace == current_program_space)
7715 delete_breakpoint (b);
7716}
7717
cae688ec
JJ
7718void
7719remove_solib_event_breakpoints (void)
7720{
35df4500 7721 struct breakpoint *b, *b_tmp;
cae688ec 7722
35df4500 7723 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7724 if (b->type == bp_shlib_event
7725 && b->loc->pspace == current_program_space)
cae688ec
JJ
7726 delete_breakpoint (b);
7727}
7728
f37f681c
PA
7729/* See breakpoint.h. */
7730
7731void
7732remove_solib_event_breakpoints_at_next_stop (void)
7733{
7734 struct breakpoint *b, *b_tmp;
7735
7736 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7737 if (b->type == bp_shlib_event
7738 && b->loc->pspace == current_program_space)
7739 b->disposition = disp_del_at_next_stop;
7740}
7741
04086b45
PA
7742/* Helper for create_solib_event_breakpoint /
7743 create_and_insert_solib_event_breakpoint. Allows specifying which
7744 INSERT_MODE to pass through to update_global_location_list. */
7745
7746static struct breakpoint *
7747create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7748 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7749{
7750 struct breakpoint *b;
7751
06edf0c0
PA
7752 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7753 &internal_breakpoint_ops);
04086b45 7754 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7755 return b;
7756}
7757
04086b45
PA
7758struct breakpoint *
7759create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7760{
7761 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7762}
7763
f37f681c
PA
7764/* See breakpoint.h. */
7765
7766struct breakpoint *
7767create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7768{
7769 struct breakpoint *b;
7770
04086b45
PA
7771 /* Explicitly tell update_global_location_list to insert
7772 locations. */
7773 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7774 if (!b->loc->inserted)
7775 {
7776 delete_breakpoint (b);
7777 return NULL;
7778 }
7779 return b;
7780}
7781
cae688ec
JJ
7782/* Disable any breakpoints that are on code in shared libraries. Only
7783 apply to enabled breakpoints, disabled ones can just stay disabled. */
7784
7785void
cb851954 7786disable_breakpoints_in_shlibs (void)
cae688ec 7787{
876fa593 7788 struct bp_location *loc, **locp_tmp;
cae688ec 7789
876fa593 7790 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7791 {
2bdf28a0 7792 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7793 struct breakpoint *b = loc->owner;
2bdf28a0 7794
4a64f543
MS
7795 /* We apply the check to all breakpoints, including disabled for
7796 those with loc->duplicate set. This is so that when breakpoint
7797 becomes enabled, or the duplicate is removed, gdb will try to
7798 insert all breakpoints. If we don't set shlib_disabled here,
7799 we'll try to insert those breakpoints and fail. */
1042e4c0 7800 if (((b->type == bp_breakpoint)
508ccb1f 7801 || (b->type == bp_jit_event)
1042e4c0 7802 || (b->type == bp_hardware_breakpoint)
d77f58be 7803 || (is_tracepoint (b)))
6c95b8df 7804 && loc->pspace == current_program_space
0d381245 7805 && !loc->shlib_disabled
6c95b8df 7806 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7807 )
0d381245
VP
7808 {
7809 loc->shlib_disabled = 1;
7810 }
cae688ec
JJ
7811 }
7812}
7813
63644780
NB
7814/* Disable any breakpoints and tracepoints that are in SOLIB upon
7815 notification of unloaded_shlib. Only apply to enabled breakpoints,
7816 disabled ones can just stay disabled. */
84acb35a 7817
75149521 7818static void
84acb35a
JJ
7819disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7820{
876fa593 7821 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7822 int disabled_shlib_breaks = 0;
7823
c86cf029
VP
7824 /* SunOS a.out shared libraries are always mapped, so do not
7825 disable breakpoints; they will only be reported as unloaded
7826 through clear_solib when GDB discards its shared library
7827 list. See clear_solib for more information. */
7828 if (exec_bfd != NULL
7829 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7830 return;
7831
876fa593 7832 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7833 {
2bdf28a0 7834 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7835 struct breakpoint *b = loc->owner;
cc59ec59 7836
1e4d1764 7837 if (solib->pspace == loc->pspace
e2dd7057 7838 && !loc->shlib_disabled
1e4d1764
YQ
7839 && (((b->type == bp_breakpoint
7840 || b->type == bp_jit_event
7841 || b->type == bp_hardware_breakpoint)
7842 && (loc->loc_type == bp_loc_hardware_breakpoint
7843 || loc->loc_type == bp_loc_software_breakpoint))
7844 || is_tracepoint (b))
e2dd7057 7845 && solib_contains_address_p (solib, loc->address))
84acb35a 7846 {
e2dd7057
PP
7847 loc->shlib_disabled = 1;
7848 /* At this point, we cannot rely on remove_breakpoint
7849 succeeding so we must mark the breakpoint as not inserted
7850 to prevent future errors occurring in remove_breakpoints. */
7851 loc->inserted = 0;
8d3788bd
VP
7852
7853 /* This may cause duplicate notifications for the same breakpoint. */
7854 observer_notify_breakpoint_modified (b);
7855
e2dd7057
PP
7856 if (!disabled_shlib_breaks)
7857 {
7858 target_terminal_ours_for_output ();
3e43a32a
MS
7859 warning (_("Temporarily disabling breakpoints "
7860 "for unloaded shared library \"%s\""),
e2dd7057 7861 solib->so_name);
84acb35a 7862 }
e2dd7057 7863 disabled_shlib_breaks = 1;
84acb35a
JJ
7864 }
7865 }
84acb35a
JJ
7866}
7867
63644780
NB
7868/* Disable any breakpoints and tracepoints in OBJFILE upon
7869 notification of free_objfile. Only apply to enabled breakpoints,
7870 disabled ones can just stay disabled. */
7871
7872static void
7873disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7874{
7875 struct breakpoint *b;
7876
7877 if (objfile == NULL)
7878 return;
7879
d03de421
PA
7880 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7881 managed by the user with add-symbol-file/remove-symbol-file.
7882 Similarly to how breakpoints in shared libraries are handled in
7883 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7884 shlib_disabled so they end up uninserted on the next global
7885 location list update. Shared libraries not loaded by the user
7886 aren't handled here -- they're already handled in
7887 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7888 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7889 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7890 main objfile). */
7891 if ((objfile->flags & OBJF_SHARED) == 0
7892 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7893 return;
7894
7895 ALL_BREAKPOINTS (b)
7896 {
7897 struct bp_location *loc;
7898 int bp_modified = 0;
7899
7900 if (!is_breakpoint (b) && !is_tracepoint (b))
7901 continue;
7902
7903 for (loc = b->loc; loc != NULL; loc = loc->next)
7904 {
7905 CORE_ADDR loc_addr = loc->address;
7906
7907 if (loc->loc_type != bp_loc_hardware_breakpoint
7908 && loc->loc_type != bp_loc_software_breakpoint)
7909 continue;
7910
7911 if (loc->shlib_disabled != 0)
7912 continue;
7913
7914 if (objfile->pspace != loc->pspace)
7915 continue;
7916
7917 if (loc->loc_type != bp_loc_hardware_breakpoint
7918 && loc->loc_type != bp_loc_software_breakpoint)
7919 continue;
7920
7921 if (is_addr_in_objfile (loc_addr, objfile))
7922 {
7923 loc->shlib_disabled = 1;
08351840
PA
7924 /* At this point, we don't know whether the object was
7925 unmapped from the inferior or not, so leave the
7926 inserted flag alone. We'll handle failure to
7927 uninsert quietly, in case the object was indeed
7928 unmapped. */
63644780
NB
7929
7930 mark_breakpoint_location_modified (loc);
7931
7932 bp_modified = 1;
7933 }
7934 }
7935
7936 if (bp_modified)
7937 observer_notify_breakpoint_modified (b);
7938 }
7939}
7940
ce78b96d
JB
7941/* FORK & VFORK catchpoints. */
7942
e29a4733
PA
7943/* An instance of this type is used to represent a fork or vfork
7944 catchpoint. It includes a "struct breakpoint" as a kind of base
7945 class; users downcast to "struct breakpoint *" when needed. A
7946 breakpoint is really of this type iff its ops pointer points to
7947 CATCH_FORK_BREAKPOINT_OPS. */
7948
7949struct fork_catchpoint
7950{
7951 /* The base class. */
7952 struct breakpoint base;
7953
7954 /* Process id of a child process whose forking triggered this
7955 catchpoint. This field is only valid immediately after this
7956 catchpoint has triggered. */
7957 ptid_t forked_inferior_pid;
7958};
7959
4a64f543
MS
7960/* Implement the "insert" breakpoint_ops method for fork
7961 catchpoints. */
ce78b96d 7962
77b06cd7
TJB
7963static int
7964insert_catch_fork (struct bp_location *bl)
ce78b96d 7965{
dfd4cc63 7966 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7967}
7968
4a64f543
MS
7969/* Implement the "remove" breakpoint_ops method for fork
7970 catchpoints. */
ce78b96d
JB
7971
7972static int
77b06cd7 7973remove_catch_fork (struct bp_location *bl)
ce78b96d 7974{
dfd4cc63 7975 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7976}
7977
7978/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7979 catchpoints. */
7980
7981static int
f1310107 7982breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7983 struct address_space *aspace, CORE_ADDR bp_addr,
7984 const struct target_waitstatus *ws)
ce78b96d 7985{
e29a4733
PA
7986 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7987
f90263c1
TT
7988 if (ws->kind != TARGET_WAITKIND_FORKED)
7989 return 0;
7990
7991 c->forked_inferior_pid = ws->value.related_pid;
7992 return 1;
ce78b96d
JB
7993}
7994
4a64f543
MS
7995/* Implement the "print_it" breakpoint_ops method for fork
7996 catchpoints. */
ce78b96d
JB
7997
7998static enum print_stop_action
348d480f 7999print_it_catch_fork (bpstat bs)
ce78b96d 8000{
36dfb11c 8001 struct ui_out *uiout = current_uiout;
348d480f
PA
8002 struct breakpoint *b = bs->breakpoint_at;
8003 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8004
ce78b96d 8005 annotate_catchpoint (b->number);
36dfb11c
TT
8006 if (b->disposition == disp_del)
8007 ui_out_text (uiout, "\nTemporary catchpoint ");
8008 else
8009 ui_out_text (uiout, "\nCatchpoint ");
8010 if (ui_out_is_mi_like_p (uiout))
8011 {
8012 ui_out_field_string (uiout, "reason",
8013 async_reason_lookup (EXEC_ASYNC_FORK));
8014 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8015 }
8016 ui_out_field_int (uiout, "bkptno", b->number);
8017 ui_out_text (uiout, " (forked process ");
8018 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8019 ui_out_text (uiout, "), ");
ce78b96d
JB
8020 return PRINT_SRC_AND_LOC;
8021}
8022
4a64f543
MS
8023/* Implement the "print_one" breakpoint_ops method for fork
8024 catchpoints. */
ce78b96d
JB
8025
8026static void
a6d9a66e 8027print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8028{
e29a4733 8029 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8030 struct value_print_options opts;
79a45e25 8031 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8032
8033 get_user_print_options (&opts);
8034
4a64f543
MS
8035 /* Field 4, the address, is omitted (which makes the columns not
8036 line up too nicely with the headers, but the effect is relatively
8037 readable). */
79a45b7d 8038 if (opts.addressprint)
ce78b96d
JB
8039 ui_out_field_skip (uiout, "addr");
8040 annotate_field (5);
8041 ui_out_text (uiout, "fork");
e29a4733 8042 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8043 {
8044 ui_out_text (uiout, ", process ");
8045 ui_out_field_int (uiout, "what",
e29a4733 8046 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8047 ui_out_spaces (uiout, 1);
8048 }
8ac3646f
TT
8049
8050 if (ui_out_is_mi_like_p (uiout))
8051 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8052}
8053
8054/* Implement the "print_mention" breakpoint_ops method for fork
8055 catchpoints. */
8056
8057static void
8058print_mention_catch_fork (struct breakpoint *b)
8059{
8060 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8061}
8062
6149aea9
PA
8063/* Implement the "print_recreate" breakpoint_ops method for fork
8064 catchpoints. */
8065
8066static void
8067print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8068{
8069 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8070 print_recreate_thread (b, fp);
6149aea9
PA
8071}
8072
ce78b96d
JB
8073/* The breakpoint_ops structure to be used in fork catchpoints. */
8074
2060206e 8075static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8076
4a64f543
MS
8077/* Implement the "insert" breakpoint_ops method for vfork
8078 catchpoints. */
ce78b96d 8079
77b06cd7
TJB
8080static int
8081insert_catch_vfork (struct bp_location *bl)
ce78b96d 8082{
dfd4cc63 8083 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8084}
8085
4a64f543
MS
8086/* Implement the "remove" breakpoint_ops method for vfork
8087 catchpoints. */
ce78b96d
JB
8088
8089static int
77b06cd7 8090remove_catch_vfork (struct bp_location *bl)
ce78b96d 8091{
dfd4cc63 8092 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8093}
8094
8095/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8096 catchpoints. */
8097
8098static int
f1310107 8099breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8100 struct address_space *aspace, CORE_ADDR bp_addr,
8101 const struct target_waitstatus *ws)
ce78b96d 8102{
e29a4733
PA
8103 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8104
f90263c1
TT
8105 if (ws->kind != TARGET_WAITKIND_VFORKED)
8106 return 0;
8107
8108 c->forked_inferior_pid = ws->value.related_pid;
8109 return 1;
ce78b96d
JB
8110}
8111
4a64f543
MS
8112/* Implement the "print_it" breakpoint_ops method for vfork
8113 catchpoints. */
ce78b96d
JB
8114
8115static enum print_stop_action
348d480f 8116print_it_catch_vfork (bpstat bs)
ce78b96d 8117{
36dfb11c 8118 struct ui_out *uiout = current_uiout;
348d480f 8119 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8120 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8121
ce78b96d 8122 annotate_catchpoint (b->number);
36dfb11c
TT
8123 if (b->disposition == disp_del)
8124 ui_out_text (uiout, "\nTemporary catchpoint ");
8125 else
8126 ui_out_text (uiout, "\nCatchpoint ");
8127 if (ui_out_is_mi_like_p (uiout))
8128 {
8129 ui_out_field_string (uiout, "reason",
8130 async_reason_lookup (EXEC_ASYNC_VFORK));
8131 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8132 }
8133 ui_out_field_int (uiout, "bkptno", b->number);
8134 ui_out_text (uiout, " (vforked process ");
8135 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8136 ui_out_text (uiout, "), ");
ce78b96d
JB
8137 return PRINT_SRC_AND_LOC;
8138}
8139
4a64f543
MS
8140/* Implement the "print_one" breakpoint_ops method for vfork
8141 catchpoints. */
ce78b96d
JB
8142
8143static void
a6d9a66e 8144print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8145{
e29a4733 8146 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8147 struct value_print_options opts;
79a45e25 8148 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8149
8150 get_user_print_options (&opts);
4a64f543
MS
8151 /* Field 4, the address, is omitted (which makes the columns not
8152 line up too nicely with the headers, but the effect is relatively
8153 readable). */
79a45b7d 8154 if (opts.addressprint)
ce78b96d
JB
8155 ui_out_field_skip (uiout, "addr");
8156 annotate_field (5);
8157 ui_out_text (uiout, "vfork");
e29a4733 8158 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8159 {
8160 ui_out_text (uiout, ", process ");
8161 ui_out_field_int (uiout, "what",
e29a4733 8162 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8163 ui_out_spaces (uiout, 1);
8164 }
8ac3646f
TT
8165
8166 if (ui_out_is_mi_like_p (uiout))
8167 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8168}
8169
8170/* Implement the "print_mention" breakpoint_ops method for vfork
8171 catchpoints. */
8172
8173static void
8174print_mention_catch_vfork (struct breakpoint *b)
8175{
8176 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8177}
8178
6149aea9
PA
8179/* Implement the "print_recreate" breakpoint_ops method for vfork
8180 catchpoints. */
8181
8182static void
8183print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8184{
8185 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8186 print_recreate_thread (b, fp);
6149aea9
PA
8187}
8188
ce78b96d
JB
8189/* The breakpoint_ops structure to be used in vfork catchpoints. */
8190
2060206e 8191static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8192
edcc5120
TT
8193/* An instance of this type is used to represent an solib catchpoint.
8194 It includes a "struct breakpoint" as a kind of base class; users
8195 downcast to "struct breakpoint *" when needed. A breakpoint is
8196 really of this type iff its ops pointer points to
8197 CATCH_SOLIB_BREAKPOINT_OPS. */
8198
8199struct solib_catchpoint
8200{
8201 /* The base class. */
8202 struct breakpoint base;
8203
8204 /* True for "catch load", false for "catch unload". */
8205 unsigned char is_load;
8206
8207 /* Regular expression to match, if any. COMPILED is only valid when
8208 REGEX is non-NULL. */
8209 char *regex;
8210 regex_t compiled;
8211};
8212
8213static void
8214dtor_catch_solib (struct breakpoint *b)
8215{
8216 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8217
8218 if (self->regex)
8219 regfree (&self->compiled);
8220 xfree (self->regex);
8221
8222 base_breakpoint_ops.dtor (b);
8223}
8224
8225static int
8226insert_catch_solib (struct bp_location *ignore)
8227{
8228 return 0;
8229}
8230
8231static int
8232remove_catch_solib (struct bp_location *ignore)
8233{
8234 return 0;
8235}
8236
8237static int
8238breakpoint_hit_catch_solib (const struct bp_location *bl,
8239 struct address_space *aspace,
8240 CORE_ADDR bp_addr,
8241 const struct target_waitstatus *ws)
8242{
8243 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8244 struct breakpoint *other;
8245
8246 if (ws->kind == TARGET_WAITKIND_LOADED)
8247 return 1;
8248
8249 ALL_BREAKPOINTS (other)
8250 {
8251 struct bp_location *other_bl;
8252
8253 if (other == bl->owner)
8254 continue;
8255
8256 if (other->type != bp_shlib_event)
8257 continue;
8258
8259 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8260 continue;
8261
8262 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8263 {
8264 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8265 return 1;
8266 }
8267 }
8268
8269 return 0;
8270}
8271
8272static void
8273check_status_catch_solib (struct bpstats *bs)
8274{
8275 struct solib_catchpoint *self
8276 = (struct solib_catchpoint *) bs->breakpoint_at;
8277 int ix;
8278
8279 if (self->is_load)
8280 {
8281 struct so_list *iter;
8282
8283 for (ix = 0;
8284 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8285 ix, iter);
8286 ++ix)
8287 {
8288 if (!self->regex
8289 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8290 return;
8291 }
8292 }
8293 else
8294 {
8295 char *iter;
8296
8297 for (ix = 0;
8298 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8299 ix, iter);
8300 ++ix)
8301 {
8302 if (!self->regex
8303 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8304 return;
8305 }
8306 }
8307
8308 bs->stop = 0;
8309 bs->print_it = print_it_noop;
8310}
8311
8312static enum print_stop_action
8313print_it_catch_solib (bpstat bs)
8314{
8315 struct breakpoint *b = bs->breakpoint_at;
8316 struct ui_out *uiout = current_uiout;
8317
8318 annotate_catchpoint (b->number);
8319 if (b->disposition == disp_del)
8320 ui_out_text (uiout, "\nTemporary catchpoint ");
8321 else
8322 ui_out_text (uiout, "\nCatchpoint ");
8323 ui_out_field_int (uiout, "bkptno", b->number);
8324 ui_out_text (uiout, "\n");
8325 if (ui_out_is_mi_like_p (uiout))
8326 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8327 print_solib_event (1);
8328 return PRINT_SRC_AND_LOC;
8329}
8330
8331static void
8332print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8333{
8334 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8335 struct value_print_options opts;
8336 struct ui_out *uiout = current_uiout;
8337 char *msg;
8338
8339 get_user_print_options (&opts);
8340 /* Field 4, the address, is omitted (which makes the columns not
8341 line up too nicely with the headers, but the effect is relatively
8342 readable). */
8343 if (opts.addressprint)
8344 {
8345 annotate_field (4);
8346 ui_out_field_skip (uiout, "addr");
8347 }
8348
8349 annotate_field (5);
8350 if (self->is_load)
8351 {
8352 if (self->regex)
8353 msg = xstrprintf (_("load of library matching %s"), self->regex);
8354 else
8355 msg = xstrdup (_("load of library"));
8356 }
8357 else
8358 {
8359 if (self->regex)
8360 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8361 else
8362 msg = xstrdup (_("unload of library"));
8363 }
8364 ui_out_field_string (uiout, "what", msg);
8365 xfree (msg);
8ac3646f
TT
8366
8367 if (ui_out_is_mi_like_p (uiout))
8368 ui_out_field_string (uiout, "catch-type",
8369 self->is_load ? "load" : "unload");
edcc5120
TT
8370}
8371
8372static void
8373print_mention_catch_solib (struct breakpoint *b)
8374{
8375 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8376
8377 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8378 self->is_load ? "load" : "unload");
8379}
8380
8381static void
8382print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8383{
8384 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8385
8386 fprintf_unfiltered (fp, "%s %s",
8387 b->disposition == disp_del ? "tcatch" : "catch",
8388 self->is_load ? "load" : "unload");
8389 if (self->regex)
8390 fprintf_unfiltered (fp, " %s", self->regex);
8391 fprintf_unfiltered (fp, "\n");
8392}
8393
8394static struct breakpoint_ops catch_solib_breakpoint_ops;
8395
91985142
MG
8396/* Shared helper function (MI and CLI) for creating and installing
8397 a shared object event catchpoint. If IS_LOAD is non-zero then
8398 the events to be caught are load events, otherwise they are
8399 unload events. If IS_TEMP is non-zero the catchpoint is a
8400 temporary one. If ENABLED is non-zero the catchpoint is
8401 created in an enabled state. */
edcc5120 8402
91985142
MG
8403void
8404add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8405{
8406 struct solib_catchpoint *c;
8407 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8408 struct cleanup *cleanup;
8409
edcc5120
TT
8410 if (!arg)
8411 arg = "";
8412 arg = skip_spaces (arg);
8413
8414 c = XCNEW (struct solib_catchpoint);
8415 cleanup = make_cleanup (xfree, c);
8416
8417 if (*arg != '\0')
8418 {
8419 int errcode;
8420
8421 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8422 if (errcode != 0)
8423 {
8424 char *err = get_regcomp_error (errcode, &c->compiled);
8425
8426 make_cleanup (xfree, err);
8427 error (_("Invalid regexp (%s): %s"), err, arg);
8428 }
8429 c->regex = xstrdup (arg);
8430 }
8431
8432 c->is_load = is_load;
91985142 8433 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8434 &catch_solib_breakpoint_ops);
8435
91985142
MG
8436 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8437
edcc5120
TT
8438 discard_cleanups (cleanup);
8439 install_breakpoint (0, &c->base, 1);
8440}
8441
91985142
MG
8442/* A helper function that does all the work for "catch load" and
8443 "catch unload". */
8444
8445static void
8446catch_load_or_unload (char *arg, int from_tty, int is_load,
8447 struct cmd_list_element *command)
8448{
8449 int tempflag;
8450 const int enabled = 1;
8451
8452 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8453
8454 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8455}
8456
edcc5120
TT
8457static void
8458catch_load_command_1 (char *arg, int from_tty,
8459 struct cmd_list_element *command)
8460{
8461 catch_load_or_unload (arg, from_tty, 1, command);
8462}
8463
8464static void
8465catch_unload_command_1 (char *arg, int from_tty,
8466 struct cmd_list_element *command)
8467{
8468 catch_load_or_unload (arg, from_tty, 0, command);
8469}
8470
346774a9
PA
8471/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8472 is non-zero, then make the breakpoint temporary. If COND_STRING is
8473 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8474 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8475
ab04a2af 8476void
346774a9
PA
8477init_catchpoint (struct breakpoint *b,
8478 struct gdbarch *gdbarch, int tempflag,
8479 char *cond_string,
c0a91b2b 8480 const struct breakpoint_ops *ops)
c906108c 8481{
c5aa993b 8482 struct symtab_and_line sal;
346774a9 8483
fe39c653 8484 init_sal (&sal);
6c95b8df 8485 sal.pspace = current_program_space;
c5aa993b 8486
28010a5d 8487 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8488
1b36a34b 8489 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8490 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8491}
8492
28010a5d 8493void
3ea46bff 8494install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8495{
8496 add_to_breakpoint_chain (b);
3a5c3e22 8497 set_breakpoint_number (internal, b);
558a9d82
YQ
8498 if (is_tracepoint (b))
8499 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8500 if (!internal)
8501 mention (b);
c56053d2 8502 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8503
8504 if (update_gll)
44702360 8505 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8506}
8507
9b70b993 8508static void
a6d9a66e
UW
8509create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8510 int tempflag, char *cond_string,
c0a91b2b 8511 const struct breakpoint_ops *ops)
c906108c 8512{
e29a4733 8513 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8514
e29a4733
PA
8515 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8516
8517 c->forked_inferior_pid = null_ptid;
8518
3ea46bff 8519 install_breakpoint (0, &c->base, 1);
c906108c
SS
8520}
8521
fe798b75
JB
8522/* Exec catchpoints. */
8523
b4d90040
PA
8524/* An instance of this type is used to represent an exec catchpoint.
8525 It includes a "struct breakpoint" as a kind of base class; users
8526 downcast to "struct breakpoint *" when needed. A breakpoint is
8527 really of this type iff its ops pointer points to
8528 CATCH_EXEC_BREAKPOINT_OPS. */
8529
8530struct exec_catchpoint
8531{
8532 /* The base class. */
8533 struct breakpoint base;
8534
8535 /* Filename of a program whose exec triggered this catchpoint.
8536 This field is only valid immediately after this catchpoint has
8537 triggered. */
8538 char *exec_pathname;
8539};
8540
8541/* Implement the "dtor" breakpoint_ops method for exec
8542 catchpoints. */
8543
8544static void
8545dtor_catch_exec (struct breakpoint *b)
8546{
8547 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8548
8549 xfree (c->exec_pathname);
348d480f 8550
2060206e 8551 base_breakpoint_ops.dtor (b);
b4d90040
PA
8552}
8553
77b06cd7
TJB
8554static int
8555insert_catch_exec (struct bp_location *bl)
c906108c 8556{
dfd4cc63 8557 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8558}
c906108c 8559
fe798b75 8560static int
77b06cd7 8561remove_catch_exec (struct bp_location *bl)
fe798b75 8562{
dfd4cc63 8563 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8564}
c906108c 8565
fe798b75 8566static int
f1310107 8567breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8568 struct address_space *aspace, CORE_ADDR bp_addr,
8569 const struct target_waitstatus *ws)
fe798b75 8570{
b4d90040
PA
8571 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8572
f90263c1
TT
8573 if (ws->kind != TARGET_WAITKIND_EXECD)
8574 return 0;
8575
8576 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8577 return 1;
fe798b75 8578}
c906108c 8579
fe798b75 8580static enum print_stop_action
348d480f 8581print_it_catch_exec (bpstat bs)
fe798b75 8582{
36dfb11c 8583 struct ui_out *uiout = current_uiout;
348d480f 8584 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8585 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8586
fe798b75 8587 annotate_catchpoint (b->number);
36dfb11c
TT
8588 if (b->disposition == disp_del)
8589 ui_out_text (uiout, "\nTemporary catchpoint ");
8590 else
8591 ui_out_text (uiout, "\nCatchpoint ");
8592 if (ui_out_is_mi_like_p (uiout))
8593 {
8594 ui_out_field_string (uiout, "reason",
8595 async_reason_lookup (EXEC_ASYNC_EXEC));
8596 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8597 }
8598 ui_out_field_int (uiout, "bkptno", b->number);
8599 ui_out_text (uiout, " (exec'd ");
8600 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8601 ui_out_text (uiout, "), ");
8602
fe798b75 8603 return PRINT_SRC_AND_LOC;
c906108c
SS
8604}
8605
fe798b75 8606static void
a6d9a66e 8607print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8608{
b4d90040 8609 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8610 struct value_print_options opts;
79a45e25 8611 struct ui_out *uiout = current_uiout;
fe798b75
JB
8612
8613 get_user_print_options (&opts);
8614
8615 /* Field 4, the address, is omitted (which makes the columns
8616 not line up too nicely with the headers, but the effect
8617 is relatively readable). */
8618 if (opts.addressprint)
8619 ui_out_field_skip (uiout, "addr");
8620 annotate_field (5);
8621 ui_out_text (uiout, "exec");
b4d90040 8622 if (c->exec_pathname != NULL)
fe798b75
JB
8623 {
8624 ui_out_text (uiout, ", program \"");
b4d90040 8625 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8626 ui_out_text (uiout, "\" ");
8627 }
8ac3646f
TT
8628
8629 if (ui_out_is_mi_like_p (uiout))
8630 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8631}
8632
8633static void
8634print_mention_catch_exec (struct breakpoint *b)
8635{
8636 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8637}
8638
6149aea9
PA
8639/* Implement the "print_recreate" breakpoint_ops method for exec
8640 catchpoints. */
8641
8642static void
8643print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8644{
8645 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8646 print_recreate_thread (b, fp);
6149aea9
PA
8647}
8648
2060206e 8649static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8650
c906108c 8651static int
fba45db2 8652hw_breakpoint_used_count (void)
c906108c 8653{
c906108c 8654 int i = 0;
f1310107
TJB
8655 struct breakpoint *b;
8656 struct bp_location *bl;
c906108c
SS
8657
8658 ALL_BREAKPOINTS (b)
c5aa993b 8659 {
d6b74ac4 8660 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8661 for (bl = b->loc; bl; bl = bl->next)
8662 {
8663 /* Special types of hardware breakpoints may use more than
8664 one register. */
348d480f 8665 i += b->ops->resources_needed (bl);
f1310107 8666 }
c5aa993b 8667 }
c906108c
SS
8668
8669 return i;
8670}
8671
a1398e0c
PA
8672/* Returns the resources B would use if it were a hardware
8673 watchpoint. */
8674
c906108c 8675static int
a1398e0c 8676hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8677{
c906108c 8678 int i = 0;
e09342b5 8679 struct bp_location *bl;
c906108c 8680
a1398e0c
PA
8681 if (!breakpoint_enabled (b))
8682 return 0;
8683
8684 for (bl = b->loc; bl; bl = bl->next)
8685 {
8686 /* Special types of hardware watchpoints may use more than
8687 one register. */
8688 i += b->ops->resources_needed (bl);
8689 }
8690
8691 return i;
8692}
8693
8694/* Returns the sum the used resources of all hardware watchpoints of
8695 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8696 the sum of the used resources of all hardware watchpoints of other
8697 types _not_ TYPE. */
8698
8699static int
8700hw_watchpoint_used_count_others (struct breakpoint *except,
8701 enum bptype type, int *other_type_used)
8702{
8703 int i = 0;
8704 struct breakpoint *b;
8705
c906108c
SS
8706 *other_type_used = 0;
8707 ALL_BREAKPOINTS (b)
e09342b5 8708 {
a1398e0c
PA
8709 if (b == except)
8710 continue;
e09342b5
TJB
8711 if (!breakpoint_enabled (b))
8712 continue;
8713
a1398e0c
PA
8714 if (b->type == type)
8715 i += hw_watchpoint_use_count (b);
8716 else if (is_hardware_watchpoint (b))
8717 *other_type_used = 1;
e09342b5
TJB
8718 }
8719
c906108c
SS
8720 return i;
8721}
8722
c906108c 8723void
fba45db2 8724disable_watchpoints_before_interactive_call_start (void)
c906108c 8725{
c5aa993b 8726 struct breakpoint *b;
c906108c
SS
8727
8728 ALL_BREAKPOINTS (b)
c5aa993b 8729 {
cc60f2e3 8730 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8731 {
b5de0fa7 8732 b->enable_state = bp_call_disabled;
44702360 8733 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8734 }
8735 }
c906108c
SS
8736}
8737
8738void
fba45db2 8739enable_watchpoints_after_interactive_call_stop (void)
c906108c 8740{
c5aa993b 8741 struct breakpoint *b;
c906108c
SS
8742
8743 ALL_BREAKPOINTS (b)
c5aa993b 8744 {
cc60f2e3 8745 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8746 {
b5de0fa7 8747 b->enable_state = bp_enabled;
44702360 8748 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8749 }
8750 }
c906108c
SS
8751}
8752
8bea4e01
UW
8753void
8754disable_breakpoints_before_startup (void)
8755{
6c95b8df 8756 current_program_space->executing_startup = 1;
44702360 8757 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8758}
8759
8760void
8761enable_breakpoints_after_startup (void)
8762{
6c95b8df 8763 current_program_space->executing_startup = 0;
f8eba3c6 8764 breakpoint_re_set ();
8bea4e01
UW
8765}
8766
7c16b83e
PA
8767/* Create a new single-step breakpoint for thread THREAD, with no
8768 locations. */
c906108c 8769
7c16b83e
PA
8770static struct breakpoint *
8771new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8772{
8773 struct breakpoint *b = XNEW (struct breakpoint);
8774
8775 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8776 &momentary_breakpoint_ops);
8777
8778 b->disposition = disp_donttouch;
8779 b->frame_id = null_frame_id;
8780
8781 b->thread = thread;
8782 gdb_assert (b->thread != 0);
8783
8784 add_to_breakpoint_chain (b);
8785
8786 return b;
8787}
8788
8789/* Set a momentary breakpoint of type TYPE at address specified by
8790 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8791 frame. */
c906108c
SS
8792
8793struct breakpoint *
a6d9a66e
UW
8794set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8795 struct frame_id frame_id, enum bptype type)
c906108c 8796{
52f0bd74 8797 struct breakpoint *b;
edb3359d 8798
193facb3
JK
8799 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8800 tail-called one. */
8801 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8802
06edf0c0 8803 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8804 b->enable_state = bp_enabled;
8805 b->disposition = disp_donttouch;
818dd999 8806 b->frame_id = frame_id;
c906108c 8807
4a64f543
MS
8808 /* If we're debugging a multi-threaded program, then we want
8809 momentary breakpoints to be active in only a single thread of
8810 control. */
39f77062
KB
8811 if (in_thread_list (inferior_ptid))
8812 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 8813
44702360 8814 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8815
c906108c
SS
8816 return b;
8817}
611c83ae 8818
06edf0c0 8819/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8820 The new breakpoint will have type TYPE, use OPS as its
8821 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8822
06edf0c0
PA
8823static struct breakpoint *
8824momentary_breakpoint_from_master (struct breakpoint *orig,
8825 enum bptype type,
a1aa2221
LM
8826 const struct breakpoint_ops *ops,
8827 int loc_enabled)
e58b0e63
PA
8828{
8829 struct breakpoint *copy;
8830
06edf0c0 8831 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8832 copy->loc = allocate_bp_location (copy);
0e30163f 8833 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8834
a6d9a66e 8835 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8836 copy->loc->requested_address = orig->loc->requested_address;
8837 copy->loc->address = orig->loc->address;
8838 copy->loc->section = orig->loc->section;
6c95b8df 8839 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8840 copy->loc->probe = orig->loc->probe;
f8eba3c6 8841 copy->loc->line_number = orig->loc->line_number;
2f202fde 8842 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8843 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8844 copy->frame_id = orig->frame_id;
8845 copy->thread = orig->thread;
6c95b8df 8846 copy->pspace = orig->pspace;
e58b0e63
PA
8847
8848 copy->enable_state = bp_enabled;
8849 copy->disposition = disp_donttouch;
8850 copy->number = internal_breakpoint_number--;
8851
44702360 8852 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8853 return copy;
8854}
8855
06edf0c0
PA
8856/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8857 ORIG is NULL. */
8858
8859struct breakpoint *
8860clone_momentary_breakpoint (struct breakpoint *orig)
8861{
8862 /* If there's nothing to clone, then return nothing. */
8863 if (orig == NULL)
8864 return NULL;
8865
a1aa2221 8866 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8867}
8868
611c83ae 8869struct breakpoint *
a6d9a66e
UW
8870set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8871 enum bptype type)
611c83ae
PA
8872{
8873 struct symtab_and_line sal;
8874
8875 sal = find_pc_line (pc, 0);
8876 sal.pc = pc;
8877 sal.section = find_pc_overlay (pc);
8878 sal.explicit_pc = 1;
8879
a6d9a66e 8880 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8881}
c906108c 8882\f
c5aa993b 8883
c906108c
SS
8884/* Tell the user we have just set a breakpoint B. */
8885
8886static void
fba45db2 8887mention (struct breakpoint *b)
c906108c 8888{
348d480f 8889 b->ops->print_mention (b);
79a45e25 8890 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 8891 return;
c906108c
SS
8892 printf_filtered ("\n");
8893}
c906108c 8894\f
c5aa993b 8895
1a853c52
PA
8896static int bp_loc_is_permanent (struct bp_location *loc);
8897
0d381245 8898static struct bp_location *
39d61571 8899add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8900 const struct symtab_and_line *sal)
8901{
8902 struct bp_location *loc, **tmp;
3742cc8b
YQ
8903 CORE_ADDR adjusted_address;
8904 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8905
8906 if (loc_gdbarch == NULL)
8907 loc_gdbarch = b->gdbarch;
8908
8909 /* Adjust the breakpoint's address prior to allocating a location.
8910 Once we call allocate_bp_location(), that mostly uninitialized
8911 location will be placed on the location chain. Adjustment of the
8912 breakpoint may cause target_read_memory() to be called and we do
8913 not want its scan of the location chain to find a breakpoint and
8914 location that's only been partially initialized. */
8915 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8916 sal->pc, b->type);
0d381245 8917
d30113d4 8918 /* Sort the locations by their ADDRESS. */
39d61571 8919 loc = allocate_bp_location (b);
d30113d4
JK
8920 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8921 tmp = &((*tmp)->next))
0d381245 8922 ;
d30113d4 8923 loc->next = *tmp;
0d381245 8924 *tmp = loc;
3742cc8b 8925
0d381245 8926 loc->requested_address = sal->pc;
3742cc8b 8927 loc->address = adjusted_address;
6c95b8df 8928 loc->pspace = sal->pspace;
729662a5
TT
8929 loc->probe.probe = sal->probe;
8930 loc->probe.objfile = sal->objfile;
6c95b8df 8931 gdb_assert (loc->pspace != NULL);
0d381245 8932 loc->section = sal->section;
3742cc8b 8933 loc->gdbarch = loc_gdbarch;
f8eba3c6 8934 loc->line_number = sal->line;
2f202fde 8935 loc->symtab = sal->symtab;
f8eba3c6 8936
0e30163f
JK
8937 set_breakpoint_location_function (loc,
8938 sal->explicit_pc || sal->explicit_line);
1a853c52 8939
6ae88661
LM
8940 /* While by definition, permanent breakpoints are already present in the
8941 code, we don't mark the location as inserted. Normally one would expect
8942 that GDB could rely on that breakpoint instruction to stop the program,
8943 thus removing the need to insert its own breakpoint, except that executing
8944 the breakpoint instruction can kill the target instead of reporting a
8945 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8946 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8947 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8948 breakpoint be inserted normally results in QEMU knowing about the GDB
8949 breakpoint, and thus trap before the breakpoint instruction is executed.
8950 (If GDB later needs to continue execution past the permanent breakpoint,
8951 it manually increments the PC, thus avoiding executing the breakpoint
8952 instruction.) */
1a853c52 8953 if (bp_loc_is_permanent (loc))
6ae88661 8954 loc->permanent = 1;
1a853c52 8955
0d381245
VP
8956 return loc;
8957}
514f746b
AR
8958\f
8959
1cf4d951 8960/* See breakpoint.h. */
514f746b 8961
1cf4d951
PA
8962int
8963program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8964{
8965 int len;
8966 CORE_ADDR addr;
1afeeb75 8967 const gdb_byte *bpoint;
514f746b 8968 gdb_byte *target_mem;
939c61fa
JK
8969 struct cleanup *cleanup;
8970 int retval = 0;
514f746b 8971
1cf4d951
PA
8972 addr = address;
8973 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8974
8975 /* Software breakpoints unsupported? */
8976 if (bpoint == NULL)
8977 return 0;
8978
8979 target_mem = alloca (len);
8980
8981 /* Enable the automatic memory restoration from breakpoints while
8982 we read the memory. Otherwise we could say about our temporary
8983 breakpoints they are permanent. */
8984 cleanup = make_show_memory_breakpoints_cleanup (0);
8985
8986 if (target_read_memory (address, target_mem, len) == 0
8987 && memcmp (target_mem, bpoint, len) == 0)
8988 retval = 1;
8989
8990 do_cleanups (cleanup);
8991
8992 return retval;
8993}
8994
8995/* Return 1 if LOC is pointing to a permanent breakpoint,
8996 return 0 otherwise. */
8997
8998static int
8999bp_loc_is_permanent (struct bp_location *loc)
9000{
9001 struct cleanup *cleanup;
9002 int retval;
9003
514f746b
AR
9004 gdb_assert (loc != NULL);
9005
6c95b8df 9006 cleanup = save_current_space_and_thread ();
6c95b8df 9007 switch_to_program_space_and_thread (loc->pspace);
939c61fa 9008
1cf4d951 9009 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b 9010
939c61fa
JK
9011 do_cleanups (cleanup);
9012
9013 return retval;
514f746b
AR
9014}
9015
e7e0cddf
SS
9016/* Build a command list for the dprintf corresponding to the current
9017 settings of the dprintf style options. */
9018
9019static void
9020update_dprintf_command_list (struct breakpoint *b)
9021{
9022 char *dprintf_args = b->extra_string;
9023 char *printf_line = NULL;
9024
9025 if (!dprintf_args)
9026 return;
9027
9028 dprintf_args = skip_spaces (dprintf_args);
9029
9030 /* Allow a comma, as it may have terminated a location, but don't
9031 insist on it. */
9032 if (*dprintf_args == ',')
9033 ++dprintf_args;
9034 dprintf_args = skip_spaces (dprintf_args);
9035
9036 if (*dprintf_args != '"')
9037 error (_("Bad format string, missing '\"'."));
9038
d3ce09f5 9039 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9040 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9041 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9042 {
9043 if (!dprintf_function)
9044 error (_("No function supplied for dprintf call"));
9045
9046 if (dprintf_channel && strlen (dprintf_channel) > 0)
9047 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9048 dprintf_function,
9049 dprintf_channel,
9050 dprintf_args);
9051 else
9052 printf_line = xstrprintf ("call (void) %s (%s)",
9053 dprintf_function,
9054 dprintf_args);
9055 }
d3ce09f5
SS
9056 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9057 {
9058 if (target_can_run_breakpoint_commands ())
9059 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9060 else
9061 {
9062 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9063 printf_line = xstrprintf ("printf %s", dprintf_args);
9064 }
9065 }
e7e0cddf
SS
9066 else
9067 internal_error (__FILE__, __LINE__,
9068 _("Invalid dprintf style."));
9069
f28045c2 9070 gdb_assert (printf_line != NULL);
9d6e6e84 9071 /* Manufacture a printf sequence. */
f28045c2 9072 {
9d6e6e84
HZ
9073 struct command_line *printf_cmd_line
9074 = xmalloc (sizeof (struct command_line));
e7e0cddf 9075
f28045c2
YQ
9076 printf_cmd_line->control_type = simple_control;
9077 printf_cmd_line->body_count = 0;
9078 printf_cmd_line->body_list = NULL;
9d6e6e84 9079 printf_cmd_line->next = NULL;
f28045c2 9080 printf_cmd_line->line = printf_line;
e7e0cddf 9081
f28045c2
YQ
9082 breakpoint_set_commands (b, printf_cmd_line);
9083 }
e7e0cddf
SS
9084}
9085
9086/* Update all dprintf commands, making their command lists reflect
9087 current style settings. */
9088
9089static void
9090update_dprintf_commands (char *args, int from_tty,
9091 struct cmd_list_element *c)
9092{
9093 struct breakpoint *b;
9094
9095 ALL_BREAKPOINTS (b)
9096 {
9097 if (b->type == bp_dprintf)
9098 update_dprintf_command_list (b);
9099 }
9100}
c3f6f71d 9101
f00aae0f
KS
9102/* Create a breakpoint with SAL as location. Use LOCATION
9103 as a description of the location, and COND_STRING
db107f19 9104 as condition expression. */
018d34a4
VP
9105
9106static void
d9b3f62e 9107init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
f00aae0f
KS
9108 struct symtabs_and_lines sals,
9109 struct event_location *location,
f8eba3c6 9110 char *filter, char *cond_string,
e7e0cddf 9111 char *extra_string,
d9b3f62e
PA
9112 enum bptype type, enum bpdisp disposition,
9113 int thread, int task, int ignore_count,
c0a91b2b 9114 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9115 int enabled, int internal, unsigned flags,
9116 int display_canonical)
018d34a4 9117{
0d381245 9118 int i;
018d34a4
VP
9119
9120 if (type == bp_hardware_breakpoint)
9121 {
fbbd034e
AS
9122 int target_resources_ok;
9123
9124 i = hw_breakpoint_used_count ();
9125 target_resources_ok =
9126 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9127 i + 1, 0);
9128 if (target_resources_ok == 0)
9129 error (_("No hardware breakpoint support in the target."));
9130 else if (target_resources_ok < 0)
9131 error (_("Hardware breakpoints used exceeds limit."));
9132 }
9133
6c95b8df
PA
9134 gdb_assert (sals.nelts > 0);
9135
0d381245
VP
9136 for (i = 0; i < sals.nelts; ++i)
9137 {
9138 struct symtab_and_line sal = sals.sals[i];
9139 struct bp_location *loc;
9140
9141 if (from_tty)
5af949e3
UW
9142 {
9143 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9144 if (!loc_gdbarch)
9145 loc_gdbarch = gdbarch;
9146
9147 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9148 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9149 }
0d381245
VP
9150
9151 if (i == 0)
9152 {
d9b3f62e 9153 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9154 b->thread = thread;
4a306c9a 9155 b->task = task;
855a6e68 9156
0d381245 9157 b->cond_string = cond_string;
e7e0cddf 9158 b->extra_string = extra_string;
0d381245 9159 b->ignore_count = ignore_count;
41447f92 9160 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9161 b->disposition = disposition;
6c95b8df 9162
44f238bb
PA
9163 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9164 b->loc->inserted = 1;
9165
0fb4aa4b
PA
9166 if (type == bp_static_tracepoint)
9167 {
d9b3f62e 9168 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9169 struct static_tracepoint_marker marker;
9170
983af33b 9171 if (strace_marker_p (b))
0fb4aa4b
PA
9172 {
9173 /* We already know the marker exists, otherwise, we
9174 wouldn't see a sal for it. */
f00aae0f
KS
9175 const char *p = &event_location_to_string (b->location)[3];
9176 const char *endp;
0fb4aa4b 9177 char *marker_str;
0fb4aa4b 9178
f00aae0f 9179 p = skip_spaces_const (p);
0fb4aa4b 9180
f00aae0f 9181 endp = skip_to_space_const (p);
0fb4aa4b
PA
9182
9183 marker_str = savestring (p, endp - p);
d9b3f62e 9184 t->static_trace_marker_id = marker_str;
0fb4aa4b 9185
3e43a32a
MS
9186 printf_filtered (_("Probed static tracepoint "
9187 "marker \"%s\"\n"),
d9b3f62e 9188 t->static_trace_marker_id);
0fb4aa4b
PA
9189 }
9190 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9191 {
d9b3f62e 9192 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9193 release_static_tracepoint_marker (&marker);
9194
3e43a32a
MS
9195 printf_filtered (_("Probed static tracepoint "
9196 "marker \"%s\"\n"),
d9b3f62e 9197 t->static_trace_marker_id);
0fb4aa4b
PA
9198 }
9199 else
3e43a32a
MS
9200 warning (_("Couldn't determine the static "
9201 "tracepoint marker to probe"));
0fb4aa4b
PA
9202 }
9203
0d381245
VP
9204 loc = b->loc;
9205 }
9206 else
018d34a4 9207 {
39d61571 9208 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9209 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9210 loc->inserted = 1;
0d381245
VP
9211 }
9212
9213 if (b->cond_string)
9214 {
bbc13ae3
KS
9215 const char *arg = b->cond_string;
9216
1bb9788d
TT
9217 loc->cond = parse_exp_1 (&arg, loc->address,
9218 block_for_pc (loc->address), 0);
0d381245 9219 if (*arg)
588ae58c 9220 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9221 }
e7e0cddf
SS
9222
9223 /* Dynamic printf requires and uses additional arguments on the
9224 command line, otherwise it's an error. */
9225 if (type == bp_dprintf)
9226 {
9227 if (b->extra_string)
9228 update_dprintf_command_list (b);
9229 else
9230 error (_("Format string required"));
9231 }
9232 else if (b->extra_string)
588ae58c 9233 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9234 }
018d34a4 9235
56435ebe 9236 b->display_canonical = display_canonical;
f00aae0f
KS
9237 if (location != NULL)
9238 b->location = location;
018d34a4 9239 else
a06efdd6 9240 b->location = new_address_location (b->loc->address);
f8eba3c6 9241 b->filter = filter;
d9b3f62e 9242}
018d34a4 9243
d9b3f62e
PA
9244static void
9245create_breakpoint_sal (struct gdbarch *gdbarch,
f00aae0f
KS
9246 struct symtabs_and_lines sals,
9247 struct event_location *location,
f8eba3c6 9248 char *filter, char *cond_string,
e7e0cddf 9249 char *extra_string,
d9b3f62e
PA
9250 enum bptype type, enum bpdisp disposition,
9251 int thread, int task, int ignore_count,
c0a91b2b 9252 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9253 int enabled, int internal, unsigned flags,
9254 int display_canonical)
d9b3f62e
PA
9255{
9256 struct breakpoint *b;
9257 struct cleanup *old_chain;
9258
9259 if (is_tracepoint_type (type))
9260 {
9261 struct tracepoint *t;
9262
9263 t = XCNEW (struct tracepoint);
9264 b = &t->base;
9265 }
9266 else
9267 b = XNEW (struct breakpoint);
9268
9269 old_chain = make_cleanup (xfree, b);
9270
9271 init_breakpoint_sal (b, gdbarch,
f00aae0f 9272 sals, location,
e7e0cddf 9273 filter, cond_string, extra_string,
d9b3f62e
PA
9274 type, disposition,
9275 thread, task, ignore_count,
9276 ops, from_tty,
44f238bb
PA
9277 enabled, internal, flags,
9278 display_canonical);
d9b3f62e
PA
9279 discard_cleanups (old_chain);
9280
3ea46bff 9281 install_breakpoint (internal, b, 0);
018d34a4
VP
9282}
9283
9284/* Add SALS.nelts breakpoints to the breakpoint table. For each
9285 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9286 value. COND_STRING, if not NULL, specified the condition to be
9287 used for all breakpoints. Essentially the only case where
9288 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9289 function. In that case, it's still not possible to specify
9290 separate conditions for different overloaded functions, so
9291 we take just a single condition string.
9292
c3f6f71d 9293 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9294 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9295 array contents). If the function fails (error() is called), the
9296 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9297 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9298
9299static void
8cdf0e15 9300create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9301 struct linespec_result *canonical,
e7e0cddf 9302 char *cond_string, char *extra_string,
8cdf0e15
VP
9303 enum bptype type, enum bpdisp disposition,
9304 int thread, int task, int ignore_count,
c0a91b2b 9305 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9306 int enabled, int internal, unsigned flags)
c906108c 9307{
018d34a4 9308 int i;
f8eba3c6 9309 struct linespec_sals *lsal;
cc59ec59 9310
f8eba3c6
TT
9311 if (canonical->pre_expanded)
9312 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9313
9314 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9315 {
f00aae0f 9316 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9317 'break', without arguments. */
f00aae0f
KS
9318 struct event_location *location
9319 = (canonical->location != NULL
9320 ? copy_event_location (canonical->location) : NULL);
f8eba3c6 9321 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
f00aae0f 9322 struct cleanup *inner = make_cleanup_delete_event_location (location);
0d381245 9323
f8eba3c6
TT
9324 make_cleanup (xfree, filter_string);
9325 create_breakpoint_sal (gdbarch, lsal->sals,
f00aae0f 9326 location,
f8eba3c6 9327 filter_string,
e7e0cddf
SS
9328 cond_string, extra_string,
9329 type, disposition,
84f4c1fe 9330 thread, task, ignore_count, ops,
44f238bb 9331 from_tty, enabled, internal, flags,
56435ebe 9332 canonical->special_display);
f8eba3c6 9333 discard_cleanups (inner);
c3f6f71d 9334 }
c3f6f71d 9335}
c906108c 9336
f00aae0f 9337/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9338 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9339 addresses found. LOCATION points to the end of the SAL (for
9340 linespec locations).
9998af43
TJB
9341
9342 The array and the line spec strings are allocated on the heap, it is
9343 the caller's responsibility to free them. */
c906108c 9344
b9362cc7 9345static void
f00aae0f 9346parse_breakpoint_sals (const struct event_location *location,
58438ac1 9347 struct linespec_result *canonical)
c3f6f71d 9348{
f00aae0f
KS
9349 struct symtab_and_line cursal;
9350
9351 if (event_location_type (location) == LINESPEC_LOCATION)
9352 {
9353 const char *address = get_linespec_location (location);
9354
9355 if (address == NULL)
9356 {
9357 /* The last displayed codepoint, if it's valid, is our default
9358 breakpoint address. */
9359 if (last_displayed_sal_is_valid ())
9360 {
9361 struct linespec_sals lsal;
9362 struct symtab_and_line sal;
9363 CORE_ADDR pc;
9364
9365 init_sal (&sal); /* Initialize to zeroes. */
9366 lsal.sals.sals = (struct symtab_and_line *)
9367 xmalloc (sizeof (struct symtab_and_line));
9368
9369 /* Set sal's pspace, pc, symtab, and line to the values
9370 corresponding to the last call to print_frame_info.
9371 Be sure to reinitialize LINE with NOTCURRENT == 0
9372 as the breakpoint line number is inappropriate otherwise.
9373 find_pc_line would adjust PC, re-set it back. */
9374 get_last_displayed_sal (&sal);
9375 pc = sal.pc;
9376 sal = find_pc_line (pc, 0);
9377
9378 /* "break" without arguments is equivalent to "break *PC"
9379 where PC is the last displayed codepoint's address. So
9380 make sure to set sal.explicit_pc to prevent GDB from
9381 trying to expand the list of sals to include all other
9382 instances with the same symtab and line. */
9383 sal.pc = pc;
9384 sal.explicit_pc = 1;
9385
9386 lsal.sals.sals[0] = sal;
9387 lsal.sals.nelts = 1;
9388 lsal.canonical = NULL;
9389
9390 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9391 return;
9392 }
9393 else
9394 error (_("No default breakpoint address now."));
c906108c 9395 }
c906108c 9396 }
f00aae0f
KS
9397
9398 /* Force almost all breakpoints to be in terms of the
9399 current_source_symtab (which is decode_line_1's default).
9400 This should produce the results we want almost all of the
9401 time while leaving default_breakpoint_* alone.
9402
9403 ObjC: However, don't match an Objective-C method name which
9404 may have a '+' or '-' succeeded by a '['. */
9405 cursal = get_current_source_symtab_and_line ();
9406 if (last_displayed_sal_is_valid ())
c906108c 9407 {
f00aae0f 9408 const char *address = NULL;
cc80f267 9409
f00aae0f
KS
9410 if (event_location_type (location) == LINESPEC_LOCATION)
9411 address = get_linespec_location (location);
cc80f267 9412
f00aae0f
KS
9413 if (!cursal.symtab
9414 || (address != NULL
9415 && strchr ("+-", address[0]) != NULL
9416 && address[1] != '['))
9417 {
9418 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
9419 get_last_displayed_symtab (),
9420 get_last_displayed_line (),
9421 canonical, NULL, NULL);
9422 return;
9423 }
c906108c 9424 }
f00aae0f
KS
9425
9426 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
9427 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9428}
c906108c 9429
c906108c 9430
c3f6f71d 9431/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9432 inserted as a breakpoint. If it can't throw an error. */
c906108c 9433
b9362cc7 9434static void
23e7acfb 9435breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9436{
9437 int i;
cc59ec59 9438
c3f6f71d 9439 for (i = 0; i < sals->nelts; i++)
ee53e872 9440 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9441}
9442
7a697b8d
SS
9443/* Fast tracepoints may have restrictions on valid locations. For
9444 instance, a fast tracepoint using a jump instead of a trap will
9445 likely have to overwrite more bytes than a trap would, and so can
9446 only be placed where the instruction is longer than the jump, or a
9447 multi-instruction sequence does not have a jump into the middle of
9448 it, etc. */
9449
9450static void
9451check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9452 struct symtabs_and_lines *sals)
9453{
9454 int i, rslt;
9455 struct symtab_and_line *sal;
9456 char *msg;
9457 struct cleanup *old_chain;
9458
9459 for (i = 0; i < sals->nelts; i++)
9460 {
f8eba3c6
TT
9461 struct gdbarch *sarch;
9462
7a697b8d
SS
9463 sal = &sals->sals[i];
9464
f8eba3c6
TT
9465 sarch = get_sal_arch (*sal);
9466 /* We fall back to GDBARCH if there is no architecture
9467 associated with SAL. */
9468 if (sarch == NULL)
9469 sarch = gdbarch;
6b940e6a 9470 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
7a697b8d
SS
9471 old_chain = make_cleanup (xfree, msg);
9472
9473 if (!rslt)
9474 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9475 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9476
9477 do_cleanups (old_chain);
9478 }
9479}
9480
af4908ba
KS
9481/* Issue an invalid thread ID error. */
9482
9483static void ATTRIBUTE_NORETURN
9484invalid_thread_id_error (int id)
9485{
9486 error (_("Unknown thread %d."), id);
9487}
9488
018d34a4
VP
9489/* Given TOK, a string specification of condition and thread, as
9490 accepted by the 'break' command, extract the condition
9491 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9492 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9493 If no condition is found, *COND_STRING is set to NULL.
9494 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9495
9496static void
bbc13ae3 9497find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9498 char **cond_string, int *thread, int *task,
9499 char **rest)
018d34a4
VP
9500{
9501 *cond_string = NULL;
9502 *thread = -1;
ed1d1739
KS
9503 *task = 0;
9504 *rest = NULL;
9505
018d34a4
VP
9506 while (tok && *tok)
9507 {
bbc13ae3 9508 const char *end_tok;
018d34a4 9509 int toklen;
bbc13ae3
KS
9510 const char *cond_start = NULL;
9511 const char *cond_end = NULL;
cc59ec59 9512
bbc13ae3 9513 tok = skip_spaces_const (tok);
e7e0cddf
SS
9514
9515 if ((*tok == '"' || *tok == ',') && rest)
9516 {
9517 *rest = savestring (tok, strlen (tok));
9518 return;
9519 }
9520
bbc13ae3 9521 end_tok = skip_to_space_const (tok);
d634f2de 9522
018d34a4 9523 toklen = end_tok - tok;
d634f2de 9524
018d34a4
VP
9525 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9526 {
f7545552
TT
9527 struct expression *expr;
9528
018d34a4 9529 tok = cond_start = end_tok + 1;
1bb9788d 9530 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9531 xfree (expr);
018d34a4 9532 cond_end = tok;
d634f2de 9533 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9534 }
9535 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9536 {
9537 char *tmptok;
d634f2de 9538
018d34a4 9539 tok = end_tok + 1;
bbc13ae3 9540 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9541 if (tok == tmptok)
9542 error (_("Junk after thread keyword."));
9543 if (!valid_thread_id (*thread))
af4908ba 9544 invalid_thread_id_error (*thread);
bbc13ae3 9545 tok = tmptok;
018d34a4 9546 }
4a306c9a
JB
9547 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9548 {
9549 char *tmptok;
9550
9551 tok = end_tok + 1;
bbc13ae3 9552 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9553 if (tok == tmptok)
9554 error (_("Junk after task keyword."));
9555 if (!valid_task_id (*task))
b6199126 9556 error (_("Unknown task %d."), *task);
bbc13ae3 9557 tok = tmptok;
4a306c9a 9558 }
e7e0cddf
SS
9559 else if (rest)
9560 {
9561 *rest = savestring (tok, strlen (tok));
ccab2054 9562 return;
e7e0cddf 9563 }
018d34a4
VP
9564 else
9565 error (_("Junk at end of arguments."));
9566 }
9567}
9568
0fb4aa4b
PA
9569/* Decode a static tracepoint marker spec. */
9570
9571static struct symtabs_and_lines
f00aae0f 9572decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9573{
9574 VEC(static_tracepoint_marker_p) *markers = NULL;
9575 struct symtabs_and_lines sals;
0fb4aa4b 9576 struct cleanup *old_chain;
f00aae0f
KS
9577 const char *p = &(*arg_p)[3];
9578 const char *endp;
0fb4aa4b
PA
9579 char *marker_str;
9580 int i;
9581
f00aae0f 9582 p = skip_spaces_const (p);
0fb4aa4b 9583
f00aae0f 9584 endp = skip_to_space_const (p);
0fb4aa4b
PA
9585
9586 marker_str = savestring (p, endp - p);
9587 old_chain = make_cleanup (xfree, marker_str);
9588
9589 markers = target_static_tracepoint_markers_by_strid (marker_str);
9590 if (VEC_empty(static_tracepoint_marker_p, markers))
9591 error (_("No known static tracepoint marker named %s"), marker_str);
9592
9593 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9594 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9595
9596 for (i = 0; i < sals.nelts; i++)
9597 {
9598 struct static_tracepoint_marker *marker;
9599
9600 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9601
9602 init_sal (&sals.sals[i]);
9603
9604 sals.sals[i] = find_pc_line (marker->address, 0);
9605 sals.sals[i].pc = marker->address;
9606
9607 release_static_tracepoint_marker (marker);
9608 }
9609
9610 do_cleanups (old_chain);
9611
9612 *arg_p = endp;
9613 return sals;
9614}
9615
f00aae0f 9616/* See breakpoint.h. */
0101ce28 9617
8cdf0e15
VP
9618int
9619create_breakpoint (struct gdbarch *gdbarch,
f00aae0f 9620 const struct event_location *location, char *cond_string,
e7e0cddf 9621 int thread, char *extra_string,
f00aae0f 9622 int parse_extra,
0fb4aa4b 9623 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9624 int ignore_count,
9625 enum auto_boolean pending_break_support,
c0a91b2b 9626 const struct breakpoint_ops *ops,
44f238bb
PA
9627 int from_tty, int enabled, int internal,
9628 unsigned flags)
c3f6f71d 9629{
7efd8fc2 9630 struct linespec_result canonical;
c3f6f71d 9631 struct cleanup *old_chain;
80c99de1 9632 struct cleanup *bkpt_chain = NULL;
0101ce28 9633 int pending = 0;
4a306c9a 9634 int task = 0;
86b17b60 9635 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9636
348d480f
PA
9637 gdb_assert (ops != NULL);
9638
f00aae0f
KS
9639 /* If extra_string isn't useful, set it to NULL. */
9640 if (extra_string != NULL && *extra_string == '\0')
9641 extra_string = NULL;
9642
7efd8fc2 9643 init_linespec_result (&canonical);
c3f6f71d 9644
492d29ea 9645 TRY
b78a6381 9646 {
f00aae0f 9647 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9648 }
492d29ea 9649 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9650 {
492d29ea
PA
9651 /* If caller is interested in rc value from parse, set
9652 value. */
9653 if (e.error == NOT_FOUND_ERROR)
0101ce28 9654 {
05ff989b
AC
9655 /* If pending breakpoint support is turned off, throw
9656 error. */
fa8d40ab
JJ
9657
9658 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9659 throw_exception (e);
9660
9661 exception_print (gdb_stderr, e);
fa8d40ab 9662
05ff989b
AC
9663 /* If pending breakpoint support is auto query and the user
9664 selects no, then simply return the error code. */
059fb39f 9665 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9666 && !nquery (_("Make %s pending on future shared library load? "),
9667 bptype_string (type_wanted)))
fd9b8c24 9668 return 0;
fa8d40ab 9669
05ff989b
AC
9670 /* At this point, either the user was queried about setting
9671 a pending breakpoint and selected yes, or pending
9672 breakpoint behavior is on and thus a pending breakpoint
9673 is defaulted on behalf of the user. */
f00aae0f 9674 pending = 1;
0101ce28 9675 }
492d29ea
PA
9676 else
9677 throw_exception (e);
0101ce28 9678 }
492d29ea
PA
9679 END_CATCH
9680
f00aae0f 9681 if (!pending && VEC_empty (linespec_sals, canonical.sals))
492d29ea 9682 return 0;
c3f6f71d 9683
4a64f543 9684 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9685 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9686
c3f6f71d
JM
9687 /* ----------------------------- SNIP -----------------------------
9688 Anything added to the cleanup chain beyond this point is assumed
9689 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9690 then the memory is not reclaimed. */
9691 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9692
c3f6f71d
JM
9693 /* Resolve all line numbers to PC's and verify that the addresses
9694 are ok for the target. */
0101ce28 9695 if (!pending)
f8eba3c6
TT
9696 {
9697 int ix;
9698 struct linespec_sals *iter;
9699
9700 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9701 breakpoint_sals_to_pc (&iter->sals);
9702 }
c3f6f71d 9703
7a697b8d 9704 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9705 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9706 {
9707 int ix;
9708 struct linespec_sals *iter;
9709
9710 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9711 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9712 }
7a697b8d 9713
c3f6f71d
JM
9714 /* Verify that condition can be parsed, before setting any
9715 breakpoints. Allocate a separate condition expression for each
4a64f543 9716 breakpoint. */
0101ce28 9717 if (!pending)
c3f6f71d 9718 {
f00aae0f 9719 if (parse_extra)
72b2ff0e 9720 {
0878d0fa 9721 char *rest;
52d361e1
YQ
9722 struct linespec_sals *lsal;
9723
9724 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9725
0878d0fa
YQ
9726 /* Here we only parse 'arg' to separate condition
9727 from thread number, so parsing in context of first
9728 sal is OK. When setting the breakpoint we'll
9729 re-parse it in context of each sal. */
9730
f00aae0f
KS
9731 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9732 &cond_string, &thread, &task, &rest);
0878d0fa
YQ
9733 if (cond_string)
9734 make_cleanup (xfree, cond_string);
9735 if (rest)
9736 make_cleanup (xfree, rest);
9737 if (rest)
9738 extra_string = rest;
f00aae0f
KS
9739 else
9740 extra_string = NULL;
72b2ff0e 9741 }
2f069f6f 9742 else
72b2ff0e 9743 {
f00aae0f
KS
9744 if (type_wanted != bp_dprintf
9745 && extra_string != NULL && *extra_string != '\0')
9746 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9747
9748 /* Create a private copy of condition string. */
9749 if (cond_string)
9750 {
9751 cond_string = xstrdup (cond_string);
9752 make_cleanup (xfree, cond_string);
9753 }
9754 /* Create a private copy of any extra string. */
9755 if (extra_string)
9756 {
9757 extra_string = xstrdup (extra_string);
9758 make_cleanup (xfree, extra_string);
9759 }
72b2ff0e 9760 }
0fb4aa4b 9761
52d361e1 9762 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9763 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9764 tempflag ? disp_del : disp_donttouch,
9765 thread, task, ignore_count, ops,
44f238bb 9766 from_tty, enabled, internal, flags);
c906108c 9767 }
0101ce28
JJ
9768 else
9769 {
0101ce28
JJ
9770 struct breakpoint *b;
9771
bfccc43c
YQ
9772 if (is_tracepoint_type (type_wanted))
9773 {
9774 struct tracepoint *t;
9775
9776 t = XCNEW (struct tracepoint);
9777 b = &t->base;
9778 }
9779 else
9780 b = XNEW (struct breakpoint);
9781
9782 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
f00aae0f 9783 b->location = copy_event_location (location);
bfccc43c 9784
f00aae0f
KS
9785 if (parse_extra)
9786 b->cond_string = NULL;
e12c7713
MK
9787 else
9788 {
9789 /* Create a private copy of condition string. */
9790 if (cond_string)
9791 {
9792 cond_string = xstrdup (cond_string);
9793 make_cleanup (xfree, cond_string);
9794 }
9795 b->cond_string = cond_string;
15630549 9796 b->thread = thread;
e12c7713 9797 }
f00aae0f
KS
9798
9799 /* Create a private copy of any extra string. */
9800 if (extra_string != NULL)
9801 {
9802 extra_string = xstrdup (extra_string);
9803 make_cleanup (xfree, extra_string);
9804 }
9805 b->extra_string = extra_string;
0101ce28 9806 b->ignore_count = ignore_count;
0101ce28 9807 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9808 b->condition_not_parsed = 1;
41447f92 9809 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9810 if ((type_wanted != bp_breakpoint
9811 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9812 b->pspace = current_program_space;
8bea4e01 9813
bfccc43c 9814 install_breakpoint (internal, b, 0);
0101ce28
JJ
9815 }
9816
f8eba3c6 9817 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9818 {
3e43a32a
MS
9819 warning (_("Multiple breakpoints were set.\nUse the "
9820 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9821 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9822 }
9823
80c99de1
PA
9824 /* That's it. Discard the cleanups for data inserted into the
9825 breakpoint. */
9826 discard_cleanups (bkpt_chain);
9827 /* But cleanup everything else. */
c3f6f71d 9828 do_cleanups (old_chain);
217dc9e2 9829
80c99de1 9830 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9831 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9832
9833 return 1;
c3f6f71d 9834}
c906108c 9835
348d480f 9836/* Set a breakpoint.
72b2ff0e
VP
9837 ARG is a string describing breakpoint address,
9838 condition, and thread.
9839 FLAG specifies if a breakpoint is hardware on,
9840 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9841 and BP_TEMPFLAG. */
348d480f 9842
98deb0da 9843static void
72b2ff0e 9844break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9845{
72b2ff0e 9846 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9847 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9848 ? bp_hardware_breakpoint
9849 : bp_breakpoint);
55aa24fb 9850 struct breakpoint_ops *ops;
f00aae0f
KS
9851 struct event_location *location;
9852 struct cleanup *cleanup;
9853
9854 location = string_to_event_location (&arg, current_language);
9855 cleanup = make_cleanup_delete_event_location (location);
55aa24fb
SDJ
9856
9857 /* Matching breakpoints on probes. */
5b56227b
KS
9858 if (location != NULL
9859 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
9860 ops = &bkpt_probe_breakpoint_ops;
9861 else
9862 ops = &bkpt_breakpoint_ops;
c3f6f71d 9863
8cdf0e15 9864 create_breakpoint (get_current_arch (),
f00aae0f
KS
9865 location,
9866 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9867 tempflag, type_wanted,
8cdf0e15
VP
9868 0 /* Ignore count */,
9869 pending_break_support,
55aa24fb 9870 ops,
8cdf0e15 9871 from_tty,
84f4c1fe 9872 1 /* enabled */,
44f238bb
PA
9873 0 /* internal */,
9874 0);
f00aae0f 9875 do_cleanups (cleanup);
c906108c
SS
9876}
9877
c906108c
SS
9878/* Helper function for break_command_1 and disassemble_command. */
9879
9880void
fba45db2 9881resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9882{
9883 CORE_ADDR pc;
9884
9885 if (sal->pc == 0 && sal->symtab != NULL)
9886 {
9887 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9888 error (_("No line %d in file \"%s\"."),
05cba821 9889 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9890 sal->pc = pc;
6a048695 9891
4a64f543
MS
9892 /* If this SAL corresponds to a breakpoint inserted using a line
9893 number, then skip the function prologue if necessary. */
6a048695 9894 if (sal->explicit_line)
059acae7 9895 skip_prologue_sal (sal);
c906108c
SS
9896 }
9897
9898 if (sal->section == 0 && sal->symtab != NULL)
9899 {
346d1dfe 9900 const struct blockvector *bv;
3977b71f 9901 const struct block *b;
c5aa993b 9902 struct symbol *sym;
c906108c 9903
43f3e411
DE
9904 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9905 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9906 if (bv != NULL)
9907 {
7f0df278 9908 sym = block_linkage_function (b);
c906108c
SS
9909 if (sym != NULL)
9910 {
eb822aa6
DE
9911 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9912 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9913 sym);
c906108c
SS
9914 }
9915 else
9916 {
4a64f543
MS
9917 /* It really is worthwhile to have the section, so we'll
9918 just have to look harder. This case can be executed
9919 if we have line numbers but no functions (as can
9920 happen in assembly source). */
c906108c 9921
7cbd4a93 9922 struct bound_minimal_symbol msym;
6c95b8df
PA
9923 struct cleanup *old_chain = save_current_space_and_thread ();
9924
9925 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
9926
9927 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9928 if (msym.minsym)
efd66ac6 9929 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
9930
9931 do_cleanups (old_chain);
c906108c
SS
9932 }
9933 }
9934 }
9935}
9936
9937void
fba45db2 9938break_command (char *arg, int from_tty)
c906108c 9939{
db107f19 9940 break_command_1 (arg, 0, from_tty);
c906108c
SS
9941}
9942
c906108c 9943void
fba45db2 9944tbreak_command (char *arg, int from_tty)
c906108c 9945{
db107f19 9946 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9947}
9948
c906108c 9949static void
fba45db2 9950hbreak_command (char *arg, int from_tty)
c906108c 9951{
db107f19 9952 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9953}
9954
9955static void
fba45db2 9956thbreak_command (char *arg, int from_tty)
c906108c 9957{
db107f19 9958 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9959}
9960
9961static void
fba45db2 9962stop_command (char *arg, int from_tty)
c906108c 9963{
a3f17187 9964 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9965Usage: stop in <function | address>\n\
a3f17187 9966 stop at <line>\n"));
c906108c
SS
9967}
9968
9969static void
fba45db2 9970stopin_command (char *arg, int from_tty)
c906108c
SS
9971{
9972 int badInput = 0;
9973
c5aa993b 9974 if (arg == (char *) NULL)
c906108c
SS
9975 badInput = 1;
9976 else if (*arg != '*')
9977 {
9978 char *argptr = arg;
9979 int hasColon = 0;
9980
4a64f543 9981 /* Look for a ':'. If this is a line number specification, then
53a5351d 9982 say it is bad, otherwise, it should be an address or
4a64f543 9983 function/method name. */
c906108c 9984 while (*argptr && !hasColon)
c5aa993b
JM
9985 {
9986 hasColon = (*argptr == ':');
9987 argptr++;
9988 }
c906108c
SS
9989
9990 if (hasColon)
c5aa993b 9991 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9992 else
c5aa993b 9993 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9994 }
9995
9996 if (badInput)
a3f17187 9997 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9998 else
db107f19 9999 break_command_1 (arg, 0, from_tty);
c906108c
SS
10000}
10001
10002static void
fba45db2 10003stopat_command (char *arg, int from_tty)
c906108c
SS
10004{
10005 int badInput = 0;
10006
c5aa993b 10007 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10008 badInput = 1;
10009 else
10010 {
10011 char *argptr = arg;
10012 int hasColon = 0;
10013
4a64f543
MS
10014 /* Look for a ':'. If there is a '::' then get out, otherwise
10015 it is probably a line number. */
c906108c 10016 while (*argptr && !hasColon)
c5aa993b
JM
10017 {
10018 hasColon = (*argptr == ':');
10019 argptr++;
10020 }
c906108c
SS
10021
10022 if (hasColon)
c5aa993b 10023 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10024 else
c5aa993b 10025 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10026 }
10027
10028 if (badInput)
a3f17187 10029 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10030 else
db107f19 10031 break_command_1 (arg, 0, from_tty);
c906108c
SS
10032}
10033
e7e0cddf
SS
10034/* The dynamic printf command is mostly like a regular breakpoint, but
10035 with a prewired command list consisting of a single output command,
10036 built from extra arguments supplied on the dprintf command
10037 line. */
10038
da821c7b 10039static void
e7e0cddf
SS
10040dprintf_command (char *arg, int from_tty)
10041{
f00aae0f
KS
10042 struct event_location *location;
10043 struct cleanup *cleanup;
10044
10045 location = string_to_event_location (&arg, current_language);
10046 cleanup = make_cleanup_delete_event_location (location);
10047
10048 /* If non-NULL, ARG should have been advanced past the location;
10049 the next character must be ','. */
10050 if (arg != NULL)
10051 {
10052 if (arg[0] != ',' || arg[1] == '\0')
10053 error (_("Format string required"));
10054 else
10055 {
10056 /* Skip the comma. */
10057 ++arg;
10058 }
10059 }
10060
e7e0cddf 10061 create_breakpoint (get_current_arch (),
f00aae0f
KS
10062 location,
10063 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
10064 0, bp_dprintf,
10065 0 /* Ignore count */,
10066 pending_break_support,
10067 &dprintf_breakpoint_ops,
10068 from_tty,
10069 1 /* enabled */,
10070 0 /* internal */,
10071 0);
f00aae0f 10072 do_cleanups (cleanup);
e7e0cddf
SS
10073}
10074
d3ce09f5
SS
10075static void
10076agent_printf_command (char *arg, int from_tty)
10077{
10078 error (_("May only run agent-printf on the target"));
10079}
10080
f1310107
TJB
10081/* Implement the "breakpoint_hit" breakpoint_ops method for
10082 ranged breakpoints. */
10083
10084static int
10085breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10086 struct address_space *aspace,
09ac7c10
TT
10087 CORE_ADDR bp_addr,
10088 const struct target_waitstatus *ws)
f1310107 10089{
09ac7c10 10090 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10091 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10092 return 0;
10093
f1310107
TJB
10094 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10095 bl->length, aspace, bp_addr);
10096}
10097
10098/* Implement the "resources_needed" breakpoint_ops method for
10099 ranged breakpoints. */
10100
10101static int
10102resources_needed_ranged_breakpoint (const struct bp_location *bl)
10103{
10104 return target_ranged_break_num_registers ();
10105}
10106
10107/* Implement the "print_it" breakpoint_ops method for
10108 ranged breakpoints. */
10109
10110static enum print_stop_action
348d480f 10111print_it_ranged_breakpoint (bpstat bs)
f1310107 10112{
348d480f 10113 struct breakpoint *b = bs->breakpoint_at;
f1310107 10114 struct bp_location *bl = b->loc;
79a45e25 10115 struct ui_out *uiout = current_uiout;
f1310107
TJB
10116
10117 gdb_assert (b->type == bp_hardware_breakpoint);
10118
10119 /* Ranged breakpoints have only one location. */
10120 gdb_assert (bl && bl->next == NULL);
10121
10122 annotate_breakpoint (b->number);
10123 if (b->disposition == disp_del)
10124 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10125 else
10126 ui_out_text (uiout, "\nRanged breakpoint ");
10127 if (ui_out_is_mi_like_p (uiout))
10128 {
10129 ui_out_field_string (uiout, "reason",
10130 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10131 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10132 }
10133 ui_out_field_int (uiout, "bkptno", b->number);
10134 ui_out_text (uiout, ", ");
10135
10136 return PRINT_SRC_AND_LOC;
10137}
10138
10139/* Implement the "print_one" breakpoint_ops method for
10140 ranged breakpoints. */
10141
10142static void
10143print_one_ranged_breakpoint (struct breakpoint *b,
10144 struct bp_location **last_loc)
10145{
10146 struct bp_location *bl = b->loc;
10147 struct value_print_options opts;
79a45e25 10148 struct ui_out *uiout = current_uiout;
f1310107
TJB
10149
10150 /* Ranged breakpoints have only one location. */
10151 gdb_assert (bl && bl->next == NULL);
10152
10153 get_user_print_options (&opts);
10154
10155 if (opts.addressprint)
10156 /* We don't print the address range here, it will be printed later
10157 by print_one_detail_ranged_breakpoint. */
10158 ui_out_field_skip (uiout, "addr");
10159 annotate_field (5);
10160 print_breakpoint_location (b, bl);
10161 *last_loc = bl;
10162}
10163
10164/* Implement the "print_one_detail" breakpoint_ops method for
10165 ranged breakpoints. */
10166
10167static void
10168print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10169 struct ui_out *uiout)
10170{
10171 CORE_ADDR address_start, address_end;
10172 struct bp_location *bl = b->loc;
f99d8bf4
PA
10173 struct ui_file *stb = mem_fileopen ();
10174 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10175
10176 gdb_assert (bl);
10177
10178 address_start = bl->address;
10179 address_end = address_start + bl->length - 1;
10180
10181 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10182 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10183 print_core_address (bl->gdbarch, address_start),
10184 print_core_address (bl->gdbarch, address_end));
10185 ui_out_field_stream (uiout, "addr", stb);
10186 ui_out_text (uiout, "\n");
10187
10188 do_cleanups (cleanup);
10189}
10190
10191/* Implement the "print_mention" breakpoint_ops method for
10192 ranged breakpoints. */
10193
10194static void
10195print_mention_ranged_breakpoint (struct breakpoint *b)
10196{
10197 struct bp_location *bl = b->loc;
79a45e25 10198 struct ui_out *uiout = current_uiout;
f1310107
TJB
10199
10200 gdb_assert (bl);
10201 gdb_assert (b->type == bp_hardware_breakpoint);
10202
10203 if (ui_out_is_mi_like_p (uiout))
10204 return;
10205
10206 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10207 b->number, paddress (bl->gdbarch, bl->address),
10208 paddress (bl->gdbarch, bl->address + bl->length - 1));
10209}
10210
10211/* Implement the "print_recreate" breakpoint_ops method for
10212 ranged breakpoints. */
10213
10214static void
10215print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10216{
f00aae0f
KS
10217 fprintf_unfiltered (fp, "break-range %s, %s",
10218 event_location_to_string (b->location),
10219 event_location_to_string (b->location_range_end));
d9b3f62e 10220 print_recreate_thread (b, fp);
f1310107
TJB
10221}
10222
10223/* The breakpoint_ops structure to be used in ranged breakpoints. */
10224
2060206e 10225static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10226
10227/* Find the address where the end of the breakpoint range should be
10228 placed, given the SAL of the end of the range. This is so that if
10229 the user provides a line number, the end of the range is set to the
10230 last instruction of the given line. */
10231
10232static CORE_ADDR
10233find_breakpoint_range_end (struct symtab_and_line sal)
10234{
10235 CORE_ADDR end;
10236
10237 /* If the user provided a PC value, use it. Otherwise,
10238 find the address of the end of the given location. */
10239 if (sal.explicit_pc)
10240 end = sal.pc;
10241 else
10242 {
10243 int ret;
10244 CORE_ADDR start;
10245
10246 ret = find_line_pc_range (sal, &start, &end);
10247 if (!ret)
10248 error (_("Could not find location of the end of the range."));
10249
10250 /* find_line_pc_range returns the start of the next line. */
10251 end--;
10252 }
10253
10254 return end;
10255}
10256
10257/* Implement the "break-range" CLI command. */
10258
10259static void
10260break_range_command (char *arg, int from_tty)
10261{
10262 char *arg_start, *addr_string_start, *addr_string_end;
10263 struct linespec_result canonical_start, canonical_end;
10264 int bp_count, can_use_bp, length;
10265 CORE_ADDR end;
10266 struct breakpoint *b;
10267 struct symtab_and_line sal_start, sal_end;
f1310107 10268 struct cleanup *cleanup_bkpt;
f8eba3c6 10269 struct linespec_sals *lsal_start, *lsal_end;
f00aae0f 10270 struct event_location *start_location, *end_location;
f1310107
TJB
10271
10272 /* We don't support software ranged breakpoints. */
10273 if (target_ranged_break_num_registers () < 0)
10274 error (_("This target does not support hardware ranged breakpoints."));
10275
10276 bp_count = hw_breakpoint_used_count ();
10277 bp_count += target_ranged_break_num_registers ();
10278 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10279 bp_count, 0);
10280 if (can_use_bp < 0)
10281 error (_("Hardware breakpoints used exceeds limit."));
10282
f8eba3c6 10283 arg = skip_spaces (arg);
f1310107
TJB
10284 if (arg == NULL || arg[0] == '\0')
10285 error(_("No address range specified."));
10286
f1310107
TJB
10287 init_linespec_result (&canonical_start);
10288
f8eba3c6 10289 arg_start = arg;
f00aae0f
KS
10290 start_location = string_to_event_location (&arg, current_language);
10291 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10292 parse_breakpoint_sals (start_location, &canonical_start);
10293 make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10294
10295 if (arg[0] != ',')
10296 error (_("Too few arguments."));
f8eba3c6 10297 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10298 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10299
10300 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10301
10302 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10303 || lsal_start->sals.nelts != 1)
f1310107
TJB
10304 error (_("Cannot create a ranged breakpoint with multiple locations."));
10305
f8eba3c6
TT
10306 sal_start = lsal_start->sals.sals[0];
10307 addr_string_start = savestring (arg_start, arg - arg_start);
10308 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10309
10310 arg++; /* Skip the comma. */
f8eba3c6 10311 arg = skip_spaces (arg);
f1310107
TJB
10312
10313 /* Parse the end location. */
10314
f1310107
TJB
10315 init_linespec_result (&canonical_end);
10316 arg_start = arg;
10317
f8eba3c6 10318 /* We call decode_line_full directly here instead of using
f1310107
TJB
10319 parse_breakpoint_sals because we need to specify the start location's
10320 symtab and line as the default symtab and line for the end of the
10321 range. This makes it possible to have ranges like "foo.c:27, +14",
10322 where +14 means 14 lines from the start location. */
f00aae0f
KS
10323 end_location = string_to_event_location (&arg, current_language);
10324 make_cleanup_delete_event_location (end_location);
10325 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE,
f8eba3c6
TT
10326 sal_start.symtab, sal_start.line,
10327 &canonical_end, NULL, NULL);
10328
10329 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10330
f8eba3c6 10331 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10332 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10333
10334 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10335 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10336 || lsal_end->sals.nelts != 1)
f1310107
TJB
10337 error (_("Cannot create a ranged breakpoint with multiple locations."));
10338
f8eba3c6 10339 sal_end = lsal_end->sals.sals[0];
f1310107
TJB
10340
10341 end = find_breakpoint_range_end (sal_end);
10342 if (sal_start.pc > end)
177b42fe 10343 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10344
10345 length = end - sal_start.pc + 1;
10346 if (length < 0)
10347 /* Length overflowed. */
10348 error (_("Address range too large."));
10349 else if (length == 1)
10350 {
10351 /* This range is simple enough to be handled by
10352 the `hbreak' command. */
10353 hbreak_command (addr_string_start, 1);
10354
10355 do_cleanups (cleanup_bkpt);
10356
10357 return;
10358 }
10359
10360 /* Now set up the breakpoint. */
10361 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10362 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10363 set_breakpoint_count (breakpoint_count + 1);
10364 b->number = breakpoint_count;
10365 b->disposition = disp_donttouch;
f00aae0f
KS
10366 b->location = copy_event_location (start_location);
10367 b->location_range_end = copy_event_location (end_location);
f1310107
TJB
10368 b->loc->length = length;
10369
f8eba3c6 10370 do_cleanups (cleanup_bkpt);
f1310107
TJB
10371
10372 mention (b);
8d3788bd 10373 observer_notify_breakpoint_created (b);
44702360 10374 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10375}
10376
4a64f543
MS
10377/* Return non-zero if EXP is verified as constant. Returned zero
10378 means EXP is variable. Also the constant detection may fail for
10379 some constant expressions and in such case still falsely return
10380 zero. */
2e6e3d9c 10381
65d79d4b
SDJ
10382static int
10383watchpoint_exp_is_const (const struct expression *exp)
10384{
10385 int i = exp->nelts;
10386
10387 while (i > 0)
10388 {
10389 int oplenp, argsp;
10390
10391 /* We are only interested in the descriptor of each element. */
10392 operator_length (exp, i, &oplenp, &argsp);
10393 i -= oplenp;
10394
10395 switch (exp->elts[i].opcode)
10396 {
10397 case BINOP_ADD:
10398 case BINOP_SUB:
10399 case BINOP_MUL:
10400 case BINOP_DIV:
10401 case BINOP_REM:
10402 case BINOP_MOD:
10403 case BINOP_LSH:
10404 case BINOP_RSH:
10405 case BINOP_LOGICAL_AND:
10406 case BINOP_LOGICAL_OR:
10407 case BINOP_BITWISE_AND:
10408 case BINOP_BITWISE_IOR:
10409 case BINOP_BITWISE_XOR:
10410 case BINOP_EQUAL:
10411 case BINOP_NOTEQUAL:
10412 case BINOP_LESS:
10413 case BINOP_GTR:
10414 case BINOP_LEQ:
10415 case BINOP_GEQ:
10416 case BINOP_REPEAT:
10417 case BINOP_COMMA:
10418 case BINOP_EXP:
10419 case BINOP_MIN:
10420 case BINOP_MAX:
10421 case BINOP_INTDIV:
10422 case BINOP_CONCAT:
65d79d4b
SDJ
10423 case TERNOP_COND:
10424 case TERNOP_SLICE:
65d79d4b
SDJ
10425
10426 case OP_LONG:
10427 case OP_DOUBLE:
10428 case OP_DECFLOAT:
10429 case OP_LAST:
10430 case OP_COMPLEX:
10431 case OP_STRING:
65d79d4b
SDJ
10432 case OP_ARRAY:
10433 case OP_TYPE:
608b4967
TT
10434 case OP_TYPEOF:
10435 case OP_DECLTYPE:
6e72ca20 10436 case OP_TYPEID:
65d79d4b
SDJ
10437 case OP_NAME:
10438 case OP_OBJC_NSSTRING:
10439
10440 case UNOP_NEG:
10441 case UNOP_LOGICAL_NOT:
10442 case UNOP_COMPLEMENT:
10443 case UNOP_ADDR:
10444 case UNOP_HIGH:
aeaa2474 10445 case UNOP_CAST:
9eaf6705
TT
10446
10447 case UNOP_CAST_TYPE:
10448 case UNOP_REINTERPRET_CAST:
10449 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10450 /* Unary, binary and ternary operators: We have to check
10451 their operands. If they are constant, then so is the
10452 result of that operation. For instance, if A and B are
10453 determined to be constants, then so is "A + B".
10454
10455 UNOP_IND is one exception to the rule above, because the
10456 value of *ADDR is not necessarily a constant, even when
10457 ADDR is. */
65d79d4b
SDJ
10458 break;
10459
10460 case OP_VAR_VALUE:
10461 /* Check whether the associated symbol is a constant.
4a64f543 10462
65d79d4b 10463 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10464 possible that a buggy compiler could mark a variable as
10465 constant even when it is not, and TYPE_CONST would return
10466 true in this case, while SYMBOL_CLASS wouldn't.
10467
10468 We also have to check for function symbols because they
10469 are always constant. */
65d79d4b
SDJ
10470 {
10471 struct symbol *s = exp->elts[i + 2].symbol;
10472
10473 if (SYMBOL_CLASS (s) != LOC_BLOCK
10474 && SYMBOL_CLASS (s) != LOC_CONST
10475 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10476 return 0;
10477 break;
10478 }
10479
10480 /* The default action is to return 0 because we are using
10481 the optimistic approach here: If we don't know something,
10482 then it is not a constant. */
10483 default:
10484 return 0;
10485 }
10486 }
10487
10488 return 1;
10489}
10490
3a5c3e22
PA
10491/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10492
10493static void
10494dtor_watchpoint (struct breakpoint *self)
10495{
10496 struct watchpoint *w = (struct watchpoint *) self;
10497
10498 xfree (w->cond_exp);
10499 xfree (w->exp);
10500 xfree (w->exp_string);
10501 xfree (w->exp_string_reparse);
10502 value_free (w->val);
10503
10504 base_breakpoint_ops.dtor (self);
10505}
10506
348d480f
PA
10507/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10508
10509static void
10510re_set_watchpoint (struct breakpoint *b)
10511{
3a5c3e22
PA
10512 struct watchpoint *w = (struct watchpoint *) b;
10513
348d480f
PA
10514 /* Watchpoint can be either on expression using entirely global
10515 variables, or it can be on local variables.
10516
10517 Watchpoints of the first kind are never auto-deleted, and even
10518 persist across program restarts. Since they can use variables
10519 from shared libraries, we need to reparse expression as libraries
10520 are loaded and unloaded.
10521
10522 Watchpoints on local variables can also change meaning as result
10523 of solib event. For example, if a watchpoint uses both a local
10524 and a global variables in expression, it's a local watchpoint,
10525 but unloading of a shared library will make the expression
10526 invalid. This is not a very common use case, but we still
10527 re-evaluate expression, to avoid surprises to the user.
10528
10529 Note that for local watchpoints, we re-evaluate it only if
10530 watchpoints frame id is still valid. If it's not, it means the
10531 watchpoint is out of scope and will be deleted soon. In fact,
10532 I'm not sure we'll ever be called in this case.
10533
10534 If a local watchpoint's frame id is still valid, then
3a5c3e22 10535 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10536
3a5c3e22
PA
10537 Don't do anything about disabled watchpoints, since they will be
10538 reevaluated again when enabled. */
10539 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10540}
10541
77b06cd7
TJB
10542/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10543
10544static int
10545insert_watchpoint (struct bp_location *bl)
10546{
3a5c3e22
PA
10547 struct watchpoint *w = (struct watchpoint *) bl->owner;
10548 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10549
10550 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10551 w->cond_exp);
77b06cd7
TJB
10552}
10553
10554/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10555
10556static int
10557remove_watchpoint (struct bp_location *bl)
10558{
3a5c3e22
PA
10559 struct watchpoint *w = (struct watchpoint *) bl->owner;
10560 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10561
10562 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10563 w->cond_exp);
e09342b5
TJB
10564}
10565
e09342b5 10566static int
348d480f 10567breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10568 struct address_space *aspace, CORE_ADDR bp_addr,
10569 const struct target_waitstatus *ws)
e09342b5 10570{
348d480f 10571 struct breakpoint *b = bl->owner;
3a5c3e22 10572 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10573
348d480f
PA
10574 /* Continuable hardware watchpoints are treated as non-existent if the
10575 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10576 some data address). Otherwise gdb won't stop on a break instruction
10577 in the code (not from a breakpoint) when a hardware watchpoint has
10578 been defined. Also skip watchpoints which we know did not trigger
10579 (did not match the data address). */
10580 if (is_hardware_watchpoint (b)
3a5c3e22 10581 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10582 return 0;
9c06b0b4 10583
348d480f 10584 return 1;
9c06b0b4
TJB
10585}
10586
348d480f
PA
10587static void
10588check_status_watchpoint (bpstat bs)
9c06b0b4 10589{
348d480f 10590 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10591
348d480f 10592 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10593}
10594
10595/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10596 hardware watchpoints. */
9c06b0b4
TJB
10597
10598static int
348d480f 10599resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10600{
3a5c3e22
PA
10601 struct watchpoint *w = (struct watchpoint *) bl->owner;
10602 int length = w->exact? 1 : bl->length;
348d480f
PA
10603
10604 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10605}
10606
10607/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10608 hardware watchpoints. */
9c06b0b4
TJB
10609
10610static int
348d480f 10611works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10612{
efa80663
PA
10613 /* Read and access watchpoints only work with hardware support. */
10614 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10615}
10616
9c06b0b4 10617static enum print_stop_action
348d480f 10618print_it_watchpoint (bpstat bs)
9c06b0b4 10619{
348d480f
PA
10620 struct cleanup *old_chain;
10621 struct breakpoint *b;
f99d8bf4 10622 struct ui_file *stb;
348d480f 10623 enum print_stop_action result;
3a5c3e22 10624 struct watchpoint *w;
79a45e25 10625 struct ui_out *uiout = current_uiout;
348d480f
PA
10626
10627 gdb_assert (bs->bp_location_at != NULL);
10628
348d480f 10629 b = bs->breakpoint_at;
3a5c3e22 10630 w = (struct watchpoint *) b;
348d480f 10631
f99d8bf4
PA
10632 stb = mem_fileopen ();
10633 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10634
10635 switch (b->type)
10636 {
348d480f 10637 case bp_watchpoint:
9c06b0b4
TJB
10638 case bp_hardware_watchpoint:
10639 annotate_watchpoint (b->number);
10640 if (ui_out_is_mi_like_p (uiout))
10641 ui_out_field_string
10642 (uiout, "reason",
10643 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10644 mention (b);
10645 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10646 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10647 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10648 ui_out_field_stream (uiout, "old", stb);
10649 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10650 watchpoint_value_print (w->val, stb);
348d480f
PA
10651 ui_out_field_stream (uiout, "new", stb);
10652 ui_out_text (uiout, "\n");
10653 /* More than one watchpoint may have been triggered. */
10654 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10655 break;
10656
10657 case bp_read_watchpoint:
10658 if (ui_out_is_mi_like_p (uiout))
10659 ui_out_field_string
10660 (uiout, "reason",
10661 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10662 mention (b);
10663 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10664 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10665 watchpoint_value_print (w->val, stb);
348d480f
PA
10666 ui_out_field_stream (uiout, "value", stb);
10667 ui_out_text (uiout, "\n");
10668 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10669 break;
10670
10671 case bp_access_watchpoint:
348d480f
PA
10672 if (bs->old_val != NULL)
10673 {
10674 annotate_watchpoint (b->number);
10675 if (ui_out_is_mi_like_p (uiout))
10676 ui_out_field_string
10677 (uiout, "reason",
10678 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10679 mention (b);
10680 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10681 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10682 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10683 ui_out_field_stream (uiout, "old", stb);
10684 ui_out_text (uiout, "\nNew value = ");
10685 }
10686 else
10687 {
10688 mention (b);
10689 if (ui_out_is_mi_like_p (uiout))
10690 ui_out_field_string
10691 (uiout, "reason",
10692 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10693 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10694 ui_out_text (uiout, "\nValue = ");
10695 }
f99d8bf4 10696 watchpoint_value_print (w->val, stb);
348d480f
PA
10697 ui_out_field_stream (uiout, "new", stb);
10698 ui_out_text (uiout, "\n");
10699 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10700 break;
10701 default:
348d480f 10702 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10703 }
10704
348d480f
PA
10705 do_cleanups (old_chain);
10706 return result;
10707}
10708
10709/* Implement the "print_mention" breakpoint_ops method for hardware
10710 watchpoints. */
10711
10712static void
10713print_mention_watchpoint (struct breakpoint *b)
10714{
10715 struct cleanup *ui_out_chain;
3a5c3e22 10716 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10717 struct ui_out *uiout = current_uiout;
348d480f
PA
10718
10719 switch (b->type)
10720 {
10721 case bp_watchpoint:
10722 ui_out_text (uiout, "Watchpoint ");
10723 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10724 break;
10725 case bp_hardware_watchpoint:
10726 ui_out_text (uiout, "Hardware watchpoint ");
10727 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10728 break;
10729 case bp_read_watchpoint:
10730 ui_out_text (uiout, "Hardware read watchpoint ");
10731 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10732 break;
10733 case bp_access_watchpoint:
10734 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10735 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10736 break;
10737 default:
10738 internal_error (__FILE__, __LINE__,
10739 _("Invalid hardware watchpoint type."));
10740 }
10741
10742 ui_out_field_int (uiout, "number", b->number);
10743 ui_out_text (uiout, ": ");
3a5c3e22 10744 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10745 do_cleanups (ui_out_chain);
10746}
10747
10748/* Implement the "print_recreate" breakpoint_ops method for
10749 watchpoints. */
10750
10751static void
10752print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10753{
3a5c3e22
PA
10754 struct watchpoint *w = (struct watchpoint *) b;
10755
348d480f
PA
10756 switch (b->type)
10757 {
10758 case bp_watchpoint:
10759 case bp_hardware_watchpoint:
10760 fprintf_unfiltered (fp, "watch");
10761 break;
10762 case bp_read_watchpoint:
10763 fprintf_unfiltered (fp, "rwatch");
10764 break;
10765 case bp_access_watchpoint:
10766 fprintf_unfiltered (fp, "awatch");
10767 break;
10768 default:
10769 internal_error (__FILE__, __LINE__,
10770 _("Invalid watchpoint type."));
10771 }
10772
3a5c3e22 10773 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10774 print_recreate_thread (b, fp);
348d480f
PA
10775}
10776
427cd150
TT
10777/* Implement the "explains_signal" breakpoint_ops method for
10778 watchpoints. */
10779
47591c29 10780static int
427cd150
TT
10781explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10782{
10783 /* A software watchpoint cannot cause a signal other than
10784 GDB_SIGNAL_TRAP. */
10785 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10786 return 0;
427cd150 10787
47591c29 10788 return 1;
427cd150
TT
10789}
10790
348d480f
PA
10791/* The breakpoint_ops structure to be used in hardware watchpoints. */
10792
2060206e 10793static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10794
10795/* Implement the "insert" breakpoint_ops method for
10796 masked hardware watchpoints. */
10797
10798static int
10799insert_masked_watchpoint (struct bp_location *bl)
10800{
3a5c3e22
PA
10801 struct watchpoint *w = (struct watchpoint *) bl->owner;
10802
10803 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10804 bl->watchpoint_type);
10805}
10806
10807/* Implement the "remove" breakpoint_ops method for
10808 masked hardware watchpoints. */
10809
10810static int
10811remove_masked_watchpoint (struct bp_location *bl)
10812{
3a5c3e22
PA
10813 struct watchpoint *w = (struct watchpoint *) bl->owner;
10814
10815 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10816 bl->watchpoint_type);
10817}
10818
10819/* Implement the "resources_needed" breakpoint_ops method for
10820 masked hardware watchpoints. */
10821
10822static int
10823resources_needed_masked_watchpoint (const struct bp_location *bl)
10824{
3a5c3e22
PA
10825 struct watchpoint *w = (struct watchpoint *) bl->owner;
10826
10827 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10828}
10829
10830/* Implement the "works_in_software_mode" breakpoint_ops method for
10831 masked hardware watchpoints. */
10832
10833static int
10834works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10835{
10836 return 0;
10837}
10838
10839/* Implement the "print_it" breakpoint_ops method for
10840 masked hardware watchpoints. */
10841
10842static enum print_stop_action
10843print_it_masked_watchpoint (bpstat bs)
10844{
10845 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10846 struct ui_out *uiout = current_uiout;
348d480f
PA
10847
10848 /* Masked watchpoints have only one location. */
10849 gdb_assert (b->loc && b->loc->next == NULL);
10850
10851 switch (b->type)
10852 {
10853 case bp_hardware_watchpoint:
10854 annotate_watchpoint (b->number);
10855 if (ui_out_is_mi_like_p (uiout))
10856 ui_out_field_string
10857 (uiout, "reason",
10858 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10859 break;
10860
10861 case bp_read_watchpoint:
10862 if (ui_out_is_mi_like_p (uiout))
10863 ui_out_field_string
10864 (uiout, "reason",
10865 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10866 break;
10867
10868 case bp_access_watchpoint:
10869 if (ui_out_is_mi_like_p (uiout))
10870 ui_out_field_string
10871 (uiout, "reason",
10872 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10873 break;
10874 default:
10875 internal_error (__FILE__, __LINE__,
10876 _("Invalid hardware watchpoint type."));
10877 }
10878
10879 mention (b);
9c06b0b4
TJB
10880 ui_out_text (uiout, _("\n\
10881Check the underlying instruction at PC for the memory\n\
10882address and value which triggered this watchpoint.\n"));
10883 ui_out_text (uiout, "\n");
10884
10885 /* More than one watchpoint may have been triggered. */
10886 return PRINT_UNKNOWN;
10887}
10888
10889/* Implement the "print_one_detail" breakpoint_ops method for
10890 masked hardware watchpoints. */
10891
10892static void
10893print_one_detail_masked_watchpoint (const struct breakpoint *b,
10894 struct ui_out *uiout)
10895{
3a5c3e22
PA
10896 struct watchpoint *w = (struct watchpoint *) b;
10897
9c06b0b4
TJB
10898 /* Masked watchpoints have only one location. */
10899 gdb_assert (b->loc && b->loc->next == NULL);
10900
10901 ui_out_text (uiout, "\tmask ");
3a5c3e22 10902 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
10903 ui_out_text (uiout, "\n");
10904}
10905
10906/* Implement the "print_mention" breakpoint_ops method for
10907 masked hardware watchpoints. */
10908
10909static void
10910print_mention_masked_watchpoint (struct breakpoint *b)
10911{
3a5c3e22 10912 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10913 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
10914 struct cleanup *ui_out_chain;
10915
10916 switch (b->type)
10917 {
10918 case bp_hardware_watchpoint:
10919 ui_out_text (uiout, "Masked hardware watchpoint ");
10920 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10921 break;
10922 case bp_read_watchpoint:
10923 ui_out_text (uiout, "Masked hardware read watchpoint ");
10924 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10925 break;
10926 case bp_access_watchpoint:
10927 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10928 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10929 break;
10930 default:
10931 internal_error (__FILE__, __LINE__,
10932 _("Invalid hardware watchpoint type."));
10933 }
10934
10935 ui_out_field_int (uiout, "number", b->number);
10936 ui_out_text (uiout, ": ");
3a5c3e22 10937 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
10938 do_cleanups (ui_out_chain);
10939}
10940
10941/* Implement the "print_recreate" breakpoint_ops method for
10942 masked hardware watchpoints. */
10943
10944static void
10945print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10946{
3a5c3e22 10947 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10948 char tmp[40];
10949
10950 switch (b->type)
10951 {
10952 case bp_hardware_watchpoint:
10953 fprintf_unfiltered (fp, "watch");
10954 break;
10955 case bp_read_watchpoint:
10956 fprintf_unfiltered (fp, "rwatch");
10957 break;
10958 case bp_access_watchpoint:
10959 fprintf_unfiltered (fp, "awatch");
10960 break;
10961 default:
10962 internal_error (__FILE__, __LINE__,
10963 _("Invalid hardware watchpoint type."));
10964 }
10965
3a5c3e22
PA
10966 sprintf_vma (tmp, w->hw_wp_mask);
10967 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10968 print_recreate_thread (b, fp);
9c06b0b4
TJB
10969}
10970
10971/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10972
2060206e 10973static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10974
10975/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10976
10977static int
10978is_masked_watchpoint (const struct breakpoint *b)
10979{
10980 return b->ops == &masked_watchpoint_breakpoint_ops;
10981}
10982
53a5351d
JM
10983/* accessflag: hw_write: watch write,
10984 hw_read: watch read,
10985 hw_access: watch access (read or write) */
c906108c 10986static void
bbc13ae3 10987watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10988 int just_location, int internal)
c906108c 10989{
d983da9c 10990 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 10991 struct expression *exp;
270140bd 10992 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10993 struct value *val, *mark, *result;
bb9d5f81 10994 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 10995 struct frame_info *frame;
bbc13ae3
KS
10996 const char *exp_start = NULL;
10997 const char *exp_end = NULL;
10998 const char *tok, *end_tok;
9c06b0b4 10999 int toklen = -1;
bbc13ae3
KS
11000 const char *cond_start = NULL;
11001 const char *cond_end = NULL;
c906108c 11002 enum bptype bp_type;
37e4754d 11003 int thread = -1;
0cf6dd15 11004 int pc = 0;
9c06b0b4
TJB
11005 /* Flag to indicate whether we are going to use masks for
11006 the hardware watchpoint. */
11007 int use_mask = 0;
11008 CORE_ADDR mask = 0;
3a5c3e22 11009 struct watchpoint *w;
bbc13ae3
KS
11010 char *expression;
11011 struct cleanup *back_to;
c906108c 11012
37e4754d
LM
11013 /* Make sure that we actually have parameters to parse. */
11014 if (arg != NULL && arg[0] != '\0')
11015 {
bbc13ae3
KS
11016 const char *value_start;
11017
11018 exp_end = arg + strlen (arg);
37e4754d 11019
9c06b0b4
TJB
11020 /* Look for "parameter value" pairs at the end
11021 of the arguments string. */
bbc13ae3 11022 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11023 {
11024 /* Skip whitespace at the end of the argument list. */
11025 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11026 tok--;
11027
11028 /* Find the beginning of the last token.
11029 This is the value of the parameter. */
11030 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11031 tok--;
11032 value_start = tok + 1;
11033
11034 /* Skip whitespace. */
11035 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11036 tok--;
11037
11038 end_tok = tok;
11039
11040 /* Find the beginning of the second to last token.
11041 This is the parameter itself. */
11042 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11043 tok--;
11044 tok++;
11045 toklen = end_tok - tok + 1;
11046
61012eef 11047 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4
TJB
11048 {
11049 /* At this point we've found a "thread" token, which means
11050 the user is trying to set a watchpoint that triggers
11051 only in a specific thread. */
11052 char *endp;
37e4754d 11053
9c06b0b4
TJB
11054 if (thread != -1)
11055 error(_("You can specify only one thread."));
37e4754d 11056
9c06b0b4
TJB
11057 /* Extract the thread ID from the next token. */
11058 thread = strtol (value_start, &endp, 0);
37e4754d 11059
9c06b0b4
TJB
11060 /* Check if the user provided a valid numeric value for the
11061 thread ID. */
11062 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11063 error (_("Invalid thread ID specification %s."), value_start);
11064
11065 /* Check if the thread actually exists. */
11066 if (!valid_thread_id (thread))
af4908ba 11067 invalid_thread_id_error (thread);
9c06b0b4 11068 }
61012eef 11069 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11070 {
11071 /* We've found a "mask" token, which means the user wants to
11072 create a hardware watchpoint that is going to have the mask
11073 facility. */
11074 struct value *mask_value, *mark;
37e4754d 11075
9c06b0b4
TJB
11076 if (use_mask)
11077 error(_("You can specify only one mask."));
37e4754d 11078
9c06b0b4 11079 use_mask = just_location = 1;
37e4754d 11080
9c06b0b4
TJB
11081 mark = value_mark ();
11082 mask_value = parse_to_comma_and_eval (&value_start);
11083 mask = value_as_address (mask_value);
11084 value_free_to_mark (mark);
11085 }
11086 else
11087 /* We didn't recognize what we found. We should stop here. */
11088 break;
37e4754d 11089
9c06b0b4
TJB
11090 /* Truncate the string and get rid of the "parameter value" pair before
11091 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11092 exp_end = tok;
9c06b0b4 11093 }
37e4754d 11094 }
bbc13ae3
KS
11095 else
11096 exp_end = arg;
37e4754d 11097
bbc13ae3
KS
11098 /* Parse the rest of the arguments. From here on out, everything
11099 is in terms of a newly allocated string instead of the original
11100 ARG. */
c906108c 11101 innermost_block = NULL;
bbc13ae3
KS
11102 expression = savestring (arg, exp_end - arg);
11103 back_to = make_cleanup (xfree, expression);
11104 exp_start = arg = expression;
1bb9788d 11105 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11106 exp_end = arg;
fa8a61dc
TT
11107 /* Remove trailing whitespace from the expression before saving it.
11108 This makes the eventual display of the expression string a bit
11109 prettier. */
11110 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11111 --exp_end;
11112
65d79d4b
SDJ
11113 /* Checking if the expression is not constant. */
11114 if (watchpoint_exp_is_const (exp))
11115 {
11116 int len;
11117
11118 len = exp_end - exp_start;
11119 while (len > 0 && isspace (exp_start[len - 1]))
11120 len--;
11121 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11122 }
11123
c906108c
SS
11124 exp_valid_block = innermost_block;
11125 mark = value_mark ();
3a1115a0 11126 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11127
bb9d5f81
PP
11128 if (val != NULL && just_location)
11129 {
11130 saved_bitpos = value_bitpos (val);
11131 saved_bitsize = value_bitsize (val);
11132 }
11133
06a64a0b
TT
11134 if (just_location)
11135 {
9c06b0b4
TJB
11136 int ret;
11137
06a64a0b 11138 exp_valid_block = NULL;
a1442452 11139 val = value_addr (result);
06a64a0b
TT
11140 release_value (val);
11141 value_free_to_mark (mark);
9c06b0b4
TJB
11142
11143 if (use_mask)
11144 {
11145 ret = target_masked_watch_num_registers (value_as_address (val),
11146 mask);
11147 if (ret == -1)
11148 error (_("This target does not support masked watchpoints."));
11149 else if (ret == -2)
11150 error (_("Invalid mask or memory region."));
11151 }
06a64a0b
TT
11152 }
11153 else if (val != NULL)
fa4727a6 11154 release_value (val);
c906108c 11155
bbc13ae3
KS
11156 tok = skip_spaces_const (arg);
11157 end_tok = skip_to_space_const (tok);
c906108c
SS
11158
11159 toklen = end_tok - tok;
11160 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11161 {
2d134ed3
PA
11162 struct expression *cond;
11163
60e1c644 11164 innermost_block = NULL;
c906108c 11165 tok = cond_start = end_tok + 1;
1bb9788d 11166 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11167
11168 /* The watchpoint expression may not be local, but the condition
11169 may still be. E.g.: `watch global if local > 0'. */
11170 cond_exp_valid_block = innermost_block;
11171
2d134ed3 11172 xfree (cond);
c906108c
SS
11173 cond_end = tok;
11174 }
11175 if (*tok)
8a3fe4f8 11176 error (_("Junk at end of command."));
c906108c 11177
d983da9c 11178 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11179
11180 /* If the expression is "local", then set up a "watchpoint scope"
11181 breakpoint at the point where we've left the scope of the watchpoint
11182 expression. Create the scope breakpoint before the watchpoint, so
11183 that we will encounter it first in bpstat_stop_status. */
60e1c644 11184 if (exp_valid_block && frame)
d983da9c 11185 {
edb3359d
DJ
11186 if (frame_id_p (frame_unwind_caller_id (frame)))
11187 {
11188 scope_breakpoint
a6d9a66e
UW
11189 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11190 frame_unwind_caller_pc (frame),
06edf0c0
PA
11191 bp_watchpoint_scope,
11192 &momentary_breakpoint_ops);
d983da9c 11193
edb3359d 11194 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11195
edb3359d
DJ
11196 /* Automatically delete the breakpoint when it hits. */
11197 scope_breakpoint->disposition = disp_del;
d983da9c 11198
edb3359d
DJ
11199 /* Only break in the proper frame (help with recursion). */
11200 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11201
edb3359d 11202 /* Set the address at which we will stop. */
a6d9a66e
UW
11203 scope_breakpoint->loc->gdbarch
11204 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11205 scope_breakpoint->loc->requested_address
11206 = frame_unwind_caller_pc (frame);
11207 scope_breakpoint->loc->address
a6d9a66e
UW
11208 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11209 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11210 scope_breakpoint->type);
11211 }
d983da9c
DJ
11212 }
11213
e8369a73
AB
11214 /* Now set up the breakpoint. We create all watchpoints as hardware
11215 watchpoints here even if hardware watchpoints are turned off, a call
11216 to update_watchpoint later in this function will cause the type to
11217 drop back to bp_watchpoint (software watchpoint) if required. */
11218
11219 if (accessflag == hw_read)
11220 bp_type = bp_read_watchpoint;
11221 else if (accessflag == hw_access)
11222 bp_type = bp_access_watchpoint;
11223 else
11224 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11225
11226 w = XCNEW (struct watchpoint);
11227 b = &w->base;
348d480f 11228 if (use_mask)
3a5c3e22
PA
11229 init_raw_breakpoint_without_location (b, NULL, bp_type,
11230 &masked_watchpoint_breakpoint_ops);
348d480f 11231 else
3a5c3e22
PA
11232 init_raw_breakpoint_without_location (b, NULL, bp_type,
11233 &watchpoint_breakpoint_ops);
37e4754d 11234 b->thread = thread;
b5de0fa7 11235 b->disposition = disp_donttouch;
348d480f 11236 b->pspace = current_program_space;
3a5c3e22
PA
11237 w->exp = exp;
11238 w->exp_valid_block = exp_valid_block;
11239 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11240 if (just_location)
11241 {
11242 struct type *t = value_type (val);
11243 CORE_ADDR addr = value_as_address (val);
11244 char *name;
11245
11246 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11247 name = type_to_string (t);
11248
3a5c3e22 11249 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11250 core_addr_to_string (addr));
06a64a0b
TT
11251 xfree (name);
11252
3a5c3e22 11253 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11254 (int) (exp_end - exp_start), exp_start);
11255
06a64a0b
TT
11256 /* The above expression is in C. */
11257 b->language = language_c;
11258 }
11259 else
3a5c3e22 11260 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11261
11262 if (use_mask)
11263 {
3a5c3e22 11264 w->hw_wp_mask = mask;
9c06b0b4
TJB
11265 }
11266 else
11267 {
3a5c3e22 11268 w->val = val;
bb9d5f81
PP
11269 w->val_bitpos = saved_bitpos;
11270 w->val_bitsize = saved_bitsize;
3a5c3e22 11271 w->val_valid = 1;
9c06b0b4 11272 }
77b06cd7 11273
c906108c
SS
11274 if (cond_start)
11275 b->cond_string = savestring (cond_start, cond_end - cond_start);
11276 else
11277 b->cond_string = 0;
c5aa993b 11278
c906108c 11279 if (frame)
f6bc2008 11280 {
3a5c3e22
PA
11281 w->watchpoint_frame = get_frame_id (frame);
11282 w->watchpoint_thread = inferior_ptid;
f6bc2008 11283 }
c906108c 11284 else
f6bc2008 11285 {
3a5c3e22
PA
11286 w->watchpoint_frame = null_frame_id;
11287 w->watchpoint_thread = null_ptid;
f6bc2008 11288 }
c906108c 11289
d983da9c 11290 if (scope_breakpoint != NULL)
c906108c 11291 {
d983da9c
DJ
11292 /* The scope breakpoint is related to the watchpoint. We will
11293 need to act on them together. */
11294 b->related_breakpoint = scope_breakpoint;
11295 scope_breakpoint->related_breakpoint = b;
c906108c 11296 }
d983da9c 11297
06a64a0b
TT
11298 if (!just_location)
11299 value_free_to_mark (mark);
2d134ed3 11300
492d29ea 11301 TRY
a9634178
TJB
11302 {
11303 /* Finally update the new watchpoint. This creates the locations
11304 that should be inserted. */
3a5c3e22 11305 update_watchpoint (w, 1);
a9634178 11306 }
492d29ea 11307 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11308 {
11309 delete_breakpoint (b);
11310 throw_exception (e);
11311 }
492d29ea 11312 END_CATCH
a9634178 11313
3ea46bff 11314 install_breakpoint (internal, b, 1);
bbc13ae3 11315 do_cleanups (back_to);
c906108c
SS
11316}
11317
e09342b5 11318/* Return count of debug registers needed to watch the given expression.
e09342b5 11319 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11320
c906108c 11321static int
a9634178 11322can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11323{
11324 int found_memory_cnt = 0;
2e70b7b9 11325 struct value *head = v;
c906108c
SS
11326
11327 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11328 if (!can_use_hw_watchpoints)
c906108c 11329 return 0;
c5aa993b 11330
5c44784c
JM
11331 /* Make sure that the value of the expression depends only upon
11332 memory contents, and values computed from them within GDB. If we
11333 find any register references or function calls, we can't use a
11334 hardware watchpoint.
11335
11336 The idea here is that evaluating an expression generates a series
11337 of values, one holding the value of every subexpression. (The
11338 expression a*b+c has five subexpressions: a, b, a*b, c, and
11339 a*b+c.) GDB's values hold almost enough information to establish
11340 the criteria given above --- they identify memory lvalues,
11341 register lvalues, computed values, etcetera. So we can evaluate
11342 the expression, and then scan the chain of values that leaves
11343 behind to decide whether we can detect any possible change to the
11344 expression's final value using only hardware watchpoints.
11345
11346 However, I don't think that the values returned by inferior
11347 function calls are special in any way. So this function may not
11348 notice that an expression involving an inferior function call
11349 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11350 for (; v; v = value_next (v))
c906108c 11351 {
5c44784c 11352 if (VALUE_LVAL (v) == lval_memory)
c906108c 11353 {
8464be76
DJ
11354 if (v != head && value_lazy (v))
11355 /* A lazy memory lvalue in the chain is one that GDB never
11356 needed to fetch; we either just used its address (e.g.,
11357 `a' in `a.b') or we never needed it at all (e.g., `a'
11358 in `a,b'). This doesn't apply to HEAD; if that is
11359 lazy then it was not readable, but watch it anyway. */
5c44784c 11360 ;
53a5351d 11361 else
5c44784c
JM
11362 {
11363 /* Ahh, memory we actually used! Check if we can cover
11364 it with hardware watchpoints. */
df407dfe 11365 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11366
11367 /* We only watch structs and arrays if user asked for it
11368 explicitly, never if they just happen to appear in a
11369 middle of some value chain. */
11370 if (v == head
11371 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11372 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11373 {
42ae5230 11374 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11375 int len;
11376 int num_regs;
11377
a9634178 11378 len = (target_exact_watchpoints
e09342b5
TJB
11379 && is_scalar_type_recursive (vtype))?
11380 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11381
e09342b5
TJB
11382 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11383 if (!num_regs)
2e70b7b9
MS
11384 return 0;
11385 else
e09342b5 11386 found_memory_cnt += num_regs;
2e70b7b9 11387 }
5c44784c 11388 }
c5aa993b 11389 }
5086187c
AC
11390 else if (VALUE_LVAL (v) != not_lval
11391 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11392 return 0; /* These are values from the history (e.g., $1). */
5086187c 11393 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11394 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11395 }
11396
11397 /* The expression itself looks suitable for using a hardware
11398 watchpoint, but give the target machine a chance to reject it. */
11399 return found_memory_cnt;
11400}
11401
8b93c638 11402void
84f4c1fe 11403watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11404{
84f4c1fe 11405 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11406}
11407
06a64a0b
TT
11408/* A helper function that looks for the "-location" argument and then
11409 calls watch_command_1. */
11410
11411static void
11412watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11413{
11414 int just_location = 0;
11415
11416 if (arg
11417 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11418 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11419 {
e9cafbcc 11420 arg = skip_spaces (arg);
06a64a0b
TT
11421 just_location = 1;
11422 }
11423
84f4c1fe 11424 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11425}
8926118c 11426
c5aa993b 11427static void
fba45db2 11428watch_command (char *arg, int from_tty)
c906108c 11429{
06a64a0b 11430 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11431}
11432
8b93c638 11433void
84f4c1fe 11434rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11435{
84f4c1fe 11436 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11437}
8926118c 11438
c5aa993b 11439static void
fba45db2 11440rwatch_command (char *arg, int from_tty)
c906108c 11441{
06a64a0b 11442 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11443}
11444
8b93c638 11445void
84f4c1fe 11446awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11447{
84f4c1fe 11448 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11449}
8926118c 11450
c5aa993b 11451static void
fba45db2 11452awatch_command (char *arg, int from_tty)
c906108c 11453{
06a64a0b 11454 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11455}
c906108c 11456\f
c5aa993b 11457
43ff13b4 11458/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11459 because it uses the mechanisms of breakpoints. */
11460
bfec99b2
PA
11461struct until_break_command_continuation_args
11462{
11463 struct breakpoint *breakpoint;
11464 struct breakpoint *breakpoint2;
186c406b 11465 int thread_num;
bfec99b2
PA
11466};
11467
43ff13b4 11468/* This function is called by fetch_inferior_event via the
4a64f543 11469 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11470 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11471 command. */
c2c6d25f 11472static void
fa4cd53f 11473until_break_command_continuation (void *arg, int err)
43ff13b4 11474{
bfec99b2
PA
11475 struct until_break_command_continuation_args *a = arg;
11476
11477 delete_breakpoint (a->breakpoint);
11478 if (a->breakpoint2)
11479 delete_breakpoint (a->breakpoint2);
186c406b 11480 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11481}
11482
c906108c 11483void
ae66c1fc 11484until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11485{
11486 struct symtabs_and_lines sals;
11487 struct symtab_and_line sal;
8556afb4
PA
11488 struct frame_info *frame;
11489 struct gdbarch *frame_gdbarch;
11490 struct frame_id stack_frame_id;
11491 struct frame_id caller_frame_id;
c906108c 11492 struct breakpoint *breakpoint;
f107f563 11493 struct breakpoint *breakpoint2 = NULL;
f00aae0f 11494 struct cleanup *old_chain, *cleanup;
186c406b
TT
11495 int thread;
11496 struct thread_info *tp;
f00aae0f 11497 struct event_location *location;
c906108c 11498
70509625 11499 clear_proceed_status (0);
c906108c
SS
11500
11501 /* Set a breakpoint where the user wants it and at return from
4a64f543 11502 this function. */
c5aa993b 11503
f00aae0f
KS
11504 location = string_to_event_location (&arg, current_language);
11505 cleanup = make_cleanup_delete_event_location (location);
11506
1bfeeb0f 11507 if (last_displayed_sal_is_valid ())
f00aae0f 11508 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11509 get_last_displayed_symtab (),
f8eba3c6 11510 get_last_displayed_line ());
c906108c 11511 else
f00aae0f 11512 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
f8eba3c6 11513 (struct symtab *) NULL, 0);
c5aa993b 11514
c906108c 11515 if (sals.nelts != 1)
8a3fe4f8 11516 error (_("Couldn't get information on specified line."));
c5aa993b 11517
c906108c 11518 sal = sals.sals[0];
4a64f543 11519 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11520
c906108c 11521 if (*arg)
8a3fe4f8 11522 error (_("Junk at end of arguments."));
c5aa993b 11523
c906108c 11524 resolve_sal_pc (&sal);
c5aa993b 11525
186c406b
TT
11526 tp = inferior_thread ();
11527 thread = tp->num;
11528
883bc8d1
PA
11529 old_chain = make_cleanup (null_cleanup, NULL);
11530
8556afb4
PA
11531 /* Note linespec handling above invalidates the frame chain.
11532 Installing a breakpoint also invalidates the frame chain (as it
11533 may need to switch threads), so do any frame handling before
11534 that. */
11535
11536 frame = get_selected_frame (NULL);
11537 frame_gdbarch = get_frame_arch (frame);
11538 stack_frame_id = get_stack_frame_id (frame);
11539 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11540
ae66c1fc
EZ
11541 /* Keep within the current frame, or in frames called by the current
11542 one. */
edb3359d 11543
883bc8d1 11544 if (frame_id_p (caller_frame_id))
c906108c 11545 {
883bc8d1
PA
11546 struct symtab_and_line sal2;
11547
11548 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11549 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11550 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11551 sal2,
11552 caller_frame_id,
f107f563
VP
11553 bp_until);
11554 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11555
883bc8d1 11556 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11557 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11558 }
c5aa993b 11559
c70a6932
JK
11560 /* set_momentary_breakpoint could invalidate FRAME. */
11561 frame = NULL;
11562
883bc8d1
PA
11563 if (anywhere)
11564 /* If the user told us to continue until a specified location,
11565 we don't specify a frame at which we need to stop. */
11566 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11567 null_frame_id, bp_until);
11568 else
11569 /* Otherwise, specify the selected frame, because we want to stop
11570 only at the very same frame. */
11571 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11572 stack_frame_id, bp_until);
11573 make_cleanup_delete_breakpoint (breakpoint);
11574
64ce06e4 11575 proceed (-1, GDB_SIGNAL_DEFAULT);
f107f563 11576
4a64f543
MS
11577 /* If we are running asynchronously, and proceed call above has
11578 actually managed to start the target, arrange for breakpoints to
11579 be deleted when the target stops. Otherwise, we're already
11580 stopped and delete breakpoints via cleanup chain. */
f107f563 11581
8ea051c5 11582 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11583 {
bfec99b2
PA
11584 struct until_break_command_continuation_args *args;
11585 args = xmalloc (sizeof (*args));
f107f563 11586
bfec99b2
PA
11587 args->breakpoint = breakpoint;
11588 args->breakpoint2 = breakpoint2;
186c406b 11589 args->thread_num = thread;
f107f563
VP
11590
11591 discard_cleanups (old_chain);
95e54da7
PA
11592 add_continuation (inferior_thread (),
11593 until_break_command_continuation, args,
604ead4a 11594 xfree);
f107f563
VP
11595 }
11596 else
c5aa993b 11597 do_cleanups (old_chain);
f00aae0f
KS
11598
11599 do_cleanups (cleanup);
c906108c 11600}
ae66c1fc 11601
c906108c
SS
11602/* This function attempts to parse an optional "if <cond>" clause
11603 from the arg string. If one is not found, it returns NULL.
c5aa993b 11604
c906108c
SS
11605 Else, it returns a pointer to the condition string. (It does not
11606 attempt to evaluate the string against a particular block.) And,
11607 it updates arg to point to the first character following the parsed
4a64f543 11608 if clause in the arg string. */
53a5351d 11609
916703c0 11610char *
fba45db2 11611ep_parse_optional_if_clause (char **arg)
c906108c 11612{
c5aa993b
JM
11613 char *cond_string;
11614
11615 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11616 return NULL;
c5aa993b 11617
4a64f543 11618 /* Skip the "if" keyword. */
c906108c 11619 (*arg) += 2;
c5aa993b 11620
c906108c 11621 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11622 condition string. */
e9cafbcc 11623 *arg = skip_spaces (*arg);
c906108c 11624 cond_string = *arg;
c5aa993b 11625
4a64f543
MS
11626 /* Assume that the condition occupies the remainder of the arg
11627 string. */
c906108c 11628 (*arg) += strlen (cond_string);
c5aa993b 11629
c906108c
SS
11630 return cond_string;
11631}
c5aa993b 11632
c906108c
SS
11633/* Commands to deal with catching events, such as signals, exceptions,
11634 process start/exit, etc. */
c5aa993b
JM
11635
11636typedef enum
11637{
44feb3ce
TT
11638 catch_fork_temporary, catch_vfork_temporary,
11639 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11640}
11641catch_fork_kind;
11642
c906108c 11643static void
cc59ec59
MS
11644catch_fork_command_1 (char *arg, int from_tty,
11645 struct cmd_list_element *command)
c906108c 11646{
a6d9a66e 11647 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11648 char *cond_string = NULL;
44feb3ce
TT
11649 catch_fork_kind fork_kind;
11650 int tempflag;
11651
11652 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11653 tempflag = (fork_kind == catch_fork_temporary
11654 || fork_kind == catch_vfork_temporary);
c5aa993b 11655
44feb3ce
TT
11656 if (!arg)
11657 arg = "";
e9cafbcc 11658 arg = skip_spaces (arg);
c5aa993b 11659
c906108c 11660 /* The allowed syntax is:
c5aa993b
JM
11661 catch [v]fork
11662 catch [v]fork if <cond>
11663
4a64f543 11664 First, check if there's an if clause. */
c906108c 11665 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11666
c906108c 11667 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11668 error (_("Junk at end of arguments."));
c5aa993b 11669
c906108c 11670 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11671 and enable reporting of such events. */
c5aa993b
JM
11672 switch (fork_kind)
11673 {
44feb3ce
TT
11674 case catch_fork_temporary:
11675 case catch_fork_permanent:
a6d9a66e 11676 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11677 &catch_fork_breakpoint_ops);
c906108c 11678 break;
44feb3ce
TT
11679 case catch_vfork_temporary:
11680 case catch_vfork_permanent:
a6d9a66e 11681 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11682 &catch_vfork_breakpoint_ops);
c906108c 11683 break;
c5aa993b 11684 default:
8a3fe4f8 11685 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11686 break;
c5aa993b 11687 }
c906108c
SS
11688}
11689
11690static void
cc59ec59
MS
11691catch_exec_command_1 (char *arg, int from_tty,
11692 struct cmd_list_element *command)
c906108c 11693{
b4d90040 11694 struct exec_catchpoint *c;
a6d9a66e 11695 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11696 int tempflag;
c5aa993b 11697 char *cond_string = NULL;
c906108c 11698
44feb3ce
TT
11699 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11700
11701 if (!arg)
11702 arg = "";
e9cafbcc 11703 arg = skip_spaces (arg);
c906108c
SS
11704
11705 /* The allowed syntax is:
c5aa993b
JM
11706 catch exec
11707 catch exec if <cond>
c906108c 11708
4a64f543 11709 First, check if there's an if clause. */
c906108c
SS
11710 cond_string = ep_parse_optional_if_clause (&arg);
11711
11712 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11713 error (_("Junk at end of arguments."));
c906108c 11714
b4d90040
PA
11715 c = XNEW (struct exec_catchpoint);
11716 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11717 &catch_exec_breakpoint_ops);
11718 c->exec_pathname = NULL;
11719
3ea46bff 11720 install_breakpoint (0, &c->base, 1);
c906108c 11721}
c5aa993b 11722
9ac4176b 11723void
28010a5d
PA
11724init_ada_exception_breakpoint (struct breakpoint *b,
11725 struct gdbarch *gdbarch,
11726 struct symtab_and_line sal,
11727 char *addr_string,
c0a91b2b 11728 const struct breakpoint_ops *ops,
28010a5d 11729 int tempflag,
349774ef 11730 int enabled,
28010a5d 11731 int from_tty)
f7f9143b 11732{
f7f9143b
JB
11733 if (from_tty)
11734 {
5af949e3
UW
11735 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11736 if (!loc_gdbarch)
11737 loc_gdbarch = gdbarch;
11738
6c95b8df
PA
11739 describe_other_breakpoints (loc_gdbarch,
11740 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11741 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11742 version for exception catchpoints, because two catchpoints
11743 used for different exception names will use the same address.
11744 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11745 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11746 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11747 the user what type of catchpoint it is. The above is good
11748 enough for now, though. */
11749 }
11750
28010a5d 11751 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11752
349774ef 11753 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11754 b->disposition = tempflag ? disp_del : disp_donttouch;
f00aae0f
KS
11755 b->location = string_to_event_location (&addr_string,
11756 language_def (language_ada));
f7f9143b 11757 b->language = language_ada;
f7f9143b
JB
11758}
11759
c906108c 11760static void
fba45db2 11761catch_command (char *arg, int from_tty)
c906108c 11762{
44feb3ce 11763 error (_("Catch requires an event name."));
c906108c
SS
11764}
11765\f
11766
11767static void
fba45db2 11768tcatch_command (char *arg, int from_tty)
c906108c 11769{
44feb3ce 11770 error (_("Catch requires an event name."));
c906108c
SS
11771}
11772
8a2c437b
TT
11773/* A qsort comparison function that sorts breakpoints in order. */
11774
11775static int
11776compare_breakpoints (const void *a, const void *b)
11777{
11778 const breakpoint_p *ba = a;
11779 uintptr_t ua = (uintptr_t) *ba;
11780 const breakpoint_p *bb = b;
11781 uintptr_t ub = (uintptr_t) *bb;
11782
11783 if ((*ba)->number < (*bb)->number)
11784 return -1;
11785 else if ((*ba)->number > (*bb)->number)
11786 return 1;
11787
11788 /* Now sort by address, in case we see, e..g, two breakpoints with
11789 the number 0. */
11790 if (ua < ub)
11791 return -1;
94b0e70d 11792 return ua > ub ? 1 : 0;
8a2c437b
TT
11793}
11794
80f8a6eb 11795/* Delete breakpoints by address or line. */
c906108c
SS
11796
11797static void
fba45db2 11798clear_command (char *arg, int from_tty)
c906108c 11799{
8a2c437b 11800 struct breakpoint *b, *prev;
d6e956e5
VP
11801 VEC(breakpoint_p) *found = 0;
11802 int ix;
c906108c
SS
11803 int default_match;
11804 struct symtabs_and_lines sals;
11805 struct symtab_and_line sal;
c906108c 11806 int i;
8a2c437b 11807 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11808
11809 if (arg)
11810 {
39cf75f7
DE
11811 sals = decode_line_with_current_source (arg,
11812 (DECODE_LINE_FUNFIRSTLINE
11813 | DECODE_LINE_LIST_MODE));
cf4ded82 11814 make_cleanup (xfree, sals.sals);
c906108c
SS
11815 default_match = 0;
11816 }
11817 else
11818 {
c5aa993b 11819 sals.sals = (struct symtab_and_line *)
c906108c 11820 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 11821 make_cleanup (xfree, sals.sals);
4a64f543 11822 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11823
11824 /* Set sal's line, symtab, pc, and pspace to the values
11825 corresponding to the last call to print_frame_info. If the
11826 codepoint is not valid, this will set all the fields to 0. */
11827 get_last_displayed_sal (&sal);
c906108c 11828 if (sal.symtab == 0)
8a3fe4f8 11829 error (_("No source file specified."));
c906108c
SS
11830
11831 sals.sals[0] = sal;
11832 sals.nelts = 1;
11833
11834 default_match = 1;
11835 }
11836
4a64f543
MS
11837 /* We don't call resolve_sal_pc here. That's not as bad as it
11838 seems, because all existing breakpoints typically have both
11839 file/line and pc set. So, if clear is given file/line, we can
11840 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11841
11842 We only support clearing given the address explicitly
11843 present in breakpoint table. Say, we've set breakpoint
4a64f543 11844 at file:line. There were several PC values for that file:line,
ed0616c6 11845 due to optimization, all in one block.
4a64f543
MS
11846
11847 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11848 PC corresponding to the same file:line, the breakpoint won't
11849 be cleared. We probably can still clear the breakpoint, but
11850 since the other PC value is never presented to user, user
11851 can only find it by guessing, and it does not seem important
11852 to support that. */
11853
4a64f543
MS
11854 /* For each line spec given, delete bps which correspond to it. Do
11855 it in two passes, solely to preserve the current behavior that
11856 from_tty is forced true if we delete more than one
11857 breakpoint. */
c906108c 11858
80f8a6eb 11859 found = NULL;
8a2c437b 11860 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11861 for (i = 0; i < sals.nelts; i++)
11862 {
05cba821
JK
11863 const char *sal_fullname;
11864
c906108c 11865 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11866 If line given (pc == 0), clear all bpts on specified line.
11867 If defaulting, clear all bpts on default line
c906108c 11868 or at default pc.
c5aa993b
JM
11869
11870 defaulting sal.pc != 0 tests to do
11871
11872 0 1 pc
11873 1 1 pc _and_ line
11874 0 0 line
11875 1 0 <can't happen> */
c906108c
SS
11876
11877 sal = sals.sals[i];
05cba821
JK
11878 sal_fullname = (sal.symtab == NULL
11879 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11880
4a64f543 11881 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11882 ALL_BREAKPOINTS (b)
c5aa993b 11883 {
0d381245 11884 int match = 0;
4a64f543 11885 /* Are we going to delete b? */
cc60f2e3 11886 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11887 {
11888 struct bp_location *loc = b->loc;
11889 for (; loc; loc = loc->next)
11890 {
f8eba3c6
TT
11891 /* If the user specified file:line, don't allow a PC
11892 match. This matches historical gdb behavior. */
11893 int pc_match = (!sal.explicit_line
11894 && sal.pc
11895 && (loc->pspace == sal.pspace)
11896 && (loc->address == sal.pc)
11897 && (!section_is_overlay (loc->section)
11898 || loc->section == sal.section));
4aac40c8
TT
11899 int line_match = 0;
11900
11901 if ((default_match || sal.explicit_line)
2f202fde 11902 && loc->symtab != NULL
05cba821 11903 && sal_fullname != NULL
4aac40c8 11904 && sal.pspace == loc->pspace
05cba821
JK
11905 && loc->line_number == sal.line
11906 && filename_cmp (symtab_to_fullname (loc->symtab),
11907 sal_fullname) == 0)
11908 line_match = 1;
4aac40c8 11909
0d381245
VP
11910 if (pc_match || line_match)
11911 {
11912 match = 1;
11913 break;
11914 }
11915 }
11916 }
11917
11918 if (match)
d6e956e5 11919 VEC_safe_push(breakpoint_p, found, b);
c906108c 11920 }
80f8a6eb 11921 }
8a2c437b 11922
80f8a6eb 11923 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11924 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11925 {
11926 if (arg)
8a3fe4f8 11927 error (_("No breakpoint at %s."), arg);
80f8a6eb 11928 else
8a3fe4f8 11929 error (_("No breakpoint at this line."));
80f8a6eb 11930 }
c906108c 11931
8a2c437b
TT
11932 /* Remove duplicates from the vec. */
11933 qsort (VEC_address (breakpoint_p, found),
11934 VEC_length (breakpoint_p, found),
11935 sizeof (breakpoint_p),
11936 compare_breakpoints);
11937 prev = VEC_index (breakpoint_p, found, 0);
11938 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11939 {
11940 if (b == prev)
11941 {
11942 VEC_ordered_remove (breakpoint_p, found, ix);
11943 --ix;
11944 }
11945 }
11946
d6e956e5 11947 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11948 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11949 if (from_tty)
a3f17187 11950 {
d6e956e5 11951 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11952 printf_unfiltered (_("Deleted breakpoint "));
11953 else
11954 printf_unfiltered (_("Deleted breakpoints "));
11955 }
d6e956e5
VP
11956
11957 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11958 {
c5aa993b 11959 if (from_tty)
d6e956e5
VP
11960 printf_unfiltered ("%d ", b->number);
11961 delete_breakpoint (b);
c906108c 11962 }
80f8a6eb
MS
11963 if (from_tty)
11964 putchar_unfiltered ('\n');
8a2c437b
TT
11965
11966 do_cleanups (cleanups);
c906108c
SS
11967}
11968\f
11969/* Delete breakpoint in BS if they are `delete' breakpoints and
11970 all breakpoints that are marked for deletion, whether hit or not.
11971 This is called after any breakpoint is hit, or after errors. */
11972
11973void
fba45db2 11974breakpoint_auto_delete (bpstat bs)
c906108c 11975{
35df4500 11976 struct breakpoint *b, *b_tmp;
c906108c
SS
11977
11978 for (; bs; bs = bs->next)
f431efe5
PA
11979 if (bs->breakpoint_at
11980 && bs->breakpoint_at->disposition == disp_del
c906108c 11981 && bs->stop)
f431efe5 11982 delete_breakpoint (bs->breakpoint_at);
c906108c 11983
35df4500 11984 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11985 {
b5de0fa7 11986 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11987 delete_breakpoint (b);
11988 }
c906108c
SS
11989}
11990
4a64f543
MS
11991/* A comparison function for bp_location AP and BP being interfaced to
11992 qsort. Sort elements primarily by their ADDRESS (no matter what
11993 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 11994 secondarily by ordering first permanent elements and
4a64f543 11995 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11996 qsort being an unstable algorithm. */
876fa593
JK
11997
11998static int
494cfb0f 11999bp_location_compare (const void *ap, const void *bp)
876fa593 12000{
494cfb0f
JK
12001 struct bp_location *a = *(void **) ap;
12002 struct bp_location *b = *(void **) bp;
876fa593
JK
12003
12004 if (a->address != b->address)
12005 return (a->address > b->address) - (a->address < b->address);
12006
dea2aa5f
LM
12007 /* Sort locations at the same address by their pspace number, keeping
12008 locations of the same inferior (in a multi-inferior environment)
12009 grouped. */
12010
12011 if (a->pspace->num != b->pspace->num)
12012 return ((a->pspace->num > b->pspace->num)
12013 - (a->pspace->num < b->pspace->num));
12014
876fa593 12015 /* Sort permanent breakpoints first. */
1a853c52
PA
12016 if (a->permanent != b->permanent)
12017 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12018
c56a97f9
JK
12019 /* Make the internal GDB representation stable across GDB runs
12020 where A and B memory inside GDB can differ. Breakpoint locations of
12021 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12022
12023 if (a->owner->number != b->owner->number)
c56a97f9
JK
12024 return ((a->owner->number > b->owner->number)
12025 - (a->owner->number < b->owner->number));
876fa593
JK
12026
12027 return (a > b) - (a < b);
12028}
12029
876fa593 12030/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12031 bp_location_shadow_len_after_address_max according to the current
12032 content of the bp_location array. */
f7545552
TT
12033
12034static void
876fa593 12035bp_location_target_extensions_update (void)
f7545552 12036{
876fa593
JK
12037 struct bp_location *bl, **blp_tmp;
12038
12039 bp_location_placed_address_before_address_max = 0;
12040 bp_location_shadow_len_after_address_max = 0;
12041
12042 ALL_BP_LOCATIONS (bl, blp_tmp)
12043 {
12044 CORE_ADDR start, end, addr;
12045
12046 if (!bp_location_has_shadow (bl))
12047 continue;
12048
12049 start = bl->target_info.placed_address;
12050 end = start + bl->target_info.shadow_len;
12051
12052 gdb_assert (bl->address >= start);
12053 addr = bl->address - start;
12054 if (addr > bp_location_placed_address_before_address_max)
12055 bp_location_placed_address_before_address_max = addr;
12056
12057 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12058
12059 gdb_assert (bl->address < end);
12060 addr = end - bl->address;
12061 if (addr > bp_location_shadow_len_after_address_max)
12062 bp_location_shadow_len_after_address_max = addr;
12063 }
f7545552
TT
12064}
12065
1e4d1764
YQ
12066/* Download tracepoint locations if they haven't been. */
12067
12068static void
12069download_tracepoint_locations (void)
12070{
7ed2c994 12071 struct breakpoint *b;
1e4d1764
YQ
12072 struct cleanup *old_chain;
12073
12074 if (!target_can_download_tracepoint ())
12075 return;
12076
12077 old_chain = save_current_space_and_thread ();
12078
7ed2c994 12079 ALL_TRACEPOINTS (b)
1e4d1764 12080 {
7ed2c994 12081 struct bp_location *bl;
1e4d1764 12082 struct tracepoint *t;
f2a8bc8a 12083 int bp_location_downloaded = 0;
1e4d1764 12084
7ed2c994 12085 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12086 ? !may_insert_fast_tracepoints
12087 : !may_insert_tracepoints))
12088 continue;
12089
7ed2c994
YQ
12090 for (bl = b->loc; bl; bl = bl->next)
12091 {
12092 /* In tracepoint, locations are _never_ duplicated, so
12093 should_be_inserted is equivalent to
12094 unduplicated_should_be_inserted. */
12095 if (!should_be_inserted (bl) || bl->inserted)
12096 continue;
1e4d1764 12097
7ed2c994 12098 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12099
7ed2c994 12100 target_download_tracepoint (bl);
1e4d1764 12101
7ed2c994 12102 bl->inserted = 1;
f2a8bc8a 12103 bp_location_downloaded = 1;
7ed2c994
YQ
12104 }
12105 t = (struct tracepoint *) b;
12106 t->number_on_target = b->number;
f2a8bc8a
YQ
12107 if (bp_location_downloaded)
12108 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12109 }
12110
12111 do_cleanups (old_chain);
12112}
12113
934709f0
PW
12114/* Swap the insertion/duplication state between two locations. */
12115
12116static void
12117swap_insertion (struct bp_location *left, struct bp_location *right)
12118{
12119 const int left_inserted = left->inserted;
12120 const int left_duplicate = left->duplicate;
b775012e 12121 const int left_needs_update = left->needs_update;
934709f0
PW
12122 const struct bp_target_info left_target_info = left->target_info;
12123
1e4d1764
YQ
12124 /* Locations of tracepoints can never be duplicated. */
12125 if (is_tracepoint (left->owner))
12126 gdb_assert (!left->duplicate);
12127 if (is_tracepoint (right->owner))
12128 gdb_assert (!right->duplicate);
12129
934709f0
PW
12130 left->inserted = right->inserted;
12131 left->duplicate = right->duplicate;
b775012e 12132 left->needs_update = right->needs_update;
934709f0
PW
12133 left->target_info = right->target_info;
12134 right->inserted = left_inserted;
12135 right->duplicate = left_duplicate;
b775012e 12136 right->needs_update = left_needs_update;
934709f0
PW
12137 right->target_info = left_target_info;
12138}
12139
b775012e
LM
12140/* Force the re-insertion of the locations at ADDRESS. This is called
12141 once a new/deleted/modified duplicate location is found and we are evaluating
12142 conditions on the target's side. Such conditions need to be updated on
12143 the target. */
12144
12145static void
12146force_breakpoint_reinsertion (struct bp_location *bl)
12147{
12148 struct bp_location **locp = NULL, **loc2p;
12149 struct bp_location *loc;
12150 CORE_ADDR address = 0;
12151 int pspace_num;
12152
12153 address = bl->address;
12154 pspace_num = bl->pspace->num;
12155
12156 /* This is only meaningful if the target is
12157 evaluating conditions and if the user has
12158 opted for condition evaluation on the target's
12159 side. */
12160 if (gdb_evaluates_breakpoint_condition_p ()
12161 || !target_supports_evaluation_of_breakpoint_conditions ())
12162 return;
12163
12164 /* Flag all breakpoint locations with this address and
12165 the same program space as the location
12166 as "its condition has changed". We need to
12167 update the conditions on the target's side. */
12168 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12169 {
12170 loc = *loc2p;
12171
12172 if (!is_breakpoint (loc->owner)
12173 || pspace_num != loc->pspace->num)
12174 continue;
12175
12176 /* Flag the location appropriately. We use a different state to
12177 let everyone know that we already updated the set of locations
12178 with addr bl->address and program space bl->pspace. This is so
12179 we don't have to keep calling these functions just to mark locations
12180 that have already been marked. */
12181 loc->condition_changed = condition_updated;
12182
12183 /* Free the agent expression bytecode as well. We will compute
12184 it later on. */
12185 if (loc->cond_bytecode)
12186 {
12187 free_agent_expr (loc->cond_bytecode);
12188 loc->cond_bytecode = NULL;
12189 }
12190 }
12191}
44702360
PA
12192/* Called whether new breakpoints are created, or existing breakpoints
12193 deleted, to update the global location list and recompute which
12194 locations are duplicate of which.
b775012e 12195
04086b45
PA
12196 The INSERT_MODE flag determines whether locations may not, may, or
12197 shall be inserted now. See 'enum ugll_insert_mode' for more
12198 info. */
b60e7edf 12199
0d381245 12200static void
44702360 12201update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12202{
74960c60 12203 struct breakpoint *b;
876fa593 12204 struct bp_location **locp, *loc;
f7545552 12205 struct cleanup *cleanups;
b775012e
LM
12206 /* Last breakpoint location address that was marked for update. */
12207 CORE_ADDR last_addr = 0;
12208 /* Last breakpoint location program space that was marked for update. */
12209 int last_pspace_num = -1;
f7545552 12210
2d134ed3
PA
12211 /* Used in the duplicates detection below. When iterating over all
12212 bp_locations, points to the first bp_location of a given address.
12213 Breakpoints and watchpoints of different types are never
12214 duplicates of each other. Keep one pointer for each type of
12215 breakpoint/watchpoint, so we only need to loop over all locations
12216 once. */
12217 struct bp_location *bp_loc_first; /* breakpoint */
12218 struct bp_location *wp_loc_first; /* hardware watchpoint */
12219 struct bp_location *awp_loc_first; /* access watchpoint */
12220 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12221
4a64f543
MS
12222 /* Saved former bp_location array which we compare against the newly
12223 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12224 struct bp_location **old_location, **old_locp;
12225 unsigned old_location_count;
12226
12227 old_location = bp_location;
12228 old_location_count = bp_location_count;
12229 bp_location = NULL;
12230 bp_location_count = 0;
12231 cleanups = make_cleanup (xfree, old_location);
0d381245 12232
74960c60 12233 ALL_BREAKPOINTS (b)
876fa593
JK
12234 for (loc = b->loc; loc; loc = loc->next)
12235 bp_location_count++;
12236
12237 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12238 locp = bp_location;
12239 ALL_BREAKPOINTS (b)
12240 for (loc = b->loc; loc; loc = loc->next)
12241 *locp++ = loc;
12242 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12243 bp_location_compare);
876fa593
JK
12244
12245 bp_location_target_extensions_update ();
74960c60 12246
4a64f543
MS
12247 /* Identify bp_location instances that are no longer present in the
12248 new list, and therefore should be freed. Note that it's not
12249 necessary that those locations should be removed from inferior --
12250 if there's another location at the same address (previously
12251 marked as duplicate), we don't need to remove/insert the
12252 location.
876fa593 12253
4a64f543
MS
12254 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12255 and former bp_location array state respectively. */
876fa593
JK
12256
12257 locp = bp_location;
12258 for (old_locp = old_location; old_locp < old_location + old_location_count;
12259 old_locp++)
74960c60 12260 {
876fa593 12261 struct bp_location *old_loc = *old_locp;
c7d46a38 12262 struct bp_location **loc2p;
876fa593 12263
e5dd4106 12264 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12265 not, we have to free it. */
c7d46a38 12266 int found_object = 0;
20874c92
VP
12267 /* Tells if the location should remain inserted in the target. */
12268 int keep_in_target = 0;
12269 int removed = 0;
876fa593 12270
4a64f543
MS
12271 /* Skip LOCP entries which will definitely never be needed.
12272 Stop either at or being the one matching OLD_LOC. */
876fa593 12273 while (locp < bp_location + bp_location_count
c7d46a38 12274 && (*locp)->address < old_loc->address)
876fa593 12275 locp++;
c7d46a38
PA
12276
12277 for (loc2p = locp;
12278 (loc2p < bp_location + bp_location_count
12279 && (*loc2p)->address == old_loc->address);
12280 loc2p++)
12281 {
b775012e
LM
12282 /* Check if this is a new/duplicated location or a duplicated
12283 location that had its condition modified. If so, we want to send
12284 its condition to the target if evaluation of conditions is taking
12285 place there. */
12286 if ((*loc2p)->condition_changed == condition_modified
12287 && (last_addr != old_loc->address
12288 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12289 {
b775012e
LM
12290 force_breakpoint_reinsertion (*loc2p);
12291 last_pspace_num = old_loc->pspace->num;
c7d46a38 12292 }
b775012e
LM
12293
12294 if (*loc2p == old_loc)
12295 found_object = 1;
c7d46a38 12296 }
74960c60 12297
b775012e
LM
12298 /* We have already handled this address, update it so that we don't
12299 have to go through updates again. */
12300 last_addr = old_loc->address;
12301
12302 /* Target-side condition evaluation: Handle deleted locations. */
12303 if (!found_object)
12304 force_breakpoint_reinsertion (old_loc);
12305
4a64f543
MS
12306 /* If this location is no longer present, and inserted, look if
12307 there's maybe a new location at the same address. If so,
12308 mark that one inserted, and don't remove this one. This is
12309 needed so that we don't have a time window where a breakpoint
12310 at certain location is not inserted. */
74960c60 12311
876fa593 12312 if (old_loc->inserted)
0d381245 12313 {
4a64f543
MS
12314 /* If the location is inserted now, we might have to remove
12315 it. */
74960c60 12316
876fa593 12317 if (found_object && should_be_inserted (old_loc))
74960c60 12318 {
4a64f543
MS
12319 /* The location is still present in the location list,
12320 and still should be inserted. Don't do anything. */
20874c92 12321 keep_in_target = 1;
74960c60
VP
12322 }
12323 else
12324 {
b775012e
LM
12325 /* This location still exists, but it won't be kept in the
12326 target since it may have been disabled. We proceed to
12327 remove its target-side condition. */
12328
4a64f543
MS
12329 /* The location is either no longer present, or got
12330 disabled. See if there's another location at the
12331 same address, in which case we don't need to remove
12332 this one from the target. */
876fa593 12333
2bdf28a0 12334 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12335 if (breakpoint_address_is_meaningful (old_loc->owner))
12336 {
876fa593 12337 for (loc2p = locp;
c7d46a38
PA
12338 (loc2p < bp_location + bp_location_count
12339 && (*loc2p)->address == old_loc->address);
876fa593
JK
12340 loc2p++)
12341 {
12342 struct bp_location *loc2 = *loc2p;
12343
2d134ed3 12344 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12345 {
85d721b8
PA
12346 /* Read watchpoint locations are switched to
12347 access watchpoints, if the former are not
12348 supported, but the latter are. */
12349 if (is_hardware_watchpoint (old_loc->owner))
12350 {
12351 gdb_assert (is_hardware_watchpoint (loc2->owner));
12352 loc2->watchpoint_type = old_loc->watchpoint_type;
12353 }
12354
934709f0
PW
12355 /* loc2 is a duplicated location. We need to check
12356 if it should be inserted in case it will be
12357 unduplicated. */
12358 if (loc2 != old_loc
12359 && unduplicated_should_be_inserted (loc2))
c7d46a38 12360 {
934709f0 12361 swap_insertion (old_loc, loc2);
c7d46a38
PA
12362 keep_in_target = 1;
12363 break;
12364 }
876fa593
JK
12365 }
12366 }
12367 }
74960c60
VP
12368 }
12369
20874c92
VP
12370 if (!keep_in_target)
12371 {
876fa593 12372 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12373 {
4a64f543
MS
12374 /* This is just about all we can do. We could keep
12375 this location on the global list, and try to
12376 remove it next time, but there's no particular
12377 reason why we will succeed next time.
20874c92 12378
4a64f543
MS
12379 Note that at this point, old_loc->owner is still
12380 valid, as delete_breakpoint frees the breakpoint
12381 only after calling us. */
3e43a32a
MS
12382 printf_filtered (_("warning: Error removing "
12383 "breakpoint %d\n"),
876fa593 12384 old_loc->owner->number);
20874c92
VP
12385 }
12386 removed = 1;
12387 }
0d381245 12388 }
74960c60
VP
12389
12390 if (!found_object)
1c5cfe86 12391 {
fbea99ea 12392 if (removed && target_is_non_stop_p ()
1cf4d951 12393 && need_moribund_for_location_type (old_loc))
20874c92 12394 {
db82e815
PA
12395 /* This location was removed from the target. In
12396 non-stop mode, a race condition is possible where
12397 we've removed a breakpoint, but stop events for that
12398 breakpoint are already queued and will arrive later.
12399 We apply an heuristic to be able to distinguish such
12400 SIGTRAPs from other random SIGTRAPs: we keep this
12401 breakpoint location for a bit, and will retire it
12402 after we see some number of events. The theory here
12403 is that reporting of events should, "on the average",
12404 be fair, so after a while we'll see events from all
12405 threads that have anything of interest, and no longer
12406 need to keep this breakpoint location around. We
12407 don't hold locations forever so to reduce chances of
12408 mistaking a non-breakpoint SIGTRAP for a breakpoint
12409 SIGTRAP.
12410
12411 The heuristic failing can be disastrous on
12412 decr_pc_after_break targets.
12413
12414 On decr_pc_after_break targets, like e.g., x86-linux,
12415 if we fail to recognize a late breakpoint SIGTRAP,
12416 because events_till_retirement has reached 0 too
12417 soon, we'll fail to do the PC adjustment, and report
12418 a random SIGTRAP to the user. When the user resumes
12419 the inferior, it will most likely immediately crash
2dec564e 12420 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12421 corrupted, because of being resumed e.g., in the
12422 middle of a multi-byte instruction, or skipped a
12423 one-byte instruction. This was actually seen happen
12424 on native x86-linux, and should be less rare on
12425 targets that do not support new thread events, like
12426 remote, due to the heuristic depending on
12427 thread_count.
12428
12429 Mistaking a random SIGTRAP for a breakpoint trap
12430 causes similar symptoms (PC adjustment applied when
12431 it shouldn't), but then again, playing with SIGTRAPs
12432 behind the debugger's back is asking for trouble.
12433
12434 Since hardware watchpoint traps are always
12435 distinguishable from other traps, so we don't need to
12436 apply keep hardware watchpoint moribund locations
12437 around. We simply always ignore hardware watchpoint
12438 traps we can no longer explain. */
12439
876fa593
JK
12440 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12441 old_loc->owner = NULL;
20874c92 12442
876fa593 12443 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12444 }
12445 else
f431efe5
PA
12446 {
12447 old_loc->owner = NULL;
12448 decref_bp_location (&old_loc);
12449 }
20874c92 12450 }
74960c60 12451 }
1c5cfe86 12452
348d480f
PA
12453 /* Rescan breakpoints at the same address and section, marking the
12454 first one as "first" and any others as "duplicates". This is so
12455 that the bpt instruction is only inserted once. If we have a
12456 permanent breakpoint at the same place as BPT, make that one the
12457 official one, and the rest as duplicates. Permanent breakpoints
12458 are sorted first for the same address.
12459
12460 Do the same for hardware watchpoints, but also considering the
12461 watchpoint's type (regular/access/read) and length. */
12462
12463 bp_loc_first = NULL;
12464 wp_loc_first = NULL;
12465 awp_loc_first = NULL;
12466 rwp_loc_first = NULL;
12467 ALL_BP_LOCATIONS (loc, locp)
12468 {
12469 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12470 non-NULL. */
348d480f 12471 struct bp_location **loc_first_p;
d3fbdd86 12472 b = loc->owner;
348d480f 12473
6f380991 12474 if (!unduplicated_should_be_inserted (loc)
348d480f 12475 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12476 /* Don't detect duplicate for tracepoint locations because they are
12477 never duplicated. See the comments in field `duplicate' of
12478 `struct bp_location'. */
348d480f 12479 || is_tracepoint (b))
b775012e
LM
12480 {
12481 /* Clear the condition modification flag. */
12482 loc->condition_changed = condition_unchanged;
12483 continue;
12484 }
348d480f 12485
348d480f
PA
12486 if (b->type == bp_hardware_watchpoint)
12487 loc_first_p = &wp_loc_first;
12488 else if (b->type == bp_read_watchpoint)
12489 loc_first_p = &rwp_loc_first;
12490 else if (b->type == bp_access_watchpoint)
12491 loc_first_p = &awp_loc_first;
12492 else
12493 loc_first_p = &bp_loc_first;
12494
12495 if (*loc_first_p == NULL
12496 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12497 || !breakpoint_locations_match (loc, *loc_first_p))
12498 {
12499 *loc_first_p = loc;
12500 loc->duplicate = 0;
b775012e
LM
12501
12502 if (is_breakpoint (loc->owner) && loc->condition_changed)
12503 {
12504 loc->needs_update = 1;
12505 /* Clear the condition modification flag. */
12506 loc->condition_changed = condition_unchanged;
12507 }
348d480f
PA
12508 continue;
12509 }
12510
934709f0
PW
12511
12512 /* This and the above ensure the invariant that the first location
12513 is not duplicated, and is the inserted one.
12514 All following are marked as duplicated, and are not inserted. */
12515 if (loc->inserted)
12516 swap_insertion (loc, *loc_first_p);
348d480f
PA
12517 loc->duplicate = 1;
12518
b775012e
LM
12519 /* Clear the condition modification flag. */
12520 loc->condition_changed = condition_unchanged;
348d480f
PA
12521 }
12522
a25a5a45 12523 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12524 {
04086b45 12525 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12526 insert_breakpoint_locations ();
12527 else
12528 {
44702360
PA
12529 /* Even though the caller told us to not insert new
12530 locations, we may still need to update conditions on the
12531 target's side of breakpoints that were already inserted
12532 if the target is evaluating breakpoint conditions. We
b775012e
LM
12533 only update conditions for locations that are marked
12534 "needs_update". */
12535 update_inserted_breakpoint_locations ();
12536 }
12537 }
348d480f 12538
04086b45 12539 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12540 download_tracepoint_locations ();
12541
348d480f
PA
12542 do_cleanups (cleanups);
12543}
12544
12545void
12546breakpoint_retire_moribund (void)
12547{
12548 struct bp_location *loc;
12549 int ix;
12550
12551 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12552 if (--(loc->events_till_retirement) == 0)
12553 {
12554 decref_bp_location (&loc);
12555 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12556 --ix;
12557 }
12558}
12559
12560static void
44702360 12561update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12562{
348d480f 12563
492d29ea
PA
12564 TRY
12565 {
12566 update_global_location_list (insert_mode);
12567 }
12568 CATCH (e, RETURN_MASK_ERROR)
12569 {
12570 }
12571 END_CATCH
348d480f
PA
12572}
12573
12574/* Clear BKP from a BPS. */
12575
12576static void
12577bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12578{
12579 bpstat bs;
12580
12581 for (bs = bps; bs; bs = bs->next)
12582 if (bs->breakpoint_at == bpt)
12583 {
12584 bs->breakpoint_at = NULL;
12585 bs->old_val = NULL;
12586 /* bs->commands will be freed later. */
12587 }
12588}
12589
12590/* Callback for iterate_over_threads. */
12591static int
12592bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12593{
12594 struct breakpoint *bpt = data;
12595
12596 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12597 return 0;
12598}
12599
12600/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12601 callbacks. */
12602
12603static void
12604say_where (struct breakpoint *b)
12605{
12606 struct value_print_options opts;
12607
12608 get_user_print_options (&opts);
12609
12610 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12611 single string. */
12612 if (b->loc == NULL)
12613 {
f00aae0f
KS
12614 /* For pending locations, the output differs slightly based
12615 on b->extra_string. If this is non-NULL, it contains either
12616 a condition or dprintf arguments. */
12617 if (b->extra_string == NULL)
12618 {
12619 printf_filtered (_(" (%s) pending."),
12620 event_location_to_string (b->location));
12621 }
12622 else if (b->type == bp_dprintf)
12623 {
12624 printf_filtered (_(" (%s,%s) pending."),
12625 event_location_to_string (b->location),
12626 b->extra_string);
12627 }
12628 else
12629 {
12630 printf_filtered (_(" (%s %s) pending."),
12631 event_location_to_string (b->location),
12632 b->extra_string);
12633 }
348d480f
PA
12634 }
12635 else
12636 {
2f202fde 12637 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12638 {
12639 printf_filtered (" at ");
12640 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12641 gdb_stdout);
12642 }
2f202fde 12643 if (b->loc->symtab != NULL)
f8eba3c6
TT
12644 {
12645 /* If there is a single location, we can print the location
12646 more nicely. */
12647 if (b->loc->next == NULL)
12648 printf_filtered (": file %s, line %d.",
05cba821
JK
12649 symtab_to_filename_for_display (b->loc->symtab),
12650 b->loc->line_number);
f8eba3c6
TT
12651 else
12652 /* This is not ideal, but each location may have a
12653 different file name, and this at least reflects the
12654 real situation somewhat. */
f00aae0f
KS
12655 printf_filtered (": %s.",
12656 event_location_to_string (b->location));
f8eba3c6 12657 }
348d480f
PA
12658
12659 if (b->loc->next)
12660 {
12661 struct bp_location *loc = b->loc;
12662 int n = 0;
12663 for (; loc; loc = loc->next)
12664 ++n;
12665 printf_filtered (" (%d locations)", n);
12666 }
12667 }
12668}
12669
348d480f
PA
12670/* Default bp_location_ops methods. */
12671
12672static void
12673bp_location_dtor (struct bp_location *self)
12674{
12675 xfree (self->cond);
b775012e
LM
12676 if (self->cond_bytecode)
12677 free_agent_expr (self->cond_bytecode);
348d480f 12678 xfree (self->function_name);
8b4f3082
PA
12679
12680 VEC_free (agent_expr_p, self->target_info.conditions);
12681 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12682}
12683
12684static const struct bp_location_ops bp_location_ops =
12685{
12686 bp_location_dtor
12687};
12688
2060206e
PA
12689/* Default breakpoint_ops methods all breakpoint_ops ultimately
12690 inherit from. */
348d480f 12691
2060206e
PA
12692static void
12693base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12694{
12695 decref_counted_command_line (&self->commands);
12696 xfree (self->cond_string);
fb81d016 12697 xfree (self->extra_string);
f8eba3c6 12698 xfree (self->filter);
f00aae0f
KS
12699 delete_event_location (self->location);
12700 delete_event_location (self->location_range_end);
348d480f
PA
12701}
12702
2060206e
PA
12703static struct bp_location *
12704base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12705{
12706 struct bp_location *loc;
12707
12708 loc = XNEW (struct bp_location);
12709 init_bp_location (loc, &bp_location_ops, self);
12710 return loc;
12711}
12712
2060206e
PA
12713static void
12714base_breakpoint_re_set (struct breakpoint *b)
12715{
12716 /* Nothing to re-set. */
12717}
12718
12719#define internal_error_pure_virtual_called() \
12720 gdb_assert_not_reached ("pure virtual function called")
12721
12722static int
12723base_breakpoint_insert_location (struct bp_location *bl)
12724{
12725 internal_error_pure_virtual_called ();
12726}
12727
12728static int
12729base_breakpoint_remove_location (struct bp_location *bl)
12730{
12731 internal_error_pure_virtual_called ();
12732}
12733
12734static int
12735base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12736 struct address_space *aspace,
09ac7c10
TT
12737 CORE_ADDR bp_addr,
12738 const struct target_waitstatus *ws)
2060206e
PA
12739{
12740 internal_error_pure_virtual_called ();
12741}
12742
12743static void
12744base_breakpoint_check_status (bpstat bs)
12745{
12746 /* Always stop. */
12747}
12748
12749/* A "works_in_software_mode" breakpoint_ops method that just internal
12750 errors. */
12751
12752static int
12753base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12754{
12755 internal_error_pure_virtual_called ();
12756}
12757
12758/* A "resources_needed" breakpoint_ops method that just internal
12759 errors. */
12760
12761static int
12762base_breakpoint_resources_needed (const struct bp_location *bl)
12763{
12764 internal_error_pure_virtual_called ();
12765}
12766
12767static enum print_stop_action
12768base_breakpoint_print_it (bpstat bs)
12769{
12770 internal_error_pure_virtual_called ();
12771}
12772
12773static void
12774base_breakpoint_print_one_detail (const struct breakpoint *self,
12775 struct ui_out *uiout)
12776{
12777 /* nothing */
12778}
12779
12780static void
12781base_breakpoint_print_mention (struct breakpoint *b)
12782{
12783 internal_error_pure_virtual_called ();
12784}
12785
12786static void
12787base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12788{
12789 internal_error_pure_virtual_called ();
12790}
12791
983af33b 12792static void
f00aae0f
KS
12793base_breakpoint_create_sals_from_location
12794 (const struct event_location *location,
12795 struct linespec_result *canonical,
12796 enum bptype type_wanted)
983af33b
SDJ
12797{
12798 internal_error_pure_virtual_called ();
12799}
12800
12801static void
12802base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12803 struct linespec_result *c,
983af33b 12804 char *cond_string,
e7e0cddf 12805 char *extra_string,
983af33b
SDJ
12806 enum bptype type_wanted,
12807 enum bpdisp disposition,
12808 int thread,
12809 int task, int ignore_count,
12810 const struct breakpoint_ops *o,
12811 int from_tty, int enabled,
44f238bb 12812 int internal, unsigned flags)
983af33b
SDJ
12813{
12814 internal_error_pure_virtual_called ();
12815}
12816
12817static void
f00aae0f
KS
12818base_breakpoint_decode_location (struct breakpoint *b,
12819 const struct event_location *location,
983af33b
SDJ
12820 struct symtabs_and_lines *sals)
12821{
12822 internal_error_pure_virtual_called ();
12823}
12824
ab04a2af
TT
12825/* The default 'explains_signal' method. */
12826
47591c29 12827static int
427cd150 12828base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12829{
47591c29 12830 return 1;
ab04a2af
TT
12831}
12832
9d6e6e84
HZ
12833/* The default "after_condition_true" method. */
12834
12835static void
12836base_breakpoint_after_condition_true (struct bpstats *bs)
12837{
12838 /* Nothing to do. */
12839}
12840
ab04a2af 12841struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
12842{
12843 base_breakpoint_dtor,
12844 base_breakpoint_allocate_location,
12845 base_breakpoint_re_set,
12846 base_breakpoint_insert_location,
12847 base_breakpoint_remove_location,
12848 base_breakpoint_breakpoint_hit,
12849 base_breakpoint_check_status,
12850 base_breakpoint_resources_needed,
12851 base_breakpoint_works_in_software_mode,
12852 base_breakpoint_print_it,
12853 NULL,
12854 base_breakpoint_print_one_detail,
12855 base_breakpoint_print_mention,
983af33b 12856 base_breakpoint_print_recreate,
5f700d83 12857 base_breakpoint_create_sals_from_location,
983af33b 12858 base_breakpoint_create_breakpoints_sal,
5f700d83 12859 base_breakpoint_decode_location,
9d6e6e84
HZ
12860 base_breakpoint_explains_signal,
12861 base_breakpoint_after_condition_true,
2060206e
PA
12862};
12863
12864/* Default breakpoint_ops methods. */
12865
12866static void
348d480f
PA
12867bkpt_re_set (struct breakpoint *b)
12868{
06edf0c0 12869 /* FIXME: is this still reachable? */
f00aae0f 12870 if (event_location_empty_p (b->location))
06edf0c0 12871 {
f00aae0f 12872 /* Anything without a location can't be re-set. */
348d480f 12873 delete_breakpoint (b);
06edf0c0 12874 return;
348d480f 12875 }
06edf0c0
PA
12876
12877 breakpoint_re_set_default (b);
348d480f
PA
12878}
12879
2060206e 12880static int
348d480f
PA
12881bkpt_insert_location (struct bp_location *bl)
12882{
12883 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12884 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12885 else
7c16b83e 12886 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12887}
12888
2060206e 12889static int
348d480f
PA
12890bkpt_remove_location (struct bp_location *bl)
12891{
12892 if (bl->loc_type == bp_loc_hardware_breakpoint)
12893 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12894 else
7c16b83e 12895 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12896}
12897
2060206e 12898static int
348d480f 12899bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12900 struct address_space *aspace, CORE_ADDR bp_addr,
12901 const struct target_waitstatus *ws)
348d480f 12902{
09ac7c10 12903 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12904 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12905 return 0;
12906
348d480f
PA
12907 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12908 aspace, bp_addr))
12909 return 0;
12910
12911 if (overlay_debugging /* unmapped overlay section */
12912 && section_is_overlay (bl->section)
12913 && !section_is_mapped (bl->section))
12914 return 0;
12915
12916 return 1;
12917}
12918
cd1608cc
PA
12919static int
12920dprintf_breakpoint_hit (const struct bp_location *bl,
12921 struct address_space *aspace, CORE_ADDR bp_addr,
12922 const struct target_waitstatus *ws)
12923{
12924 if (dprintf_style == dprintf_style_agent
12925 && target_can_run_breakpoint_commands ())
12926 {
12927 /* An agent-style dprintf never causes a stop. If we see a trap
12928 for this address it must be for a breakpoint that happens to
12929 be set at the same address. */
12930 return 0;
12931 }
12932
12933 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12934}
12935
2060206e 12936static int
348d480f
PA
12937bkpt_resources_needed (const struct bp_location *bl)
12938{
12939 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12940
12941 return 1;
12942}
12943
2060206e 12944static enum print_stop_action
348d480f
PA
12945bkpt_print_it (bpstat bs)
12946{
348d480f
PA
12947 struct breakpoint *b;
12948 const struct bp_location *bl;
001c8c33 12949 int bp_temp;
79a45e25 12950 struct ui_out *uiout = current_uiout;
348d480f
PA
12951
12952 gdb_assert (bs->bp_location_at != NULL);
12953
12954 bl = bs->bp_location_at;
12955 b = bs->breakpoint_at;
12956
001c8c33
PA
12957 bp_temp = b->disposition == disp_del;
12958 if (bl->address != bl->requested_address)
12959 breakpoint_adjustment_warning (bl->requested_address,
12960 bl->address,
12961 b->number, 1);
12962 annotate_breakpoint (b->number);
12963 if (bp_temp)
12964 ui_out_text (uiout, "\nTemporary breakpoint ");
12965 else
12966 ui_out_text (uiout, "\nBreakpoint ");
12967 if (ui_out_is_mi_like_p (uiout))
348d480f 12968 {
001c8c33
PA
12969 ui_out_field_string (uiout, "reason",
12970 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12971 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 12972 }
001c8c33
PA
12973 ui_out_field_int (uiout, "bkptno", b->number);
12974 ui_out_text (uiout, ", ");
06edf0c0 12975
001c8c33 12976 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12977}
12978
2060206e 12979static void
06edf0c0
PA
12980bkpt_print_mention (struct breakpoint *b)
12981{
79a45e25 12982 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
12983 return;
12984
12985 switch (b->type)
12986 {
12987 case bp_breakpoint:
12988 case bp_gnu_ifunc_resolver:
12989 if (b->disposition == disp_del)
12990 printf_filtered (_("Temporary breakpoint"));
12991 else
12992 printf_filtered (_("Breakpoint"));
12993 printf_filtered (_(" %d"), b->number);
12994 if (b->type == bp_gnu_ifunc_resolver)
12995 printf_filtered (_(" at gnu-indirect-function resolver"));
12996 break;
12997 case bp_hardware_breakpoint:
12998 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12999 break;
e7e0cddf
SS
13000 case bp_dprintf:
13001 printf_filtered (_("Dprintf %d"), b->number);
13002 break;
06edf0c0
PA
13003 }
13004
13005 say_where (b);
13006}
13007
2060206e 13008static void
06edf0c0
PA
13009bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13010{
13011 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13012 fprintf_unfiltered (fp, "tbreak");
13013 else if (tp->type == bp_breakpoint)
13014 fprintf_unfiltered (fp, "break");
13015 else if (tp->type == bp_hardware_breakpoint
13016 && tp->disposition == disp_del)
13017 fprintf_unfiltered (fp, "thbreak");
13018 else if (tp->type == bp_hardware_breakpoint)
13019 fprintf_unfiltered (fp, "hbreak");
13020 else
13021 internal_error (__FILE__, __LINE__,
13022 _("unhandled breakpoint type %d"), (int) tp->type);
13023
f00aae0f
KS
13024 fprintf_unfiltered (fp, " %s",
13025 event_location_to_string (tp->location));
13026
13027 /* Print out extra_string if this breakpoint is pending. It might
13028 contain, for example, conditions that were set by the user. */
13029 if (tp->loc == NULL && tp->extra_string != NULL)
13030 fprintf_unfiltered (fp, " %s", tp->extra_string);
13031
dd11a36c 13032 print_recreate_thread (tp, fp);
06edf0c0
PA
13033}
13034
983af33b 13035static void
f00aae0f
KS
13036bkpt_create_sals_from_location (const struct event_location *location,
13037 struct linespec_result *canonical,
13038 enum bptype type_wanted)
983af33b 13039{
f00aae0f 13040 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13041}
13042
13043static void
13044bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13045 struct linespec_result *canonical,
983af33b 13046 char *cond_string,
e7e0cddf 13047 char *extra_string,
983af33b
SDJ
13048 enum bptype type_wanted,
13049 enum bpdisp disposition,
13050 int thread,
13051 int task, int ignore_count,
13052 const struct breakpoint_ops *ops,
13053 int from_tty, int enabled,
44f238bb 13054 int internal, unsigned flags)
983af33b 13055{
023fa29b 13056 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13057 cond_string, extra_string,
13058 type_wanted,
983af33b
SDJ
13059 disposition, thread, task,
13060 ignore_count, ops, from_tty,
44f238bb 13061 enabled, internal, flags);
983af33b
SDJ
13062}
13063
13064static void
f00aae0f
KS
13065bkpt_decode_location (struct breakpoint *b,
13066 const struct event_location *location,
983af33b
SDJ
13067 struct symtabs_and_lines *sals)
13068{
f00aae0f 13069 decode_location_default (b, location, sals);
983af33b
SDJ
13070}
13071
06edf0c0
PA
13072/* Virtual table for internal breakpoints. */
13073
13074static void
13075internal_bkpt_re_set (struct breakpoint *b)
13076{
13077 switch (b->type)
13078 {
13079 /* Delete overlay event and longjmp master breakpoints; they
13080 will be reset later by breakpoint_re_set. */
13081 case bp_overlay_event:
13082 case bp_longjmp_master:
13083 case bp_std_terminate_master:
13084 case bp_exception_master:
13085 delete_breakpoint (b);
13086 break;
13087
13088 /* This breakpoint is special, it's set up when the inferior
13089 starts and we really don't want to touch it. */
13090 case bp_shlib_event:
13091
13092 /* Like bp_shlib_event, this breakpoint type is special. Once
13093 it is set up, we do not want to touch it. */
13094 case bp_thread_event:
13095 break;
13096 }
13097}
13098
13099static void
13100internal_bkpt_check_status (bpstat bs)
13101{
a9b3a50f
PA
13102 if (bs->breakpoint_at->type == bp_shlib_event)
13103 {
13104 /* If requested, stop when the dynamic linker notifies GDB of
13105 events. This allows the user to get control and place
13106 breakpoints in initializer routines for dynamically loaded
13107 objects (among other things). */
13108 bs->stop = stop_on_solib_events;
13109 bs->print = stop_on_solib_events;
13110 }
13111 else
13112 bs->stop = 0;
06edf0c0
PA
13113}
13114
13115static enum print_stop_action
13116internal_bkpt_print_it (bpstat bs)
13117{
06edf0c0 13118 struct breakpoint *b;
06edf0c0 13119
06edf0c0
PA
13120 b = bs->breakpoint_at;
13121
06edf0c0
PA
13122 switch (b->type)
13123 {
348d480f
PA
13124 case bp_shlib_event:
13125 /* Did we stop because the user set the stop_on_solib_events
13126 variable? (If so, we report this as a generic, "Stopped due
13127 to shlib event" message.) */
edcc5120 13128 print_solib_event (0);
348d480f
PA
13129 break;
13130
13131 case bp_thread_event:
13132 /* Not sure how we will get here.
13133 GDB should not stop for these breakpoints. */
13134 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13135 break;
13136
13137 case bp_overlay_event:
13138 /* By analogy with the thread event, GDB should not stop for these. */
13139 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13140 break;
13141
13142 case bp_longjmp_master:
13143 /* These should never be enabled. */
13144 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13145 break;
13146
13147 case bp_std_terminate_master:
13148 /* These should never be enabled. */
13149 printf_filtered (_("std::terminate Master Breakpoint: "
13150 "gdb should not stop!\n"));
348d480f
PA
13151 break;
13152
13153 case bp_exception_master:
13154 /* These should never be enabled. */
13155 printf_filtered (_("Exception Master Breakpoint: "
13156 "gdb should not stop!\n"));
06edf0c0
PA
13157 break;
13158 }
13159
001c8c33 13160 return PRINT_NOTHING;
06edf0c0
PA
13161}
13162
13163static void
13164internal_bkpt_print_mention (struct breakpoint *b)
13165{
13166 /* Nothing to mention. These breakpoints are internal. */
13167}
13168
06edf0c0
PA
13169/* Virtual table for momentary breakpoints */
13170
13171static void
13172momentary_bkpt_re_set (struct breakpoint *b)
13173{
13174 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13175 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13176 Otherwise these should have been blown away via the cleanup chain
13177 or by breakpoint_init_inferior when we rerun the executable. */
13178}
13179
13180static void
13181momentary_bkpt_check_status (bpstat bs)
13182{
13183 /* Nothing. The point of these breakpoints is causing a stop. */
13184}
13185
13186static enum print_stop_action
13187momentary_bkpt_print_it (bpstat bs)
13188{
79a45e25
PA
13189 struct ui_out *uiout = current_uiout;
13190
001c8c33 13191 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13192 {
001c8c33 13193 struct breakpoint *b = bs->breakpoint_at;
348d480f 13194
001c8c33
PA
13195 switch (b->type)
13196 {
13197 case bp_finish:
13198 ui_out_field_string
13199 (uiout, "reason",
13200 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13201 break;
348d480f 13202
001c8c33
PA
13203 case bp_until:
13204 ui_out_field_string
13205 (uiout, "reason",
13206 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13207 break;
13208 }
348d480f
PA
13209 }
13210
001c8c33 13211 return PRINT_UNKNOWN;
348d480f
PA
13212}
13213
06edf0c0
PA
13214static void
13215momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13216{
06edf0c0 13217 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13218}
13219
e2e4d78b
JK
13220/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13221
13222 It gets cleared already on the removal of the first one of such placed
13223 breakpoints. This is OK as they get all removed altogether. */
13224
13225static void
13226longjmp_bkpt_dtor (struct breakpoint *self)
13227{
13228 struct thread_info *tp = find_thread_id (self->thread);
13229
13230 if (tp)
13231 tp->initiating_frame = null_frame_id;
13232
13233 momentary_breakpoint_ops.dtor (self);
13234}
13235
55aa24fb
SDJ
13236/* Specific methods for probe breakpoints. */
13237
13238static int
13239bkpt_probe_insert_location (struct bp_location *bl)
13240{
13241 int v = bkpt_insert_location (bl);
13242
13243 if (v == 0)
13244 {
13245 /* The insertion was successful, now let's set the probe's semaphore
13246 if needed. */
0ea5cda8
SDJ
13247 if (bl->probe.probe->pops->set_semaphore != NULL)
13248 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13249 bl->probe.objfile,
13250 bl->gdbarch);
55aa24fb
SDJ
13251 }
13252
13253 return v;
13254}
13255
13256static int
13257bkpt_probe_remove_location (struct bp_location *bl)
13258{
13259 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13260 if (bl->probe.probe->pops->clear_semaphore != NULL)
13261 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13262 bl->probe.objfile,
13263 bl->gdbarch);
55aa24fb
SDJ
13264
13265 return bkpt_remove_location (bl);
13266}
13267
13268static void
f00aae0f 13269bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13270 struct linespec_result *canonical,
f00aae0f 13271 enum bptype type_wanted)
55aa24fb
SDJ
13272{
13273 struct linespec_sals lsal;
13274
f00aae0f
KS
13275 lsal.sals = parse_probes (location, canonical);
13276 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
55aa24fb
SDJ
13277 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13278}
13279
13280static void
f00aae0f
KS
13281bkpt_probe_decode_location (struct breakpoint *b,
13282 const struct event_location *location,
55aa24fb
SDJ
13283 struct symtabs_and_lines *sals)
13284{
f00aae0f 13285 *sals = parse_probes (location, NULL);
55aa24fb
SDJ
13286 if (!sals->sals)
13287 error (_("probe not found"));
13288}
13289
348d480f 13290/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13291
348d480f
PA
13292static void
13293tracepoint_re_set (struct breakpoint *b)
13294{
13295 breakpoint_re_set_default (b);
13296}
876fa593 13297
348d480f
PA
13298static int
13299tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13300 struct address_space *aspace, CORE_ADDR bp_addr,
13301 const struct target_waitstatus *ws)
348d480f
PA
13302{
13303 /* By definition, the inferior does not report stops at
13304 tracepoints. */
13305 return 0;
74960c60
VP
13306}
13307
13308static void
348d480f
PA
13309tracepoint_print_one_detail (const struct breakpoint *self,
13310 struct ui_out *uiout)
74960c60 13311{
d9b3f62e
PA
13312 struct tracepoint *tp = (struct tracepoint *) self;
13313 if (tp->static_trace_marker_id)
348d480f
PA
13314 {
13315 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13316
348d480f
PA
13317 ui_out_text (uiout, "\tmarker id is ");
13318 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13319 tp->static_trace_marker_id);
348d480f
PA
13320 ui_out_text (uiout, "\n");
13321 }
0d381245
VP
13322}
13323
a474d7c2 13324static void
348d480f 13325tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13326{
79a45e25 13327 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13328 return;
cc59ec59 13329
348d480f
PA
13330 switch (b->type)
13331 {
13332 case bp_tracepoint:
13333 printf_filtered (_("Tracepoint"));
13334 printf_filtered (_(" %d"), b->number);
13335 break;
13336 case bp_fast_tracepoint:
13337 printf_filtered (_("Fast tracepoint"));
13338 printf_filtered (_(" %d"), b->number);
13339 break;
13340 case bp_static_tracepoint:
13341 printf_filtered (_("Static tracepoint"));
13342 printf_filtered (_(" %d"), b->number);
13343 break;
13344 default:
13345 internal_error (__FILE__, __LINE__,
13346 _("unhandled tracepoint type %d"), (int) b->type);
13347 }
13348
13349 say_where (b);
a474d7c2
PA
13350}
13351
348d480f 13352static void
d9b3f62e 13353tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13354{
d9b3f62e
PA
13355 struct tracepoint *tp = (struct tracepoint *) self;
13356
13357 if (self->type == bp_fast_tracepoint)
348d480f 13358 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13359 if (self->type == bp_static_tracepoint)
348d480f 13360 fprintf_unfiltered (fp, "strace");
d9b3f62e 13361 else if (self->type == bp_tracepoint)
348d480f
PA
13362 fprintf_unfiltered (fp, "trace");
13363 else
13364 internal_error (__FILE__, __LINE__,
d9b3f62e 13365 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13366
f00aae0f
KS
13367 fprintf_unfiltered (fp, " %s",
13368 event_location_to_string (self->location));
d9b3f62e
PA
13369 print_recreate_thread (self, fp);
13370
13371 if (tp->pass_count)
13372 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13373}
13374
983af33b 13375static void
f00aae0f
KS
13376tracepoint_create_sals_from_location (const struct event_location *location,
13377 struct linespec_result *canonical,
13378 enum bptype type_wanted)
983af33b 13379{
f00aae0f 13380 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13381}
13382
13383static void
13384tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13385 struct linespec_result *canonical,
983af33b 13386 char *cond_string,
e7e0cddf 13387 char *extra_string,
983af33b
SDJ
13388 enum bptype type_wanted,
13389 enum bpdisp disposition,
13390 int thread,
13391 int task, int ignore_count,
13392 const struct breakpoint_ops *ops,
13393 int from_tty, int enabled,
44f238bb 13394 int internal, unsigned flags)
983af33b 13395{
023fa29b 13396 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13397 cond_string, extra_string,
13398 type_wanted,
983af33b
SDJ
13399 disposition, thread, task,
13400 ignore_count, ops, from_tty,
44f238bb 13401 enabled, internal, flags);
983af33b
SDJ
13402}
13403
13404static void
f00aae0f
KS
13405tracepoint_decode_location (struct breakpoint *b,
13406 const struct event_location *location,
983af33b
SDJ
13407 struct symtabs_and_lines *sals)
13408{
f00aae0f 13409 decode_location_default (b, location, sals);
983af33b
SDJ
13410}
13411
2060206e 13412struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13413
55aa24fb
SDJ
13414/* The breakpoint_ops structure to be use on tracepoints placed in a
13415 static probe. */
13416
13417static void
f00aae0f
KS
13418tracepoint_probe_create_sals_from_location
13419 (const struct event_location *location,
13420 struct linespec_result *canonical,
13421 enum bptype type_wanted)
55aa24fb
SDJ
13422{
13423 /* We use the same method for breakpoint on probes. */
f00aae0f 13424 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13425}
13426
13427static void
f00aae0f
KS
13428tracepoint_probe_decode_location (struct breakpoint *b,
13429 const struct event_location *location,
55aa24fb
SDJ
13430 struct symtabs_and_lines *sals)
13431{
13432 /* We use the same method for breakpoint on probes. */
f00aae0f 13433 bkpt_probe_decode_location (b, location, sals);
55aa24fb
SDJ
13434}
13435
13436static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13437
5c2b4418
HZ
13438/* Dprintf breakpoint_ops methods. */
13439
13440static void
13441dprintf_re_set (struct breakpoint *b)
13442{
13443 breakpoint_re_set_default (b);
13444
f00aae0f
KS
13445 /* extra_string should never be non-NULL for dprintf. */
13446 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13447
13448 /* 1 - connect to target 1, that can run breakpoint commands.
13449 2 - create a dprintf, which resolves fine.
13450 3 - disconnect from target 1
13451 4 - connect to target 2, that can NOT run breakpoint commands.
13452
13453 After steps #3/#4, you'll want the dprintf command list to
13454 be updated, because target 1 and 2 may well return different
13455 answers for target_can_run_breakpoint_commands().
13456 Given absence of finer grained resetting, we get to do
13457 it all the time. */
13458 if (b->extra_string != NULL)
13459 update_dprintf_command_list (b);
13460}
13461
2d9442cc
HZ
13462/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13463
13464static void
13465dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13466{
f00aae0f
KS
13467 fprintf_unfiltered (fp, "dprintf %s,%s",
13468 event_location_to_string (tp->location),
2d9442cc
HZ
13469 tp->extra_string);
13470 print_recreate_thread (tp, fp);
13471}
13472
9d6e6e84
HZ
13473/* Implement the "after_condition_true" breakpoint_ops method for
13474 dprintf.
13475
13476 dprintf's are implemented with regular commands in their command
13477 list, but we run the commands here instead of before presenting the
13478 stop to the user, as dprintf's don't actually cause a stop. This
13479 also makes it so that the commands of multiple dprintfs at the same
13480 address are all handled. */
13481
13482static void
13483dprintf_after_condition_true (struct bpstats *bs)
13484{
13485 struct cleanup *old_chain;
13486 struct bpstats tmp_bs = { NULL };
13487 struct bpstats *tmp_bs_p = &tmp_bs;
13488
13489 /* dprintf's never cause a stop. This wasn't set in the
13490 check_status hook instead because that would make the dprintf's
13491 condition not be evaluated. */
13492 bs->stop = 0;
13493
13494 /* Run the command list here. Take ownership of it instead of
13495 copying. We never want these commands to run later in
13496 bpstat_do_actions, if a breakpoint that causes a stop happens to
13497 be set at same address as this dprintf, or even if running the
13498 commands here throws. */
13499 tmp_bs.commands = bs->commands;
13500 bs->commands = NULL;
13501 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13502
13503 bpstat_do_actions_1 (&tmp_bs_p);
13504
13505 /* 'tmp_bs.commands' will usually be NULL by now, but
13506 bpstat_do_actions_1 may return early without processing the whole
13507 list. */
13508 do_cleanups (old_chain);
13509}
13510
983af33b
SDJ
13511/* The breakpoint_ops structure to be used on static tracepoints with
13512 markers (`-m'). */
13513
13514static void
f00aae0f 13515strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13516 struct linespec_result *canonical,
f00aae0f 13517 enum bptype type_wanted)
983af33b
SDJ
13518{
13519 struct linespec_sals lsal;
f00aae0f
KS
13520 const char *arg_start, *arg;
13521 char *str;
13522 struct cleanup *cleanup;
983af33b 13523
f00aae0f
KS
13524 arg = arg_start = get_linespec_location (location);
13525 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13526
f00aae0f
KS
13527 str = savestring (arg_start, arg - arg_start);
13528 cleanup = make_cleanup (xfree, str);
13529 canonical->location = new_linespec_location (&str);
13530 do_cleanups (cleanup);
983af33b 13531
f00aae0f 13532 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
983af33b
SDJ
13533 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13534}
13535
13536static void
13537strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13538 struct linespec_result *canonical,
983af33b 13539 char *cond_string,
e7e0cddf 13540 char *extra_string,
983af33b
SDJ
13541 enum bptype type_wanted,
13542 enum bpdisp disposition,
13543 int thread,
13544 int task, int ignore_count,
13545 const struct breakpoint_ops *ops,
13546 int from_tty, int enabled,
44f238bb 13547 int internal, unsigned flags)
983af33b
SDJ
13548{
13549 int i;
52d361e1
YQ
13550 struct linespec_sals *lsal = VEC_index (linespec_sals,
13551 canonical->sals, 0);
983af33b
SDJ
13552
13553 /* If the user is creating a static tracepoint by marker id
13554 (strace -m MARKER_ID), then store the sals index, so that
13555 breakpoint_re_set can try to match up which of the newly
13556 found markers corresponds to this one, and, don't try to
13557 expand multiple locations for each sal, given than SALS
13558 already should contain all sals for MARKER_ID. */
13559
13560 for (i = 0; i < lsal->sals.nelts; ++i)
13561 {
13562 struct symtabs_and_lines expanded;
13563 struct tracepoint *tp;
13564 struct cleanup *old_chain;
f00aae0f 13565 struct event_location *location;
983af33b
SDJ
13566
13567 expanded.nelts = 1;
13568 expanded.sals = &lsal->sals.sals[i];
13569
f00aae0f
KS
13570 location = copy_event_location (canonical->location);
13571 old_chain = make_cleanup_delete_event_location (location);
983af33b
SDJ
13572
13573 tp = XCNEW (struct tracepoint);
13574 init_breakpoint_sal (&tp->base, gdbarch, expanded,
f00aae0f 13575 location, NULL,
e7e0cddf
SS
13576 cond_string, extra_string,
13577 type_wanted, disposition,
983af33b 13578 thread, task, ignore_count, ops,
44f238bb 13579 from_tty, enabled, internal, flags,
983af33b
SDJ
13580 canonical->special_display);
13581 /* Given that its possible to have multiple markers with
13582 the same string id, if the user is creating a static
13583 tracepoint by marker id ("strace -m MARKER_ID"), then
13584 store the sals index, so that breakpoint_re_set can
13585 try to match up which of the newly found markers
13586 corresponds to this one */
13587 tp->static_trace_marker_id_idx = i;
13588
13589 install_breakpoint (internal, &tp->base, 0);
13590
13591 discard_cleanups (old_chain);
13592 }
13593}
13594
13595static void
f00aae0f
KS
13596strace_marker_decode_location (struct breakpoint *b,
13597 const struct event_location *location,
983af33b
SDJ
13598 struct symtabs_and_lines *sals)
13599{
13600 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13601 const char *s = get_linespec_location (location);
983af33b 13602
f00aae0f 13603 *sals = decode_static_tracepoint_spec (&s);
983af33b
SDJ
13604 if (sals->nelts > tp->static_trace_marker_id_idx)
13605 {
13606 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13607 sals->nelts = 1;
13608 }
13609 else
13610 error (_("marker %s not found"), tp->static_trace_marker_id);
13611}
13612
13613static struct breakpoint_ops strace_marker_breakpoint_ops;
13614
13615static int
13616strace_marker_p (struct breakpoint *b)
13617{
13618 return b->ops == &strace_marker_breakpoint_ops;
13619}
13620
53a5351d 13621/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13622 structures. */
c906108c
SS
13623
13624void
fba45db2 13625delete_breakpoint (struct breakpoint *bpt)
c906108c 13626{
52f0bd74 13627 struct breakpoint *b;
c906108c 13628
8a3fe4f8 13629 gdb_assert (bpt != NULL);
c906108c 13630
4a64f543
MS
13631 /* Has this bp already been deleted? This can happen because
13632 multiple lists can hold pointers to bp's. bpstat lists are
13633 especial culprits.
13634
13635 One example of this happening is a watchpoint's scope bp. When
13636 the scope bp triggers, we notice that the watchpoint is out of
13637 scope, and delete it. We also delete its scope bp. But the
13638 scope bp is marked "auto-deleting", and is already on a bpstat.
13639 That bpstat is then checked for auto-deleting bp's, which are
13640 deleted.
13641
13642 A real solution to this problem might involve reference counts in
13643 bp's, and/or giving them pointers back to their referencing
13644 bpstat's, and teaching delete_breakpoint to only free a bp's
13645 storage when no more references were extent. A cheaper bandaid
13646 was chosen. */
c906108c
SS
13647 if (bpt->type == bp_none)
13648 return;
13649
4a64f543
MS
13650 /* At least avoid this stale reference until the reference counting
13651 of breakpoints gets resolved. */
d0fb5eae 13652 if (bpt->related_breakpoint != bpt)
e5a0a904 13653 {
d0fb5eae 13654 struct breakpoint *related;
3a5c3e22 13655 struct watchpoint *w;
d0fb5eae
JK
13656
13657 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13658 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13659 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13660 w = (struct watchpoint *) bpt;
13661 else
13662 w = NULL;
13663 if (w != NULL)
13664 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13665
13666 /* Unlink bpt from the bpt->related_breakpoint ring. */
13667 for (related = bpt; related->related_breakpoint != bpt;
13668 related = related->related_breakpoint);
13669 related->related_breakpoint = bpt->related_breakpoint;
13670 bpt->related_breakpoint = bpt;
e5a0a904
JK
13671 }
13672
a9634178
TJB
13673 /* watch_command_1 creates a watchpoint but only sets its number if
13674 update_watchpoint succeeds in creating its bp_locations. If there's
13675 a problem in that process, we'll be asked to delete the half-created
13676 watchpoint. In that case, don't announce the deletion. */
13677 if (bpt->number)
13678 observer_notify_breakpoint_deleted (bpt);
c906108c 13679
c906108c
SS
13680 if (breakpoint_chain == bpt)
13681 breakpoint_chain = bpt->next;
13682
c906108c
SS
13683 ALL_BREAKPOINTS (b)
13684 if (b->next == bpt)
c5aa993b
JM
13685 {
13686 b->next = bpt->next;
13687 break;
13688 }
c906108c 13689
f431efe5
PA
13690 /* Be sure no bpstat's are pointing at the breakpoint after it's
13691 been freed. */
13692 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13693 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13694 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13695 commands are associated with the bpstat; if we remove it here,
13696 then the later call to bpstat_do_actions (&stop_bpstat); in
13697 event-top.c won't do anything, and temporary breakpoints with
13698 commands won't work. */
13699
13700 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13701
4a64f543
MS
13702 /* Now that breakpoint is removed from breakpoint list, update the
13703 global location list. This will remove locations that used to
13704 belong to this breakpoint. Do this before freeing the breakpoint
13705 itself, since remove_breakpoint looks at location's owner. It
13706 might be better design to have location completely
13707 self-contained, but it's not the case now. */
44702360 13708 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13709
348d480f 13710 bpt->ops->dtor (bpt);
4a64f543
MS
13711 /* On the chance that someone will soon try again to delete this
13712 same bp, we mark it as deleted before freeing its storage. */
c906108c 13713 bpt->type = bp_none;
b8c9b27d 13714 xfree (bpt);
c906108c
SS
13715}
13716
4d6140d9
AC
13717static void
13718do_delete_breakpoint_cleanup (void *b)
13719{
13720 delete_breakpoint (b);
13721}
13722
13723struct cleanup *
13724make_cleanup_delete_breakpoint (struct breakpoint *b)
13725{
13726 return make_cleanup (do_delete_breakpoint_cleanup, b);
13727}
13728
51be5b68
PA
13729/* Iterator function to call a user-provided callback function once
13730 for each of B and its related breakpoints. */
13731
13732static void
13733iterate_over_related_breakpoints (struct breakpoint *b,
13734 void (*function) (struct breakpoint *,
13735 void *),
13736 void *data)
13737{
13738 struct breakpoint *related;
13739
13740 related = b;
13741 do
13742 {
13743 struct breakpoint *next;
13744
13745 /* FUNCTION may delete RELATED. */
13746 next = related->related_breakpoint;
13747
13748 if (next == related)
13749 {
13750 /* RELATED is the last ring entry. */
13751 function (related, data);
13752
13753 /* FUNCTION may have deleted it, so we'd never reach back to
13754 B. There's nothing left to do anyway, so just break
13755 out. */
13756 break;
13757 }
13758 else
13759 function (related, data);
13760
13761 related = next;
13762 }
13763 while (related != b);
13764}
95a42b64
TT
13765
13766static void
13767do_delete_breakpoint (struct breakpoint *b, void *ignore)
13768{
13769 delete_breakpoint (b);
13770}
13771
51be5b68
PA
13772/* A callback for map_breakpoint_numbers that calls
13773 delete_breakpoint. */
13774
13775static void
13776do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13777{
13778 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13779}
13780
c906108c 13781void
fba45db2 13782delete_command (char *arg, int from_tty)
c906108c 13783{
35df4500 13784 struct breakpoint *b, *b_tmp;
c906108c 13785
ea9365bb
TT
13786 dont_repeat ();
13787
c906108c
SS
13788 if (arg == 0)
13789 {
13790 int breaks_to_delete = 0;
13791
46c6471b
PA
13792 /* Delete all breakpoints if no argument. Do not delete
13793 internal breakpoints, these have to be deleted with an
13794 explicit breakpoint number argument. */
c5aa993b 13795 ALL_BREAKPOINTS (b)
46c6471b 13796 if (user_breakpoint_p (b))
973d738b
DJ
13797 {
13798 breaks_to_delete = 1;
13799 break;
13800 }
c906108c
SS
13801
13802 /* Ask user only if there are some breakpoints to delete. */
13803 if (!from_tty
e2e0b3e5 13804 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13805 {
35df4500 13806 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13807 if (user_breakpoint_p (b))
c5aa993b 13808 delete_breakpoint (b);
c906108c
SS
13809 }
13810 }
13811 else
51be5b68 13812 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13813}
13814
0d381245
VP
13815static int
13816all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 13817{
0d381245 13818 for (; loc; loc = loc->next)
8645ff69
UW
13819 if (!loc->shlib_disabled
13820 && !loc->pspace->executing_startup)
0d381245
VP
13821 return 0;
13822 return 1;
fe3f5fa8
VP
13823}
13824
776592bf
DE
13825/* Subroutine of update_breakpoint_locations to simplify it.
13826 Return non-zero if multiple fns in list LOC have the same name.
13827 Null names are ignored. */
13828
13829static int
13830ambiguous_names_p (struct bp_location *loc)
13831{
13832 struct bp_location *l;
13833 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13834 (int (*) (const void *,
13835 const void *)) streq,
776592bf
DE
13836 NULL, xcalloc, xfree);
13837
13838 for (l = loc; l != NULL; l = l->next)
13839 {
13840 const char **slot;
13841 const char *name = l->function_name;
13842
13843 /* Allow for some names to be NULL, ignore them. */
13844 if (name == NULL)
13845 continue;
13846
13847 slot = (const char **) htab_find_slot (htab, (const void *) name,
13848 INSERT);
4a64f543
MS
13849 /* NOTE: We can assume slot != NULL here because xcalloc never
13850 returns NULL. */
776592bf
DE
13851 if (*slot != NULL)
13852 {
13853 htab_delete (htab);
13854 return 1;
13855 }
13856 *slot = name;
13857 }
13858
13859 htab_delete (htab);
13860 return 0;
13861}
13862
0fb4aa4b
PA
13863/* When symbols change, it probably means the sources changed as well,
13864 and it might mean the static tracepoint markers are no longer at
13865 the same address or line numbers they used to be at last we
13866 checked. Losing your static tracepoints whenever you rebuild is
13867 undesirable. This function tries to resync/rematch gdb static
13868 tracepoints with the markers on the target, for static tracepoints
13869 that have not been set by marker id. Static tracepoint that have
13870 been set by marker id are reset by marker id in breakpoint_re_set.
13871 The heuristic is:
13872
13873 1) For a tracepoint set at a specific address, look for a marker at
13874 the old PC. If one is found there, assume to be the same marker.
13875 If the name / string id of the marker found is different from the
13876 previous known name, assume that means the user renamed the marker
13877 in the sources, and output a warning.
13878
13879 2) For a tracepoint set at a given line number, look for a marker
13880 at the new address of the old line number. If one is found there,
13881 assume to be the same marker. If the name / string id of the
13882 marker found is different from the previous known name, assume that
13883 means the user renamed the marker in the sources, and output a
13884 warning.
13885
13886 3) If a marker is no longer found at the same address or line, it
13887 may mean the marker no longer exists. But it may also just mean
13888 the code changed a bit. Maybe the user added a few lines of code
13889 that made the marker move up or down (in line number terms). Ask
13890 the target for info about the marker with the string id as we knew
13891 it. If found, update line number and address in the matching
13892 static tracepoint. This will get confused if there's more than one
13893 marker with the same ID (possible in UST, although unadvised
13894 precisely because it confuses tools). */
13895
13896static struct symtab_and_line
13897update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13898{
d9b3f62e 13899 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13900 struct static_tracepoint_marker marker;
13901 CORE_ADDR pc;
0fb4aa4b
PA
13902
13903 pc = sal.pc;
13904 if (sal.line)
13905 find_line_pc (sal.symtab, sal.line, &pc);
13906
13907 if (target_static_tracepoint_marker_at (pc, &marker))
13908 {
d9b3f62e 13909 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13910 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13911 b->number,
d9b3f62e 13912 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13913
d9b3f62e
PA
13914 xfree (tp->static_trace_marker_id);
13915 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13916 release_static_tracepoint_marker (&marker);
13917
13918 return sal;
13919 }
13920
13921 /* Old marker wasn't found on target at lineno. Try looking it up
13922 by string ID. */
13923 if (!sal.explicit_pc
13924 && sal.line != 0
13925 && sal.symtab != NULL
d9b3f62e 13926 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13927 {
13928 VEC(static_tracepoint_marker_p) *markers;
13929
13930 markers
d9b3f62e 13931 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13932
13933 if (!VEC_empty(static_tracepoint_marker_p, markers))
13934 {
f00aae0f 13935 char *p, *tmp;
80e1d417 13936 struct symtab_and_line sal2;
0fb4aa4b 13937 struct symbol *sym;
80e1d417 13938 struct static_tracepoint_marker *tpmarker;
79a45e25 13939 struct ui_out *uiout = current_uiout;
f00aae0f 13940 struct cleanup *cleanup;
0fb4aa4b 13941
80e1d417 13942 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13943
d9b3f62e 13944 xfree (tp->static_trace_marker_id);
80e1d417 13945 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13946
13947 warning (_("marker for static tracepoint %d (%s) not "
13948 "found at previous line number"),
d9b3f62e 13949 b->number, tp->static_trace_marker_id);
0fb4aa4b 13950
80e1d417 13951 init_sal (&sal2);
0fb4aa4b 13952
80e1d417 13953 sal2.pc = tpmarker->address;
0fb4aa4b 13954
80e1d417
AS
13955 sal2 = find_pc_line (tpmarker->address, 0);
13956 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
13957 ui_out_text (uiout, "Now in ");
13958 if (sym)
13959 {
13960 ui_out_field_string (uiout, "func",
13961 SYMBOL_PRINT_NAME (sym));
13962 ui_out_text (uiout, " at ");
13963 }
05cba821
JK
13964 ui_out_field_string (uiout, "file",
13965 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
13966 ui_out_text (uiout, ":");
13967
13968 if (ui_out_is_mi_like_p (uiout))
13969 {
0b0865da 13970 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13971
f35a17b5 13972 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
13973 }
13974
80e1d417 13975 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
13976 ui_out_text (uiout, "\n");
13977
80e1d417 13978 b->loc->line_number = sal2.line;
2f202fde 13979 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13980
f00aae0f
KS
13981 delete_event_location (b->location);
13982 p = tmp = xstrprintf ("%s:%d",
13983 symtab_to_filename_for_display (sal2.symtab),
13984 b->loc->line_number);
13985 cleanup = make_cleanup (xfree, tmp);
13986 b->location = new_linespec_location (&tmp);
13987 do_cleanups (cleanup);
0fb4aa4b
PA
13988
13989 /* Might be nice to check if function changed, and warn if
13990 so. */
13991
80e1d417 13992 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13993 }
13994 }
13995 return sal;
13996}
13997
8d3788bd
VP
13998/* Returns 1 iff locations A and B are sufficiently same that
13999 we don't need to report breakpoint as changed. */
14000
14001static int
14002locations_are_equal (struct bp_location *a, struct bp_location *b)
14003{
14004 while (a && b)
14005 {
14006 if (a->address != b->address)
14007 return 0;
14008
14009 if (a->shlib_disabled != b->shlib_disabled)
14010 return 0;
14011
14012 if (a->enabled != b->enabled)
14013 return 0;
14014
14015 a = a->next;
14016 b = b->next;
14017 }
14018
14019 if ((a == NULL) != (b == NULL))
14020 return 0;
14021
14022 return 1;
14023}
14024
f1310107
TJB
14025/* Create new breakpoint locations for B (a hardware or software breakpoint)
14026 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14027 a ranged breakpoint. */
14028
0e30163f 14029void
0d381245 14030update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14031 struct symtabs_and_lines sals,
14032 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14033{
14034 int i;
0d381245
VP
14035 struct bp_location *existing_locations = b->loc;
14036
f8eba3c6
TT
14037 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14038 {
14039 /* Ranged breakpoints have only one start location and one end
14040 location. */
14041 b->enable_state = bp_disabled;
44702360 14042 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
14043 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14044 "multiple locations found\n"),
14045 b->number);
14046 return;
14047 }
f1310107 14048
4a64f543
MS
14049 /* If there's no new locations, and all existing locations are
14050 pending, don't do anything. This optimizes the common case where
14051 all locations are in the same shared library, that was unloaded.
14052 We'd like to retain the location, so that when the library is
14053 loaded again, we don't loose the enabled/disabled status of the
14054 individual locations. */
0d381245 14055 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14056 return;
14057
fe3f5fa8
VP
14058 b->loc = NULL;
14059
0d381245 14060 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14061 {
f8eba3c6
TT
14062 struct bp_location *new_loc;
14063
14064 switch_to_program_space_and_thread (sals.sals[i].pspace);
14065
14066 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14067
0d381245
VP
14068 /* Reparse conditions, they might contain references to the
14069 old symtab. */
14070 if (b->cond_string != NULL)
14071 {
bbc13ae3 14072 const char *s;
fe3f5fa8 14073
0d381245 14074 s = b->cond_string;
492d29ea 14075 TRY
0d381245 14076 {
1bb9788d
TT
14077 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14078 block_for_pc (sals.sals[i].pc),
0d381245
VP
14079 0);
14080 }
492d29ea 14081 CATCH (e, RETURN_MASK_ERROR)
0d381245 14082 {
3e43a32a
MS
14083 warning (_("failed to reevaluate condition "
14084 "for breakpoint %d: %s"),
0d381245
VP
14085 b->number, e.message);
14086 new_loc->enabled = 0;
14087 }
492d29ea 14088 END_CATCH
0d381245 14089 }
fe3f5fa8 14090
f1310107
TJB
14091 if (sals_end.nelts)
14092 {
14093 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14094
14095 new_loc->length = end - sals.sals[0].pc + 1;
14096 }
0d381245 14097 }
fe3f5fa8 14098
4a64f543
MS
14099 /* If possible, carry over 'disable' status from existing
14100 breakpoints. */
0d381245
VP
14101 {
14102 struct bp_location *e = existing_locations;
776592bf
DE
14103 /* If there are multiple breakpoints with the same function name,
14104 e.g. for inline functions, comparing function names won't work.
14105 Instead compare pc addresses; this is just a heuristic as things
14106 may have moved, but in practice it gives the correct answer
14107 often enough until a better solution is found. */
14108 int have_ambiguous_names = ambiguous_names_p (b->loc);
14109
0d381245
VP
14110 for (; e; e = e->next)
14111 {
14112 if (!e->enabled && e->function_name)
14113 {
14114 struct bp_location *l = b->loc;
776592bf
DE
14115 if (have_ambiguous_names)
14116 {
14117 for (; l; l = l->next)
f1310107 14118 if (breakpoint_locations_match (e, l))
776592bf
DE
14119 {
14120 l->enabled = 0;
14121 break;
14122 }
14123 }
14124 else
14125 {
14126 for (; l; l = l->next)
14127 if (l->function_name
14128 && strcmp (e->function_name, l->function_name) == 0)
14129 {
14130 l->enabled = 0;
14131 break;
14132 }
14133 }
0d381245
VP
14134 }
14135 }
14136 }
fe3f5fa8 14137
8d3788bd
VP
14138 if (!locations_are_equal (existing_locations, b->loc))
14139 observer_notify_breakpoint_modified (b);
14140
44702360 14141 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14142}
14143
f00aae0f 14144/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
14145 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14146
14147static struct symtabs_and_lines
f00aae0f
KS
14148location_to_sals (struct breakpoint *b, struct event_location *location,
14149 int *found)
ef23e705 14150{
02d20e4a 14151 struct symtabs_and_lines sals = {0};
492d29ea 14152 struct gdb_exception exception = exception_none;
ef23e705 14153
983af33b 14154 gdb_assert (b->ops != NULL);
ef23e705 14155
492d29ea 14156 TRY
ef23e705 14157 {
f00aae0f 14158 b->ops->decode_location (b, location, &sals);
ef23e705 14159 }
492d29ea 14160 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14161 {
14162 int not_found_and_ok = 0;
492d29ea
PA
14163
14164 exception = e;
14165
ef23e705
TJB
14166 /* For pending breakpoints, it's expected that parsing will
14167 fail until the right shared library is loaded. User has
14168 already told to create pending breakpoints and don't need
14169 extra messages. If breakpoint is in bp_shlib_disabled
14170 state, then user already saw the message about that
14171 breakpoint being disabled, and don't want to see more
14172 errors. */
58438ac1 14173 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14174 && (b->condition_not_parsed
14175 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14176 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14177 || b->enable_state == bp_disabled))
14178 not_found_and_ok = 1;
14179
14180 if (!not_found_and_ok)
14181 {
14182 /* We surely don't want to warn about the same breakpoint
14183 10 times. One solution, implemented here, is disable
14184 the breakpoint on error. Another solution would be to
14185 have separate 'warning emitted' flag. Since this
14186 happens only when a binary has changed, I don't know
14187 which approach is better. */
14188 b->enable_state = bp_disabled;
14189 throw_exception (e);
14190 }
14191 }
492d29ea 14192 END_CATCH
ef23e705 14193
492d29ea 14194 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14195 {
f8eba3c6 14196 int i;
ef23e705 14197
f8eba3c6
TT
14198 for (i = 0; i < sals.nelts; ++i)
14199 resolve_sal_pc (&sals.sals[i]);
f00aae0f 14200 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14201 {
ed1d1739
KS
14202 char *cond_string, *extra_string;
14203 int thread, task;
ef23e705 14204
f00aae0f 14205 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
e7e0cddf
SS
14206 &cond_string, &thread, &task,
14207 &extra_string);
f00aae0f 14208 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14209 if (cond_string)
14210 b->cond_string = cond_string;
14211 b->thread = thread;
14212 b->task = task;
e7e0cddf 14213 if (extra_string)
f00aae0f
KS
14214 {
14215 xfree (b->extra_string);
14216 b->extra_string = extra_string;
14217 }
ef23e705
TJB
14218 b->condition_not_parsed = 0;
14219 }
14220
983af33b 14221 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14222 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14223
58438ac1
TT
14224 *found = 1;
14225 }
14226 else
14227 *found = 0;
ef23e705
TJB
14228
14229 return sals;
14230}
14231
348d480f
PA
14232/* The default re_set method, for typical hardware or software
14233 breakpoints. Reevaluate the breakpoint and recreate its
14234 locations. */
14235
14236static void
28010a5d 14237breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14238{
14239 int found;
f1310107 14240 struct symtabs_and_lines sals, sals_end;
ef23e705 14241 struct symtabs_and_lines expanded = {0};
f1310107 14242 struct symtabs_and_lines expanded_end = {0};
ef23e705 14243
f00aae0f 14244 sals = location_to_sals (b, b->location, &found);
ef23e705
TJB
14245 if (found)
14246 {
14247 make_cleanup (xfree, sals.sals);
f8eba3c6 14248 expanded = sals;
ef23e705
TJB
14249 }
14250
f00aae0f 14251 if (b->location_range_end != NULL)
f1310107 14252 {
f00aae0f 14253 sals_end = location_to_sals (b, b->location_range_end, &found);
f1310107
TJB
14254 if (found)
14255 {
14256 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14257 expanded_end = sals_end;
f1310107
TJB
14258 }
14259 }
14260
14261 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14262}
14263
983af33b
SDJ
14264/* Default method for creating SALs from an address string. It basically
14265 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14266
14267static void
f00aae0f
KS
14268create_sals_from_location_default (const struct event_location *location,
14269 struct linespec_result *canonical,
14270 enum bptype type_wanted)
983af33b 14271{
f00aae0f 14272 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14273}
14274
14275/* Call create_breakpoints_sal for the given arguments. This is the default
14276 function for the `create_breakpoints_sal' method of
14277 breakpoint_ops. */
14278
14279static void
14280create_breakpoints_sal_default (struct gdbarch *gdbarch,
14281 struct linespec_result *canonical,
983af33b 14282 char *cond_string,
e7e0cddf 14283 char *extra_string,
983af33b
SDJ
14284 enum bptype type_wanted,
14285 enum bpdisp disposition,
14286 int thread,
14287 int task, int ignore_count,
14288 const struct breakpoint_ops *ops,
14289 int from_tty, int enabled,
44f238bb 14290 int internal, unsigned flags)
983af33b
SDJ
14291{
14292 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14293 extra_string,
983af33b
SDJ
14294 type_wanted, disposition,
14295 thread, task, ignore_count, ops, from_tty,
44f238bb 14296 enabled, internal, flags);
983af33b
SDJ
14297}
14298
14299/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14300 default function for the `decode_location' method of breakpoint_ops. */
983af33b
SDJ
14301
14302static void
f00aae0f
KS
14303decode_location_default (struct breakpoint *b,
14304 const struct event_location *location,
983af33b
SDJ
14305 struct symtabs_and_lines *sals)
14306{
14307 struct linespec_result canonical;
14308
14309 init_linespec_result (&canonical);
f00aae0f 14310 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
983af33b
SDJ
14311 (struct symtab *) NULL, 0,
14312 &canonical, multiple_symbols_all,
14313 b->filter);
14314
14315 /* We should get 0 or 1 resulting SALs. */
14316 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14317
14318 if (VEC_length (linespec_sals, canonical.sals) > 0)
14319 {
14320 struct linespec_sals *lsal;
14321
14322 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14323 *sals = lsal->sals;
14324 /* Arrange it so the destructor does not free the
14325 contents. */
14326 lsal->sals.sals = NULL;
14327 }
14328
14329 destroy_linespec_result (&canonical);
14330}
14331
28010a5d
PA
14332/* Prepare the global context for a re-set of breakpoint B. */
14333
14334static struct cleanup *
14335prepare_re_set_context (struct breakpoint *b)
14336{
14337 struct cleanup *cleanups;
14338
14339 input_radix = b->input_radix;
14340 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14341 if (b->pspace != NULL)
14342 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14343 set_language (b->language);
14344
14345 return cleanups;
ef23e705
TJB
14346}
14347
c906108c
SS
14348/* Reset a breakpoint given it's struct breakpoint * BINT.
14349 The value we return ends up being the return value from catch_errors.
14350 Unused in this case. */
14351
14352static int
4efb68b1 14353breakpoint_re_set_one (void *bint)
c906108c 14354{
4a64f543 14355 /* Get past catch_errs. */
53a5351d 14356 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14357 struct cleanup *cleanups;
c906108c 14358
348d480f
PA
14359 cleanups = prepare_re_set_context (b);
14360 b->ops->re_set (b);
14361 do_cleanups (cleanups);
c906108c
SS
14362 return 0;
14363}
14364
69de3c6a 14365/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14366void
69de3c6a 14367breakpoint_re_set (void)
c906108c 14368{
35df4500 14369 struct breakpoint *b, *b_tmp;
c906108c
SS
14370 enum language save_language;
14371 int save_input_radix;
6c95b8df 14372 struct cleanup *old_chain;
c5aa993b 14373
c906108c
SS
14374 save_language = current_language->la_language;
14375 save_input_radix = input_radix;
6c95b8df
PA
14376 old_chain = save_current_program_space ();
14377
35df4500 14378 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14379 {
4a64f543 14380 /* Format possible error msg. */
fe3f5fa8 14381 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14382 b->number);
14383 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14384 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14385 do_cleanups (cleanups);
c5aa993b 14386 }
c906108c
SS
14387 set_language (save_language);
14388 input_radix = save_input_radix;
e62c965a 14389
0756c555 14390 jit_breakpoint_re_set ();
4efc6507 14391
6c95b8df
PA
14392 do_cleanups (old_chain);
14393
af02033e
PP
14394 create_overlay_event_breakpoint ();
14395 create_longjmp_master_breakpoint ();
14396 create_std_terminate_master_breakpoint ();
186c406b 14397 create_exception_master_breakpoint ();
c906108c
SS
14398}
14399\f
c906108c
SS
14400/* Reset the thread number of this breakpoint:
14401
14402 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14403 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14404void
fba45db2 14405breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14406{
14407 if (b->thread != -1)
14408 {
39f77062
KB
14409 if (in_thread_list (inferior_ptid))
14410 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14411
14412 /* We're being called after following a fork. The new fork is
14413 selected as current, and unless this was a vfork will have a
14414 different program space from the original thread. Reset that
14415 as well. */
14416 b->loc->pspace = current_program_space;
c906108c
SS
14417 }
14418}
14419
03ac34d5
MS
14420/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14421 If from_tty is nonzero, it prints a message to that effect,
14422 which ends with a period (no newline). */
14423
c906108c 14424void
fba45db2 14425set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14426{
52f0bd74 14427 struct breakpoint *b;
c906108c
SS
14428
14429 if (count < 0)
14430 count = 0;
14431
14432 ALL_BREAKPOINTS (b)
14433 if (b->number == bptnum)
c5aa993b 14434 {
d77f58be
SS
14435 if (is_tracepoint (b))
14436 {
14437 if (from_tty && count != 0)
14438 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14439 bptnum);
14440 return;
14441 }
14442
c5aa993b 14443 b->ignore_count = count;
221ea385
KS
14444 if (from_tty)
14445 {
14446 if (count == 0)
3e43a32a
MS
14447 printf_filtered (_("Will stop next time "
14448 "breakpoint %d is reached."),
221ea385
KS
14449 bptnum);
14450 else if (count == 1)
a3f17187 14451 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14452 bptnum);
14453 else
3e43a32a
MS
14454 printf_filtered (_("Will ignore next %d "
14455 "crossings of breakpoint %d."),
221ea385
KS
14456 count, bptnum);
14457 }
8d3788bd 14458 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14459 return;
14460 }
c906108c 14461
8a3fe4f8 14462 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14463}
14464
c906108c
SS
14465/* Command to set ignore-count of breakpoint N to COUNT. */
14466
14467static void
fba45db2 14468ignore_command (char *args, int from_tty)
c906108c
SS
14469{
14470 char *p = args;
52f0bd74 14471 int num;
c906108c
SS
14472
14473 if (p == 0)
e2e0b3e5 14474 error_no_arg (_("a breakpoint number"));
c5aa993b 14475
c906108c 14476 num = get_number (&p);
5c44784c 14477 if (num == 0)
8a3fe4f8 14478 error (_("bad breakpoint number: '%s'"), args);
c906108c 14479 if (*p == 0)
8a3fe4f8 14480 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14481
14482 set_ignore_count (num,
14483 longest_to_int (value_as_long (parse_and_eval (p))),
14484 from_tty);
221ea385
KS
14485 if (from_tty)
14486 printf_filtered ("\n");
c906108c
SS
14487}
14488\f
14489/* Call FUNCTION on each of the breakpoints
14490 whose numbers are given in ARGS. */
14491
14492static void
95a42b64
TT
14493map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14494 void *),
14495 void *data)
c906108c 14496{
52f0bd74
AC
14497 int num;
14498 struct breakpoint *b, *tmp;
11cf8741 14499 int match;
197f0a60 14500 struct get_number_or_range_state state;
c906108c 14501
b9d61307 14502 if (args == 0 || *args == '\0')
e2e0b3e5 14503 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14504
197f0a60
TT
14505 init_number_or_range (&state, args);
14506
14507 while (!state.finished)
c906108c 14508 {
e799154c 14509 const char *p = state.string;
197f0a60 14510
11cf8741 14511 match = 0;
c5aa993b 14512
197f0a60 14513 num = get_number_or_range (&state);
5c44784c 14514 if (num == 0)
c5aa993b 14515 {
8a3fe4f8 14516 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14517 }
14518 else
14519 {
14520 ALL_BREAKPOINTS_SAFE (b, tmp)
14521 if (b->number == num)
14522 {
11cf8741 14523 match = 1;
cdac0397 14524 function (b, data);
11cf8741 14525 break;
5c44784c 14526 }
11cf8741 14527 if (match == 0)
a3f17187 14528 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14529 }
c906108c
SS
14530 }
14531}
14532
0d381245
VP
14533static struct bp_location *
14534find_location_by_number (char *number)
14535{
14536 char *dot = strchr (number, '.');
14537 char *p1;
14538 int bp_num;
14539 int loc_num;
14540 struct breakpoint *b;
14541 struct bp_location *loc;
14542
14543 *dot = '\0';
14544
14545 p1 = number;
197f0a60 14546 bp_num = get_number (&p1);
0d381245
VP
14547 if (bp_num == 0)
14548 error (_("Bad breakpoint number '%s'"), number);
14549
14550 ALL_BREAKPOINTS (b)
14551 if (b->number == bp_num)
14552 {
14553 break;
14554 }
14555
14556 if (!b || b->number != bp_num)
14557 error (_("Bad breakpoint number '%s'"), number);
14558
14559 p1 = dot+1;
197f0a60 14560 loc_num = get_number (&p1);
0d381245
VP
14561 if (loc_num == 0)
14562 error (_("Bad breakpoint location number '%s'"), number);
14563
14564 --loc_num;
14565 loc = b->loc;
14566 for (;loc_num && loc; --loc_num, loc = loc->next)
14567 ;
14568 if (!loc)
14569 error (_("Bad breakpoint location number '%s'"), dot+1);
14570
14571 return loc;
14572}
14573
14574
1900040c
MS
14575/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14576 If from_tty is nonzero, it prints a message to that effect,
14577 which ends with a period (no newline). */
14578
c906108c 14579void
fba45db2 14580disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14581{
14582 /* Never disable a watchpoint scope breakpoint; we want to
14583 hit them when we leave scope so we can delete both the
14584 watchpoint and its scope breakpoint at that time. */
14585 if (bpt->type == bp_watchpoint_scope)
14586 return;
14587
b5de0fa7 14588 bpt->enable_state = bp_disabled;
c906108c 14589
b775012e
LM
14590 /* Mark breakpoint locations modified. */
14591 mark_breakpoint_modified (bpt);
14592
d248b706
KY
14593 if (target_supports_enable_disable_tracepoint ()
14594 && current_trace_status ()->running && is_tracepoint (bpt))
14595 {
14596 struct bp_location *location;
14597
14598 for (location = bpt->loc; location; location = location->next)
14599 target_disable_tracepoint (location);
14600 }
14601
44702360 14602 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14603
8d3788bd 14604 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14605}
14606
51be5b68
PA
14607/* A callback for iterate_over_related_breakpoints. */
14608
14609static void
14610do_disable_breakpoint (struct breakpoint *b, void *ignore)
14611{
14612 disable_breakpoint (b);
14613}
14614
95a42b64
TT
14615/* A callback for map_breakpoint_numbers that calls
14616 disable_breakpoint. */
14617
14618static void
14619do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14620{
51be5b68 14621 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14622}
14623
c906108c 14624static void
fba45db2 14625disable_command (char *args, int from_tty)
c906108c 14626{
c906108c 14627 if (args == 0)
46c6471b
PA
14628 {
14629 struct breakpoint *bpt;
14630
14631 ALL_BREAKPOINTS (bpt)
14632 if (user_breakpoint_p (bpt))
14633 disable_breakpoint (bpt);
14634 }
9eaabc75 14635 else
0d381245 14636 {
9eaabc75
MW
14637 char *num = extract_arg (&args);
14638
14639 while (num)
d248b706 14640 {
9eaabc75 14641 if (strchr (num, '.'))
b775012e 14642 {
9eaabc75
MW
14643 struct bp_location *loc = find_location_by_number (num);
14644
14645 if (loc)
14646 {
14647 if (loc->enabled)
14648 {
14649 loc->enabled = 0;
14650 mark_breakpoint_location_modified (loc);
14651 }
14652 if (target_supports_enable_disable_tracepoint ()
14653 && current_trace_status ()->running && loc->owner
14654 && is_tracepoint (loc->owner))
14655 target_disable_tracepoint (loc);
14656 }
44702360 14657 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14658 }
9eaabc75
MW
14659 else
14660 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14661 num = extract_arg (&args);
d248b706 14662 }
0d381245 14663 }
c906108c
SS
14664}
14665
14666static void
816338b5
SS
14667enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14668 int count)
c906108c 14669{
afe38095 14670 int target_resources_ok;
c906108c
SS
14671
14672 if (bpt->type == bp_hardware_breakpoint)
14673 {
14674 int i;
c5aa993b 14675 i = hw_breakpoint_used_count ();
53a5351d 14676 target_resources_ok =
d92524f1 14677 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14678 i + 1, 0);
c906108c 14679 if (target_resources_ok == 0)
8a3fe4f8 14680 error (_("No hardware breakpoint support in the target."));
c906108c 14681 else if (target_resources_ok < 0)
8a3fe4f8 14682 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14683 }
14684
cc60f2e3 14685 if (is_watchpoint (bpt))
c906108c 14686 {
d07205c2 14687 /* Initialize it just to avoid a GCC false warning. */
f486487f 14688 enum enable_state orig_enable_state = bp_disabled;
dde02812 14689
492d29ea 14690 TRY
c906108c 14691 {
3a5c3e22
PA
14692 struct watchpoint *w = (struct watchpoint *) bpt;
14693
1e718ff1
TJB
14694 orig_enable_state = bpt->enable_state;
14695 bpt->enable_state = bp_enabled;
3a5c3e22 14696 update_watchpoint (w, 1 /* reparse */);
c906108c 14697 }
492d29ea 14698 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14699 {
1e718ff1 14700 bpt->enable_state = orig_enable_state;
dde02812
ES
14701 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14702 bpt->number);
14703 return;
c5aa993b 14704 }
492d29ea 14705 END_CATCH
c906108c 14706 }
0101ce28 14707
b775012e
LM
14708 bpt->enable_state = bp_enabled;
14709
14710 /* Mark breakpoint locations modified. */
14711 mark_breakpoint_modified (bpt);
14712
d248b706
KY
14713 if (target_supports_enable_disable_tracepoint ()
14714 && current_trace_status ()->running && is_tracepoint (bpt))
14715 {
14716 struct bp_location *location;
14717
14718 for (location = bpt->loc; location; location = location->next)
14719 target_enable_tracepoint (location);
14720 }
14721
b4c291bb 14722 bpt->disposition = disposition;
816338b5 14723 bpt->enable_count = count;
44702360 14724 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14725
8d3788bd 14726 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14727}
14728
fe3f5fa8 14729
c906108c 14730void
fba45db2 14731enable_breakpoint (struct breakpoint *bpt)
c906108c 14732{
816338b5 14733 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14734}
14735
14736static void
14737do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14738{
14739 enable_breakpoint (bpt);
c906108c
SS
14740}
14741
95a42b64
TT
14742/* A callback for map_breakpoint_numbers that calls
14743 enable_breakpoint. */
14744
14745static void
14746do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14747{
51be5b68 14748 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14749}
14750
c906108c
SS
14751/* The enable command enables the specified breakpoints (or all defined
14752 breakpoints) so they once again become (or continue to be) effective
1272ad14 14753 in stopping the inferior. */
c906108c 14754
c906108c 14755static void
fba45db2 14756enable_command (char *args, int from_tty)
c906108c 14757{
c906108c 14758 if (args == 0)
46c6471b
PA
14759 {
14760 struct breakpoint *bpt;
14761
14762 ALL_BREAKPOINTS (bpt)
14763 if (user_breakpoint_p (bpt))
14764 enable_breakpoint (bpt);
14765 }
9eaabc75 14766 else
0d381245 14767 {
9eaabc75
MW
14768 char *num = extract_arg (&args);
14769
14770 while (num)
d248b706 14771 {
9eaabc75 14772 if (strchr (num, '.'))
b775012e 14773 {
9eaabc75
MW
14774 struct bp_location *loc = find_location_by_number (num);
14775
14776 if (loc)
14777 {
14778 if (!loc->enabled)
14779 {
14780 loc->enabled = 1;
14781 mark_breakpoint_location_modified (loc);
14782 }
14783 if (target_supports_enable_disable_tracepoint ()
14784 && current_trace_status ()->running && loc->owner
14785 && is_tracepoint (loc->owner))
14786 target_enable_tracepoint (loc);
14787 }
44702360 14788 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14789 }
9eaabc75
MW
14790 else
14791 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14792 num = extract_arg (&args);
d248b706 14793 }
0d381245 14794 }
c906108c
SS
14795}
14796
816338b5
SS
14797/* This struct packages up disposition data for application to multiple
14798 breakpoints. */
14799
14800struct disp_data
14801{
14802 enum bpdisp disp;
14803 int count;
14804};
14805
c906108c 14806static void
51be5b68
PA
14807do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14808{
816338b5 14809 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14810
816338b5 14811 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14812}
14813
14814static void
14815do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14816{
816338b5 14817 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14818
14819 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14820}
14821
c906108c 14822static void
fba45db2 14823enable_once_command (char *args, int from_tty)
c906108c 14824{
51be5b68 14825 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14826}
14827
816338b5
SS
14828static void
14829do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14830{
14831 struct disp_data disp = { disp_disable, *(int *) countptr };
14832
14833 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14834}
14835
14836static void
14837enable_count_command (char *args, int from_tty)
14838{
b9d61307
SM
14839 int count;
14840
14841 if (args == NULL)
14842 error_no_arg (_("hit count"));
14843
14844 count = get_number (&args);
816338b5
SS
14845
14846 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14847}
14848
c906108c 14849static void
51be5b68 14850do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14851{
816338b5 14852 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14853
14854 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14855}
14856
c906108c 14857static void
fba45db2 14858enable_delete_command (char *args, int from_tty)
c906108c 14859{
51be5b68 14860 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14861}
14862\f
fa8d40ab
JJ
14863static void
14864set_breakpoint_cmd (char *args, int from_tty)
14865{
14866}
14867
14868static void
14869show_breakpoint_cmd (char *args, int from_tty)
14870{
14871}
14872
1f3b5d1b
PP
14873/* Invalidate last known value of any hardware watchpoint if
14874 the memory which that value represents has been written to by
14875 GDB itself. */
14876
14877static void
8de0566d
YQ
14878invalidate_bp_value_on_memory_change (struct inferior *inferior,
14879 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14880 const bfd_byte *data)
14881{
14882 struct breakpoint *bp;
14883
14884 ALL_BREAKPOINTS (bp)
14885 if (bp->enable_state == bp_enabled
3a5c3e22 14886 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14887 {
3a5c3e22 14888 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14889
3a5c3e22
PA
14890 if (wp->val_valid && wp->val)
14891 {
14892 struct bp_location *loc;
14893
14894 for (loc = bp->loc; loc != NULL; loc = loc->next)
14895 if (loc->loc_type == bp_loc_hardware_watchpoint
14896 && loc->address + loc->length > addr
14897 && addr + len > loc->address)
14898 {
14899 value_free (wp->val);
14900 wp->val = NULL;
14901 wp->val_valid = 0;
14902 }
14903 }
1f3b5d1b
PP
14904 }
14905}
14906
8181d85f
DJ
14907/* Create and insert a breakpoint for software single step. */
14908
14909void
6c95b8df 14910insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14911 struct address_space *aspace,
14912 CORE_ADDR next_pc)
8181d85f 14913{
7c16b83e
PA
14914 struct thread_info *tp = inferior_thread ();
14915 struct symtab_and_line sal;
14916 CORE_ADDR pc = next_pc;
8181d85f 14917
34b7e8a6
PA
14918 if (tp->control.single_step_breakpoints == NULL)
14919 {
14920 tp->control.single_step_breakpoints
14921 = new_single_step_breakpoint (tp->num, gdbarch);
14922 }
8181d85f 14923
7c16b83e
PA
14924 sal = find_pc_line (pc, 0);
14925 sal.pc = pc;
14926 sal.section = find_pc_overlay (pc);
14927 sal.explicit_pc = 1;
34b7e8a6 14928 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14929
7c16b83e 14930 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14931}
14932
34b7e8a6 14933/* See breakpoint.h. */
f02253f1
HZ
14934
14935int
7c16b83e
PA
14936breakpoint_has_location_inserted_here (struct breakpoint *bp,
14937 struct address_space *aspace,
14938 CORE_ADDR pc)
1aafd4da 14939{
7c16b83e 14940 struct bp_location *loc;
1aafd4da 14941
7c16b83e
PA
14942 for (loc = bp->loc; loc != NULL; loc = loc->next)
14943 if (loc->inserted
14944 && breakpoint_location_address_match (loc, aspace, pc))
14945 return 1;
1aafd4da 14946
7c16b83e 14947 return 0;
ef370185
JB
14948}
14949
14950/* Check whether a software single-step breakpoint is inserted at
14951 PC. */
14952
14953int
14954single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14955 CORE_ADDR pc)
14956{
34b7e8a6
PA
14957 struct breakpoint *bpt;
14958
14959 ALL_BREAKPOINTS (bpt)
14960 {
14961 if (bpt->type == bp_single_step
14962 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14963 return 1;
14964 }
14965 return 0;
1aafd4da
UW
14966}
14967
1042e4c0
SS
14968/* Tracepoint-specific operations. */
14969
14970/* Set tracepoint count to NUM. */
14971static void
14972set_tracepoint_count (int num)
14973{
14974 tracepoint_count = num;
4fa62494 14975 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14976}
14977
70221824 14978static void
1042e4c0
SS
14979trace_command (char *arg, int from_tty)
14980{
55aa24fb 14981 struct breakpoint_ops *ops;
f00aae0f
KS
14982 struct event_location *location;
14983 struct cleanup *back_to;
55aa24fb 14984
f00aae0f
KS
14985 location = string_to_event_location (&arg, current_language);
14986 back_to = make_cleanup_delete_event_location (location);
5b56227b
KS
14987 if (location != NULL
14988 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
14989 ops = &tracepoint_probe_breakpoint_ops;
14990 else
14991 ops = &tracepoint_breakpoint_ops;
14992
558a9d82 14993 create_breakpoint (get_current_arch (),
f00aae0f
KS
14994 location,
14995 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14996 0 /* tempflag */,
14997 bp_tracepoint /* type_wanted */,
14998 0 /* Ignore count */,
14999 pending_break_support,
15000 ops,
15001 from_tty,
15002 1 /* enabled */,
15003 0 /* internal */, 0);
f00aae0f 15004 do_cleanups (back_to);
1042e4c0
SS
15005}
15006
70221824 15007static void
7a697b8d
SS
15008ftrace_command (char *arg, int from_tty)
15009{
f00aae0f
KS
15010 struct event_location *location;
15011 struct cleanup *back_to;
15012
15013 location = string_to_event_location (&arg, current_language);
15014 back_to = make_cleanup_delete_event_location (location);
558a9d82 15015 create_breakpoint (get_current_arch (),
f00aae0f
KS
15016 location,
15017 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15018 0 /* tempflag */,
15019 bp_fast_tracepoint /* type_wanted */,
15020 0 /* Ignore count */,
15021 pending_break_support,
15022 &tracepoint_breakpoint_ops,
15023 from_tty,
15024 1 /* enabled */,
15025 0 /* internal */, 0);
f00aae0f 15026 do_cleanups (back_to);
0fb4aa4b
PA
15027}
15028
15029/* strace command implementation. Creates a static tracepoint. */
15030
70221824 15031static void
0fb4aa4b
PA
15032strace_command (char *arg, int from_tty)
15033{
983af33b 15034 struct breakpoint_ops *ops;
f00aae0f
KS
15035 struct event_location *location;
15036 struct cleanup *back_to;
983af33b
SDJ
15037
15038 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15039 or with a normal static tracepoint. */
61012eef 15040 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
15041 {
15042 ops = &strace_marker_breakpoint_ops;
15043 location = new_linespec_location (&arg);
15044 }
983af33b 15045 else
f00aae0f
KS
15046 {
15047 ops = &tracepoint_breakpoint_ops;
15048 location = string_to_event_location (&arg, current_language);
15049 }
983af33b 15050
f00aae0f 15051 back_to = make_cleanup_delete_event_location (location);
558a9d82 15052 create_breakpoint (get_current_arch (),
f00aae0f
KS
15053 location,
15054 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15055 0 /* tempflag */,
15056 bp_static_tracepoint /* type_wanted */,
15057 0 /* Ignore count */,
15058 pending_break_support,
15059 ops,
15060 from_tty,
15061 1 /* enabled */,
15062 0 /* internal */, 0);
f00aae0f 15063 do_cleanups (back_to);
7a697b8d
SS
15064}
15065
409873ef
SS
15066/* Set up a fake reader function that gets command lines from a linked
15067 list that was acquired during tracepoint uploading. */
15068
15069static struct uploaded_tp *this_utp;
3149d8c1 15070static int next_cmd;
409873ef
SS
15071
15072static char *
15073read_uploaded_action (void)
15074{
15075 char *rslt;
15076
3149d8c1 15077 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15078
3149d8c1 15079 next_cmd++;
409873ef
SS
15080
15081 return rslt;
15082}
15083
00bf0b85
SS
15084/* Given information about a tracepoint as recorded on a target (which
15085 can be either a live system or a trace file), attempt to create an
15086 equivalent GDB tracepoint. This is not a reliable process, since
15087 the target does not necessarily have all the information used when
15088 the tracepoint was originally defined. */
15089
d9b3f62e 15090struct tracepoint *
00bf0b85 15091create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15092{
409873ef 15093 char *addr_str, small_buf[100];
d9b3f62e 15094 struct tracepoint *tp;
f00aae0f
KS
15095 struct event_location *location;
15096 struct cleanup *cleanup;
fd9b8c24 15097
409873ef
SS
15098 if (utp->at_string)
15099 addr_str = utp->at_string;
15100 else
15101 {
15102 /* In the absence of a source location, fall back to raw
15103 address. Since there is no way to confirm that the address
15104 means the same thing as when the trace was started, warn the
15105 user. */
3e43a32a
MS
15106 warning (_("Uploaded tracepoint %d has no "
15107 "source location, using raw address"),
409873ef 15108 utp->number);
8c042590 15109 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15110 addr_str = small_buf;
15111 }
15112
15113 /* There's not much we can do with a sequence of bytecodes. */
15114 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15115 warning (_("Uploaded tracepoint %d condition "
15116 "has no source form, ignoring it"),
409873ef 15117 utp->number);
d5551862 15118
f00aae0f
KS
15119 location = string_to_event_location (&addr_str, current_language);
15120 cleanup = make_cleanup_delete_event_location (location);
8cdf0e15 15121 if (!create_breakpoint (get_current_arch (),
f00aae0f
KS
15122 location,
15123 utp->cond_string, -1, addr_str,
e7e0cddf 15124 0 /* parse cond/thread */,
8cdf0e15 15125 0 /* tempflag */,
0fb4aa4b 15126 utp->type /* type_wanted */,
8cdf0e15
VP
15127 0 /* Ignore count */,
15128 pending_break_support,
348d480f 15129 &tracepoint_breakpoint_ops,
8cdf0e15 15130 0 /* from_tty */,
84f4c1fe 15131 utp->enabled /* enabled */,
44f238bb
PA
15132 0 /* internal */,
15133 CREATE_BREAKPOINT_FLAGS_INSERTED))
f00aae0f
KS
15134 {
15135 do_cleanups (cleanup);
15136 return NULL;
15137 }
15138
15139 do_cleanups (cleanup);
fd9b8c24 15140
409873ef 15141 /* Get the tracepoint we just created. */
fd9b8c24
PA
15142 tp = get_tracepoint (tracepoint_count);
15143 gdb_assert (tp != NULL);
d5551862 15144
00bf0b85
SS
15145 if (utp->pass > 0)
15146 {
8c042590
PM
15147 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15148 tp->base.number);
00bf0b85 15149
409873ef 15150 trace_pass_command (small_buf, 0);
00bf0b85
SS
15151 }
15152
409873ef
SS
15153 /* If we have uploaded versions of the original commands, set up a
15154 special-purpose "reader" function and call the usual command line
15155 reader, then pass the result to the breakpoint command-setting
15156 function. */
3149d8c1 15157 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15158 {
409873ef 15159 struct command_line *cmd_list;
00bf0b85 15160
409873ef 15161 this_utp = utp;
3149d8c1 15162 next_cmd = 0;
d5551862 15163
409873ef
SS
15164 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15165
d9b3f62e 15166 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15167 }
3149d8c1
SS
15168 else if (!VEC_empty (char_ptr, utp->actions)
15169 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15170 warning (_("Uploaded tracepoint %d actions "
15171 "have no source form, ignoring them"),
409873ef 15172 utp->number);
00bf0b85 15173
f196051f
SS
15174 /* Copy any status information that might be available. */
15175 tp->base.hit_count = utp->hit_count;
15176 tp->traceframe_usage = utp->traceframe_usage;
15177
00bf0b85 15178 return tp;
d9b3f62e 15179}
00bf0b85 15180
1042e4c0
SS
15181/* Print information on tracepoint number TPNUM_EXP, or all if
15182 omitted. */
15183
15184static void
e5a67952 15185tracepoints_info (char *args, int from_tty)
1042e4c0 15186{
79a45e25 15187 struct ui_out *uiout = current_uiout;
e5a67952 15188 int num_printed;
1042e4c0 15189
e5a67952 15190 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15191
15192 if (num_printed == 0)
1042e4c0 15193 {
e5a67952 15194 if (args == NULL || *args == '\0')
d77f58be
SS
15195 ui_out_message (uiout, 0, "No tracepoints.\n");
15196 else
e5a67952 15197 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15198 }
ad443146
SS
15199
15200 default_collect_info ();
1042e4c0
SS
15201}
15202
4a64f543 15203/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15204 Not supported by all targets. */
15205static void
15206enable_trace_command (char *args, int from_tty)
15207{
15208 enable_command (args, from_tty);
15209}
15210
4a64f543 15211/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15212 Not supported by all targets. */
15213static void
15214disable_trace_command (char *args, int from_tty)
15215{
15216 disable_command (args, from_tty);
15217}
15218
4a64f543 15219/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15220static void
15221delete_trace_command (char *arg, int from_tty)
15222{
35df4500 15223 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15224
15225 dont_repeat ();
15226
15227 if (arg == 0)
15228 {
15229 int breaks_to_delete = 0;
15230
15231 /* Delete all breakpoints if no argument.
15232 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15233 have to be deleted with an explicit breakpoint number
15234 argument. */
1042e4c0 15235 ALL_TRACEPOINTS (b)
46c6471b 15236 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15237 {
15238 breaks_to_delete = 1;
15239 break;
15240 }
1042e4c0
SS
15241
15242 /* Ask user only if there are some breakpoints to delete. */
15243 if (!from_tty
15244 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15245 {
35df4500 15246 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15247 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15248 delete_breakpoint (b);
1042e4c0
SS
15249 }
15250 }
15251 else
51be5b68 15252 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15253}
15254
197f0a60
TT
15255/* Helper function for trace_pass_command. */
15256
15257static void
d9b3f62e 15258trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15259{
d9b3f62e 15260 tp->pass_count = count;
6f6484cd 15261 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15262 if (from_tty)
15263 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15264 tp->base.number, count);
197f0a60
TT
15265}
15266
1042e4c0
SS
15267/* Set passcount for tracepoint.
15268
15269 First command argument is passcount, second is tracepoint number.
15270 If tracepoint number omitted, apply to most recently defined.
15271 Also accepts special argument "all". */
15272
15273static void
15274trace_pass_command (char *args, int from_tty)
15275{
d9b3f62e 15276 struct tracepoint *t1;
1042e4c0 15277 unsigned int count;
1042e4c0
SS
15278
15279 if (args == 0 || *args == 0)
3e43a32a
MS
15280 error (_("passcount command requires an "
15281 "argument (count + optional TP num)"));
1042e4c0 15282
4a64f543 15283 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15284
529480d0 15285 args = skip_spaces (args);
1042e4c0
SS
15286 if (*args && strncasecmp (args, "all", 3) == 0)
15287 {
d9b3f62e
PA
15288 struct breakpoint *b;
15289
1042e4c0 15290 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15291 if (*args)
15292 error (_("Junk at end of arguments."));
1042e4c0 15293
d9b3f62e 15294 ALL_TRACEPOINTS (b)
197f0a60 15295 {
d9b3f62e 15296 t1 = (struct tracepoint *) b;
197f0a60
TT
15297 trace_pass_set_count (t1, count, from_tty);
15298 }
15299 }
15300 else if (*args == '\0')
1042e4c0 15301 {
5fa1d40e 15302 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15303 if (t1)
197f0a60
TT
15304 trace_pass_set_count (t1, count, from_tty);
15305 }
15306 else
15307 {
15308 struct get_number_or_range_state state;
15309
15310 init_number_or_range (&state, args);
15311 while (!state.finished)
1042e4c0 15312 {
5fa1d40e 15313 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15314 if (t1)
15315 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15316 }
15317 }
1042e4c0
SS
15318}
15319
d9b3f62e 15320struct tracepoint *
1042e4c0
SS
15321get_tracepoint (int num)
15322{
15323 struct breakpoint *t;
15324
15325 ALL_TRACEPOINTS (t)
15326 if (t->number == num)
d9b3f62e 15327 return (struct tracepoint *) t;
1042e4c0
SS
15328
15329 return NULL;
15330}
15331
d5551862
SS
15332/* Find the tracepoint with the given target-side number (which may be
15333 different from the tracepoint number after disconnecting and
15334 reconnecting). */
15335
d9b3f62e 15336struct tracepoint *
d5551862
SS
15337get_tracepoint_by_number_on_target (int num)
15338{
d9b3f62e 15339 struct breakpoint *b;
d5551862 15340
d9b3f62e
PA
15341 ALL_TRACEPOINTS (b)
15342 {
15343 struct tracepoint *t = (struct tracepoint *) b;
15344
15345 if (t->number_on_target == num)
15346 return t;
15347 }
d5551862
SS
15348
15349 return NULL;
15350}
15351
1042e4c0 15352/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15353 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15354 If the argument is missing, the most recent tracepoint
15355 (tracepoint_count) is returned. */
15356
d9b3f62e 15357struct tracepoint *
197f0a60 15358get_tracepoint_by_number (char **arg,
5fa1d40e 15359 struct get_number_or_range_state *state)
1042e4c0 15360{
1042e4c0
SS
15361 struct breakpoint *t;
15362 int tpnum;
15363 char *instring = arg == NULL ? NULL : *arg;
15364
197f0a60
TT
15365 if (state)
15366 {
15367 gdb_assert (!state->finished);
15368 tpnum = get_number_or_range (state);
15369 }
15370 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15371 tpnum = tracepoint_count;
1042e4c0 15372 else
197f0a60 15373 tpnum = get_number (arg);
1042e4c0
SS
15374
15375 if (tpnum <= 0)
15376 {
15377 if (instring && *instring)
15378 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15379 instring);
15380 else
5fa1d40e 15381 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15382 return NULL;
15383 }
15384
15385 ALL_TRACEPOINTS (t)
15386 if (t->number == tpnum)
15387 {
d9b3f62e 15388 return (struct tracepoint *) t;
1042e4c0
SS
15389 }
15390
1042e4c0
SS
15391 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15392 return NULL;
15393}
15394
d9b3f62e
PA
15395void
15396print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15397{
15398 if (b->thread != -1)
15399 fprintf_unfiltered (fp, " thread %d", b->thread);
15400
15401 if (b->task != 0)
15402 fprintf_unfiltered (fp, " task %d", b->task);
15403
15404 fprintf_unfiltered (fp, "\n");
15405}
15406
6149aea9
PA
15407/* Save information on user settable breakpoints (watchpoints, etc) to
15408 a new script file named FILENAME. If FILTER is non-NULL, call it
15409 on each breakpoint and only include the ones for which it returns
15410 non-zero. */
15411
1042e4c0 15412static void
6149aea9
PA
15413save_breakpoints (char *filename, int from_tty,
15414 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15415{
15416 struct breakpoint *tp;
6149aea9 15417 int any = 0;
1042e4c0 15418 struct cleanup *cleanup;
a7bdde9e 15419 struct ui_file *fp;
6149aea9 15420 int extra_trace_bits = 0;
1042e4c0 15421
6149aea9
PA
15422 if (filename == 0 || *filename == 0)
15423 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15424
15425 /* See if we have anything to save. */
6149aea9 15426 ALL_BREAKPOINTS (tp)
1042e4c0 15427 {
6149aea9 15428 /* Skip internal and momentary breakpoints. */
09d682a4 15429 if (!user_breakpoint_p (tp))
6149aea9
PA
15430 continue;
15431
15432 /* If we have a filter, only save the breakpoints it accepts. */
15433 if (filter && !filter (tp))
15434 continue;
15435
15436 any = 1;
15437
15438 if (is_tracepoint (tp))
15439 {
15440 extra_trace_bits = 1;
15441
15442 /* We can stop searching. */
15443 break;
15444 }
1042e4c0 15445 }
6149aea9
PA
15446
15447 if (!any)
1042e4c0 15448 {
6149aea9 15449 warning (_("Nothing to save."));
1042e4c0
SS
15450 return;
15451 }
15452
c718be47
PA
15453 filename = tilde_expand (filename);
15454 cleanup = make_cleanup (xfree, filename);
15455 fp = gdb_fopen (filename, "w");
059fb39f 15456 if (!fp)
6149aea9
PA
15457 error (_("Unable to open file '%s' for saving (%s)"),
15458 filename, safe_strerror (errno));
a7bdde9e 15459 make_cleanup_ui_file_delete (fp);
8bf6485c 15460
6149aea9
PA
15461 if (extra_trace_bits)
15462 save_trace_state_variables (fp);
8bf6485c 15463
6149aea9 15464 ALL_BREAKPOINTS (tp)
1042e4c0 15465 {
6149aea9 15466 /* Skip internal and momentary breakpoints. */
09d682a4 15467 if (!user_breakpoint_p (tp))
6149aea9 15468 continue;
8bf6485c 15469
6149aea9
PA
15470 /* If we have a filter, only save the breakpoints it accepts. */
15471 if (filter && !filter (tp))
15472 continue;
15473
348d480f 15474 tp->ops->print_recreate (tp, fp);
1042e4c0 15475
6149aea9
PA
15476 /* Note, we can't rely on tp->number for anything, as we can't
15477 assume the recreated breakpoint numbers will match. Use $bpnum
15478 instead. */
15479
15480 if (tp->cond_string)
15481 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15482
15483 if (tp->ignore_count)
15484 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15485
2d9442cc 15486 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15487 {
6c63c96a 15488 struct gdb_exception exception;
a7bdde9e 15489
6149aea9 15490 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15491
79a45e25 15492 ui_out_redirect (current_uiout, fp);
492d29ea 15493 TRY
1042e4c0 15494 {
79a45e25 15495 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15496 }
492d29ea
PA
15497 CATCH (ex, RETURN_MASK_ALL)
15498 {
6c63c96a 15499 ui_out_redirect (current_uiout, NULL);
492d29ea
PA
15500 throw_exception (ex);
15501 }
15502 END_CATCH
1042e4c0 15503
6c63c96a 15504 ui_out_redirect (current_uiout, NULL);
a7bdde9e 15505 fprintf_unfiltered (fp, " end\n");
1042e4c0 15506 }
6149aea9
PA
15507
15508 if (tp->enable_state == bp_disabled)
99894e11 15509 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15510
15511 /* If this is a multi-location breakpoint, check if the locations
15512 should be individually disabled. Watchpoint locations are
15513 special, and not user visible. */
15514 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15515 {
15516 struct bp_location *loc;
15517 int n = 1;
15518
15519 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15520 if (!loc->enabled)
15521 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15522 }
1042e4c0 15523 }
8bf6485c 15524
6149aea9 15525 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15526 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15527
1042e4c0 15528 if (from_tty)
6149aea9 15529 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15530 do_cleanups (cleanup);
6149aea9
PA
15531}
15532
15533/* The `save breakpoints' command. */
15534
15535static void
15536save_breakpoints_command (char *args, int from_tty)
15537{
15538 save_breakpoints (args, from_tty, NULL);
15539}
15540
15541/* The `save tracepoints' command. */
15542
15543static void
15544save_tracepoints_command (char *args, int from_tty)
15545{
15546 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15547}
15548
15549/* Create a vector of all tracepoints. */
15550
15551VEC(breakpoint_p) *
eeae04df 15552all_tracepoints (void)
1042e4c0
SS
15553{
15554 VEC(breakpoint_p) *tp_vec = 0;
15555 struct breakpoint *tp;
15556
15557 ALL_TRACEPOINTS (tp)
15558 {
15559 VEC_safe_push (breakpoint_p, tp_vec, tp);
15560 }
15561
15562 return tp_vec;
15563}
15564
c906108c 15565\f
4a64f543
MS
15566/* This help string is used for the break, hbreak, tbreak and thbreak
15567 commands. It is defined as a macro to prevent duplication.
15568 COMMAND should be a string constant containing the name of the
15569 command. */
31e2b00f 15570#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15571command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15572PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15573probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15574guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15575`-probe-dtrace' (for a DTrace probe).\n\
31e2b00f
AS
15576LOCATION may be a line number, function name, or \"*\" and an address.\n\
15577If a line number is specified, break at start of code for that line.\n\
15578If a function is specified, break at start of code for that function.\n\
15579If an address is specified, break at that exact address.\n\
dc10affe
PA
15580With no LOCATION, uses current execution address of the selected\n\
15581stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15582\n\
15583THREADNUM is the number from \"info threads\".\n\
15584CONDITION is a boolean expression.\n\
15585\n\
d41c0fc8
PA
15586Multiple breakpoints at one place are permitted, and useful if their\n\
15587conditions are different.\n\
31e2b00f
AS
15588\n\
15589Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15590
44feb3ce
TT
15591/* List of subcommands for "catch". */
15592static struct cmd_list_element *catch_cmdlist;
15593
15594/* List of subcommands for "tcatch". */
15595static struct cmd_list_element *tcatch_cmdlist;
15596
9ac4176b 15597void
44feb3ce 15598add_catch_command (char *name, char *docstring,
82ae6c8d 15599 cmd_sfunc_ftype *sfunc,
625e8578 15600 completer_ftype *completer,
44feb3ce
TT
15601 void *user_data_catch,
15602 void *user_data_tcatch)
15603{
15604 struct cmd_list_element *command;
15605
15606 command = add_cmd (name, class_breakpoint, NULL, docstring,
15607 &catch_cmdlist);
15608 set_cmd_sfunc (command, sfunc);
15609 set_cmd_context (command, user_data_catch);
a96d9b2e 15610 set_cmd_completer (command, completer);
44feb3ce
TT
15611
15612 command = add_cmd (name, class_breakpoint, NULL, docstring,
15613 &tcatch_cmdlist);
15614 set_cmd_sfunc (command, sfunc);
15615 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15616 set_cmd_completer (command, completer);
44feb3ce
TT
15617}
15618
6149aea9
PA
15619static void
15620save_command (char *arg, int from_tty)
15621{
3e43a32a
MS
15622 printf_unfiltered (_("\"save\" must be followed by "
15623 "the name of a save subcommand.\n"));
635c7e8a 15624 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15625}
15626
84f4c1fe
PM
15627struct breakpoint *
15628iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15629 void *data)
15630{
35df4500 15631 struct breakpoint *b, *b_tmp;
84f4c1fe 15632
35df4500 15633 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15634 {
15635 if ((*callback) (b, data))
15636 return b;
15637 }
15638
15639 return NULL;
15640}
15641
0574c78f
GB
15642/* Zero if any of the breakpoint's locations could be a location where
15643 functions have been inlined, nonzero otherwise. */
15644
15645static int
15646is_non_inline_function (struct breakpoint *b)
15647{
15648 /* The shared library event breakpoint is set on the address of a
15649 non-inline function. */
15650 if (b->type == bp_shlib_event)
15651 return 1;
15652
15653 return 0;
15654}
15655
15656/* Nonzero if the specified PC cannot be a location where functions
15657 have been inlined. */
15658
15659int
09ac7c10
TT
15660pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15661 const struct target_waitstatus *ws)
0574c78f
GB
15662{
15663 struct breakpoint *b;
15664 struct bp_location *bl;
15665
15666 ALL_BREAKPOINTS (b)
15667 {
15668 if (!is_non_inline_function (b))
15669 continue;
15670
15671 for (bl = b->loc; bl != NULL; bl = bl->next)
15672 {
15673 if (!bl->shlib_disabled
09ac7c10 15674 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15675 return 1;
15676 }
15677 }
15678
15679 return 0;
15680}
15681
2f202fde
JK
15682/* Remove any references to OBJFILE which is going to be freed. */
15683
15684void
15685breakpoint_free_objfile (struct objfile *objfile)
15686{
15687 struct bp_location **locp, *loc;
15688
15689 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15690 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15691 loc->symtab = NULL;
15692}
15693
2060206e
PA
15694void
15695initialize_breakpoint_ops (void)
15696{
15697 static int initialized = 0;
15698
15699 struct breakpoint_ops *ops;
15700
15701 if (initialized)
15702 return;
15703 initialized = 1;
15704
15705 /* The breakpoint_ops structure to be inherit by all kinds of
15706 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15707 internal and momentary breakpoints, etc.). */
15708 ops = &bkpt_base_breakpoint_ops;
15709 *ops = base_breakpoint_ops;
15710 ops->re_set = bkpt_re_set;
15711 ops->insert_location = bkpt_insert_location;
15712 ops->remove_location = bkpt_remove_location;
15713 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15714 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15715 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15716 ops->decode_location = bkpt_decode_location;
2060206e
PA
15717
15718 /* The breakpoint_ops structure to be used in regular breakpoints. */
15719 ops = &bkpt_breakpoint_ops;
15720 *ops = bkpt_base_breakpoint_ops;
15721 ops->re_set = bkpt_re_set;
15722 ops->resources_needed = bkpt_resources_needed;
15723 ops->print_it = bkpt_print_it;
15724 ops->print_mention = bkpt_print_mention;
15725 ops->print_recreate = bkpt_print_recreate;
15726
15727 /* Ranged breakpoints. */
15728 ops = &ranged_breakpoint_ops;
15729 *ops = bkpt_breakpoint_ops;
15730 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15731 ops->resources_needed = resources_needed_ranged_breakpoint;
15732 ops->print_it = print_it_ranged_breakpoint;
15733 ops->print_one = print_one_ranged_breakpoint;
15734 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15735 ops->print_mention = print_mention_ranged_breakpoint;
15736 ops->print_recreate = print_recreate_ranged_breakpoint;
15737
15738 /* Internal breakpoints. */
15739 ops = &internal_breakpoint_ops;
15740 *ops = bkpt_base_breakpoint_ops;
15741 ops->re_set = internal_bkpt_re_set;
15742 ops->check_status = internal_bkpt_check_status;
15743 ops->print_it = internal_bkpt_print_it;
15744 ops->print_mention = internal_bkpt_print_mention;
15745
15746 /* Momentary breakpoints. */
15747 ops = &momentary_breakpoint_ops;
15748 *ops = bkpt_base_breakpoint_ops;
15749 ops->re_set = momentary_bkpt_re_set;
15750 ops->check_status = momentary_bkpt_check_status;
15751 ops->print_it = momentary_bkpt_print_it;
15752 ops->print_mention = momentary_bkpt_print_mention;
15753
e2e4d78b
JK
15754 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15755 ops = &longjmp_breakpoint_ops;
15756 *ops = momentary_breakpoint_ops;
15757 ops->dtor = longjmp_bkpt_dtor;
15758
55aa24fb
SDJ
15759 /* Probe breakpoints. */
15760 ops = &bkpt_probe_breakpoint_ops;
15761 *ops = bkpt_breakpoint_ops;
15762 ops->insert_location = bkpt_probe_insert_location;
15763 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15764 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15765 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15766
2060206e
PA
15767 /* Watchpoints. */
15768 ops = &watchpoint_breakpoint_ops;
15769 *ops = base_breakpoint_ops;
3a5c3e22 15770 ops->dtor = dtor_watchpoint;
2060206e
PA
15771 ops->re_set = re_set_watchpoint;
15772 ops->insert_location = insert_watchpoint;
15773 ops->remove_location = remove_watchpoint;
15774 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15775 ops->check_status = check_status_watchpoint;
15776 ops->resources_needed = resources_needed_watchpoint;
15777 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15778 ops->print_it = print_it_watchpoint;
15779 ops->print_mention = print_mention_watchpoint;
15780 ops->print_recreate = print_recreate_watchpoint;
427cd150 15781 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15782
15783 /* Masked watchpoints. */
15784 ops = &masked_watchpoint_breakpoint_ops;
15785 *ops = watchpoint_breakpoint_ops;
15786 ops->insert_location = insert_masked_watchpoint;
15787 ops->remove_location = remove_masked_watchpoint;
15788 ops->resources_needed = resources_needed_masked_watchpoint;
15789 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15790 ops->print_it = print_it_masked_watchpoint;
15791 ops->print_one_detail = print_one_detail_masked_watchpoint;
15792 ops->print_mention = print_mention_masked_watchpoint;
15793 ops->print_recreate = print_recreate_masked_watchpoint;
15794
15795 /* Tracepoints. */
15796 ops = &tracepoint_breakpoint_ops;
15797 *ops = base_breakpoint_ops;
15798 ops->re_set = tracepoint_re_set;
15799 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15800 ops->print_one_detail = tracepoint_print_one_detail;
15801 ops->print_mention = tracepoint_print_mention;
15802 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15803 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15804 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15805 ops->decode_location = tracepoint_decode_location;
983af33b 15806
55aa24fb
SDJ
15807 /* Probe tracepoints. */
15808 ops = &tracepoint_probe_breakpoint_ops;
15809 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15810 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15811 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15812
983af33b
SDJ
15813 /* Static tracepoints with marker (`-m'). */
15814 ops = &strace_marker_breakpoint_ops;
15815 *ops = tracepoint_breakpoint_ops;
5f700d83 15816 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15817 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15818 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15819
15820 /* Fork catchpoints. */
15821 ops = &catch_fork_breakpoint_ops;
15822 *ops = base_breakpoint_ops;
15823 ops->insert_location = insert_catch_fork;
15824 ops->remove_location = remove_catch_fork;
15825 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15826 ops->print_it = print_it_catch_fork;
15827 ops->print_one = print_one_catch_fork;
15828 ops->print_mention = print_mention_catch_fork;
15829 ops->print_recreate = print_recreate_catch_fork;
15830
15831 /* Vfork catchpoints. */
15832 ops = &catch_vfork_breakpoint_ops;
15833 *ops = base_breakpoint_ops;
15834 ops->insert_location = insert_catch_vfork;
15835 ops->remove_location = remove_catch_vfork;
15836 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15837 ops->print_it = print_it_catch_vfork;
15838 ops->print_one = print_one_catch_vfork;
15839 ops->print_mention = print_mention_catch_vfork;
15840 ops->print_recreate = print_recreate_catch_vfork;
15841
15842 /* Exec catchpoints. */
15843 ops = &catch_exec_breakpoint_ops;
15844 *ops = base_breakpoint_ops;
15845 ops->dtor = dtor_catch_exec;
15846 ops->insert_location = insert_catch_exec;
15847 ops->remove_location = remove_catch_exec;
15848 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15849 ops->print_it = print_it_catch_exec;
15850 ops->print_one = print_one_catch_exec;
15851 ops->print_mention = print_mention_catch_exec;
15852 ops->print_recreate = print_recreate_catch_exec;
15853
edcc5120
TT
15854 /* Solib-related catchpoints. */
15855 ops = &catch_solib_breakpoint_ops;
15856 *ops = base_breakpoint_ops;
15857 ops->dtor = dtor_catch_solib;
15858 ops->insert_location = insert_catch_solib;
15859 ops->remove_location = remove_catch_solib;
15860 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15861 ops->check_status = check_status_catch_solib;
15862 ops->print_it = print_it_catch_solib;
15863 ops->print_one = print_one_catch_solib;
15864 ops->print_mention = print_mention_catch_solib;
15865 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15866
15867 ops = &dprintf_breakpoint_ops;
15868 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15869 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15870 ops->resources_needed = bkpt_resources_needed;
15871 ops->print_it = bkpt_print_it;
15872 ops->print_mention = bkpt_print_mention;
2d9442cc 15873 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15874 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15875 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15876}
15877
8bfd80db
YQ
15878/* Chain containing all defined "enable breakpoint" subcommands. */
15879
15880static struct cmd_list_element *enablebreaklist = NULL;
15881
c906108c 15882void
fba45db2 15883_initialize_breakpoint (void)
c906108c
SS
15884{
15885 struct cmd_list_element *c;
15886
2060206e
PA
15887 initialize_breakpoint_ops ();
15888
84acb35a 15889 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15890 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15891 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15892
55aa24fb
SDJ
15893 breakpoint_objfile_key
15894 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15895
c906108c
SS
15896 breakpoint_chain = 0;
15897 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15898 before a breakpoint is set. */
15899 breakpoint_count = 0;
15900
1042e4c0
SS
15901 tracepoint_count = 0;
15902
1bedd215
AC
15903 add_com ("ignore", class_breakpoint, ignore_command, _("\
15904Set ignore-count of breakpoint number N to COUNT.\n\
15905Usage is `ignore N COUNT'."));
c906108c 15906
1bedd215
AC
15907 add_com ("commands", class_breakpoint, commands_command, _("\
15908Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
15909Give breakpoint number as argument after \"commands\".\n\
15910With no argument, the targeted breakpoint is the last one set.\n\
15911The commands themselves follow starting on the next line.\n\
15912Type a line containing \"end\" to indicate the end of them.\n\
15913Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15914then no output is printed when it is hit, except what the commands print."));
c906108c 15915
d55637df 15916 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15917Specify breakpoint number N to break only if COND is true.\n\
c906108c 15918Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15919expression to be evaluated whenever breakpoint N is reached."));
d55637df 15920 set_cmd_completer (c, condition_completer);
c906108c 15921
1bedd215 15922 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15923Set a temporary breakpoint.\n\
c906108c
SS
15924Like \"break\" except the breakpoint is only temporary,\n\
15925so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15926by using \"enable delete\" on the breakpoint number.\n\
15927\n"
15928BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15929 set_cmd_completer (c, location_completer);
c94fdfd0 15930
1bedd215 15931 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15932Set a hardware assisted breakpoint.\n\
c906108c 15933Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15934some target hardware may not have this support.\n\
15935\n"
15936BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15937 set_cmd_completer (c, location_completer);
c906108c 15938
1bedd215 15939 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15940Set a temporary hardware assisted breakpoint.\n\
c906108c 15941Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15942so it will be deleted when hit.\n\
15943\n"
15944BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15945 set_cmd_completer (c, location_completer);
c906108c 15946
1bedd215
AC
15947 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15948Enable some breakpoints.\n\
c906108c
SS
15949Give breakpoint numbers (separated by spaces) as arguments.\n\
15950With no subcommand, breakpoints are enabled until you command otherwise.\n\
15951This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15952With a subcommand you can enable temporarily."),
c906108c 15953 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15954
15955 add_com_alias ("en", "enable", class_breakpoint, 1);
15956
84951ab5 15957 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15958Enable some breakpoints.\n\
c906108c
SS
15959Give breakpoint numbers (separated by spaces) as arguments.\n\
15960This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15961May be abbreviated to simply \"enable\".\n"),
c5aa993b 15962 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15963
1a966eab
AC
15964 add_cmd ("once", no_class, enable_once_command, _("\
15965Enable breakpoints for one hit. Give breakpoint numbers.\n\
15966If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15967 &enablebreaklist);
15968
1a966eab
AC
15969 add_cmd ("delete", no_class, enable_delete_command, _("\
15970Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15971If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15972 &enablebreaklist);
15973
816338b5
SS
15974 add_cmd ("count", no_class, enable_count_command, _("\
15975Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15976If a breakpoint is hit while enabled in this fashion,\n\
15977the count is decremented; when it reaches zero, the breakpoint is disabled."),
15978 &enablebreaklist);
15979
1a966eab
AC
15980 add_cmd ("delete", no_class, enable_delete_command, _("\
15981Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15982If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15983 &enablelist);
15984
1a966eab
AC
15985 add_cmd ("once", no_class, enable_once_command, _("\
15986Enable breakpoints for one hit. Give breakpoint numbers.\n\
15987If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15988 &enablelist);
15989
15990 add_cmd ("count", no_class, enable_count_command, _("\
15991Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15992If a breakpoint is hit while enabled in this fashion,\n\
15993the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15994 &enablelist);
15995
1bedd215
AC
15996 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15997Disable some breakpoints.\n\
c906108c
SS
15998Arguments are breakpoint numbers with spaces in between.\n\
15999To disable all breakpoints, give no argument.\n\
64b9b334 16000A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16001 &disablelist, "disable ", 1, &cmdlist);
16002 add_com_alias ("dis", "disable", class_breakpoint, 1);
16003 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 16004
1a966eab
AC
16005 add_cmd ("breakpoints", class_alias, disable_command, _("\
16006Disable some breakpoints.\n\
c906108c
SS
16007Arguments are breakpoint numbers with spaces in between.\n\
16008To disable all breakpoints, give no argument.\n\
64b9b334 16009A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16010This command may be abbreviated \"disable\"."),
c906108c
SS
16011 &disablelist);
16012
1bedd215
AC
16013 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16014Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16015Arguments are breakpoint numbers with spaces in between.\n\
16016To delete all breakpoints, give no argument.\n\
16017\n\
16018Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16019The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16020 &deletelist, "delete ", 1, &cmdlist);
16021 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16022 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16023
1a966eab
AC
16024 add_cmd ("breakpoints", class_alias, delete_command, _("\
16025Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16026Arguments are breakpoint numbers with spaces in between.\n\
16027To delete all breakpoints, give no argument.\n\
1a966eab 16028This command may be abbreviated \"delete\"."),
c906108c
SS
16029 &deletelist);
16030
1bedd215
AC
16031 add_com ("clear", class_breakpoint, clear_command, _("\
16032Clear breakpoint at specified line or function.\n\
c906108c
SS
16033Argument may be line number, function name, or \"*\" and an address.\n\
16034If line number is specified, all breakpoints in that line are cleared.\n\
16035If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16036If an address is specified, breakpoints at that address are cleared.\n\
16037\n\
16038With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16039is executing in.\n\
16040\n\
1bedd215 16041See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16042 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16043
1bedd215 16044 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16045Set breakpoint at specified line or function.\n"
16046BREAK_ARGS_HELP ("break")));
5ba2abeb 16047 set_cmd_completer (c, location_completer);
c94fdfd0 16048
c906108c
SS
16049 add_com_alias ("b", "break", class_run, 1);
16050 add_com_alias ("br", "break", class_run, 1);
16051 add_com_alias ("bre", "break", class_run, 1);
16052 add_com_alias ("brea", "break", class_run, 1);
16053
c906108c
SS
16054 if (dbx_commands)
16055 {
1bedd215
AC
16056 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16057Break in function/address or break at a line in the current file."),
c5aa993b
JM
16058 &stoplist, "stop ", 1, &cmdlist);
16059 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16060 _("Break in function or address."), &stoplist);
c5aa993b 16061 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16062 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16063 add_com ("status", class_info, breakpoints_info, _("\
16064Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16065The \"Type\" column indicates one of:\n\
16066\tbreakpoint - normal breakpoint\n\
16067\twatchpoint - watchpoint\n\
16068The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16069the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16070breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16071address and file/line number respectively.\n\
16072\n\
16073Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16074are set to the address of the last breakpoint listed unless the command\n\
16075is prefixed with \"server \".\n\n\
c906108c 16076Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16077breakpoint set."));
c906108c
SS
16078 }
16079
1bedd215 16080 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16081Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16082The \"Type\" column indicates one of:\n\
16083\tbreakpoint - normal breakpoint\n\
16084\twatchpoint - watchpoint\n\
16085The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16086the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16087breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16088address and file/line number respectively.\n\
16089\n\
16090Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16091are set to the address of the last breakpoint listed unless the command\n\
16092is prefixed with \"server \".\n\n\
c906108c 16093Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16094breakpoint set."));
c906108c 16095
6b04bdb7
MS
16096 add_info_alias ("b", "breakpoints", 1);
16097
1a966eab
AC
16098 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16099Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16100The \"Type\" column indicates one of:\n\
16101\tbreakpoint - normal breakpoint\n\
16102\twatchpoint - watchpoint\n\
16103\tlongjmp - internal breakpoint used to step through longjmp()\n\
16104\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16105\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16106\tfinish - internal breakpoint used by the \"finish\" command\n\
16107The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16108the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16109breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16110address and file/line number respectively.\n\
16111\n\
16112Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16113are set to the address of the last breakpoint listed unless the command\n\
16114is prefixed with \"server \".\n\n\
c906108c 16115Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16116breakpoint set."),
c906108c
SS
16117 &maintenanceinfolist);
16118
44feb3ce
TT
16119 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16120Set catchpoints to catch events."),
16121 &catch_cmdlist, "catch ",
16122 0/*allow-unknown*/, &cmdlist);
16123
16124 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16125Set temporary catchpoints to catch events."),
16126 &tcatch_cmdlist, "tcatch ",
16127 0/*allow-unknown*/, &cmdlist);
16128
44feb3ce
TT
16129 add_catch_command ("fork", _("Catch calls to fork."),
16130 catch_fork_command_1,
a96d9b2e 16131 NULL,
44feb3ce
TT
16132 (void *) (uintptr_t) catch_fork_permanent,
16133 (void *) (uintptr_t) catch_fork_temporary);
16134 add_catch_command ("vfork", _("Catch calls to vfork."),
16135 catch_fork_command_1,
a96d9b2e 16136 NULL,
44feb3ce
TT
16137 (void *) (uintptr_t) catch_vfork_permanent,
16138 (void *) (uintptr_t) catch_vfork_temporary);
16139 add_catch_command ("exec", _("Catch calls to exec."),
16140 catch_exec_command_1,
a96d9b2e
SDJ
16141 NULL,
16142 CATCH_PERMANENT,
16143 CATCH_TEMPORARY);
edcc5120
TT
16144 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16145Usage: catch load [REGEX]\n\
16146If REGEX is given, only stop for libraries matching the regular expression."),
16147 catch_load_command_1,
16148 NULL,
16149 CATCH_PERMANENT,
16150 CATCH_TEMPORARY);
16151 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16152Usage: catch unload [REGEX]\n\
16153If REGEX is given, only stop for libraries matching the regular expression."),
16154 catch_unload_command_1,
16155 NULL,
16156 CATCH_PERMANENT,
16157 CATCH_TEMPORARY);
c5aa993b 16158
1bedd215
AC
16159 c = add_com ("watch", class_breakpoint, watch_command, _("\
16160Set a watchpoint for an expression.\n\
06a64a0b 16161Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16162A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16163an expression changes.\n\
16164If -l or -location is given, this evaluates EXPRESSION and watches\n\
16165the memory to which it refers."));
65d12d83 16166 set_cmd_completer (c, expression_completer);
c906108c 16167
1bedd215
AC
16168 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16169Set a read watchpoint for an expression.\n\
06a64a0b 16170Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16171A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16172an expression is read.\n\
16173If -l or -location is given, this evaluates EXPRESSION and watches\n\
16174the memory to which it refers."));
65d12d83 16175 set_cmd_completer (c, expression_completer);
c906108c 16176
1bedd215
AC
16177 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16178Set a watchpoint for an expression.\n\
06a64a0b 16179Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16180A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16181an expression is either read or written.\n\
16182If -l or -location is given, this evaluates EXPRESSION and watches\n\
16183the memory to which it refers."));
65d12d83 16184 set_cmd_completer (c, expression_completer);
c906108c 16185
d77f58be 16186 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16187Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16188
920d2a44
AC
16189 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16190 respond to changes - contrary to the description. */
85c07804
AC
16191 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16192 &can_use_hw_watchpoints, _("\
16193Set debugger's willingness to use watchpoint hardware."), _("\
16194Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16195If zero, gdb will not use hardware for new watchpoints, even if\n\
16196such is available. (However, any hardware watchpoints that were\n\
16197created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16198hardware.)"),
16199 NULL,
920d2a44 16200 show_can_use_hw_watchpoints,
85c07804 16201 &setlist, &showlist);
c906108c
SS
16202
16203 can_use_hw_watchpoints = 1;
fa8d40ab 16204
1042e4c0
SS
16205 /* Tracepoint manipulation commands. */
16206
16207 c = add_com ("trace", class_breakpoint, trace_command, _("\
16208Set a tracepoint at specified line or function.\n\
16209\n"
16210BREAK_ARGS_HELP ("trace") "\n\
16211Do \"help tracepoints\" for info on other tracepoint commands."));
16212 set_cmd_completer (c, location_completer);
16213
16214 add_com_alias ("tp", "trace", class_alias, 0);
16215 add_com_alias ("tr", "trace", class_alias, 1);
16216 add_com_alias ("tra", "trace", class_alias, 1);
16217 add_com_alias ("trac", "trace", class_alias, 1);
16218
7a697b8d
SS
16219 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16220Set a fast tracepoint at specified line or function.\n\
16221\n"
16222BREAK_ARGS_HELP ("ftrace") "\n\
16223Do \"help tracepoints\" for info on other tracepoint commands."));
16224 set_cmd_completer (c, location_completer);
16225
0fb4aa4b
PA
16226 c = add_com ("strace", class_breakpoint, strace_command, _("\
16227Set a static tracepoint at specified line, function or marker.\n\
16228\n\
16229strace [LOCATION] [if CONDITION]\n\
16230LOCATION may be a line number, function name, \"*\" and an address,\n\
16231or -m MARKER_ID.\n\
16232If a line number is specified, probe the marker at start of code\n\
16233for that line. If a function is specified, probe the marker at start\n\
16234of code for that function. If an address is specified, probe the marker\n\
16235at that exact address. If a marker id is specified, probe the marker\n\
16236with that name. With no LOCATION, uses current execution address of\n\
16237the selected stack frame.\n\
16238Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16239This collects arbitrary user data passed in the probe point call to the\n\
16240tracing library. You can inspect it when analyzing the trace buffer,\n\
16241by printing the $_sdata variable like any other convenience variable.\n\
16242\n\
16243CONDITION is a boolean expression.\n\
16244\n\
d41c0fc8
PA
16245Multiple tracepoints at one place are permitted, and useful if their\n\
16246conditions are different.\n\
0fb4aa4b
PA
16247\n\
16248Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16249Do \"help tracepoints\" for info on other tracepoint commands."));
16250 set_cmd_completer (c, location_completer);
16251
1042e4c0 16252 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16253Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16254Convenience variable \"$tpnum\" contains the number of the\n\
16255last tracepoint set."));
16256
16257 add_info_alias ("tp", "tracepoints", 1);
16258
16259 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16260Delete specified tracepoints.\n\
16261Arguments are tracepoint numbers, separated by spaces.\n\
16262No argument means delete all tracepoints."),
16263 &deletelist);
7e20dfcd 16264 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16265
16266 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16267Disable specified tracepoints.\n\
16268Arguments are tracepoint numbers, separated by spaces.\n\
16269No argument means disable all tracepoints."),
16270 &disablelist);
16271 deprecate_cmd (c, "disable");
16272
16273 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16274Enable specified tracepoints.\n\
16275Arguments are tracepoint numbers, separated by spaces.\n\
16276No argument means enable all tracepoints."),
16277 &enablelist);
16278 deprecate_cmd (c, "enable");
16279
16280 add_com ("passcount", class_trace, trace_pass_command, _("\
16281Set the passcount for a tracepoint.\n\
16282The trace will end when the tracepoint has been passed 'count' times.\n\
16283Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16284if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16285
6149aea9
PA
16286 add_prefix_cmd ("save", class_breakpoint, save_command,
16287 _("Save breakpoint definitions as a script."),
16288 &save_cmdlist, "save ",
16289 0/*allow-unknown*/, &cmdlist);
16290
16291 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16292Save current breakpoint definitions as a script.\n\
cce7e648 16293This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16294catchpoints, tracepoints). Use the 'source' command in another debug\n\
16295session to restore them."),
16296 &save_cmdlist);
16297 set_cmd_completer (c, filename_completer);
16298
16299 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16300Save current tracepoint definitions as a script.\n\
6149aea9
PA
16301Use the 'source' command in another debug session to restore them."),
16302 &save_cmdlist);
1042e4c0
SS
16303 set_cmd_completer (c, filename_completer);
16304
6149aea9
PA
16305 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16306 deprecate_cmd (c, "save tracepoints");
16307
1bedd215 16308 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16309Breakpoint specific settings\n\
16310Configure various breakpoint-specific variables such as\n\
1bedd215 16311pending breakpoint behavior"),
fa8d40ab
JJ
16312 &breakpoint_set_cmdlist, "set breakpoint ",
16313 0/*allow-unknown*/, &setlist);
1bedd215 16314 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16315Breakpoint specific settings\n\
16316Configure various breakpoint-specific variables such as\n\
1bedd215 16317pending breakpoint behavior"),
fa8d40ab
JJ
16318 &breakpoint_show_cmdlist, "show breakpoint ",
16319 0/*allow-unknown*/, &showlist);
16320
7915a72c
AC
16321 add_setshow_auto_boolean_cmd ("pending", no_class,
16322 &pending_break_support, _("\
16323Set debugger's behavior regarding pending breakpoints."), _("\
16324Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16325If on, an unrecognized breakpoint location will cause gdb to create a\n\
16326pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16327an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16328user-query to see if a pending breakpoint should be created."),
2c5b56ce 16329 NULL,
920d2a44 16330 show_pending_break_support,
6e1d7d6c
AC
16331 &breakpoint_set_cmdlist,
16332 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16333
16334 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16335
16336 add_setshow_boolean_cmd ("auto-hw", no_class,
16337 &automatic_hardware_breakpoints, _("\
16338Set automatic usage of hardware breakpoints."), _("\
16339Show automatic usage of hardware breakpoints."), _("\
16340If set, the debugger will automatically use hardware breakpoints for\n\
16341breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16342a warning will be emitted for such breakpoints."),
16343 NULL,
16344 show_automatic_hardware_breakpoints,
16345 &breakpoint_set_cmdlist,
16346 &breakpoint_show_cmdlist);
74960c60 16347
a25a5a45
PA
16348 add_setshow_boolean_cmd ("always-inserted", class_support,
16349 &always_inserted_mode, _("\
74960c60
VP
16350Set mode for inserting breakpoints."), _("\
16351Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16352When this mode is on, breakpoints are inserted immediately as soon as\n\
16353they're created, kept inserted even when execution stops, and removed\n\
16354only when the user deletes them. When this mode is off (the default),\n\
16355breakpoints are inserted only when execution continues, and removed\n\
16356when execution stops."),
72d0e2c5
YQ
16357 NULL,
16358 &show_always_inserted_mode,
16359 &breakpoint_set_cmdlist,
16360 &breakpoint_show_cmdlist);
f1310107 16361
b775012e
LM
16362 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16363 condition_evaluation_enums,
16364 &condition_evaluation_mode_1, _("\
16365Set mode of breakpoint condition evaluation."), _("\
16366Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16367When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16368evaluated on the host's side by GDB. When it is set to \"target\",\n\
16369breakpoint conditions will be downloaded to the target (if the target\n\
16370supports such feature) and conditions will be evaluated on the target's side.\n\
16371If this is set to \"auto\" (default), this will be automatically set to\n\
16372\"target\" if it supports condition evaluation, otherwise it will\n\
16373be set to \"gdb\""),
16374 &set_condition_evaluation_mode,
16375 &show_condition_evaluation_mode,
16376 &breakpoint_set_cmdlist,
16377 &breakpoint_show_cmdlist);
16378
f1310107
TJB
16379 add_com ("break-range", class_breakpoint, break_range_command, _("\
16380Set a breakpoint for an address range.\n\
16381break-range START-LOCATION, END-LOCATION\n\
16382where START-LOCATION and END-LOCATION can be one of the following:\n\
16383 LINENUM, for that line in the current file,\n\
16384 FILE:LINENUM, for that line in that file,\n\
16385 +OFFSET, for that number of lines after the current line\n\
16386 or the start of the range\n\
16387 FUNCTION, for the first line in that function,\n\
16388 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16389 *ADDRESS, for the instruction at that address.\n\
16390\n\
16391The breakpoint will stop execution of the inferior whenever it executes\n\
16392an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16393range (including START-LOCATION and END-LOCATION)."));
16394
e7e0cddf
SS
16395 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16396Set a dynamic printf at specified line or function.\n\
16397dprintf location,format string,arg1,arg2,...\n\
16398location may be a line number, function name, or \"*\" and an address.\n\
16399If a line number is specified, break at start of code for that line.\n\
0e4777df 16400If a function is specified, break at start of code for that function."));
e7e0cddf
SS
16401 set_cmd_completer (c, location_completer);
16402
16403 add_setshow_enum_cmd ("dprintf-style", class_support,
16404 dprintf_style_enums, &dprintf_style, _("\
16405Set the style of usage for dynamic printf."), _("\
16406Show the style of usage for dynamic printf."), _("\
16407This setting chooses how GDB will do a dynamic printf.\n\
16408If the value is \"gdb\", then the printing is done by GDB to its own\n\
16409console, as with the \"printf\" command.\n\
16410If the value is \"call\", the print is done by calling a function in your\n\
16411program; by default printf(), but you can choose a different function or\n\
16412output stream by setting dprintf-function and dprintf-channel."),
16413 update_dprintf_commands, NULL,
16414 &setlist, &showlist);
16415
16416 dprintf_function = xstrdup ("printf");
16417 add_setshow_string_cmd ("dprintf-function", class_support,
16418 &dprintf_function, _("\
16419Set the function to use for dynamic printf"), _("\
16420Show the function to use for dynamic printf"), NULL,
16421 update_dprintf_commands, NULL,
16422 &setlist, &showlist);
16423
16424 dprintf_channel = xstrdup ("");
16425 add_setshow_string_cmd ("dprintf-channel", class_support,
16426 &dprintf_channel, _("\
16427Set the channel to use for dynamic printf"), _("\
16428Show the channel to use for dynamic printf"), NULL,
16429 update_dprintf_commands, NULL,
16430 &setlist, &showlist);
16431
d3ce09f5
SS
16432 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16433 &disconnected_dprintf, _("\
16434Set whether dprintf continues after GDB disconnects."), _("\
16435Show whether dprintf continues after GDB disconnects."), _("\
16436Use this to let dprintf commands continue to hit and produce output\n\
16437even if GDB disconnects or detaches from the target."),
16438 NULL,
16439 NULL,
16440 &setlist, &showlist);
16441
16442 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16443agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16444(target agent only) This is useful for formatted output in user-defined commands."));
16445
765dc015 16446 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16447
16448 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16449 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16450}
This page took 2.725535 seconds and 4 git commands to generate.