Constify add_com
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
61baf725 3 Copyright (C) 1986-2017 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"
8b93c638 47#include "ui-out.h"
e1507482 48#include "cli/cli-script.h"
fe898f56 49#include "block.h"
a77053c2 50#include "solib.h"
84acb35a
JJ
51#include "solist.h"
52#include "observer.h"
765dc015 53#include "memattr.h"
f7f9143b 54#include "ada-lang.h"
d1aa2f50 55#include "top.h"
79a45b7d 56#include "valprint.h"
4efc6507 57#include "jit.h"
65d79d4b 58#include "parser-defs.h"
55aa24fb
SDJ
59#include "gdb_regex.h"
60#include "probe.h"
e9cafbcc 61#include "cli/cli-utils.h"
be34f849 62#include "continuations.h"
1bfeeb0f
JL
63#include "stack.h"
64#include "skip.h"
b775012e 65#include "ax-gdb.h"
e2e4d78b 66#include "dummy-frame.h"
5589af0e 67#include "interps.h"
d3ce09f5 68#include "format.h"
cfc31633 69#include "thread-fsm.h"
5d5658a1 70#include "tid-parse.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"
325fac50 81#include <algorithm>
5ed8105e 82#include "progspace-and-thread.h"
6c5b2ebe 83#include "common/array-view.h"
76f9c9cf 84#include "common/gdb_optional.h"
104c1213 85
e7e8980f
YQ
86/* Enums for exception-handling support. */
87enum exception_event_kind
88{
89 EX_EVENT_THROW,
591f19e8 90 EX_EVENT_RETHROW,
e7e8980f
YQ
91 EX_EVENT_CATCH
92};
93
4a64f543 94/* Prototypes for local functions. */
c906108c 95
896b6bda 96static void map_breakpoint_numbers (const char *,
48649e1b 97 gdb::function_view<void (breakpoint *)>);
c906108c 98
348d480f
PA
99static void breakpoint_re_set_default (struct breakpoint *);
100
f00aae0f
KS
101static void
102 create_sals_from_location_default (const struct event_location *location,
103 struct linespec_result *canonical,
104 enum bptype type_wanted);
983af33b
SDJ
105
106static void create_breakpoints_sal_default (struct gdbarch *,
107 struct linespec_result *,
e1e01040
PA
108 gdb::unique_xmalloc_ptr<char>,
109 gdb::unique_xmalloc_ptr<char>,
110 enum bptype,
983af33b
SDJ
111 enum bpdisp, int, int,
112 int,
113 const struct breakpoint_ops *,
44f238bb 114 int, int, int, unsigned);
983af33b 115
6c5b2ebe
PA
116static std::vector<symtab_and_line> decode_location_default
117 (struct breakpoint *b, const struct event_location *location,
118 struct program_space *search_pspace);
983af33b 119
a9634178 120static int can_use_hardware_watchpoint (struct value *);
c906108c 121
a14ed312 122static void mention (struct breakpoint *);
c906108c 123
348d480f
PA
124static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
125 enum bptype,
c0a91b2b 126 const struct breakpoint_ops *);
3742cc8b
YQ
127static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
128 const struct symtab_and_line *);
129
4a64f543
MS
130/* This function is used in gdbtk sources and thus can not be made
131 static. */
63c252f8 132struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 133 struct symtab_and_line,
c0a91b2b
TT
134 enum bptype,
135 const struct breakpoint_ops *);
c906108c 136
06edf0c0
PA
137static struct breakpoint *
138 momentary_breakpoint_from_master (struct breakpoint *orig,
139 enum bptype type,
a1aa2221
LM
140 const struct breakpoint_ops *ops,
141 int loc_enabled);
06edf0c0 142
76897487
KB
143static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
144
a6d9a66e
UW
145static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
146 CORE_ADDR bpaddr,
88f7da05 147 enum bptype bptype);
76897487 148
6c95b8df
PA
149static void describe_other_breakpoints (struct gdbarch *,
150 struct program_space *, CORE_ADDR,
5af949e3 151 struct obj_section *, int);
c906108c 152
85d721b8
PA
153static int watchpoint_locations_match (struct bp_location *loc1,
154 struct bp_location *loc2);
155
f1310107 156static int breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 157 const struct address_space *aspace,
f1310107
TJB
158 CORE_ADDR addr);
159
d35ae833 160static int breakpoint_location_address_range_overlap (struct bp_location *,
accd0bcd 161 const address_space *,
d35ae833
PA
162 CORE_ADDR, int);
163
11db9430 164static void info_watchpoints_command (char *, int);
d77f58be 165
834c0d03 166static int remove_breakpoint (struct bp_location *);
b2b6a7da 167static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 168
e514a9d6 169static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 170
a14ed312 171static int hw_breakpoint_used_count (void);
c906108c 172
a1398e0c
PA
173static int hw_watchpoint_use_count (struct breakpoint *);
174
175static int hw_watchpoint_used_count_others (struct breakpoint *except,
176 enum bptype type,
177 int *other_type_used);
c906108c 178
816338b5
SS
179static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
180 int count);
c906108c 181
fe3f5fa8 182static void free_bp_location (struct bp_location *loc);
f431efe5
PA
183static void incref_bp_location (struct bp_location *loc);
184static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 185
39d61571 186static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 187
44702360
PA
188/* update_global_location_list's modes of operation wrt to whether to
189 insert locations now. */
190enum ugll_insert_mode
191{
192 /* Don't insert any breakpoint locations into the inferior, only
193 remove already-inserted locations that no longer should be
194 inserted. Functions that delete a breakpoint or breakpoints
195 should specify this mode, so that deleting a breakpoint doesn't
196 have the side effect of inserting the locations of other
197 breakpoints that are marked not-inserted, but should_be_inserted
198 returns true on them.
199
200 This behavior is useful is situations close to tear-down -- e.g.,
201 after an exec, while the target still has execution, but
202 breakpoint shadows of the previous executable image should *NOT*
203 be restored to the new image; or before detaching, where the
204 target still has execution and wants to delete breakpoints from
205 GDB's lists, and all breakpoints had already been removed from
206 the inferior. */
207 UGLL_DONT_INSERT,
208
a25a5a45
PA
209 /* May insert breakpoints iff breakpoints_should_be_inserted_now
210 claims breakpoints should be inserted now. */
04086b45
PA
211 UGLL_MAY_INSERT,
212
a25a5a45
PA
213 /* Insert locations now, irrespective of
214 breakpoints_should_be_inserted_now. E.g., say all threads are
215 stopped right now, and the user did "continue". We need to
216 insert breakpoints _before_ resuming the target, but
217 UGLL_MAY_INSERT wouldn't insert them, because
218 breakpoints_should_be_inserted_now returns false at that point,
219 as no thread is running yet. */
04086b45 220 UGLL_INSERT
44702360
PA
221};
222
223static void update_global_location_list (enum ugll_insert_mode);
a5606eee 224
44702360 225static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 226
d77f58be 227static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
228
229static void insert_breakpoint_locations (void);
a5606eee 230
11db9430 231static void info_tracepoints_command (char *, int);
1042e4c0 232
1042e4c0
SS
233static void enable_trace_command (char *, int);
234
235static void disable_trace_command (char *, int);
236
0b39b52e 237static void trace_pass_command (const char *, int);
1042e4c0 238
558a9d82
YQ
239static void set_tracepoint_count (int num);
240
9c06b0b4
TJB
241static int is_masked_watchpoint (const struct breakpoint *b);
242
b775012e
LM
243static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
244
983af33b
SDJ
245/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
246 otherwise. */
247
248static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 249
2060206e
PA
250/* The breakpoint_ops structure to be inherited by all breakpoint_ops
251 that are implemented on top of software or hardware breakpoints
252 (user breakpoints, internal and momentary breakpoints, etc.). */
253static struct breakpoint_ops bkpt_base_breakpoint_ops;
254
255/* Internal breakpoints class type. */
06edf0c0 256static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
257
258/* Momentary breakpoints class type. */
06edf0c0
PA
259static struct breakpoint_ops momentary_breakpoint_ops;
260
2060206e
PA
261/* The breakpoint_ops structure to be used in regular user created
262 breakpoints. */
263struct breakpoint_ops bkpt_breakpoint_ops;
264
55aa24fb
SDJ
265/* Breakpoints set on probes. */
266static struct breakpoint_ops bkpt_probe_breakpoint_ops;
267
e7e0cddf 268/* Dynamic printf class type. */
c5867ab6 269struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 270
d3ce09f5
SS
271/* The style in which to perform a dynamic printf. This is a user
272 option because different output options have different tradeoffs;
273 if GDB does the printing, there is better error handling if there
274 is a problem with any of the arguments, but using an inferior
275 function lets you have special-purpose printers and sending of
276 output to the same place as compiled-in print functions. */
277
278static const char dprintf_style_gdb[] = "gdb";
279static const char dprintf_style_call[] = "call";
280static const char dprintf_style_agent[] = "agent";
281static const char *const dprintf_style_enums[] = {
282 dprintf_style_gdb,
283 dprintf_style_call,
284 dprintf_style_agent,
285 NULL
286};
287static const char *dprintf_style = dprintf_style_gdb;
288
289/* The function to use for dynamic printf if the preferred style is to
290 call into the inferior. The value is simply a string that is
291 copied into the command, so it can be anything that GDB can
292 evaluate to a callable address, not necessarily a function name. */
293
bde6261a 294static char *dprintf_function;
d3ce09f5
SS
295
296/* The channel to use for dynamic printf if the preferred style is to
297 call into the inferior; if a nonempty string, it will be passed to
298 the call as the first argument, with the format string as the
299 second. As with the dprintf function, this can be anything that
300 GDB knows how to evaluate, so in addition to common choices like
301 "stderr", this could be an app-specific expression like
302 "mystreams[curlogger]". */
303
bde6261a 304static char *dprintf_channel;
d3ce09f5
SS
305
306/* True if dprintf commands should continue to operate even if GDB
307 has disconnected. */
308static int disconnected_dprintf = 1;
309
5cea2a26
PA
310struct command_line *
311breakpoint_commands (struct breakpoint *b)
312{
d1b0a7bf 313 return b->commands ? b->commands.get () : NULL;
5cea2a26 314}
3daf8fe5 315
f3b1572e
PA
316/* Flag indicating that a command has proceeded the inferior past the
317 current breakpoint. */
318
319static int breakpoint_proceeded;
320
956a9fb9 321const char *
2cec12e5
AR
322bpdisp_text (enum bpdisp disp)
323{
4a64f543
MS
324 /* NOTE: the following values are a part of MI protocol and
325 represent values of 'disp' field returned when inferior stops at
326 a breakpoint. */
bc043ef3 327 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 328
2cec12e5
AR
329 return bpdisps[(int) disp];
330}
c906108c 331
4a64f543 332/* Prototypes for exported functions. */
c906108c 333/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 334 if such is available. */
c906108c
SS
335static int can_use_hw_watchpoints;
336
920d2a44
AC
337static void
338show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
339 struct cmd_list_element *c,
340 const char *value)
341{
3e43a32a
MS
342 fprintf_filtered (file,
343 _("Debugger's willingness to use "
344 "watchpoint hardware is %s.\n"),
920d2a44
AC
345 value);
346}
347
fa8d40ab
JJ
348/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
349 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 350 for unrecognized breakpoint locations.
fa8d40ab
JJ
351 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
352static enum auto_boolean pending_break_support;
920d2a44
AC
353static void
354show_pending_break_support (struct ui_file *file, int from_tty,
355 struct cmd_list_element *c,
356 const char *value)
357{
3e43a32a
MS
358 fprintf_filtered (file,
359 _("Debugger's behavior regarding "
360 "pending breakpoints is %s.\n"),
920d2a44
AC
361 value);
362}
fa8d40ab 363
765dc015 364/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 365 set with "break" but falling in read-only memory.
765dc015
VP
366 If 0, gdb will warn about such breakpoints, but won't automatically
367 use hardware breakpoints. */
368static int automatic_hardware_breakpoints;
369static void
370show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
371 struct cmd_list_element *c,
372 const char *value)
373{
3e43a32a
MS
374 fprintf_filtered (file,
375 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
376 value);
377}
378
a25a5a45
PA
379/* If on, GDB keeps breakpoints inserted even if the inferior is
380 stopped, and immediately inserts any new breakpoints as soon as
381 they're created. If off (default), GDB keeps breakpoints off of
382 the target as long as possible. That is, it delays inserting
383 breakpoints until the next resume, and removes them again when the
384 target fully stops. This is a bit safer in case GDB crashes while
385 processing user input. */
386static int always_inserted_mode = 0;
72d0e2c5 387
33e5cbd6 388static void
74960c60 389show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 390 struct cmd_list_element *c, const char *value)
74960c60 391{
a25a5a45
PA
392 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
393 value);
74960c60
VP
394}
395
b57bacec
PA
396/* See breakpoint.h. */
397
33e5cbd6 398int
a25a5a45 399breakpoints_should_be_inserted_now (void)
33e5cbd6 400{
a25a5a45
PA
401 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
402 {
403 /* If breakpoints are global, they should be inserted even if no
404 thread under gdb's control is running, or even if there are
405 no threads under GDB's control yet. */
406 return 1;
407 }
408 else if (target_has_execution)
409 {
372316f1
PA
410 struct thread_info *tp;
411
a25a5a45
PA
412 if (always_inserted_mode)
413 {
414 /* The user wants breakpoints inserted even if all threads
415 are stopped. */
416 return 1;
417 }
418
b57bacec
PA
419 if (threads_are_executing ())
420 return 1;
372316f1
PA
421
422 /* Don't remove breakpoints yet if, even though all threads are
423 stopped, we still have events to process. */
424 ALL_NON_EXITED_THREADS (tp)
425 if (tp->resumed
426 && tp->suspend.waitstatus_pending_p)
427 return 1;
a25a5a45
PA
428 }
429 return 0;
33e5cbd6 430}
765dc015 431
b775012e
LM
432static const char condition_evaluation_both[] = "host or target";
433
434/* Modes for breakpoint condition evaluation. */
435static const char condition_evaluation_auto[] = "auto";
436static const char condition_evaluation_host[] = "host";
437static const char condition_evaluation_target[] = "target";
438static const char *const condition_evaluation_enums[] = {
439 condition_evaluation_auto,
440 condition_evaluation_host,
441 condition_evaluation_target,
442 NULL
443};
444
445/* Global that holds the current mode for breakpoint condition evaluation. */
446static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
447
448/* Global that we use to display information to the user (gets its value from
449 condition_evaluation_mode_1. */
450static const char *condition_evaluation_mode = condition_evaluation_auto;
451
452/* Translate a condition evaluation mode MODE into either "host"
453 or "target". This is used mostly to translate from "auto" to the
454 real setting that is being used. It returns the translated
455 evaluation mode. */
456
457static const char *
458translate_condition_evaluation_mode (const char *mode)
459{
460 if (mode == condition_evaluation_auto)
461 {
462 if (target_supports_evaluation_of_breakpoint_conditions ())
463 return condition_evaluation_target;
464 else
465 return condition_evaluation_host;
466 }
467 else
468 return mode;
469}
470
471/* Discovers what condition_evaluation_auto translates to. */
472
473static const char *
474breakpoint_condition_evaluation_mode (void)
475{
476 return translate_condition_evaluation_mode (condition_evaluation_mode);
477}
478
479/* Return true if GDB should evaluate breakpoint conditions or false
480 otherwise. */
481
482static int
483gdb_evaluates_breakpoint_condition_p (void)
484{
485 const char *mode = breakpoint_condition_evaluation_mode ();
486
487 return (mode == condition_evaluation_host);
488}
489
c906108c
SS
490/* Are we executing breakpoint commands? */
491static int executing_breakpoint_commands;
492
c02f5703
MS
493/* Are overlay event breakpoints enabled? */
494static int overlay_events_enabled;
495
e09342b5
TJB
496/* See description in breakpoint.h. */
497int target_exact_watchpoints = 0;
498
c906108c 499/* Walk the following statement or block through all breakpoints.
e5dd4106 500 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 501 current breakpoint. */
c906108c 502
5c44784c 503#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 504
5c44784c
JM
505#define ALL_BREAKPOINTS_SAFE(B,TMP) \
506 for (B = breakpoint_chain; \
507 B ? (TMP=B->next, 1): 0; \
508 B = TMP)
c906108c 509
4a64f543
MS
510/* Similar iterator for the low-level breakpoints. SAFE variant is
511 not provided so update_global_location_list must not be called
512 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 513
876fa593 514#define ALL_BP_LOCATIONS(B,BP_TMP) \
f5336ca5
PA
515 for (BP_TMP = bp_locations; \
516 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
876fa593 517 BP_TMP++)
7cc221ef 518
b775012e
LM
519/* Iterates through locations with address ADDRESS for the currently selected
520 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
521 to where the loop should start from.
522 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
523 appropriate location to start with. */
524
525#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
526 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
527 BP_LOCP_TMP = BP_LOCP_START; \
528 BP_LOCP_START \
f5336ca5 529 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
b775012e
LM
530 && (*BP_LOCP_TMP)->address == ADDRESS); \
531 BP_LOCP_TMP++)
532
1042e4c0
SS
533/* Iterator for tracepoints only. */
534
535#define ALL_TRACEPOINTS(B) \
536 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 537 if (is_tracepoint (B))
1042e4c0 538
7cc221ef 539/* Chains of all breakpoints defined. */
c906108c
SS
540
541struct breakpoint *breakpoint_chain;
542
f5336ca5 543/* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
876fa593 544
f5336ca5 545static struct bp_location **bp_locations;
876fa593 546
f5336ca5 547/* Number of elements of BP_LOCATIONS. */
876fa593 548
f5336ca5 549static unsigned bp_locations_count;
876fa593 550
4a64f543 551/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 552 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 553 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 554 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 555 an address you need to read. */
876fa593 556
f5336ca5 557static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 558
4a64f543
MS
559/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
560 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
561 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
562 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 563 scan for shadow bytes for an address you need to read. */
876fa593 564
f5336ca5 565static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 566
4a64f543 567/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
568 from the bp_locations array, but for which a hit may still be
569 reported by a target. */
20874c92
VP
570VEC(bp_location_p) *moribund_locations = NULL;
571
c906108c
SS
572/* Number of last breakpoint made. */
573
95a42b64
TT
574static int breakpoint_count;
575
86b17b60
PA
576/* The value of `breakpoint_count' before the last command that
577 created breakpoints. If the last (break-like) command created more
578 than one breakpoint, then the difference between BREAKPOINT_COUNT
579 and PREV_BREAKPOINT_COUNT is more than one. */
580static int prev_breakpoint_count;
c906108c 581
1042e4c0
SS
582/* Number of last tracepoint made. */
583
95a42b64 584static int tracepoint_count;
1042e4c0 585
6149aea9
PA
586static struct cmd_list_element *breakpoint_set_cmdlist;
587static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 588struct cmd_list_element *save_cmdlist;
6149aea9 589
badd37ce
SDJ
590/* See declaration at breakpoint.h. */
591
592struct breakpoint *
593breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
594 void *user_data)
595{
596 struct breakpoint *b = NULL;
597
598 ALL_BREAKPOINTS (b)
599 {
600 if (func (b, user_data) != 0)
601 break;
602 }
603
604 return b;
605}
606
468d015d
JJ
607/* Return whether a breakpoint is an active enabled breakpoint. */
608static int
609breakpoint_enabled (struct breakpoint *b)
610{
0d381245 611 return (b->enable_state == bp_enabled);
468d015d
JJ
612}
613
c906108c
SS
614/* Set breakpoint count to NUM. */
615
95a42b64 616static void
fba45db2 617set_breakpoint_count (int num)
c906108c 618{
86b17b60 619 prev_breakpoint_count = breakpoint_count;
c906108c 620 breakpoint_count = num;
4fa62494 621 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
622}
623
86b17b60
PA
624/* Used by `start_rbreak_breakpoints' below, to record the current
625 breakpoint count before "rbreak" creates any breakpoint. */
626static int rbreak_start_breakpoint_count;
627
95a42b64
TT
628/* Called at the start an "rbreak" command to record the first
629 breakpoint made. */
86b17b60 630
c80049d3 631scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 632{
86b17b60 633 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
634}
635
636/* Called at the end of an "rbreak" command to record the last
637 breakpoint made. */
86b17b60 638
c80049d3 639scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 640{
86b17b60 641 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
642}
643
4a64f543 644/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
645
646void
fba45db2 647clear_breakpoint_hit_counts (void)
c906108c
SS
648{
649 struct breakpoint *b;
650
651 ALL_BREAKPOINTS (b)
652 b->hit_count = 0;
653}
654
c906108c 655\f
48cb2d85
VP
656/* Return the breakpoint with the specified number, or NULL
657 if the number does not refer to an existing breakpoint. */
658
659struct breakpoint *
660get_breakpoint (int num)
661{
662 struct breakpoint *b;
663
664 ALL_BREAKPOINTS (b)
665 if (b->number == num)
666 return b;
667
668 return NULL;
669}
5c44784c 670
c906108c 671\f
adc36818 672
b775012e
LM
673/* Mark locations as "conditions have changed" in case the target supports
674 evaluating conditions on its side. */
675
676static void
677mark_breakpoint_modified (struct breakpoint *b)
678{
679 struct bp_location *loc;
680
681 /* This is only meaningful if the target is
682 evaluating conditions and if the user has
683 opted for condition evaluation on the target's
684 side. */
685 if (gdb_evaluates_breakpoint_condition_p ()
686 || !target_supports_evaluation_of_breakpoint_conditions ())
687 return;
688
689 if (!is_breakpoint (b))
690 return;
691
692 for (loc = b->loc; loc; loc = loc->next)
693 loc->condition_changed = condition_modified;
694}
695
696/* Mark location as "conditions have changed" in case the target supports
697 evaluating conditions on its side. */
698
699static void
700mark_breakpoint_location_modified (struct bp_location *loc)
701{
702 /* This is only meaningful if the target is
703 evaluating conditions and if the user has
704 opted for condition evaluation on the target's
705 side. */
706 if (gdb_evaluates_breakpoint_condition_p ()
707 || !target_supports_evaluation_of_breakpoint_conditions ())
708
709 return;
710
711 if (!is_breakpoint (loc->owner))
712 return;
713
714 loc->condition_changed = condition_modified;
715}
716
717/* Sets the condition-evaluation mode using the static global
718 condition_evaluation_mode. */
719
720static void
721set_condition_evaluation_mode (char *args, int from_tty,
722 struct cmd_list_element *c)
723{
b775012e
LM
724 const char *old_mode, *new_mode;
725
726 if ((condition_evaluation_mode_1 == condition_evaluation_target)
727 && !target_supports_evaluation_of_breakpoint_conditions ())
728 {
729 condition_evaluation_mode_1 = condition_evaluation_mode;
730 warning (_("Target does not support breakpoint condition evaluation.\n"
731 "Using host evaluation mode instead."));
732 return;
733 }
734
735 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
736 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
737
abf1152a
JK
738 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
739 settings was "auto". */
740 condition_evaluation_mode = condition_evaluation_mode_1;
741
b775012e
LM
742 /* Only update the mode if the user picked a different one. */
743 if (new_mode != old_mode)
744 {
745 struct bp_location *loc, **loc_tmp;
746 /* If the user switched to a different evaluation mode, we
747 need to synch the changes with the target as follows:
748
749 "host" -> "target": Send all (valid) conditions to the target.
750 "target" -> "host": Remove all the conditions from the target.
751 */
752
b775012e
LM
753 if (new_mode == condition_evaluation_target)
754 {
755 /* Mark everything modified and synch conditions with the
756 target. */
757 ALL_BP_LOCATIONS (loc, loc_tmp)
758 mark_breakpoint_location_modified (loc);
759 }
760 else
761 {
762 /* Manually mark non-duplicate locations to synch conditions
763 with the target. We do this to remove all the conditions the
764 target knows about. */
765 ALL_BP_LOCATIONS (loc, loc_tmp)
766 if (is_breakpoint (loc->owner) && loc->inserted)
767 loc->needs_update = 1;
768 }
769
770 /* Do the update. */
44702360 771 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
772 }
773
774 return;
775}
776
777/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
778 what "auto" is translating to. */
779
780static void
781show_condition_evaluation_mode (struct ui_file *file, int from_tty,
782 struct cmd_list_element *c, const char *value)
783{
784 if (condition_evaluation_mode == condition_evaluation_auto)
785 fprintf_filtered (file,
786 _("Breakpoint condition evaluation "
787 "mode is %s (currently %s).\n"),
788 value,
789 breakpoint_condition_evaluation_mode ());
790 else
791 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
792 value);
793}
794
795/* A comparison function for bp_location AP and BP that is used by
796 bsearch. This comparison function only cares about addresses, unlike
f5336ca5 797 the more general bp_locations_compare function. */
b775012e
LM
798
799static int
f5336ca5 800bp_locations_compare_addrs (const void *ap, const void *bp)
b775012e 801{
9a3c8263
SM
802 const struct bp_location *a = *(const struct bp_location **) ap;
803 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
804
805 if (a->address == b->address)
806 return 0;
807 else
808 return ((a->address > b->address) - (a->address < b->address));
809}
810
811/* Helper function to skip all bp_locations with addresses
812 less than ADDRESS. It returns the first bp_location that
813 is greater than or equal to ADDRESS. If none is found, just
814 return NULL. */
815
816static struct bp_location **
817get_first_locp_gte_addr (CORE_ADDR address)
818{
819 struct bp_location dummy_loc;
820 struct bp_location *dummy_locp = &dummy_loc;
821 struct bp_location **locp_found = NULL;
822
823 /* Initialize the dummy location's address field. */
b775012e
LM
824 dummy_loc.address = address;
825
826 /* Find a close match to the first location at ADDRESS. */
9a3c8263 827 locp_found = ((struct bp_location **)
f5336ca5 828 bsearch (&dummy_locp, bp_locations, bp_locations_count,
9a3c8263 829 sizeof (struct bp_location **),
f5336ca5 830 bp_locations_compare_addrs));
b775012e
LM
831
832 /* Nothing was found, nothing left to do. */
833 if (locp_found == NULL)
834 return NULL;
835
836 /* We may have found a location that is at ADDRESS but is not the first in the
837 location's list. Go backwards (if possible) and locate the first one. */
f5336ca5 838 while ((locp_found - 1) >= bp_locations
b775012e
LM
839 && (*(locp_found - 1))->address == address)
840 locp_found--;
841
842 return locp_found;
843}
844
adc36818 845void
7a26bd4d 846set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
847 int from_tty)
848{
3a5c3e22
PA
849 xfree (b->cond_string);
850 b->cond_string = NULL;
adc36818 851
3a5c3e22 852 if (is_watchpoint (b))
adc36818 853 {
3a5c3e22
PA
854 struct watchpoint *w = (struct watchpoint *) b;
855
4d01a485 856 w->cond_exp.reset ();
3a5c3e22
PA
857 }
858 else
859 {
860 struct bp_location *loc;
861
862 for (loc = b->loc; loc; loc = loc->next)
863 {
4d01a485 864 loc->cond.reset ();
b775012e
LM
865
866 /* No need to free the condition agent expression
867 bytecode (if we have one). We will handle this
868 when we go through update_global_location_list. */
3a5c3e22 869 }
adc36818 870 }
adc36818
PM
871
872 if (*exp == 0)
873 {
874 if (from_tty)
875 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
876 }
877 else
878 {
bbc13ae3 879 const char *arg = exp;
cc59ec59 880
adc36818
PM
881 /* I don't know if it matters whether this is the string the user
882 typed in or the decompiled expression. */
883 b->cond_string = xstrdup (arg);
884 b->condition_not_parsed = 0;
885
886 if (is_watchpoint (b))
887 {
3a5c3e22
PA
888 struct watchpoint *w = (struct watchpoint *) b;
889
adc36818
PM
890 innermost_block = NULL;
891 arg = exp;
1bb9788d 892 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
893 if (*arg)
894 error (_("Junk at end of expression"));
3a5c3e22 895 w->cond_exp_valid_block = innermost_block;
adc36818
PM
896 }
897 else
898 {
3a5c3e22
PA
899 struct bp_location *loc;
900
adc36818
PM
901 for (loc = b->loc; loc; loc = loc->next)
902 {
903 arg = exp;
904 loc->cond =
1bb9788d
TT
905 parse_exp_1 (&arg, loc->address,
906 block_for_pc (loc->address), 0);
adc36818
PM
907 if (*arg)
908 error (_("Junk at end of expression"));
909 }
910 }
911 }
b775012e
LM
912 mark_breakpoint_modified (b);
913
8d3788bd 914 observer_notify_breakpoint_modified (b);
adc36818
PM
915}
916
d55637df
TT
917/* Completion for the "condition" command. */
918
eb3ff9a5 919static void
6f937416 920condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 921 completion_tracker &tracker,
6f937416 922 const char *text, const char *word)
d55637df 923{
6f937416 924 const char *space;
d55637df 925
f1735a53
TT
926 text = skip_spaces (text);
927 space = skip_to_space (text);
d55637df
TT
928 if (*space == '\0')
929 {
930 int len;
931 struct breakpoint *b;
932 VEC (char_ptr) *result = NULL;
933
934 if (text[0] == '$')
935 {
936 /* We don't support completion of history indices. */
eb3ff9a5
PA
937 if (!isdigit (text[1]))
938 complete_internalvar (tracker, &text[1]);
939 return;
d55637df
TT
940 }
941
942 /* We're completing the breakpoint number. */
943 len = strlen (text);
944
945 ALL_BREAKPOINTS (b)
58ce7251
SDJ
946 {
947 char number[50];
948
949 xsnprintf (number, sizeof (number), "%d", b->number);
950
951 if (strncmp (number, text, len) == 0)
eb3ff9a5
PA
952 {
953 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
954 tracker.add_completion (std::move (copy));
955 }
58ce7251 956 }
d55637df 957
eb3ff9a5 958 return;
d55637df
TT
959 }
960
961 /* We're completing the expression part. */
f1735a53 962 text = skip_spaces (space);
eb3ff9a5 963 expression_completer (cmd, tracker, text, word);
d55637df
TT
964}
965
c906108c
SS
966/* condition N EXP -- set break condition of breakpoint N to EXP. */
967
968static void
0b39b52e 969condition_command (const char *arg, int from_tty)
c906108c 970{
52f0bd74 971 struct breakpoint *b;
0b39b52e 972 const char *p;
52f0bd74 973 int bnum;
c906108c
SS
974
975 if (arg == 0)
e2e0b3e5 976 error_no_arg (_("breakpoint number"));
c906108c
SS
977
978 p = arg;
979 bnum = get_number (&p);
5c44784c 980 if (bnum == 0)
8a3fe4f8 981 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
982
983 ALL_BREAKPOINTS (b)
984 if (b->number == bnum)
2f069f6f 985 {
6dddc817
DE
986 /* Check if this breakpoint has a "stop" method implemented in an
987 extension language. This method and conditions entered into GDB
988 from the CLI are mutually exclusive. */
989 const struct extension_language_defn *extlang
990 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
991
992 if (extlang != NULL)
993 {
994 error (_("Only one stop condition allowed. There is currently"
995 " a %s stop condition defined for this breakpoint."),
996 ext_lang_capitalized_name (extlang));
997 }
2566ad2d 998 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
999
1000 if (is_breakpoint (b))
44702360 1001 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1002
2f069f6f
JB
1003 return;
1004 }
c906108c 1005
8a3fe4f8 1006 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1007}
1008
a7bdde9e
VP
1009/* Check that COMMAND do not contain commands that are suitable
1010 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1011 Throw if any such commands is found. */
1012
a7bdde9e
VP
1013static void
1014check_no_tracepoint_commands (struct command_line *commands)
1015{
1016 struct command_line *c;
cc59ec59 1017
a7bdde9e
VP
1018 for (c = commands; c; c = c->next)
1019 {
1020 int i;
1021
1022 if (c->control_type == while_stepping_control)
3e43a32a
MS
1023 error (_("The 'while-stepping' command can "
1024 "only be used for tracepoints"));
a7bdde9e
VP
1025
1026 for (i = 0; i < c->body_count; ++i)
1027 check_no_tracepoint_commands ((c->body_list)[i]);
1028
1029 /* Not that command parsing removes leading whitespace and comment
4a64f543 1030 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1031 command directly. */
1032 if (strstr (c->line, "collect ") == c->line)
1033 error (_("The 'collect' command can only be used for tracepoints"));
1034
51661e93
VP
1035 if (strstr (c->line, "teval ") == c->line)
1036 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1037 }
1038}
1039
c1fc2657 1040struct longjmp_breakpoint : public breakpoint
3b0871f4 1041{
c1fc2657 1042 ~longjmp_breakpoint () override;
3b0871f4
SM
1043};
1044
d77f58be
SS
1045/* Encapsulate tests for different types of tracepoints. */
1046
3b0871f4
SM
1047static bool
1048is_tracepoint_type (bptype type)
d9b3f62e
PA
1049{
1050 return (type == bp_tracepoint
1051 || type == bp_fast_tracepoint
1052 || type == bp_static_tracepoint);
1053}
1054
3b0871f4
SM
1055static bool
1056is_longjmp_type (bptype type)
1057{
1058 return type == bp_longjmp || type == bp_exception;
1059}
1060
a7bdde9e 1061int
d77f58be 1062is_tracepoint (const struct breakpoint *b)
a7bdde9e 1063{
d9b3f62e 1064 return is_tracepoint_type (b->type);
a7bdde9e 1065}
d9b3f62e 1066
a5e364af
SM
1067/* Factory function to create an appropriate instance of breakpoint given
1068 TYPE. */
1069
1070static std::unique_ptr<breakpoint>
1071new_breakpoint_from_type (bptype type)
1072{
1073 breakpoint *b;
1074
1075 if (is_tracepoint_type (type))
c1fc2657 1076 b = new tracepoint ();
3b0871f4 1077 else if (is_longjmp_type (type))
c1fc2657 1078 b = new longjmp_breakpoint ();
a5e364af
SM
1079 else
1080 b = new breakpoint ();
1081
1082 return std::unique_ptr<breakpoint> (b);
1083}
1084
e5dd4106 1085/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1086 breakpoint. This function will throw an exception if a problem is
1087 found. */
48cb2d85 1088
95a42b64
TT
1089static void
1090validate_commands_for_breakpoint (struct breakpoint *b,
1091 struct command_line *commands)
48cb2d85 1092{
d77f58be 1093 if (is_tracepoint (b))
a7bdde9e 1094 {
c9a6ce02 1095 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1096 struct command_line *c;
1097 struct command_line *while_stepping = 0;
c9a6ce02
PA
1098
1099 /* Reset the while-stepping step count. The previous commands
1100 might have included a while-stepping action, while the new
1101 ones might not. */
1102 t->step_count = 0;
1103
1104 /* We need to verify that each top-level element of commands is
1105 valid for tracepoints, that there's at most one
1106 while-stepping element, and that the while-stepping's body
1107 has valid tracing commands excluding nested while-stepping.
1108 We also need to validate the tracepoint action line in the
1109 context of the tracepoint --- validate_actionline actually
1110 has side effects, like setting the tracepoint's
1111 while-stepping STEP_COUNT, in addition to checking if the
1112 collect/teval actions parse and make sense in the
1113 tracepoint's context. */
a7bdde9e
VP
1114 for (c = commands; c; c = c->next)
1115 {
a7bdde9e
VP
1116 if (c->control_type == while_stepping_control)
1117 {
1118 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1119 error (_("The 'while-stepping' command "
1120 "cannot be used for fast tracepoint"));
0fb4aa4b 1121 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1122 error (_("The 'while-stepping' command "
1123 "cannot be used for static tracepoint"));
a7bdde9e
VP
1124
1125 if (while_stepping)
3e43a32a
MS
1126 error (_("The 'while-stepping' command "
1127 "can be used only once"));
a7bdde9e
VP
1128 else
1129 while_stepping = c;
1130 }
c9a6ce02
PA
1131
1132 validate_actionline (c->line, b);
a7bdde9e
VP
1133 }
1134 if (while_stepping)
1135 {
1136 struct command_line *c2;
1137
1138 gdb_assert (while_stepping->body_count == 1);
1139 c2 = while_stepping->body_list[0];
1140 for (; c2; c2 = c2->next)
1141 {
a7bdde9e
VP
1142 if (c2->control_type == while_stepping_control)
1143 error (_("The 'while-stepping' command cannot be nested"));
1144 }
1145 }
1146 }
1147 else
1148 {
1149 check_no_tracepoint_commands (commands);
1150 }
95a42b64
TT
1151}
1152
0fb4aa4b
PA
1153/* Return a vector of all the static tracepoints set at ADDR. The
1154 caller is responsible for releasing the vector. */
1155
1156VEC(breakpoint_p) *
1157static_tracepoints_here (CORE_ADDR addr)
1158{
1159 struct breakpoint *b;
1160 VEC(breakpoint_p) *found = 0;
1161 struct bp_location *loc;
1162
1163 ALL_BREAKPOINTS (b)
1164 if (b->type == bp_static_tracepoint)
1165 {
1166 for (loc = b->loc; loc; loc = loc->next)
1167 if (loc->address == addr)
1168 VEC_safe_push(breakpoint_p, found, b);
1169 }
1170
1171 return found;
1172}
1173
95a42b64 1174/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1175 validate that only allowed commands are included. */
95a42b64
TT
1176
1177void
4a64f543 1178breakpoint_set_commands (struct breakpoint *b,
93921405 1179 command_line_up &&commands)
95a42b64 1180{
93921405 1181 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1182
d1b0a7bf 1183 b->commands = std::move (commands);
8d3788bd 1184 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1185}
1186
45a43567
TT
1187/* Set the internal `silent' flag on the breakpoint. Note that this
1188 is not the same as the "silent" that may appear in the breakpoint's
1189 commands. */
1190
1191void
1192breakpoint_set_silent (struct breakpoint *b, int silent)
1193{
1194 int old_silent = b->silent;
1195
1196 b->silent = silent;
1197 if (old_silent != silent)
8d3788bd 1198 observer_notify_breakpoint_modified (b);
45a43567
TT
1199}
1200
1201/* Set the thread for this breakpoint. If THREAD is -1, make the
1202 breakpoint work for any thread. */
1203
1204void
1205breakpoint_set_thread (struct breakpoint *b, int thread)
1206{
1207 int old_thread = b->thread;
1208
1209 b->thread = thread;
1210 if (old_thread != thread)
8d3788bd 1211 observer_notify_breakpoint_modified (b);
45a43567
TT
1212}
1213
1214/* Set the task for this breakpoint. If TASK is 0, make the
1215 breakpoint work for any task. */
1216
1217void
1218breakpoint_set_task (struct breakpoint *b, int task)
1219{
1220 int old_task = b->task;
1221
1222 b->task = task;
1223 if (old_task != task)
8d3788bd 1224 observer_notify_breakpoint_modified (b);
45a43567
TT
1225}
1226
95a42b64
TT
1227void
1228check_tracepoint_command (char *line, void *closure)
a7bdde9e 1229{
9a3c8263 1230 struct breakpoint *b = (struct breakpoint *) closure;
cc59ec59 1231
6f937416 1232 validate_actionline (line, b);
a7bdde9e
VP
1233}
1234
95a42b64 1235static void
896b6bda 1236commands_command_1 (const char *arg, int from_tty,
4a64f543 1237 struct command_line *control)
95a42b64 1238{
d1b0a7bf 1239 counted_command_line cmd;
95a42b64 1240
896b6bda
PA
1241 std::string new_arg;
1242
95a42b64
TT
1243 if (arg == NULL || !*arg)
1244 {
86b17b60 1245 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1246 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1247 breakpoint_count);
95a42b64 1248 else if (breakpoint_count > 0)
896b6bda 1249 new_arg = string_printf ("%d", breakpoint_count);
48649e1b
TT
1250 arg = new_arg.c_str ();
1251 }
1252
1253 map_breakpoint_numbers
1254 (arg, [&] (breakpoint *b)
1255 {
1256 if (cmd == NULL)
1257 {
48649e1b 1258 if (control != NULL)
d1b0a7bf 1259 cmd = copy_command_lines (control->body_list[0]);
48649e1b
TT
1260 else
1261 {
81b1e71c
TT
1262 std::string str
1263 = string_printf (_("Type commands for breakpoint(s) "
1264 "%s, one per line."),
1265 arg);
48649e1b 1266
81b1e71c 1267 cmd = read_command_lines (&str[0],
d1b0a7bf
TT
1268 from_tty, 1,
1269 (is_tracepoint (b)
1270 ? check_tracepoint_command : 0),
1271 b);
48649e1b 1272 }
48649e1b
TT
1273 }
1274
1275 /* If a breakpoint was on the list more than once, we don't need to
1276 do anything. */
1277 if (b->commands != cmd)
1278 {
d1b0a7bf 1279 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b
TT
1280 b->commands = cmd;
1281 observer_notify_breakpoint_modified (b);
1282 }
1283 });
95a42b64 1284
48649e1b 1285 if (cmd == NULL)
95a42b64 1286 error (_("No breakpoints specified."));
95a42b64
TT
1287}
1288
1289static void
0b39b52e 1290commands_command (const char *arg, int from_tty)
95a42b64
TT
1291{
1292 commands_command_1 (arg, from_tty, NULL);
c906108c 1293}
40c03ae8
EZ
1294
1295/* Like commands_command, but instead of reading the commands from
1296 input stream, takes them from an already parsed command structure.
1297
1298 This is used by cli-script.c to DTRT with breakpoint commands
1299 that are part of if and while bodies. */
1300enum command_control_type
896b6bda 1301commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1302{
95a42b64
TT
1303 commands_command_1 (arg, 0, cmd);
1304 return simple_control;
40c03ae8 1305}
876fa593
JK
1306
1307/* Return non-zero if BL->TARGET_INFO contains valid information. */
1308
1309static int
1310bp_location_has_shadow (struct bp_location *bl)
1311{
1312 if (bl->loc_type != bp_loc_software_breakpoint)
1313 return 0;
1314 if (!bl->inserted)
1315 return 0;
1316 if (bl->target_info.shadow_len == 0)
e5dd4106 1317 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1318 return 0;
1319 return 1;
1320}
1321
9d497a19
PA
1322/* Update BUF, which is LEN bytes read from the target address
1323 MEMADDR, by replacing a memory breakpoint with its shadowed
1324 contents.
1325
1326 If READBUF is not NULL, this buffer must not overlap with the of
1327 the breakpoint location's shadow_contents buffer. Otherwise, a
1328 failed assertion internal error will be raised. */
1329
1330static void
1331one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1332 const gdb_byte *writebuf_org,
1333 ULONGEST memaddr, LONGEST len,
1334 struct bp_target_info *target_info,
1335 struct gdbarch *gdbarch)
1336{
1337 /* Now do full processing of the found relevant range of elements. */
1338 CORE_ADDR bp_addr = 0;
1339 int bp_size = 0;
1340 int bptoffset = 0;
1341
1342 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1343 current_program_space->aspace, 0))
1344 {
1345 /* The breakpoint is inserted in a different address space. */
1346 return;
1347 }
1348
1349 /* Addresses and length of the part of the breakpoint that
1350 we need to copy. */
1351 bp_addr = target_info->placed_address;
1352 bp_size = target_info->shadow_len;
1353
1354 if (bp_addr + bp_size <= memaddr)
1355 {
1356 /* The breakpoint is entirely before the chunk of memory we are
1357 reading. */
1358 return;
1359 }
1360
1361 if (bp_addr >= memaddr + len)
1362 {
1363 /* The breakpoint is entirely after the chunk of memory we are
1364 reading. */
1365 return;
1366 }
1367
1368 /* Offset within shadow_contents. */
1369 if (bp_addr < memaddr)
1370 {
1371 /* Only copy the second part of the breakpoint. */
1372 bp_size -= memaddr - bp_addr;
1373 bptoffset = memaddr - bp_addr;
1374 bp_addr = memaddr;
1375 }
1376
1377 if (bp_addr + bp_size > memaddr + len)
1378 {
1379 /* Only copy the first part of the breakpoint. */
1380 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1381 }
1382
1383 if (readbuf != NULL)
1384 {
1385 /* Verify that the readbuf buffer does not overlap with the
1386 shadow_contents buffer. */
1387 gdb_assert (target_info->shadow_contents >= readbuf + len
1388 || readbuf >= (target_info->shadow_contents
1389 + target_info->shadow_len));
1390
1391 /* Update the read buffer with this inserted breakpoint's
1392 shadow. */
1393 memcpy (readbuf + bp_addr - memaddr,
1394 target_info->shadow_contents + bptoffset, bp_size);
1395 }
1396 else
1397 {
1398 const unsigned char *bp;
0d5ed153
MR
1399 CORE_ADDR addr = target_info->reqstd_address;
1400 int placed_size;
9d497a19
PA
1401
1402 /* Update the shadow with what we want to write to memory. */
1403 memcpy (target_info->shadow_contents + bptoffset,
1404 writebuf_org + bp_addr - memaddr, bp_size);
1405
1406 /* Determine appropriate breakpoint contents and size for this
1407 address. */
0d5ed153 1408 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1409
1410 /* Update the final write buffer with this inserted
1411 breakpoint's INSN. */
1412 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1413 }
1414}
1415
8defab1a 1416/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1417 by replacing any memory breakpoints with their shadowed contents.
1418
35c63cd8
JB
1419 If READBUF is not NULL, this buffer must not overlap with any of
1420 the breakpoint location's shadow_contents buffers. Otherwise,
1421 a failed assertion internal error will be raised.
1422
876fa593 1423 The range of shadowed area by each bp_location is:
f5336ca5
PA
1424 bl->address - bp_locations_placed_address_before_address_max
1425 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1426 The range we were requested to resolve shadows for is:
1427 memaddr ... memaddr + len
1428 Thus the safe cutoff boundaries for performance optimization are
35df4500 1429 memaddr + len <= (bl->address
f5336ca5 1430 - bp_locations_placed_address_before_address_max)
876fa593 1431 and:
f5336ca5 1432 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1433
8defab1a 1434void
f0ba3972
PA
1435breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1436 const gdb_byte *writebuf_org,
1437 ULONGEST memaddr, LONGEST len)
c906108c 1438{
4a64f543
MS
1439 /* Left boundary, right boundary and median element of our binary
1440 search. */
876fa593
JK
1441 unsigned bc_l, bc_r, bc;
1442
4a64f543
MS
1443 /* Find BC_L which is a leftmost element which may affect BUF
1444 content. It is safe to report lower value but a failure to
1445 report higher one. */
876fa593
JK
1446
1447 bc_l = 0;
f5336ca5 1448 bc_r = bp_locations_count;
876fa593
JK
1449 while (bc_l + 1 < bc_r)
1450 {
35df4500 1451 struct bp_location *bl;
876fa593
JK
1452
1453 bc = (bc_l + bc_r) / 2;
f5336ca5 1454 bl = bp_locations[bc];
876fa593 1455
4a64f543
MS
1456 /* Check first BL->ADDRESS will not overflow due to the added
1457 constant. Then advance the left boundary only if we are sure
1458 the BC element can in no way affect the BUF content (MEMADDR
1459 to MEMADDR + LEN range).
876fa593 1460
f5336ca5 1461 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1462 offset so that we cannot miss a breakpoint with its shadow
1463 range tail still reaching MEMADDR. */
c5aa993b 1464
f5336ca5 1465 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1466 >= bl->address)
f5336ca5 1467 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1468 <= memaddr))
876fa593
JK
1469 bc_l = bc;
1470 else
1471 bc_r = bc;
1472 }
1473
128070bb
PA
1474 /* Due to the binary search above, we need to make sure we pick the
1475 first location that's at BC_L's address. E.g., if there are
1476 multiple locations at the same address, BC_L may end up pointing
1477 at a duplicate location, and miss the "master"/"inserted"
1478 location. Say, given locations L1, L2 and L3 at addresses A and
1479 B:
1480
1481 L1@A, L2@A, L3@B, ...
1482
1483 BC_L could end up pointing at location L2, while the "master"
1484 location could be L1. Since the `loc->inserted' flag is only set
1485 on "master" locations, we'd forget to restore the shadow of L1
1486 and L2. */
1487 while (bc_l > 0
f5336ca5 1488 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1489 bc_l--;
1490
876fa593
JK
1491 /* Now do full processing of the found relevant range of elements. */
1492
f5336ca5 1493 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1494 {
f5336ca5 1495 struct bp_location *bl = bp_locations[bc];
876fa593 1496
35df4500
TJB
1497 /* bp_location array has BL->OWNER always non-NULL. */
1498 if (bl->owner->type == bp_none)
8a3fe4f8 1499 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1500 bl->owner->number);
ffce0d52 1501
e5dd4106 1502 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1503 content. */
1504
f5336ca5
PA
1505 if (bl->address >= bp_locations_placed_address_before_address_max
1506 && memaddr + len <= (bl->address
1507 - bp_locations_placed_address_before_address_max))
876fa593
JK
1508 break;
1509
35df4500 1510 if (!bp_location_has_shadow (bl))
c5aa993b 1511 continue;
6c95b8df 1512
9d497a19
PA
1513 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1514 memaddr, len, &bl->target_info, bl->gdbarch);
1515 }
c906108c 1516}
9d497a19 1517
c906108c 1518\f
c5aa993b 1519
b775012e
LM
1520/* Return true if BPT is either a software breakpoint or a hardware
1521 breakpoint. */
1522
1523int
1524is_breakpoint (const struct breakpoint *bpt)
1525{
1526 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1527 || bpt->type == bp_hardware_breakpoint
1528 || bpt->type == bp_dprintf);
b775012e
LM
1529}
1530
60e1c644
PA
1531/* Return true if BPT is of any hardware watchpoint kind. */
1532
a5606eee 1533static int
d77f58be 1534is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1535{
1536 return (bpt->type == bp_hardware_watchpoint
1537 || bpt->type == bp_read_watchpoint
1538 || bpt->type == bp_access_watchpoint);
1539}
7270d8f2 1540
60e1c644
PA
1541/* Return true if BPT is of any watchpoint kind, hardware or
1542 software. */
1543
3a5c3e22 1544int
d77f58be 1545is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1546{
1547 return (is_hardware_watchpoint (bpt)
1548 || bpt->type == bp_watchpoint);
1549}
1550
3a5c3e22
PA
1551/* Returns true if the current thread and its running state are safe
1552 to evaluate or update watchpoint B. Watchpoints on local
1553 expressions need to be evaluated in the context of the thread that
1554 was current when the watchpoint was created, and, that thread needs
1555 to be stopped to be able to select the correct frame context.
1556 Watchpoints on global expressions can be evaluated on any thread,
1557 and in any state. It is presently left to the target allowing
1558 memory accesses when threads are running. */
f6bc2008
PA
1559
1560static int
3a5c3e22 1561watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1562{
c1fc2657 1563 return (b->pspace == current_program_space
d0d8b0c6
JK
1564 && (ptid_equal (b->watchpoint_thread, null_ptid)
1565 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1566 && !is_executing (inferior_ptid))));
f6bc2008
PA
1567}
1568
d0fb5eae
JK
1569/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1570 associated bp_watchpoint_scope breakpoint. */
1571
1572static void
3a5c3e22 1573watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1574{
c1fc2657 1575 if (w->related_breakpoint != w)
d0fb5eae 1576 {
c1fc2657
SM
1577 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1578 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1579 w->related_breakpoint->disposition = disp_del_at_next_stop;
1580 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1581 w->related_breakpoint = w;
d0fb5eae 1582 }
c1fc2657 1583 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1584}
1585
bb9d5f81
PP
1586/* Extract a bitfield value from value VAL using the bit parameters contained in
1587 watchpoint W. */
1588
1589static struct value *
1590extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1591{
1592 struct value *bit_val;
1593
1594 if (val == NULL)
1595 return NULL;
1596
1597 bit_val = allocate_value (value_type (val));
1598
1599 unpack_value_bitfield (bit_val,
1600 w->val_bitpos,
1601 w->val_bitsize,
1602 value_contents_for_printing (val),
1603 value_offset (val),
1604 val);
1605
1606 return bit_val;
1607}
1608
c6d81124
PA
1609/* Allocate a dummy location and add it to B, which must be a software
1610 watchpoint. This is required because even if a software watchpoint
1611 is not watching any memory, bpstat_stop_status requires a location
1612 to be able to report stops. */
1613
1614static void
1615software_watchpoint_add_no_memory_location (struct breakpoint *b,
1616 struct program_space *pspace)
1617{
1618 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1619
1620 b->loc = allocate_bp_location (b);
1621 b->loc->pspace = pspace;
1622 b->loc->address = -1;
1623 b->loc->length = -1;
1624}
1625
1626/* Returns true if B is a software watchpoint that is not watching any
1627 memory (e.g., "watch $pc"). */
1628
1629static int
1630is_no_memory_software_watchpoint (struct breakpoint *b)
1631{
1632 return (b->type == bp_watchpoint
1633 && b->loc != NULL
1634 && b->loc->next == NULL
1635 && b->loc->address == -1
1636 && b->loc->length == -1);
1637}
1638
567e1b4e
JB
1639/* Assuming that B is a watchpoint:
1640 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1641 - Evaluate expression and store the result in B->val
567e1b4e
JB
1642 - Evaluate the condition if there is one, and store the result
1643 in b->loc->cond.
a5606eee
VP
1644 - Update the list of values that must be watched in B->loc.
1645
4a64f543
MS
1646 If the watchpoint disposition is disp_del_at_next_stop, then do
1647 nothing. If this is local watchpoint that is out of scope, delete
1648 it.
1649
1650 Even with `set breakpoint always-inserted on' the watchpoints are
1651 removed + inserted on each stop here. Normal breakpoints must
1652 never be removed because they might be missed by a running thread
1653 when debugging in non-stop mode. On the other hand, hardware
1654 watchpoints (is_hardware_watchpoint; processed here) are specific
1655 to each LWP since they are stored in each LWP's hardware debug
1656 registers. Therefore, such LWP must be stopped first in order to
1657 be able to modify its hardware watchpoints.
1658
1659 Hardware watchpoints must be reset exactly once after being
1660 presented to the user. It cannot be done sooner, because it would
1661 reset the data used to present the watchpoint hit to the user. And
1662 it must not be done later because it could display the same single
1663 watchpoint hit during multiple GDB stops. Note that the latter is
1664 relevant only to the hardware watchpoint types bp_read_watchpoint
1665 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1666 not user-visible - its hit is suppressed if the memory content has
1667 not changed.
1668
1669 The following constraints influence the location where we can reset
1670 hardware watchpoints:
1671
1672 * target_stopped_by_watchpoint and target_stopped_data_address are
1673 called several times when GDB stops.
1674
1675 [linux]
1676 * Multiple hardware watchpoints can be hit at the same time,
1677 causing GDB to stop. GDB only presents one hardware watchpoint
1678 hit at a time as the reason for stopping, and all the other hits
1679 are presented later, one after the other, each time the user
1680 requests the execution to be resumed. Execution is not resumed
1681 for the threads still having pending hit event stored in
1682 LWP_INFO->STATUS. While the watchpoint is already removed from
1683 the inferior on the first stop the thread hit event is kept being
1684 reported from its cached value by linux_nat_stopped_data_address
1685 until the real thread resume happens after the watchpoint gets
1686 presented and thus its LWP_INFO->STATUS gets reset.
1687
1688 Therefore the hardware watchpoint hit can get safely reset on the
1689 watchpoint removal from inferior. */
a79d3c27 1690
b40ce68a 1691static void
3a5c3e22 1692update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1693{
a5606eee 1694 int within_current_scope;
a5606eee 1695 struct frame_id saved_frame_id;
66076460 1696 int frame_saved;
a5606eee 1697
f6bc2008
PA
1698 /* If this is a local watchpoint, we only want to check if the
1699 watchpoint frame is in scope if the current thread is the thread
1700 that was used to create the watchpoint. */
1701 if (!watchpoint_in_thread_scope (b))
1702 return;
1703
c1fc2657 1704 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1705 return;
1706
66076460 1707 frame_saved = 0;
a5606eee
VP
1708
1709 /* Determine if the watchpoint is within scope. */
1710 if (b->exp_valid_block == NULL)
1711 within_current_scope = 1;
1712 else
1713 {
b5db5dfc
UW
1714 struct frame_info *fi = get_current_frame ();
1715 struct gdbarch *frame_arch = get_frame_arch (fi);
1716 CORE_ADDR frame_pc = get_frame_pc (fi);
1717
c9cf6e20
MG
1718 /* If we're at a point where the stack has been destroyed
1719 (e.g. in a function epilogue), unwinding may not work
1720 properly. Do not attempt to recreate locations at this
b5db5dfc 1721 point. See similar comments in watchpoint_check. */
c9cf6e20 1722 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1723 return;
66076460
DJ
1724
1725 /* Save the current frame's ID so we can restore it after
1726 evaluating the watchpoint expression on its own frame. */
1727 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1728 took a frame parameter, so that we didn't have to change the
1729 selected frame. */
1730 frame_saved = 1;
1731 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1732
a5606eee
VP
1733 fi = frame_find_by_id (b->watchpoint_frame);
1734 within_current_scope = (fi != NULL);
1735 if (within_current_scope)
1736 select_frame (fi);
1737 }
1738
b5db5dfc
UW
1739 /* We don't free locations. They are stored in the bp_location array
1740 and update_global_location_list will eventually delete them and
1741 remove breakpoints if needed. */
c1fc2657 1742 b->loc = NULL;
b5db5dfc 1743
a5606eee
VP
1744 if (within_current_scope && reparse)
1745 {
bbc13ae3 1746 const char *s;
d63d0675 1747
4d01a485 1748 b->exp.reset ();
d63d0675 1749 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1750 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1751 /* If the meaning of expression itself changed, the old value is
1752 no longer relevant. We don't want to report a watchpoint hit
1753 to the user when the old value and the new value may actually
1754 be completely different objects. */
1755 value_free (b->val);
fa4727a6
DJ
1756 b->val = NULL;
1757 b->val_valid = 0;
60e1c644
PA
1758
1759 /* Note that unlike with breakpoints, the watchpoint's condition
1760 expression is stored in the breakpoint object, not in the
1761 locations (re)created below. */
c1fc2657 1762 if (b->cond_string != NULL)
60e1c644 1763 {
4d01a485 1764 b->cond_exp.reset ();
60e1c644 1765
c1fc2657 1766 s = b->cond_string;
1bb9788d 1767 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1768 }
a5606eee 1769 }
a5606eee
VP
1770
1771 /* If we failed to parse the expression, for example because
1772 it refers to a global variable in a not-yet-loaded shared library,
1773 don't try to insert watchpoint. We don't automatically delete
1774 such watchpoint, though, since failure to parse expression
1775 is different from out-of-scope watchpoint. */
e8369a73 1776 if (!target_has_execution)
2d134ed3
PA
1777 {
1778 /* Without execution, memory can't change. No use to try and
1779 set watchpoint locations. The watchpoint will be reset when
1780 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1781 if (!can_use_hw_watchpoints)
1782 {
c1fc2657
SM
1783 if (b->ops->works_in_software_mode (b))
1784 b->type = bp_watchpoint;
e8369a73 1785 else
638aa5a1
AB
1786 error (_("Can't set read/access watchpoint when "
1787 "hardware watchpoints are disabled."));
e8369a73 1788 }
2d134ed3
PA
1789 }
1790 else if (within_current_scope && b->exp)
a5606eee 1791 {
0cf6dd15 1792 int pc = 0;
fa4727a6 1793 struct value *val_chain, *v, *result, *next;
2d134ed3 1794 struct program_space *frame_pspace;
a5606eee 1795
4d01a485 1796 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1797
a5606eee
VP
1798 /* Avoid setting b->val if it's already set. The meaning of
1799 b->val is 'the last value' user saw, and we should update
1800 it only if we reported that last value to user. As it
9c06b0b4
TJB
1801 happens, the code that reports it updates b->val directly.
1802 We don't keep track of the memory value for masked
1803 watchpoints. */
c1fc2657 1804 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1805 {
bb9d5f81
PP
1806 if (b->val_bitsize != 0)
1807 {
1808 v = extract_bitfield_from_watchpoint_value (b, v);
1809 if (v != NULL)
1810 release_value (v);
1811 }
fa4727a6
DJ
1812 b->val = v;
1813 b->val_valid = 1;
1814 }
a5606eee 1815
2d134ed3
PA
1816 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1817
a5606eee 1818 /* Look at each value on the value chain. */
9fa40276 1819 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1820 {
1821 /* If it's a memory location, and GDB actually needed
1822 its contents to evaluate the expression, then we
fa4727a6
DJ
1823 must watch it. If the first value returned is
1824 still lazy, that means an error occurred reading it;
1825 watch it anyway in case it becomes readable. */
a5606eee 1826 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1827 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1828 {
1829 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1830
a5606eee
VP
1831 /* We only watch structs and arrays if user asked
1832 for it explicitly, never if they just happen to
1833 appear in the middle of some value chain. */
fa4727a6 1834 if (v == result
a5606eee
VP
1835 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1836 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1837 {
1838 CORE_ADDR addr;
f486487f 1839 enum target_hw_bp_type type;
a5606eee 1840 struct bp_location *loc, **tmp;
bb9d5f81
PP
1841 int bitpos = 0, bitsize = 0;
1842
1843 if (value_bitsize (v) != 0)
1844 {
1845 /* Extract the bit parameters out from the bitfield
1846 sub-expression. */
1847 bitpos = value_bitpos (v);
1848 bitsize = value_bitsize (v);
1849 }
1850 else if (v == result && b->val_bitsize != 0)
1851 {
1852 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1853 lvalue whose bit parameters are saved in the fields
1854 VAL_BITPOS and VAL_BITSIZE. */
1855 bitpos = b->val_bitpos;
1856 bitsize = b->val_bitsize;
1857 }
a5606eee 1858
42ae5230 1859 addr = value_address (v);
bb9d5f81
PP
1860 if (bitsize != 0)
1861 {
1862 /* Skip the bytes that don't contain the bitfield. */
1863 addr += bitpos / 8;
1864 }
1865
a5606eee 1866 type = hw_write;
c1fc2657 1867 if (b->type == bp_read_watchpoint)
a5606eee 1868 type = hw_read;
c1fc2657 1869 else if (b->type == bp_access_watchpoint)
a5606eee 1870 type = hw_access;
3a5c3e22 1871
c1fc2657
SM
1872 loc = allocate_bp_location (b);
1873 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1874 ;
1875 *tmp = loc;
a6d9a66e 1876 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1877
1878 loc->pspace = frame_pspace;
a5606eee 1879 loc->address = addr;
bb9d5f81
PP
1880
1881 if (bitsize != 0)
1882 {
1883 /* Just cover the bytes that make up the bitfield. */
1884 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1885 }
1886 else
1887 loc->length = TYPE_LENGTH (value_type (v));
1888
a5606eee
VP
1889 loc->watchpoint_type = type;
1890 }
1891 }
9fa40276
TJB
1892 }
1893
1894 /* Change the type of breakpoint between hardware assisted or
1895 an ordinary watchpoint depending on the hardware support
1896 and free hardware slots. REPARSE is set when the inferior
1897 is started. */
a9634178 1898 if (reparse)
9fa40276 1899 {
e09342b5 1900 int reg_cnt;
9fa40276
TJB
1901 enum bp_loc_type loc_type;
1902 struct bp_location *bl;
a5606eee 1903
a9634178 1904 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1905
1906 if (reg_cnt)
9fa40276
TJB
1907 {
1908 int i, target_resources_ok, other_type_used;
a1398e0c 1909 enum bptype type;
9fa40276 1910
a9634178
TJB
1911 /* Use an exact watchpoint when there's only one memory region to be
1912 watched, and only one debug register is needed to watch it. */
1913 b->exact = target_exact_watchpoints && reg_cnt == 1;
1914
9fa40276 1915 /* We need to determine how many resources are already
e09342b5
TJB
1916 used for all other hardware watchpoints plus this one
1917 to see if we still have enough resources to also fit
a1398e0c
PA
1918 this watchpoint in as well. */
1919
1920 /* If this is a software watchpoint, we try to turn it
1921 to a hardware one -- count resources as if B was of
1922 hardware watchpoint type. */
c1fc2657 1923 type = b->type;
a1398e0c
PA
1924 if (type == bp_watchpoint)
1925 type = bp_hardware_watchpoint;
1926
1927 /* This watchpoint may or may not have been placed on
1928 the list yet at this point (it won't be in the list
1929 if we're trying to create it for the first time,
1930 through watch_command), so always account for it
1931 manually. */
1932
1933 /* Count resources used by all watchpoints except B. */
c1fc2657 1934 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
1935
1936 /* Add in the resources needed for B. */
c1fc2657 1937 i += hw_watchpoint_use_count (b);
a1398e0c
PA
1938
1939 target_resources_ok
1940 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1941 if (target_resources_ok <= 0)
a9634178 1942 {
c1fc2657 1943 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
1944
1945 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1946 error (_("Target does not support this type of "
1947 "hardware watchpoint."));
9c06b0b4
TJB
1948 else if (target_resources_ok < 0 && !sw_mode)
1949 error (_("There are not enough available hardware "
1950 "resources for this watchpoint."));
a1398e0c
PA
1951
1952 /* Downgrade to software watchpoint. */
c1fc2657 1953 b->type = bp_watchpoint;
a1398e0c
PA
1954 }
1955 else
1956 {
1957 /* If this was a software watchpoint, we've just
1958 found we have enough resources to turn it to a
1959 hardware watchpoint. Otherwise, this is a
1960 nop. */
c1fc2657 1961 b->type = type;
a9634178 1962 }
9fa40276 1963 }
c1fc2657 1964 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
1965 {
1966 if (!can_use_hw_watchpoints)
1967 error (_("Can't set read/access watchpoint when "
1968 "hardware watchpoints are disabled."));
1969 else
1970 error (_("Expression cannot be implemented with "
1971 "read/access watchpoint."));
1972 }
9fa40276 1973 else
c1fc2657 1974 b->type = bp_watchpoint;
9fa40276 1975
c1fc2657 1976 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 1977 : bp_loc_hardware_watchpoint);
c1fc2657 1978 for (bl = b->loc; bl; bl = bl->next)
9fa40276
TJB
1979 bl->loc_type = loc_type;
1980 }
1981
1982 for (v = val_chain; v; v = next)
1983 {
a5606eee
VP
1984 next = value_next (v);
1985 if (v != b->val)
1986 value_free (v);
1987 }
1988
c7437ca6
PA
1989 /* If a software watchpoint is not watching any memory, then the
1990 above left it without any location set up. But,
1991 bpstat_stop_status requires a location to be able to report
1992 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
1993 if (b->type == bp_watchpoint && b->loc == NULL)
1994 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
1995 }
1996 else if (!within_current_scope)
7270d8f2 1997 {
ac74f770
MS
1998 printf_filtered (_("\
1999Watchpoint %d deleted because the program has left the block\n\
2000in which its expression is valid.\n"),
c1fc2657 2001 b->number);
d0fb5eae 2002 watchpoint_del_at_next_stop (b);
7270d8f2 2003 }
a5606eee
VP
2004
2005 /* Restore the selected frame. */
66076460
DJ
2006 if (frame_saved)
2007 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2008}
2009
a5606eee 2010
74960c60 2011/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2012 inserted in the inferior. We don't differentiate the type of BL's owner
2013 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2014 breakpoint_ops is not defined, because in insert_bp_location,
2015 tracepoint's insert_location will not be called. */
74960c60 2016static int
35df4500 2017should_be_inserted (struct bp_location *bl)
74960c60 2018{
35df4500 2019 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2020 return 0;
2021
35df4500 2022 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2023 return 0;
2024
35df4500 2025 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2026 return 0;
2027
f8eba3c6
TT
2028 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2029 return 0;
2030
56710373
PA
2031 /* This is set for example, when we're attached to the parent of a
2032 vfork, and have detached from the child. The child is running
2033 free, and we expect it to do an exec or exit, at which point the
2034 OS makes the parent schedulable again (and the target reports
2035 that the vfork is done). Until the child is done with the shared
2036 memory region, do not insert breakpoints in the parent, otherwise
2037 the child could still trip on the parent's breakpoints. Since
2038 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2039 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2040 return 0;
2041
31e77af2 2042 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2043 location, except if the breakpoint is a single-step breakpoint,
2044 and the breakpoint's thread is the thread which is stepping past
2045 a breakpoint. */
31e77af2
PA
2046 if ((bl->loc_type == bp_loc_software_breakpoint
2047 || bl->loc_type == bp_loc_hardware_breakpoint)
2048 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2049 bl->address)
2050 /* The single-step breakpoint may be inserted at the location
2051 we're trying to step if the instruction branches to itself.
2052 However, the instruction won't be executed at all and it may
2053 break the semantics of the instruction, for example, the
2054 instruction is a conditional branch or updates some flags.
2055 We can't fix it unless GDB is able to emulate the instruction
2056 or switch to displaced stepping. */
2057 && !(bl->owner->type == bp_single_step
2058 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2059 {
2060 if (debug_infrun)
2061 {
2062 fprintf_unfiltered (gdb_stdlog,
2063 "infrun: skipping breakpoint: "
2064 "stepping past insn at: %s\n",
2065 paddress (bl->gdbarch, bl->address));
2066 }
2067 return 0;
2068 }
31e77af2 2069
963f9c80
PA
2070 /* Don't insert watchpoints if we're trying to step past the
2071 instruction that triggered one. */
2072 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2073 && stepping_past_nonsteppable_watchpoint ())
2074 {
2075 if (debug_infrun)
2076 {
2077 fprintf_unfiltered (gdb_stdlog,
2078 "infrun: stepping past non-steppable watchpoint. "
2079 "skipping watchpoint at %s:%d\n",
2080 paddress (bl->gdbarch, bl->address),
2081 bl->length);
2082 }
2083 return 0;
2084 }
2085
74960c60
VP
2086 return 1;
2087}
2088
934709f0
PW
2089/* Same as should_be_inserted but does the check assuming
2090 that the location is not duplicated. */
2091
2092static int
2093unduplicated_should_be_inserted (struct bp_location *bl)
2094{
2095 int result;
2096 const int save_duplicate = bl->duplicate;
2097
2098 bl->duplicate = 0;
2099 result = should_be_inserted (bl);
2100 bl->duplicate = save_duplicate;
2101 return result;
2102}
2103
b775012e
LM
2104/* Parses a conditional described by an expression COND into an
2105 agent expression bytecode suitable for evaluation
2106 by the bytecode interpreter. Return NULL if there was
2107 any error during parsing. */
2108
833177a4 2109static agent_expr_up
b775012e
LM
2110parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2111{
833177a4 2112 if (cond == NULL)
b775012e
LM
2113 return NULL;
2114
833177a4
PA
2115 agent_expr_up aexpr;
2116
b775012e
LM
2117 /* We don't want to stop processing, so catch any errors
2118 that may show up. */
492d29ea 2119 TRY
b775012e 2120 {
036e657b 2121 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2122 }
2123
492d29ea 2124 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2125 {
2126 /* If we got here, it means the condition could not be parsed to a valid
2127 bytecode expression and thus can't be evaluated on the target's side.
2128 It's no use iterating through the conditions. */
b775012e 2129 }
492d29ea 2130 END_CATCH
b775012e
LM
2131
2132 /* We have a valid agent expression. */
2133 return aexpr;
2134}
2135
2136/* Based on location BL, create a list of breakpoint conditions to be
2137 passed on to the target. If we have duplicated locations with different
2138 conditions, we will add such conditions to the list. The idea is that the
2139 target will evaluate the list of conditions and will only notify GDB when
2140 one of them is true. */
2141
2142static void
2143build_target_condition_list (struct bp_location *bl)
2144{
2145 struct bp_location **locp = NULL, **loc2p;
2146 int null_condition_or_parse_error = 0;
2147 int modified = bl->needs_update;
2148 struct bp_location *loc;
2149
8b4f3082 2150 /* Release conditions left over from a previous insert. */
3cde5c42 2151 bl->target_info.conditions.clear ();
8b4f3082 2152
b775012e
LM
2153 /* This is only meaningful if the target is
2154 evaluating conditions and if the user has
2155 opted for condition evaluation on the target's
2156 side. */
2157 if (gdb_evaluates_breakpoint_condition_p ()
2158 || !target_supports_evaluation_of_breakpoint_conditions ())
2159 return;
2160
2161 /* Do a first pass to check for locations with no assigned
2162 conditions or conditions that fail to parse to a valid agent expression
2163 bytecode. If any of these happen, then it's no use to send conditions
2164 to the target since this location will always trigger and generate a
2165 response back to GDB. */
2166 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2167 {
2168 loc = (*loc2p);
2169 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2170 {
2171 if (modified)
2172 {
b775012e
LM
2173 /* Re-parse the conditions since something changed. In that
2174 case we already freed the condition bytecodes (see
2175 force_breakpoint_reinsertion). We just
2176 need to parse the condition to bytecodes again. */
833177a4
PA
2177 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2178 loc->cond.get ());
b775012e
LM
2179 }
2180
2181 /* If we have a NULL bytecode expression, it means something
2182 went wrong or we have a null condition expression. */
2183 if (!loc->cond_bytecode)
2184 {
2185 null_condition_or_parse_error = 1;
2186 break;
2187 }
2188 }
2189 }
2190
2191 /* If any of these happened, it means we will have to evaluate the conditions
2192 for the location's address on gdb's side. It is no use keeping bytecodes
2193 for all the other duplicate locations, thus we free all of them here.
2194
2195 This is so we have a finer control over which locations' conditions are
2196 being evaluated by GDB or the remote stub. */
2197 if (null_condition_or_parse_error)
2198 {
2199 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2200 {
2201 loc = (*loc2p);
2202 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2203 {
2204 /* Only go as far as the first NULL bytecode is
2205 located. */
2206 if (!loc->cond_bytecode)
2207 return;
2208
833177a4 2209 loc->cond_bytecode.reset ();
b775012e
LM
2210 }
2211 }
2212 }
2213
2214 /* No NULL conditions or failed bytecode generation. Build a condition list
2215 for this location's address. */
2216 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2217 {
2218 loc = (*loc2p);
2219 if (loc->cond
2220 && is_breakpoint (loc->owner)
2221 && loc->pspace->num == bl->pspace->num
2222 && loc->owner->enable_state == bp_enabled
2223 && loc->enabled)
3cde5c42
PA
2224 {
2225 /* Add the condition to the vector. This will be used later
2226 to send the conditions to the target. */
2227 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2228 }
b775012e
LM
2229 }
2230
2231 return;
2232}
2233
d3ce09f5
SS
2234/* Parses a command described by string CMD into an agent expression
2235 bytecode suitable for evaluation by the bytecode interpreter.
2236 Return NULL if there was any error during parsing. */
2237
833177a4 2238static agent_expr_up
d3ce09f5
SS
2239parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2240{
2241 struct cleanup *old_cleanups = 0;
4d01a485 2242 struct expression **argvec;
bbc13ae3
KS
2243 const char *cmdrest;
2244 const char *format_start, *format_end;
d3ce09f5
SS
2245 struct format_piece *fpieces;
2246 int nargs;
2247 struct gdbarch *gdbarch = get_current_arch ();
2248
833177a4 2249 if (cmd == NULL)
d3ce09f5
SS
2250 return NULL;
2251
2252 cmdrest = cmd;
2253
2254 if (*cmdrest == ',')
2255 ++cmdrest;
f1735a53 2256 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2257
2258 if (*cmdrest++ != '"')
2259 error (_("No format string following the location"));
2260
2261 format_start = cmdrest;
2262
2263 fpieces = parse_format_string (&cmdrest);
2264
2265 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2266
2267 format_end = cmdrest;
2268
2269 if (*cmdrest++ != '"')
2270 error (_("Bad format string, non-terminated '\"'."));
2271
f1735a53 2272 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2273
2274 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2275 error (_("Invalid argument syntax"));
2276
2277 if (*cmdrest == ',')
2278 cmdrest++;
f1735a53 2279 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2280
2281 /* For each argument, make an expression. */
2282
2283 argvec = (struct expression **) alloca (strlen (cmd)
2284 * sizeof (struct expression *));
2285
2286 nargs = 0;
2287 while (*cmdrest != '\0')
2288 {
bbc13ae3 2289 const char *cmd1;
d3ce09f5
SS
2290
2291 cmd1 = cmdrest;
4d01a485
PA
2292 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2293 argvec[nargs++] = expr.release ();
d3ce09f5
SS
2294 cmdrest = cmd1;
2295 if (*cmdrest == ',')
2296 ++cmdrest;
2297 }
2298
833177a4
PA
2299 agent_expr_up aexpr;
2300
d3ce09f5
SS
2301 /* We don't want to stop processing, so catch any errors
2302 that may show up. */
492d29ea 2303 TRY
d3ce09f5 2304 {
036e657b
JB
2305 aexpr = gen_printf (scope, gdbarch, 0, 0,
2306 format_start, format_end - format_start,
2307 fpieces, nargs, argvec);
d3ce09f5 2308 }
492d29ea 2309 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2310 {
2311 /* If we got here, it means the command could not be parsed to a valid
2312 bytecode expression and thus can't be evaluated on the target's side.
2313 It's no use iterating through the other commands. */
d3ce09f5 2314 }
492d29ea
PA
2315 END_CATCH
2316
2317 do_cleanups (old_cleanups);
d3ce09f5 2318
d3ce09f5
SS
2319 /* We have a valid agent expression, return it. */
2320 return aexpr;
2321}
2322
2323/* Based on location BL, create a list of breakpoint commands to be
2324 passed on to the target. If we have duplicated locations with
2325 different commands, we will add any such to the list. */
2326
2327static void
2328build_target_command_list (struct bp_location *bl)
2329{
2330 struct bp_location **locp = NULL, **loc2p;
2331 int null_command_or_parse_error = 0;
2332 int modified = bl->needs_update;
2333 struct bp_location *loc;
2334
3cde5c42
PA
2335 /* Clear commands left over from a previous insert. */
2336 bl->target_info.tcommands.clear ();
8b4f3082 2337
41fac0cf 2338 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2339 return;
2340
41fac0cf
PA
2341 /* For now, limit to agent-style dprintf breakpoints. */
2342 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2343 return;
2344
41fac0cf
PA
2345 /* For now, if we have any duplicate location that isn't a dprintf,
2346 don't install the target-side commands, as that would make the
2347 breakpoint not be reported to the core, and we'd lose
2348 control. */
2349 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2350 {
2351 loc = (*loc2p);
2352 if (is_breakpoint (loc->owner)
2353 && loc->pspace->num == bl->pspace->num
2354 && loc->owner->type != bp_dprintf)
2355 return;
2356 }
2357
d3ce09f5
SS
2358 /* Do a first pass to check for locations with no assigned
2359 conditions or conditions that fail to parse to a valid agent expression
2360 bytecode. If any of these happen, then it's no use to send conditions
2361 to the target since this location will always trigger and generate a
2362 response back to GDB. */
2363 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2364 {
2365 loc = (*loc2p);
2366 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2367 {
2368 if (modified)
2369 {
d3ce09f5
SS
2370 /* Re-parse the commands since something changed. In that
2371 case we already freed the command bytecodes (see
2372 force_breakpoint_reinsertion). We just
2373 need to parse the command to bytecodes again. */
833177a4
PA
2374 loc->cmd_bytecode
2375 = parse_cmd_to_aexpr (bl->address,
2376 loc->owner->extra_string);
d3ce09f5
SS
2377 }
2378
2379 /* If we have a NULL bytecode expression, it means something
2380 went wrong or we have a null command expression. */
2381 if (!loc->cmd_bytecode)
2382 {
2383 null_command_or_parse_error = 1;
2384 break;
2385 }
2386 }
2387 }
2388
2389 /* If anything failed, then we're not doing target-side commands,
2390 and so clean up. */
2391 if (null_command_or_parse_error)
2392 {
2393 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2394 {
2395 loc = (*loc2p);
2396 if (is_breakpoint (loc->owner)
2397 && loc->pspace->num == bl->pspace->num)
2398 {
2399 /* Only go as far as the first NULL bytecode is
2400 located. */
40fb6c5e 2401 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2402 return;
2403
833177a4 2404 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2405 }
2406 }
2407 }
2408
2409 /* No NULL commands or failed bytecode generation. Build a command list
2410 for this location's address. */
2411 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2412 {
2413 loc = (*loc2p);
2414 if (loc->owner->extra_string
2415 && is_breakpoint (loc->owner)
2416 && loc->pspace->num == bl->pspace->num
2417 && loc->owner->enable_state == bp_enabled
2418 && loc->enabled)
3cde5c42
PA
2419 {
2420 /* Add the command to the vector. This will be used later
2421 to send the commands to the target. */
2422 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2423 }
d3ce09f5
SS
2424 }
2425
2426 bl->target_info.persist = 0;
2427 /* Maybe flag this location as persistent. */
2428 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2429 bl->target_info.persist = 1;
2430}
2431
833b7ab5
YQ
2432/* Return the kind of breakpoint on address *ADDR. Get the kind
2433 of breakpoint according to ADDR except single-step breakpoint.
2434 Get the kind of single-step breakpoint according to the current
2435 registers state. */
cd6c3b4f
YQ
2436
2437static int
2438breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2439{
833b7ab5
YQ
2440 if (bl->owner->type == bp_single_step)
2441 {
2442 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2443 struct regcache *regcache;
2444
2445 regcache = get_thread_regcache (thr->ptid);
2446
2447 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2448 regcache, addr);
2449 }
2450 else
2451 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2452}
2453
35df4500
TJB
2454/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2455 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2456 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2457 Returns 0 for success, 1 if the bp_location type is not supported or
2458 -1 for failure.
879bfdc2 2459
4a64f543
MS
2460 NOTE drow/2003-09-09: This routine could be broken down to an
2461 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2462static int
35df4500 2463insert_bp_location (struct bp_location *bl,
26bb91f3 2464 struct ui_file *tmp_error_stream,
3fbb6ffa 2465 int *disabled_breaks,
dd61ec5c
MW
2466 int *hw_breakpoint_error,
2467 int *hw_bp_error_explained_already)
879bfdc2 2468{
0000e5cc
PA
2469 enum errors bp_err = GDB_NO_ERROR;
2470 const char *bp_err_message = NULL;
879bfdc2 2471
b775012e 2472 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2473 return 0;
2474
35c63cd8
JB
2475 /* Note we don't initialize bl->target_info, as that wipes out
2476 the breakpoint location's shadow_contents if the breakpoint
2477 is still inserted at that location. This in turn breaks
2478 target_read_memory which depends on these buffers when
2479 a memory read is requested at the breakpoint location:
2480 Once the target_info has been wiped, we fail to see that
2481 we have a breakpoint inserted at that address and thus
2482 read the breakpoint instead of returning the data saved in
2483 the breakpoint location's shadow contents. */
0d5ed153 2484 bl->target_info.reqstd_address = bl->address;
35df4500 2485 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2486 bl->target_info.length = bl->length;
8181d85f 2487
b775012e
LM
2488 /* When working with target-side conditions, we must pass all the conditions
2489 for the same breakpoint address down to the target since GDB will not
2490 insert those locations. With a list of breakpoint conditions, the target
2491 can decide when to stop and notify GDB. */
2492
2493 if (is_breakpoint (bl->owner))
2494 {
2495 build_target_condition_list (bl);
d3ce09f5
SS
2496 build_target_command_list (bl);
2497 /* Reset the modification marker. */
b775012e
LM
2498 bl->needs_update = 0;
2499 }
2500
35df4500
TJB
2501 if (bl->loc_type == bp_loc_software_breakpoint
2502 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2503 {
35df4500 2504 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2505 {
2506 /* If the explicitly specified breakpoint type
2507 is not hardware breakpoint, check the memory map to see
2508 if the breakpoint address is in read only memory or not.
4a64f543 2509
765dc015
VP
2510 Two important cases are:
2511 - location type is not hardware breakpoint, memory
2512 is readonly. We change the type of the location to
2513 hardware breakpoint.
4a64f543
MS
2514 - location type is hardware breakpoint, memory is
2515 read-write. This means we've previously made the
2516 location hardware one, but then the memory map changed,
2517 so we undo.
765dc015 2518
4a64f543
MS
2519 When breakpoints are removed, remove_breakpoints will use
2520 location types we've just set here, the only possible
2521 problem is that memory map has changed during running
2522 program, but it's not going to work anyway with current
2523 gdb. */
765dc015 2524 struct mem_region *mr
0d5ed153 2525 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2526
2527 if (mr)
2528 {
2529 if (automatic_hardware_breakpoints)
2530 {
765dc015
VP
2531 enum bp_loc_type new_type;
2532
2533 if (mr->attrib.mode != MEM_RW)
2534 new_type = bp_loc_hardware_breakpoint;
2535 else
2536 new_type = bp_loc_software_breakpoint;
2537
35df4500 2538 if (new_type != bl->loc_type)
765dc015
VP
2539 {
2540 static int said = 0;
cc59ec59 2541
35df4500 2542 bl->loc_type = new_type;
765dc015
VP
2543 if (!said)
2544 {
3e43a32a
MS
2545 fprintf_filtered (gdb_stdout,
2546 _("Note: automatically using "
2547 "hardware breakpoints for "
2548 "read-only addresses.\n"));
765dc015
VP
2549 said = 1;
2550 }
2551 }
2552 }
35df4500 2553 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2554 && mr->attrib.mode != MEM_RW)
2555 {
2556 fprintf_unfiltered (tmp_error_stream,
2557 _("Cannot insert breakpoint %d.\n"
2558 "Cannot set software breakpoint "
2559 "at read-only address %s\n"),
2560 bl->owner->number,
2561 paddress (bl->gdbarch, bl->address));
2562 return 1;
2563 }
765dc015
VP
2564 }
2565 }
2566
879bfdc2
DJ
2567 /* First check to see if we have to handle an overlay. */
2568 if (overlay_debugging == ovly_off
35df4500
TJB
2569 || bl->section == NULL
2570 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2571 {
2572 /* No overlay handling: just set the breakpoint. */
492d29ea 2573 TRY
dd61ec5c 2574 {
0000e5cc
PA
2575 int val;
2576
dd61ec5c 2577 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2578 if (val)
2579 bp_err = GENERIC_ERROR;
dd61ec5c 2580 }
492d29ea 2581 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2582 {
0000e5cc
PA
2583 bp_err = e.error;
2584 bp_err_message = e.message;
dd61ec5c 2585 }
492d29ea 2586 END_CATCH
879bfdc2
DJ
2587 }
2588 else
2589 {
4a64f543 2590 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2591 Shall we set a breakpoint at the LMA? */
2592 if (!overlay_events_enabled)
2593 {
2594 /* Yes -- overlay event support is not active,
2595 so we must try to set a breakpoint at the LMA.
2596 This will not work for a hardware breakpoint. */
35df4500 2597 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2598 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2599 bl->owner->number);
879bfdc2
DJ
2600 else
2601 {
35df4500
TJB
2602 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2603 bl->section);
879bfdc2 2604 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2605 bl->overlay_target_info = bl->target_info;
0d5ed153 2606 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2607
2608 /* No overlay handling: just set the breakpoint. */
492d29ea 2609 TRY
0000e5cc
PA
2610 {
2611 int val;
2612
579c6ad9 2613 bl->overlay_target_info.kind
cd6c3b4f
YQ
2614 = breakpoint_kind (bl, &addr);
2615 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2616 val = target_insert_breakpoint (bl->gdbarch,
2617 &bl->overlay_target_info);
2618 if (val)
2619 bp_err = GENERIC_ERROR;
2620 }
492d29ea 2621 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2622 {
2623 bp_err = e.error;
2624 bp_err_message = e.message;
2625 }
492d29ea 2626 END_CATCH
0000e5cc
PA
2627
2628 if (bp_err != GDB_NO_ERROR)
99361f52 2629 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2630 "Overlay breakpoint %d "
2631 "failed: in ROM?\n",
35df4500 2632 bl->owner->number);
879bfdc2
DJ
2633 }
2634 }
2635 /* Shall we set a breakpoint at the VMA? */
35df4500 2636 if (section_is_mapped (bl->section))
879bfdc2
DJ
2637 {
2638 /* Yes. This overlay section is mapped into memory. */
492d29ea 2639 TRY
dd61ec5c 2640 {
0000e5cc
PA
2641 int val;
2642
dd61ec5c 2643 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2644 if (val)
2645 bp_err = GENERIC_ERROR;
dd61ec5c 2646 }
492d29ea 2647 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2648 {
0000e5cc
PA
2649 bp_err = e.error;
2650 bp_err_message = e.message;
dd61ec5c 2651 }
492d29ea 2652 END_CATCH
879bfdc2
DJ
2653 }
2654 else
2655 {
2656 /* No. This breakpoint will not be inserted.
2657 No error, but do not mark the bp as 'inserted'. */
2658 return 0;
2659 }
2660 }
2661
0000e5cc 2662 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2663 {
2664 /* Can't set the breakpoint. */
0000e5cc
PA
2665
2666 /* In some cases, we might not be able to insert a
2667 breakpoint in a shared library that has already been
2668 removed, but we have not yet processed the shlib unload
2669 event. Unfortunately, some targets that implement
076855f9
PA
2670 breakpoint insertion themselves can't tell why the
2671 breakpoint insertion failed (e.g., the remote target
2672 doesn't define error codes), so we must treat generic
2673 errors as memory errors. */
0000e5cc 2674 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2675 && bl->loc_type == bp_loc_software_breakpoint
08351840 2676 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2677 || shared_objfile_contains_address_p (bl->pspace,
2678 bl->address)))
879bfdc2 2679 {
4a64f543 2680 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2681 bl->shlib_disabled = 1;
8d3788bd 2682 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2683 if (!*disabled_breaks)
2684 {
2685 fprintf_unfiltered (tmp_error_stream,
2686 "Cannot insert breakpoint %d.\n",
2687 bl->owner->number);
2688 fprintf_unfiltered (tmp_error_stream,
2689 "Temporarily disabling shared "
2690 "library breakpoints:\n");
2691 }
2692 *disabled_breaks = 1;
879bfdc2 2693 fprintf_unfiltered (tmp_error_stream,
35df4500 2694 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2695 return 0;
879bfdc2
DJ
2696 }
2697 else
879bfdc2 2698 {
35df4500 2699 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2700 {
0000e5cc
PA
2701 *hw_breakpoint_error = 1;
2702 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2703 fprintf_unfiltered (tmp_error_stream,
2704 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2705 bl->owner->number, bp_err_message ? ":" : ".\n");
2706 if (bp_err_message != NULL)
2707 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2708 }
2709 else
2710 {
0000e5cc
PA
2711 if (bp_err_message == NULL)
2712 {
1ccbe998 2713 std::string message
0000e5cc
PA
2714 = memory_error_message (TARGET_XFER_E_IO,
2715 bl->gdbarch, bl->address);
0000e5cc
PA
2716
2717 fprintf_unfiltered (tmp_error_stream,
2718 "Cannot insert breakpoint %d.\n"
2719 "%s\n",
1ccbe998 2720 bl->owner->number, message.c_str ());
0000e5cc
PA
2721 }
2722 else
2723 {
2724 fprintf_unfiltered (tmp_error_stream,
2725 "Cannot insert breakpoint %d: %s\n",
2726 bl->owner->number,
2727 bp_err_message);
2728 }
879bfdc2 2729 }
0000e5cc 2730 return 1;
879bfdc2
DJ
2731
2732 }
2733 }
2734 else
35df4500 2735 bl->inserted = 1;
879bfdc2 2736
0000e5cc 2737 return 0;
879bfdc2
DJ
2738 }
2739
35df4500 2740 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2741 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2742 watchpoints. It's not clear that it's necessary... */
35df4500 2743 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2744 {
0000e5cc
PA
2745 int val;
2746
77b06cd7
TJB
2747 gdb_assert (bl->owner->ops != NULL
2748 && bl->owner->ops->insert_location != NULL);
2749
2750 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2751
2752 /* If trying to set a read-watchpoint, and it turns out it's not
2753 supported, try emulating one with an access watchpoint. */
35df4500 2754 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2755 {
2756 struct bp_location *loc, **loc_temp;
2757
2758 /* But don't try to insert it, if there's already another
2759 hw_access location that would be considered a duplicate
2760 of this one. */
2761 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2762 if (loc != bl
85d721b8 2763 && loc->watchpoint_type == hw_access
35df4500 2764 && watchpoint_locations_match (bl, loc))
85d721b8 2765 {
35df4500
TJB
2766 bl->duplicate = 1;
2767 bl->inserted = 1;
2768 bl->target_info = loc->target_info;
2769 bl->watchpoint_type = hw_access;
85d721b8
PA
2770 val = 0;
2771 break;
2772 }
2773
2774 if (val == 1)
2775 {
77b06cd7
TJB
2776 bl->watchpoint_type = hw_access;
2777 val = bl->owner->ops->insert_location (bl);
2778
2779 if (val)
2780 /* Back to the original value. */
2781 bl->watchpoint_type = hw_read;
85d721b8
PA
2782 }
2783 }
2784
35df4500 2785 bl->inserted = (val == 0);
879bfdc2
DJ
2786 }
2787
35df4500 2788 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2789 {
0000e5cc
PA
2790 int val;
2791
77b06cd7
TJB
2792 gdb_assert (bl->owner->ops != NULL
2793 && bl->owner->ops->insert_location != NULL);
2794
2795 val = bl->owner->ops->insert_location (bl);
2796 if (val)
2797 {
2798 bl->owner->enable_state = bp_disabled;
2799
2800 if (val == 1)
2801 warning (_("\
2802Error inserting catchpoint %d: Your system does not support this type\n\
2803of catchpoint."), bl->owner->number);
2804 else
2805 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2806 }
2807
2808 bl->inserted = (val == 0);
1640b821
DJ
2809
2810 /* We've already printed an error message if there was a problem
2811 inserting this catchpoint, and we've disabled the catchpoint,
2812 so just return success. */
2813 return 0;
879bfdc2
DJ
2814 }
2815
2816 return 0;
2817}
2818
6c95b8df
PA
2819/* This function is called when program space PSPACE is about to be
2820 deleted. It takes care of updating breakpoints to not reference
2821 PSPACE anymore. */
2822
2823void
2824breakpoint_program_space_exit (struct program_space *pspace)
2825{
2826 struct breakpoint *b, *b_temp;
876fa593 2827 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2828
2829 /* Remove any breakpoint that was set through this program space. */
2830 ALL_BREAKPOINTS_SAFE (b, b_temp)
2831 {
2832 if (b->pspace == pspace)
2833 delete_breakpoint (b);
2834 }
2835
2836 /* Breakpoints set through other program spaces could have locations
2837 bound to PSPACE as well. Remove those. */
876fa593 2838 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2839 {
2840 struct bp_location *tmp;
2841
2842 if (loc->pspace == pspace)
2843 {
2bdf28a0 2844 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2845 if (loc->owner->loc == loc)
2846 loc->owner->loc = loc->next;
2847 else
2848 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2849 if (tmp->next == loc)
2850 {
2851 tmp->next = loc->next;
2852 break;
2853 }
2854 }
2855 }
2856
2857 /* Now update the global location list to permanently delete the
2858 removed locations above. */
44702360 2859 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2860}
2861
74960c60
VP
2862/* Make sure all breakpoints are inserted in inferior.
2863 Throws exception on any error.
2864 A breakpoint that is already inserted won't be inserted
2865 again, so calling this function twice is safe. */
2866void
2867insert_breakpoints (void)
2868{
2869 struct breakpoint *bpt;
2870
2871 ALL_BREAKPOINTS (bpt)
2872 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2873 {
2874 struct watchpoint *w = (struct watchpoint *) bpt;
2875
2876 update_watchpoint (w, 0 /* don't reparse. */);
2877 }
74960c60 2878
04086b45
PA
2879 /* Updating watchpoints creates new locations, so update the global
2880 location list. Explicitly tell ugll to insert locations and
2881 ignore breakpoints_always_inserted_mode. */
2882 update_global_location_list (UGLL_INSERT);
74960c60
VP
2883}
2884
20388dd6
YQ
2885/* Invoke CALLBACK for each of bp_location. */
2886
2887void
2888iterate_over_bp_locations (walk_bp_location_callback callback)
2889{
2890 struct bp_location *loc, **loc_tmp;
2891
2892 ALL_BP_LOCATIONS (loc, loc_tmp)
2893 {
2894 callback (loc, NULL);
2895 }
2896}
2897
b775012e
LM
2898/* This is used when we need to synch breakpoint conditions between GDB and the
2899 target. It is the case with deleting and disabling of breakpoints when using
2900 always-inserted mode. */
2901
2902static void
2903update_inserted_breakpoint_locations (void)
2904{
2905 struct bp_location *bl, **blp_tmp;
2906 int error_flag = 0;
2907 int val = 0;
2908 int disabled_breaks = 0;
2909 int hw_breakpoint_error = 0;
dd61ec5c 2910 int hw_bp_details_reported = 0;
b775012e 2911
d7e74731 2912 string_file tmp_error_stream;
b775012e
LM
2913
2914 /* Explicitly mark the warning -- this will only be printed if
2915 there was an error. */
d7e74731 2916 tmp_error_stream.puts ("Warning:\n");
b775012e 2917
5ed8105e 2918 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
2919
2920 ALL_BP_LOCATIONS (bl, blp_tmp)
2921 {
2922 /* We only want to update software breakpoints and hardware
2923 breakpoints. */
2924 if (!is_breakpoint (bl->owner))
2925 continue;
2926
2927 /* We only want to update locations that are already inserted
2928 and need updating. This is to avoid unwanted insertion during
2929 deletion of breakpoints. */
2930 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2931 continue;
2932
2933 switch_to_program_space_and_thread (bl->pspace);
2934
2935 /* For targets that support global breakpoints, there's no need
2936 to select an inferior to insert breakpoint to. In fact, even
2937 if we aren't attached to any process yet, we should still
2938 insert breakpoints. */
f5656ead 2939 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
2940 && ptid_equal (inferior_ptid, null_ptid))
2941 continue;
2942
d7e74731 2943 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2944 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2945 if (val)
2946 error_flag = val;
2947 }
2948
2949 if (error_flag)
2950 {
223ffa71 2951 target_terminal::ours_for_output ();
b775012e
LM
2952 error_stream (tmp_error_stream);
2953 }
b775012e
LM
2954}
2955
c30eee59 2956/* Used when starting or continuing the program. */
c906108c 2957
74960c60
VP
2958static void
2959insert_breakpoint_locations (void)
c906108c 2960{
a5606eee 2961 struct breakpoint *bpt;
35df4500 2962 struct bp_location *bl, **blp_tmp;
eacd795a 2963 int error_flag = 0;
c906108c 2964 int val = 0;
3fbb6ffa 2965 int disabled_breaks = 0;
81d0cc19 2966 int hw_breakpoint_error = 0;
dd61ec5c 2967 int hw_bp_error_explained_already = 0;
c906108c 2968
d7e74731
PA
2969 string_file tmp_error_stream;
2970
81d0cc19
GS
2971 /* Explicitly mark the warning -- this will only be printed if
2972 there was an error. */
d7e74731 2973 tmp_error_stream.puts ("Warning:\n");
6c95b8df 2974
5ed8105e 2975 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 2976
35df4500 2977 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2978 {
b775012e 2979 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2980 continue;
2981
4a64f543
MS
2982 /* There is no point inserting thread-specific breakpoints if
2983 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2984 has BL->OWNER always non-NULL. */
35df4500 2985 if (bl->owner->thread != -1
5d5658a1 2986 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
2987 continue;
2988
35df4500 2989 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2990
2991 /* For targets that support global breakpoints, there's no need
2992 to select an inferior to insert breakpoint to. In fact, even
2993 if we aren't attached to any process yet, we should still
2994 insert breakpoints. */
f5656ead 2995 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
2996 && ptid_equal (inferior_ptid, null_ptid))
2997 continue;
2998
d7e74731 2999 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 3000 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3001 if (val)
eacd795a 3002 error_flag = val;
879bfdc2 3003 }
c906108c 3004
4a64f543
MS
3005 /* If we failed to insert all locations of a watchpoint, remove
3006 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3007 ALL_BREAKPOINTS (bpt)
3008 {
3009 int some_failed = 0;
3010 struct bp_location *loc;
3011
3012 if (!is_hardware_watchpoint (bpt))
3013 continue;
3014
d6b74ac4 3015 if (!breakpoint_enabled (bpt))
a5606eee 3016 continue;
74960c60
VP
3017
3018 if (bpt->disposition == disp_del_at_next_stop)
3019 continue;
a5606eee
VP
3020
3021 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3022 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3023 {
3024 some_failed = 1;
3025 break;
3026 }
3027 if (some_failed)
3028 {
3029 for (loc = bpt->loc; loc; loc = loc->next)
3030 if (loc->inserted)
834c0d03 3031 remove_breakpoint (loc);
a5606eee
VP
3032
3033 hw_breakpoint_error = 1;
d7e74731
PA
3034 tmp_error_stream.printf ("Could not insert "
3035 "hardware watchpoint %d.\n",
3036 bpt->number);
eacd795a 3037 error_flag = -1;
a5606eee
VP
3038 }
3039 }
3040
eacd795a 3041 if (error_flag)
81d0cc19
GS
3042 {
3043 /* If a hardware breakpoint or watchpoint was inserted, add a
3044 message about possibly exhausted resources. */
dd61ec5c 3045 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3046 {
d7e74731 3047 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3048You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3049 }
223ffa71 3050 target_terminal::ours_for_output ();
81d0cc19
GS
3051 error_stream (tmp_error_stream);
3052 }
c906108c
SS
3053}
3054
c30eee59
TJB
3055/* Used when the program stops.
3056 Returns zero if successful, or non-zero if there was a problem
3057 removing a breakpoint location. */
3058
c906108c 3059int
fba45db2 3060remove_breakpoints (void)
c906108c 3061{
35df4500 3062 struct bp_location *bl, **blp_tmp;
3a1bae8e 3063 int val = 0;
c906108c 3064
35df4500 3065 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3066 {
1e4d1764 3067 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3068 val |= remove_breakpoint (bl);
c5aa993b 3069 }
3a1bae8e 3070 return val;
c906108c
SS
3071}
3072
49fa26b0
PA
3073/* When a thread exits, remove breakpoints that are related to
3074 that thread. */
3075
3076static void
3077remove_threaded_breakpoints (struct thread_info *tp, int silent)
3078{
3079 struct breakpoint *b, *b_tmp;
3080
3081 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3082 {
5d5658a1 3083 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3084 {
3085 b->disposition = disp_del_at_next_stop;
3086
3087 printf_filtered (_("\
43792cf0
PA
3088Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3089 b->number, print_thread_id (tp));
49fa26b0
PA
3090
3091 /* Hide it from the user. */
3092 b->number = 0;
3093 }
3094 }
3095}
3096
6c95b8df
PA
3097/* Remove breakpoints of process PID. */
3098
3099int
3100remove_breakpoints_pid (int pid)
3101{
35df4500 3102 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3103 int val;
3104 struct inferior *inf = find_inferior_pid (pid);
3105
35df4500 3106 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3107 {
35df4500 3108 if (bl->pspace != inf->pspace)
6c95b8df
PA
3109 continue;
3110
fc126975 3111 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3112 {
834c0d03 3113 val = remove_breakpoint (bl);
6c95b8df
PA
3114 if (val != 0)
3115 return val;
3116 }
3117 }
3118 return 0;
3119}
3120
e58b0e63
PA
3121static int internal_breakpoint_number = -1;
3122
84f4c1fe
PM
3123/* Set the breakpoint number of B, depending on the value of INTERNAL.
3124 If INTERNAL is non-zero, the breakpoint number will be populated
3125 from internal_breakpoint_number and that variable decremented.
e5dd4106 3126 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3127 breakpoint_count and that value incremented. Internal breakpoints
3128 do not set the internal var bpnum. */
3129static void
3130set_breakpoint_number (int internal, struct breakpoint *b)
3131{
3132 if (internal)
3133 b->number = internal_breakpoint_number--;
3134 else
3135 {
3136 set_breakpoint_count (breakpoint_count + 1);
3137 b->number = breakpoint_count;
3138 }
3139}
3140
e62c965a 3141static struct breakpoint *
a6d9a66e 3142create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3143 CORE_ADDR address, enum bptype type,
c0a91b2b 3144 const struct breakpoint_ops *ops)
e62c965a 3145{
51abb421 3146 symtab_and_line sal;
e62c965a
PP
3147 sal.pc = address;
3148 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3149 sal.pspace = current_program_space;
e62c965a 3150
51abb421 3151 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3152 b->number = internal_breakpoint_number--;
3153 b->disposition = disp_donttouch;
3154
3155 return b;
3156}
3157
17450429
PP
3158static const char *const longjmp_names[] =
3159 {
3160 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3161 };
3162#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3163
3164/* Per-objfile data private to breakpoint.c. */
3165struct breakpoint_objfile_data
3166{
3167 /* Minimal symbol for "_ovly_debug_event" (if any). */
43dce439 3168 struct bound_minimal_symbol overlay_msym {};
17450429
PP
3169
3170 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
43dce439 3171 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
17450429 3172
28106bc2 3173 /* True if we have looked for longjmp probes. */
43dce439 3174 int longjmp_searched = 0;
28106bc2 3175
45461e0d
SM
3176 /* SystemTap probe points for longjmp (if any). These are non-owning
3177 references. */
3178 std::vector<probe *> longjmp_probes;
28106bc2 3179
17450429 3180 /* Minimal symbol for "std::terminate()" (if any). */
43dce439 3181 struct bound_minimal_symbol terminate_msym {};
17450429
PP
3182
3183 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
43dce439 3184 struct bound_minimal_symbol exception_msym {};
28106bc2
SDJ
3185
3186 /* True if we have looked for exception probes. */
43dce439 3187 int exception_searched = 0;
28106bc2 3188
45461e0d
SM
3189 /* SystemTap probe points for unwinding (if any). These are non-owning
3190 references. */
3191 std::vector<probe *> exception_probes;
17450429
PP
3192};
3193
3194static const struct objfile_data *breakpoint_objfile_key;
3195
3196/* Minimal symbol not found sentinel. */
3197static struct minimal_symbol msym_not_found;
3198
3199/* Returns TRUE if MSYM point to the "not found" sentinel. */
3200
3201static int
3202msym_not_found_p (const struct minimal_symbol *msym)
3203{
3204 return msym == &msym_not_found;
3205}
3206
3207/* Return per-objfile data needed by breakpoint.c.
3208 Allocate the data if necessary. */
3209
3210static struct breakpoint_objfile_data *
3211get_breakpoint_objfile_data (struct objfile *objfile)
3212{
3213 struct breakpoint_objfile_data *bp_objfile_data;
3214
9a3c8263
SM
3215 bp_objfile_data = ((struct breakpoint_objfile_data *)
3216 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3217 if (bp_objfile_data == NULL)
3218 {
43dce439 3219 bp_objfile_data = new breakpoint_objfile_data ();
17450429
PP
3220 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3221 }
3222 return bp_objfile_data;
3223}
3224
28106bc2 3225static void
43dce439 3226free_breakpoint_objfile_data (struct objfile *obj, void *data)
28106bc2 3227{
9a3c8263
SM
3228 struct breakpoint_objfile_data *bp_objfile_data
3229 = (struct breakpoint_objfile_data *) data;
28106bc2 3230
43dce439 3231 delete bp_objfile_data;
28106bc2
SDJ
3232}
3233
e62c965a 3234static void
af02033e 3235create_overlay_event_breakpoint (void)
e62c965a 3236{
69de3c6a 3237 struct objfile *objfile;
af02033e 3238 const char *const func_name = "_ovly_debug_event";
e62c965a 3239
69de3c6a
PP
3240 ALL_OBJFILES (objfile)
3241 {
3242 struct breakpoint *b;
17450429
PP
3243 struct breakpoint_objfile_data *bp_objfile_data;
3244 CORE_ADDR addr;
67994074 3245 struct explicit_location explicit_loc;
69de3c6a 3246
17450429
PP
3247 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3248
3b7344d5 3249 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3250 continue;
3251
3b7344d5 3252 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3253 {
3b7344d5 3254 struct bound_minimal_symbol m;
17450429
PP
3255
3256 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3257 if (m.minsym == NULL)
17450429
PP
3258 {
3259 /* Avoid future lookups in this objfile. */
3b7344d5 3260 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3261 continue;
3262 }
3263 bp_objfile_data->overlay_msym = m;
3264 }
e62c965a 3265
77e371c0 3266 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3267 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3268 bp_overlay_event,
3269 &internal_breakpoint_ops);
67994074
KS
3270 initialize_explicit_location (&explicit_loc);
3271 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3272 b->location = new_explicit_location (&explicit_loc);
e62c965a 3273
69de3c6a
PP
3274 if (overlay_debugging == ovly_auto)
3275 {
3276 b->enable_state = bp_enabled;
3277 overlay_events_enabled = 1;
3278 }
3279 else
3280 {
3281 b->enable_state = bp_disabled;
3282 overlay_events_enabled = 0;
3283 }
e62c965a 3284 }
e62c965a
PP
3285}
3286
0fd8e87f 3287static void
af02033e 3288create_longjmp_master_breakpoint (void)
0fd8e87f 3289{
6c95b8df 3290 struct program_space *pspace;
6c95b8df 3291
5ed8105e 3292 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3293
6c95b8df 3294 ALL_PSPACES (pspace)
af02033e
PP
3295 {
3296 struct objfile *objfile;
3297
3298 set_current_program_space (pspace);
3299
3300 ALL_OBJFILES (objfile)
0fd8e87f 3301 {
af02033e
PP
3302 int i;
3303 struct gdbarch *gdbarch;
17450429 3304 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3305
af02033e 3306 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3307
17450429
PP
3308 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3309
28106bc2
SDJ
3310 if (!bp_objfile_data->longjmp_searched)
3311 {
45461e0d
SM
3312 std::vector<probe *> ret
3313 = find_probes_in_objfile (objfile, "libc", "longjmp");
25f9533e 3314
45461e0d 3315 if (!ret.empty ())
25f9533e
SDJ
3316 {
3317 /* We are only interested in checking one element. */
45461e0d 3318 probe *p = ret[0];
25f9533e
SDJ
3319
3320 if (!can_evaluate_probe_arguments (p))
3321 {
3322 /* We cannot use the probe interface here, because it does
3323 not know how to evaluate arguments. */
45461e0d 3324 ret.clear ();
25f9533e
SDJ
3325 }
3326 }
3327 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3328 bp_objfile_data->longjmp_searched = 1;
3329 }
3330
45461e0d 3331 if (!bp_objfile_data->longjmp_probes.empty ())
28106bc2 3332 {
28106bc2
SDJ
3333 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3334
45461e0d 3335 for (probe *p : bp_objfile_data->longjmp_probes)
28106bc2
SDJ
3336 {
3337 struct breakpoint *b;
3338
729662a5 3339 b = create_internal_breakpoint (gdbarch,
45461e0d 3340 get_probe_address (p, objfile),
28106bc2
SDJ
3341 bp_longjmp_master,
3342 &internal_breakpoint_ops);
d28cd78a 3343 b->location = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3344 b->enable_state = bp_disabled;
3345 }
3346
3347 continue;
3348 }
3349
0569175e
TSD
3350 if (!gdbarch_get_longjmp_target_p (gdbarch))
3351 continue;
3352
17450429 3353 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3354 {
3355 struct breakpoint *b;
af02033e 3356 const char *func_name;
17450429 3357 CORE_ADDR addr;
67994074 3358 struct explicit_location explicit_loc;
6c95b8df 3359
3b7344d5 3360 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3361 continue;
0fd8e87f 3362
17450429 3363 func_name = longjmp_names[i];
3b7344d5 3364 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3365 {
3b7344d5 3366 struct bound_minimal_symbol m;
17450429
PP
3367
3368 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3369 if (m.minsym == NULL)
17450429
PP
3370 {
3371 /* Prevent future lookups in this objfile. */
3b7344d5 3372 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3373 continue;
3374 }
3375 bp_objfile_data->longjmp_msym[i] = m;
3376 }
3377
77e371c0 3378 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3379 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3380 &internal_breakpoint_ops);
67994074
KS
3381 initialize_explicit_location (&explicit_loc);
3382 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3383 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3384 b->enable_state = bp_disabled;
3385 }
0fd8e87f 3386 }
af02033e 3387 }
0fd8e87f
UW
3388}
3389
af02033e 3390/* Create a master std::terminate breakpoint. */
aa7d318d 3391static void
af02033e 3392create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3393{
3394 struct program_space *pspace;
af02033e 3395 const char *const func_name = "std::terminate()";
aa7d318d 3396
5ed8105e 3397 scoped_restore_current_program_space restore_pspace;
aa7d318d
TT
3398
3399 ALL_PSPACES (pspace)
17450429
PP
3400 {
3401 struct objfile *objfile;
3402 CORE_ADDR addr;
3403
3404 set_current_program_space (pspace);
3405
aa7d318d
TT
3406 ALL_OBJFILES (objfile)
3407 {
3408 struct breakpoint *b;
17450429 3409 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3410 struct explicit_location explicit_loc;
aa7d318d 3411
17450429 3412 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3413
3b7344d5 3414 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3415 continue;
3416
3b7344d5 3417 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3418 {
3b7344d5 3419 struct bound_minimal_symbol m;
17450429
PP
3420
3421 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3422 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3423 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3424 {
3425 /* Prevent future lookups in this objfile. */
3b7344d5 3426 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3427 continue;
3428 }
3429 bp_objfile_data->terminate_msym = m;
3430 }
aa7d318d 3431
77e371c0 3432 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3433 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3434 bp_std_terminate_master,
3435 &internal_breakpoint_ops);
67994074
KS
3436 initialize_explicit_location (&explicit_loc);
3437 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3438 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3439 b->enable_state = bp_disabled;
3440 }
17450429 3441 }
aa7d318d
TT
3442}
3443
186c406b
TT
3444/* Install a master breakpoint on the unwinder's debug hook. */
3445
70221824 3446static void
186c406b
TT
3447create_exception_master_breakpoint (void)
3448{
3449 struct objfile *objfile;
17450429 3450 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3451
3452 ALL_OBJFILES (objfile)
3453 {
17450429
PP
3454 struct breakpoint *b;
3455 struct gdbarch *gdbarch;
3456 struct breakpoint_objfile_data *bp_objfile_data;
3457 CORE_ADDR addr;
67994074 3458 struct explicit_location explicit_loc;
17450429
PP
3459
3460 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3461
28106bc2
SDJ
3462 /* We prefer the SystemTap probe point if it exists. */
3463 if (!bp_objfile_data->exception_searched)
3464 {
45461e0d
SM
3465 std::vector<probe *> ret
3466 = find_probes_in_objfile (objfile, "libgcc", "unwind");
25f9533e 3467
45461e0d 3468 if (!ret.empty ())
25f9533e
SDJ
3469 {
3470 /* We are only interested in checking one element. */
45461e0d 3471 probe *p = ret[0];
25f9533e
SDJ
3472
3473 if (!can_evaluate_probe_arguments (p))
3474 {
3475 /* We cannot use the probe interface here, because it does
3476 not know how to evaluate arguments. */
45461e0d 3477 ret.clear ();
25f9533e
SDJ
3478 }
3479 }
3480 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3481 bp_objfile_data->exception_searched = 1;
3482 }
3483
45461e0d 3484 if (!bp_objfile_data->exception_probes.empty ())
28106bc2
SDJ
3485 {
3486 struct gdbarch *gdbarch = get_objfile_arch (objfile);
45461e0d
SM
3487
3488 for (probe *p : bp_objfile_data->exception_probes)
28106bc2
SDJ
3489 {
3490 struct breakpoint *b;
3491
729662a5 3492 b = create_internal_breakpoint (gdbarch,
45461e0d 3493 get_probe_address (p, objfile),
28106bc2
SDJ
3494 bp_exception_master,
3495 &internal_breakpoint_ops);
d28cd78a 3496 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3497 b->enable_state = bp_disabled;
3498 }
3499
3500 continue;
3501 }
3502
3503 /* Otherwise, try the hook function. */
3504
3b7344d5 3505 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3506 continue;
3507
3508 gdbarch = get_objfile_arch (objfile);
186c406b 3509
3b7344d5 3510 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3511 {
3b7344d5 3512 struct bound_minimal_symbol debug_hook;
186c406b 3513
17450429 3514 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3515 if (debug_hook.minsym == NULL)
17450429 3516 {
3b7344d5 3517 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3518 continue;
3519 }
3520
3521 bp_objfile_data->exception_msym = debug_hook;
186c406b 3522 }
17450429 3523
77e371c0 3524 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3525 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3526 &current_target);
06edf0c0
PA
3527 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3528 &internal_breakpoint_ops);
67994074
KS
3529 initialize_explicit_location (&explicit_loc);
3530 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3531 b->location = new_explicit_location (&explicit_loc);
17450429 3532 b->enable_state = bp_disabled;
186c406b 3533 }
186c406b
TT
3534}
3535
9ef9e6a6
KS
3536/* Does B have a location spec? */
3537
3538static int
3539breakpoint_event_location_empty_p (const struct breakpoint *b)
3540{
d28cd78a 3541 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3542}
3543
c906108c 3544void
fba45db2 3545update_breakpoints_after_exec (void)
c906108c 3546{
35df4500 3547 struct breakpoint *b, *b_tmp;
876fa593 3548 struct bp_location *bploc, **bplocp_tmp;
c906108c 3549
25b22b0a
PA
3550 /* We're about to delete breakpoints from GDB's lists. If the
3551 INSERTED flag is true, GDB will try to lift the breakpoints by
3552 writing the breakpoints' "shadow contents" back into memory. The
3553 "shadow contents" are NOT valid after an exec, so GDB should not
3554 do that. Instead, the target is responsible from marking
3555 breakpoints out as soon as it detects an exec. We don't do that
3556 here instead, because there may be other attempts to delete
3557 breakpoints after detecting an exec and before reaching here. */
876fa593 3558 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3559 if (bploc->pspace == current_program_space)
3560 gdb_assert (!bploc->inserted);
c906108c 3561
35df4500 3562 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3563 {
6c95b8df
PA
3564 if (b->pspace != current_program_space)
3565 continue;
3566
4a64f543 3567 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3568 if (b->type == bp_shlib_event)
3569 {
3570 delete_breakpoint (b);
3571 continue;
3572 }
c906108c 3573
4a64f543 3574 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3575 if (b->type == bp_jit_event)
3576 {
3577 delete_breakpoint (b);
3578 continue;
3579 }
3580
1900040c 3581 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3582 as must overlay event and longjmp master breakpoints. */
3583 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3584 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3585 || b->type == bp_exception_master)
c4093a6a
JM
3586 {
3587 delete_breakpoint (b);
3588 continue;
3589 }
3590
4a64f543 3591 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3592 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3593 {
3594 delete_breakpoint (b);
3595 continue;
3596 }
3597
7c16b83e
PA
3598 /* Just like single-step breakpoints. */
3599 if (b->type == bp_single_step)
3600 {
3601 delete_breakpoint (b);
3602 continue;
3603 }
3604
611c83ae
PA
3605 /* Longjmp and longjmp-resume breakpoints are also meaningless
3606 after an exec. */
186c406b 3607 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3608 || b->type == bp_longjmp_call_dummy
186c406b 3609 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3610 {
3611 delete_breakpoint (b);
3612 continue;
3613 }
3614
ce78b96d
JB
3615 if (b->type == bp_catchpoint)
3616 {
3617 /* For now, none of the bp_catchpoint breakpoints need to
3618 do anything at this point. In the future, if some of
3619 the catchpoints need to something, we will need to add
3620 a new method, and call this method from here. */
3621 continue;
3622 }
3623
c5aa993b
JM
3624 /* bp_finish is a special case. The only way we ought to be able
3625 to see one of these when an exec() has happened, is if the user
3626 caught a vfork, and then said "finish". Ordinarily a finish just
3627 carries them to the call-site of the current callee, by setting
3628 a temporary bp there and resuming. But in this case, the finish
3629 will carry them entirely through the vfork & exec.
3630
3631 We don't want to allow a bp_finish to remain inserted now. But
3632 we can't safely delete it, 'cause finish_command has a handle to
3633 the bp on a bpstat, and will later want to delete it. There's a
3634 chance (and I've seen it happen) that if we delete the bp_finish
3635 here, that its storage will get reused by the time finish_command
3636 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3637 We really must allow finish_command to delete a bp_finish.
3638
e5dd4106 3639 In the absence of a general solution for the "how do we know
53a5351d
JM
3640 it's safe to delete something others may have handles to?"
3641 problem, what we'll do here is just uninsert the bp_finish, and
3642 let finish_command delete it.
3643
3644 (We know the bp_finish is "doomed" in the sense that it's
3645 momentary, and will be deleted as soon as finish_command sees
3646 the inferior stopped. So it doesn't matter that the bp's
3647 address is probably bogus in the new a.out, unlike e.g., the
3648 solib breakpoints.) */
c5aa993b 3649
c5aa993b
JM
3650 if (b->type == bp_finish)
3651 {
3652 continue;
3653 }
3654
3655 /* Without a symbolic address, we have little hope of the
3656 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3657 a.out. */
9ef9e6a6 3658 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3659 {
3660 delete_breakpoint (b);
3661 continue;
3662 }
c5aa993b 3663 }
c906108c
SS
3664}
3665
3666int
d80ee84f 3667detach_breakpoints (ptid_t ptid)
c906108c 3668{
35df4500 3669 struct bp_location *bl, **blp_tmp;
3a1bae8e 3670 int val = 0;
2989a365 3671 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3672 struct inferior *inf = current_inferior ();
c5aa993b 3673
dfd4cc63 3674 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3675 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3676
6c95b8df 3677 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3678 inferior_ptid = ptid;
35df4500 3679 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3680 {
35df4500 3681 if (bl->pspace != inf->pspace)
6c95b8df
PA
3682 continue;
3683
bd9673a4
PW
3684 /* This function must physically remove breakpoints locations
3685 from the specified ptid, without modifying the breakpoint
3686 package's state. Locations of type bp_loc_other are only
3687 maintained at GDB side. So, there is no need to remove
3688 these bp_loc_other locations. Moreover, removing these
3689 would modify the breakpoint package's state. */
3690 if (bl->loc_type == bp_loc_other)
3691 continue;
3692
35df4500 3693 if (bl->inserted)
b2b6a7da 3694 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3695 }
d03285ec 3696
3a1bae8e 3697 return val;
c906108c
SS
3698}
3699
35df4500 3700/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3701 Note that this is used to detach breakpoints from a child fork.
3702 When we get here, the child isn't in the inferior list, and neither
3703 do we have objects to represent its address space --- we should
35df4500 3704 *not* look at bl->pspace->aspace here. */
6c95b8df 3705
c906108c 3706static int
b2b6a7da 3707remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3708{
3709 int val;
c5aa993b 3710
35df4500
TJB
3711 /* BL is never in moribund_locations by our callers. */
3712 gdb_assert (bl->owner != NULL);
2bdf28a0 3713
74960c60
VP
3714 /* The type of none suggests that owner is actually deleted.
3715 This should not ever happen. */
35df4500 3716 gdb_assert (bl->owner->type != bp_none);
0bde7532 3717
35df4500
TJB
3718 if (bl->loc_type == bp_loc_software_breakpoint
3719 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3720 {
c02f5703
MS
3721 /* "Normal" instruction breakpoint: either the standard
3722 trap-instruction bp (bp_breakpoint), or a
3723 bp_hardware_breakpoint. */
3724
3725 /* First check to see if we have to handle an overlay. */
3726 if (overlay_debugging == ovly_off
35df4500
TJB
3727 || bl->section == NULL
3728 || !(section_is_overlay (bl->section)))
c02f5703
MS
3729 {
3730 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3731
3732 /* If we're trying to uninsert a memory breakpoint that we
3733 know is set in a dynamic object that is marked
3734 shlib_disabled, then either the dynamic object was
3735 removed with "remove-symbol-file" or with
3736 "nosharedlibrary". In the former case, we don't know
3737 whether another dynamic object might have loaded over the
3738 breakpoint's address -- the user might well let us know
3739 about it next with add-symbol-file (the whole point of
d03de421 3740 add-symbol-file is letting the user manually maintain a
08351840
PA
3741 list of dynamically loaded objects). If we have the
3742 breakpoint's shadow memory, that is, this is a software
3743 breakpoint managed by GDB, check whether the breakpoint
3744 is still inserted in memory, to avoid overwriting wrong
3745 code with stale saved shadow contents. Note that HW
3746 breakpoints don't have shadow memory, as they're
3747 implemented using a mechanism that is not dependent on
3748 being able to modify the target's memory, and as such
3749 they should always be removed. */
3750 if (bl->shlib_disabled
3751 && bl->target_info.shadow_len != 0
3752 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3753 val = 0;
3754 else
73971819 3755 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3756 }
c906108c
SS
3757 else
3758 {
4a64f543 3759 /* This breakpoint is in an overlay section.
c02f5703
MS
3760 Did we set a breakpoint at the LMA? */
3761 if (!overlay_events_enabled)
3762 {
3763 /* Yes -- overlay event support is not active, so we
3764 should have set a breakpoint at the LMA. Remove it.
3765 */
c02f5703
MS
3766 /* Ignore any failures: if the LMA is in ROM, we will
3767 have already warned when we failed to insert it. */
35df4500
TJB
3768 if (bl->loc_type == bp_loc_hardware_breakpoint)
3769 target_remove_hw_breakpoint (bl->gdbarch,
3770 &bl->overlay_target_info);
c02f5703 3771 else
35df4500 3772 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3773 &bl->overlay_target_info,
3774 reason);
c02f5703
MS
3775 }
3776 /* Did we set a breakpoint at the VMA?
3777 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3778 if (bl->inserted)
c906108c 3779 {
c02f5703
MS
3780 /* Yes -- remove it. Previously we did not bother to
3781 remove the breakpoint if the section had been
3782 unmapped, but let's not rely on that being safe. We
3783 don't know what the overlay manager might do. */
aa67235e
UW
3784
3785 /* However, we should remove *software* breakpoints only
3786 if the section is still mapped, or else we overwrite
3787 wrong code with the saved shadow contents. */
348d480f
PA
3788 if (bl->loc_type == bp_loc_hardware_breakpoint
3789 || section_is_mapped (bl->section))
73971819 3790 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3791 else
3792 val = 0;
c906108c 3793 }
c02f5703
MS
3794 else
3795 {
3796 /* No -- not inserted, so no need to remove. No error. */
3797 val = 0;
3798 }
c906108c 3799 }
879d1e6b 3800
08351840
PA
3801 /* In some cases, we might not be able to remove a breakpoint in
3802 a shared library that has already been removed, but we have
3803 not yet processed the shlib unload event. Similarly for an
3804 unloaded add-symbol-file object - the user might not yet have
3805 had the chance to remove-symbol-file it. shlib_disabled will
3806 be set if the library/object has already been removed, but
3807 the breakpoint hasn't been uninserted yet, e.g., after
3808 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3809 always-inserted mode. */
076855f9 3810 if (val
08351840
PA
3811 && (bl->loc_type == bp_loc_software_breakpoint
3812 && (bl->shlib_disabled
3813 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3814 || shared_objfile_contains_address_p (bl->pspace,
3815 bl->address))))
879d1e6b
UW
3816 val = 0;
3817
c906108c
SS
3818 if (val)
3819 return val;
b2b6a7da 3820 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3821 }
35df4500 3822 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3823 {
77b06cd7
TJB
3824 gdb_assert (bl->owner->ops != NULL
3825 && bl->owner->ops->remove_location != NULL);
3826
b2b6a7da 3827 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3828 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3829
c906108c 3830 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3831 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3832 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3833 bl->owner->number);
c906108c 3834 }
35df4500
TJB
3835 else if (bl->owner->type == bp_catchpoint
3836 && breakpoint_enabled (bl->owner)
3837 && !bl->duplicate)
ce78b96d 3838 {
77b06cd7
TJB
3839 gdb_assert (bl->owner->ops != NULL
3840 && bl->owner->ops->remove_location != NULL);
ce78b96d 3841
73971819 3842 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3843 if (val)
3844 return val;
77b06cd7 3845
b2b6a7da 3846 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3847 }
c906108c
SS
3848
3849 return 0;
3850}
3851
6c95b8df 3852static int
834c0d03 3853remove_breakpoint (struct bp_location *bl)
6c95b8df 3854{
35df4500
TJB
3855 /* BL is never in moribund_locations by our callers. */
3856 gdb_assert (bl->owner != NULL);
2bdf28a0 3857
6c95b8df
PA
3858 /* The type of none suggests that owner is actually deleted.
3859 This should not ever happen. */
35df4500 3860 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3861
5ed8105e 3862 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3863
35df4500 3864 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3865
5ed8105e 3866 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3867}
3868
c906108c
SS
3869/* Clear the "inserted" flag in all breakpoints. */
3870
25b22b0a 3871void
fba45db2 3872mark_breakpoints_out (void)
c906108c 3873{
35df4500 3874 struct bp_location *bl, **blp_tmp;
c906108c 3875
35df4500 3876 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 3877 if (bl->pspace == current_program_space)
35df4500 3878 bl->inserted = 0;
c906108c
SS
3879}
3880
53a5351d
JM
3881/* Clear the "inserted" flag in all breakpoints and delete any
3882 breakpoints which should go away between runs of the program.
c906108c
SS
3883
3884 Plus other such housekeeping that has to be done for breakpoints
3885 between runs.
3886
53a5351d
JM
3887 Note: this function gets called at the end of a run (by
3888 generic_mourn_inferior) and when a run begins (by
4a64f543 3889 init_wait_for_inferior). */
c906108c
SS
3890
3891
3892
3893void
fba45db2 3894breakpoint_init_inferior (enum inf_context context)
c906108c 3895{
35df4500 3896 struct breakpoint *b, *b_tmp;
870f88f7 3897 struct bp_location *bl;
1c5cfe86 3898 int ix;
6c95b8df 3899 struct program_space *pspace = current_program_space;
c906108c 3900
50c71eaf
PA
3901 /* If breakpoint locations are shared across processes, then there's
3902 nothing to do. */
f5656ead 3903 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3904 return;
3905
1a853c52 3906 mark_breakpoints_out ();
075f6582 3907
35df4500 3908 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3909 {
6c95b8df
PA
3910 if (b->loc && b->loc->pspace != pspace)
3911 continue;
3912
c5aa993b
JM
3913 switch (b->type)
3914 {
3915 case bp_call_dummy:
e2e4d78b 3916 case bp_longjmp_call_dummy:
c906108c 3917
c5aa993b 3918 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3919 cause problems when the inferior is rerun, so we better get
3920 rid of it. */
3921
3922 case bp_watchpoint_scope:
3923
3924 /* Also get rid of scope breakpoints. */
3925
3926 case bp_shlib_event:
3927
3928 /* Also remove solib event breakpoints. Their addresses may
3929 have changed since the last time we ran the program.
3930 Actually we may now be debugging against different target;
3931 and so the solib backend that installed this breakpoint may
3932 not be used in by the target. E.g.,
3933
3934 (gdb) file prog-linux
3935 (gdb) run # native linux target
3936 ...
3937 (gdb) kill
3938 (gdb) file prog-win.exe
3939 (gdb) tar rem :9999 # remote Windows gdbserver.
3940 */
c906108c 3941
f59f708a
PA
3942 case bp_step_resume:
3943
3944 /* Also remove step-resume breakpoints. */
3945
7c16b83e
PA
3946 case bp_single_step:
3947
3948 /* Also remove single-step breakpoints. */
3949
c5aa993b
JM
3950 delete_breakpoint (b);
3951 break;
c906108c 3952
c5aa993b
JM
3953 case bp_watchpoint:
3954 case bp_hardware_watchpoint:
3955 case bp_read_watchpoint:
3956 case bp_access_watchpoint:
3a5c3e22
PA
3957 {
3958 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3959
3a5c3e22
PA
3960 /* Likewise for watchpoints on local expressions. */
3961 if (w->exp_valid_block != NULL)
3962 delete_breakpoint (b);
63000888 3963 else
3a5c3e22 3964 {
63000888
PA
3965 /* Get rid of existing locations, which are no longer
3966 valid. New ones will be created in
3967 update_watchpoint, when the inferior is restarted.
3968 The next update_global_location_list call will
3969 garbage collect them. */
3970 b->loc = NULL;
3971
3972 if (context == inf_starting)
3973 {
3974 /* Reset val field to force reread of starting value in
3975 insert_breakpoints. */
3976 if (w->val)
3977 value_free (w->val);
3978 w->val = NULL;
3979 w->val_valid = 0;
3980 }
3981 }
3a5c3e22 3982 }
c5aa993b
JM
3983 break;
3984 default:
c5aa993b
JM
3985 break;
3986 }
3987 }
1c5cfe86
PA
3988
3989 /* Get rid of the moribund locations. */
35df4500
TJB
3990 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3991 decref_bp_location (&bl);
1c5cfe86 3992 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
3993}
3994
6c95b8df
PA
3995/* These functions concern about actual breakpoints inserted in the
3996 target --- to e.g. check if we need to do decr_pc adjustment or if
3997 we need to hop over the bkpt --- so we check for address space
3998 match, not program space. */
3999
c2c6d25f
JM
4000/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4001 exists at PC. It returns ordinary_breakpoint_here if it's an
4002 ordinary breakpoint, or permanent_breakpoint_here if it's a
4003 permanent breakpoint.
4004 - When continuing from a location with an ordinary breakpoint, we
4005 actually single step once before calling insert_breakpoints.
e5dd4106 4006 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4007 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4008 the target, to advance the PC past the breakpoint. */
c906108c 4009
c2c6d25f 4010enum breakpoint_here
accd0bcd 4011breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4012{
35df4500 4013 struct bp_location *bl, **blp_tmp;
c2c6d25f 4014 int any_breakpoint_here = 0;
c906108c 4015
35df4500 4016 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4017 {
35df4500
TJB
4018 if (bl->loc_type != bp_loc_software_breakpoint
4019 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4020 continue;
4021
f1310107 4022 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4023 if ((breakpoint_enabled (bl->owner)
1a853c52 4024 || bl->permanent)
f1310107 4025 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4026 {
4027 if (overlay_debugging
35df4500
TJB
4028 && section_is_overlay (bl->section)
4029 && !section_is_mapped (bl->section))
075f6582 4030 continue; /* unmapped overlay -- can't be a match */
1a853c52 4031 else if (bl->permanent)
075f6582
DJ
4032 return permanent_breakpoint_here;
4033 else
4034 any_breakpoint_here = 1;
4035 }
4036 }
c906108c 4037
f486487f 4038 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4039}
4040
d35ae833
PA
4041/* See breakpoint.h. */
4042
4043int
accd0bcd 4044breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4045 CORE_ADDR addr, ULONGEST len)
4046{
4047 struct bp_location *bl, **blp_tmp;
4048
4049 ALL_BP_LOCATIONS (bl, blp_tmp)
4050 {
4051 if (bl->loc_type != bp_loc_software_breakpoint
4052 && bl->loc_type != bp_loc_hardware_breakpoint)
4053 continue;
4054
4055 if ((breakpoint_enabled (bl->owner)
4056 || bl->permanent)
4057 && breakpoint_location_address_range_overlap (bl, aspace,
4058 addr, len))
4059 {
4060 if (overlay_debugging
4061 && section_is_overlay (bl->section)
4062 && !section_is_mapped (bl->section))
4063 {
4064 /* Unmapped overlay -- can't be a match. */
4065 continue;
4066 }
4067
4068 return 1;
4069 }
4070 }
4071
4072 return 0;
4073}
4074
1c5cfe86
PA
4075/* Return true if there's a moribund breakpoint at PC. */
4076
4077int
accd0bcd 4078moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4079{
4080 struct bp_location *loc;
4081 int ix;
4082
4083 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4084 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4085 return 1;
4086
4087 return 0;
4088}
c2c6d25f 4089
f7ce857f
PA
4090/* Returns non-zero iff BL is inserted at PC, in address space
4091 ASPACE. */
4092
4093static int
4094bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4095 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4096{
4097 if (bl->inserted
4098 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4099 aspace, pc))
4100 {
4101 if (overlay_debugging
4102 && section_is_overlay (bl->section)
4103 && !section_is_mapped (bl->section))
4104 return 0; /* unmapped overlay -- can't be a match */
4105 else
4106 return 1;
4107 }
4108 return 0;
4109}
4110
a1fd2fa5 4111/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4112
4113int
accd0bcd 4114breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4115{
f7ce857f 4116 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4117
f7ce857f 4118 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4119 {
f7ce857f
PA
4120 struct bp_location *bl = *blp;
4121
35df4500
TJB
4122 if (bl->loc_type != bp_loc_software_breakpoint
4123 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4124 continue;
4125
f7ce857f
PA
4126 if (bp_location_inserted_here_p (bl, aspace, pc))
4127 return 1;
c5aa993b 4128 }
c36b740a
VP
4129 return 0;
4130}
4131
a1fd2fa5
PA
4132/* This function returns non-zero iff there is a software breakpoint
4133 inserted at PC. */
c36b740a
VP
4134
4135int
accd0bcd 4136software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4137 CORE_ADDR pc)
4fa8626c 4138{
f7ce857f 4139 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4140
f7ce857f 4141 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4142 {
f7ce857f
PA
4143 struct bp_location *bl = *blp;
4144
35df4500 4145 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4146 continue;
4147
f7ce857f
PA
4148 if (bp_location_inserted_here_p (bl, aspace, pc))
4149 return 1;
4fa8626c
DJ
4150 }
4151
4152 return 0;
9c02b525
PA
4153}
4154
4155/* See breakpoint.h. */
4156
4157int
accd0bcd 4158hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4159 CORE_ADDR pc)
4160{
4161 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4162
4163 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4164 {
4165 struct bp_location *bl = *blp;
4166
4167 if (bl->loc_type != bp_loc_hardware_breakpoint)
4168 continue;
4169
4170 if (bp_location_inserted_here_p (bl, aspace, pc))
4171 return 1;
4172 }
4173
4174 return 0;
4fa8626c
DJ
4175}
4176
9093389c 4177int
accd0bcd 4178hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4179 CORE_ADDR addr, ULONGEST len)
4180{
4181 struct breakpoint *bpt;
4182
4183 ALL_BREAKPOINTS (bpt)
4184 {
4185 struct bp_location *loc;
4186
4187 if (bpt->type != bp_hardware_watchpoint
4188 && bpt->type != bp_access_watchpoint)
4189 continue;
4190
4191 if (!breakpoint_enabled (bpt))
4192 continue;
4193
4194 for (loc = bpt->loc; loc; loc = loc->next)
4195 if (loc->pspace->aspace == aspace && loc->inserted)
4196 {
4197 CORE_ADDR l, h;
4198
4199 /* Check for intersection. */
768adc05
PA
4200 l = std::max<CORE_ADDR> (loc->address, addr);
4201 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4202 if (l < h)
4203 return 1;
4204 }
4205 }
4206 return 0;
4207}
c906108c 4208\f
c5aa993b 4209
c906108c
SS
4210/* bpstat stuff. External routines' interfaces are documented
4211 in breakpoint.h. */
4212
4213int
c326b90e 4214is_catchpoint (struct breakpoint *ep)
c906108c 4215{
533be4dd 4216 return (ep->type == bp_catchpoint);
c906108c
SS
4217}
4218
f431efe5
PA
4219/* Frees any storage that is part of a bpstat. Does not walk the
4220 'next' chain. */
4221
04afa70c 4222bpstats::~bpstats ()
198757a8 4223{
04afa70c
TT
4224 if (old_val != NULL)
4225 value_free (old_val);
04afa70c
TT
4226 if (bp_location_at != NULL)
4227 decref_bp_location (&bp_location_at);
198757a8
VP
4228}
4229
c906108c
SS
4230/* Clear a bpstat so that it says we are not at any breakpoint.
4231 Also free any storage that is part of a bpstat. */
4232
4233void
fba45db2 4234bpstat_clear (bpstat *bsp)
c906108c
SS
4235{
4236 bpstat p;
4237 bpstat q;
4238
4239 if (bsp == 0)
4240 return;
4241 p = *bsp;
4242 while (p != NULL)
4243 {
4244 q = p->next;
04afa70c 4245 delete p;
c906108c
SS
4246 p = q;
4247 }
4248 *bsp = NULL;
4249}
4250
04afa70c
TT
4251bpstats::bpstats (const bpstats &other)
4252 : next (NULL),
4253 bp_location_at (other.bp_location_at),
4254 breakpoint_at (other.breakpoint_at),
4255 commands (other.commands),
4256 old_val (other.old_val),
4257 print (other.print),
4258 stop (other.stop),
4259 print_it (other.print_it)
4260{
4261 if (old_val != NULL)
4262 {
4263 old_val = value_copy (old_val);
4264 release_value (old_val);
4265 }
4266 incref_bp_location (bp_location_at);
04afa70c
TT
4267}
4268
c906108c
SS
4269/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4270 is part of the bpstat is copied as well. */
4271
4272bpstat
fba45db2 4273bpstat_copy (bpstat bs)
c906108c
SS
4274{
4275 bpstat p = NULL;
4276 bpstat tmp;
4277 bpstat retval = NULL;
4278
4279 if (bs == NULL)
4280 return bs;
4281
4282 for (; bs != NULL; bs = bs->next)
4283 {
04afa70c 4284 tmp = new bpstats (*bs);
31cc81e9 4285
c906108c
SS
4286 if (p == NULL)
4287 /* This is the first thing in the chain. */
4288 retval = tmp;
4289 else
4290 p->next = tmp;
4291 p = tmp;
4292 }
4293 p->next = NULL;
4294 return retval;
4295}
4296
4a64f543 4297/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4298
4299bpstat
fba45db2 4300bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4301{
c5aa993b
JM
4302 if (bsp == NULL)
4303 return NULL;
c906108c 4304
c5aa993b
JM
4305 for (; bsp != NULL; bsp = bsp->next)
4306 {
f431efe5 4307 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4308 return bsp;
4309 }
c906108c
SS
4310 return NULL;
4311}
4312
ab04a2af
TT
4313/* See breakpoint.h. */
4314
47591c29 4315int
427cd150 4316bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4317{
ab04a2af
TT
4318 for (; bsp != NULL; bsp = bsp->next)
4319 {
427cd150
TT
4320 if (bsp->breakpoint_at == NULL)
4321 {
4322 /* A moribund location can never explain a signal other than
4323 GDB_SIGNAL_TRAP. */
4324 if (sig == GDB_SIGNAL_TRAP)
47591c29 4325 return 1;
427cd150
TT
4326 }
4327 else
47591c29
PA
4328 {
4329 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4330 sig))
4331 return 1;
4332 }
ab04a2af
TT
4333 }
4334
47591c29 4335 return 0;
ab04a2af
TT
4336}
4337
4a64f543
MS
4338/* Put in *NUM the breakpoint number of the first breakpoint we are
4339 stopped at. *BSP upon return is a bpstat which points to the
4340 remaining breakpoints stopped at (but which is not guaranteed to be
4341 good for anything but further calls to bpstat_num).
4342
8671a17b
PA
4343 Return 0 if passed a bpstat which does not indicate any breakpoints.
4344 Return -1 if stopped at a breakpoint that has been deleted since
4345 we set it.
4346 Return 1 otherwise. */
c906108c
SS
4347
4348int
8671a17b 4349bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4350{
4351 struct breakpoint *b;
4352
4353 if ((*bsp) == NULL)
4354 return 0; /* No more breakpoint values */
8671a17b 4355
4a64f543
MS
4356 /* We assume we'll never have several bpstats that correspond to a
4357 single breakpoint -- otherwise, this function might return the
4358 same number more than once and this will look ugly. */
f431efe5 4359 b = (*bsp)->breakpoint_at;
8671a17b
PA
4360 *bsp = (*bsp)->next;
4361 if (b == NULL)
4362 return -1; /* breakpoint that's been deleted since */
4363
4364 *num = b->number; /* We have its number */
4365 return 1;
c906108c
SS
4366}
4367
e93ca019 4368/* See breakpoint.h. */
c906108c
SS
4369
4370void
e93ca019 4371bpstat_clear_actions (void)
c906108c 4372{
e93ca019
JK
4373 struct thread_info *tp;
4374 bpstat bs;
4375
4376 if (ptid_equal (inferior_ptid, null_ptid))
4377 return;
4378
4379 tp = find_thread_ptid (inferior_ptid);
4380 if (tp == NULL)
4381 return;
4382
4383 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4384 {
d1b0a7bf 4385 bs->commands = NULL;
abf85f46 4386
c906108c
SS
4387 if (bs->old_val != NULL)
4388 {
4389 value_free (bs->old_val);
4390 bs->old_val = NULL;
4391 }
4392 }
4393}
4394
f3b1572e
PA
4395/* Called when a command is about to proceed the inferior. */
4396
4397static void
4398breakpoint_about_to_proceed (void)
4399{
4400 if (!ptid_equal (inferior_ptid, null_ptid))
4401 {
4402 struct thread_info *tp = inferior_thread ();
4403
4404 /* Allow inferior function calls in breakpoint commands to not
4405 interrupt the command list. When the call finishes
4406 successfully, the inferior will be standing at the same
4407 breakpoint as if nothing happened. */
16c381f0 4408 if (tp->control.in_infcall)
f3b1572e
PA
4409 return;
4410 }
4411
4412 breakpoint_proceeded = 1;
4413}
4414
abf85f46
JK
4415/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4416 or its equivalent. */
4417
4418static int
4419command_line_is_silent (struct command_line *cmd)
4420{
4f45d445 4421 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4422}
4423
4a64f543
MS
4424/* Execute all the commands associated with all the breakpoints at
4425 this location. Any of these commands could cause the process to
4426 proceed beyond this point, etc. We look out for such changes by
4427 checking the global "breakpoint_proceeded" after each command.
c906108c 4428
347bddb7
PA
4429 Returns true if a breakpoint command resumed the inferior. In that
4430 case, it is the caller's responsibility to recall it again with the
4431 bpstat of the current thread. */
4432
4433static int
4434bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4435{
4436 bpstat bs;
347bddb7 4437 int again = 0;
c906108c
SS
4438
4439 /* Avoid endless recursion if a `source' command is contained
4440 in bs->commands. */
4441 if (executing_breakpoint_commands)
347bddb7 4442 return 0;
c906108c 4443
81b1e71c
TT
4444 scoped_restore save_executing
4445 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4446
1ac32117 4447 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4448
4a64f543 4449 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4450 bs = *bsp;
4451
4452 breakpoint_proceeded = 0;
4453 for (; bs != NULL; bs = bs->next)
4454 {
d1b0a7bf 4455 struct command_line *cmd = NULL;
6c50ab1c
JB
4456
4457 /* Take ownership of the BSP's command tree, if it has one.
4458
4459 The command tree could legitimately contain commands like
4460 'step' and 'next', which call clear_proceed_status, which
4461 frees stop_bpstat's command tree. To make sure this doesn't
4462 free the tree we're executing out from under us, we need to
4463 take ownership of the tree ourselves. Since a given bpstat's
4464 commands are only executed once, we don't need to copy it; we
4465 can clear the pointer in the bpstat, and make sure we free
4466 the tree when we're done. */
d1b0a7bf 4467 counted_command_line ccmd = bs->commands;
9add0f1b 4468 bs->commands = NULL;
d1b0a7bf
TT
4469 if (ccmd != NULL)
4470 cmd = ccmd.get ();
abf85f46
JK
4471 if (command_line_is_silent (cmd))
4472 {
4473 /* The action has been already done by bpstat_stop_status. */
4474 cmd = cmd->next;
4475 }
6c50ab1c 4476
c906108c
SS
4477 while (cmd != NULL)
4478 {
4479 execute_control_command (cmd);
4480
4481 if (breakpoint_proceeded)
4482 break;
4483 else
4484 cmd = cmd->next;
4485 }
6c50ab1c 4486
c906108c 4487 if (breakpoint_proceeded)
32c1e744 4488 {
cb814510 4489 if (current_ui->async)
347bddb7
PA
4490 /* If we are in async mode, then the target might be still
4491 running, not stopped at any breakpoint, so nothing for
4492 us to do here -- just return to the event loop. */
4493 ;
32c1e744
VP
4494 else
4495 /* In sync mode, when execute_control_command returns
4496 we're already standing on the next breakpoint.
347bddb7
PA
4497 Breakpoint commands for that stop were not run, since
4498 execute_command does not run breakpoint commands --
4499 only command_line_handler does, but that one is not
4500 involved in execution of breakpoint commands. So, we
4501 can now execute breakpoint commands. It should be
4502 noted that making execute_command do bpstat actions is
4503 not an option -- in this case we'll have recursive
4504 invocation of bpstat for each breakpoint with a
4505 command, and can easily blow up GDB stack. Instead, we
4506 return true, which will trigger the caller to recall us
4507 with the new stop_bpstat. */
4508 again = 1;
4509 break;
32c1e744 4510 }
c906108c 4511 }
347bddb7
PA
4512 return again;
4513}
4514
4515void
4516bpstat_do_actions (void)
4517{
353d1d73
JK
4518 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4519
347bddb7
PA
4520 /* Do any commands attached to breakpoint we are stopped at. */
4521 while (!ptid_equal (inferior_ptid, null_ptid)
4522 && target_has_execution
4523 && !is_exited (inferior_ptid)
4524 && !is_executing (inferior_ptid))
4525 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4526 and only return when it is stopped at the next breakpoint, we
4527 keep doing breakpoint actions until it returns false to
4528 indicate the inferior was not resumed. */
16c381f0 4529 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4530 break;
353d1d73
JK
4531
4532 discard_cleanups (cleanup_if_error);
c906108c
SS
4533}
4534
fa4727a6
DJ
4535/* Print out the (old or new) value associated with a watchpoint. */
4536
4537static void
4538watchpoint_value_print (struct value *val, struct ui_file *stream)
4539{
4540 if (val == NULL)
4541 fprintf_unfiltered (stream, _("<unreadable>"));
4542 else
79a45b7d
TT
4543 {
4544 struct value_print_options opts;
4545 get_user_print_options (&opts);
4546 value_print (val, stream, &opts);
4547 }
fa4727a6
DJ
4548}
4549
f303dbd6
PA
4550/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4551 debugging multiple threads. */
4552
4553void
4554maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4555{
112e8700 4556 if (uiout->is_mi_like_p ())
f303dbd6
PA
4557 return;
4558
112e8700 4559 uiout->text ("\n");
f303dbd6
PA
4560
4561 if (show_thread_that_caused_stop ())
4562 {
4563 const char *name;
4564 struct thread_info *thr = inferior_thread ();
4565
112e8700
SM
4566 uiout->text ("Thread ");
4567 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
f303dbd6
PA
4568
4569 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4570 if (name != NULL)
4571 {
112e8700
SM
4572 uiout->text (" \"");
4573 uiout->field_fmt ("name", "%s", name);
4574 uiout->text ("\"");
f303dbd6
PA
4575 }
4576
112e8700 4577 uiout->text (" hit ");
f303dbd6
PA
4578 }
4579}
4580
e514a9d6 4581/* Generic routine for printing messages indicating why we
4a64f543 4582 stopped. The behavior of this function depends on the value
e514a9d6
JM
4583 'print_it' in the bpstat structure. Under some circumstances we
4584 may decide not to print anything here and delegate the task to
4a64f543 4585 normal_stop(). */
e514a9d6
JM
4586
4587static enum print_stop_action
4588print_bp_stop_message (bpstat bs)
4589{
4590 switch (bs->print_it)
4591 {
4592 case print_it_noop:
4a64f543 4593 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4594 return PRINT_UNKNOWN;
4595 break;
4596
4597 case print_it_done:
4598 /* We still want to print the frame, but we already printed the
4a64f543 4599 relevant messages. */
e514a9d6
JM
4600 return PRINT_SRC_AND_LOC;
4601 break;
4602
4603 case print_it_normal:
4f8d1dc6 4604 {
f431efe5
PA
4605 struct breakpoint *b = bs->breakpoint_at;
4606
1a6a67de
TJB
4607 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4608 which has since been deleted. */
4609 if (b == NULL)
4610 return PRINT_UNKNOWN;
4611
348d480f
PA
4612 /* Normal case. Call the breakpoint's print_it method. */
4613 return b->ops->print_it (bs);
4f8d1dc6 4614 }
348d480f 4615 break;
3086aeae 4616
e514a9d6 4617 default:
8e65ff28 4618 internal_error (__FILE__, __LINE__,
e2e0b3e5 4619 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4620 break;
c906108c 4621 }
c906108c
SS
4622}
4623
edcc5120
TT
4624/* A helper function that prints a shared library stopped event. */
4625
4626static void
4627print_solib_event (int is_catchpoint)
4628{
4629 int any_deleted
4630 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4631 int any_added
4632 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4633
4634 if (!is_catchpoint)
4635 {
4636 if (any_added || any_deleted)
112e8700 4637 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4638 else
112e8700
SM
4639 current_uiout->text (_("Stopped due to shared library event (no "
4640 "libraries added or removed)\n"));
edcc5120
TT
4641 }
4642
112e8700
SM
4643 if (current_uiout->is_mi_like_p ())
4644 current_uiout->field_string ("reason",
4645 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4646
4647 if (any_deleted)
4648 {
edcc5120
TT
4649 char *name;
4650 int ix;
4651
112e8700 4652 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4653 ui_out_emit_list list_emitter (current_uiout, "removed");
edcc5120
TT
4654 for (ix = 0;
4655 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4656 ix, name);
4657 ++ix)
4658 {
4659 if (ix > 0)
112e8700
SM
4660 current_uiout->text (" ");
4661 current_uiout->field_string ("library", name);
4662 current_uiout->text ("\n");
edcc5120 4663 }
edcc5120
TT
4664 }
4665
4666 if (any_added)
4667 {
4668 struct so_list *iter;
4669 int ix;
edcc5120 4670
112e8700 4671 current_uiout->text (_(" Inferior loaded "));
10f489e5 4672 ui_out_emit_list list_emitter (current_uiout, "added");
edcc5120
TT
4673 for (ix = 0;
4674 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4675 ix, iter);
4676 ++ix)
4677 {
4678 if (ix > 0)
112e8700
SM
4679 current_uiout->text (" ");
4680 current_uiout->field_string ("library", iter->so_name);
4681 current_uiout->text ("\n");
edcc5120 4682 }
edcc5120
TT
4683 }
4684}
4685
e514a9d6
JM
4686/* Print a message indicating what happened. This is called from
4687 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4688 list - a list of the eventpoints that caused this stop. KIND is
4689 the target_waitkind for the stopping event. This
e514a9d6
JM
4690 routine calls the generic print routine for printing a message
4691 about reasons for stopping. This will print (for example) the
4692 "Breakpoint n," part of the output. The return value of this
4693 routine is one of:
c906108c 4694
4a64f543 4695 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4696 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4697 code to print the location. An example is
c5aa993b
JM
4698 "Breakpoint 1, " which should be followed by
4699 the location.
917317f4 4700 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4701 to also print the location part of the message.
4702 An example is the catch/throw messages, which
4a64f543 4703 don't require a location appended to the end.
917317f4 4704 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4705 further info to be printed. */
c906108c 4706
917317f4 4707enum print_stop_action
36dfb11c 4708bpstat_print (bpstat bs, int kind)
c906108c 4709{
f486487f 4710 enum print_stop_action val;
c5aa993b 4711
c906108c 4712 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4713 (Currently all watchpoints go on the bpstat whether hit or not.
4714 That probably could (should) be changed, provided care is taken
c906108c 4715 with respect to bpstat_explains_signal). */
e514a9d6
JM
4716 for (; bs; bs = bs->next)
4717 {
4718 val = print_bp_stop_message (bs);
4719 if (val == PRINT_SRC_ONLY
4720 || val == PRINT_SRC_AND_LOC
4721 || val == PRINT_NOTHING)
4722 return val;
4723 }
c906108c 4724
36dfb11c
TT
4725 /* If we had hit a shared library event breakpoint,
4726 print_bp_stop_message would print out this message. If we hit an
4727 OS-level shared library event, do the same thing. */
4728 if (kind == TARGET_WAITKIND_LOADED)
4729 {
edcc5120 4730 print_solib_event (0);
36dfb11c
TT
4731 return PRINT_NOTHING;
4732 }
4733
e514a9d6 4734 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4735 with and nothing was printed. */
917317f4 4736 return PRINT_UNKNOWN;
c906108c
SS
4737}
4738
bf469271 4739/* Evaluate the boolean expression EXP and return the result. */
c906108c 4740
bf469271
PA
4741static bool
4742breakpoint_cond_eval (expression *exp)
c906108c 4743{
278cd55f 4744 struct value *mark = value_mark ();
bf469271 4745 bool res = value_true (evaluate_expression (exp));
cc59ec59 4746
c906108c 4747 value_free_to_mark (mark);
bf469271 4748 return res;
c906108c
SS
4749}
4750
5760d0ab 4751/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4752
04afa70c
TT
4753bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4754 : next (NULL),
4755 bp_location_at (bl),
4756 breakpoint_at (bl->owner),
4757 commands (NULL),
4758 old_val (NULL),
4759 print (0),
4760 stop (0),
4761 print_it (print_it_normal)
c906108c 4762{
f431efe5 4763 incref_bp_location (bl);
04afa70c
TT
4764 **bs_link_pointer = this;
4765 *bs_link_pointer = &next;
4766}
4767
4768bpstats::bpstats ()
4769 : next (NULL),
4770 bp_location_at (NULL),
4771 breakpoint_at (NULL),
4772 commands (NULL),
4773 old_val (NULL),
4774 print (0),
4775 stop (0),
4776 print_it (print_it_normal)
4777{
c906108c
SS
4778}
4779\f
d983da9c
DJ
4780/* The target has stopped with waitstatus WS. Check if any hardware
4781 watchpoints have triggered, according to the target. */
4782
4783int
4784watchpoints_triggered (struct target_waitstatus *ws)
4785{
d92524f1 4786 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4787 CORE_ADDR addr;
4788 struct breakpoint *b;
4789
4790 if (!stopped_by_watchpoint)
4791 {
4792 /* We were not stopped by a watchpoint. Mark all watchpoints
4793 as not triggered. */
4794 ALL_BREAKPOINTS (b)
cc60f2e3 4795 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4796 {
4797 struct watchpoint *w = (struct watchpoint *) b;
4798
4799 w->watchpoint_triggered = watch_triggered_no;
4800 }
d983da9c
DJ
4801
4802 return 0;
4803 }
4804
4805 if (!target_stopped_data_address (&current_target, &addr))
4806 {
4807 /* We were stopped by a watchpoint, but we don't know where.
4808 Mark all watchpoints as unknown. */
4809 ALL_BREAKPOINTS (b)
cc60f2e3 4810 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4811 {
4812 struct watchpoint *w = (struct watchpoint *) b;
4813
4814 w->watchpoint_triggered = watch_triggered_unknown;
4815 }
d983da9c 4816
3c4797ba 4817 return 1;
d983da9c
DJ
4818 }
4819
4820 /* The target could report the data address. Mark watchpoints
4821 affected by this data address as triggered, and all others as not
4822 triggered. */
4823
4824 ALL_BREAKPOINTS (b)
cc60f2e3 4825 if (is_hardware_watchpoint (b))
d983da9c 4826 {
3a5c3e22 4827 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4828 struct bp_location *loc;
d983da9c 4829
3a5c3e22 4830 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4831 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4832 {
3a5c3e22 4833 if (is_masked_watchpoint (b))
9c06b0b4 4834 {
3a5c3e22
PA
4835 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4836 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4837
4838 if (newaddr == start)
4839 {
3a5c3e22 4840 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4841 break;
4842 }
4843 }
4844 /* Exact match not required. Within range is sufficient. */
4845 else if (target_watchpoint_addr_within_range (&current_target,
4846 addr, loc->address,
4847 loc->length))
4848 {
3a5c3e22 4849 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4850 break;
4851 }
4852 }
d983da9c
DJ
4853 }
4854
4855 return 1;
4856}
4857
bf469271
PA
4858/* Possible return values for watchpoint_check. */
4859enum wp_check_result
4860 {
4861 /* The watchpoint has been deleted. */
4862 WP_DELETED = 1,
4863
4864 /* The value has changed. */
4865 WP_VALUE_CHANGED = 2,
4866
4867 /* The value has not changed. */
4868 WP_VALUE_NOT_CHANGED = 3,
4869
4870 /* Ignore this watchpoint, no matter if the value changed or not. */
4871 WP_IGNORE = 4,
4872 };
c906108c
SS
4873
4874#define BP_TEMPFLAG 1
4875#define BP_HARDWAREFLAG 2
4876
4a64f543 4877/* Evaluate watchpoint condition expression and check if its value
bf469271 4878 changed. */
553e4c11 4879
bf469271
PA
4880static wp_check_result
4881watchpoint_check (bpstat bs)
c906108c 4882{
3a5c3e22 4883 struct watchpoint *b;
c906108c
SS
4884 struct frame_info *fr;
4885 int within_current_scope;
4886
f431efe5 4887 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4888 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4889 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4890
f6bc2008
PA
4891 /* If this is a local watchpoint, we only want to check if the
4892 watchpoint frame is in scope if the current thread is the thread
4893 that was used to create the watchpoint. */
4894 if (!watchpoint_in_thread_scope (b))
60e1c644 4895 return WP_IGNORE;
f6bc2008 4896
c906108c
SS
4897 if (b->exp_valid_block == NULL)
4898 within_current_scope = 1;
4899 else
4900 {
edb3359d
DJ
4901 struct frame_info *frame = get_current_frame ();
4902 struct gdbarch *frame_arch = get_frame_arch (frame);
4903 CORE_ADDR frame_pc = get_frame_pc (frame);
4904
c9cf6e20 4905 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4906 still in the function but the stack frame has already been
4907 invalidated. Since we can't rely on the values of local
4908 variables after the stack has been destroyed, we are treating
4909 the watchpoint in that state as `not changed' without further
4910 checking. Don't mark watchpoints as changed if the current
4911 frame is in an epilogue - even if they are in some other
4912 frame, our view of the stack is likely to be wrong and
4913 frame_find_by_id could error out. */
c9cf6e20 4914 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4915 return WP_IGNORE;
a0f49112 4916
101dcfbe 4917 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4918 within_current_scope = (fr != NULL);
69fbadd5
DJ
4919
4920 /* If we've gotten confused in the unwinder, we might have
4921 returned a frame that can't describe this variable. */
edb3359d
DJ
4922 if (within_current_scope)
4923 {
4924 struct symbol *function;
4925
4926 function = get_frame_function (fr);
4927 if (function == NULL
4928 || !contained_in (b->exp_valid_block,
4929 SYMBOL_BLOCK_VALUE (function)))
4930 within_current_scope = 0;
4931 }
69fbadd5 4932
edb3359d 4933 if (within_current_scope)
c906108c
SS
4934 /* If we end up stopping, the current frame will get selected
4935 in normal_stop. So this call to select_frame won't affect
4936 the user. */
0f7d239c 4937 select_frame (fr);
c906108c 4938 }
c5aa993b 4939
c906108c
SS
4940 if (within_current_scope)
4941 {
4a64f543
MS
4942 /* We use value_{,free_to_}mark because it could be a *long*
4943 time before we return to the command level and call
4944 free_all_values. We can't call free_all_values because we
4945 might be in the middle of evaluating a function call. */
c906108c 4946
0cf6dd15 4947 int pc = 0;
9c06b0b4 4948 struct value *mark;
fa4727a6
DJ
4949 struct value *new_val;
4950
c1fc2657 4951 if (is_masked_watchpoint (b))
9c06b0b4
TJB
4952 /* Since we don't know the exact trigger address (from
4953 stopped_data_address), just tell the user we've triggered
4954 a mask watchpoint. */
4955 return WP_VALUE_CHANGED;
4956
4957 mark = value_mark ();
4d01a485 4958 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 4959
bb9d5f81
PP
4960 if (b->val_bitsize != 0)
4961 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4962
4a64f543
MS
4963 /* We use value_equal_contents instead of value_equal because
4964 the latter coerces an array to a pointer, thus comparing just
4965 the address of the array instead of its contents. This is
4966 not what we want. */
fa4727a6 4967 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 4968 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 4969 {
fa4727a6
DJ
4970 if (new_val != NULL)
4971 {
4972 release_value (new_val);
4973 value_free_to_mark (mark);
4974 }
c906108c
SS
4975 bs->old_val = b->val;
4976 b->val = new_val;
fa4727a6 4977 b->val_valid = 1;
c906108c
SS
4978 return WP_VALUE_CHANGED;
4979 }
4980 else
4981 {
60e1c644 4982 /* Nothing changed. */
c906108c 4983 value_free_to_mark (mark);
c906108c
SS
4984 return WP_VALUE_NOT_CHANGED;
4985 }
4986 }
4987 else
4988 {
4989 /* This seems like the only logical thing to do because
c5aa993b
JM
4990 if we temporarily ignored the watchpoint, then when
4991 we reenter the block in which it is valid it contains
4992 garbage (in the case of a function, it may have two
4993 garbage values, one before and one after the prologue).
4994 So we can't even detect the first assignment to it and
4995 watch after that (since the garbage may or may not equal
4996 the first value assigned). */
348d480f
PA
4997 /* We print all the stop information in
4998 breakpoint_ops->print_it, but in this case, by the time we
4999 call breakpoint_ops->print_it this bp will be deleted
5000 already. So we have no choice but print the information
5001 here. */
468afe6c 5002
0e454242 5003 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
5004 {
5005 struct ui_out *uiout = current_uiout;
5006
112e8700
SM
5007 if (uiout->is_mi_like_p ())
5008 uiout->field_string
5009 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5010 uiout->text ("\nWatchpoint ");
c1fc2657 5011 uiout->field_int ("wpnum", b->number);
112e8700 5012 uiout->text (" deleted because the program has left the block in\n"
468afe6c
PA
5013 "which its expression is valid.\n");
5014 }
4ce44c66 5015
cdac0397 5016 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5017 b->commands = NULL;
d0fb5eae 5018 watchpoint_del_at_next_stop (b);
c906108c
SS
5019
5020 return WP_DELETED;
5021 }
5022}
5023
18a18393 5024/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5025 breakpoint location BL. This function does not check if we should
5026 stop, only if BL explains the stop. */
5027
18a18393 5028static int
6c95b8df 5029bpstat_check_location (const struct bp_location *bl,
accd0bcd 5030 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5031 const struct target_waitstatus *ws)
18a18393
VP
5032{
5033 struct breakpoint *b = bl->owner;
5034
348d480f 5035 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5036 gdb_assert (b != NULL);
5037
bd522513 5038 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5039}
5040
3a5c3e22
PA
5041/* Determine if the watched values have actually changed, and we
5042 should stop. If not, set BS->stop to 0. */
5043
18a18393
VP
5044static void
5045bpstat_check_watchpoint (bpstat bs)
5046{
2bdf28a0 5047 const struct bp_location *bl;
3a5c3e22 5048 struct watchpoint *b;
2bdf28a0
JK
5049
5050 /* BS is built for existing struct breakpoint. */
f431efe5 5051 bl = bs->bp_location_at;
2bdf28a0 5052 gdb_assert (bl != NULL);
3a5c3e22 5053 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5054 gdb_assert (b != NULL);
18a18393 5055
18a18393 5056 {
18a18393
VP
5057 int must_check_value = 0;
5058
c1fc2657 5059 if (b->type == bp_watchpoint)
18a18393
VP
5060 /* For a software watchpoint, we must always check the
5061 watched value. */
5062 must_check_value = 1;
5063 else if (b->watchpoint_triggered == watch_triggered_yes)
5064 /* We have a hardware watchpoint (read, write, or access)
5065 and the target earlier reported an address watched by
5066 this watchpoint. */
5067 must_check_value = 1;
5068 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5069 && b->type == bp_hardware_watchpoint)
18a18393
VP
5070 /* We were stopped by a hardware watchpoint, but the target could
5071 not report the data address. We must check the watchpoint's
5072 value. Access and read watchpoints are out of luck; without
5073 a data address, we can't figure it out. */
5074 must_check_value = 1;
3a5c3e22 5075
18a18393
VP
5076 if (must_check_value)
5077 {
bf469271
PA
5078 wp_check_result e;
5079
5080 TRY
5081 {
5082 e = watchpoint_check (bs);
5083 }
5084 CATCH (ex, RETURN_MASK_ALL)
5085 {
5086 exception_fprintf (gdb_stderr, ex,
5087 "Error evaluating expression "
5088 "for watchpoint %d\n",
5089 b->number);
5090
5091 SWITCH_THRU_ALL_UIS ()
5092 {
5093 printf_filtered (_("Watchpoint %d deleted.\n"),
5094 b->number);
5095 }
5096 watchpoint_del_at_next_stop (b);
5097 e = WP_DELETED;
5098 }
5099 END_CATCH
5100
18a18393
VP
5101 switch (e)
5102 {
5103 case WP_DELETED:
5104 /* We've already printed what needs to be printed. */
5105 bs->print_it = print_it_done;
5106 /* Stop. */
5107 break;
60e1c644
PA
5108 case WP_IGNORE:
5109 bs->print_it = print_it_noop;
5110 bs->stop = 0;
5111 break;
18a18393 5112 case WP_VALUE_CHANGED:
c1fc2657 5113 if (b->type == bp_read_watchpoint)
18a18393 5114 {
85d721b8
PA
5115 /* There are two cases to consider here:
5116
4a64f543 5117 1. We're watching the triggered memory for reads.
85d721b8
PA
5118 In that case, trust the target, and always report
5119 the watchpoint hit to the user. Even though
5120 reads don't cause value changes, the value may
5121 have changed since the last time it was read, and
5122 since we're not trapping writes, we will not see
5123 those, and as such we should ignore our notion of
5124 old value.
5125
4a64f543 5126 2. We're watching the triggered memory for both
85d721b8
PA
5127 reads and writes. There are two ways this may
5128 happen:
5129
4a64f543 5130 2.1. This is a target that can't break on data
85d721b8
PA
5131 reads only, but can break on accesses (reads or
5132 writes), such as e.g., x86. We detect this case
5133 at the time we try to insert read watchpoints.
5134
4a64f543 5135 2.2. Otherwise, the target supports read
85d721b8
PA
5136 watchpoints, but, the user set an access or write
5137 watchpoint watching the same memory as this read
5138 watchpoint.
5139
5140 If we're watching memory writes as well as reads,
5141 ignore watchpoint hits when we find that the
5142 value hasn't changed, as reads don't cause
5143 changes. This still gives false positives when
5144 the program writes the same value to memory as
5145 what there was already in memory (we will confuse
5146 it for a read), but it's much better than
5147 nothing. */
5148
5149 int other_write_watchpoint = 0;
5150
5151 if (bl->watchpoint_type == hw_read)
5152 {
5153 struct breakpoint *other_b;
5154
5155 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5156 if (other_b->type == bp_hardware_watchpoint
5157 || other_b->type == bp_access_watchpoint)
85d721b8 5158 {
3a5c3e22
PA
5159 struct watchpoint *other_w =
5160 (struct watchpoint *) other_b;
5161
5162 if (other_w->watchpoint_triggered
5163 == watch_triggered_yes)
5164 {
5165 other_write_watchpoint = 1;
5166 break;
5167 }
85d721b8
PA
5168 }
5169 }
5170
5171 if (other_write_watchpoint
5172 || bl->watchpoint_type == hw_access)
5173 {
5174 /* We're watching the same memory for writes,
5175 and the value changed since the last time we
5176 updated it, so this trap must be for a write.
5177 Ignore it. */
5178 bs->print_it = print_it_noop;
5179 bs->stop = 0;
5180 }
18a18393
VP
5181 }
5182 break;
5183 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5184 if (b->type == bp_hardware_watchpoint
5185 || b->type == bp_watchpoint)
18a18393
VP
5186 {
5187 /* Don't stop: write watchpoints shouldn't fire if
5188 the value hasn't changed. */
5189 bs->print_it = print_it_noop;
5190 bs->stop = 0;
5191 }
5192 /* Stop. */
5193 break;
5194 default:
5195 /* Can't happen. */
18a18393
VP
5196 break;
5197 }
5198 }
5199 else /* must_check_value == 0 */
5200 {
5201 /* This is a case where some watchpoint(s) triggered, but
5202 not at the address of this watchpoint, or else no
5203 watchpoint triggered after all. So don't print
5204 anything for this watchpoint. */
5205 bs->print_it = print_it_noop;
5206 bs->stop = 0;
5207 }
5208 }
5209}
5210
7d4df6a4
DE
5211/* For breakpoints that are currently marked as telling gdb to stop,
5212 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5213 of breakpoint referred to by BS. If we should not stop for this
5214 breakpoint, set BS->stop to 0. */
f431efe5 5215
18a18393
VP
5216static void
5217bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5218{
2bdf28a0
JK
5219 const struct bp_location *bl;
5220 struct breakpoint *b;
bf469271
PA
5221 /* Assume stop. */
5222 bool condition_result = true;
7d4df6a4
DE
5223 struct expression *cond;
5224
5225 gdb_assert (bs->stop);
2bdf28a0
JK
5226
5227 /* BS is built for existing struct breakpoint. */
f431efe5 5228 bl = bs->bp_location_at;
2bdf28a0 5229 gdb_assert (bl != NULL);
f431efe5 5230 b = bs->breakpoint_at;
2bdf28a0 5231 gdb_assert (b != NULL);
18a18393 5232
b775012e
LM
5233 /* Even if the target evaluated the condition on its end and notified GDB, we
5234 need to do so again since GDB does not know if we stopped due to a
5235 breakpoint or a single step breakpoint. */
5236
18a18393 5237 if (frame_id_p (b->frame_id)
edb3359d 5238 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5239 {
7d4df6a4
DE
5240 bs->stop = 0;
5241 return;
5242 }
60e1c644 5243
12ab52e9
PA
5244 /* If this is a thread/task-specific breakpoint, don't waste cpu
5245 evaluating the condition if this isn't the specified
5246 thread/task. */
5d5658a1 5247 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5248 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5249
6c1b0f7b
DE
5250 {
5251 bs->stop = 0;
5252 return;
5253 }
5254
6dddc817
DE
5255 /* Evaluate extension language breakpoints that have a "stop" method
5256 implemented. */
5257 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5258
7d4df6a4
DE
5259 if (is_watchpoint (b))
5260 {
5261 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5262
4d01a485 5263 cond = w->cond_exp.get ();
7d4df6a4
DE
5264 }
5265 else
4d01a485 5266 cond = bl->cond.get ();
60e1c644 5267
7d4df6a4
DE
5268 if (cond && b->disposition != disp_del_at_next_stop)
5269 {
5270 int within_current_scope = 1;
5271 struct watchpoint * w;
60e1c644 5272
7d4df6a4
DE
5273 /* We use value_mark and value_free_to_mark because it could
5274 be a long time before we return to the command level and
5275 call free_all_values. We can't call free_all_values
5276 because we might be in the middle of evaluating a
5277 function call. */
5278 struct value *mark = value_mark ();
5279
5280 if (is_watchpoint (b))
5281 w = (struct watchpoint *) b;
5282 else
5283 w = NULL;
5284
5285 /* Need to select the frame, with all that implies so that
5286 the conditions will have the right context. Because we
5287 use the frame, we will not see an inlined function's
5288 variables when we arrive at a breakpoint at the start
5289 of the inlined function; the current frame will be the
5290 call site. */
5291 if (w == NULL || w->cond_exp_valid_block == NULL)
5292 select_frame (get_current_frame ());
5293 else
18a18393 5294 {
7d4df6a4
DE
5295 struct frame_info *frame;
5296
5297 /* For local watchpoint expressions, which particular
5298 instance of a local is being watched matters, so we
5299 keep track of the frame to evaluate the expression
5300 in. To evaluate the condition however, it doesn't
5301 really matter which instantiation of the function
5302 where the condition makes sense triggers the
5303 watchpoint. This allows an expression like "watch
5304 global if q > 10" set in `func', catch writes to
5305 global on all threads that call `func', or catch
5306 writes on all recursive calls of `func' by a single
5307 thread. We simply always evaluate the condition in
5308 the innermost frame that's executing where it makes
5309 sense to evaluate the condition. It seems
5310 intuitive. */
5311 frame = block_innermost_frame (w->cond_exp_valid_block);
5312 if (frame != NULL)
5313 select_frame (frame);
5314 else
5315 within_current_scope = 0;
18a18393 5316 }
7d4df6a4 5317 if (within_current_scope)
bf469271
PA
5318 {
5319 TRY
5320 {
5321 condition_result = breakpoint_cond_eval (cond);
5322 }
5323 CATCH (ex, RETURN_MASK_ALL)
5324 {
5325 exception_fprintf (gdb_stderr, ex,
5326 "Error in testing breakpoint condition:\n");
5327 }
5328 END_CATCH
5329 }
7d4df6a4 5330 else
18a18393 5331 {
7d4df6a4
DE
5332 warning (_("Watchpoint condition cannot be tested "
5333 "in the current scope"));
5334 /* If we failed to set the right context for this
5335 watchpoint, unconditionally report it. */
18a18393 5336 }
7d4df6a4
DE
5337 /* FIXME-someday, should give breakpoint #. */
5338 value_free_to_mark (mark);
18a18393 5339 }
7d4df6a4 5340
bf469271 5341 if (cond && !condition_result)
7d4df6a4
DE
5342 {
5343 bs->stop = 0;
5344 }
7d4df6a4
DE
5345 else if (b->ignore_count > 0)
5346 {
5347 b->ignore_count--;
5348 bs->stop = 0;
5349 /* Increase the hit count even though we don't stop. */
5350 ++(b->hit_count);
5351 observer_notify_breakpoint_modified (b);
5352 }
18a18393
VP
5353}
5354
1cf4d951
PA
5355/* Returns true if we need to track moribund locations of LOC's type
5356 on the current target. */
5357
5358static int
5359need_moribund_for_location_type (struct bp_location *loc)
5360{
5361 return ((loc->loc_type == bp_loc_software_breakpoint
5362 && !target_supports_stopped_by_sw_breakpoint ())
5363 || (loc->loc_type == bp_loc_hardware_breakpoint
5364 && !target_supports_stopped_by_hw_breakpoint ()));
5365}
5366
18a18393 5367
9709f61c 5368/* Get a bpstat associated with having just stopped at address
d983da9c 5369 BP_ADDR in thread PTID.
c906108c 5370
d983da9c 5371 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5372 don't understand this stop. Result is a chain of bpstat's such
5373 that:
c906108c 5374
c5aa993b 5375 if we don't understand the stop, the result is a null pointer.
c906108c 5376
c5aa993b 5377 if we understand why we stopped, the result is not null.
c906108c 5378
c5aa993b
JM
5379 Each element of the chain refers to a particular breakpoint or
5380 watchpoint at which we have stopped. (We may have stopped for
5381 several reasons concurrently.)
c906108c 5382
c5aa993b
JM
5383 Each element of the chain has valid next, breakpoint_at,
5384 commands, FIXME??? fields. */
c906108c
SS
5385
5386bpstat
accd0bcd 5387bpstat_stop_status (const address_space *aspace,
09ac7c10
TT
5388 CORE_ADDR bp_addr, ptid_t ptid,
5389 const struct target_waitstatus *ws)
c906108c 5390{
0d381245 5391 struct breakpoint *b = NULL;
afe38095 5392 struct bp_location *bl;
20874c92 5393 struct bp_location *loc;
5760d0ab
JK
5394 /* First item of allocated bpstat's. */
5395 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5396 /* Pointer to the last thing in the chain currently. */
5760d0ab 5397 bpstat bs;
20874c92 5398 int ix;
429374b8 5399 int need_remove_insert;
f431efe5 5400 int removed_any;
c906108c 5401
f431efe5
PA
5402 /* First, build the bpstat chain with locations that explain a
5403 target stop, while being careful to not set the target running,
5404 as that may invalidate locations (in particular watchpoint
5405 locations are recreated). Resuming will happen here with
5406 breakpoint conditions or watchpoint expressions that include
5407 inferior function calls. */
c5aa993b 5408
429374b8
JK
5409 ALL_BREAKPOINTS (b)
5410 {
1a853c52 5411 if (!breakpoint_enabled (b))
429374b8 5412 continue;
a5606eee 5413
429374b8
JK
5414 for (bl = b->loc; bl != NULL; bl = bl->next)
5415 {
4a64f543
MS
5416 /* For hardware watchpoints, we look only at the first
5417 location. The watchpoint_check function will work on the
5418 entire expression, not the individual locations. For
5419 read watchpoints, the watchpoints_triggered function has
5420 checked all locations already. */
429374b8
JK
5421 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5422 break;
18a18393 5423
f6592439 5424 if (!bl->enabled || bl->shlib_disabled)
429374b8 5425 continue;
c5aa993b 5426
09ac7c10 5427 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5428 continue;
c5aa993b 5429
4a64f543
MS
5430 /* Come here if it's a watchpoint, or if the break address
5431 matches. */
c5aa993b 5432
04afa70c 5433 bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
4a64f543 5434 explain stop. */
c5aa993b 5435
f431efe5
PA
5436 /* Assume we stop. Should we find a watchpoint that is not
5437 actually triggered, or if the condition of the breakpoint
5438 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5439 bs->stop = 1;
5440 bs->print = 1;
d983da9c 5441
f431efe5
PA
5442 /* If this is a scope breakpoint, mark the associated
5443 watchpoint as triggered so that we will handle the
5444 out-of-scope event. We'll get to the watchpoint next
5445 iteration. */
d0fb5eae 5446 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5447 {
5448 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5449
5450 w->watchpoint_triggered = watch_triggered_yes;
5451 }
f431efe5
PA
5452 }
5453 }
5454
7c16b83e 5455 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5456 if (!target_supports_stopped_by_sw_breakpoint ()
5457 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5458 {
1cf4d951 5459 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5460 {
1cf4d951
PA
5461 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5462 && need_moribund_for_location_type (loc))
5463 {
04afa70c 5464 bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5465 /* For hits of moribund locations, we should just proceed. */
5466 bs->stop = 0;
5467 bs->print = 0;
5468 bs->print_it = print_it_noop;
5469 }
f431efe5
PA
5470 }
5471 }
5472
edcc5120
TT
5473 /* A bit of special processing for shlib breakpoints. We need to
5474 process solib loading here, so that the lists of loaded and
5475 unloaded libraries are correct before we handle "catch load" and
5476 "catch unload". */
5477 for (bs = bs_head; bs != NULL; bs = bs->next)
5478 {
5d268276 5479 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5480 {
5481 handle_solib_event ();
5482 break;
5483 }
5484 }
5485
f431efe5
PA
5486 /* Now go through the locations that caused the target to stop, and
5487 check whether we're interested in reporting this stop to higher
5488 layers, or whether we should resume the target transparently. */
5489
5490 removed_any = 0;
5491
5760d0ab 5492 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5493 {
5494 if (!bs->stop)
5495 continue;
5496
f431efe5 5497 b = bs->breakpoint_at;
348d480f
PA
5498 b->ops->check_status (bs);
5499 if (bs->stop)
28010a5d 5500 {
348d480f 5501 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5502
429374b8
JK
5503 if (bs->stop)
5504 {
5505 ++(b->hit_count);
8d3788bd 5506 observer_notify_breakpoint_modified (b);
c906108c 5507
4a64f543 5508 /* We will stop here. */
429374b8
JK
5509 if (b->disposition == disp_disable)
5510 {
816338b5 5511 --(b->enable_count);
1a853c52 5512 if (b->enable_count <= 0)
429374b8 5513 b->enable_state = bp_disabled;
f431efe5 5514 removed_any = 1;
429374b8
JK
5515 }
5516 if (b->silent)
5517 bs->print = 0;
5518 bs->commands = b->commands;
abf85f46 5519 if (command_line_is_silent (bs->commands
d1b0a7bf 5520 ? bs->commands.get () : NULL))
abf85f46 5521 bs->print = 0;
9d6e6e84
HZ
5522
5523 b->ops->after_condition_true (bs);
429374b8
JK
5524 }
5525
348d480f 5526 }
a9b3a50f
PA
5527
5528 /* Print nothing for this entry if we don't stop or don't
5529 print. */
5530 if (!bs->stop || !bs->print)
5531 bs->print_it = print_it_noop;
429374b8 5532 }
876fa593 5533
d983da9c
DJ
5534 /* If we aren't stopping, the value of some hardware watchpoint may
5535 not have changed, but the intermediate memory locations we are
5536 watching may have. Don't bother if we're stopping; this will get
5537 done later. */
d832cb68 5538 need_remove_insert = 0;
5760d0ab
JK
5539 if (! bpstat_causes_stop (bs_head))
5540 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5541 if (!bs->stop
f431efe5
PA
5542 && bs->breakpoint_at
5543 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5544 {
3a5c3e22
PA
5545 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5546
5547 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5548 need_remove_insert = 1;
d983da9c
DJ
5549 }
5550
d832cb68 5551 if (need_remove_insert)
44702360 5552 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5553 else if (removed_any)
44702360 5554 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5555
5760d0ab 5556 return bs_head;
c906108c 5557}
628fe4e4
JK
5558
5559static void
5560handle_jit_event (void)
5561{
5562 struct frame_info *frame;
5563 struct gdbarch *gdbarch;
5564
243a9253
PA
5565 if (debug_infrun)
5566 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5567
628fe4e4
JK
5568 /* Switch terminal for any messages produced by
5569 breakpoint_re_set. */
223ffa71 5570 target_terminal::ours_for_output ();
628fe4e4
JK
5571
5572 frame = get_current_frame ();
5573 gdbarch = get_frame_arch (frame);
5574
5575 jit_event_handler (gdbarch);
5576
223ffa71 5577 target_terminal::inferior ();
628fe4e4
JK
5578}
5579
5580/* Prepare WHAT final decision for infrun. */
5581
5582/* Decide what infrun needs to do with this bpstat. */
5583
c906108c 5584struct bpstat_what
0e30163f 5585bpstat_what (bpstat bs_head)
c906108c 5586{
c906108c 5587 struct bpstat_what retval;
0e30163f 5588 bpstat bs;
c906108c 5589
628fe4e4 5590 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5591 retval.call_dummy = STOP_NONE;
186c406b 5592 retval.is_longjmp = 0;
628fe4e4 5593
0e30163f 5594 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5595 {
628fe4e4
JK
5596 /* Extract this BS's action. After processing each BS, we check
5597 if its action overrides all we've seem so far. */
5598 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5599 enum bptype bptype;
5600
c906108c 5601 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5602 {
5603 /* I suspect this can happen if it was a momentary
5604 breakpoint which has since been deleted. */
5605 bptype = bp_none;
5606 }
20874c92 5607 else
f431efe5 5608 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5609
5610 switch (bptype)
c906108c
SS
5611 {
5612 case bp_none:
628fe4e4 5613 break;
c906108c
SS
5614 case bp_breakpoint:
5615 case bp_hardware_breakpoint:
7c16b83e 5616 case bp_single_step:
c906108c
SS
5617 case bp_until:
5618 case bp_finish:
a9b3a50f 5619 case bp_shlib_event:
c906108c
SS
5620 if (bs->stop)
5621 {
5622 if (bs->print)
628fe4e4 5623 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5624 else
628fe4e4 5625 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5626 }
5627 else
628fe4e4 5628 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5629 break;
5630 case bp_watchpoint:
5631 case bp_hardware_watchpoint:
5632 case bp_read_watchpoint:
5633 case bp_access_watchpoint:
5634 if (bs->stop)
5635 {
5636 if (bs->print)
628fe4e4 5637 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5638 else
628fe4e4 5639 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5640 }
5641 else
628fe4e4
JK
5642 {
5643 /* There was a watchpoint, but we're not stopping.
5644 This requires no further action. */
5645 }
c906108c
SS
5646 break;
5647 case bp_longjmp:
e2e4d78b 5648 case bp_longjmp_call_dummy:
186c406b 5649 case bp_exception:
0a39bb32
PA
5650 if (bs->stop)
5651 {
5652 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5653 retval.is_longjmp = bptype != bp_exception;
5654 }
5655 else
5656 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5657 break;
5658 case bp_longjmp_resume:
186c406b 5659 case bp_exception_resume:
0a39bb32
PA
5660 if (bs->stop)
5661 {
5662 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5663 retval.is_longjmp = bptype == bp_longjmp_resume;
5664 }
5665 else
5666 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5667 break;
5668 case bp_step_resume:
5669 if (bs->stop)
628fe4e4
JK
5670 this_action = BPSTAT_WHAT_STEP_RESUME;
5671 else
c906108c 5672 {
628fe4e4
JK
5673 /* It is for the wrong frame. */
5674 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5675 }
c906108c 5676 break;
2c03e5be
PA
5677 case bp_hp_step_resume:
5678 if (bs->stop)
5679 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5680 else
5681 {
5682 /* It is for the wrong frame. */
5683 this_action = BPSTAT_WHAT_SINGLE;
5684 }
5685 break;
c906108c 5686 case bp_watchpoint_scope:
c4093a6a 5687 case bp_thread_event:
1900040c 5688 case bp_overlay_event:
0fd8e87f 5689 case bp_longjmp_master:
aa7d318d 5690 case bp_std_terminate_master:
186c406b 5691 case bp_exception_master:
628fe4e4 5692 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5693 break;
ce78b96d 5694 case bp_catchpoint:
c5aa993b
JM
5695 if (bs->stop)
5696 {
5697 if (bs->print)
628fe4e4 5698 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5699 else
628fe4e4 5700 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5701 }
5702 else
628fe4e4
JK
5703 {
5704 /* There was a catchpoint, but we're not stopping.
5705 This requires no further action. */
5706 }
5707 break;
628fe4e4 5708 case bp_jit_event:
628fe4e4 5709 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5710 break;
c906108c 5711 case bp_call_dummy:
53a5351d
JM
5712 /* Make sure the action is stop (silent or noisy),
5713 so infrun.c pops the dummy frame. */
aa7d318d 5714 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5715 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5716 break;
5717 case bp_std_terminate:
5718 /* Make sure the action is stop (silent or noisy),
5719 so infrun.c pops the dummy frame. */
aa7d318d 5720 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5721 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5722 break;
1042e4c0 5723 case bp_tracepoint:
7a697b8d 5724 case bp_fast_tracepoint:
0fb4aa4b 5725 case bp_static_tracepoint:
1042e4c0
SS
5726 /* Tracepoint hits should not be reported back to GDB, and
5727 if one got through somehow, it should have been filtered
5728 out already. */
5729 internal_error (__FILE__, __LINE__,
7a697b8d 5730 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5731 break;
5732 case bp_gnu_ifunc_resolver:
5733 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5734 this_action = BPSTAT_WHAT_SINGLE;
5735 break;
5736 case bp_gnu_ifunc_resolver_return:
5737 /* The breakpoint will be removed, execution will restart from the
5738 PC of the former breakpoint. */
5739 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5740 break;
e7e0cddf
SS
5741
5742 case bp_dprintf:
a11cfd87
HZ
5743 if (bs->stop)
5744 this_action = BPSTAT_WHAT_STOP_SILENT;
5745 else
5746 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5747 break;
5748
628fe4e4
JK
5749 default:
5750 internal_error (__FILE__, __LINE__,
5751 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5752 }
628fe4e4 5753
325fac50 5754 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5755 }
628fe4e4 5756
243a9253
PA
5757 return retval;
5758}
628fe4e4 5759
243a9253
PA
5760void
5761bpstat_run_callbacks (bpstat bs_head)
5762{
5763 bpstat bs;
628fe4e4 5764
0e30163f
JK
5765 for (bs = bs_head; bs != NULL; bs = bs->next)
5766 {
5767 struct breakpoint *b = bs->breakpoint_at;
5768
5769 if (b == NULL)
5770 continue;
5771 switch (b->type)
5772 {
243a9253
PA
5773 case bp_jit_event:
5774 handle_jit_event ();
5775 break;
0e30163f
JK
5776 case bp_gnu_ifunc_resolver:
5777 gnu_ifunc_resolver_stop (b);
5778 break;
5779 case bp_gnu_ifunc_resolver_return:
5780 gnu_ifunc_resolver_return_stop (b);
5781 break;
5782 }
5783 }
c906108c
SS
5784}
5785
5786/* Nonzero if we should step constantly (e.g. watchpoints on machines
5787 without hardware support). This isn't related to a specific bpstat,
5788 just to things like whether watchpoints are set. */
5789
c5aa993b 5790int
fba45db2 5791bpstat_should_step (void)
c906108c
SS
5792{
5793 struct breakpoint *b;
cc59ec59 5794
c906108c 5795 ALL_BREAKPOINTS (b)
717a8278 5796 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5797 return 1;
c906108c
SS
5798 return 0;
5799}
5800
67822962
PA
5801int
5802bpstat_causes_stop (bpstat bs)
5803{
5804 for (; bs != NULL; bs = bs->next)
5805 if (bs->stop)
5806 return 1;
5807
5808 return 0;
5809}
5810
c906108c 5811\f
c5aa993b 5812
170b53b2
UW
5813/* Compute a string of spaces suitable to indent the next line
5814 so it starts at the position corresponding to the table column
5815 named COL_NAME in the currently active table of UIOUT. */
5816
5817static char *
5818wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5819{
5820 static char wrap_indent[80];
5821 int i, total_width, width, align;
c5209615 5822 const char *text;
170b53b2
UW
5823
5824 total_width = 0;
112e8700 5825 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5826 {
5827 if (strcmp (text, col_name) == 0)
5828 {
5829 gdb_assert (total_width < sizeof wrap_indent);
5830 memset (wrap_indent, ' ', total_width);
5831 wrap_indent[total_width] = 0;
5832
5833 return wrap_indent;
5834 }
5835
5836 total_width += width + 1;
5837 }
5838
5839 return NULL;
5840}
5841
b775012e
LM
5842/* Determine if the locations of this breakpoint will have their conditions
5843 evaluated by the target, host or a mix of both. Returns the following:
5844
5845 "host": Host evals condition.
5846 "host or target": Host or Target evals condition.
5847 "target": Target evals condition.
5848*/
5849
5850static const char *
5851bp_condition_evaluator (struct breakpoint *b)
5852{
5853 struct bp_location *bl;
5854 char host_evals = 0;
5855 char target_evals = 0;
5856
5857 if (!b)
5858 return NULL;
5859
5860 if (!is_breakpoint (b))
5861 return NULL;
5862
5863 if (gdb_evaluates_breakpoint_condition_p ()
5864 || !target_supports_evaluation_of_breakpoint_conditions ())
5865 return condition_evaluation_host;
5866
5867 for (bl = b->loc; bl; bl = bl->next)
5868 {
5869 if (bl->cond_bytecode)
5870 target_evals++;
5871 else
5872 host_evals++;
5873 }
5874
5875 if (host_evals && target_evals)
5876 return condition_evaluation_both;
5877 else if (target_evals)
5878 return condition_evaluation_target;
5879 else
5880 return condition_evaluation_host;
5881}
5882
5883/* Determine the breakpoint location's condition evaluator. This is
5884 similar to bp_condition_evaluator, but for locations. */
5885
5886static const char *
5887bp_location_condition_evaluator (struct bp_location *bl)
5888{
5889 if (bl && !is_breakpoint (bl->owner))
5890 return NULL;
5891
5892 if (gdb_evaluates_breakpoint_condition_p ()
5893 || !target_supports_evaluation_of_breakpoint_conditions ())
5894 return condition_evaluation_host;
5895
5896 if (bl && bl->cond_bytecode)
5897 return condition_evaluation_target;
5898 else
5899 return condition_evaluation_host;
5900}
5901
859825b8
JK
5902/* Print the LOC location out of the list of B->LOC locations. */
5903
170b53b2
UW
5904static void
5905print_breakpoint_location (struct breakpoint *b,
5906 struct bp_location *loc)
0d381245 5907{
79a45e25 5908 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5909
5910 scoped_restore_current_program_space restore_pspace;
6c95b8df 5911
859825b8
JK
5912 if (loc != NULL && loc->shlib_disabled)
5913 loc = NULL;
5914
6c95b8df
PA
5915 if (loc != NULL)
5916 set_current_program_space (loc->pspace);
5917
56435ebe 5918 if (b->display_canonical)
d28cd78a 5919 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5920 else if (loc && loc->symtab)
0d381245 5921 {
4a27f119
KS
5922 const struct symbol *sym = loc->symbol;
5923
5924 if (sym == NULL)
5925 sym = find_pc_sect_function (loc->address, loc->section);
5926
0d381245
VP
5927 if (sym)
5928 {
112e8700
SM
5929 uiout->text ("in ");
5930 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
5931 uiout->text (" ");
5932 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5933 uiout->text ("at ");
0d381245 5934 }
112e8700 5935 uiout->field_string ("file",
05cba821 5936 symtab_to_filename_for_display (loc->symtab));
112e8700 5937 uiout->text (":");
05cba821 5938
112e8700
SM
5939 if (uiout->is_mi_like_p ())
5940 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 5941
112e8700 5942 uiout->field_int ("line", loc->line_number);
0d381245 5943 }
859825b8 5944 else if (loc)
0d381245 5945 {
d7e74731 5946 string_file stb;
170b53b2 5947
d7e74731 5948 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 5949 demangle, "");
112e8700 5950 uiout->field_stream ("at", stb);
0d381245 5951 }
859825b8 5952 else
f00aae0f 5953 {
d28cd78a
TT
5954 uiout->field_string ("pending",
5955 event_location_to_string (b->location.get ()));
f00aae0f
KS
5956 /* If extra_string is available, it could be holding a condition
5957 or dprintf arguments. In either case, make sure it is printed,
5958 too, but only for non-MI streams. */
112e8700 5959 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
5960 {
5961 if (b->type == bp_dprintf)
112e8700 5962 uiout->text (",");
f00aae0f 5963 else
112e8700
SM
5964 uiout->text (" ");
5965 uiout->text (b->extra_string);
f00aae0f
KS
5966 }
5967 }
6c95b8df 5968
b775012e
LM
5969 if (loc && is_breakpoint (b)
5970 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5971 && bp_condition_evaluator (b) == condition_evaluation_both)
5972 {
112e8700
SM
5973 uiout->text (" (");
5974 uiout->field_string ("evaluated-by",
b775012e 5975 bp_location_condition_evaluator (loc));
112e8700 5976 uiout->text (")");
b775012e 5977 }
0d381245
VP
5978}
5979
269b11a2
PA
5980static const char *
5981bptype_string (enum bptype type)
c906108c 5982{
c4093a6a
JM
5983 struct ep_type_description
5984 {
5985 enum bptype type;
a121b7c1 5986 const char *description;
c4093a6a
JM
5987 };
5988 static struct ep_type_description bptypes[] =
c906108c 5989 {
c5aa993b
JM
5990 {bp_none, "?deleted?"},
5991 {bp_breakpoint, "breakpoint"},
c906108c 5992 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 5993 {bp_single_step, "sw single-step"},
c5aa993b
JM
5994 {bp_until, "until"},
5995 {bp_finish, "finish"},
5996 {bp_watchpoint, "watchpoint"},
c906108c 5997 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5998 {bp_read_watchpoint, "read watchpoint"},
5999 {bp_access_watchpoint, "acc watchpoint"},
6000 {bp_longjmp, "longjmp"},
6001 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6002 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6003 {bp_exception, "exception"},
6004 {bp_exception_resume, "exception resume"},
c5aa993b 6005 {bp_step_resume, "step resume"},
2c03e5be 6006 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6007 {bp_watchpoint_scope, "watchpoint scope"},
6008 {bp_call_dummy, "call dummy"},
aa7d318d 6009 {bp_std_terminate, "std::terminate"},
c5aa993b 6010 {bp_shlib_event, "shlib events"},
c4093a6a 6011 {bp_thread_event, "thread events"},
1900040c 6012 {bp_overlay_event, "overlay events"},
0fd8e87f 6013 {bp_longjmp_master, "longjmp master"},
aa7d318d 6014 {bp_std_terminate_master, "std::terminate master"},
186c406b 6015 {bp_exception_master, "exception master"},
ce78b96d 6016 {bp_catchpoint, "catchpoint"},
1042e4c0 6017 {bp_tracepoint, "tracepoint"},
7a697b8d 6018 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6019 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6020 {bp_dprintf, "dprintf"},
4efc6507 6021 {bp_jit_event, "jit events"},
0e30163f
JK
6022 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6023 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6024 };
269b11a2
PA
6025
6026 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6027 || ((int) type != bptypes[(int) type].type))
6028 internal_error (__FILE__, __LINE__,
6029 _("bptypes table does not describe type #%d."),
6030 (int) type);
6031
6032 return bptypes[(int) type].description;
6033}
6034
998580f1
MK
6035/* For MI, output a field named 'thread-groups' with a list as the value.
6036 For CLI, prefix the list with the string 'inf'. */
6037
6038static void
6039output_thread_groups (struct ui_out *uiout,
6040 const char *field_name,
6041 VEC(int) *inf_num,
6042 int mi_only)
6043{
112e8700 6044 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6045 int inf;
6046 int i;
6047
6048 /* For backward compatibility, don't display inferiors in CLI unless
6049 there are several. Always display them for MI. */
6050 if (!is_mi && mi_only)
6051 return;
6052
10f489e5 6053 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6054
998580f1
MK
6055 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6056 {
6057 if (is_mi)
6058 {
6059 char mi_group[10];
6060
6061 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
112e8700 6062 uiout->field_string (NULL, mi_group);
998580f1
MK
6063 }
6064 else
6065 {
6066 if (i == 0)
112e8700 6067 uiout->text (" inf ");
998580f1 6068 else
112e8700 6069 uiout->text (", ");
998580f1 6070
112e8700 6071 uiout->text (plongest (inf));
998580f1
MK
6072 }
6073 }
998580f1
MK
6074}
6075
269b11a2
PA
6076/* Print B to gdb_stdout. */
6077
6078static void
6079print_one_breakpoint_location (struct breakpoint *b,
6080 struct bp_location *loc,
6081 int loc_number,
6082 struct bp_location **last_loc,
269b11a2
PA
6083 int allflag)
6084{
6085 struct command_line *l;
c2c6d25f 6086 static char bpenables[] = "nynny";
c906108c 6087
79a45e25 6088 struct ui_out *uiout = current_uiout;
0d381245
VP
6089 int header_of_multiple = 0;
6090 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6091 struct value_print_options opts;
6092
6093 get_user_print_options (&opts);
0d381245
VP
6094
6095 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6096 /* See comment in print_one_breakpoint concerning treatment of
6097 breakpoints with single disabled location. */
0d381245
VP
6098 if (loc == NULL
6099 && (b->loc != NULL
6100 && (b->loc->next != NULL || !b->loc->enabled)))
6101 header_of_multiple = 1;
6102 if (loc == NULL)
6103 loc = b->loc;
6104
c4093a6a
JM
6105 annotate_record ();
6106
6107 /* 1 */
6108 annotate_field (0);
0d381245
VP
6109 if (part_of_multiple)
6110 {
6111 char *formatted;
0c6773c1 6112 formatted = xstrprintf ("%d.%d", b->number, loc_number);
112e8700 6113 uiout->field_string ("number", formatted);
0d381245
VP
6114 xfree (formatted);
6115 }
6116 else
6117 {
112e8700 6118 uiout->field_int ("number", b->number);
0d381245 6119 }
c4093a6a
JM
6120
6121 /* 2 */
6122 annotate_field (1);
0d381245 6123 if (part_of_multiple)
112e8700 6124 uiout->field_skip ("type");
269b11a2 6125 else
112e8700 6126 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6127
6128 /* 3 */
6129 annotate_field (2);
0d381245 6130 if (part_of_multiple)
112e8700 6131 uiout->field_skip ("disp");
0d381245 6132 else
112e8700 6133 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6134
c4093a6a
JM
6135
6136 /* 4 */
6137 annotate_field (3);
0d381245 6138 if (part_of_multiple)
112e8700 6139 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6140 else
112e8700
SM
6141 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6142 uiout->spaces (2);
0d381245 6143
c4093a6a
JM
6144
6145 /* 5 and 6 */
3086aeae 6146 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6147 {
4a64f543
MS
6148 /* Although the print_one can possibly print all locations,
6149 calling it here is not likely to get any nice result. So,
6150 make sure there's just one location. */
0d381245 6151 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6152 b->ops->print_one (b, last_loc);
0d381245 6153 }
3086aeae
DJ
6154 else
6155 switch (b->type)
6156 {
6157 case bp_none:
6158 internal_error (__FILE__, __LINE__,
e2e0b3e5 6159 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6160 break;
c906108c 6161
3086aeae
DJ
6162 case bp_watchpoint:
6163 case bp_hardware_watchpoint:
6164 case bp_read_watchpoint:
6165 case bp_access_watchpoint:
3a5c3e22
PA
6166 {
6167 struct watchpoint *w = (struct watchpoint *) b;
6168
6169 /* Field 4, the address, is omitted (which makes the columns
6170 not line up too nicely with the headers, but the effect
6171 is relatively readable). */
6172 if (opts.addressprint)
112e8700 6173 uiout->field_skip ("addr");
3a5c3e22 6174 annotate_field (5);
112e8700 6175 uiout->field_string ("what", w->exp_string);
3a5c3e22 6176 }
3086aeae
DJ
6177 break;
6178
3086aeae
DJ
6179 case bp_breakpoint:
6180 case bp_hardware_breakpoint:
7c16b83e 6181 case bp_single_step:
3086aeae
DJ
6182 case bp_until:
6183 case bp_finish:
6184 case bp_longjmp:
6185 case bp_longjmp_resume:
e2e4d78b 6186 case bp_longjmp_call_dummy:
186c406b
TT
6187 case bp_exception:
6188 case bp_exception_resume:
3086aeae 6189 case bp_step_resume:
2c03e5be 6190 case bp_hp_step_resume:
3086aeae
DJ
6191 case bp_watchpoint_scope:
6192 case bp_call_dummy:
aa7d318d 6193 case bp_std_terminate:
3086aeae
DJ
6194 case bp_shlib_event:
6195 case bp_thread_event:
6196 case bp_overlay_event:
0fd8e87f 6197 case bp_longjmp_master:
aa7d318d 6198 case bp_std_terminate_master:
186c406b 6199 case bp_exception_master:
1042e4c0 6200 case bp_tracepoint:
7a697b8d 6201 case bp_fast_tracepoint:
0fb4aa4b 6202 case bp_static_tracepoint:
e7e0cddf 6203 case bp_dprintf:
4efc6507 6204 case bp_jit_event:
0e30163f
JK
6205 case bp_gnu_ifunc_resolver:
6206 case bp_gnu_ifunc_resolver_return:
79a45b7d 6207 if (opts.addressprint)
3086aeae
DJ
6208 {
6209 annotate_field (4);
54e52265 6210 if (header_of_multiple)
112e8700 6211 uiout->field_string ("addr", "<MULTIPLE>");
e9bbd7c5 6212 else if (b->loc == NULL || loc->shlib_disabled)
112e8700 6213 uiout->field_string ("addr", "<PENDING>");
0101ce28 6214 else
112e8700 6215 uiout->field_core_addr ("addr",
5af949e3 6216 loc->gdbarch, loc->address);
3086aeae
DJ
6217 }
6218 annotate_field (5);
0d381245 6219 if (!header_of_multiple)
170b53b2 6220 print_breakpoint_location (b, loc);
0d381245 6221 if (b->loc)
a6d9a66e 6222 *last_loc = b->loc;
3086aeae
DJ
6223 break;
6224 }
c906108c 6225
6c95b8df 6226
998580f1 6227 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6228 {
6229 struct inferior *inf;
998580f1
MK
6230 VEC(int) *inf_num = NULL;
6231 int mi_only = 1;
6c95b8df 6232
998580f1 6233 ALL_INFERIORS (inf)
6c95b8df
PA
6234 {
6235 if (inf->pspace == loc->pspace)
998580f1 6236 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6237 }
998580f1
MK
6238
6239 /* For backward compatibility, don't display inferiors in CLI unless
6240 there are several. Always display for MI. */
6241 if (allflag
6242 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6243 && (number_of_program_spaces () > 1
6244 || number_of_inferiors () > 1)
6245 /* LOC is for existing B, it cannot be in
6246 moribund_locations and thus having NULL OWNER. */
6247 && loc->owner->type != bp_catchpoint))
6248 mi_only = 0;
6249 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6250 VEC_free (int, inf_num);
6c95b8df
PA
6251 }
6252
4a306c9a 6253 if (!part_of_multiple)
c4093a6a 6254 {
4a306c9a
JB
6255 if (b->thread != -1)
6256 {
6257 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6258 "stop only in" line a little further down. */
112e8700
SM
6259 uiout->text (" thread ");
6260 uiout->field_int ("thread", b->thread);
4a306c9a
JB
6261 }
6262 else if (b->task != 0)
6263 {
112e8700
SM
6264 uiout->text (" task ");
6265 uiout->field_int ("task", b->task);
4a306c9a 6266 }
c4093a6a 6267 }
f1310107 6268
112e8700 6269 uiout->text ("\n");
f1310107 6270
348d480f 6271 if (!part_of_multiple)
f1310107
TJB
6272 b->ops->print_one_detail (b, uiout);
6273
0d381245 6274 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6275 {
6276 annotate_field (6);
112e8700 6277 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6278 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6279 the frame ID. */
112e8700 6280 uiout->field_core_addr ("frame",
5af949e3 6281 b->gdbarch, b->frame_id.stack_addr);
112e8700 6282 uiout->text ("\n");
c4093a6a
JM
6283 }
6284
28010a5d 6285 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6286 {
6287 annotate_field (7);
d77f58be 6288 if (is_tracepoint (b))
112e8700 6289 uiout->text ("\ttrace only if ");
1042e4c0 6290 else
112e8700
SM
6291 uiout->text ("\tstop only if ");
6292 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6293
6294 /* Print whether the target is doing the breakpoint's condition
6295 evaluation. If GDB is doing the evaluation, don't print anything. */
6296 if (is_breakpoint (b)
6297 && breakpoint_condition_evaluation_mode ()
6298 == condition_evaluation_target)
6299 {
112e8700
SM
6300 uiout->text (" (");
6301 uiout->field_string ("evaluated-by",
b775012e 6302 bp_condition_evaluator (b));
112e8700 6303 uiout->text (" evals)");
b775012e 6304 }
112e8700 6305 uiout->text ("\n");
0101ce28
JJ
6306 }
6307
0d381245 6308 if (!part_of_multiple && b->thread != -1)
c4093a6a 6309 {
4a64f543 6310 /* FIXME should make an annotation for this. */
112e8700
SM
6311 uiout->text ("\tstop only in thread ");
6312 if (uiout->is_mi_like_p ())
6313 uiout->field_int ("thread", b->thread);
5d5658a1
PA
6314 else
6315 {
6316 struct thread_info *thr = find_thread_global_id (b->thread);
6317
112e8700 6318 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6319 }
112e8700 6320 uiout->text ("\n");
c4093a6a
JM
6321 }
6322
556ec64d
YQ
6323 if (!part_of_multiple)
6324 {
6325 if (b->hit_count)
31f56a27
YQ
6326 {
6327 /* FIXME should make an annotation for this. */
6328 if (is_catchpoint (b))
112e8700 6329 uiout->text ("\tcatchpoint");
31f56a27 6330 else if (is_tracepoint (b))
112e8700 6331 uiout->text ("\ttracepoint");
31f56a27 6332 else
112e8700
SM
6333 uiout->text ("\tbreakpoint");
6334 uiout->text (" already hit ");
6335 uiout->field_int ("times", b->hit_count);
31f56a27 6336 if (b->hit_count == 1)
112e8700 6337 uiout->text (" time\n");
31f56a27 6338 else
112e8700 6339 uiout->text (" times\n");
31f56a27 6340 }
556ec64d
YQ
6341 else
6342 {
31f56a27 6343 /* Output the count also if it is zero, but only if this is mi. */
112e8700
SM
6344 if (uiout->is_mi_like_p ())
6345 uiout->field_int ("times", b->hit_count);
556ec64d
YQ
6346 }
6347 }
8b93c638 6348
0d381245 6349 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6350 {
6351 annotate_field (8);
112e8700
SM
6352 uiout->text ("\tignore next ");
6353 uiout->field_int ("ignore", b->ignore_count);
6354 uiout->text (" hits\n");
c4093a6a 6355 }
059fb39f 6356
816338b5
SS
6357 /* Note that an enable count of 1 corresponds to "enable once"
6358 behavior, which is reported by the combination of enablement and
6359 disposition, so we don't need to mention it here. */
6360 if (!part_of_multiple && b->enable_count > 1)
6361 {
6362 annotate_field (8);
112e8700 6363 uiout->text ("\tdisable after ");
816338b5
SS
6364 /* Tweak the wording to clarify that ignore and enable counts
6365 are distinct, and have additive effect. */
6366 if (b->ignore_count)
112e8700 6367 uiout->text ("additional ");
816338b5 6368 else
112e8700
SM
6369 uiout->text ("next ");
6370 uiout->field_int ("enable", b->enable_count);
6371 uiout->text (" hits\n");
816338b5
SS
6372 }
6373
f196051f
SS
6374 if (!part_of_multiple && is_tracepoint (b))
6375 {
6376 struct tracepoint *tp = (struct tracepoint *) b;
6377
6378 if (tp->traceframe_usage)
6379 {
112e8700
SM
6380 uiout->text ("\ttrace buffer usage ");
6381 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6382 uiout->text (" bytes\n");
f196051f
SS
6383 }
6384 }
d3ce09f5 6385
d1b0a7bf 6386 l = b->commands ? b->commands.get () : NULL;
059fb39f 6387 if (!part_of_multiple && l)
c4093a6a
JM
6388 {
6389 annotate_field (9);
2e783024 6390 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6391 print_command_lines (uiout, l, 4);
c4093a6a 6392 }
d24317b4 6393
d9b3f62e 6394 if (is_tracepoint (b))
1042e4c0 6395 {
d9b3f62e
PA
6396 struct tracepoint *t = (struct tracepoint *) b;
6397
6398 if (!part_of_multiple && t->pass_count)
6399 {
6400 annotate_field (10);
112e8700
SM
6401 uiout->text ("\tpass count ");
6402 uiout->field_int ("pass", t->pass_count);
6403 uiout->text (" \n");
d9b3f62e 6404 }
f2a8bc8a
YQ
6405
6406 /* Don't display it when tracepoint or tracepoint location is
6407 pending. */
6408 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6409 {
6410 annotate_field (11);
6411
112e8700
SM
6412 if (uiout->is_mi_like_p ())
6413 uiout->field_string ("installed",
f2a8bc8a
YQ
6414 loc->inserted ? "y" : "n");
6415 else
6416 {
6417 if (loc->inserted)
112e8700 6418 uiout->text ("\t");
f2a8bc8a 6419 else
112e8700
SM
6420 uiout->text ("\tnot ");
6421 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6422 }
6423 }
1042e4c0
SS
6424 }
6425
112e8700 6426 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6427 {
3a5c3e22
PA
6428 if (is_watchpoint (b))
6429 {
6430 struct watchpoint *w = (struct watchpoint *) b;
6431
112e8700 6432 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6433 }
f00aae0f 6434 else if (b->location != NULL
d28cd78a 6435 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6436 uiout->field_string ("original-location",
d28cd78a 6437 event_location_to_string (b->location.get ()));
d24317b4 6438 }
c4093a6a 6439}
c5aa993b 6440
0d381245
VP
6441static void
6442print_one_breakpoint (struct breakpoint *b,
4a64f543 6443 struct bp_location **last_loc,
6c95b8df 6444 int allflag)
0d381245 6445{
79a45e25 6446 struct ui_out *uiout = current_uiout;
8d3788bd 6447
2e783024
TT
6448 {
6449 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
8d3788bd 6450
2e783024
TT
6451 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6452 }
0d381245
VP
6453
6454 /* If this breakpoint has custom print function,
6455 it's already printed. Otherwise, print individual
6456 locations, if any. */
6457 if (b->ops == NULL || b->ops->print_one == NULL)
6458 {
4a64f543
MS
6459 /* If breakpoint has a single location that is disabled, we
6460 print it as if it had several locations, since otherwise it's
6461 hard to represent "breakpoint enabled, location disabled"
6462 situation.
6463
6464 Note that while hardware watchpoints have several locations
a3be7890 6465 internally, that's not a property exposed to user. */
0d381245 6466 if (b->loc
a5606eee 6467 && !is_hardware_watchpoint (b)
8d3788bd 6468 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6469 {
6470 struct bp_location *loc;
6471 int n = 1;
8d3788bd 6472
0d381245 6473 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd 6474 {
2e783024 6475 ui_out_emit_tuple tuple_emitter (uiout, NULL);
8d3788bd 6476 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
8d3788bd 6477 }
0d381245
VP
6478 }
6479 }
6480}
6481
a6d9a66e
UW
6482static int
6483breakpoint_address_bits (struct breakpoint *b)
6484{
6485 int print_address_bits = 0;
6486 struct bp_location *loc;
6487
c6d81124
PA
6488 /* Software watchpoints that aren't watching memory don't have an
6489 address to print. */
6490 if (is_no_memory_software_watchpoint (b))
6491 return 0;
6492
a6d9a66e
UW
6493 for (loc = b->loc; loc; loc = loc->next)
6494 {
c7437ca6
PA
6495 int addr_bit;
6496
c7437ca6 6497 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6498 if (addr_bit > print_address_bits)
6499 print_address_bits = addr_bit;
6500 }
6501
6502 return print_address_bits;
6503}
0d381245 6504
65630365 6505/* See breakpoint.h. */
c5aa993b 6506
65630365
PA
6507void
6508print_breakpoint (breakpoint *b)
c4093a6a 6509{
a6d9a66e 6510 struct bp_location *dummy_loc = NULL;
65630365 6511 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6512}
c5aa993b 6513
09d682a4
TT
6514/* Return true if this breakpoint was set by the user, false if it is
6515 internal or momentary. */
6516
6517int
6518user_breakpoint_p (struct breakpoint *b)
6519{
46c6471b 6520 return b->number > 0;
09d682a4
TT
6521}
6522
93daf339
TT
6523/* See breakpoint.h. */
6524
6525int
6526pending_breakpoint_p (struct breakpoint *b)
6527{
6528 return b->loc == NULL;
6529}
6530
7f3b0473 6531/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6532 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6533 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6534 FILTER is non-NULL, call it on each breakpoint and only include the
6535 ones for which it returns non-zero. Return the total number of
6536 breakpoints listed. */
c906108c 6537
d77f58be 6538static int
4495129a 6539breakpoint_1 (const char *args, int allflag,
4a64f543 6540 int (*filter) (const struct breakpoint *))
c4093a6a 6541{
52f0bd74 6542 struct breakpoint *b;
a6d9a66e 6543 struct bp_location *last_loc = NULL;
7f3b0473 6544 int nr_printable_breakpoints;
79a45b7d 6545 struct value_print_options opts;
a6d9a66e 6546 int print_address_bits = 0;
269b11a2 6547 int print_type_col_width = 14;
79a45e25 6548 struct ui_out *uiout = current_uiout;
269b11a2 6549
79a45b7d
TT
6550 get_user_print_options (&opts);
6551
4a64f543
MS
6552 /* Compute the number of rows in the table, as well as the size
6553 required for address fields. */
7f3b0473
AC
6554 nr_printable_breakpoints = 0;
6555 ALL_BREAKPOINTS (b)
e5a67952
MS
6556 {
6557 /* If we have a filter, only list the breakpoints it accepts. */
6558 if (filter && !filter (b))
6559 continue;
6560
6561 /* If we have an "args" string, it is a list of breakpoints to
6562 accept. Skip the others. */
6563 if (args != NULL && *args != '\0')
6564 {
6565 if (allflag && parse_and_eval_long (args) != b->number)
6566 continue;
6567 if (!allflag && !number_is_in_list (args, b->number))
6568 continue;
6569 }
269b11a2 6570
e5a67952
MS
6571 if (allflag || user_breakpoint_p (b))
6572 {
6573 int addr_bit, type_len;
a6d9a66e 6574
e5a67952
MS
6575 addr_bit = breakpoint_address_bits (b);
6576 if (addr_bit > print_address_bits)
6577 print_address_bits = addr_bit;
269b11a2 6578
e5a67952
MS
6579 type_len = strlen (bptype_string (b->type));
6580 if (type_len > print_type_col_width)
6581 print_type_col_width = type_len;
6582
6583 nr_printable_breakpoints++;
6584 }
6585 }
7f3b0473 6586
4a2b031d
TT
6587 {
6588 ui_out_emit_table table_emitter (uiout,
6589 opts.addressprint ? 6 : 5,
6590 nr_printable_breakpoints,
6591 "BreakpointTable");
6592
6593 if (nr_printable_breakpoints > 0)
6594 annotate_breakpoints_headers ();
6595 if (nr_printable_breakpoints > 0)
6596 annotate_field (0);
6597 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6598 if (nr_printable_breakpoints > 0)
6599 annotate_field (1);
6600 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6601 if (nr_printable_breakpoints > 0)
6602 annotate_field (2);
6603 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6604 if (nr_printable_breakpoints > 0)
6605 annotate_field (3);
6606 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6607 if (opts.addressprint)
6608 {
6609 if (nr_printable_breakpoints > 0)
6610 annotate_field (4);
6611 if (print_address_bits <= 32)
6612 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6613 else
6614 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6615 }
6616 if (nr_printable_breakpoints > 0)
6617 annotate_field (5);
6618 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6619 uiout->table_body ();
6620 if (nr_printable_breakpoints > 0)
6621 annotate_breakpoints_table ();
6622
6623 ALL_BREAKPOINTS (b)
6624 {
6625 QUIT;
6626 /* If we have a filter, only list the breakpoints it accepts. */
6627 if (filter && !filter (b))
6628 continue;
e5a67952 6629
4a2b031d
TT
6630 /* If we have an "args" string, it is a list of breakpoints to
6631 accept. Skip the others. */
e5a67952 6632
4a2b031d
TT
6633 if (args != NULL && *args != '\0')
6634 {
6635 if (allflag) /* maintenance info breakpoint */
6636 {
6637 if (parse_and_eval_long (args) != b->number)
6638 continue;
6639 }
6640 else /* all others */
6641 {
6642 if (!number_is_in_list (args, b->number))
6643 continue;
6644 }
6645 }
6646 /* We only print out user settable breakpoints unless the
6647 allflag is set. */
6648 if (allflag || user_breakpoint_p (b))
6649 print_one_breakpoint (b, &last_loc, allflag);
6650 }
6651 }
698384cd 6652
7f3b0473 6653 if (nr_printable_breakpoints == 0)
c906108c 6654 {
4a64f543
MS
6655 /* If there's a filter, let the caller decide how to report
6656 empty list. */
d77f58be
SS
6657 if (!filter)
6658 {
e5a67952 6659 if (args == NULL || *args == '\0')
112e8700 6660 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6661 else
112e8700 6662 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
e5a67952 6663 args);
d77f58be 6664 }
c906108c
SS
6665 }
6666 else
c4093a6a 6667 {
a6d9a66e
UW
6668 if (last_loc && !server_command)
6669 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6670 }
c906108c 6671
4a64f543 6672 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6673 there have been breakpoints? */
c906108c 6674 annotate_breakpoints_table_end ();
d77f58be
SS
6675
6676 return nr_printable_breakpoints;
c906108c
SS
6677}
6678
ad443146
SS
6679/* Display the value of default-collect in a way that is generally
6680 compatible with the breakpoint list. */
6681
6682static void
6683default_collect_info (void)
6684{
79a45e25
PA
6685 struct ui_out *uiout = current_uiout;
6686
ad443146
SS
6687 /* If it has no value (which is frequently the case), say nothing; a
6688 message like "No default-collect." gets in user's face when it's
6689 not wanted. */
6690 if (!*default_collect)
6691 return;
6692
6693 /* The following phrase lines up nicely with per-tracepoint collect
6694 actions. */
112e8700
SM
6695 uiout->text ("default collect ");
6696 uiout->field_string ("default-collect", default_collect);
6697 uiout->text (" \n");
ad443146
SS
6698}
6699
c906108c 6700static void
0b39b52e 6701info_breakpoints_command (const char *args, int from_tty)
c906108c 6702{
e5a67952 6703 breakpoint_1 (args, 0, NULL);
ad443146
SS
6704
6705 default_collect_info ();
d77f58be
SS
6706}
6707
0b39b52e
TT
6708/* Temporary non-const overload. */
6709
6710static void
6711info_breakpoints_command (char *args, int from_tty)
6712{
6713 info_breakpoints_command ((const char *) args, from_tty);
6714}
6715
d77f58be 6716static void
11db9430 6717info_watchpoints_command (char *args, int from_tty)
d77f58be 6718{
e5a67952 6719 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6720 struct ui_out *uiout = current_uiout;
d77f58be
SS
6721
6722 if (num_printed == 0)
6723 {
e5a67952 6724 if (args == NULL || *args == '\0')
112e8700 6725 uiout->message ("No watchpoints.\n");
d77f58be 6726 else
112e8700 6727 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6728 }
c906108c
SS
6729}
6730
7a292a7a 6731static void
4495129a 6732maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6733{
e5a67952 6734 breakpoint_1 (args, 1, NULL);
ad443146
SS
6735
6736 default_collect_info ();
c906108c
SS
6737}
6738
0d381245 6739static int
714835d5 6740breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6741 struct program_space *pspace,
714835d5 6742 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6743{
6744 struct bp_location *bl = b->loc;
cc59ec59 6745
0d381245
VP
6746 for (; bl; bl = bl->next)
6747 {
6c95b8df
PA
6748 if (bl->pspace == pspace
6749 && bl->address == pc
0d381245
VP
6750 && (!overlay_debugging || bl->section == section))
6751 return 1;
6752 }
6753 return 0;
6754}
6755
672f9b60 6756/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6757 concerns with logical breakpoints, so we match program spaces, not
6758 address spaces. */
c906108c
SS
6759
6760static void
6c95b8df
PA
6761describe_other_breakpoints (struct gdbarch *gdbarch,
6762 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6763 struct obj_section *section, int thread)
c906108c 6764{
52f0bd74
AC
6765 int others = 0;
6766 struct breakpoint *b;
c906108c
SS
6767
6768 ALL_BREAKPOINTS (b)
672f9b60
KP
6769 others += (user_breakpoint_p (b)
6770 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6771 if (others > 0)
6772 {
a3f17187
AC
6773 if (others == 1)
6774 printf_filtered (_("Note: breakpoint "));
6775 else /* if (others == ???) */
6776 printf_filtered (_("Note: breakpoints "));
c906108c 6777 ALL_BREAKPOINTS (b)
672f9b60 6778 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6779 {
6780 others--;
6781 printf_filtered ("%d", b->number);
6782 if (b->thread == -1 && thread != -1)
6783 printf_filtered (" (all threads)");
6784 else if (b->thread != -1)
6785 printf_filtered (" (thread %d)", b->thread);
6786 printf_filtered ("%s%s ",
059fb39f 6787 ((b->enable_state == bp_disabled
f8eba3c6 6788 || b->enable_state == bp_call_disabled)
0d381245 6789 ? " (disabled)"
0d381245
VP
6790 : ""),
6791 (others > 1) ? ","
6792 : ((others == 1) ? " and" : ""));
6793 }
a3f17187 6794 printf_filtered (_("also set at pc "));
5af949e3 6795 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6796 printf_filtered (".\n");
6797 }
6798}
6799\f
c906108c 6800
e4f237da 6801/* Return true iff it is meaningful to use the address member of
244558af
LM
6802 BPT locations. For some breakpoint types, the locations' address members
6803 are irrelevant and it makes no sense to attempt to compare them to other
6804 addresses (or use them for any other purpose either).
e4f237da 6805
4a64f543 6806 More specifically, each of the following breakpoint types will
244558af 6807 always have a zero valued location address and we don't want to mark
4a64f543 6808 breakpoints of any of these types to be a duplicate of an actual
244558af 6809 breakpoint location at address zero:
e4f237da
KB
6810
6811 bp_watchpoint
2d134ed3
PA
6812 bp_catchpoint
6813
6814*/
e4f237da
KB
6815
6816static int
6817breakpoint_address_is_meaningful (struct breakpoint *bpt)
6818{
6819 enum bptype type = bpt->type;
6820
2d134ed3
PA
6821 return (type != bp_watchpoint && type != bp_catchpoint);
6822}
6823
6824/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6825 true if LOC1 and LOC2 represent the same watchpoint location. */
6826
6827static int
4a64f543
MS
6828watchpoint_locations_match (struct bp_location *loc1,
6829 struct bp_location *loc2)
2d134ed3 6830{
3a5c3e22
PA
6831 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6832 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6833
6834 /* Both of them must exist. */
6835 gdb_assert (w1 != NULL);
6836 gdb_assert (w2 != NULL);
2bdf28a0 6837
4a64f543
MS
6838 /* If the target can evaluate the condition expression in hardware,
6839 then we we need to insert both watchpoints even if they are at
6840 the same place. Otherwise the watchpoint will only trigger when
6841 the condition of whichever watchpoint was inserted evaluates to
6842 true, not giving a chance for GDB to check the condition of the
6843 other watchpoint. */
3a5c3e22 6844 if ((w1->cond_exp
4a64f543
MS
6845 && target_can_accel_watchpoint_condition (loc1->address,
6846 loc1->length,
0cf6dd15 6847 loc1->watchpoint_type,
4d01a485 6848 w1->cond_exp.get ()))
3a5c3e22 6849 || (w2->cond_exp
4a64f543
MS
6850 && target_can_accel_watchpoint_condition (loc2->address,
6851 loc2->length,
0cf6dd15 6852 loc2->watchpoint_type,
4d01a485 6853 w2->cond_exp.get ())))
0cf6dd15
TJB
6854 return 0;
6855
85d721b8
PA
6856 /* Note that this checks the owner's type, not the location's. In
6857 case the target does not support read watchpoints, but does
6858 support access watchpoints, we'll have bp_read_watchpoint
6859 watchpoints with hw_access locations. Those should be considered
6860 duplicates of hw_read locations. The hw_read locations will
6861 become hw_access locations later. */
2d134ed3
PA
6862 return (loc1->owner->type == loc2->owner->type
6863 && loc1->pspace->aspace == loc2->pspace->aspace
6864 && loc1->address == loc2->address
6865 && loc1->length == loc2->length);
e4f237da
KB
6866}
6867
31e77af2 6868/* See breakpoint.h. */
6c95b8df 6869
31e77af2 6870int
accd0bcd
YQ
6871breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6872 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6873{
f5656ead 6874 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6875 || aspace1 == aspace2)
6876 && addr1 == addr2);
6877}
6878
f1310107
TJB
6879/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6880 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6881 matches ASPACE2. On targets that have global breakpoints, the address
6882 space doesn't really matter. */
6883
6884static int
accd0bcd
YQ
6885breakpoint_address_match_range (const address_space *aspace1,
6886 CORE_ADDR addr1,
6887 int len1, const address_space *aspace2,
f1310107
TJB
6888 CORE_ADDR addr2)
6889{
f5656ead 6890 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6891 || aspace1 == aspace2)
6892 && addr2 >= addr1 && addr2 < addr1 + len1);
6893}
6894
6895/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6896 a ranged breakpoint. In most targets, a match happens only if ASPACE
6897 matches the breakpoint's address space. On targets that have global
6898 breakpoints, the address space doesn't really matter. */
6899
6900static int
6901breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6902 const address_space *aspace,
f1310107
TJB
6903 CORE_ADDR addr)
6904{
6905 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6906 aspace, addr)
6907 || (bl->length
6908 && breakpoint_address_match_range (bl->pspace->aspace,
6909 bl->address, bl->length,
6910 aspace, addr)));
6911}
6912
d35ae833
PA
6913/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6914 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6915 match happens only if ASPACE matches the breakpoint's address
6916 space. On targets that have global breakpoints, the address space
6917 doesn't really matter. */
6918
6919static int
6920breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6921 const address_space *aspace,
d35ae833
PA
6922 CORE_ADDR addr, int len)
6923{
6924 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6925 || bl->pspace->aspace == aspace)
6926 {
6927 int bl_len = bl->length != 0 ? bl->length : 1;
6928
6929 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6930 return 1;
6931 }
6932 return 0;
6933}
6934
1e4d1764
YQ
6935/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6936 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6937 true, otherwise returns false. */
6938
6939static int
6940tracepoint_locations_match (struct bp_location *loc1,
6941 struct bp_location *loc2)
6942{
6943 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6944 /* Since tracepoint locations are never duplicated with others', tracepoint
6945 locations at the same address of different tracepoints are regarded as
6946 different locations. */
6947 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6948 else
6949 return 0;
6950}
6951
2d134ed3
PA
6952/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6953 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6954 represent the same location. */
6955
6956static int
4a64f543
MS
6957breakpoint_locations_match (struct bp_location *loc1,
6958 struct bp_location *loc2)
2d134ed3 6959{
2bdf28a0
JK
6960 int hw_point1, hw_point2;
6961
6962 /* Both of them must not be in moribund_locations. */
6963 gdb_assert (loc1->owner != NULL);
6964 gdb_assert (loc2->owner != NULL);
6965
6966 hw_point1 = is_hardware_watchpoint (loc1->owner);
6967 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6968
6969 if (hw_point1 != hw_point2)
6970 return 0;
6971 else if (hw_point1)
6972 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6973 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6974 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6975 else
f1310107
TJB
6976 /* We compare bp_location.length in order to cover ranged breakpoints. */
6977 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6978 loc2->pspace->aspace, loc2->address)
6979 && loc1->length == loc2->length);
2d134ed3
PA
6980}
6981
76897487
KB
6982static void
6983breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6984 int bnum, int have_bnum)
6985{
f63fbe86
MS
6986 /* The longest string possibly returned by hex_string_custom
6987 is 50 chars. These must be at least that big for safety. */
6988 char astr1[64];
6989 char astr2[64];
76897487 6990
bb599908
PH
6991 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6992 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6993 if (have_bnum)
8a3fe4f8 6994 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6995 bnum, astr1, astr2);
6996 else
8a3fe4f8 6997 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6998}
6999
4a64f543
MS
7000/* Adjust a breakpoint's address to account for architectural
7001 constraints on breakpoint placement. Return the adjusted address.
7002 Note: Very few targets require this kind of adjustment. For most
7003 targets, this function is simply the identity function. */
76897487
KB
7004
7005static CORE_ADDR
a6d9a66e
UW
7006adjust_breakpoint_address (struct gdbarch *gdbarch,
7007 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7008{
a6d9a66e 7009 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7010 {
7011 /* Very few targets need any kind of breakpoint adjustment. */
7012 return bpaddr;
7013 }
88f7da05
KB
7014 else if (bptype == bp_watchpoint
7015 || bptype == bp_hardware_watchpoint
7016 || bptype == bp_read_watchpoint
7017 || bptype == bp_access_watchpoint
fe798b75 7018 || bptype == bp_catchpoint)
88f7da05
KB
7019 {
7020 /* Watchpoints and the various bp_catch_* eventpoints should not
7021 have their addresses modified. */
7022 return bpaddr;
7023 }
7c16b83e
PA
7024 else if (bptype == bp_single_step)
7025 {
7026 /* Single-step breakpoints should not have their addresses
7027 modified. If there's any architectural constrain that
7028 applies to this address, then it should have already been
7029 taken into account when the breakpoint was created in the
7030 first place. If we didn't do this, stepping through e.g.,
7031 Thumb-2 IT blocks would break. */
7032 return bpaddr;
7033 }
76897487
KB
7034 else
7035 {
7036 CORE_ADDR adjusted_bpaddr;
7037
7038 /* Some targets have architectural constraints on the placement
7039 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7040 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7041
7042 /* An adjusted breakpoint address can significantly alter
7043 a user's expectations. Print a warning if an adjustment
7044 is required. */
7045 if (adjusted_bpaddr != bpaddr)
7046 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7047
7048 return adjusted_bpaddr;
7049 }
7050}
7051
5625a286 7052bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7cc221ef 7053{
5625a286 7054 bp_location *loc = this;
7cc221ef 7055
348d480f
PA
7056 gdb_assert (ops != NULL);
7057
28010a5d
PA
7058 loc->ops = ops;
7059 loc->owner = owner;
b775012e 7060 loc->cond_bytecode = NULL;
0d381245
VP
7061 loc->shlib_disabled = 0;
7062 loc->enabled = 1;
e049a4b5 7063
28010a5d 7064 switch (owner->type)
e049a4b5
DJ
7065 {
7066 case bp_breakpoint:
7c16b83e 7067 case bp_single_step:
e049a4b5
DJ
7068 case bp_until:
7069 case bp_finish:
7070 case bp_longjmp:
7071 case bp_longjmp_resume:
e2e4d78b 7072 case bp_longjmp_call_dummy:
186c406b
TT
7073 case bp_exception:
7074 case bp_exception_resume:
e049a4b5 7075 case bp_step_resume:
2c03e5be 7076 case bp_hp_step_resume:
e049a4b5
DJ
7077 case bp_watchpoint_scope:
7078 case bp_call_dummy:
aa7d318d 7079 case bp_std_terminate:
e049a4b5
DJ
7080 case bp_shlib_event:
7081 case bp_thread_event:
7082 case bp_overlay_event:
4efc6507 7083 case bp_jit_event:
0fd8e87f 7084 case bp_longjmp_master:
aa7d318d 7085 case bp_std_terminate_master:
186c406b 7086 case bp_exception_master:
0e30163f
JK
7087 case bp_gnu_ifunc_resolver:
7088 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7089 case bp_dprintf:
e049a4b5 7090 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7091 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7092 break;
7093 case bp_hardware_breakpoint:
7094 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7095 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7096 break;
7097 case bp_hardware_watchpoint:
7098 case bp_read_watchpoint:
7099 case bp_access_watchpoint:
7100 loc->loc_type = bp_loc_hardware_watchpoint;
7101 break;
7102 case bp_watchpoint:
ce78b96d 7103 case bp_catchpoint:
15c3d785
PA
7104 case bp_tracepoint:
7105 case bp_fast_tracepoint:
0fb4aa4b 7106 case bp_static_tracepoint:
e049a4b5
DJ
7107 loc->loc_type = bp_loc_other;
7108 break;
7109 default:
e2e0b3e5 7110 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7111 }
7112
f431efe5 7113 loc->refc = 1;
28010a5d
PA
7114}
7115
7116/* Allocate a struct bp_location. */
7117
7118static struct bp_location *
7119allocate_bp_location (struct breakpoint *bpt)
7120{
348d480f
PA
7121 return bpt->ops->allocate_location (bpt);
7122}
7cc221ef 7123
f431efe5
PA
7124static void
7125free_bp_location (struct bp_location *loc)
fe3f5fa8 7126{
348d480f 7127 loc->ops->dtor (loc);
4d01a485 7128 delete loc;
fe3f5fa8
VP
7129}
7130
f431efe5
PA
7131/* Increment reference count. */
7132
7133static void
7134incref_bp_location (struct bp_location *bl)
7135{
7136 ++bl->refc;
7137}
7138
7139/* Decrement reference count. If the reference count reaches 0,
7140 destroy the bp_location. Sets *BLP to NULL. */
7141
7142static void
7143decref_bp_location (struct bp_location **blp)
7144{
0807b50c
PA
7145 gdb_assert ((*blp)->refc > 0);
7146
f431efe5
PA
7147 if (--(*blp)->refc == 0)
7148 free_bp_location (*blp);
7149 *blp = NULL;
7150}
7151
346774a9 7152/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7153
b270e6f9
TT
7154static breakpoint *
7155add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7156{
346774a9 7157 struct breakpoint *b1;
b270e6f9 7158 struct breakpoint *result = b.get ();
c906108c 7159
346774a9
PA
7160 /* Add this breakpoint to the end of the chain so that a list of
7161 breakpoints will come out in order of increasing numbers. */
7162
7163 b1 = breakpoint_chain;
7164 if (b1 == 0)
b270e6f9 7165 breakpoint_chain = b.release ();
346774a9
PA
7166 else
7167 {
7168 while (b1->next)
7169 b1 = b1->next;
b270e6f9 7170 b1->next = b.release ();
346774a9 7171 }
b270e6f9
TT
7172
7173 return result;
346774a9
PA
7174}
7175
7176/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7177
7178static void
7179init_raw_breakpoint_without_location (struct breakpoint *b,
7180 struct gdbarch *gdbarch,
28010a5d 7181 enum bptype bptype,
c0a91b2b 7182 const struct breakpoint_ops *ops)
346774a9 7183{
348d480f
PA
7184 gdb_assert (ops != NULL);
7185
28010a5d 7186 b->ops = ops;
4d28f7a8 7187 b->type = bptype;
a6d9a66e 7188 b->gdbarch = gdbarch;
c906108c
SS
7189 b->language = current_language->la_language;
7190 b->input_radix = input_radix;
d0fb5eae 7191 b->related_breakpoint = b;
346774a9
PA
7192}
7193
7194/* Helper to set_raw_breakpoint below. Creates a breakpoint
7195 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7196
7197static struct breakpoint *
7198set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7199 enum bptype bptype,
c0a91b2b 7200 const struct breakpoint_ops *ops)
346774a9 7201{
3b0871f4 7202 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7203
3b0871f4 7204 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7205 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7206}
7207
0e30163f
JK
7208/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7209 resolutions should be made as the user specified the location explicitly
7210 enough. */
7211
0d381245 7212static void
0e30163f 7213set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7214{
2bdf28a0
JK
7215 gdb_assert (loc->owner != NULL);
7216
0d381245 7217 if (loc->owner->type == bp_breakpoint
1042e4c0 7218 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7219 || is_tracepoint (loc->owner))
0d381245 7220 {
0e30163f 7221 int is_gnu_ifunc;
2c02bd72 7222 const char *function_name;
6a3a010b 7223 CORE_ADDR func_addr;
0e30163f 7224
2c02bd72 7225 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7226 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7227
7228 if (is_gnu_ifunc && !explicit_loc)
7229 {
7230 struct breakpoint *b = loc->owner;
7231
7232 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7233 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7234 &loc->requested_address))
7235 {
7236 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7237 loc->address = adjust_breakpoint_address (loc->gdbarch,
7238 loc->requested_address,
7239 b->type);
7240 }
7241 else if (b->type == bp_breakpoint && b->loc == loc
7242 && loc->next == NULL && b->related_breakpoint == b)
7243 {
7244 /* Create only the whole new breakpoint of this type but do not
7245 mess more complicated breakpoints with multiple locations. */
7246 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7247 /* Remember the resolver's address for use by the return
7248 breakpoint. */
7249 loc->related_address = func_addr;
0e30163f
JK
7250 }
7251 }
7252
2c02bd72
DE
7253 if (function_name)
7254 loc->function_name = xstrdup (function_name);
0d381245
VP
7255 }
7256}
7257
a6d9a66e 7258/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7259struct gdbarch *
a6d9a66e
UW
7260get_sal_arch (struct symtab_and_line sal)
7261{
7262 if (sal.section)
7263 return get_objfile_arch (sal.section->objfile);
7264 if (sal.symtab)
eb822aa6 7265 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7266
7267 return NULL;
7268}
7269
346774a9
PA
7270/* Low level routine for partially initializing a breakpoint of type
7271 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7272 file name, and line number are provided by SAL.
0d381245
VP
7273
7274 It is expected that the caller will complete the initialization of
7275 the newly created breakpoint struct as well as output any status
c56053d2 7276 information regarding the creation of a new breakpoint. */
0d381245 7277
346774a9
PA
7278static void
7279init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7280 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7281 const struct breakpoint_ops *ops)
0d381245 7282{
28010a5d 7283 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7284
3742cc8b 7285 add_location_to_breakpoint (b, &sal);
0d381245 7286
6c95b8df
PA
7287 if (bptype != bp_catchpoint)
7288 gdb_assert (sal.pspace != NULL);
7289
f8eba3c6
TT
7290 /* Store the program space that was used to set the breakpoint,
7291 except for ordinary breakpoints, which are independent of the
7292 program space. */
7293 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7294 b->pspace = sal.pspace;
346774a9 7295}
c906108c 7296
346774a9
PA
7297/* set_raw_breakpoint is a low level routine for allocating and
7298 partially initializing a breakpoint of type BPTYPE. The newly
7299 created breakpoint's address, section, source file name, and line
7300 number are provided by SAL. The newly created and partially
7301 initialized breakpoint is added to the breakpoint chain and
7302 is also returned as the value of this function.
7303
7304 It is expected that the caller will complete the initialization of
7305 the newly created breakpoint struct as well as output any status
7306 information regarding the creation of a new breakpoint. In
7307 particular, set_raw_breakpoint does NOT set the breakpoint
7308 number! Care should be taken to not allow an error to occur
7309 prior to completing the initialization of the breakpoint. If this
7310 should happen, a bogus breakpoint will be left on the chain. */
7311
7312struct breakpoint *
7313set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7314 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7315 const struct breakpoint_ops *ops)
346774a9 7316{
3b0871f4 7317 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7318
3b0871f4 7319 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7320 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7321}
7322
53a5351d 7323/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7324 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7325 initiated the operation. */
c906108c
SS
7326
7327void
186c406b 7328set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7329{
35df4500 7330 struct breakpoint *b, *b_tmp;
5d5658a1 7331 int thread = tp->global_num;
0fd8e87f
UW
7332
7333 /* To avoid having to rescan all objfile symbols at every step,
7334 we maintain a list of continually-inserted but always disabled
7335 longjmp "master" breakpoints. Here, we simply create momentary
7336 clones of those and enable them for the requested thread. */
35df4500 7337 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7338 if (b->pspace == current_program_space
186c406b
TT
7339 && (b->type == bp_longjmp_master
7340 || b->type == bp_exception_master))
0fd8e87f 7341 {
06edf0c0
PA
7342 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7343 struct breakpoint *clone;
cc59ec59 7344
e2e4d78b
JK
7345 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7346 after their removal. */
06edf0c0 7347 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7348 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7349 clone->thread = thread;
7350 }
186c406b
TT
7351
7352 tp->initiating_frame = frame;
c906108c
SS
7353}
7354
611c83ae 7355/* Delete all longjmp breakpoints from THREAD. */
c906108c 7356void
611c83ae 7357delete_longjmp_breakpoint (int thread)
c906108c 7358{
35df4500 7359 struct breakpoint *b, *b_tmp;
c906108c 7360
35df4500 7361 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7362 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7363 {
7364 if (b->thread == thread)
7365 delete_breakpoint (b);
7366 }
c906108c
SS
7367}
7368
f59f708a
PA
7369void
7370delete_longjmp_breakpoint_at_next_stop (int thread)
7371{
7372 struct breakpoint *b, *b_tmp;
7373
7374 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7375 if (b->type == bp_longjmp || b->type == bp_exception)
7376 {
7377 if (b->thread == thread)
7378 b->disposition = disp_del_at_next_stop;
7379 }
7380}
7381
e2e4d78b
JK
7382/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7383 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7384 pointer to any of them. Return NULL if this system cannot place longjmp
7385 breakpoints. */
7386
7387struct breakpoint *
7388set_longjmp_breakpoint_for_call_dummy (void)
7389{
7390 struct breakpoint *b, *retval = NULL;
7391
7392 ALL_BREAKPOINTS (b)
7393 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7394 {
7395 struct breakpoint *new_b;
7396
7397 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7398 &momentary_breakpoint_ops,
7399 1);
5d5658a1 7400 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7401
7402 /* Link NEW_B into the chain of RETVAL breakpoints. */
7403
7404 gdb_assert (new_b->related_breakpoint == new_b);
7405 if (retval == NULL)
7406 retval = new_b;
7407 new_b->related_breakpoint = retval;
7408 while (retval->related_breakpoint != new_b->related_breakpoint)
7409 retval = retval->related_breakpoint;
7410 retval->related_breakpoint = new_b;
7411 }
7412
7413 return retval;
7414}
7415
7416/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7417 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7418 stack.
7419
7420 You should call this function only at places where it is safe to currently
7421 unwind the whole stack. Failed stack unwind would discard live dummy
7422 frames. */
7423
7424void
b67a2c6f 7425check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7426{
7427 struct breakpoint *b, *b_tmp;
7428
7429 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7430 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7431 {
7432 struct breakpoint *dummy_b = b->related_breakpoint;
7433
7434 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7435 dummy_b = dummy_b->related_breakpoint;
7436 if (dummy_b->type != bp_call_dummy
7437 || frame_find_by_id (dummy_b->frame_id) != NULL)
7438 continue;
7439
b67a2c6f 7440 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7441
7442 while (b->related_breakpoint != b)
7443 {
7444 if (b_tmp == b->related_breakpoint)
7445 b_tmp = b->related_breakpoint->next;
7446 delete_breakpoint (b->related_breakpoint);
7447 }
7448 delete_breakpoint (b);
7449 }
7450}
7451
1900040c
MS
7452void
7453enable_overlay_breakpoints (void)
7454{
52f0bd74 7455 struct breakpoint *b;
1900040c
MS
7456
7457 ALL_BREAKPOINTS (b)
7458 if (b->type == bp_overlay_event)
7459 {
7460 b->enable_state = bp_enabled;
44702360 7461 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7462 overlay_events_enabled = 1;
1900040c
MS
7463 }
7464}
7465
7466void
7467disable_overlay_breakpoints (void)
7468{
52f0bd74 7469 struct breakpoint *b;
1900040c
MS
7470
7471 ALL_BREAKPOINTS (b)
7472 if (b->type == bp_overlay_event)
7473 {
7474 b->enable_state = bp_disabled;
44702360 7475 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7476 overlay_events_enabled = 0;
1900040c
MS
7477 }
7478}
7479
aa7d318d
TT
7480/* Set an active std::terminate breakpoint for each std::terminate
7481 master breakpoint. */
7482void
7483set_std_terminate_breakpoint (void)
7484{
35df4500 7485 struct breakpoint *b, *b_tmp;
aa7d318d 7486
35df4500 7487 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7488 if (b->pspace == current_program_space
7489 && b->type == bp_std_terminate_master)
7490 {
06edf0c0 7491 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7492 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7493 }
7494}
7495
7496/* Delete all the std::terminate breakpoints. */
7497void
7498delete_std_terminate_breakpoint (void)
7499{
35df4500 7500 struct breakpoint *b, *b_tmp;
aa7d318d 7501
35df4500 7502 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7503 if (b->type == bp_std_terminate)
7504 delete_breakpoint (b);
7505}
7506
c4093a6a 7507struct breakpoint *
a6d9a66e 7508create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7509{
7510 struct breakpoint *b;
c4093a6a 7511
06edf0c0
PA
7512 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7513 &internal_breakpoint_ops);
7514
b5de0fa7 7515 b->enable_state = bp_enabled;
f00aae0f 7516 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7517 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7518
44702360 7519 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7520
c4093a6a
JM
7521 return b;
7522}
7523
0101ce28
JJ
7524struct lang_and_radix
7525 {
7526 enum language lang;
7527 int radix;
7528 };
7529
4efc6507
DE
7530/* Create a breakpoint for JIT code registration and unregistration. */
7531
7532struct breakpoint *
7533create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7534{
2a7f3dff
PA
7535 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7536 &internal_breakpoint_ops);
4efc6507 7537}
0101ce28 7538
03673fc7
PP
7539/* Remove JIT code registration and unregistration breakpoint(s). */
7540
7541void
7542remove_jit_event_breakpoints (void)
7543{
7544 struct breakpoint *b, *b_tmp;
7545
7546 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7547 if (b->type == bp_jit_event
7548 && b->loc->pspace == current_program_space)
7549 delete_breakpoint (b);
7550}
7551
cae688ec
JJ
7552void
7553remove_solib_event_breakpoints (void)
7554{
35df4500 7555 struct breakpoint *b, *b_tmp;
cae688ec 7556
35df4500 7557 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7558 if (b->type == bp_shlib_event
7559 && b->loc->pspace == current_program_space)
cae688ec
JJ
7560 delete_breakpoint (b);
7561}
7562
f37f681c
PA
7563/* See breakpoint.h. */
7564
7565void
7566remove_solib_event_breakpoints_at_next_stop (void)
7567{
7568 struct breakpoint *b, *b_tmp;
7569
7570 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7571 if (b->type == bp_shlib_event
7572 && b->loc->pspace == current_program_space)
7573 b->disposition = disp_del_at_next_stop;
7574}
7575
04086b45
PA
7576/* Helper for create_solib_event_breakpoint /
7577 create_and_insert_solib_event_breakpoint. Allows specifying which
7578 INSERT_MODE to pass through to update_global_location_list. */
7579
7580static struct breakpoint *
7581create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7582 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7583{
7584 struct breakpoint *b;
7585
06edf0c0
PA
7586 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7587 &internal_breakpoint_ops);
04086b45 7588 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7589 return b;
7590}
7591
04086b45
PA
7592struct breakpoint *
7593create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7594{
7595 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7596}
7597
f37f681c
PA
7598/* See breakpoint.h. */
7599
7600struct breakpoint *
7601create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7602{
7603 struct breakpoint *b;
7604
04086b45
PA
7605 /* Explicitly tell update_global_location_list to insert
7606 locations. */
7607 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7608 if (!b->loc->inserted)
7609 {
7610 delete_breakpoint (b);
7611 return NULL;
7612 }
7613 return b;
7614}
7615
cae688ec
JJ
7616/* Disable any breakpoints that are on code in shared libraries. Only
7617 apply to enabled breakpoints, disabled ones can just stay disabled. */
7618
7619void
cb851954 7620disable_breakpoints_in_shlibs (void)
cae688ec 7621{
876fa593 7622 struct bp_location *loc, **locp_tmp;
cae688ec 7623
876fa593 7624 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7625 {
2bdf28a0 7626 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7627 struct breakpoint *b = loc->owner;
2bdf28a0 7628
4a64f543
MS
7629 /* We apply the check to all breakpoints, including disabled for
7630 those with loc->duplicate set. This is so that when breakpoint
7631 becomes enabled, or the duplicate is removed, gdb will try to
7632 insert all breakpoints. If we don't set shlib_disabled here,
7633 we'll try to insert those breakpoints and fail. */
1042e4c0 7634 if (((b->type == bp_breakpoint)
508ccb1f 7635 || (b->type == bp_jit_event)
1042e4c0 7636 || (b->type == bp_hardware_breakpoint)
d77f58be 7637 || (is_tracepoint (b)))
6c95b8df 7638 && loc->pspace == current_program_space
0d381245 7639 && !loc->shlib_disabled
6c95b8df 7640 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7641 )
0d381245
VP
7642 {
7643 loc->shlib_disabled = 1;
7644 }
cae688ec
JJ
7645 }
7646}
7647
63644780
NB
7648/* Disable any breakpoints and tracepoints that are in SOLIB upon
7649 notification of unloaded_shlib. Only apply to enabled breakpoints,
7650 disabled ones can just stay disabled. */
84acb35a 7651
75149521 7652static void
84acb35a
JJ
7653disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7654{
876fa593 7655 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7656 int disabled_shlib_breaks = 0;
7657
876fa593 7658 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7659 {
2bdf28a0 7660 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7661 struct breakpoint *b = loc->owner;
cc59ec59 7662
1e4d1764 7663 if (solib->pspace == loc->pspace
e2dd7057 7664 && !loc->shlib_disabled
1e4d1764
YQ
7665 && (((b->type == bp_breakpoint
7666 || b->type == bp_jit_event
7667 || b->type == bp_hardware_breakpoint)
7668 && (loc->loc_type == bp_loc_hardware_breakpoint
7669 || loc->loc_type == bp_loc_software_breakpoint))
7670 || is_tracepoint (b))
e2dd7057 7671 && solib_contains_address_p (solib, loc->address))
84acb35a 7672 {
e2dd7057
PP
7673 loc->shlib_disabled = 1;
7674 /* At this point, we cannot rely on remove_breakpoint
7675 succeeding so we must mark the breakpoint as not inserted
7676 to prevent future errors occurring in remove_breakpoints. */
7677 loc->inserted = 0;
8d3788bd
VP
7678
7679 /* This may cause duplicate notifications for the same breakpoint. */
7680 observer_notify_breakpoint_modified (b);
7681
e2dd7057
PP
7682 if (!disabled_shlib_breaks)
7683 {
223ffa71 7684 target_terminal::ours_for_output ();
3e43a32a
MS
7685 warning (_("Temporarily disabling breakpoints "
7686 "for unloaded shared library \"%s\""),
e2dd7057 7687 solib->so_name);
84acb35a 7688 }
e2dd7057 7689 disabled_shlib_breaks = 1;
84acb35a
JJ
7690 }
7691 }
84acb35a
JJ
7692}
7693
63644780
NB
7694/* Disable any breakpoints and tracepoints in OBJFILE upon
7695 notification of free_objfile. Only apply to enabled breakpoints,
7696 disabled ones can just stay disabled. */
7697
7698static void
7699disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7700{
7701 struct breakpoint *b;
7702
7703 if (objfile == NULL)
7704 return;
7705
d03de421
PA
7706 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7707 managed by the user with add-symbol-file/remove-symbol-file.
7708 Similarly to how breakpoints in shared libraries are handled in
7709 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7710 shlib_disabled so they end up uninserted on the next global
7711 location list update. Shared libraries not loaded by the user
7712 aren't handled here -- they're already handled in
7713 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7714 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7715 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7716 main objfile). */
7717 if ((objfile->flags & OBJF_SHARED) == 0
7718 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7719 return;
7720
7721 ALL_BREAKPOINTS (b)
7722 {
7723 struct bp_location *loc;
7724 int bp_modified = 0;
7725
7726 if (!is_breakpoint (b) && !is_tracepoint (b))
7727 continue;
7728
7729 for (loc = b->loc; loc != NULL; loc = loc->next)
7730 {
7731 CORE_ADDR loc_addr = loc->address;
7732
7733 if (loc->loc_type != bp_loc_hardware_breakpoint
7734 && loc->loc_type != bp_loc_software_breakpoint)
7735 continue;
7736
7737 if (loc->shlib_disabled != 0)
7738 continue;
7739
7740 if (objfile->pspace != loc->pspace)
7741 continue;
7742
7743 if (loc->loc_type != bp_loc_hardware_breakpoint
7744 && loc->loc_type != bp_loc_software_breakpoint)
7745 continue;
7746
7747 if (is_addr_in_objfile (loc_addr, objfile))
7748 {
7749 loc->shlib_disabled = 1;
08351840
PA
7750 /* At this point, we don't know whether the object was
7751 unmapped from the inferior or not, so leave the
7752 inserted flag alone. We'll handle failure to
7753 uninsert quietly, in case the object was indeed
7754 unmapped. */
63644780
NB
7755
7756 mark_breakpoint_location_modified (loc);
7757
7758 bp_modified = 1;
7759 }
7760 }
7761
7762 if (bp_modified)
7763 observer_notify_breakpoint_modified (b);
7764 }
7765}
7766
ce78b96d
JB
7767/* FORK & VFORK catchpoints. */
7768
e29a4733 7769/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7770 catchpoint. A breakpoint is really of this type iff its ops pointer points
7771 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7772
c1fc2657 7773struct fork_catchpoint : public breakpoint
e29a4733 7774{
e29a4733
PA
7775 /* Process id of a child process whose forking triggered this
7776 catchpoint. This field is only valid immediately after this
7777 catchpoint has triggered. */
7778 ptid_t forked_inferior_pid;
7779};
7780
4a64f543
MS
7781/* Implement the "insert" breakpoint_ops method for fork
7782 catchpoints. */
ce78b96d 7783
77b06cd7
TJB
7784static int
7785insert_catch_fork (struct bp_location *bl)
ce78b96d 7786{
dfd4cc63 7787 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7788}
7789
4a64f543
MS
7790/* Implement the "remove" breakpoint_ops method for fork
7791 catchpoints. */
ce78b96d
JB
7792
7793static int
73971819 7794remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7795{
dfd4cc63 7796 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7797}
7798
7799/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7800 catchpoints. */
7801
7802static int
f1310107 7803breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7804 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7805 const struct target_waitstatus *ws)
ce78b96d 7806{
e29a4733
PA
7807 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7808
f90263c1
TT
7809 if (ws->kind != TARGET_WAITKIND_FORKED)
7810 return 0;
7811
7812 c->forked_inferior_pid = ws->value.related_pid;
7813 return 1;
ce78b96d
JB
7814}
7815
4a64f543
MS
7816/* Implement the "print_it" breakpoint_ops method for fork
7817 catchpoints. */
ce78b96d
JB
7818
7819static enum print_stop_action
348d480f 7820print_it_catch_fork (bpstat bs)
ce78b96d 7821{
36dfb11c 7822 struct ui_out *uiout = current_uiout;
348d480f
PA
7823 struct breakpoint *b = bs->breakpoint_at;
7824 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7825
ce78b96d 7826 annotate_catchpoint (b->number);
f303dbd6 7827 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7828 if (b->disposition == disp_del)
112e8700 7829 uiout->text ("Temporary catchpoint ");
36dfb11c 7830 else
112e8700
SM
7831 uiout->text ("Catchpoint ");
7832 if (uiout->is_mi_like_p ())
36dfb11c 7833 {
112e8700
SM
7834 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7835 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7836 }
112e8700
SM
7837 uiout->field_int ("bkptno", b->number);
7838 uiout->text (" (forked process ");
7839 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7840 uiout->text ("), ");
ce78b96d
JB
7841 return PRINT_SRC_AND_LOC;
7842}
7843
4a64f543
MS
7844/* Implement the "print_one" breakpoint_ops method for fork
7845 catchpoints. */
ce78b96d
JB
7846
7847static void
a6d9a66e 7848print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7849{
e29a4733 7850 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7851 struct value_print_options opts;
79a45e25 7852 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7853
7854 get_user_print_options (&opts);
7855
4a64f543
MS
7856 /* Field 4, the address, is omitted (which makes the columns not
7857 line up too nicely with the headers, but the effect is relatively
7858 readable). */
79a45b7d 7859 if (opts.addressprint)
112e8700 7860 uiout->field_skip ("addr");
ce78b96d 7861 annotate_field (5);
112e8700 7862 uiout->text ("fork");
e29a4733 7863 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 7864 {
112e8700
SM
7865 uiout->text (", process ");
7866 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
7867 uiout->spaces (1);
ce78b96d 7868 }
8ac3646f 7869
112e8700
SM
7870 if (uiout->is_mi_like_p ())
7871 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7872}
7873
7874/* Implement the "print_mention" breakpoint_ops method for fork
7875 catchpoints. */
7876
7877static void
7878print_mention_catch_fork (struct breakpoint *b)
7879{
7880 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7881}
7882
6149aea9
PA
7883/* Implement the "print_recreate" breakpoint_ops method for fork
7884 catchpoints. */
7885
7886static void
7887print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7888{
7889 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7890 print_recreate_thread (b, fp);
6149aea9
PA
7891}
7892
ce78b96d
JB
7893/* The breakpoint_ops structure to be used in fork catchpoints. */
7894
2060206e 7895static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7896
4a64f543
MS
7897/* Implement the "insert" breakpoint_ops method for vfork
7898 catchpoints. */
ce78b96d 7899
77b06cd7
TJB
7900static int
7901insert_catch_vfork (struct bp_location *bl)
ce78b96d 7902{
dfd4cc63 7903 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7904}
7905
4a64f543
MS
7906/* Implement the "remove" breakpoint_ops method for vfork
7907 catchpoints. */
ce78b96d
JB
7908
7909static int
73971819 7910remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7911{
dfd4cc63 7912 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7913}
7914
7915/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7916 catchpoints. */
7917
7918static int
f1310107 7919breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7920 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7921 const struct target_waitstatus *ws)
ce78b96d 7922{
e29a4733
PA
7923 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7924
f90263c1
TT
7925 if (ws->kind != TARGET_WAITKIND_VFORKED)
7926 return 0;
7927
7928 c->forked_inferior_pid = ws->value.related_pid;
7929 return 1;
ce78b96d
JB
7930}
7931
4a64f543
MS
7932/* Implement the "print_it" breakpoint_ops method for vfork
7933 catchpoints. */
ce78b96d
JB
7934
7935static enum print_stop_action
348d480f 7936print_it_catch_vfork (bpstat bs)
ce78b96d 7937{
36dfb11c 7938 struct ui_out *uiout = current_uiout;
348d480f 7939 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7940 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7941
ce78b96d 7942 annotate_catchpoint (b->number);
f303dbd6 7943 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7944 if (b->disposition == disp_del)
112e8700 7945 uiout->text ("Temporary catchpoint ");
36dfb11c 7946 else
112e8700
SM
7947 uiout->text ("Catchpoint ");
7948 if (uiout->is_mi_like_p ())
36dfb11c 7949 {
112e8700
SM
7950 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7951 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7952 }
112e8700
SM
7953 uiout->field_int ("bkptno", b->number);
7954 uiout->text (" (vforked process ");
7955 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7956 uiout->text ("), ");
ce78b96d
JB
7957 return PRINT_SRC_AND_LOC;
7958}
7959
4a64f543
MS
7960/* Implement the "print_one" breakpoint_ops method for vfork
7961 catchpoints. */
ce78b96d
JB
7962
7963static void
a6d9a66e 7964print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7965{
e29a4733 7966 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7967 struct value_print_options opts;
79a45e25 7968 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7969
7970 get_user_print_options (&opts);
4a64f543
MS
7971 /* Field 4, the address, is omitted (which makes the columns not
7972 line up too nicely with the headers, but the effect is relatively
7973 readable). */
79a45b7d 7974 if (opts.addressprint)
112e8700 7975 uiout->field_skip ("addr");
ce78b96d 7976 annotate_field (5);
112e8700 7977 uiout->text ("vfork");
e29a4733 7978 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 7979 {
112e8700
SM
7980 uiout->text (", process ");
7981 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
7982 uiout->spaces (1);
ce78b96d 7983 }
8ac3646f 7984
112e8700
SM
7985 if (uiout->is_mi_like_p ())
7986 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
7987}
7988
7989/* Implement the "print_mention" breakpoint_ops method for vfork
7990 catchpoints. */
7991
7992static void
7993print_mention_catch_vfork (struct breakpoint *b)
7994{
7995 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7996}
7997
6149aea9
PA
7998/* Implement the "print_recreate" breakpoint_ops method for vfork
7999 catchpoints. */
8000
8001static void
8002print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8003{
8004 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8005 print_recreate_thread (b, fp);
6149aea9
PA
8006}
8007
ce78b96d
JB
8008/* The breakpoint_ops structure to be used in vfork catchpoints. */
8009
2060206e 8010static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8011
edcc5120 8012/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 8013 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
8014 CATCH_SOLIB_BREAKPOINT_OPS. */
8015
c1fc2657 8016struct solib_catchpoint : public breakpoint
edcc5120 8017{
c1fc2657 8018 ~solib_catchpoint () override;
edcc5120
TT
8019
8020 /* True for "catch load", false for "catch unload". */
8021 unsigned char is_load;
8022
8023 /* Regular expression to match, if any. COMPILED is only valid when
8024 REGEX is non-NULL. */
8025 char *regex;
2d7cc5c7 8026 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
8027};
8028
c1fc2657 8029solib_catchpoint::~solib_catchpoint ()
edcc5120 8030{
c1fc2657 8031 xfree (this->regex);
edcc5120
TT
8032}
8033
8034static int
8035insert_catch_solib (struct bp_location *ignore)
8036{
8037 return 0;
8038}
8039
8040static int
73971819 8041remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8042{
8043 return 0;
8044}
8045
8046static int
8047breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 8048 const address_space *aspace,
edcc5120
TT
8049 CORE_ADDR bp_addr,
8050 const struct target_waitstatus *ws)
8051{
8052 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8053 struct breakpoint *other;
8054
8055 if (ws->kind == TARGET_WAITKIND_LOADED)
8056 return 1;
8057
8058 ALL_BREAKPOINTS (other)
8059 {
8060 struct bp_location *other_bl;
8061
8062 if (other == bl->owner)
8063 continue;
8064
8065 if (other->type != bp_shlib_event)
8066 continue;
8067
c1fc2657 8068 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
8069 continue;
8070
8071 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8072 {
8073 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8074 return 1;
8075 }
8076 }
8077
8078 return 0;
8079}
8080
8081static void
8082check_status_catch_solib (struct bpstats *bs)
8083{
8084 struct solib_catchpoint *self
8085 = (struct solib_catchpoint *) bs->breakpoint_at;
8086 int ix;
8087
8088 if (self->is_load)
8089 {
8090 struct so_list *iter;
8091
8092 for (ix = 0;
8093 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8094 ix, iter);
8095 ++ix)
8096 {
8097 if (!self->regex
2d7cc5c7 8098 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8099 return;
8100 }
8101 }
8102 else
8103 {
8104 char *iter;
8105
8106 for (ix = 0;
8107 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8108 ix, iter);
8109 ++ix)
8110 {
8111 if (!self->regex
2d7cc5c7 8112 || self->compiled->exec (iter, 0, NULL, 0) == 0)
edcc5120
TT
8113 return;
8114 }
8115 }
8116
8117 bs->stop = 0;
8118 bs->print_it = print_it_noop;
8119}
8120
8121static enum print_stop_action
8122print_it_catch_solib (bpstat bs)
8123{
8124 struct breakpoint *b = bs->breakpoint_at;
8125 struct ui_out *uiout = current_uiout;
8126
8127 annotate_catchpoint (b->number);
f303dbd6 8128 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8129 if (b->disposition == disp_del)
112e8700 8130 uiout->text ("Temporary catchpoint ");
edcc5120 8131 else
112e8700
SM
8132 uiout->text ("Catchpoint ");
8133 uiout->field_int ("bkptno", b->number);
8134 uiout->text ("\n");
8135 if (uiout->is_mi_like_p ())
8136 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8137 print_solib_event (1);
8138 return PRINT_SRC_AND_LOC;
8139}
8140
8141static void
8142print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8143{
8144 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8145 struct value_print_options opts;
8146 struct ui_out *uiout = current_uiout;
8147 char *msg;
8148
8149 get_user_print_options (&opts);
8150 /* Field 4, the address, is omitted (which makes the columns not
8151 line up too nicely with the headers, but the effect is relatively
8152 readable). */
8153 if (opts.addressprint)
8154 {
8155 annotate_field (4);
112e8700 8156 uiout->field_skip ("addr");
edcc5120
TT
8157 }
8158
8159 annotate_field (5);
8160 if (self->is_load)
8161 {
8162 if (self->regex)
8163 msg = xstrprintf (_("load of library matching %s"), self->regex);
8164 else
8165 msg = xstrdup (_("load of library"));
8166 }
8167 else
8168 {
8169 if (self->regex)
8170 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8171 else
8172 msg = xstrdup (_("unload of library"));
8173 }
112e8700 8174 uiout->field_string ("what", msg);
edcc5120 8175 xfree (msg);
8ac3646f 8176
112e8700
SM
8177 if (uiout->is_mi_like_p ())
8178 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8179}
8180
8181static void
8182print_mention_catch_solib (struct breakpoint *b)
8183{
8184 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8185
8186 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8187 self->is_load ? "load" : "unload");
8188}
8189
8190static void
8191print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8192{
8193 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8194
8195 fprintf_unfiltered (fp, "%s %s",
8196 b->disposition == disp_del ? "tcatch" : "catch",
8197 self->is_load ? "load" : "unload");
8198 if (self->regex)
8199 fprintf_unfiltered (fp, " %s", self->regex);
8200 fprintf_unfiltered (fp, "\n");
8201}
8202
8203static struct breakpoint_ops catch_solib_breakpoint_ops;
8204
91985142
MG
8205/* Shared helper function (MI and CLI) for creating and installing
8206 a shared object event catchpoint. If IS_LOAD is non-zero then
8207 the events to be caught are load events, otherwise they are
8208 unload events. If IS_TEMP is non-zero the catchpoint is a
8209 temporary one. If ENABLED is non-zero the catchpoint is
8210 created in an enabled state. */
edcc5120 8211
91985142 8212void
a121b7c1 8213add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120 8214{
edcc5120 8215 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8216
edcc5120
TT
8217 if (!arg)
8218 arg = "";
f1735a53 8219 arg = skip_spaces (arg);
edcc5120 8220
36bd8eaa 8221 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8222
8223 if (*arg != '\0')
8224 {
2d7cc5c7
PA
8225 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8226 _("Invalid regexp")));
edcc5120
TT
8227 c->regex = xstrdup (arg);
8228 }
8229
8230 c->is_load = is_load;
36bd8eaa 8231 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8232 &catch_solib_breakpoint_ops);
8233
c1fc2657 8234 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8235
b270e6f9 8236 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8237}
8238
91985142
MG
8239/* A helper function that does all the work for "catch load" and
8240 "catch unload". */
8241
8242static void
8243catch_load_or_unload (char *arg, int from_tty, int is_load,
8244 struct cmd_list_element *command)
8245{
8246 int tempflag;
8247 const int enabled = 1;
8248
8249 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8250
8251 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8252}
8253
edcc5120
TT
8254static void
8255catch_load_command_1 (char *arg, int from_tty,
8256 struct cmd_list_element *command)
8257{
8258 catch_load_or_unload (arg, from_tty, 1, command);
8259}
8260
8261static void
8262catch_unload_command_1 (char *arg, int from_tty,
8263 struct cmd_list_element *command)
8264{
8265 catch_load_or_unload (arg, from_tty, 0, command);
8266}
8267
346774a9
PA
8268/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8269 is non-zero, then make the breakpoint temporary. If COND_STRING is
8270 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8271 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8272
ab04a2af 8273void
346774a9
PA
8274init_catchpoint (struct breakpoint *b,
8275 struct gdbarch *gdbarch, int tempflag,
63160a43 8276 const char *cond_string,
c0a91b2b 8277 const struct breakpoint_ops *ops)
c906108c 8278{
51abb421 8279 symtab_and_line sal;
6c95b8df 8280 sal.pspace = current_program_space;
c5aa993b 8281
28010a5d 8282 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8283
1b36a34b 8284 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8285 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8286}
8287
28010a5d 8288void
b270e6f9 8289install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8290{
b270e6f9 8291 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8292 set_breakpoint_number (internal, b);
558a9d82
YQ
8293 if (is_tracepoint (b))
8294 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8295 if (!internal)
8296 mention (b);
c56053d2 8297 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8298
8299 if (update_gll)
44702360 8300 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8301}
8302
9b70b993 8303static void
a6d9a66e 8304create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8305 int tempflag, const char *cond_string,
c0a91b2b 8306 const struct breakpoint_ops *ops)
c906108c 8307{
b270e6f9 8308 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8309
b270e6f9 8310 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
e29a4733
PA
8311
8312 c->forked_inferior_pid = null_ptid;
8313
b270e6f9 8314 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8315}
8316
fe798b75
JB
8317/* Exec catchpoints. */
8318
b4d90040 8319/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8320 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8321 CATCH_EXEC_BREAKPOINT_OPS. */
8322
c1fc2657 8323struct exec_catchpoint : public breakpoint
b4d90040 8324{
c1fc2657 8325 ~exec_catchpoint () override;
b4d90040
PA
8326
8327 /* Filename of a program whose exec triggered this catchpoint.
8328 This field is only valid immediately after this catchpoint has
8329 triggered. */
8330 char *exec_pathname;
8331};
8332
c1fc2657 8333/* Exec catchpoint destructor. */
b4d90040 8334
c1fc2657 8335exec_catchpoint::~exec_catchpoint ()
b4d90040 8336{
c1fc2657 8337 xfree (this->exec_pathname);
b4d90040
PA
8338}
8339
77b06cd7
TJB
8340static int
8341insert_catch_exec (struct bp_location *bl)
c906108c 8342{
dfd4cc63 8343 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8344}
c906108c 8345
fe798b75 8346static int
73971819 8347remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8348{
dfd4cc63 8349 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8350}
c906108c 8351
fe798b75 8352static int
f1310107 8353breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8354 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8355 const struct target_waitstatus *ws)
fe798b75 8356{
b4d90040
PA
8357 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8358
f90263c1
TT
8359 if (ws->kind != TARGET_WAITKIND_EXECD)
8360 return 0;
8361
8362 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8363 return 1;
fe798b75 8364}
c906108c 8365
fe798b75 8366static enum print_stop_action
348d480f 8367print_it_catch_exec (bpstat bs)
fe798b75 8368{
36dfb11c 8369 struct ui_out *uiout = current_uiout;
348d480f 8370 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8371 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8372
fe798b75 8373 annotate_catchpoint (b->number);
f303dbd6 8374 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8375 if (b->disposition == disp_del)
112e8700 8376 uiout->text ("Temporary catchpoint ");
36dfb11c 8377 else
112e8700
SM
8378 uiout->text ("Catchpoint ");
8379 if (uiout->is_mi_like_p ())
36dfb11c 8380 {
112e8700
SM
8381 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8382 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8383 }
112e8700
SM
8384 uiout->field_int ("bkptno", b->number);
8385 uiout->text (" (exec'd ");
8386 uiout->field_string ("new-exec", c->exec_pathname);
8387 uiout->text ("), ");
36dfb11c 8388
fe798b75 8389 return PRINT_SRC_AND_LOC;
c906108c
SS
8390}
8391
fe798b75 8392static void
a6d9a66e 8393print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8394{
b4d90040 8395 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8396 struct value_print_options opts;
79a45e25 8397 struct ui_out *uiout = current_uiout;
fe798b75
JB
8398
8399 get_user_print_options (&opts);
8400
8401 /* Field 4, the address, is omitted (which makes the columns
8402 not line up too nicely with the headers, but the effect
8403 is relatively readable). */
8404 if (opts.addressprint)
112e8700 8405 uiout->field_skip ("addr");
fe798b75 8406 annotate_field (5);
112e8700 8407 uiout->text ("exec");
b4d90040 8408 if (c->exec_pathname != NULL)
fe798b75 8409 {
112e8700
SM
8410 uiout->text (", program \"");
8411 uiout->field_string ("what", c->exec_pathname);
8412 uiout->text ("\" ");
fe798b75 8413 }
8ac3646f 8414
112e8700
SM
8415 if (uiout->is_mi_like_p ())
8416 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8417}
8418
8419static void
8420print_mention_catch_exec (struct breakpoint *b)
8421{
8422 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8423}
8424
6149aea9
PA
8425/* Implement the "print_recreate" breakpoint_ops method for exec
8426 catchpoints. */
8427
8428static void
8429print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8430{
8431 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8432 print_recreate_thread (b, fp);
6149aea9
PA
8433}
8434
2060206e 8435static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8436
c906108c 8437static int
fba45db2 8438hw_breakpoint_used_count (void)
c906108c 8439{
c906108c 8440 int i = 0;
f1310107
TJB
8441 struct breakpoint *b;
8442 struct bp_location *bl;
c906108c
SS
8443
8444 ALL_BREAKPOINTS (b)
c5aa993b 8445 {
d6b74ac4 8446 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8447 for (bl = b->loc; bl; bl = bl->next)
8448 {
8449 /* Special types of hardware breakpoints may use more than
8450 one register. */
348d480f 8451 i += b->ops->resources_needed (bl);
f1310107 8452 }
c5aa993b 8453 }
c906108c
SS
8454
8455 return i;
8456}
8457
a1398e0c
PA
8458/* Returns the resources B would use if it were a hardware
8459 watchpoint. */
8460
c906108c 8461static int
a1398e0c 8462hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8463{
c906108c 8464 int i = 0;
e09342b5 8465 struct bp_location *bl;
c906108c 8466
a1398e0c
PA
8467 if (!breakpoint_enabled (b))
8468 return 0;
8469
8470 for (bl = b->loc; bl; bl = bl->next)
8471 {
8472 /* Special types of hardware watchpoints may use more than
8473 one register. */
8474 i += b->ops->resources_needed (bl);
8475 }
8476
8477 return i;
8478}
8479
8480/* Returns the sum the used resources of all hardware watchpoints of
8481 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8482 the sum of the used resources of all hardware watchpoints of other
8483 types _not_ TYPE. */
8484
8485static int
8486hw_watchpoint_used_count_others (struct breakpoint *except,
8487 enum bptype type, int *other_type_used)
8488{
8489 int i = 0;
8490 struct breakpoint *b;
8491
c906108c
SS
8492 *other_type_used = 0;
8493 ALL_BREAKPOINTS (b)
e09342b5 8494 {
a1398e0c
PA
8495 if (b == except)
8496 continue;
e09342b5
TJB
8497 if (!breakpoint_enabled (b))
8498 continue;
8499
a1398e0c
PA
8500 if (b->type == type)
8501 i += hw_watchpoint_use_count (b);
8502 else if (is_hardware_watchpoint (b))
8503 *other_type_used = 1;
e09342b5
TJB
8504 }
8505
c906108c
SS
8506 return i;
8507}
8508
c906108c 8509void
fba45db2 8510disable_watchpoints_before_interactive_call_start (void)
c906108c 8511{
c5aa993b 8512 struct breakpoint *b;
c906108c
SS
8513
8514 ALL_BREAKPOINTS (b)
c5aa993b 8515 {
cc60f2e3 8516 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8517 {
b5de0fa7 8518 b->enable_state = bp_call_disabled;
44702360 8519 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8520 }
8521 }
c906108c
SS
8522}
8523
8524void
fba45db2 8525enable_watchpoints_after_interactive_call_stop (void)
c906108c 8526{
c5aa993b 8527 struct breakpoint *b;
c906108c
SS
8528
8529 ALL_BREAKPOINTS (b)
c5aa993b 8530 {
cc60f2e3 8531 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8532 {
b5de0fa7 8533 b->enable_state = bp_enabled;
44702360 8534 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8535 }
8536 }
c906108c
SS
8537}
8538
8bea4e01
UW
8539void
8540disable_breakpoints_before_startup (void)
8541{
6c95b8df 8542 current_program_space->executing_startup = 1;
44702360 8543 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8544}
8545
8546void
8547enable_breakpoints_after_startup (void)
8548{
6c95b8df 8549 current_program_space->executing_startup = 0;
f8eba3c6 8550 breakpoint_re_set ();
8bea4e01
UW
8551}
8552
7c16b83e
PA
8553/* Create a new single-step breakpoint for thread THREAD, with no
8554 locations. */
c906108c 8555
7c16b83e
PA
8556static struct breakpoint *
8557new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8558{
b270e6f9 8559 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8560
b270e6f9 8561 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8562 &momentary_breakpoint_ops);
8563
8564 b->disposition = disp_donttouch;
8565 b->frame_id = null_frame_id;
8566
8567 b->thread = thread;
8568 gdb_assert (b->thread != 0);
8569
b270e6f9 8570 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8571}
8572
8573/* Set a momentary breakpoint of type TYPE at address specified by
8574 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8575 frame. */
c906108c 8576
454dafbd 8577breakpoint_up
a6d9a66e
UW
8578set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8579 struct frame_id frame_id, enum bptype type)
c906108c 8580{
52f0bd74 8581 struct breakpoint *b;
edb3359d 8582
193facb3
JK
8583 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8584 tail-called one. */
8585 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8586
06edf0c0 8587 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8588 b->enable_state = bp_enabled;
8589 b->disposition = disp_donttouch;
818dd999 8590 b->frame_id = frame_id;
c906108c 8591
4a64f543
MS
8592 /* If we're debugging a multi-threaded program, then we want
8593 momentary breakpoints to be active in only a single thread of
8594 control. */
39f77062 8595 if (in_thread_list (inferior_ptid))
5d5658a1 8596 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8597
44702360 8598 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8599
454dafbd 8600 return breakpoint_up (b);
c906108c 8601}
611c83ae 8602
06edf0c0 8603/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8604 The new breakpoint will have type TYPE, use OPS as its
8605 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8606
06edf0c0
PA
8607static struct breakpoint *
8608momentary_breakpoint_from_master (struct breakpoint *orig,
8609 enum bptype type,
a1aa2221
LM
8610 const struct breakpoint_ops *ops,
8611 int loc_enabled)
e58b0e63
PA
8612{
8613 struct breakpoint *copy;
8614
06edf0c0 8615 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8616 copy->loc = allocate_bp_location (copy);
0e30163f 8617 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8618
a6d9a66e 8619 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8620 copy->loc->requested_address = orig->loc->requested_address;
8621 copy->loc->address = orig->loc->address;
8622 copy->loc->section = orig->loc->section;
6c95b8df 8623 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8624 copy->loc->probe = orig->loc->probe;
f8eba3c6 8625 copy->loc->line_number = orig->loc->line_number;
2f202fde 8626 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8627 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8628 copy->frame_id = orig->frame_id;
8629 copy->thread = orig->thread;
6c95b8df 8630 copy->pspace = orig->pspace;
e58b0e63
PA
8631
8632 copy->enable_state = bp_enabled;
8633 copy->disposition = disp_donttouch;
8634 copy->number = internal_breakpoint_number--;
8635
44702360 8636 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8637 return copy;
8638}
8639
06edf0c0
PA
8640/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8641 ORIG is NULL. */
8642
8643struct breakpoint *
8644clone_momentary_breakpoint (struct breakpoint *orig)
8645{
8646 /* If there's nothing to clone, then return nothing. */
8647 if (orig == NULL)
8648 return NULL;
8649
a1aa2221 8650 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8651}
8652
454dafbd 8653breakpoint_up
a6d9a66e
UW
8654set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8655 enum bptype type)
611c83ae
PA
8656{
8657 struct symtab_and_line sal;
8658
8659 sal = find_pc_line (pc, 0);
8660 sal.pc = pc;
8661 sal.section = find_pc_overlay (pc);
8662 sal.explicit_pc = 1;
8663
a6d9a66e 8664 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8665}
c906108c 8666\f
c5aa993b 8667
c906108c
SS
8668/* Tell the user we have just set a breakpoint B. */
8669
8670static void
fba45db2 8671mention (struct breakpoint *b)
c906108c 8672{
348d480f 8673 b->ops->print_mention (b);
112e8700 8674 if (current_uiout->is_mi_like_p ())
fb40c209 8675 return;
c906108c
SS
8676 printf_filtered ("\n");
8677}
c906108c 8678\f
c5aa993b 8679
1a853c52
PA
8680static int bp_loc_is_permanent (struct bp_location *loc);
8681
0d381245 8682static struct bp_location *
39d61571 8683add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8684 const struct symtab_and_line *sal)
8685{
8686 struct bp_location *loc, **tmp;
3742cc8b
YQ
8687 CORE_ADDR adjusted_address;
8688 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8689
8690 if (loc_gdbarch == NULL)
8691 loc_gdbarch = b->gdbarch;
8692
8693 /* Adjust the breakpoint's address prior to allocating a location.
8694 Once we call allocate_bp_location(), that mostly uninitialized
8695 location will be placed on the location chain. Adjustment of the
8696 breakpoint may cause target_read_memory() to be called and we do
8697 not want its scan of the location chain to find a breakpoint and
8698 location that's only been partially initialized. */
8699 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8700 sal->pc, b->type);
0d381245 8701
d30113d4 8702 /* Sort the locations by their ADDRESS. */
39d61571 8703 loc = allocate_bp_location (b);
d30113d4
JK
8704 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8705 tmp = &((*tmp)->next))
0d381245 8706 ;
d30113d4 8707 loc->next = *tmp;
0d381245 8708 *tmp = loc;
3742cc8b 8709
0d381245 8710 loc->requested_address = sal->pc;
3742cc8b 8711 loc->address = adjusted_address;
6c95b8df 8712 loc->pspace = sal->pspace;
729662a5
TT
8713 loc->probe.probe = sal->probe;
8714 loc->probe.objfile = sal->objfile;
6c95b8df 8715 gdb_assert (loc->pspace != NULL);
0d381245 8716 loc->section = sal->section;
3742cc8b 8717 loc->gdbarch = loc_gdbarch;
f8eba3c6 8718 loc->line_number = sal->line;
2f202fde 8719 loc->symtab = sal->symtab;
4a27f119 8720 loc->symbol = sal->symbol;
f8eba3c6 8721
0e30163f
JK
8722 set_breakpoint_location_function (loc,
8723 sal->explicit_pc || sal->explicit_line);
1a853c52 8724
6ae88661
LM
8725 /* While by definition, permanent breakpoints are already present in the
8726 code, we don't mark the location as inserted. Normally one would expect
8727 that GDB could rely on that breakpoint instruction to stop the program,
8728 thus removing the need to insert its own breakpoint, except that executing
8729 the breakpoint instruction can kill the target instead of reporting a
8730 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8731 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8732 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8733 breakpoint be inserted normally results in QEMU knowing about the GDB
8734 breakpoint, and thus trap before the breakpoint instruction is executed.
8735 (If GDB later needs to continue execution past the permanent breakpoint,
8736 it manually increments the PC, thus avoiding executing the breakpoint
8737 instruction.) */
1a853c52 8738 if (bp_loc_is_permanent (loc))
6ae88661 8739 loc->permanent = 1;
1a853c52 8740
0d381245
VP
8741 return loc;
8742}
514f746b
AR
8743\f
8744
1cf4d951 8745/* See breakpoint.h. */
514f746b 8746
1cf4d951
PA
8747int
8748program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8749{
8750 int len;
8751 CORE_ADDR addr;
1afeeb75 8752 const gdb_byte *bpoint;
514f746b
AR
8753 gdb_byte *target_mem;
8754
1cf4d951
PA
8755 addr = address;
8756 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8757
8758 /* Software breakpoints unsupported? */
8759 if (bpoint == NULL)
8760 return 0;
8761
224c3ddb 8762 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
8763
8764 /* Enable the automatic memory restoration from breakpoints while
8765 we read the memory. Otherwise we could say about our temporary
8766 breakpoints they are permanent. */
cb85b21b
TT
8767 scoped_restore restore_memory
8768 = make_scoped_restore_show_memory_breakpoints (0);
1cf4d951
PA
8769
8770 if (target_read_memory (address, target_mem, len) == 0
8771 && memcmp (target_mem, bpoint, len) == 0)
cb85b21b 8772 return 1;
1cf4d951 8773
cb85b21b 8774 return 0;
1cf4d951
PA
8775}
8776
8777/* Return 1 if LOC is pointing to a permanent breakpoint,
8778 return 0 otherwise. */
8779
8780static int
8781bp_loc_is_permanent (struct bp_location *loc)
8782{
514f746b
AR
8783 gdb_assert (loc != NULL);
8784
244558af
LM
8785 /* If we have a catchpoint or a watchpoint, just return 0. We should not
8786 attempt to read from the addresses the locations of these breakpoint types
8787 point to. program_breakpoint_here_p, below, will attempt to read
8788 memory. */
8789 if (!breakpoint_address_is_meaningful (loc->owner))
8790 return 0;
8791
5ed8105e 8792 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8793 switch_to_program_space_and_thread (loc->pspace);
5ed8105e 8794 return program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8795}
8796
e7e0cddf
SS
8797/* Build a command list for the dprintf corresponding to the current
8798 settings of the dprintf style options. */
8799
8800static void
8801update_dprintf_command_list (struct breakpoint *b)
8802{
8803 char *dprintf_args = b->extra_string;
8804 char *printf_line = NULL;
8805
8806 if (!dprintf_args)
8807 return;
8808
8809 dprintf_args = skip_spaces (dprintf_args);
8810
8811 /* Allow a comma, as it may have terminated a location, but don't
8812 insist on it. */
8813 if (*dprintf_args == ',')
8814 ++dprintf_args;
8815 dprintf_args = skip_spaces (dprintf_args);
8816
8817 if (*dprintf_args != '"')
8818 error (_("Bad format string, missing '\"'."));
8819
d3ce09f5 8820 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8821 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8822 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8823 {
8824 if (!dprintf_function)
8825 error (_("No function supplied for dprintf call"));
8826
8827 if (dprintf_channel && strlen (dprintf_channel) > 0)
8828 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8829 dprintf_function,
8830 dprintf_channel,
8831 dprintf_args);
8832 else
8833 printf_line = xstrprintf ("call (void) %s (%s)",
8834 dprintf_function,
8835 dprintf_args);
8836 }
d3ce09f5
SS
8837 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8838 {
8839 if (target_can_run_breakpoint_commands ())
8840 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8841 else
8842 {
8843 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8844 printf_line = xstrprintf ("printf %s", dprintf_args);
8845 }
8846 }
e7e0cddf
SS
8847 else
8848 internal_error (__FILE__, __LINE__,
8849 _("Invalid dprintf style."));
8850
f28045c2 8851 gdb_assert (printf_line != NULL);
9d6e6e84 8852 /* Manufacture a printf sequence. */
f28045c2 8853 {
8d749320 8854 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 8855
f28045c2
YQ
8856 printf_cmd_line->control_type = simple_control;
8857 printf_cmd_line->body_count = 0;
8858 printf_cmd_line->body_list = NULL;
9d6e6e84 8859 printf_cmd_line->next = NULL;
f28045c2 8860 printf_cmd_line->line = printf_line;
e7e0cddf 8861
93921405 8862 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
f28045c2 8863 }
e7e0cddf
SS
8864}
8865
8866/* Update all dprintf commands, making their command lists reflect
8867 current style settings. */
8868
8869static void
8870update_dprintf_commands (char *args, int from_tty,
8871 struct cmd_list_element *c)
8872{
8873 struct breakpoint *b;
8874
8875 ALL_BREAKPOINTS (b)
8876 {
8877 if (b->type == bp_dprintf)
8878 update_dprintf_command_list (b);
8879 }
8880}
c3f6f71d 8881
f00aae0f
KS
8882/* Create a breakpoint with SAL as location. Use LOCATION
8883 as a description of the location, and COND_STRING
b35a8b2f
DE
8884 as condition expression. If LOCATION is NULL then create an
8885 "address location" from the address in the SAL. */
018d34a4
VP
8886
8887static void
d9b3f62e 8888init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8889 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8890 event_location_up &&location,
e1e01040
PA
8891 gdb::unique_xmalloc_ptr<char> filter,
8892 gdb::unique_xmalloc_ptr<char> cond_string,
8893 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8894 enum bptype type, enum bpdisp disposition,
8895 int thread, int task, int ignore_count,
c0a91b2b 8896 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8897 int enabled, int internal, unsigned flags,
8898 int display_canonical)
018d34a4 8899{
0d381245 8900 int i;
018d34a4
VP
8901
8902 if (type == bp_hardware_breakpoint)
8903 {
fbbd034e
AS
8904 int target_resources_ok;
8905
8906 i = hw_breakpoint_used_count ();
8907 target_resources_ok =
8908 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8909 i + 1, 0);
8910 if (target_resources_ok == 0)
8911 error (_("No hardware breakpoint support in the target."));
8912 else if (target_resources_ok < 0)
8913 error (_("Hardware breakpoints used exceeds limit."));
8914 }
8915
6c5b2ebe 8916 gdb_assert (!sals.empty ());
6c95b8df 8917
6c5b2ebe 8918 for (const auto &sal : sals)
0d381245 8919 {
0d381245
VP
8920 struct bp_location *loc;
8921
8922 if (from_tty)
5af949e3
UW
8923 {
8924 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8925 if (!loc_gdbarch)
8926 loc_gdbarch = gdbarch;
8927
8928 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8929 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8930 }
0d381245 8931
6c5b2ebe 8932 if (&sal == &sals[0])
0d381245 8933 {
d9b3f62e 8934 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8935 b->thread = thread;
4a306c9a 8936 b->task = task;
855a6e68 8937
e1e01040
PA
8938 b->cond_string = cond_string.release ();
8939 b->extra_string = extra_string.release ();
0d381245 8940 b->ignore_count = ignore_count;
41447f92 8941 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8942 b->disposition = disposition;
6c95b8df 8943
44f238bb
PA
8944 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8945 b->loc->inserted = 1;
8946
0fb4aa4b
PA
8947 if (type == bp_static_tracepoint)
8948 {
d9b3f62e 8949 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8950 struct static_tracepoint_marker marker;
8951
983af33b 8952 if (strace_marker_p (b))
0fb4aa4b
PA
8953 {
8954 /* We already know the marker exists, otherwise, we
8955 wouldn't see a sal for it. */
d28cd78a
TT
8956 const char *p
8957 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8958 const char *endp;
0fb4aa4b 8959 char *marker_str;
0fb4aa4b 8960
f1735a53 8961 p = skip_spaces (p);
0fb4aa4b 8962
f1735a53 8963 endp = skip_to_space (p);
0fb4aa4b
PA
8964
8965 marker_str = savestring (p, endp - p);
d9b3f62e 8966 t->static_trace_marker_id = marker_str;
0fb4aa4b 8967
3e43a32a
MS
8968 printf_filtered (_("Probed static tracepoint "
8969 "marker \"%s\"\n"),
d9b3f62e 8970 t->static_trace_marker_id);
0fb4aa4b
PA
8971 }
8972 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8973 {
d9b3f62e 8974 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
8975 release_static_tracepoint_marker (&marker);
8976
3e43a32a
MS
8977 printf_filtered (_("Probed static tracepoint "
8978 "marker \"%s\"\n"),
d9b3f62e 8979 t->static_trace_marker_id);
0fb4aa4b
PA
8980 }
8981 else
3e43a32a
MS
8982 warning (_("Couldn't determine the static "
8983 "tracepoint marker to probe"));
0fb4aa4b
PA
8984 }
8985
0d381245
VP
8986 loc = b->loc;
8987 }
8988 else
018d34a4 8989 {
39d61571 8990 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8991 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8992 loc->inserted = 1;
0d381245
VP
8993 }
8994
8995 if (b->cond_string)
8996 {
bbc13ae3
KS
8997 const char *arg = b->cond_string;
8998
1bb9788d
TT
8999 loc->cond = parse_exp_1 (&arg, loc->address,
9000 block_for_pc (loc->address), 0);
0d381245 9001 if (*arg)
588ae58c 9002 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9003 }
e7e0cddf
SS
9004
9005 /* Dynamic printf requires and uses additional arguments on the
9006 command line, otherwise it's an error. */
9007 if (type == bp_dprintf)
9008 {
9009 if (b->extra_string)
9010 update_dprintf_command_list (b);
9011 else
9012 error (_("Format string required"));
9013 }
9014 else if (b->extra_string)
588ae58c 9015 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9016 }
018d34a4 9017
56435ebe 9018 b->display_canonical = display_canonical;
f00aae0f 9019 if (location != NULL)
d28cd78a 9020 b->location = std::move (location);
018d34a4 9021 else
d28cd78a 9022 b->location = new_address_location (b->loc->address, NULL, 0);
e1e01040 9023 b->filter = filter.release ();
d9b3f62e 9024}
018d34a4 9025
d9b3f62e
PA
9026static void
9027create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 9028 gdb::array_view<const symtab_and_line> sals,
ffc2605c 9029 event_location_up &&location,
e1e01040
PA
9030 gdb::unique_xmalloc_ptr<char> filter,
9031 gdb::unique_xmalloc_ptr<char> cond_string,
9032 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9033 enum bptype type, enum bpdisp disposition,
9034 int thread, int task, int ignore_count,
c0a91b2b 9035 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9036 int enabled, int internal, unsigned flags,
9037 int display_canonical)
d9b3f62e 9038{
a5e364af 9039 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 9040
a5e364af 9041 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 9042 sals, std::move (location),
e1e01040
PA
9043 std::move (filter),
9044 std::move (cond_string),
9045 std::move (extra_string),
d9b3f62e
PA
9046 type, disposition,
9047 thread, task, ignore_count,
9048 ops, from_tty,
44f238bb
PA
9049 enabled, internal, flags,
9050 display_canonical);
d9b3f62e 9051
b270e6f9 9052 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
9053}
9054
9055/* Add SALS.nelts breakpoints to the breakpoint table. For each
9056 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9057 value. COND_STRING, if not NULL, specified the condition to be
9058 used for all breakpoints. Essentially the only case where
9059 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9060 function. In that case, it's still not possible to specify
9061 separate conditions for different overloaded functions, so
9062 we take just a single condition string.
9063
c3f6f71d 9064 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9065 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9066 array contents). If the function fails (error() is called), the
9067 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9068 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9069
9070static void
8cdf0e15 9071create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9072 struct linespec_result *canonical,
e1e01040
PA
9073 gdb::unique_xmalloc_ptr<char> cond_string,
9074 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
9075 enum bptype type, enum bpdisp disposition,
9076 int thread, int task, int ignore_count,
c0a91b2b 9077 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9078 int enabled, int internal, unsigned flags)
c906108c 9079{
f8eba3c6 9080 if (canonical->pre_expanded)
6c5b2ebe 9081 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 9082
6c5b2ebe 9083 for (const auto &lsal : canonical->lsals)
c3f6f71d 9084 {
f00aae0f 9085 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9086 'break', without arguments. */
ffc2605c 9087 event_location_up location
f00aae0f 9088 = (canonical->location != NULL
8e9e35b1 9089 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 9090 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 9091 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 9092
6c5b2ebe 9093 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 9094 std::move (location),
e1e01040
PA
9095 std::move (filter_string),
9096 std::move (cond_string),
9097 std::move (extra_string),
e7e0cddf 9098 type, disposition,
84f4c1fe 9099 thread, task, ignore_count, ops,
44f238bb 9100 from_tty, enabled, internal, flags,
56435ebe 9101 canonical->special_display);
c3f6f71d 9102 }
c3f6f71d 9103}
c906108c 9104
f00aae0f 9105/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9106 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9107 addresses found. LOCATION points to the end of the SAL (for
9108 linespec locations).
9998af43
TJB
9109
9110 The array and the line spec strings are allocated on the heap, it is
9111 the caller's responsibility to free them. */
c906108c 9112
b9362cc7 9113static void
f00aae0f 9114parse_breakpoint_sals (const struct event_location *location,
58438ac1 9115 struct linespec_result *canonical)
c3f6f71d 9116{
f00aae0f
KS
9117 struct symtab_and_line cursal;
9118
9119 if (event_location_type (location) == LINESPEC_LOCATION)
9120 {
9121 const char *address = get_linespec_location (location);
9122
9123 if (address == NULL)
9124 {
9125 /* The last displayed codepoint, if it's valid, is our default
9126 breakpoint address. */
9127 if (last_displayed_sal_is_valid ())
9128 {
f00aae0f
KS
9129 /* Set sal's pspace, pc, symtab, and line to the values
9130 corresponding to the last call to print_frame_info.
9131 Be sure to reinitialize LINE with NOTCURRENT == 0
9132 as the breakpoint line number is inappropriate otherwise.
9133 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
9134 symtab_and_line sal = get_last_displayed_sal ();
9135 CORE_ADDR pc = sal.pc;
9136
f00aae0f
KS
9137 sal = find_pc_line (pc, 0);
9138
9139 /* "break" without arguments is equivalent to "break *PC"
9140 where PC is the last displayed codepoint's address. So
9141 make sure to set sal.explicit_pc to prevent GDB from
9142 trying to expand the list of sals to include all other
9143 instances with the same symtab and line. */
9144 sal.pc = pc;
9145 sal.explicit_pc = 1;
9146
6c5b2ebe
PA
9147 struct linespec_sals lsal;
9148 lsal.sals = {sal};
f00aae0f
KS
9149 lsal.canonical = NULL;
9150
6c5b2ebe 9151 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9152 return;
9153 }
9154 else
9155 error (_("No default breakpoint address now."));
c906108c 9156 }
c906108c 9157 }
f00aae0f
KS
9158
9159 /* Force almost all breakpoints to be in terms of the
9160 current_source_symtab (which is decode_line_1's default).
9161 This should produce the results we want almost all of the
9162 time while leaving default_breakpoint_* alone.
9163
9164 ObjC: However, don't match an Objective-C method name which
9165 may have a '+' or '-' succeeded by a '['. */
9166 cursal = get_current_source_symtab_and_line ();
9167 if (last_displayed_sal_is_valid ())
c906108c 9168 {
f00aae0f 9169 const char *address = NULL;
cc80f267 9170
f00aae0f
KS
9171 if (event_location_type (location) == LINESPEC_LOCATION)
9172 address = get_linespec_location (location);
cc80f267 9173
f00aae0f
KS
9174 if (!cursal.symtab
9175 || (address != NULL
9176 && strchr ("+-", address[0]) != NULL
9177 && address[1] != '['))
9178 {
c2f4122d 9179 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9180 get_last_displayed_symtab (),
9181 get_last_displayed_line (),
9182 canonical, NULL, NULL);
9183 return;
9184 }
c906108c 9185 }
f00aae0f 9186
c2f4122d 9187 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9188 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9189}
c906108c 9190
c906108c 9191
c3f6f71d 9192/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9193 inserted as a breakpoint. If it can't throw an error. */
c906108c 9194
b9362cc7 9195static void
6c5b2ebe 9196breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9197{
6c5b2ebe
PA
9198 for (auto &sal : sals)
9199 resolve_sal_pc (&sal);
c3f6f71d
JM
9200}
9201
7a697b8d
SS
9202/* Fast tracepoints may have restrictions on valid locations. For
9203 instance, a fast tracepoint using a jump instead of a trap will
9204 likely have to overwrite more bytes than a trap would, and so can
9205 only be placed where the instruction is longer than the jump, or a
9206 multi-instruction sequence does not have a jump into the middle of
9207 it, etc. */
9208
9209static void
9210check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9211 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9212{
6c5b2ebe 9213 int rslt;
7a697b8d
SS
9214 char *msg;
9215 struct cleanup *old_chain;
9216
6c5b2ebe 9217 for (const auto &sal : sals)
7a697b8d 9218 {
f8eba3c6
TT
9219 struct gdbarch *sarch;
9220
6c5b2ebe 9221 sarch = get_sal_arch (sal);
f8eba3c6
TT
9222 /* We fall back to GDBARCH if there is no architecture
9223 associated with SAL. */
9224 if (sarch == NULL)
9225 sarch = gdbarch;
6c5b2ebe 9226 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg);
7a697b8d
SS
9227 old_chain = make_cleanup (xfree, msg);
9228
9229 if (!rslt)
53c3572a 9230 error (_("May not have a fast tracepoint at %s%s"),
6c5b2ebe 9231 paddress (sarch, sal.pc), (msg ? msg : ""));
7a697b8d
SS
9232
9233 do_cleanups (old_chain);
9234 }
9235}
9236
018d34a4
VP
9237/* Given TOK, a string specification of condition and thread, as
9238 accepted by the 'break' command, extract the condition
9239 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9240 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9241 If no condition is found, *COND_STRING is set to NULL.
9242 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9243
9244static void
bbc13ae3 9245find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9246 char **cond_string, int *thread, int *task,
9247 char **rest)
018d34a4
VP
9248{
9249 *cond_string = NULL;
9250 *thread = -1;
ed1d1739
KS
9251 *task = 0;
9252 *rest = NULL;
9253
018d34a4
VP
9254 while (tok && *tok)
9255 {
bbc13ae3 9256 const char *end_tok;
018d34a4 9257 int toklen;
bbc13ae3
KS
9258 const char *cond_start = NULL;
9259 const char *cond_end = NULL;
cc59ec59 9260
f1735a53 9261 tok = skip_spaces (tok);
e7e0cddf
SS
9262
9263 if ((*tok == '"' || *tok == ',') && rest)
9264 {
9265 *rest = savestring (tok, strlen (tok));
9266 return;
9267 }
9268
f1735a53 9269 end_tok = skip_to_space (tok);
d634f2de 9270
018d34a4 9271 toklen = end_tok - tok;
d634f2de 9272
018d34a4
VP
9273 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9274 {
9275 tok = cond_start = end_tok + 1;
4d01a485 9276 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9277 cond_end = tok;
d634f2de 9278 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9279 }
9280 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9281 {
5d5658a1
PA
9282 const char *tmptok;
9283 struct thread_info *thr;
d634f2de 9284
018d34a4 9285 tok = end_tok + 1;
5d5658a1 9286 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9287 if (tok == tmptok)
9288 error (_("Junk after thread keyword."));
5d5658a1 9289 *thread = thr->global_num;
bbc13ae3 9290 tok = tmptok;
018d34a4 9291 }
4a306c9a
JB
9292 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9293 {
9294 char *tmptok;
9295
9296 tok = end_tok + 1;
bbc13ae3 9297 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9298 if (tok == tmptok)
9299 error (_("Junk after task keyword."));
9300 if (!valid_task_id (*task))
b6199126 9301 error (_("Unknown task %d."), *task);
bbc13ae3 9302 tok = tmptok;
4a306c9a 9303 }
e7e0cddf
SS
9304 else if (rest)
9305 {
9306 *rest = savestring (tok, strlen (tok));
ccab2054 9307 return;
e7e0cddf 9308 }
018d34a4
VP
9309 else
9310 error (_("Junk at end of arguments."));
9311 }
9312}
9313
0fb4aa4b
PA
9314/* Decode a static tracepoint marker spec. */
9315
6c5b2ebe 9316static std::vector<symtab_and_line>
f00aae0f 9317decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9318{
9319 VEC(static_tracepoint_marker_p) *markers = NULL;
f00aae0f
KS
9320 const char *p = &(*arg_p)[3];
9321 const char *endp;
0fb4aa4b
PA
9322 int i;
9323
f1735a53 9324 p = skip_spaces (p);
0fb4aa4b 9325
f1735a53 9326 endp = skip_to_space (p);
0fb4aa4b 9327
81b1e71c 9328 std::string marker_str (p, endp - p);
0fb4aa4b 9329
81b1e71c 9330 markers = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
0fb4aa4b 9331 if (VEC_empty(static_tracepoint_marker_p, markers))
81b1e71c
TT
9332 error (_("No known static tracepoint marker named %s"),
9333 marker_str.c_str ());
0fb4aa4b 9334
6c5b2ebe
PA
9335 std::vector<symtab_and_line> sals;
9336 sals.reserve (VEC_length(static_tracepoint_marker_p, markers));
0fb4aa4b 9337
6c5b2ebe 9338 for (i = 0; i < VEC_length(static_tracepoint_marker_p, markers); i++)
0fb4aa4b
PA
9339 {
9340 struct static_tracepoint_marker *marker;
9341
9342 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9343
51abb421 9344 symtab_and_line sal = find_pc_line (marker->address, 0);
6c5b2ebe
PA
9345 sal.pc = marker->address;
9346 sals.push_back (sal);
0fb4aa4b
PA
9347
9348 release_static_tracepoint_marker (marker);
9349 }
9350
0fb4aa4b
PA
9351 *arg_p = endp;
9352 return sals;
9353}
9354
f00aae0f 9355/* See breakpoint.h. */
0101ce28 9356
8cdf0e15
VP
9357int
9358create_breakpoint (struct gdbarch *gdbarch,
e1e01040
PA
9359 const struct event_location *location,
9360 const char *cond_string,
9361 int thread, const char *extra_string,
f00aae0f 9362 int parse_extra,
0fb4aa4b 9363 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9364 int ignore_count,
9365 enum auto_boolean pending_break_support,
c0a91b2b 9366 const struct breakpoint_ops *ops,
44f238bb
PA
9367 int from_tty, int enabled, int internal,
9368 unsigned flags)
c3f6f71d 9369{
7efd8fc2 9370 struct linespec_result canonical;
80c99de1 9371 struct cleanup *bkpt_chain = NULL;
0101ce28 9372 int pending = 0;
4a306c9a 9373 int task = 0;
86b17b60 9374 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9375
348d480f
PA
9376 gdb_assert (ops != NULL);
9377
f00aae0f
KS
9378 /* If extra_string isn't useful, set it to NULL. */
9379 if (extra_string != NULL && *extra_string == '\0')
9380 extra_string = NULL;
9381
492d29ea 9382 TRY
b78a6381 9383 {
f00aae0f 9384 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9385 }
492d29ea 9386 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9387 {
492d29ea
PA
9388 /* If caller is interested in rc value from parse, set
9389 value. */
9390 if (e.error == NOT_FOUND_ERROR)
0101ce28 9391 {
05ff989b
AC
9392 /* If pending breakpoint support is turned off, throw
9393 error. */
fa8d40ab
JJ
9394
9395 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9396 throw_exception (e);
9397
9398 exception_print (gdb_stderr, e);
fa8d40ab 9399
05ff989b
AC
9400 /* If pending breakpoint support is auto query and the user
9401 selects no, then simply return the error code. */
059fb39f 9402 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9403 && !nquery (_("Make %s pending on future shared library load? "),
9404 bptype_string (type_wanted)))
fd9b8c24 9405 return 0;
fa8d40ab 9406
05ff989b
AC
9407 /* At this point, either the user was queried about setting
9408 a pending breakpoint and selected yes, or pending
9409 breakpoint behavior is on and thus a pending breakpoint
9410 is defaulted on behalf of the user. */
f00aae0f 9411 pending = 1;
0101ce28 9412 }
492d29ea
PA
9413 else
9414 throw_exception (e);
0101ce28 9415 }
492d29ea
PA
9416 END_CATCH
9417
6c5b2ebe 9418 if (!pending && canonical.lsals.empty ())
492d29ea 9419 return 0;
c3f6f71d 9420
c3f6f71d
JM
9421 /* ----------------------------- SNIP -----------------------------
9422 Anything added to the cleanup chain beyond this point is assumed
9423 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9424 then the memory is not reclaimed. */
9425 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9426
c3f6f71d
JM
9427 /* Resolve all line numbers to PC's and verify that the addresses
9428 are ok for the target. */
0101ce28 9429 if (!pending)
f8eba3c6 9430 {
6c5b2ebe
PA
9431 for (auto &lsal : canonical.lsals)
9432 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9433 }
c3f6f71d 9434
7a697b8d 9435 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9436 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9437 {
6c5b2ebe
PA
9438 for (const auto &lsal : canonical.lsals)
9439 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9440 }
7a697b8d 9441
c3f6f71d
JM
9442 /* Verify that condition can be parsed, before setting any
9443 breakpoints. Allocate a separate condition expression for each
4a64f543 9444 breakpoint. */
0101ce28 9445 if (!pending)
c3f6f71d 9446 {
e1e01040
PA
9447 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9448 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9449
f00aae0f 9450 if (parse_extra)
72b2ff0e 9451 {
0878d0fa 9452 char *rest;
e1e01040 9453 char *cond;
52d361e1 9454
6c5b2ebe 9455 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9456
0878d0fa
YQ
9457 /* Here we only parse 'arg' to separate condition
9458 from thread number, so parsing in context of first
9459 sal is OK. When setting the breakpoint we'll
9460 re-parse it in context of each sal. */
9461
6c5b2ebe 9462 find_condition_and_thread (extra_string, lsal.sals[0].pc,
e1e01040
PA
9463 &cond, &thread, &task, &rest);
9464 cond_string_copy.reset (cond);
9465 extra_string_copy.reset (rest);
72b2ff0e 9466 }
2f069f6f 9467 else
72b2ff0e 9468 {
f00aae0f
KS
9469 if (type_wanted != bp_dprintf
9470 && extra_string != NULL && *extra_string != '\0')
9471 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9472
9473 /* Create a private copy of condition string. */
9474 if (cond_string)
e1e01040 9475 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9476 /* Create a private copy of any extra string. */
9477 if (extra_string)
e1e01040 9478 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9479 }
0fb4aa4b 9480
52d361e1 9481 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9482 std::move (cond_string_copy),
9483 std::move (extra_string_copy),
9484 type_wanted,
d9b3f62e
PA
9485 tempflag ? disp_del : disp_donttouch,
9486 thread, task, ignore_count, ops,
44f238bb 9487 from_tty, enabled, internal, flags);
c906108c 9488 }
0101ce28
JJ
9489 else
9490 {
a5e364af 9491 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9492
a5e364af 9493 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9494 b->location = copy_event_location (location);
bfccc43c 9495
f00aae0f
KS
9496 if (parse_extra)
9497 b->cond_string = NULL;
e12c7713
MK
9498 else
9499 {
9500 /* Create a private copy of condition string. */
e1e01040 9501 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9502 b->thread = thread;
e12c7713 9503 }
f00aae0f
KS
9504
9505 /* Create a private copy of any extra string. */
e1e01040 9506 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9507 b->ignore_count = ignore_count;
0101ce28 9508 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9509 b->condition_not_parsed = 1;
41447f92 9510 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9511 if ((type_wanted != bp_breakpoint
9512 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9513 b->pspace = current_program_space;
8bea4e01 9514
b270e6f9 9515 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9516 }
9517
6c5b2ebe 9518 if (canonical.lsals.size () > 1)
95a42b64 9519 {
3e43a32a
MS
9520 warning (_("Multiple breakpoints were set.\nUse the "
9521 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9522 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9523 }
9524
80c99de1
PA
9525 /* That's it. Discard the cleanups for data inserted into the
9526 breakpoint. */
9527 discard_cleanups (bkpt_chain);
217dc9e2 9528
80c99de1 9529 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9530 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9531
9532 return 1;
c3f6f71d 9533}
c906108c 9534
348d480f 9535/* Set a breakpoint.
72b2ff0e
VP
9536 ARG is a string describing breakpoint address,
9537 condition, and thread.
9538 FLAG specifies if a breakpoint is hardware on,
9539 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9540 and BP_TEMPFLAG. */
348d480f 9541
98deb0da 9542static void
f2fc3015 9543break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9544{
72b2ff0e 9545 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9546 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9547 ? bp_hardware_breakpoint
9548 : bp_breakpoint);
55aa24fb 9549 struct breakpoint_ops *ops;
f00aae0f 9550
ffc2605c 9551 event_location_up location = string_to_event_location (&arg, current_language);
55aa24fb
SDJ
9552
9553 /* Matching breakpoints on probes. */
5b56227b 9554 if (location != NULL
ffc2605c 9555 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
9556 ops = &bkpt_probe_breakpoint_ops;
9557 else
9558 ops = &bkpt_breakpoint_ops;
c3f6f71d 9559
8cdf0e15 9560 create_breakpoint (get_current_arch (),
ffc2605c 9561 location.get (),
f00aae0f 9562 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9563 tempflag, type_wanted,
8cdf0e15
VP
9564 0 /* Ignore count */,
9565 pending_break_support,
55aa24fb 9566 ops,
8cdf0e15 9567 from_tty,
84f4c1fe 9568 1 /* enabled */,
44f238bb
PA
9569 0 /* internal */,
9570 0);
c906108c
SS
9571}
9572
c906108c
SS
9573/* Helper function for break_command_1 and disassemble_command. */
9574
9575void
fba45db2 9576resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9577{
9578 CORE_ADDR pc;
9579
9580 if (sal->pc == 0 && sal->symtab != NULL)
9581 {
9582 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9583 error (_("No line %d in file \"%s\"."),
05cba821 9584 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9585 sal->pc = pc;
6a048695 9586
4a64f543
MS
9587 /* If this SAL corresponds to a breakpoint inserted using a line
9588 number, then skip the function prologue if necessary. */
6a048695 9589 if (sal->explicit_line)
059acae7 9590 skip_prologue_sal (sal);
c906108c
SS
9591 }
9592
9593 if (sal->section == 0 && sal->symtab != NULL)
9594 {
346d1dfe 9595 const struct blockvector *bv;
3977b71f 9596 const struct block *b;
c5aa993b 9597 struct symbol *sym;
c906108c 9598
43f3e411
DE
9599 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9600 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9601 if (bv != NULL)
9602 {
7f0df278 9603 sym = block_linkage_function (b);
c906108c
SS
9604 if (sym != NULL)
9605 {
eb822aa6
DE
9606 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9607 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9608 sym);
c906108c
SS
9609 }
9610 else
9611 {
4a64f543
MS
9612 /* It really is worthwhile to have the section, so we'll
9613 just have to look harder. This case can be executed
9614 if we have line numbers but no functions (as can
9615 happen in assembly source). */
c906108c 9616
5ed8105e 9617 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9618 switch_to_program_space_and_thread (sal->pspace);
c906108c 9619
5ed8105e 9620 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9621 if (msym.minsym)
efd66ac6 9622 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9623 }
9624 }
9625 }
9626}
9627
9628void
0b39b52e 9629break_command (const char *arg, int from_tty)
c906108c 9630{
db107f19 9631 break_command_1 (arg, 0, from_tty);
c906108c
SS
9632}
9633
c906108c 9634void
0b39b52e 9635tbreak_command (const char *arg, int from_tty)
c906108c 9636{
db107f19 9637 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9638}
9639
c906108c 9640static void
0b39b52e 9641hbreak_command (const char *arg, int from_tty)
c906108c 9642{
db107f19 9643 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9644}
9645
9646static void
0b39b52e 9647thbreak_command (const char *arg, int from_tty)
c906108c 9648{
db107f19 9649 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9650}
9651
9652static void
ee7ddd71 9653stop_command (const char *arg, int from_tty)
c906108c 9654{
a3f17187 9655 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9656Usage: stop in <function | address>\n\
a3f17187 9657 stop at <line>\n"));
c906108c
SS
9658}
9659
9660static void
4495129a 9661stopin_command (const char *arg, int from_tty)
c906108c
SS
9662{
9663 int badInput = 0;
9664
c5aa993b 9665 if (arg == (char *) NULL)
c906108c
SS
9666 badInput = 1;
9667 else if (*arg != '*')
9668 {
4495129a 9669 const char *argptr = arg;
c906108c
SS
9670 int hasColon = 0;
9671
4a64f543 9672 /* Look for a ':'. If this is a line number specification, then
53a5351d 9673 say it is bad, otherwise, it should be an address or
4a64f543 9674 function/method name. */
c906108c 9675 while (*argptr && !hasColon)
c5aa993b
JM
9676 {
9677 hasColon = (*argptr == ':');
9678 argptr++;
9679 }
c906108c
SS
9680
9681 if (hasColon)
c5aa993b 9682 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9683 else
c5aa993b 9684 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9685 }
9686
9687 if (badInput)
a3f17187 9688 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9689 else
db107f19 9690 break_command_1 (arg, 0, from_tty);
c906108c
SS
9691}
9692
9693static void
4495129a 9694stopat_command (const char *arg, int from_tty)
c906108c
SS
9695{
9696 int badInput = 0;
9697
c5aa993b 9698 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9699 badInput = 1;
9700 else
9701 {
4495129a 9702 const char *argptr = arg;
c906108c
SS
9703 int hasColon = 0;
9704
4a64f543
MS
9705 /* Look for a ':'. If there is a '::' then get out, otherwise
9706 it is probably a line number. */
c906108c 9707 while (*argptr && !hasColon)
c5aa993b
JM
9708 {
9709 hasColon = (*argptr == ':');
9710 argptr++;
9711 }
c906108c
SS
9712
9713 if (hasColon)
c5aa993b 9714 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9715 else
c5aa993b 9716 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9717 }
9718
9719 if (badInput)
a3f17187 9720 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 9721 else
db107f19 9722 break_command_1 (arg, 0, from_tty);
c906108c
SS
9723}
9724
e7e0cddf
SS
9725/* The dynamic printf command is mostly like a regular breakpoint, but
9726 with a prewired command list consisting of a single output command,
9727 built from extra arguments supplied on the dprintf command
9728 line. */
9729
da821c7b 9730static void
0b39b52e 9731dprintf_command (const char *arg, int from_tty)
e7e0cddf 9732{
ffc2605c 9733 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9734
9735 /* If non-NULL, ARG should have been advanced past the location;
9736 the next character must be ','. */
9737 if (arg != NULL)
9738 {
9739 if (arg[0] != ',' || arg[1] == '\0')
9740 error (_("Format string required"));
9741 else
9742 {
9743 /* Skip the comma. */
9744 ++arg;
9745 }
9746 }
9747
e7e0cddf 9748 create_breakpoint (get_current_arch (),
ffc2605c 9749 location.get (),
f00aae0f 9750 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
9751 0, bp_dprintf,
9752 0 /* Ignore count */,
9753 pending_break_support,
9754 &dprintf_breakpoint_ops,
9755 from_tty,
9756 1 /* enabled */,
9757 0 /* internal */,
9758 0);
9759}
9760
d3ce09f5 9761static void
0b39b52e 9762agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9763{
9764 error (_("May only run agent-printf on the target"));
9765}
9766
f1310107
TJB
9767/* Implement the "breakpoint_hit" breakpoint_ops method for
9768 ranged breakpoints. */
9769
9770static int
9771breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9772 const address_space *aspace,
09ac7c10
TT
9773 CORE_ADDR bp_addr,
9774 const struct target_waitstatus *ws)
f1310107 9775{
09ac7c10 9776 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9777 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9778 return 0;
9779
f1310107
TJB
9780 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9781 bl->length, aspace, bp_addr);
9782}
9783
9784/* Implement the "resources_needed" breakpoint_ops method for
9785 ranged breakpoints. */
9786
9787static int
9788resources_needed_ranged_breakpoint (const struct bp_location *bl)
9789{
9790 return target_ranged_break_num_registers ();
9791}
9792
9793/* Implement the "print_it" breakpoint_ops method for
9794 ranged breakpoints. */
9795
9796static enum print_stop_action
348d480f 9797print_it_ranged_breakpoint (bpstat bs)
f1310107 9798{
348d480f 9799 struct breakpoint *b = bs->breakpoint_at;
f1310107 9800 struct bp_location *bl = b->loc;
79a45e25 9801 struct ui_out *uiout = current_uiout;
f1310107
TJB
9802
9803 gdb_assert (b->type == bp_hardware_breakpoint);
9804
9805 /* Ranged breakpoints have only one location. */
9806 gdb_assert (bl && bl->next == NULL);
9807
9808 annotate_breakpoint (b->number);
f303dbd6
PA
9809
9810 maybe_print_thread_hit_breakpoint (uiout);
9811
f1310107 9812 if (b->disposition == disp_del)
112e8700 9813 uiout->text ("Temporary ranged breakpoint ");
f1310107 9814 else
112e8700
SM
9815 uiout->text ("Ranged breakpoint ");
9816 if (uiout->is_mi_like_p ())
f1310107 9817 {
112e8700 9818 uiout->field_string ("reason",
f1310107 9819 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9820 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9821 }
112e8700
SM
9822 uiout->field_int ("bkptno", b->number);
9823 uiout->text (", ");
f1310107
TJB
9824
9825 return PRINT_SRC_AND_LOC;
9826}
9827
9828/* Implement the "print_one" breakpoint_ops method for
9829 ranged breakpoints. */
9830
9831static void
9832print_one_ranged_breakpoint (struct breakpoint *b,
9833 struct bp_location **last_loc)
9834{
9835 struct bp_location *bl = b->loc;
9836 struct value_print_options opts;
79a45e25 9837 struct ui_out *uiout = current_uiout;
f1310107
TJB
9838
9839 /* Ranged breakpoints have only one location. */
9840 gdb_assert (bl && bl->next == NULL);
9841
9842 get_user_print_options (&opts);
9843
9844 if (opts.addressprint)
9845 /* We don't print the address range here, it will be printed later
9846 by print_one_detail_ranged_breakpoint. */
112e8700 9847 uiout->field_skip ("addr");
f1310107
TJB
9848 annotate_field (5);
9849 print_breakpoint_location (b, bl);
9850 *last_loc = bl;
9851}
9852
9853/* Implement the "print_one_detail" breakpoint_ops method for
9854 ranged breakpoints. */
9855
9856static void
9857print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9858 struct ui_out *uiout)
9859{
9860 CORE_ADDR address_start, address_end;
9861 struct bp_location *bl = b->loc;
d7e74731 9862 string_file stb;
f1310107
TJB
9863
9864 gdb_assert (bl);
9865
9866 address_start = bl->address;
9867 address_end = address_start + bl->length - 1;
9868
112e8700 9869 uiout->text ("\taddress range: ");
d7e74731
PA
9870 stb.printf ("[%s, %s]",
9871 print_core_address (bl->gdbarch, address_start),
9872 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9873 uiout->field_stream ("addr", stb);
9874 uiout->text ("\n");
f1310107
TJB
9875}
9876
9877/* Implement the "print_mention" breakpoint_ops method for
9878 ranged breakpoints. */
9879
9880static void
9881print_mention_ranged_breakpoint (struct breakpoint *b)
9882{
9883 struct bp_location *bl = b->loc;
79a45e25 9884 struct ui_out *uiout = current_uiout;
f1310107
TJB
9885
9886 gdb_assert (bl);
9887 gdb_assert (b->type == bp_hardware_breakpoint);
9888
112e8700 9889 if (uiout->is_mi_like_p ())
f1310107
TJB
9890 return;
9891
9892 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9893 b->number, paddress (bl->gdbarch, bl->address),
9894 paddress (bl->gdbarch, bl->address + bl->length - 1));
9895}
9896
9897/* Implement the "print_recreate" breakpoint_ops method for
9898 ranged breakpoints. */
9899
9900static void
9901print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9902{
f00aae0f 9903 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
9904 event_location_to_string (b->location.get ()),
9905 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 9906 print_recreate_thread (b, fp);
f1310107
TJB
9907}
9908
9909/* The breakpoint_ops structure to be used in ranged breakpoints. */
9910
2060206e 9911static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9912
9913/* Find the address where the end of the breakpoint range should be
9914 placed, given the SAL of the end of the range. This is so that if
9915 the user provides a line number, the end of the range is set to the
9916 last instruction of the given line. */
9917
9918static CORE_ADDR
9919find_breakpoint_range_end (struct symtab_and_line sal)
9920{
9921 CORE_ADDR end;
9922
9923 /* If the user provided a PC value, use it. Otherwise,
9924 find the address of the end of the given location. */
9925 if (sal.explicit_pc)
9926 end = sal.pc;
9927 else
9928 {
9929 int ret;
9930 CORE_ADDR start;
9931
9932 ret = find_line_pc_range (sal, &start, &end);
9933 if (!ret)
9934 error (_("Could not find location of the end of the range."));
9935
9936 /* find_line_pc_range returns the start of the next line. */
9937 end--;
9938 }
9939
9940 return end;
9941}
9942
9943/* Implement the "break-range" CLI command. */
9944
9945static void
0b39b52e 9946break_range_command (const char *arg, int from_tty)
f1310107 9947{
f2fc3015 9948 const char *arg_start;
f1310107
TJB
9949 struct linespec_result canonical_start, canonical_end;
9950 int bp_count, can_use_bp, length;
9951 CORE_ADDR end;
9952 struct breakpoint *b;
f1310107
TJB
9953
9954 /* We don't support software ranged breakpoints. */
9955 if (target_ranged_break_num_registers () < 0)
9956 error (_("This target does not support hardware ranged breakpoints."));
9957
9958 bp_count = hw_breakpoint_used_count ();
9959 bp_count += target_ranged_break_num_registers ();
9960 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9961 bp_count, 0);
9962 if (can_use_bp < 0)
9963 error (_("Hardware breakpoints used exceeds limit."));
9964
f8eba3c6 9965 arg = skip_spaces (arg);
f1310107
TJB
9966 if (arg == NULL || arg[0] == '\0')
9967 error(_("No address range specified."));
9968
f8eba3c6 9969 arg_start = arg;
ffc2605c
TT
9970 event_location_up start_location = string_to_event_location (&arg,
9971 current_language);
9972 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
9973
9974 if (arg[0] != ',')
9975 error (_("Too few arguments."));
6c5b2ebe 9976 else if (canonical_start.lsals.empty ())
f1310107 9977 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9978
6c5b2ebe 9979 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9980
6c5b2ebe
PA
9981 if (canonical_start.lsals.size () > 1
9982 || lsal_start.sals.size () != 1)
f1310107
TJB
9983 error (_("Cannot create a ranged breakpoint with multiple locations."));
9984
6c5b2ebe 9985 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9986 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9987
9988 arg++; /* Skip the comma. */
f8eba3c6 9989 arg = skip_spaces (arg);
f1310107
TJB
9990
9991 /* Parse the end location. */
9992
f1310107
TJB
9993 arg_start = arg;
9994
f8eba3c6 9995 /* We call decode_line_full directly here instead of using
f1310107
TJB
9996 parse_breakpoint_sals because we need to specify the start location's
9997 symtab and line as the default symtab and line for the end of the
9998 range. This makes it possible to have ranges like "foo.c:27, +14",
9999 where +14 means 14 lines from the start location. */
ffc2605c
TT
10000 event_location_up end_location = string_to_event_location (&arg,
10001 current_language);
10002 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
10003 sal_start.symtab, sal_start.line,
10004 &canonical_end, NULL, NULL);
10005
6c5b2ebe 10006 if (canonical_end.lsals.empty ())
f1310107 10007 error (_("Could not find location of the end of the range."));
f8eba3c6 10008
6c5b2ebe
PA
10009 const linespec_sals &lsal_end = canonical_end.lsals[0];
10010 if (canonical_end.lsals.size () > 1
10011 || lsal_end.sals.size () != 1)
f1310107
TJB
10012 error (_("Cannot create a ranged breakpoint with multiple locations."));
10013
6c5b2ebe 10014 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
10015
10016 end = find_breakpoint_range_end (sal_end);
10017 if (sal_start.pc > end)
177b42fe 10018 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10019
10020 length = end - sal_start.pc + 1;
10021 if (length < 0)
10022 /* Length overflowed. */
10023 error (_("Address range too large."));
10024 else if (length == 1)
10025 {
10026 /* This range is simple enough to be handled by
10027 the `hbreak' command. */
81b1e71c 10028 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
10029
10030 return;
10031 }
10032
10033 /* Now set up the breakpoint. */
10034 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10035 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10036 set_breakpoint_count (breakpoint_count + 1);
10037 b->number = breakpoint_count;
10038 b->disposition = disp_donttouch;
d28cd78a
TT
10039 b->location = std::move (start_location);
10040 b->location_range_end = std::move (end_location);
f1310107
TJB
10041 b->loc->length = length;
10042
f1310107 10043 mention (b);
8d3788bd 10044 observer_notify_breakpoint_created (b);
44702360 10045 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10046}
10047
4a64f543
MS
10048/* Return non-zero if EXP is verified as constant. Returned zero
10049 means EXP is variable. Also the constant detection may fail for
10050 some constant expressions and in such case still falsely return
10051 zero. */
2e6e3d9c 10052
65d79d4b
SDJ
10053static int
10054watchpoint_exp_is_const (const struct expression *exp)
10055{
10056 int i = exp->nelts;
10057
10058 while (i > 0)
10059 {
10060 int oplenp, argsp;
10061
10062 /* We are only interested in the descriptor of each element. */
10063 operator_length (exp, i, &oplenp, &argsp);
10064 i -= oplenp;
10065
10066 switch (exp->elts[i].opcode)
10067 {
10068 case BINOP_ADD:
10069 case BINOP_SUB:
10070 case BINOP_MUL:
10071 case BINOP_DIV:
10072 case BINOP_REM:
10073 case BINOP_MOD:
10074 case BINOP_LSH:
10075 case BINOP_RSH:
10076 case BINOP_LOGICAL_AND:
10077 case BINOP_LOGICAL_OR:
10078 case BINOP_BITWISE_AND:
10079 case BINOP_BITWISE_IOR:
10080 case BINOP_BITWISE_XOR:
10081 case BINOP_EQUAL:
10082 case BINOP_NOTEQUAL:
10083 case BINOP_LESS:
10084 case BINOP_GTR:
10085 case BINOP_LEQ:
10086 case BINOP_GEQ:
10087 case BINOP_REPEAT:
10088 case BINOP_COMMA:
10089 case BINOP_EXP:
10090 case BINOP_MIN:
10091 case BINOP_MAX:
10092 case BINOP_INTDIV:
10093 case BINOP_CONCAT:
65d79d4b
SDJ
10094 case TERNOP_COND:
10095 case TERNOP_SLICE:
65d79d4b
SDJ
10096
10097 case OP_LONG:
edd079d9 10098 case OP_FLOAT:
65d79d4b
SDJ
10099 case OP_LAST:
10100 case OP_COMPLEX:
10101 case OP_STRING:
65d79d4b
SDJ
10102 case OP_ARRAY:
10103 case OP_TYPE:
608b4967
TT
10104 case OP_TYPEOF:
10105 case OP_DECLTYPE:
6e72ca20 10106 case OP_TYPEID:
65d79d4b
SDJ
10107 case OP_NAME:
10108 case OP_OBJC_NSSTRING:
10109
10110 case UNOP_NEG:
10111 case UNOP_LOGICAL_NOT:
10112 case UNOP_COMPLEMENT:
10113 case UNOP_ADDR:
10114 case UNOP_HIGH:
aeaa2474 10115 case UNOP_CAST:
9eaf6705
TT
10116
10117 case UNOP_CAST_TYPE:
10118 case UNOP_REINTERPRET_CAST:
10119 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10120 /* Unary, binary and ternary operators: We have to check
10121 their operands. If they are constant, then so is the
10122 result of that operation. For instance, if A and B are
10123 determined to be constants, then so is "A + B".
10124
10125 UNOP_IND is one exception to the rule above, because the
10126 value of *ADDR is not necessarily a constant, even when
10127 ADDR is. */
65d79d4b
SDJ
10128 break;
10129
10130 case OP_VAR_VALUE:
10131 /* Check whether the associated symbol is a constant.
4a64f543 10132
65d79d4b 10133 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10134 possible that a buggy compiler could mark a variable as
10135 constant even when it is not, and TYPE_CONST would return
10136 true in this case, while SYMBOL_CLASS wouldn't.
10137
10138 We also have to check for function symbols because they
10139 are always constant. */
65d79d4b
SDJ
10140 {
10141 struct symbol *s = exp->elts[i + 2].symbol;
10142
10143 if (SYMBOL_CLASS (s) != LOC_BLOCK
10144 && SYMBOL_CLASS (s) != LOC_CONST
10145 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10146 return 0;
10147 break;
10148 }
10149
10150 /* The default action is to return 0 because we are using
10151 the optimistic approach here: If we don't know something,
10152 then it is not a constant. */
10153 default:
10154 return 0;
10155 }
10156 }
10157
10158 return 1;
10159}
10160
c1fc2657 10161/* Watchpoint destructor. */
3a5c3e22 10162
c1fc2657 10163watchpoint::~watchpoint ()
3a5c3e22 10164{
c1fc2657
SM
10165 xfree (this->exp_string);
10166 xfree (this->exp_string_reparse);
10167 value_free (this->val);
3a5c3e22
PA
10168}
10169
348d480f
PA
10170/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10171
10172static void
10173re_set_watchpoint (struct breakpoint *b)
10174{
3a5c3e22
PA
10175 struct watchpoint *w = (struct watchpoint *) b;
10176
348d480f
PA
10177 /* Watchpoint can be either on expression using entirely global
10178 variables, or it can be on local variables.
10179
10180 Watchpoints of the first kind are never auto-deleted, and even
10181 persist across program restarts. Since they can use variables
10182 from shared libraries, we need to reparse expression as libraries
10183 are loaded and unloaded.
10184
10185 Watchpoints on local variables can also change meaning as result
10186 of solib event. For example, if a watchpoint uses both a local
10187 and a global variables in expression, it's a local watchpoint,
10188 but unloading of a shared library will make the expression
10189 invalid. This is not a very common use case, but we still
10190 re-evaluate expression, to avoid surprises to the user.
10191
10192 Note that for local watchpoints, we re-evaluate it only if
10193 watchpoints frame id is still valid. If it's not, it means the
10194 watchpoint is out of scope and will be deleted soon. In fact,
10195 I'm not sure we'll ever be called in this case.
10196
10197 If a local watchpoint's frame id is still valid, then
3a5c3e22 10198 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10199
3a5c3e22
PA
10200 Don't do anything about disabled watchpoints, since they will be
10201 reevaluated again when enabled. */
10202 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10203}
10204
77b06cd7
TJB
10205/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10206
10207static int
10208insert_watchpoint (struct bp_location *bl)
10209{
3a5c3e22
PA
10210 struct watchpoint *w = (struct watchpoint *) bl->owner;
10211 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10212
10213 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10214 w->cond_exp.get ());
77b06cd7
TJB
10215}
10216
10217/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10218
10219static int
73971819 10220remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10221{
3a5c3e22
PA
10222 struct watchpoint *w = (struct watchpoint *) bl->owner;
10223 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10224
10225 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10226 w->cond_exp.get ());
e09342b5
TJB
10227}
10228
e09342b5 10229static int
348d480f 10230breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10231 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10232 const struct target_waitstatus *ws)
e09342b5 10233{
348d480f 10234 struct breakpoint *b = bl->owner;
3a5c3e22 10235 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10236
348d480f
PA
10237 /* Continuable hardware watchpoints are treated as non-existent if the
10238 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10239 some data address). Otherwise gdb won't stop on a break instruction
10240 in the code (not from a breakpoint) when a hardware watchpoint has
10241 been defined. Also skip watchpoints which we know did not trigger
10242 (did not match the data address). */
10243 if (is_hardware_watchpoint (b)
3a5c3e22 10244 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10245 return 0;
9c06b0b4 10246
348d480f 10247 return 1;
9c06b0b4
TJB
10248}
10249
348d480f
PA
10250static void
10251check_status_watchpoint (bpstat bs)
9c06b0b4 10252{
348d480f 10253 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10254
348d480f 10255 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10256}
10257
10258/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10259 hardware watchpoints. */
9c06b0b4
TJB
10260
10261static int
348d480f 10262resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10263{
3a5c3e22
PA
10264 struct watchpoint *w = (struct watchpoint *) bl->owner;
10265 int length = w->exact? 1 : bl->length;
348d480f
PA
10266
10267 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10268}
10269
10270/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10271 hardware watchpoints. */
9c06b0b4
TJB
10272
10273static int
348d480f 10274works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10275{
efa80663
PA
10276 /* Read and access watchpoints only work with hardware support. */
10277 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10278}
10279
9c06b0b4 10280static enum print_stop_action
348d480f 10281print_it_watchpoint (bpstat bs)
9c06b0b4 10282{
348d480f 10283 struct breakpoint *b;
348d480f 10284 enum print_stop_action result;
3a5c3e22 10285 struct watchpoint *w;
79a45e25 10286 struct ui_out *uiout = current_uiout;
348d480f
PA
10287
10288 gdb_assert (bs->bp_location_at != NULL);
10289
348d480f 10290 b = bs->breakpoint_at;
3a5c3e22 10291 w = (struct watchpoint *) b;
348d480f 10292
f303dbd6
PA
10293 annotate_watchpoint (b->number);
10294 maybe_print_thread_hit_breakpoint (uiout);
10295
d7e74731
PA
10296 string_file stb;
10297
76f9c9cf 10298 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10299 switch (b->type)
10300 {
348d480f 10301 case bp_watchpoint:
9c06b0b4 10302 case bp_hardware_watchpoint:
112e8700
SM
10303 if (uiout->is_mi_like_p ())
10304 uiout->field_string
10305 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10306 mention (b);
76f9c9cf 10307 tuple_emitter.emplace (uiout, "value");
112e8700 10308 uiout->text ("\nOld value = ");
d7e74731 10309 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10310 uiout->field_stream ("old", stb);
10311 uiout->text ("\nNew value = ");
d7e74731 10312 watchpoint_value_print (w->val, &stb);
112e8700
SM
10313 uiout->field_stream ("new", stb);
10314 uiout->text ("\n");
348d480f
PA
10315 /* More than one watchpoint may have been triggered. */
10316 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10317 break;
10318
10319 case bp_read_watchpoint:
112e8700
SM
10320 if (uiout->is_mi_like_p ())
10321 uiout->field_string
10322 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10323 mention (b);
76f9c9cf 10324 tuple_emitter.emplace (uiout, "value");
112e8700 10325 uiout->text ("\nValue = ");
d7e74731 10326 watchpoint_value_print (w->val, &stb);
112e8700
SM
10327 uiout->field_stream ("value", stb);
10328 uiout->text ("\n");
348d480f 10329 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10330 break;
10331
10332 case bp_access_watchpoint:
348d480f
PA
10333 if (bs->old_val != NULL)
10334 {
112e8700
SM
10335 if (uiout->is_mi_like_p ())
10336 uiout->field_string
10337 ("reason",
348d480f
PA
10338 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10339 mention (b);
76f9c9cf 10340 tuple_emitter.emplace (uiout, "value");
112e8700 10341 uiout->text ("\nOld value = ");
d7e74731 10342 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10343 uiout->field_stream ("old", stb);
10344 uiout->text ("\nNew value = ");
348d480f
PA
10345 }
10346 else
10347 {
10348 mention (b);
112e8700
SM
10349 if (uiout->is_mi_like_p ())
10350 uiout->field_string
10351 ("reason",
348d480f 10352 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10353 tuple_emitter.emplace (uiout, "value");
112e8700 10354 uiout->text ("\nValue = ");
348d480f 10355 }
d7e74731 10356 watchpoint_value_print (w->val, &stb);
112e8700
SM
10357 uiout->field_stream ("new", stb);
10358 uiout->text ("\n");
348d480f 10359 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10360 break;
10361 default:
348d480f 10362 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10363 }
10364
348d480f
PA
10365 return result;
10366}
10367
10368/* Implement the "print_mention" breakpoint_ops method for hardware
10369 watchpoints. */
10370
10371static void
10372print_mention_watchpoint (struct breakpoint *b)
10373{
3a5c3e22 10374 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10375 struct ui_out *uiout = current_uiout;
46b9c129 10376 const char *tuple_name;
348d480f
PA
10377
10378 switch (b->type)
10379 {
10380 case bp_watchpoint:
112e8700 10381 uiout->text ("Watchpoint ");
46b9c129 10382 tuple_name = "wpt";
348d480f
PA
10383 break;
10384 case bp_hardware_watchpoint:
112e8700 10385 uiout->text ("Hardware watchpoint ");
46b9c129 10386 tuple_name = "wpt";
348d480f
PA
10387 break;
10388 case bp_read_watchpoint:
112e8700 10389 uiout->text ("Hardware read watchpoint ");
46b9c129 10390 tuple_name = "hw-rwpt";
348d480f
PA
10391 break;
10392 case bp_access_watchpoint:
112e8700 10393 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10394 tuple_name = "hw-awpt";
348d480f
PA
10395 break;
10396 default:
10397 internal_error (__FILE__, __LINE__,
10398 _("Invalid hardware watchpoint type."));
10399 }
10400
46b9c129 10401 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10402 uiout->field_int ("number", b->number);
10403 uiout->text (": ");
10404 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10405}
10406
10407/* Implement the "print_recreate" breakpoint_ops method for
10408 watchpoints. */
10409
10410static void
10411print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10412{
3a5c3e22
PA
10413 struct watchpoint *w = (struct watchpoint *) b;
10414
348d480f
PA
10415 switch (b->type)
10416 {
10417 case bp_watchpoint:
10418 case bp_hardware_watchpoint:
10419 fprintf_unfiltered (fp, "watch");
10420 break;
10421 case bp_read_watchpoint:
10422 fprintf_unfiltered (fp, "rwatch");
10423 break;
10424 case bp_access_watchpoint:
10425 fprintf_unfiltered (fp, "awatch");
10426 break;
10427 default:
10428 internal_error (__FILE__, __LINE__,
10429 _("Invalid watchpoint type."));
10430 }
10431
3a5c3e22 10432 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10433 print_recreate_thread (b, fp);
348d480f
PA
10434}
10435
427cd150
TT
10436/* Implement the "explains_signal" breakpoint_ops method for
10437 watchpoints. */
10438
47591c29 10439static int
427cd150
TT
10440explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10441{
10442 /* A software watchpoint cannot cause a signal other than
10443 GDB_SIGNAL_TRAP. */
10444 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10445 return 0;
427cd150 10446
47591c29 10447 return 1;
427cd150
TT
10448}
10449
348d480f
PA
10450/* The breakpoint_ops structure to be used in hardware watchpoints. */
10451
2060206e 10452static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10453
10454/* Implement the "insert" breakpoint_ops method for
10455 masked hardware watchpoints. */
10456
10457static int
10458insert_masked_watchpoint (struct bp_location *bl)
10459{
3a5c3e22
PA
10460 struct watchpoint *w = (struct watchpoint *) bl->owner;
10461
10462 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10463 bl->watchpoint_type);
10464}
10465
10466/* Implement the "remove" breakpoint_ops method for
10467 masked hardware watchpoints. */
10468
10469static int
73971819 10470remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10471{
3a5c3e22
PA
10472 struct watchpoint *w = (struct watchpoint *) bl->owner;
10473
10474 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10475 bl->watchpoint_type);
10476}
10477
10478/* Implement the "resources_needed" breakpoint_ops method for
10479 masked hardware watchpoints. */
10480
10481static int
10482resources_needed_masked_watchpoint (const struct bp_location *bl)
10483{
3a5c3e22
PA
10484 struct watchpoint *w = (struct watchpoint *) bl->owner;
10485
10486 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10487}
10488
10489/* Implement the "works_in_software_mode" breakpoint_ops method for
10490 masked hardware watchpoints. */
10491
10492static int
10493works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10494{
10495 return 0;
10496}
10497
10498/* Implement the "print_it" breakpoint_ops method for
10499 masked hardware watchpoints. */
10500
10501static enum print_stop_action
10502print_it_masked_watchpoint (bpstat bs)
10503{
10504 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10505 struct ui_out *uiout = current_uiout;
348d480f
PA
10506
10507 /* Masked watchpoints have only one location. */
10508 gdb_assert (b->loc && b->loc->next == NULL);
10509
f303dbd6
PA
10510 annotate_watchpoint (b->number);
10511 maybe_print_thread_hit_breakpoint (uiout);
10512
348d480f
PA
10513 switch (b->type)
10514 {
10515 case bp_hardware_watchpoint:
112e8700
SM
10516 if (uiout->is_mi_like_p ())
10517 uiout->field_string
10518 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10519 break;
10520
10521 case bp_read_watchpoint:
112e8700
SM
10522 if (uiout->is_mi_like_p ())
10523 uiout->field_string
10524 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10525 break;
10526
10527 case bp_access_watchpoint:
112e8700
SM
10528 if (uiout->is_mi_like_p ())
10529 uiout->field_string
10530 ("reason",
348d480f
PA
10531 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10532 break;
10533 default:
10534 internal_error (__FILE__, __LINE__,
10535 _("Invalid hardware watchpoint type."));
10536 }
10537
10538 mention (b);
112e8700 10539 uiout->text (_("\n\
9c06b0b4
TJB
10540Check the underlying instruction at PC for the memory\n\
10541address and value which triggered this watchpoint.\n"));
112e8700 10542 uiout->text ("\n");
9c06b0b4
TJB
10543
10544 /* More than one watchpoint may have been triggered. */
10545 return PRINT_UNKNOWN;
10546}
10547
10548/* Implement the "print_one_detail" breakpoint_ops method for
10549 masked hardware watchpoints. */
10550
10551static void
10552print_one_detail_masked_watchpoint (const struct breakpoint *b,
10553 struct ui_out *uiout)
10554{
3a5c3e22
PA
10555 struct watchpoint *w = (struct watchpoint *) b;
10556
9c06b0b4
TJB
10557 /* Masked watchpoints have only one location. */
10558 gdb_assert (b->loc && b->loc->next == NULL);
10559
112e8700
SM
10560 uiout->text ("\tmask ");
10561 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10562 uiout->text ("\n");
9c06b0b4
TJB
10563}
10564
10565/* Implement the "print_mention" breakpoint_ops method for
10566 masked hardware watchpoints. */
10567
10568static void
10569print_mention_masked_watchpoint (struct breakpoint *b)
10570{
3a5c3e22 10571 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10572 struct ui_out *uiout = current_uiout;
46b9c129 10573 const char *tuple_name;
9c06b0b4
TJB
10574
10575 switch (b->type)
10576 {
10577 case bp_hardware_watchpoint:
112e8700 10578 uiout->text ("Masked hardware watchpoint ");
46b9c129 10579 tuple_name = "wpt";
9c06b0b4
TJB
10580 break;
10581 case bp_read_watchpoint:
112e8700 10582 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10583 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10584 break;
10585 case bp_access_watchpoint:
112e8700 10586 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10587 tuple_name = "hw-awpt";
9c06b0b4
TJB
10588 break;
10589 default:
10590 internal_error (__FILE__, __LINE__,
10591 _("Invalid hardware watchpoint type."));
10592 }
10593
46b9c129 10594 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10595 uiout->field_int ("number", b->number);
10596 uiout->text (": ");
10597 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10598}
10599
10600/* Implement the "print_recreate" breakpoint_ops method for
10601 masked hardware watchpoints. */
10602
10603static void
10604print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10605{
3a5c3e22 10606 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10607 char tmp[40];
10608
10609 switch (b->type)
10610 {
10611 case bp_hardware_watchpoint:
10612 fprintf_unfiltered (fp, "watch");
10613 break;
10614 case bp_read_watchpoint:
10615 fprintf_unfiltered (fp, "rwatch");
10616 break;
10617 case bp_access_watchpoint:
10618 fprintf_unfiltered (fp, "awatch");
10619 break;
10620 default:
10621 internal_error (__FILE__, __LINE__,
10622 _("Invalid hardware watchpoint type."));
10623 }
10624
3a5c3e22
PA
10625 sprintf_vma (tmp, w->hw_wp_mask);
10626 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10627 print_recreate_thread (b, fp);
9c06b0b4
TJB
10628}
10629
10630/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10631
2060206e 10632static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10633
10634/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10635
10636static int
10637is_masked_watchpoint (const struct breakpoint *b)
10638{
10639 return b->ops == &masked_watchpoint_breakpoint_ops;
10640}
10641
53a5351d
JM
10642/* accessflag: hw_write: watch write,
10643 hw_read: watch read,
10644 hw_access: watch access (read or write) */
c906108c 10645static void
bbc13ae3 10646watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10647 int just_location, int internal)
c906108c 10648{
c1fc2657 10649 struct breakpoint *scope_breakpoint = NULL;
270140bd 10650 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10651 struct value *val, *mark, *result;
bb9d5f81 10652 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10653 const char *exp_start = NULL;
10654 const char *exp_end = NULL;
10655 const char *tok, *end_tok;
9c06b0b4 10656 int toklen = -1;
bbc13ae3
KS
10657 const char *cond_start = NULL;
10658 const char *cond_end = NULL;
c906108c 10659 enum bptype bp_type;
37e4754d 10660 int thread = -1;
0cf6dd15 10661 int pc = 0;
9c06b0b4
TJB
10662 /* Flag to indicate whether we are going to use masks for
10663 the hardware watchpoint. */
10664 int use_mask = 0;
10665 CORE_ADDR mask = 0;
c906108c 10666
37e4754d
LM
10667 /* Make sure that we actually have parameters to parse. */
10668 if (arg != NULL && arg[0] != '\0')
10669 {
bbc13ae3
KS
10670 const char *value_start;
10671
10672 exp_end = arg + strlen (arg);
37e4754d 10673
9c06b0b4
TJB
10674 /* Look for "parameter value" pairs at the end
10675 of the arguments string. */
bbc13ae3 10676 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10677 {
10678 /* Skip whitespace at the end of the argument list. */
10679 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10680 tok--;
10681
10682 /* Find the beginning of the last token.
10683 This is the value of the parameter. */
10684 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10685 tok--;
10686 value_start = tok + 1;
10687
10688 /* Skip whitespace. */
10689 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10690 tok--;
10691
10692 end_tok = tok;
10693
10694 /* Find the beginning of the second to last token.
10695 This is the parameter itself. */
10696 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10697 tok--;
10698 tok++;
10699 toklen = end_tok - tok + 1;
10700
61012eef 10701 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10702 {
5d5658a1 10703 struct thread_info *thr;
9c06b0b4
TJB
10704 /* At this point we've found a "thread" token, which means
10705 the user is trying to set a watchpoint that triggers
10706 only in a specific thread. */
5d5658a1 10707 const char *endp;
37e4754d 10708
9c06b0b4
TJB
10709 if (thread != -1)
10710 error(_("You can specify only one thread."));
37e4754d 10711
9c06b0b4 10712 /* Extract the thread ID from the next token. */
5d5658a1 10713 thr = parse_thread_id (value_start, &endp);
37e4754d 10714
5d5658a1 10715 /* Check if the user provided a valid thread ID. */
9c06b0b4 10716 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10717 invalid_thread_id_error (value_start);
9c06b0b4 10718
5d5658a1 10719 thread = thr->global_num;
9c06b0b4 10720 }
61012eef 10721 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10722 {
10723 /* We've found a "mask" token, which means the user wants to
10724 create a hardware watchpoint that is going to have the mask
10725 facility. */
10726 struct value *mask_value, *mark;
37e4754d 10727
9c06b0b4
TJB
10728 if (use_mask)
10729 error(_("You can specify only one mask."));
37e4754d 10730
9c06b0b4 10731 use_mask = just_location = 1;
37e4754d 10732
9c06b0b4
TJB
10733 mark = value_mark ();
10734 mask_value = parse_to_comma_and_eval (&value_start);
10735 mask = value_as_address (mask_value);
10736 value_free_to_mark (mark);
10737 }
10738 else
10739 /* We didn't recognize what we found. We should stop here. */
10740 break;
37e4754d 10741
9c06b0b4
TJB
10742 /* Truncate the string and get rid of the "parameter value" pair before
10743 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10744 exp_end = tok;
9c06b0b4 10745 }
37e4754d 10746 }
bbc13ae3
KS
10747 else
10748 exp_end = arg;
37e4754d 10749
bbc13ae3
KS
10750 /* Parse the rest of the arguments. From here on out, everything
10751 is in terms of a newly allocated string instead of the original
10752 ARG. */
c906108c 10753 innermost_block = NULL;
81b1e71c
TT
10754 std::string expression (arg, exp_end - arg);
10755 exp_start = arg = expression.c_str ();
4d01a485 10756 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 10757 exp_end = arg;
fa8a61dc
TT
10758 /* Remove trailing whitespace from the expression before saving it.
10759 This makes the eventual display of the expression string a bit
10760 prettier. */
10761 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10762 --exp_end;
10763
65d79d4b 10764 /* Checking if the expression is not constant. */
4d01a485 10765 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10766 {
10767 int len;
10768
10769 len = exp_end - exp_start;
10770 while (len > 0 && isspace (exp_start[len - 1]))
10771 len--;
10772 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10773 }
10774
c906108c
SS
10775 exp_valid_block = innermost_block;
10776 mark = value_mark ();
4d01a485 10777 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
06a64a0b 10778
bb9d5f81
PP
10779 if (val != NULL && just_location)
10780 {
10781 saved_bitpos = value_bitpos (val);
10782 saved_bitsize = value_bitsize (val);
10783 }
10784
06a64a0b
TT
10785 if (just_location)
10786 {
9c06b0b4
TJB
10787 int ret;
10788
06a64a0b 10789 exp_valid_block = NULL;
a1442452 10790 val = value_addr (result);
06a64a0b
TT
10791 release_value (val);
10792 value_free_to_mark (mark);
9c06b0b4
TJB
10793
10794 if (use_mask)
10795 {
10796 ret = target_masked_watch_num_registers (value_as_address (val),
10797 mask);
10798 if (ret == -1)
10799 error (_("This target does not support masked watchpoints."));
10800 else if (ret == -2)
10801 error (_("Invalid mask or memory region."));
10802 }
06a64a0b
TT
10803 }
10804 else if (val != NULL)
fa4727a6 10805 release_value (val);
c906108c 10806
f1735a53
TT
10807 tok = skip_spaces (arg);
10808 end_tok = skip_to_space (tok);
c906108c
SS
10809
10810 toklen = end_tok - tok;
10811 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10812 {
60e1c644 10813 innermost_block = NULL;
c906108c 10814 tok = cond_start = end_tok + 1;
4d01a485 10815 parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
10816
10817 /* The watchpoint expression may not be local, but the condition
10818 may still be. E.g.: `watch global if local > 0'. */
10819 cond_exp_valid_block = innermost_block;
10820
c906108c
SS
10821 cond_end = tok;
10822 }
10823 if (*tok)
8a3fe4f8 10824 error (_("Junk at end of command."));
c906108c 10825
441d7c93
PA
10826 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10827
10828 /* Save this because create_internal_breakpoint below invalidates
10829 'wp_frame'. */
10830 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10831
10832 /* If the expression is "local", then set up a "watchpoint scope"
10833 breakpoint at the point where we've left the scope of the watchpoint
10834 expression. Create the scope breakpoint before the watchpoint, so
10835 that we will encounter it first in bpstat_stop_status. */
441d7c93 10836 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10837 {
441d7c93
PA
10838 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10839
10840 if (frame_id_p (caller_frame_id))
edb3359d 10841 {
441d7c93
PA
10842 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10843 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10844
edb3359d 10845 scope_breakpoint
441d7c93 10846 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10847 bp_watchpoint_scope,
10848 &momentary_breakpoint_ops);
d983da9c 10849
441d7c93
PA
10850 /* create_internal_breakpoint could invalidate WP_FRAME. */
10851 wp_frame = NULL;
10852
edb3359d 10853 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10854
edb3359d
DJ
10855 /* Automatically delete the breakpoint when it hits. */
10856 scope_breakpoint->disposition = disp_del;
d983da9c 10857
edb3359d 10858 /* Only break in the proper frame (help with recursion). */
441d7c93 10859 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10860
edb3359d 10861 /* Set the address at which we will stop. */
441d7c93
PA
10862 scope_breakpoint->loc->gdbarch = caller_arch;
10863 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10864 scope_breakpoint->loc->address
a6d9a66e
UW
10865 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10866 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10867 scope_breakpoint->type);
10868 }
d983da9c
DJ
10869 }
10870
e8369a73
AB
10871 /* Now set up the breakpoint. We create all watchpoints as hardware
10872 watchpoints here even if hardware watchpoints are turned off, a call
10873 to update_watchpoint later in this function will cause the type to
10874 drop back to bp_watchpoint (software watchpoint) if required. */
10875
10876 if (accessflag == hw_read)
10877 bp_type = bp_read_watchpoint;
10878 else if (accessflag == hw_access)
10879 bp_type = bp_access_watchpoint;
10880 else
10881 bp_type = bp_hardware_watchpoint;
3a5c3e22 10882
b270e6f9 10883 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10884
348d480f 10885 if (use_mask)
b270e6f9 10886 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10887 &masked_watchpoint_breakpoint_ops);
348d480f 10888 else
b270e6f9 10889 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10890 &watchpoint_breakpoint_ops);
c1fc2657
SM
10891 w->thread = thread;
10892 w->disposition = disp_donttouch;
10893 w->pspace = current_program_space;
b22e99fd 10894 w->exp = std::move (exp);
3a5c3e22
PA
10895 w->exp_valid_block = exp_valid_block;
10896 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10897 if (just_location)
10898 {
10899 struct type *t = value_type (val);
10900 CORE_ADDR addr = value_as_address (val);
06a64a0b 10901
43cc5389
TT
10902 w->exp_string_reparse
10903 = current_language->la_watch_location_expression (t, addr).release ();
06a64a0b 10904
3a5c3e22 10905 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10906 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10907 }
10908 else
3a5c3e22 10909 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10910
10911 if (use_mask)
10912 {
3a5c3e22 10913 w->hw_wp_mask = mask;
9c06b0b4
TJB
10914 }
10915 else
10916 {
3a5c3e22 10917 w->val = val;
bb9d5f81
PP
10918 w->val_bitpos = saved_bitpos;
10919 w->val_bitsize = saved_bitsize;
3a5c3e22 10920 w->val_valid = 1;
9c06b0b4 10921 }
77b06cd7 10922
c906108c 10923 if (cond_start)
c1fc2657 10924 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10925 else
c1fc2657 10926 w->cond_string = 0;
c5aa993b 10927
441d7c93 10928 if (frame_id_p (watchpoint_frame))
f6bc2008 10929 {
441d7c93 10930 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10931 w->watchpoint_thread = inferior_ptid;
f6bc2008 10932 }
c906108c 10933 else
f6bc2008 10934 {
3a5c3e22
PA
10935 w->watchpoint_frame = null_frame_id;
10936 w->watchpoint_thread = null_ptid;
f6bc2008 10937 }
c906108c 10938
d983da9c 10939 if (scope_breakpoint != NULL)
c906108c 10940 {
d983da9c
DJ
10941 /* The scope breakpoint is related to the watchpoint. We will
10942 need to act on them together. */
c1fc2657 10943 w->related_breakpoint = scope_breakpoint;
b270e6f9 10944 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10945 }
d983da9c 10946
06a64a0b
TT
10947 if (!just_location)
10948 value_free_to_mark (mark);
2d134ed3 10949
b270e6f9
TT
10950 /* Finally update the new watchpoint. This creates the locations
10951 that should be inserted. */
10952 update_watchpoint (w.get (), 1);
a9634178 10953
b270e6f9 10954 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10955}
10956
e09342b5 10957/* Return count of debug registers needed to watch the given expression.
e09342b5 10958 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10959
c906108c 10960static int
a9634178 10961can_use_hardware_watchpoint (struct value *v)
c906108c
SS
10962{
10963 int found_memory_cnt = 0;
2e70b7b9 10964 struct value *head = v;
c906108c
SS
10965
10966 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10967 if (!can_use_hw_watchpoints)
c906108c 10968 return 0;
c5aa993b 10969
5c44784c
JM
10970 /* Make sure that the value of the expression depends only upon
10971 memory contents, and values computed from them within GDB. If we
10972 find any register references or function calls, we can't use a
10973 hardware watchpoint.
10974
10975 The idea here is that evaluating an expression generates a series
10976 of values, one holding the value of every subexpression. (The
10977 expression a*b+c has five subexpressions: a, b, a*b, c, and
10978 a*b+c.) GDB's values hold almost enough information to establish
10979 the criteria given above --- they identify memory lvalues,
10980 register lvalues, computed values, etcetera. So we can evaluate
10981 the expression, and then scan the chain of values that leaves
10982 behind to decide whether we can detect any possible change to the
10983 expression's final value using only hardware watchpoints.
10984
10985 However, I don't think that the values returned by inferior
10986 function calls are special in any way. So this function may not
10987 notice that an expression involving an inferior function call
10988 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 10989 for (; v; v = value_next (v))
c906108c 10990 {
5c44784c 10991 if (VALUE_LVAL (v) == lval_memory)
c906108c 10992 {
8464be76
DJ
10993 if (v != head && value_lazy (v))
10994 /* A lazy memory lvalue in the chain is one that GDB never
10995 needed to fetch; we either just used its address (e.g.,
10996 `a' in `a.b') or we never needed it at all (e.g., `a'
10997 in `a,b'). This doesn't apply to HEAD; if that is
10998 lazy then it was not readable, but watch it anyway. */
5c44784c 10999 ;
53a5351d 11000 else
5c44784c
JM
11001 {
11002 /* Ahh, memory we actually used! Check if we can cover
11003 it with hardware watchpoints. */
df407dfe 11004 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11005
11006 /* We only watch structs and arrays if user asked for it
11007 explicitly, never if they just happen to appear in a
11008 middle of some value chain. */
11009 if (v == head
11010 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11011 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11012 {
42ae5230 11013 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11014 int len;
11015 int num_regs;
11016
a9634178 11017 len = (target_exact_watchpoints
e09342b5
TJB
11018 && is_scalar_type_recursive (vtype))?
11019 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11020
e09342b5
TJB
11021 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11022 if (!num_regs)
2e70b7b9
MS
11023 return 0;
11024 else
e09342b5 11025 found_memory_cnt += num_regs;
2e70b7b9 11026 }
5c44784c 11027 }
c5aa993b 11028 }
5086187c
AC
11029 else if (VALUE_LVAL (v) != not_lval
11030 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11031 return 0; /* These are values from the history (e.g., $1). */
5086187c 11032 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11033 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11034 }
11035
11036 /* The expression itself looks suitable for using a hardware
11037 watchpoint, but give the target machine a chance to reject it. */
11038 return found_memory_cnt;
11039}
11040
8b93c638 11041void
f2fc3015 11042watch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11043{
84f4c1fe 11044 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11045}
11046
06a64a0b
TT
11047/* A helper function that looks for the "-location" argument and then
11048 calls watch_command_1. */
11049
11050static void
0b39b52e 11051watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b
TT
11052{
11053 int just_location = 0;
11054
11055 if (arg
11056 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11057 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11058 {
e9cafbcc 11059 arg = skip_spaces (arg);
06a64a0b
TT
11060 just_location = 1;
11061 }
11062
84f4c1fe 11063 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11064}
8926118c 11065
c5aa993b 11066static void
0b39b52e 11067watch_command (const char *arg, int from_tty)
c906108c 11068{
06a64a0b 11069 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11070}
11071
8b93c638 11072void
f2fc3015 11073rwatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11074{
84f4c1fe 11075 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11076}
8926118c 11077
c5aa993b 11078static void
0b39b52e 11079rwatch_command (const char *arg, int from_tty)
c906108c 11080{
06a64a0b 11081 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11082}
11083
8b93c638 11084void
f2fc3015 11085awatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11086{
84f4c1fe 11087 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11088}
8926118c 11089
c5aa993b 11090static void
0b39b52e 11091awatch_command (const char *arg, int from_tty)
c906108c 11092{
06a64a0b 11093 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11094}
c906108c 11095\f
c5aa993b 11096
cfc31633
PA
11097/* Data for the FSM that manages the until(location)/advance commands
11098 in infcmd.c. Here because it uses the mechanisms of
11099 breakpoints. */
c906108c 11100
cfc31633 11101struct until_break_fsm
bfec99b2 11102{
cfc31633
PA
11103 /* The base class. */
11104 struct thread_fsm thread_fsm;
11105
11106 /* The thread that as current when the command was executed. */
11107 int thread;
11108
11109 /* The breakpoint set at the destination location. */
11110 struct breakpoint *location_breakpoint;
11111
11112 /* Breakpoint set at the return address in the caller frame. May be
11113 NULL. */
11114 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11115};
11116
8980e177
PA
11117static void until_break_fsm_clean_up (struct thread_fsm *self,
11118 struct thread_info *thread);
11119static int until_break_fsm_should_stop (struct thread_fsm *self,
11120 struct thread_info *thread);
cfc31633
PA
11121static enum async_reply_reason
11122 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11123
11124/* until_break_fsm's vtable. */
11125
11126static struct thread_fsm_ops until_break_fsm_ops =
11127{
11128 NULL, /* dtor */
11129 until_break_fsm_clean_up,
11130 until_break_fsm_should_stop,
11131 NULL, /* return_value */
11132 until_break_fsm_async_reply_reason,
11133};
11134
11135/* Allocate a new until_break_command_fsm. */
11136
11137static struct until_break_fsm *
8980e177 11138new_until_break_fsm (struct interp *cmd_interp, int thread,
454dafbd
TT
11139 breakpoint_up &&location_breakpoint,
11140 breakpoint_up &&caller_breakpoint)
cfc31633
PA
11141{
11142 struct until_break_fsm *sm;
11143
11144 sm = XCNEW (struct until_break_fsm);
8980e177 11145 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11146
11147 sm->thread = thread;
454dafbd
TT
11148 sm->location_breakpoint = location_breakpoint.release ();
11149 sm->caller_breakpoint = caller_breakpoint.release ();
cfc31633
PA
11150
11151 return sm;
11152}
11153
11154/* Implementation of the 'should_stop' FSM method for the
11155 until(location)/advance commands. */
11156
11157static int
8980e177
PA
11158until_break_fsm_should_stop (struct thread_fsm *self,
11159 struct thread_info *tp)
cfc31633
PA
11160{
11161 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11162
11163 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11164 sm->location_breakpoint) != NULL
11165 || (sm->caller_breakpoint != NULL
11166 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11167 sm->caller_breakpoint) != NULL))
11168 thread_fsm_set_finished (self);
11169
11170 return 1;
11171}
11172
11173/* Implementation of the 'clean_up' FSM method for the
11174 until(location)/advance commands. */
11175
c2c6d25f 11176static void
8980e177
PA
11177until_break_fsm_clean_up (struct thread_fsm *self,
11178 struct thread_info *thread)
43ff13b4 11179{
cfc31633 11180 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11181
cfc31633
PA
11182 /* Clean up our temporary breakpoints. */
11183 if (sm->location_breakpoint != NULL)
11184 {
11185 delete_breakpoint (sm->location_breakpoint);
11186 sm->location_breakpoint = NULL;
11187 }
11188 if (sm->caller_breakpoint != NULL)
11189 {
11190 delete_breakpoint (sm->caller_breakpoint);
11191 sm->caller_breakpoint = NULL;
11192 }
11193 delete_longjmp_breakpoint (sm->thread);
11194}
11195
11196/* Implementation of the 'async_reply_reason' FSM method for the
11197 until(location)/advance commands. */
11198
11199static enum async_reply_reason
11200until_break_fsm_async_reply_reason (struct thread_fsm *self)
11201{
11202 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11203}
11204
c906108c 11205void
f2fc3015 11206until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11207{
8556afb4
PA
11208 struct frame_info *frame;
11209 struct gdbarch *frame_gdbarch;
11210 struct frame_id stack_frame_id;
11211 struct frame_id caller_frame_id;
ffc2605c 11212 struct cleanup *old_chain;
186c406b
TT
11213 int thread;
11214 struct thread_info *tp;
cfc31633 11215 struct until_break_fsm *sm;
c906108c 11216
70509625 11217 clear_proceed_status (0);
c906108c
SS
11218
11219 /* Set a breakpoint where the user wants it and at return from
4a64f543 11220 this function. */
c5aa993b 11221
ffc2605c 11222 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11223
6c5b2ebe
PA
11224 std::vector<symtab_and_line> sals
11225 = (last_displayed_sal_is_valid ()
11226 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11227 get_last_displayed_symtab (),
11228 get_last_displayed_line ())
11229 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11230 NULL, (struct symtab *) NULL, 0));
c5aa993b 11231
6c5b2ebe 11232 if (sals.size () != 1)
8a3fe4f8 11233 error (_("Couldn't get information on specified line."));
c5aa993b 11234
6c5b2ebe 11235 symtab_and_line &sal = sals[0];
c5aa993b 11236
c906108c 11237 if (*arg)
8a3fe4f8 11238 error (_("Junk at end of arguments."));
c5aa993b 11239
c906108c 11240 resolve_sal_pc (&sal);
c5aa993b 11241
186c406b 11242 tp = inferior_thread ();
5d5658a1 11243 thread = tp->global_num;
186c406b 11244
883bc8d1
PA
11245 old_chain = make_cleanup (null_cleanup, NULL);
11246
8556afb4
PA
11247 /* Note linespec handling above invalidates the frame chain.
11248 Installing a breakpoint also invalidates the frame chain (as it
11249 may need to switch threads), so do any frame handling before
11250 that. */
11251
11252 frame = get_selected_frame (NULL);
11253 frame_gdbarch = get_frame_arch (frame);
11254 stack_frame_id = get_stack_frame_id (frame);
11255 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11256
ae66c1fc
EZ
11257 /* Keep within the current frame, or in frames called by the current
11258 one. */
edb3359d 11259
454dafbd 11260 breakpoint_up caller_breakpoint;
883bc8d1 11261 if (frame_id_p (caller_frame_id))
c906108c 11262 {
883bc8d1 11263 struct symtab_and_line sal2;
cfc31633 11264 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11265
11266 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11267 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11268 caller_gdbarch = frame_unwind_caller_arch (frame);
11269 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11270 sal2,
11271 caller_frame_id,
11272 bp_until);
186c406b 11273
883bc8d1 11274 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11275 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11276 }
c5aa993b 11277
c70a6932
JK
11278 /* set_momentary_breakpoint could invalidate FRAME. */
11279 frame = NULL;
11280
454dafbd 11281 breakpoint_up location_breakpoint;
883bc8d1
PA
11282 if (anywhere)
11283 /* If the user told us to continue until a specified location,
11284 we don't specify a frame at which we need to stop. */
cfc31633
PA
11285 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11286 null_frame_id, bp_until);
883bc8d1
PA
11287 else
11288 /* Otherwise, specify the selected frame, because we want to stop
11289 only at the very same frame. */
cfc31633
PA
11290 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11291 stack_frame_id, bp_until);
883bc8d1 11292
8980e177 11293 sm = new_until_break_fsm (command_interp (), tp->global_num,
454dafbd
TT
11294 std::move (location_breakpoint),
11295 std::move (caller_breakpoint));
cfc31633 11296 tp->thread_fsm = &sm->thread_fsm;
f107f563 11297
cfc31633 11298 discard_cleanups (old_chain);
f107f563 11299
cfc31633 11300 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11301}
ae66c1fc 11302
c906108c
SS
11303/* This function attempts to parse an optional "if <cond>" clause
11304 from the arg string. If one is not found, it returns NULL.
c5aa993b 11305
c906108c
SS
11306 Else, it returns a pointer to the condition string. (It does not
11307 attempt to evaluate the string against a particular block.) And,
11308 it updates arg to point to the first character following the parsed
4a64f543 11309 if clause in the arg string. */
53a5351d 11310
63160a43
PA
11311const char *
11312ep_parse_optional_if_clause (const char **arg)
c906108c 11313{
63160a43 11314 const char *cond_string;
c5aa993b
JM
11315
11316 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11317 return NULL;
c5aa993b 11318
4a64f543 11319 /* Skip the "if" keyword. */
c906108c 11320 (*arg) += 2;
c5aa993b 11321
c906108c 11322 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11323 condition string. */
f1735a53 11324 *arg = skip_spaces (*arg);
c906108c 11325 cond_string = *arg;
c5aa993b 11326
4a64f543
MS
11327 /* Assume that the condition occupies the remainder of the arg
11328 string. */
c906108c 11329 (*arg) += strlen (cond_string);
c5aa993b 11330
c906108c
SS
11331 return cond_string;
11332}
c5aa993b 11333
c906108c
SS
11334/* Commands to deal with catching events, such as signals, exceptions,
11335 process start/exit, etc. */
c5aa993b
JM
11336
11337typedef enum
11338{
44feb3ce
TT
11339 catch_fork_temporary, catch_vfork_temporary,
11340 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11341}
11342catch_fork_kind;
11343
c906108c 11344static void
63160a43 11345catch_fork_command_1 (char *arg_entry, int from_tty,
cc59ec59 11346 struct cmd_list_element *command)
c906108c 11347{
63160a43 11348 const char *arg = arg_entry;
a6d9a66e 11349 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11350 const char *cond_string = NULL;
44feb3ce
TT
11351 catch_fork_kind fork_kind;
11352 int tempflag;
11353
11354 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11355 tempflag = (fork_kind == catch_fork_temporary
11356 || fork_kind == catch_vfork_temporary);
c5aa993b 11357
44feb3ce
TT
11358 if (!arg)
11359 arg = "";
f1735a53 11360 arg = skip_spaces (arg);
c5aa993b 11361
c906108c 11362 /* The allowed syntax is:
c5aa993b
JM
11363 catch [v]fork
11364 catch [v]fork if <cond>
11365
4a64f543 11366 First, check if there's an if clause. */
c906108c 11367 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11368
c906108c 11369 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11370 error (_("Junk at end of arguments."));
c5aa993b 11371
c906108c 11372 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11373 and enable reporting of such events. */
c5aa993b
JM
11374 switch (fork_kind)
11375 {
44feb3ce
TT
11376 case catch_fork_temporary:
11377 case catch_fork_permanent:
a6d9a66e 11378 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11379 &catch_fork_breakpoint_ops);
c906108c 11380 break;
44feb3ce
TT
11381 case catch_vfork_temporary:
11382 case catch_vfork_permanent:
a6d9a66e 11383 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11384 &catch_vfork_breakpoint_ops);
c906108c 11385 break;
c5aa993b 11386 default:
8a3fe4f8 11387 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11388 break;
c5aa993b 11389 }
c906108c
SS
11390}
11391
11392static void
63160a43 11393catch_exec_command_1 (char *arg_entry, int from_tty,
cc59ec59 11394 struct cmd_list_element *command)
c906108c 11395{
63160a43 11396 const char *arg = arg_entry;
a6d9a66e 11397 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11398 int tempflag;
63160a43 11399 const char *cond_string = NULL;
c906108c 11400
44feb3ce
TT
11401 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11402
11403 if (!arg)
11404 arg = "";
f1735a53 11405 arg = skip_spaces (arg);
c906108c
SS
11406
11407 /* The allowed syntax is:
c5aa993b
JM
11408 catch exec
11409 catch exec if <cond>
c906108c 11410
4a64f543 11411 First, check if there's an if clause. */
c906108c
SS
11412 cond_string = ep_parse_optional_if_clause (&arg);
11413
11414 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11415 error (_("Junk at end of arguments."));
c906108c 11416
b270e6f9
TT
11417 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11418 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
b4d90040
PA
11419 &catch_exec_breakpoint_ops);
11420 c->exec_pathname = NULL;
11421
b270e6f9 11422 install_breakpoint (0, std::move (c), 1);
c906108c 11423}
c5aa993b 11424
9ac4176b 11425void
28010a5d
PA
11426init_ada_exception_breakpoint (struct breakpoint *b,
11427 struct gdbarch *gdbarch,
11428 struct symtab_and_line sal,
f2fc3015 11429 const char *addr_string,
c0a91b2b 11430 const struct breakpoint_ops *ops,
28010a5d 11431 int tempflag,
349774ef 11432 int enabled,
28010a5d 11433 int from_tty)
f7f9143b 11434{
f7f9143b
JB
11435 if (from_tty)
11436 {
5af949e3
UW
11437 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11438 if (!loc_gdbarch)
11439 loc_gdbarch = gdbarch;
11440
6c95b8df
PA
11441 describe_other_breakpoints (loc_gdbarch,
11442 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11443 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11444 version for exception catchpoints, because two catchpoints
11445 used for different exception names will use the same address.
11446 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11447 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11448 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11449 the user what type of catchpoint it is. The above is good
11450 enough for now, though. */
11451 }
11452
28010a5d 11453 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11454
349774ef 11455 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11456 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11457 b->location = string_to_event_location (&addr_string,
11458 language_def (language_ada));
f7f9143b 11459 b->language = language_ada;
f7f9143b
JB
11460}
11461
c906108c 11462static void
981a3fb3 11463catch_command (const char *arg, int from_tty)
c906108c 11464{
44feb3ce 11465 error (_("Catch requires an event name."));
c906108c
SS
11466}
11467\f
11468
11469static void
981a3fb3 11470tcatch_command (const char *arg, int from_tty)
c906108c 11471{
44feb3ce 11472 error (_("Catch requires an event name."));
c906108c
SS
11473}
11474
81b1e71c 11475/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11476
11477static int
81b1e71c 11478compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11479{
81b1e71c
TT
11480 uintptr_t ua = (uintptr_t) a;
11481 uintptr_t ub = (uintptr_t) b;
8a2c437b 11482
81b1e71c 11483 if (a->number < b->number)
8a2c437b 11484 return -1;
81b1e71c 11485 else if (a->number > b->number)
8a2c437b
TT
11486 return 1;
11487
11488 /* Now sort by address, in case we see, e..g, two breakpoints with
11489 the number 0. */
11490 if (ua < ub)
11491 return -1;
94b0e70d 11492 return ua > ub ? 1 : 0;
8a2c437b
TT
11493}
11494
80f8a6eb 11495/* Delete breakpoints by address or line. */
c906108c
SS
11496
11497static void
0b39b52e 11498clear_command (const char *arg, int from_tty)
c906108c 11499{
81b1e71c 11500 struct breakpoint *b;
c906108c 11501 int default_match;
c906108c
SS
11502 int i;
11503
6c5b2ebe
PA
11504 std::vector<symtab_and_line> decoded_sals;
11505 symtab_and_line last_sal;
11506 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11507 if (arg)
11508 {
6c5b2ebe
PA
11509 decoded_sals
11510 = decode_line_with_current_source (arg,
11511 (DECODE_LINE_FUNFIRSTLINE
11512 | DECODE_LINE_LIST_MODE));
c906108c 11513 default_match = 0;
6c5b2ebe 11514 sals = decoded_sals;
c906108c
SS
11515 }
11516 else
11517 {
1bfeeb0f
JL
11518 /* Set sal's line, symtab, pc, and pspace to the values
11519 corresponding to the last call to print_frame_info. If the
11520 codepoint is not valid, this will set all the fields to 0. */
51abb421 11521 last_sal = get_last_displayed_sal ();
6c5b2ebe 11522 if (last_sal.symtab == 0)
8a3fe4f8 11523 error (_("No source file specified."));
c906108c 11524
c906108c 11525 default_match = 1;
6c5b2ebe 11526 sals = last_sal;
c906108c
SS
11527 }
11528
4a64f543
MS
11529 /* We don't call resolve_sal_pc here. That's not as bad as it
11530 seems, because all existing breakpoints typically have both
11531 file/line and pc set. So, if clear is given file/line, we can
11532 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11533
11534 We only support clearing given the address explicitly
11535 present in breakpoint table. Say, we've set breakpoint
4a64f543 11536 at file:line. There were several PC values for that file:line,
ed0616c6 11537 due to optimization, all in one block.
4a64f543
MS
11538
11539 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11540 PC corresponding to the same file:line, the breakpoint won't
11541 be cleared. We probably can still clear the breakpoint, but
11542 since the other PC value is never presented to user, user
11543 can only find it by guessing, and it does not seem important
11544 to support that. */
11545
4a64f543
MS
11546 /* For each line spec given, delete bps which correspond to it. Do
11547 it in two passes, solely to preserve the current behavior that
11548 from_tty is forced true if we delete more than one
11549 breakpoint. */
c906108c 11550
81b1e71c 11551 std::vector<struct breakpoint *> found;
6c5b2ebe 11552 for (const auto &sal : sals)
c906108c 11553 {
05cba821
JK
11554 const char *sal_fullname;
11555
c906108c 11556 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11557 If line given (pc == 0), clear all bpts on specified line.
11558 If defaulting, clear all bpts on default line
c906108c 11559 or at default pc.
c5aa993b
JM
11560
11561 defaulting sal.pc != 0 tests to do
11562
11563 0 1 pc
11564 1 1 pc _and_ line
11565 0 0 line
11566 1 0 <can't happen> */
c906108c 11567
05cba821
JK
11568 sal_fullname = (sal.symtab == NULL
11569 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11570
4a64f543 11571 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11572 ALL_BREAKPOINTS (b)
c5aa993b 11573 {
0d381245 11574 int match = 0;
4a64f543 11575 /* Are we going to delete b? */
cc60f2e3 11576 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11577 {
11578 struct bp_location *loc = b->loc;
11579 for (; loc; loc = loc->next)
11580 {
f8eba3c6
TT
11581 /* If the user specified file:line, don't allow a PC
11582 match. This matches historical gdb behavior. */
11583 int pc_match = (!sal.explicit_line
11584 && sal.pc
11585 && (loc->pspace == sal.pspace)
11586 && (loc->address == sal.pc)
11587 && (!section_is_overlay (loc->section)
11588 || loc->section == sal.section));
4aac40c8
TT
11589 int line_match = 0;
11590
11591 if ((default_match || sal.explicit_line)
2f202fde 11592 && loc->symtab != NULL
05cba821 11593 && sal_fullname != NULL
4aac40c8 11594 && sal.pspace == loc->pspace
05cba821
JK
11595 && loc->line_number == sal.line
11596 && filename_cmp (symtab_to_fullname (loc->symtab),
11597 sal_fullname) == 0)
11598 line_match = 1;
4aac40c8 11599
0d381245
VP
11600 if (pc_match || line_match)
11601 {
11602 match = 1;
11603 break;
11604 }
11605 }
11606 }
11607
11608 if (match)
81b1e71c 11609 found.push_back (b);
c906108c 11610 }
80f8a6eb 11611 }
8a2c437b 11612
80f8a6eb 11613 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11614 if (found.empty ())
80f8a6eb
MS
11615 {
11616 if (arg)
8a3fe4f8 11617 error (_("No breakpoint at %s."), arg);
80f8a6eb 11618 else
8a3fe4f8 11619 error (_("No breakpoint at this line."));
80f8a6eb 11620 }
c906108c 11621
8a2c437b 11622 /* Remove duplicates from the vec. */
81b1e71c
TT
11623 std::sort (found.begin (), found.end (),
11624 [] (const breakpoint *a, const breakpoint *b)
11625 {
11626 return compare_breakpoints (a, b) < 0;
11627 });
11628 found.erase (std::unique (found.begin (), found.end (),
11629 [] (const breakpoint *a, const breakpoint *b)
11630 {
11631 return compare_breakpoints (a, b) == 0;
11632 }),
11633 found.end ());
8a2c437b 11634
81b1e71c 11635 if (found.size () > 1)
4a64f543 11636 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11637 if (from_tty)
a3f17187 11638 {
81b1e71c 11639 if (found.size () == 1)
a3f17187
AC
11640 printf_unfiltered (_("Deleted breakpoint "));
11641 else
11642 printf_unfiltered (_("Deleted breakpoints "));
11643 }
d6e956e5 11644
81b1e71c 11645 for (breakpoint *iter : found)
80f8a6eb 11646 {
c5aa993b 11647 if (from_tty)
81b1e71c
TT
11648 printf_unfiltered ("%d ", iter->number);
11649 delete_breakpoint (iter);
c906108c 11650 }
80f8a6eb
MS
11651 if (from_tty)
11652 putchar_unfiltered ('\n');
c906108c
SS
11653}
11654\f
11655/* Delete breakpoint in BS if they are `delete' breakpoints and
11656 all breakpoints that are marked for deletion, whether hit or not.
11657 This is called after any breakpoint is hit, or after errors. */
11658
11659void
fba45db2 11660breakpoint_auto_delete (bpstat bs)
c906108c 11661{
35df4500 11662 struct breakpoint *b, *b_tmp;
c906108c
SS
11663
11664 for (; bs; bs = bs->next)
f431efe5
PA
11665 if (bs->breakpoint_at
11666 && bs->breakpoint_at->disposition == disp_del
c906108c 11667 && bs->stop)
f431efe5 11668 delete_breakpoint (bs->breakpoint_at);
c906108c 11669
35df4500 11670 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11671 {
b5de0fa7 11672 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11673 delete_breakpoint (b);
11674 }
c906108c
SS
11675}
11676
4a64f543
MS
11677/* A comparison function for bp_location AP and BP being interfaced to
11678 qsort. Sort elements primarily by their ADDRESS (no matter what
11679 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 11680 secondarily by ordering first permanent elements and
4a64f543 11681 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11682 qsort being an unstable algorithm. */
876fa593
JK
11683
11684static int
f5336ca5 11685bp_locations_compare (const void *ap, const void *bp)
876fa593 11686{
9a3c8263
SM
11687 const struct bp_location *a = *(const struct bp_location **) ap;
11688 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
11689
11690 if (a->address != b->address)
11691 return (a->address > b->address) - (a->address < b->address);
11692
dea2aa5f
LM
11693 /* Sort locations at the same address by their pspace number, keeping
11694 locations of the same inferior (in a multi-inferior environment)
11695 grouped. */
11696
11697 if (a->pspace->num != b->pspace->num)
11698 return ((a->pspace->num > b->pspace->num)
11699 - (a->pspace->num < b->pspace->num));
11700
876fa593 11701 /* Sort permanent breakpoints first. */
1a853c52
PA
11702 if (a->permanent != b->permanent)
11703 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 11704
c56a97f9
JK
11705 /* Make the internal GDB representation stable across GDB runs
11706 where A and B memory inside GDB can differ. Breakpoint locations of
11707 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11708
11709 if (a->owner->number != b->owner->number)
c56a97f9
JK
11710 return ((a->owner->number > b->owner->number)
11711 - (a->owner->number < b->owner->number));
876fa593
JK
11712
11713 return (a > b) - (a < b);
11714}
11715
f5336ca5
PA
11716/* Set bp_locations_placed_address_before_address_max and
11717 bp_locations_shadow_len_after_address_max according to the current
11718 content of the bp_locations array. */
f7545552
TT
11719
11720static void
f5336ca5 11721bp_locations_target_extensions_update (void)
f7545552 11722{
876fa593
JK
11723 struct bp_location *bl, **blp_tmp;
11724
f5336ca5
PA
11725 bp_locations_placed_address_before_address_max = 0;
11726 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11727
11728 ALL_BP_LOCATIONS (bl, blp_tmp)
11729 {
11730 CORE_ADDR start, end, addr;
11731
11732 if (!bp_location_has_shadow (bl))
11733 continue;
11734
11735 start = bl->target_info.placed_address;
11736 end = start + bl->target_info.shadow_len;
11737
11738 gdb_assert (bl->address >= start);
11739 addr = bl->address - start;
f5336ca5
PA
11740 if (addr > bp_locations_placed_address_before_address_max)
11741 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11742
11743 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11744
11745 gdb_assert (bl->address < end);
11746 addr = end - bl->address;
f5336ca5
PA
11747 if (addr > bp_locations_shadow_len_after_address_max)
11748 bp_locations_shadow_len_after_address_max = addr;
876fa593 11749 }
f7545552
TT
11750}
11751
1e4d1764
YQ
11752/* Download tracepoint locations if they haven't been. */
11753
11754static void
11755download_tracepoint_locations (void)
11756{
7ed2c994 11757 struct breakpoint *b;
dd2e65cc 11758 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11759
5ed8105e 11760 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11761
7ed2c994 11762 ALL_TRACEPOINTS (b)
1e4d1764 11763 {
7ed2c994 11764 struct bp_location *bl;
1e4d1764 11765 struct tracepoint *t;
f2a8bc8a 11766 int bp_location_downloaded = 0;
1e4d1764 11767
7ed2c994 11768 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11769 ? !may_insert_fast_tracepoints
11770 : !may_insert_tracepoints))
11771 continue;
11772
dd2e65cc
YQ
11773 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11774 {
11775 if (target_can_download_tracepoint ())
11776 can_download_tracepoint = TRIBOOL_TRUE;
11777 else
11778 can_download_tracepoint = TRIBOOL_FALSE;
11779 }
11780
11781 if (can_download_tracepoint == TRIBOOL_FALSE)
11782 break;
11783
7ed2c994
YQ
11784 for (bl = b->loc; bl; bl = bl->next)
11785 {
11786 /* In tracepoint, locations are _never_ duplicated, so
11787 should_be_inserted is equivalent to
11788 unduplicated_should_be_inserted. */
11789 if (!should_be_inserted (bl) || bl->inserted)
11790 continue;
1e4d1764 11791
7ed2c994 11792 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11793
7ed2c994 11794 target_download_tracepoint (bl);
1e4d1764 11795
7ed2c994 11796 bl->inserted = 1;
f2a8bc8a 11797 bp_location_downloaded = 1;
7ed2c994
YQ
11798 }
11799 t = (struct tracepoint *) b;
11800 t->number_on_target = b->number;
f2a8bc8a
YQ
11801 if (bp_location_downloaded)
11802 observer_notify_breakpoint_modified (b);
1e4d1764 11803 }
1e4d1764
YQ
11804}
11805
934709f0
PW
11806/* Swap the insertion/duplication state between two locations. */
11807
11808static void
11809swap_insertion (struct bp_location *left, struct bp_location *right)
11810{
11811 const int left_inserted = left->inserted;
11812 const int left_duplicate = left->duplicate;
b775012e 11813 const int left_needs_update = left->needs_update;
934709f0
PW
11814 const struct bp_target_info left_target_info = left->target_info;
11815
1e4d1764
YQ
11816 /* Locations of tracepoints can never be duplicated. */
11817 if (is_tracepoint (left->owner))
11818 gdb_assert (!left->duplicate);
11819 if (is_tracepoint (right->owner))
11820 gdb_assert (!right->duplicate);
11821
934709f0
PW
11822 left->inserted = right->inserted;
11823 left->duplicate = right->duplicate;
b775012e 11824 left->needs_update = right->needs_update;
934709f0
PW
11825 left->target_info = right->target_info;
11826 right->inserted = left_inserted;
11827 right->duplicate = left_duplicate;
b775012e 11828 right->needs_update = left_needs_update;
934709f0
PW
11829 right->target_info = left_target_info;
11830}
11831
b775012e
LM
11832/* Force the re-insertion of the locations at ADDRESS. This is called
11833 once a new/deleted/modified duplicate location is found and we are evaluating
11834 conditions on the target's side. Such conditions need to be updated on
11835 the target. */
11836
11837static void
11838force_breakpoint_reinsertion (struct bp_location *bl)
11839{
11840 struct bp_location **locp = NULL, **loc2p;
11841 struct bp_location *loc;
11842 CORE_ADDR address = 0;
11843 int pspace_num;
11844
11845 address = bl->address;
11846 pspace_num = bl->pspace->num;
11847
11848 /* This is only meaningful if the target is
11849 evaluating conditions and if the user has
11850 opted for condition evaluation on the target's
11851 side. */
11852 if (gdb_evaluates_breakpoint_condition_p ()
11853 || !target_supports_evaluation_of_breakpoint_conditions ())
11854 return;
11855
11856 /* Flag all breakpoint locations with this address and
11857 the same program space as the location
11858 as "its condition has changed". We need to
11859 update the conditions on the target's side. */
11860 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11861 {
11862 loc = *loc2p;
11863
11864 if (!is_breakpoint (loc->owner)
11865 || pspace_num != loc->pspace->num)
11866 continue;
11867
11868 /* Flag the location appropriately. We use a different state to
11869 let everyone know that we already updated the set of locations
11870 with addr bl->address and program space bl->pspace. This is so
11871 we don't have to keep calling these functions just to mark locations
11872 that have already been marked. */
11873 loc->condition_changed = condition_updated;
11874
11875 /* Free the agent expression bytecode as well. We will compute
11876 it later on. */
833177a4 11877 loc->cond_bytecode.reset ();
b775012e
LM
11878 }
11879}
44702360
PA
11880/* Called whether new breakpoints are created, or existing breakpoints
11881 deleted, to update the global location list and recompute which
11882 locations are duplicate of which.
b775012e 11883
04086b45
PA
11884 The INSERT_MODE flag determines whether locations may not, may, or
11885 shall be inserted now. See 'enum ugll_insert_mode' for more
11886 info. */
b60e7edf 11887
0d381245 11888static void
44702360 11889update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11890{
74960c60 11891 struct breakpoint *b;
876fa593 11892 struct bp_location **locp, *loc;
b775012e
LM
11893 /* Last breakpoint location address that was marked for update. */
11894 CORE_ADDR last_addr = 0;
11895 /* Last breakpoint location program space that was marked for update. */
11896 int last_pspace_num = -1;
f7545552 11897
2d134ed3
PA
11898 /* Used in the duplicates detection below. When iterating over all
11899 bp_locations, points to the first bp_location of a given address.
11900 Breakpoints and watchpoints of different types are never
11901 duplicates of each other. Keep one pointer for each type of
11902 breakpoint/watchpoint, so we only need to loop over all locations
11903 once. */
11904 struct bp_location *bp_loc_first; /* breakpoint */
11905 struct bp_location *wp_loc_first; /* hardware watchpoint */
11906 struct bp_location *awp_loc_first; /* access watchpoint */
11907 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11908
f5336ca5
PA
11909 /* Saved former bp_locations array which we compare against the newly
11910 built bp_locations from the current state of ALL_BREAKPOINTS. */
81b1e71c 11911 struct bp_location **old_locp;
f5336ca5 11912 unsigned old_locations_count;
81b1e71c 11913 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
876fa593 11914
f5336ca5
PA
11915 old_locations_count = bp_locations_count;
11916 bp_locations = NULL;
11917 bp_locations_count = 0;
0d381245 11918
74960c60 11919 ALL_BREAKPOINTS (b)
876fa593 11920 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 11921 bp_locations_count++;
876fa593 11922
f5336ca5
PA
11923 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11924 locp = bp_locations;
876fa593
JK
11925 ALL_BREAKPOINTS (b)
11926 for (loc = b->loc; loc; loc = loc->next)
11927 *locp++ = loc;
f5336ca5
PA
11928 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
11929 bp_locations_compare);
876fa593 11930
f5336ca5 11931 bp_locations_target_extensions_update ();
74960c60 11932
4a64f543
MS
11933 /* Identify bp_location instances that are no longer present in the
11934 new list, and therefore should be freed. Note that it's not
11935 necessary that those locations should be removed from inferior --
11936 if there's another location at the same address (previously
11937 marked as duplicate), we don't need to remove/insert the
11938 location.
876fa593 11939
4a64f543
MS
11940 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11941 and former bp_location array state respectively. */
876fa593 11942
f5336ca5 11943 locp = bp_locations;
81b1e71c
TT
11944 for (old_locp = old_locations.get ();
11945 old_locp < old_locations.get () + old_locations_count;
876fa593 11946 old_locp++)
74960c60 11947 {
876fa593 11948 struct bp_location *old_loc = *old_locp;
c7d46a38 11949 struct bp_location **loc2p;
876fa593 11950
e5dd4106 11951 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11952 not, we have to free it. */
c7d46a38 11953 int found_object = 0;
20874c92
VP
11954 /* Tells if the location should remain inserted in the target. */
11955 int keep_in_target = 0;
11956 int removed = 0;
876fa593 11957
4a64f543
MS
11958 /* Skip LOCP entries which will definitely never be needed.
11959 Stop either at or being the one matching OLD_LOC. */
f5336ca5 11960 while (locp < bp_locations + bp_locations_count
c7d46a38 11961 && (*locp)->address < old_loc->address)
876fa593 11962 locp++;
c7d46a38
PA
11963
11964 for (loc2p = locp;
f5336ca5 11965 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
11966 && (*loc2p)->address == old_loc->address);
11967 loc2p++)
11968 {
b775012e
LM
11969 /* Check if this is a new/duplicated location or a duplicated
11970 location that had its condition modified. If so, we want to send
11971 its condition to the target if evaluation of conditions is taking
11972 place there. */
11973 if ((*loc2p)->condition_changed == condition_modified
11974 && (last_addr != old_loc->address
11975 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11976 {
b775012e
LM
11977 force_breakpoint_reinsertion (*loc2p);
11978 last_pspace_num = old_loc->pspace->num;
c7d46a38 11979 }
b775012e
LM
11980
11981 if (*loc2p == old_loc)
11982 found_object = 1;
c7d46a38 11983 }
74960c60 11984
b775012e
LM
11985 /* We have already handled this address, update it so that we don't
11986 have to go through updates again. */
11987 last_addr = old_loc->address;
11988
11989 /* Target-side condition evaluation: Handle deleted locations. */
11990 if (!found_object)
11991 force_breakpoint_reinsertion (old_loc);
11992
4a64f543
MS
11993 /* If this location is no longer present, and inserted, look if
11994 there's maybe a new location at the same address. If so,
11995 mark that one inserted, and don't remove this one. This is
11996 needed so that we don't have a time window where a breakpoint
11997 at certain location is not inserted. */
74960c60 11998
876fa593 11999 if (old_loc->inserted)
0d381245 12000 {
4a64f543
MS
12001 /* If the location is inserted now, we might have to remove
12002 it. */
74960c60 12003
876fa593 12004 if (found_object && should_be_inserted (old_loc))
74960c60 12005 {
4a64f543
MS
12006 /* The location is still present in the location list,
12007 and still should be inserted. Don't do anything. */
20874c92 12008 keep_in_target = 1;
74960c60
VP
12009 }
12010 else
12011 {
b775012e
LM
12012 /* This location still exists, but it won't be kept in the
12013 target since it may have been disabled. We proceed to
12014 remove its target-side condition. */
12015
4a64f543
MS
12016 /* The location is either no longer present, or got
12017 disabled. See if there's another location at the
12018 same address, in which case we don't need to remove
12019 this one from the target. */
876fa593 12020
2bdf28a0 12021 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12022 if (breakpoint_address_is_meaningful (old_loc->owner))
12023 {
876fa593 12024 for (loc2p = locp;
f5336ca5 12025 (loc2p < bp_locations + bp_locations_count
c7d46a38 12026 && (*loc2p)->address == old_loc->address);
876fa593
JK
12027 loc2p++)
12028 {
12029 struct bp_location *loc2 = *loc2p;
12030
2d134ed3 12031 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12032 {
85d721b8
PA
12033 /* Read watchpoint locations are switched to
12034 access watchpoints, if the former are not
12035 supported, but the latter are. */
12036 if (is_hardware_watchpoint (old_loc->owner))
12037 {
12038 gdb_assert (is_hardware_watchpoint (loc2->owner));
12039 loc2->watchpoint_type = old_loc->watchpoint_type;
12040 }
12041
934709f0
PW
12042 /* loc2 is a duplicated location. We need to check
12043 if it should be inserted in case it will be
12044 unduplicated. */
12045 if (loc2 != old_loc
12046 && unduplicated_should_be_inserted (loc2))
c7d46a38 12047 {
934709f0 12048 swap_insertion (old_loc, loc2);
c7d46a38
PA
12049 keep_in_target = 1;
12050 break;
12051 }
876fa593
JK
12052 }
12053 }
12054 }
74960c60
VP
12055 }
12056
20874c92
VP
12057 if (!keep_in_target)
12058 {
834c0d03 12059 if (remove_breakpoint (old_loc))
20874c92 12060 {
4a64f543
MS
12061 /* This is just about all we can do. We could keep
12062 this location on the global list, and try to
12063 remove it next time, but there's no particular
12064 reason why we will succeed next time.
20874c92 12065
4a64f543
MS
12066 Note that at this point, old_loc->owner is still
12067 valid, as delete_breakpoint frees the breakpoint
12068 only after calling us. */
3e43a32a
MS
12069 printf_filtered (_("warning: Error removing "
12070 "breakpoint %d\n"),
876fa593 12071 old_loc->owner->number);
20874c92
VP
12072 }
12073 removed = 1;
12074 }
0d381245 12075 }
74960c60
VP
12076
12077 if (!found_object)
1c5cfe86 12078 {
fbea99ea 12079 if (removed && target_is_non_stop_p ()
1cf4d951 12080 && need_moribund_for_location_type (old_loc))
20874c92 12081 {
db82e815
PA
12082 /* This location was removed from the target. In
12083 non-stop mode, a race condition is possible where
12084 we've removed a breakpoint, but stop events for that
12085 breakpoint are already queued and will arrive later.
12086 We apply an heuristic to be able to distinguish such
12087 SIGTRAPs from other random SIGTRAPs: we keep this
12088 breakpoint location for a bit, and will retire it
12089 after we see some number of events. The theory here
12090 is that reporting of events should, "on the average",
12091 be fair, so after a while we'll see events from all
12092 threads that have anything of interest, and no longer
12093 need to keep this breakpoint location around. We
12094 don't hold locations forever so to reduce chances of
12095 mistaking a non-breakpoint SIGTRAP for a breakpoint
12096 SIGTRAP.
12097
12098 The heuristic failing can be disastrous on
12099 decr_pc_after_break targets.
12100
12101 On decr_pc_after_break targets, like e.g., x86-linux,
12102 if we fail to recognize a late breakpoint SIGTRAP,
12103 because events_till_retirement has reached 0 too
12104 soon, we'll fail to do the PC adjustment, and report
12105 a random SIGTRAP to the user. When the user resumes
12106 the inferior, it will most likely immediately crash
2dec564e 12107 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12108 corrupted, because of being resumed e.g., in the
12109 middle of a multi-byte instruction, or skipped a
12110 one-byte instruction. This was actually seen happen
12111 on native x86-linux, and should be less rare on
12112 targets that do not support new thread events, like
12113 remote, due to the heuristic depending on
12114 thread_count.
12115
12116 Mistaking a random SIGTRAP for a breakpoint trap
12117 causes similar symptoms (PC adjustment applied when
12118 it shouldn't), but then again, playing with SIGTRAPs
12119 behind the debugger's back is asking for trouble.
12120
12121 Since hardware watchpoint traps are always
12122 distinguishable from other traps, so we don't need to
12123 apply keep hardware watchpoint moribund locations
12124 around. We simply always ignore hardware watchpoint
12125 traps we can no longer explain. */
12126
876fa593
JK
12127 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12128 old_loc->owner = NULL;
20874c92 12129
876fa593 12130 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12131 }
12132 else
f431efe5
PA
12133 {
12134 old_loc->owner = NULL;
12135 decref_bp_location (&old_loc);
12136 }
20874c92 12137 }
74960c60 12138 }
1c5cfe86 12139
348d480f
PA
12140 /* Rescan breakpoints at the same address and section, marking the
12141 first one as "first" and any others as "duplicates". This is so
12142 that the bpt instruction is only inserted once. If we have a
12143 permanent breakpoint at the same place as BPT, make that one the
12144 official one, and the rest as duplicates. Permanent breakpoints
12145 are sorted first for the same address.
12146
12147 Do the same for hardware watchpoints, but also considering the
12148 watchpoint's type (regular/access/read) and length. */
12149
12150 bp_loc_first = NULL;
12151 wp_loc_first = NULL;
12152 awp_loc_first = NULL;
12153 rwp_loc_first = NULL;
12154 ALL_BP_LOCATIONS (loc, locp)
12155 {
12156 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12157 non-NULL. */
348d480f 12158 struct bp_location **loc_first_p;
d3fbdd86 12159 b = loc->owner;
348d480f 12160
6f380991 12161 if (!unduplicated_should_be_inserted (loc)
348d480f 12162 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12163 /* Don't detect duplicate for tracepoint locations because they are
12164 never duplicated. See the comments in field `duplicate' of
12165 `struct bp_location'. */
348d480f 12166 || is_tracepoint (b))
b775012e
LM
12167 {
12168 /* Clear the condition modification flag. */
12169 loc->condition_changed = condition_unchanged;
12170 continue;
12171 }
348d480f 12172
348d480f
PA
12173 if (b->type == bp_hardware_watchpoint)
12174 loc_first_p = &wp_loc_first;
12175 else if (b->type == bp_read_watchpoint)
12176 loc_first_p = &rwp_loc_first;
12177 else if (b->type == bp_access_watchpoint)
12178 loc_first_p = &awp_loc_first;
12179 else
12180 loc_first_p = &bp_loc_first;
12181
12182 if (*loc_first_p == NULL
12183 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12184 || !breakpoint_locations_match (loc, *loc_first_p))
12185 {
12186 *loc_first_p = loc;
12187 loc->duplicate = 0;
b775012e
LM
12188
12189 if (is_breakpoint (loc->owner) && loc->condition_changed)
12190 {
12191 loc->needs_update = 1;
12192 /* Clear the condition modification flag. */
12193 loc->condition_changed = condition_unchanged;
12194 }
348d480f
PA
12195 continue;
12196 }
12197
934709f0
PW
12198
12199 /* This and the above ensure the invariant that the first location
12200 is not duplicated, and is the inserted one.
12201 All following are marked as duplicated, and are not inserted. */
12202 if (loc->inserted)
12203 swap_insertion (loc, *loc_first_p);
348d480f
PA
12204 loc->duplicate = 1;
12205
b775012e
LM
12206 /* Clear the condition modification flag. */
12207 loc->condition_changed = condition_unchanged;
348d480f
PA
12208 }
12209
a25a5a45 12210 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12211 {
04086b45 12212 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12213 insert_breakpoint_locations ();
12214 else
12215 {
44702360
PA
12216 /* Even though the caller told us to not insert new
12217 locations, we may still need to update conditions on the
12218 target's side of breakpoints that were already inserted
12219 if the target is evaluating breakpoint conditions. We
b775012e
LM
12220 only update conditions for locations that are marked
12221 "needs_update". */
12222 update_inserted_breakpoint_locations ();
12223 }
12224 }
348d480f 12225
04086b45 12226 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12227 download_tracepoint_locations ();
348d480f
PA
12228}
12229
12230void
12231breakpoint_retire_moribund (void)
12232{
12233 struct bp_location *loc;
12234 int ix;
12235
12236 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12237 if (--(loc->events_till_retirement) == 0)
12238 {
12239 decref_bp_location (&loc);
12240 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12241 --ix;
12242 }
12243}
12244
12245static void
44702360 12246update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12247{
348d480f 12248
492d29ea
PA
12249 TRY
12250 {
12251 update_global_location_list (insert_mode);
12252 }
12253 CATCH (e, RETURN_MASK_ERROR)
12254 {
12255 }
12256 END_CATCH
348d480f
PA
12257}
12258
12259/* Clear BKP from a BPS. */
12260
12261static void
12262bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12263{
12264 bpstat bs;
12265
12266 for (bs = bps; bs; bs = bs->next)
12267 if (bs->breakpoint_at == bpt)
12268 {
12269 bs->breakpoint_at = NULL;
12270 bs->old_val = NULL;
12271 /* bs->commands will be freed later. */
12272 }
12273}
12274
12275/* Callback for iterate_over_threads. */
12276static int
12277bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12278{
9a3c8263 12279 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12280
12281 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12282 return 0;
12283}
12284
12285/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12286 callbacks. */
12287
12288static void
12289say_where (struct breakpoint *b)
12290{
12291 struct value_print_options opts;
12292
12293 get_user_print_options (&opts);
12294
12295 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12296 single string. */
12297 if (b->loc == NULL)
12298 {
f00aae0f
KS
12299 /* For pending locations, the output differs slightly based
12300 on b->extra_string. If this is non-NULL, it contains either
12301 a condition or dprintf arguments. */
12302 if (b->extra_string == NULL)
12303 {
12304 printf_filtered (_(" (%s) pending."),
d28cd78a 12305 event_location_to_string (b->location.get ()));
f00aae0f
KS
12306 }
12307 else if (b->type == bp_dprintf)
12308 {
12309 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12310 event_location_to_string (b->location.get ()),
f00aae0f
KS
12311 b->extra_string);
12312 }
12313 else
12314 {
12315 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12316 event_location_to_string (b->location.get ()),
f00aae0f
KS
12317 b->extra_string);
12318 }
348d480f
PA
12319 }
12320 else
12321 {
2f202fde 12322 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12323 {
12324 printf_filtered (" at ");
12325 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12326 gdb_stdout);
12327 }
2f202fde 12328 if (b->loc->symtab != NULL)
f8eba3c6
TT
12329 {
12330 /* If there is a single location, we can print the location
12331 more nicely. */
12332 if (b->loc->next == NULL)
12333 printf_filtered (": file %s, line %d.",
05cba821
JK
12334 symtab_to_filename_for_display (b->loc->symtab),
12335 b->loc->line_number);
f8eba3c6
TT
12336 else
12337 /* This is not ideal, but each location may have a
12338 different file name, and this at least reflects the
12339 real situation somewhat. */
f00aae0f 12340 printf_filtered (": %s.",
d28cd78a 12341 event_location_to_string (b->location.get ()));
f8eba3c6 12342 }
348d480f
PA
12343
12344 if (b->loc->next)
12345 {
12346 struct bp_location *loc = b->loc;
12347 int n = 0;
12348 for (; loc; loc = loc->next)
12349 ++n;
12350 printf_filtered (" (%d locations)", n);
12351 }
12352 }
12353}
12354
348d480f
PA
12355/* Default bp_location_ops methods. */
12356
12357static void
12358bp_location_dtor (struct bp_location *self)
12359{
348d480f
PA
12360 xfree (self->function_name);
12361}
12362
12363static const struct bp_location_ops bp_location_ops =
12364{
12365 bp_location_dtor
12366};
12367
c1fc2657 12368/* Destructor for the breakpoint base class. */
348d480f 12369
c1fc2657 12370breakpoint::~breakpoint ()
348d480f 12371{
c1fc2657
SM
12372 xfree (this->cond_string);
12373 xfree (this->extra_string);
12374 xfree (this->filter);
348d480f
PA
12375}
12376
2060206e
PA
12377static struct bp_location *
12378base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12379{
5625a286 12380 return new bp_location (&bp_location_ops, self);
348d480f
PA
12381}
12382
2060206e
PA
12383static void
12384base_breakpoint_re_set (struct breakpoint *b)
12385{
12386 /* Nothing to re-set. */
12387}
12388
12389#define internal_error_pure_virtual_called() \
12390 gdb_assert_not_reached ("pure virtual function called")
12391
12392static int
12393base_breakpoint_insert_location (struct bp_location *bl)
12394{
12395 internal_error_pure_virtual_called ();
12396}
12397
12398static int
73971819
PA
12399base_breakpoint_remove_location (struct bp_location *bl,
12400 enum remove_bp_reason reason)
2060206e
PA
12401{
12402 internal_error_pure_virtual_called ();
12403}
12404
12405static int
12406base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12407 const address_space *aspace,
09ac7c10
TT
12408 CORE_ADDR bp_addr,
12409 const struct target_waitstatus *ws)
2060206e
PA
12410{
12411 internal_error_pure_virtual_called ();
12412}
12413
12414static void
12415base_breakpoint_check_status (bpstat bs)
12416{
12417 /* Always stop. */
12418}
12419
12420/* A "works_in_software_mode" breakpoint_ops method that just internal
12421 errors. */
12422
12423static int
12424base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12425{
12426 internal_error_pure_virtual_called ();
12427}
12428
12429/* A "resources_needed" breakpoint_ops method that just internal
12430 errors. */
12431
12432static int
12433base_breakpoint_resources_needed (const struct bp_location *bl)
12434{
12435 internal_error_pure_virtual_called ();
12436}
12437
12438static enum print_stop_action
12439base_breakpoint_print_it (bpstat bs)
12440{
12441 internal_error_pure_virtual_called ();
12442}
12443
12444static void
12445base_breakpoint_print_one_detail (const struct breakpoint *self,
12446 struct ui_out *uiout)
12447{
12448 /* nothing */
12449}
12450
12451static void
12452base_breakpoint_print_mention (struct breakpoint *b)
12453{
12454 internal_error_pure_virtual_called ();
12455}
12456
12457static void
12458base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12459{
12460 internal_error_pure_virtual_called ();
12461}
12462
983af33b 12463static void
f00aae0f
KS
12464base_breakpoint_create_sals_from_location
12465 (const struct event_location *location,
12466 struct linespec_result *canonical,
12467 enum bptype type_wanted)
983af33b
SDJ
12468{
12469 internal_error_pure_virtual_called ();
12470}
12471
12472static void
12473base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12474 struct linespec_result *c,
e1e01040
PA
12475 gdb::unique_xmalloc_ptr<char> cond_string,
12476 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12477 enum bptype type_wanted,
12478 enum bpdisp disposition,
12479 int thread,
12480 int task, int ignore_count,
12481 const struct breakpoint_ops *o,
12482 int from_tty, int enabled,
44f238bb 12483 int internal, unsigned flags)
983af33b
SDJ
12484{
12485 internal_error_pure_virtual_called ();
12486}
12487
6c5b2ebe 12488static std::vector<symtab_and_line>
f00aae0f
KS
12489base_breakpoint_decode_location (struct breakpoint *b,
12490 const struct event_location *location,
6c5b2ebe 12491 struct program_space *search_pspace)
983af33b
SDJ
12492{
12493 internal_error_pure_virtual_called ();
12494}
12495
ab04a2af
TT
12496/* The default 'explains_signal' method. */
12497
47591c29 12498static int
427cd150 12499base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12500{
47591c29 12501 return 1;
ab04a2af
TT
12502}
12503
9d6e6e84
HZ
12504/* The default "after_condition_true" method. */
12505
12506static void
12507base_breakpoint_after_condition_true (struct bpstats *bs)
12508{
12509 /* Nothing to do. */
12510}
12511
ab04a2af 12512struct breakpoint_ops base_breakpoint_ops =
2060206e 12513{
2060206e
PA
12514 base_breakpoint_allocate_location,
12515 base_breakpoint_re_set,
12516 base_breakpoint_insert_location,
12517 base_breakpoint_remove_location,
12518 base_breakpoint_breakpoint_hit,
12519 base_breakpoint_check_status,
12520 base_breakpoint_resources_needed,
12521 base_breakpoint_works_in_software_mode,
12522 base_breakpoint_print_it,
12523 NULL,
12524 base_breakpoint_print_one_detail,
12525 base_breakpoint_print_mention,
983af33b 12526 base_breakpoint_print_recreate,
5f700d83 12527 base_breakpoint_create_sals_from_location,
983af33b 12528 base_breakpoint_create_breakpoints_sal,
5f700d83 12529 base_breakpoint_decode_location,
9d6e6e84
HZ
12530 base_breakpoint_explains_signal,
12531 base_breakpoint_after_condition_true,
2060206e
PA
12532};
12533
12534/* Default breakpoint_ops methods. */
12535
12536static void
348d480f
PA
12537bkpt_re_set (struct breakpoint *b)
12538{
06edf0c0 12539 /* FIXME: is this still reachable? */
9ef9e6a6 12540 if (breakpoint_event_location_empty_p (b))
06edf0c0 12541 {
f00aae0f 12542 /* Anything without a location can't be re-set. */
348d480f 12543 delete_breakpoint (b);
06edf0c0 12544 return;
348d480f 12545 }
06edf0c0
PA
12546
12547 breakpoint_re_set_default (b);
348d480f
PA
12548}
12549
2060206e 12550static int
348d480f
PA
12551bkpt_insert_location (struct bp_location *bl)
12552{
cd6c3b4f
YQ
12553 CORE_ADDR addr = bl->target_info.reqstd_address;
12554
579c6ad9 12555 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12556 bl->target_info.placed_address = addr;
12557
348d480f 12558 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12559 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12560 else
7c16b83e 12561 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12562}
12563
2060206e 12564static int
73971819 12565bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12566{
12567 if (bl->loc_type == bp_loc_hardware_breakpoint)
12568 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12569 else
73971819 12570 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12571}
12572
2060206e 12573static int
348d480f 12574bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12575 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12576 const struct target_waitstatus *ws)
348d480f 12577{
09ac7c10 12578 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12579 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12580 return 0;
12581
348d480f
PA
12582 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12583 aspace, bp_addr))
12584 return 0;
12585
12586 if (overlay_debugging /* unmapped overlay section */
12587 && section_is_overlay (bl->section)
12588 && !section_is_mapped (bl->section))
12589 return 0;
12590
12591 return 1;
12592}
12593
cd1608cc
PA
12594static int
12595dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12596 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12597 const struct target_waitstatus *ws)
12598{
12599 if (dprintf_style == dprintf_style_agent
12600 && target_can_run_breakpoint_commands ())
12601 {
12602 /* An agent-style dprintf never causes a stop. If we see a trap
12603 for this address it must be for a breakpoint that happens to
12604 be set at the same address. */
12605 return 0;
12606 }
12607
12608 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12609}
12610
2060206e 12611static int
348d480f
PA
12612bkpt_resources_needed (const struct bp_location *bl)
12613{
12614 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12615
12616 return 1;
12617}
12618
2060206e 12619static enum print_stop_action
348d480f
PA
12620bkpt_print_it (bpstat bs)
12621{
348d480f
PA
12622 struct breakpoint *b;
12623 const struct bp_location *bl;
001c8c33 12624 int bp_temp;
79a45e25 12625 struct ui_out *uiout = current_uiout;
348d480f
PA
12626
12627 gdb_assert (bs->bp_location_at != NULL);
12628
12629 bl = bs->bp_location_at;
12630 b = bs->breakpoint_at;
12631
001c8c33
PA
12632 bp_temp = b->disposition == disp_del;
12633 if (bl->address != bl->requested_address)
12634 breakpoint_adjustment_warning (bl->requested_address,
12635 bl->address,
12636 b->number, 1);
12637 annotate_breakpoint (b->number);
f303dbd6
PA
12638 maybe_print_thread_hit_breakpoint (uiout);
12639
001c8c33 12640 if (bp_temp)
112e8700 12641 uiout->text ("Temporary breakpoint ");
001c8c33 12642 else
112e8700
SM
12643 uiout->text ("Breakpoint ");
12644 if (uiout->is_mi_like_p ())
348d480f 12645 {
112e8700 12646 uiout->field_string ("reason",
001c8c33 12647 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12648 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12649 }
112e8700
SM
12650 uiout->field_int ("bkptno", b->number);
12651 uiout->text (", ");
06edf0c0 12652
001c8c33 12653 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12654}
12655
2060206e 12656static void
06edf0c0
PA
12657bkpt_print_mention (struct breakpoint *b)
12658{
112e8700 12659 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12660 return;
12661
12662 switch (b->type)
12663 {
12664 case bp_breakpoint:
12665 case bp_gnu_ifunc_resolver:
12666 if (b->disposition == disp_del)
12667 printf_filtered (_("Temporary breakpoint"));
12668 else
12669 printf_filtered (_("Breakpoint"));
12670 printf_filtered (_(" %d"), b->number);
12671 if (b->type == bp_gnu_ifunc_resolver)
12672 printf_filtered (_(" at gnu-indirect-function resolver"));
12673 break;
12674 case bp_hardware_breakpoint:
12675 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12676 break;
e7e0cddf
SS
12677 case bp_dprintf:
12678 printf_filtered (_("Dprintf %d"), b->number);
12679 break;
06edf0c0
PA
12680 }
12681
12682 say_where (b);
12683}
12684
2060206e 12685static void
06edf0c0
PA
12686bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12687{
12688 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12689 fprintf_unfiltered (fp, "tbreak");
12690 else if (tp->type == bp_breakpoint)
12691 fprintf_unfiltered (fp, "break");
12692 else if (tp->type == bp_hardware_breakpoint
12693 && tp->disposition == disp_del)
12694 fprintf_unfiltered (fp, "thbreak");
12695 else if (tp->type == bp_hardware_breakpoint)
12696 fprintf_unfiltered (fp, "hbreak");
12697 else
12698 internal_error (__FILE__, __LINE__,
12699 _("unhandled breakpoint type %d"), (int) tp->type);
12700
f00aae0f 12701 fprintf_unfiltered (fp, " %s",
d28cd78a 12702 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12703
12704 /* Print out extra_string if this breakpoint is pending. It might
12705 contain, for example, conditions that were set by the user. */
12706 if (tp->loc == NULL && tp->extra_string != NULL)
12707 fprintf_unfiltered (fp, " %s", tp->extra_string);
12708
dd11a36c 12709 print_recreate_thread (tp, fp);
06edf0c0
PA
12710}
12711
983af33b 12712static void
f00aae0f
KS
12713bkpt_create_sals_from_location (const struct event_location *location,
12714 struct linespec_result *canonical,
12715 enum bptype type_wanted)
983af33b 12716{
f00aae0f 12717 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12718}
12719
12720static void
12721bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12722 struct linespec_result *canonical,
e1e01040
PA
12723 gdb::unique_xmalloc_ptr<char> cond_string,
12724 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12725 enum bptype type_wanted,
12726 enum bpdisp disposition,
12727 int thread,
12728 int task, int ignore_count,
12729 const struct breakpoint_ops *ops,
12730 int from_tty, int enabled,
44f238bb 12731 int internal, unsigned flags)
983af33b 12732{
023fa29b 12733 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12734 std::move (cond_string),
12735 std::move (extra_string),
e7e0cddf 12736 type_wanted,
983af33b
SDJ
12737 disposition, thread, task,
12738 ignore_count, ops, from_tty,
44f238bb 12739 enabled, internal, flags);
983af33b
SDJ
12740}
12741
6c5b2ebe 12742static std::vector<symtab_and_line>
f00aae0f
KS
12743bkpt_decode_location (struct breakpoint *b,
12744 const struct event_location *location,
6c5b2ebe 12745 struct program_space *search_pspace)
983af33b 12746{
6c5b2ebe 12747 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12748}
12749
06edf0c0
PA
12750/* Virtual table for internal breakpoints. */
12751
12752static void
12753internal_bkpt_re_set (struct breakpoint *b)
12754{
12755 switch (b->type)
12756 {
12757 /* Delete overlay event and longjmp master breakpoints; they
12758 will be reset later by breakpoint_re_set. */
12759 case bp_overlay_event:
12760 case bp_longjmp_master:
12761 case bp_std_terminate_master:
12762 case bp_exception_master:
12763 delete_breakpoint (b);
12764 break;
12765
12766 /* This breakpoint is special, it's set up when the inferior
12767 starts and we really don't want to touch it. */
12768 case bp_shlib_event:
12769
12770 /* Like bp_shlib_event, this breakpoint type is special. Once
12771 it is set up, we do not want to touch it. */
12772 case bp_thread_event:
12773 break;
12774 }
12775}
12776
12777static void
12778internal_bkpt_check_status (bpstat bs)
12779{
a9b3a50f
PA
12780 if (bs->breakpoint_at->type == bp_shlib_event)
12781 {
12782 /* If requested, stop when the dynamic linker notifies GDB of
12783 events. This allows the user to get control and place
12784 breakpoints in initializer routines for dynamically loaded
12785 objects (among other things). */
12786 bs->stop = stop_on_solib_events;
12787 bs->print = stop_on_solib_events;
12788 }
12789 else
12790 bs->stop = 0;
06edf0c0
PA
12791}
12792
12793static enum print_stop_action
12794internal_bkpt_print_it (bpstat bs)
12795{
06edf0c0 12796 struct breakpoint *b;
06edf0c0 12797
06edf0c0
PA
12798 b = bs->breakpoint_at;
12799
06edf0c0
PA
12800 switch (b->type)
12801 {
348d480f
PA
12802 case bp_shlib_event:
12803 /* Did we stop because the user set the stop_on_solib_events
12804 variable? (If so, we report this as a generic, "Stopped due
12805 to shlib event" message.) */
edcc5120 12806 print_solib_event (0);
348d480f
PA
12807 break;
12808
12809 case bp_thread_event:
12810 /* Not sure how we will get here.
12811 GDB should not stop for these breakpoints. */
12812 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12813 break;
12814
12815 case bp_overlay_event:
12816 /* By analogy with the thread event, GDB should not stop for these. */
12817 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12818 break;
12819
12820 case bp_longjmp_master:
12821 /* These should never be enabled. */
12822 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12823 break;
12824
12825 case bp_std_terminate_master:
12826 /* These should never be enabled. */
12827 printf_filtered (_("std::terminate Master Breakpoint: "
12828 "gdb should not stop!\n"));
348d480f
PA
12829 break;
12830
12831 case bp_exception_master:
12832 /* These should never be enabled. */
12833 printf_filtered (_("Exception Master Breakpoint: "
12834 "gdb should not stop!\n"));
06edf0c0
PA
12835 break;
12836 }
12837
001c8c33 12838 return PRINT_NOTHING;
06edf0c0
PA
12839}
12840
12841static void
12842internal_bkpt_print_mention (struct breakpoint *b)
12843{
12844 /* Nothing to mention. These breakpoints are internal. */
12845}
12846
06edf0c0
PA
12847/* Virtual table for momentary breakpoints */
12848
12849static void
12850momentary_bkpt_re_set (struct breakpoint *b)
12851{
12852 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12853 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12854 Otherwise these should have been blown away via the cleanup chain
12855 or by breakpoint_init_inferior when we rerun the executable. */
12856}
12857
12858static void
12859momentary_bkpt_check_status (bpstat bs)
12860{
12861 /* Nothing. The point of these breakpoints is causing a stop. */
12862}
12863
12864static enum print_stop_action
12865momentary_bkpt_print_it (bpstat bs)
12866{
001c8c33 12867 return PRINT_UNKNOWN;
348d480f
PA
12868}
12869
06edf0c0
PA
12870static void
12871momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12872{
06edf0c0 12873 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12874}
12875
e2e4d78b
JK
12876/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12877
12878 It gets cleared already on the removal of the first one of such placed
12879 breakpoints. This is OK as they get all removed altogether. */
12880
c1fc2657 12881longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12882{
c1fc2657 12883 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12884
c1fc2657 12885 if (tp != NULL)
e2e4d78b 12886 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12887}
12888
55aa24fb
SDJ
12889/* Specific methods for probe breakpoints. */
12890
12891static int
12892bkpt_probe_insert_location (struct bp_location *bl)
12893{
12894 int v = bkpt_insert_location (bl);
12895
12896 if (v == 0)
12897 {
12898 /* The insertion was successful, now let's set the probe's semaphore
12899 if needed. */
0ea5cda8
SDJ
12900 if (bl->probe.probe->pops->set_semaphore != NULL)
12901 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
12902 bl->probe.objfile,
12903 bl->gdbarch);
55aa24fb
SDJ
12904 }
12905
12906 return v;
12907}
12908
12909static int
73971819
PA
12910bkpt_probe_remove_location (struct bp_location *bl,
12911 enum remove_bp_reason reason)
55aa24fb
SDJ
12912{
12913 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
12914 if (bl->probe.probe->pops->clear_semaphore != NULL)
12915 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
12916 bl->probe.objfile,
12917 bl->gdbarch);
55aa24fb 12918
73971819 12919 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12920}
12921
12922static void
f00aae0f 12923bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 12924 struct linespec_result *canonical,
f00aae0f 12925 enum bptype type_wanted)
55aa24fb
SDJ
12926{
12927 struct linespec_sals lsal;
12928
c2f4122d 12929 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12930 lsal.canonical
12931 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12932 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12933}
12934
6c5b2ebe 12935static std::vector<symtab_and_line>
f00aae0f
KS
12936bkpt_probe_decode_location (struct breakpoint *b,
12937 const struct event_location *location,
6c5b2ebe 12938 struct program_space *search_pspace)
55aa24fb 12939{
6c5b2ebe
PA
12940 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12941 if (sals.empty ())
55aa24fb 12942 error (_("probe not found"));
6c5b2ebe 12943 return sals;
55aa24fb
SDJ
12944}
12945
348d480f 12946/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12947
348d480f
PA
12948static void
12949tracepoint_re_set (struct breakpoint *b)
12950{
12951 breakpoint_re_set_default (b);
12952}
876fa593 12953
348d480f
PA
12954static int
12955tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12956 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12957 const struct target_waitstatus *ws)
348d480f
PA
12958{
12959 /* By definition, the inferior does not report stops at
12960 tracepoints. */
12961 return 0;
74960c60
VP
12962}
12963
12964static void
348d480f
PA
12965tracepoint_print_one_detail (const struct breakpoint *self,
12966 struct ui_out *uiout)
74960c60 12967{
d9b3f62e
PA
12968 struct tracepoint *tp = (struct tracepoint *) self;
12969 if (tp->static_trace_marker_id)
348d480f
PA
12970 {
12971 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12972
112e8700
SM
12973 uiout->text ("\tmarker id is ");
12974 uiout->field_string ("static-tracepoint-marker-string-id",
d9b3f62e 12975 tp->static_trace_marker_id);
112e8700 12976 uiout->text ("\n");
348d480f 12977 }
0d381245
VP
12978}
12979
a474d7c2 12980static void
348d480f 12981tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12982{
112e8700 12983 if (current_uiout->is_mi_like_p ())
348d480f 12984 return;
cc59ec59 12985
348d480f
PA
12986 switch (b->type)
12987 {
12988 case bp_tracepoint:
12989 printf_filtered (_("Tracepoint"));
12990 printf_filtered (_(" %d"), b->number);
12991 break;
12992 case bp_fast_tracepoint:
12993 printf_filtered (_("Fast tracepoint"));
12994 printf_filtered (_(" %d"), b->number);
12995 break;
12996 case bp_static_tracepoint:
12997 printf_filtered (_("Static tracepoint"));
12998 printf_filtered (_(" %d"), b->number);
12999 break;
13000 default:
13001 internal_error (__FILE__, __LINE__,
13002 _("unhandled tracepoint type %d"), (int) b->type);
13003 }
13004
13005 say_where (b);
a474d7c2
PA
13006}
13007
348d480f 13008static void
d9b3f62e 13009tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13010{
d9b3f62e
PA
13011 struct tracepoint *tp = (struct tracepoint *) self;
13012
13013 if (self->type == bp_fast_tracepoint)
348d480f 13014 fprintf_unfiltered (fp, "ftrace");
c93e8391 13015 else if (self->type == bp_static_tracepoint)
348d480f 13016 fprintf_unfiltered (fp, "strace");
d9b3f62e 13017 else if (self->type == bp_tracepoint)
348d480f
PA
13018 fprintf_unfiltered (fp, "trace");
13019 else
13020 internal_error (__FILE__, __LINE__,
d9b3f62e 13021 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13022
f00aae0f 13023 fprintf_unfiltered (fp, " %s",
d28cd78a 13024 event_location_to_string (self->location.get ()));
d9b3f62e
PA
13025 print_recreate_thread (self, fp);
13026
13027 if (tp->pass_count)
13028 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13029}
13030
983af33b 13031static void
f00aae0f
KS
13032tracepoint_create_sals_from_location (const struct event_location *location,
13033 struct linespec_result *canonical,
13034 enum bptype type_wanted)
983af33b 13035{
f00aae0f 13036 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13037}
13038
13039static void
13040tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13041 struct linespec_result *canonical,
e1e01040
PA
13042 gdb::unique_xmalloc_ptr<char> cond_string,
13043 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13044 enum bptype type_wanted,
13045 enum bpdisp disposition,
13046 int thread,
13047 int task, int ignore_count,
13048 const struct breakpoint_ops *ops,
13049 int from_tty, int enabled,
44f238bb 13050 int internal, unsigned flags)
983af33b 13051{
023fa29b 13052 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13053 std::move (cond_string),
13054 std::move (extra_string),
e7e0cddf 13055 type_wanted,
983af33b
SDJ
13056 disposition, thread, task,
13057 ignore_count, ops, from_tty,
44f238bb 13058 enabled, internal, flags);
983af33b
SDJ
13059}
13060
6c5b2ebe 13061static std::vector<symtab_and_line>
f00aae0f
KS
13062tracepoint_decode_location (struct breakpoint *b,
13063 const struct event_location *location,
6c5b2ebe 13064 struct program_space *search_pspace)
983af33b 13065{
6c5b2ebe 13066 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
13067}
13068
2060206e 13069struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13070
55aa24fb
SDJ
13071/* The breakpoint_ops structure to be use on tracepoints placed in a
13072 static probe. */
13073
13074static void
f00aae0f
KS
13075tracepoint_probe_create_sals_from_location
13076 (const struct event_location *location,
13077 struct linespec_result *canonical,
13078 enum bptype type_wanted)
55aa24fb
SDJ
13079{
13080 /* We use the same method for breakpoint on probes. */
f00aae0f 13081 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13082}
13083
6c5b2ebe 13084static std::vector<symtab_and_line>
f00aae0f
KS
13085tracepoint_probe_decode_location (struct breakpoint *b,
13086 const struct event_location *location,
6c5b2ebe 13087 struct program_space *search_pspace)
55aa24fb
SDJ
13088{
13089 /* We use the same method for breakpoint on probes. */
6c5b2ebe 13090 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
13091}
13092
13093static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13094
5c2b4418
HZ
13095/* Dprintf breakpoint_ops methods. */
13096
13097static void
13098dprintf_re_set (struct breakpoint *b)
13099{
13100 breakpoint_re_set_default (b);
13101
f00aae0f
KS
13102 /* extra_string should never be non-NULL for dprintf. */
13103 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13104
13105 /* 1 - connect to target 1, that can run breakpoint commands.
13106 2 - create a dprintf, which resolves fine.
13107 3 - disconnect from target 1
13108 4 - connect to target 2, that can NOT run breakpoint commands.
13109
13110 After steps #3/#4, you'll want the dprintf command list to
13111 be updated, because target 1 and 2 may well return different
13112 answers for target_can_run_breakpoint_commands().
13113 Given absence of finer grained resetting, we get to do
13114 it all the time. */
13115 if (b->extra_string != NULL)
13116 update_dprintf_command_list (b);
13117}
13118
2d9442cc
HZ
13119/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13120
13121static void
13122dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13123{
f00aae0f 13124 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13125 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13126 tp->extra_string);
13127 print_recreate_thread (tp, fp);
13128}
13129
9d6e6e84
HZ
13130/* Implement the "after_condition_true" breakpoint_ops method for
13131 dprintf.
13132
13133 dprintf's are implemented with regular commands in their command
13134 list, but we run the commands here instead of before presenting the
13135 stop to the user, as dprintf's don't actually cause a stop. This
13136 also makes it so that the commands of multiple dprintfs at the same
13137 address are all handled. */
13138
13139static void
13140dprintf_after_condition_true (struct bpstats *bs)
13141{
04afa70c 13142 struct bpstats tmp_bs;
9d6e6e84
HZ
13143 struct bpstats *tmp_bs_p = &tmp_bs;
13144
13145 /* dprintf's never cause a stop. This wasn't set in the
13146 check_status hook instead because that would make the dprintf's
13147 condition not be evaluated. */
13148 bs->stop = 0;
13149
13150 /* Run the command list here. Take ownership of it instead of
13151 copying. We never want these commands to run later in
13152 bpstat_do_actions, if a breakpoint that causes a stop happens to
13153 be set at same address as this dprintf, or even if running the
13154 commands here throws. */
13155 tmp_bs.commands = bs->commands;
13156 bs->commands = NULL;
9d6e6e84
HZ
13157
13158 bpstat_do_actions_1 (&tmp_bs_p);
13159
13160 /* 'tmp_bs.commands' will usually be NULL by now, but
13161 bpstat_do_actions_1 may return early without processing the whole
13162 list. */
9d6e6e84
HZ
13163}
13164
983af33b
SDJ
13165/* The breakpoint_ops structure to be used on static tracepoints with
13166 markers (`-m'). */
13167
13168static void
f00aae0f 13169strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13170 struct linespec_result *canonical,
f00aae0f 13171 enum bptype type_wanted)
983af33b
SDJ
13172{
13173 struct linespec_sals lsal;
f00aae0f 13174 const char *arg_start, *arg;
983af33b 13175
f00aae0f
KS
13176 arg = arg_start = get_linespec_location (location);
13177 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13178
f2fc3015
TT
13179 std::string str (arg_start, arg - arg_start);
13180 const char *ptr = str.c_str ();
13181 canonical->location = new_linespec_location (&ptr);
983af33b 13182
8e9e35b1
TT
13183 lsal.canonical
13184 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13185 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
13186}
13187
13188static void
13189strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13190 struct linespec_result *canonical,
e1e01040
PA
13191 gdb::unique_xmalloc_ptr<char> cond_string,
13192 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13193 enum bptype type_wanted,
13194 enum bpdisp disposition,
13195 int thread,
13196 int task, int ignore_count,
13197 const struct breakpoint_ops *ops,
13198 int from_tty, int enabled,
44f238bb 13199 int internal, unsigned flags)
983af33b 13200{
6c5b2ebe 13201 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13202
13203 /* If the user is creating a static tracepoint by marker id
13204 (strace -m MARKER_ID), then store the sals index, so that
13205 breakpoint_re_set can try to match up which of the newly
13206 found markers corresponds to this one, and, don't try to
13207 expand multiple locations for each sal, given than SALS
13208 already should contain all sals for MARKER_ID. */
13209
6c5b2ebe 13210 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13211 {
6c5b2ebe
PA
13212 event_location_up location
13213 = copy_event_location (canonical->location.get ());
983af33b 13214
b270e6f9 13215 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13216 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13217 std::move (location), NULL,
e1e01040
PA
13218 std::move (cond_string),
13219 std::move (extra_string),
e7e0cddf 13220 type_wanted, disposition,
983af33b 13221 thread, task, ignore_count, ops,
44f238bb 13222 from_tty, enabled, internal, flags,
983af33b
SDJ
13223 canonical->special_display);
13224 /* Given that its possible to have multiple markers with
13225 the same string id, if the user is creating a static
13226 tracepoint by marker id ("strace -m MARKER_ID"), then
13227 store the sals index, so that breakpoint_re_set can
13228 try to match up which of the newly found markers
13229 corresponds to this one */
13230 tp->static_trace_marker_id_idx = i;
13231
b270e6f9 13232 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13233 }
13234}
13235
6c5b2ebe 13236static std::vector<symtab_and_line>
f00aae0f
KS
13237strace_marker_decode_location (struct breakpoint *b,
13238 const struct event_location *location,
6c5b2ebe 13239 struct program_space *search_pspace)
983af33b
SDJ
13240{
13241 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13242 const char *s = get_linespec_location (location);
983af33b 13243
6c5b2ebe
PA
13244 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13245 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13246 {
6c5b2ebe
PA
13247 sals[0] = sals[tp->static_trace_marker_id_idx];
13248 sals.resize (1);
13249 return sals;
983af33b
SDJ
13250 }
13251 else
13252 error (_("marker %s not found"), tp->static_trace_marker_id);
13253}
13254
13255static struct breakpoint_ops strace_marker_breakpoint_ops;
13256
13257static int
13258strace_marker_p (struct breakpoint *b)
13259{
13260 return b->ops == &strace_marker_breakpoint_ops;
13261}
13262
53a5351d 13263/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13264 structures. */
c906108c
SS
13265
13266void
fba45db2 13267delete_breakpoint (struct breakpoint *bpt)
c906108c 13268{
52f0bd74 13269 struct breakpoint *b;
c906108c 13270
8a3fe4f8 13271 gdb_assert (bpt != NULL);
c906108c 13272
4a64f543
MS
13273 /* Has this bp already been deleted? This can happen because
13274 multiple lists can hold pointers to bp's. bpstat lists are
13275 especial culprits.
13276
13277 One example of this happening is a watchpoint's scope bp. When
13278 the scope bp triggers, we notice that the watchpoint is out of
13279 scope, and delete it. We also delete its scope bp. But the
13280 scope bp is marked "auto-deleting", and is already on a bpstat.
13281 That bpstat is then checked for auto-deleting bp's, which are
13282 deleted.
13283
13284 A real solution to this problem might involve reference counts in
13285 bp's, and/or giving them pointers back to their referencing
13286 bpstat's, and teaching delete_breakpoint to only free a bp's
13287 storage when no more references were extent. A cheaper bandaid
13288 was chosen. */
c906108c
SS
13289 if (bpt->type == bp_none)
13290 return;
13291
4a64f543
MS
13292 /* At least avoid this stale reference until the reference counting
13293 of breakpoints gets resolved. */
d0fb5eae 13294 if (bpt->related_breakpoint != bpt)
e5a0a904 13295 {
d0fb5eae 13296 struct breakpoint *related;
3a5c3e22 13297 struct watchpoint *w;
d0fb5eae
JK
13298
13299 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13300 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13301 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13302 w = (struct watchpoint *) bpt;
13303 else
13304 w = NULL;
13305 if (w != NULL)
13306 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13307
13308 /* Unlink bpt from the bpt->related_breakpoint ring. */
13309 for (related = bpt; related->related_breakpoint != bpt;
13310 related = related->related_breakpoint);
13311 related->related_breakpoint = bpt->related_breakpoint;
13312 bpt->related_breakpoint = bpt;
e5a0a904
JK
13313 }
13314
a9634178
TJB
13315 /* watch_command_1 creates a watchpoint but only sets its number if
13316 update_watchpoint succeeds in creating its bp_locations. If there's
13317 a problem in that process, we'll be asked to delete the half-created
13318 watchpoint. In that case, don't announce the deletion. */
13319 if (bpt->number)
13320 observer_notify_breakpoint_deleted (bpt);
c906108c 13321
c906108c
SS
13322 if (breakpoint_chain == bpt)
13323 breakpoint_chain = bpt->next;
13324
c906108c
SS
13325 ALL_BREAKPOINTS (b)
13326 if (b->next == bpt)
c5aa993b
JM
13327 {
13328 b->next = bpt->next;
13329 break;
13330 }
c906108c 13331
f431efe5
PA
13332 /* Be sure no bpstat's are pointing at the breakpoint after it's
13333 been freed. */
13334 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13335 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13336 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13337 commands are associated with the bpstat; if we remove it here,
13338 then the later call to bpstat_do_actions (&stop_bpstat); in
13339 event-top.c won't do anything, and temporary breakpoints with
13340 commands won't work. */
13341
13342 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13343
4a64f543
MS
13344 /* Now that breakpoint is removed from breakpoint list, update the
13345 global location list. This will remove locations that used to
13346 belong to this breakpoint. Do this before freeing the breakpoint
13347 itself, since remove_breakpoint looks at location's owner. It
13348 might be better design to have location completely
13349 self-contained, but it's not the case now. */
44702360 13350 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13351
4a64f543
MS
13352 /* On the chance that someone will soon try again to delete this
13353 same bp, we mark it as deleted before freeing its storage. */
c906108c 13354 bpt->type = bp_none;
4d01a485 13355 delete bpt;
c906108c
SS
13356}
13357
51be5b68
PA
13358/* Iterator function to call a user-provided callback function once
13359 for each of B and its related breakpoints. */
13360
13361static void
13362iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13363 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13364{
13365 struct breakpoint *related;
13366
13367 related = b;
13368 do
13369 {
13370 struct breakpoint *next;
13371
13372 /* FUNCTION may delete RELATED. */
13373 next = related->related_breakpoint;
13374
13375 if (next == related)
13376 {
13377 /* RELATED is the last ring entry. */
48649e1b 13378 function (related);
51be5b68
PA
13379
13380 /* FUNCTION may have deleted it, so we'd never reach back to
13381 B. There's nothing left to do anyway, so just break
13382 out. */
13383 break;
13384 }
13385 else
48649e1b 13386 function (related);
51be5b68
PA
13387
13388 related = next;
13389 }
13390 while (related != b);
13391}
95a42b64 13392
4495129a 13393static void
981a3fb3 13394delete_command (const char *arg, int from_tty)
c906108c 13395{
35df4500 13396 struct breakpoint *b, *b_tmp;
c906108c 13397
ea9365bb
TT
13398 dont_repeat ();
13399
c906108c
SS
13400 if (arg == 0)
13401 {
13402 int breaks_to_delete = 0;
13403
46c6471b
PA
13404 /* Delete all breakpoints if no argument. Do not delete
13405 internal breakpoints, these have to be deleted with an
13406 explicit breakpoint number argument. */
c5aa993b 13407 ALL_BREAKPOINTS (b)
46c6471b 13408 if (user_breakpoint_p (b))
973d738b
DJ
13409 {
13410 breaks_to_delete = 1;
13411 break;
13412 }
c906108c
SS
13413
13414 /* Ask user only if there are some breakpoints to delete. */
13415 if (!from_tty
e2e0b3e5 13416 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13417 {
35df4500 13418 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13419 if (user_breakpoint_p (b))
c5aa993b 13420 delete_breakpoint (b);
c906108c
SS
13421 }
13422 }
13423 else
48649e1b
TT
13424 map_breakpoint_numbers
13425 (arg, [&] (breakpoint *b)
13426 {
13427 iterate_over_related_breakpoints (b, delete_breakpoint);
13428 });
c906108c
SS
13429}
13430
c2f4122d
PA
13431/* Return true if all locations of B bound to PSPACE are pending. If
13432 PSPACE is NULL, all locations of all program spaces are
13433 considered. */
13434
0d381245 13435static int
c2f4122d 13436all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13437{
c2f4122d
PA
13438 struct bp_location *loc;
13439
13440 for (loc = b->loc; loc != NULL; loc = loc->next)
13441 if ((pspace == NULL
13442 || loc->pspace == pspace)
13443 && !loc->shlib_disabled
8645ff69 13444 && !loc->pspace->executing_startup)
0d381245
VP
13445 return 0;
13446 return 1;
fe3f5fa8
VP
13447}
13448
776592bf
DE
13449/* Subroutine of update_breakpoint_locations to simplify it.
13450 Return non-zero if multiple fns in list LOC have the same name.
13451 Null names are ignored. */
13452
13453static int
13454ambiguous_names_p (struct bp_location *loc)
13455{
13456 struct bp_location *l;
13457 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13458 (int (*) (const void *,
13459 const void *)) streq,
776592bf
DE
13460 NULL, xcalloc, xfree);
13461
13462 for (l = loc; l != NULL; l = l->next)
13463 {
13464 const char **slot;
13465 const char *name = l->function_name;
13466
13467 /* Allow for some names to be NULL, ignore them. */
13468 if (name == NULL)
13469 continue;
13470
13471 slot = (const char **) htab_find_slot (htab, (const void *) name,
13472 INSERT);
4a64f543
MS
13473 /* NOTE: We can assume slot != NULL here because xcalloc never
13474 returns NULL. */
776592bf
DE
13475 if (*slot != NULL)
13476 {
13477 htab_delete (htab);
13478 return 1;
13479 }
13480 *slot = name;
13481 }
13482
13483 htab_delete (htab);
13484 return 0;
13485}
13486
0fb4aa4b
PA
13487/* When symbols change, it probably means the sources changed as well,
13488 and it might mean the static tracepoint markers are no longer at
13489 the same address or line numbers they used to be at last we
13490 checked. Losing your static tracepoints whenever you rebuild is
13491 undesirable. This function tries to resync/rematch gdb static
13492 tracepoints with the markers on the target, for static tracepoints
13493 that have not been set by marker id. Static tracepoint that have
13494 been set by marker id are reset by marker id in breakpoint_re_set.
13495 The heuristic is:
13496
13497 1) For a tracepoint set at a specific address, look for a marker at
13498 the old PC. If one is found there, assume to be the same marker.
13499 If the name / string id of the marker found is different from the
13500 previous known name, assume that means the user renamed the marker
13501 in the sources, and output a warning.
13502
13503 2) For a tracepoint set at a given line number, look for a marker
13504 at the new address of the old line number. If one is found there,
13505 assume to be the same marker. If the name / string id of the
13506 marker found is different from the previous known name, assume that
13507 means the user renamed the marker in the sources, and output a
13508 warning.
13509
13510 3) If a marker is no longer found at the same address or line, it
13511 may mean the marker no longer exists. But it may also just mean
13512 the code changed a bit. Maybe the user added a few lines of code
13513 that made the marker move up or down (in line number terms). Ask
13514 the target for info about the marker with the string id as we knew
13515 it. If found, update line number and address in the matching
13516 static tracepoint. This will get confused if there's more than one
13517 marker with the same ID (possible in UST, although unadvised
13518 precisely because it confuses tools). */
13519
13520static struct symtab_and_line
13521update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13522{
d9b3f62e 13523 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13524 struct static_tracepoint_marker marker;
13525 CORE_ADDR pc;
0fb4aa4b
PA
13526
13527 pc = sal.pc;
13528 if (sal.line)
13529 find_line_pc (sal.symtab, sal.line, &pc);
13530
13531 if (target_static_tracepoint_marker_at (pc, &marker))
13532 {
d9b3f62e 13533 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13534 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13535 b->number,
d9b3f62e 13536 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13537
d9b3f62e
PA
13538 xfree (tp->static_trace_marker_id);
13539 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13540 release_static_tracepoint_marker (&marker);
13541
13542 return sal;
13543 }
13544
13545 /* Old marker wasn't found on target at lineno. Try looking it up
13546 by string ID. */
13547 if (!sal.explicit_pc
13548 && sal.line != 0
13549 && sal.symtab != NULL
d9b3f62e 13550 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13551 {
13552 VEC(static_tracepoint_marker_p) *markers;
13553
13554 markers
d9b3f62e 13555 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13556
13557 if (!VEC_empty(static_tracepoint_marker_p, markers))
13558 {
0fb4aa4b 13559 struct symbol *sym;
80e1d417 13560 struct static_tracepoint_marker *tpmarker;
79a45e25 13561 struct ui_out *uiout = current_uiout;
67994074 13562 struct explicit_location explicit_loc;
0fb4aa4b 13563
80e1d417 13564 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13565
d9b3f62e 13566 xfree (tp->static_trace_marker_id);
80e1d417 13567 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13568
13569 warning (_("marker for static tracepoint %d (%s) not "
13570 "found at previous line number"),
d9b3f62e 13571 b->number, tp->static_trace_marker_id);
0fb4aa4b 13572
51abb421 13573 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13574 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13575 uiout->text ("Now in ");
0fb4aa4b
PA
13576 if (sym)
13577 {
112e8700
SM
13578 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13579 uiout->text (" at ");
0fb4aa4b 13580 }
112e8700 13581 uiout->field_string ("file",
05cba821 13582 symtab_to_filename_for_display (sal2.symtab));
112e8700 13583 uiout->text (":");
0fb4aa4b 13584
112e8700 13585 if (uiout->is_mi_like_p ())
0fb4aa4b 13586 {
0b0865da 13587 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13588
112e8700 13589 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13590 }
13591
112e8700
SM
13592 uiout->field_int ("line", sal2.line);
13593 uiout->text ("\n");
0fb4aa4b 13594
80e1d417 13595 b->loc->line_number = sal2.line;
2f202fde 13596 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13597
d28cd78a 13598 b->location.reset (NULL);
67994074
KS
13599 initialize_explicit_location (&explicit_loc);
13600 explicit_loc.source_filename
00e52e53 13601 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13602 explicit_loc.line_offset.offset = b->loc->line_number;
13603 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13604 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13605
13606 /* Might be nice to check if function changed, and warn if
13607 so. */
13608
80e1d417 13609 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13610 }
13611 }
13612 return sal;
13613}
13614
8d3788bd
VP
13615/* Returns 1 iff locations A and B are sufficiently same that
13616 we don't need to report breakpoint as changed. */
13617
13618static int
13619locations_are_equal (struct bp_location *a, struct bp_location *b)
13620{
13621 while (a && b)
13622 {
13623 if (a->address != b->address)
13624 return 0;
13625
13626 if (a->shlib_disabled != b->shlib_disabled)
13627 return 0;
13628
13629 if (a->enabled != b->enabled)
13630 return 0;
13631
13632 a = a->next;
13633 b = b->next;
13634 }
13635
13636 if ((a == NULL) != (b == NULL))
13637 return 0;
13638
13639 return 1;
13640}
13641
c2f4122d
PA
13642/* Split all locations of B that are bound to PSPACE out of B's
13643 location list to a separate list and return that list's head. If
13644 PSPACE is NULL, hoist out all locations of B. */
13645
13646static struct bp_location *
13647hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13648{
13649 struct bp_location head;
13650 struct bp_location *i = b->loc;
13651 struct bp_location **i_link = &b->loc;
13652 struct bp_location *hoisted = &head;
13653
13654 if (pspace == NULL)
13655 {
13656 i = b->loc;
13657 b->loc = NULL;
13658 return i;
13659 }
13660
13661 head.next = NULL;
13662
13663 while (i != NULL)
13664 {
13665 if (i->pspace == pspace)
13666 {
13667 *i_link = i->next;
13668 i->next = NULL;
13669 hoisted->next = i;
13670 hoisted = i;
13671 }
13672 else
13673 i_link = &i->next;
13674 i = *i_link;
13675 }
13676
13677 return head.next;
13678}
13679
13680/* Create new breakpoint locations for B (a hardware or software
13681 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13682 zero, then B is a ranged breakpoint. Only recreates locations for
13683 FILTER_PSPACE. Locations of other program spaces are left
13684 untouched. */
f1310107 13685
0e30163f 13686void
0d381245 13687update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13688 struct program_space *filter_pspace,
6c5b2ebe
PA
13689 gdb::array_view<const symtab_and_line> sals,
13690 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8
VP
13691{
13692 int i;
c2f4122d 13693 struct bp_location *existing_locations;
0d381245 13694
6c5b2ebe 13695 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13696 {
13697 /* Ranged breakpoints have only one start location and one end
13698 location. */
13699 b->enable_state = bp_disabled;
f8eba3c6
TT
13700 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13701 "multiple locations found\n"),
13702 b->number);
13703 return;
13704 }
f1310107 13705
4a64f543
MS
13706 /* If there's no new locations, and all existing locations are
13707 pending, don't do anything. This optimizes the common case where
13708 all locations are in the same shared library, that was unloaded.
13709 We'd like to retain the location, so that when the library is
13710 loaded again, we don't loose the enabled/disabled status of the
13711 individual locations. */
6c5b2ebe 13712 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13713 return;
13714
c2f4122d 13715 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13716
6c5b2ebe 13717 for (const auto &sal : sals)
fe3f5fa8 13718 {
f8eba3c6
TT
13719 struct bp_location *new_loc;
13720
6c5b2ebe 13721 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13722
6c5b2ebe 13723 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13724
0d381245
VP
13725 /* Reparse conditions, they might contain references to the
13726 old symtab. */
13727 if (b->cond_string != NULL)
13728 {
bbc13ae3 13729 const char *s;
fe3f5fa8 13730
0d381245 13731 s = b->cond_string;
492d29ea 13732 TRY
0d381245 13733 {
6c5b2ebe
PA
13734 new_loc->cond = parse_exp_1 (&s, sal.pc,
13735 block_for_pc (sal.pc),
0d381245
VP
13736 0);
13737 }
492d29ea 13738 CATCH (e, RETURN_MASK_ERROR)
0d381245 13739 {
3e43a32a
MS
13740 warning (_("failed to reevaluate condition "
13741 "for breakpoint %d: %s"),
0d381245
VP
13742 b->number, e.message);
13743 new_loc->enabled = 0;
13744 }
492d29ea 13745 END_CATCH
0d381245 13746 }
fe3f5fa8 13747
6c5b2ebe 13748 if (!sals_end.empty ())
f1310107 13749 {
6c5b2ebe 13750 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13751
6c5b2ebe 13752 new_loc->length = end - sals[0].pc + 1;
f1310107 13753 }
0d381245 13754 }
fe3f5fa8 13755
4a64f543
MS
13756 /* If possible, carry over 'disable' status from existing
13757 breakpoints. */
0d381245
VP
13758 {
13759 struct bp_location *e = existing_locations;
776592bf
DE
13760 /* If there are multiple breakpoints with the same function name,
13761 e.g. for inline functions, comparing function names won't work.
13762 Instead compare pc addresses; this is just a heuristic as things
13763 may have moved, but in practice it gives the correct answer
13764 often enough until a better solution is found. */
13765 int have_ambiguous_names = ambiguous_names_p (b->loc);
13766
0d381245
VP
13767 for (; e; e = e->next)
13768 {
13769 if (!e->enabled && e->function_name)
13770 {
13771 struct bp_location *l = b->loc;
776592bf
DE
13772 if (have_ambiguous_names)
13773 {
13774 for (; l; l = l->next)
f1310107 13775 if (breakpoint_locations_match (e, l))
776592bf
DE
13776 {
13777 l->enabled = 0;
13778 break;
13779 }
13780 }
13781 else
13782 {
13783 for (; l; l = l->next)
13784 if (l->function_name
13785 && strcmp (e->function_name, l->function_name) == 0)
13786 {
13787 l->enabled = 0;
13788 break;
13789 }
13790 }
0d381245
VP
13791 }
13792 }
13793 }
fe3f5fa8 13794
8d3788bd
VP
13795 if (!locations_are_equal (existing_locations, b->loc))
13796 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
13797}
13798
f00aae0f 13799/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13800 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13801
6c5b2ebe 13802static std::vector<symtab_and_line>
f00aae0f 13803location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13804 struct program_space *search_pspace, int *found)
ef23e705 13805{
492d29ea 13806 struct gdb_exception exception = exception_none;
ef23e705 13807
983af33b 13808 gdb_assert (b->ops != NULL);
ef23e705 13809
6c5b2ebe
PA
13810 std::vector<symtab_and_line> sals;
13811
492d29ea 13812 TRY
ef23e705 13813 {
6c5b2ebe 13814 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13815 }
492d29ea 13816 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
13817 {
13818 int not_found_and_ok = 0;
492d29ea
PA
13819
13820 exception = e;
13821
ef23e705
TJB
13822 /* For pending breakpoints, it's expected that parsing will
13823 fail until the right shared library is loaded. User has
13824 already told to create pending breakpoints and don't need
13825 extra messages. If breakpoint is in bp_shlib_disabled
13826 state, then user already saw the message about that
13827 breakpoint being disabled, and don't want to see more
13828 errors. */
58438ac1 13829 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13830 && (b->condition_not_parsed
13831 || (b->loc != NULL
13832 && search_pspace != NULL
13833 && b->loc->pspace != search_pspace)
ef23e705 13834 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13835 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13836 || b->enable_state == bp_disabled))
13837 not_found_and_ok = 1;
13838
13839 if (!not_found_and_ok)
13840 {
13841 /* We surely don't want to warn about the same breakpoint
13842 10 times. One solution, implemented here, is disable
13843 the breakpoint on error. Another solution would be to
13844 have separate 'warning emitted' flag. Since this
13845 happens only when a binary has changed, I don't know
13846 which approach is better. */
13847 b->enable_state = bp_disabled;
13848 throw_exception (e);
13849 }
13850 }
492d29ea 13851 END_CATCH
ef23e705 13852
492d29ea 13853 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13854 {
6c5b2ebe
PA
13855 for (auto &sal : sals)
13856 resolve_sal_pc (&sal);
f00aae0f 13857 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13858 {
ed1d1739
KS
13859 char *cond_string, *extra_string;
13860 int thread, task;
ef23e705 13861
6c5b2ebe 13862 find_condition_and_thread (b->extra_string, sals[0].pc,
e7e0cddf
SS
13863 &cond_string, &thread, &task,
13864 &extra_string);
f00aae0f 13865 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13866 if (cond_string)
13867 b->cond_string = cond_string;
13868 b->thread = thread;
13869 b->task = task;
e7e0cddf 13870 if (extra_string)
f00aae0f
KS
13871 {
13872 xfree (b->extra_string);
13873 b->extra_string = extra_string;
13874 }
ef23e705
TJB
13875 b->condition_not_parsed = 0;
13876 }
13877
983af33b 13878 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13879 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13880
58438ac1
TT
13881 *found = 1;
13882 }
13883 else
13884 *found = 0;
ef23e705
TJB
13885
13886 return sals;
13887}
13888
348d480f
PA
13889/* The default re_set method, for typical hardware or software
13890 breakpoints. Reevaluate the breakpoint and recreate its
13891 locations. */
13892
13893static void
28010a5d 13894breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13895{
c2f4122d 13896 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13897 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13898
6c5b2ebe
PA
13899 int found;
13900 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13901 filter_pspace, &found);
ef23e705 13902 if (found)
6c5b2ebe 13903 expanded = std::move (sals);
ef23e705 13904
f00aae0f 13905 if (b->location_range_end != NULL)
f1310107 13906 {
6c5b2ebe
PA
13907 std::vector<symtab_and_line> sals_end
13908 = location_to_sals (b, b->location_range_end.get (),
13909 filter_pspace, &found);
f1310107 13910 if (found)
6c5b2ebe 13911 expanded_end = std::move (sals_end);
f1310107
TJB
13912 }
13913
c2f4122d 13914 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13915}
13916
983af33b
SDJ
13917/* Default method for creating SALs from an address string. It basically
13918 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13919
13920static void
f00aae0f
KS
13921create_sals_from_location_default (const struct event_location *location,
13922 struct linespec_result *canonical,
13923 enum bptype type_wanted)
983af33b 13924{
f00aae0f 13925 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13926}
13927
13928/* Call create_breakpoints_sal for the given arguments. This is the default
13929 function for the `create_breakpoints_sal' method of
13930 breakpoint_ops. */
13931
13932static void
13933create_breakpoints_sal_default (struct gdbarch *gdbarch,
13934 struct linespec_result *canonical,
e1e01040
PA
13935 gdb::unique_xmalloc_ptr<char> cond_string,
13936 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13937 enum bptype type_wanted,
13938 enum bpdisp disposition,
13939 int thread,
13940 int task, int ignore_count,
13941 const struct breakpoint_ops *ops,
13942 int from_tty, int enabled,
44f238bb 13943 int internal, unsigned flags)
983af33b 13944{
e1e01040
PA
13945 create_breakpoints_sal (gdbarch, canonical,
13946 std::move (cond_string),
13947 std::move (extra_string),
983af33b
SDJ
13948 type_wanted, disposition,
13949 thread, task, ignore_count, ops, from_tty,
44f238bb 13950 enabled, internal, flags);
983af33b
SDJ
13951}
13952
13953/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13954 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13955
6c5b2ebe 13956static std::vector<symtab_and_line>
f00aae0f
KS
13957decode_location_default (struct breakpoint *b,
13958 const struct event_location *location,
6c5b2ebe 13959 struct program_space *search_pspace)
983af33b
SDJ
13960{
13961 struct linespec_result canonical;
13962
c2f4122d 13963 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
13964 (struct symtab *) NULL, 0,
13965 &canonical, multiple_symbols_all,
13966 b->filter);
13967
13968 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13969 gdb_assert (canonical.lsals.size () < 2);
983af33b 13970
6c5b2ebe 13971 if (!canonical.lsals.empty ())
983af33b 13972 {
6c5b2ebe
PA
13973 const linespec_sals &lsal = canonical.lsals[0];
13974 return std::move (lsal.sals);
983af33b 13975 }
6c5b2ebe 13976 return {};
983af33b
SDJ
13977}
13978
bf469271 13979/* Reset a breakpoint. */
c906108c 13980
bf469271
PA
13981static void
13982breakpoint_re_set_one (breakpoint *b)
c906108c 13983{
fdf44873
TT
13984 input_radix = b->input_radix;
13985 set_language (b->language);
c906108c 13986
348d480f 13987 b->ops->re_set (b);
c906108c
SS
13988}
13989
c2f4122d
PA
13990/* Re-set breakpoint locations for the current program space.
13991 Locations bound to other program spaces are left untouched. */
13992
c906108c 13993void
69de3c6a 13994breakpoint_re_set (void)
c906108c 13995{
35df4500 13996 struct breakpoint *b, *b_tmp;
2a7f3dff 13997
c5aa993b 13998 {
fdf44873
TT
13999 scoped_restore_current_language save_language;
14000 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 14001 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 14002
5ed8105e
PA
14003 /* Note: we must not try to insert locations until after all
14004 breakpoints have been re-set. Otherwise, e.g., when re-setting
14005 breakpoint 1, we'd insert the locations of breakpoint 2, which
14006 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 14007
5ed8105e
PA
14008 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14009 {
bf469271
PA
14010 TRY
14011 {
14012 breakpoint_re_set_one (b);
14013 }
14014 CATCH (ex, RETURN_MASK_ALL)
14015 {
14016 exception_fprintf (gdb_stderr, ex,
14017 "Error in re-setting breakpoint %d: ",
14018 b->number);
14019 }
14020 END_CATCH
5ed8105e 14021 }
5ed8105e
PA
14022
14023 jit_breakpoint_re_set ();
14024 }
6c95b8df 14025
af02033e
PP
14026 create_overlay_event_breakpoint ();
14027 create_longjmp_master_breakpoint ();
14028 create_std_terminate_master_breakpoint ();
186c406b 14029 create_exception_master_breakpoint ();
2a7f3dff
PA
14030
14031 /* Now we can insert. */
14032 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14033}
14034\f
c906108c
SS
14035/* Reset the thread number of this breakpoint:
14036
14037 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14038 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14039void
fba45db2 14040breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14041{
14042 if (b->thread != -1)
14043 {
39f77062 14044 if (in_thread_list (inferior_ptid))
5d5658a1 14045 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14046
14047 /* We're being called after following a fork. The new fork is
14048 selected as current, and unless this was a vfork will have a
14049 different program space from the original thread. Reset that
14050 as well. */
14051 b->loc->pspace = current_program_space;
c906108c
SS
14052 }
14053}
14054
03ac34d5
MS
14055/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14056 If from_tty is nonzero, it prints a message to that effect,
14057 which ends with a period (no newline). */
14058
c906108c 14059void
fba45db2 14060set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14061{
52f0bd74 14062 struct breakpoint *b;
c906108c
SS
14063
14064 if (count < 0)
14065 count = 0;
14066
14067 ALL_BREAKPOINTS (b)
14068 if (b->number == bptnum)
c5aa993b 14069 {
d77f58be
SS
14070 if (is_tracepoint (b))
14071 {
14072 if (from_tty && count != 0)
14073 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14074 bptnum);
14075 return;
14076 }
14077
c5aa993b 14078 b->ignore_count = count;
221ea385
KS
14079 if (from_tty)
14080 {
14081 if (count == 0)
3e43a32a
MS
14082 printf_filtered (_("Will stop next time "
14083 "breakpoint %d is reached."),
221ea385
KS
14084 bptnum);
14085 else if (count == 1)
a3f17187 14086 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14087 bptnum);
14088 else
3e43a32a
MS
14089 printf_filtered (_("Will ignore next %d "
14090 "crossings of breakpoint %d."),
221ea385
KS
14091 count, bptnum);
14092 }
8d3788bd 14093 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14094 return;
14095 }
c906108c 14096
8a3fe4f8 14097 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14098}
14099
c906108c
SS
14100/* Command to set ignore-count of breakpoint N to COUNT. */
14101
14102static void
0b39b52e 14103ignore_command (const char *args, int from_tty)
c906108c 14104{
0b39b52e 14105 const char *p = args;
52f0bd74 14106 int num;
c906108c
SS
14107
14108 if (p == 0)
e2e0b3e5 14109 error_no_arg (_("a breakpoint number"));
c5aa993b 14110
c906108c 14111 num = get_number (&p);
5c44784c 14112 if (num == 0)
8a3fe4f8 14113 error (_("bad breakpoint number: '%s'"), args);
c906108c 14114 if (*p == 0)
8a3fe4f8 14115 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14116
14117 set_ignore_count (num,
14118 longest_to_int (value_as_long (parse_and_eval (p))),
14119 from_tty);
221ea385
KS
14120 if (from_tty)
14121 printf_filtered ("\n");
c906108c
SS
14122}
14123\f
d0fe4701
XR
14124
14125/* Call FUNCTION on each of the breakpoints with numbers in the range
14126 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
14127
14128static void
d0fe4701
XR
14129map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14130 gdb::function_view<void (breakpoint *)> function)
c906108c 14131{
d0fe4701
XR
14132 if (bp_num_range.first == 0)
14133 {
14134 warning (_("bad breakpoint number at or near '%d'"),
14135 bp_num_range.first);
14136 }
14137 else
c906108c 14138 {
d0fe4701 14139 struct breakpoint *b, *tmp;
197f0a60 14140
d0fe4701 14141 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 14142 {
d0fe4701
XR
14143 bool match = false;
14144
5c44784c 14145 ALL_BREAKPOINTS_SAFE (b, tmp)
d0fe4701 14146 if (b->number == i)
5c44784c 14147 {
bfd28288 14148 match = true;
48649e1b 14149 function (b);
11cf8741 14150 break;
5c44784c 14151 }
bfd28288 14152 if (!match)
d0fe4701 14153 printf_unfiltered (_("No breakpoint number %d.\n"), i);
c5aa993b 14154 }
c906108c
SS
14155 }
14156}
14157
d0fe4701
XR
14158/* Call FUNCTION on each of the breakpoints whose numbers are given in
14159 ARGS. */
14160
14161static void
14162map_breakpoint_numbers (const char *args,
14163 gdb::function_view<void (breakpoint *)> function)
14164{
14165 if (args == NULL || *args == '\0')
14166 error_no_arg (_("one or more breakpoint numbers"));
14167
14168 number_or_range_parser parser (args);
14169
14170 while (!parser.finished ())
14171 {
14172 int num = parser.get_number ();
14173 map_breakpoint_number_range (std::make_pair (num, num), function);
14174 }
14175}
14176
14177/* Return the breakpoint location structure corresponding to the
14178 BP_NUM and LOC_NUM values. */
14179
0d381245 14180static struct bp_location *
d0fe4701 14181find_location_by_number (int bp_num, int loc_num)
0d381245 14182{
0d381245 14183 struct breakpoint *b;
0d381245
VP
14184
14185 ALL_BREAKPOINTS (b)
14186 if (b->number == bp_num)
14187 {
14188 break;
14189 }
14190
14191 if (!b || b->number != bp_num)
d0fe4701 14192 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 14193
0d381245 14194 if (loc_num == 0)
d0fe4701 14195 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 14196
d0fe4701
XR
14197 int n = 0;
14198 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14199 if (++n == loc_num)
14200 return loc;
14201
14202 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
14203}
14204
95e95a6d
PA
14205/* Modes of operation for extract_bp_num. */
14206enum class extract_bp_kind
14207{
14208 /* Extracting a breakpoint number. */
14209 bp,
14210
14211 /* Extracting a location number. */
14212 loc,
14213};
14214
14215/* Extract a breakpoint or location number (as determined by KIND)
14216 from the string starting at START. TRAILER is a character which
14217 can be found after the number. If you don't want a trailer, use
14218 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14219 string. This always returns a positive integer. */
14220
14221static int
14222extract_bp_num (extract_bp_kind kind, const char *start,
14223 int trailer, const char **end_out = NULL)
14224{
14225 const char *end = start;
14226 int num = get_number_trailer (&end, trailer);
14227 if (num < 0)
14228 error (kind == extract_bp_kind::bp
14229 ? _("Negative breakpoint number '%.*s'")
14230 : _("Negative breakpoint location number '%.*s'"),
14231 int (end - start), start);
14232 if (num == 0)
14233 error (kind == extract_bp_kind::bp
14234 ? _("Bad breakpoint number '%.*s'")
14235 : _("Bad breakpoint location number '%.*s'"),
14236 int (end - start), start);
14237
14238 if (end_out != NULL)
14239 *end_out = end;
14240 return num;
14241}
14242
14243/* Extract a breakpoint or location range (as determined by KIND) in
14244 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14245 representing the (inclusive) range. The returned pair's elements
14246 are always positive integers. */
14247
14248static std::pair<int, int>
14249extract_bp_or_bp_range (extract_bp_kind kind,
14250 const std::string &arg,
14251 std::string::size_type arg_offset)
14252{
14253 std::pair<int, int> range;
14254 const char *bp_loc = &arg[arg_offset];
14255 std::string::size_type dash = arg.find ('-', arg_offset);
14256 if (dash != std::string::npos)
14257 {
14258 /* bp_loc is a range (x-z). */
14259 if (arg.length () == dash + 1)
14260 error (kind == extract_bp_kind::bp
14261 ? _("Bad breakpoint number at or near: '%s'")
14262 : _("Bad breakpoint location number at or near: '%s'"),
14263 bp_loc);
14264
14265 const char *end;
14266 const char *start_first = bp_loc;
14267 const char *start_second = &arg[dash + 1];
14268 range.first = extract_bp_num (kind, start_first, '-');
14269 range.second = extract_bp_num (kind, start_second, '\0', &end);
14270
14271 if (range.first > range.second)
14272 error (kind == extract_bp_kind::bp
14273 ? _("Inverted breakpoint range at '%.*s'")
14274 : _("Inverted breakpoint location range at '%.*s'"),
14275 int (end - start_first), start_first);
14276 }
14277 else
14278 {
14279 /* bp_loc is a single value. */
14280 range.first = extract_bp_num (kind, bp_loc, '\0');
14281 range.second = range.first;
14282 }
14283 return range;
14284}
14285
d0fe4701
XR
14286/* Extract the breakpoint/location range specified by ARG. Returns
14287 the breakpoint range in BP_NUM_RANGE, and the location range in
14288 BP_LOC_RANGE.
14289
14290 ARG may be in any of the following forms:
14291
14292 x where 'x' is a breakpoint number.
14293 x-y where 'x' and 'y' specify a breakpoint numbers range.
14294 x.y where 'x' is a breakpoint number and 'y' a location number.
14295 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14296 location number range.
14297*/
14298
cc638e86 14299static void
d0fe4701
XR
14300extract_bp_number_and_location (const std::string &arg,
14301 std::pair<int, int> &bp_num_range,
14302 std::pair<int, int> &bp_loc_range)
14303{
14304 std::string::size_type dot = arg.find ('.');
14305
14306 if (dot != std::string::npos)
14307 {
14308 /* Handle 'x.y' and 'x.y-z' cases. */
14309
14310 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 14311 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 14312
95e95a6d
PA
14313 bp_num_range.first
14314 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14315 bp_num_range.second = bp_num_range.first;
d0fe4701 14316
95e95a6d
PA
14317 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14318 arg, dot + 1);
d0fe4701
XR
14319 }
14320 else
14321 {
14322 /* Handle x and x-y cases. */
d0fe4701 14323
95e95a6d 14324 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
14325 bp_loc_range.first = 0;
14326 bp_loc_range.second = 0;
14327 }
d0fe4701
XR
14328}
14329
14330/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14331 specifies whether to enable or disable. */
14332
14333static void
14334enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14335{
14336 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14337 if (loc != NULL)
14338 {
14339 if (loc->enabled != enable)
14340 {
14341 loc->enabled = enable;
14342 mark_breakpoint_location_modified (loc);
14343 }
14344 if (target_supports_enable_disable_tracepoint ()
14345 && current_trace_status ()->running && loc->owner
14346 && is_tracepoint (loc->owner))
14347 target_disable_tracepoint (loc);
14348 }
14349 update_global_location_list (UGLL_DONT_INSERT);
14350}
14351
14352/* Enable or disable a range of breakpoint locations. BP_NUM is the
14353 number of the breakpoint, and BP_LOC_RANGE specifies the
14354 (inclusive) range of location numbers of that breakpoint to
14355 enable/disable. ENABLE specifies whether to enable or disable the
14356 location. */
14357
14358static void
14359enable_disable_breakpoint_location_range (int bp_num,
14360 std::pair<int, int> &bp_loc_range,
14361 bool enable)
14362{
14363 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14364 enable_disable_bp_num_loc (bp_num, i, enable);
14365}
0d381245 14366
1900040c
MS
14367/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14368 If from_tty is nonzero, it prints a message to that effect,
14369 which ends with a period (no newline). */
14370
c906108c 14371void
fba45db2 14372disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14373{
14374 /* Never disable a watchpoint scope breakpoint; we want to
14375 hit them when we leave scope so we can delete both the
14376 watchpoint and its scope breakpoint at that time. */
14377 if (bpt->type == bp_watchpoint_scope)
14378 return;
14379
b5de0fa7 14380 bpt->enable_state = bp_disabled;
c906108c 14381
b775012e
LM
14382 /* Mark breakpoint locations modified. */
14383 mark_breakpoint_modified (bpt);
14384
d248b706
KY
14385 if (target_supports_enable_disable_tracepoint ()
14386 && current_trace_status ()->running && is_tracepoint (bpt))
14387 {
14388 struct bp_location *location;
14389
14390 for (location = bpt->loc; location; location = location->next)
14391 target_disable_tracepoint (location);
14392 }
14393
44702360 14394 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14395
8d3788bd 14396 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14397}
14398
d0fe4701
XR
14399/* Enable or disable the breakpoint(s) or breakpoint location(s)
14400 specified in ARGS. ARGS may be in any of the formats handled by
14401 extract_bp_number_and_location. ENABLE specifies whether to enable
14402 or disable the breakpoints/locations. */
14403
c906108c 14404static void
d0fe4701 14405enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 14406{
c906108c 14407 if (args == 0)
46c6471b
PA
14408 {
14409 struct breakpoint *bpt;
14410
14411 ALL_BREAKPOINTS (bpt)
14412 if (user_breakpoint_p (bpt))
d0fe4701
XR
14413 {
14414 if (enable)
14415 enable_breakpoint (bpt);
14416 else
14417 disable_breakpoint (bpt);
14418 }
46c6471b 14419 }
9eaabc75 14420 else
0d381245 14421 {
cb791d59 14422 std::string num = extract_arg (&args);
9eaabc75 14423
cb791d59 14424 while (!num.empty ())
d248b706 14425 {
d0fe4701 14426 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 14427
cc638e86
PA
14428 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14429
14430 if (bp_loc_range.first == bp_loc_range.second
14431 && bp_loc_range.first == 0)
d0fe4701 14432 {
cc638e86
PA
14433 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14434 map_breakpoint_number_range (bp_num_range,
14435 enable
14436 ? enable_breakpoint
14437 : disable_breakpoint);
14438 }
14439 else
14440 {
14441 /* Handle breakpoint ids with formats 'x.y' or
14442 'x.y-z'. */
14443 enable_disable_breakpoint_location_range
14444 (bp_num_range.first, bp_loc_range, enable);
b775012e 14445 }
9eaabc75 14446 num = extract_arg (&args);
d248b706 14447 }
0d381245 14448 }
c906108c
SS
14449}
14450
d0fe4701
XR
14451/* The disable command disables the specified breakpoints/locations
14452 (or all defined breakpoints) so they're no longer effective in
14453 stopping the inferior. ARGS may be in any of the forms defined in
14454 extract_bp_number_and_location. */
14455
14456static void
14457disable_command (const char *args, int from_tty)
14458{
14459 enable_disable_command (args, from_tty, false);
14460}
14461
c906108c 14462static void
816338b5
SS
14463enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14464 int count)
c906108c 14465{
afe38095 14466 int target_resources_ok;
c906108c
SS
14467
14468 if (bpt->type == bp_hardware_breakpoint)
14469 {
14470 int i;
c5aa993b 14471 i = hw_breakpoint_used_count ();
53a5351d 14472 target_resources_ok =
d92524f1 14473 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14474 i + 1, 0);
c906108c 14475 if (target_resources_ok == 0)
8a3fe4f8 14476 error (_("No hardware breakpoint support in the target."));
c906108c 14477 else if (target_resources_ok < 0)
8a3fe4f8 14478 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14479 }
14480
cc60f2e3 14481 if (is_watchpoint (bpt))
c906108c 14482 {
d07205c2 14483 /* Initialize it just to avoid a GCC false warning. */
f486487f 14484 enum enable_state orig_enable_state = bp_disabled;
dde02812 14485
492d29ea 14486 TRY
c906108c 14487 {
3a5c3e22
PA
14488 struct watchpoint *w = (struct watchpoint *) bpt;
14489
1e718ff1
TJB
14490 orig_enable_state = bpt->enable_state;
14491 bpt->enable_state = bp_enabled;
3a5c3e22 14492 update_watchpoint (w, 1 /* reparse */);
c906108c 14493 }
492d29ea 14494 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14495 {
1e718ff1 14496 bpt->enable_state = orig_enable_state;
dde02812
ES
14497 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14498 bpt->number);
14499 return;
c5aa993b 14500 }
492d29ea 14501 END_CATCH
c906108c 14502 }
0101ce28 14503
b775012e
LM
14504 bpt->enable_state = bp_enabled;
14505
14506 /* Mark breakpoint locations modified. */
14507 mark_breakpoint_modified (bpt);
14508
d248b706
KY
14509 if (target_supports_enable_disable_tracepoint ()
14510 && current_trace_status ()->running && is_tracepoint (bpt))
14511 {
14512 struct bp_location *location;
14513
14514 for (location = bpt->loc; location; location = location->next)
14515 target_enable_tracepoint (location);
14516 }
14517
b4c291bb 14518 bpt->disposition = disposition;
816338b5 14519 bpt->enable_count = count;
44702360 14520 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14521
8d3788bd 14522 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14523}
14524
fe3f5fa8 14525
c906108c 14526void
fba45db2 14527enable_breakpoint (struct breakpoint *bpt)
c906108c 14528{
816338b5 14529 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14530}
14531
d0fe4701
XR
14532/* The enable command enables the specified breakpoints/locations (or
14533 all defined breakpoints) so they once again become (or continue to
14534 be) effective in stopping the inferior. ARGS may be in any of the
14535 forms defined in extract_bp_number_and_location. */
c906108c 14536
c906108c 14537static void
981a3fb3 14538enable_command (const char *args, int from_tty)
c906108c 14539{
d0fe4701 14540 enable_disable_command (args, from_tty, true);
c906108c
SS
14541}
14542
c906108c 14543static void
4495129a 14544enable_once_command (const char *args, int from_tty)
c906108c 14545{
48649e1b
TT
14546 map_breakpoint_numbers
14547 (args, [&] (breakpoint *b)
14548 {
14549 iterate_over_related_breakpoints
14550 (b, [&] (breakpoint *bpt)
14551 {
14552 enable_breakpoint_disp (bpt, disp_disable, 1);
14553 });
14554 });
816338b5
SS
14555}
14556
14557static void
4495129a 14558enable_count_command (const char *args, int from_tty)
816338b5 14559{
b9d61307
SM
14560 int count;
14561
14562 if (args == NULL)
14563 error_no_arg (_("hit count"));
14564
14565 count = get_number (&args);
816338b5 14566
48649e1b
TT
14567 map_breakpoint_numbers
14568 (args, [&] (breakpoint *b)
14569 {
14570 iterate_over_related_breakpoints
14571 (b, [&] (breakpoint *bpt)
14572 {
14573 enable_breakpoint_disp (bpt, disp_disable, count);
14574 });
14575 });
c906108c
SS
14576}
14577
c906108c 14578static void
4495129a 14579enable_delete_command (const char *args, int from_tty)
c906108c 14580{
48649e1b
TT
14581 map_breakpoint_numbers
14582 (args, [&] (breakpoint *b)
14583 {
14584 iterate_over_related_breakpoints
14585 (b, [&] (breakpoint *bpt)
14586 {
14587 enable_breakpoint_disp (bpt, disp_del, 1);
14588 });
14589 });
c906108c
SS
14590}
14591\f
fa8d40ab 14592static void
981a3fb3 14593set_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14594{
14595}
14596
14597static void
981a3fb3 14598show_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14599{
14600}
14601
1f3b5d1b
PP
14602/* Invalidate last known value of any hardware watchpoint if
14603 the memory which that value represents has been written to by
14604 GDB itself. */
14605
14606static void
8de0566d
YQ
14607invalidate_bp_value_on_memory_change (struct inferior *inferior,
14608 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14609 const bfd_byte *data)
14610{
14611 struct breakpoint *bp;
14612
14613 ALL_BREAKPOINTS (bp)
14614 if (bp->enable_state == bp_enabled
3a5c3e22 14615 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14616 {
3a5c3e22 14617 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14618
3a5c3e22
PA
14619 if (wp->val_valid && wp->val)
14620 {
14621 struct bp_location *loc;
14622
14623 for (loc = bp->loc; loc != NULL; loc = loc->next)
14624 if (loc->loc_type == bp_loc_hardware_watchpoint
14625 && loc->address + loc->length > addr
14626 && addr + len > loc->address)
14627 {
14628 value_free (wp->val);
14629 wp->val = NULL;
14630 wp->val_valid = 0;
14631 }
14632 }
1f3b5d1b
PP
14633 }
14634}
14635
8181d85f
DJ
14636/* Create and insert a breakpoint for software single step. */
14637
14638void
6c95b8df 14639insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14640 const address_space *aspace,
4a64f543 14641 CORE_ADDR next_pc)
8181d85f 14642{
7c16b83e
PA
14643 struct thread_info *tp = inferior_thread ();
14644 struct symtab_and_line sal;
14645 CORE_ADDR pc = next_pc;
8181d85f 14646
34b7e8a6
PA
14647 if (tp->control.single_step_breakpoints == NULL)
14648 {
14649 tp->control.single_step_breakpoints
5d5658a1 14650 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14651 }
8181d85f 14652
7c16b83e
PA
14653 sal = find_pc_line (pc, 0);
14654 sal.pc = pc;
14655 sal.section = find_pc_overlay (pc);
14656 sal.explicit_pc = 1;
34b7e8a6 14657 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14658
7c16b83e 14659 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14660}
14661
93f9a11f
YQ
14662/* Insert single step breakpoints according to the current state. */
14663
14664int
14665insert_single_step_breakpoints (struct gdbarch *gdbarch)
14666{
f5ea389a 14667 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14668 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14669
f5ea389a 14670 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14671
a0ff9e1a 14672 if (!next_pcs.empty ())
93f9a11f 14673 {
f5ea389a 14674 struct frame_info *frame = get_current_frame ();
8b86c959 14675 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14676
a0ff9e1a 14677 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14678 insert_single_step_breakpoint (gdbarch, aspace, pc);
14679
93f9a11f
YQ
14680 return 1;
14681 }
14682 else
14683 return 0;
14684}
14685
34b7e8a6 14686/* See breakpoint.h. */
f02253f1
HZ
14687
14688int
7c16b83e 14689breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14690 const address_space *aspace,
7c16b83e 14691 CORE_ADDR pc)
1aafd4da 14692{
7c16b83e 14693 struct bp_location *loc;
1aafd4da 14694
7c16b83e
PA
14695 for (loc = bp->loc; loc != NULL; loc = loc->next)
14696 if (loc->inserted
14697 && breakpoint_location_address_match (loc, aspace, pc))
14698 return 1;
1aafd4da 14699
7c16b83e 14700 return 0;
ef370185
JB
14701}
14702
14703/* Check whether a software single-step breakpoint is inserted at
14704 PC. */
14705
14706int
accd0bcd 14707single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14708 CORE_ADDR pc)
14709{
34b7e8a6
PA
14710 struct breakpoint *bpt;
14711
14712 ALL_BREAKPOINTS (bpt)
14713 {
14714 if (bpt->type == bp_single_step
14715 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14716 return 1;
14717 }
14718 return 0;
1aafd4da
UW
14719}
14720
1042e4c0
SS
14721/* Tracepoint-specific operations. */
14722
14723/* Set tracepoint count to NUM. */
14724static void
14725set_tracepoint_count (int num)
14726{
14727 tracepoint_count = num;
4fa62494 14728 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14729}
14730
70221824 14731static void
0b39b52e 14732trace_command (const char *arg, int from_tty)
1042e4c0 14733{
55aa24fb 14734 struct breakpoint_ops *ops;
55aa24fb 14735
ffc2605c
TT
14736 event_location_up location = string_to_event_location (&arg,
14737 current_language);
5b56227b 14738 if (location != NULL
ffc2605c 14739 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
14740 ops = &tracepoint_probe_breakpoint_ops;
14741 else
14742 ops = &tracepoint_breakpoint_ops;
14743
558a9d82 14744 create_breakpoint (get_current_arch (),
ffc2605c 14745 location.get (),
f00aae0f 14746 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14747 0 /* tempflag */,
14748 bp_tracepoint /* type_wanted */,
14749 0 /* Ignore count */,
14750 pending_break_support,
14751 ops,
14752 from_tty,
14753 1 /* enabled */,
14754 0 /* internal */, 0);
1042e4c0
SS
14755}
14756
70221824 14757static void
0b39b52e 14758ftrace_command (const char *arg, int from_tty)
7a697b8d 14759{
ffc2605c
TT
14760 event_location_up location = string_to_event_location (&arg,
14761 current_language);
558a9d82 14762 create_breakpoint (get_current_arch (),
ffc2605c 14763 location.get (),
f00aae0f 14764 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14765 0 /* tempflag */,
14766 bp_fast_tracepoint /* type_wanted */,
14767 0 /* Ignore count */,
14768 pending_break_support,
14769 &tracepoint_breakpoint_ops,
14770 from_tty,
14771 1 /* enabled */,
14772 0 /* internal */, 0);
0fb4aa4b
PA
14773}
14774
14775/* strace command implementation. Creates a static tracepoint. */
14776
70221824 14777static void
0b39b52e 14778strace_command (const char *arg, int from_tty)
0fb4aa4b 14779{
983af33b 14780 struct breakpoint_ops *ops;
ffc2605c 14781 event_location_up location;
983af33b
SDJ
14782
14783 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14784 or with a normal static tracepoint. */
61012eef 14785 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14786 {
14787 ops = &strace_marker_breakpoint_ops;
14788 location = new_linespec_location (&arg);
14789 }
983af33b 14790 else
f00aae0f
KS
14791 {
14792 ops = &tracepoint_breakpoint_ops;
14793 location = string_to_event_location (&arg, current_language);
14794 }
983af33b 14795
558a9d82 14796 create_breakpoint (get_current_arch (),
ffc2605c 14797 location.get (),
f00aae0f 14798 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14799 0 /* tempflag */,
14800 bp_static_tracepoint /* type_wanted */,
14801 0 /* Ignore count */,
14802 pending_break_support,
14803 ops,
14804 from_tty,
14805 1 /* enabled */,
14806 0 /* internal */, 0);
7a697b8d
SS
14807}
14808
409873ef
SS
14809/* Set up a fake reader function that gets command lines from a linked
14810 list that was acquired during tracepoint uploading. */
14811
14812static struct uploaded_tp *this_utp;
3149d8c1 14813static int next_cmd;
409873ef
SS
14814
14815static char *
14816read_uploaded_action (void)
14817{
14818 char *rslt;
14819
3149d8c1 14820 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 14821
3149d8c1 14822 next_cmd++;
409873ef
SS
14823
14824 return rslt;
14825}
14826
00bf0b85
SS
14827/* Given information about a tracepoint as recorded on a target (which
14828 can be either a live system or a trace file), attempt to create an
14829 equivalent GDB tracepoint. This is not a reliable process, since
14830 the target does not necessarily have all the information used when
14831 the tracepoint was originally defined. */
14832
d9b3f62e 14833struct tracepoint *
00bf0b85 14834create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14835{
f2fc3015
TT
14836 const char *addr_str;
14837 char small_buf[100];
d9b3f62e 14838 struct tracepoint *tp;
fd9b8c24 14839
409873ef
SS
14840 if (utp->at_string)
14841 addr_str = utp->at_string;
14842 else
14843 {
14844 /* In the absence of a source location, fall back to raw
14845 address. Since there is no way to confirm that the address
14846 means the same thing as when the trace was started, warn the
14847 user. */
3e43a32a
MS
14848 warning (_("Uploaded tracepoint %d has no "
14849 "source location, using raw address"),
409873ef 14850 utp->number);
8c042590 14851 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14852 addr_str = small_buf;
14853 }
14854
14855 /* There's not much we can do with a sequence of bytecodes. */
14856 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14857 warning (_("Uploaded tracepoint %d condition "
14858 "has no source form, ignoring it"),
409873ef 14859 utp->number);
d5551862 14860
ffc2605c
TT
14861 event_location_up location = string_to_event_location (&addr_str,
14862 current_language);
8cdf0e15 14863 if (!create_breakpoint (get_current_arch (),
ffc2605c 14864 location.get (),
f00aae0f 14865 utp->cond_string, -1, addr_str,
e7e0cddf 14866 0 /* parse cond/thread */,
8cdf0e15 14867 0 /* tempflag */,
0fb4aa4b 14868 utp->type /* type_wanted */,
8cdf0e15
VP
14869 0 /* Ignore count */,
14870 pending_break_support,
348d480f 14871 &tracepoint_breakpoint_ops,
8cdf0e15 14872 0 /* from_tty */,
84f4c1fe 14873 utp->enabled /* enabled */,
44f238bb
PA
14874 0 /* internal */,
14875 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14876 return NULL;
fd9b8c24 14877
409873ef 14878 /* Get the tracepoint we just created. */
fd9b8c24
PA
14879 tp = get_tracepoint (tracepoint_count);
14880 gdb_assert (tp != NULL);
d5551862 14881
00bf0b85
SS
14882 if (utp->pass > 0)
14883 {
8c042590 14884 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14885 tp->number);
00bf0b85 14886
409873ef 14887 trace_pass_command (small_buf, 0);
00bf0b85
SS
14888 }
14889
409873ef
SS
14890 /* If we have uploaded versions of the original commands, set up a
14891 special-purpose "reader" function and call the usual command line
14892 reader, then pass the result to the breakpoint command-setting
14893 function. */
3149d8c1 14894 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 14895 {
93921405 14896 command_line_up cmd_list;
00bf0b85 14897
409873ef 14898 this_utp = utp;
3149d8c1 14899 next_cmd = 0;
d5551862 14900
409873ef
SS
14901 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14902
c1fc2657 14903 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14904 }
3149d8c1
SS
14905 else if (!VEC_empty (char_ptr, utp->actions)
14906 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
14907 warning (_("Uploaded tracepoint %d actions "
14908 "have no source form, ignoring them"),
409873ef 14909 utp->number);
00bf0b85 14910
f196051f 14911 /* Copy any status information that might be available. */
c1fc2657 14912 tp->hit_count = utp->hit_count;
f196051f
SS
14913 tp->traceframe_usage = utp->traceframe_usage;
14914
00bf0b85 14915 return tp;
d9b3f62e 14916}
00bf0b85 14917
1042e4c0
SS
14918/* Print information on tracepoint number TPNUM_EXP, or all if
14919 omitted. */
14920
14921static void
11db9430 14922info_tracepoints_command (char *args, int from_tty)
1042e4c0 14923{
79a45e25 14924 struct ui_out *uiout = current_uiout;
e5a67952 14925 int num_printed;
1042e4c0 14926
e5a67952 14927 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
14928
14929 if (num_printed == 0)
1042e4c0 14930 {
e5a67952 14931 if (args == NULL || *args == '\0')
112e8700 14932 uiout->message ("No tracepoints.\n");
d77f58be 14933 else
112e8700 14934 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14935 }
ad443146
SS
14936
14937 default_collect_info ();
1042e4c0
SS
14938}
14939
4a64f543 14940/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14941 Not supported by all targets. */
14942static void
14943enable_trace_command (char *args, int from_tty)
14944{
14945 enable_command (args, from_tty);
14946}
14947
4a64f543 14948/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14949 Not supported by all targets. */
14950static void
14951disable_trace_command (char *args, int from_tty)
14952{
14953 disable_command (args, from_tty);
14954}
14955
4a64f543 14956/* Remove a tracepoint (or all if no argument). */
1042e4c0 14957static void
4495129a 14958delete_trace_command (const char *arg, int from_tty)
1042e4c0 14959{
35df4500 14960 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14961
14962 dont_repeat ();
14963
14964 if (arg == 0)
14965 {
14966 int breaks_to_delete = 0;
14967
14968 /* Delete all breakpoints if no argument.
14969 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14970 have to be deleted with an explicit breakpoint number
14971 argument. */
1042e4c0 14972 ALL_TRACEPOINTS (b)
46c6471b 14973 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14974 {
14975 breaks_to_delete = 1;
14976 break;
14977 }
1042e4c0
SS
14978
14979 /* Ask user only if there are some breakpoints to delete. */
14980 if (!from_tty
14981 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14982 {
35df4500 14983 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14984 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14985 delete_breakpoint (b);
1042e4c0
SS
14986 }
14987 }
14988 else
48649e1b
TT
14989 map_breakpoint_numbers
14990 (arg, [&] (breakpoint *b)
14991 {
14992 iterate_over_related_breakpoints (b, delete_breakpoint);
14993 });
1042e4c0
SS
14994}
14995
197f0a60
TT
14996/* Helper function for trace_pass_command. */
14997
14998static void
d9b3f62e 14999trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15000{
d9b3f62e 15001 tp->pass_count = count;
c1fc2657 15002 observer_notify_breakpoint_modified (tp);
197f0a60
TT
15003 if (from_tty)
15004 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 15005 tp->number, count);
197f0a60
TT
15006}
15007
1042e4c0
SS
15008/* Set passcount for tracepoint.
15009
15010 First command argument is passcount, second is tracepoint number.
15011 If tracepoint number omitted, apply to most recently defined.
15012 Also accepts special argument "all". */
15013
15014static void
0b39b52e 15015trace_pass_command (const char *args, int from_tty)
1042e4c0 15016{
d9b3f62e 15017 struct tracepoint *t1;
0b39b52e 15018 ULONGEST count;
1042e4c0
SS
15019
15020 if (args == 0 || *args == 0)
3e43a32a
MS
15021 error (_("passcount command requires an "
15022 "argument (count + optional TP num)"));
1042e4c0 15023
0b39b52e 15024 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15025
529480d0 15026 args = skip_spaces (args);
1042e4c0
SS
15027 if (*args && strncasecmp (args, "all", 3) == 0)
15028 {
d9b3f62e
PA
15029 struct breakpoint *b;
15030
1042e4c0 15031 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15032 if (*args)
15033 error (_("Junk at end of arguments."));
1042e4c0 15034
d9b3f62e 15035 ALL_TRACEPOINTS (b)
197f0a60 15036 {
d9b3f62e 15037 t1 = (struct tracepoint *) b;
197f0a60
TT
15038 trace_pass_set_count (t1, count, from_tty);
15039 }
15040 }
15041 else if (*args == '\0')
1042e4c0 15042 {
5fa1d40e 15043 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15044 if (t1)
197f0a60
TT
15045 trace_pass_set_count (t1, count, from_tty);
15046 }
15047 else
15048 {
bfd28288
PA
15049 number_or_range_parser parser (args);
15050 while (!parser.finished ())
1042e4c0 15051 {
bfd28288 15052 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
15053 if (t1)
15054 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15055 }
15056 }
1042e4c0
SS
15057}
15058
d9b3f62e 15059struct tracepoint *
1042e4c0
SS
15060get_tracepoint (int num)
15061{
15062 struct breakpoint *t;
15063
15064 ALL_TRACEPOINTS (t)
15065 if (t->number == num)
d9b3f62e 15066 return (struct tracepoint *) t;
1042e4c0
SS
15067
15068 return NULL;
15069}
15070
d5551862
SS
15071/* Find the tracepoint with the given target-side number (which may be
15072 different from the tracepoint number after disconnecting and
15073 reconnecting). */
15074
d9b3f62e 15075struct tracepoint *
d5551862
SS
15076get_tracepoint_by_number_on_target (int num)
15077{
d9b3f62e 15078 struct breakpoint *b;
d5551862 15079
d9b3f62e
PA
15080 ALL_TRACEPOINTS (b)
15081 {
15082 struct tracepoint *t = (struct tracepoint *) b;
15083
15084 if (t->number_on_target == num)
15085 return t;
15086 }
d5551862
SS
15087
15088 return NULL;
15089}
15090
1042e4c0 15091/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15092 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15093 If the argument is missing, the most recent tracepoint
15094 (tracepoint_count) is returned. */
15095
d9b3f62e 15096struct tracepoint *
0b39b52e 15097get_tracepoint_by_number (const char **arg,
bfd28288 15098 number_or_range_parser *parser)
1042e4c0 15099{
1042e4c0
SS
15100 struct breakpoint *t;
15101 int tpnum;
0b39b52e 15102 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 15103
bfd28288 15104 if (parser != NULL)
197f0a60 15105 {
bfd28288
PA
15106 gdb_assert (!parser->finished ());
15107 tpnum = parser->get_number ();
197f0a60
TT
15108 }
15109 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15110 tpnum = tracepoint_count;
1042e4c0 15111 else
197f0a60 15112 tpnum = get_number (arg);
1042e4c0
SS
15113
15114 if (tpnum <= 0)
15115 {
15116 if (instring && *instring)
15117 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15118 instring);
15119 else
5fa1d40e 15120 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15121 return NULL;
15122 }
15123
15124 ALL_TRACEPOINTS (t)
15125 if (t->number == tpnum)
15126 {
d9b3f62e 15127 return (struct tracepoint *) t;
1042e4c0
SS
15128 }
15129
1042e4c0
SS
15130 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15131 return NULL;
15132}
15133
d9b3f62e
PA
15134void
15135print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15136{
15137 if (b->thread != -1)
15138 fprintf_unfiltered (fp, " thread %d", b->thread);
15139
15140 if (b->task != 0)
15141 fprintf_unfiltered (fp, " task %d", b->task);
15142
15143 fprintf_unfiltered (fp, "\n");
15144}
15145
6149aea9
PA
15146/* Save information on user settable breakpoints (watchpoints, etc) to
15147 a new script file named FILENAME. If FILTER is non-NULL, call it
15148 on each breakpoint and only include the ones for which it returns
15149 non-zero. */
15150
1042e4c0 15151static void
4495129a 15152save_breakpoints (const char *filename, int from_tty,
6149aea9 15153 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15154{
15155 struct breakpoint *tp;
6149aea9 15156 int any = 0;
6149aea9 15157 int extra_trace_bits = 0;
1042e4c0 15158
6149aea9
PA
15159 if (filename == 0 || *filename == 0)
15160 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15161
15162 /* See if we have anything to save. */
6149aea9 15163 ALL_BREAKPOINTS (tp)
1042e4c0 15164 {
6149aea9 15165 /* Skip internal and momentary breakpoints. */
09d682a4 15166 if (!user_breakpoint_p (tp))
6149aea9
PA
15167 continue;
15168
15169 /* If we have a filter, only save the breakpoints it accepts. */
15170 if (filter && !filter (tp))
15171 continue;
15172
15173 any = 1;
15174
15175 if (is_tracepoint (tp))
15176 {
15177 extra_trace_bits = 1;
15178
15179 /* We can stop searching. */
15180 break;
15181 }
1042e4c0 15182 }
6149aea9
PA
15183
15184 if (!any)
1042e4c0 15185 {
6149aea9 15186 warning (_("Nothing to save."));
1042e4c0
SS
15187 return;
15188 }
15189
ee0c3293 15190 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15191
15192 stdio_file fp;
15193
ee0c3293 15194 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15195 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15196 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15197
6149aea9 15198 if (extra_trace_bits)
d7e74731 15199 save_trace_state_variables (&fp);
8bf6485c 15200
6149aea9 15201 ALL_BREAKPOINTS (tp)
1042e4c0 15202 {
6149aea9 15203 /* Skip internal and momentary breakpoints. */
09d682a4 15204 if (!user_breakpoint_p (tp))
6149aea9 15205 continue;
8bf6485c 15206
6149aea9
PA
15207 /* If we have a filter, only save the breakpoints it accepts. */
15208 if (filter && !filter (tp))
15209 continue;
15210
d7e74731 15211 tp->ops->print_recreate (tp, &fp);
1042e4c0 15212
6149aea9
PA
15213 /* Note, we can't rely on tp->number for anything, as we can't
15214 assume the recreated breakpoint numbers will match. Use $bpnum
15215 instead. */
15216
15217 if (tp->cond_string)
d7e74731 15218 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15219
15220 if (tp->ignore_count)
d7e74731 15221 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15222
2d9442cc 15223 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15224 {
d7e74731 15225 fp.puts (" commands\n");
a7bdde9e 15226
d7e74731 15227 current_uiout->redirect (&fp);
492d29ea 15228 TRY
1042e4c0 15229 {
d1b0a7bf 15230 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 15231 }
492d29ea
PA
15232 CATCH (ex, RETURN_MASK_ALL)
15233 {
112e8700 15234 current_uiout->redirect (NULL);
492d29ea
PA
15235 throw_exception (ex);
15236 }
15237 END_CATCH
1042e4c0 15238
112e8700 15239 current_uiout->redirect (NULL);
d7e74731 15240 fp.puts (" end\n");
1042e4c0 15241 }
6149aea9
PA
15242
15243 if (tp->enable_state == bp_disabled)
d7e74731 15244 fp.puts ("disable $bpnum\n");
6149aea9
PA
15245
15246 /* If this is a multi-location breakpoint, check if the locations
15247 should be individually disabled. Watchpoint locations are
15248 special, and not user visible. */
15249 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15250 {
15251 struct bp_location *loc;
15252 int n = 1;
15253
15254 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15255 if (!loc->enabled)
d7e74731 15256 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15257 }
1042e4c0 15258 }
8bf6485c 15259
6149aea9 15260 if (extra_trace_bits && *default_collect)
d7e74731 15261 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15262
1042e4c0 15263 if (from_tty)
ee0c3293 15264 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15265}
15266
15267/* The `save breakpoints' command. */
15268
15269static void
4495129a 15270save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15271{
15272 save_breakpoints (args, from_tty, NULL);
15273}
15274
15275/* The `save tracepoints' command. */
15276
15277static void
4495129a 15278save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15279{
15280 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15281}
15282
15283/* Create a vector of all tracepoints. */
15284
15285VEC(breakpoint_p) *
eeae04df 15286all_tracepoints (void)
1042e4c0
SS
15287{
15288 VEC(breakpoint_p) *tp_vec = 0;
15289 struct breakpoint *tp;
15290
15291 ALL_TRACEPOINTS (tp)
15292 {
15293 VEC_safe_push (breakpoint_p, tp_vec, tp);
15294 }
15295
15296 return tp_vec;
15297}
15298
c906108c 15299\f
629500fa
KS
15300/* This help string is used to consolidate all the help string for specifying
15301 locations used by several commands. */
15302
15303#define LOCATION_HELP_STRING \
15304"Linespecs are colon-separated lists of location parameters, such as\n\
15305source filename, function name, label name, and line number.\n\
15306Example: To specify the start of a label named \"the_top\" in the\n\
15307function \"fact\" in the file \"factorial.c\", use\n\
15308\"factorial.c:fact:the_top\".\n\
15309\n\
15310Address locations begin with \"*\" and specify an exact address in the\n\
15311program. Example: To specify the fourth byte past the start function\n\
15312\"main\", use \"*main + 4\".\n\
15313\n\
15314Explicit locations are similar to linespecs but use an option/argument\n\
15315syntax to specify location parameters.\n\
15316Example: To specify the start of the label named \"the_top\" in the\n\
15317function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15318-function fact -label the_top\".\n"
15319
4a64f543
MS
15320/* This help string is used for the break, hbreak, tbreak and thbreak
15321 commands. It is defined as a macro to prevent duplication.
15322 COMMAND should be a string constant containing the name of the
15323 command. */
629500fa 15324
31e2b00f 15325#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15326command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15327PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15328probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15329guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15330`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15331LOCATION may be a linespec, address, or explicit location as described\n\
15332below.\n\
15333\n\
dc10affe
PA
15334With no LOCATION, uses current execution address of the selected\n\
15335stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15336\n\
15337THREADNUM is the number from \"info threads\".\n\
15338CONDITION is a boolean expression.\n\
629500fa 15339\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15340Multiple breakpoints at one place are permitted, and useful if their\n\
15341conditions are different.\n\
31e2b00f
AS
15342\n\
15343Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15344
44feb3ce
TT
15345/* List of subcommands for "catch". */
15346static struct cmd_list_element *catch_cmdlist;
15347
15348/* List of subcommands for "tcatch". */
15349static struct cmd_list_element *tcatch_cmdlist;
15350
9ac4176b 15351void
a121b7c1 15352add_catch_command (const char *name, const char *docstring,
82ae6c8d 15353 cmd_sfunc_ftype *sfunc,
625e8578 15354 completer_ftype *completer,
44feb3ce
TT
15355 void *user_data_catch,
15356 void *user_data_tcatch)
15357{
15358 struct cmd_list_element *command;
15359
0450cc4c 15360 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15361 &catch_cmdlist);
15362 set_cmd_sfunc (command, sfunc);
15363 set_cmd_context (command, user_data_catch);
a96d9b2e 15364 set_cmd_completer (command, completer);
44feb3ce 15365
0450cc4c 15366 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15367 &tcatch_cmdlist);
15368 set_cmd_sfunc (command, sfunc);
15369 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15370 set_cmd_completer (command, completer);
44feb3ce
TT
15371}
15372
6149aea9 15373static void
981a3fb3 15374save_command (const char *arg, int from_tty)
6149aea9 15375{
3e43a32a
MS
15376 printf_unfiltered (_("\"save\" must be followed by "
15377 "the name of a save subcommand.\n"));
635c7e8a 15378 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15379}
15380
84f4c1fe
PM
15381struct breakpoint *
15382iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15383 void *data)
15384{
35df4500 15385 struct breakpoint *b, *b_tmp;
84f4c1fe 15386
35df4500 15387 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15388 {
15389 if ((*callback) (b, data))
15390 return b;
15391 }
15392
15393 return NULL;
15394}
15395
0574c78f
GB
15396/* Zero if any of the breakpoint's locations could be a location where
15397 functions have been inlined, nonzero otherwise. */
15398
15399static int
15400is_non_inline_function (struct breakpoint *b)
15401{
15402 /* The shared library event breakpoint is set on the address of a
15403 non-inline function. */
15404 if (b->type == bp_shlib_event)
15405 return 1;
15406
15407 return 0;
15408}
15409
15410/* Nonzero if the specified PC cannot be a location where functions
15411 have been inlined. */
15412
15413int
accd0bcd 15414pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15415 const struct target_waitstatus *ws)
0574c78f
GB
15416{
15417 struct breakpoint *b;
15418 struct bp_location *bl;
15419
15420 ALL_BREAKPOINTS (b)
15421 {
15422 if (!is_non_inline_function (b))
15423 continue;
15424
15425 for (bl = b->loc; bl != NULL; bl = bl->next)
15426 {
15427 if (!bl->shlib_disabled
09ac7c10 15428 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15429 return 1;
15430 }
15431 }
15432
15433 return 0;
15434}
15435
2f202fde
JK
15436/* Remove any references to OBJFILE which is going to be freed. */
15437
15438void
15439breakpoint_free_objfile (struct objfile *objfile)
15440{
15441 struct bp_location **locp, *loc;
15442
15443 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15444 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15445 loc->symtab = NULL;
15446}
15447
2060206e
PA
15448void
15449initialize_breakpoint_ops (void)
15450{
15451 static int initialized = 0;
15452
15453 struct breakpoint_ops *ops;
15454
15455 if (initialized)
15456 return;
15457 initialized = 1;
15458
15459 /* The breakpoint_ops structure to be inherit by all kinds of
15460 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15461 internal and momentary breakpoints, etc.). */
15462 ops = &bkpt_base_breakpoint_ops;
15463 *ops = base_breakpoint_ops;
15464 ops->re_set = bkpt_re_set;
15465 ops->insert_location = bkpt_insert_location;
15466 ops->remove_location = bkpt_remove_location;
15467 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15468 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15469 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15470 ops->decode_location = bkpt_decode_location;
2060206e
PA
15471
15472 /* The breakpoint_ops structure to be used in regular breakpoints. */
15473 ops = &bkpt_breakpoint_ops;
15474 *ops = bkpt_base_breakpoint_ops;
15475 ops->re_set = bkpt_re_set;
15476 ops->resources_needed = bkpt_resources_needed;
15477 ops->print_it = bkpt_print_it;
15478 ops->print_mention = bkpt_print_mention;
15479 ops->print_recreate = bkpt_print_recreate;
15480
15481 /* Ranged breakpoints. */
15482 ops = &ranged_breakpoint_ops;
15483 *ops = bkpt_breakpoint_ops;
15484 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15485 ops->resources_needed = resources_needed_ranged_breakpoint;
15486 ops->print_it = print_it_ranged_breakpoint;
15487 ops->print_one = print_one_ranged_breakpoint;
15488 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15489 ops->print_mention = print_mention_ranged_breakpoint;
15490 ops->print_recreate = print_recreate_ranged_breakpoint;
15491
15492 /* Internal breakpoints. */
15493 ops = &internal_breakpoint_ops;
15494 *ops = bkpt_base_breakpoint_ops;
15495 ops->re_set = internal_bkpt_re_set;
15496 ops->check_status = internal_bkpt_check_status;
15497 ops->print_it = internal_bkpt_print_it;
15498 ops->print_mention = internal_bkpt_print_mention;
15499
15500 /* Momentary breakpoints. */
15501 ops = &momentary_breakpoint_ops;
15502 *ops = bkpt_base_breakpoint_ops;
15503 ops->re_set = momentary_bkpt_re_set;
15504 ops->check_status = momentary_bkpt_check_status;
15505 ops->print_it = momentary_bkpt_print_it;
15506 ops->print_mention = momentary_bkpt_print_mention;
15507
55aa24fb
SDJ
15508 /* Probe breakpoints. */
15509 ops = &bkpt_probe_breakpoint_ops;
15510 *ops = bkpt_breakpoint_ops;
15511 ops->insert_location = bkpt_probe_insert_location;
15512 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15513 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15514 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15515
2060206e
PA
15516 /* Watchpoints. */
15517 ops = &watchpoint_breakpoint_ops;
15518 *ops = base_breakpoint_ops;
15519 ops->re_set = re_set_watchpoint;
15520 ops->insert_location = insert_watchpoint;
15521 ops->remove_location = remove_watchpoint;
15522 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15523 ops->check_status = check_status_watchpoint;
15524 ops->resources_needed = resources_needed_watchpoint;
15525 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15526 ops->print_it = print_it_watchpoint;
15527 ops->print_mention = print_mention_watchpoint;
15528 ops->print_recreate = print_recreate_watchpoint;
427cd150 15529 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15530
15531 /* Masked watchpoints. */
15532 ops = &masked_watchpoint_breakpoint_ops;
15533 *ops = watchpoint_breakpoint_ops;
15534 ops->insert_location = insert_masked_watchpoint;
15535 ops->remove_location = remove_masked_watchpoint;
15536 ops->resources_needed = resources_needed_masked_watchpoint;
15537 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15538 ops->print_it = print_it_masked_watchpoint;
15539 ops->print_one_detail = print_one_detail_masked_watchpoint;
15540 ops->print_mention = print_mention_masked_watchpoint;
15541 ops->print_recreate = print_recreate_masked_watchpoint;
15542
15543 /* Tracepoints. */
15544 ops = &tracepoint_breakpoint_ops;
15545 *ops = base_breakpoint_ops;
15546 ops->re_set = tracepoint_re_set;
15547 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15548 ops->print_one_detail = tracepoint_print_one_detail;
15549 ops->print_mention = tracepoint_print_mention;
15550 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15551 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15552 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15553 ops->decode_location = tracepoint_decode_location;
983af33b 15554
55aa24fb
SDJ
15555 /* Probe tracepoints. */
15556 ops = &tracepoint_probe_breakpoint_ops;
15557 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15558 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15559 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15560
983af33b
SDJ
15561 /* Static tracepoints with marker (`-m'). */
15562 ops = &strace_marker_breakpoint_ops;
15563 *ops = tracepoint_breakpoint_ops;
5f700d83 15564 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15565 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15566 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15567
15568 /* Fork catchpoints. */
15569 ops = &catch_fork_breakpoint_ops;
15570 *ops = base_breakpoint_ops;
15571 ops->insert_location = insert_catch_fork;
15572 ops->remove_location = remove_catch_fork;
15573 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15574 ops->print_it = print_it_catch_fork;
15575 ops->print_one = print_one_catch_fork;
15576 ops->print_mention = print_mention_catch_fork;
15577 ops->print_recreate = print_recreate_catch_fork;
15578
15579 /* Vfork catchpoints. */
15580 ops = &catch_vfork_breakpoint_ops;
15581 *ops = base_breakpoint_ops;
15582 ops->insert_location = insert_catch_vfork;
15583 ops->remove_location = remove_catch_vfork;
15584 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15585 ops->print_it = print_it_catch_vfork;
15586 ops->print_one = print_one_catch_vfork;
15587 ops->print_mention = print_mention_catch_vfork;
15588 ops->print_recreate = print_recreate_catch_vfork;
15589
15590 /* Exec catchpoints. */
15591 ops = &catch_exec_breakpoint_ops;
15592 *ops = base_breakpoint_ops;
2060206e
PA
15593 ops->insert_location = insert_catch_exec;
15594 ops->remove_location = remove_catch_exec;
15595 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15596 ops->print_it = print_it_catch_exec;
15597 ops->print_one = print_one_catch_exec;
15598 ops->print_mention = print_mention_catch_exec;
15599 ops->print_recreate = print_recreate_catch_exec;
15600
edcc5120
TT
15601 /* Solib-related catchpoints. */
15602 ops = &catch_solib_breakpoint_ops;
15603 *ops = base_breakpoint_ops;
edcc5120
TT
15604 ops->insert_location = insert_catch_solib;
15605 ops->remove_location = remove_catch_solib;
15606 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15607 ops->check_status = check_status_catch_solib;
15608 ops->print_it = print_it_catch_solib;
15609 ops->print_one = print_one_catch_solib;
15610 ops->print_mention = print_mention_catch_solib;
15611 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15612
15613 ops = &dprintf_breakpoint_ops;
15614 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15615 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15616 ops->resources_needed = bkpt_resources_needed;
15617 ops->print_it = bkpt_print_it;
15618 ops->print_mention = bkpt_print_mention;
2d9442cc 15619 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15620 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15621 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15622}
15623
8bfd80db
YQ
15624/* Chain containing all defined "enable breakpoint" subcommands. */
15625
15626static struct cmd_list_element *enablebreaklist = NULL;
15627
c906108c 15628void
fba45db2 15629_initialize_breakpoint (void)
c906108c
SS
15630{
15631 struct cmd_list_element *c;
15632
2060206e
PA
15633 initialize_breakpoint_ops ();
15634
84acb35a 15635 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15636 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15637 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15638
55aa24fb 15639 breakpoint_objfile_key
43dce439 15640 = register_objfile_data_with_cleanup (NULL, free_breakpoint_objfile_data);
17450429 15641
c906108c
SS
15642 breakpoint_chain = 0;
15643 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15644 before a breakpoint is set. */
15645 breakpoint_count = 0;
15646
1042e4c0
SS
15647 tracepoint_count = 0;
15648
1bedd215
AC
15649 add_com ("ignore", class_breakpoint, ignore_command, _("\
15650Set ignore-count of breakpoint number N to COUNT.\n\
15651Usage is `ignore N COUNT'."));
c906108c 15652
1bedd215 15653 add_com ("commands", class_breakpoint, commands_command, _("\
18da0c51
MG
15654Set commands to be executed when the given breakpoints are hit.\n\
15655Give a space-separated breakpoint list as argument after \"commands\".\n\
15656A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15657(e.g. `5-7').\n\
c906108c
SS
15658With no argument, the targeted breakpoint is the last one set.\n\
15659The commands themselves follow starting on the next line.\n\
15660Type a line containing \"end\" to indicate the end of them.\n\
15661Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15662then no output is printed when it is hit, except what the commands print."));
c906108c 15663
d55637df 15664 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15665Specify breakpoint number N to break only if COND is true.\n\
c906108c 15666Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15667expression to be evaluated whenever breakpoint N is reached."));
d55637df 15668 set_cmd_completer (c, condition_completer);
c906108c 15669
1bedd215 15670 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15671Set a temporary breakpoint.\n\
c906108c
SS
15672Like \"break\" except the breakpoint is only temporary,\n\
15673so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15674by using \"enable delete\" on the breakpoint number.\n\
15675\n"
15676BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15677 set_cmd_completer (c, location_completer);
c94fdfd0 15678
1bedd215 15679 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15680Set a hardware assisted breakpoint.\n\
c906108c 15681Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15682some target hardware may not have this support.\n\
15683\n"
15684BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15685 set_cmd_completer (c, location_completer);
c906108c 15686
1bedd215 15687 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15688Set a temporary hardware assisted breakpoint.\n\
c906108c 15689Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15690so it will be deleted when hit.\n\
15691\n"
15692BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15693 set_cmd_completer (c, location_completer);
c906108c 15694
1bedd215
AC
15695 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15696Enable some breakpoints.\n\
c906108c
SS
15697Give breakpoint numbers (separated by spaces) as arguments.\n\
15698With no subcommand, breakpoints are enabled until you command otherwise.\n\
15699This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15700With a subcommand you can enable temporarily."),
c906108c 15701 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15702
15703 add_com_alias ("en", "enable", class_breakpoint, 1);
15704
84951ab5 15705 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15706Enable some breakpoints.\n\
c906108c
SS
15707Give breakpoint numbers (separated by spaces) as arguments.\n\
15708This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15709May be abbreviated to simply \"enable\".\n"),
c5aa993b 15710 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15711
1a966eab
AC
15712 add_cmd ("once", no_class, enable_once_command, _("\
15713Enable breakpoints for one hit. Give breakpoint numbers.\n\
15714If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15715 &enablebreaklist);
15716
1a966eab
AC
15717 add_cmd ("delete", no_class, enable_delete_command, _("\
15718Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15719If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15720 &enablebreaklist);
15721
816338b5
SS
15722 add_cmd ("count", no_class, enable_count_command, _("\
15723Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15724If a breakpoint is hit while enabled in this fashion,\n\
15725the count is decremented; when it reaches zero, the breakpoint is disabled."),
15726 &enablebreaklist);
15727
1a966eab
AC
15728 add_cmd ("delete", no_class, enable_delete_command, _("\
15729Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15730If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15731 &enablelist);
15732
1a966eab
AC
15733 add_cmd ("once", no_class, enable_once_command, _("\
15734Enable breakpoints for one hit. Give breakpoint numbers.\n\
15735If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15736 &enablelist);
15737
15738 add_cmd ("count", no_class, enable_count_command, _("\
15739Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15740If a breakpoint is hit while enabled in this fashion,\n\
15741the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15742 &enablelist);
15743
1bedd215
AC
15744 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15745Disable some breakpoints.\n\
c906108c
SS
15746Arguments are breakpoint numbers with spaces in between.\n\
15747To disable all breakpoints, give no argument.\n\
64b9b334 15748A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15749 &disablelist, "disable ", 1, &cmdlist);
15750 add_com_alias ("dis", "disable", class_breakpoint, 1);
15751 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15752
1a966eab
AC
15753 add_cmd ("breakpoints", class_alias, disable_command, _("\
15754Disable some breakpoints.\n\
c906108c
SS
15755Arguments are breakpoint numbers with spaces in between.\n\
15756To disable all breakpoints, give no argument.\n\
64b9b334 15757A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15758This command may be abbreviated \"disable\"."),
c906108c
SS
15759 &disablelist);
15760
1bedd215
AC
15761 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15762Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15763Arguments are breakpoint numbers with spaces in between.\n\
15764To delete all breakpoints, give no argument.\n\
15765\n\
15766Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15767The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15768 &deletelist, "delete ", 1, &cmdlist);
15769 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15770 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15771
1a966eab
AC
15772 add_cmd ("breakpoints", class_alias, delete_command, _("\
15773Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15774Arguments are breakpoint numbers with spaces in between.\n\
15775To delete all breakpoints, give no argument.\n\
1a966eab 15776This command may be abbreviated \"delete\"."),
c906108c
SS
15777 &deletelist);
15778
1bedd215 15779 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15780Clear breakpoint at specified location.\n\
15781Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15782\n\
15783With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
15784is executing in.\n"
15785"\n" LOCATION_HELP_STRING "\n\
1bedd215 15786See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15787 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15788
1bedd215 15789 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15790Set breakpoint at specified location.\n"
31e2b00f 15791BREAK_ARGS_HELP ("break")));
5ba2abeb 15792 set_cmd_completer (c, location_completer);
c94fdfd0 15793
c906108c
SS
15794 add_com_alias ("b", "break", class_run, 1);
15795 add_com_alias ("br", "break", class_run, 1);
15796 add_com_alias ("bre", "break", class_run, 1);
15797 add_com_alias ("brea", "break", class_run, 1);
15798
c906108c
SS
15799 if (dbx_commands)
15800 {
1bedd215
AC
15801 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15802Break in function/address or break at a line in the current file."),
c5aa993b
JM
15803 &stoplist, "stop ", 1, &cmdlist);
15804 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15805 _("Break in function or address."), &stoplist);
c5aa993b 15806 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15807 _("Break at a line in the current file."), &stoplist);
11db9430 15808 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15809Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15810The \"Type\" column indicates one of:\n\
15811\tbreakpoint - normal breakpoint\n\
15812\twatchpoint - watchpoint\n\
15813The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15814the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15815breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15816address and file/line number respectively.\n\
15817\n\
15818Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15819are set to the address of the last breakpoint listed unless the command\n\
15820is prefixed with \"server \".\n\n\
c906108c 15821Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15822breakpoint set."));
c906108c
SS
15823 }
15824
11db9430 15825 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15826Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15827The \"Type\" column indicates one of:\n\
15828\tbreakpoint - normal breakpoint\n\
15829\twatchpoint - watchpoint\n\
15830The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15831the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15832breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15833address and file/line number respectively.\n\
15834\n\
15835Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15836are set to the address of the last breakpoint listed unless the command\n\
15837is prefixed with \"server \".\n\n\
c906108c 15838Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15839breakpoint set."));
c906108c 15840
6b04bdb7
MS
15841 add_info_alias ("b", "breakpoints", 1);
15842
1a966eab
AC
15843 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15844Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15845The \"Type\" column indicates one of:\n\
15846\tbreakpoint - normal breakpoint\n\
15847\twatchpoint - watchpoint\n\
15848\tlongjmp - internal breakpoint used to step through longjmp()\n\
15849\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15850\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15851\tfinish - internal breakpoint used by the \"finish\" command\n\
15852The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15853the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15854breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15855address and file/line number respectively.\n\
15856\n\
15857Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15858are set to the address of the last breakpoint listed unless the command\n\
15859is prefixed with \"server \".\n\n\
c906108c 15860Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15861breakpoint set."),
c906108c
SS
15862 &maintenanceinfolist);
15863
44feb3ce
TT
15864 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15865Set catchpoints to catch events."),
15866 &catch_cmdlist, "catch ",
15867 0/*allow-unknown*/, &cmdlist);
15868
15869 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15870Set temporary catchpoints to catch events."),
15871 &tcatch_cmdlist, "tcatch ",
15872 0/*allow-unknown*/, &cmdlist);
15873
44feb3ce
TT
15874 add_catch_command ("fork", _("Catch calls to fork."),
15875 catch_fork_command_1,
a96d9b2e 15876 NULL,
44feb3ce
TT
15877 (void *) (uintptr_t) catch_fork_permanent,
15878 (void *) (uintptr_t) catch_fork_temporary);
15879 add_catch_command ("vfork", _("Catch calls to vfork."),
15880 catch_fork_command_1,
a96d9b2e 15881 NULL,
44feb3ce
TT
15882 (void *) (uintptr_t) catch_vfork_permanent,
15883 (void *) (uintptr_t) catch_vfork_temporary);
15884 add_catch_command ("exec", _("Catch calls to exec."),
15885 catch_exec_command_1,
a96d9b2e
SDJ
15886 NULL,
15887 CATCH_PERMANENT,
15888 CATCH_TEMPORARY);
edcc5120
TT
15889 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15890Usage: catch load [REGEX]\n\
15891If REGEX is given, only stop for libraries matching the regular expression."),
15892 catch_load_command_1,
15893 NULL,
15894 CATCH_PERMANENT,
15895 CATCH_TEMPORARY);
15896 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15897Usage: catch unload [REGEX]\n\
15898If REGEX is given, only stop for libraries matching the regular expression."),
15899 catch_unload_command_1,
15900 NULL,
15901 CATCH_PERMANENT,
15902 CATCH_TEMPORARY);
c5aa993b 15903
1bedd215
AC
15904 c = add_com ("watch", class_breakpoint, watch_command, _("\
15905Set a watchpoint for an expression.\n\
06a64a0b 15906Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15907A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15908an expression changes.\n\
15909If -l or -location is given, this evaluates EXPRESSION and watches\n\
15910the memory to which it refers."));
65d12d83 15911 set_cmd_completer (c, expression_completer);
c906108c 15912
1bedd215
AC
15913 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15914Set a read watchpoint for an expression.\n\
06a64a0b 15915Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15916A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15917an expression is read.\n\
15918If -l or -location is given, this evaluates EXPRESSION and watches\n\
15919the memory to which it refers."));
65d12d83 15920 set_cmd_completer (c, expression_completer);
c906108c 15921
1bedd215
AC
15922 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15923Set a watchpoint for an expression.\n\
06a64a0b 15924Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15925A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15926an expression is either read or written.\n\
15927If -l or -location is given, this evaluates EXPRESSION and watches\n\
15928the memory to which it refers."));
65d12d83 15929 set_cmd_completer (c, expression_completer);
c906108c 15930
11db9430 15931 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15932Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15933
920d2a44
AC
15934 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15935 respond to changes - contrary to the description. */
85c07804
AC
15936 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15937 &can_use_hw_watchpoints, _("\
15938Set debugger's willingness to use watchpoint hardware."), _("\
15939Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15940If zero, gdb will not use hardware for new watchpoints, even if\n\
15941such is available. (However, any hardware watchpoints that were\n\
15942created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15943hardware.)"),
15944 NULL,
920d2a44 15945 show_can_use_hw_watchpoints,
85c07804 15946 &setlist, &showlist);
c906108c
SS
15947
15948 can_use_hw_watchpoints = 1;
fa8d40ab 15949
1042e4c0
SS
15950 /* Tracepoint manipulation commands. */
15951
15952 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15953Set a tracepoint at specified location.\n\
1042e4c0
SS
15954\n"
15955BREAK_ARGS_HELP ("trace") "\n\
15956Do \"help tracepoints\" for info on other tracepoint commands."));
15957 set_cmd_completer (c, location_completer);
15958
15959 add_com_alias ("tp", "trace", class_alias, 0);
15960 add_com_alias ("tr", "trace", class_alias, 1);
15961 add_com_alias ("tra", "trace", class_alias, 1);
15962 add_com_alias ("trac", "trace", class_alias, 1);
15963
7a697b8d 15964 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15965Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15966\n"
15967BREAK_ARGS_HELP ("ftrace") "\n\
15968Do \"help tracepoints\" for info on other tracepoint commands."));
15969 set_cmd_completer (c, location_completer);
15970
0fb4aa4b 15971 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15972Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15973\n\
15974strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15975LOCATION may be a linespec, explicit, or address location (described below) \n\
15976or -m MARKER_ID.\n\n\
15977If a marker id is specified, probe the marker with that name. With\n\
15978no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15979Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15980This collects arbitrary user data passed in the probe point call to the\n\
15981tracing library. You can inspect it when analyzing the trace buffer,\n\
15982by printing the $_sdata variable like any other convenience variable.\n\
15983\n\
15984CONDITION is a boolean expression.\n\
629500fa 15985\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15986Multiple tracepoints at one place are permitted, and useful if their\n\
15987conditions are different.\n\
0fb4aa4b
PA
15988\n\
15989Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15990Do \"help tracepoints\" for info on other tracepoint commands."));
15991 set_cmd_completer (c, location_completer);
15992
11db9430 15993 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15994Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15995Convenience variable \"$tpnum\" contains the number of the\n\
15996last tracepoint set."));
15997
15998 add_info_alias ("tp", "tracepoints", 1);
15999
16000 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16001Delete specified tracepoints.\n\
16002Arguments are tracepoint numbers, separated by spaces.\n\
16003No argument means delete all tracepoints."),
16004 &deletelist);
7e20dfcd 16005 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16006
16007 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16008Disable specified tracepoints.\n\
16009Arguments are tracepoint numbers, separated by spaces.\n\
16010No argument means disable all tracepoints."),
16011 &disablelist);
16012 deprecate_cmd (c, "disable");
16013
16014 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16015Enable specified tracepoints.\n\
16016Arguments are tracepoint numbers, separated by spaces.\n\
16017No argument means enable all tracepoints."),
16018 &enablelist);
16019 deprecate_cmd (c, "enable");
16020
16021 add_com ("passcount", class_trace, trace_pass_command, _("\
16022Set the passcount for a tracepoint.\n\
16023The trace will end when the tracepoint has been passed 'count' times.\n\
16024Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16025if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16026
6149aea9
PA
16027 add_prefix_cmd ("save", class_breakpoint, save_command,
16028 _("Save breakpoint definitions as a script."),
16029 &save_cmdlist, "save ",
16030 0/*allow-unknown*/, &cmdlist);
16031
16032 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16033Save current breakpoint definitions as a script.\n\
cce7e648 16034This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16035catchpoints, tracepoints). Use the 'source' command in another debug\n\
16036session to restore them."),
16037 &save_cmdlist);
16038 set_cmd_completer (c, filename_completer);
16039
16040 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16041Save current tracepoint definitions as a script.\n\
6149aea9
PA
16042Use the 'source' command in another debug session to restore them."),
16043 &save_cmdlist);
1042e4c0
SS
16044 set_cmd_completer (c, filename_completer);
16045
6149aea9
PA
16046 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16047 deprecate_cmd (c, "save tracepoints");
16048
1bedd215 16049 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16050Breakpoint specific settings\n\
16051Configure various breakpoint-specific variables such as\n\
1bedd215 16052pending breakpoint behavior"),
fa8d40ab
JJ
16053 &breakpoint_set_cmdlist, "set breakpoint ",
16054 0/*allow-unknown*/, &setlist);
1bedd215 16055 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16056Breakpoint specific settings\n\
16057Configure various breakpoint-specific variables such as\n\
1bedd215 16058pending breakpoint behavior"),
fa8d40ab
JJ
16059 &breakpoint_show_cmdlist, "show breakpoint ",
16060 0/*allow-unknown*/, &showlist);
16061
7915a72c
AC
16062 add_setshow_auto_boolean_cmd ("pending", no_class,
16063 &pending_break_support, _("\
16064Set debugger's behavior regarding pending breakpoints."), _("\
16065Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16066If on, an unrecognized breakpoint location will cause gdb to create a\n\
16067pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16068an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16069user-query to see if a pending breakpoint should be created."),
2c5b56ce 16070 NULL,
920d2a44 16071 show_pending_break_support,
6e1d7d6c
AC
16072 &breakpoint_set_cmdlist,
16073 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16074
16075 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16076
16077 add_setshow_boolean_cmd ("auto-hw", no_class,
16078 &automatic_hardware_breakpoints, _("\
16079Set automatic usage of hardware breakpoints."), _("\
16080Show automatic usage of hardware breakpoints."), _("\
16081If set, the debugger will automatically use hardware breakpoints for\n\
16082breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16083a warning will be emitted for such breakpoints."),
16084 NULL,
16085 show_automatic_hardware_breakpoints,
16086 &breakpoint_set_cmdlist,
16087 &breakpoint_show_cmdlist);
74960c60 16088
a25a5a45
PA
16089 add_setshow_boolean_cmd ("always-inserted", class_support,
16090 &always_inserted_mode, _("\
74960c60
VP
16091Set mode for inserting breakpoints."), _("\
16092Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16093When this mode is on, breakpoints are inserted immediately as soon as\n\
16094they're created, kept inserted even when execution stops, and removed\n\
16095only when the user deletes them. When this mode is off (the default),\n\
16096breakpoints are inserted only when execution continues, and removed\n\
16097when execution stops."),
72d0e2c5
YQ
16098 NULL,
16099 &show_always_inserted_mode,
16100 &breakpoint_set_cmdlist,
16101 &breakpoint_show_cmdlist);
f1310107 16102
b775012e
LM
16103 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16104 condition_evaluation_enums,
16105 &condition_evaluation_mode_1, _("\
16106Set mode of breakpoint condition evaluation."), _("\
16107Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16108When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16109evaluated on the host's side by GDB. When it is set to \"target\",\n\
16110breakpoint conditions will be downloaded to the target (if the target\n\
16111supports such feature) and conditions will be evaluated on the target's side.\n\
16112If this is set to \"auto\" (default), this will be automatically set to\n\
16113\"target\" if it supports condition evaluation, otherwise it will\n\
16114be set to \"gdb\""),
16115 &set_condition_evaluation_mode,
16116 &show_condition_evaluation_mode,
16117 &breakpoint_set_cmdlist,
16118 &breakpoint_show_cmdlist);
16119
f1310107
TJB
16120 add_com ("break-range", class_breakpoint, break_range_command, _("\
16121Set a breakpoint for an address range.\n\
16122break-range START-LOCATION, END-LOCATION\n\
16123where START-LOCATION and END-LOCATION can be one of the following:\n\
16124 LINENUM, for that line in the current file,\n\
16125 FILE:LINENUM, for that line in that file,\n\
16126 +OFFSET, for that number of lines after the current line\n\
16127 or the start of the range\n\
16128 FUNCTION, for the first line in that function,\n\
16129 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16130 *ADDRESS, for the instruction at that address.\n\
16131\n\
16132The breakpoint will stop execution of the inferior whenever it executes\n\
16133an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16134range (including START-LOCATION and END-LOCATION)."));
16135
e7e0cddf 16136 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16137Set a dynamic printf at specified location.\n\
e7e0cddf 16138dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16139location may be a linespec, explicit, or address location.\n"
16140"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16141 set_cmd_completer (c, location_completer);
16142
16143 add_setshow_enum_cmd ("dprintf-style", class_support,
16144 dprintf_style_enums, &dprintf_style, _("\
16145Set the style of usage for dynamic printf."), _("\
16146Show the style of usage for dynamic printf."), _("\
16147This setting chooses how GDB will do a dynamic printf.\n\
16148If the value is \"gdb\", then the printing is done by GDB to its own\n\
16149console, as with the \"printf\" command.\n\
16150If the value is \"call\", the print is done by calling a function in your\n\
16151program; by default printf(), but you can choose a different function or\n\
16152output stream by setting dprintf-function and dprintf-channel."),
16153 update_dprintf_commands, NULL,
16154 &setlist, &showlist);
16155
16156 dprintf_function = xstrdup ("printf");
16157 add_setshow_string_cmd ("dprintf-function", class_support,
16158 &dprintf_function, _("\
16159Set the function to use for dynamic printf"), _("\
16160Show the function to use for dynamic printf"), NULL,
16161 update_dprintf_commands, NULL,
16162 &setlist, &showlist);
16163
16164 dprintf_channel = xstrdup ("");
16165 add_setshow_string_cmd ("dprintf-channel", class_support,
16166 &dprintf_channel, _("\
16167Set the channel to use for dynamic printf"), _("\
16168Show the channel to use for dynamic printf"), NULL,
16169 update_dprintf_commands, NULL,
16170 &setlist, &showlist);
16171
d3ce09f5
SS
16172 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16173 &disconnected_dprintf, _("\
16174Set whether dprintf continues after GDB disconnects."), _("\
16175Show whether dprintf continues after GDB disconnects."), _("\
16176Use this to let dprintf commands continue to hit and produce output\n\
16177even if GDB disconnects or detaches from the target."),
16178 NULL,
16179 NULL,
16180 &setlist, &showlist);
16181
16182 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16183agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16184(target agent only) This is useful for formatted output in user-defined commands."));
16185
765dc015 16186 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16187
16188 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16189 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16190}
This page took 3.111306 seconds and 4 git commands to generate.