x86: Pass "%F%P:" to linker callback in case of error
[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
834c0d03 164static int remove_breakpoint (struct bp_location *);
b2b6a7da 165static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
c906108c 166
e514a9d6 167static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 168
a14ed312 169static int hw_breakpoint_used_count (void);
c906108c 170
a1398e0c
PA
171static int hw_watchpoint_use_count (struct breakpoint *);
172
173static int hw_watchpoint_used_count_others (struct breakpoint *except,
174 enum bptype type,
175 int *other_type_used);
c906108c 176
816338b5
SS
177static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
178 int count);
c906108c 179
fe3f5fa8 180static void free_bp_location (struct bp_location *loc);
f431efe5
PA
181static void incref_bp_location (struct bp_location *loc);
182static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 183
39d61571 184static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 185
44702360
PA
186/* update_global_location_list's modes of operation wrt to whether to
187 insert locations now. */
188enum ugll_insert_mode
189{
190 /* Don't insert any breakpoint locations into the inferior, only
191 remove already-inserted locations that no longer should be
192 inserted. Functions that delete a breakpoint or breakpoints
193 should specify this mode, so that deleting a breakpoint doesn't
194 have the side effect of inserting the locations of other
195 breakpoints that are marked not-inserted, but should_be_inserted
196 returns true on them.
197
198 This behavior is useful is situations close to tear-down -- e.g.,
199 after an exec, while the target still has execution, but
200 breakpoint shadows of the previous executable image should *NOT*
201 be restored to the new image; or before detaching, where the
202 target still has execution and wants to delete breakpoints from
203 GDB's lists, and all breakpoints had already been removed from
204 the inferior. */
205 UGLL_DONT_INSERT,
206
a25a5a45
PA
207 /* May insert breakpoints iff breakpoints_should_be_inserted_now
208 claims breakpoints should be inserted now. */
04086b45
PA
209 UGLL_MAY_INSERT,
210
a25a5a45
PA
211 /* Insert locations now, irrespective of
212 breakpoints_should_be_inserted_now. E.g., say all threads are
213 stopped right now, and the user did "continue". We need to
214 insert breakpoints _before_ resuming the target, but
215 UGLL_MAY_INSERT wouldn't insert them, because
216 breakpoints_should_be_inserted_now returns false at that point,
217 as no thread is running yet. */
04086b45 218 UGLL_INSERT
44702360
PA
219};
220
221static void update_global_location_list (enum ugll_insert_mode);
a5606eee 222
44702360 223static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 224
d77f58be 225static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
226
227static void insert_breakpoint_locations (void);
a5606eee 228
0b39b52e 229static void trace_pass_command (const char *, int);
1042e4c0 230
558a9d82
YQ
231static void set_tracepoint_count (int num);
232
9c06b0b4
TJB
233static int is_masked_watchpoint (const struct breakpoint *b);
234
b775012e
LM
235static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
236
983af33b
SDJ
237/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
238 otherwise. */
239
240static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 241
2060206e
PA
242/* The breakpoint_ops structure to be inherited by all breakpoint_ops
243 that are implemented on top of software or hardware breakpoints
244 (user breakpoints, internal and momentary breakpoints, etc.). */
245static struct breakpoint_ops bkpt_base_breakpoint_ops;
246
247/* Internal breakpoints class type. */
06edf0c0 248static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
249
250/* Momentary breakpoints class type. */
06edf0c0
PA
251static struct breakpoint_ops momentary_breakpoint_ops;
252
2060206e
PA
253/* The breakpoint_ops structure to be used in regular user created
254 breakpoints. */
255struct breakpoint_ops bkpt_breakpoint_ops;
256
55aa24fb
SDJ
257/* Breakpoints set on probes. */
258static struct breakpoint_ops bkpt_probe_breakpoint_ops;
259
e7e0cddf 260/* Dynamic printf class type. */
c5867ab6 261struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 262
d3ce09f5
SS
263/* The style in which to perform a dynamic printf. This is a user
264 option because different output options have different tradeoffs;
265 if GDB does the printing, there is better error handling if there
266 is a problem with any of the arguments, but using an inferior
267 function lets you have special-purpose printers and sending of
268 output to the same place as compiled-in print functions. */
269
270static const char dprintf_style_gdb[] = "gdb";
271static const char dprintf_style_call[] = "call";
272static const char dprintf_style_agent[] = "agent";
273static const char *const dprintf_style_enums[] = {
274 dprintf_style_gdb,
275 dprintf_style_call,
276 dprintf_style_agent,
277 NULL
278};
279static const char *dprintf_style = dprintf_style_gdb;
280
281/* The function to use for dynamic printf if the preferred style is to
282 call into the inferior. The value is simply a string that is
283 copied into the command, so it can be anything that GDB can
284 evaluate to a callable address, not necessarily a function name. */
285
bde6261a 286static char *dprintf_function;
d3ce09f5
SS
287
288/* The channel to use for dynamic printf if the preferred style is to
289 call into the inferior; if a nonempty string, it will be passed to
290 the call as the first argument, with the format string as the
291 second. As with the dprintf function, this can be anything that
292 GDB knows how to evaluate, so in addition to common choices like
293 "stderr", this could be an app-specific expression like
294 "mystreams[curlogger]". */
295
bde6261a 296static char *dprintf_channel;
d3ce09f5
SS
297
298/* True if dprintf commands should continue to operate even if GDB
299 has disconnected. */
300static int disconnected_dprintf = 1;
301
5cea2a26
PA
302struct command_line *
303breakpoint_commands (struct breakpoint *b)
304{
d1b0a7bf 305 return b->commands ? b->commands.get () : NULL;
5cea2a26 306}
3daf8fe5 307
f3b1572e
PA
308/* Flag indicating that a command has proceeded the inferior past the
309 current breakpoint. */
310
311static int breakpoint_proceeded;
312
956a9fb9 313const char *
2cec12e5
AR
314bpdisp_text (enum bpdisp disp)
315{
4a64f543
MS
316 /* NOTE: the following values are a part of MI protocol and
317 represent values of 'disp' field returned when inferior stops at
318 a breakpoint. */
bc043ef3 319 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 320
2cec12e5
AR
321 return bpdisps[(int) disp];
322}
c906108c 323
4a64f543 324/* Prototypes for exported functions. */
c906108c 325/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 326 if such is available. */
c906108c
SS
327static int can_use_hw_watchpoints;
328
920d2a44
AC
329static void
330show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
331 struct cmd_list_element *c,
332 const char *value)
333{
3e43a32a
MS
334 fprintf_filtered (file,
335 _("Debugger's willingness to use "
336 "watchpoint hardware is %s.\n"),
920d2a44
AC
337 value);
338}
339
fa8d40ab
JJ
340/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
341 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 342 for unrecognized breakpoint locations.
fa8d40ab
JJ
343 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
344static enum auto_boolean pending_break_support;
920d2a44
AC
345static void
346show_pending_break_support (struct ui_file *file, int from_tty,
347 struct cmd_list_element *c,
348 const char *value)
349{
3e43a32a
MS
350 fprintf_filtered (file,
351 _("Debugger's behavior regarding "
352 "pending breakpoints is %s.\n"),
920d2a44
AC
353 value);
354}
fa8d40ab 355
765dc015 356/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 357 set with "break" but falling in read-only memory.
765dc015
VP
358 If 0, gdb will warn about such breakpoints, but won't automatically
359 use hardware breakpoints. */
360static int automatic_hardware_breakpoints;
361static void
362show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
363 struct cmd_list_element *c,
364 const char *value)
365{
3e43a32a
MS
366 fprintf_filtered (file,
367 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
368 value);
369}
370
a25a5a45
PA
371/* If on, GDB keeps breakpoints inserted even if the inferior is
372 stopped, and immediately inserts any new breakpoints as soon as
373 they're created. If off (default), GDB keeps breakpoints off of
374 the target as long as possible. That is, it delays inserting
375 breakpoints until the next resume, and removes them again when the
376 target fully stops. This is a bit safer in case GDB crashes while
377 processing user input. */
378static int always_inserted_mode = 0;
72d0e2c5 379
33e5cbd6 380static void
74960c60 381show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 382 struct cmd_list_element *c, const char *value)
74960c60 383{
a25a5a45
PA
384 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
385 value);
74960c60
VP
386}
387
b57bacec
PA
388/* See breakpoint.h. */
389
33e5cbd6 390int
a25a5a45 391breakpoints_should_be_inserted_now (void)
33e5cbd6 392{
a25a5a45
PA
393 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
394 {
395 /* If breakpoints are global, they should be inserted even if no
396 thread under gdb's control is running, or even if there are
397 no threads under GDB's control yet. */
398 return 1;
399 }
400 else if (target_has_execution)
401 {
372316f1
PA
402 struct thread_info *tp;
403
a25a5a45
PA
404 if (always_inserted_mode)
405 {
406 /* The user wants breakpoints inserted even if all threads
407 are stopped. */
408 return 1;
409 }
410
b57bacec
PA
411 if (threads_are_executing ())
412 return 1;
372316f1
PA
413
414 /* Don't remove breakpoints yet if, even though all threads are
415 stopped, we still have events to process. */
416 ALL_NON_EXITED_THREADS (tp)
417 if (tp->resumed
418 && tp->suspend.waitstatus_pending_p)
419 return 1;
a25a5a45
PA
420 }
421 return 0;
33e5cbd6 422}
765dc015 423
b775012e
LM
424static const char condition_evaluation_both[] = "host or target";
425
426/* Modes for breakpoint condition evaluation. */
427static const char condition_evaluation_auto[] = "auto";
428static const char condition_evaluation_host[] = "host";
429static const char condition_evaluation_target[] = "target";
430static const char *const condition_evaluation_enums[] = {
431 condition_evaluation_auto,
432 condition_evaluation_host,
433 condition_evaluation_target,
434 NULL
435};
436
437/* Global that holds the current mode for breakpoint condition evaluation. */
438static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
439
440/* Global that we use to display information to the user (gets its value from
441 condition_evaluation_mode_1. */
442static const char *condition_evaluation_mode = condition_evaluation_auto;
443
444/* Translate a condition evaluation mode MODE into either "host"
445 or "target". This is used mostly to translate from "auto" to the
446 real setting that is being used. It returns the translated
447 evaluation mode. */
448
449static const char *
450translate_condition_evaluation_mode (const char *mode)
451{
452 if (mode == condition_evaluation_auto)
453 {
454 if (target_supports_evaluation_of_breakpoint_conditions ())
455 return condition_evaluation_target;
456 else
457 return condition_evaluation_host;
458 }
459 else
460 return mode;
461}
462
463/* Discovers what condition_evaluation_auto translates to. */
464
465static const char *
466breakpoint_condition_evaluation_mode (void)
467{
468 return translate_condition_evaluation_mode (condition_evaluation_mode);
469}
470
471/* Return true if GDB should evaluate breakpoint conditions or false
472 otherwise. */
473
474static int
475gdb_evaluates_breakpoint_condition_p (void)
476{
477 const char *mode = breakpoint_condition_evaluation_mode ();
478
479 return (mode == condition_evaluation_host);
480}
481
c906108c
SS
482/* Are we executing breakpoint commands? */
483static int executing_breakpoint_commands;
484
c02f5703
MS
485/* Are overlay event breakpoints enabled? */
486static int overlay_events_enabled;
487
e09342b5
TJB
488/* See description in breakpoint.h. */
489int target_exact_watchpoints = 0;
490
c906108c 491/* Walk the following statement or block through all breakpoints.
e5dd4106 492 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 493 current breakpoint. */
c906108c 494
5c44784c 495#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 496
5c44784c
JM
497#define ALL_BREAKPOINTS_SAFE(B,TMP) \
498 for (B = breakpoint_chain; \
499 B ? (TMP=B->next, 1): 0; \
500 B = TMP)
c906108c 501
4a64f543
MS
502/* Similar iterator for the low-level breakpoints. SAFE variant is
503 not provided so update_global_location_list must not be called
504 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 505
876fa593 506#define ALL_BP_LOCATIONS(B,BP_TMP) \
f5336ca5
PA
507 for (BP_TMP = bp_locations; \
508 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
876fa593 509 BP_TMP++)
7cc221ef 510
b775012e
LM
511/* Iterates through locations with address ADDRESS for the currently selected
512 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
513 to where the loop should start from.
514 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
515 appropriate location to start with. */
516
517#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
518 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
519 BP_LOCP_TMP = BP_LOCP_START; \
520 BP_LOCP_START \
f5336ca5 521 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
b775012e
LM
522 && (*BP_LOCP_TMP)->address == ADDRESS); \
523 BP_LOCP_TMP++)
524
1042e4c0
SS
525/* Iterator for tracepoints only. */
526
527#define ALL_TRACEPOINTS(B) \
528 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 529 if (is_tracepoint (B))
1042e4c0 530
7cc221ef 531/* Chains of all breakpoints defined. */
c906108c
SS
532
533struct breakpoint *breakpoint_chain;
534
f5336ca5 535/* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
876fa593 536
f5336ca5 537static struct bp_location **bp_locations;
876fa593 538
f5336ca5 539/* Number of elements of BP_LOCATIONS. */
876fa593 540
f5336ca5 541static unsigned bp_locations_count;
876fa593 542
4a64f543 543/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
f5336ca5 544 ADDRESS for the current elements of BP_LOCATIONS which get a valid
4a64f543 545 result from bp_location_has_shadow. You can use it for roughly
f5336ca5 546 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
4a64f543 547 an address you need to read. */
876fa593 548
f5336ca5 549static CORE_ADDR bp_locations_placed_address_before_address_max;
876fa593 550
4a64f543
MS
551/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
552 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
f5336ca5
PA
553 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
554 You can use it for roughly limiting the subrange of BP_LOCATIONS to
4a64f543 555 scan for shadow bytes for an address you need to read. */
876fa593 556
f5336ca5 557static CORE_ADDR bp_locations_shadow_len_after_address_max;
7cc221ef 558
4a64f543 559/* The locations that no longer correspond to any breakpoint, unlinked
f5336ca5
PA
560 from the bp_locations array, but for which a hit may still be
561 reported by a target. */
20874c92
VP
562VEC(bp_location_p) *moribund_locations = NULL;
563
c906108c
SS
564/* Number of last breakpoint made. */
565
95a42b64
TT
566static int breakpoint_count;
567
86b17b60
PA
568/* The value of `breakpoint_count' before the last command that
569 created breakpoints. If the last (break-like) command created more
570 than one breakpoint, then the difference between BREAKPOINT_COUNT
571 and PREV_BREAKPOINT_COUNT is more than one. */
572static int prev_breakpoint_count;
c906108c 573
1042e4c0
SS
574/* Number of last tracepoint made. */
575
95a42b64 576static int tracepoint_count;
1042e4c0 577
6149aea9
PA
578static struct cmd_list_element *breakpoint_set_cmdlist;
579static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 580struct cmd_list_element *save_cmdlist;
6149aea9 581
badd37ce
SDJ
582/* See declaration at breakpoint.h. */
583
584struct breakpoint *
585breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
586 void *user_data)
587{
588 struct breakpoint *b = NULL;
589
590 ALL_BREAKPOINTS (b)
591 {
592 if (func (b, user_data) != 0)
593 break;
594 }
595
596 return b;
597}
598
468d015d
JJ
599/* Return whether a breakpoint is an active enabled breakpoint. */
600static int
601breakpoint_enabled (struct breakpoint *b)
602{
0d381245 603 return (b->enable_state == bp_enabled);
468d015d
JJ
604}
605
c906108c
SS
606/* Set breakpoint count to NUM. */
607
95a42b64 608static void
fba45db2 609set_breakpoint_count (int num)
c906108c 610{
86b17b60 611 prev_breakpoint_count = breakpoint_count;
c906108c 612 breakpoint_count = num;
4fa62494 613 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
614}
615
86b17b60
PA
616/* Used by `start_rbreak_breakpoints' below, to record the current
617 breakpoint count before "rbreak" creates any breakpoint. */
618static int rbreak_start_breakpoint_count;
619
95a42b64
TT
620/* Called at the start an "rbreak" command to record the first
621 breakpoint made. */
86b17b60 622
c80049d3 623scoped_rbreak_breakpoints::scoped_rbreak_breakpoints ()
95a42b64 624{
86b17b60 625 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
626}
627
628/* Called at the end of an "rbreak" command to record the last
629 breakpoint made. */
86b17b60 630
c80049d3 631scoped_rbreak_breakpoints::~scoped_rbreak_breakpoints ()
95a42b64 632{
86b17b60 633 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
634}
635
4a64f543 636/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
637
638void
fba45db2 639clear_breakpoint_hit_counts (void)
c906108c
SS
640{
641 struct breakpoint *b;
642
643 ALL_BREAKPOINTS (b)
644 b->hit_count = 0;
645}
646
c906108c 647\f
48cb2d85
VP
648/* Return the breakpoint with the specified number, or NULL
649 if the number does not refer to an existing breakpoint. */
650
651struct breakpoint *
652get_breakpoint (int num)
653{
654 struct breakpoint *b;
655
656 ALL_BREAKPOINTS (b)
657 if (b->number == num)
658 return b;
659
660 return NULL;
661}
5c44784c 662
c906108c 663\f
adc36818 664
b775012e
LM
665/* Mark locations as "conditions have changed" in case the target supports
666 evaluating conditions on its side. */
667
668static void
669mark_breakpoint_modified (struct breakpoint *b)
670{
671 struct bp_location *loc;
672
673 /* This is only meaningful if the target is
674 evaluating conditions and if the user has
675 opted for condition evaluation on the target's
676 side. */
677 if (gdb_evaluates_breakpoint_condition_p ()
678 || !target_supports_evaluation_of_breakpoint_conditions ())
679 return;
680
681 if (!is_breakpoint (b))
682 return;
683
684 for (loc = b->loc; loc; loc = loc->next)
685 loc->condition_changed = condition_modified;
686}
687
688/* Mark location as "conditions have changed" in case the target supports
689 evaluating conditions on its side. */
690
691static void
692mark_breakpoint_location_modified (struct bp_location *loc)
693{
694 /* This is only meaningful if the target is
695 evaluating conditions and if the user has
696 opted for condition evaluation on the target's
697 side. */
698 if (gdb_evaluates_breakpoint_condition_p ()
699 || !target_supports_evaluation_of_breakpoint_conditions ())
700
701 return;
702
703 if (!is_breakpoint (loc->owner))
704 return;
705
706 loc->condition_changed = condition_modified;
707}
708
709/* Sets the condition-evaluation mode using the static global
710 condition_evaluation_mode. */
711
712static void
eb4c3f4a 713set_condition_evaluation_mode (const char *args, int from_tty,
b775012e
LM
714 struct cmd_list_element *c)
715{
b775012e
LM
716 const char *old_mode, *new_mode;
717
718 if ((condition_evaluation_mode_1 == condition_evaluation_target)
719 && !target_supports_evaluation_of_breakpoint_conditions ())
720 {
721 condition_evaluation_mode_1 = condition_evaluation_mode;
722 warning (_("Target does not support breakpoint condition evaluation.\n"
723 "Using host evaluation mode instead."));
724 return;
725 }
726
727 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
728 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
729
abf1152a
JK
730 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
731 settings was "auto". */
732 condition_evaluation_mode = condition_evaluation_mode_1;
733
b775012e
LM
734 /* Only update the mode if the user picked a different one. */
735 if (new_mode != old_mode)
736 {
737 struct bp_location *loc, **loc_tmp;
738 /* If the user switched to a different evaluation mode, we
739 need to synch the changes with the target as follows:
740
741 "host" -> "target": Send all (valid) conditions to the target.
742 "target" -> "host": Remove all the conditions from the target.
743 */
744
b775012e
LM
745 if (new_mode == condition_evaluation_target)
746 {
747 /* Mark everything modified and synch conditions with the
748 target. */
749 ALL_BP_LOCATIONS (loc, loc_tmp)
750 mark_breakpoint_location_modified (loc);
751 }
752 else
753 {
754 /* Manually mark non-duplicate locations to synch conditions
755 with the target. We do this to remove all the conditions the
756 target knows about. */
757 ALL_BP_LOCATIONS (loc, loc_tmp)
758 if (is_breakpoint (loc->owner) && loc->inserted)
759 loc->needs_update = 1;
760 }
761
762 /* Do the update. */
44702360 763 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
764 }
765
766 return;
767}
768
769/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
770 what "auto" is translating to. */
771
772static void
773show_condition_evaluation_mode (struct ui_file *file, int from_tty,
774 struct cmd_list_element *c, const char *value)
775{
776 if (condition_evaluation_mode == condition_evaluation_auto)
777 fprintf_filtered (file,
778 _("Breakpoint condition evaluation "
779 "mode is %s (currently %s).\n"),
780 value,
781 breakpoint_condition_evaluation_mode ());
782 else
783 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
784 value);
785}
786
787/* A comparison function for bp_location AP and BP that is used by
788 bsearch. This comparison function only cares about addresses, unlike
f5336ca5 789 the more general bp_locations_compare function. */
b775012e
LM
790
791static int
f5336ca5 792bp_locations_compare_addrs (const void *ap, const void *bp)
b775012e 793{
9a3c8263
SM
794 const struct bp_location *a = *(const struct bp_location **) ap;
795 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
796
797 if (a->address == b->address)
798 return 0;
799 else
800 return ((a->address > b->address) - (a->address < b->address));
801}
802
803/* Helper function to skip all bp_locations with addresses
804 less than ADDRESS. It returns the first bp_location that
805 is greater than or equal to ADDRESS. If none is found, just
806 return NULL. */
807
808static struct bp_location **
809get_first_locp_gte_addr (CORE_ADDR address)
810{
811 struct bp_location dummy_loc;
812 struct bp_location *dummy_locp = &dummy_loc;
813 struct bp_location **locp_found = NULL;
814
815 /* Initialize the dummy location's address field. */
b775012e
LM
816 dummy_loc.address = address;
817
818 /* Find a close match to the first location at ADDRESS. */
9a3c8263 819 locp_found = ((struct bp_location **)
f5336ca5 820 bsearch (&dummy_locp, bp_locations, bp_locations_count,
9a3c8263 821 sizeof (struct bp_location **),
f5336ca5 822 bp_locations_compare_addrs));
b775012e
LM
823
824 /* Nothing was found, nothing left to do. */
825 if (locp_found == NULL)
826 return NULL;
827
828 /* We may have found a location that is at ADDRESS but is not the first in the
829 location's list. Go backwards (if possible) and locate the first one. */
f5336ca5 830 while ((locp_found - 1) >= bp_locations
b775012e
LM
831 && (*(locp_found - 1))->address == address)
832 locp_found--;
833
834 return locp_found;
835}
836
adc36818 837void
7a26bd4d 838set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
839 int from_tty)
840{
3a5c3e22
PA
841 xfree (b->cond_string);
842 b->cond_string = NULL;
adc36818 843
3a5c3e22 844 if (is_watchpoint (b))
adc36818 845 {
3a5c3e22
PA
846 struct watchpoint *w = (struct watchpoint *) b;
847
4d01a485 848 w->cond_exp.reset ();
3a5c3e22
PA
849 }
850 else
851 {
852 struct bp_location *loc;
853
854 for (loc = b->loc; loc; loc = loc->next)
855 {
4d01a485 856 loc->cond.reset ();
b775012e
LM
857
858 /* No need to free the condition agent expression
859 bytecode (if we have one). We will handle this
860 when we go through update_global_location_list. */
3a5c3e22 861 }
adc36818 862 }
adc36818
PM
863
864 if (*exp == 0)
865 {
866 if (from_tty)
867 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
868 }
869 else
870 {
bbc13ae3 871 const char *arg = exp;
cc59ec59 872
adc36818
PM
873 /* I don't know if it matters whether this is the string the user
874 typed in or the decompiled expression. */
875 b->cond_string = xstrdup (arg);
876 b->condition_not_parsed = 0;
877
878 if (is_watchpoint (b))
879 {
3a5c3e22
PA
880 struct watchpoint *w = (struct watchpoint *) b;
881
adc36818
PM
882 innermost_block = NULL;
883 arg = exp;
1bb9788d 884 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
885 if (*arg)
886 error (_("Junk at end of expression"));
3a5c3e22 887 w->cond_exp_valid_block = innermost_block;
adc36818
PM
888 }
889 else
890 {
3a5c3e22
PA
891 struct bp_location *loc;
892
adc36818
PM
893 for (loc = b->loc; loc; loc = loc->next)
894 {
895 arg = exp;
896 loc->cond =
1bb9788d
TT
897 parse_exp_1 (&arg, loc->address,
898 block_for_pc (loc->address), 0);
adc36818
PM
899 if (*arg)
900 error (_("Junk at end of expression"));
901 }
902 }
903 }
b775012e
LM
904 mark_breakpoint_modified (b);
905
8d3788bd 906 observer_notify_breakpoint_modified (b);
adc36818
PM
907}
908
d55637df
TT
909/* Completion for the "condition" command. */
910
eb3ff9a5 911static void
6f937416 912condition_completer (struct cmd_list_element *cmd,
eb3ff9a5 913 completion_tracker &tracker,
6f937416 914 const char *text, const char *word)
d55637df 915{
6f937416 916 const char *space;
d55637df 917
f1735a53
TT
918 text = skip_spaces (text);
919 space = skip_to_space (text);
d55637df
TT
920 if (*space == '\0')
921 {
922 int len;
923 struct breakpoint *b;
924 VEC (char_ptr) *result = NULL;
925
926 if (text[0] == '$')
927 {
928 /* We don't support completion of history indices. */
eb3ff9a5
PA
929 if (!isdigit (text[1]))
930 complete_internalvar (tracker, &text[1]);
931 return;
d55637df
TT
932 }
933
934 /* We're completing the breakpoint number. */
935 len = strlen (text);
936
937 ALL_BREAKPOINTS (b)
58ce7251
SDJ
938 {
939 char number[50];
940
941 xsnprintf (number, sizeof (number), "%d", b->number);
942
943 if (strncmp (number, text, len) == 0)
eb3ff9a5
PA
944 {
945 gdb::unique_xmalloc_ptr<char> copy (xstrdup (number));
946 tracker.add_completion (std::move (copy));
947 }
58ce7251 948 }
d55637df 949
eb3ff9a5 950 return;
d55637df
TT
951 }
952
953 /* We're completing the expression part. */
f1735a53 954 text = skip_spaces (space);
eb3ff9a5 955 expression_completer (cmd, tracker, text, word);
d55637df
TT
956}
957
c906108c
SS
958/* condition N EXP -- set break condition of breakpoint N to EXP. */
959
960static void
0b39b52e 961condition_command (const char *arg, int from_tty)
c906108c 962{
52f0bd74 963 struct breakpoint *b;
0b39b52e 964 const char *p;
52f0bd74 965 int bnum;
c906108c
SS
966
967 if (arg == 0)
e2e0b3e5 968 error_no_arg (_("breakpoint number"));
c906108c
SS
969
970 p = arg;
971 bnum = get_number (&p);
5c44784c 972 if (bnum == 0)
8a3fe4f8 973 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
974
975 ALL_BREAKPOINTS (b)
976 if (b->number == bnum)
2f069f6f 977 {
6dddc817
DE
978 /* Check if this breakpoint has a "stop" method implemented in an
979 extension language. This method and conditions entered into GDB
980 from the CLI are mutually exclusive. */
981 const struct extension_language_defn *extlang
982 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
983
984 if (extlang != NULL)
985 {
986 error (_("Only one stop condition allowed. There is currently"
987 " a %s stop condition defined for this breakpoint."),
988 ext_lang_capitalized_name (extlang));
989 }
2566ad2d 990 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
991
992 if (is_breakpoint (b))
44702360 993 update_global_location_list (UGLL_MAY_INSERT);
b775012e 994
2f069f6f
JB
995 return;
996 }
c906108c 997
8a3fe4f8 998 error (_("No breakpoint number %d."), bnum);
c906108c
SS
999}
1000
a7bdde9e
VP
1001/* Check that COMMAND do not contain commands that are suitable
1002 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1003 Throw if any such commands is found. */
1004
a7bdde9e
VP
1005static void
1006check_no_tracepoint_commands (struct command_line *commands)
1007{
1008 struct command_line *c;
cc59ec59 1009
a7bdde9e
VP
1010 for (c = commands; c; c = c->next)
1011 {
1012 int i;
1013
1014 if (c->control_type == while_stepping_control)
3e43a32a
MS
1015 error (_("The 'while-stepping' command can "
1016 "only be used for tracepoints"));
a7bdde9e
VP
1017
1018 for (i = 0; i < c->body_count; ++i)
1019 check_no_tracepoint_commands ((c->body_list)[i]);
1020
1021 /* Not that command parsing removes leading whitespace and comment
4a64f543 1022 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1023 command directly. */
1024 if (strstr (c->line, "collect ") == c->line)
1025 error (_("The 'collect' command can only be used for tracepoints"));
1026
51661e93
VP
1027 if (strstr (c->line, "teval ") == c->line)
1028 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1029 }
1030}
1031
c1fc2657 1032struct longjmp_breakpoint : public breakpoint
3b0871f4 1033{
c1fc2657 1034 ~longjmp_breakpoint () override;
3b0871f4
SM
1035};
1036
d77f58be
SS
1037/* Encapsulate tests for different types of tracepoints. */
1038
3b0871f4
SM
1039static bool
1040is_tracepoint_type (bptype type)
d9b3f62e
PA
1041{
1042 return (type == bp_tracepoint
1043 || type == bp_fast_tracepoint
1044 || type == bp_static_tracepoint);
1045}
1046
3b0871f4
SM
1047static bool
1048is_longjmp_type (bptype type)
1049{
1050 return type == bp_longjmp || type == bp_exception;
1051}
1052
a7bdde9e 1053int
d77f58be 1054is_tracepoint (const struct breakpoint *b)
a7bdde9e 1055{
d9b3f62e 1056 return is_tracepoint_type (b->type);
a7bdde9e 1057}
d9b3f62e 1058
a5e364af
SM
1059/* Factory function to create an appropriate instance of breakpoint given
1060 TYPE. */
1061
1062static std::unique_ptr<breakpoint>
1063new_breakpoint_from_type (bptype type)
1064{
1065 breakpoint *b;
1066
1067 if (is_tracepoint_type (type))
c1fc2657 1068 b = new tracepoint ();
3b0871f4 1069 else if (is_longjmp_type (type))
c1fc2657 1070 b = new longjmp_breakpoint ();
a5e364af
SM
1071 else
1072 b = new breakpoint ();
1073
1074 return std::unique_ptr<breakpoint> (b);
1075}
1076
e5dd4106 1077/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1078 breakpoint. This function will throw an exception if a problem is
1079 found. */
48cb2d85 1080
95a42b64
TT
1081static void
1082validate_commands_for_breakpoint (struct breakpoint *b,
1083 struct command_line *commands)
48cb2d85 1084{
d77f58be 1085 if (is_tracepoint (b))
a7bdde9e 1086 {
c9a6ce02 1087 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1088 struct command_line *c;
1089 struct command_line *while_stepping = 0;
c9a6ce02
PA
1090
1091 /* Reset the while-stepping step count. The previous commands
1092 might have included a while-stepping action, while the new
1093 ones might not. */
1094 t->step_count = 0;
1095
1096 /* We need to verify that each top-level element of commands is
1097 valid for tracepoints, that there's at most one
1098 while-stepping element, and that the while-stepping's body
1099 has valid tracing commands excluding nested while-stepping.
1100 We also need to validate the tracepoint action line in the
1101 context of the tracepoint --- validate_actionline actually
1102 has side effects, like setting the tracepoint's
1103 while-stepping STEP_COUNT, in addition to checking if the
1104 collect/teval actions parse and make sense in the
1105 tracepoint's context. */
a7bdde9e
VP
1106 for (c = commands; c; c = c->next)
1107 {
a7bdde9e
VP
1108 if (c->control_type == while_stepping_control)
1109 {
1110 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1111 error (_("The 'while-stepping' command "
1112 "cannot be used for fast tracepoint"));
0fb4aa4b 1113 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1114 error (_("The 'while-stepping' command "
1115 "cannot be used for static tracepoint"));
a7bdde9e
VP
1116
1117 if (while_stepping)
3e43a32a
MS
1118 error (_("The 'while-stepping' command "
1119 "can be used only once"));
a7bdde9e
VP
1120 else
1121 while_stepping = c;
1122 }
c9a6ce02
PA
1123
1124 validate_actionline (c->line, b);
a7bdde9e
VP
1125 }
1126 if (while_stepping)
1127 {
1128 struct command_line *c2;
1129
1130 gdb_assert (while_stepping->body_count == 1);
1131 c2 = while_stepping->body_list[0];
1132 for (; c2; c2 = c2->next)
1133 {
a7bdde9e
VP
1134 if (c2->control_type == while_stepping_control)
1135 error (_("The 'while-stepping' command cannot be nested"));
1136 }
1137 }
1138 }
1139 else
1140 {
1141 check_no_tracepoint_commands (commands);
1142 }
95a42b64
TT
1143}
1144
0fb4aa4b
PA
1145/* Return a vector of all the static tracepoints set at ADDR. The
1146 caller is responsible for releasing the vector. */
1147
1148VEC(breakpoint_p) *
1149static_tracepoints_here (CORE_ADDR addr)
1150{
1151 struct breakpoint *b;
1152 VEC(breakpoint_p) *found = 0;
1153 struct bp_location *loc;
1154
1155 ALL_BREAKPOINTS (b)
1156 if (b->type == bp_static_tracepoint)
1157 {
1158 for (loc = b->loc; loc; loc = loc->next)
1159 if (loc->address == addr)
1160 VEC_safe_push(breakpoint_p, found, b);
1161 }
1162
1163 return found;
1164}
1165
95a42b64 1166/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1167 validate that only allowed commands are included. */
95a42b64
TT
1168
1169void
4a64f543 1170breakpoint_set_commands (struct breakpoint *b,
93921405 1171 command_line_up &&commands)
95a42b64 1172{
93921405 1173 validate_commands_for_breakpoint (b, commands.get ());
a7bdde9e 1174
d1b0a7bf 1175 b->commands = std::move (commands);
8d3788bd 1176 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1177}
1178
45a43567
TT
1179/* Set the internal `silent' flag on the breakpoint. Note that this
1180 is not the same as the "silent" that may appear in the breakpoint's
1181 commands. */
1182
1183void
1184breakpoint_set_silent (struct breakpoint *b, int silent)
1185{
1186 int old_silent = b->silent;
1187
1188 b->silent = silent;
1189 if (old_silent != silent)
8d3788bd 1190 observer_notify_breakpoint_modified (b);
45a43567
TT
1191}
1192
1193/* Set the thread for this breakpoint. If THREAD is -1, make the
1194 breakpoint work for any thread. */
1195
1196void
1197breakpoint_set_thread (struct breakpoint *b, int thread)
1198{
1199 int old_thread = b->thread;
1200
1201 b->thread = thread;
1202 if (old_thread != thread)
8d3788bd 1203 observer_notify_breakpoint_modified (b);
45a43567
TT
1204}
1205
1206/* Set the task for this breakpoint. If TASK is 0, make the
1207 breakpoint work for any task. */
1208
1209void
1210breakpoint_set_task (struct breakpoint *b, int task)
1211{
1212 int old_task = b->task;
1213
1214 b->task = task;
1215 if (old_task != task)
8d3788bd 1216 observer_notify_breakpoint_modified (b);
45a43567
TT
1217}
1218
95a42b64
TT
1219void
1220check_tracepoint_command (char *line, void *closure)
a7bdde9e 1221{
9a3c8263 1222 struct breakpoint *b = (struct breakpoint *) closure;
cc59ec59 1223
6f937416 1224 validate_actionline (line, b);
a7bdde9e
VP
1225}
1226
95a42b64 1227static void
896b6bda 1228commands_command_1 (const char *arg, int from_tty,
4a64f543 1229 struct command_line *control)
95a42b64 1230{
d1b0a7bf 1231 counted_command_line cmd;
95a42b64 1232
896b6bda
PA
1233 std::string new_arg;
1234
95a42b64
TT
1235 if (arg == NULL || !*arg)
1236 {
86b17b60 1237 if (breakpoint_count - prev_breakpoint_count > 1)
896b6bda
PA
1238 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1239 breakpoint_count);
95a42b64 1240 else if (breakpoint_count > 0)
896b6bda 1241 new_arg = string_printf ("%d", breakpoint_count);
48649e1b
TT
1242 arg = new_arg.c_str ();
1243 }
1244
1245 map_breakpoint_numbers
1246 (arg, [&] (breakpoint *b)
1247 {
1248 if (cmd == NULL)
1249 {
48649e1b 1250 if (control != NULL)
d1b0a7bf 1251 cmd = copy_command_lines (control->body_list[0]);
48649e1b
TT
1252 else
1253 {
81b1e71c
TT
1254 std::string str
1255 = string_printf (_("Type commands for breakpoint(s) "
1256 "%s, one per line."),
1257 arg);
48649e1b 1258
81b1e71c 1259 cmd = read_command_lines (&str[0],
d1b0a7bf
TT
1260 from_tty, 1,
1261 (is_tracepoint (b)
1262 ? check_tracepoint_command : 0),
1263 b);
48649e1b 1264 }
48649e1b
TT
1265 }
1266
1267 /* If a breakpoint was on the list more than once, we don't need to
1268 do anything. */
1269 if (b->commands != cmd)
1270 {
d1b0a7bf 1271 validate_commands_for_breakpoint (b, cmd.get ());
48649e1b
TT
1272 b->commands = cmd;
1273 observer_notify_breakpoint_modified (b);
1274 }
1275 });
95a42b64 1276
48649e1b 1277 if (cmd == NULL)
95a42b64 1278 error (_("No breakpoints specified."));
95a42b64
TT
1279}
1280
1281static void
0b39b52e 1282commands_command (const char *arg, int from_tty)
95a42b64
TT
1283{
1284 commands_command_1 (arg, from_tty, NULL);
c906108c 1285}
40c03ae8
EZ
1286
1287/* Like commands_command, but instead of reading the commands from
1288 input stream, takes them from an already parsed command structure.
1289
1290 This is used by cli-script.c to DTRT with breakpoint commands
1291 that are part of if and while bodies. */
1292enum command_control_type
896b6bda 1293commands_from_control_command (const char *arg, struct command_line *cmd)
40c03ae8 1294{
95a42b64
TT
1295 commands_command_1 (arg, 0, cmd);
1296 return simple_control;
40c03ae8 1297}
876fa593
JK
1298
1299/* Return non-zero if BL->TARGET_INFO contains valid information. */
1300
1301static int
1302bp_location_has_shadow (struct bp_location *bl)
1303{
1304 if (bl->loc_type != bp_loc_software_breakpoint)
1305 return 0;
1306 if (!bl->inserted)
1307 return 0;
1308 if (bl->target_info.shadow_len == 0)
e5dd4106 1309 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1310 return 0;
1311 return 1;
1312}
1313
9d497a19
PA
1314/* Update BUF, which is LEN bytes read from the target address
1315 MEMADDR, by replacing a memory breakpoint with its shadowed
1316 contents.
1317
1318 If READBUF is not NULL, this buffer must not overlap with the of
1319 the breakpoint location's shadow_contents buffer. Otherwise, a
1320 failed assertion internal error will be raised. */
1321
1322static void
1323one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1324 const gdb_byte *writebuf_org,
1325 ULONGEST memaddr, LONGEST len,
1326 struct bp_target_info *target_info,
1327 struct gdbarch *gdbarch)
1328{
1329 /* Now do full processing of the found relevant range of elements. */
1330 CORE_ADDR bp_addr = 0;
1331 int bp_size = 0;
1332 int bptoffset = 0;
1333
1334 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1335 current_program_space->aspace, 0))
1336 {
1337 /* The breakpoint is inserted in a different address space. */
1338 return;
1339 }
1340
1341 /* Addresses and length of the part of the breakpoint that
1342 we need to copy. */
1343 bp_addr = target_info->placed_address;
1344 bp_size = target_info->shadow_len;
1345
1346 if (bp_addr + bp_size <= memaddr)
1347 {
1348 /* The breakpoint is entirely before the chunk of memory we are
1349 reading. */
1350 return;
1351 }
1352
1353 if (bp_addr >= memaddr + len)
1354 {
1355 /* The breakpoint is entirely after the chunk of memory we are
1356 reading. */
1357 return;
1358 }
1359
1360 /* Offset within shadow_contents. */
1361 if (bp_addr < memaddr)
1362 {
1363 /* Only copy the second part of the breakpoint. */
1364 bp_size -= memaddr - bp_addr;
1365 bptoffset = memaddr - bp_addr;
1366 bp_addr = memaddr;
1367 }
1368
1369 if (bp_addr + bp_size > memaddr + len)
1370 {
1371 /* Only copy the first part of the breakpoint. */
1372 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1373 }
1374
1375 if (readbuf != NULL)
1376 {
1377 /* Verify that the readbuf buffer does not overlap with the
1378 shadow_contents buffer. */
1379 gdb_assert (target_info->shadow_contents >= readbuf + len
1380 || readbuf >= (target_info->shadow_contents
1381 + target_info->shadow_len));
1382
1383 /* Update the read buffer with this inserted breakpoint's
1384 shadow. */
1385 memcpy (readbuf + bp_addr - memaddr,
1386 target_info->shadow_contents + bptoffset, bp_size);
1387 }
1388 else
1389 {
1390 const unsigned char *bp;
0d5ed153
MR
1391 CORE_ADDR addr = target_info->reqstd_address;
1392 int placed_size;
9d497a19
PA
1393
1394 /* Update the shadow with what we want to write to memory. */
1395 memcpy (target_info->shadow_contents + bptoffset,
1396 writebuf_org + bp_addr - memaddr, bp_size);
1397
1398 /* Determine appropriate breakpoint contents and size for this
1399 address. */
0d5ed153 1400 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1401
1402 /* Update the final write buffer with this inserted
1403 breakpoint's INSN. */
1404 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1405 }
1406}
1407
8defab1a 1408/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1409 by replacing any memory breakpoints with their shadowed contents.
1410
35c63cd8
JB
1411 If READBUF is not NULL, this buffer must not overlap with any of
1412 the breakpoint location's shadow_contents buffers. Otherwise,
1413 a failed assertion internal error will be raised.
1414
876fa593 1415 The range of shadowed area by each bp_location is:
f5336ca5
PA
1416 bl->address - bp_locations_placed_address_before_address_max
1417 up to bl->address + bp_locations_shadow_len_after_address_max
876fa593
JK
1418 The range we were requested to resolve shadows for is:
1419 memaddr ... memaddr + len
1420 Thus the safe cutoff boundaries for performance optimization are
35df4500 1421 memaddr + len <= (bl->address
f5336ca5 1422 - bp_locations_placed_address_before_address_max)
876fa593 1423 and:
f5336ca5 1424 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
c906108c 1425
8defab1a 1426void
f0ba3972
PA
1427breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1428 const gdb_byte *writebuf_org,
1429 ULONGEST memaddr, LONGEST len)
c906108c 1430{
4a64f543
MS
1431 /* Left boundary, right boundary and median element of our binary
1432 search. */
876fa593
JK
1433 unsigned bc_l, bc_r, bc;
1434
4a64f543
MS
1435 /* Find BC_L which is a leftmost element which may affect BUF
1436 content. It is safe to report lower value but a failure to
1437 report higher one. */
876fa593
JK
1438
1439 bc_l = 0;
f5336ca5 1440 bc_r = bp_locations_count;
876fa593
JK
1441 while (bc_l + 1 < bc_r)
1442 {
35df4500 1443 struct bp_location *bl;
876fa593
JK
1444
1445 bc = (bc_l + bc_r) / 2;
f5336ca5 1446 bl = bp_locations[bc];
876fa593 1447
4a64f543
MS
1448 /* Check first BL->ADDRESS will not overflow due to the added
1449 constant. Then advance the left boundary only if we are sure
1450 the BC element can in no way affect the BUF content (MEMADDR
1451 to MEMADDR + LEN range).
876fa593 1452
f5336ca5 1453 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
4a64f543
MS
1454 offset so that we cannot miss a breakpoint with its shadow
1455 range tail still reaching MEMADDR. */
c5aa993b 1456
f5336ca5 1457 if ((bl->address + bp_locations_shadow_len_after_address_max
35df4500 1458 >= bl->address)
f5336ca5 1459 && (bl->address + bp_locations_shadow_len_after_address_max
35df4500 1460 <= memaddr))
876fa593
JK
1461 bc_l = bc;
1462 else
1463 bc_r = bc;
1464 }
1465
128070bb
PA
1466 /* Due to the binary search above, we need to make sure we pick the
1467 first location that's at BC_L's address. E.g., if there are
1468 multiple locations at the same address, BC_L may end up pointing
1469 at a duplicate location, and miss the "master"/"inserted"
1470 location. Say, given locations L1, L2 and L3 at addresses A and
1471 B:
1472
1473 L1@A, L2@A, L3@B, ...
1474
1475 BC_L could end up pointing at location L2, while the "master"
1476 location could be L1. Since the `loc->inserted' flag is only set
1477 on "master" locations, we'd forget to restore the shadow of L1
1478 and L2. */
1479 while (bc_l > 0
f5336ca5 1480 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
128070bb
PA
1481 bc_l--;
1482
876fa593
JK
1483 /* Now do full processing of the found relevant range of elements. */
1484
f5336ca5 1485 for (bc = bc_l; bc < bp_locations_count; bc++)
c5aa993b 1486 {
f5336ca5 1487 struct bp_location *bl = bp_locations[bc];
876fa593 1488
35df4500
TJB
1489 /* bp_location array has BL->OWNER always non-NULL. */
1490 if (bl->owner->type == bp_none)
8a3fe4f8 1491 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1492 bl->owner->number);
ffce0d52 1493
e5dd4106 1494 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1495 content. */
1496
f5336ca5
PA
1497 if (bl->address >= bp_locations_placed_address_before_address_max
1498 && memaddr + len <= (bl->address
1499 - bp_locations_placed_address_before_address_max))
876fa593
JK
1500 break;
1501
35df4500 1502 if (!bp_location_has_shadow (bl))
c5aa993b 1503 continue;
6c95b8df 1504
9d497a19
PA
1505 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1506 memaddr, len, &bl->target_info, bl->gdbarch);
1507 }
c906108c 1508}
9d497a19 1509
c906108c 1510\f
c5aa993b 1511
b775012e
LM
1512/* Return true if BPT is either a software breakpoint or a hardware
1513 breakpoint. */
1514
1515int
1516is_breakpoint (const struct breakpoint *bpt)
1517{
1518 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1519 || bpt->type == bp_hardware_breakpoint
1520 || bpt->type == bp_dprintf);
b775012e
LM
1521}
1522
60e1c644
PA
1523/* Return true if BPT is of any hardware watchpoint kind. */
1524
a5606eee 1525static int
d77f58be 1526is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1527{
1528 return (bpt->type == bp_hardware_watchpoint
1529 || bpt->type == bp_read_watchpoint
1530 || bpt->type == bp_access_watchpoint);
1531}
7270d8f2 1532
60e1c644
PA
1533/* Return true if BPT is of any watchpoint kind, hardware or
1534 software. */
1535
3a5c3e22 1536int
d77f58be 1537is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1538{
1539 return (is_hardware_watchpoint (bpt)
1540 || bpt->type == bp_watchpoint);
1541}
1542
3a5c3e22
PA
1543/* Returns true if the current thread and its running state are safe
1544 to evaluate or update watchpoint B. Watchpoints on local
1545 expressions need to be evaluated in the context of the thread that
1546 was current when the watchpoint was created, and, that thread needs
1547 to be stopped to be able to select the correct frame context.
1548 Watchpoints on global expressions can be evaluated on any thread,
1549 and in any state. It is presently left to the target allowing
1550 memory accesses when threads are running. */
f6bc2008
PA
1551
1552static int
3a5c3e22 1553watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1554{
c1fc2657 1555 return (b->pspace == current_program_space
d0d8b0c6
JK
1556 && (ptid_equal (b->watchpoint_thread, null_ptid)
1557 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1558 && !is_executing (inferior_ptid))));
f6bc2008
PA
1559}
1560
d0fb5eae
JK
1561/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1562 associated bp_watchpoint_scope breakpoint. */
1563
1564static void
3a5c3e22 1565watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1566{
c1fc2657 1567 if (w->related_breakpoint != w)
d0fb5eae 1568 {
c1fc2657
SM
1569 gdb_assert (w->related_breakpoint->type == bp_watchpoint_scope);
1570 gdb_assert (w->related_breakpoint->related_breakpoint == w);
1571 w->related_breakpoint->disposition = disp_del_at_next_stop;
1572 w->related_breakpoint->related_breakpoint = w->related_breakpoint;
1573 w->related_breakpoint = w;
d0fb5eae 1574 }
c1fc2657 1575 w->disposition = disp_del_at_next_stop;
d0fb5eae
JK
1576}
1577
bb9d5f81
PP
1578/* Extract a bitfield value from value VAL using the bit parameters contained in
1579 watchpoint W. */
1580
1581static struct value *
1582extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1583{
1584 struct value *bit_val;
1585
1586 if (val == NULL)
1587 return NULL;
1588
1589 bit_val = allocate_value (value_type (val));
1590
1591 unpack_value_bitfield (bit_val,
1592 w->val_bitpos,
1593 w->val_bitsize,
1594 value_contents_for_printing (val),
1595 value_offset (val),
1596 val);
1597
1598 return bit_val;
1599}
1600
c6d81124
PA
1601/* Allocate a dummy location and add it to B, which must be a software
1602 watchpoint. This is required because even if a software watchpoint
1603 is not watching any memory, bpstat_stop_status requires a location
1604 to be able to report stops. */
1605
1606static void
1607software_watchpoint_add_no_memory_location (struct breakpoint *b,
1608 struct program_space *pspace)
1609{
1610 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1611
1612 b->loc = allocate_bp_location (b);
1613 b->loc->pspace = pspace;
1614 b->loc->address = -1;
1615 b->loc->length = -1;
1616}
1617
1618/* Returns true if B is a software watchpoint that is not watching any
1619 memory (e.g., "watch $pc"). */
1620
1621static int
1622is_no_memory_software_watchpoint (struct breakpoint *b)
1623{
1624 return (b->type == bp_watchpoint
1625 && b->loc != NULL
1626 && b->loc->next == NULL
1627 && b->loc->address == -1
1628 && b->loc->length == -1);
1629}
1630
567e1b4e
JB
1631/* Assuming that B is a watchpoint:
1632 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1633 - Evaluate expression and store the result in B->val
567e1b4e
JB
1634 - Evaluate the condition if there is one, and store the result
1635 in b->loc->cond.
a5606eee
VP
1636 - Update the list of values that must be watched in B->loc.
1637
4a64f543
MS
1638 If the watchpoint disposition is disp_del_at_next_stop, then do
1639 nothing. If this is local watchpoint that is out of scope, delete
1640 it.
1641
1642 Even with `set breakpoint always-inserted on' the watchpoints are
1643 removed + inserted on each stop here. Normal breakpoints must
1644 never be removed because they might be missed by a running thread
1645 when debugging in non-stop mode. On the other hand, hardware
1646 watchpoints (is_hardware_watchpoint; processed here) are specific
1647 to each LWP since they are stored in each LWP's hardware debug
1648 registers. Therefore, such LWP must be stopped first in order to
1649 be able to modify its hardware watchpoints.
1650
1651 Hardware watchpoints must be reset exactly once after being
1652 presented to the user. It cannot be done sooner, because it would
1653 reset the data used to present the watchpoint hit to the user. And
1654 it must not be done later because it could display the same single
1655 watchpoint hit during multiple GDB stops. Note that the latter is
1656 relevant only to the hardware watchpoint types bp_read_watchpoint
1657 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1658 not user-visible - its hit is suppressed if the memory content has
1659 not changed.
1660
1661 The following constraints influence the location where we can reset
1662 hardware watchpoints:
1663
1664 * target_stopped_by_watchpoint and target_stopped_data_address are
1665 called several times when GDB stops.
1666
1667 [linux]
1668 * Multiple hardware watchpoints can be hit at the same time,
1669 causing GDB to stop. GDB only presents one hardware watchpoint
1670 hit at a time as the reason for stopping, and all the other hits
1671 are presented later, one after the other, each time the user
1672 requests the execution to be resumed. Execution is not resumed
1673 for the threads still having pending hit event stored in
1674 LWP_INFO->STATUS. While the watchpoint is already removed from
1675 the inferior on the first stop the thread hit event is kept being
1676 reported from its cached value by linux_nat_stopped_data_address
1677 until the real thread resume happens after the watchpoint gets
1678 presented and thus its LWP_INFO->STATUS gets reset.
1679
1680 Therefore the hardware watchpoint hit can get safely reset on the
1681 watchpoint removal from inferior. */
a79d3c27 1682
b40ce68a 1683static void
3a5c3e22 1684update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1685{
a5606eee 1686 int within_current_scope;
a5606eee 1687 struct frame_id saved_frame_id;
66076460 1688 int frame_saved;
a5606eee 1689
f6bc2008
PA
1690 /* If this is a local watchpoint, we only want to check if the
1691 watchpoint frame is in scope if the current thread is the thread
1692 that was used to create the watchpoint. */
1693 if (!watchpoint_in_thread_scope (b))
1694 return;
1695
c1fc2657 1696 if (b->disposition == disp_del_at_next_stop)
a5606eee
VP
1697 return;
1698
66076460 1699 frame_saved = 0;
a5606eee
VP
1700
1701 /* Determine if the watchpoint is within scope. */
1702 if (b->exp_valid_block == NULL)
1703 within_current_scope = 1;
1704 else
1705 {
b5db5dfc
UW
1706 struct frame_info *fi = get_current_frame ();
1707 struct gdbarch *frame_arch = get_frame_arch (fi);
1708 CORE_ADDR frame_pc = get_frame_pc (fi);
1709
c9cf6e20
MG
1710 /* If we're at a point where the stack has been destroyed
1711 (e.g. in a function epilogue), unwinding may not work
1712 properly. Do not attempt to recreate locations at this
b5db5dfc 1713 point. See similar comments in watchpoint_check. */
c9cf6e20 1714 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1715 return;
66076460
DJ
1716
1717 /* Save the current frame's ID so we can restore it after
1718 evaluating the watchpoint expression on its own frame. */
1719 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1720 took a frame parameter, so that we didn't have to change the
1721 selected frame. */
1722 frame_saved = 1;
1723 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1724
a5606eee
VP
1725 fi = frame_find_by_id (b->watchpoint_frame);
1726 within_current_scope = (fi != NULL);
1727 if (within_current_scope)
1728 select_frame (fi);
1729 }
1730
b5db5dfc
UW
1731 /* We don't free locations. They are stored in the bp_location array
1732 and update_global_location_list will eventually delete them and
1733 remove breakpoints if needed. */
c1fc2657 1734 b->loc = NULL;
b5db5dfc 1735
a5606eee
VP
1736 if (within_current_scope && reparse)
1737 {
bbc13ae3 1738 const char *s;
d63d0675 1739
4d01a485 1740 b->exp.reset ();
d63d0675 1741 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1742 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1743 /* If the meaning of expression itself changed, the old value is
1744 no longer relevant. We don't want to report a watchpoint hit
1745 to the user when the old value and the new value may actually
1746 be completely different objects. */
1747 value_free (b->val);
fa4727a6
DJ
1748 b->val = NULL;
1749 b->val_valid = 0;
60e1c644
PA
1750
1751 /* Note that unlike with breakpoints, the watchpoint's condition
1752 expression is stored in the breakpoint object, not in the
1753 locations (re)created below. */
c1fc2657 1754 if (b->cond_string != NULL)
60e1c644 1755 {
4d01a485 1756 b->cond_exp.reset ();
60e1c644 1757
c1fc2657 1758 s = b->cond_string;
1bb9788d 1759 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1760 }
a5606eee 1761 }
a5606eee
VP
1762
1763 /* If we failed to parse the expression, for example because
1764 it refers to a global variable in a not-yet-loaded shared library,
1765 don't try to insert watchpoint. We don't automatically delete
1766 such watchpoint, though, since failure to parse expression
1767 is different from out-of-scope watchpoint. */
e8369a73 1768 if (!target_has_execution)
2d134ed3
PA
1769 {
1770 /* Without execution, memory can't change. No use to try and
1771 set watchpoint locations. The watchpoint will be reset when
1772 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1773 if (!can_use_hw_watchpoints)
1774 {
c1fc2657
SM
1775 if (b->ops->works_in_software_mode (b))
1776 b->type = bp_watchpoint;
e8369a73 1777 else
638aa5a1
AB
1778 error (_("Can't set read/access watchpoint when "
1779 "hardware watchpoints are disabled."));
e8369a73 1780 }
2d134ed3
PA
1781 }
1782 else if (within_current_scope && b->exp)
a5606eee 1783 {
0cf6dd15 1784 int pc = 0;
fa4727a6 1785 struct value *val_chain, *v, *result, *next;
2d134ed3 1786 struct program_space *frame_pspace;
a5606eee 1787
4d01a485 1788 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
a5606eee 1789
a5606eee
VP
1790 /* Avoid setting b->val if it's already set. The meaning of
1791 b->val is 'the last value' user saw, and we should update
1792 it only if we reported that last value to user. As it
9c06b0b4
TJB
1793 happens, the code that reports it updates b->val directly.
1794 We don't keep track of the memory value for masked
1795 watchpoints. */
c1fc2657 1796 if (!b->val_valid && !is_masked_watchpoint (b))
fa4727a6 1797 {
bb9d5f81
PP
1798 if (b->val_bitsize != 0)
1799 {
1800 v = extract_bitfield_from_watchpoint_value (b, v);
1801 if (v != NULL)
1802 release_value (v);
1803 }
fa4727a6
DJ
1804 b->val = v;
1805 b->val_valid = 1;
1806 }
a5606eee 1807
2d134ed3
PA
1808 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1809
a5606eee 1810 /* Look at each value on the value chain. */
9fa40276 1811 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1812 {
1813 /* If it's a memory location, and GDB actually needed
1814 its contents to evaluate the expression, then we
fa4727a6
DJ
1815 must watch it. If the first value returned is
1816 still lazy, that means an error occurred reading it;
1817 watch it anyway in case it becomes readable. */
a5606eee 1818 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1819 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1820 {
1821 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1822
a5606eee
VP
1823 /* We only watch structs and arrays if user asked
1824 for it explicitly, never if they just happen to
1825 appear in the middle of some value chain. */
fa4727a6 1826 if (v == result
a5606eee
VP
1827 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1828 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1829 {
1830 CORE_ADDR addr;
f486487f 1831 enum target_hw_bp_type type;
a5606eee 1832 struct bp_location *loc, **tmp;
bb9d5f81
PP
1833 int bitpos = 0, bitsize = 0;
1834
1835 if (value_bitsize (v) != 0)
1836 {
1837 /* Extract the bit parameters out from the bitfield
1838 sub-expression. */
1839 bitpos = value_bitpos (v);
1840 bitsize = value_bitsize (v);
1841 }
1842 else if (v == result && b->val_bitsize != 0)
1843 {
1844 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1845 lvalue whose bit parameters are saved in the fields
1846 VAL_BITPOS and VAL_BITSIZE. */
1847 bitpos = b->val_bitpos;
1848 bitsize = b->val_bitsize;
1849 }
a5606eee 1850
42ae5230 1851 addr = value_address (v);
bb9d5f81
PP
1852 if (bitsize != 0)
1853 {
1854 /* Skip the bytes that don't contain the bitfield. */
1855 addr += bitpos / 8;
1856 }
1857
a5606eee 1858 type = hw_write;
c1fc2657 1859 if (b->type == bp_read_watchpoint)
a5606eee 1860 type = hw_read;
c1fc2657 1861 else if (b->type == bp_access_watchpoint)
a5606eee 1862 type = hw_access;
3a5c3e22 1863
c1fc2657
SM
1864 loc = allocate_bp_location (b);
1865 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1866 ;
1867 *tmp = loc;
a6d9a66e 1868 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1869
1870 loc->pspace = frame_pspace;
a5606eee 1871 loc->address = addr;
bb9d5f81
PP
1872
1873 if (bitsize != 0)
1874 {
1875 /* Just cover the bytes that make up the bitfield. */
1876 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
1877 }
1878 else
1879 loc->length = TYPE_LENGTH (value_type (v));
1880
a5606eee
VP
1881 loc->watchpoint_type = type;
1882 }
1883 }
9fa40276
TJB
1884 }
1885
1886 /* Change the type of breakpoint between hardware assisted or
1887 an ordinary watchpoint depending on the hardware support
1888 and free hardware slots. REPARSE is set when the inferior
1889 is started. */
a9634178 1890 if (reparse)
9fa40276 1891 {
e09342b5 1892 int reg_cnt;
9fa40276
TJB
1893 enum bp_loc_type loc_type;
1894 struct bp_location *bl;
a5606eee 1895
a9634178 1896 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1897
1898 if (reg_cnt)
9fa40276
TJB
1899 {
1900 int i, target_resources_ok, other_type_used;
a1398e0c 1901 enum bptype type;
9fa40276 1902
a9634178
TJB
1903 /* Use an exact watchpoint when there's only one memory region to be
1904 watched, and only one debug register is needed to watch it. */
1905 b->exact = target_exact_watchpoints && reg_cnt == 1;
1906
9fa40276 1907 /* We need to determine how many resources are already
e09342b5
TJB
1908 used for all other hardware watchpoints plus this one
1909 to see if we still have enough resources to also fit
a1398e0c
PA
1910 this watchpoint in as well. */
1911
1912 /* If this is a software watchpoint, we try to turn it
1913 to a hardware one -- count resources as if B was of
1914 hardware watchpoint type. */
c1fc2657 1915 type = b->type;
a1398e0c
PA
1916 if (type == bp_watchpoint)
1917 type = bp_hardware_watchpoint;
1918
1919 /* This watchpoint may or may not have been placed on
1920 the list yet at this point (it won't be in the list
1921 if we're trying to create it for the first time,
1922 through watch_command), so always account for it
1923 manually. */
1924
1925 /* Count resources used by all watchpoints except B. */
c1fc2657 1926 i = hw_watchpoint_used_count_others (b, type, &other_type_used);
a1398e0c
PA
1927
1928 /* Add in the resources needed for B. */
c1fc2657 1929 i += hw_watchpoint_use_count (b);
a1398e0c
PA
1930
1931 target_resources_ok
1932 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1933 if (target_resources_ok <= 0)
a9634178 1934 {
c1fc2657 1935 int sw_mode = b->ops->works_in_software_mode (b);
9c06b0b4
TJB
1936
1937 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1938 error (_("Target does not support this type of "
1939 "hardware watchpoint."));
9c06b0b4
TJB
1940 else if (target_resources_ok < 0 && !sw_mode)
1941 error (_("There are not enough available hardware "
1942 "resources for this watchpoint."));
a1398e0c
PA
1943
1944 /* Downgrade to software watchpoint. */
c1fc2657 1945 b->type = bp_watchpoint;
a1398e0c
PA
1946 }
1947 else
1948 {
1949 /* If this was a software watchpoint, we've just
1950 found we have enough resources to turn it to a
1951 hardware watchpoint. Otherwise, this is a
1952 nop. */
c1fc2657 1953 b->type = type;
a9634178 1954 }
9fa40276 1955 }
c1fc2657 1956 else if (!b->ops->works_in_software_mode (b))
638aa5a1
AB
1957 {
1958 if (!can_use_hw_watchpoints)
1959 error (_("Can't set read/access watchpoint when "
1960 "hardware watchpoints are disabled."));
1961 else
1962 error (_("Expression cannot be implemented with "
1963 "read/access watchpoint."));
1964 }
9fa40276 1965 else
c1fc2657 1966 b->type = bp_watchpoint;
9fa40276 1967
c1fc2657 1968 loc_type = (b->type == bp_watchpoint? bp_loc_other
9fa40276 1969 : bp_loc_hardware_watchpoint);
c1fc2657 1970 for (bl = b->loc; bl; bl = bl->next)
9fa40276
TJB
1971 bl->loc_type = loc_type;
1972 }
1973
1974 for (v = val_chain; v; v = next)
1975 {
a5606eee
VP
1976 next = value_next (v);
1977 if (v != b->val)
1978 value_free (v);
1979 }
1980
c7437ca6
PA
1981 /* If a software watchpoint is not watching any memory, then the
1982 above left it without any location set up. But,
1983 bpstat_stop_status requires a location to be able to report
1984 stops, so make sure there's at least a dummy one. */
c1fc2657
SM
1985 if (b->type == bp_watchpoint && b->loc == NULL)
1986 software_watchpoint_add_no_memory_location (b, frame_pspace);
a5606eee
VP
1987 }
1988 else if (!within_current_scope)
7270d8f2 1989 {
ac74f770
MS
1990 printf_filtered (_("\
1991Watchpoint %d deleted because the program has left the block\n\
1992in which its expression is valid.\n"),
c1fc2657 1993 b->number);
d0fb5eae 1994 watchpoint_del_at_next_stop (b);
7270d8f2 1995 }
a5606eee
VP
1996
1997 /* Restore the selected frame. */
66076460
DJ
1998 if (frame_saved)
1999 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2000}
2001
a5606eee 2002
74960c60 2003/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2004 inserted in the inferior. We don't differentiate the type of BL's owner
2005 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2006 breakpoint_ops is not defined, because in insert_bp_location,
2007 tracepoint's insert_location will not be called. */
74960c60 2008static int
35df4500 2009should_be_inserted (struct bp_location *bl)
74960c60 2010{
35df4500 2011 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2012 return 0;
2013
35df4500 2014 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2015 return 0;
2016
35df4500 2017 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2018 return 0;
2019
f8eba3c6
TT
2020 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2021 return 0;
2022
56710373
PA
2023 /* This is set for example, when we're attached to the parent of a
2024 vfork, and have detached from the child. The child is running
2025 free, and we expect it to do an exec or exit, at which point the
2026 OS makes the parent schedulable again (and the target reports
2027 that the vfork is done). Until the child is done with the shared
2028 memory region, do not insert breakpoints in the parent, otherwise
2029 the child could still trip on the parent's breakpoints. Since
2030 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2031 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2032 return 0;
2033
31e77af2 2034 /* Don't insert a breakpoint if we're trying to step past its
21edc42f
YQ
2035 location, except if the breakpoint is a single-step breakpoint,
2036 and the breakpoint's thread is the thread which is stepping past
2037 a breakpoint. */
31e77af2
PA
2038 if ((bl->loc_type == bp_loc_software_breakpoint
2039 || bl->loc_type == bp_loc_hardware_breakpoint)
2040 && stepping_past_instruction_at (bl->pspace->aspace,
21edc42f
YQ
2041 bl->address)
2042 /* The single-step breakpoint may be inserted at the location
2043 we're trying to step if the instruction branches to itself.
2044 However, the instruction won't be executed at all and it may
2045 break the semantics of the instruction, for example, the
2046 instruction is a conditional branch or updates some flags.
2047 We can't fix it unless GDB is able to emulate the instruction
2048 or switch to displaced stepping. */
2049 && !(bl->owner->type == bp_single_step
2050 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
e558d7c1
PA
2051 {
2052 if (debug_infrun)
2053 {
2054 fprintf_unfiltered (gdb_stdlog,
2055 "infrun: skipping breakpoint: "
2056 "stepping past insn at: %s\n",
2057 paddress (bl->gdbarch, bl->address));
2058 }
2059 return 0;
2060 }
31e77af2 2061
963f9c80
PA
2062 /* Don't insert watchpoints if we're trying to step past the
2063 instruction that triggered one. */
2064 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2065 && stepping_past_nonsteppable_watchpoint ())
2066 {
2067 if (debug_infrun)
2068 {
2069 fprintf_unfiltered (gdb_stdlog,
2070 "infrun: stepping past non-steppable watchpoint. "
2071 "skipping watchpoint at %s:%d\n",
2072 paddress (bl->gdbarch, bl->address),
2073 bl->length);
2074 }
2075 return 0;
2076 }
2077
74960c60
VP
2078 return 1;
2079}
2080
934709f0
PW
2081/* Same as should_be_inserted but does the check assuming
2082 that the location is not duplicated. */
2083
2084static int
2085unduplicated_should_be_inserted (struct bp_location *bl)
2086{
2087 int result;
2088 const int save_duplicate = bl->duplicate;
2089
2090 bl->duplicate = 0;
2091 result = should_be_inserted (bl);
2092 bl->duplicate = save_duplicate;
2093 return result;
2094}
2095
b775012e
LM
2096/* Parses a conditional described by an expression COND into an
2097 agent expression bytecode suitable for evaluation
2098 by the bytecode interpreter. Return NULL if there was
2099 any error during parsing. */
2100
833177a4 2101static agent_expr_up
b775012e
LM
2102parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2103{
833177a4 2104 if (cond == NULL)
b775012e
LM
2105 return NULL;
2106
833177a4
PA
2107 agent_expr_up aexpr;
2108
b775012e
LM
2109 /* We don't want to stop processing, so catch any errors
2110 that may show up. */
492d29ea 2111 TRY
b775012e 2112 {
036e657b 2113 aexpr = gen_eval_for_expr (scope, cond);
b775012e
LM
2114 }
2115
492d29ea 2116 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2117 {
2118 /* If we got here, it means the condition could not be parsed to a valid
2119 bytecode expression and thus can't be evaluated on the target's side.
2120 It's no use iterating through the conditions. */
b775012e 2121 }
492d29ea 2122 END_CATCH
b775012e
LM
2123
2124 /* We have a valid agent expression. */
2125 return aexpr;
2126}
2127
2128/* Based on location BL, create a list of breakpoint conditions to be
2129 passed on to the target. If we have duplicated locations with different
2130 conditions, we will add such conditions to the list. The idea is that the
2131 target will evaluate the list of conditions and will only notify GDB when
2132 one of them is true. */
2133
2134static void
2135build_target_condition_list (struct bp_location *bl)
2136{
2137 struct bp_location **locp = NULL, **loc2p;
2138 int null_condition_or_parse_error = 0;
2139 int modified = bl->needs_update;
2140 struct bp_location *loc;
2141
8b4f3082 2142 /* Release conditions left over from a previous insert. */
3cde5c42 2143 bl->target_info.conditions.clear ();
8b4f3082 2144
b775012e
LM
2145 /* This is only meaningful if the target is
2146 evaluating conditions and if the user has
2147 opted for condition evaluation on the target's
2148 side. */
2149 if (gdb_evaluates_breakpoint_condition_p ()
2150 || !target_supports_evaluation_of_breakpoint_conditions ())
2151 return;
2152
2153 /* Do a first pass to check for locations with no assigned
2154 conditions or conditions that fail to parse to a valid agent expression
2155 bytecode. If any of these happen, then it's no use to send conditions
2156 to the target since this location will always trigger and generate a
2157 response back to GDB. */
2158 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2159 {
2160 loc = (*loc2p);
2161 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2162 {
2163 if (modified)
2164 {
b775012e
LM
2165 /* Re-parse the conditions since something changed. In that
2166 case we already freed the condition bytecodes (see
2167 force_breakpoint_reinsertion). We just
2168 need to parse the condition to bytecodes again. */
833177a4
PA
2169 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2170 loc->cond.get ());
b775012e
LM
2171 }
2172
2173 /* If we have a NULL bytecode expression, it means something
2174 went wrong or we have a null condition expression. */
2175 if (!loc->cond_bytecode)
2176 {
2177 null_condition_or_parse_error = 1;
2178 break;
2179 }
2180 }
2181 }
2182
2183 /* If any of these happened, it means we will have to evaluate the conditions
2184 for the location's address on gdb's side. It is no use keeping bytecodes
2185 for all the other duplicate locations, thus we free all of them here.
2186
2187 This is so we have a finer control over which locations' conditions are
2188 being evaluated by GDB or the remote stub. */
2189 if (null_condition_or_parse_error)
2190 {
2191 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2192 {
2193 loc = (*loc2p);
2194 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2195 {
2196 /* Only go as far as the first NULL bytecode is
2197 located. */
2198 if (!loc->cond_bytecode)
2199 return;
2200
833177a4 2201 loc->cond_bytecode.reset ();
b775012e
LM
2202 }
2203 }
2204 }
2205
2206 /* No NULL conditions or failed bytecode generation. Build a condition list
2207 for this location's address. */
2208 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2209 {
2210 loc = (*loc2p);
2211 if (loc->cond
2212 && is_breakpoint (loc->owner)
2213 && loc->pspace->num == bl->pspace->num
2214 && loc->owner->enable_state == bp_enabled
2215 && loc->enabled)
3cde5c42
PA
2216 {
2217 /* Add the condition to the vector. This will be used later
2218 to send the conditions to the target. */
2219 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2220 }
b775012e
LM
2221 }
2222
2223 return;
2224}
2225
d3ce09f5
SS
2226/* Parses a command described by string CMD into an agent expression
2227 bytecode suitable for evaluation by the bytecode interpreter.
2228 Return NULL if there was any error during parsing. */
2229
833177a4 2230static agent_expr_up
d3ce09f5
SS
2231parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2232{
2233 struct cleanup *old_cleanups = 0;
4d01a485 2234 struct expression **argvec;
bbc13ae3
KS
2235 const char *cmdrest;
2236 const char *format_start, *format_end;
d3ce09f5
SS
2237 struct format_piece *fpieces;
2238 int nargs;
2239 struct gdbarch *gdbarch = get_current_arch ();
2240
833177a4 2241 if (cmd == NULL)
d3ce09f5
SS
2242 return NULL;
2243
2244 cmdrest = cmd;
2245
2246 if (*cmdrest == ',')
2247 ++cmdrest;
f1735a53 2248 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2249
2250 if (*cmdrest++ != '"')
2251 error (_("No format string following the location"));
2252
2253 format_start = cmdrest;
2254
2255 fpieces = parse_format_string (&cmdrest);
2256
2257 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2258
2259 format_end = cmdrest;
2260
2261 if (*cmdrest++ != '"')
2262 error (_("Bad format string, non-terminated '\"'."));
2263
f1735a53 2264 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2265
2266 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2267 error (_("Invalid argument syntax"));
2268
2269 if (*cmdrest == ',')
2270 cmdrest++;
f1735a53 2271 cmdrest = skip_spaces (cmdrest);
d3ce09f5
SS
2272
2273 /* For each argument, make an expression. */
2274
2275 argvec = (struct expression **) alloca (strlen (cmd)
2276 * sizeof (struct expression *));
2277
2278 nargs = 0;
2279 while (*cmdrest != '\0')
2280 {
bbc13ae3 2281 const char *cmd1;
d3ce09f5
SS
2282
2283 cmd1 = cmdrest;
4d01a485
PA
2284 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2285 argvec[nargs++] = expr.release ();
d3ce09f5
SS
2286 cmdrest = cmd1;
2287 if (*cmdrest == ',')
2288 ++cmdrest;
2289 }
2290
833177a4
PA
2291 agent_expr_up aexpr;
2292
d3ce09f5
SS
2293 /* We don't want to stop processing, so catch any errors
2294 that may show up. */
492d29ea 2295 TRY
d3ce09f5 2296 {
036e657b
JB
2297 aexpr = gen_printf (scope, gdbarch, 0, 0,
2298 format_start, format_end - format_start,
2299 fpieces, nargs, argvec);
d3ce09f5 2300 }
492d29ea 2301 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2302 {
2303 /* If we got here, it means the command could not be parsed to a valid
2304 bytecode expression and thus can't be evaluated on the target's side.
2305 It's no use iterating through the other commands. */
d3ce09f5 2306 }
492d29ea
PA
2307 END_CATCH
2308
2309 do_cleanups (old_cleanups);
d3ce09f5 2310
d3ce09f5
SS
2311 /* We have a valid agent expression, return it. */
2312 return aexpr;
2313}
2314
2315/* Based on location BL, create a list of breakpoint commands to be
2316 passed on to the target. If we have duplicated locations with
2317 different commands, we will add any such to the list. */
2318
2319static void
2320build_target_command_list (struct bp_location *bl)
2321{
2322 struct bp_location **locp = NULL, **loc2p;
2323 int null_command_or_parse_error = 0;
2324 int modified = bl->needs_update;
2325 struct bp_location *loc;
2326
3cde5c42
PA
2327 /* Clear commands left over from a previous insert. */
2328 bl->target_info.tcommands.clear ();
8b4f3082 2329
41fac0cf 2330 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2331 return;
2332
41fac0cf
PA
2333 /* For now, limit to agent-style dprintf breakpoints. */
2334 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2335 return;
2336
41fac0cf
PA
2337 /* For now, if we have any duplicate location that isn't a dprintf,
2338 don't install the target-side commands, as that would make the
2339 breakpoint not be reported to the core, and we'd lose
2340 control. */
2341 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2342 {
2343 loc = (*loc2p);
2344 if (is_breakpoint (loc->owner)
2345 && loc->pspace->num == bl->pspace->num
2346 && loc->owner->type != bp_dprintf)
2347 return;
2348 }
2349
d3ce09f5
SS
2350 /* Do a first pass to check for locations with no assigned
2351 conditions or conditions that fail to parse to a valid agent expression
2352 bytecode. If any of these happen, then it's no use to send conditions
2353 to the target since this location will always trigger and generate a
2354 response back to GDB. */
2355 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2356 {
2357 loc = (*loc2p);
2358 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2359 {
2360 if (modified)
2361 {
d3ce09f5
SS
2362 /* Re-parse the commands since something changed. In that
2363 case we already freed the command bytecodes (see
2364 force_breakpoint_reinsertion). We just
2365 need to parse the command to bytecodes again. */
833177a4
PA
2366 loc->cmd_bytecode
2367 = parse_cmd_to_aexpr (bl->address,
2368 loc->owner->extra_string);
d3ce09f5
SS
2369 }
2370
2371 /* If we have a NULL bytecode expression, it means something
2372 went wrong or we have a null command expression. */
2373 if (!loc->cmd_bytecode)
2374 {
2375 null_command_or_parse_error = 1;
2376 break;
2377 }
2378 }
2379 }
2380
2381 /* If anything failed, then we're not doing target-side commands,
2382 and so clean up. */
2383 if (null_command_or_parse_error)
2384 {
2385 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2386 {
2387 loc = (*loc2p);
2388 if (is_breakpoint (loc->owner)
2389 && loc->pspace->num == bl->pspace->num)
2390 {
2391 /* Only go as far as the first NULL bytecode is
2392 located. */
40fb6c5e 2393 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2394 return;
2395
833177a4 2396 loc->cmd_bytecode.reset ();
d3ce09f5
SS
2397 }
2398 }
2399 }
2400
2401 /* No NULL commands or failed bytecode generation. Build a command list
2402 for this location's address. */
2403 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2404 {
2405 loc = (*loc2p);
2406 if (loc->owner->extra_string
2407 && is_breakpoint (loc->owner)
2408 && loc->pspace->num == bl->pspace->num
2409 && loc->owner->enable_state == bp_enabled
2410 && loc->enabled)
3cde5c42
PA
2411 {
2412 /* Add the command to the vector. This will be used later
2413 to send the commands to the target. */
2414 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2415 }
d3ce09f5
SS
2416 }
2417
2418 bl->target_info.persist = 0;
2419 /* Maybe flag this location as persistent. */
2420 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2421 bl->target_info.persist = 1;
2422}
2423
833b7ab5
YQ
2424/* Return the kind of breakpoint on address *ADDR. Get the kind
2425 of breakpoint according to ADDR except single-step breakpoint.
2426 Get the kind of single-step breakpoint according to the current
2427 registers state. */
cd6c3b4f
YQ
2428
2429static int
2430breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2431{
833b7ab5
YQ
2432 if (bl->owner->type == bp_single_step)
2433 {
2434 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2435 struct regcache *regcache;
2436
2437 regcache = get_thread_regcache (thr->ptid);
2438
2439 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2440 regcache, addr);
2441 }
2442 else
2443 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
cd6c3b4f
YQ
2444}
2445
35df4500
TJB
2446/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2447 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2448 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2449 Returns 0 for success, 1 if the bp_location type is not supported or
2450 -1 for failure.
879bfdc2 2451
4a64f543
MS
2452 NOTE drow/2003-09-09: This routine could be broken down to an
2453 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2454static int
35df4500 2455insert_bp_location (struct bp_location *bl,
26bb91f3 2456 struct ui_file *tmp_error_stream,
3fbb6ffa 2457 int *disabled_breaks,
dd61ec5c
MW
2458 int *hw_breakpoint_error,
2459 int *hw_bp_error_explained_already)
879bfdc2 2460{
688fca4f 2461 gdb_exception bp_excpt = exception_none;
879bfdc2 2462
b775012e 2463 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2464 return 0;
2465
35c63cd8
JB
2466 /* Note we don't initialize bl->target_info, as that wipes out
2467 the breakpoint location's shadow_contents if the breakpoint
2468 is still inserted at that location. This in turn breaks
2469 target_read_memory which depends on these buffers when
2470 a memory read is requested at the breakpoint location:
2471 Once the target_info has been wiped, we fail to see that
2472 we have a breakpoint inserted at that address and thus
2473 read the breakpoint instead of returning the data saved in
2474 the breakpoint location's shadow contents. */
0d5ed153 2475 bl->target_info.reqstd_address = bl->address;
35df4500 2476 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2477 bl->target_info.length = bl->length;
8181d85f 2478
b775012e
LM
2479 /* When working with target-side conditions, we must pass all the conditions
2480 for the same breakpoint address down to the target since GDB will not
2481 insert those locations. With a list of breakpoint conditions, the target
2482 can decide when to stop and notify GDB. */
2483
2484 if (is_breakpoint (bl->owner))
2485 {
2486 build_target_condition_list (bl);
d3ce09f5
SS
2487 build_target_command_list (bl);
2488 /* Reset the modification marker. */
b775012e
LM
2489 bl->needs_update = 0;
2490 }
2491
35df4500
TJB
2492 if (bl->loc_type == bp_loc_software_breakpoint
2493 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2494 {
35df4500 2495 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2496 {
2497 /* If the explicitly specified breakpoint type
2498 is not hardware breakpoint, check the memory map to see
2499 if the breakpoint address is in read only memory or not.
4a64f543 2500
765dc015
VP
2501 Two important cases are:
2502 - location type is not hardware breakpoint, memory
2503 is readonly. We change the type of the location to
2504 hardware breakpoint.
4a64f543
MS
2505 - location type is hardware breakpoint, memory is
2506 read-write. This means we've previously made the
2507 location hardware one, but then the memory map changed,
2508 so we undo.
765dc015 2509
4a64f543
MS
2510 When breakpoints are removed, remove_breakpoints will use
2511 location types we've just set here, the only possible
2512 problem is that memory map has changed during running
2513 program, but it's not going to work anyway with current
2514 gdb. */
765dc015 2515 struct mem_region *mr
0d5ed153 2516 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2517
2518 if (mr)
2519 {
2520 if (automatic_hardware_breakpoints)
2521 {
765dc015
VP
2522 enum bp_loc_type new_type;
2523
2524 if (mr->attrib.mode != MEM_RW)
2525 new_type = bp_loc_hardware_breakpoint;
2526 else
2527 new_type = bp_loc_software_breakpoint;
2528
35df4500 2529 if (new_type != bl->loc_type)
765dc015
VP
2530 {
2531 static int said = 0;
cc59ec59 2532
35df4500 2533 bl->loc_type = new_type;
765dc015
VP
2534 if (!said)
2535 {
3e43a32a
MS
2536 fprintf_filtered (gdb_stdout,
2537 _("Note: automatically using "
2538 "hardware breakpoints for "
2539 "read-only addresses.\n"));
765dc015
VP
2540 said = 1;
2541 }
2542 }
2543 }
35df4500 2544 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2545 && mr->attrib.mode != MEM_RW)
2546 {
2547 fprintf_unfiltered (tmp_error_stream,
2548 _("Cannot insert breakpoint %d.\n"
2549 "Cannot set software breakpoint "
2550 "at read-only address %s\n"),
2551 bl->owner->number,
2552 paddress (bl->gdbarch, bl->address));
2553 return 1;
2554 }
765dc015
VP
2555 }
2556 }
2557
879bfdc2
DJ
2558 /* First check to see if we have to handle an overlay. */
2559 if (overlay_debugging == ovly_off
35df4500
TJB
2560 || bl->section == NULL
2561 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2562 {
2563 /* No overlay handling: just set the breakpoint. */
492d29ea 2564 TRY
dd61ec5c 2565 {
0000e5cc
PA
2566 int val;
2567
dd61ec5c 2568 val = bl->owner->ops->insert_location (bl);
0000e5cc 2569 if (val)
688fca4f 2570 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2571 }
492d29ea 2572 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2573 {
688fca4f 2574 bp_excpt = e;
dd61ec5c 2575 }
492d29ea 2576 END_CATCH
879bfdc2
DJ
2577 }
2578 else
2579 {
4a64f543 2580 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2581 Shall we set a breakpoint at the LMA? */
2582 if (!overlay_events_enabled)
2583 {
2584 /* Yes -- overlay event support is not active,
2585 so we must try to set a breakpoint at the LMA.
2586 This will not work for a hardware breakpoint. */
35df4500 2587 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2588 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2589 bl->owner->number);
879bfdc2
DJ
2590 else
2591 {
35df4500
TJB
2592 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2593 bl->section);
879bfdc2 2594 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2595 bl->overlay_target_info = bl->target_info;
0d5ed153 2596 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2597
2598 /* No overlay handling: just set the breakpoint. */
492d29ea 2599 TRY
0000e5cc
PA
2600 {
2601 int val;
2602
579c6ad9 2603 bl->overlay_target_info.kind
cd6c3b4f
YQ
2604 = breakpoint_kind (bl, &addr);
2605 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2606 val = target_insert_breakpoint (bl->gdbarch,
2607 &bl->overlay_target_info);
2608 if (val)
688fca4f
PA
2609 bp_excpt
2610 = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
0000e5cc 2611 }
492d29ea 2612 CATCH (e, RETURN_MASK_ALL)
0000e5cc 2613 {
688fca4f 2614 bp_excpt = e;
0000e5cc 2615 }
492d29ea 2616 END_CATCH
0000e5cc 2617
688fca4f 2618 if (bp_excpt.reason != 0)
99361f52 2619 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2620 "Overlay breakpoint %d "
2621 "failed: in ROM?\n",
35df4500 2622 bl->owner->number);
879bfdc2
DJ
2623 }
2624 }
2625 /* Shall we set a breakpoint at the VMA? */
35df4500 2626 if (section_is_mapped (bl->section))
879bfdc2
DJ
2627 {
2628 /* Yes. This overlay section is mapped into memory. */
492d29ea 2629 TRY
dd61ec5c 2630 {
0000e5cc
PA
2631 int val;
2632
dd61ec5c 2633 val = bl->owner->ops->insert_location (bl);
0000e5cc 2634 if (val)
688fca4f 2635 bp_excpt = gdb_exception {RETURN_ERROR, GENERIC_ERROR};
dd61ec5c 2636 }
492d29ea 2637 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2638 {
688fca4f 2639 bp_excpt = e;
dd61ec5c 2640 }
492d29ea 2641 END_CATCH
879bfdc2
DJ
2642 }
2643 else
2644 {
2645 /* No. This breakpoint will not be inserted.
2646 No error, but do not mark the bp as 'inserted'. */
2647 return 0;
2648 }
2649 }
2650
688fca4f 2651 if (bp_excpt.reason != 0)
879bfdc2
DJ
2652 {
2653 /* Can't set the breakpoint. */
0000e5cc
PA
2654
2655 /* In some cases, we might not be able to insert a
2656 breakpoint in a shared library that has already been
2657 removed, but we have not yet processed the shlib unload
2658 event. Unfortunately, some targets that implement
076855f9
PA
2659 breakpoint insertion themselves can't tell why the
2660 breakpoint insertion failed (e.g., the remote target
2661 doesn't define error codes), so we must treat generic
2662 errors as memory errors. */
688fca4f
PA
2663 if (bp_excpt.reason == RETURN_ERROR
2664 && (bp_excpt.error == GENERIC_ERROR
2665 || bp_excpt.error == MEMORY_ERROR)
076855f9 2666 && bl->loc_type == bp_loc_software_breakpoint
08351840 2667 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2668 || shared_objfile_contains_address_p (bl->pspace,
2669 bl->address)))
879bfdc2 2670 {
4a64f543 2671 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2672 bl->shlib_disabled = 1;
8d3788bd 2673 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2674 if (!*disabled_breaks)
2675 {
2676 fprintf_unfiltered (tmp_error_stream,
2677 "Cannot insert breakpoint %d.\n",
2678 bl->owner->number);
2679 fprintf_unfiltered (tmp_error_stream,
2680 "Temporarily disabling shared "
2681 "library breakpoints:\n");
2682 }
2683 *disabled_breaks = 1;
879bfdc2 2684 fprintf_unfiltered (tmp_error_stream,
35df4500 2685 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2686 return 0;
879bfdc2
DJ
2687 }
2688 else
879bfdc2 2689 {
35df4500 2690 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2691 {
0000e5cc 2692 *hw_breakpoint_error = 1;
688fca4f 2693 *hw_bp_error_explained_already = bp_excpt.message != NULL;
dd61ec5c
MW
2694 fprintf_unfiltered (tmp_error_stream,
2695 "Cannot insert hardware breakpoint %d%s",
688fca4f
PA
2696 bl->owner->number,
2697 bp_excpt.message ? ":" : ".\n");
2698 if (bp_excpt.message != NULL)
2699 fprintf_unfiltered (tmp_error_stream, "%s.\n",
2700 bp_excpt.message);
879bfdc2
DJ
2701 }
2702 else
2703 {
688fca4f 2704 if (bp_excpt.message == NULL)
0000e5cc 2705 {
1ccbe998 2706 std::string message
0000e5cc
PA
2707 = memory_error_message (TARGET_XFER_E_IO,
2708 bl->gdbarch, bl->address);
0000e5cc
PA
2709
2710 fprintf_unfiltered (tmp_error_stream,
2711 "Cannot insert breakpoint %d.\n"
2712 "%s\n",
1ccbe998 2713 bl->owner->number, message.c_str ());
0000e5cc
PA
2714 }
2715 else
2716 {
2717 fprintf_unfiltered (tmp_error_stream,
2718 "Cannot insert breakpoint %d: %s\n",
2719 bl->owner->number,
688fca4f 2720 bp_excpt.message);
0000e5cc 2721 }
879bfdc2 2722 }
0000e5cc 2723 return 1;
879bfdc2
DJ
2724
2725 }
2726 }
2727 else
35df4500 2728 bl->inserted = 1;
879bfdc2 2729
0000e5cc 2730 return 0;
879bfdc2
DJ
2731 }
2732
35df4500 2733 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2734 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2735 watchpoints. It's not clear that it's necessary... */
35df4500 2736 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2737 {
0000e5cc
PA
2738 int val;
2739
77b06cd7
TJB
2740 gdb_assert (bl->owner->ops != NULL
2741 && bl->owner->ops->insert_location != NULL);
2742
2743 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2744
2745 /* If trying to set a read-watchpoint, and it turns out it's not
2746 supported, try emulating one with an access watchpoint. */
35df4500 2747 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2748 {
2749 struct bp_location *loc, **loc_temp;
2750
2751 /* But don't try to insert it, if there's already another
2752 hw_access location that would be considered a duplicate
2753 of this one. */
2754 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2755 if (loc != bl
85d721b8 2756 && loc->watchpoint_type == hw_access
35df4500 2757 && watchpoint_locations_match (bl, loc))
85d721b8 2758 {
35df4500
TJB
2759 bl->duplicate = 1;
2760 bl->inserted = 1;
2761 bl->target_info = loc->target_info;
2762 bl->watchpoint_type = hw_access;
85d721b8
PA
2763 val = 0;
2764 break;
2765 }
2766
2767 if (val == 1)
2768 {
77b06cd7
TJB
2769 bl->watchpoint_type = hw_access;
2770 val = bl->owner->ops->insert_location (bl);
2771
2772 if (val)
2773 /* Back to the original value. */
2774 bl->watchpoint_type = hw_read;
85d721b8
PA
2775 }
2776 }
2777
35df4500 2778 bl->inserted = (val == 0);
879bfdc2
DJ
2779 }
2780
35df4500 2781 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2782 {
0000e5cc
PA
2783 int val;
2784
77b06cd7
TJB
2785 gdb_assert (bl->owner->ops != NULL
2786 && bl->owner->ops->insert_location != NULL);
2787
2788 val = bl->owner->ops->insert_location (bl);
2789 if (val)
2790 {
2791 bl->owner->enable_state = bp_disabled;
2792
2793 if (val == 1)
2794 warning (_("\
2795Error inserting catchpoint %d: Your system does not support this type\n\
2796of catchpoint."), bl->owner->number);
2797 else
2798 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2799 }
2800
2801 bl->inserted = (val == 0);
1640b821
DJ
2802
2803 /* We've already printed an error message if there was a problem
2804 inserting this catchpoint, and we've disabled the catchpoint,
2805 so just return success. */
2806 return 0;
879bfdc2
DJ
2807 }
2808
2809 return 0;
2810}
2811
6c95b8df
PA
2812/* This function is called when program space PSPACE is about to be
2813 deleted. It takes care of updating breakpoints to not reference
2814 PSPACE anymore. */
2815
2816void
2817breakpoint_program_space_exit (struct program_space *pspace)
2818{
2819 struct breakpoint *b, *b_temp;
876fa593 2820 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2821
2822 /* Remove any breakpoint that was set through this program space. */
2823 ALL_BREAKPOINTS_SAFE (b, b_temp)
2824 {
2825 if (b->pspace == pspace)
2826 delete_breakpoint (b);
2827 }
2828
2829 /* Breakpoints set through other program spaces could have locations
2830 bound to PSPACE as well. Remove those. */
876fa593 2831 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2832 {
2833 struct bp_location *tmp;
2834
2835 if (loc->pspace == pspace)
2836 {
2bdf28a0 2837 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2838 if (loc->owner->loc == loc)
2839 loc->owner->loc = loc->next;
2840 else
2841 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2842 if (tmp->next == loc)
2843 {
2844 tmp->next = loc->next;
2845 break;
2846 }
2847 }
2848 }
2849
2850 /* Now update the global location list to permanently delete the
2851 removed locations above. */
44702360 2852 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2853}
2854
74960c60
VP
2855/* Make sure all breakpoints are inserted in inferior.
2856 Throws exception on any error.
2857 A breakpoint that is already inserted won't be inserted
2858 again, so calling this function twice is safe. */
2859void
2860insert_breakpoints (void)
2861{
2862 struct breakpoint *bpt;
2863
2864 ALL_BREAKPOINTS (bpt)
2865 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2866 {
2867 struct watchpoint *w = (struct watchpoint *) bpt;
2868
2869 update_watchpoint (w, 0 /* don't reparse. */);
2870 }
74960c60 2871
04086b45
PA
2872 /* Updating watchpoints creates new locations, so update the global
2873 location list. Explicitly tell ugll to insert locations and
2874 ignore breakpoints_always_inserted_mode. */
2875 update_global_location_list (UGLL_INSERT);
74960c60
VP
2876}
2877
20388dd6
YQ
2878/* Invoke CALLBACK for each of bp_location. */
2879
2880void
2881iterate_over_bp_locations (walk_bp_location_callback callback)
2882{
2883 struct bp_location *loc, **loc_tmp;
2884
2885 ALL_BP_LOCATIONS (loc, loc_tmp)
2886 {
2887 callback (loc, NULL);
2888 }
2889}
2890
b775012e
LM
2891/* This is used when we need to synch breakpoint conditions between GDB and the
2892 target. It is the case with deleting and disabling of breakpoints when using
2893 always-inserted mode. */
2894
2895static void
2896update_inserted_breakpoint_locations (void)
2897{
2898 struct bp_location *bl, **blp_tmp;
2899 int error_flag = 0;
2900 int val = 0;
2901 int disabled_breaks = 0;
2902 int hw_breakpoint_error = 0;
dd61ec5c 2903 int hw_bp_details_reported = 0;
b775012e 2904
d7e74731 2905 string_file tmp_error_stream;
b775012e
LM
2906
2907 /* Explicitly mark the warning -- this will only be printed if
2908 there was an error. */
d7e74731 2909 tmp_error_stream.puts ("Warning:\n");
b775012e 2910
5ed8105e 2911 scoped_restore_current_pspace_and_thread restore_pspace_thread;
b775012e
LM
2912
2913 ALL_BP_LOCATIONS (bl, blp_tmp)
2914 {
2915 /* We only want to update software breakpoints and hardware
2916 breakpoints. */
2917 if (!is_breakpoint (bl->owner))
2918 continue;
2919
2920 /* We only want to update locations that are already inserted
2921 and need updating. This is to avoid unwanted insertion during
2922 deletion of breakpoints. */
2923 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2924 continue;
2925
2926 switch_to_program_space_and_thread (bl->pspace);
2927
2928 /* For targets that support global breakpoints, there's no need
2929 to select an inferior to insert breakpoint to. In fact, even
2930 if we aren't attached to any process yet, we should still
2931 insert breakpoints. */
f5656ead 2932 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
2933 && ptid_equal (inferior_ptid, null_ptid))
2934 continue;
2935
d7e74731 2936 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2937 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2938 if (val)
2939 error_flag = val;
2940 }
2941
2942 if (error_flag)
2943 {
223ffa71 2944 target_terminal::ours_for_output ();
b775012e
LM
2945 error_stream (tmp_error_stream);
2946 }
b775012e
LM
2947}
2948
c30eee59 2949/* Used when starting or continuing the program. */
c906108c 2950
74960c60
VP
2951static void
2952insert_breakpoint_locations (void)
c906108c 2953{
a5606eee 2954 struct breakpoint *bpt;
35df4500 2955 struct bp_location *bl, **blp_tmp;
eacd795a 2956 int error_flag = 0;
c906108c 2957 int val = 0;
3fbb6ffa 2958 int disabled_breaks = 0;
81d0cc19 2959 int hw_breakpoint_error = 0;
dd61ec5c 2960 int hw_bp_error_explained_already = 0;
c906108c 2961
d7e74731
PA
2962 string_file tmp_error_stream;
2963
81d0cc19
GS
2964 /* Explicitly mark the warning -- this will only be printed if
2965 there was an error. */
d7e74731 2966 tmp_error_stream.puts ("Warning:\n");
6c95b8df 2967
5ed8105e 2968 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 2969
35df4500 2970 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2971 {
b775012e 2972 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2973 continue;
2974
4a64f543
MS
2975 /* There is no point inserting thread-specific breakpoints if
2976 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2977 has BL->OWNER always non-NULL. */
35df4500 2978 if (bl->owner->thread != -1
5d5658a1 2979 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
2980 continue;
2981
35df4500 2982 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2983
2984 /* For targets that support global breakpoints, there's no need
2985 to select an inferior to insert breakpoint to. In fact, even
2986 if we aren't attached to any process yet, we should still
2987 insert breakpoints. */
f5656ead 2988 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
2989 && ptid_equal (inferior_ptid, null_ptid))
2990 continue;
2991
d7e74731 2992 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
dd61ec5c 2993 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 2994 if (val)
eacd795a 2995 error_flag = val;
879bfdc2 2996 }
c906108c 2997
4a64f543
MS
2998 /* If we failed to insert all locations of a watchpoint, remove
2999 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3000 ALL_BREAKPOINTS (bpt)
3001 {
3002 int some_failed = 0;
3003 struct bp_location *loc;
3004
3005 if (!is_hardware_watchpoint (bpt))
3006 continue;
3007
d6b74ac4 3008 if (!breakpoint_enabled (bpt))
a5606eee 3009 continue;
74960c60
VP
3010
3011 if (bpt->disposition == disp_del_at_next_stop)
3012 continue;
a5606eee
VP
3013
3014 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3015 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3016 {
3017 some_failed = 1;
3018 break;
3019 }
3020 if (some_failed)
3021 {
3022 for (loc = bpt->loc; loc; loc = loc->next)
3023 if (loc->inserted)
834c0d03 3024 remove_breakpoint (loc);
a5606eee
VP
3025
3026 hw_breakpoint_error = 1;
d7e74731
PA
3027 tmp_error_stream.printf ("Could not insert "
3028 "hardware watchpoint %d.\n",
3029 bpt->number);
eacd795a 3030 error_flag = -1;
a5606eee
VP
3031 }
3032 }
3033
eacd795a 3034 if (error_flag)
81d0cc19
GS
3035 {
3036 /* If a hardware breakpoint or watchpoint was inserted, add a
3037 message about possibly exhausted resources. */
dd61ec5c 3038 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3039 {
d7e74731 3040 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
c6510018 3041You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3042 }
223ffa71 3043 target_terminal::ours_for_output ();
81d0cc19
GS
3044 error_stream (tmp_error_stream);
3045 }
c906108c
SS
3046}
3047
c30eee59
TJB
3048/* Used when the program stops.
3049 Returns zero if successful, or non-zero if there was a problem
3050 removing a breakpoint location. */
3051
c906108c 3052int
fba45db2 3053remove_breakpoints (void)
c906108c 3054{
35df4500 3055 struct bp_location *bl, **blp_tmp;
3a1bae8e 3056 int val = 0;
c906108c 3057
35df4500 3058 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3059 {
1e4d1764 3060 if (bl->inserted && !is_tracepoint (bl->owner))
834c0d03 3061 val |= remove_breakpoint (bl);
c5aa993b 3062 }
3a1bae8e 3063 return val;
c906108c
SS
3064}
3065
49fa26b0
PA
3066/* When a thread exits, remove breakpoints that are related to
3067 that thread. */
3068
3069static void
3070remove_threaded_breakpoints (struct thread_info *tp, int silent)
3071{
3072 struct breakpoint *b, *b_tmp;
3073
3074 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3075 {
5d5658a1 3076 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3077 {
3078 b->disposition = disp_del_at_next_stop;
3079
3080 printf_filtered (_("\
43792cf0
PA
3081Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3082 b->number, print_thread_id (tp));
49fa26b0
PA
3083
3084 /* Hide it from the user. */
3085 b->number = 0;
3086 }
3087 }
3088}
3089
6c95b8df
PA
3090/* Remove breakpoints of process PID. */
3091
3092int
3093remove_breakpoints_pid (int pid)
3094{
35df4500 3095 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3096 int val;
3097 struct inferior *inf = find_inferior_pid (pid);
3098
35df4500 3099 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3100 {
35df4500 3101 if (bl->pspace != inf->pspace)
6c95b8df
PA
3102 continue;
3103
fc126975 3104 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3105 {
834c0d03 3106 val = remove_breakpoint (bl);
6c95b8df
PA
3107 if (val != 0)
3108 return val;
3109 }
3110 }
3111 return 0;
3112}
3113
e58b0e63
PA
3114static int internal_breakpoint_number = -1;
3115
84f4c1fe
PM
3116/* Set the breakpoint number of B, depending on the value of INTERNAL.
3117 If INTERNAL is non-zero, the breakpoint number will be populated
3118 from internal_breakpoint_number and that variable decremented.
e5dd4106 3119 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3120 breakpoint_count and that value incremented. Internal breakpoints
3121 do not set the internal var bpnum. */
3122static void
3123set_breakpoint_number (int internal, struct breakpoint *b)
3124{
3125 if (internal)
3126 b->number = internal_breakpoint_number--;
3127 else
3128 {
3129 set_breakpoint_count (breakpoint_count + 1);
3130 b->number = breakpoint_count;
3131 }
3132}
3133
e62c965a 3134static struct breakpoint *
a6d9a66e 3135create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3136 CORE_ADDR address, enum bptype type,
c0a91b2b 3137 const struct breakpoint_ops *ops)
e62c965a 3138{
51abb421 3139 symtab_and_line sal;
e62c965a
PP
3140 sal.pc = address;
3141 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3142 sal.pspace = current_program_space;
e62c965a 3143
51abb421 3144 breakpoint *b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3145 b->number = internal_breakpoint_number--;
3146 b->disposition = disp_donttouch;
3147
3148 return b;
3149}
3150
17450429
PP
3151static const char *const longjmp_names[] =
3152 {
3153 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3154 };
3155#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3156
3157/* Per-objfile data private to breakpoint.c. */
3158struct breakpoint_objfile_data
3159{
3160 /* Minimal symbol for "_ovly_debug_event" (if any). */
43dce439 3161 struct bound_minimal_symbol overlay_msym {};
17450429
PP
3162
3163 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
43dce439 3164 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES] {};
17450429 3165
28106bc2 3166 /* True if we have looked for longjmp probes. */
43dce439 3167 int longjmp_searched = 0;
28106bc2 3168
45461e0d
SM
3169 /* SystemTap probe points for longjmp (if any). These are non-owning
3170 references. */
3171 std::vector<probe *> longjmp_probes;
28106bc2 3172
17450429 3173 /* Minimal symbol for "std::terminate()" (if any). */
43dce439 3174 struct bound_minimal_symbol terminate_msym {};
17450429
PP
3175
3176 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
43dce439 3177 struct bound_minimal_symbol exception_msym {};
28106bc2
SDJ
3178
3179 /* True if we have looked for exception probes. */
43dce439 3180 int exception_searched = 0;
28106bc2 3181
45461e0d
SM
3182 /* SystemTap probe points for unwinding (if any). These are non-owning
3183 references. */
3184 std::vector<probe *> exception_probes;
17450429
PP
3185};
3186
3187static const struct objfile_data *breakpoint_objfile_key;
3188
3189/* Minimal symbol not found sentinel. */
3190static struct minimal_symbol msym_not_found;
3191
3192/* Returns TRUE if MSYM point to the "not found" sentinel. */
3193
3194static int
3195msym_not_found_p (const struct minimal_symbol *msym)
3196{
3197 return msym == &msym_not_found;
3198}
3199
3200/* Return per-objfile data needed by breakpoint.c.
3201 Allocate the data if necessary. */
3202
3203static struct breakpoint_objfile_data *
3204get_breakpoint_objfile_data (struct objfile *objfile)
3205{
3206 struct breakpoint_objfile_data *bp_objfile_data;
3207
9a3c8263
SM
3208 bp_objfile_data = ((struct breakpoint_objfile_data *)
3209 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3210 if (bp_objfile_data == NULL)
3211 {
43dce439 3212 bp_objfile_data = new breakpoint_objfile_data ();
17450429
PP
3213 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3214 }
3215 return bp_objfile_data;
3216}
3217
28106bc2 3218static void
43dce439 3219free_breakpoint_objfile_data (struct objfile *obj, void *data)
28106bc2 3220{
9a3c8263
SM
3221 struct breakpoint_objfile_data *bp_objfile_data
3222 = (struct breakpoint_objfile_data *) data;
28106bc2 3223
43dce439 3224 delete bp_objfile_data;
28106bc2
SDJ
3225}
3226
e62c965a 3227static void
af02033e 3228create_overlay_event_breakpoint (void)
e62c965a 3229{
69de3c6a 3230 struct objfile *objfile;
af02033e 3231 const char *const func_name = "_ovly_debug_event";
e62c965a 3232
69de3c6a
PP
3233 ALL_OBJFILES (objfile)
3234 {
3235 struct breakpoint *b;
17450429
PP
3236 struct breakpoint_objfile_data *bp_objfile_data;
3237 CORE_ADDR addr;
67994074 3238 struct explicit_location explicit_loc;
69de3c6a 3239
17450429
PP
3240 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3241
3b7344d5 3242 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3243 continue;
3244
3b7344d5 3245 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3246 {
3b7344d5 3247 struct bound_minimal_symbol m;
17450429
PP
3248
3249 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3250 if (m.minsym == NULL)
17450429
PP
3251 {
3252 /* Avoid future lookups in this objfile. */
3b7344d5 3253 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3254 continue;
3255 }
3256 bp_objfile_data->overlay_msym = m;
3257 }
e62c965a 3258
77e371c0 3259 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3260 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3261 bp_overlay_event,
3262 &internal_breakpoint_ops);
67994074
KS
3263 initialize_explicit_location (&explicit_loc);
3264 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3265 b->location = new_explicit_location (&explicit_loc);
e62c965a 3266
69de3c6a
PP
3267 if (overlay_debugging == ovly_auto)
3268 {
3269 b->enable_state = bp_enabled;
3270 overlay_events_enabled = 1;
3271 }
3272 else
3273 {
3274 b->enable_state = bp_disabled;
3275 overlay_events_enabled = 0;
3276 }
e62c965a 3277 }
e62c965a
PP
3278}
3279
0fd8e87f 3280static void
af02033e 3281create_longjmp_master_breakpoint (void)
0fd8e87f 3282{
6c95b8df 3283 struct program_space *pspace;
6c95b8df 3284
5ed8105e 3285 scoped_restore_current_program_space restore_pspace;
0fd8e87f 3286
6c95b8df 3287 ALL_PSPACES (pspace)
af02033e
PP
3288 {
3289 struct objfile *objfile;
3290
3291 set_current_program_space (pspace);
3292
3293 ALL_OBJFILES (objfile)
0fd8e87f 3294 {
af02033e
PP
3295 int i;
3296 struct gdbarch *gdbarch;
17450429 3297 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3298
af02033e 3299 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3300
17450429
PP
3301 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3302
28106bc2
SDJ
3303 if (!bp_objfile_data->longjmp_searched)
3304 {
45461e0d
SM
3305 std::vector<probe *> ret
3306 = find_probes_in_objfile (objfile, "libc", "longjmp");
25f9533e 3307
45461e0d 3308 if (!ret.empty ())
25f9533e
SDJ
3309 {
3310 /* We are only interested in checking one element. */
45461e0d 3311 probe *p = ret[0];
25f9533e
SDJ
3312
3313 if (!can_evaluate_probe_arguments (p))
3314 {
3315 /* We cannot use the probe interface here, because it does
3316 not know how to evaluate arguments. */
45461e0d 3317 ret.clear ();
25f9533e
SDJ
3318 }
3319 }
3320 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3321 bp_objfile_data->longjmp_searched = 1;
3322 }
3323
45461e0d 3324 if (!bp_objfile_data->longjmp_probes.empty ())
28106bc2 3325 {
28106bc2
SDJ
3326 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3327
45461e0d 3328 for (probe *p : bp_objfile_data->longjmp_probes)
28106bc2
SDJ
3329 {
3330 struct breakpoint *b;
3331
729662a5 3332 b = create_internal_breakpoint (gdbarch,
45461e0d 3333 get_probe_address (p, objfile),
28106bc2
SDJ
3334 bp_longjmp_master,
3335 &internal_breakpoint_ops);
d28cd78a 3336 b->location = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3337 b->enable_state = bp_disabled;
3338 }
3339
3340 continue;
3341 }
3342
0569175e
TSD
3343 if (!gdbarch_get_longjmp_target_p (gdbarch))
3344 continue;
3345
17450429 3346 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3347 {
3348 struct breakpoint *b;
af02033e 3349 const char *func_name;
17450429 3350 CORE_ADDR addr;
67994074 3351 struct explicit_location explicit_loc;
6c95b8df 3352
3b7344d5 3353 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3354 continue;
0fd8e87f 3355
17450429 3356 func_name = longjmp_names[i];
3b7344d5 3357 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3358 {
3b7344d5 3359 struct bound_minimal_symbol m;
17450429
PP
3360
3361 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3362 if (m.minsym == NULL)
17450429
PP
3363 {
3364 /* Prevent future lookups in this objfile. */
3b7344d5 3365 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3366 continue;
3367 }
3368 bp_objfile_data->longjmp_msym[i] = m;
3369 }
3370
77e371c0 3371 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3372 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3373 &internal_breakpoint_ops);
67994074
KS
3374 initialize_explicit_location (&explicit_loc);
3375 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3376 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3377 b->enable_state = bp_disabled;
3378 }
0fd8e87f 3379 }
af02033e 3380 }
0fd8e87f
UW
3381}
3382
af02033e 3383/* Create a master std::terminate breakpoint. */
aa7d318d 3384static void
af02033e 3385create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3386{
3387 struct program_space *pspace;
af02033e 3388 const char *const func_name = "std::terminate()";
aa7d318d 3389
5ed8105e 3390 scoped_restore_current_program_space restore_pspace;
aa7d318d
TT
3391
3392 ALL_PSPACES (pspace)
17450429
PP
3393 {
3394 struct objfile *objfile;
3395 CORE_ADDR addr;
3396
3397 set_current_program_space (pspace);
3398
aa7d318d
TT
3399 ALL_OBJFILES (objfile)
3400 {
3401 struct breakpoint *b;
17450429 3402 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3403 struct explicit_location explicit_loc;
aa7d318d 3404
17450429 3405 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3406
3b7344d5 3407 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3408 continue;
3409
3b7344d5 3410 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3411 {
3b7344d5 3412 struct bound_minimal_symbol m;
17450429
PP
3413
3414 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3415 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3416 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3417 {
3418 /* Prevent future lookups in this objfile. */
3b7344d5 3419 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3420 continue;
3421 }
3422 bp_objfile_data->terminate_msym = m;
3423 }
aa7d318d 3424
77e371c0 3425 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3426 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3427 bp_std_terminate_master,
3428 &internal_breakpoint_ops);
67994074
KS
3429 initialize_explicit_location (&explicit_loc);
3430 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3431 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3432 b->enable_state = bp_disabled;
3433 }
17450429 3434 }
aa7d318d
TT
3435}
3436
186c406b
TT
3437/* Install a master breakpoint on the unwinder's debug hook. */
3438
70221824 3439static void
186c406b
TT
3440create_exception_master_breakpoint (void)
3441{
3442 struct objfile *objfile;
17450429 3443 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3444
3445 ALL_OBJFILES (objfile)
3446 {
17450429
PP
3447 struct breakpoint *b;
3448 struct gdbarch *gdbarch;
3449 struct breakpoint_objfile_data *bp_objfile_data;
3450 CORE_ADDR addr;
67994074 3451 struct explicit_location explicit_loc;
17450429
PP
3452
3453 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3454
28106bc2
SDJ
3455 /* We prefer the SystemTap probe point if it exists. */
3456 if (!bp_objfile_data->exception_searched)
3457 {
45461e0d
SM
3458 std::vector<probe *> ret
3459 = find_probes_in_objfile (objfile, "libgcc", "unwind");
25f9533e 3460
45461e0d 3461 if (!ret.empty ())
25f9533e
SDJ
3462 {
3463 /* We are only interested in checking one element. */
45461e0d 3464 probe *p = ret[0];
25f9533e
SDJ
3465
3466 if (!can_evaluate_probe_arguments (p))
3467 {
3468 /* We cannot use the probe interface here, because it does
3469 not know how to evaluate arguments. */
45461e0d 3470 ret.clear ();
25f9533e
SDJ
3471 }
3472 }
3473 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3474 bp_objfile_data->exception_searched = 1;
3475 }
3476
45461e0d 3477 if (!bp_objfile_data->exception_probes.empty ())
28106bc2
SDJ
3478 {
3479 struct gdbarch *gdbarch = get_objfile_arch (objfile);
45461e0d
SM
3480
3481 for (probe *p : bp_objfile_data->exception_probes)
28106bc2
SDJ
3482 {
3483 struct breakpoint *b;
3484
729662a5 3485 b = create_internal_breakpoint (gdbarch,
45461e0d 3486 get_probe_address (p, objfile),
28106bc2
SDJ
3487 bp_exception_master,
3488 &internal_breakpoint_ops);
d28cd78a 3489 b->location = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3490 b->enable_state = bp_disabled;
3491 }
3492
3493 continue;
3494 }
3495
3496 /* Otherwise, try the hook function. */
3497
3b7344d5 3498 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3499 continue;
3500
3501 gdbarch = get_objfile_arch (objfile);
186c406b 3502
3b7344d5 3503 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3504 {
3b7344d5 3505 struct bound_minimal_symbol debug_hook;
186c406b 3506
17450429 3507 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3508 if (debug_hook.minsym == NULL)
17450429 3509 {
3b7344d5 3510 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3511 continue;
3512 }
3513
3514 bp_objfile_data->exception_msym = debug_hook;
186c406b 3515 }
17450429 3516
77e371c0 3517 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3518 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3519 &current_target);
06edf0c0
PA
3520 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3521 &internal_breakpoint_ops);
67994074
KS
3522 initialize_explicit_location (&explicit_loc);
3523 explicit_loc.function_name = ASTRDUP (func_name);
d28cd78a 3524 b->location = new_explicit_location (&explicit_loc);
17450429 3525 b->enable_state = bp_disabled;
186c406b 3526 }
186c406b
TT
3527}
3528
9ef9e6a6
KS
3529/* Does B have a location spec? */
3530
3531static int
3532breakpoint_event_location_empty_p (const struct breakpoint *b)
3533{
d28cd78a 3534 return b->location != NULL && event_location_empty_p (b->location.get ());
9ef9e6a6
KS
3535}
3536
c906108c 3537void
fba45db2 3538update_breakpoints_after_exec (void)
c906108c 3539{
35df4500 3540 struct breakpoint *b, *b_tmp;
876fa593 3541 struct bp_location *bploc, **bplocp_tmp;
c906108c 3542
25b22b0a
PA
3543 /* We're about to delete breakpoints from GDB's lists. If the
3544 INSERTED flag is true, GDB will try to lift the breakpoints by
3545 writing the breakpoints' "shadow contents" back into memory. The
3546 "shadow contents" are NOT valid after an exec, so GDB should not
3547 do that. Instead, the target is responsible from marking
3548 breakpoints out as soon as it detects an exec. We don't do that
3549 here instead, because there may be other attempts to delete
3550 breakpoints after detecting an exec and before reaching here. */
876fa593 3551 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3552 if (bploc->pspace == current_program_space)
3553 gdb_assert (!bploc->inserted);
c906108c 3554
35df4500 3555 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3556 {
6c95b8df
PA
3557 if (b->pspace != current_program_space)
3558 continue;
3559
4a64f543 3560 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3561 if (b->type == bp_shlib_event)
3562 {
3563 delete_breakpoint (b);
3564 continue;
3565 }
c906108c 3566
4a64f543 3567 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3568 if (b->type == bp_jit_event)
3569 {
3570 delete_breakpoint (b);
3571 continue;
3572 }
3573
1900040c 3574 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3575 as must overlay event and longjmp master breakpoints. */
3576 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3577 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3578 || b->type == bp_exception_master)
c4093a6a
JM
3579 {
3580 delete_breakpoint (b);
3581 continue;
3582 }
3583
4a64f543 3584 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3585 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3586 {
3587 delete_breakpoint (b);
3588 continue;
3589 }
3590
7c16b83e
PA
3591 /* Just like single-step breakpoints. */
3592 if (b->type == bp_single_step)
3593 {
3594 delete_breakpoint (b);
3595 continue;
3596 }
3597
611c83ae
PA
3598 /* Longjmp and longjmp-resume breakpoints are also meaningless
3599 after an exec. */
186c406b 3600 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3601 || b->type == bp_longjmp_call_dummy
186c406b 3602 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3603 {
3604 delete_breakpoint (b);
3605 continue;
3606 }
3607
ce78b96d
JB
3608 if (b->type == bp_catchpoint)
3609 {
3610 /* For now, none of the bp_catchpoint breakpoints need to
3611 do anything at this point. In the future, if some of
3612 the catchpoints need to something, we will need to add
3613 a new method, and call this method from here. */
3614 continue;
3615 }
3616
c5aa993b
JM
3617 /* bp_finish is a special case. The only way we ought to be able
3618 to see one of these when an exec() has happened, is if the user
3619 caught a vfork, and then said "finish". Ordinarily a finish just
3620 carries them to the call-site of the current callee, by setting
3621 a temporary bp there and resuming. But in this case, the finish
3622 will carry them entirely through the vfork & exec.
3623
3624 We don't want to allow a bp_finish to remain inserted now. But
3625 we can't safely delete it, 'cause finish_command has a handle to
3626 the bp on a bpstat, and will later want to delete it. There's a
3627 chance (and I've seen it happen) that if we delete the bp_finish
3628 here, that its storage will get reused by the time finish_command
3629 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3630 We really must allow finish_command to delete a bp_finish.
3631
e5dd4106 3632 In the absence of a general solution for the "how do we know
53a5351d
JM
3633 it's safe to delete something others may have handles to?"
3634 problem, what we'll do here is just uninsert the bp_finish, and
3635 let finish_command delete it.
3636
3637 (We know the bp_finish is "doomed" in the sense that it's
3638 momentary, and will be deleted as soon as finish_command sees
3639 the inferior stopped. So it doesn't matter that the bp's
3640 address is probably bogus in the new a.out, unlike e.g., the
3641 solib breakpoints.) */
c5aa993b 3642
c5aa993b
JM
3643 if (b->type == bp_finish)
3644 {
3645 continue;
3646 }
3647
3648 /* Without a symbolic address, we have little hope of the
3649 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3650 a.out. */
9ef9e6a6 3651 if (breakpoint_event_location_empty_p (b))
c5aa993b
JM
3652 {
3653 delete_breakpoint (b);
3654 continue;
3655 }
c5aa993b 3656 }
c906108c
SS
3657}
3658
3659int
d80ee84f 3660detach_breakpoints (ptid_t ptid)
c906108c 3661{
35df4500 3662 struct bp_location *bl, **blp_tmp;
3a1bae8e 3663 int val = 0;
2989a365 3664 scoped_restore save_inferior_ptid = make_scoped_restore (&inferior_ptid);
6c95b8df 3665 struct inferior *inf = current_inferior ();
c5aa993b 3666
dfd4cc63 3667 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3668 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3669
6c95b8df 3670 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3671 inferior_ptid = ptid;
35df4500 3672 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3673 {
35df4500 3674 if (bl->pspace != inf->pspace)
6c95b8df
PA
3675 continue;
3676
bd9673a4
PW
3677 /* This function must physically remove breakpoints locations
3678 from the specified ptid, without modifying the breakpoint
3679 package's state. Locations of type bp_loc_other are only
3680 maintained at GDB side. So, there is no need to remove
3681 these bp_loc_other locations. Moreover, removing these
3682 would modify the breakpoint package's state. */
3683 if (bl->loc_type == bp_loc_other)
3684 continue;
3685
35df4500 3686 if (bl->inserted)
b2b6a7da 3687 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
c5aa993b 3688 }
d03285ec 3689
3a1bae8e 3690 return val;
c906108c
SS
3691}
3692
35df4500 3693/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3694 Note that this is used to detach breakpoints from a child fork.
3695 When we get here, the child isn't in the inferior list, and neither
3696 do we have objects to represent its address space --- we should
35df4500 3697 *not* look at bl->pspace->aspace here. */
6c95b8df 3698
c906108c 3699static int
b2b6a7da 3700remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
c906108c
SS
3701{
3702 int val;
c5aa993b 3703
35df4500
TJB
3704 /* BL is never in moribund_locations by our callers. */
3705 gdb_assert (bl->owner != NULL);
2bdf28a0 3706
74960c60
VP
3707 /* The type of none suggests that owner is actually deleted.
3708 This should not ever happen. */
35df4500 3709 gdb_assert (bl->owner->type != bp_none);
0bde7532 3710
35df4500
TJB
3711 if (bl->loc_type == bp_loc_software_breakpoint
3712 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3713 {
c02f5703
MS
3714 /* "Normal" instruction breakpoint: either the standard
3715 trap-instruction bp (bp_breakpoint), or a
3716 bp_hardware_breakpoint. */
3717
3718 /* First check to see if we have to handle an overlay. */
3719 if (overlay_debugging == ovly_off
35df4500
TJB
3720 || bl->section == NULL
3721 || !(section_is_overlay (bl->section)))
c02f5703
MS
3722 {
3723 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3724
3725 /* If we're trying to uninsert a memory breakpoint that we
3726 know is set in a dynamic object that is marked
3727 shlib_disabled, then either the dynamic object was
3728 removed with "remove-symbol-file" or with
3729 "nosharedlibrary". In the former case, we don't know
3730 whether another dynamic object might have loaded over the
3731 breakpoint's address -- the user might well let us know
3732 about it next with add-symbol-file (the whole point of
d03de421 3733 add-symbol-file is letting the user manually maintain a
08351840
PA
3734 list of dynamically loaded objects). If we have the
3735 breakpoint's shadow memory, that is, this is a software
3736 breakpoint managed by GDB, check whether the breakpoint
3737 is still inserted in memory, to avoid overwriting wrong
3738 code with stale saved shadow contents. Note that HW
3739 breakpoints don't have shadow memory, as they're
3740 implemented using a mechanism that is not dependent on
3741 being able to modify the target's memory, and as such
3742 they should always be removed. */
3743 if (bl->shlib_disabled
3744 && bl->target_info.shadow_len != 0
3745 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3746 val = 0;
3747 else
73971819 3748 val = bl->owner->ops->remove_location (bl, reason);
c02f5703 3749 }
c906108c
SS
3750 else
3751 {
4a64f543 3752 /* This breakpoint is in an overlay section.
c02f5703
MS
3753 Did we set a breakpoint at the LMA? */
3754 if (!overlay_events_enabled)
3755 {
3756 /* Yes -- overlay event support is not active, so we
3757 should have set a breakpoint at the LMA. Remove it.
3758 */
c02f5703
MS
3759 /* Ignore any failures: if the LMA is in ROM, we will
3760 have already warned when we failed to insert it. */
35df4500
TJB
3761 if (bl->loc_type == bp_loc_hardware_breakpoint)
3762 target_remove_hw_breakpoint (bl->gdbarch,
3763 &bl->overlay_target_info);
c02f5703 3764 else
35df4500 3765 target_remove_breakpoint (bl->gdbarch,
73971819
PA
3766 &bl->overlay_target_info,
3767 reason);
c02f5703
MS
3768 }
3769 /* Did we set a breakpoint at the VMA?
3770 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3771 if (bl->inserted)
c906108c 3772 {
c02f5703
MS
3773 /* Yes -- remove it. Previously we did not bother to
3774 remove the breakpoint if the section had been
3775 unmapped, but let's not rely on that being safe. We
3776 don't know what the overlay manager might do. */
aa67235e
UW
3777
3778 /* However, we should remove *software* breakpoints only
3779 if the section is still mapped, or else we overwrite
3780 wrong code with the saved shadow contents. */
348d480f
PA
3781 if (bl->loc_type == bp_loc_hardware_breakpoint
3782 || section_is_mapped (bl->section))
73971819 3783 val = bl->owner->ops->remove_location (bl, reason);
aa67235e
UW
3784 else
3785 val = 0;
c906108c 3786 }
c02f5703
MS
3787 else
3788 {
3789 /* No -- not inserted, so no need to remove. No error. */
3790 val = 0;
3791 }
c906108c 3792 }
879d1e6b 3793
08351840
PA
3794 /* In some cases, we might not be able to remove a breakpoint in
3795 a shared library that has already been removed, but we have
3796 not yet processed the shlib unload event. Similarly for an
3797 unloaded add-symbol-file object - the user might not yet have
3798 had the chance to remove-symbol-file it. shlib_disabled will
3799 be set if the library/object has already been removed, but
3800 the breakpoint hasn't been uninserted yet, e.g., after
3801 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3802 always-inserted mode. */
076855f9 3803 if (val
08351840
PA
3804 && (bl->loc_type == bp_loc_software_breakpoint
3805 && (bl->shlib_disabled
3806 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3807 || shared_objfile_contains_address_p (bl->pspace,
3808 bl->address))))
879d1e6b
UW
3809 val = 0;
3810
c906108c
SS
3811 if (val)
3812 return val;
b2b6a7da 3813 bl->inserted = (reason == DETACH_BREAKPOINT);
c906108c 3814 }
35df4500 3815 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3816 {
77b06cd7
TJB
3817 gdb_assert (bl->owner->ops != NULL
3818 && bl->owner->ops->remove_location != NULL);
3819
b2b6a7da 3820 bl->inserted = (reason == DETACH_BREAKPOINT);
73971819 3821 bl->owner->ops->remove_location (bl, reason);
2e70b7b9 3822
c906108c 3823 /* Failure to remove any of the hardware watchpoints comes here. */
b2b6a7da 3824 if (reason == REMOVE_BREAKPOINT && bl->inserted)
8a3fe4f8 3825 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3826 bl->owner->number);
c906108c 3827 }
35df4500
TJB
3828 else if (bl->owner->type == bp_catchpoint
3829 && breakpoint_enabled (bl->owner)
3830 && !bl->duplicate)
ce78b96d 3831 {
77b06cd7
TJB
3832 gdb_assert (bl->owner->ops != NULL
3833 && bl->owner->ops->remove_location != NULL);
ce78b96d 3834
73971819 3835 val = bl->owner->ops->remove_location (bl, reason);
ce78b96d
JB
3836 if (val)
3837 return val;
77b06cd7 3838
b2b6a7da 3839 bl->inserted = (reason == DETACH_BREAKPOINT);
ce78b96d 3840 }
c906108c
SS
3841
3842 return 0;
3843}
3844
6c95b8df 3845static int
834c0d03 3846remove_breakpoint (struct bp_location *bl)
6c95b8df 3847{
35df4500
TJB
3848 /* BL is never in moribund_locations by our callers. */
3849 gdb_assert (bl->owner != NULL);
2bdf28a0 3850
6c95b8df
PA
3851 /* The type of none suggests that owner is actually deleted.
3852 This should not ever happen. */
35df4500 3853 gdb_assert (bl->owner->type != bp_none);
6c95b8df 3854
5ed8105e 3855 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 3856
35df4500 3857 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3858
5ed8105e 3859 return remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
6c95b8df
PA
3860}
3861
c906108c
SS
3862/* Clear the "inserted" flag in all breakpoints. */
3863
25b22b0a 3864void
fba45db2 3865mark_breakpoints_out (void)
c906108c 3866{
35df4500 3867 struct bp_location *bl, **blp_tmp;
c906108c 3868
35df4500 3869 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 3870 if (bl->pspace == current_program_space)
35df4500 3871 bl->inserted = 0;
c906108c
SS
3872}
3873
53a5351d
JM
3874/* Clear the "inserted" flag in all breakpoints and delete any
3875 breakpoints which should go away between runs of the program.
c906108c
SS
3876
3877 Plus other such housekeeping that has to be done for breakpoints
3878 between runs.
3879
53a5351d
JM
3880 Note: this function gets called at the end of a run (by
3881 generic_mourn_inferior) and when a run begins (by
4a64f543 3882 init_wait_for_inferior). */
c906108c
SS
3883
3884
3885
3886void
fba45db2 3887breakpoint_init_inferior (enum inf_context context)
c906108c 3888{
35df4500 3889 struct breakpoint *b, *b_tmp;
870f88f7 3890 struct bp_location *bl;
1c5cfe86 3891 int ix;
6c95b8df 3892 struct program_space *pspace = current_program_space;
c906108c 3893
50c71eaf
PA
3894 /* If breakpoint locations are shared across processes, then there's
3895 nothing to do. */
f5656ead 3896 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3897 return;
3898
1a853c52 3899 mark_breakpoints_out ();
075f6582 3900
35df4500 3901 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3902 {
6c95b8df
PA
3903 if (b->loc && b->loc->pspace != pspace)
3904 continue;
3905
c5aa993b
JM
3906 switch (b->type)
3907 {
3908 case bp_call_dummy:
e2e4d78b 3909 case bp_longjmp_call_dummy:
c906108c 3910
c5aa993b 3911 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3912 cause problems when the inferior is rerun, so we better get
3913 rid of it. */
3914
3915 case bp_watchpoint_scope:
3916
3917 /* Also get rid of scope breakpoints. */
3918
3919 case bp_shlib_event:
3920
3921 /* Also remove solib event breakpoints. Their addresses may
3922 have changed since the last time we ran the program.
3923 Actually we may now be debugging against different target;
3924 and so the solib backend that installed this breakpoint may
3925 not be used in by the target. E.g.,
3926
3927 (gdb) file prog-linux
3928 (gdb) run # native linux target
3929 ...
3930 (gdb) kill
3931 (gdb) file prog-win.exe
3932 (gdb) tar rem :9999 # remote Windows gdbserver.
3933 */
c906108c 3934
f59f708a
PA
3935 case bp_step_resume:
3936
3937 /* Also remove step-resume breakpoints. */
3938
7c16b83e
PA
3939 case bp_single_step:
3940
3941 /* Also remove single-step breakpoints. */
3942
c5aa993b
JM
3943 delete_breakpoint (b);
3944 break;
c906108c 3945
c5aa993b
JM
3946 case bp_watchpoint:
3947 case bp_hardware_watchpoint:
3948 case bp_read_watchpoint:
3949 case bp_access_watchpoint:
3a5c3e22
PA
3950 {
3951 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3952
3a5c3e22
PA
3953 /* Likewise for watchpoints on local expressions. */
3954 if (w->exp_valid_block != NULL)
3955 delete_breakpoint (b);
63000888 3956 else
3a5c3e22 3957 {
63000888
PA
3958 /* Get rid of existing locations, which are no longer
3959 valid. New ones will be created in
3960 update_watchpoint, when the inferior is restarted.
3961 The next update_global_location_list call will
3962 garbage collect them. */
3963 b->loc = NULL;
3964
3965 if (context == inf_starting)
3966 {
3967 /* Reset val field to force reread of starting value in
3968 insert_breakpoints. */
3969 if (w->val)
3970 value_free (w->val);
3971 w->val = NULL;
3972 w->val_valid = 0;
3973 }
3974 }
3a5c3e22 3975 }
c5aa993b
JM
3976 break;
3977 default:
c5aa993b
JM
3978 break;
3979 }
3980 }
1c5cfe86
PA
3981
3982 /* Get rid of the moribund locations. */
35df4500
TJB
3983 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3984 decref_bp_location (&bl);
1c5cfe86 3985 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
3986}
3987
6c95b8df
PA
3988/* These functions concern about actual breakpoints inserted in the
3989 target --- to e.g. check if we need to do decr_pc adjustment or if
3990 we need to hop over the bkpt --- so we check for address space
3991 match, not program space. */
3992
c2c6d25f
JM
3993/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3994 exists at PC. It returns ordinary_breakpoint_here if it's an
3995 ordinary breakpoint, or permanent_breakpoint_here if it's a
3996 permanent breakpoint.
3997 - When continuing from a location with an ordinary breakpoint, we
3998 actually single step once before calling insert_breakpoints.
e5dd4106 3999 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4000 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4001 the target, to advance the PC past the breakpoint. */
c906108c 4002
c2c6d25f 4003enum breakpoint_here
accd0bcd 4004breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4005{
35df4500 4006 struct bp_location *bl, **blp_tmp;
c2c6d25f 4007 int any_breakpoint_here = 0;
c906108c 4008
35df4500 4009 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4010 {
35df4500
TJB
4011 if (bl->loc_type != bp_loc_software_breakpoint
4012 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4013 continue;
4014
f1310107 4015 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4016 if ((breakpoint_enabled (bl->owner)
1a853c52 4017 || bl->permanent)
f1310107 4018 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4019 {
4020 if (overlay_debugging
35df4500
TJB
4021 && section_is_overlay (bl->section)
4022 && !section_is_mapped (bl->section))
075f6582 4023 continue; /* unmapped overlay -- can't be a match */
1a853c52 4024 else if (bl->permanent)
075f6582
DJ
4025 return permanent_breakpoint_here;
4026 else
4027 any_breakpoint_here = 1;
4028 }
4029 }
c906108c 4030
f486487f 4031 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4032}
4033
d35ae833
PA
4034/* See breakpoint.h. */
4035
4036int
accd0bcd 4037breakpoint_in_range_p (const address_space *aspace,
d35ae833
PA
4038 CORE_ADDR addr, ULONGEST len)
4039{
4040 struct bp_location *bl, **blp_tmp;
4041
4042 ALL_BP_LOCATIONS (bl, blp_tmp)
4043 {
4044 if (bl->loc_type != bp_loc_software_breakpoint
4045 && bl->loc_type != bp_loc_hardware_breakpoint)
4046 continue;
4047
4048 if ((breakpoint_enabled (bl->owner)
4049 || bl->permanent)
4050 && breakpoint_location_address_range_overlap (bl, aspace,
4051 addr, len))
4052 {
4053 if (overlay_debugging
4054 && section_is_overlay (bl->section)
4055 && !section_is_mapped (bl->section))
4056 {
4057 /* Unmapped overlay -- can't be a match. */
4058 continue;
4059 }
4060
4061 return 1;
4062 }
4063 }
4064
4065 return 0;
4066}
4067
1c5cfe86
PA
4068/* Return true if there's a moribund breakpoint at PC. */
4069
4070int
accd0bcd 4071moribund_breakpoint_here_p (const address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4072{
4073 struct bp_location *loc;
4074 int ix;
4075
4076 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4077 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4078 return 1;
4079
4080 return 0;
4081}
c2c6d25f 4082
f7ce857f
PA
4083/* Returns non-zero iff BL is inserted at PC, in address space
4084 ASPACE. */
4085
4086static int
4087bp_location_inserted_here_p (struct bp_location *bl,
accd0bcd 4088 const address_space *aspace, CORE_ADDR pc)
f7ce857f
PA
4089{
4090 if (bl->inserted
4091 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4092 aspace, pc))
4093 {
4094 if (overlay_debugging
4095 && section_is_overlay (bl->section)
4096 && !section_is_mapped (bl->section))
4097 return 0; /* unmapped overlay -- can't be a match */
4098 else
4099 return 1;
4100 }
4101 return 0;
4102}
4103
a1fd2fa5 4104/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4105
4106int
accd0bcd 4107breakpoint_inserted_here_p (const address_space *aspace, CORE_ADDR pc)
c906108c 4108{
f7ce857f 4109 struct bp_location **blp, **blp_tmp = NULL;
c906108c 4110
f7ce857f 4111 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4112 {
f7ce857f
PA
4113 struct bp_location *bl = *blp;
4114
35df4500
TJB
4115 if (bl->loc_type != bp_loc_software_breakpoint
4116 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4117 continue;
4118
f7ce857f
PA
4119 if (bp_location_inserted_here_p (bl, aspace, pc))
4120 return 1;
c5aa993b 4121 }
c36b740a
VP
4122 return 0;
4123}
4124
a1fd2fa5
PA
4125/* This function returns non-zero iff there is a software breakpoint
4126 inserted at PC. */
c36b740a
VP
4127
4128int
accd0bcd 4129software_breakpoint_inserted_here_p (const address_space *aspace,
a1fd2fa5 4130 CORE_ADDR pc)
4fa8626c 4131{
f7ce857f 4132 struct bp_location **blp, **blp_tmp = NULL;
4fa8626c 4133
f7ce857f 4134 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4135 {
f7ce857f
PA
4136 struct bp_location *bl = *blp;
4137
35df4500 4138 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4139 continue;
4140
f7ce857f
PA
4141 if (bp_location_inserted_here_p (bl, aspace, pc))
4142 return 1;
4fa8626c
DJ
4143 }
4144
4145 return 0;
9c02b525
PA
4146}
4147
4148/* See breakpoint.h. */
4149
4150int
accd0bcd 4151hardware_breakpoint_inserted_here_p (const address_space *aspace,
9c02b525
PA
4152 CORE_ADDR pc)
4153{
4154 struct bp_location **blp, **blp_tmp = NULL;
9c02b525
PA
4155
4156 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4157 {
4158 struct bp_location *bl = *blp;
4159
4160 if (bl->loc_type != bp_loc_hardware_breakpoint)
4161 continue;
4162
4163 if (bp_location_inserted_here_p (bl, aspace, pc))
4164 return 1;
4165 }
4166
4167 return 0;
4fa8626c
DJ
4168}
4169
9093389c 4170int
accd0bcd 4171hardware_watchpoint_inserted_in_range (const address_space *aspace,
9093389c
PA
4172 CORE_ADDR addr, ULONGEST len)
4173{
4174 struct breakpoint *bpt;
4175
4176 ALL_BREAKPOINTS (bpt)
4177 {
4178 struct bp_location *loc;
4179
4180 if (bpt->type != bp_hardware_watchpoint
4181 && bpt->type != bp_access_watchpoint)
4182 continue;
4183
4184 if (!breakpoint_enabled (bpt))
4185 continue;
4186
4187 for (loc = bpt->loc; loc; loc = loc->next)
4188 if (loc->pspace->aspace == aspace && loc->inserted)
4189 {
4190 CORE_ADDR l, h;
4191
4192 /* Check for intersection. */
768adc05
PA
4193 l = std::max<CORE_ADDR> (loc->address, addr);
4194 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
9093389c
PA
4195 if (l < h)
4196 return 1;
4197 }
4198 }
4199 return 0;
4200}
c906108c 4201\f
c5aa993b 4202
c906108c
SS
4203/* bpstat stuff. External routines' interfaces are documented
4204 in breakpoint.h. */
4205
4206int
c326b90e 4207is_catchpoint (struct breakpoint *ep)
c906108c 4208{
533be4dd 4209 return (ep->type == bp_catchpoint);
c906108c
SS
4210}
4211
f431efe5
PA
4212/* Frees any storage that is part of a bpstat. Does not walk the
4213 'next' chain. */
4214
04afa70c 4215bpstats::~bpstats ()
198757a8 4216{
04afa70c
TT
4217 if (old_val != NULL)
4218 value_free (old_val);
04afa70c
TT
4219 if (bp_location_at != NULL)
4220 decref_bp_location (&bp_location_at);
198757a8
VP
4221}
4222
c906108c
SS
4223/* Clear a bpstat so that it says we are not at any breakpoint.
4224 Also free any storage that is part of a bpstat. */
4225
4226void
fba45db2 4227bpstat_clear (bpstat *bsp)
c906108c
SS
4228{
4229 bpstat p;
4230 bpstat q;
4231
4232 if (bsp == 0)
4233 return;
4234 p = *bsp;
4235 while (p != NULL)
4236 {
4237 q = p->next;
04afa70c 4238 delete p;
c906108c
SS
4239 p = q;
4240 }
4241 *bsp = NULL;
4242}
4243
04afa70c
TT
4244bpstats::bpstats (const bpstats &other)
4245 : next (NULL),
4246 bp_location_at (other.bp_location_at),
4247 breakpoint_at (other.breakpoint_at),
4248 commands (other.commands),
4249 old_val (other.old_val),
4250 print (other.print),
4251 stop (other.stop),
4252 print_it (other.print_it)
4253{
4254 if (old_val != NULL)
4255 {
4256 old_val = value_copy (old_val);
4257 release_value (old_val);
4258 }
4259 incref_bp_location (bp_location_at);
04afa70c
TT
4260}
4261
c906108c
SS
4262/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4263 is part of the bpstat is copied as well. */
4264
4265bpstat
fba45db2 4266bpstat_copy (bpstat bs)
c906108c
SS
4267{
4268 bpstat p = NULL;
4269 bpstat tmp;
4270 bpstat retval = NULL;
4271
4272 if (bs == NULL)
4273 return bs;
4274
4275 for (; bs != NULL; bs = bs->next)
4276 {
04afa70c 4277 tmp = new bpstats (*bs);
31cc81e9 4278
c906108c
SS
4279 if (p == NULL)
4280 /* This is the first thing in the chain. */
4281 retval = tmp;
4282 else
4283 p->next = tmp;
4284 p = tmp;
4285 }
4286 p->next = NULL;
4287 return retval;
4288}
4289
4a64f543 4290/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4291
4292bpstat
fba45db2 4293bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4294{
c5aa993b
JM
4295 if (bsp == NULL)
4296 return NULL;
c906108c 4297
c5aa993b
JM
4298 for (; bsp != NULL; bsp = bsp->next)
4299 {
f431efe5 4300 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4301 return bsp;
4302 }
c906108c
SS
4303 return NULL;
4304}
4305
ab04a2af
TT
4306/* See breakpoint.h. */
4307
47591c29 4308int
427cd150 4309bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4310{
ab04a2af
TT
4311 for (; bsp != NULL; bsp = bsp->next)
4312 {
427cd150
TT
4313 if (bsp->breakpoint_at == NULL)
4314 {
4315 /* A moribund location can never explain a signal other than
4316 GDB_SIGNAL_TRAP. */
4317 if (sig == GDB_SIGNAL_TRAP)
47591c29 4318 return 1;
427cd150
TT
4319 }
4320 else
47591c29
PA
4321 {
4322 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4323 sig))
4324 return 1;
4325 }
ab04a2af
TT
4326 }
4327
47591c29 4328 return 0;
ab04a2af
TT
4329}
4330
4a64f543
MS
4331/* Put in *NUM the breakpoint number of the first breakpoint we are
4332 stopped at. *BSP upon return is a bpstat which points to the
4333 remaining breakpoints stopped at (but which is not guaranteed to be
4334 good for anything but further calls to bpstat_num).
4335
8671a17b
PA
4336 Return 0 if passed a bpstat which does not indicate any breakpoints.
4337 Return -1 if stopped at a breakpoint that has been deleted since
4338 we set it.
4339 Return 1 otherwise. */
c906108c
SS
4340
4341int
8671a17b 4342bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4343{
4344 struct breakpoint *b;
4345
4346 if ((*bsp) == NULL)
4347 return 0; /* No more breakpoint values */
8671a17b 4348
4a64f543
MS
4349 /* We assume we'll never have several bpstats that correspond to a
4350 single breakpoint -- otherwise, this function might return the
4351 same number more than once and this will look ugly. */
f431efe5 4352 b = (*bsp)->breakpoint_at;
8671a17b
PA
4353 *bsp = (*bsp)->next;
4354 if (b == NULL)
4355 return -1; /* breakpoint that's been deleted since */
4356
4357 *num = b->number; /* We have its number */
4358 return 1;
c906108c
SS
4359}
4360
e93ca019 4361/* See breakpoint.h. */
c906108c
SS
4362
4363void
e93ca019 4364bpstat_clear_actions (void)
c906108c 4365{
e93ca019
JK
4366 struct thread_info *tp;
4367 bpstat bs;
4368
4369 if (ptid_equal (inferior_ptid, null_ptid))
4370 return;
4371
4372 tp = find_thread_ptid (inferior_ptid);
4373 if (tp == NULL)
4374 return;
4375
4376 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4377 {
d1b0a7bf 4378 bs->commands = NULL;
abf85f46 4379
c906108c
SS
4380 if (bs->old_val != NULL)
4381 {
4382 value_free (bs->old_val);
4383 bs->old_val = NULL;
4384 }
4385 }
4386}
4387
f3b1572e
PA
4388/* Called when a command is about to proceed the inferior. */
4389
4390static void
4391breakpoint_about_to_proceed (void)
4392{
4393 if (!ptid_equal (inferior_ptid, null_ptid))
4394 {
4395 struct thread_info *tp = inferior_thread ();
4396
4397 /* Allow inferior function calls in breakpoint commands to not
4398 interrupt the command list. When the call finishes
4399 successfully, the inferior will be standing at the same
4400 breakpoint as if nothing happened. */
16c381f0 4401 if (tp->control.in_infcall)
f3b1572e
PA
4402 return;
4403 }
4404
4405 breakpoint_proceeded = 1;
4406}
4407
abf85f46
JK
4408/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4409 or its equivalent. */
4410
4411static int
4412command_line_is_silent (struct command_line *cmd)
4413{
4f45d445 4414 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4415}
4416
4a64f543
MS
4417/* Execute all the commands associated with all the breakpoints at
4418 this location. Any of these commands could cause the process to
4419 proceed beyond this point, etc. We look out for such changes by
4420 checking the global "breakpoint_proceeded" after each command.
c906108c 4421
347bddb7
PA
4422 Returns true if a breakpoint command resumed the inferior. In that
4423 case, it is the caller's responsibility to recall it again with the
4424 bpstat of the current thread. */
4425
4426static int
4427bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4428{
4429 bpstat bs;
347bddb7 4430 int again = 0;
c906108c
SS
4431
4432 /* Avoid endless recursion if a `source' command is contained
4433 in bs->commands. */
4434 if (executing_breakpoint_commands)
347bddb7 4435 return 0;
c906108c 4436
81b1e71c
TT
4437 scoped_restore save_executing
4438 = make_scoped_restore (&executing_breakpoint_commands, 1);
c906108c 4439
1ac32117 4440 scoped_restore preventer = prevent_dont_repeat ();
cf6c5ffb 4441
4a64f543 4442 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4443 bs = *bsp;
4444
4445 breakpoint_proceeded = 0;
4446 for (; bs != NULL; bs = bs->next)
4447 {
d1b0a7bf 4448 struct command_line *cmd = NULL;
6c50ab1c
JB
4449
4450 /* Take ownership of the BSP's command tree, if it has one.
4451
4452 The command tree could legitimately contain commands like
4453 'step' and 'next', which call clear_proceed_status, which
4454 frees stop_bpstat's command tree. To make sure this doesn't
4455 free the tree we're executing out from under us, we need to
4456 take ownership of the tree ourselves. Since a given bpstat's
4457 commands are only executed once, we don't need to copy it; we
4458 can clear the pointer in the bpstat, and make sure we free
4459 the tree when we're done. */
d1b0a7bf 4460 counted_command_line ccmd = bs->commands;
9add0f1b 4461 bs->commands = NULL;
d1b0a7bf
TT
4462 if (ccmd != NULL)
4463 cmd = ccmd.get ();
abf85f46
JK
4464 if (command_line_is_silent (cmd))
4465 {
4466 /* The action has been already done by bpstat_stop_status. */
4467 cmd = cmd->next;
4468 }
6c50ab1c 4469
c906108c
SS
4470 while (cmd != NULL)
4471 {
4472 execute_control_command (cmd);
4473
4474 if (breakpoint_proceeded)
4475 break;
4476 else
4477 cmd = cmd->next;
4478 }
6c50ab1c 4479
c906108c 4480 if (breakpoint_proceeded)
32c1e744 4481 {
cb814510 4482 if (current_ui->async)
347bddb7
PA
4483 /* If we are in async mode, then the target might be still
4484 running, not stopped at any breakpoint, so nothing for
4485 us to do here -- just return to the event loop. */
4486 ;
32c1e744
VP
4487 else
4488 /* In sync mode, when execute_control_command returns
4489 we're already standing on the next breakpoint.
347bddb7
PA
4490 Breakpoint commands for that stop were not run, since
4491 execute_command does not run breakpoint commands --
4492 only command_line_handler does, but that one is not
4493 involved in execution of breakpoint commands. So, we
4494 can now execute breakpoint commands. It should be
4495 noted that making execute_command do bpstat actions is
4496 not an option -- in this case we'll have recursive
4497 invocation of bpstat for each breakpoint with a
4498 command, and can easily blow up GDB stack. Instead, we
4499 return true, which will trigger the caller to recall us
4500 with the new stop_bpstat. */
4501 again = 1;
4502 break;
32c1e744 4503 }
c906108c 4504 }
347bddb7
PA
4505 return again;
4506}
4507
4508void
4509bpstat_do_actions (void)
4510{
353d1d73
JK
4511 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4512
347bddb7
PA
4513 /* Do any commands attached to breakpoint we are stopped at. */
4514 while (!ptid_equal (inferior_ptid, null_ptid)
4515 && target_has_execution
4516 && !is_exited (inferior_ptid)
4517 && !is_executing (inferior_ptid))
4518 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4519 and only return when it is stopped at the next breakpoint, we
4520 keep doing breakpoint actions until it returns false to
4521 indicate the inferior was not resumed. */
16c381f0 4522 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4523 break;
353d1d73
JK
4524
4525 discard_cleanups (cleanup_if_error);
c906108c
SS
4526}
4527
fa4727a6
DJ
4528/* Print out the (old or new) value associated with a watchpoint. */
4529
4530static void
4531watchpoint_value_print (struct value *val, struct ui_file *stream)
4532{
4533 if (val == NULL)
4534 fprintf_unfiltered (stream, _("<unreadable>"));
4535 else
79a45b7d
TT
4536 {
4537 struct value_print_options opts;
4538 get_user_print_options (&opts);
4539 value_print (val, stream, &opts);
4540 }
fa4727a6
DJ
4541}
4542
f303dbd6
PA
4543/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4544 debugging multiple threads. */
4545
4546void
4547maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4548{
112e8700 4549 if (uiout->is_mi_like_p ())
f303dbd6
PA
4550 return;
4551
112e8700 4552 uiout->text ("\n");
f303dbd6
PA
4553
4554 if (show_thread_that_caused_stop ())
4555 {
4556 const char *name;
4557 struct thread_info *thr = inferior_thread ();
4558
112e8700
SM
4559 uiout->text ("Thread ");
4560 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
f303dbd6
PA
4561
4562 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4563 if (name != NULL)
4564 {
112e8700
SM
4565 uiout->text (" \"");
4566 uiout->field_fmt ("name", "%s", name);
4567 uiout->text ("\"");
f303dbd6
PA
4568 }
4569
112e8700 4570 uiout->text (" hit ");
f303dbd6
PA
4571 }
4572}
4573
e514a9d6 4574/* Generic routine for printing messages indicating why we
4a64f543 4575 stopped. The behavior of this function depends on the value
e514a9d6
JM
4576 'print_it' in the bpstat structure. Under some circumstances we
4577 may decide not to print anything here and delegate the task to
4a64f543 4578 normal_stop(). */
e514a9d6
JM
4579
4580static enum print_stop_action
4581print_bp_stop_message (bpstat bs)
4582{
4583 switch (bs->print_it)
4584 {
4585 case print_it_noop:
4a64f543 4586 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4587 return PRINT_UNKNOWN;
4588 break;
4589
4590 case print_it_done:
4591 /* We still want to print the frame, but we already printed the
4a64f543 4592 relevant messages. */
e514a9d6
JM
4593 return PRINT_SRC_AND_LOC;
4594 break;
4595
4596 case print_it_normal:
4f8d1dc6 4597 {
f431efe5
PA
4598 struct breakpoint *b = bs->breakpoint_at;
4599
1a6a67de
TJB
4600 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4601 which has since been deleted. */
4602 if (b == NULL)
4603 return PRINT_UNKNOWN;
4604
348d480f
PA
4605 /* Normal case. Call the breakpoint's print_it method. */
4606 return b->ops->print_it (bs);
4f8d1dc6 4607 }
348d480f 4608 break;
3086aeae 4609
e514a9d6 4610 default:
8e65ff28 4611 internal_error (__FILE__, __LINE__,
e2e0b3e5 4612 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4613 break;
c906108c 4614 }
c906108c
SS
4615}
4616
edcc5120
TT
4617/* A helper function that prints a shared library stopped event. */
4618
4619static void
4620print_solib_event (int is_catchpoint)
4621{
4622 int any_deleted
4623 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4624 int any_added
4625 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4626
4627 if (!is_catchpoint)
4628 {
4629 if (any_added || any_deleted)
112e8700 4630 current_uiout->text (_("Stopped due to shared library event:\n"));
edcc5120 4631 else
112e8700
SM
4632 current_uiout->text (_("Stopped due to shared library event (no "
4633 "libraries added or removed)\n"));
edcc5120
TT
4634 }
4635
112e8700
SM
4636 if (current_uiout->is_mi_like_p ())
4637 current_uiout->field_string ("reason",
4638 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
edcc5120
TT
4639
4640 if (any_deleted)
4641 {
edcc5120
TT
4642 char *name;
4643 int ix;
4644
112e8700 4645 current_uiout->text (_(" Inferior unloaded "));
10f489e5 4646 ui_out_emit_list list_emitter (current_uiout, "removed");
edcc5120
TT
4647 for (ix = 0;
4648 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4649 ix, name);
4650 ++ix)
4651 {
4652 if (ix > 0)
112e8700
SM
4653 current_uiout->text (" ");
4654 current_uiout->field_string ("library", name);
4655 current_uiout->text ("\n");
edcc5120 4656 }
edcc5120
TT
4657 }
4658
4659 if (any_added)
4660 {
4661 struct so_list *iter;
4662 int ix;
edcc5120 4663
112e8700 4664 current_uiout->text (_(" Inferior loaded "));
10f489e5 4665 ui_out_emit_list list_emitter (current_uiout, "added");
edcc5120
TT
4666 for (ix = 0;
4667 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4668 ix, iter);
4669 ++ix)
4670 {
4671 if (ix > 0)
112e8700
SM
4672 current_uiout->text (" ");
4673 current_uiout->field_string ("library", iter->so_name);
4674 current_uiout->text ("\n");
edcc5120 4675 }
edcc5120
TT
4676 }
4677}
4678
e514a9d6
JM
4679/* Print a message indicating what happened. This is called from
4680 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4681 list - a list of the eventpoints that caused this stop. KIND is
4682 the target_waitkind for the stopping event. This
e514a9d6
JM
4683 routine calls the generic print routine for printing a message
4684 about reasons for stopping. This will print (for example) the
4685 "Breakpoint n," part of the output. The return value of this
4686 routine is one of:
c906108c 4687
4a64f543 4688 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4689 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4690 code to print the location. An example is
c5aa993b
JM
4691 "Breakpoint 1, " which should be followed by
4692 the location.
917317f4 4693 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4694 to also print the location part of the message.
4695 An example is the catch/throw messages, which
4a64f543 4696 don't require a location appended to the end.
917317f4 4697 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4698 further info to be printed. */
c906108c 4699
917317f4 4700enum print_stop_action
36dfb11c 4701bpstat_print (bpstat bs, int kind)
c906108c 4702{
f486487f 4703 enum print_stop_action val;
c5aa993b 4704
c906108c 4705 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4706 (Currently all watchpoints go on the bpstat whether hit or not.
4707 That probably could (should) be changed, provided care is taken
c906108c 4708 with respect to bpstat_explains_signal). */
e514a9d6
JM
4709 for (; bs; bs = bs->next)
4710 {
4711 val = print_bp_stop_message (bs);
4712 if (val == PRINT_SRC_ONLY
4713 || val == PRINT_SRC_AND_LOC
4714 || val == PRINT_NOTHING)
4715 return val;
4716 }
c906108c 4717
36dfb11c
TT
4718 /* If we had hit a shared library event breakpoint,
4719 print_bp_stop_message would print out this message. If we hit an
4720 OS-level shared library event, do the same thing. */
4721 if (kind == TARGET_WAITKIND_LOADED)
4722 {
edcc5120 4723 print_solib_event (0);
36dfb11c
TT
4724 return PRINT_NOTHING;
4725 }
4726
e514a9d6 4727 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4728 with and nothing was printed. */
917317f4 4729 return PRINT_UNKNOWN;
c906108c
SS
4730}
4731
bf469271 4732/* Evaluate the boolean expression EXP and return the result. */
c906108c 4733
bf469271
PA
4734static bool
4735breakpoint_cond_eval (expression *exp)
c906108c 4736{
278cd55f 4737 struct value *mark = value_mark ();
bf469271 4738 bool res = value_true (evaluate_expression (exp));
cc59ec59 4739
c906108c 4740 value_free_to_mark (mark);
bf469271 4741 return res;
c906108c
SS
4742}
4743
5760d0ab 4744/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c 4745
04afa70c
TT
4746bpstats::bpstats (struct bp_location *bl, bpstat **bs_link_pointer)
4747 : next (NULL),
4748 bp_location_at (bl),
4749 breakpoint_at (bl->owner),
4750 commands (NULL),
4751 old_val (NULL),
4752 print (0),
4753 stop (0),
4754 print_it (print_it_normal)
c906108c 4755{
f431efe5 4756 incref_bp_location (bl);
04afa70c
TT
4757 **bs_link_pointer = this;
4758 *bs_link_pointer = &next;
4759}
4760
4761bpstats::bpstats ()
4762 : next (NULL),
4763 bp_location_at (NULL),
4764 breakpoint_at (NULL),
4765 commands (NULL),
4766 old_val (NULL),
4767 print (0),
4768 stop (0),
4769 print_it (print_it_normal)
4770{
c906108c
SS
4771}
4772\f
d983da9c
DJ
4773/* The target has stopped with waitstatus WS. Check if any hardware
4774 watchpoints have triggered, according to the target. */
4775
4776int
4777watchpoints_triggered (struct target_waitstatus *ws)
4778{
d92524f1 4779 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4780 CORE_ADDR addr;
4781 struct breakpoint *b;
4782
4783 if (!stopped_by_watchpoint)
4784 {
4785 /* We were not stopped by a watchpoint. Mark all watchpoints
4786 as not triggered. */
4787 ALL_BREAKPOINTS (b)
cc60f2e3 4788 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4789 {
4790 struct watchpoint *w = (struct watchpoint *) b;
4791
4792 w->watchpoint_triggered = watch_triggered_no;
4793 }
d983da9c
DJ
4794
4795 return 0;
4796 }
4797
4798 if (!target_stopped_data_address (&current_target, &addr))
4799 {
4800 /* We were stopped by a watchpoint, but we don't know where.
4801 Mark all watchpoints as unknown. */
4802 ALL_BREAKPOINTS (b)
cc60f2e3 4803 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4804 {
4805 struct watchpoint *w = (struct watchpoint *) b;
4806
4807 w->watchpoint_triggered = watch_triggered_unknown;
4808 }
d983da9c 4809
3c4797ba 4810 return 1;
d983da9c
DJ
4811 }
4812
4813 /* The target could report the data address. Mark watchpoints
4814 affected by this data address as triggered, and all others as not
4815 triggered. */
4816
4817 ALL_BREAKPOINTS (b)
cc60f2e3 4818 if (is_hardware_watchpoint (b))
d983da9c 4819 {
3a5c3e22 4820 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4821 struct bp_location *loc;
d983da9c 4822
3a5c3e22 4823 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4824 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4825 {
3a5c3e22 4826 if (is_masked_watchpoint (b))
9c06b0b4 4827 {
3a5c3e22
PA
4828 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4829 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4830
4831 if (newaddr == start)
4832 {
3a5c3e22 4833 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4834 break;
4835 }
4836 }
4837 /* Exact match not required. Within range is sufficient. */
4838 else if (target_watchpoint_addr_within_range (&current_target,
4839 addr, loc->address,
4840 loc->length))
4841 {
3a5c3e22 4842 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4843 break;
4844 }
4845 }
d983da9c
DJ
4846 }
4847
4848 return 1;
4849}
4850
bf469271
PA
4851/* Possible return values for watchpoint_check. */
4852enum wp_check_result
4853 {
4854 /* The watchpoint has been deleted. */
4855 WP_DELETED = 1,
4856
4857 /* The value has changed. */
4858 WP_VALUE_CHANGED = 2,
4859
4860 /* The value has not changed. */
4861 WP_VALUE_NOT_CHANGED = 3,
4862
4863 /* Ignore this watchpoint, no matter if the value changed or not. */
4864 WP_IGNORE = 4,
4865 };
c906108c
SS
4866
4867#define BP_TEMPFLAG 1
4868#define BP_HARDWAREFLAG 2
4869
4a64f543 4870/* Evaluate watchpoint condition expression and check if its value
bf469271 4871 changed. */
553e4c11 4872
bf469271
PA
4873static wp_check_result
4874watchpoint_check (bpstat bs)
c906108c 4875{
3a5c3e22 4876 struct watchpoint *b;
c906108c
SS
4877 struct frame_info *fr;
4878 int within_current_scope;
4879
f431efe5 4880 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4881 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4882 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4883
f6bc2008
PA
4884 /* If this is a local watchpoint, we only want to check if the
4885 watchpoint frame is in scope if the current thread is the thread
4886 that was used to create the watchpoint. */
4887 if (!watchpoint_in_thread_scope (b))
60e1c644 4888 return WP_IGNORE;
f6bc2008 4889
c906108c
SS
4890 if (b->exp_valid_block == NULL)
4891 within_current_scope = 1;
4892 else
4893 {
edb3359d
DJ
4894 struct frame_info *frame = get_current_frame ();
4895 struct gdbarch *frame_arch = get_frame_arch (frame);
4896 CORE_ADDR frame_pc = get_frame_pc (frame);
4897
c9cf6e20 4898 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
4899 still in the function but the stack frame has already been
4900 invalidated. Since we can't rely on the values of local
4901 variables after the stack has been destroyed, we are treating
4902 the watchpoint in that state as `not changed' without further
4903 checking. Don't mark watchpoints as changed if the current
4904 frame is in an epilogue - even if they are in some other
4905 frame, our view of the stack is likely to be wrong and
4906 frame_find_by_id could error out. */
c9cf6e20 4907 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 4908 return WP_IGNORE;
a0f49112 4909
101dcfbe 4910 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4911 within_current_scope = (fr != NULL);
69fbadd5
DJ
4912
4913 /* If we've gotten confused in the unwinder, we might have
4914 returned a frame that can't describe this variable. */
edb3359d
DJ
4915 if (within_current_scope)
4916 {
4917 struct symbol *function;
4918
4919 function = get_frame_function (fr);
4920 if (function == NULL
4921 || !contained_in (b->exp_valid_block,
4922 SYMBOL_BLOCK_VALUE (function)))
4923 within_current_scope = 0;
4924 }
69fbadd5 4925
edb3359d 4926 if (within_current_scope)
c906108c
SS
4927 /* If we end up stopping, the current frame will get selected
4928 in normal_stop. So this call to select_frame won't affect
4929 the user. */
0f7d239c 4930 select_frame (fr);
c906108c 4931 }
c5aa993b 4932
c906108c
SS
4933 if (within_current_scope)
4934 {
4a64f543
MS
4935 /* We use value_{,free_to_}mark because it could be a *long*
4936 time before we return to the command level and call
4937 free_all_values. We can't call free_all_values because we
4938 might be in the middle of evaluating a function call. */
c906108c 4939
0cf6dd15 4940 int pc = 0;
9c06b0b4 4941 struct value *mark;
fa4727a6
DJ
4942 struct value *new_val;
4943
c1fc2657 4944 if (is_masked_watchpoint (b))
9c06b0b4
TJB
4945 /* Since we don't know the exact trigger address (from
4946 stopped_data_address), just tell the user we've triggered
4947 a mask watchpoint. */
4948 return WP_VALUE_CHANGED;
4949
4950 mark = value_mark ();
4d01a485 4951 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
218d2fc6 4952
bb9d5f81
PP
4953 if (b->val_bitsize != 0)
4954 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
4955
4a64f543
MS
4956 /* We use value_equal_contents instead of value_equal because
4957 the latter coerces an array to a pointer, thus comparing just
4958 the address of the array instead of its contents. This is
4959 not what we want. */
fa4727a6 4960 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 4961 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 4962 {
fa4727a6
DJ
4963 if (new_val != NULL)
4964 {
4965 release_value (new_val);
4966 value_free_to_mark (mark);
4967 }
c906108c
SS
4968 bs->old_val = b->val;
4969 b->val = new_val;
fa4727a6 4970 b->val_valid = 1;
c906108c
SS
4971 return WP_VALUE_CHANGED;
4972 }
4973 else
4974 {
60e1c644 4975 /* Nothing changed. */
c906108c 4976 value_free_to_mark (mark);
c906108c
SS
4977 return WP_VALUE_NOT_CHANGED;
4978 }
4979 }
4980 else
4981 {
4982 /* This seems like the only logical thing to do because
c5aa993b
JM
4983 if we temporarily ignored the watchpoint, then when
4984 we reenter the block in which it is valid it contains
4985 garbage (in the case of a function, it may have two
4986 garbage values, one before and one after the prologue).
4987 So we can't even detect the first assignment to it and
4988 watch after that (since the garbage may or may not equal
4989 the first value assigned). */
348d480f
PA
4990 /* We print all the stop information in
4991 breakpoint_ops->print_it, but in this case, by the time we
4992 call breakpoint_ops->print_it this bp will be deleted
4993 already. So we have no choice but print the information
4994 here. */
468afe6c 4995
0e454242 4996 SWITCH_THRU_ALL_UIS ()
468afe6c
PA
4997 {
4998 struct ui_out *uiout = current_uiout;
4999
112e8700
SM
5000 if (uiout->is_mi_like_p ())
5001 uiout->field_string
5002 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5003 uiout->text ("\nWatchpoint ");
c1fc2657 5004 uiout->field_int ("wpnum", b->number);
112e8700 5005 uiout->text (" deleted because the program has left the block in\n"
468afe6c
PA
5006 "which its expression is valid.\n");
5007 }
4ce44c66 5008
cdac0397 5009 /* Make sure the watchpoint's commands aren't executed. */
d1b0a7bf 5010 b->commands = NULL;
d0fb5eae 5011 watchpoint_del_at_next_stop (b);
c906108c
SS
5012
5013 return WP_DELETED;
5014 }
5015}
5016
18a18393 5017/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5018 breakpoint location BL. This function does not check if we should
5019 stop, only if BL explains the stop. */
5020
18a18393 5021static int
6c95b8df 5022bpstat_check_location (const struct bp_location *bl,
accd0bcd 5023 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 5024 const struct target_waitstatus *ws)
18a18393
VP
5025{
5026 struct breakpoint *b = bl->owner;
5027
348d480f 5028 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5029 gdb_assert (b != NULL);
5030
bd522513 5031 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5032}
5033
3a5c3e22
PA
5034/* Determine if the watched values have actually changed, and we
5035 should stop. If not, set BS->stop to 0. */
5036
18a18393
VP
5037static void
5038bpstat_check_watchpoint (bpstat bs)
5039{
2bdf28a0 5040 const struct bp_location *bl;
3a5c3e22 5041 struct watchpoint *b;
2bdf28a0
JK
5042
5043 /* BS is built for existing struct breakpoint. */
f431efe5 5044 bl = bs->bp_location_at;
2bdf28a0 5045 gdb_assert (bl != NULL);
3a5c3e22 5046 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5047 gdb_assert (b != NULL);
18a18393 5048
18a18393 5049 {
18a18393
VP
5050 int must_check_value = 0;
5051
c1fc2657 5052 if (b->type == bp_watchpoint)
18a18393
VP
5053 /* For a software watchpoint, we must always check the
5054 watched value. */
5055 must_check_value = 1;
5056 else if (b->watchpoint_triggered == watch_triggered_yes)
5057 /* We have a hardware watchpoint (read, write, or access)
5058 and the target earlier reported an address watched by
5059 this watchpoint. */
5060 must_check_value = 1;
5061 else if (b->watchpoint_triggered == watch_triggered_unknown
c1fc2657 5062 && b->type == bp_hardware_watchpoint)
18a18393
VP
5063 /* We were stopped by a hardware watchpoint, but the target could
5064 not report the data address. We must check the watchpoint's
5065 value. Access and read watchpoints are out of luck; without
5066 a data address, we can't figure it out. */
5067 must_check_value = 1;
3a5c3e22 5068
18a18393
VP
5069 if (must_check_value)
5070 {
bf469271
PA
5071 wp_check_result e;
5072
5073 TRY
5074 {
5075 e = watchpoint_check (bs);
5076 }
5077 CATCH (ex, RETURN_MASK_ALL)
5078 {
5079 exception_fprintf (gdb_stderr, ex,
5080 "Error evaluating expression "
5081 "for watchpoint %d\n",
5082 b->number);
5083
5084 SWITCH_THRU_ALL_UIS ()
5085 {
5086 printf_filtered (_("Watchpoint %d deleted.\n"),
5087 b->number);
5088 }
5089 watchpoint_del_at_next_stop (b);
5090 e = WP_DELETED;
5091 }
5092 END_CATCH
5093
18a18393
VP
5094 switch (e)
5095 {
5096 case WP_DELETED:
5097 /* We've already printed what needs to be printed. */
5098 bs->print_it = print_it_done;
5099 /* Stop. */
5100 break;
60e1c644
PA
5101 case WP_IGNORE:
5102 bs->print_it = print_it_noop;
5103 bs->stop = 0;
5104 break;
18a18393 5105 case WP_VALUE_CHANGED:
c1fc2657 5106 if (b->type == bp_read_watchpoint)
18a18393 5107 {
85d721b8
PA
5108 /* There are two cases to consider here:
5109
4a64f543 5110 1. We're watching the triggered memory for reads.
85d721b8
PA
5111 In that case, trust the target, and always report
5112 the watchpoint hit to the user. Even though
5113 reads don't cause value changes, the value may
5114 have changed since the last time it was read, and
5115 since we're not trapping writes, we will not see
5116 those, and as such we should ignore our notion of
5117 old value.
5118
4a64f543 5119 2. We're watching the triggered memory for both
85d721b8
PA
5120 reads and writes. There are two ways this may
5121 happen:
5122
4a64f543 5123 2.1. This is a target that can't break on data
85d721b8
PA
5124 reads only, but can break on accesses (reads or
5125 writes), such as e.g., x86. We detect this case
5126 at the time we try to insert read watchpoints.
5127
4a64f543 5128 2.2. Otherwise, the target supports read
85d721b8
PA
5129 watchpoints, but, the user set an access or write
5130 watchpoint watching the same memory as this read
5131 watchpoint.
5132
5133 If we're watching memory writes as well as reads,
5134 ignore watchpoint hits when we find that the
5135 value hasn't changed, as reads don't cause
5136 changes. This still gives false positives when
5137 the program writes the same value to memory as
5138 what there was already in memory (we will confuse
5139 it for a read), but it's much better than
5140 nothing. */
5141
5142 int other_write_watchpoint = 0;
5143
5144 if (bl->watchpoint_type == hw_read)
5145 {
5146 struct breakpoint *other_b;
5147
5148 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5149 if (other_b->type == bp_hardware_watchpoint
5150 || other_b->type == bp_access_watchpoint)
85d721b8 5151 {
3a5c3e22
PA
5152 struct watchpoint *other_w =
5153 (struct watchpoint *) other_b;
5154
5155 if (other_w->watchpoint_triggered
5156 == watch_triggered_yes)
5157 {
5158 other_write_watchpoint = 1;
5159 break;
5160 }
85d721b8
PA
5161 }
5162 }
5163
5164 if (other_write_watchpoint
5165 || bl->watchpoint_type == hw_access)
5166 {
5167 /* We're watching the same memory for writes,
5168 and the value changed since the last time we
5169 updated it, so this trap must be for a write.
5170 Ignore it. */
5171 bs->print_it = print_it_noop;
5172 bs->stop = 0;
5173 }
18a18393
VP
5174 }
5175 break;
5176 case WP_VALUE_NOT_CHANGED:
c1fc2657
SM
5177 if (b->type == bp_hardware_watchpoint
5178 || b->type == bp_watchpoint)
18a18393
VP
5179 {
5180 /* Don't stop: write watchpoints shouldn't fire if
5181 the value hasn't changed. */
5182 bs->print_it = print_it_noop;
5183 bs->stop = 0;
5184 }
5185 /* Stop. */
5186 break;
5187 default:
5188 /* Can't happen. */
18a18393
VP
5189 break;
5190 }
5191 }
5192 else /* must_check_value == 0 */
5193 {
5194 /* This is a case where some watchpoint(s) triggered, but
5195 not at the address of this watchpoint, or else no
5196 watchpoint triggered after all. So don't print
5197 anything for this watchpoint. */
5198 bs->print_it = print_it_noop;
5199 bs->stop = 0;
5200 }
5201 }
5202}
5203
7d4df6a4
DE
5204/* For breakpoints that are currently marked as telling gdb to stop,
5205 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5206 of breakpoint referred to by BS. If we should not stop for this
5207 breakpoint, set BS->stop to 0. */
f431efe5 5208
18a18393
VP
5209static void
5210bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5211{
2bdf28a0
JK
5212 const struct bp_location *bl;
5213 struct breakpoint *b;
bf469271
PA
5214 /* Assume stop. */
5215 bool condition_result = true;
7d4df6a4
DE
5216 struct expression *cond;
5217
5218 gdb_assert (bs->stop);
2bdf28a0
JK
5219
5220 /* BS is built for existing struct breakpoint. */
f431efe5 5221 bl = bs->bp_location_at;
2bdf28a0 5222 gdb_assert (bl != NULL);
f431efe5 5223 b = bs->breakpoint_at;
2bdf28a0 5224 gdb_assert (b != NULL);
18a18393 5225
b775012e
LM
5226 /* Even if the target evaluated the condition on its end and notified GDB, we
5227 need to do so again since GDB does not know if we stopped due to a
5228 breakpoint or a single step breakpoint. */
5229
18a18393 5230 if (frame_id_p (b->frame_id)
edb3359d 5231 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5232 {
7d4df6a4
DE
5233 bs->stop = 0;
5234 return;
5235 }
60e1c644 5236
12ab52e9
PA
5237 /* If this is a thread/task-specific breakpoint, don't waste cpu
5238 evaluating the condition if this isn't the specified
5239 thread/task. */
5d5658a1 5240 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5241 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5242
6c1b0f7b
DE
5243 {
5244 bs->stop = 0;
5245 return;
5246 }
5247
6dddc817
DE
5248 /* Evaluate extension language breakpoints that have a "stop" method
5249 implemented. */
5250 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5251
7d4df6a4
DE
5252 if (is_watchpoint (b))
5253 {
5254 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5255
4d01a485 5256 cond = w->cond_exp.get ();
7d4df6a4
DE
5257 }
5258 else
4d01a485 5259 cond = bl->cond.get ();
60e1c644 5260
7d4df6a4
DE
5261 if (cond && b->disposition != disp_del_at_next_stop)
5262 {
5263 int within_current_scope = 1;
5264 struct watchpoint * w;
60e1c644 5265
7d4df6a4
DE
5266 /* We use value_mark and value_free_to_mark because it could
5267 be a long time before we return to the command level and
5268 call free_all_values. We can't call free_all_values
5269 because we might be in the middle of evaluating a
5270 function call. */
5271 struct value *mark = value_mark ();
5272
5273 if (is_watchpoint (b))
5274 w = (struct watchpoint *) b;
5275 else
5276 w = NULL;
5277
5278 /* Need to select the frame, with all that implies so that
5279 the conditions will have the right context. Because we
5280 use the frame, we will not see an inlined function's
5281 variables when we arrive at a breakpoint at the start
5282 of the inlined function; the current frame will be the
5283 call site. */
5284 if (w == NULL || w->cond_exp_valid_block == NULL)
5285 select_frame (get_current_frame ());
5286 else
18a18393 5287 {
7d4df6a4
DE
5288 struct frame_info *frame;
5289
5290 /* For local watchpoint expressions, which particular
5291 instance of a local is being watched matters, so we
5292 keep track of the frame to evaluate the expression
5293 in. To evaluate the condition however, it doesn't
5294 really matter which instantiation of the function
5295 where the condition makes sense triggers the
5296 watchpoint. This allows an expression like "watch
5297 global if q > 10" set in `func', catch writes to
5298 global on all threads that call `func', or catch
5299 writes on all recursive calls of `func' by a single
5300 thread. We simply always evaluate the condition in
5301 the innermost frame that's executing where it makes
5302 sense to evaluate the condition. It seems
5303 intuitive. */
5304 frame = block_innermost_frame (w->cond_exp_valid_block);
5305 if (frame != NULL)
5306 select_frame (frame);
5307 else
5308 within_current_scope = 0;
18a18393 5309 }
7d4df6a4 5310 if (within_current_scope)
bf469271
PA
5311 {
5312 TRY
5313 {
5314 condition_result = breakpoint_cond_eval (cond);
5315 }
5316 CATCH (ex, RETURN_MASK_ALL)
5317 {
5318 exception_fprintf (gdb_stderr, ex,
5319 "Error in testing breakpoint condition:\n");
5320 }
5321 END_CATCH
5322 }
7d4df6a4 5323 else
18a18393 5324 {
7d4df6a4
DE
5325 warning (_("Watchpoint condition cannot be tested "
5326 "in the current scope"));
5327 /* If we failed to set the right context for this
5328 watchpoint, unconditionally report it. */
18a18393 5329 }
7d4df6a4
DE
5330 /* FIXME-someday, should give breakpoint #. */
5331 value_free_to_mark (mark);
18a18393 5332 }
7d4df6a4 5333
bf469271 5334 if (cond && !condition_result)
7d4df6a4
DE
5335 {
5336 bs->stop = 0;
5337 }
7d4df6a4
DE
5338 else if (b->ignore_count > 0)
5339 {
5340 b->ignore_count--;
5341 bs->stop = 0;
5342 /* Increase the hit count even though we don't stop. */
5343 ++(b->hit_count);
5344 observer_notify_breakpoint_modified (b);
5345 }
18a18393
VP
5346}
5347
1cf4d951
PA
5348/* Returns true if we need to track moribund locations of LOC's type
5349 on the current target. */
5350
5351static int
5352need_moribund_for_location_type (struct bp_location *loc)
5353{
5354 return ((loc->loc_type == bp_loc_software_breakpoint
5355 && !target_supports_stopped_by_sw_breakpoint ())
5356 || (loc->loc_type == bp_loc_hardware_breakpoint
5357 && !target_supports_stopped_by_hw_breakpoint ()));
5358}
5359
18a18393 5360
9709f61c 5361/* Get a bpstat associated with having just stopped at address
d983da9c 5362 BP_ADDR in thread PTID.
c906108c 5363
d983da9c 5364 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5365 don't understand this stop. Result is a chain of bpstat's such
5366 that:
c906108c 5367
c5aa993b 5368 if we don't understand the stop, the result is a null pointer.
c906108c 5369
c5aa993b 5370 if we understand why we stopped, the result is not null.
c906108c 5371
c5aa993b
JM
5372 Each element of the chain refers to a particular breakpoint or
5373 watchpoint at which we have stopped. (We may have stopped for
5374 several reasons concurrently.)
c906108c 5375
c5aa993b
JM
5376 Each element of the chain has valid next, breakpoint_at,
5377 commands, FIXME??? fields. */
c906108c
SS
5378
5379bpstat
accd0bcd 5380bpstat_stop_status (const address_space *aspace,
09ac7c10
TT
5381 CORE_ADDR bp_addr, ptid_t ptid,
5382 const struct target_waitstatus *ws)
c906108c 5383{
0d381245 5384 struct breakpoint *b = NULL;
afe38095 5385 struct bp_location *bl;
20874c92 5386 struct bp_location *loc;
5760d0ab
JK
5387 /* First item of allocated bpstat's. */
5388 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5389 /* Pointer to the last thing in the chain currently. */
5760d0ab 5390 bpstat bs;
20874c92 5391 int ix;
429374b8 5392 int need_remove_insert;
f431efe5 5393 int removed_any;
c906108c 5394
f431efe5
PA
5395 /* First, build the bpstat chain with locations that explain a
5396 target stop, while being careful to not set the target running,
5397 as that may invalidate locations (in particular watchpoint
5398 locations are recreated). Resuming will happen here with
5399 breakpoint conditions or watchpoint expressions that include
5400 inferior function calls. */
c5aa993b 5401
429374b8
JK
5402 ALL_BREAKPOINTS (b)
5403 {
1a853c52 5404 if (!breakpoint_enabled (b))
429374b8 5405 continue;
a5606eee 5406
429374b8
JK
5407 for (bl = b->loc; bl != NULL; bl = bl->next)
5408 {
4a64f543
MS
5409 /* For hardware watchpoints, we look only at the first
5410 location. The watchpoint_check function will work on the
5411 entire expression, not the individual locations. For
5412 read watchpoints, the watchpoints_triggered function has
5413 checked all locations already. */
429374b8
JK
5414 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5415 break;
18a18393 5416
f6592439 5417 if (!bl->enabled || bl->shlib_disabled)
429374b8 5418 continue;
c5aa993b 5419
09ac7c10 5420 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5421 continue;
c5aa993b 5422
4a64f543
MS
5423 /* Come here if it's a watchpoint, or if the break address
5424 matches. */
c5aa993b 5425
04afa70c 5426 bs = new bpstats (bl, &bs_link); /* Alloc a bpstat to
4a64f543 5427 explain stop. */
c5aa993b 5428
f431efe5
PA
5429 /* Assume we stop. Should we find a watchpoint that is not
5430 actually triggered, or if the condition of the breakpoint
5431 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5432 bs->stop = 1;
5433 bs->print = 1;
d983da9c 5434
f431efe5
PA
5435 /* If this is a scope breakpoint, mark the associated
5436 watchpoint as triggered so that we will handle the
5437 out-of-scope event. We'll get to the watchpoint next
5438 iteration. */
d0fb5eae 5439 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5440 {
5441 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5442
5443 w->watchpoint_triggered = watch_triggered_yes;
5444 }
f431efe5
PA
5445 }
5446 }
5447
7c16b83e 5448 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5449 if (!target_supports_stopped_by_sw_breakpoint ()
5450 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5451 {
1cf4d951 5452 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5453 {
1cf4d951
PA
5454 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5455 && need_moribund_for_location_type (loc))
5456 {
04afa70c 5457 bs = new bpstats (loc, &bs_link);
1cf4d951
PA
5458 /* For hits of moribund locations, we should just proceed. */
5459 bs->stop = 0;
5460 bs->print = 0;
5461 bs->print_it = print_it_noop;
5462 }
f431efe5
PA
5463 }
5464 }
5465
edcc5120
TT
5466 /* A bit of special processing for shlib breakpoints. We need to
5467 process solib loading here, so that the lists of loaded and
5468 unloaded libraries are correct before we handle "catch load" and
5469 "catch unload". */
5470 for (bs = bs_head; bs != NULL; bs = bs->next)
5471 {
5d268276 5472 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5473 {
5474 handle_solib_event ();
5475 break;
5476 }
5477 }
5478
f431efe5
PA
5479 /* Now go through the locations that caused the target to stop, and
5480 check whether we're interested in reporting this stop to higher
5481 layers, or whether we should resume the target transparently. */
5482
5483 removed_any = 0;
5484
5760d0ab 5485 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5486 {
5487 if (!bs->stop)
5488 continue;
5489
f431efe5 5490 b = bs->breakpoint_at;
348d480f
PA
5491 b->ops->check_status (bs);
5492 if (bs->stop)
28010a5d 5493 {
348d480f 5494 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5495
429374b8
JK
5496 if (bs->stop)
5497 {
5498 ++(b->hit_count);
8d3788bd 5499 observer_notify_breakpoint_modified (b);
c906108c 5500
4a64f543 5501 /* We will stop here. */
429374b8
JK
5502 if (b->disposition == disp_disable)
5503 {
816338b5 5504 --(b->enable_count);
1a853c52 5505 if (b->enable_count <= 0)
429374b8 5506 b->enable_state = bp_disabled;
f431efe5 5507 removed_any = 1;
429374b8
JK
5508 }
5509 if (b->silent)
5510 bs->print = 0;
5511 bs->commands = b->commands;
abf85f46 5512 if (command_line_is_silent (bs->commands
d1b0a7bf 5513 ? bs->commands.get () : NULL))
abf85f46 5514 bs->print = 0;
9d6e6e84
HZ
5515
5516 b->ops->after_condition_true (bs);
429374b8
JK
5517 }
5518
348d480f 5519 }
a9b3a50f
PA
5520
5521 /* Print nothing for this entry if we don't stop or don't
5522 print. */
5523 if (!bs->stop || !bs->print)
5524 bs->print_it = print_it_noop;
429374b8 5525 }
876fa593 5526
d983da9c
DJ
5527 /* If we aren't stopping, the value of some hardware watchpoint may
5528 not have changed, but the intermediate memory locations we are
5529 watching may have. Don't bother if we're stopping; this will get
5530 done later. */
d832cb68 5531 need_remove_insert = 0;
5760d0ab
JK
5532 if (! bpstat_causes_stop (bs_head))
5533 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5534 if (!bs->stop
f431efe5
PA
5535 && bs->breakpoint_at
5536 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5537 {
3a5c3e22
PA
5538 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5539
5540 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5541 need_remove_insert = 1;
d983da9c
DJ
5542 }
5543
d832cb68 5544 if (need_remove_insert)
44702360 5545 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5546 else if (removed_any)
44702360 5547 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5548
5760d0ab 5549 return bs_head;
c906108c 5550}
628fe4e4
JK
5551
5552static void
5553handle_jit_event (void)
5554{
5555 struct frame_info *frame;
5556 struct gdbarch *gdbarch;
5557
243a9253
PA
5558 if (debug_infrun)
5559 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5560
628fe4e4
JK
5561 /* Switch terminal for any messages produced by
5562 breakpoint_re_set. */
223ffa71 5563 target_terminal::ours_for_output ();
628fe4e4
JK
5564
5565 frame = get_current_frame ();
5566 gdbarch = get_frame_arch (frame);
5567
5568 jit_event_handler (gdbarch);
5569
223ffa71 5570 target_terminal::inferior ();
628fe4e4
JK
5571}
5572
5573/* Prepare WHAT final decision for infrun. */
5574
5575/* Decide what infrun needs to do with this bpstat. */
5576
c906108c 5577struct bpstat_what
0e30163f 5578bpstat_what (bpstat bs_head)
c906108c 5579{
c906108c 5580 struct bpstat_what retval;
0e30163f 5581 bpstat bs;
c906108c 5582
628fe4e4 5583 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5584 retval.call_dummy = STOP_NONE;
186c406b 5585 retval.is_longjmp = 0;
628fe4e4 5586
0e30163f 5587 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5588 {
628fe4e4
JK
5589 /* Extract this BS's action. After processing each BS, we check
5590 if its action overrides all we've seem so far. */
5591 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5592 enum bptype bptype;
5593
c906108c 5594 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5595 {
5596 /* I suspect this can happen if it was a momentary
5597 breakpoint which has since been deleted. */
5598 bptype = bp_none;
5599 }
20874c92 5600 else
f431efe5 5601 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5602
5603 switch (bptype)
c906108c
SS
5604 {
5605 case bp_none:
628fe4e4 5606 break;
c906108c
SS
5607 case bp_breakpoint:
5608 case bp_hardware_breakpoint:
7c16b83e 5609 case bp_single_step:
c906108c
SS
5610 case bp_until:
5611 case bp_finish:
a9b3a50f 5612 case bp_shlib_event:
c906108c
SS
5613 if (bs->stop)
5614 {
5615 if (bs->print)
628fe4e4 5616 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5617 else
628fe4e4 5618 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5619 }
5620 else
628fe4e4 5621 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5622 break;
5623 case bp_watchpoint:
5624 case bp_hardware_watchpoint:
5625 case bp_read_watchpoint:
5626 case bp_access_watchpoint:
5627 if (bs->stop)
5628 {
5629 if (bs->print)
628fe4e4 5630 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5631 else
628fe4e4 5632 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5633 }
5634 else
628fe4e4
JK
5635 {
5636 /* There was a watchpoint, but we're not stopping.
5637 This requires no further action. */
5638 }
c906108c
SS
5639 break;
5640 case bp_longjmp:
e2e4d78b 5641 case bp_longjmp_call_dummy:
186c406b 5642 case bp_exception:
0a39bb32
PA
5643 if (bs->stop)
5644 {
5645 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5646 retval.is_longjmp = bptype != bp_exception;
5647 }
5648 else
5649 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5650 break;
5651 case bp_longjmp_resume:
186c406b 5652 case bp_exception_resume:
0a39bb32
PA
5653 if (bs->stop)
5654 {
5655 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5656 retval.is_longjmp = bptype == bp_longjmp_resume;
5657 }
5658 else
5659 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5660 break;
5661 case bp_step_resume:
5662 if (bs->stop)
628fe4e4
JK
5663 this_action = BPSTAT_WHAT_STEP_RESUME;
5664 else
c906108c 5665 {
628fe4e4
JK
5666 /* It is for the wrong frame. */
5667 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5668 }
c906108c 5669 break;
2c03e5be
PA
5670 case bp_hp_step_resume:
5671 if (bs->stop)
5672 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5673 else
5674 {
5675 /* It is for the wrong frame. */
5676 this_action = BPSTAT_WHAT_SINGLE;
5677 }
5678 break;
c906108c 5679 case bp_watchpoint_scope:
c4093a6a 5680 case bp_thread_event:
1900040c 5681 case bp_overlay_event:
0fd8e87f 5682 case bp_longjmp_master:
aa7d318d 5683 case bp_std_terminate_master:
186c406b 5684 case bp_exception_master:
628fe4e4 5685 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5686 break;
ce78b96d 5687 case bp_catchpoint:
c5aa993b
JM
5688 if (bs->stop)
5689 {
5690 if (bs->print)
628fe4e4 5691 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5692 else
628fe4e4 5693 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5694 }
5695 else
628fe4e4
JK
5696 {
5697 /* There was a catchpoint, but we're not stopping.
5698 This requires no further action. */
5699 }
5700 break;
628fe4e4 5701 case bp_jit_event:
628fe4e4 5702 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5703 break;
c906108c 5704 case bp_call_dummy:
53a5351d
JM
5705 /* Make sure the action is stop (silent or noisy),
5706 so infrun.c pops the dummy frame. */
aa7d318d 5707 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5708 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5709 break;
5710 case bp_std_terminate:
5711 /* Make sure the action is stop (silent or noisy),
5712 so infrun.c pops the dummy frame. */
aa7d318d 5713 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5714 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5715 break;
1042e4c0 5716 case bp_tracepoint:
7a697b8d 5717 case bp_fast_tracepoint:
0fb4aa4b 5718 case bp_static_tracepoint:
1042e4c0
SS
5719 /* Tracepoint hits should not be reported back to GDB, and
5720 if one got through somehow, it should have been filtered
5721 out already. */
5722 internal_error (__FILE__, __LINE__,
7a697b8d 5723 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5724 break;
5725 case bp_gnu_ifunc_resolver:
5726 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5727 this_action = BPSTAT_WHAT_SINGLE;
5728 break;
5729 case bp_gnu_ifunc_resolver_return:
5730 /* The breakpoint will be removed, execution will restart from the
5731 PC of the former breakpoint. */
5732 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5733 break;
e7e0cddf
SS
5734
5735 case bp_dprintf:
a11cfd87
HZ
5736 if (bs->stop)
5737 this_action = BPSTAT_WHAT_STOP_SILENT;
5738 else
5739 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5740 break;
5741
628fe4e4
JK
5742 default:
5743 internal_error (__FILE__, __LINE__,
5744 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5745 }
628fe4e4 5746
325fac50 5747 retval.main_action = std::max (retval.main_action, this_action);
c906108c 5748 }
628fe4e4 5749
243a9253
PA
5750 return retval;
5751}
628fe4e4 5752
243a9253
PA
5753void
5754bpstat_run_callbacks (bpstat bs_head)
5755{
5756 bpstat bs;
628fe4e4 5757
0e30163f
JK
5758 for (bs = bs_head; bs != NULL; bs = bs->next)
5759 {
5760 struct breakpoint *b = bs->breakpoint_at;
5761
5762 if (b == NULL)
5763 continue;
5764 switch (b->type)
5765 {
243a9253
PA
5766 case bp_jit_event:
5767 handle_jit_event ();
5768 break;
0e30163f
JK
5769 case bp_gnu_ifunc_resolver:
5770 gnu_ifunc_resolver_stop (b);
5771 break;
5772 case bp_gnu_ifunc_resolver_return:
5773 gnu_ifunc_resolver_return_stop (b);
5774 break;
5775 }
5776 }
c906108c
SS
5777}
5778
5779/* Nonzero if we should step constantly (e.g. watchpoints on machines
5780 without hardware support). This isn't related to a specific bpstat,
5781 just to things like whether watchpoints are set. */
5782
c5aa993b 5783int
fba45db2 5784bpstat_should_step (void)
c906108c
SS
5785{
5786 struct breakpoint *b;
cc59ec59 5787
c906108c 5788 ALL_BREAKPOINTS (b)
717a8278 5789 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5790 return 1;
c906108c
SS
5791 return 0;
5792}
5793
67822962
PA
5794int
5795bpstat_causes_stop (bpstat bs)
5796{
5797 for (; bs != NULL; bs = bs->next)
5798 if (bs->stop)
5799 return 1;
5800
5801 return 0;
5802}
5803
c906108c 5804\f
c5aa993b 5805
170b53b2
UW
5806/* Compute a string of spaces suitable to indent the next line
5807 so it starts at the position corresponding to the table column
5808 named COL_NAME in the currently active table of UIOUT. */
5809
5810static char *
5811wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5812{
5813 static char wrap_indent[80];
5814 int i, total_width, width, align;
c5209615 5815 const char *text;
170b53b2
UW
5816
5817 total_width = 0;
112e8700 5818 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
170b53b2
UW
5819 {
5820 if (strcmp (text, col_name) == 0)
5821 {
5822 gdb_assert (total_width < sizeof wrap_indent);
5823 memset (wrap_indent, ' ', total_width);
5824 wrap_indent[total_width] = 0;
5825
5826 return wrap_indent;
5827 }
5828
5829 total_width += width + 1;
5830 }
5831
5832 return NULL;
5833}
5834
b775012e
LM
5835/* Determine if the locations of this breakpoint will have their conditions
5836 evaluated by the target, host or a mix of both. Returns the following:
5837
5838 "host": Host evals condition.
5839 "host or target": Host or Target evals condition.
5840 "target": Target evals condition.
5841*/
5842
5843static const char *
5844bp_condition_evaluator (struct breakpoint *b)
5845{
5846 struct bp_location *bl;
5847 char host_evals = 0;
5848 char target_evals = 0;
5849
5850 if (!b)
5851 return NULL;
5852
5853 if (!is_breakpoint (b))
5854 return NULL;
5855
5856 if (gdb_evaluates_breakpoint_condition_p ()
5857 || !target_supports_evaluation_of_breakpoint_conditions ())
5858 return condition_evaluation_host;
5859
5860 for (bl = b->loc; bl; bl = bl->next)
5861 {
5862 if (bl->cond_bytecode)
5863 target_evals++;
5864 else
5865 host_evals++;
5866 }
5867
5868 if (host_evals && target_evals)
5869 return condition_evaluation_both;
5870 else if (target_evals)
5871 return condition_evaluation_target;
5872 else
5873 return condition_evaluation_host;
5874}
5875
5876/* Determine the breakpoint location's condition evaluator. This is
5877 similar to bp_condition_evaluator, but for locations. */
5878
5879static const char *
5880bp_location_condition_evaluator (struct bp_location *bl)
5881{
5882 if (bl && !is_breakpoint (bl->owner))
5883 return NULL;
5884
5885 if (gdb_evaluates_breakpoint_condition_p ()
5886 || !target_supports_evaluation_of_breakpoint_conditions ())
5887 return condition_evaluation_host;
5888
5889 if (bl && bl->cond_bytecode)
5890 return condition_evaluation_target;
5891 else
5892 return condition_evaluation_host;
5893}
5894
859825b8
JK
5895/* Print the LOC location out of the list of B->LOC locations. */
5896
170b53b2
UW
5897static void
5898print_breakpoint_location (struct breakpoint *b,
5899 struct bp_location *loc)
0d381245 5900{
79a45e25 5901 struct ui_out *uiout = current_uiout;
5ed8105e
PA
5902
5903 scoped_restore_current_program_space restore_pspace;
6c95b8df 5904
859825b8
JK
5905 if (loc != NULL && loc->shlib_disabled)
5906 loc = NULL;
5907
6c95b8df
PA
5908 if (loc != NULL)
5909 set_current_program_space (loc->pspace);
5910
56435ebe 5911 if (b->display_canonical)
d28cd78a 5912 uiout->field_string ("what", event_location_to_string (b->location.get ()));
2f202fde 5913 else if (loc && loc->symtab)
0d381245 5914 {
4a27f119
KS
5915 const struct symbol *sym = loc->symbol;
5916
5917 if (sym == NULL)
5918 sym = find_pc_sect_function (loc->address, loc->section);
5919
0d381245
VP
5920 if (sym)
5921 {
112e8700
SM
5922 uiout->text ("in ");
5923 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
5924 uiout->text (" ");
5925 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
5926 uiout->text ("at ");
0d381245 5927 }
112e8700 5928 uiout->field_string ("file",
05cba821 5929 symtab_to_filename_for_display (loc->symtab));
112e8700 5930 uiout->text (":");
05cba821 5931
112e8700
SM
5932 if (uiout->is_mi_like_p ())
5933 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
0d381245 5934
112e8700 5935 uiout->field_int ("line", loc->line_number);
0d381245 5936 }
859825b8 5937 else if (loc)
0d381245 5938 {
d7e74731 5939 string_file stb;
170b53b2 5940
d7e74731 5941 print_address_symbolic (loc->gdbarch, loc->address, &stb,
22e722e1 5942 demangle, "");
112e8700 5943 uiout->field_stream ("at", stb);
0d381245 5944 }
859825b8 5945 else
f00aae0f 5946 {
d28cd78a
TT
5947 uiout->field_string ("pending",
5948 event_location_to_string (b->location.get ()));
f00aae0f
KS
5949 /* If extra_string is available, it could be holding a condition
5950 or dprintf arguments. In either case, make sure it is printed,
5951 too, but only for non-MI streams. */
112e8700 5952 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
f00aae0f
KS
5953 {
5954 if (b->type == bp_dprintf)
112e8700 5955 uiout->text (",");
f00aae0f 5956 else
112e8700
SM
5957 uiout->text (" ");
5958 uiout->text (b->extra_string);
f00aae0f
KS
5959 }
5960 }
6c95b8df 5961
b775012e
LM
5962 if (loc && is_breakpoint (b)
5963 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5964 && bp_condition_evaluator (b) == condition_evaluation_both)
5965 {
112e8700
SM
5966 uiout->text (" (");
5967 uiout->field_string ("evaluated-by",
b775012e 5968 bp_location_condition_evaluator (loc));
112e8700 5969 uiout->text (")");
b775012e 5970 }
0d381245
VP
5971}
5972
269b11a2
PA
5973static const char *
5974bptype_string (enum bptype type)
c906108c 5975{
c4093a6a
JM
5976 struct ep_type_description
5977 {
5978 enum bptype type;
a121b7c1 5979 const char *description;
c4093a6a
JM
5980 };
5981 static struct ep_type_description bptypes[] =
c906108c 5982 {
c5aa993b
JM
5983 {bp_none, "?deleted?"},
5984 {bp_breakpoint, "breakpoint"},
c906108c 5985 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 5986 {bp_single_step, "sw single-step"},
c5aa993b
JM
5987 {bp_until, "until"},
5988 {bp_finish, "finish"},
5989 {bp_watchpoint, "watchpoint"},
c906108c 5990 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5991 {bp_read_watchpoint, "read watchpoint"},
5992 {bp_access_watchpoint, "acc watchpoint"},
5993 {bp_longjmp, "longjmp"},
5994 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5995 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5996 {bp_exception, "exception"},
5997 {bp_exception_resume, "exception resume"},
c5aa993b 5998 {bp_step_resume, "step resume"},
2c03e5be 5999 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6000 {bp_watchpoint_scope, "watchpoint scope"},
6001 {bp_call_dummy, "call dummy"},
aa7d318d 6002 {bp_std_terminate, "std::terminate"},
c5aa993b 6003 {bp_shlib_event, "shlib events"},
c4093a6a 6004 {bp_thread_event, "thread events"},
1900040c 6005 {bp_overlay_event, "overlay events"},
0fd8e87f 6006 {bp_longjmp_master, "longjmp master"},
aa7d318d 6007 {bp_std_terminate_master, "std::terminate master"},
186c406b 6008 {bp_exception_master, "exception master"},
ce78b96d 6009 {bp_catchpoint, "catchpoint"},
1042e4c0 6010 {bp_tracepoint, "tracepoint"},
7a697b8d 6011 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6012 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6013 {bp_dprintf, "dprintf"},
4efc6507 6014 {bp_jit_event, "jit events"},
0e30163f
JK
6015 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6016 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6017 };
269b11a2
PA
6018
6019 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6020 || ((int) type != bptypes[(int) type].type))
6021 internal_error (__FILE__, __LINE__,
6022 _("bptypes table does not describe type #%d."),
6023 (int) type);
6024
6025 return bptypes[(int) type].description;
6026}
6027
998580f1
MK
6028/* For MI, output a field named 'thread-groups' with a list as the value.
6029 For CLI, prefix the list with the string 'inf'. */
6030
6031static void
6032output_thread_groups (struct ui_out *uiout,
6033 const char *field_name,
6034 VEC(int) *inf_num,
6035 int mi_only)
6036{
112e8700 6037 int is_mi = uiout->is_mi_like_p ();
998580f1
MK
6038 int inf;
6039 int i;
6040
6041 /* For backward compatibility, don't display inferiors in CLI unless
6042 there are several. Always display them for MI. */
6043 if (!is_mi && mi_only)
6044 return;
6045
10f489e5 6046 ui_out_emit_list list_emitter (uiout, field_name);
752eb8b4 6047
998580f1
MK
6048 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6049 {
6050 if (is_mi)
6051 {
6052 char mi_group[10];
6053
6054 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
112e8700 6055 uiout->field_string (NULL, mi_group);
998580f1
MK
6056 }
6057 else
6058 {
6059 if (i == 0)
112e8700 6060 uiout->text (" inf ");
998580f1 6061 else
112e8700 6062 uiout->text (", ");
998580f1 6063
112e8700 6064 uiout->text (plongest (inf));
998580f1
MK
6065 }
6066 }
998580f1
MK
6067}
6068
269b11a2
PA
6069/* Print B to gdb_stdout. */
6070
6071static void
6072print_one_breakpoint_location (struct breakpoint *b,
6073 struct bp_location *loc,
6074 int loc_number,
6075 struct bp_location **last_loc,
269b11a2
PA
6076 int allflag)
6077{
6078 struct command_line *l;
c2c6d25f 6079 static char bpenables[] = "nynny";
c906108c 6080
79a45e25 6081 struct ui_out *uiout = current_uiout;
0d381245
VP
6082 int header_of_multiple = 0;
6083 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6084 struct value_print_options opts;
6085
6086 get_user_print_options (&opts);
0d381245
VP
6087
6088 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6089 /* See comment in print_one_breakpoint concerning treatment of
6090 breakpoints with single disabled location. */
0d381245
VP
6091 if (loc == NULL
6092 && (b->loc != NULL
6093 && (b->loc->next != NULL || !b->loc->enabled)))
6094 header_of_multiple = 1;
6095 if (loc == NULL)
6096 loc = b->loc;
6097
c4093a6a
JM
6098 annotate_record ();
6099
6100 /* 1 */
6101 annotate_field (0);
0d381245
VP
6102 if (part_of_multiple)
6103 {
6104 char *formatted;
0c6773c1 6105 formatted = xstrprintf ("%d.%d", b->number, loc_number);
112e8700 6106 uiout->field_string ("number", formatted);
0d381245
VP
6107 xfree (formatted);
6108 }
6109 else
6110 {
112e8700 6111 uiout->field_int ("number", b->number);
0d381245 6112 }
c4093a6a
JM
6113
6114 /* 2 */
6115 annotate_field (1);
0d381245 6116 if (part_of_multiple)
112e8700 6117 uiout->field_skip ("type");
269b11a2 6118 else
112e8700 6119 uiout->field_string ("type", bptype_string (b->type));
c4093a6a
JM
6120
6121 /* 3 */
6122 annotate_field (2);
0d381245 6123 if (part_of_multiple)
112e8700 6124 uiout->field_skip ("disp");
0d381245 6125 else
112e8700 6126 uiout->field_string ("disp", bpdisp_text (b->disposition));
0d381245 6127
c4093a6a
JM
6128
6129 /* 4 */
6130 annotate_field (3);
0d381245 6131 if (part_of_multiple)
112e8700 6132 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
0d381245 6133 else
112e8700
SM
6134 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6135 uiout->spaces (2);
0d381245 6136
c4093a6a
JM
6137
6138 /* 5 and 6 */
3086aeae 6139 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6140 {
4a64f543
MS
6141 /* Although the print_one can possibly print all locations,
6142 calling it here is not likely to get any nice result. So,
6143 make sure there's just one location. */
0d381245 6144 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6145 b->ops->print_one (b, last_loc);
0d381245 6146 }
3086aeae
DJ
6147 else
6148 switch (b->type)
6149 {
6150 case bp_none:
6151 internal_error (__FILE__, __LINE__,
e2e0b3e5 6152 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6153 break;
c906108c 6154
3086aeae
DJ
6155 case bp_watchpoint:
6156 case bp_hardware_watchpoint:
6157 case bp_read_watchpoint:
6158 case bp_access_watchpoint:
3a5c3e22
PA
6159 {
6160 struct watchpoint *w = (struct watchpoint *) b;
6161
6162 /* Field 4, the address, is omitted (which makes the columns
6163 not line up too nicely with the headers, but the effect
6164 is relatively readable). */
6165 if (opts.addressprint)
112e8700 6166 uiout->field_skip ("addr");
3a5c3e22 6167 annotate_field (5);
112e8700 6168 uiout->field_string ("what", w->exp_string);
3a5c3e22 6169 }
3086aeae
DJ
6170 break;
6171
3086aeae
DJ
6172 case bp_breakpoint:
6173 case bp_hardware_breakpoint:
7c16b83e 6174 case bp_single_step:
3086aeae
DJ
6175 case bp_until:
6176 case bp_finish:
6177 case bp_longjmp:
6178 case bp_longjmp_resume:
e2e4d78b 6179 case bp_longjmp_call_dummy:
186c406b
TT
6180 case bp_exception:
6181 case bp_exception_resume:
3086aeae 6182 case bp_step_resume:
2c03e5be 6183 case bp_hp_step_resume:
3086aeae
DJ
6184 case bp_watchpoint_scope:
6185 case bp_call_dummy:
aa7d318d 6186 case bp_std_terminate:
3086aeae
DJ
6187 case bp_shlib_event:
6188 case bp_thread_event:
6189 case bp_overlay_event:
0fd8e87f 6190 case bp_longjmp_master:
aa7d318d 6191 case bp_std_terminate_master:
186c406b 6192 case bp_exception_master:
1042e4c0 6193 case bp_tracepoint:
7a697b8d 6194 case bp_fast_tracepoint:
0fb4aa4b 6195 case bp_static_tracepoint:
e7e0cddf 6196 case bp_dprintf:
4efc6507 6197 case bp_jit_event:
0e30163f
JK
6198 case bp_gnu_ifunc_resolver:
6199 case bp_gnu_ifunc_resolver_return:
79a45b7d 6200 if (opts.addressprint)
3086aeae
DJ
6201 {
6202 annotate_field (4);
54e52265 6203 if (header_of_multiple)
112e8700 6204 uiout->field_string ("addr", "<MULTIPLE>");
e9bbd7c5 6205 else if (b->loc == NULL || loc->shlib_disabled)
112e8700 6206 uiout->field_string ("addr", "<PENDING>");
0101ce28 6207 else
112e8700 6208 uiout->field_core_addr ("addr",
5af949e3 6209 loc->gdbarch, loc->address);
3086aeae
DJ
6210 }
6211 annotate_field (5);
0d381245 6212 if (!header_of_multiple)
170b53b2 6213 print_breakpoint_location (b, loc);
0d381245 6214 if (b->loc)
a6d9a66e 6215 *last_loc = b->loc;
3086aeae
DJ
6216 break;
6217 }
c906108c 6218
6c95b8df 6219
998580f1 6220 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6221 {
6222 struct inferior *inf;
998580f1
MK
6223 VEC(int) *inf_num = NULL;
6224 int mi_only = 1;
6c95b8df 6225
998580f1 6226 ALL_INFERIORS (inf)
6c95b8df
PA
6227 {
6228 if (inf->pspace == loc->pspace)
998580f1 6229 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6230 }
998580f1
MK
6231
6232 /* For backward compatibility, don't display inferiors in CLI unless
6233 there are several. Always display for MI. */
6234 if (allflag
6235 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6236 && (number_of_program_spaces () > 1
6237 || number_of_inferiors () > 1)
6238 /* LOC is for existing B, it cannot be in
6239 moribund_locations and thus having NULL OWNER. */
6240 && loc->owner->type != bp_catchpoint))
6241 mi_only = 0;
6242 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6243 VEC_free (int, inf_num);
6c95b8df
PA
6244 }
6245
4a306c9a 6246 if (!part_of_multiple)
c4093a6a 6247 {
4a306c9a
JB
6248 if (b->thread != -1)
6249 {
6250 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6251 "stop only in" line a little further down. */
112e8700
SM
6252 uiout->text (" thread ");
6253 uiout->field_int ("thread", b->thread);
4a306c9a
JB
6254 }
6255 else if (b->task != 0)
6256 {
112e8700
SM
6257 uiout->text (" task ");
6258 uiout->field_int ("task", b->task);
4a306c9a 6259 }
c4093a6a 6260 }
f1310107 6261
112e8700 6262 uiout->text ("\n");
f1310107 6263
348d480f 6264 if (!part_of_multiple)
f1310107
TJB
6265 b->ops->print_one_detail (b, uiout);
6266
0d381245 6267 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6268 {
6269 annotate_field (6);
112e8700 6270 uiout->text ("\tstop only in stack frame at ");
e5dd4106 6271 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6272 the frame ID. */
112e8700 6273 uiout->field_core_addr ("frame",
5af949e3 6274 b->gdbarch, b->frame_id.stack_addr);
112e8700 6275 uiout->text ("\n");
c4093a6a
JM
6276 }
6277
28010a5d 6278 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6279 {
6280 annotate_field (7);
d77f58be 6281 if (is_tracepoint (b))
112e8700 6282 uiout->text ("\ttrace only if ");
1042e4c0 6283 else
112e8700
SM
6284 uiout->text ("\tstop only if ");
6285 uiout->field_string ("cond", b->cond_string);
b775012e
LM
6286
6287 /* Print whether the target is doing the breakpoint's condition
6288 evaluation. If GDB is doing the evaluation, don't print anything. */
6289 if (is_breakpoint (b)
6290 && breakpoint_condition_evaluation_mode ()
6291 == condition_evaluation_target)
6292 {
112e8700
SM
6293 uiout->text (" (");
6294 uiout->field_string ("evaluated-by",
b775012e 6295 bp_condition_evaluator (b));
112e8700 6296 uiout->text (" evals)");
b775012e 6297 }
112e8700 6298 uiout->text ("\n");
0101ce28
JJ
6299 }
6300
0d381245 6301 if (!part_of_multiple && b->thread != -1)
c4093a6a 6302 {
4a64f543 6303 /* FIXME should make an annotation for this. */
112e8700
SM
6304 uiout->text ("\tstop only in thread ");
6305 if (uiout->is_mi_like_p ())
6306 uiout->field_int ("thread", b->thread);
5d5658a1
PA
6307 else
6308 {
6309 struct thread_info *thr = find_thread_global_id (b->thread);
6310
112e8700 6311 uiout->field_string ("thread", print_thread_id (thr));
5d5658a1 6312 }
112e8700 6313 uiout->text ("\n");
c4093a6a
JM
6314 }
6315
556ec64d
YQ
6316 if (!part_of_multiple)
6317 {
6318 if (b->hit_count)
31f56a27
YQ
6319 {
6320 /* FIXME should make an annotation for this. */
6321 if (is_catchpoint (b))
112e8700 6322 uiout->text ("\tcatchpoint");
31f56a27 6323 else if (is_tracepoint (b))
112e8700 6324 uiout->text ("\ttracepoint");
31f56a27 6325 else
112e8700
SM
6326 uiout->text ("\tbreakpoint");
6327 uiout->text (" already hit ");
6328 uiout->field_int ("times", b->hit_count);
31f56a27 6329 if (b->hit_count == 1)
112e8700 6330 uiout->text (" time\n");
31f56a27 6331 else
112e8700 6332 uiout->text (" times\n");
31f56a27 6333 }
556ec64d
YQ
6334 else
6335 {
31f56a27 6336 /* Output the count also if it is zero, but only if this is mi. */
112e8700
SM
6337 if (uiout->is_mi_like_p ())
6338 uiout->field_int ("times", b->hit_count);
556ec64d
YQ
6339 }
6340 }
8b93c638 6341
0d381245 6342 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6343 {
6344 annotate_field (8);
112e8700
SM
6345 uiout->text ("\tignore next ");
6346 uiout->field_int ("ignore", b->ignore_count);
6347 uiout->text (" hits\n");
c4093a6a 6348 }
059fb39f 6349
816338b5
SS
6350 /* Note that an enable count of 1 corresponds to "enable once"
6351 behavior, which is reported by the combination of enablement and
6352 disposition, so we don't need to mention it here. */
6353 if (!part_of_multiple && b->enable_count > 1)
6354 {
6355 annotate_field (8);
112e8700 6356 uiout->text ("\tdisable after ");
816338b5
SS
6357 /* Tweak the wording to clarify that ignore and enable counts
6358 are distinct, and have additive effect. */
6359 if (b->ignore_count)
112e8700 6360 uiout->text ("additional ");
816338b5 6361 else
112e8700
SM
6362 uiout->text ("next ");
6363 uiout->field_int ("enable", b->enable_count);
6364 uiout->text (" hits\n");
816338b5
SS
6365 }
6366
f196051f
SS
6367 if (!part_of_multiple && is_tracepoint (b))
6368 {
6369 struct tracepoint *tp = (struct tracepoint *) b;
6370
6371 if (tp->traceframe_usage)
6372 {
112e8700
SM
6373 uiout->text ("\ttrace buffer usage ");
6374 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6375 uiout->text (" bytes\n");
f196051f
SS
6376 }
6377 }
d3ce09f5 6378
d1b0a7bf 6379 l = b->commands ? b->commands.get () : NULL;
059fb39f 6380 if (!part_of_multiple && l)
c4093a6a
JM
6381 {
6382 annotate_field (9);
2e783024 6383 ui_out_emit_tuple tuple_emitter (uiout, "script");
8b93c638 6384 print_command_lines (uiout, l, 4);
c4093a6a 6385 }
d24317b4 6386
d9b3f62e 6387 if (is_tracepoint (b))
1042e4c0 6388 {
d9b3f62e
PA
6389 struct tracepoint *t = (struct tracepoint *) b;
6390
6391 if (!part_of_multiple && t->pass_count)
6392 {
6393 annotate_field (10);
112e8700
SM
6394 uiout->text ("\tpass count ");
6395 uiout->field_int ("pass", t->pass_count);
6396 uiout->text (" \n");
d9b3f62e 6397 }
f2a8bc8a
YQ
6398
6399 /* Don't display it when tracepoint or tracepoint location is
6400 pending. */
6401 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6402 {
6403 annotate_field (11);
6404
112e8700
SM
6405 if (uiout->is_mi_like_p ())
6406 uiout->field_string ("installed",
f2a8bc8a
YQ
6407 loc->inserted ? "y" : "n");
6408 else
6409 {
6410 if (loc->inserted)
112e8700 6411 uiout->text ("\t");
f2a8bc8a 6412 else
112e8700
SM
6413 uiout->text ("\tnot ");
6414 uiout->text ("installed on target\n");
f2a8bc8a
YQ
6415 }
6416 }
1042e4c0
SS
6417 }
6418
112e8700 6419 if (uiout->is_mi_like_p () && !part_of_multiple)
d24317b4 6420 {
3a5c3e22
PA
6421 if (is_watchpoint (b))
6422 {
6423 struct watchpoint *w = (struct watchpoint *) b;
6424
112e8700 6425 uiout->field_string ("original-location", w->exp_string);
3a5c3e22 6426 }
f00aae0f 6427 else if (b->location != NULL
d28cd78a 6428 && event_location_to_string (b->location.get ()) != NULL)
112e8700 6429 uiout->field_string ("original-location",
d28cd78a 6430 event_location_to_string (b->location.get ()));
d24317b4 6431 }
c4093a6a 6432}
c5aa993b 6433
0d381245
VP
6434static void
6435print_one_breakpoint (struct breakpoint *b,
4a64f543 6436 struct bp_location **last_loc,
6c95b8df 6437 int allflag)
0d381245 6438{
79a45e25 6439 struct ui_out *uiout = current_uiout;
8d3788bd 6440
2e783024
TT
6441 {
6442 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
8d3788bd 6443
2e783024
TT
6444 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6445 }
0d381245
VP
6446
6447 /* If this breakpoint has custom print function,
6448 it's already printed. Otherwise, print individual
6449 locations, if any. */
6450 if (b->ops == NULL || b->ops->print_one == NULL)
6451 {
4a64f543
MS
6452 /* If breakpoint has a single location that is disabled, we
6453 print it as if it had several locations, since otherwise it's
6454 hard to represent "breakpoint enabled, location disabled"
6455 situation.
6456
6457 Note that while hardware watchpoints have several locations
a3be7890 6458 internally, that's not a property exposed to user. */
0d381245 6459 if (b->loc
a5606eee 6460 && !is_hardware_watchpoint (b)
8d3788bd 6461 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6462 {
6463 struct bp_location *loc;
6464 int n = 1;
8d3788bd 6465
0d381245 6466 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd 6467 {
2e783024 6468 ui_out_emit_tuple tuple_emitter (uiout, NULL);
8d3788bd 6469 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
8d3788bd 6470 }
0d381245
VP
6471 }
6472 }
6473}
6474
a6d9a66e
UW
6475static int
6476breakpoint_address_bits (struct breakpoint *b)
6477{
6478 int print_address_bits = 0;
6479 struct bp_location *loc;
6480
c6d81124
PA
6481 /* Software watchpoints that aren't watching memory don't have an
6482 address to print. */
6483 if (is_no_memory_software_watchpoint (b))
6484 return 0;
6485
a6d9a66e
UW
6486 for (loc = b->loc; loc; loc = loc->next)
6487 {
c7437ca6
PA
6488 int addr_bit;
6489
c7437ca6 6490 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6491 if (addr_bit > print_address_bits)
6492 print_address_bits = addr_bit;
6493 }
6494
6495 return print_address_bits;
6496}
0d381245 6497
65630365 6498/* See breakpoint.h. */
c5aa993b 6499
65630365
PA
6500void
6501print_breakpoint (breakpoint *b)
c4093a6a 6502{
a6d9a66e 6503 struct bp_location *dummy_loc = NULL;
65630365 6504 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6505}
c5aa993b 6506
09d682a4
TT
6507/* Return true if this breakpoint was set by the user, false if it is
6508 internal or momentary. */
6509
6510int
6511user_breakpoint_p (struct breakpoint *b)
6512{
46c6471b 6513 return b->number > 0;
09d682a4
TT
6514}
6515
93daf339
TT
6516/* See breakpoint.h. */
6517
6518int
6519pending_breakpoint_p (struct breakpoint *b)
6520{
6521 return b->loc == NULL;
6522}
6523
7f3b0473 6524/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6525 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6526 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6527 FILTER is non-NULL, call it on each breakpoint and only include the
6528 ones for which it returns non-zero. Return the total number of
6529 breakpoints listed. */
c906108c 6530
d77f58be 6531static int
4495129a 6532breakpoint_1 (const char *args, int allflag,
4a64f543 6533 int (*filter) (const struct breakpoint *))
c4093a6a 6534{
52f0bd74 6535 struct breakpoint *b;
a6d9a66e 6536 struct bp_location *last_loc = NULL;
7f3b0473 6537 int nr_printable_breakpoints;
79a45b7d 6538 struct value_print_options opts;
a6d9a66e 6539 int print_address_bits = 0;
269b11a2 6540 int print_type_col_width = 14;
79a45e25 6541 struct ui_out *uiout = current_uiout;
269b11a2 6542
79a45b7d
TT
6543 get_user_print_options (&opts);
6544
4a64f543
MS
6545 /* Compute the number of rows in the table, as well as the size
6546 required for address fields. */
7f3b0473
AC
6547 nr_printable_breakpoints = 0;
6548 ALL_BREAKPOINTS (b)
e5a67952
MS
6549 {
6550 /* If we have a filter, only list the breakpoints it accepts. */
6551 if (filter && !filter (b))
6552 continue;
6553
6554 /* If we have an "args" string, it is a list of breakpoints to
6555 accept. Skip the others. */
6556 if (args != NULL && *args != '\0')
6557 {
6558 if (allflag && parse_and_eval_long (args) != b->number)
6559 continue;
6560 if (!allflag && !number_is_in_list (args, b->number))
6561 continue;
6562 }
269b11a2 6563
e5a67952
MS
6564 if (allflag || user_breakpoint_p (b))
6565 {
6566 int addr_bit, type_len;
a6d9a66e 6567
e5a67952
MS
6568 addr_bit = breakpoint_address_bits (b);
6569 if (addr_bit > print_address_bits)
6570 print_address_bits = addr_bit;
269b11a2 6571
e5a67952
MS
6572 type_len = strlen (bptype_string (b->type));
6573 if (type_len > print_type_col_width)
6574 print_type_col_width = type_len;
6575
6576 nr_printable_breakpoints++;
6577 }
6578 }
7f3b0473 6579
4a2b031d
TT
6580 {
6581 ui_out_emit_table table_emitter (uiout,
6582 opts.addressprint ? 6 : 5,
6583 nr_printable_breakpoints,
6584 "BreakpointTable");
6585
6586 if (nr_printable_breakpoints > 0)
6587 annotate_breakpoints_headers ();
6588 if (nr_printable_breakpoints > 0)
6589 annotate_field (0);
6590 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6591 if (nr_printable_breakpoints > 0)
6592 annotate_field (1);
6593 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6594 if (nr_printable_breakpoints > 0)
6595 annotate_field (2);
6596 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6597 if (nr_printable_breakpoints > 0)
6598 annotate_field (3);
6599 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6600 if (opts.addressprint)
6601 {
6602 if (nr_printable_breakpoints > 0)
6603 annotate_field (4);
6604 if (print_address_bits <= 32)
6605 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6606 else
6607 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6608 }
6609 if (nr_printable_breakpoints > 0)
6610 annotate_field (5);
6611 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6612 uiout->table_body ();
6613 if (nr_printable_breakpoints > 0)
6614 annotate_breakpoints_table ();
6615
6616 ALL_BREAKPOINTS (b)
6617 {
6618 QUIT;
6619 /* If we have a filter, only list the breakpoints it accepts. */
6620 if (filter && !filter (b))
6621 continue;
e5a67952 6622
4a2b031d
TT
6623 /* If we have an "args" string, it is a list of breakpoints to
6624 accept. Skip the others. */
e5a67952 6625
4a2b031d
TT
6626 if (args != NULL && *args != '\0')
6627 {
6628 if (allflag) /* maintenance info breakpoint */
6629 {
6630 if (parse_and_eval_long (args) != b->number)
6631 continue;
6632 }
6633 else /* all others */
6634 {
6635 if (!number_is_in_list (args, b->number))
6636 continue;
6637 }
6638 }
6639 /* We only print out user settable breakpoints unless the
6640 allflag is set. */
6641 if (allflag || user_breakpoint_p (b))
6642 print_one_breakpoint (b, &last_loc, allflag);
6643 }
6644 }
698384cd 6645
7f3b0473 6646 if (nr_printable_breakpoints == 0)
c906108c 6647 {
4a64f543
MS
6648 /* If there's a filter, let the caller decide how to report
6649 empty list. */
d77f58be
SS
6650 if (!filter)
6651 {
e5a67952 6652 if (args == NULL || *args == '\0')
112e8700 6653 uiout->message ("No breakpoints or watchpoints.\n");
d77f58be 6654 else
112e8700 6655 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
e5a67952 6656 args);
d77f58be 6657 }
c906108c
SS
6658 }
6659 else
c4093a6a 6660 {
a6d9a66e
UW
6661 if (last_loc && !server_command)
6662 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6663 }
c906108c 6664
4a64f543 6665 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6666 there have been breakpoints? */
c906108c 6667 annotate_breakpoints_table_end ();
d77f58be
SS
6668
6669 return nr_printable_breakpoints;
c906108c
SS
6670}
6671
ad443146
SS
6672/* Display the value of default-collect in a way that is generally
6673 compatible with the breakpoint list. */
6674
6675static void
6676default_collect_info (void)
6677{
79a45e25
PA
6678 struct ui_out *uiout = current_uiout;
6679
ad443146
SS
6680 /* If it has no value (which is frequently the case), say nothing; a
6681 message like "No default-collect." gets in user's face when it's
6682 not wanted. */
6683 if (!*default_collect)
6684 return;
6685
6686 /* The following phrase lines up nicely with per-tracepoint collect
6687 actions. */
112e8700
SM
6688 uiout->text ("default collect ");
6689 uiout->field_string ("default-collect", default_collect);
6690 uiout->text (" \n");
ad443146
SS
6691}
6692
c906108c 6693static void
0b39b52e 6694info_breakpoints_command (const char *args, int from_tty)
c906108c 6695{
e5a67952 6696 breakpoint_1 (args, 0, NULL);
ad443146
SS
6697
6698 default_collect_info ();
d77f58be
SS
6699}
6700
6701static void
1d12d88f 6702info_watchpoints_command (const char *args, int from_tty)
d77f58be 6703{
e5a67952 6704 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6705 struct ui_out *uiout = current_uiout;
d77f58be
SS
6706
6707 if (num_printed == 0)
6708 {
e5a67952 6709 if (args == NULL || *args == '\0')
112e8700 6710 uiout->message ("No watchpoints.\n");
d77f58be 6711 else
112e8700 6712 uiout->message ("No watchpoint matching '%s'.\n", args);
d77f58be 6713 }
c906108c
SS
6714}
6715
7a292a7a 6716static void
4495129a 6717maintenance_info_breakpoints (const char *args, int from_tty)
c906108c 6718{
e5a67952 6719 breakpoint_1 (args, 1, NULL);
ad443146
SS
6720
6721 default_collect_info ();
c906108c
SS
6722}
6723
0d381245 6724static int
714835d5 6725breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6726 struct program_space *pspace,
714835d5 6727 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6728{
6729 struct bp_location *bl = b->loc;
cc59ec59 6730
0d381245
VP
6731 for (; bl; bl = bl->next)
6732 {
6c95b8df
PA
6733 if (bl->pspace == pspace
6734 && bl->address == pc
0d381245
VP
6735 && (!overlay_debugging || bl->section == section))
6736 return 1;
6737 }
6738 return 0;
6739}
6740
672f9b60 6741/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6742 concerns with logical breakpoints, so we match program spaces, not
6743 address spaces. */
c906108c
SS
6744
6745static void
6c95b8df
PA
6746describe_other_breakpoints (struct gdbarch *gdbarch,
6747 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6748 struct obj_section *section, int thread)
c906108c 6749{
52f0bd74
AC
6750 int others = 0;
6751 struct breakpoint *b;
c906108c
SS
6752
6753 ALL_BREAKPOINTS (b)
672f9b60
KP
6754 others += (user_breakpoint_p (b)
6755 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6756 if (others > 0)
6757 {
a3f17187
AC
6758 if (others == 1)
6759 printf_filtered (_("Note: breakpoint "));
6760 else /* if (others == ???) */
6761 printf_filtered (_("Note: breakpoints "));
c906108c 6762 ALL_BREAKPOINTS (b)
672f9b60 6763 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6764 {
6765 others--;
6766 printf_filtered ("%d", b->number);
6767 if (b->thread == -1 && thread != -1)
6768 printf_filtered (" (all threads)");
6769 else if (b->thread != -1)
6770 printf_filtered (" (thread %d)", b->thread);
6771 printf_filtered ("%s%s ",
059fb39f 6772 ((b->enable_state == bp_disabled
f8eba3c6 6773 || b->enable_state == bp_call_disabled)
0d381245 6774 ? " (disabled)"
0d381245
VP
6775 : ""),
6776 (others > 1) ? ","
6777 : ((others == 1) ? " and" : ""));
6778 }
a3f17187 6779 printf_filtered (_("also set at pc "));
5af949e3 6780 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6781 printf_filtered (".\n");
6782 }
6783}
6784\f
c906108c 6785
e4f237da 6786/* Return true iff it is meaningful to use the address member of
244558af
LM
6787 BPT locations. For some breakpoint types, the locations' address members
6788 are irrelevant and it makes no sense to attempt to compare them to other
6789 addresses (or use them for any other purpose either).
e4f237da 6790
4a64f543 6791 More specifically, each of the following breakpoint types will
244558af 6792 always have a zero valued location address and we don't want to mark
4a64f543 6793 breakpoints of any of these types to be a duplicate of an actual
244558af 6794 breakpoint location at address zero:
e4f237da
KB
6795
6796 bp_watchpoint
2d134ed3
PA
6797 bp_catchpoint
6798
6799*/
e4f237da
KB
6800
6801static int
6802breakpoint_address_is_meaningful (struct breakpoint *bpt)
6803{
6804 enum bptype type = bpt->type;
6805
2d134ed3
PA
6806 return (type != bp_watchpoint && type != bp_catchpoint);
6807}
6808
6809/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6810 true if LOC1 and LOC2 represent the same watchpoint location. */
6811
6812static int
4a64f543
MS
6813watchpoint_locations_match (struct bp_location *loc1,
6814 struct bp_location *loc2)
2d134ed3 6815{
3a5c3e22
PA
6816 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6817 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6818
6819 /* Both of them must exist. */
6820 gdb_assert (w1 != NULL);
6821 gdb_assert (w2 != NULL);
2bdf28a0 6822
4a64f543
MS
6823 /* If the target can evaluate the condition expression in hardware,
6824 then we we need to insert both watchpoints even if they are at
6825 the same place. Otherwise the watchpoint will only trigger when
6826 the condition of whichever watchpoint was inserted evaluates to
6827 true, not giving a chance for GDB to check the condition of the
6828 other watchpoint. */
3a5c3e22 6829 if ((w1->cond_exp
4a64f543
MS
6830 && target_can_accel_watchpoint_condition (loc1->address,
6831 loc1->length,
0cf6dd15 6832 loc1->watchpoint_type,
4d01a485 6833 w1->cond_exp.get ()))
3a5c3e22 6834 || (w2->cond_exp
4a64f543
MS
6835 && target_can_accel_watchpoint_condition (loc2->address,
6836 loc2->length,
0cf6dd15 6837 loc2->watchpoint_type,
4d01a485 6838 w2->cond_exp.get ())))
0cf6dd15
TJB
6839 return 0;
6840
85d721b8
PA
6841 /* Note that this checks the owner's type, not the location's. In
6842 case the target does not support read watchpoints, but does
6843 support access watchpoints, we'll have bp_read_watchpoint
6844 watchpoints with hw_access locations. Those should be considered
6845 duplicates of hw_read locations. The hw_read locations will
6846 become hw_access locations later. */
2d134ed3
PA
6847 return (loc1->owner->type == loc2->owner->type
6848 && loc1->pspace->aspace == loc2->pspace->aspace
6849 && loc1->address == loc2->address
6850 && loc1->length == loc2->length);
e4f237da
KB
6851}
6852
31e77af2 6853/* See breakpoint.h. */
6c95b8df 6854
31e77af2 6855int
accd0bcd
YQ
6856breakpoint_address_match (const address_space *aspace1, CORE_ADDR addr1,
6857 const address_space *aspace2, CORE_ADDR addr2)
6c95b8df 6858{
f5656ead 6859 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6860 || aspace1 == aspace2)
6861 && addr1 == addr2);
6862}
6863
f1310107
TJB
6864/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6865 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6866 matches ASPACE2. On targets that have global breakpoints, the address
6867 space doesn't really matter. */
6868
6869static int
accd0bcd
YQ
6870breakpoint_address_match_range (const address_space *aspace1,
6871 CORE_ADDR addr1,
6872 int len1, const address_space *aspace2,
f1310107
TJB
6873 CORE_ADDR addr2)
6874{
f5656ead 6875 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6876 || aspace1 == aspace2)
6877 && addr2 >= addr1 && addr2 < addr1 + len1);
6878}
6879
6880/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6881 a ranged breakpoint. In most targets, a match happens only if ASPACE
6882 matches the breakpoint's address space. On targets that have global
6883 breakpoints, the address space doesn't really matter. */
6884
6885static int
6886breakpoint_location_address_match (struct bp_location *bl,
accd0bcd 6887 const address_space *aspace,
f1310107
TJB
6888 CORE_ADDR addr)
6889{
6890 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6891 aspace, addr)
6892 || (bl->length
6893 && breakpoint_address_match_range (bl->pspace->aspace,
6894 bl->address, bl->length,
6895 aspace, addr)));
6896}
6897
d35ae833
PA
6898/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
6899 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
6900 match happens only if ASPACE matches the breakpoint's address
6901 space. On targets that have global breakpoints, the address space
6902 doesn't really matter. */
6903
6904static int
6905breakpoint_location_address_range_overlap (struct bp_location *bl,
accd0bcd 6906 const address_space *aspace,
d35ae833
PA
6907 CORE_ADDR addr, int len)
6908{
6909 if (gdbarch_has_global_breakpoints (target_gdbarch ())
6910 || bl->pspace->aspace == aspace)
6911 {
6912 int bl_len = bl->length != 0 ? bl->length : 1;
6913
6914 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
6915 return 1;
6916 }
6917 return 0;
6918}
6919
1e4d1764
YQ
6920/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6921 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6922 true, otherwise returns false. */
6923
6924static int
6925tracepoint_locations_match (struct bp_location *loc1,
6926 struct bp_location *loc2)
6927{
6928 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6929 /* Since tracepoint locations are never duplicated with others', tracepoint
6930 locations at the same address of different tracepoints are regarded as
6931 different locations. */
6932 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6933 else
6934 return 0;
6935}
6936
2d134ed3
PA
6937/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6938 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6939 represent the same location. */
6940
6941static int
4a64f543
MS
6942breakpoint_locations_match (struct bp_location *loc1,
6943 struct bp_location *loc2)
2d134ed3 6944{
2bdf28a0
JK
6945 int hw_point1, hw_point2;
6946
6947 /* Both of them must not be in moribund_locations. */
6948 gdb_assert (loc1->owner != NULL);
6949 gdb_assert (loc2->owner != NULL);
6950
6951 hw_point1 = is_hardware_watchpoint (loc1->owner);
6952 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6953
6954 if (hw_point1 != hw_point2)
6955 return 0;
6956 else if (hw_point1)
6957 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6958 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6959 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6960 else
f1310107
TJB
6961 /* We compare bp_location.length in order to cover ranged breakpoints. */
6962 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6963 loc2->pspace->aspace, loc2->address)
6964 && loc1->length == loc2->length);
2d134ed3
PA
6965}
6966
76897487
KB
6967static void
6968breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6969 int bnum, int have_bnum)
6970{
f63fbe86
MS
6971 /* The longest string possibly returned by hex_string_custom
6972 is 50 chars. These must be at least that big for safety. */
6973 char astr1[64];
6974 char astr2[64];
76897487 6975
bb599908
PH
6976 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6977 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6978 if (have_bnum)
8a3fe4f8 6979 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6980 bnum, astr1, astr2);
6981 else
8a3fe4f8 6982 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6983}
6984
4a64f543
MS
6985/* Adjust a breakpoint's address to account for architectural
6986 constraints on breakpoint placement. Return the adjusted address.
6987 Note: Very few targets require this kind of adjustment. For most
6988 targets, this function is simply the identity function. */
76897487
KB
6989
6990static CORE_ADDR
a6d9a66e
UW
6991adjust_breakpoint_address (struct gdbarch *gdbarch,
6992 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6993{
a6d9a66e 6994 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
6995 {
6996 /* Very few targets need any kind of breakpoint adjustment. */
6997 return bpaddr;
6998 }
88f7da05
KB
6999 else if (bptype == bp_watchpoint
7000 || bptype == bp_hardware_watchpoint
7001 || bptype == bp_read_watchpoint
7002 || bptype == bp_access_watchpoint
fe798b75 7003 || bptype == bp_catchpoint)
88f7da05
KB
7004 {
7005 /* Watchpoints and the various bp_catch_* eventpoints should not
7006 have their addresses modified. */
7007 return bpaddr;
7008 }
7c16b83e
PA
7009 else if (bptype == bp_single_step)
7010 {
7011 /* Single-step breakpoints should not have their addresses
7012 modified. If there's any architectural constrain that
7013 applies to this address, then it should have already been
7014 taken into account when the breakpoint was created in the
7015 first place. If we didn't do this, stepping through e.g.,
7016 Thumb-2 IT blocks would break. */
7017 return bpaddr;
7018 }
76897487
KB
7019 else
7020 {
7021 CORE_ADDR adjusted_bpaddr;
7022
7023 /* Some targets have architectural constraints on the placement
7024 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7025 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7026
7027 /* An adjusted breakpoint address can significantly alter
7028 a user's expectations. Print a warning if an adjustment
7029 is required. */
7030 if (adjusted_bpaddr != bpaddr)
7031 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7032
7033 return adjusted_bpaddr;
7034 }
7035}
7036
5625a286 7037bp_location::bp_location (const bp_location_ops *ops, breakpoint *owner)
7cc221ef 7038{
5625a286 7039 bp_location *loc = this;
7cc221ef 7040
348d480f
PA
7041 gdb_assert (ops != NULL);
7042
28010a5d
PA
7043 loc->ops = ops;
7044 loc->owner = owner;
b775012e 7045 loc->cond_bytecode = NULL;
0d381245
VP
7046 loc->shlib_disabled = 0;
7047 loc->enabled = 1;
e049a4b5 7048
28010a5d 7049 switch (owner->type)
e049a4b5
DJ
7050 {
7051 case bp_breakpoint:
7c16b83e 7052 case bp_single_step:
e049a4b5
DJ
7053 case bp_until:
7054 case bp_finish:
7055 case bp_longjmp:
7056 case bp_longjmp_resume:
e2e4d78b 7057 case bp_longjmp_call_dummy:
186c406b
TT
7058 case bp_exception:
7059 case bp_exception_resume:
e049a4b5 7060 case bp_step_resume:
2c03e5be 7061 case bp_hp_step_resume:
e049a4b5
DJ
7062 case bp_watchpoint_scope:
7063 case bp_call_dummy:
aa7d318d 7064 case bp_std_terminate:
e049a4b5
DJ
7065 case bp_shlib_event:
7066 case bp_thread_event:
7067 case bp_overlay_event:
4efc6507 7068 case bp_jit_event:
0fd8e87f 7069 case bp_longjmp_master:
aa7d318d 7070 case bp_std_terminate_master:
186c406b 7071 case bp_exception_master:
0e30163f
JK
7072 case bp_gnu_ifunc_resolver:
7073 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7074 case bp_dprintf:
e049a4b5 7075 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7076 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7077 break;
7078 case bp_hardware_breakpoint:
7079 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7080 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7081 break;
7082 case bp_hardware_watchpoint:
7083 case bp_read_watchpoint:
7084 case bp_access_watchpoint:
7085 loc->loc_type = bp_loc_hardware_watchpoint;
7086 break;
7087 case bp_watchpoint:
ce78b96d 7088 case bp_catchpoint:
15c3d785
PA
7089 case bp_tracepoint:
7090 case bp_fast_tracepoint:
0fb4aa4b 7091 case bp_static_tracepoint:
e049a4b5
DJ
7092 loc->loc_type = bp_loc_other;
7093 break;
7094 default:
e2e0b3e5 7095 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7096 }
7097
f431efe5 7098 loc->refc = 1;
28010a5d
PA
7099}
7100
7101/* Allocate a struct bp_location. */
7102
7103static struct bp_location *
7104allocate_bp_location (struct breakpoint *bpt)
7105{
348d480f
PA
7106 return bpt->ops->allocate_location (bpt);
7107}
7cc221ef 7108
f431efe5
PA
7109static void
7110free_bp_location (struct bp_location *loc)
fe3f5fa8 7111{
348d480f 7112 loc->ops->dtor (loc);
4d01a485 7113 delete loc;
fe3f5fa8
VP
7114}
7115
f431efe5
PA
7116/* Increment reference count. */
7117
7118static void
7119incref_bp_location (struct bp_location *bl)
7120{
7121 ++bl->refc;
7122}
7123
7124/* Decrement reference count. If the reference count reaches 0,
7125 destroy the bp_location. Sets *BLP to NULL. */
7126
7127static void
7128decref_bp_location (struct bp_location **blp)
7129{
0807b50c
PA
7130 gdb_assert ((*blp)->refc > 0);
7131
f431efe5
PA
7132 if (--(*blp)->refc == 0)
7133 free_bp_location (*blp);
7134 *blp = NULL;
7135}
7136
346774a9 7137/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7138
b270e6f9
TT
7139static breakpoint *
7140add_to_breakpoint_chain (std::unique_ptr<breakpoint> &&b)
c906108c 7141{
346774a9 7142 struct breakpoint *b1;
b270e6f9 7143 struct breakpoint *result = b.get ();
c906108c 7144
346774a9
PA
7145 /* Add this breakpoint to the end of the chain so that a list of
7146 breakpoints will come out in order of increasing numbers. */
7147
7148 b1 = breakpoint_chain;
7149 if (b1 == 0)
b270e6f9 7150 breakpoint_chain = b.release ();
346774a9
PA
7151 else
7152 {
7153 while (b1->next)
7154 b1 = b1->next;
b270e6f9 7155 b1->next = b.release ();
346774a9 7156 }
b270e6f9
TT
7157
7158 return result;
346774a9
PA
7159}
7160
7161/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7162
7163static void
7164init_raw_breakpoint_without_location (struct breakpoint *b,
7165 struct gdbarch *gdbarch,
28010a5d 7166 enum bptype bptype,
c0a91b2b 7167 const struct breakpoint_ops *ops)
346774a9 7168{
348d480f
PA
7169 gdb_assert (ops != NULL);
7170
28010a5d 7171 b->ops = ops;
4d28f7a8 7172 b->type = bptype;
a6d9a66e 7173 b->gdbarch = gdbarch;
c906108c
SS
7174 b->language = current_language->la_language;
7175 b->input_radix = input_radix;
d0fb5eae 7176 b->related_breakpoint = b;
346774a9
PA
7177}
7178
7179/* Helper to set_raw_breakpoint below. Creates a breakpoint
7180 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7181
7182static struct breakpoint *
7183set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7184 enum bptype bptype,
c0a91b2b 7185 const struct breakpoint_ops *ops)
346774a9 7186{
3b0871f4 7187 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7188
3b0871f4 7189 init_raw_breakpoint_without_location (b.get (), gdbarch, bptype, ops);
b270e6f9 7190 return add_to_breakpoint_chain (std::move (b));
0d381245
VP
7191}
7192
0e30163f
JK
7193/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7194 resolutions should be made as the user specified the location explicitly
7195 enough. */
7196
0d381245 7197static void
0e30163f 7198set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7199{
2bdf28a0
JK
7200 gdb_assert (loc->owner != NULL);
7201
0d381245 7202 if (loc->owner->type == bp_breakpoint
1042e4c0 7203 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7204 || is_tracepoint (loc->owner))
0d381245 7205 {
0e30163f 7206 int is_gnu_ifunc;
2c02bd72 7207 const char *function_name;
6a3a010b 7208 CORE_ADDR func_addr;
0e30163f 7209
2c02bd72 7210 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7211 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7212
7213 if (is_gnu_ifunc && !explicit_loc)
7214 {
7215 struct breakpoint *b = loc->owner;
7216
7217 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7218 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7219 &loc->requested_address))
7220 {
7221 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7222 loc->address = adjust_breakpoint_address (loc->gdbarch,
7223 loc->requested_address,
7224 b->type);
7225 }
7226 else if (b->type == bp_breakpoint && b->loc == loc
7227 && loc->next == NULL && b->related_breakpoint == b)
7228 {
7229 /* Create only the whole new breakpoint of this type but do not
7230 mess more complicated breakpoints with multiple locations. */
7231 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7232 /* Remember the resolver's address for use by the return
7233 breakpoint. */
7234 loc->related_address = func_addr;
0e30163f
JK
7235 }
7236 }
7237
2c02bd72
DE
7238 if (function_name)
7239 loc->function_name = xstrdup (function_name);
0d381245
VP
7240 }
7241}
7242
a6d9a66e 7243/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7244struct gdbarch *
a6d9a66e
UW
7245get_sal_arch (struct symtab_and_line sal)
7246{
7247 if (sal.section)
7248 return get_objfile_arch (sal.section->objfile);
7249 if (sal.symtab)
eb822aa6 7250 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7251
7252 return NULL;
7253}
7254
346774a9
PA
7255/* Low level routine for partially initializing a breakpoint of type
7256 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7257 file name, and line number are provided by SAL.
0d381245
VP
7258
7259 It is expected that the caller will complete the initialization of
7260 the newly created breakpoint struct as well as output any status
c56053d2 7261 information regarding the creation of a new breakpoint. */
0d381245 7262
346774a9
PA
7263static void
7264init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7265 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7266 const struct breakpoint_ops *ops)
0d381245 7267{
28010a5d 7268 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7269
3742cc8b 7270 add_location_to_breakpoint (b, &sal);
0d381245 7271
6c95b8df
PA
7272 if (bptype != bp_catchpoint)
7273 gdb_assert (sal.pspace != NULL);
7274
f8eba3c6
TT
7275 /* Store the program space that was used to set the breakpoint,
7276 except for ordinary breakpoints, which are independent of the
7277 program space. */
7278 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7279 b->pspace = sal.pspace;
346774a9 7280}
c906108c 7281
346774a9
PA
7282/* set_raw_breakpoint is a low level routine for allocating and
7283 partially initializing a breakpoint of type BPTYPE. The newly
7284 created breakpoint's address, section, source file name, and line
7285 number are provided by SAL. The newly created and partially
7286 initialized breakpoint is added to the breakpoint chain and
7287 is also returned as the value of this function.
7288
7289 It is expected that the caller will complete the initialization of
7290 the newly created breakpoint struct as well as output any status
7291 information regarding the creation of a new breakpoint. In
7292 particular, set_raw_breakpoint does NOT set the breakpoint
7293 number! Care should be taken to not allow an error to occur
7294 prior to completing the initialization of the breakpoint. If this
7295 should happen, a bogus breakpoint will be left on the chain. */
7296
7297struct breakpoint *
7298set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7299 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7300 const struct breakpoint_ops *ops)
346774a9 7301{
3b0871f4 7302 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (bptype);
346774a9 7303
3b0871f4 7304 init_raw_breakpoint (b.get (), gdbarch, sal, bptype, ops);
b270e6f9 7305 return add_to_breakpoint_chain (std::move (b));
c906108c
SS
7306}
7307
53a5351d 7308/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7309 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7310 initiated the operation. */
c906108c
SS
7311
7312void
186c406b 7313set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7314{
35df4500 7315 struct breakpoint *b, *b_tmp;
5d5658a1 7316 int thread = tp->global_num;
0fd8e87f
UW
7317
7318 /* To avoid having to rescan all objfile symbols at every step,
7319 we maintain a list of continually-inserted but always disabled
7320 longjmp "master" breakpoints. Here, we simply create momentary
7321 clones of those and enable them for the requested thread. */
35df4500 7322 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7323 if (b->pspace == current_program_space
186c406b
TT
7324 && (b->type == bp_longjmp_master
7325 || b->type == bp_exception_master))
0fd8e87f 7326 {
06edf0c0
PA
7327 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7328 struct breakpoint *clone;
cc59ec59 7329
e2e4d78b
JK
7330 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7331 after their removal. */
06edf0c0 7332 clone = momentary_breakpoint_from_master (b, type,
c1fc2657 7333 &momentary_breakpoint_ops, 1);
0fd8e87f
UW
7334 clone->thread = thread;
7335 }
186c406b
TT
7336
7337 tp->initiating_frame = frame;
c906108c
SS
7338}
7339
611c83ae 7340/* Delete all longjmp breakpoints from THREAD. */
c906108c 7341void
611c83ae 7342delete_longjmp_breakpoint (int thread)
c906108c 7343{
35df4500 7344 struct breakpoint *b, *b_tmp;
c906108c 7345
35df4500 7346 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7347 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7348 {
7349 if (b->thread == thread)
7350 delete_breakpoint (b);
7351 }
c906108c
SS
7352}
7353
f59f708a
PA
7354void
7355delete_longjmp_breakpoint_at_next_stop (int thread)
7356{
7357 struct breakpoint *b, *b_tmp;
7358
7359 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7360 if (b->type == bp_longjmp || b->type == bp_exception)
7361 {
7362 if (b->thread == thread)
7363 b->disposition = disp_del_at_next_stop;
7364 }
7365}
7366
e2e4d78b
JK
7367/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7368 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7369 pointer to any of them. Return NULL if this system cannot place longjmp
7370 breakpoints. */
7371
7372struct breakpoint *
7373set_longjmp_breakpoint_for_call_dummy (void)
7374{
7375 struct breakpoint *b, *retval = NULL;
7376
7377 ALL_BREAKPOINTS (b)
7378 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7379 {
7380 struct breakpoint *new_b;
7381
7382 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7383 &momentary_breakpoint_ops,
7384 1);
5d5658a1 7385 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7386
7387 /* Link NEW_B into the chain of RETVAL breakpoints. */
7388
7389 gdb_assert (new_b->related_breakpoint == new_b);
7390 if (retval == NULL)
7391 retval = new_b;
7392 new_b->related_breakpoint = retval;
7393 while (retval->related_breakpoint != new_b->related_breakpoint)
7394 retval = retval->related_breakpoint;
7395 retval->related_breakpoint = new_b;
7396 }
7397
7398 return retval;
7399}
7400
7401/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7402 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7403 stack.
7404
7405 You should call this function only at places where it is safe to currently
7406 unwind the whole stack. Failed stack unwind would discard live dummy
7407 frames. */
7408
7409void
b67a2c6f 7410check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7411{
7412 struct breakpoint *b, *b_tmp;
7413
7414 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7415 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7416 {
7417 struct breakpoint *dummy_b = b->related_breakpoint;
7418
7419 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7420 dummy_b = dummy_b->related_breakpoint;
7421 if (dummy_b->type != bp_call_dummy
7422 || frame_find_by_id (dummy_b->frame_id) != NULL)
7423 continue;
7424
b67a2c6f 7425 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7426
7427 while (b->related_breakpoint != b)
7428 {
7429 if (b_tmp == b->related_breakpoint)
7430 b_tmp = b->related_breakpoint->next;
7431 delete_breakpoint (b->related_breakpoint);
7432 }
7433 delete_breakpoint (b);
7434 }
7435}
7436
1900040c
MS
7437void
7438enable_overlay_breakpoints (void)
7439{
52f0bd74 7440 struct breakpoint *b;
1900040c
MS
7441
7442 ALL_BREAKPOINTS (b)
7443 if (b->type == bp_overlay_event)
7444 {
7445 b->enable_state = bp_enabled;
44702360 7446 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7447 overlay_events_enabled = 1;
1900040c
MS
7448 }
7449}
7450
7451void
7452disable_overlay_breakpoints (void)
7453{
52f0bd74 7454 struct breakpoint *b;
1900040c
MS
7455
7456 ALL_BREAKPOINTS (b)
7457 if (b->type == bp_overlay_event)
7458 {
7459 b->enable_state = bp_disabled;
44702360 7460 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7461 overlay_events_enabled = 0;
1900040c
MS
7462 }
7463}
7464
aa7d318d
TT
7465/* Set an active std::terminate breakpoint for each std::terminate
7466 master breakpoint. */
7467void
7468set_std_terminate_breakpoint (void)
7469{
35df4500 7470 struct breakpoint *b, *b_tmp;
aa7d318d 7471
35df4500 7472 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7473 if (b->pspace == current_program_space
7474 && b->type == bp_std_terminate_master)
7475 {
06edf0c0 7476 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7477 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7478 }
7479}
7480
7481/* Delete all the std::terminate breakpoints. */
7482void
7483delete_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->type == bp_std_terminate)
7489 delete_breakpoint (b);
7490}
7491
c4093a6a 7492struct breakpoint *
a6d9a66e 7493create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7494{
7495 struct breakpoint *b;
c4093a6a 7496
06edf0c0
PA
7497 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7498 &internal_breakpoint_ops);
7499
b5de0fa7 7500 b->enable_state = bp_enabled;
f00aae0f 7501 /* location has to be used or breakpoint_re_set will delete me. */
d28cd78a 7502 b->location = new_address_location (b->loc->address, NULL, 0);
c4093a6a 7503
44702360 7504 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7505
c4093a6a
JM
7506 return b;
7507}
7508
0101ce28
JJ
7509struct lang_and_radix
7510 {
7511 enum language lang;
7512 int radix;
7513 };
7514
4efc6507
DE
7515/* Create a breakpoint for JIT code registration and unregistration. */
7516
7517struct breakpoint *
7518create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7519{
2a7f3dff
PA
7520 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7521 &internal_breakpoint_ops);
4efc6507 7522}
0101ce28 7523
03673fc7
PP
7524/* Remove JIT code registration and unregistration breakpoint(s). */
7525
7526void
7527remove_jit_event_breakpoints (void)
7528{
7529 struct breakpoint *b, *b_tmp;
7530
7531 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7532 if (b->type == bp_jit_event
7533 && b->loc->pspace == current_program_space)
7534 delete_breakpoint (b);
7535}
7536
cae688ec
JJ
7537void
7538remove_solib_event_breakpoints (void)
7539{
35df4500 7540 struct breakpoint *b, *b_tmp;
cae688ec 7541
35df4500 7542 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7543 if (b->type == bp_shlib_event
7544 && b->loc->pspace == current_program_space)
cae688ec
JJ
7545 delete_breakpoint (b);
7546}
7547
f37f681c
PA
7548/* See breakpoint.h. */
7549
7550void
7551remove_solib_event_breakpoints_at_next_stop (void)
7552{
7553 struct breakpoint *b, *b_tmp;
7554
7555 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7556 if (b->type == bp_shlib_event
7557 && b->loc->pspace == current_program_space)
7558 b->disposition = disp_del_at_next_stop;
7559}
7560
04086b45
PA
7561/* Helper for create_solib_event_breakpoint /
7562 create_and_insert_solib_event_breakpoint. Allows specifying which
7563 INSERT_MODE to pass through to update_global_location_list. */
7564
7565static struct breakpoint *
7566create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7567 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7568{
7569 struct breakpoint *b;
7570
06edf0c0
PA
7571 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7572 &internal_breakpoint_ops);
04086b45 7573 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7574 return b;
7575}
7576
04086b45
PA
7577struct breakpoint *
7578create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7579{
7580 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7581}
7582
f37f681c
PA
7583/* See breakpoint.h. */
7584
7585struct breakpoint *
7586create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7587{
7588 struct breakpoint *b;
7589
04086b45
PA
7590 /* Explicitly tell update_global_location_list to insert
7591 locations. */
7592 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7593 if (!b->loc->inserted)
7594 {
7595 delete_breakpoint (b);
7596 return NULL;
7597 }
7598 return b;
7599}
7600
cae688ec
JJ
7601/* Disable any breakpoints that are on code in shared libraries. Only
7602 apply to enabled breakpoints, disabled ones can just stay disabled. */
7603
7604void
cb851954 7605disable_breakpoints_in_shlibs (void)
cae688ec 7606{
876fa593 7607 struct bp_location *loc, **locp_tmp;
cae688ec 7608
876fa593 7609 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7610 {
2bdf28a0 7611 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7612 struct breakpoint *b = loc->owner;
2bdf28a0 7613
4a64f543
MS
7614 /* We apply the check to all breakpoints, including disabled for
7615 those with loc->duplicate set. This is so that when breakpoint
7616 becomes enabled, or the duplicate is removed, gdb will try to
7617 insert all breakpoints. If we don't set shlib_disabled here,
7618 we'll try to insert those breakpoints and fail. */
1042e4c0 7619 if (((b->type == bp_breakpoint)
508ccb1f 7620 || (b->type == bp_jit_event)
1042e4c0 7621 || (b->type == bp_hardware_breakpoint)
d77f58be 7622 || (is_tracepoint (b)))
6c95b8df 7623 && loc->pspace == current_program_space
0d381245 7624 && !loc->shlib_disabled
6c95b8df 7625 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7626 )
0d381245
VP
7627 {
7628 loc->shlib_disabled = 1;
7629 }
cae688ec
JJ
7630 }
7631}
7632
63644780
NB
7633/* Disable any breakpoints and tracepoints that are in SOLIB upon
7634 notification of unloaded_shlib. Only apply to enabled breakpoints,
7635 disabled ones can just stay disabled. */
84acb35a 7636
75149521 7637static void
84acb35a
JJ
7638disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7639{
876fa593 7640 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7641 int disabled_shlib_breaks = 0;
7642
876fa593 7643 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7644 {
2bdf28a0 7645 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7646 struct breakpoint *b = loc->owner;
cc59ec59 7647
1e4d1764 7648 if (solib->pspace == loc->pspace
e2dd7057 7649 && !loc->shlib_disabled
1e4d1764
YQ
7650 && (((b->type == bp_breakpoint
7651 || b->type == bp_jit_event
7652 || b->type == bp_hardware_breakpoint)
7653 && (loc->loc_type == bp_loc_hardware_breakpoint
7654 || loc->loc_type == bp_loc_software_breakpoint))
7655 || is_tracepoint (b))
e2dd7057 7656 && solib_contains_address_p (solib, loc->address))
84acb35a 7657 {
e2dd7057
PP
7658 loc->shlib_disabled = 1;
7659 /* At this point, we cannot rely on remove_breakpoint
7660 succeeding so we must mark the breakpoint as not inserted
7661 to prevent future errors occurring in remove_breakpoints. */
7662 loc->inserted = 0;
8d3788bd
VP
7663
7664 /* This may cause duplicate notifications for the same breakpoint. */
7665 observer_notify_breakpoint_modified (b);
7666
e2dd7057
PP
7667 if (!disabled_shlib_breaks)
7668 {
223ffa71 7669 target_terminal::ours_for_output ();
3e43a32a
MS
7670 warning (_("Temporarily disabling breakpoints "
7671 "for unloaded shared library \"%s\""),
e2dd7057 7672 solib->so_name);
84acb35a 7673 }
e2dd7057 7674 disabled_shlib_breaks = 1;
84acb35a
JJ
7675 }
7676 }
84acb35a
JJ
7677}
7678
63644780
NB
7679/* Disable any breakpoints and tracepoints in OBJFILE upon
7680 notification of free_objfile. Only apply to enabled breakpoints,
7681 disabled ones can just stay disabled. */
7682
7683static void
7684disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7685{
7686 struct breakpoint *b;
7687
7688 if (objfile == NULL)
7689 return;
7690
d03de421
PA
7691 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7692 managed by the user with add-symbol-file/remove-symbol-file.
7693 Similarly to how breakpoints in shared libraries are handled in
7694 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7695 shlib_disabled so they end up uninserted on the next global
7696 location list update. Shared libraries not loaded by the user
7697 aren't handled here -- they're already handled in
7698 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7699 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7700 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7701 main objfile). */
7702 if ((objfile->flags & OBJF_SHARED) == 0
7703 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7704 return;
7705
7706 ALL_BREAKPOINTS (b)
7707 {
7708 struct bp_location *loc;
7709 int bp_modified = 0;
7710
7711 if (!is_breakpoint (b) && !is_tracepoint (b))
7712 continue;
7713
7714 for (loc = b->loc; loc != NULL; loc = loc->next)
7715 {
7716 CORE_ADDR loc_addr = loc->address;
7717
7718 if (loc->loc_type != bp_loc_hardware_breakpoint
7719 && loc->loc_type != bp_loc_software_breakpoint)
7720 continue;
7721
7722 if (loc->shlib_disabled != 0)
7723 continue;
7724
7725 if (objfile->pspace != loc->pspace)
7726 continue;
7727
7728 if (loc->loc_type != bp_loc_hardware_breakpoint
7729 && loc->loc_type != bp_loc_software_breakpoint)
7730 continue;
7731
7732 if (is_addr_in_objfile (loc_addr, objfile))
7733 {
7734 loc->shlib_disabled = 1;
08351840
PA
7735 /* At this point, we don't know whether the object was
7736 unmapped from the inferior or not, so leave the
7737 inserted flag alone. We'll handle failure to
7738 uninsert quietly, in case the object was indeed
7739 unmapped. */
63644780
NB
7740
7741 mark_breakpoint_location_modified (loc);
7742
7743 bp_modified = 1;
7744 }
7745 }
7746
7747 if (bp_modified)
7748 observer_notify_breakpoint_modified (b);
7749 }
7750}
7751
ce78b96d
JB
7752/* FORK & VFORK catchpoints. */
7753
e29a4733 7754/* An instance of this type is used to represent a fork or vfork
c1fc2657
SM
7755 catchpoint. A breakpoint is really of this type iff its ops pointer points
7756 to CATCH_FORK_BREAKPOINT_OPS. */
e29a4733 7757
c1fc2657 7758struct fork_catchpoint : public breakpoint
e29a4733 7759{
e29a4733
PA
7760 /* Process id of a child process whose forking triggered this
7761 catchpoint. This field is only valid immediately after this
7762 catchpoint has triggered. */
7763 ptid_t forked_inferior_pid;
7764};
7765
4a64f543
MS
7766/* Implement the "insert" breakpoint_ops method for fork
7767 catchpoints. */
ce78b96d 7768
77b06cd7
TJB
7769static int
7770insert_catch_fork (struct bp_location *bl)
ce78b96d 7771{
dfd4cc63 7772 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7773}
7774
4a64f543
MS
7775/* Implement the "remove" breakpoint_ops method for fork
7776 catchpoints. */
ce78b96d
JB
7777
7778static int
73971819 7779remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7780{
dfd4cc63 7781 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7782}
7783
7784/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7785 catchpoints. */
7786
7787static int
f1310107 7788breakpoint_hit_catch_fork (const struct bp_location *bl,
bd522513 7789 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7790 const struct target_waitstatus *ws)
ce78b96d 7791{
e29a4733
PA
7792 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7793
f90263c1
TT
7794 if (ws->kind != TARGET_WAITKIND_FORKED)
7795 return 0;
7796
7797 c->forked_inferior_pid = ws->value.related_pid;
7798 return 1;
ce78b96d
JB
7799}
7800
4a64f543
MS
7801/* Implement the "print_it" breakpoint_ops method for fork
7802 catchpoints. */
ce78b96d
JB
7803
7804static enum print_stop_action
348d480f 7805print_it_catch_fork (bpstat bs)
ce78b96d 7806{
36dfb11c 7807 struct ui_out *uiout = current_uiout;
348d480f
PA
7808 struct breakpoint *b = bs->breakpoint_at;
7809 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7810
ce78b96d 7811 annotate_catchpoint (b->number);
f303dbd6 7812 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7813 if (b->disposition == disp_del)
112e8700 7814 uiout->text ("Temporary catchpoint ");
36dfb11c 7815 else
112e8700
SM
7816 uiout->text ("Catchpoint ");
7817 if (uiout->is_mi_like_p ())
36dfb11c 7818 {
112e8700
SM
7819 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
7820 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7821 }
112e8700
SM
7822 uiout->field_int ("bkptno", b->number);
7823 uiout->text (" (forked process ");
7824 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7825 uiout->text ("), ");
ce78b96d
JB
7826 return PRINT_SRC_AND_LOC;
7827}
7828
4a64f543
MS
7829/* Implement the "print_one" breakpoint_ops method for fork
7830 catchpoints. */
ce78b96d
JB
7831
7832static void
a6d9a66e 7833print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7834{
e29a4733 7835 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7836 struct value_print_options opts;
79a45e25 7837 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7838
7839 get_user_print_options (&opts);
7840
4a64f543
MS
7841 /* Field 4, the address, is omitted (which makes the columns not
7842 line up too nicely with the headers, but the effect is relatively
7843 readable). */
79a45b7d 7844 if (opts.addressprint)
112e8700 7845 uiout->field_skip ("addr");
ce78b96d 7846 annotate_field (5);
112e8700 7847 uiout->text ("fork");
e29a4733 7848 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 7849 {
112e8700
SM
7850 uiout->text (", process ");
7851 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
7852 uiout->spaces (1);
ce78b96d 7853 }
8ac3646f 7854
112e8700
SM
7855 if (uiout->is_mi_like_p ())
7856 uiout->field_string ("catch-type", "fork");
ce78b96d
JB
7857}
7858
7859/* Implement the "print_mention" breakpoint_ops method for fork
7860 catchpoints. */
7861
7862static void
7863print_mention_catch_fork (struct breakpoint *b)
7864{
7865 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7866}
7867
6149aea9
PA
7868/* Implement the "print_recreate" breakpoint_ops method for fork
7869 catchpoints. */
7870
7871static void
7872print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7873{
7874 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7875 print_recreate_thread (b, fp);
6149aea9
PA
7876}
7877
ce78b96d
JB
7878/* The breakpoint_ops structure to be used in fork catchpoints. */
7879
2060206e 7880static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7881
4a64f543
MS
7882/* Implement the "insert" breakpoint_ops method for vfork
7883 catchpoints. */
ce78b96d 7884
77b06cd7
TJB
7885static int
7886insert_catch_vfork (struct bp_location *bl)
ce78b96d 7887{
dfd4cc63 7888 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7889}
7890
4a64f543
MS
7891/* Implement the "remove" breakpoint_ops method for vfork
7892 catchpoints. */
ce78b96d
JB
7893
7894static int
73971819 7895remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
ce78b96d 7896{
dfd4cc63 7897 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7898}
7899
7900/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7901 catchpoints. */
7902
7903static int
f1310107 7904breakpoint_hit_catch_vfork (const struct bp_location *bl,
bd522513 7905 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 7906 const struct target_waitstatus *ws)
ce78b96d 7907{
e29a4733
PA
7908 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7909
f90263c1
TT
7910 if (ws->kind != TARGET_WAITKIND_VFORKED)
7911 return 0;
7912
7913 c->forked_inferior_pid = ws->value.related_pid;
7914 return 1;
ce78b96d
JB
7915}
7916
4a64f543
MS
7917/* Implement the "print_it" breakpoint_ops method for vfork
7918 catchpoints. */
ce78b96d
JB
7919
7920static enum print_stop_action
348d480f 7921print_it_catch_vfork (bpstat bs)
ce78b96d 7922{
36dfb11c 7923 struct ui_out *uiout = current_uiout;
348d480f 7924 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7925 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7926
ce78b96d 7927 annotate_catchpoint (b->number);
f303dbd6 7928 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 7929 if (b->disposition == disp_del)
112e8700 7930 uiout->text ("Temporary catchpoint ");
36dfb11c 7931 else
112e8700
SM
7932 uiout->text ("Catchpoint ");
7933 if (uiout->is_mi_like_p ())
36dfb11c 7934 {
112e8700
SM
7935 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
7936 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 7937 }
112e8700
SM
7938 uiout->field_int ("bkptno", b->number);
7939 uiout->text (" (vforked process ");
7940 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
7941 uiout->text ("), ");
ce78b96d
JB
7942 return PRINT_SRC_AND_LOC;
7943}
7944
4a64f543
MS
7945/* Implement the "print_one" breakpoint_ops method for vfork
7946 catchpoints. */
ce78b96d
JB
7947
7948static void
a6d9a66e 7949print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7950{
e29a4733 7951 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7952 struct value_print_options opts;
79a45e25 7953 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7954
7955 get_user_print_options (&opts);
4a64f543
MS
7956 /* Field 4, the address, is omitted (which makes the columns not
7957 line up too nicely with the headers, but the effect is relatively
7958 readable). */
79a45b7d 7959 if (opts.addressprint)
112e8700 7960 uiout->field_skip ("addr");
ce78b96d 7961 annotate_field (5);
112e8700 7962 uiout->text ("vfork");
e29a4733 7963 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d 7964 {
112e8700
SM
7965 uiout->text (", process ");
7966 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
7967 uiout->spaces (1);
ce78b96d 7968 }
8ac3646f 7969
112e8700
SM
7970 if (uiout->is_mi_like_p ())
7971 uiout->field_string ("catch-type", "vfork");
ce78b96d
JB
7972}
7973
7974/* Implement the "print_mention" breakpoint_ops method for vfork
7975 catchpoints. */
7976
7977static void
7978print_mention_catch_vfork (struct breakpoint *b)
7979{
7980 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7981}
7982
6149aea9
PA
7983/* Implement the "print_recreate" breakpoint_ops method for vfork
7984 catchpoints. */
7985
7986static void
7987print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7988{
7989 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7990 print_recreate_thread (b, fp);
6149aea9
PA
7991}
7992
ce78b96d
JB
7993/* The breakpoint_ops structure to be used in vfork catchpoints. */
7994
2060206e 7995static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7996
edcc5120 7997/* An instance of this type is used to represent an solib catchpoint.
c1fc2657 7998 A breakpoint is really of this type iff its ops pointer points to
edcc5120
TT
7999 CATCH_SOLIB_BREAKPOINT_OPS. */
8000
c1fc2657 8001struct solib_catchpoint : public breakpoint
edcc5120 8002{
c1fc2657 8003 ~solib_catchpoint () override;
edcc5120
TT
8004
8005 /* True for "catch load", false for "catch unload". */
8006 unsigned char is_load;
8007
8008 /* Regular expression to match, if any. COMPILED is only valid when
8009 REGEX is non-NULL. */
8010 char *regex;
2d7cc5c7 8011 std::unique_ptr<compiled_regex> compiled;
edcc5120
TT
8012};
8013
c1fc2657 8014solib_catchpoint::~solib_catchpoint ()
edcc5120 8015{
c1fc2657 8016 xfree (this->regex);
edcc5120
TT
8017}
8018
8019static int
8020insert_catch_solib (struct bp_location *ignore)
8021{
8022 return 0;
8023}
8024
8025static int
73971819 8026remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
edcc5120
TT
8027{
8028 return 0;
8029}
8030
8031static int
8032breakpoint_hit_catch_solib (const struct bp_location *bl,
bd522513 8033 const address_space *aspace,
edcc5120
TT
8034 CORE_ADDR bp_addr,
8035 const struct target_waitstatus *ws)
8036{
8037 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8038 struct breakpoint *other;
8039
8040 if (ws->kind == TARGET_WAITKIND_LOADED)
8041 return 1;
8042
8043 ALL_BREAKPOINTS (other)
8044 {
8045 struct bp_location *other_bl;
8046
8047 if (other == bl->owner)
8048 continue;
8049
8050 if (other->type != bp_shlib_event)
8051 continue;
8052
c1fc2657 8053 if (self->pspace != NULL && other->pspace != self->pspace)
edcc5120
TT
8054 continue;
8055
8056 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8057 {
8058 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8059 return 1;
8060 }
8061 }
8062
8063 return 0;
8064}
8065
8066static void
8067check_status_catch_solib (struct bpstats *bs)
8068{
8069 struct solib_catchpoint *self
8070 = (struct solib_catchpoint *) bs->breakpoint_at;
8071 int ix;
8072
8073 if (self->is_load)
8074 {
8075 struct so_list *iter;
8076
8077 for (ix = 0;
8078 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8079 ix, iter);
8080 ++ix)
8081 {
8082 if (!self->regex
2d7cc5c7 8083 || self->compiled->exec (iter->so_name, 0, NULL, 0) == 0)
edcc5120
TT
8084 return;
8085 }
8086 }
8087 else
8088 {
8089 char *iter;
8090
8091 for (ix = 0;
8092 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8093 ix, iter);
8094 ++ix)
8095 {
8096 if (!self->regex
2d7cc5c7 8097 || self->compiled->exec (iter, 0, NULL, 0) == 0)
edcc5120
TT
8098 return;
8099 }
8100 }
8101
8102 bs->stop = 0;
8103 bs->print_it = print_it_noop;
8104}
8105
8106static enum print_stop_action
8107print_it_catch_solib (bpstat bs)
8108{
8109 struct breakpoint *b = bs->breakpoint_at;
8110 struct ui_out *uiout = current_uiout;
8111
8112 annotate_catchpoint (b->number);
f303dbd6 8113 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8114 if (b->disposition == disp_del)
112e8700 8115 uiout->text ("Temporary catchpoint ");
edcc5120 8116 else
112e8700
SM
8117 uiout->text ("Catchpoint ");
8118 uiout->field_int ("bkptno", b->number);
8119 uiout->text ("\n");
8120 if (uiout->is_mi_like_p ())
8121 uiout->field_string ("disp", bpdisp_text (b->disposition));
edcc5120
TT
8122 print_solib_event (1);
8123 return PRINT_SRC_AND_LOC;
8124}
8125
8126static void
8127print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8128{
8129 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8130 struct value_print_options opts;
8131 struct ui_out *uiout = current_uiout;
8132 char *msg;
8133
8134 get_user_print_options (&opts);
8135 /* Field 4, the address, is omitted (which makes the columns not
8136 line up too nicely with the headers, but the effect is relatively
8137 readable). */
8138 if (opts.addressprint)
8139 {
8140 annotate_field (4);
112e8700 8141 uiout->field_skip ("addr");
edcc5120
TT
8142 }
8143
8144 annotate_field (5);
8145 if (self->is_load)
8146 {
8147 if (self->regex)
8148 msg = xstrprintf (_("load of library matching %s"), self->regex);
8149 else
8150 msg = xstrdup (_("load of library"));
8151 }
8152 else
8153 {
8154 if (self->regex)
8155 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8156 else
8157 msg = xstrdup (_("unload of library"));
8158 }
112e8700 8159 uiout->field_string ("what", msg);
edcc5120 8160 xfree (msg);
8ac3646f 8161
112e8700
SM
8162 if (uiout->is_mi_like_p ())
8163 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
edcc5120
TT
8164}
8165
8166static void
8167print_mention_catch_solib (struct breakpoint *b)
8168{
8169 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8170
8171 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8172 self->is_load ? "load" : "unload");
8173}
8174
8175static void
8176print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8177{
8178 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8179
8180 fprintf_unfiltered (fp, "%s %s",
8181 b->disposition == disp_del ? "tcatch" : "catch",
8182 self->is_load ? "load" : "unload");
8183 if (self->regex)
8184 fprintf_unfiltered (fp, " %s", self->regex);
8185 fprintf_unfiltered (fp, "\n");
8186}
8187
8188static struct breakpoint_ops catch_solib_breakpoint_ops;
8189
91985142
MG
8190/* Shared helper function (MI and CLI) for creating and installing
8191 a shared object event catchpoint. If IS_LOAD is non-zero then
8192 the events to be caught are load events, otherwise they are
8193 unload events. If IS_TEMP is non-zero the catchpoint is a
8194 temporary one. If ENABLED is non-zero the catchpoint is
8195 created in an enabled state. */
edcc5120 8196
91985142 8197void
a121b7c1 8198add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
edcc5120 8199{
edcc5120 8200 struct gdbarch *gdbarch = get_current_arch ();
edcc5120 8201
edcc5120
TT
8202 if (!arg)
8203 arg = "";
f1735a53 8204 arg = skip_spaces (arg);
edcc5120 8205
36bd8eaa 8206 std::unique_ptr<solib_catchpoint> c (new solib_catchpoint ());
edcc5120
TT
8207
8208 if (*arg != '\0')
8209 {
2d7cc5c7
PA
8210 c->compiled.reset (new compiled_regex (arg, REG_NOSUB,
8211 _("Invalid regexp")));
edcc5120
TT
8212 c->regex = xstrdup (arg);
8213 }
8214
8215 c->is_load = is_load;
36bd8eaa 8216 init_catchpoint (c.get (), gdbarch, is_temp, NULL,
edcc5120
TT
8217 &catch_solib_breakpoint_ops);
8218
c1fc2657 8219 c->enable_state = enabled ? bp_enabled : bp_disabled;
91985142 8220
b270e6f9 8221 install_breakpoint (0, std::move (c), 1);
edcc5120
TT
8222}
8223
91985142
MG
8224/* A helper function that does all the work for "catch load" and
8225 "catch unload". */
8226
8227static void
eb4c3f4a 8228catch_load_or_unload (const char *arg, int from_tty, int is_load,
91985142
MG
8229 struct cmd_list_element *command)
8230{
8231 int tempflag;
8232 const int enabled = 1;
8233
8234 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8235
8236 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8237}
8238
edcc5120 8239static void
eb4c3f4a 8240catch_load_command_1 (const char *arg, int from_tty,
edcc5120
TT
8241 struct cmd_list_element *command)
8242{
8243 catch_load_or_unload (arg, from_tty, 1, command);
8244}
8245
8246static void
eb4c3f4a 8247catch_unload_command_1 (const char *arg, int from_tty,
edcc5120
TT
8248 struct cmd_list_element *command)
8249{
8250 catch_load_or_unload (arg, from_tty, 0, command);
8251}
8252
346774a9
PA
8253/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8254 is non-zero, then make the breakpoint temporary. If COND_STRING is
8255 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8256 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8257
ab04a2af 8258void
346774a9
PA
8259init_catchpoint (struct breakpoint *b,
8260 struct gdbarch *gdbarch, int tempflag,
63160a43 8261 const char *cond_string,
c0a91b2b 8262 const struct breakpoint_ops *ops)
c906108c 8263{
51abb421 8264 symtab_and_line sal;
6c95b8df 8265 sal.pspace = current_program_space;
c5aa993b 8266
28010a5d 8267 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8268
1b36a34b 8269 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8270 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8271}
8272
28010a5d 8273void
b270e6f9 8274install_breakpoint (int internal, std::unique_ptr<breakpoint> &&arg, int update_gll)
c56053d2 8275{
b270e6f9 8276 breakpoint *b = add_to_breakpoint_chain (std::move (arg));
3a5c3e22 8277 set_breakpoint_number (internal, b);
558a9d82
YQ
8278 if (is_tracepoint (b))
8279 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8280 if (!internal)
8281 mention (b);
c56053d2 8282 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8283
8284 if (update_gll)
44702360 8285 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8286}
8287
9b70b993 8288static void
a6d9a66e 8289create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
63160a43 8290 int tempflag, const char *cond_string,
c0a91b2b 8291 const struct breakpoint_ops *ops)
c906108c 8292{
b270e6f9 8293 std::unique_ptr<fork_catchpoint> c (new fork_catchpoint ());
ce78b96d 8294
b270e6f9 8295 init_catchpoint (c.get (), gdbarch, tempflag, cond_string, ops);
e29a4733
PA
8296
8297 c->forked_inferior_pid = null_ptid;
8298
b270e6f9 8299 install_breakpoint (0, std::move (c), 1);
c906108c
SS
8300}
8301
fe798b75
JB
8302/* Exec catchpoints. */
8303
b4d90040 8304/* An instance of this type is used to represent an exec catchpoint.
c1fc2657 8305 A breakpoint is really of this type iff its ops pointer points to
b4d90040
PA
8306 CATCH_EXEC_BREAKPOINT_OPS. */
8307
c1fc2657 8308struct exec_catchpoint : public breakpoint
b4d90040 8309{
c1fc2657 8310 ~exec_catchpoint () override;
b4d90040
PA
8311
8312 /* Filename of a program whose exec triggered this catchpoint.
8313 This field is only valid immediately after this catchpoint has
8314 triggered. */
8315 char *exec_pathname;
8316};
8317
c1fc2657 8318/* Exec catchpoint destructor. */
b4d90040 8319
c1fc2657 8320exec_catchpoint::~exec_catchpoint ()
b4d90040 8321{
c1fc2657 8322 xfree (this->exec_pathname);
b4d90040
PA
8323}
8324
77b06cd7
TJB
8325static int
8326insert_catch_exec (struct bp_location *bl)
c906108c 8327{
dfd4cc63 8328 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8329}
c906108c 8330
fe798b75 8331static int
73971819 8332remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
fe798b75 8333{
dfd4cc63 8334 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8335}
c906108c 8336
fe798b75 8337static int
f1310107 8338breakpoint_hit_catch_exec (const struct bp_location *bl,
bd522513 8339 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 8340 const struct target_waitstatus *ws)
fe798b75 8341{
b4d90040
PA
8342 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8343
f90263c1
TT
8344 if (ws->kind != TARGET_WAITKIND_EXECD)
8345 return 0;
8346
8347 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8348 return 1;
fe798b75 8349}
c906108c 8350
fe798b75 8351static enum print_stop_action
348d480f 8352print_it_catch_exec (bpstat bs)
fe798b75 8353{
36dfb11c 8354 struct ui_out *uiout = current_uiout;
348d480f 8355 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8356 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8357
fe798b75 8358 annotate_catchpoint (b->number);
f303dbd6 8359 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8360 if (b->disposition == disp_del)
112e8700 8361 uiout->text ("Temporary catchpoint ");
36dfb11c 8362 else
112e8700
SM
8363 uiout->text ("Catchpoint ");
8364 if (uiout->is_mi_like_p ())
36dfb11c 8365 {
112e8700
SM
8366 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8367 uiout->field_string ("disp", bpdisp_text (b->disposition));
36dfb11c 8368 }
112e8700
SM
8369 uiout->field_int ("bkptno", b->number);
8370 uiout->text (" (exec'd ");
8371 uiout->field_string ("new-exec", c->exec_pathname);
8372 uiout->text ("), ");
36dfb11c 8373
fe798b75 8374 return PRINT_SRC_AND_LOC;
c906108c
SS
8375}
8376
fe798b75 8377static void
a6d9a66e 8378print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8379{
b4d90040 8380 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8381 struct value_print_options opts;
79a45e25 8382 struct ui_out *uiout = current_uiout;
fe798b75
JB
8383
8384 get_user_print_options (&opts);
8385
8386 /* Field 4, the address, is omitted (which makes the columns
8387 not line up too nicely with the headers, but the effect
8388 is relatively readable). */
8389 if (opts.addressprint)
112e8700 8390 uiout->field_skip ("addr");
fe798b75 8391 annotate_field (5);
112e8700 8392 uiout->text ("exec");
b4d90040 8393 if (c->exec_pathname != NULL)
fe798b75 8394 {
112e8700
SM
8395 uiout->text (", program \"");
8396 uiout->field_string ("what", c->exec_pathname);
8397 uiout->text ("\" ");
fe798b75 8398 }
8ac3646f 8399
112e8700
SM
8400 if (uiout->is_mi_like_p ())
8401 uiout->field_string ("catch-type", "exec");
fe798b75
JB
8402}
8403
8404static void
8405print_mention_catch_exec (struct breakpoint *b)
8406{
8407 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8408}
8409
6149aea9
PA
8410/* Implement the "print_recreate" breakpoint_ops method for exec
8411 catchpoints. */
8412
8413static void
8414print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8415{
8416 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8417 print_recreate_thread (b, fp);
6149aea9
PA
8418}
8419
2060206e 8420static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8421
c906108c 8422static int
fba45db2 8423hw_breakpoint_used_count (void)
c906108c 8424{
c906108c 8425 int i = 0;
f1310107
TJB
8426 struct breakpoint *b;
8427 struct bp_location *bl;
c906108c
SS
8428
8429 ALL_BREAKPOINTS (b)
c5aa993b 8430 {
d6b74ac4 8431 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8432 for (bl = b->loc; bl; bl = bl->next)
8433 {
8434 /* Special types of hardware breakpoints may use more than
8435 one register. */
348d480f 8436 i += b->ops->resources_needed (bl);
f1310107 8437 }
c5aa993b 8438 }
c906108c
SS
8439
8440 return i;
8441}
8442
a1398e0c
PA
8443/* Returns the resources B would use if it were a hardware
8444 watchpoint. */
8445
c906108c 8446static int
a1398e0c 8447hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8448{
c906108c 8449 int i = 0;
e09342b5 8450 struct bp_location *bl;
c906108c 8451
a1398e0c
PA
8452 if (!breakpoint_enabled (b))
8453 return 0;
8454
8455 for (bl = b->loc; bl; bl = bl->next)
8456 {
8457 /* Special types of hardware watchpoints may use more than
8458 one register. */
8459 i += b->ops->resources_needed (bl);
8460 }
8461
8462 return i;
8463}
8464
8465/* Returns the sum the used resources of all hardware watchpoints of
8466 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8467 the sum of the used resources of all hardware watchpoints of other
8468 types _not_ TYPE. */
8469
8470static int
8471hw_watchpoint_used_count_others (struct breakpoint *except,
8472 enum bptype type, int *other_type_used)
8473{
8474 int i = 0;
8475 struct breakpoint *b;
8476
c906108c
SS
8477 *other_type_used = 0;
8478 ALL_BREAKPOINTS (b)
e09342b5 8479 {
a1398e0c
PA
8480 if (b == except)
8481 continue;
e09342b5
TJB
8482 if (!breakpoint_enabled (b))
8483 continue;
8484
a1398e0c
PA
8485 if (b->type == type)
8486 i += hw_watchpoint_use_count (b);
8487 else if (is_hardware_watchpoint (b))
8488 *other_type_used = 1;
e09342b5
TJB
8489 }
8490
c906108c
SS
8491 return i;
8492}
8493
c906108c 8494void
fba45db2 8495disable_watchpoints_before_interactive_call_start (void)
c906108c 8496{
c5aa993b 8497 struct breakpoint *b;
c906108c
SS
8498
8499 ALL_BREAKPOINTS (b)
c5aa993b 8500 {
cc60f2e3 8501 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8502 {
b5de0fa7 8503 b->enable_state = bp_call_disabled;
44702360 8504 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8505 }
8506 }
c906108c
SS
8507}
8508
8509void
fba45db2 8510enable_watchpoints_after_interactive_call_stop (void)
c906108c 8511{
c5aa993b 8512 struct breakpoint *b;
c906108c
SS
8513
8514 ALL_BREAKPOINTS (b)
c5aa993b 8515 {
cc60f2e3 8516 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8517 {
b5de0fa7 8518 b->enable_state = bp_enabled;
44702360 8519 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8520 }
8521 }
c906108c
SS
8522}
8523
8bea4e01
UW
8524void
8525disable_breakpoints_before_startup (void)
8526{
6c95b8df 8527 current_program_space->executing_startup = 1;
44702360 8528 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8529}
8530
8531void
8532enable_breakpoints_after_startup (void)
8533{
6c95b8df 8534 current_program_space->executing_startup = 0;
f8eba3c6 8535 breakpoint_re_set ();
8bea4e01
UW
8536}
8537
7c16b83e
PA
8538/* Create a new single-step breakpoint for thread THREAD, with no
8539 locations. */
c906108c 8540
7c16b83e
PA
8541static struct breakpoint *
8542new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8543{
b270e6f9 8544 std::unique_ptr<breakpoint> b (new breakpoint ());
7c16b83e 8545
b270e6f9 8546 init_raw_breakpoint_without_location (b.get (), gdbarch, bp_single_step,
7c16b83e
PA
8547 &momentary_breakpoint_ops);
8548
8549 b->disposition = disp_donttouch;
8550 b->frame_id = null_frame_id;
8551
8552 b->thread = thread;
8553 gdb_assert (b->thread != 0);
8554
b270e6f9 8555 return add_to_breakpoint_chain (std::move (b));
7c16b83e
PA
8556}
8557
8558/* Set a momentary breakpoint of type TYPE at address specified by
8559 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8560 frame. */
c906108c 8561
454dafbd 8562breakpoint_up
a6d9a66e
UW
8563set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8564 struct frame_id frame_id, enum bptype type)
c906108c 8565{
52f0bd74 8566 struct breakpoint *b;
edb3359d 8567
193facb3
JK
8568 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8569 tail-called one. */
8570 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8571
06edf0c0 8572 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8573 b->enable_state = bp_enabled;
8574 b->disposition = disp_donttouch;
818dd999 8575 b->frame_id = frame_id;
c906108c 8576
4a64f543
MS
8577 /* If we're debugging a multi-threaded program, then we want
8578 momentary breakpoints to be active in only a single thread of
8579 control. */
39f77062 8580 if (in_thread_list (inferior_ptid))
5d5658a1 8581 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8582
44702360 8583 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8584
454dafbd 8585 return breakpoint_up (b);
c906108c 8586}
611c83ae 8587
06edf0c0 8588/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8589 The new breakpoint will have type TYPE, use OPS as its
8590 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8591
06edf0c0
PA
8592static struct breakpoint *
8593momentary_breakpoint_from_master (struct breakpoint *orig,
8594 enum bptype type,
a1aa2221
LM
8595 const struct breakpoint_ops *ops,
8596 int loc_enabled)
e58b0e63
PA
8597{
8598 struct breakpoint *copy;
8599
06edf0c0 8600 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8601 copy->loc = allocate_bp_location (copy);
0e30163f 8602 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8603
a6d9a66e 8604 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8605 copy->loc->requested_address = orig->loc->requested_address;
8606 copy->loc->address = orig->loc->address;
8607 copy->loc->section = orig->loc->section;
6c95b8df 8608 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8609 copy->loc->probe = orig->loc->probe;
f8eba3c6 8610 copy->loc->line_number = orig->loc->line_number;
2f202fde 8611 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8612 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8613 copy->frame_id = orig->frame_id;
8614 copy->thread = orig->thread;
6c95b8df 8615 copy->pspace = orig->pspace;
e58b0e63
PA
8616
8617 copy->enable_state = bp_enabled;
8618 copy->disposition = disp_donttouch;
8619 copy->number = internal_breakpoint_number--;
8620
44702360 8621 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8622 return copy;
8623}
8624
06edf0c0
PA
8625/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8626 ORIG is NULL. */
8627
8628struct breakpoint *
8629clone_momentary_breakpoint (struct breakpoint *orig)
8630{
8631 /* If there's nothing to clone, then return nothing. */
8632 if (orig == NULL)
8633 return NULL;
8634
a1aa2221 8635 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8636}
8637
454dafbd 8638breakpoint_up
a6d9a66e
UW
8639set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8640 enum bptype type)
611c83ae
PA
8641{
8642 struct symtab_and_line sal;
8643
8644 sal = find_pc_line (pc, 0);
8645 sal.pc = pc;
8646 sal.section = find_pc_overlay (pc);
8647 sal.explicit_pc = 1;
8648
a6d9a66e 8649 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8650}
c906108c 8651\f
c5aa993b 8652
c906108c
SS
8653/* Tell the user we have just set a breakpoint B. */
8654
8655static void
fba45db2 8656mention (struct breakpoint *b)
c906108c 8657{
348d480f 8658 b->ops->print_mention (b);
112e8700 8659 if (current_uiout->is_mi_like_p ())
fb40c209 8660 return;
c906108c
SS
8661 printf_filtered ("\n");
8662}
c906108c 8663\f
c5aa993b 8664
1a853c52
PA
8665static int bp_loc_is_permanent (struct bp_location *loc);
8666
0d381245 8667static struct bp_location *
39d61571 8668add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8669 const struct symtab_and_line *sal)
8670{
8671 struct bp_location *loc, **tmp;
3742cc8b
YQ
8672 CORE_ADDR adjusted_address;
8673 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8674
8675 if (loc_gdbarch == NULL)
8676 loc_gdbarch = b->gdbarch;
8677
8678 /* Adjust the breakpoint's address prior to allocating a location.
8679 Once we call allocate_bp_location(), that mostly uninitialized
8680 location will be placed on the location chain. Adjustment of the
8681 breakpoint may cause target_read_memory() to be called and we do
8682 not want its scan of the location chain to find a breakpoint and
8683 location that's only been partially initialized. */
8684 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8685 sal->pc, b->type);
0d381245 8686
d30113d4 8687 /* Sort the locations by their ADDRESS. */
39d61571 8688 loc = allocate_bp_location (b);
d30113d4
JK
8689 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8690 tmp = &((*tmp)->next))
0d381245 8691 ;
d30113d4 8692 loc->next = *tmp;
0d381245 8693 *tmp = loc;
3742cc8b 8694
0d381245 8695 loc->requested_address = sal->pc;
3742cc8b 8696 loc->address = adjusted_address;
6c95b8df 8697 loc->pspace = sal->pspace;
729662a5
TT
8698 loc->probe.probe = sal->probe;
8699 loc->probe.objfile = sal->objfile;
6c95b8df 8700 gdb_assert (loc->pspace != NULL);
0d381245 8701 loc->section = sal->section;
3742cc8b 8702 loc->gdbarch = loc_gdbarch;
f8eba3c6 8703 loc->line_number = sal->line;
2f202fde 8704 loc->symtab = sal->symtab;
4a27f119 8705 loc->symbol = sal->symbol;
f8eba3c6 8706
0e30163f
JK
8707 set_breakpoint_location_function (loc,
8708 sal->explicit_pc || sal->explicit_line);
1a853c52 8709
6ae88661
LM
8710 /* While by definition, permanent breakpoints are already present in the
8711 code, we don't mark the location as inserted. Normally one would expect
8712 that GDB could rely on that breakpoint instruction to stop the program,
8713 thus removing the need to insert its own breakpoint, except that executing
8714 the breakpoint instruction can kill the target instead of reporting a
8715 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8716 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8717 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8718 breakpoint be inserted normally results in QEMU knowing about the GDB
8719 breakpoint, and thus trap before the breakpoint instruction is executed.
8720 (If GDB later needs to continue execution past the permanent breakpoint,
8721 it manually increments the PC, thus avoiding executing the breakpoint
8722 instruction.) */
1a853c52 8723 if (bp_loc_is_permanent (loc))
6ae88661 8724 loc->permanent = 1;
1a853c52 8725
0d381245
VP
8726 return loc;
8727}
514f746b
AR
8728\f
8729
1cf4d951 8730/* See breakpoint.h. */
514f746b 8731
1cf4d951
PA
8732int
8733program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8734{
8735 int len;
8736 CORE_ADDR addr;
1afeeb75 8737 const gdb_byte *bpoint;
514f746b
AR
8738 gdb_byte *target_mem;
8739
1cf4d951
PA
8740 addr = address;
8741 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8742
8743 /* Software breakpoints unsupported? */
8744 if (bpoint == NULL)
8745 return 0;
8746
224c3ddb 8747 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
8748
8749 /* Enable the automatic memory restoration from breakpoints while
8750 we read the memory. Otherwise we could say about our temporary
8751 breakpoints they are permanent. */
cb85b21b
TT
8752 scoped_restore restore_memory
8753 = make_scoped_restore_show_memory_breakpoints (0);
1cf4d951
PA
8754
8755 if (target_read_memory (address, target_mem, len) == 0
8756 && memcmp (target_mem, bpoint, len) == 0)
cb85b21b 8757 return 1;
1cf4d951 8758
cb85b21b 8759 return 0;
1cf4d951
PA
8760}
8761
8762/* Return 1 if LOC is pointing to a permanent breakpoint,
8763 return 0 otherwise. */
8764
8765static int
8766bp_loc_is_permanent (struct bp_location *loc)
8767{
514f746b
AR
8768 gdb_assert (loc != NULL);
8769
244558af
LM
8770 /* If we have a catchpoint or a watchpoint, just return 0. We should not
8771 attempt to read from the addresses the locations of these breakpoint types
8772 point to. program_breakpoint_here_p, below, will attempt to read
8773 memory. */
8774 if (!breakpoint_address_is_meaningful (loc->owner))
8775 return 0;
8776
5ed8105e 8777 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 8778 switch_to_program_space_and_thread (loc->pspace);
5ed8105e 8779 return program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b
AR
8780}
8781
e7e0cddf
SS
8782/* Build a command list for the dprintf corresponding to the current
8783 settings of the dprintf style options. */
8784
8785static void
8786update_dprintf_command_list (struct breakpoint *b)
8787{
8788 char *dprintf_args = b->extra_string;
8789 char *printf_line = NULL;
8790
8791 if (!dprintf_args)
8792 return;
8793
8794 dprintf_args = skip_spaces (dprintf_args);
8795
8796 /* Allow a comma, as it may have terminated a location, but don't
8797 insist on it. */
8798 if (*dprintf_args == ',')
8799 ++dprintf_args;
8800 dprintf_args = skip_spaces (dprintf_args);
8801
8802 if (*dprintf_args != '"')
8803 error (_("Bad format string, missing '\"'."));
8804
d3ce09f5 8805 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8806 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8807 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8808 {
8809 if (!dprintf_function)
8810 error (_("No function supplied for dprintf call"));
8811
8812 if (dprintf_channel && strlen (dprintf_channel) > 0)
8813 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8814 dprintf_function,
8815 dprintf_channel,
8816 dprintf_args);
8817 else
8818 printf_line = xstrprintf ("call (void) %s (%s)",
8819 dprintf_function,
8820 dprintf_args);
8821 }
d3ce09f5
SS
8822 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8823 {
8824 if (target_can_run_breakpoint_commands ())
8825 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8826 else
8827 {
8828 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8829 printf_line = xstrprintf ("printf %s", dprintf_args);
8830 }
8831 }
e7e0cddf
SS
8832 else
8833 internal_error (__FILE__, __LINE__,
8834 _("Invalid dprintf style."));
8835
f28045c2 8836 gdb_assert (printf_line != NULL);
9d6e6e84 8837 /* Manufacture a printf sequence. */
f28045c2 8838 {
8d749320 8839 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 8840
f28045c2
YQ
8841 printf_cmd_line->control_type = simple_control;
8842 printf_cmd_line->body_count = 0;
8843 printf_cmd_line->body_list = NULL;
9d6e6e84 8844 printf_cmd_line->next = NULL;
f28045c2 8845 printf_cmd_line->line = printf_line;
e7e0cddf 8846
93921405 8847 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
f28045c2 8848 }
e7e0cddf
SS
8849}
8850
8851/* Update all dprintf commands, making their command lists reflect
8852 current style settings. */
8853
8854static void
eb4c3f4a 8855update_dprintf_commands (const char *args, int from_tty,
e7e0cddf
SS
8856 struct cmd_list_element *c)
8857{
8858 struct breakpoint *b;
8859
8860 ALL_BREAKPOINTS (b)
8861 {
8862 if (b->type == bp_dprintf)
8863 update_dprintf_command_list (b);
8864 }
8865}
c3f6f71d 8866
f00aae0f
KS
8867/* Create a breakpoint with SAL as location. Use LOCATION
8868 as a description of the location, and COND_STRING
b35a8b2f
DE
8869 as condition expression. If LOCATION is NULL then create an
8870 "address location" from the address in the SAL. */
018d34a4
VP
8871
8872static void
d9b3f62e 8873init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
6c5b2ebe 8874 gdb::array_view<const symtab_and_line> sals,
ffc2605c 8875 event_location_up &&location,
e1e01040
PA
8876 gdb::unique_xmalloc_ptr<char> filter,
8877 gdb::unique_xmalloc_ptr<char> cond_string,
8878 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
8879 enum bptype type, enum bpdisp disposition,
8880 int thread, int task, int ignore_count,
c0a91b2b 8881 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8882 int enabled, int internal, unsigned flags,
8883 int display_canonical)
018d34a4 8884{
0d381245 8885 int i;
018d34a4
VP
8886
8887 if (type == bp_hardware_breakpoint)
8888 {
fbbd034e
AS
8889 int target_resources_ok;
8890
8891 i = hw_breakpoint_used_count ();
8892 target_resources_ok =
8893 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8894 i + 1, 0);
8895 if (target_resources_ok == 0)
8896 error (_("No hardware breakpoint support in the target."));
8897 else if (target_resources_ok < 0)
8898 error (_("Hardware breakpoints used exceeds limit."));
8899 }
8900
6c5b2ebe 8901 gdb_assert (!sals.empty ());
6c95b8df 8902
6c5b2ebe 8903 for (const auto &sal : sals)
0d381245 8904 {
0d381245
VP
8905 struct bp_location *loc;
8906
8907 if (from_tty)
5af949e3
UW
8908 {
8909 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8910 if (!loc_gdbarch)
8911 loc_gdbarch = gdbarch;
8912
8913 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8914 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8915 }
0d381245 8916
6c5b2ebe 8917 if (&sal == &sals[0])
0d381245 8918 {
d9b3f62e 8919 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8920 b->thread = thread;
4a306c9a 8921 b->task = task;
855a6e68 8922
e1e01040
PA
8923 b->cond_string = cond_string.release ();
8924 b->extra_string = extra_string.release ();
0d381245 8925 b->ignore_count = ignore_count;
41447f92 8926 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8927 b->disposition = disposition;
6c95b8df 8928
44f238bb
PA
8929 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8930 b->loc->inserted = 1;
8931
0fb4aa4b
PA
8932 if (type == bp_static_tracepoint)
8933 {
d9b3f62e 8934 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8935 struct static_tracepoint_marker marker;
8936
983af33b 8937 if (strace_marker_p (b))
0fb4aa4b
PA
8938 {
8939 /* We already know the marker exists, otherwise, we
8940 wouldn't see a sal for it. */
d28cd78a
TT
8941 const char *p
8942 = &event_location_to_string (b->location.get ())[3];
f00aae0f 8943 const char *endp;
0fb4aa4b 8944 char *marker_str;
0fb4aa4b 8945
f1735a53 8946 p = skip_spaces (p);
0fb4aa4b 8947
f1735a53 8948 endp = skip_to_space (p);
0fb4aa4b
PA
8949
8950 marker_str = savestring (p, endp - p);
d9b3f62e 8951 t->static_trace_marker_id = marker_str;
0fb4aa4b 8952
3e43a32a
MS
8953 printf_filtered (_("Probed static tracepoint "
8954 "marker \"%s\"\n"),
d9b3f62e 8955 t->static_trace_marker_id);
0fb4aa4b
PA
8956 }
8957 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8958 {
d9b3f62e 8959 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
8960 release_static_tracepoint_marker (&marker);
8961
3e43a32a
MS
8962 printf_filtered (_("Probed static tracepoint "
8963 "marker \"%s\"\n"),
d9b3f62e 8964 t->static_trace_marker_id);
0fb4aa4b
PA
8965 }
8966 else
3e43a32a
MS
8967 warning (_("Couldn't determine the static "
8968 "tracepoint marker to probe"));
0fb4aa4b
PA
8969 }
8970
0d381245
VP
8971 loc = b->loc;
8972 }
8973 else
018d34a4 8974 {
39d61571 8975 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8976 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8977 loc->inserted = 1;
0d381245
VP
8978 }
8979
8980 if (b->cond_string)
8981 {
bbc13ae3
KS
8982 const char *arg = b->cond_string;
8983
1bb9788d
TT
8984 loc->cond = parse_exp_1 (&arg, loc->address,
8985 block_for_pc (loc->address), 0);
0d381245 8986 if (*arg)
588ae58c 8987 error (_("Garbage '%s' follows condition"), arg);
018d34a4 8988 }
e7e0cddf
SS
8989
8990 /* Dynamic printf requires and uses additional arguments on the
8991 command line, otherwise it's an error. */
8992 if (type == bp_dprintf)
8993 {
8994 if (b->extra_string)
8995 update_dprintf_command_list (b);
8996 else
8997 error (_("Format string required"));
8998 }
8999 else if (b->extra_string)
588ae58c 9000 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9001 }
018d34a4 9002
56435ebe 9003 b->display_canonical = display_canonical;
f00aae0f 9004 if (location != NULL)
d28cd78a 9005 b->location = std::move (location);
018d34a4 9006 else
d28cd78a 9007 b->location = new_address_location (b->loc->address, NULL, 0);
e1e01040 9008 b->filter = filter.release ();
d9b3f62e 9009}
018d34a4 9010
d9b3f62e
PA
9011static void
9012create_breakpoint_sal (struct gdbarch *gdbarch,
6c5b2ebe 9013 gdb::array_view<const symtab_and_line> sals,
ffc2605c 9014 event_location_up &&location,
e1e01040
PA
9015 gdb::unique_xmalloc_ptr<char> filter,
9016 gdb::unique_xmalloc_ptr<char> cond_string,
9017 gdb::unique_xmalloc_ptr<char> extra_string,
d9b3f62e
PA
9018 enum bptype type, enum bpdisp disposition,
9019 int thread, int task, int ignore_count,
c0a91b2b 9020 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9021 int enabled, int internal, unsigned flags,
9022 int display_canonical)
d9b3f62e 9023{
a5e364af 9024 std::unique_ptr<breakpoint> b = new_breakpoint_from_type (type);
d9b3f62e 9025
a5e364af 9026 init_breakpoint_sal (b.get (), gdbarch,
ffc2605c 9027 sals, std::move (location),
e1e01040
PA
9028 std::move (filter),
9029 std::move (cond_string),
9030 std::move (extra_string),
d9b3f62e
PA
9031 type, disposition,
9032 thread, task, ignore_count,
9033 ops, from_tty,
44f238bb
PA
9034 enabled, internal, flags,
9035 display_canonical);
d9b3f62e 9036
b270e6f9 9037 install_breakpoint (internal, std::move (b), 0);
018d34a4
VP
9038}
9039
9040/* Add SALS.nelts breakpoints to the breakpoint table. For each
9041 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9042 value. COND_STRING, if not NULL, specified the condition to be
9043 used for all breakpoints. Essentially the only case where
9044 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9045 function. In that case, it's still not possible to specify
9046 separate conditions for different overloaded functions, so
9047 we take just a single condition string.
9048
c3f6f71d 9049 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9050 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9051 array contents). If the function fails (error() is called), the
9052 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9053 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9054
9055static void
8cdf0e15 9056create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9057 struct linespec_result *canonical,
e1e01040
PA
9058 gdb::unique_xmalloc_ptr<char> cond_string,
9059 gdb::unique_xmalloc_ptr<char> extra_string,
8cdf0e15
VP
9060 enum bptype type, enum bpdisp disposition,
9061 int thread, int task, int ignore_count,
c0a91b2b 9062 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9063 int enabled, int internal, unsigned flags)
c906108c 9064{
f8eba3c6 9065 if (canonical->pre_expanded)
6c5b2ebe 9066 gdb_assert (canonical->lsals.size () == 1);
f8eba3c6 9067
6c5b2ebe 9068 for (const auto &lsal : canonical->lsals)
c3f6f71d 9069 {
f00aae0f 9070 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9071 'break', without arguments. */
ffc2605c 9072 event_location_up location
f00aae0f 9073 = (canonical->location != NULL
8e9e35b1 9074 ? copy_event_location (canonical->location.get ()) : NULL);
e1e01040 9075 gdb::unique_xmalloc_ptr<char> filter_string
6c5b2ebe 9076 (lsal.canonical != NULL ? xstrdup (lsal.canonical) : NULL);
0d381245 9077
6c5b2ebe 9078 create_breakpoint_sal (gdbarch, lsal.sals,
ffc2605c 9079 std::move (location),
e1e01040
PA
9080 std::move (filter_string),
9081 std::move (cond_string),
9082 std::move (extra_string),
e7e0cddf 9083 type, disposition,
84f4c1fe 9084 thread, task, ignore_count, ops,
44f238bb 9085 from_tty, enabled, internal, flags,
56435ebe 9086 canonical->special_display);
c3f6f71d 9087 }
c3f6f71d 9088}
c906108c 9089
f00aae0f 9090/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9091 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9092 addresses found. LOCATION points to the end of the SAL (for
9093 linespec locations).
9998af43
TJB
9094
9095 The array and the line spec strings are allocated on the heap, it is
9096 the caller's responsibility to free them. */
c906108c 9097
b9362cc7 9098static void
f00aae0f 9099parse_breakpoint_sals (const struct event_location *location,
58438ac1 9100 struct linespec_result *canonical)
c3f6f71d 9101{
f00aae0f
KS
9102 struct symtab_and_line cursal;
9103
9104 if (event_location_type (location) == LINESPEC_LOCATION)
9105 {
9106 const char *address = get_linespec_location (location);
9107
9108 if (address == NULL)
9109 {
9110 /* The last displayed codepoint, if it's valid, is our default
9111 breakpoint address. */
9112 if (last_displayed_sal_is_valid ())
9113 {
f00aae0f
KS
9114 /* Set sal's pspace, pc, symtab, and line to the values
9115 corresponding to the last call to print_frame_info.
9116 Be sure to reinitialize LINE with NOTCURRENT == 0
9117 as the breakpoint line number is inappropriate otherwise.
9118 find_pc_line would adjust PC, re-set it back. */
51abb421
PA
9119 symtab_and_line sal = get_last_displayed_sal ();
9120 CORE_ADDR pc = sal.pc;
9121
f00aae0f
KS
9122 sal = find_pc_line (pc, 0);
9123
9124 /* "break" without arguments is equivalent to "break *PC"
9125 where PC is the last displayed codepoint's address. So
9126 make sure to set sal.explicit_pc to prevent GDB from
9127 trying to expand the list of sals to include all other
9128 instances with the same symtab and line. */
9129 sal.pc = pc;
9130 sal.explicit_pc = 1;
9131
6c5b2ebe
PA
9132 struct linespec_sals lsal;
9133 lsal.sals = {sal};
f00aae0f
KS
9134 lsal.canonical = NULL;
9135
6c5b2ebe 9136 canonical->lsals.push_back (std::move (lsal));
f00aae0f
KS
9137 return;
9138 }
9139 else
9140 error (_("No default breakpoint address now."));
c906108c 9141 }
c906108c 9142 }
f00aae0f
KS
9143
9144 /* Force almost all breakpoints to be in terms of the
9145 current_source_symtab (which is decode_line_1's default).
9146 This should produce the results we want almost all of the
9147 time while leaving default_breakpoint_* alone.
9148
9149 ObjC: However, don't match an Objective-C method name which
9150 may have a '+' or '-' succeeded by a '['. */
9151 cursal = get_current_source_symtab_and_line ();
9152 if (last_displayed_sal_is_valid ())
c906108c 9153 {
f00aae0f 9154 const char *address = NULL;
cc80f267 9155
f00aae0f
KS
9156 if (event_location_type (location) == LINESPEC_LOCATION)
9157 address = get_linespec_location (location);
cc80f267 9158
f00aae0f
KS
9159 if (!cursal.symtab
9160 || (address != NULL
9161 && strchr ("+-", address[0]) != NULL
9162 && address[1] != '['))
9163 {
c2f4122d 9164 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f
KS
9165 get_last_displayed_symtab (),
9166 get_last_displayed_line (),
9167 canonical, NULL, NULL);
9168 return;
9169 }
c906108c 9170 }
f00aae0f 9171
c2f4122d 9172 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
f00aae0f 9173 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9174}
c906108c 9175
c906108c 9176
c3f6f71d 9177/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9178 inserted as a breakpoint. If it can't throw an error. */
c906108c 9179
b9362cc7 9180static void
6c5b2ebe 9181breakpoint_sals_to_pc (std::vector<symtab_and_line> &sals)
c3f6f71d 9182{
6c5b2ebe
PA
9183 for (auto &sal : sals)
9184 resolve_sal_pc (&sal);
c3f6f71d
JM
9185}
9186
7a697b8d
SS
9187/* Fast tracepoints may have restrictions on valid locations. For
9188 instance, a fast tracepoint using a jump instead of a trap will
9189 likely have to overwrite more bytes than a trap would, and so can
9190 only be placed where the instruction is longer than the jump, or a
9191 multi-instruction sequence does not have a jump into the middle of
9192 it, etc. */
9193
9194static void
9195check_fast_tracepoint_sals (struct gdbarch *gdbarch,
6c5b2ebe 9196 gdb::array_view<const symtab_and_line> sals)
7a697b8d 9197{
6c5b2ebe 9198 int rslt;
7a697b8d
SS
9199 char *msg;
9200 struct cleanup *old_chain;
9201
6c5b2ebe 9202 for (const auto &sal : sals)
7a697b8d 9203 {
f8eba3c6
TT
9204 struct gdbarch *sarch;
9205
6c5b2ebe 9206 sarch = get_sal_arch (sal);
f8eba3c6
TT
9207 /* We fall back to GDBARCH if there is no architecture
9208 associated with SAL. */
9209 if (sarch == NULL)
9210 sarch = gdbarch;
6c5b2ebe 9211 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal.pc, &msg);
7a697b8d
SS
9212 old_chain = make_cleanup (xfree, msg);
9213
9214 if (!rslt)
53c3572a 9215 error (_("May not have a fast tracepoint at %s%s"),
6c5b2ebe 9216 paddress (sarch, sal.pc), (msg ? msg : ""));
7a697b8d
SS
9217
9218 do_cleanups (old_chain);
9219 }
9220}
9221
018d34a4
VP
9222/* Given TOK, a string specification of condition and thread, as
9223 accepted by the 'break' command, extract the condition
9224 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9225 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9226 If no condition is found, *COND_STRING is set to NULL.
9227 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9228
9229static void
bbc13ae3 9230find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9231 char **cond_string, int *thread, int *task,
9232 char **rest)
018d34a4
VP
9233{
9234 *cond_string = NULL;
9235 *thread = -1;
ed1d1739
KS
9236 *task = 0;
9237 *rest = NULL;
9238
018d34a4
VP
9239 while (tok && *tok)
9240 {
bbc13ae3 9241 const char *end_tok;
018d34a4 9242 int toklen;
bbc13ae3
KS
9243 const char *cond_start = NULL;
9244 const char *cond_end = NULL;
cc59ec59 9245
f1735a53 9246 tok = skip_spaces (tok);
e7e0cddf
SS
9247
9248 if ((*tok == '"' || *tok == ',') && rest)
9249 {
9250 *rest = savestring (tok, strlen (tok));
9251 return;
9252 }
9253
f1735a53 9254 end_tok = skip_to_space (tok);
d634f2de 9255
018d34a4 9256 toklen = end_tok - tok;
d634f2de 9257
018d34a4
VP
9258 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9259 {
9260 tok = cond_start = end_tok + 1;
4d01a485 9261 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
018d34a4 9262 cond_end = tok;
d634f2de 9263 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9264 }
9265 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9266 {
5d5658a1
PA
9267 const char *tmptok;
9268 struct thread_info *thr;
d634f2de 9269
018d34a4 9270 tok = end_tok + 1;
5d5658a1 9271 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9272 if (tok == tmptok)
9273 error (_("Junk after thread keyword."));
5d5658a1 9274 *thread = thr->global_num;
bbc13ae3 9275 tok = tmptok;
018d34a4 9276 }
4a306c9a
JB
9277 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9278 {
9279 char *tmptok;
9280
9281 tok = end_tok + 1;
bbc13ae3 9282 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9283 if (tok == tmptok)
9284 error (_("Junk after task keyword."));
9285 if (!valid_task_id (*task))
b6199126 9286 error (_("Unknown task %d."), *task);
bbc13ae3 9287 tok = tmptok;
4a306c9a 9288 }
e7e0cddf
SS
9289 else if (rest)
9290 {
9291 *rest = savestring (tok, strlen (tok));
ccab2054 9292 return;
e7e0cddf 9293 }
018d34a4
VP
9294 else
9295 error (_("Junk at end of arguments."));
9296 }
9297}
9298
0fb4aa4b
PA
9299/* Decode a static tracepoint marker spec. */
9300
6c5b2ebe 9301static std::vector<symtab_and_line>
f00aae0f 9302decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9303{
9304 VEC(static_tracepoint_marker_p) *markers = NULL;
f00aae0f
KS
9305 const char *p = &(*arg_p)[3];
9306 const char *endp;
0fb4aa4b
PA
9307 int i;
9308
f1735a53 9309 p = skip_spaces (p);
0fb4aa4b 9310
f1735a53 9311 endp = skip_to_space (p);
0fb4aa4b 9312
81b1e71c 9313 std::string marker_str (p, endp - p);
0fb4aa4b 9314
81b1e71c 9315 markers = target_static_tracepoint_markers_by_strid (marker_str.c_str ());
0fb4aa4b 9316 if (VEC_empty(static_tracepoint_marker_p, markers))
81b1e71c
TT
9317 error (_("No known static tracepoint marker named %s"),
9318 marker_str.c_str ());
0fb4aa4b 9319
6c5b2ebe
PA
9320 std::vector<symtab_and_line> sals;
9321 sals.reserve (VEC_length(static_tracepoint_marker_p, markers));
0fb4aa4b 9322
6c5b2ebe 9323 for (i = 0; i < VEC_length(static_tracepoint_marker_p, markers); i++)
0fb4aa4b
PA
9324 {
9325 struct static_tracepoint_marker *marker;
9326
9327 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9328
51abb421 9329 symtab_and_line sal = find_pc_line (marker->address, 0);
6c5b2ebe
PA
9330 sal.pc = marker->address;
9331 sals.push_back (sal);
0fb4aa4b
PA
9332
9333 release_static_tracepoint_marker (marker);
9334 }
9335
0fb4aa4b
PA
9336 *arg_p = endp;
9337 return sals;
9338}
9339
f00aae0f 9340/* See breakpoint.h. */
0101ce28 9341
8cdf0e15
VP
9342int
9343create_breakpoint (struct gdbarch *gdbarch,
e1e01040
PA
9344 const struct event_location *location,
9345 const char *cond_string,
9346 int thread, const char *extra_string,
f00aae0f 9347 int parse_extra,
0fb4aa4b 9348 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9349 int ignore_count,
9350 enum auto_boolean pending_break_support,
c0a91b2b 9351 const struct breakpoint_ops *ops,
44f238bb
PA
9352 int from_tty, int enabled, int internal,
9353 unsigned flags)
c3f6f71d 9354{
7efd8fc2 9355 struct linespec_result canonical;
80c99de1 9356 struct cleanup *bkpt_chain = NULL;
0101ce28 9357 int pending = 0;
4a306c9a 9358 int task = 0;
86b17b60 9359 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9360
348d480f
PA
9361 gdb_assert (ops != NULL);
9362
f00aae0f
KS
9363 /* If extra_string isn't useful, set it to NULL. */
9364 if (extra_string != NULL && *extra_string == '\0')
9365 extra_string = NULL;
9366
492d29ea 9367 TRY
b78a6381 9368 {
f00aae0f 9369 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9370 }
492d29ea 9371 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9372 {
492d29ea
PA
9373 /* If caller is interested in rc value from parse, set
9374 value. */
9375 if (e.error == NOT_FOUND_ERROR)
0101ce28 9376 {
05ff989b
AC
9377 /* If pending breakpoint support is turned off, throw
9378 error. */
fa8d40ab
JJ
9379
9380 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9381 throw_exception (e);
9382
9383 exception_print (gdb_stderr, e);
fa8d40ab 9384
05ff989b
AC
9385 /* If pending breakpoint support is auto query and the user
9386 selects no, then simply return the error code. */
059fb39f 9387 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9388 && !nquery (_("Make %s pending on future shared library load? "),
9389 bptype_string (type_wanted)))
fd9b8c24 9390 return 0;
fa8d40ab 9391
05ff989b
AC
9392 /* At this point, either the user was queried about setting
9393 a pending breakpoint and selected yes, or pending
9394 breakpoint behavior is on and thus a pending breakpoint
9395 is defaulted on behalf of the user. */
f00aae0f 9396 pending = 1;
0101ce28 9397 }
492d29ea
PA
9398 else
9399 throw_exception (e);
0101ce28 9400 }
492d29ea
PA
9401 END_CATCH
9402
6c5b2ebe 9403 if (!pending && canonical.lsals.empty ())
492d29ea 9404 return 0;
c3f6f71d 9405
c3f6f71d
JM
9406 /* ----------------------------- SNIP -----------------------------
9407 Anything added to the cleanup chain beyond this point is assumed
9408 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9409 then the memory is not reclaimed. */
9410 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9411
c3f6f71d
JM
9412 /* Resolve all line numbers to PC's and verify that the addresses
9413 are ok for the target. */
0101ce28 9414 if (!pending)
f8eba3c6 9415 {
6c5b2ebe
PA
9416 for (auto &lsal : canonical.lsals)
9417 breakpoint_sals_to_pc (lsal.sals);
f8eba3c6 9418 }
c3f6f71d 9419
7a697b8d 9420 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9421 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6 9422 {
6c5b2ebe
PA
9423 for (const auto &lsal : canonical.lsals)
9424 check_fast_tracepoint_sals (gdbarch, lsal.sals);
f8eba3c6 9425 }
7a697b8d 9426
c3f6f71d
JM
9427 /* Verify that condition can be parsed, before setting any
9428 breakpoints. Allocate a separate condition expression for each
4a64f543 9429 breakpoint. */
0101ce28 9430 if (!pending)
c3f6f71d 9431 {
e1e01040
PA
9432 gdb::unique_xmalloc_ptr<char> cond_string_copy;
9433 gdb::unique_xmalloc_ptr<char> extra_string_copy;
9434
f00aae0f 9435 if (parse_extra)
72b2ff0e 9436 {
0878d0fa 9437 char *rest;
e1e01040 9438 char *cond;
52d361e1 9439
6c5b2ebe 9440 const linespec_sals &lsal = canonical.lsals[0];
52d361e1 9441
0878d0fa
YQ
9442 /* Here we only parse 'arg' to separate condition
9443 from thread number, so parsing in context of first
9444 sal is OK. When setting the breakpoint we'll
9445 re-parse it in context of each sal. */
9446
6c5b2ebe 9447 find_condition_and_thread (extra_string, lsal.sals[0].pc,
e1e01040
PA
9448 &cond, &thread, &task, &rest);
9449 cond_string_copy.reset (cond);
9450 extra_string_copy.reset (rest);
72b2ff0e 9451 }
2f069f6f 9452 else
72b2ff0e 9453 {
f00aae0f
KS
9454 if (type_wanted != bp_dprintf
9455 && extra_string != NULL && *extra_string != '\0')
9456 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9457
9458 /* Create a private copy of condition string. */
9459 if (cond_string)
e1e01040 9460 cond_string_copy.reset (xstrdup (cond_string));
0878d0fa
YQ
9461 /* Create a private copy of any extra string. */
9462 if (extra_string)
e1e01040 9463 extra_string_copy.reset (xstrdup (extra_string));
72b2ff0e 9464 }
0fb4aa4b 9465
52d361e1 9466 ops->create_breakpoints_sal (gdbarch, &canonical,
e1e01040
PA
9467 std::move (cond_string_copy),
9468 std::move (extra_string_copy),
9469 type_wanted,
d9b3f62e
PA
9470 tempflag ? disp_del : disp_donttouch,
9471 thread, task, ignore_count, ops,
44f238bb 9472 from_tty, enabled, internal, flags);
c906108c 9473 }
0101ce28
JJ
9474 else
9475 {
a5e364af 9476 std::unique_ptr <breakpoint> b = new_breakpoint_from_type (type_wanted);
bfccc43c 9477
a5e364af 9478 init_raw_breakpoint_without_location (b.get (), gdbarch, type_wanted, ops);
d28cd78a 9479 b->location = copy_event_location (location);
bfccc43c 9480
f00aae0f
KS
9481 if (parse_extra)
9482 b->cond_string = NULL;
e12c7713
MK
9483 else
9484 {
9485 /* Create a private copy of condition string. */
e1e01040 9486 b->cond_string = cond_string != NULL ? xstrdup (cond_string) : NULL;
15630549 9487 b->thread = thread;
e12c7713 9488 }
f00aae0f
KS
9489
9490 /* Create a private copy of any extra string. */
e1e01040 9491 b->extra_string = extra_string != NULL ? xstrdup (extra_string) : NULL;
0101ce28 9492 b->ignore_count = ignore_count;
0101ce28 9493 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9494 b->condition_not_parsed = 1;
41447f92 9495 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9496 if ((type_wanted != bp_breakpoint
9497 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9498 b->pspace = current_program_space;
8bea4e01 9499
b270e6f9 9500 install_breakpoint (internal, std::move (b), 0);
0101ce28
JJ
9501 }
9502
6c5b2ebe 9503 if (canonical.lsals.size () > 1)
95a42b64 9504 {
3e43a32a
MS
9505 warning (_("Multiple breakpoints were set.\nUse the "
9506 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9507 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9508 }
9509
80c99de1
PA
9510 /* That's it. Discard the cleanups for data inserted into the
9511 breakpoint. */
9512 discard_cleanups (bkpt_chain);
217dc9e2 9513
80c99de1 9514 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9515 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9516
9517 return 1;
c3f6f71d 9518}
c906108c 9519
348d480f 9520/* Set a breakpoint.
72b2ff0e
VP
9521 ARG is a string describing breakpoint address,
9522 condition, and thread.
9523 FLAG specifies if a breakpoint is hardware on,
9524 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9525 and BP_TEMPFLAG. */
348d480f 9526
98deb0da 9527static void
f2fc3015 9528break_command_1 (const char *arg, int flag, int from_tty)
c3f6f71d 9529{
72b2ff0e 9530 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9531 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9532 ? bp_hardware_breakpoint
9533 : bp_breakpoint);
55aa24fb 9534 struct breakpoint_ops *ops;
f00aae0f 9535
ffc2605c 9536 event_location_up location = string_to_event_location (&arg, current_language);
55aa24fb
SDJ
9537
9538 /* Matching breakpoints on probes. */
5b56227b 9539 if (location != NULL
ffc2605c 9540 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
9541 ops = &bkpt_probe_breakpoint_ops;
9542 else
9543 ops = &bkpt_breakpoint_ops;
c3f6f71d 9544
8cdf0e15 9545 create_breakpoint (get_current_arch (),
ffc2605c 9546 location.get (),
f00aae0f 9547 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9548 tempflag, type_wanted,
8cdf0e15
VP
9549 0 /* Ignore count */,
9550 pending_break_support,
55aa24fb 9551 ops,
8cdf0e15 9552 from_tty,
84f4c1fe 9553 1 /* enabled */,
44f238bb
PA
9554 0 /* internal */,
9555 0);
c906108c
SS
9556}
9557
c906108c
SS
9558/* Helper function for break_command_1 and disassemble_command. */
9559
9560void
fba45db2 9561resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9562{
9563 CORE_ADDR pc;
9564
9565 if (sal->pc == 0 && sal->symtab != NULL)
9566 {
9567 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9568 error (_("No line %d in file \"%s\"."),
05cba821 9569 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9570 sal->pc = pc;
6a048695 9571
4a64f543
MS
9572 /* If this SAL corresponds to a breakpoint inserted using a line
9573 number, then skip the function prologue if necessary. */
6a048695 9574 if (sal->explicit_line)
059acae7 9575 skip_prologue_sal (sal);
c906108c
SS
9576 }
9577
9578 if (sal->section == 0 && sal->symtab != NULL)
9579 {
346d1dfe 9580 const struct blockvector *bv;
3977b71f 9581 const struct block *b;
c5aa993b 9582 struct symbol *sym;
c906108c 9583
43f3e411
DE
9584 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9585 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9586 if (bv != NULL)
9587 {
7f0df278 9588 sym = block_linkage_function (b);
c906108c
SS
9589 if (sym != NULL)
9590 {
eb822aa6
DE
9591 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9592 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9593 sym);
c906108c
SS
9594 }
9595 else
9596 {
4a64f543
MS
9597 /* It really is worthwhile to have the section, so we'll
9598 just have to look harder. This case can be executed
9599 if we have line numbers but no functions (as can
9600 happen in assembly source). */
c906108c 9601
5ed8105e 9602 scoped_restore_current_pspace_and_thread restore_pspace_thread;
6c95b8df 9603 switch_to_program_space_and_thread (sal->pspace);
c906108c 9604
5ed8105e 9605 bound_minimal_symbol msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9606 if (msym.minsym)
efd66ac6 9607 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
c906108c
SS
9608 }
9609 }
9610 }
9611}
9612
9613void
0b39b52e 9614break_command (const char *arg, int from_tty)
c906108c 9615{
db107f19 9616 break_command_1 (arg, 0, from_tty);
c906108c
SS
9617}
9618
c906108c 9619void
0b39b52e 9620tbreak_command (const char *arg, int from_tty)
c906108c 9621{
db107f19 9622 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9623}
9624
c906108c 9625static void
0b39b52e 9626hbreak_command (const char *arg, int from_tty)
c906108c 9627{
db107f19 9628 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9629}
9630
9631static void
0b39b52e 9632thbreak_command (const char *arg, int from_tty)
c906108c 9633{
db107f19 9634 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9635}
9636
9637static void
ee7ddd71 9638stop_command (const char *arg, int from_tty)
c906108c 9639{
a3f17187 9640 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9641Usage: stop in <function | address>\n\
a3f17187 9642 stop at <line>\n"));
c906108c
SS
9643}
9644
9645static void
4495129a 9646stopin_command (const char *arg, int from_tty)
c906108c
SS
9647{
9648 int badInput = 0;
9649
c5aa993b 9650 if (arg == (char *) NULL)
c906108c
SS
9651 badInput = 1;
9652 else if (*arg != '*')
9653 {
4495129a 9654 const char *argptr = arg;
c906108c
SS
9655 int hasColon = 0;
9656
4a64f543 9657 /* Look for a ':'. If this is a line number specification, then
53a5351d 9658 say it is bad, otherwise, it should be an address or
4a64f543 9659 function/method name. */
c906108c 9660 while (*argptr && !hasColon)
c5aa993b
JM
9661 {
9662 hasColon = (*argptr == ':');
9663 argptr++;
9664 }
c906108c
SS
9665
9666 if (hasColon)
c5aa993b 9667 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9668 else
c5aa993b 9669 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9670 }
9671
9672 if (badInput)
a3f17187 9673 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9674 else
db107f19 9675 break_command_1 (arg, 0, from_tty);
c906108c
SS
9676}
9677
9678static void
4495129a 9679stopat_command (const char *arg, int from_tty)
c906108c
SS
9680{
9681 int badInput = 0;
9682
c5aa993b 9683 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9684 badInput = 1;
9685 else
9686 {
4495129a 9687 const char *argptr = arg;
c906108c
SS
9688 int hasColon = 0;
9689
4a64f543
MS
9690 /* Look for a ':'. If there is a '::' then get out, otherwise
9691 it is probably a line number. */
c906108c 9692 while (*argptr && !hasColon)
c5aa993b
JM
9693 {
9694 hasColon = (*argptr == ':');
9695 argptr++;
9696 }
c906108c
SS
9697
9698 if (hasColon)
c5aa993b 9699 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9700 else
c5aa993b 9701 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9702 }
9703
9704 if (badInput)
a3f17187 9705 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 9706 else
db107f19 9707 break_command_1 (arg, 0, from_tty);
c906108c
SS
9708}
9709
e7e0cddf
SS
9710/* The dynamic printf command is mostly like a regular breakpoint, but
9711 with a prewired command list consisting of a single output command,
9712 built from extra arguments supplied on the dprintf command
9713 line. */
9714
da821c7b 9715static void
0b39b52e 9716dprintf_command (const char *arg, int from_tty)
e7e0cddf 9717{
ffc2605c 9718 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f
KS
9719
9720 /* If non-NULL, ARG should have been advanced past the location;
9721 the next character must be ','. */
9722 if (arg != NULL)
9723 {
9724 if (arg[0] != ',' || arg[1] == '\0')
9725 error (_("Format string required"));
9726 else
9727 {
9728 /* Skip the comma. */
9729 ++arg;
9730 }
9731 }
9732
e7e0cddf 9733 create_breakpoint (get_current_arch (),
ffc2605c 9734 location.get (),
f00aae0f 9735 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
9736 0, bp_dprintf,
9737 0 /* Ignore count */,
9738 pending_break_support,
9739 &dprintf_breakpoint_ops,
9740 from_tty,
9741 1 /* enabled */,
9742 0 /* internal */,
9743 0);
9744}
9745
d3ce09f5 9746static void
0b39b52e 9747agent_printf_command (const char *arg, int from_tty)
d3ce09f5
SS
9748{
9749 error (_("May only run agent-printf on the target"));
9750}
9751
f1310107
TJB
9752/* Implement the "breakpoint_hit" breakpoint_ops method for
9753 ranged breakpoints. */
9754
9755static int
9756breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
bd522513 9757 const address_space *aspace,
09ac7c10
TT
9758 CORE_ADDR bp_addr,
9759 const struct target_waitstatus *ws)
f1310107 9760{
09ac7c10 9761 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9762 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9763 return 0;
9764
f1310107
TJB
9765 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9766 bl->length, aspace, bp_addr);
9767}
9768
9769/* Implement the "resources_needed" breakpoint_ops method for
9770 ranged breakpoints. */
9771
9772static int
9773resources_needed_ranged_breakpoint (const struct bp_location *bl)
9774{
9775 return target_ranged_break_num_registers ();
9776}
9777
9778/* Implement the "print_it" breakpoint_ops method for
9779 ranged breakpoints. */
9780
9781static enum print_stop_action
348d480f 9782print_it_ranged_breakpoint (bpstat bs)
f1310107 9783{
348d480f 9784 struct breakpoint *b = bs->breakpoint_at;
f1310107 9785 struct bp_location *bl = b->loc;
79a45e25 9786 struct ui_out *uiout = current_uiout;
f1310107
TJB
9787
9788 gdb_assert (b->type == bp_hardware_breakpoint);
9789
9790 /* Ranged breakpoints have only one location. */
9791 gdb_assert (bl && bl->next == NULL);
9792
9793 annotate_breakpoint (b->number);
f303dbd6
PA
9794
9795 maybe_print_thread_hit_breakpoint (uiout);
9796
f1310107 9797 if (b->disposition == disp_del)
112e8700 9798 uiout->text ("Temporary ranged breakpoint ");
f1310107 9799 else
112e8700
SM
9800 uiout->text ("Ranged breakpoint ");
9801 if (uiout->is_mi_like_p ())
f1310107 9802 {
112e8700 9803 uiout->field_string ("reason",
f1310107 9804 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 9805 uiout->field_string ("disp", bpdisp_text (b->disposition));
f1310107 9806 }
112e8700
SM
9807 uiout->field_int ("bkptno", b->number);
9808 uiout->text (", ");
f1310107
TJB
9809
9810 return PRINT_SRC_AND_LOC;
9811}
9812
9813/* Implement the "print_one" breakpoint_ops method for
9814 ranged breakpoints. */
9815
9816static void
9817print_one_ranged_breakpoint (struct breakpoint *b,
9818 struct bp_location **last_loc)
9819{
9820 struct bp_location *bl = b->loc;
9821 struct value_print_options opts;
79a45e25 9822 struct ui_out *uiout = current_uiout;
f1310107
TJB
9823
9824 /* Ranged breakpoints have only one location. */
9825 gdb_assert (bl && bl->next == NULL);
9826
9827 get_user_print_options (&opts);
9828
9829 if (opts.addressprint)
9830 /* We don't print the address range here, it will be printed later
9831 by print_one_detail_ranged_breakpoint. */
112e8700 9832 uiout->field_skip ("addr");
f1310107
TJB
9833 annotate_field (5);
9834 print_breakpoint_location (b, bl);
9835 *last_loc = bl;
9836}
9837
9838/* Implement the "print_one_detail" breakpoint_ops method for
9839 ranged breakpoints. */
9840
9841static void
9842print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9843 struct ui_out *uiout)
9844{
9845 CORE_ADDR address_start, address_end;
9846 struct bp_location *bl = b->loc;
d7e74731 9847 string_file stb;
f1310107
TJB
9848
9849 gdb_assert (bl);
9850
9851 address_start = bl->address;
9852 address_end = address_start + bl->length - 1;
9853
112e8700 9854 uiout->text ("\taddress range: ");
d7e74731
PA
9855 stb.printf ("[%s, %s]",
9856 print_core_address (bl->gdbarch, address_start),
9857 print_core_address (bl->gdbarch, address_end));
112e8700
SM
9858 uiout->field_stream ("addr", stb);
9859 uiout->text ("\n");
f1310107
TJB
9860}
9861
9862/* Implement the "print_mention" breakpoint_ops method for
9863 ranged breakpoints. */
9864
9865static void
9866print_mention_ranged_breakpoint (struct breakpoint *b)
9867{
9868 struct bp_location *bl = b->loc;
79a45e25 9869 struct ui_out *uiout = current_uiout;
f1310107
TJB
9870
9871 gdb_assert (bl);
9872 gdb_assert (b->type == bp_hardware_breakpoint);
9873
112e8700 9874 if (uiout->is_mi_like_p ())
f1310107
TJB
9875 return;
9876
9877 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9878 b->number, paddress (bl->gdbarch, bl->address),
9879 paddress (bl->gdbarch, bl->address + bl->length - 1));
9880}
9881
9882/* Implement the "print_recreate" breakpoint_ops method for
9883 ranged breakpoints. */
9884
9885static void
9886print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9887{
f00aae0f 9888 fprintf_unfiltered (fp, "break-range %s, %s",
d28cd78a
TT
9889 event_location_to_string (b->location.get ()),
9890 event_location_to_string (b->location_range_end.get ()));
d9b3f62e 9891 print_recreate_thread (b, fp);
f1310107
TJB
9892}
9893
9894/* The breakpoint_ops structure to be used in ranged breakpoints. */
9895
2060206e 9896static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9897
9898/* Find the address where the end of the breakpoint range should be
9899 placed, given the SAL of the end of the range. This is so that if
9900 the user provides a line number, the end of the range is set to the
9901 last instruction of the given line. */
9902
9903static CORE_ADDR
9904find_breakpoint_range_end (struct symtab_and_line sal)
9905{
9906 CORE_ADDR end;
9907
9908 /* If the user provided a PC value, use it. Otherwise,
9909 find the address of the end of the given location. */
9910 if (sal.explicit_pc)
9911 end = sal.pc;
9912 else
9913 {
9914 int ret;
9915 CORE_ADDR start;
9916
9917 ret = find_line_pc_range (sal, &start, &end);
9918 if (!ret)
9919 error (_("Could not find location of the end of the range."));
9920
9921 /* find_line_pc_range returns the start of the next line. */
9922 end--;
9923 }
9924
9925 return end;
9926}
9927
9928/* Implement the "break-range" CLI command. */
9929
9930static void
0b39b52e 9931break_range_command (const char *arg, int from_tty)
f1310107 9932{
f2fc3015 9933 const char *arg_start;
f1310107
TJB
9934 struct linespec_result canonical_start, canonical_end;
9935 int bp_count, can_use_bp, length;
9936 CORE_ADDR end;
9937 struct breakpoint *b;
f1310107
TJB
9938
9939 /* We don't support software ranged breakpoints. */
9940 if (target_ranged_break_num_registers () < 0)
9941 error (_("This target does not support hardware ranged breakpoints."));
9942
9943 bp_count = hw_breakpoint_used_count ();
9944 bp_count += target_ranged_break_num_registers ();
9945 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9946 bp_count, 0);
9947 if (can_use_bp < 0)
9948 error (_("Hardware breakpoints used exceeds limit."));
9949
f8eba3c6 9950 arg = skip_spaces (arg);
f1310107
TJB
9951 if (arg == NULL || arg[0] == '\0')
9952 error(_("No address range specified."));
9953
f8eba3c6 9954 arg_start = arg;
ffc2605c
TT
9955 event_location_up start_location = string_to_event_location (&arg,
9956 current_language);
9957 parse_breakpoint_sals (start_location.get (), &canonical_start);
f1310107
TJB
9958
9959 if (arg[0] != ',')
9960 error (_("Too few arguments."));
6c5b2ebe 9961 else if (canonical_start.lsals.empty ())
f1310107 9962 error (_("Could not find location of the beginning of the range."));
f8eba3c6 9963
6c5b2ebe 9964 const linespec_sals &lsal_start = canonical_start.lsals[0];
f8eba3c6 9965
6c5b2ebe
PA
9966 if (canonical_start.lsals.size () > 1
9967 || lsal_start.sals.size () != 1)
f1310107
TJB
9968 error (_("Cannot create a ranged breakpoint with multiple locations."));
9969
6c5b2ebe 9970 const symtab_and_line &sal_start = lsal_start.sals[0];
81b1e71c 9971 std::string addr_string_start (arg_start, arg - arg_start);
f1310107
TJB
9972
9973 arg++; /* Skip the comma. */
f8eba3c6 9974 arg = skip_spaces (arg);
f1310107
TJB
9975
9976 /* Parse the end location. */
9977
f1310107
TJB
9978 arg_start = arg;
9979
f8eba3c6 9980 /* We call decode_line_full directly here instead of using
f1310107
TJB
9981 parse_breakpoint_sals because we need to specify the start location's
9982 symtab and line as the default symtab and line for the end of the
9983 range. This makes it possible to have ranges like "foo.c:27, +14",
9984 where +14 means 14 lines from the start location. */
ffc2605c
TT
9985 event_location_up end_location = string_to_event_location (&arg,
9986 current_language);
9987 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
f8eba3c6
TT
9988 sal_start.symtab, sal_start.line,
9989 &canonical_end, NULL, NULL);
9990
6c5b2ebe 9991 if (canonical_end.lsals.empty ())
f1310107 9992 error (_("Could not find location of the end of the range."));
f8eba3c6 9993
6c5b2ebe
PA
9994 const linespec_sals &lsal_end = canonical_end.lsals[0];
9995 if (canonical_end.lsals.size () > 1
9996 || lsal_end.sals.size () != 1)
f1310107
TJB
9997 error (_("Cannot create a ranged breakpoint with multiple locations."));
9998
6c5b2ebe 9999 const symtab_and_line &sal_end = lsal_end.sals[0];
f1310107
TJB
10000
10001 end = find_breakpoint_range_end (sal_end);
10002 if (sal_start.pc > end)
177b42fe 10003 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10004
10005 length = end - sal_start.pc + 1;
10006 if (length < 0)
10007 /* Length overflowed. */
10008 error (_("Address range too large."));
10009 else if (length == 1)
10010 {
10011 /* This range is simple enough to be handled by
10012 the `hbreak' command. */
81b1e71c 10013 hbreak_command (&addr_string_start[0], 1);
f1310107
TJB
10014
10015 return;
10016 }
10017
10018 /* Now set up the breakpoint. */
10019 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10020 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10021 set_breakpoint_count (breakpoint_count + 1);
10022 b->number = breakpoint_count;
10023 b->disposition = disp_donttouch;
d28cd78a
TT
10024 b->location = std::move (start_location);
10025 b->location_range_end = std::move (end_location);
f1310107
TJB
10026 b->loc->length = length;
10027
f1310107 10028 mention (b);
8d3788bd 10029 observer_notify_breakpoint_created (b);
44702360 10030 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10031}
10032
4a64f543
MS
10033/* Return non-zero if EXP is verified as constant. Returned zero
10034 means EXP is variable. Also the constant detection may fail for
10035 some constant expressions and in such case still falsely return
10036 zero. */
2e6e3d9c 10037
65d79d4b
SDJ
10038static int
10039watchpoint_exp_is_const (const struct expression *exp)
10040{
10041 int i = exp->nelts;
10042
10043 while (i > 0)
10044 {
10045 int oplenp, argsp;
10046
10047 /* We are only interested in the descriptor of each element. */
10048 operator_length (exp, i, &oplenp, &argsp);
10049 i -= oplenp;
10050
10051 switch (exp->elts[i].opcode)
10052 {
10053 case BINOP_ADD:
10054 case BINOP_SUB:
10055 case BINOP_MUL:
10056 case BINOP_DIV:
10057 case BINOP_REM:
10058 case BINOP_MOD:
10059 case BINOP_LSH:
10060 case BINOP_RSH:
10061 case BINOP_LOGICAL_AND:
10062 case BINOP_LOGICAL_OR:
10063 case BINOP_BITWISE_AND:
10064 case BINOP_BITWISE_IOR:
10065 case BINOP_BITWISE_XOR:
10066 case BINOP_EQUAL:
10067 case BINOP_NOTEQUAL:
10068 case BINOP_LESS:
10069 case BINOP_GTR:
10070 case BINOP_LEQ:
10071 case BINOP_GEQ:
10072 case BINOP_REPEAT:
10073 case BINOP_COMMA:
10074 case BINOP_EXP:
10075 case BINOP_MIN:
10076 case BINOP_MAX:
10077 case BINOP_INTDIV:
10078 case BINOP_CONCAT:
65d79d4b
SDJ
10079 case TERNOP_COND:
10080 case TERNOP_SLICE:
65d79d4b
SDJ
10081
10082 case OP_LONG:
edd079d9 10083 case OP_FLOAT:
65d79d4b
SDJ
10084 case OP_LAST:
10085 case OP_COMPLEX:
10086 case OP_STRING:
65d79d4b
SDJ
10087 case OP_ARRAY:
10088 case OP_TYPE:
608b4967
TT
10089 case OP_TYPEOF:
10090 case OP_DECLTYPE:
6e72ca20 10091 case OP_TYPEID:
65d79d4b
SDJ
10092 case OP_NAME:
10093 case OP_OBJC_NSSTRING:
10094
10095 case UNOP_NEG:
10096 case UNOP_LOGICAL_NOT:
10097 case UNOP_COMPLEMENT:
10098 case UNOP_ADDR:
10099 case UNOP_HIGH:
aeaa2474 10100 case UNOP_CAST:
9eaf6705
TT
10101
10102 case UNOP_CAST_TYPE:
10103 case UNOP_REINTERPRET_CAST:
10104 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10105 /* Unary, binary and ternary operators: We have to check
10106 their operands. If they are constant, then so is the
10107 result of that operation. For instance, if A and B are
10108 determined to be constants, then so is "A + B".
10109
10110 UNOP_IND is one exception to the rule above, because the
10111 value of *ADDR is not necessarily a constant, even when
10112 ADDR is. */
65d79d4b
SDJ
10113 break;
10114
10115 case OP_VAR_VALUE:
10116 /* Check whether the associated symbol is a constant.
4a64f543 10117
65d79d4b 10118 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10119 possible that a buggy compiler could mark a variable as
10120 constant even when it is not, and TYPE_CONST would return
10121 true in this case, while SYMBOL_CLASS wouldn't.
10122
10123 We also have to check for function symbols because they
10124 are always constant. */
65d79d4b
SDJ
10125 {
10126 struct symbol *s = exp->elts[i + 2].symbol;
10127
10128 if (SYMBOL_CLASS (s) != LOC_BLOCK
10129 && SYMBOL_CLASS (s) != LOC_CONST
10130 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10131 return 0;
10132 break;
10133 }
10134
10135 /* The default action is to return 0 because we are using
10136 the optimistic approach here: If we don't know something,
10137 then it is not a constant. */
10138 default:
10139 return 0;
10140 }
10141 }
10142
10143 return 1;
10144}
10145
c1fc2657 10146/* Watchpoint destructor. */
3a5c3e22 10147
c1fc2657 10148watchpoint::~watchpoint ()
3a5c3e22 10149{
c1fc2657
SM
10150 xfree (this->exp_string);
10151 xfree (this->exp_string_reparse);
10152 value_free (this->val);
3a5c3e22
PA
10153}
10154
348d480f
PA
10155/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10156
10157static void
10158re_set_watchpoint (struct breakpoint *b)
10159{
3a5c3e22
PA
10160 struct watchpoint *w = (struct watchpoint *) b;
10161
348d480f
PA
10162 /* Watchpoint can be either on expression using entirely global
10163 variables, or it can be on local variables.
10164
10165 Watchpoints of the first kind are never auto-deleted, and even
10166 persist across program restarts. Since they can use variables
10167 from shared libraries, we need to reparse expression as libraries
10168 are loaded and unloaded.
10169
10170 Watchpoints on local variables can also change meaning as result
10171 of solib event. For example, if a watchpoint uses both a local
10172 and a global variables in expression, it's a local watchpoint,
10173 but unloading of a shared library will make the expression
10174 invalid. This is not a very common use case, but we still
10175 re-evaluate expression, to avoid surprises to the user.
10176
10177 Note that for local watchpoints, we re-evaluate it only if
10178 watchpoints frame id is still valid. If it's not, it means the
10179 watchpoint is out of scope and will be deleted soon. In fact,
10180 I'm not sure we'll ever be called in this case.
10181
10182 If a local watchpoint's frame id is still valid, then
3a5c3e22 10183 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10184
3a5c3e22
PA
10185 Don't do anything about disabled watchpoints, since they will be
10186 reevaluated again when enabled. */
10187 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10188}
10189
77b06cd7
TJB
10190/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10191
10192static int
10193insert_watchpoint (struct bp_location *bl)
10194{
3a5c3e22
PA
10195 struct watchpoint *w = (struct watchpoint *) bl->owner;
10196 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10197
10198 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10199 w->cond_exp.get ());
77b06cd7
TJB
10200}
10201
10202/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10203
10204static int
73971819 10205remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
77b06cd7 10206{
3a5c3e22
PA
10207 struct watchpoint *w = (struct watchpoint *) bl->owner;
10208 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10209
10210 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
4d01a485 10211 w->cond_exp.get ());
e09342b5
TJB
10212}
10213
e09342b5 10214static int
348d480f 10215breakpoint_hit_watchpoint (const struct bp_location *bl,
bd522513 10216 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 10217 const struct target_waitstatus *ws)
e09342b5 10218{
348d480f 10219 struct breakpoint *b = bl->owner;
3a5c3e22 10220 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10221
348d480f
PA
10222 /* Continuable hardware watchpoints are treated as non-existent if the
10223 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10224 some data address). Otherwise gdb won't stop on a break instruction
10225 in the code (not from a breakpoint) when a hardware watchpoint has
10226 been defined. Also skip watchpoints which we know did not trigger
10227 (did not match the data address). */
10228 if (is_hardware_watchpoint (b)
3a5c3e22 10229 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10230 return 0;
9c06b0b4 10231
348d480f 10232 return 1;
9c06b0b4
TJB
10233}
10234
348d480f
PA
10235static void
10236check_status_watchpoint (bpstat bs)
9c06b0b4 10237{
348d480f 10238 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10239
348d480f 10240 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10241}
10242
10243/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10244 hardware watchpoints. */
9c06b0b4
TJB
10245
10246static int
348d480f 10247resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10248{
3a5c3e22
PA
10249 struct watchpoint *w = (struct watchpoint *) bl->owner;
10250 int length = w->exact? 1 : bl->length;
348d480f
PA
10251
10252 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10253}
10254
10255/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10256 hardware watchpoints. */
9c06b0b4
TJB
10257
10258static int
348d480f 10259works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10260{
efa80663
PA
10261 /* Read and access watchpoints only work with hardware support. */
10262 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10263}
10264
9c06b0b4 10265static enum print_stop_action
348d480f 10266print_it_watchpoint (bpstat bs)
9c06b0b4 10267{
348d480f 10268 struct breakpoint *b;
348d480f 10269 enum print_stop_action result;
3a5c3e22 10270 struct watchpoint *w;
79a45e25 10271 struct ui_out *uiout = current_uiout;
348d480f
PA
10272
10273 gdb_assert (bs->bp_location_at != NULL);
10274
348d480f 10275 b = bs->breakpoint_at;
3a5c3e22 10276 w = (struct watchpoint *) b;
348d480f 10277
f303dbd6
PA
10278 annotate_watchpoint (b->number);
10279 maybe_print_thread_hit_breakpoint (uiout);
10280
d7e74731
PA
10281 string_file stb;
10282
76f9c9cf 10283 gdb::optional<ui_out_emit_tuple> tuple_emitter;
9c06b0b4
TJB
10284 switch (b->type)
10285 {
348d480f 10286 case bp_watchpoint:
9c06b0b4 10287 case bp_hardware_watchpoint:
112e8700
SM
10288 if (uiout->is_mi_like_p ())
10289 uiout->field_string
10290 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f 10291 mention (b);
76f9c9cf 10292 tuple_emitter.emplace (uiout, "value");
112e8700 10293 uiout->text ("\nOld value = ");
d7e74731 10294 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10295 uiout->field_stream ("old", stb);
10296 uiout->text ("\nNew value = ");
d7e74731 10297 watchpoint_value_print (w->val, &stb);
112e8700
SM
10298 uiout->field_stream ("new", stb);
10299 uiout->text ("\n");
348d480f
PA
10300 /* More than one watchpoint may have been triggered. */
10301 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10302 break;
10303
10304 case bp_read_watchpoint:
112e8700
SM
10305 if (uiout->is_mi_like_p ())
10306 uiout->field_string
10307 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f 10308 mention (b);
76f9c9cf 10309 tuple_emitter.emplace (uiout, "value");
112e8700 10310 uiout->text ("\nValue = ");
d7e74731 10311 watchpoint_value_print (w->val, &stb);
112e8700
SM
10312 uiout->field_stream ("value", stb);
10313 uiout->text ("\n");
348d480f 10314 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10315 break;
10316
10317 case bp_access_watchpoint:
348d480f
PA
10318 if (bs->old_val != NULL)
10319 {
112e8700
SM
10320 if (uiout->is_mi_like_p ())
10321 uiout->field_string
10322 ("reason",
348d480f
PA
10323 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10324 mention (b);
76f9c9cf 10325 tuple_emitter.emplace (uiout, "value");
112e8700 10326 uiout->text ("\nOld value = ");
d7e74731 10327 watchpoint_value_print (bs->old_val, &stb);
112e8700
SM
10328 uiout->field_stream ("old", stb);
10329 uiout->text ("\nNew value = ");
348d480f
PA
10330 }
10331 else
10332 {
10333 mention (b);
112e8700
SM
10334 if (uiout->is_mi_like_p ())
10335 uiout->field_string
10336 ("reason",
348d480f 10337 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
76f9c9cf 10338 tuple_emitter.emplace (uiout, "value");
112e8700 10339 uiout->text ("\nValue = ");
348d480f 10340 }
d7e74731 10341 watchpoint_value_print (w->val, &stb);
112e8700
SM
10342 uiout->field_stream ("new", stb);
10343 uiout->text ("\n");
348d480f 10344 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10345 break;
10346 default:
348d480f 10347 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10348 }
10349
348d480f
PA
10350 return result;
10351}
10352
10353/* Implement the "print_mention" breakpoint_ops method for hardware
10354 watchpoints. */
10355
10356static void
10357print_mention_watchpoint (struct breakpoint *b)
10358{
3a5c3e22 10359 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10360 struct ui_out *uiout = current_uiout;
46b9c129 10361 const char *tuple_name;
348d480f
PA
10362
10363 switch (b->type)
10364 {
10365 case bp_watchpoint:
112e8700 10366 uiout->text ("Watchpoint ");
46b9c129 10367 tuple_name = "wpt";
348d480f
PA
10368 break;
10369 case bp_hardware_watchpoint:
112e8700 10370 uiout->text ("Hardware watchpoint ");
46b9c129 10371 tuple_name = "wpt";
348d480f
PA
10372 break;
10373 case bp_read_watchpoint:
112e8700 10374 uiout->text ("Hardware read watchpoint ");
46b9c129 10375 tuple_name = "hw-rwpt";
348d480f
PA
10376 break;
10377 case bp_access_watchpoint:
112e8700 10378 uiout->text ("Hardware access (read/write) watchpoint ");
46b9c129 10379 tuple_name = "hw-awpt";
348d480f
PA
10380 break;
10381 default:
10382 internal_error (__FILE__, __LINE__,
10383 _("Invalid hardware watchpoint type."));
10384 }
10385
46b9c129 10386 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10387 uiout->field_int ("number", b->number);
10388 uiout->text (": ");
10389 uiout->field_string ("exp", w->exp_string);
348d480f
PA
10390}
10391
10392/* Implement the "print_recreate" breakpoint_ops method for
10393 watchpoints. */
10394
10395static void
10396print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10397{
3a5c3e22
PA
10398 struct watchpoint *w = (struct watchpoint *) b;
10399
348d480f
PA
10400 switch (b->type)
10401 {
10402 case bp_watchpoint:
10403 case bp_hardware_watchpoint:
10404 fprintf_unfiltered (fp, "watch");
10405 break;
10406 case bp_read_watchpoint:
10407 fprintf_unfiltered (fp, "rwatch");
10408 break;
10409 case bp_access_watchpoint:
10410 fprintf_unfiltered (fp, "awatch");
10411 break;
10412 default:
10413 internal_error (__FILE__, __LINE__,
10414 _("Invalid watchpoint type."));
10415 }
10416
3a5c3e22 10417 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10418 print_recreate_thread (b, fp);
348d480f
PA
10419}
10420
427cd150
TT
10421/* Implement the "explains_signal" breakpoint_ops method for
10422 watchpoints. */
10423
47591c29 10424static int
427cd150
TT
10425explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10426{
10427 /* A software watchpoint cannot cause a signal other than
10428 GDB_SIGNAL_TRAP. */
10429 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10430 return 0;
427cd150 10431
47591c29 10432 return 1;
427cd150
TT
10433}
10434
348d480f
PA
10435/* The breakpoint_ops structure to be used in hardware watchpoints. */
10436
2060206e 10437static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10438
10439/* Implement the "insert" breakpoint_ops method for
10440 masked hardware watchpoints. */
10441
10442static int
10443insert_masked_watchpoint (struct bp_location *bl)
10444{
3a5c3e22
PA
10445 struct watchpoint *w = (struct watchpoint *) bl->owner;
10446
10447 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10448 bl->watchpoint_type);
10449}
10450
10451/* Implement the "remove" breakpoint_ops method for
10452 masked hardware watchpoints. */
10453
10454static int
73971819 10455remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
348d480f 10456{
3a5c3e22
PA
10457 struct watchpoint *w = (struct watchpoint *) bl->owner;
10458
10459 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10460 bl->watchpoint_type);
10461}
10462
10463/* Implement the "resources_needed" breakpoint_ops method for
10464 masked hardware watchpoints. */
10465
10466static int
10467resources_needed_masked_watchpoint (const struct bp_location *bl)
10468{
3a5c3e22
PA
10469 struct watchpoint *w = (struct watchpoint *) bl->owner;
10470
10471 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10472}
10473
10474/* Implement the "works_in_software_mode" breakpoint_ops method for
10475 masked hardware watchpoints. */
10476
10477static int
10478works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10479{
10480 return 0;
10481}
10482
10483/* Implement the "print_it" breakpoint_ops method for
10484 masked hardware watchpoints. */
10485
10486static enum print_stop_action
10487print_it_masked_watchpoint (bpstat bs)
10488{
10489 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10490 struct ui_out *uiout = current_uiout;
348d480f
PA
10491
10492 /* Masked watchpoints have only one location. */
10493 gdb_assert (b->loc && b->loc->next == NULL);
10494
f303dbd6
PA
10495 annotate_watchpoint (b->number);
10496 maybe_print_thread_hit_breakpoint (uiout);
10497
348d480f
PA
10498 switch (b->type)
10499 {
10500 case bp_hardware_watchpoint:
112e8700
SM
10501 if (uiout->is_mi_like_p ())
10502 uiout->field_string
10503 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10504 break;
10505
10506 case bp_read_watchpoint:
112e8700
SM
10507 if (uiout->is_mi_like_p ())
10508 uiout->field_string
10509 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10510 break;
10511
10512 case bp_access_watchpoint:
112e8700
SM
10513 if (uiout->is_mi_like_p ())
10514 uiout->field_string
10515 ("reason",
348d480f
PA
10516 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10517 break;
10518 default:
10519 internal_error (__FILE__, __LINE__,
10520 _("Invalid hardware watchpoint type."));
10521 }
10522
10523 mention (b);
112e8700 10524 uiout->text (_("\n\
9c06b0b4
TJB
10525Check the underlying instruction at PC for the memory\n\
10526address and value which triggered this watchpoint.\n"));
112e8700 10527 uiout->text ("\n");
9c06b0b4
TJB
10528
10529 /* More than one watchpoint may have been triggered. */
10530 return PRINT_UNKNOWN;
10531}
10532
10533/* Implement the "print_one_detail" breakpoint_ops method for
10534 masked hardware watchpoints. */
10535
10536static void
10537print_one_detail_masked_watchpoint (const struct breakpoint *b,
10538 struct ui_out *uiout)
10539{
3a5c3e22
PA
10540 struct watchpoint *w = (struct watchpoint *) b;
10541
9c06b0b4
TJB
10542 /* Masked watchpoints have only one location. */
10543 gdb_assert (b->loc && b->loc->next == NULL);
10544
112e8700
SM
10545 uiout->text ("\tmask ");
10546 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10547 uiout->text ("\n");
9c06b0b4
TJB
10548}
10549
10550/* Implement the "print_mention" breakpoint_ops method for
10551 masked hardware watchpoints. */
10552
10553static void
10554print_mention_masked_watchpoint (struct breakpoint *b)
10555{
3a5c3e22 10556 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10557 struct ui_out *uiout = current_uiout;
46b9c129 10558 const char *tuple_name;
9c06b0b4
TJB
10559
10560 switch (b->type)
10561 {
10562 case bp_hardware_watchpoint:
112e8700 10563 uiout->text ("Masked hardware watchpoint ");
46b9c129 10564 tuple_name = "wpt";
9c06b0b4
TJB
10565 break;
10566 case bp_read_watchpoint:
112e8700 10567 uiout->text ("Masked hardware read watchpoint ");
46b9c129 10568 tuple_name = "hw-rwpt";
9c06b0b4
TJB
10569 break;
10570 case bp_access_watchpoint:
112e8700 10571 uiout->text ("Masked hardware access (read/write) watchpoint ");
46b9c129 10572 tuple_name = "hw-awpt";
9c06b0b4
TJB
10573 break;
10574 default:
10575 internal_error (__FILE__, __LINE__,
10576 _("Invalid hardware watchpoint type."));
10577 }
10578
46b9c129 10579 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
112e8700
SM
10580 uiout->field_int ("number", b->number);
10581 uiout->text (": ");
10582 uiout->field_string ("exp", w->exp_string);
9c06b0b4
TJB
10583}
10584
10585/* Implement the "print_recreate" breakpoint_ops method for
10586 masked hardware watchpoints. */
10587
10588static void
10589print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10590{
3a5c3e22 10591 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10592 char tmp[40];
10593
10594 switch (b->type)
10595 {
10596 case bp_hardware_watchpoint:
10597 fprintf_unfiltered (fp, "watch");
10598 break;
10599 case bp_read_watchpoint:
10600 fprintf_unfiltered (fp, "rwatch");
10601 break;
10602 case bp_access_watchpoint:
10603 fprintf_unfiltered (fp, "awatch");
10604 break;
10605 default:
10606 internal_error (__FILE__, __LINE__,
10607 _("Invalid hardware watchpoint type."));
10608 }
10609
3a5c3e22
PA
10610 sprintf_vma (tmp, w->hw_wp_mask);
10611 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10612 print_recreate_thread (b, fp);
9c06b0b4
TJB
10613}
10614
10615/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10616
2060206e 10617static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10618
10619/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10620
10621static int
10622is_masked_watchpoint (const struct breakpoint *b)
10623{
10624 return b->ops == &masked_watchpoint_breakpoint_ops;
10625}
10626
53a5351d
JM
10627/* accessflag: hw_write: watch write,
10628 hw_read: watch read,
10629 hw_access: watch access (read or write) */
c906108c 10630static void
bbc13ae3 10631watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10632 int just_location, int internal)
c906108c 10633{
c1fc2657 10634 struct breakpoint *scope_breakpoint = NULL;
270140bd 10635 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10636 struct value *val, *mark, *result;
bb9d5f81 10637 int saved_bitpos = 0, saved_bitsize = 0;
bbc13ae3
KS
10638 const char *exp_start = NULL;
10639 const char *exp_end = NULL;
10640 const char *tok, *end_tok;
9c06b0b4 10641 int toklen = -1;
bbc13ae3
KS
10642 const char *cond_start = NULL;
10643 const char *cond_end = NULL;
c906108c 10644 enum bptype bp_type;
37e4754d 10645 int thread = -1;
0cf6dd15 10646 int pc = 0;
9c06b0b4
TJB
10647 /* Flag to indicate whether we are going to use masks for
10648 the hardware watchpoint. */
10649 int use_mask = 0;
10650 CORE_ADDR mask = 0;
c906108c 10651
37e4754d
LM
10652 /* Make sure that we actually have parameters to parse. */
10653 if (arg != NULL && arg[0] != '\0')
10654 {
bbc13ae3
KS
10655 const char *value_start;
10656
10657 exp_end = arg + strlen (arg);
37e4754d 10658
9c06b0b4
TJB
10659 /* Look for "parameter value" pairs at the end
10660 of the arguments string. */
bbc13ae3 10661 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10662 {
10663 /* Skip whitespace at the end of the argument list. */
10664 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10665 tok--;
10666
10667 /* Find the beginning of the last token.
10668 This is the value of the parameter. */
10669 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10670 tok--;
10671 value_start = tok + 1;
10672
10673 /* Skip whitespace. */
10674 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10675 tok--;
10676
10677 end_tok = tok;
10678
10679 /* Find the beginning of the second to last token.
10680 This is the parameter itself. */
10681 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10682 tok--;
10683 tok++;
10684 toklen = end_tok - tok + 1;
10685
61012eef 10686 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 10687 {
5d5658a1 10688 struct thread_info *thr;
9c06b0b4
TJB
10689 /* At this point we've found a "thread" token, which means
10690 the user is trying to set a watchpoint that triggers
10691 only in a specific thread. */
5d5658a1 10692 const char *endp;
37e4754d 10693
9c06b0b4
TJB
10694 if (thread != -1)
10695 error(_("You can specify only one thread."));
37e4754d 10696
9c06b0b4 10697 /* Extract the thread ID from the next token. */
5d5658a1 10698 thr = parse_thread_id (value_start, &endp);
37e4754d 10699
5d5658a1 10700 /* Check if the user provided a valid thread ID. */
9c06b0b4 10701 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 10702 invalid_thread_id_error (value_start);
9c06b0b4 10703
5d5658a1 10704 thread = thr->global_num;
9c06b0b4 10705 }
61012eef 10706 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
10707 {
10708 /* We've found a "mask" token, which means the user wants to
10709 create a hardware watchpoint that is going to have the mask
10710 facility. */
10711 struct value *mask_value, *mark;
37e4754d 10712
9c06b0b4
TJB
10713 if (use_mask)
10714 error(_("You can specify only one mask."));
37e4754d 10715
9c06b0b4 10716 use_mask = just_location = 1;
37e4754d 10717
9c06b0b4
TJB
10718 mark = value_mark ();
10719 mask_value = parse_to_comma_and_eval (&value_start);
10720 mask = value_as_address (mask_value);
10721 value_free_to_mark (mark);
10722 }
10723 else
10724 /* We didn't recognize what we found. We should stop here. */
10725 break;
37e4754d 10726
9c06b0b4
TJB
10727 /* Truncate the string and get rid of the "parameter value" pair before
10728 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 10729 exp_end = tok;
9c06b0b4 10730 }
37e4754d 10731 }
bbc13ae3
KS
10732 else
10733 exp_end = arg;
37e4754d 10734
bbc13ae3
KS
10735 /* Parse the rest of the arguments. From here on out, everything
10736 is in terms of a newly allocated string instead of the original
10737 ARG. */
c906108c 10738 innermost_block = NULL;
81b1e71c
TT
10739 std::string expression (arg, exp_end - arg);
10740 exp_start = arg = expression.c_str ();
4d01a485 10741 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 10742 exp_end = arg;
fa8a61dc
TT
10743 /* Remove trailing whitespace from the expression before saving it.
10744 This makes the eventual display of the expression string a bit
10745 prettier. */
10746 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10747 --exp_end;
10748
65d79d4b 10749 /* Checking if the expression is not constant. */
4d01a485 10750 if (watchpoint_exp_is_const (exp.get ()))
65d79d4b
SDJ
10751 {
10752 int len;
10753
10754 len = exp_end - exp_start;
10755 while (len > 0 && isspace (exp_start[len - 1]))
10756 len--;
10757 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10758 }
10759
c906108c
SS
10760 exp_valid_block = innermost_block;
10761 mark = value_mark ();
4d01a485 10762 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
06a64a0b 10763
bb9d5f81
PP
10764 if (val != NULL && just_location)
10765 {
10766 saved_bitpos = value_bitpos (val);
10767 saved_bitsize = value_bitsize (val);
10768 }
10769
06a64a0b
TT
10770 if (just_location)
10771 {
9c06b0b4
TJB
10772 int ret;
10773
06a64a0b 10774 exp_valid_block = NULL;
a1442452 10775 val = value_addr (result);
06a64a0b
TT
10776 release_value (val);
10777 value_free_to_mark (mark);
9c06b0b4
TJB
10778
10779 if (use_mask)
10780 {
10781 ret = target_masked_watch_num_registers (value_as_address (val),
10782 mask);
10783 if (ret == -1)
10784 error (_("This target does not support masked watchpoints."));
10785 else if (ret == -2)
10786 error (_("Invalid mask or memory region."));
10787 }
06a64a0b
TT
10788 }
10789 else if (val != NULL)
fa4727a6 10790 release_value (val);
c906108c 10791
f1735a53
TT
10792 tok = skip_spaces (arg);
10793 end_tok = skip_to_space (tok);
c906108c
SS
10794
10795 toklen = end_tok - tok;
10796 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10797 {
60e1c644 10798 innermost_block = NULL;
c906108c 10799 tok = cond_start = end_tok + 1;
4d01a485 10800 parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
10801
10802 /* The watchpoint expression may not be local, but the condition
10803 may still be. E.g.: `watch global if local > 0'. */
10804 cond_exp_valid_block = innermost_block;
10805
c906108c
SS
10806 cond_end = tok;
10807 }
10808 if (*tok)
8a3fe4f8 10809 error (_("Junk at end of command."));
c906108c 10810
441d7c93
PA
10811 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
10812
10813 /* Save this because create_internal_breakpoint below invalidates
10814 'wp_frame'. */
10815 frame_id watchpoint_frame = get_frame_id (wp_frame);
d983da9c
DJ
10816
10817 /* If the expression is "local", then set up a "watchpoint scope"
10818 breakpoint at the point where we've left the scope of the watchpoint
10819 expression. Create the scope breakpoint before the watchpoint, so
10820 that we will encounter it first in bpstat_stop_status. */
441d7c93 10821 if (exp_valid_block != NULL && wp_frame != NULL)
d983da9c 10822 {
441d7c93
PA
10823 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
10824
10825 if (frame_id_p (caller_frame_id))
edb3359d 10826 {
441d7c93
PA
10827 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
10828 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
10829
edb3359d 10830 scope_breakpoint
441d7c93 10831 = create_internal_breakpoint (caller_arch, caller_pc,
06edf0c0
PA
10832 bp_watchpoint_scope,
10833 &momentary_breakpoint_ops);
d983da9c 10834
441d7c93
PA
10835 /* create_internal_breakpoint could invalidate WP_FRAME. */
10836 wp_frame = NULL;
10837
edb3359d 10838 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10839
edb3359d
DJ
10840 /* Automatically delete the breakpoint when it hits. */
10841 scope_breakpoint->disposition = disp_del;
d983da9c 10842
edb3359d 10843 /* Only break in the proper frame (help with recursion). */
441d7c93 10844 scope_breakpoint->frame_id = caller_frame_id;
d983da9c 10845
edb3359d 10846 /* Set the address at which we will stop. */
441d7c93
PA
10847 scope_breakpoint->loc->gdbarch = caller_arch;
10848 scope_breakpoint->loc->requested_address = caller_pc;
edb3359d 10849 scope_breakpoint->loc->address
a6d9a66e
UW
10850 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10851 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10852 scope_breakpoint->type);
10853 }
d983da9c
DJ
10854 }
10855
e8369a73
AB
10856 /* Now set up the breakpoint. We create all watchpoints as hardware
10857 watchpoints here even if hardware watchpoints are turned off, a call
10858 to update_watchpoint later in this function will cause the type to
10859 drop back to bp_watchpoint (software watchpoint) if required. */
10860
10861 if (accessflag == hw_read)
10862 bp_type = bp_read_watchpoint;
10863 else if (accessflag == hw_access)
10864 bp_type = bp_access_watchpoint;
10865 else
10866 bp_type = bp_hardware_watchpoint;
3a5c3e22 10867
b270e6f9 10868 std::unique_ptr<watchpoint> w (new watchpoint ());
c1fc2657 10869
348d480f 10870 if (use_mask)
b270e6f9 10871 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10872 &masked_watchpoint_breakpoint_ops);
348d480f 10873 else
b270e6f9 10874 init_raw_breakpoint_without_location (w.get (), NULL, bp_type,
3a5c3e22 10875 &watchpoint_breakpoint_ops);
c1fc2657
SM
10876 w->thread = thread;
10877 w->disposition = disp_donttouch;
10878 w->pspace = current_program_space;
b22e99fd 10879 w->exp = std::move (exp);
3a5c3e22
PA
10880 w->exp_valid_block = exp_valid_block;
10881 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10882 if (just_location)
10883 {
10884 struct type *t = value_type (val);
10885 CORE_ADDR addr = value_as_address (val);
06a64a0b 10886
43cc5389
TT
10887 w->exp_string_reparse
10888 = current_language->la_watch_location_expression (t, addr).release ();
06a64a0b 10889
3a5c3e22 10890 w->exp_string = xstrprintf ("-location %.*s",
d63d0675 10891 (int) (exp_end - exp_start), exp_start);
06a64a0b
TT
10892 }
10893 else
3a5c3e22 10894 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10895
10896 if (use_mask)
10897 {
3a5c3e22 10898 w->hw_wp_mask = mask;
9c06b0b4
TJB
10899 }
10900 else
10901 {
3a5c3e22 10902 w->val = val;
bb9d5f81
PP
10903 w->val_bitpos = saved_bitpos;
10904 w->val_bitsize = saved_bitsize;
3a5c3e22 10905 w->val_valid = 1;
9c06b0b4 10906 }
77b06cd7 10907
c906108c 10908 if (cond_start)
c1fc2657 10909 w->cond_string = savestring (cond_start, cond_end - cond_start);
c906108c 10910 else
c1fc2657 10911 w->cond_string = 0;
c5aa993b 10912
441d7c93 10913 if (frame_id_p (watchpoint_frame))
f6bc2008 10914 {
441d7c93 10915 w->watchpoint_frame = watchpoint_frame;
3a5c3e22 10916 w->watchpoint_thread = inferior_ptid;
f6bc2008 10917 }
c906108c 10918 else
f6bc2008 10919 {
3a5c3e22
PA
10920 w->watchpoint_frame = null_frame_id;
10921 w->watchpoint_thread = null_ptid;
f6bc2008 10922 }
c906108c 10923
d983da9c 10924 if (scope_breakpoint != NULL)
c906108c 10925 {
d983da9c
DJ
10926 /* The scope breakpoint is related to the watchpoint. We will
10927 need to act on them together. */
c1fc2657 10928 w->related_breakpoint = scope_breakpoint;
b270e6f9 10929 scope_breakpoint->related_breakpoint = w.get ();
c906108c 10930 }
d983da9c 10931
06a64a0b
TT
10932 if (!just_location)
10933 value_free_to_mark (mark);
2d134ed3 10934
b270e6f9
TT
10935 /* Finally update the new watchpoint. This creates the locations
10936 that should be inserted. */
10937 update_watchpoint (w.get (), 1);
a9634178 10938
b270e6f9 10939 install_breakpoint (internal, std::move (w), 1);
c906108c
SS
10940}
10941
e09342b5 10942/* Return count of debug registers needed to watch the given expression.
e09342b5 10943 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10944
c906108c 10945static int
a9634178 10946can_use_hardware_watchpoint (struct value *v)
c906108c
SS
10947{
10948 int found_memory_cnt = 0;
2e70b7b9 10949 struct value *head = v;
c906108c
SS
10950
10951 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10952 if (!can_use_hw_watchpoints)
c906108c 10953 return 0;
c5aa993b 10954
5c44784c
JM
10955 /* Make sure that the value of the expression depends only upon
10956 memory contents, and values computed from them within GDB. If we
10957 find any register references or function calls, we can't use a
10958 hardware watchpoint.
10959
10960 The idea here is that evaluating an expression generates a series
10961 of values, one holding the value of every subexpression. (The
10962 expression a*b+c has five subexpressions: a, b, a*b, c, and
10963 a*b+c.) GDB's values hold almost enough information to establish
10964 the criteria given above --- they identify memory lvalues,
10965 register lvalues, computed values, etcetera. So we can evaluate
10966 the expression, and then scan the chain of values that leaves
10967 behind to decide whether we can detect any possible change to the
10968 expression's final value using only hardware watchpoints.
10969
10970 However, I don't think that the values returned by inferior
10971 function calls are special in any way. So this function may not
10972 notice that an expression involving an inferior function call
10973 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 10974 for (; v; v = value_next (v))
c906108c 10975 {
5c44784c 10976 if (VALUE_LVAL (v) == lval_memory)
c906108c 10977 {
8464be76
DJ
10978 if (v != head && value_lazy (v))
10979 /* A lazy memory lvalue in the chain is one that GDB never
10980 needed to fetch; we either just used its address (e.g.,
10981 `a' in `a.b') or we never needed it at all (e.g., `a'
10982 in `a,b'). This doesn't apply to HEAD; if that is
10983 lazy then it was not readable, but watch it anyway. */
5c44784c 10984 ;
53a5351d 10985 else
5c44784c
JM
10986 {
10987 /* Ahh, memory we actually used! Check if we can cover
10988 it with hardware watchpoints. */
df407dfe 10989 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10990
10991 /* We only watch structs and arrays if user asked for it
10992 explicitly, never if they just happen to appear in a
10993 middle of some value chain. */
10994 if (v == head
10995 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10996 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10997 {
42ae5230 10998 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10999 int len;
11000 int num_regs;
11001
a9634178 11002 len = (target_exact_watchpoints
e09342b5
TJB
11003 && is_scalar_type_recursive (vtype))?
11004 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11005
e09342b5
TJB
11006 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11007 if (!num_regs)
2e70b7b9
MS
11008 return 0;
11009 else
e09342b5 11010 found_memory_cnt += num_regs;
2e70b7b9 11011 }
5c44784c 11012 }
c5aa993b 11013 }
5086187c
AC
11014 else if (VALUE_LVAL (v) != not_lval
11015 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11016 return 0; /* These are values from the history (e.g., $1). */
5086187c 11017 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11018 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11019 }
11020
11021 /* The expression itself looks suitable for using a hardware
11022 watchpoint, but give the target machine a chance to reject it. */
11023 return found_memory_cnt;
11024}
11025
8b93c638 11026void
f2fc3015 11027watch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11028{
84f4c1fe 11029 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11030}
11031
06a64a0b
TT
11032/* A helper function that looks for the "-location" argument and then
11033 calls watch_command_1. */
11034
11035static void
0b39b52e 11036watch_maybe_just_location (const char *arg, int accessflag, int from_tty)
06a64a0b
TT
11037{
11038 int just_location = 0;
11039
11040 if (arg
11041 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11042 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11043 {
e9cafbcc 11044 arg = skip_spaces (arg);
06a64a0b
TT
11045 just_location = 1;
11046 }
11047
84f4c1fe 11048 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11049}
8926118c 11050
c5aa993b 11051static void
0b39b52e 11052watch_command (const char *arg, int from_tty)
c906108c 11053{
06a64a0b 11054 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11055}
11056
8b93c638 11057void
f2fc3015 11058rwatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11059{
84f4c1fe 11060 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11061}
8926118c 11062
c5aa993b 11063static void
0b39b52e 11064rwatch_command (const char *arg, int from_tty)
c906108c 11065{
06a64a0b 11066 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11067}
11068
8b93c638 11069void
f2fc3015 11070awatch_command_wrapper (const char *arg, int from_tty, int internal)
8b93c638 11071{
84f4c1fe 11072 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11073}
8926118c 11074
c5aa993b 11075static void
0b39b52e 11076awatch_command (const char *arg, int from_tty)
c906108c 11077{
06a64a0b 11078 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11079}
c906108c 11080\f
c5aa993b 11081
cfc31633
PA
11082/* Data for the FSM that manages the until(location)/advance commands
11083 in infcmd.c. Here because it uses the mechanisms of
11084 breakpoints. */
c906108c 11085
cfc31633 11086struct until_break_fsm
bfec99b2 11087{
cfc31633
PA
11088 /* The base class. */
11089 struct thread_fsm thread_fsm;
11090
11091 /* The thread that as current when the command was executed. */
11092 int thread;
11093
11094 /* The breakpoint set at the destination location. */
11095 struct breakpoint *location_breakpoint;
11096
11097 /* Breakpoint set at the return address in the caller frame. May be
11098 NULL. */
11099 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11100};
11101
8980e177
PA
11102static void until_break_fsm_clean_up (struct thread_fsm *self,
11103 struct thread_info *thread);
11104static int until_break_fsm_should_stop (struct thread_fsm *self,
11105 struct thread_info *thread);
cfc31633
PA
11106static enum async_reply_reason
11107 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11108
11109/* until_break_fsm's vtable. */
11110
11111static struct thread_fsm_ops until_break_fsm_ops =
11112{
11113 NULL, /* dtor */
11114 until_break_fsm_clean_up,
11115 until_break_fsm_should_stop,
11116 NULL, /* return_value */
11117 until_break_fsm_async_reply_reason,
11118};
11119
11120/* Allocate a new until_break_command_fsm. */
11121
11122static struct until_break_fsm *
8980e177 11123new_until_break_fsm (struct interp *cmd_interp, int thread,
454dafbd
TT
11124 breakpoint_up &&location_breakpoint,
11125 breakpoint_up &&caller_breakpoint)
cfc31633
PA
11126{
11127 struct until_break_fsm *sm;
11128
11129 sm = XCNEW (struct until_break_fsm);
8980e177 11130 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
cfc31633
PA
11131
11132 sm->thread = thread;
454dafbd
TT
11133 sm->location_breakpoint = location_breakpoint.release ();
11134 sm->caller_breakpoint = caller_breakpoint.release ();
cfc31633
PA
11135
11136 return sm;
11137}
11138
11139/* Implementation of the 'should_stop' FSM method for the
11140 until(location)/advance commands. */
11141
11142static int
8980e177
PA
11143until_break_fsm_should_stop (struct thread_fsm *self,
11144 struct thread_info *tp)
cfc31633
PA
11145{
11146 struct until_break_fsm *sm = (struct until_break_fsm *) self;
cfc31633
PA
11147
11148 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11149 sm->location_breakpoint) != NULL
11150 || (sm->caller_breakpoint != NULL
11151 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11152 sm->caller_breakpoint) != NULL))
11153 thread_fsm_set_finished (self);
11154
11155 return 1;
11156}
11157
11158/* Implementation of the 'clean_up' FSM method for the
11159 until(location)/advance commands. */
11160
c2c6d25f 11161static void
8980e177
PA
11162until_break_fsm_clean_up (struct thread_fsm *self,
11163 struct thread_info *thread)
43ff13b4 11164{
cfc31633 11165 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11166
cfc31633
PA
11167 /* Clean up our temporary breakpoints. */
11168 if (sm->location_breakpoint != NULL)
11169 {
11170 delete_breakpoint (sm->location_breakpoint);
11171 sm->location_breakpoint = NULL;
11172 }
11173 if (sm->caller_breakpoint != NULL)
11174 {
11175 delete_breakpoint (sm->caller_breakpoint);
11176 sm->caller_breakpoint = NULL;
11177 }
11178 delete_longjmp_breakpoint (sm->thread);
11179}
11180
11181/* Implementation of the 'async_reply_reason' FSM method for the
11182 until(location)/advance commands. */
11183
11184static enum async_reply_reason
11185until_break_fsm_async_reply_reason (struct thread_fsm *self)
11186{
11187 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11188}
11189
c906108c 11190void
f2fc3015 11191until_break_command (const char *arg, int from_tty, int anywhere)
c906108c 11192{
8556afb4
PA
11193 struct frame_info *frame;
11194 struct gdbarch *frame_gdbarch;
11195 struct frame_id stack_frame_id;
11196 struct frame_id caller_frame_id;
ffc2605c 11197 struct cleanup *old_chain;
186c406b
TT
11198 int thread;
11199 struct thread_info *tp;
cfc31633 11200 struct until_break_fsm *sm;
c906108c 11201
70509625 11202 clear_proceed_status (0);
c906108c
SS
11203
11204 /* Set a breakpoint where the user wants it and at return from
4a64f543 11205 this function. */
c5aa993b 11206
ffc2605c 11207 event_location_up location = string_to_event_location (&arg, current_language);
f00aae0f 11208
6c5b2ebe
PA
11209 std::vector<symtab_and_line> sals
11210 = (last_displayed_sal_is_valid ()
11211 ? decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11212 get_last_displayed_symtab (),
11213 get_last_displayed_line ())
11214 : decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11215 NULL, (struct symtab *) NULL, 0));
c5aa993b 11216
6c5b2ebe 11217 if (sals.size () != 1)
8a3fe4f8 11218 error (_("Couldn't get information on specified line."));
c5aa993b 11219
6c5b2ebe 11220 symtab_and_line &sal = sals[0];
c5aa993b 11221
c906108c 11222 if (*arg)
8a3fe4f8 11223 error (_("Junk at end of arguments."));
c5aa993b 11224
c906108c 11225 resolve_sal_pc (&sal);
c5aa993b 11226
186c406b 11227 tp = inferior_thread ();
5d5658a1 11228 thread = tp->global_num;
186c406b 11229
883bc8d1
PA
11230 old_chain = make_cleanup (null_cleanup, NULL);
11231
8556afb4
PA
11232 /* Note linespec handling above invalidates the frame chain.
11233 Installing a breakpoint also invalidates the frame chain (as it
11234 may need to switch threads), so do any frame handling before
11235 that. */
11236
11237 frame = get_selected_frame (NULL);
11238 frame_gdbarch = get_frame_arch (frame);
11239 stack_frame_id = get_stack_frame_id (frame);
11240 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11241
ae66c1fc
EZ
11242 /* Keep within the current frame, or in frames called by the current
11243 one. */
edb3359d 11244
454dafbd 11245 breakpoint_up caller_breakpoint;
883bc8d1 11246 if (frame_id_p (caller_frame_id))
c906108c 11247 {
883bc8d1 11248 struct symtab_and_line sal2;
cfc31633 11249 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11250
11251 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11252 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11253 caller_gdbarch = frame_unwind_caller_arch (frame);
11254 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11255 sal2,
11256 caller_frame_id,
11257 bp_until);
186c406b 11258
883bc8d1 11259 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11260 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11261 }
c5aa993b 11262
c70a6932
JK
11263 /* set_momentary_breakpoint could invalidate FRAME. */
11264 frame = NULL;
11265
454dafbd 11266 breakpoint_up location_breakpoint;
883bc8d1
PA
11267 if (anywhere)
11268 /* If the user told us to continue until a specified location,
11269 we don't specify a frame at which we need to stop. */
cfc31633
PA
11270 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11271 null_frame_id, bp_until);
883bc8d1
PA
11272 else
11273 /* Otherwise, specify the selected frame, because we want to stop
11274 only at the very same frame. */
cfc31633
PA
11275 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11276 stack_frame_id, bp_until);
883bc8d1 11277
8980e177 11278 sm = new_until_break_fsm (command_interp (), tp->global_num,
454dafbd
TT
11279 std::move (location_breakpoint),
11280 std::move (caller_breakpoint));
cfc31633 11281 tp->thread_fsm = &sm->thread_fsm;
f107f563 11282
cfc31633 11283 discard_cleanups (old_chain);
f107f563 11284
cfc31633 11285 proceed (-1, GDB_SIGNAL_DEFAULT);
c906108c 11286}
ae66c1fc 11287
c906108c
SS
11288/* This function attempts to parse an optional "if <cond>" clause
11289 from the arg string. If one is not found, it returns NULL.
c5aa993b 11290
c906108c
SS
11291 Else, it returns a pointer to the condition string. (It does not
11292 attempt to evaluate the string against a particular block.) And,
11293 it updates arg to point to the first character following the parsed
4a64f543 11294 if clause in the arg string. */
53a5351d 11295
63160a43
PA
11296const char *
11297ep_parse_optional_if_clause (const char **arg)
c906108c 11298{
63160a43 11299 const char *cond_string;
c5aa993b
JM
11300
11301 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11302 return NULL;
c5aa993b 11303
4a64f543 11304 /* Skip the "if" keyword. */
c906108c 11305 (*arg) += 2;
c5aa993b 11306
c906108c 11307 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11308 condition string. */
f1735a53 11309 *arg = skip_spaces (*arg);
c906108c 11310 cond_string = *arg;
c5aa993b 11311
4a64f543
MS
11312 /* Assume that the condition occupies the remainder of the arg
11313 string. */
c906108c 11314 (*arg) += strlen (cond_string);
c5aa993b 11315
c906108c
SS
11316 return cond_string;
11317}
c5aa993b 11318
c906108c
SS
11319/* Commands to deal with catching events, such as signals, exceptions,
11320 process start/exit, etc. */
c5aa993b
JM
11321
11322typedef enum
11323{
44feb3ce
TT
11324 catch_fork_temporary, catch_vfork_temporary,
11325 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11326}
11327catch_fork_kind;
11328
c906108c 11329static void
eb4c3f4a 11330catch_fork_command_1 (const char *arg, int from_tty,
cc59ec59 11331 struct cmd_list_element *command)
c906108c 11332{
a6d9a66e 11333 struct gdbarch *gdbarch = get_current_arch ();
63160a43 11334 const char *cond_string = NULL;
44feb3ce
TT
11335 catch_fork_kind fork_kind;
11336 int tempflag;
11337
11338 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11339 tempflag = (fork_kind == catch_fork_temporary
11340 || fork_kind == catch_vfork_temporary);
c5aa993b 11341
44feb3ce
TT
11342 if (!arg)
11343 arg = "";
f1735a53 11344 arg = skip_spaces (arg);
c5aa993b 11345
c906108c 11346 /* The allowed syntax is:
c5aa993b
JM
11347 catch [v]fork
11348 catch [v]fork if <cond>
11349
4a64f543 11350 First, check if there's an if clause. */
c906108c 11351 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11352
c906108c 11353 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11354 error (_("Junk at end of arguments."));
c5aa993b 11355
c906108c 11356 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11357 and enable reporting of such events. */
c5aa993b
JM
11358 switch (fork_kind)
11359 {
44feb3ce
TT
11360 case catch_fork_temporary:
11361 case catch_fork_permanent:
a6d9a66e 11362 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11363 &catch_fork_breakpoint_ops);
c906108c 11364 break;
44feb3ce
TT
11365 case catch_vfork_temporary:
11366 case catch_vfork_permanent:
a6d9a66e 11367 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11368 &catch_vfork_breakpoint_ops);
c906108c 11369 break;
c5aa993b 11370 default:
8a3fe4f8 11371 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11372 break;
c5aa993b 11373 }
c906108c
SS
11374}
11375
11376static void
eb4c3f4a 11377catch_exec_command_1 (const char *arg, int from_tty,
cc59ec59 11378 struct cmd_list_element *command)
c906108c 11379{
a6d9a66e 11380 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11381 int tempflag;
63160a43 11382 const char *cond_string = NULL;
c906108c 11383
44feb3ce
TT
11384 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11385
11386 if (!arg)
11387 arg = "";
f1735a53 11388 arg = skip_spaces (arg);
c906108c
SS
11389
11390 /* The allowed syntax is:
c5aa993b
JM
11391 catch exec
11392 catch exec if <cond>
c906108c 11393
4a64f543 11394 First, check if there's an if clause. */
c906108c
SS
11395 cond_string = ep_parse_optional_if_clause (&arg);
11396
11397 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11398 error (_("Junk at end of arguments."));
c906108c 11399
b270e6f9
TT
11400 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
11401 init_catchpoint (c.get (), gdbarch, tempflag, cond_string,
b4d90040
PA
11402 &catch_exec_breakpoint_ops);
11403 c->exec_pathname = NULL;
11404
b270e6f9 11405 install_breakpoint (0, std::move (c), 1);
c906108c 11406}
c5aa993b 11407
9ac4176b 11408void
28010a5d
PA
11409init_ada_exception_breakpoint (struct breakpoint *b,
11410 struct gdbarch *gdbarch,
11411 struct symtab_and_line sal,
f2fc3015 11412 const char *addr_string,
c0a91b2b 11413 const struct breakpoint_ops *ops,
28010a5d 11414 int tempflag,
349774ef 11415 int enabled,
28010a5d 11416 int from_tty)
f7f9143b 11417{
f7f9143b
JB
11418 if (from_tty)
11419 {
5af949e3
UW
11420 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11421 if (!loc_gdbarch)
11422 loc_gdbarch = gdbarch;
11423
6c95b8df
PA
11424 describe_other_breakpoints (loc_gdbarch,
11425 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11426 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11427 version for exception catchpoints, because two catchpoints
11428 used for different exception names will use the same address.
11429 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11430 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11431 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11432 the user what type of catchpoint it is. The above is good
11433 enough for now, though. */
11434 }
11435
28010a5d 11436 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11437
349774ef 11438 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11439 b->disposition = tempflag ? disp_del : disp_donttouch;
d28cd78a
TT
11440 b->location = string_to_event_location (&addr_string,
11441 language_def (language_ada));
f7f9143b 11442 b->language = language_ada;
f7f9143b
JB
11443}
11444
c906108c 11445static void
981a3fb3 11446catch_command (const char *arg, int from_tty)
c906108c 11447{
44feb3ce 11448 error (_("Catch requires an event name."));
c906108c
SS
11449}
11450\f
11451
11452static void
981a3fb3 11453tcatch_command (const char *arg, int from_tty)
c906108c 11454{
44feb3ce 11455 error (_("Catch requires an event name."));
c906108c
SS
11456}
11457
81b1e71c 11458/* Compare two breakpoints and return a strcmp-like result. */
8a2c437b
TT
11459
11460static int
81b1e71c 11461compare_breakpoints (const breakpoint *a, const breakpoint *b)
8a2c437b 11462{
81b1e71c
TT
11463 uintptr_t ua = (uintptr_t) a;
11464 uintptr_t ub = (uintptr_t) b;
8a2c437b 11465
81b1e71c 11466 if (a->number < b->number)
8a2c437b 11467 return -1;
81b1e71c 11468 else if (a->number > b->number)
8a2c437b
TT
11469 return 1;
11470
11471 /* Now sort by address, in case we see, e..g, two breakpoints with
11472 the number 0. */
11473 if (ua < ub)
11474 return -1;
94b0e70d 11475 return ua > ub ? 1 : 0;
8a2c437b
TT
11476}
11477
80f8a6eb 11478/* Delete breakpoints by address or line. */
c906108c
SS
11479
11480static void
0b39b52e 11481clear_command (const char *arg, int from_tty)
c906108c 11482{
81b1e71c 11483 struct breakpoint *b;
c906108c 11484 int default_match;
c906108c
SS
11485 int i;
11486
6c5b2ebe
PA
11487 std::vector<symtab_and_line> decoded_sals;
11488 symtab_and_line last_sal;
11489 gdb::array_view<symtab_and_line> sals;
c906108c
SS
11490 if (arg)
11491 {
6c5b2ebe
PA
11492 decoded_sals
11493 = decode_line_with_current_source (arg,
11494 (DECODE_LINE_FUNFIRSTLINE
11495 | DECODE_LINE_LIST_MODE));
c906108c 11496 default_match = 0;
6c5b2ebe 11497 sals = decoded_sals;
c906108c
SS
11498 }
11499 else
11500 {
1bfeeb0f
JL
11501 /* Set sal's line, symtab, pc, and pspace to the values
11502 corresponding to the last call to print_frame_info. If the
11503 codepoint is not valid, this will set all the fields to 0. */
51abb421 11504 last_sal = get_last_displayed_sal ();
6c5b2ebe 11505 if (last_sal.symtab == 0)
8a3fe4f8 11506 error (_("No source file specified."));
c906108c 11507
c906108c 11508 default_match = 1;
6c5b2ebe 11509 sals = last_sal;
c906108c
SS
11510 }
11511
4a64f543
MS
11512 /* We don't call resolve_sal_pc here. That's not as bad as it
11513 seems, because all existing breakpoints typically have both
11514 file/line and pc set. So, if clear is given file/line, we can
11515 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11516
11517 We only support clearing given the address explicitly
11518 present in breakpoint table. Say, we've set breakpoint
4a64f543 11519 at file:line. There were several PC values for that file:line,
ed0616c6 11520 due to optimization, all in one block.
4a64f543
MS
11521
11522 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11523 PC corresponding to the same file:line, the breakpoint won't
11524 be cleared. We probably can still clear the breakpoint, but
11525 since the other PC value is never presented to user, user
11526 can only find it by guessing, and it does not seem important
11527 to support that. */
11528
4a64f543
MS
11529 /* For each line spec given, delete bps which correspond to it. Do
11530 it in two passes, solely to preserve the current behavior that
11531 from_tty is forced true if we delete more than one
11532 breakpoint. */
c906108c 11533
81b1e71c 11534 std::vector<struct breakpoint *> found;
6c5b2ebe 11535 for (const auto &sal : sals)
c906108c 11536 {
05cba821
JK
11537 const char *sal_fullname;
11538
c906108c 11539 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11540 If line given (pc == 0), clear all bpts on specified line.
11541 If defaulting, clear all bpts on default line
c906108c 11542 or at default pc.
c5aa993b
JM
11543
11544 defaulting sal.pc != 0 tests to do
11545
11546 0 1 pc
11547 1 1 pc _and_ line
11548 0 0 line
11549 1 0 <can't happen> */
c906108c 11550
05cba821
JK
11551 sal_fullname = (sal.symtab == NULL
11552 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11553
4a64f543 11554 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11555 ALL_BREAKPOINTS (b)
c5aa993b 11556 {
0d381245 11557 int match = 0;
4a64f543 11558 /* Are we going to delete b? */
cc60f2e3 11559 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11560 {
11561 struct bp_location *loc = b->loc;
11562 for (; loc; loc = loc->next)
11563 {
f8eba3c6
TT
11564 /* If the user specified file:line, don't allow a PC
11565 match. This matches historical gdb behavior. */
11566 int pc_match = (!sal.explicit_line
11567 && sal.pc
11568 && (loc->pspace == sal.pspace)
11569 && (loc->address == sal.pc)
11570 && (!section_is_overlay (loc->section)
11571 || loc->section == sal.section));
4aac40c8
TT
11572 int line_match = 0;
11573
11574 if ((default_match || sal.explicit_line)
2f202fde 11575 && loc->symtab != NULL
05cba821 11576 && sal_fullname != NULL
4aac40c8 11577 && sal.pspace == loc->pspace
05cba821
JK
11578 && loc->line_number == sal.line
11579 && filename_cmp (symtab_to_fullname (loc->symtab),
11580 sal_fullname) == 0)
11581 line_match = 1;
4aac40c8 11582
0d381245
VP
11583 if (pc_match || line_match)
11584 {
11585 match = 1;
11586 break;
11587 }
11588 }
11589 }
11590
11591 if (match)
81b1e71c 11592 found.push_back (b);
c906108c 11593 }
80f8a6eb 11594 }
8a2c437b 11595
80f8a6eb 11596 /* Now go thru the 'found' chain and delete them. */
81b1e71c 11597 if (found.empty ())
80f8a6eb
MS
11598 {
11599 if (arg)
8a3fe4f8 11600 error (_("No breakpoint at %s."), arg);
80f8a6eb 11601 else
8a3fe4f8 11602 error (_("No breakpoint at this line."));
80f8a6eb 11603 }
c906108c 11604
8a2c437b 11605 /* Remove duplicates from the vec. */
81b1e71c
TT
11606 std::sort (found.begin (), found.end (),
11607 [] (const breakpoint *a, const breakpoint *b)
11608 {
11609 return compare_breakpoints (a, b) < 0;
11610 });
11611 found.erase (std::unique (found.begin (), found.end (),
11612 [] (const breakpoint *a, const breakpoint *b)
11613 {
11614 return compare_breakpoints (a, b) == 0;
11615 }),
11616 found.end ());
8a2c437b 11617
81b1e71c 11618 if (found.size () > 1)
4a64f543 11619 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11620 if (from_tty)
a3f17187 11621 {
81b1e71c 11622 if (found.size () == 1)
a3f17187
AC
11623 printf_unfiltered (_("Deleted breakpoint "));
11624 else
11625 printf_unfiltered (_("Deleted breakpoints "));
11626 }
d6e956e5 11627
81b1e71c 11628 for (breakpoint *iter : found)
80f8a6eb 11629 {
c5aa993b 11630 if (from_tty)
81b1e71c
TT
11631 printf_unfiltered ("%d ", iter->number);
11632 delete_breakpoint (iter);
c906108c 11633 }
80f8a6eb
MS
11634 if (from_tty)
11635 putchar_unfiltered ('\n');
c906108c
SS
11636}
11637\f
11638/* Delete breakpoint in BS if they are `delete' breakpoints and
11639 all breakpoints that are marked for deletion, whether hit or not.
11640 This is called after any breakpoint is hit, or after errors. */
11641
11642void
fba45db2 11643breakpoint_auto_delete (bpstat bs)
c906108c 11644{
35df4500 11645 struct breakpoint *b, *b_tmp;
c906108c
SS
11646
11647 for (; bs; bs = bs->next)
f431efe5
PA
11648 if (bs->breakpoint_at
11649 && bs->breakpoint_at->disposition == disp_del
c906108c 11650 && bs->stop)
f431efe5 11651 delete_breakpoint (bs->breakpoint_at);
c906108c 11652
35df4500 11653 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11654 {
b5de0fa7 11655 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11656 delete_breakpoint (b);
11657 }
c906108c
SS
11658}
11659
4a64f543
MS
11660/* A comparison function for bp_location AP and BP being interfaced to
11661 qsort. Sort elements primarily by their ADDRESS (no matter what
11662 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 11663 secondarily by ordering first permanent elements and
4a64f543 11664 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11665 qsort being an unstable algorithm. */
876fa593
JK
11666
11667static int
f5336ca5 11668bp_locations_compare (const void *ap, const void *bp)
876fa593 11669{
9a3c8263
SM
11670 const struct bp_location *a = *(const struct bp_location **) ap;
11671 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
11672
11673 if (a->address != b->address)
11674 return (a->address > b->address) - (a->address < b->address);
11675
dea2aa5f
LM
11676 /* Sort locations at the same address by their pspace number, keeping
11677 locations of the same inferior (in a multi-inferior environment)
11678 grouped. */
11679
11680 if (a->pspace->num != b->pspace->num)
11681 return ((a->pspace->num > b->pspace->num)
11682 - (a->pspace->num < b->pspace->num));
11683
876fa593 11684 /* Sort permanent breakpoints first. */
1a853c52
PA
11685 if (a->permanent != b->permanent)
11686 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 11687
c56a97f9
JK
11688 /* Make the internal GDB representation stable across GDB runs
11689 where A and B memory inside GDB can differ. Breakpoint locations of
11690 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11691
11692 if (a->owner->number != b->owner->number)
c56a97f9
JK
11693 return ((a->owner->number > b->owner->number)
11694 - (a->owner->number < b->owner->number));
876fa593
JK
11695
11696 return (a > b) - (a < b);
11697}
11698
f5336ca5
PA
11699/* Set bp_locations_placed_address_before_address_max and
11700 bp_locations_shadow_len_after_address_max according to the current
11701 content of the bp_locations array. */
f7545552
TT
11702
11703static void
f5336ca5 11704bp_locations_target_extensions_update (void)
f7545552 11705{
876fa593
JK
11706 struct bp_location *bl, **blp_tmp;
11707
f5336ca5
PA
11708 bp_locations_placed_address_before_address_max = 0;
11709 bp_locations_shadow_len_after_address_max = 0;
876fa593
JK
11710
11711 ALL_BP_LOCATIONS (bl, blp_tmp)
11712 {
11713 CORE_ADDR start, end, addr;
11714
11715 if (!bp_location_has_shadow (bl))
11716 continue;
11717
11718 start = bl->target_info.placed_address;
11719 end = start + bl->target_info.shadow_len;
11720
11721 gdb_assert (bl->address >= start);
11722 addr = bl->address - start;
f5336ca5
PA
11723 if (addr > bp_locations_placed_address_before_address_max)
11724 bp_locations_placed_address_before_address_max = addr;
876fa593
JK
11725
11726 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11727
11728 gdb_assert (bl->address < end);
11729 addr = end - bl->address;
f5336ca5
PA
11730 if (addr > bp_locations_shadow_len_after_address_max)
11731 bp_locations_shadow_len_after_address_max = addr;
876fa593 11732 }
f7545552
TT
11733}
11734
1e4d1764
YQ
11735/* Download tracepoint locations if they haven't been. */
11736
11737static void
11738download_tracepoint_locations (void)
11739{
7ed2c994 11740 struct breakpoint *b;
dd2e65cc 11741 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764 11742
5ed8105e 11743 scoped_restore_current_pspace_and_thread restore_pspace_thread;
1e4d1764 11744
7ed2c994 11745 ALL_TRACEPOINTS (b)
1e4d1764 11746 {
7ed2c994 11747 struct bp_location *bl;
1e4d1764 11748 struct tracepoint *t;
f2a8bc8a 11749 int bp_location_downloaded = 0;
1e4d1764 11750
7ed2c994 11751 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
11752 ? !may_insert_fast_tracepoints
11753 : !may_insert_tracepoints))
11754 continue;
11755
dd2e65cc
YQ
11756 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
11757 {
11758 if (target_can_download_tracepoint ())
11759 can_download_tracepoint = TRIBOOL_TRUE;
11760 else
11761 can_download_tracepoint = TRIBOOL_FALSE;
11762 }
11763
11764 if (can_download_tracepoint == TRIBOOL_FALSE)
11765 break;
11766
7ed2c994
YQ
11767 for (bl = b->loc; bl; bl = bl->next)
11768 {
11769 /* In tracepoint, locations are _never_ duplicated, so
11770 should_be_inserted is equivalent to
11771 unduplicated_should_be_inserted. */
11772 if (!should_be_inserted (bl) || bl->inserted)
11773 continue;
1e4d1764 11774
7ed2c994 11775 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 11776
7ed2c994 11777 target_download_tracepoint (bl);
1e4d1764 11778
7ed2c994 11779 bl->inserted = 1;
f2a8bc8a 11780 bp_location_downloaded = 1;
7ed2c994
YQ
11781 }
11782 t = (struct tracepoint *) b;
11783 t->number_on_target = b->number;
f2a8bc8a
YQ
11784 if (bp_location_downloaded)
11785 observer_notify_breakpoint_modified (b);
1e4d1764 11786 }
1e4d1764
YQ
11787}
11788
934709f0
PW
11789/* Swap the insertion/duplication state between two locations. */
11790
11791static void
11792swap_insertion (struct bp_location *left, struct bp_location *right)
11793{
11794 const int left_inserted = left->inserted;
11795 const int left_duplicate = left->duplicate;
b775012e 11796 const int left_needs_update = left->needs_update;
934709f0
PW
11797 const struct bp_target_info left_target_info = left->target_info;
11798
1e4d1764
YQ
11799 /* Locations of tracepoints can never be duplicated. */
11800 if (is_tracepoint (left->owner))
11801 gdb_assert (!left->duplicate);
11802 if (is_tracepoint (right->owner))
11803 gdb_assert (!right->duplicate);
11804
934709f0
PW
11805 left->inserted = right->inserted;
11806 left->duplicate = right->duplicate;
b775012e 11807 left->needs_update = right->needs_update;
934709f0
PW
11808 left->target_info = right->target_info;
11809 right->inserted = left_inserted;
11810 right->duplicate = left_duplicate;
b775012e 11811 right->needs_update = left_needs_update;
934709f0
PW
11812 right->target_info = left_target_info;
11813}
11814
b775012e
LM
11815/* Force the re-insertion of the locations at ADDRESS. This is called
11816 once a new/deleted/modified duplicate location is found and we are evaluating
11817 conditions on the target's side. Such conditions need to be updated on
11818 the target. */
11819
11820static void
11821force_breakpoint_reinsertion (struct bp_location *bl)
11822{
11823 struct bp_location **locp = NULL, **loc2p;
11824 struct bp_location *loc;
11825 CORE_ADDR address = 0;
11826 int pspace_num;
11827
11828 address = bl->address;
11829 pspace_num = bl->pspace->num;
11830
11831 /* This is only meaningful if the target is
11832 evaluating conditions and if the user has
11833 opted for condition evaluation on the target's
11834 side. */
11835 if (gdb_evaluates_breakpoint_condition_p ()
11836 || !target_supports_evaluation_of_breakpoint_conditions ())
11837 return;
11838
11839 /* Flag all breakpoint locations with this address and
11840 the same program space as the location
11841 as "its condition has changed". We need to
11842 update the conditions on the target's side. */
11843 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11844 {
11845 loc = *loc2p;
11846
11847 if (!is_breakpoint (loc->owner)
11848 || pspace_num != loc->pspace->num)
11849 continue;
11850
11851 /* Flag the location appropriately. We use a different state to
11852 let everyone know that we already updated the set of locations
11853 with addr bl->address and program space bl->pspace. This is so
11854 we don't have to keep calling these functions just to mark locations
11855 that have already been marked. */
11856 loc->condition_changed = condition_updated;
11857
11858 /* Free the agent expression bytecode as well. We will compute
11859 it later on. */
833177a4 11860 loc->cond_bytecode.reset ();
b775012e
LM
11861 }
11862}
44702360
PA
11863/* Called whether new breakpoints are created, or existing breakpoints
11864 deleted, to update the global location list and recompute which
11865 locations are duplicate of which.
b775012e 11866
04086b45
PA
11867 The INSERT_MODE flag determines whether locations may not, may, or
11868 shall be inserted now. See 'enum ugll_insert_mode' for more
11869 info. */
b60e7edf 11870
0d381245 11871static void
44702360 11872update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 11873{
74960c60 11874 struct breakpoint *b;
876fa593 11875 struct bp_location **locp, *loc;
b775012e
LM
11876 /* Last breakpoint location address that was marked for update. */
11877 CORE_ADDR last_addr = 0;
11878 /* Last breakpoint location program space that was marked for update. */
11879 int last_pspace_num = -1;
f7545552 11880
2d134ed3
PA
11881 /* Used in the duplicates detection below. When iterating over all
11882 bp_locations, points to the first bp_location of a given address.
11883 Breakpoints and watchpoints of different types are never
11884 duplicates of each other. Keep one pointer for each type of
11885 breakpoint/watchpoint, so we only need to loop over all locations
11886 once. */
11887 struct bp_location *bp_loc_first; /* breakpoint */
11888 struct bp_location *wp_loc_first; /* hardware watchpoint */
11889 struct bp_location *awp_loc_first; /* access watchpoint */
11890 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11891
f5336ca5
PA
11892 /* Saved former bp_locations array which we compare against the newly
11893 built bp_locations from the current state of ALL_BREAKPOINTS. */
81b1e71c 11894 struct bp_location **old_locp;
f5336ca5 11895 unsigned old_locations_count;
81b1e71c 11896 gdb::unique_xmalloc_ptr<struct bp_location *> old_locations (bp_locations);
876fa593 11897
f5336ca5
PA
11898 old_locations_count = bp_locations_count;
11899 bp_locations = NULL;
11900 bp_locations_count = 0;
0d381245 11901
74960c60 11902 ALL_BREAKPOINTS (b)
876fa593 11903 for (loc = b->loc; loc; loc = loc->next)
f5336ca5 11904 bp_locations_count++;
876fa593 11905
f5336ca5
PA
11906 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
11907 locp = bp_locations;
876fa593
JK
11908 ALL_BREAKPOINTS (b)
11909 for (loc = b->loc; loc; loc = loc->next)
11910 *locp++ = loc;
f5336ca5
PA
11911 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
11912 bp_locations_compare);
876fa593 11913
f5336ca5 11914 bp_locations_target_extensions_update ();
74960c60 11915
4a64f543
MS
11916 /* Identify bp_location instances that are no longer present in the
11917 new list, and therefore should be freed. Note that it's not
11918 necessary that those locations should be removed from inferior --
11919 if there's another location at the same address (previously
11920 marked as duplicate), we don't need to remove/insert the
11921 location.
876fa593 11922
4a64f543
MS
11923 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11924 and former bp_location array state respectively. */
876fa593 11925
f5336ca5 11926 locp = bp_locations;
81b1e71c
TT
11927 for (old_locp = old_locations.get ();
11928 old_locp < old_locations.get () + old_locations_count;
876fa593 11929 old_locp++)
74960c60 11930 {
876fa593 11931 struct bp_location *old_loc = *old_locp;
c7d46a38 11932 struct bp_location **loc2p;
876fa593 11933
e5dd4106 11934 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11935 not, we have to free it. */
c7d46a38 11936 int found_object = 0;
20874c92
VP
11937 /* Tells if the location should remain inserted in the target. */
11938 int keep_in_target = 0;
11939 int removed = 0;
876fa593 11940
4a64f543
MS
11941 /* Skip LOCP entries which will definitely never be needed.
11942 Stop either at or being the one matching OLD_LOC. */
f5336ca5 11943 while (locp < bp_locations + bp_locations_count
c7d46a38 11944 && (*locp)->address < old_loc->address)
876fa593 11945 locp++;
c7d46a38
PA
11946
11947 for (loc2p = locp;
f5336ca5 11948 (loc2p < bp_locations + bp_locations_count
c7d46a38
PA
11949 && (*loc2p)->address == old_loc->address);
11950 loc2p++)
11951 {
b775012e
LM
11952 /* Check if this is a new/duplicated location or a duplicated
11953 location that had its condition modified. If so, we want to send
11954 its condition to the target if evaluation of conditions is taking
11955 place there. */
11956 if ((*loc2p)->condition_changed == condition_modified
11957 && (last_addr != old_loc->address
11958 || last_pspace_num != old_loc->pspace->num))
c7d46a38 11959 {
b775012e
LM
11960 force_breakpoint_reinsertion (*loc2p);
11961 last_pspace_num = old_loc->pspace->num;
c7d46a38 11962 }
b775012e
LM
11963
11964 if (*loc2p == old_loc)
11965 found_object = 1;
c7d46a38 11966 }
74960c60 11967
b775012e
LM
11968 /* We have already handled this address, update it so that we don't
11969 have to go through updates again. */
11970 last_addr = old_loc->address;
11971
11972 /* Target-side condition evaluation: Handle deleted locations. */
11973 if (!found_object)
11974 force_breakpoint_reinsertion (old_loc);
11975
4a64f543
MS
11976 /* If this location is no longer present, and inserted, look if
11977 there's maybe a new location at the same address. If so,
11978 mark that one inserted, and don't remove this one. This is
11979 needed so that we don't have a time window where a breakpoint
11980 at certain location is not inserted. */
74960c60 11981
876fa593 11982 if (old_loc->inserted)
0d381245 11983 {
4a64f543
MS
11984 /* If the location is inserted now, we might have to remove
11985 it. */
74960c60 11986
876fa593 11987 if (found_object && should_be_inserted (old_loc))
74960c60 11988 {
4a64f543
MS
11989 /* The location is still present in the location list,
11990 and still should be inserted. Don't do anything. */
20874c92 11991 keep_in_target = 1;
74960c60
VP
11992 }
11993 else
11994 {
b775012e
LM
11995 /* This location still exists, but it won't be kept in the
11996 target since it may have been disabled. We proceed to
11997 remove its target-side condition. */
11998
4a64f543
MS
11999 /* The location is either no longer present, or got
12000 disabled. See if there's another location at the
12001 same address, in which case we don't need to remove
12002 this one from the target. */
876fa593 12003
2bdf28a0 12004 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12005 if (breakpoint_address_is_meaningful (old_loc->owner))
12006 {
876fa593 12007 for (loc2p = locp;
f5336ca5 12008 (loc2p < bp_locations + bp_locations_count
c7d46a38 12009 && (*loc2p)->address == old_loc->address);
876fa593
JK
12010 loc2p++)
12011 {
12012 struct bp_location *loc2 = *loc2p;
12013
2d134ed3 12014 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12015 {
85d721b8
PA
12016 /* Read watchpoint locations are switched to
12017 access watchpoints, if the former are not
12018 supported, but the latter are. */
12019 if (is_hardware_watchpoint (old_loc->owner))
12020 {
12021 gdb_assert (is_hardware_watchpoint (loc2->owner));
12022 loc2->watchpoint_type = old_loc->watchpoint_type;
12023 }
12024
934709f0
PW
12025 /* loc2 is a duplicated location. We need to check
12026 if it should be inserted in case it will be
12027 unduplicated. */
12028 if (loc2 != old_loc
12029 && unduplicated_should_be_inserted (loc2))
c7d46a38 12030 {
934709f0 12031 swap_insertion (old_loc, loc2);
c7d46a38
PA
12032 keep_in_target = 1;
12033 break;
12034 }
876fa593
JK
12035 }
12036 }
12037 }
74960c60
VP
12038 }
12039
20874c92
VP
12040 if (!keep_in_target)
12041 {
834c0d03 12042 if (remove_breakpoint (old_loc))
20874c92 12043 {
4a64f543
MS
12044 /* This is just about all we can do. We could keep
12045 this location on the global list, and try to
12046 remove it next time, but there's no particular
12047 reason why we will succeed next time.
20874c92 12048
4a64f543
MS
12049 Note that at this point, old_loc->owner is still
12050 valid, as delete_breakpoint frees the breakpoint
12051 only after calling us. */
3e43a32a
MS
12052 printf_filtered (_("warning: Error removing "
12053 "breakpoint %d\n"),
876fa593 12054 old_loc->owner->number);
20874c92
VP
12055 }
12056 removed = 1;
12057 }
0d381245 12058 }
74960c60
VP
12059
12060 if (!found_object)
1c5cfe86 12061 {
fbea99ea 12062 if (removed && target_is_non_stop_p ()
1cf4d951 12063 && need_moribund_for_location_type (old_loc))
20874c92 12064 {
db82e815
PA
12065 /* This location was removed from the target. In
12066 non-stop mode, a race condition is possible where
12067 we've removed a breakpoint, but stop events for that
12068 breakpoint are already queued and will arrive later.
12069 We apply an heuristic to be able to distinguish such
12070 SIGTRAPs from other random SIGTRAPs: we keep this
12071 breakpoint location for a bit, and will retire it
12072 after we see some number of events. The theory here
12073 is that reporting of events should, "on the average",
12074 be fair, so after a while we'll see events from all
12075 threads that have anything of interest, and no longer
12076 need to keep this breakpoint location around. We
12077 don't hold locations forever so to reduce chances of
12078 mistaking a non-breakpoint SIGTRAP for a breakpoint
12079 SIGTRAP.
12080
12081 The heuristic failing can be disastrous on
12082 decr_pc_after_break targets.
12083
12084 On decr_pc_after_break targets, like e.g., x86-linux,
12085 if we fail to recognize a late breakpoint SIGTRAP,
12086 because events_till_retirement has reached 0 too
12087 soon, we'll fail to do the PC adjustment, and report
12088 a random SIGTRAP to the user. When the user resumes
12089 the inferior, it will most likely immediately crash
2dec564e 12090 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12091 corrupted, because of being resumed e.g., in the
12092 middle of a multi-byte instruction, or skipped a
12093 one-byte instruction. This was actually seen happen
12094 on native x86-linux, and should be less rare on
12095 targets that do not support new thread events, like
12096 remote, due to the heuristic depending on
12097 thread_count.
12098
12099 Mistaking a random SIGTRAP for a breakpoint trap
12100 causes similar symptoms (PC adjustment applied when
12101 it shouldn't), but then again, playing with SIGTRAPs
12102 behind the debugger's back is asking for trouble.
12103
12104 Since hardware watchpoint traps are always
12105 distinguishable from other traps, so we don't need to
12106 apply keep hardware watchpoint moribund locations
12107 around. We simply always ignore hardware watchpoint
12108 traps we can no longer explain. */
12109
876fa593
JK
12110 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12111 old_loc->owner = NULL;
20874c92 12112
876fa593 12113 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12114 }
12115 else
f431efe5
PA
12116 {
12117 old_loc->owner = NULL;
12118 decref_bp_location (&old_loc);
12119 }
20874c92 12120 }
74960c60 12121 }
1c5cfe86 12122
348d480f
PA
12123 /* Rescan breakpoints at the same address and section, marking the
12124 first one as "first" and any others as "duplicates". This is so
12125 that the bpt instruction is only inserted once. If we have a
12126 permanent breakpoint at the same place as BPT, make that one the
12127 official one, and the rest as duplicates. Permanent breakpoints
12128 are sorted first for the same address.
12129
12130 Do the same for hardware watchpoints, but also considering the
12131 watchpoint's type (regular/access/read) and length. */
12132
12133 bp_loc_first = NULL;
12134 wp_loc_first = NULL;
12135 awp_loc_first = NULL;
12136 rwp_loc_first = NULL;
12137 ALL_BP_LOCATIONS (loc, locp)
12138 {
12139 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12140 non-NULL. */
348d480f 12141 struct bp_location **loc_first_p;
d3fbdd86 12142 b = loc->owner;
348d480f 12143
6f380991 12144 if (!unduplicated_should_be_inserted (loc)
348d480f 12145 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12146 /* Don't detect duplicate for tracepoint locations because they are
12147 never duplicated. See the comments in field `duplicate' of
12148 `struct bp_location'. */
348d480f 12149 || is_tracepoint (b))
b775012e
LM
12150 {
12151 /* Clear the condition modification flag. */
12152 loc->condition_changed = condition_unchanged;
12153 continue;
12154 }
348d480f 12155
348d480f
PA
12156 if (b->type == bp_hardware_watchpoint)
12157 loc_first_p = &wp_loc_first;
12158 else if (b->type == bp_read_watchpoint)
12159 loc_first_p = &rwp_loc_first;
12160 else if (b->type == bp_access_watchpoint)
12161 loc_first_p = &awp_loc_first;
12162 else
12163 loc_first_p = &bp_loc_first;
12164
12165 if (*loc_first_p == NULL
12166 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12167 || !breakpoint_locations_match (loc, *loc_first_p))
12168 {
12169 *loc_first_p = loc;
12170 loc->duplicate = 0;
b775012e
LM
12171
12172 if (is_breakpoint (loc->owner) && loc->condition_changed)
12173 {
12174 loc->needs_update = 1;
12175 /* Clear the condition modification flag. */
12176 loc->condition_changed = condition_unchanged;
12177 }
348d480f
PA
12178 continue;
12179 }
12180
934709f0
PW
12181
12182 /* This and the above ensure the invariant that the first location
12183 is not duplicated, and is the inserted one.
12184 All following are marked as duplicated, and are not inserted. */
12185 if (loc->inserted)
12186 swap_insertion (loc, *loc_first_p);
348d480f
PA
12187 loc->duplicate = 1;
12188
b775012e
LM
12189 /* Clear the condition modification flag. */
12190 loc->condition_changed = condition_unchanged;
348d480f
PA
12191 }
12192
a25a5a45 12193 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12194 {
04086b45 12195 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12196 insert_breakpoint_locations ();
12197 else
12198 {
44702360
PA
12199 /* Even though the caller told us to not insert new
12200 locations, we may still need to update conditions on the
12201 target's side of breakpoints that were already inserted
12202 if the target is evaluating breakpoint conditions. We
b775012e
LM
12203 only update conditions for locations that are marked
12204 "needs_update". */
12205 update_inserted_breakpoint_locations ();
12206 }
12207 }
348d480f 12208
04086b45 12209 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764 12210 download_tracepoint_locations ();
348d480f
PA
12211}
12212
12213void
12214breakpoint_retire_moribund (void)
12215{
12216 struct bp_location *loc;
12217 int ix;
12218
12219 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12220 if (--(loc->events_till_retirement) == 0)
12221 {
12222 decref_bp_location (&loc);
12223 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12224 --ix;
12225 }
12226}
12227
12228static void
44702360 12229update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12230{
348d480f 12231
492d29ea
PA
12232 TRY
12233 {
12234 update_global_location_list (insert_mode);
12235 }
12236 CATCH (e, RETURN_MASK_ERROR)
12237 {
12238 }
12239 END_CATCH
348d480f
PA
12240}
12241
12242/* Clear BKP from a BPS. */
12243
12244static void
12245bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12246{
12247 bpstat bs;
12248
12249 for (bs = bps; bs; bs = bs->next)
12250 if (bs->breakpoint_at == bpt)
12251 {
12252 bs->breakpoint_at = NULL;
12253 bs->old_val = NULL;
12254 /* bs->commands will be freed later. */
12255 }
12256}
12257
12258/* Callback for iterate_over_threads. */
12259static int
12260bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12261{
9a3c8263 12262 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12263
12264 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12265 return 0;
12266}
12267
12268/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12269 callbacks. */
12270
12271static void
12272say_where (struct breakpoint *b)
12273{
12274 struct value_print_options opts;
12275
12276 get_user_print_options (&opts);
12277
12278 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12279 single string. */
12280 if (b->loc == NULL)
12281 {
f00aae0f
KS
12282 /* For pending locations, the output differs slightly based
12283 on b->extra_string. If this is non-NULL, it contains either
12284 a condition or dprintf arguments. */
12285 if (b->extra_string == NULL)
12286 {
12287 printf_filtered (_(" (%s) pending."),
d28cd78a 12288 event_location_to_string (b->location.get ()));
f00aae0f
KS
12289 }
12290 else if (b->type == bp_dprintf)
12291 {
12292 printf_filtered (_(" (%s,%s) pending."),
d28cd78a 12293 event_location_to_string (b->location.get ()),
f00aae0f
KS
12294 b->extra_string);
12295 }
12296 else
12297 {
12298 printf_filtered (_(" (%s %s) pending."),
d28cd78a 12299 event_location_to_string (b->location.get ()),
f00aae0f
KS
12300 b->extra_string);
12301 }
348d480f
PA
12302 }
12303 else
12304 {
2f202fde 12305 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12306 {
12307 printf_filtered (" at ");
12308 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12309 gdb_stdout);
12310 }
2f202fde 12311 if (b->loc->symtab != NULL)
f8eba3c6
TT
12312 {
12313 /* If there is a single location, we can print the location
12314 more nicely. */
12315 if (b->loc->next == NULL)
12316 printf_filtered (": file %s, line %d.",
05cba821
JK
12317 symtab_to_filename_for_display (b->loc->symtab),
12318 b->loc->line_number);
f8eba3c6
TT
12319 else
12320 /* This is not ideal, but each location may have a
12321 different file name, and this at least reflects the
12322 real situation somewhat. */
f00aae0f 12323 printf_filtered (": %s.",
d28cd78a 12324 event_location_to_string (b->location.get ()));
f8eba3c6 12325 }
348d480f
PA
12326
12327 if (b->loc->next)
12328 {
12329 struct bp_location *loc = b->loc;
12330 int n = 0;
12331 for (; loc; loc = loc->next)
12332 ++n;
12333 printf_filtered (" (%d locations)", n);
12334 }
12335 }
12336}
12337
348d480f
PA
12338/* Default bp_location_ops methods. */
12339
12340static void
12341bp_location_dtor (struct bp_location *self)
12342{
348d480f
PA
12343 xfree (self->function_name);
12344}
12345
12346static const struct bp_location_ops bp_location_ops =
12347{
12348 bp_location_dtor
12349};
12350
c1fc2657 12351/* Destructor for the breakpoint base class. */
348d480f 12352
c1fc2657 12353breakpoint::~breakpoint ()
348d480f 12354{
c1fc2657
SM
12355 xfree (this->cond_string);
12356 xfree (this->extra_string);
12357 xfree (this->filter);
348d480f
PA
12358}
12359
2060206e
PA
12360static struct bp_location *
12361base_breakpoint_allocate_location (struct breakpoint *self)
348d480f 12362{
5625a286 12363 return new bp_location (&bp_location_ops, self);
348d480f
PA
12364}
12365
2060206e
PA
12366static void
12367base_breakpoint_re_set (struct breakpoint *b)
12368{
12369 /* Nothing to re-set. */
12370}
12371
12372#define internal_error_pure_virtual_called() \
12373 gdb_assert_not_reached ("pure virtual function called")
12374
12375static int
12376base_breakpoint_insert_location (struct bp_location *bl)
12377{
12378 internal_error_pure_virtual_called ();
12379}
12380
12381static int
73971819
PA
12382base_breakpoint_remove_location (struct bp_location *bl,
12383 enum remove_bp_reason reason)
2060206e
PA
12384{
12385 internal_error_pure_virtual_called ();
12386}
12387
12388static int
12389base_breakpoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12390 const address_space *aspace,
09ac7c10
TT
12391 CORE_ADDR bp_addr,
12392 const struct target_waitstatus *ws)
2060206e
PA
12393{
12394 internal_error_pure_virtual_called ();
12395}
12396
12397static void
12398base_breakpoint_check_status (bpstat bs)
12399{
12400 /* Always stop. */
12401}
12402
12403/* A "works_in_software_mode" breakpoint_ops method that just internal
12404 errors. */
12405
12406static int
12407base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12408{
12409 internal_error_pure_virtual_called ();
12410}
12411
12412/* A "resources_needed" breakpoint_ops method that just internal
12413 errors. */
12414
12415static int
12416base_breakpoint_resources_needed (const struct bp_location *bl)
12417{
12418 internal_error_pure_virtual_called ();
12419}
12420
12421static enum print_stop_action
12422base_breakpoint_print_it (bpstat bs)
12423{
12424 internal_error_pure_virtual_called ();
12425}
12426
12427static void
12428base_breakpoint_print_one_detail (const struct breakpoint *self,
12429 struct ui_out *uiout)
12430{
12431 /* nothing */
12432}
12433
12434static void
12435base_breakpoint_print_mention (struct breakpoint *b)
12436{
12437 internal_error_pure_virtual_called ();
12438}
12439
12440static void
12441base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12442{
12443 internal_error_pure_virtual_called ();
12444}
12445
983af33b 12446static void
f00aae0f
KS
12447base_breakpoint_create_sals_from_location
12448 (const struct event_location *location,
12449 struct linespec_result *canonical,
12450 enum bptype type_wanted)
983af33b
SDJ
12451{
12452 internal_error_pure_virtual_called ();
12453}
12454
12455static void
12456base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12457 struct linespec_result *c,
e1e01040
PA
12458 gdb::unique_xmalloc_ptr<char> cond_string,
12459 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12460 enum bptype type_wanted,
12461 enum bpdisp disposition,
12462 int thread,
12463 int task, int ignore_count,
12464 const struct breakpoint_ops *o,
12465 int from_tty, int enabled,
44f238bb 12466 int internal, unsigned flags)
983af33b
SDJ
12467{
12468 internal_error_pure_virtual_called ();
12469}
12470
6c5b2ebe 12471static std::vector<symtab_and_line>
f00aae0f
KS
12472base_breakpoint_decode_location (struct breakpoint *b,
12473 const struct event_location *location,
6c5b2ebe 12474 struct program_space *search_pspace)
983af33b
SDJ
12475{
12476 internal_error_pure_virtual_called ();
12477}
12478
ab04a2af
TT
12479/* The default 'explains_signal' method. */
12480
47591c29 12481static int
427cd150 12482base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12483{
47591c29 12484 return 1;
ab04a2af
TT
12485}
12486
9d6e6e84
HZ
12487/* The default "after_condition_true" method. */
12488
12489static void
12490base_breakpoint_after_condition_true (struct bpstats *bs)
12491{
12492 /* Nothing to do. */
12493}
12494
ab04a2af 12495struct breakpoint_ops base_breakpoint_ops =
2060206e 12496{
2060206e
PA
12497 base_breakpoint_allocate_location,
12498 base_breakpoint_re_set,
12499 base_breakpoint_insert_location,
12500 base_breakpoint_remove_location,
12501 base_breakpoint_breakpoint_hit,
12502 base_breakpoint_check_status,
12503 base_breakpoint_resources_needed,
12504 base_breakpoint_works_in_software_mode,
12505 base_breakpoint_print_it,
12506 NULL,
12507 base_breakpoint_print_one_detail,
12508 base_breakpoint_print_mention,
983af33b 12509 base_breakpoint_print_recreate,
5f700d83 12510 base_breakpoint_create_sals_from_location,
983af33b 12511 base_breakpoint_create_breakpoints_sal,
5f700d83 12512 base_breakpoint_decode_location,
9d6e6e84
HZ
12513 base_breakpoint_explains_signal,
12514 base_breakpoint_after_condition_true,
2060206e
PA
12515};
12516
12517/* Default breakpoint_ops methods. */
12518
12519static void
348d480f
PA
12520bkpt_re_set (struct breakpoint *b)
12521{
06edf0c0 12522 /* FIXME: is this still reachable? */
9ef9e6a6 12523 if (breakpoint_event_location_empty_p (b))
06edf0c0 12524 {
f00aae0f 12525 /* Anything without a location can't be re-set. */
348d480f 12526 delete_breakpoint (b);
06edf0c0 12527 return;
348d480f 12528 }
06edf0c0
PA
12529
12530 breakpoint_re_set_default (b);
348d480f
PA
12531}
12532
2060206e 12533static int
348d480f
PA
12534bkpt_insert_location (struct bp_location *bl)
12535{
cd6c3b4f
YQ
12536 CORE_ADDR addr = bl->target_info.reqstd_address;
12537
579c6ad9 12538 bl->target_info.kind = breakpoint_kind (bl, &addr);
cd6c3b4f
YQ
12539 bl->target_info.placed_address = addr;
12540
348d480f 12541 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12542 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12543 else
7c16b83e 12544 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12545}
12546
2060206e 12547static int
73971819 12548bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
348d480f
PA
12549{
12550 if (bl->loc_type == bp_loc_hardware_breakpoint)
12551 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12552 else
73971819 12553 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
348d480f
PA
12554}
12555
2060206e 12556static int
348d480f 12557bkpt_breakpoint_hit (const struct bp_location *bl,
bd522513 12558 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12559 const struct target_waitstatus *ws)
348d480f 12560{
09ac7c10 12561 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12562 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12563 return 0;
12564
348d480f
PA
12565 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12566 aspace, bp_addr))
12567 return 0;
12568
12569 if (overlay_debugging /* unmapped overlay section */
12570 && section_is_overlay (bl->section)
12571 && !section_is_mapped (bl->section))
12572 return 0;
12573
12574 return 1;
12575}
12576
cd1608cc
PA
12577static int
12578dprintf_breakpoint_hit (const struct bp_location *bl,
bd522513 12579 const address_space *aspace, CORE_ADDR bp_addr,
cd1608cc
PA
12580 const struct target_waitstatus *ws)
12581{
12582 if (dprintf_style == dprintf_style_agent
12583 && target_can_run_breakpoint_commands ())
12584 {
12585 /* An agent-style dprintf never causes a stop. If we see a trap
12586 for this address it must be for a breakpoint that happens to
12587 be set at the same address. */
12588 return 0;
12589 }
12590
12591 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12592}
12593
2060206e 12594static int
348d480f
PA
12595bkpt_resources_needed (const struct bp_location *bl)
12596{
12597 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12598
12599 return 1;
12600}
12601
2060206e 12602static enum print_stop_action
348d480f
PA
12603bkpt_print_it (bpstat bs)
12604{
348d480f
PA
12605 struct breakpoint *b;
12606 const struct bp_location *bl;
001c8c33 12607 int bp_temp;
79a45e25 12608 struct ui_out *uiout = current_uiout;
348d480f
PA
12609
12610 gdb_assert (bs->bp_location_at != NULL);
12611
12612 bl = bs->bp_location_at;
12613 b = bs->breakpoint_at;
12614
001c8c33
PA
12615 bp_temp = b->disposition == disp_del;
12616 if (bl->address != bl->requested_address)
12617 breakpoint_adjustment_warning (bl->requested_address,
12618 bl->address,
12619 b->number, 1);
12620 annotate_breakpoint (b->number);
f303dbd6
PA
12621 maybe_print_thread_hit_breakpoint (uiout);
12622
001c8c33 12623 if (bp_temp)
112e8700 12624 uiout->text ("Temporary breakpoint ");
001c8c33 12625 else
112e8700
SM
12626 uiout->text ("Breakpoint ");
12627 if (uiout->is_mi_like_p ())
348d480f 12628 {
112e8700 12629 uiout->field_string ("reason",
001c8c33 12630 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
112e8700 12631 uiout->field_string ("disp", bpdisp_text (b->disposition));
06edf0c0 12632 }
112e8700
SM
12633 uiout->field_int ("bkptno", b->number);
12634 uiout->text (", ");
06edf0c0 12635
001c8c33 12636 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12637}
12638
2060206e 12639static void
06edf0c0
PA
12640bkpt_print_mention (struct breakpoint *b)
12641{
112e8700 12642 if (current_uiout->is_mi_like_p ())
06edf0c0
PA
12643 return;
12644
12645 switch (b->type)
12646 {
12647 case bp_breakpoint:
12648 case bp_gnu_ifunc_resolver:
12649 if (b->disposition == disp_del)
12650 printf_filtered (_("Temporary breakpoint"));
12651 else
12652 printf_filtered (_("Breakpoint"));
12653 printf_filtered (_(" %d"), b->number);
12654 if (b->type == bp_gnu_ifunc_resolver)
12655 printf_filtered (_(" at gnu-indirect-function resolver"));
12656 break;
12657 case bp_hardware_breakpoint:
12658 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12659 break;
e7e0cddf
SS
12660 case bp_dprintf:
12661 printf_filtered (_("Dprintf %d"), b->number);
12662 break;
06edf0c0
PA
12663 }
12664
12665 say_where (b);
12666}
12667
2060206e 12668static void
06edf0c0
PA
12669bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12670{
12671 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12672 fprintf_unfiltered (fp, "tbreak");
12673 else if (tp->type == bp_breakpoint)
12674 fprintf_unfiltered (fp, "break");
12675 else if (tp->type == bp_hardware_breakpoint
12676 && tp->disposition == disp_del)
12677 fprintf_unfiltered (fp, "thbreak");
12678 else if (tp->type == bp_hardware_breakpoint)
12679 fprintf_unfiltered (fp, "hbreak");
12680 else
12681 internal_error (__FILE__, __LINE__,
12682 _("unhandled breakpoint type %d"), (int) tp->type);
12683
f00aae0f 12684 fprintf_unfiltered (fp, " %s",
d28cd78a 12685 event_location_to_string (tp->location.get ()));
f00aae0f
KS
12686
12687 /* Print out extra_string if this breakpoint is pending. It might
12688 contain, for example, conditions that were set by the user. */
12689 if (tp->loc == NULL && tp->extra_string != NULL)
12690 fprintf_unfiltered (fp, " %s", tp->extra_string);
12691
dd11a36c 12692 print_recreate_thread (tp, fp);
06edf0c0
PA
12693}
12694
983af33b 12695static void
f00aae0f
KS
12696bkpt_create_sals_from_location (const struct event_location *location,
12697 struct linespec_result *canonical,
12698 enum bptype type_wanted)
983af33b 12699{
f00aae0f 12700 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
12701}
12702
12703static void
12704bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12705 struct linespec_result *canonical,
e1e01040
PA
12706 gdb::unique_xmalloc_ptr<char> cond_string,
12707 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
12708 enum bptype type_wanted,
12709 enum bpdisp disposition,
12710 int thread,
12711 int task, int ignore_count,
12712 const struct breakpoint_ops *ops,
12713 int from_tty, int enabled,
44f238bb 12714 int internal, unsigned flags)
983af33b 12715{
023fa29b 12716 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
12717 std::move (cond_string),
12718 std::move (extra_string),
e7e0cddf 12719 type_wanted,
983af33b
SDJ
12720 disposition, thread, task,
12721 ignore_count, ops, from_tty,
44f238bb 12722 enabled, internal, flags);
983af33b
SDJ
12723}
12724
6c5b2ebe 12725static std::vector<symtab_and_line>
f00aae0f
KS
12726bkpt_decode_location (struct breakpoint *b,
12727 const struct event_location *location,
6c5b2ebe 12728 struct program_space *search_pspace)
983af33b 12729{
6c5b2ebe 12730 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
12731}
12732
06edf0c0
PA
12733/* Virtual table for internal breakpoints. */
12734
12735static void
12736internal_bkpt_re_set (struct breakpoint *b)
12737{
12738 switch (b->type)
12739 {
12740 /* Delete overlay event and longjmp master breakpoints; they
12741 will be reset later by breakpoint_re_set. */
12742 case bp_overlay_event:
12743 case bp_longjmp_master:
12744 case bp_std_terminate_master:
12745 case bp_exception_master:
12746 delete_breakpoint (b);
12747 break;
12748
12749 /* This breakpoint is special, it's set up when the inferior
12750 starts and we really don't want to touch it. */
12751 case bp_shlib_event:
12752
12753 /* Like bp_shlib_event, this breakpoint type is special. Once
12754 it is set up, we do not want to touch it. */
12755 case bp_thread_event:
12756 break;
12757 }
12758}
12759
12760static void
12761internal_bkpt_check_status (bpstat bs)
12762{
a9b3a50f
PA
12763 if (bs->breakpoint_at->type == bp_shlib_event)
12764 {
12765 /* If requested, stop when the dynamic linker notifies GDB of
12766 events. This allows the user to get control and place
12767 breakpoints in initializer routines for dynamically loaded
12768 objects (among other things). */
12769 bs->stop = stop_on_solib_events;
12770 bs->print = stop_on_solib_events;
12771 }
12772 else
12773 bs->stop = 0;
06edf0c0
PA
12774}
12775
12776static enum print_stop_action
12777internal_bkpt_print_it (bpstat bs)
12778{
06edf0c0 12779 struct breakpoint *b;
06edf0c0 12780
06edf0c0
PA
12781 b = bs->breakpoint_at;
12782
06edf0c0
PA
12783 switch (b->type)
12784 {
348d480f
PA
12785 case bp_shlib_event:
12786 /* Did we stop because the user set the stop_on_solib_events
12787 variable? (If so, we report this as a generic, "Stopped due
12788 to shlib event" message.) */
edcc5120 12789 print_solib_event (0);
348d480f
PA
12790 break;
12791
12792 case bp_thread_event:
12793 /* Not sure how we will get here.
12794 GDB should not stop for these breakpoints. */
12795 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12796 break;
12797
12798 case bp_overlay_event:
12799 /* By analogy with the thread event, GDB should not stop for these. */
12800 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12801 break;
12802
12803 case bp_longjmp_master:
12804 /* These should never be enabled. */
12805 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12806 break;
12807
12808 case bp_std_terminate_master:
12809 /* These should never be enabled. */
12810 printf_filtered (_("std::terminate Master Breakpoint: "
12811 "gdb should not stop!\n"));
348d480f
PA
12812 break;
12813
12814 case bp_exception_master:
12815 /* These should never be enabled. */
12816 printf_filtered (_("Exception Master Breakpoint: "
12817 "gdb should not stop!\n"));
06edf0c0
PA
12818 break;
12819 }
12820
001c8c33 12821 return PRINT_NOTHING;
06edf0c0
PA
12822}
12823
12824static void
12825internal_bkpt_print_mention (struct breakpoint *b)
12826{
12827 /* Nothing to mention. These breakpoints are internal. */
12828}
12829
06edf0c0
PA
12830/* Virtual table for momentary breakpoints */
12831
12832static void
12833momentary_bkpt_re_set (struct breakpoint *b)
12834{
12835 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 12836 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
12837 Otherwise these should have been blown away via the cleanup chain
12838 or by breakpoint_init_inferior when we rerun the executable. */
12839}
12840
12841static void
12842momentary_bkpt_check_status (bpstat bs)
12843{
12844 /* Nothing. The point of these breakpoints is causing a stop. */
12845}
12846
12847static enum print_stop_action
12848momentary_bkpt_print_it (bpstat bs)
12849{
001c8c33 12850 return PRINT_UNKNOWN;
348d480f
PA
12851}
12852
06edf0c0
PA
12853static void
12854momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12855{
06edf0c0 12856 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12857}
12858
e2e4d78b
JK
12859/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12860
12861 It gets cleared already on the removal of the first one of such placed
12862 breakpoints. This is OK as they get all removed altogether. */
12863
c1fc2657 12864longjmp_breakpoint::~longjmp_breakpoint ()
e2e4d78b 12865{
c1fc2657 12866 thread_info *tp = find_thread_global_id (this->thread);
e2e4d78b 12867
c1fc2657 12868 if (tp != NULL)
e2e4d78b 12869 tp->initiating_frame = null_frame_id;
e2e4d78b
JK
12870}
12871
55aa24fb
SDJ
12872/* Specific methods for probe breakpoints. */
12873
12874static int
12875bkpt_probe_insert_location (struct bp_location *bl)
12876{
12877 int v = bkpt_insert_location (bl);
12878
12879 if (v == 0)
12880 {
12881 /* The insertion was successful, now let's set the probe's semaphore
12882 if needed. */
0ea5cda8
SDJ
12883 if (bl->probe.probe->pops->set_semaphore != NULL)
12884 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
12885 bl->probe.objfile,
12886 bl->gdbarch);
55aa24fb
SDJ
12887 }
12888
12889 return v;
12890}
12891
12892static int
73971819
PA
12893bkpt_probe_remove_location (struct bp_location *bl,
12894 enum remove_bp_reason reason)
55aa24fb
SDJ
12895{
12896 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
12897 if (bl->probe.probe->pops->clear_semaphore != NULL)
12898 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
12899 bl->probe.objfile,
12900 bl->gdbarch);
55aa24fb 12901
73971819 12902 return bkpt_remove_location (bl, reason);
55aa24fb
SDJ
12903}
12904
12905static void
f00aae0f 12906bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 12907 struct linespec_result *canonical,
f00aae0f 12908 enum bptype type_wanted)
55aa24fb
SDJ
12909{
12910 struct linespec_sals lsal;
12911
c2f4122d 12912 lsal.sals = parse_probes (location, NULL, canonical);
8e9e35b1
TT
12913 lsal.canonical
12914 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 12915 canonical->lsals.push_back (std::move (lsal));
55aa24fb
SDJ
12916}
12917
6c5b2ebe 12918static std::vector<symtab_and_line>
f00aae0f
KS
12919bkpt_probe_decode_location (struct breakpoint *b,
12920 const struct event_location *location,
6c5b2ebe 12921 struct program_space *search_pspace)
55aa24fb 12922{
6c5b2ebe
PA
12923 std::vector<symtab_and_line> sals = parse_probes (location, search_pspace, NULL);
12924 if (sals.empty ())
55aa24fb 12925 error (_("probe not found"));
6c5b2ebe 12926 return sals;
55aa24fb
SDJ
12927}
12928
348d480f 12929/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12930
348d480f
PA
12931static void
12932tracepoint_re_set (struct breakpoint *b)
12933{
12934 breakpoint_re_set_default (b);
12935}
876fa593 12936
348d480f
PA
12937static int
12938tracepoint_breakpoint_hit (const struct bp_location *bl,
bd522513 12939 const address_space *aspace, CORE_ADDR bp_addr,
09ac7c10 12940 const struct target_waitstatus *ws)
348d480f
PA
12941{
12942 /* By definition, the inferior does not report stops at
12943 tracepoints. */
12944 return 0;
74960c60
VP
12945}
12946
12947static void
348d480f
PA
12948tracepoint_print_one_detail (const struct breakpoint *self,
12949 struct ui_out *uiout)
74960c60 12950{
d9b3f62e
PA
12951 struct tracepoint *tp = (struct tracepoint *) self;
12952 if (tp->static_trace_marker_id)
348d480f
PA
12953 {
12954 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12955
112e8700
SM
12956 uiout->text ("\tmarker id is ");
12957 uiout->field_string ("static-tracepoint-marker-string-id",
d9b3f62e 12958 tp->static_trace_marker_id);
112e8700 12959 uiout->text ("\n");
348d480f 12960 }
0d381245
VP
12961}
12962
a474d7c2 12963static void
348d480f 12964tracepoint_print_mention (struct breakpoint *b)
a474d7c2 12965{
112e8700 12966 if (current_uiout->is_mi_like_p ())
348d480f 12967 return;
cc59ec59 12968
348d480f
PA
12969 switch (b->type)
12970 {
12971 case bp_tracepoint:
12972 printf_filtered (_("Tracepoint"));
12973 printf_filtered (_(" %d"), b->number);
12974 break;
12975 case bp_fast_tracepoint:
12976 printf_filtered (_("Fast tracepoint"));
12977 printf_filtered (_(" %d"), b->number);
12978 break;
12979 case bp_static_tracepoint:
12980 printf_filtered (_("Static tracepoint"));
12981 printf_filtered (_(" %d"), b->number);
12982 break;
12983 default:
12984 internal_error (__FILE__, __LINE__,
12985 _("unhandled tracepoint type %d"), (int) b->type);
12986 }
12987
12988 say_where (b);
a474d7c2
PA
12989}
12990
348d480f 12991static void
d9b3f62e 12992tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 12993{
d9b3f62e
PA
12994 struct tracepoint *tp = (struct tracepoint *) self;
12995
12996 if (self->type == bp_fast_tracepoint)
348d480f 12997 fprintf_unfiltered (fp, "ftrace");
c93e8391 12998 else if (self->type == bp_static_tracepoint)
348d480f 12999 fprintf_unfiltered (fp, "strace");
d9b3f62e 13000 else if (self->type == bp_tracepoint)
348d480f
PA
13001 fprintf_unfiltered (fp, "trace");
13002 else
13003 internal_error (__FILE__, __LINE__,
d9b3f62e 13004 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13005
f00aae0f 13006 fprintf_unfiltered (fp, " %s",
d28cd78a 13007 event_location_to_string (self->location.get ()));
d9b3f62e
PA
13008 print_recreate_thread (self, fp);
13009
13010 if (tp->pass_count)
13011 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13012}
13013
983af33b 13014static void
f00aae0f
KS
13015tracepoint_create_sals_from_location (const struct event_location *location,
13016 struct linespec_result *canonical,
13017 enum bptype type_wanted)
983af33b 13018{
f00aae0f 13019 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13020}
13021
13022static void
13023tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13024 struct linespec_result *canonical,
e1e01040
PA
13025 gdb::unique_xmalloc_ptr<char> cond_string,
13026 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13027 enum bptype type_wanted,
13028 enum bpdisp disposition,
13029 int thread,
13030 int task, int ignore_count,
13031 const struct breakpoint_ops *ops,
13032 int from_tty, int enabled,
44f238bb 13033 int internal, unsigned flags)
983af33b 13034{
023fa29b 13035 create_breakpoints_sal_default (gdbarch, canonical,
e1e01040
PA
13036 std::move (cond_string),
13037 std::move (extra_string),
e7e0cddf 13038 type_wanted,
983af33b
SDJ
13039 disposition, thread, task,
13040 ignore_count, ops, from_tty,
44f238bb 13041 enabled, internal, flags);
983af33b
SDJ
13042}
13043
6c5b2ebe 13044static std::vector<symtab_and_line>
f00aae0f
KS
13045tracepoint_decode_location (struct breakpoint *b,
13046 const struct event_location *location,
6c5b2ebe 13047 struct program_space *search_pspace)
983af33b 13048{
6c5b2ebe 13049 return decode_location_default (b, location, search_pspace);
983af33b
SDJ
13050}
13051
2060206e 13052struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13053
55aa24fb
SDJ
13054/* The breakpoint_ops structure to be use on tracepoints placed in a
13055 static probe. */
13056
13057static void
f00aae0f
KS
13058tracepoint_probe_create_sals_from_location
13059 (const struct event_location *location,
13060 struct linespec_result *canonical,
13061 enum bptype type_wanted)
55aa24fb
SDJ
13062{
13063 /* We use the same method for breakpoint on probes. */
f00aae0f 13064 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13065}
13066
6c5b2ebe 13067static std::vector<symtab_and_line>
f00aae0f
KS
13068tracepoint_probe_decode_location (struct breakpoint *b,
13069 const struct event_location *location,
6c5b2ebe 13070 struct program_space *search_pspace)
55aa24fb
SDJ
13071{
13072 /* We use the same method for breakpoint on probes. */
6c5b2ebe 13073 return bkpt_probe_decode_location (b, location, search_pspace);
55aa24fb
SDJ
13074}
13075
13076static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13077
5c2b4418
HZ
13078/* Dprintf breakpoint_ops methods. */
13079
13080static void
13081dprintf_re_set (struct breakpoint *b)
13082{
13083 breakpoint_re_set_default (b);
13084
f00aae0f
KS
13085 /* extra_string should never be non-NULL for dprintf. */
13086 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13087
13088 /* 1 - connect to target 1, that can run breakpoint commands.
13089 2 - create a dprintf, which resolves fine.
13090 3 - disconnect from target 1
13091 4 - connect to target 2, that can NOT run breakpoint commands.
13092
13093 After steps #3/#4, you'll want the dprintf command list to
13094 be updated, because target 1 and 2 may well return different
13095 answers for target_can_run_breakpoint_commands().
13096 Given absence of finer grained resetting, we get to do
13097 it all the time. */
13098 if (b->extra_string != NULL)
13099 update_dprintf_command_list (b);
13100}
13101
2d9442cc
HZ
13102/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13103
13104static void
13105dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13106{
f00aae0f 13107 fprintf_unfiltered (fp, "dprintf %s,%s",
d28cd78a 13108 event_location_to_string (tp->location.get ()),
2d9442cc
HZ
13109 tp->extra_string);
13110 print_recreate_thread (tp, fp);
13111}
13112
9d6e6e84
HZ
13113/* Implement the "after_condition_true" breakpoint_ops method for
13114 dprintf.
13115
13116 dprintf's are implemented with regular commands in their command
13117 list, but we run the commands here instead of before presenting the
13118 stop to the user, as dprintf's don't actually cause a stop. This
13119 also makes it so that the commands of multiple dprintfs at the same
13120 address are all handled. */
13121
13122static void
13123dprintf_after_condition_true (struct bpstats *bs)
13124{
04afa70c 13125 struct bpstats tmp_bs;
9d6e6e84
HZ
13126 struct bpstats *tmp_bs_p = &tmp_bs;
13127
13128 /* dprintf's never cause a stop. This wasn't set in the
13129 check_status hook instead because that would make the dprintf's
13130 condition not be evaluated. */
13131 bs->stop = 0;
13132
13133 /* Run the command list here. Take ownership of it instead of
13134 copying. We never want these commands to run later in
13135 bpstat_do_actions, if a breakpoint that causes a stop happens to
13136 be set at same address as this dprintf, or even if running the
13137 commands here throws. */
13138 tmp_bs.commands = bs->commands;
13139 bs->commands = NULL;
9d6e6e84
HZ
13140
13141 bpstat_do_actions_1 (&tmp_bs_p);
13142
13143 /* 'tmp_bs.commands' will usually be NULL by now, but
13144 bpstat_do_actions_1 may return early without processing the whole
13145 list. */
9d6e6e84
HZ
13146}
13147
983af33b
SDJ
13148/* The breakpoint_ops structure to be used on static tracepoints with
13149 markers (`-m'). */
13150
13151static void
f00aae0f 13152strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13153 struct linespec_result *canonical,
f00aae0f 13154 enum bptype type_wanted)
983af33b
SDJ
13155{
13156 struct linespec_sals lsal;
f00aae0f 13157 const char *arg_start, *arg;
983af33b 13158
f00aae0f
KS
13159 arg = arg_start = get_linespec_location (location);
13160 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13161
f2fc3015
TT
13162 std::string str (arg_start, arg - arg_start);
13163 const char *ptr = str.c_str ();
13164 canonical->location = new_linespec_location (&ptr);
983af33b 13165
8e9e35b1
TT
13166 lsal.canonical
13167 = xstrdup (event_location_to_string (canonical->location.get ()));
6c5b2ebe 13168 canonical->lsals.push_back (std::move (lsal));
983af33b
SDJ
13169}
13170
13171static void
13172strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13173 struct linespec_result *canonical,
e1e01040
PA
13174 gdb::unique_xmalloc_ptr<char> cond_string,
13175 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13176 enum bptype type_wanted,
13177 enum bpdisp disposition,
13178 int thread,
13179 int task, int ignore_count,
13180 const struct breakpoint_ops *ops,
13181 int from_tty, int enabled,
44f238bb 13182 int internal, unsigned flags)
983af33b 13183{
6c5b2ebe 13184 const linespec_sals &lsal = canonical->lsals[0];
983af33b
SDJ
13185
13186 /* If the user is creating a static tracepoint by marker id
13187 (strace -m MARKER_ID), then store the sals index, so that
13188 breakpoint_re_set can try to match up which of the newly
13189 found markers corresponds to this one, and, don't try to
13190 expand multiple locations for each sal, given than SALS
13191 already should contain all sals for MARKER_ID. */
13192
6c5b2ebe 13193 for (size_t i = 0; i < lsal.sals.size (); i++)
983af33b 13194 {
6c5b2ebe
PA
13195 event_location_up location
13196 = copy_event_location (canonical->location.get ());
983af33b 13197
b270e6f9 13198 std::unique_ptr<tracepoint> tp (new tracepoint ());
6c5b2ebe 13199 init_breakpoint_sal (tp.get (), gdbarch, lsal.sals[i],
ffc2605c 13200 std::move (location), NULL,
e1e01040
PA
13201 std::move (cond_string),
13202 std::move (extra_string),
e7e0cddf 13203 type_wanted, disposition,
983af33b 13204 thread, task, ignore_count, ops,
44f238bb 13205 from_tty, enabled, internal, flags,
983af33b
SDJ
13206 canonical->special_display);
13207 /* Given that its possible to have multiple markers with
13208 the same string id, if the user is creating a static
13209 tracepoint by marker id ("strace -m MARKER_ID"), then
13210 store the sals index, so that breakpoint_re_set can
13211 try to match up which of the newly found markers
13212 corresponds to this one */
13213 tp->static_trace_marker_id_idx = i;
13214
b270e6f9 13215 install_breakpoint (internal, std::move (tp), 0);
983af33b
SDJ
13216 }
13217}
13218
6c5b2ebe 13219static std::vector<symtab_and_line>
f00aae0f
KS
13220strace_marker_decode_location (struct breakpoint *b,
13221 const struct event_location *location,
6c5b2ebe 13222 struct program_space *search_pspace)
983af33b
SDJ
13223{
13224 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13225 const char *s = get_linespec_location (location);
983af33b 13226
6c5b2ebe
PA
13227 std::vector<symtab_and_line> sals = decode_static_tracepoint_spec (&s);
13228 if (sals.size () > tp->static_trace_marker_id_idx)
983af33b 13229 {
6c5b2ebe
PA
13230 sals[0] = sals[tp->static_trace_marker_id_idx];
13231 sals.resize (1);
13232 return sals;
983af33b
SDJ
13233 }
13234 else
13235 error (_("marker %s not found"), tp->static_trace_marker_id);
13236}
13237
13238static struct breakpoint_ops strace_marker_breakpoint_ops;
13239
13240static int
13241strace_marker_p (struct breakpoint *b)
13242{
13243 return b->ops == &strace_marker_breakpoint_ops;
13244}
13245
53a5351d 13246/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13247 structures. */
c906108c
SS
13248
13249void
fba45db2 13250delete_breakpoint (struct breakpoint *bpt)
c906108c 13251{
52f0bd74 13252 struct breakpoint *b;
c906108c 13253
8a3fe4f8 13254 gdb_assert (bpt != NULL);
c906108c 13255
4a64f543
MS
13256 /* Has this bp already been deleted? This can happen because
13257 multiple lists can hold pointers to bp's. bpstat lists are
13258 especial culprits.
13259
13260 One example of this happening is a watchpoint's scope bp. When
13261 the scope bp triggers, we notice that the watchpoint is out of
13262 scope, and delete it. We also delete its scope bp. But the
13263 scope bp is marked "auto-deleting", and is already on a bpstat.
13264 That bpstat is then checked for auto-deleting bp's, which are
13265 deleted.
13266
13267 A real solution to this problem might involve reference counts in
13268 bp's, and/or giving them pointers back to their referencing
13269 bpstat's, and teaching delete_breakpoint to only free a bp's
13270 storage when no more references were extent. A cheaper bandaid
13271 was chosen. */
c906108c
SS
13272 if (bpt->type == bp_none)
13273 return;
13274
4a64f543
MS
13275 /* At least avoid this stale reference until the reference counting
13276 of breakpoints gets resolved. */
d0fb5eae 13277 if (bpt->related_breakpoint != bpt)
e5a0a904 13278 {
d0fb5eae 13279 struct breakpoint *related;
3a5c3e22 13280 struct watchpoint *w;
d0fb5eae
JK
13281
13282 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13283 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13284 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13285 w = (struct watchpoint *) bpt;
13286 else
13287 w = NULL;
13288 if (w != NULL)
13289 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13290
13291 /* Unlink bpt from the bpt->related_breakpoint ring. */
13292 for (related = bpt; related->related_breakpoint != bpt;
13293 related = related->related_breakpoint);
13294 related->related_breakpoint = bpt->related_breakpoint;
13295 bpt->related_breakpoint = bpt;
e5a0a904
JK
13296 }
13297
a9634178
TJB
13298 /* watch_command_1 creates a watchpoint but only sets its number if
13299 update_watchpoint succeeds in creating its bp_locations. If there's
13300 a problem in that process, we'll be asked to delete the half-created
13301 watchpoint. In that case, don't announce the deletion. */
13302 if (bpt->number)
13303 observer_notify_breakpoint_deleted (bpt);
c906108c 13304
c906108c
SS
13305 if (breakpoint_chain == bpt)
13306 breakpoint_chain = bpt->next;
13307
c906108c
SS
13308 ALL_BREAKPOINTS (b)
13309 if (b->next == bpt)
c5aa993b
JM
13310 {
13311 b->next = bpt->next;
13312 break;
13313 }
c906108c 13314
f431efe5
PA
13315 /* Be sure no bpstat's are pointing at the breakpoint after it's
13316 been freed. */
13317 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13318 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13319 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13320 commands are associated with the bpstat; if we remove it here,
13321 then the later call to bpstat_do_actions (&stop_bpstat); in
13322 event-top.c won't do anything, and temporary breakpoints with
13323 commands won't work. */
13324
13325 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13326
4a64f543
MS
13327 /* Now that breakpoint is removed from breakpoint list, update the
13328 global location list. This will remove locations that used to
13329 belong to this breakpoint. Do this before freeing the breakpoint
13330 itself, since remove_breakpoint looks at location's owner. It
13331 might be better design to have location completely
13332 self-contained, but it's not the case now. */
44702360 13333 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13334
4a64f543
MS
13335 /* On the chance that someone will soon try again to delete this
13336 same bp, we mark it as deleted before freeing its storage. */
c906108c 13337 bpt->type = bp_none;
4d01a485 13338 delete bpt;
c906108c
SS
13339}
13340
51be5b68
PA
13341/* Iterator function to call a user-provided callback function once
13342 for each of B and its related breakpoints. */
13343
13344static void
13345iterate_over_related_breakpoints (struct breakpoint *b,
48649e1b 13346 gdb::function_view<void (breakpoint *)> function)
51be5b68
PA
13347{
13348 struct breakpoint *related;
13349
13350 related = b;
13351 do
13352 {
13353 struct breakpoint *next;
13354
13355 /* FUNCTION may delete RELATED. */
13356 next = related->related_breakpoint;
13357
13358 if (next == related)
13359 {
13360 /* RELATED is the last ring entry. */
48649e1b 13361 function (related);
51be5b68
PA
13362
13363 /* FUNCTION may have deleted it, so we'd never reach back to
13364 B. There's nothing left to do anyway, so just break
13365 out. */
13366 break;
13367 }
13368 else
48649e1b 13369 function (related);
51be5b68
PA
13370
13371 related = next;
13372 }
13373 while (related != b);
13374}
95a42b64 13375
4495129a 13376static void
981a3fb3 13377delete_command (const char *arg, int from_tty)
c906108c 13378{
35df4500 13379 struct breakpoint *b, *b_tmp;
c906108c 13380
ea9365bb
TT
13381 dont_repeat ();
13382
c906108c
SS
13383 if (arg == 0)
13384 {
13385 int breaks_to_delete = 0;
13386
46c6471b
PA
13387 /* Delete all breakpoints if no argument. Do not delete
13388 internal breakpoints, these have to be deleted with an
13389 explicit breakpoint number argument. */
c5aa993b 13390 ALL_BREAKPOINTS (b)
46c6471b 13391 if (user_breakpoint_p (b))
973d738b
DJ
13392 {
13393 breaks_to_delete = 1;
13394 break;
13395 }
c906108c
SS
13396
13397 /* Ask user only if there are some breakpoints to delete. */
13398 if (!from_tty
e2e0b3e5 13399 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13400 {
35df4500 13401 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13402 if (user_breakpoint_p (b))
c5aa993b 13403 delete_breakpoint (b);
c906108c
SS
13404 }
13405 }
13406 else
48649e1b
TT
13407 map_breakpoint_numbers
13408 (arg, [&] (breakpoint *b)
13409 {
13410 iterate_over_related_breakpoints (b, delete_breakpoint);
13411 });
c906108c
SS
13412}
13413
c2f4122d
PA
13414/* Return true if all locations of B bound to PSPACE are pending. If
13415 PSPACE is NULL, all locations of all program spaces are
13416 considered. */
13417
0d381245 13418static int
c2f4122d 13419all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
fe3f5fa8 13420{
c2f4122d
PA
13421 struct bp_location *loc;
13422
13423 for (loc = b->loc; loc != NULL; loc = loc->next)
13424 if ((pspace == NULL
13425 || loc->pspace == pspace)
13426 && !loc->shlib_disabled
8645ff69 13427 && !loc->pspace->executing_startup)
0d381245
VP
13428 return 0;
13429 return 1;
fe3f5fa8
VP
13430}
13431
776592bf
DE
13432/* Subroutine of update_breakpoint_locations to simplify it.
13433 Return non-zero if multiple fns in list LOC have the same name.
13434 Null names are ignored. */
13435
13436static int
13437ambiguous_names_p (struct bp_location *loc)
13438{
13439 struct bp_location *l;
13440 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13441 (int (*) (const void *,
13442 const void *)) streq,
776592bf
DE
13443 NULL, xcalloc, xfree);
13444
13445 for (l = loc; l != NULL; l = l->next)
13446 {
13447 const char **slot;
13448 const char *name = l->function_name;
13449
13450 /* Allow for some names to be NULL, ignore them. */
13451 if (name == NULL)
13452 continue;
13453
13454 slot = (const char **) htab_find_slot (htab, (const void *) name,
13455 INSERT);
4a64f543
MS
13456 /* NOTE: We can assume slot != NULL here because xcalloc never
13457 returns NULL. */
776592bf
DE
13458 if (*slot != NULL)
13459 {
13460 htab_delete (htab);
13461 return 1;
13462 }
13463 *slot = name;
13464 }
13465
13466 htab_delete (htab);
13467 return 0;
13468}
13469
0fb4aa4b
PA
13470/* When symbols change, it probably means the sources changed as well,
13471 and it might mean the static tracepoint markers are no longer at
13472 the same address or line numbers they used to be at last we
13473 checked. Losing your static tracepoints whenever you rebuild is
13474 undesirable. This function tries to resync/rematch gdb static
13475 tracepoints with the markers on the target, for static tracepoints
13476 that have not been set by marker id. Static tracepoint that have
13477 been set by marker id are reset by marker id in breakpoint_re_set.
13478 The heuristic is:
13479
13480 1) For a tracepoint set at a specific address, look for a marker at
13481 the old PC. If one is found there, assume to be the same marker.
13482 If the name / string id of the marker found is different from the
13483 previous known name, assume that means the user renamed the marker
13484 in the sources, and output a warning.
13485
13486 2) For a tracepoint set at a given line number, look for a marker
13487 at the new address of the old line number. If one is found there,
13488 assume to be the same marker. If the name / string id of the
13489 marker found is different from the previous known name, assume that
13490 means the user renamed the marker in the sources, and output a
13491 warning.
13492
13493 3) If a marker is no longer found at the same address or line, it
13494 may mean the marker no longer exists. But it may also just mean
13495 the code changed a bit. Maybe the user added a few lines of code
13496 that made the marker move up or down (in line number terms). Ask
13497 the target for info about the marker with the string id as we knew
13498 it. If found, update line number and address in the matching
13499 static tracepoint. This will get confused if there's more than one
13500 marker with the same ID (possible in UST, although unadvised
13501 precisely because it confuses tools). */
13502
13503static struct symtab_and_line
13504update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13505{
d9b3f62e 13506 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13507 struct static_tracepoint_marker marker;
13508 CORE_ADDR pc;
0fb4aa4b
PA
13509
13510 pc = sal.pc;
13511 if (sal.line)
13512 find_line_pc (sal.symtab, sal.line, &pc);
13513
13514 if (target_static_tracepoint_marker_at (pc, &marker))
13515 {
d9b3f62e 13516 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13517 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13518 b->number,
d9b3f62e 13519 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13520
d9b3f62e
PA
13521 xfree (tp->static_trace_marker_id);
13522 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13523 release_static_tracepoint_marker (&marker);
13524
13525 return sal;
13526 }
13527
13528 /* Old marker wasn't found on target at lineno. Try looking it up
13529 by string ID. */
13530 if (!sal.explicit_pc
13531 && sal.line != 0
13532 && sal.symtab != NULL
d9b3f62e 13533 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13534 {
13535 VEC(static_tracepoint_marker_p) *markers;
13536
13537 markers
d9b3f62e 13538 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13539
13540 if (!VEC_empty(static_tracepoint_marker_p, markers))
13541 {
0fb4aa4b 13542 struct symbol *sym;
80e1d417 13543 struct static_tracepoint_marker *tpmarker;
79a45e25 13544 struct ui_out *uiout = current_uiout;
67994074 13545 struct explicit_location explicit_loc;
0fb4aa4b 13546
80e1d417 13547 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13548
d9b3f62e 13549 xfree (tp->static_trace_marker_id);
80e1d417 13550 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13551
13552 warning (_("marker for static tracepoint %d (%s) not "
13553 "found at previous line number"),
d9b3f62e 13554 b->number, tp->static_trace_marker_id);
0fb4aa4b 13555
51abb421 13556 symtab_and_line sal2 = find_pc_line (tpmarker->address, 0);
80e1d417 13557 sym = find_pc_sect_function (tpmarker->address, NULL);
112e8700 13558 uiout->text ("Now in ");
0fb4aa4b
PA
13559 if (sym)
13560 {
112e8700
SM
13561 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
13562 uiout->text (" at ");
0fb4aa4b 13563 }
112e8700 13564 uiout->field_string ("file",
05cba821 13565 symtab_to_filename_for_display (sal2.symtab));
112e8700 13566 uiout->text (":");
0fb4aa4b 13567
112e8700 13568 if (uiout->is_mi_like_p ())
0fb4aa4b 13569 {
0b0865da 13570 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13571
112e8700 13572 uiout->field_string ("fullname", fullname);
0fb4aa4b
PA
13573 }
13574
112e8700
SM
13575 uiout->field_int ("line", sal2.line);
13576 uiout->text ("\n");
0fb4aa4b 13577
80e1d417 13578 b->loc->line_number = sal2.line;
2f202fde 13579 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13580
d28cd78a 13581 b->location.reset (NULL);
67994074
KS
13582 initialize_explicit_location (&explicit_loc);
13583 explicit_loc.source_filename
00e52e53 13584 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13585 explicit_loc.line_offset.offset = b->loc->line_number;
13586 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
d28cd78a 13587 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13588
13589 /* Might be nice to check if function changed, and warn if
13590 so. */
13591
80e1d417 13592 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13593 }
13594 }
13595 return sal;
13596}
13597
8d3788bd
VP
13598/* Returns 1 iff locations A and B are sufficiently same that
13599 we don't need to report breakpoint as changed. */
13600
13601static int
13602locations_are_equal (struct bp_location *a, struct bp_location *b)
13603{
13604 while (a && b)
13605 {
13606 if (a->address != b->address)
13607 return 0;
13608
13609 if (a->shlib_disabled != b->shlib_disabled)
13610 return 0;
13611
13612 if (a->enabled != b->enabled)
13613 return 0;
13614
13615 a = a->next;
13616 b = b->next;
13617 }
13618
13619 if ((a == NULL) != (b == NULL))
13620 return 0;
13621
13622 return 1;
13623}
13624
c2f4122d
PA
13625/* Split all locations of B that are bound to PSPACE out of B's
13626 location list to a separate list and return that list's head. If
13627 PSPACE is NULL, hoist out all locations of B. */
13628
13629static struct bp_location *
13630hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
13631{
13632 struct bp_location head;
13633 struct bp_location *i = b->loc;
13634 struct bp_location **i_link = &b->loc;
13635 struct bp_location *hoisted = &head;
13636
13637 if (pspace == NULL)
13638 {
13639 i = b->loc;
13640 b->loc = NULL;
13641 return i;
13642 }
13643
13644 head.next = NULL;
13645
13646 while (i != NULL)
13647 {
13648 if (i->pspace == pspace)
13649 {
13650 *i_link = i->next;
13651 i->next = NULL;
13652 hoisted->next = i;
13653 hoisted = i;
13654 }
13655 else
13656 i_link = &i->next;
13657 i = *i_link;
13658 }
13659
13660 return head.next;
13661}
13662
13663/* Create new breakpoint locations for B (a hardware or software
13664 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
13665 zero, then B is a ranged breakpoint. Only recreates locations for
13666 FILTER_PSPACE. Locations of other program spaces are left
13667 untouched. */
f1310107 13668
0e30163f 13669void
0d381245 13670update_breakpoint_locations (struct breakpoint *b,
c2f4122d 13671 struct program_space *filter_pspace,
6c5b2ebe
PA
13672 gdb::array_view<const symtab_and_line> sals,
13673 gdb::array_view<const symtab_and_line> sals_end)
fe3f5fa8
VP
13674{
13675 int i;
c2f4122d 13676 struct bp_location *existing_locations;
0d381245 13677
6c5b2ebe 13678 if (!sals_end.empty () && (sals.size () != 1 || sals_end.size () != 1))
f8eba3c6
TT
13679 {
13680 /* Ranged breakpoints have only one start location and one end
13681 location. */
13682 b->enable_state = bp_disabled;
f8eba3c6
TT
13683 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13684 "multiple locations found\n"),
13685 b->number);
13686 return;
13687 }
f1310107 13688
4a64f543
MS
13689 /* If there's no new locations, and all existing locations are
13690 pending, don't do anything. This optimizes the common case where
13691 all locations are in the same shared library, that was unloaded.
13692 We'd like to retain the location, so that when the library is
13693 loaded again, we don't loose the enabled/disabled status of the
13694 individual locations. */
6c5b2ebe 13695 if (all_locations_are_pending (b, filter_pspace) && sals.empty ())
fe3f5fa8
VP
13696 return;
13697
c2f4122d 13698 existing_locations = hoist_existing_locations (b, filter_pspace);
fe3f5fa8 13699
6c5b2ebe 13700 for (const auto &sal : sals)
fe3f5fa8 13701 {
f8eba3c6
TT
13702 struct bp_location *new_loc;
13703
6c5b2ebe 13704 switch_to_program_space_and_thread (sal.pspace);
f8eba3c6 13705
6c5b2ebe 13706 new_loc = add_location_to_breakpoint (b, &sal);
fe3f5fa8 13707
0d381245
VP
13708 /* Reparse conditions, they might contain references to the
13709 old symtab. */
13710 if (b->cond_string != NULL)
13711 {
bbc13ae3 13712 const char *s;
fe3f5fa8 13713
0d381245 13714 s = b->cond_string;
492d29ea 13715 TRY
0d381245 13716 {
6c5b2ebe
PA
13717 new_loc->cond = parse_exp_1 (&s, sal.pc,
13718 block_for_pc (sal.pc),
0d381245
VP
13719 0);
13720 }
492d29ea 13721 CATCH (e, RETURN_MASK_ERROR)
0d381245 13722 {
3e43a32a
MS
13723 warning (_("failed to reevaluate condition "
13724 "for breakpoint %d: %s"),
0d381245
VP
13725 b->number, e.message);
13726 new_loc->enabled = 0;
13727 }
492d29ea 13728 END_CATCH
0d381245 13729 }
fe3f5fa8 13730
6c5b2ebe 13731 if (!sals_end.empty ())
f1310107 13732 {
6c5b2ebe 13733 CORE_ADDR end = find_breakpoint_range_end (sals_end[0]);
f1310107 13734
6c5b2ebe 13735 new_loc->length = end - sals[0].pc + 1;
f1310107 13736 }
0d381245 13737 }
fe3f5fa8 13738
4a64f543
MS
13739 /* If possible, carry over 'disable' status from existing
13740 breakpoints. */
0d381245
VP
13741 {
13742 struct bp_location *e = existing_locations;
776592bf
DE
13743 /* If there are multiple breakpoints with the same function name,
13744 e.g. for inline functions, comparing function names won't work.
13745 Instead compare pc addresses; this is just a heuristic as things
13746 may have moved, but in practice it gives the correct answer
13747 often enough until a better solution is found. */
13748 int have_ambiguous_names = ambiguous_names_p (b->loc);
13749
0d381245
VP
13750 for (; e; e = e->next)
13751 {
13752 if (!e->enabled && e->function_name)
13753 {
13754 struct bp_location *l = b->loc;
776592bf
DE
13755 if (have_ambiguous_names)
13756 {
13757 for (; l; l = l->next)
f1310107 13758 if (breakpoint_locations_match (e, l))
776592bf
DE
13759 {
13760 l->enabled = 0;
13761 break;
13762 }
13763 }
13764 else
13765 {
13766 for (; l; l = l->next)
13767 if (l->function_name
13768 && strcmp (e->function_name, l->function_name) == 0)
13769 {
13770 l->enabled = 0;
13771 break;
13772 }
13773 }
0d381245
VP
13774 }
13775 }
13776 }
fe3f5fa8 13777
8d3788bd
VP
13778 if (!locations_are_equal (existing_locations, b->loc))
13779 observer_notify_breakpoint_modified (b);
fe3f5fa8
VP
13780}
13781
f00aae0f 13782/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
13783 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13784
6c5b2ebe 13785static std::vector<symtab_and_line>
f00aae0f 13786location_to_sals (struct breakpoint *b, struct event_location *location,
c2f4122d 13787 struct program_space *search_pspace, int *found)
ef23e705 13788{
492d29ea 13789 struct gdb_exception exception = exception_none;
ef23e705 13790
983af33b 13791 gdb_assert (b->ops != NULL);
ef23e705 13792
6c5b2ebe
PA
13793 std::vector<symtab_and_line> sals;
13794
492d29ea 13795 TRY
ef23e705 13796 {
6c5b2ebe 13797 sals = b->ops->decode_location (b, location, search_pspace);
ef23e705 13798 }
492d29ea 13799 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
13800 {
13801 int not_found_and_ok = 0;
492d29ea
PA
13802
13803 exception = e;
13804
ef23e705
TJB
13805 /* For pending breakpoints, it's expected that parsing will
13806 fail until the right shared library is loaded. User has
13807 already told to create pending breakpoints and don't need
13808 extra messages. If breakpoint is in bp_shlib_disabled
13809 state, then user already saw the message about that
13810 breakpoint being disabled, and don't want to see more
13811 errors. */
58438ac1 13812 if (e.error == NOT_FOUND_ERROR
c2f4122d
PA
13813 && (b->condition_not_parsed
13814 || (b->loc != NULL
13815 && search_pspace != NULL
13816 && b->loc->pspace != search_pspace)
ef23e705 13817 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13818 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13819 || b->enable_state == bp_disabled))
13820 not_found_and_ok = 1;
13821
13822 if (!not_found_and_ok)
13823 {
13824 /* We surely don't want to warn about the same breakpoint
13825 10 times. One solution, implemented here, is disable
13826 the breakpoint on error. Another solution would be to
13827 have separate 'warning emitted' flag. Since this
13828 happens only when a binary has changed, I don't know
13829 which approach is better. */
13830 b->enable_state = bp_disabled;
13831 throw_exception (e);
13832 }
13833 }
492d29ea 13834 END_CATCH
ef23e705 13835
492d29ea 13836 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 13837 {
6c5b2ebe
PA
13838 for (auto &sal : sals)
13839 resolve_sal_pc (&sal);
f00aae0f 13840 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 13841 {
ed1d1739
KS
13842 char *cond_string, *extra_string;
13843 int thread, task;
ef23e705 13844
6c5b2ebe 13845 find_condition_and_thread (b->extra_string, sals[0].pc,
e7e0cddf
SS
13846 &cond_string, &thread, &task,
13847 &extra_string);
f00aae0f 13848 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
13849 if (cond_string)
13850 b->cond_string = cond_string;
13851 b->thread = thread;
13852 b->task = task;
e7e0cddf 13853 if (extra_string)
f00aae0f
KS
13854 {
13855 xfree (b->extra_string);
13856 b->extra_string = extra_string;
13857 }
ef23e705
TJB
13858 b->condition_not_parsed = 0;
13859 }
13860
983af33b 13861 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
6c5b2ebe 13862 sals[0] = update_static_tracepoint (b, sals[0]);
ef23e705 13863
58438ac1
TT
13864 *found = 1;
13865 }
13866 else
13867 *found = 0;
ef23e705
TJB
13868
13869 return sals;
13870}
13871
348d480f
PA
13872/* The default re_set method, for typical hardware or software
13873 breakpoints. Reevaluate the breakpoint and recreate its
13874 locations. */
13875
13876static void
28010a5d 13877breakpoint_re_set_default (struct breakpoint *b)
ef23e705 13878{
c2f4122d 13879 struct program_space *filter_pspace = current_program_space;
6c5b2ebe 13880 std::vector<symtab_and_line> expanded, expanded_end;
ef23e705 13881
6c5b2ebe
PA
13882 int found;
13883 std::vector<symtab_and_line> sals = location_to_sals (b, b->location.get (),
13884 filter_pspace, &found);
ef23e705 13885 if (found)
6c5b2ebe 13886 expanded = std::move (sals);
ef23e705 13887
f00aae0f 13888 if (b->location_range_end != NULL)
f1310107 13889 {
6c5b2ebe
PA
13890 std::vector<symtab_and_line> sals_end
13891 = location_to_sals (b, b->location_range_end.get (),
13892 filter_pspace, &found);
f1310107 13893 if (found)
6c5b2ebe 13894 expanded_end = std::move (sals_end);
f1310107
TJB
13895 }
13896
c2f4122d 13897 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
28010a5d
PA
13898}
13899
983af33b
SDJ
13900/* Default method for creating SALs from an address string. It basically
13901 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13902
13903static void
f00aae0f
KS
13904create_sals_from_location_default (const struct event_location *location,
13905 struct linespec_result *canonical,
13906 enum bptype type_wanted)
983af33b 13907{
f00aae0f 13908 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
13909}
13910
13911/* Call create_breakpoints_sal for the given arguments. This is the default
13912 function for the `create_breakpoints_sal' method of
13913 breakpoint_ops. */
13914
13915static void
13916create_breakpoints_sal_default (struct gdbarch *gdbarch,
13917 struct linespec_result *canonical,
e1e01040
PA
13918 gdb::unique_xmalloc_ptr<char> cond_string,
13919 gdb::unique_xmalloc_ptr<char> extra_string,
983af33b
SDJ
13920 enum bptype type_wanted,
13921 enum bpdisp disposition,
13922 int thread,
13923 int task, int ignore_count,
13924 const struct breakpoint_ops *ops,
13925 int from_tty, int enabled,
44f238bb 13926 int internal, unsigned flags)
983af33b 13927{
e1e01040
PA
13928 create_breakpoints_sal (gdbarch, canonical,
13929 std::move (cond_string),
13930 std::move (extra_string),
983af33b
SDJ
13931 type_wanted, disposition,
13932 thread, task, ignore_count, ops, from_tty,
44f238bb 13933 enabled, internal, flags);
983af33b
SDJ
13934}
13935
13936/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 13937 default function for the `decode_location' method of breakpoint_ops. */
983af33b 13938
6c5b2ebe 13939static std::vector<symtab_and_line>
f00aae0f
KS
13940decode_location_default (struct breakpoint *b,
13941 const struct event_location *location,
6c5b2ebe 13942 struct program_space *search_pspace)
983af33b
SDJ
13943{
13944 struct linespec_result canonical;
13945
c2f4122d 13946 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
983af33b
SDJ
13947 (struct symtab *) NULL, 0,
13948 &canonical, multiple_symbols_all,
13949 b->filter);
13950
13951 /* We should get 0 or 1 resulting SALs. */
6c5b2ebe 13952 gdb_assert (canonical.lsals.size () < 2);
983af33b 13953
6c5b2ebe 13954 if (!canonical.lsals.empty ())
983af33b 13955 {
6c5b2ebe
PA
13956 const linespec_sals &lsal = canonical.lsals[0];
13957 return std::move (lsal.sals);
983af33b 13958 }
6c5b2ebe 13959 return {};
983af33b
SDJ
13960}
13961
bf469271 13962/* Reset a breakpoint. */
c906108c 13963
bf469271
PA
13964static void
13965breakpoint_re_set_one (breakpoint *b)
c906108c 13966{
fdf44873
TT
13967 input_radix = b->input_radix;
13968 set_language (b->language);
c906108c 13969
348d480f 13970 b->ops->re_set (b);
c906108c
SS
13971}
13972
c2f4122d
PA
13973/* Re-set breakpoint locations for the current program space.
13974 Locations bound to other program spaces are left untouched. */
13975
c906108c 13976void
69de3c6a 13977breakpoint_re_set (void)
c906108c 13978{
35df4500 13979 struct breakpoint *b, *b_tmp;
2a7f3dff 13980
c5aa993b 13981 {
fdf44873
TT
13982 scoped_restore_current_language save_language;
13983 scoped_restore save_input_radix = make_scoped_restore (&input_radix);
5ed8105e 13984 scoped_restore_current_pspace_and_thread restore_pspace_thread;
e62c965a 13985
5ed8105e
PA
13986 /* Note: we must not try to insert locations until after all
13987 breakpoints have been re-set. Otherwise, e.g., when re-setting
13988 breakpoint 1, we'd insert the locations of breakpoint 2, which
13989 hadn't been re-set yet, and thus may have stale locations. */
4efc6507 13990
5ed8105e
PA
13991 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13992 {
bf469271
PA
13993 TRY
13994 {
13995 breakpoint_re_set_one (b);
13996 }
13997 CATCH (ex, RETURN_MASK_ALL)
13998 {
13999 exception_fprintf (gdb_stderr, ex,
14000 "Error in re-setting breakpoint %d: ",
14001 b->number);
14002 }
14003 END_CATCH
5ed8105e 14004 }
5ed8105e
PA
14005
14006 jit_breakpoint_re_set ();
14007 }
6c95b8df 14008
af02033e
PP
14009 create_overlay_event_breakpoint ();
14010 create_longjmp_master_breakpoint ();
14011 create_std_terminate_master_breakpoint ();
186c406b 14012 create_exception_master_breakpoint ();
2a7f3dff
PA
14013
14014 /* Now we can insert. */
14015 update_global_location_list (UGLL_MAY_INSERT);
c906108c
SS
14016}
14017\f
c906108c
SS
14018/* Reset the thread number of this breakpoint:
14019
14020 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14021 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14022void
fba45db2 14023breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14024{
14025 if (b->thread != -1)
14026 {
39f77062 14027 if (in_thread_list (inferior_ptid))
5d5658a1 14028 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14029
14030 /* We're being called after following a fork. The new fork is
14031 selected as current, and unless this was a vfork will have a
14032 different program space from the original thread. Reset that
14033 as well. */
14034 b->loc->pspace = current_program_space;
c906108c
SS
14035 }
14036}
14037
03ac34d5
MS
14038/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14039 If from_tty is nonzero, it prints a message to that effect,
14040 which ends with a period (no newline). */
14041
c906108c 14042void
fba45db2 14043set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14044{
52f0bd74 14045 struct breakpoint *b;
c906108c
SS
14046
14047 if (count < 0)
14048 count = 0;
14049
14050 ALL_BREAKPOINTS (b)
14051 if (b->number == bptnum)
c5aa993b 14052 {
d77f58be
SS
14053 if (is_tracepoint (b))
14054 {
14055 if (from_tty && count != 0)
14056 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14057 bptnum);
14058 return;
14059 }
14060
c5aa993b 14061 b->ignore_count = count;
221ea385
KS
14062 if (from_tty)
14063 {
14064 if (count == 0)
3e43a32a
MS
14065 printf_filtered (_("Will stop next time "
14066 "breakpoint %d is reached."),
221ea385
KS
14067 bptnum);
14068 else if (count == 1)
a3f17187 14069 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14070 bptnum);
14071 else
3e43a32a
MS
14072 printf_filtered (_("Will ignore next %d "
14073 "crossings of breakpoint %d."),
221ea385
KS
14074 count, bptnum);
14075 }
8d3788bd 14076 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14077 return;
14078 }
c906108c 14079
8a3fe4f8 14080 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14081}
14082
c906108c
SS
14083/* Command to set ignore-count of breakpoint N to COUNT. */
14084
14085static void
0b39b52e 14086ignore_command (const char *args, int from_tty)
c906108c 14087{
0b39b52e 14088 const char *p = args;
52f0bd74 14089 int num;
c906108c
SS
14090
14091 if (p == 0)
e2e0b3e5 14092 error_no_arg (_("a breakpoint number"));
c5aa993b 14093
c906108c 14094 num = get_number (&p);
5c44784c 14095 if (num == 0)
8a3fe4f8 14096 error (_("bad breakpoint number: '%s'"), args);
c906108c 14097 if (*p == 0)
8a3fe4f8 14098 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14099
14100 set_ignore_count (num,
14101 longest_to_int (value_as_long (parse_and_eval (p))),
14102 from_tty);
221ea385
KS
14103 if (from_tty)
14104 printf_filtered ("\n");
c906108c
SS
14105}
14106\f
d0fe4701
XR
14107
14108/* Call FUNCTION on each of the breakpoints with numbers in the range
14109 defined by BP_NUM_RANGE (an inclusive range). */
c906108c
SS
14110
14111static void
d0fe4701
XR
14112map_breakpoint_number_range (std::pair<int, int> bp_num_range,
14113 gdb::function_view<void (breakpoint *)> function)
c906108c 14114{
d0fe4701
XR
14115 if (bp_num_range.first == 0)
14116 {
14117 warning (_("bad breakpoint number at or near '%d'"),
14118 bp_num_range.first);
14119 }
14120 else
c906108c 14121 {
d0fe4701 14122 struct breakpoint *b, *tmp;
197f0a60 14123
d0fe4701 14124 for (int i = bp_num_range.first; i <= bp_num_range.second; i++)
5c44784c 14125 {
d0fe4701
XR
14126 bool match = false;
14127
5c44784c 14128 ALL_BREAKPOINTS_SAFE (b, tmp)
d0fe4701 14129 if (b->number == i)
5c44784c 14130 {
bfd28288 14131 match = true;
48649e1b 14132 function (b);
11cf8741 14133 break;
5c44784c 14134 }
bfd28288 14135 if (!match)
d0fe4701 14136 printf_unfiltered (_("No breakpoint number %d.\n"), i);
c5aa993b 14137 }
c906108c
SS
14138 }
14139}
14140
d0fe4701
XR
14141/* Call FUNCTION on each of the breakpoints whose numbers are given in
14142 ARGS. */
14143
14144static void
14145map_breakpoint_numbers (const char *args,
14146 gdb::function_view<void (breakpoint *)> function)
14147{
14148 if (args == NULL || *args == '\0')
14149 error_no_arg (_("one or more breakpoint numbers"));
14150
14151 number_or_range_parser parser (args);
14152
14153 while (!parser.finished ())
14154 {
14155 int num = parser.get_number ();
14156 map_breakpoint_number_range (std::make_pair (num, num), function);
14157 }
14158}
14159
14160/* Return the breakpoint location structure corresponding to the
14161 BP_NUM and LOC_NUM values. */
14162
0d381245 14163static struct bp_location *
d0fe4701 14164find_location_by_number (int bp_num, int loc_num)
0d381245 14165{
0d381245 14166 struct breakpoint *b;
0d381245
VP
14167
14168 ALL_BREAKPOINTS (b)
14169 if (b->number == bp_num)
14170 {
14171 break;
14172 }
14173
14174 if (!b || b->number != bp_num)
d0fe4701 14175 error (_("Bad breakpoint number '%d'"), bp_num);
0d381245 14176
0d381245 14177 if (loc_num == 0)
d0fe4701 14178 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245 14179
d0fe4701
XR
14180 int n = 0;
14181 for (bp_location *loc = b->loc; loc != NULL; loc = loc->next)
14182 if (++n == loc_num)
14183 return loc;
14184
14185 error (_("Bad breakpoint location number '%d'"), loc_num);
0d381245
VP
14186}
14187
95e95a6d
PA
14188/* Modes of operation for extract_bp_num. */
14189enum class extract_bp_kind
14190{
14191 /* Extracting a breakpoint number. */
14192 bp,
14193
14194 /* Extracting a location number. */
14195 loc,
14196};
14197
14198/* Extract a breakpoint or location number (as determined by KIND)
14199 from the string starting at START. TRAILER is a character which
14200 can be found after the number. If you don't want a trailer, use
14201 '\0'. If END_OUT is not NULL, it is set to point after the parsed
14202 string. This always returns a positive integer. */
14203
14204static int
14205extract_bp_num (extract_bp_kind kind, const char *start,
14206 int trailer, const char **end_out = NULL)
14207{
14208 const char *end = start;
14209 int num = get_number_trailer (&end, trailer);
14210 if (num < 0)
14211 error (kind == extract_bp_kind::bp
14212 ? _("Negative breakpoint number '%.*s'")
14213 : _("Negative breakpoint location number '%.*s'"),
14214 int (end - start), start);
14215 if (num == 0)
14216 error (kind == extract_bp_kind::bp
14217 ? _("Bad breakpoint number '%.*s'")
14218 : _("Bad breakpoint location number '%.*s'"),
14219 int (end - start), start);
14220
14221 if (end_out != NULL)
14222 *end_out = end;
14223 return num;
14224}
14225
14226/* Extract a breakpoint or location range (as determined by KIND) in
14227 the form NUM1-NUM2 stored at &ARG[arg_offset]. Returns a std::pair
14228 representing the (inclusive) range. The returned pair's elements
14229 are always positive integers. */
14230
14231static std::pair<int, int>
14232extract_bp_or_bp_range (extract_bp_kind kind,
14233 const std::string &arg,
14234 std::string::size_type arg_offset)
14235{
14236 std::pair<int, int> range;
14237 const char *bp_loc = &arg[arg_offset];
14238 std::string::size_type dash = arg.find ('-', arg_offset);
14239 if (dash != std::string::npos)
14240 {
14241 /* bp_loc is a range (x-z). */
14242 if (arg.length () == dash + 1)
14243 error (kind == extract_bp_kind::bp
14244 ? _("Bad breakpoint number at or near: '%s'")
14245 : _("Bad breakpoint location number at or near: '%s'"),
14246 bp_loc);
14247
14248 const char *end;
14249 const char *start_first = bp_loc;
14250 const char *start_second = &arg[dash + 1];
14251 range.first = extract_bp_num (kind, start_first, '-');
14252 range.second = extract_bp_num (kind, start_second, '\0', &end);
14253
14254 if (range.first > range.second)
14255 error (kind == extract_bp_kind::bp
14256 ? _("Inverted breakpoint range at '%.*s'")
14257 : _("Inverted breakpoint location range at '%.*s'"),
14258 int (end - start_first), start_first);
14259 }
14260 else
14261 {
14262 /* bp_loc is a single value. */
14263 range.first = extract_bp_num (kind, bp_loc, '\0');
14264 range.second = range.first;
14265 }
14266 return range;
14267}
14268
d0fe4701
XR
14269/* Extract the breakpoint/location range specified by ARG. Returns
14270 the breakpoint range in BP_NUM_RANGE, and the location range in
14271 BP_LOC_RANGE.
14272
14273 ARG may be in any of the following forms:
14274
14275 x where 'x' is a breakpoint number.
14276 x-y where 'x' and 'y' specify a breakpoint numbers range.
14277 x.y where 'x' is a breakpoint number and 'y' a location number.
14278 x.y-z where 'x' is a breakpoint number and 'y' and 'z' specify a
14279 location number range.
14280*/
14281
cc638e86 14282static void
d0fe4701
XR
14283extract_bp_number_and_location (const std::string &arg,
14284 std::pair<int, int> &bp_num_range,
14285 std::pair<int, int> &bp_loc_range)
14286{
14287 std::string::size_type dot = arg.find ('.');
14288
14289 if (dot != std::string::npos)
14290 {
14291 /* Handle 'x.y' and 'x.y-z' cases. */
14292
14293 if (arg.length () == dot + 1 || dot == 0)
95e95a6d 14294 error (_("Bad breakpoint number at or near: '%s'"), arg.c_str ());
d0fe4701 14295
95e95a6d
PA
14296 bp_num_range.first
14297 = extract_bp_num (extract_bp_kind::bp, arg.c_str (), '.');
14298 bp_num_range.second = bp_num_range.first;
d0fe4701 14299
95e95a6d
PA
14300 bp_loc_range = extract_bp_or_bp_range (extract_bp_kind::loc,
14301 arg, dot + 1);
d0fe4701
XR
14302 }
14303 else
14304 {
14305 /* Handle x and x-y cases. */
d0fe4701 14306
95e95a6d 14307 bp_num_range = extract_bp_or_bp_range (extract_bp_kind::bp, arg, 0);
d0fe4701
XR
14308 bp_loc_range.first = 0;
14309 bp_loc_range.second = 0;
14310 }
d0fe4701
XR
14311}
14312
14313/* Enable or disable a breakpoint location BP_NUM.LOC_NUM. ENABLE
14314 specifies whether to enable or disable. */
14315
14316static void
14317enable_disable_bp_num_loc (int bp_num, int loc_num, bool enable)
14318{
14319 struct bp_location *loc = find_location_by_number (bp_num, loc_num);
14320 if (loc != NULL)
14321 {
14322 if (loc->enabled != enable)
14323 {
14324 loc->enabled = enable;
14325 mark_breakpoint_location_modified (loc);
14326 }
14327 if (target_supports_enable_disable_tracepoint ()
14328 && current_trace_status ()->running && loc->owner
14329 && is_tracepoint (loc->owner))
14330 target_disable_tracepoint (loc);
14331 }
14332 update_global_location_list (UGLL_DONT_INSERT);
14333}
14334
14335/* Enable or disable a range of breakpoint locations. BP_NUM is the
14336 number of the breakpoint, and BP_LOC_RANGE specifies the
14337 (inclusive) range of location numbers of that breakpoint to
14338 enable/disable. ENABLE specifies whether to enable or disable the
14339 location. */
14340
14341static void
14342enable_disable_breakpoint_location_range (int bp_num,
14343 std::pair<int, int> &bp_loc_range,
14344 bool enable)
14345{
14346 for (int i = bp_loc_range.first; i <= bp_loc_range.second; i++)
14347 enable_disable_bp_num_loc (bp_num, i, enable);
14348}
0d381245 14349
1900040c
MS
14350/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14351 If from_tty is nonzero, it prints a message to that effect,
14352 which ends with a period (no newline). */
14353
c906108c 14354void
fba45db2 14355disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14356{
14357 /* Never disable a watchpoint scope breakpoint; we want to
14358 hit them when we leave scope so we can delete both the
14359 watchpoint and its scope breakpoint at that time. */
14360 if (bpt->type == bp_watchpoint_scope)
14361 return;
14362
b5de0fa7 14363 bpt->enable_state = bp_disabled;
c906108c 14364
b775012e
LM
14365 /* Mark breakpoint locations modified. */
14366 mark_breakpoint_modified (bpt);
14367
d248b706
KY
14368 if (target_supports_enable_disable_tracepoint ()
14369 && current_trace_status ()->running && is_tracepoint (bpt))
14370 {
14371 struct bp_location *location;
14372
14373 for (location = bpt->loc; location; location = location->next)
14374 target_disable_tracepoint (location);
14375 }
14376
44702360 14377 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14378
8d3788bd 14379 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14380}
14381
d0fe4701
XR
14382/* Enable or disable the breakpoint(s) or breakpoint location(s)
14383 specified in ARGS. ARGS may be in any of the formats handled by
14384 extract_bp_number_and_location. ENABLE specifies whether to enable
14385 or disable the breakpoints/locations. */
14386
c906108c 14387static void
d0fe4701 14388enable_disable_command (const char *args, int from_tty, bool enable)
c906108c 14389{
c906108c 14390 if (args == 0)
46c6471b
PA
14391 {
14392 struct breakpoint *bpt;
14393
14394 ALL_BREAKPOINTS (bpt)
14395 if (user_breakpoint_p (bpt))
d0fe4701
XR
14396 {
14397 if (enable)
14398 enable_breakpoint (bpt);
14399 else
14400 disable_breakpoint (bpt);
14401 }
46c6471b 14402 }
9eaabc75 14403 else
0d381245 14404 {
cb791d59 14405 std::string num = extract_arg (&args);
9eaabc75 14406
cb791d59 14407 while (!num.empty ())
d248b706 14408 {
d0fe4701 14409 std::pair<int, int> bp_num_range, bp_loc_range;
9eaabc75 14410
cc638e86
PA
14411 extract_bp_number_and_location (num, bp_num_range, bp_loc_range);
14412
14413 if (bp_loc_range.first == bp_loc_range.second
14414 && bp_loc_range.first == 0)
d0fe4701 14415 {
cc638e86
PA
14416 /* Handle breakpoint ids with formats 'x' or 'x-z'. */
14417 map_breakpoint_number_range (bp_num_range,
14418 enable
14419 ? enable_breakpoint
14420 : disable_breakpoint);
14421 }
14422 else
14423 {
14424 /* Handle breakpoint ids with formats 'x.y' or
14425 'x.y-z'. */
14426 enable_disable_breakpoint_location_range
14427 (bp_num_range.first, bp_loc_range, enable);
b775012e 14428 }
9eaabc75 14429 num = extract_arg (&args);
d248b706 14430 }
0d381245 14431 }
c906108c
SS
14432}
14433
d0fe4701
XR
14434/* The disable command disables the specified breakpoints/locations
14435 (or all defined breakpoints) so they're no longer effective in
14436 stopping the inferior. ARGS may be in any of the forms defined in
14437 extract_bp_number_and_location. */
14438
14439static void
14440disable_command (const char *args, int from_tty)
14441{
14442 enable_disable_command (args, from_tty, false);
14443}
14444
c906108c 14445static void
816338b5
SS
14446enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14447 int count)
c906108c 14448{
afe38095 14449 int target_resources_ok;
c906108c
SS
14450
14451 if (bpt->type == bp_hardware_breakpoint)
14452 {
14453 int i;
c5aa993b 14454 i = hw_breakpoint_used_count ();
53a5351d 14455 target_resources_ok =
d92524f1 14456 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14457 i + 1, 0);
c906108c 14458 if (target_resources_ok == 0)
8a3fe4f8 14459 error (_("No hardware breakpoint support in the target."));
c906108c 14460 else if (target_resources_ok < 0)
8a3fe4f8 14461 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14462 }
14463
cc60f2e3 14464 if (is_watchpoint (bpt))
c906108c 14465 {
d07205c2 14466 /* Initialize it just to avoid a GCC false warning. */
f486487f 14467 enum enable_state orig_enable_state = bp_disabled;
dde02812 14468
492d29ea 14469 TRY
c906108c 14470 {
3a5c3e22
PA
14471 struct watchpoint *w = (struct watchpoint *) bpt;
14472
1e718ff1
TJB
14473 orig_enable_state = bpt->enable_state;
14474 bpt->enable_state = bp_enabled;
3a5c3e22 14475 update_watchpoint (w, 1 /* reparse */);
c906108c 14476 }
492d29ea 14477 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14478 {
1e718ff1 14479 bpt->enable_state = orig_enable_state;
dde02812
ES
14480 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14481 bpt->number);
14482 return;
c5aa993b 14483 }
492d29ea 14484 END_CATCH
c906108c 14485 }
0101ce28 14486
b775012e
LM
14487 bpt->enable_state = bp_enabled;
14488
14489 /* Mark breakpoint locations modified. */
14490 mark_breakpoint_modified (bpt);
14491
d248b706
KY
14492 if (target_supports_enable_disable_tracepoint ()
14493 && current_trace_status ()->running && is_tracepoint (bpt))
14494 {
14495 struct bp_location *location;
14496
14497 for (location = bpt->loc; location; location = location->next)
14498 target_enable_tracepoint (location);
14499 }
14500
b4c291bb 14501 bpt->disposition = disposition;
816338b5 14502 bpt->enable_count = count;
44702360 14503 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14504
8d3788bd 14505 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14506}
14507
fe3f5fa8 14508
c906108c 14509void
fba45db2 14510enable_breakpoint (struct breakpoint *bpt)
c906108c 14511{
816338b5 14512 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14513}
14514
d0fe4701
XR
14515/* The enable command enables the specified breakpoints/locations (or
14516 all defined breakpoints) so they once again become (or continue to
14517 be) effective in stopping the inferior. ARGS may be in any of the
14518 forms defined in extract_bp_number_and_location. */
c906108c 14519
c906108c 14520static void
981a3fb3 14521enable_command (const char *args, int from_tty)
c906108c 14522{
d0fe4701 14523 enable_disable_command (args, from_tty, true);
c906108c
SS
14524}
14525
c906108c 14526static void
4495129a 14527enable_once_command (const char *args, int from_tty)
c906108c 14528{
48649e1b
TT
14529 map_breakpoint_numbers
14530 (args, [&] (breakpoint *b)
14531 {
14532 iterate_over_related_breakpoints
14533 (b, [&] (breakpoint *bpt)
14534 {
14535 enable_breakpoint_disp (bpt, disp_disable, 1);
14536 });
14537 });
816338b5
SS
14538}
14539
14540static void
4495129a 14541enable_count_command (const char *args, int from_tty)
816338b5 14542{
b9d61307
SM
14543 int count;
14544
14545 if (args == NULL)
14546 error_no_arg (_("hit count"));
14547
14548 count = get_number (&args);
816338b5 14549
48649e1b
TT
14550 map_breakpoint_numbers
14551 (args, [&] (breakpoint *b)
14552 {
14553 iterate_over_related_breakpoints
14554 (b, [&] (breakpoint *bpt)
14555 {
14556 enable_breakpoint_disp (bpt, disp_disable, count);
14557 });
14558 });
c906108c
SS
14559}
14560
c906108c 14561static void
4495129a 14562enable_delete_command (const char *args, int from_tty)
c906108c 14563{
48649e1b
TT
14564 map_breakpoint_numbers
14565 (args, [&] (breakpoint *b)
14566 {
14567 iterate_over_related_breakpoints
14568 (b, [&] (breakpoint *bpt)
14569 {
14570 enable_breakpoint_disp (bpt, disp_del, 1);
14571 });
14572 });
c906108c
SS
14573}
14574\f
fa8d40ab 14575static void
981a3fb3 14576set_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14577{
14578}
14579
14580static void
981a3fb3 14581show_breakpoint_cmd (const char *args, int from_tty)
fa8d40ab
JJ
14582{
14583}
14584
1f3b5d1b
PP
14585/* Invalidate last known value of any hardware watchpoint if
14586 the memory which that value represents has been written to by
14587 GDB itself. */
14588
14589static void
8de0566d
YQ
14590invalidate_bp_value_on_memory_change (struct inferior *inferior,
14591 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14592 const bfd_byte *data)
14593{
14594 struct breakpoint *bp;
14595
14596 ALL_BREAKPOINTS (bp)
14597 if (bp->enable_state == bp_enabled
3a5c3e22 14598 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14599 {
3a5c3e22 14600 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14601
3a5c3e22
PA
14602 if (wp->val_valid && wp->val)
14603 {
14604 struct bp_location *loc;
14605
14606 for (loc = bp->loc; loc != NULL; loc = loc->next)
14607 if (loc->loc_type == bp_loc_hardware_watchpoint
14608 && loc->address + loc->length > addr
14609 && addr + len > loc->address)
14610 {
14611 value_free (wp->val);
14612 wp->val = NULL;
14613 wp->val_valid = 0;
14614 }
14615 }
1f3b5d1b
PP
14616 }
14617}
14618
8181d85f
DJ
14619/* Create and insert a breakpoint for software single step. */
14620
14621void
6c95b8df 14622insert_single_step_breakpoint (struct gdbarch *gdbarch,
accd0bcd 14623 const address_space *aspace,
4a64f543 14624 CORE_ADDR next_pc)
8181d85f 14625{
7c16b83e
PA
14626 struct thread_info *tp = inferior_thread ();
14627 struct symtab_and_line sal;
14628 CORE_ADDR pc = next_pc;
8181d85f 14629
34b7e8a6
PA
14630 if (tp->control.single_step_breakpoints == NULL)
14631 {
14632 tp->control.single_step_breakpoints
5d5658a1 14633 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 14634 }
8181d85f 14635
7c16b83e
PA
14636 sal = find_pc_line (pc, 0);
14637 sal.pc = pc;
14638 sal.section = find_pc_overlay (pc);
14639 sal.explicit_pc = 1;
34b7e8a6 14640 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14641
7c16b83e 14642 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14643}
14644
93f9a11f
YQ
14645/* Insert single step breakpoints according to the current state. */
14646
14647int
14648insert_single_step_breakpoints (struct gdbarch *gdbarch)
14649{
f5ea389a 14650 struct regcache *regcache = get_current_regcache ();
a0ff9e1a 14651 std::vector<CORE_ADDR> next_pcs;
93f9a11f 14652
f5ea389a 14653 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
93f9a11f 14654
a0ff9e1a 14655 if (!next_pcs.empty ())
93f9a11f 14656 {
f5ea389a 14657 struct frame_info *frame = get_current_frame ();
8b86c959 14658 const address_space *aspace = get_frame_address_space (frame);
93f9a11f 14659
a0ff9e1a 14660 for (CORE_ADDR pc : next_pcs)
93f9a11f
YQ
14661 insert_single_step_breakpoint (gdbarch, aspace, pc);
14662
93f9a11f
YQ
14663 return 1;
14664 }
14665 else
14666 return 0;
14667}
14668
34b7e8a6 14669/* See breakpoint.h. */
f02253f1
HZ
14670
14671int
7c16b83e 14672breakpoint_has_location_inserted_here (struct breakpoint *bp,
accd0bcd 14673 const address_space *aspace,
7c16b83e 14674 CORE_ADDR pc)
1aafd4da 14675{
7c16b83e 14676 struct bp_location *loc;
1aafd4da 14677
7c16b83e
PA
14678 for (loc = bp->loc; loc != NULL; loc = loc->next)
14679 if (loc->inserted
14680 && breakpoint_location_address_match (loc, aspace, pc))
14681 return 1;
1aafd4da 14682
7c16b83e 14683 return 0;
ef370185
JB
14684}
14685
14686/* Check whether a software single-step breakpoint is inserted at
14687 PC. */
14688
14689int
accd0bcd 14690single_step_breakpoint_inserted_here_p (const address_space *aspace,
ef370185
JB
14691 CORE_ADDR pc)
14692{
34b7e8a6
PA
14693 struct breakpoint *bpt;
14694
14695 ALL_BREAKPOINTS (bpt)
14696 {
14697 if (bpt->type == bp_single_step
14698 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14699 return 1;
14700 }
14701 return 0;
1aafd4da
UW
14702}
14703
1042e4c0
SS
14704/* Tracepoint-specific operations. */
14705
14706/* Set tracepoint count to NUM. */
14707static void
14708set_tracepoint_count (int num)
14709{
14710 tracepoint_count = num;
4fa62494 14711 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14712}
14713
70221824 14714static void
0b39b52e 14715trace_command (const char *arg, int from_tty)
1042e4c0 14716{
55aa24fb 14717 struct breakpoint_ops *ops;
55aa24fb 14718
ffc2605c
TT
14719 event_location_up location = string_to_event_location (&arg,
14720 current_language);
5b56227b 14721 if (location != NULL
ffc2605c 14722 && event_location_type (location.get ()) == PROBE_LOCATION)
55aa24fb
SDJ
14723 ops = &tracepoint_probe_breakpoint_ops;
14724 else
14725 ops = &tracepoint_breakpoint_ops;
14726
558a9d82 14727 create_breakpoint (get_current_arch (),
ffc2605c 14728 location.get (),
f00aae0f 14729 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14730 0 /* tempflag */,
14731 bp_tracepoint /* type_wanted */,
14732 0 /* Ignore count */,
14733 pending_break_support,
14734 ops,
14735 from_tty,
14736 1 /* enabled */,
14737 0 /* internal */, 0);
1042e4c0
SS
14738}
14739
70221824 14740static void
0b39b52e 14741ftrace_command (const char *arg, int from_tty)
7a697b8d 14742{
ffc2605c
TT
14743 event_location_up location = string_to_event_location (&arg,
14744 current_language);
558a9d82 14745 create_breakpoint (get_current_arch (),
ffc2605c 14746 location.get (),
f00aae0f 14747 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14748 0 /* tempflag */,
14749 bp_fast_tracepoint /* type_wanted */,
14750 0 /* Ignore count */,
14751 pending_break_support,
14752 &tracepoint_breakpoint_ops,
14753 from_tty,
14754 1 /* enabled */,
14755 0 /* internal */, 0);
0fb4aa4b
PA
14756}
14757
14758/* strace command implementation. Creates a static tracepoint. */
14759
70221824 14760static void
0b39b52e 14761strace_command (const char *arg, int from_tty)
0fb4aa4b 14762{
983af33b 14763 struct breakpoint_ops *ops;
ffc2605c 14764 event_location_up location;
983af33b
SDJ
14765
14766 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14767 or with a normal static tracepoint. */
61012eef 14768 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
14769 {
14770 ops = &strace_marker_breakpoint_ops;
14771 location = new_linespec_location (&arg);
14772 }
983af33b 14773 else
f00aae0f
KS
14774 {
14775 ops = &tracepoint_breakpoint_ops;
14776 location = string_to_event_location (&arg, current_language);
14777 }
983af33b 14778
558a9d82 14779 create_breakpoint (get_current_arch (),
ffc2605c 14780 location.get (),
f00aae0f 14781 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14782 0 /* tempflag */,
14783 bp_static_tracepoint /* type_wanted */,
14784 0 /* Ignore count */,
14785 pending_break_support,
14786 ops,
14787 from_tty,
14788 1 /* enabled */,
14789 0 /* internal */, 0);
7a697b8d
SS
14790}
14791
409873ef
SS
14792/* Set up a fake reader function that gets command lines from a linked
14793 list that was acquired during tracepoint uploading. */
14794
14795static struct uploaded_tp *this_utp;
3149d8c1 14796static int next_cmd;
409873ef
SS
14797
14798static char *
14799read_uploaded_action (void)
14800{
14801 char *rslt;
14802
3149d8c1 14803 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 14804
3149d8c1 14805 next_cmd++;
409873ef
SS
14806
14807 return rslt;
14808}
14809
00bf0b85
SS
14810/* Given information about a tracepoint as recorded on a target (which
14811 can be either a live system or a trace file), attempt to create an
14812 equivalent GDB tracepoint. This is not a reliable process, since
14813 the target does not necessarily have all the information used when
14814 the tracepoint was originally defined. */
14815
d9b3f62e 14816struct tracepoint *
00bf0b85 14817create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14818{
f2fc3015
TT
14819 const char *addr_str;
14820 char small_buf[100];
d9b3f62e 14821 struct tracepoint *tp;
fd9b8c24 14822
409873ef
SS
14823 if (utp->at_string)
14824 addr_str = utp->at_string;
14825 else
14826 {
14827 /* In the absence of a source location, fall back to raw
14828 address. Since there is no way to confirm that the address
14829 means the same thing as when the trace was started, warn the
14830 user. */
3e43a32a
MS
14831 warning (_("Uploaded tracepoint %d has no "
14832 "source location, using raw address"),
409873ef 14833 utp->number);
8c042590 14834 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14835 addr_str = small_buf;
14836 }
14837
14838 /* There's not much we can do with a sequence of bytecodes. */
14839 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14840 warning (_("Uploaded tracepoint %d condition "
14841 "has no source form, ignoring it"),
409873ef 14842 utp->number);
d5551862 14843
ffc2605c
TT
14844 event_location_up location = string_to_event_location (&addr_str,
14845 current_language);
8cdf0e15 14846 if (!create_breakpoint (get_current_arch (),
ffc2605c 14847 location.get (),
f00aae0f 14848 utp->cond_string, -1, addr_str,
e7e0cddf 14849 0 /* parse cond/thread */,
8cdf0e15 14850 0 /* tempflag */,
0fb4aa4b 14851 utp->type /* type_wanted */,
8cdf0e15
VP
14852 0 /* Ignore count */,
14853 pending_break_support,
348d480f 14854 &tracepoint_breakpoint_ops,
8cdf0e15 14855 0 /* from_tty */,
84f4c1fe 14856 utp->enabled /* enabled */,
44f238bb
PA
14857 0 /* internal */,
14858 CREATE_BREAKPOINT_FLAGS_INSERTED))
ffc2605c 14859 return NULL;
fd9b8c24 14860
409873ef 14861 /* Get the tracepoint we just created. */
fd9b8c24
PA
14862 tp = get_tracepoint (tracepoint_count);
14863 gdb_assert (tp != NULL);
d5551862 14864
00bf0b85
SS
14865 if (utp->pass > 0)
14866 {
8c042590 14867 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
c1fc2657 14868 tp->number);
00bf0b85 14869
409873ef 14870 trace_pass_command (small_buf, 0);
00bf0b85
SS
14871 }
14872
409873ef
SS
14873 /* If we have uploaded versions of the original commands, set up a
14874 special-purpose "reader" function and call the usual command line
14875 reader, then pass the result to the breakpoint command-setting
14876 function. */
3149d8c1 14877 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 14878 {
93921405 14879 command_line_up cmd_list;
00bf0b85 14880
409873ef 14881 this_utp = utp;
3149d8c1 14882 next_cmd = 0;
d5551862 14883
409873ef
SS
14884 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14885
c1fc2657 14886 breakpoint_set_commands (tp, std::move (cmd_list));
00bf0b85 14887 }
3149d8c1
SS
14888 else if (!VEC_empty (char_ptr, utp->actions)
14889 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
14890 warning (_("Uploaded tracepoint %d actions "
14891 "have no source form, ignoring them"),
409873ef 14892 utp->number);
00bf0b85 14893
f196051f 14894 /* Copy any status information that might be available. */
c1fc2657 14895 tp->hit_count = utp->hit_count;
f196051f
SS
14896 tp->traceframe_usage = utp->traceframe_usage;
14897
00bf0b85 14898 return tp;
d9b3f62e 14899}
00bf0b85 14900
1042e4c0
SS
14901/* Print information on tracepoint number TPNUM_EXP, or all if
14902 omitted. */
14903
14904static void
1d12d88f 14905info_tracepoints_command (const char *args, int from_tty)
1042e4c0 14906{
79a45e25 14907 struct ui_out *uiout = current_uiout;
e5a67952 14908 int num_printed;
1042e4c0 14909
e5a67952 14910 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
14911
14912 if (num_printed == 0)
1042e4c0 14913 {
e5a67952 14914 if (args == NULL || *args == '\0')
112e8700 14915 uiout->message ("No tracepoints.\n");
d77f58be 14916 else
112e8700 14917 uiout->message ("No tracepoint matching '%s'.\n", args);
1042e4c0 14918 }
ad443146
SS
14919
14920 default_collect_info ();
1042e4c0
SS
14921}
14922
4a64f543 14923/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14924 Not supported by all targets. */
14925static void
5fed81ff 14926enable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14927{
14928 enable_command (args, from_tty);
14929}
14930
4a64f543 14931/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14932 Not supported by all targets. */
14933static void
5fed81ff 14934disable_trace_command (const char *args, int from_tty)
1042e4c0
SS
14935{
14936 disable_command (args, from_tty);
14937}
14938
4a64f543 14939/* Remove a tracepoint (or all if no argument). */
1042e4c0 14940static void
4495129a 14941delete_trace_command (const char *arg, int from_tty)
1042e4c0 14942{
35df4500 14943 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14944
14945 dont_repeat ();
14946
14947 if (arg == 0)
14948 {
14949 int breaks_to_delete = 0;
14950
14951 /* Delete all breakpoints if no argument.
14952 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14953 have to be deleted with an explicit breakpoint number
14954 argument. */
1042e4c0 14955 ALL_TRACEPOINTS (b)
46c6471b 14956 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14957 {
14958 breaks_to_delete = 1;
14959 break;
14960 }
1042e4c0
SS
14961
14962 /* Ask user only if there are some breakpoints to delete. */
14963 if (!from_tty
14964 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14965 {
35df4500 14966 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14967 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14968 delete_breakpoint (b);
1042e4c0
SS
14969 }
14970 }
14971 else
48649e1b
TT
14972 map_breakpoint_numbers
14973 (arg, [&] (breakpoint *b)
14974 {
14975 iterate_over_related_breakpoints (b, delete_breakpoint);
14976 });
1042e4c0
SS
14977}
14978
197f0a60
TT
14979/* Helper function for trace_pass_command. */
14980
14981static void
d9b3f62e 14982trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14983{
d9b3f62e 14984 tp->pass_count = count;
c1fc2657 14985 observer_notify_breakpoint_modified (tp);
197f0a60
TT
14986 if (from_tty)
14987 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
c1fc2657 14988 tp->number, count);
197f0a60
TT
14989}
14990
1042e4c0
SS
14991/* Set passcount for tracepoint.
14992
14993 First command argument is passcount, second is tracepoint number.
14994 If tracepoint number omitted, apply to most recently defined.
14995 Also accepts special argument "all". */
14996
14997static void
0b39b52e 14998trace_pass_command (const char *args, int from_tty)
1042e4c0 14999{
d9b3f62e 15000 struct tracepoint *t1;
0b39b52e 15001 ULONGEST count;
1042e4c0
SS
15002
15003 if (args == 0 || *args == 0)
3e43a32a
MS
15004 error (_("passcount command requires an "
15005 "argument (count + optional TP num)"));
1042e4c0 15006
0b39b52e 15007 count = strtoulst (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15008
529480d0 15009 args = skip_spaces (args);
1042e4c0
SS
15010 if (*args && strncasecmp (args, "all", 3) == 0)
15011 {
d9b3f62e
PA
15012 struct breakpoint *b;
15013
1042e4c0 15014 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15015 if (*args)
15016 error (_("Junk at end of arguments."));
1042e4c0 15017
d9b3f62e 15018 ALL_TRACEPOINTS (b)
197f0a60 15019 {
d9b3f62e 15020 t1 = (struct tracepoint *) b;
197f0a60
TT
15021 trace_pass_set_count (t1, count, from_tty);
15022 }
15023 }
15024 else if (*args == '\0')
1042e4c0 15025 {
5fa1d40e 15026 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15027 if (t1)
197f0a60
TT
15028 trace_pass_set_count (t1, count, from_tty);
15029 }
15030 else
15031 {
bfd28288
PA
15032 number_or_range_parser parser (args);
15033 while (!parser.finished ())
1042e4c0 15034 {
bfd28288 15035 t1 = get_tracepoint_by_number (&args, &parser);
197f0a60
TT
15036 if (t1)
15037 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15038 }
15039 }
1042e4c0
SS
15040}
15041
d9b3f62e 15042struct tracepoint *
1042e4c0
SS
15043get_tracepoint (int num)
15044{
15045 struct breakpoint *t;
15046
15047 ALL_TRACEPOINTS (t)
15048 if (t->number == num)
d9b3f62e 15049 return (struct tracepoint *) t;
1042e4c0
SS
15050
15051 return NULL;
15052}
15053
d5551862
SS
15054/* Find the tracepoint with the given target-side number (which may be
15055 different from the tracepoint number after disconnecting and
15056 reconnecting). */
15057
d9b3f62e 15058struct tracepoint *
d5551862
SS
15059get_tracepoint_by_number_on_target (int num)
15060{
d9b3f62e 15061 struct breakpoint *b;
d5551862 15062
d9b3f62e
PA
15063 ALL_TRACEPOINTS (b)
15064 {
15065 struct tracepoint *t = (struct tracepoint *) b;
15066
15067 if (t->number_on_target == num)
15068 return t;
15069 }
d5551862
SS
15070
15071 return NULL;
15072}
15073
1042e4c0 15074/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15075 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15076 If the argument is missing, the most recent tracepoint
15077 (tracepoint_count) is returned. */
15078
d9b3f62e 15079struct tracepoint *
0b39b52e 15080get_tracepoint_by_number (const char **arg,
bfd28288 15081 number_or_range_parser *parser)
1042e4c0 15082{
1042e4c0
SS
15083 struct breakpoint *t;
15084 int tpnum;
0b39b52e 15085 const char *instring = arg == NULL ? NULL : *arg;
1042e4c0 15086
bfd28288 15087 if (parser != NULL)
197f0a60 15088 {
bfd28288
PA
15089 gdb_assert (!parser->finished ());
15090 tpnum = parser->get_number ();
197f0a60
TT
15091 }
15092 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15093 tpnum = tracepoint_count;
1042e4c0 15094 else
197f0a60 15095 tpnum = get_number (arg);
1042e4c0
SS
15096
15097 if (tpnum <= 0)
15098 {
15099 if (instring && *instring)
15100 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15101 instring);
15102 else
5fa1d40e 15103 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15104 return NULL;
15105 }
15106
15107 ALL_TRACEPOINTS (t)
15108 if (t->number == tpnum)
15109 {
d9b3f62e 15110 return (struct tracepoint *) t;
1042e4c0
SS
15111 }
15112
1042e4c0
SS
15113 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15114 return NULL;
15115}
15116
d9b3f62e
PA
15117void
15118print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15119{
15120 if (b->thread != -1)
15121 fprintf_unfiltered (fp, " thread %d", b->thread);
15122
15123 if (b->task != 0)
15124 fprintf_unfiltered (fp, " task %d", b->task);
15125
15126 fprintf_unfiltered (fp, "\n");
15127}
15128
6149aea9
PA
15129/* Save information on user settable breakpoints (watchpoints, etc) to
15130 a new script file named FILENAME. If FILTER is non-NULL, call it
15131 on each breakpoint and only include the ones for which it returns
15132 non-zero. */
15133
1042e4c0 15134static void
4495129a 15135save_breakpoints (const char *filename, int from_tty,
6149aea9 15136 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15137{
15138 struct breakpoint *tp;
6149aea9 15139 int any = 0;
6149aea9 15140 int extra_trace_bits = 0;
1042e4c0 15141
6149aea9
PA
15142 if (filename == 0 || *filename == 0)
15143 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15144
15145 /* See if we have anything to save. */
6149aea9 15146 ALL_BREAKPOINTS (tp)
1042e4c0 15147 {
6149aea9 15148 /* Skip internal and momentary breakpoints. */
09d682a4 15149 if (!user_breakpoint_p (tp))
6149aea9
PA
15150 continue;
15151
15152 /* If we have a filter, only save the breakpoints it accepts. */
15153 if (filter && !filter (tp))
15154 continue;
15155
15156 any = 1;
15157
15158 if (is_tracepoint (tp))
15159 {
15160 extra_trace_bits = 1;
15161
15162 /* We can stop searching. */
15163 break;
15164 }
1042e4c0 15165 }
6149aea9
PA
15166
15167 if (!any)
1042e4c0 15168 {
6149aea9 15169 warning (_("Nothing to save."));
1042e4c0
SS
15170 return;
15171 }
15172
ee0c3293 15173 gdb::unique_xmalloc_ptr<char> expanded_filename (tilde_expand (filename));
d7e74731
PA
15174
15175 stdio_file fp;
15176
ee0c3293 15177 if (!fp.open (expanded_filename.get (), "w"))
6149aea9 15178 error (_("Unable to open file '%s' for saving (%s)"),
ee0c3293 15179 expanded_filename.get (), safe_strerror (errno));
8bf6485c 15180
6149aea9 15181 if (extra_trace_bits)
d7e74731 15182 save_trace_state_variables (&fp);
8bf6485c 15183
6149aea9 15184 ALL_BREAKPOINTS (tp)
1042e4c0 15185 {
6149aea9 15186 /* Skip internal and momentary breakpoints. */
09d682a4 15187 if (!user_breakpoint_p (tp))
6149aea9 15188 continue;
8bf6485c 15189
6149aea9
PA
15190 /* If we have a filter, only save the breakpoints it accepts. */
15191 if (filter && !filter (tp))
15192 continue;
15193
d7e74731 15194 tp->ops->print_recreate (tp, &fp);
1042e4c0 15195
6149aea9
PA
15196 /* Note, we can't rely on tp->number for anything, as we can't
15197 assume the recreated breakpoint numbers will match. Use $bpnum
15198 instead. */
15199
15200 if (tp->cond_string)
d7e74731 15201 fp.printf (" condition $bpnum %s\n", tp->cond_string);
6149aea9
PA
15202
15203 if (tp->ignore_count)
d7e74731 15204 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
6149aea9 15205
2d9442cc 15206 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15207 {
d7e74731 15208 fp.puts (" commands\n");
a7bdde9e 15209
d7e74731 15210 current_uiout->redirect (&fp);
492d29ea 15211 TRY
1042e4c0 15212 {
d1b0a7bf 15213 print_command_lines (current_uiout, tp->commands.get (), 2);
a7bdde9e 15214 }
492d29ea
PA
15215 CATCH (ex, RETURN_MASK_ALL)
15216 {
112e8700 15217 current_uiout->redirect (NULL);
492d29ea
PA
15218 throw_exception (ex);
15219 }
15220 END_CATCH
1042e4c0 15221
112e8700 15222 current_uiout->redirect (NULL);
d7e74731 15223 fp.puts (" end\n");
1042e4c0 15224 }
6149aea9
PA
15225
15226 if (tp->enable_state == bp_disabled)
d7e74731 15227 fp.puts ("disable $bpnum\n");
6149aea9
PA
15228
15229 /* If this is a multi-location breakpoint, check if the locations
15230 should be individually disabled. Watchpoint locations are
15231 special, and not user visible. */
15232 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15233 {
15234 struct bp_location *loc;
15235 int n = 1;
15236
15237 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15238 if (!loc->enabled)
d7e74731 15239 fp.printf ("disable $bpnum.%d\n", n);
6149aea9 15240 }
1042e4c0 15241 }
8bf6485c 15242
6149aea9 15243 if (extra_trace_bits && *default_collect)
d7e74731 15244 fp.printf ("set default-collect %s\n", default_collect);
8bf6485c 15245
1042e4c0 15246 if (from_tty)
ee0c3293 15247 printf_filtered (_("Saved to file '%s'.\n"), expanded_filename.get ());
6149aea9
PA
15248}
15249
15250/* The `save breakpoints' command. */
15251
15252static void
4495129a 15253save_breakpoints_command (const char *args, int from_tty)
6149aea9
PA
15254{
15255 save_breakpoints (args, from_tty, NULL);
15256}
15257
15258/* The `save tracepoints' command. */
15259
15260static void
4495129a 15261save_tracepoints_command (const char *args, int from_tty)
6149aea9
PA
15262{
15263 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15264}
15265
15266/* Create a vector of all tracepoints. */
15267
15268VEC(breakpoint_p) *
eeae04df 15269all_tracepoints (void)
1042e4c0
SS
15270{
15271 VEC(breakpoint_p) *tp_vec = 0;
15272 struct breakpoint *tp;
15273
15274 ALL_TRACEPOINTS (tp)
15275 {
15276 VEC_safe_push (breakpoint_p, tp_vec, tp);
15277 }
15278
15279 return tp_vec;
15280}
15281
c906108c 15282\f
629500fa
KS
15283/* This help string is used to consolidate all the help string for specifying
15284 locations used by several commands. */
15285
15286#define LOCATION_HELP_STRING \
15287"Linespecs are colon-separated lists of location parameters, such as\n\
15288source filename, function name, label name, and line number.\n\
15289Example: To specify the start of a label named \"the_top\" in the\n\
15290function \"fact\" in the file \"factorial.c\", use\n\
15291\"factorial.c:fact:the_top\".\n\
15292\n\
15293Address locations begin with \"*\" and specify an exact address in the\n\
15294program. Example: To specify the fourth byte past the start function\n\
15295\"main\", use \"*main + 4\".\n\
15296\n\
15297Explicit locations are similar to linespecs but use an option/argument\n\
15298syntax to specify location parameters.\n\
15299Example: To specify the start of the label named \"the_top\" in the\n\
15300function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15301-function fact -label the_top\".\n"
15302
4a64f543
MS
15303/* This help string is used for the break, hbreak, tbreak and thbreak
15304 commands. It is defined as a macro to prevent duplication.
15305 COMMAND should be a string constant containing the name of the
15306 command. */
629500fa 15307
31e2b00f 15308#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15309command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15310PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15311probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15312guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15313`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15314LOCATION may be a linespec, address, or explicit location as described\n\
15315below.\n\
15316\n\
dc10affe
PA
15317With no LOCATION, uses current execution address of the selected\n\
15318stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15319\n\
15320THREADNUM is the number from \"info threads\".\n\
15321CONDITION is a boolean expression.\n\
629500fa 15322\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15323Multiple breakpoints at one place are permitted, and useful if their\n\
15324conditions are different.\n\
31e2b00f
AS
15325\n\
15326Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15327
44feb3ce
TT
15328/* List of subcommands for "catch". */
15329static struct cmd_list_element *catch_cmdlist;
15330
15331/* List of subcommands for "tcatch". */
15332static struct cmd_list_element *tcatch_cmdlist;
15333
9ac4176b 15334void
a121b7c1 15335add_catch_command (const char *name, const char *docstring,
eb4c3f4a 15336 cmd_const_sfunc_ftype *sfunc,
625e8578 15337 completer_ftype *completer,
44feb3ce
TT
15338 void *user_data_catch,
15339 void *user_data_tcatch)
15340{
15341 struct cmd_list_element *command;
15342
0450cc4c 15343 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15344 &catch_cmdlist);
15345 set_cmd_sfunc (command, sfunc);
15346 set_cmd_context (command, user_data_catch);
a96d9b2e 15347 set_cmd_completer (command, completer);
44feb3ce 15348
0450cc4c 15349 command = add_cmd (name, class_breakpoint, docstring,
44feb3ce
TT
15350 &tcatch_cmdlist);
15351 set_cmd_sfunc (command, sfunc);
15352 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15353 set_cmd_completer (command, completer);
44feb3ce
TT
15354}
15355
6149aea9 15356static void
981a3fb3 15357save_command (const char *arg, int from_tty)
6149aea9 15358{
3e43a32a
MS
15359 printf_unfiltered (_("\"save\" must be followed by "
15360 "the name of a save subcommand.\n"));
635c7e8a 15361 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15362}
15363
84f4c1fe
PM
15364struct breakpoint *
15365iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15366 void *data)
15367{
35df4500 15368 struct breakpoint *b, *b_tmp;
84f4c1fe 15369
35df4500 15370 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15371 {
15372 if ((*callback) (b, data))
15373 return b;
15374 }
15375
15376 return NULL;
15377}
15378
0574c78f
GB
15379/* Zero if any of the breakpoint's locations could be a location where
15380 functions have been inlined, nonzero otherwise. */
15381
15382static int
15383is_non_inline_function (struct breakpoint *b)
15384{
15385 /* The shared library event breakpoint is set on the address of a
15386 non-inline function. */
15387 if (b->type == bp_shlib_event)
15388 return 1;
15389
15390 return 0;
15391}
15392
15393/* Nonzero if the specified PC cannot be a location where functions
15394 have been inlined. */
15395
15396int
accd0bcd 15397pc_at_non_inline_function (const address_space *aspace, CORE_ADDR pc,
09ac7c10 15398 const struct target_waitstatus *ws)
0574c78f
GB
15399{
15400 struct breakpoint *b;
15401 struct bp_location *bl;
15402
15403 ALL_BREAKPOINTS (b)
15404 {
15405 if (!is_non_inline_function (b))
15406 continue;
15407
15408 for (bl = b->loc; bl != NULL; bl = bl->next)
15409 {
15410 if (!bl->shlib_disabled
09ac7c10 15411 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15412 return 1;
15413 }
15414 }
15415
15416 return 0;
15417}
15418
2f202fde
JK
15419/* Remove any references to OBJFILE which is going to be freed. */
15420
15421void
15422breakpoint_free_objfile (struct objfile *objfile)
15423{
15424 struct bp_location **locp, *loc;
15425
15426 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15427 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15428 loc->symtab = NULL;
15429}
15430
2060206e
PA
15431void
15432initialize_breakpoint_ops (void)
15433{
15434 static int initialized = 0;
15435
15436 struct breakpoint_ops *ops;
15437
15438 if (initialized)
15439 return;
15440 initialized = 1;
15441
15442 /* The breakpoint_ops structure to be inherit by all kinds of
15443 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15444 internal and momentary breakpoints, etc.). */
15445 ops = &bkpt_base_breakpoint_ops;
15446 *ops = base_breakpoint_ops;
15447 ops->re_set = bkpt_re_set;
15448 ops->insert_location = bkpt_insert_location;
15449 ops->remove_location = bkpt_remove_location;
15450 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15451 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15452 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15453 ops->decode_location = bkpt_decode_location;
2060206e
PA
15454
15455 /* The breakpoint_ops structure to be used in regular breakpoints. */
15456 ops = &bkpt_breakpoint_ops;
15457 *ops = bkpt_base_breakpoint_ops;
15458 ops->re_set = bkpt_re_set;
15459 ops->resources_needed = bkpt_resources_needed;
15460 ops->print_it = bkpt_print_it;
15461 ops->print_mention = bkpt_print_mention;
15462 ops->print_recreate = bkpt_print_recreate;
15463
15464 /* Ranged breakpoints. */
15465 ops = &ranged_breakpoint_ops;
15466 *ops = bkpt_breakpoint_ops;
15467 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15468 ops->resources_needed = resources_needed_ranged_breakpoint;
15469 ops->print_it = print_it_ranged_breakpoint;
15470 ops->print_one = print_one_ranged_breakpoint;
15471 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15472 ops->print_mention = print_mention_ranged_breakpoint;
15473 ops->print_recreate = print_recreate_ranged_breakpoint;
15474
15475 /* Internal breakpoints. */
15476 ops = &internal_breakpoint_ops;
15477 *ops = bkpt_base_breakpoint_ops;
15478 ops->re_set = internal_bkpt_re_set;
15479 ops->check_status = internal_bkpt_check_status;
15480 ops->print_it = internal_bkpt_print_it;
15481 ops->print_mention = internal_bkpt_print_mention;
15482
15483 /* Momentary breakpoints. */
15484 ops = &momentary_breakpoint_ops;
15485 *ops = bkpt_base_breakpoint_ops;
15486 ops->re_set = momentary_bkpt_re_set;
15487 ops->check_status = momentary_bkpt_check_status;
15488 ops->print_it = momentary_bkpt_print_it;
15489 ops->print_mention = momentary_bkpt_print_mention;
15490
55aa24fb
SDJ
15491 /* Probe breakpoints. */
15492 ops = &bkpt_probe_breakpoint_ops;
15493 *ops = bkpt_breakpoint_ops;
15494 ops->insert_location = bkpt_probe_insert_location;
15495 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15496 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15497 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15498
2060206e
PA
15499 /* Watchpoints. */
15500 ops = &watchpoint_breakpoint_ops;
15501 *ops = base_breakpoint_ops;
15502 ops->re_set = re_set_watchpoint;
15503 ops->insert_location = insert_watchpoint;
15504 ops->remove_location = remove_watchpoint;
15505 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15506 ops->check_status = check_status_watchpoint;
15507 ops->resources_needed = resources_needed_watchpoint;
15508 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15509 ops->print_it = print_it_watchpoint;
15510 ops->print_mention = print_mention_watchpoint;
15511 ops->print_recreate = print_recreate_watchpoint;
427cd150 15512 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15513
15514 /* Masked watchpoints. */
15515 ops = &masked_watchpoint_breakpoint_ops;
15516 *ops = watchpoint_breakpoint_ops;
15517 ops->insert_location = insert_masked_watchpoint;
15518 ops->remove_location = remove_masked_watchpoint;
15519 ops->resources_needed = resources_needed_masked_watchpoint;
15520 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15521 ops->print_it = print_it_masked_watchpoint;
15522 ops->print_one_detail = print_one_detail_masked_watchpoint;
15523 ops->print_mention = print_mention_masked_watchpoint;
15524 ops->print_recreate = print_recreate_masked_watchpoint;
15525
15526 /* Tracepoints. */
15527 ops = &tracepoint_breakpoint_ops;
15528 *ops = base_breakpoint_ops;
15529 ops->re_set = tracepoint_re_set;
15530 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15531 ops->print_one_detail = tracepoint_print_one_detail;
15532 ops->print_mention = tracepoint_print_mention;
15533 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15534 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15535 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15536 ops->decode_location = tracepoint_decode_location;
983af33b 15537
55aa24fb
SDJ
15538 /* Probe tracepoints. */
15539 ops = &tracepoint_probe_breakpoint_ops;
15540 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15541 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15542 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15543
983af33b
SDJ
15544 /* Static tracepoints with marker (`-m'). */
15545 ops = &strace_marker_breakpoint_ops;
15546 *ops = tracepoint_breakpoint_ops;
5f700d83 15547 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15548 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15549 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15550
15551 /* Fork catchpoints. */
15552 ops = &catch_fork_breakpoint_ops;
15553 *ops = base_breakpoint_ops;
15554 ops->insert_location = insert_catch_fork;
15555 ops->remove_location = remove_catch_fork;
15556 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15557 ops->print_it = print_it_catch_fork;
15558 ops->print_one = print_one_catch_fork;
15559 ops->print_mention = print_mention_catch_fork;
15560 ops->print_recreate = print_recreate_catch_fork;
15561
15562 /* Vfork catchpoints. */
15563 ops = &catch_vfork_breakpoint_ops;
15564 *ops = base_breakpoint_ops;
15565 ops->insert_location = insert_catch_vfork;
15566 ops->remove_location = remove_catch_vfork;
15567 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15568 ops->print_it = print_it_catch_vfork;
15569 ops->print_one = print_one_catch_vfork;
15570 ops->print_mention = print_mention_catch_vfork;
15571 ops->print_recreate = print_recreate_catch_vfork;
15572
15573 /* Exec catchpoints. */
15574 ops = &catch_exec_breakpoint_ops;
15575 *ops = base_breakpoint_ops;
2060206e
PA
15576 ops->insert_location = insert_catch_exec;
15577 ops->remove_location = remove_catch_exec;
15578 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15579 ops->print_it = print_it_catch_exec;
15580 ops->print_one = print_one_catch_exec;
15581 ops->print_mention = print_mention_catch_exec;
15582 ops->print_recreate = print_recreate_catch_exec;
15583
edcc5120
TT
15584 /* Solib-related catchpoints. */
15585 ops = &catch_solib_breakpoint_ops;
15586 *ops = base_breakpoint_ops;
edcc5120
TT
15587 ops->insert_location = insert_catch_solib;
15588 ops->remove_location = remove_catch_solib;
15589 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15590 ops->check_status = check_status_catch_solib;
15591 ops->print_it = print_it_catch_solib;
15592 ops->print_one = print_one_catch_solib;
15593 ops->print_mention = print_mention_catch_solib;
15594 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15595
15596 ops = &dprintf_breakpoint_ops;
15597 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15598 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15599 ops->resources_needed = bkpt_resources_needed;
15600 ops->print_it = bkpt_print_it;
15601 ops->print_mention = bkpt_print_mention;
2d9442cc 15602 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15603 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15604 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15605}
15606
8bfd80db
YQ
15607/* Chain containing all defined "enable breakpoint" subcommands. */
15608
15609static struct cmd_list_element *enablebreaklist = NULL;
15610
c906108c 15611void
fba45db2 15612_initialize_breakpoint (void)
c906108c
SS
15613{
15614 struct cmd_list_element *c;
15615
2060206e
PA
15616 initialize_breakpoint_ops ();
15617
84acb35a 15618 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15619 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15620 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15621
55aa24fb 15622 breakpoint_objfile_key
43dce439 15623 = register_objfile_data_with_cleanup (NULL, free_breakpoint_objfile_data);
17450429 15624
c906108c
SS
15625 breakpoint_chain = 0;
15626 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15627 before a breakpoint is set. */
15628 breakpoint_count = 0;
15629
1042e4c0
SS
15630 tracepoint_count = 0;
15631
1bedd215
AC
15632 add_com ("ignore", class_breakpoint, ignore_command, _("\
15633Set ignore-count of breakpoint number N to COUNT.\n\
15634Usage is `ignore N COUNT'."));
c906108c 15635
1bedd215 15636 add_com ("commands", class_breakpoint, commands_command, _("\
18da0c51
MG
15637Set commands to be executed when the given breakpoints are hit.\n\
15638Give a space-separated breakpoint list as argument after \"commands\".\n\
15639A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
15640(e.g. `5-7').\n\
c906108c
SS
15641With no argument, the targeted breakpoint is the last one set.\n\
15642The commands themselves follow starting on the next line.\n\
15643Type a line containing \"end\" to indicate the end of them.\n\
15644Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15645then no output is printed when it is hit, except what the commands print."));
c906108c 15646
d55637df 15647 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15648Specify breakpoint number N to break only if COND is true.\n\
c906108c 15649Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15650expression to be evaluated whenever breakpoint N is reached."));
d55637df 15651 set_cmd_completer (c, condition_completer);
c906108c 15652
1bedd215 15653 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15654Set a temporary breakpoint.\n\
c906108c
SS
15655Like \"break\" except the breakpoint is only temporary,\n\
15656so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15657by using \"enable delete\" on the breakpoint number.\n\
15658\n"
15659BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15660 set_cmd_completer (c, location_completer);
c94fdfd0 15661
1bedd215 15662 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15663Set a hardware assisted breakpoint.\n\
c906108c 15664Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15665some target hardware may not have this support.\n\
15666\n"
15667BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15668 set_cmd_completer (c, location_completer);
c906108c 15669
1bedd215 15670 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15671Set a temporary hardware assisted breakpoint.\n\
c906108c 15672Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15673so it will be deleted when hit.\n\
15674\n"
15675BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15676 set_cmd_completer (c, location_completer);
c906108c 15677
1bedd215
AC
15678 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15679Enable some breakpoints.\n\
c906108c
SS
15680Give breakpoint numbers (separated by spaces) as arguments.\n\
15681With no subcommand, breakpoints are enabled until you command otherwise.\n\
15682This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15683With a subcommand you can enable temporarily."),
c906108c 15684 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15685
15686 add_com_alias ("en", "enable", class_breakpoint, 1);
15687
84951ab5 15688 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15689Enable some breakpoints.\n\
c906108c
SS
15690Give breakpoint numbers (separated by spaces) as arguments.\n\
15691This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15692May be abbreviated to simply \"enable\".\n"),
c5aa993b 15693 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15694
1a966eab
AC
15695 add_cmd ("once", no_class, enable_once_command, _("\
15696Enable breakpoints for one hit. Give breakpoint numbers.\n\
15697If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15698 &enablebreaklist);
15699
1a966eab
AC
15700 add_cmd ("delete", no_class, enable_delete_command, _("\
15701Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15702If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15703 &enablebreaklist);
15704
816338b5
SS
15705 add_cmd ("count", no_class, enable_count_command, _("\
15706Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15707If a breakpoint is hit while enabled in this fashion,\n\
15708the count is decremented; when it reaches zero, the breakpoint is disabled."),
15709 &enablebreaklist);
15710
1a966eab
AC
15711 add_cmd ("delete", no_class, enable_delete_command, _("\
15712Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15713If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15714 &enablelist);
15715
1a966eab
AC
15716 add_cmd ("once", no_class, enable_once_command, _("\
15717Enable breakpoints for one hit. Give breakpoint numbers.\n\
15718If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15719 &enablelist);
15720
15721 add_cmd ("count", no_class, enable_count_command, _("\
15722Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15723If a breakpoint is hit while enabled in this fashion,\n\
15724the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15725 &enablelist);
15726
1bedd215
AC
15727 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15728Disable some breakpoints.\n\
c906108c
SS
15729Arguments are breakpoint numbers with spaces in between.\n\
15730To disable all breakpoints, give no argument.\n\
64b9b334 15731A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15732 &disablelist, "disable ", 1, &cmdlist);
15733 add_com_alias ("dis", "disable", class_breakpoint, 1);
15734 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15735
1a966eab
AC
15736 add_cmd ("breakpoints", class_alias, disable_command, _("\
15737Disable some breakpoints.\n\
c906108c
SS
15738Arguments are breakpoint numbers with spaces in between.\n\
15739To disable all breakpoints, give no argument.\n\
64b9b334 15740A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15741This command may be abbreviated \"disable\"."),
c906108c
SS
15742 &disablelist);
15743
1bedd215
AC
15744 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15745Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15746Arguments are breakpoint numbers with spaces in between.\n\
15747To delete all breakpoints, give no argument.\n\
15748\n\
15749Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15750The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15751 &deletelist, "delete ", 1, &cmdlist);
15752 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15753 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15754
1a966eab
AC
15755 add_cmd ("breakpoints", class_alias, delete_command, _("\
15756Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15757Arguments are breakpoint numbers with spaces in between.\n\
15758To delete all breakpoints, give no argument.\n\
1a966eab 15759This command may be abbreviated \"delete\"."),
c906108c
SS
15760 &deletelist);
15761
1bedd215 15762 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
15763Clear breakpoint at specified location.\n\
15764Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
15765\n\
15766With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
15767is executing in.\n"
15768"\n" LOCATION_HELP_STRING "\n\
1bedd215 15769See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15770 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15771
1bedd215 15772 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 15773Set breakpoint at specified location.\n"
31e2b00f 15774BREAK_ARGS_HELP ("break")));
5ba2abeb 15775 set_cmd_completer (c, location_completer);
c94fdfd0 15776
c906108c
SS
15777 add_com_alias ("b", "break", class_run, 1);
15778 add_com_alias ("br", "break", class_run, 1);
15779 add_com_alias ("bre", "break", class_run, 1);
15780 add_com_alias ("brea", "break", class_run, 1);
15781
c906108c
SS
15782 if (dbx_commands)
15783 {
1bedd215
AC
15784 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15785Break in function/address or break at a line in the current file."),
c5aa993b
JM
15786 &stoplist, "stop ", 1, &cmdlist);
15787 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15788 _("Break in function or address."), &stoplist);
c5aa993b 15789 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15790 _("Break at a line in the current file."), &stoplist);
11db9430 15791 add_com ("status", class_info, info_breakpoints_command, _("\
1bedd215 15792Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15793The \"Type\" column indicates one of:\n\
15794\tbreakpoint - normal breakpoint\n\
15795\twatchpoint - watchpoint\n\
15796The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15797the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15798breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15799address and file/line number respectively.\n\
15800\n\
15801Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15802are set to the address of the last breakpoint listed unless the command\n\
15803is prefixed with \"server \".\n\n\
c906108c 15804Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15805breakpoint set."));
c906108c
SS
15806 }
15807
11db9430 15808 add_info ("breakpoints", info_breakpoints_command, _("\
e5a67952 15809Status of specified breakpoints (all user-settable breakpoints if no argument).\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 15823
6b04bdb7
MS
15824 add_info_alias ("b", "breakpoints", 1);
15825
1a966eab
AC
15826 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15827Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15828The \"Type\" column indicates one of:\n\
15829\tbreakpoint - normal breakpoint\n\
15830\twatchpoint - watchpoint\n\
15831\tlongjmp - internal breakpoint used to step through longjmp()\n\
15832\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15833\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15834\tfinish - internal breakpoint used by the \"finish\" command\n\
15835The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15836the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15837breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15838address and file/line number respectively.\n\
15839\n\
15840Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15841are set to the address of the last breakpoint listed unless the command\n\
15842is prefixed with \"server \".\n\n\
c906108c 15843Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15844breakpoint set."),
c906108c
SS
15845 &maintenanceinfolist);
15846
44feb3ce
TT
15847 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15848Set catchpoints to catch events."),
15849 &catch_cmdlist, "catch ",
15850 0/*allow-unknown*/, &cmdlist);
15851
15852 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15853Set temporary catchpoints to catch events."),
15854 &tcatch_cmdlist, "tcatch ",
15855 0/*allow-unknown*/, &cmdlist);
15856
44feb3ce
TT
15857 add_catch_command ("fork", _("Catch calls to fork."),
15858 catch_fork_command_1,
a96d9b2e 15859 NULL,
44feb3ce
TT
15860 (void *) (uintptr_t) catch_fork_permanent,
15861 (void *) (uintptr_t) catch_fork_temporary);
15862 add_catch_command ("vfork", _("Catch calls to vfork."),
15863 catch_fork_command_1,
a96d9b2e 15864 NULL,
44feb3ce
TT
15865 (void *) (uintptr_t) catch_vfork_permanent,
15866 (void *) (uintptr_t) catch_vfork_temporary);
15867 add_catch_command ("exec", _("Catch calls to exec."),
15868 catch_exec_command_1,
a96d9b2e
SDJ
15869 NULL,
15870 CATCH_PERMANENT,
15871 CATCH_TEMPORARY);
edcc5120
TT
15872 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15873Usage: catch load [REGEX]\n\
15874If REGEX is given, only stop for libraries matching the regular expression."),
15875 catch_load_command_1,
15876 NULL,
15877 CATCH_PERMANENT,
15878 CATCH_TEMPORARY);
15879 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15880Usage: catch unload [REGEX]\n\
15881If REGEX is given, only stop for libraries matching the regular expression."),
15882 catch_unload_command_1,
15883 NULL,
15884 CATCH_PERMANENT,
15885 CATCH_TEMPORARY);
c5aa993b 15886
1bedd215
AC
15887 c = add_com ("watch", class_breakpoint, watch_command, _("\
15888Set a watchpoint for an expression.\n\
06a64a0b 15889Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15890A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15891an expression changes.\n\
15892If -l or -location is given, this evaluates EXPRESSION and watches\n\
15893the memory to which it refers."));
65d12d83 15894 set_cmd_completer (c, expression_completer);
c906108c 15895
1bedd215
AC
15896 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15897Set a read watchpoint for an expression.\n\
06a64a0b 15898Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15899A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15900an expression is read.\n\
15901If -l or -location is given, this evaluates EXPRESSION and watches\n\
15902the memory to which it refers."));
65d12d83 15903 set_cmd_completer (c, expression_completer);
c906108c 15904
1bedd215
AC
15905 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15906Set a watchpoint for an expression.\n\
06a64a0b 15907Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15908A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15909an expression is either read or written.\n\
15910If -l or -location is given, this evaluates EXPRESSION and watches\n\
15911the memory to which it refers."));
65d12d83 15912 set_cmd_completer (c, expression_completer);
c906108c 15913
11db9430 15914 add_info ("watchpoints", info_watchpoints_command, _("\
e5a67952 15915Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 15916
920d2a44
AC
15917 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15918 respond to changes - contrary to the description. */
85c07804
AC
15919 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15920 &can_use_hw_watchpoints, _("\
15921Set debugger's willingness to use watchpoint hardware."), _("\
15922Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
15923If zero, gdb will not use hardware for new watchpoints, even if\n\
15924such is available. (However, any hardware watchpoints that were\n\
15925created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
15926hardware.)"),
15927 NULL,
920d2a44 15928 show_can_use_hw_watchpoints,
85c07804 15929 &setlist, &showlist);
c906108c
SS
15930
15931 can_use_hw_watchpoints = 1;
fa8d40ab 15932
1042e4c0
SS
15933 /* Tracepoint manipulation commands. */
15934
15935 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 15936Set a tracepoint at specified location.\n\
1042e4c0
SS
15937\n"
15938BREAK_ARGS_HELP ("trace") "\n\
15939Do \"help tracepoints\" for info on other tracepoint commands."));
15940 set_cmd_completer (c, location_completer);
15941
15942 add_com_alias ("tp", "trace", class_alias, 0);
15943 add_com_alias ("tr", "trace", class_alias, 1);
15944 add_com_alias ("tra", "trace", class_alias, 1);
15945 add_com_alias ("trac", "trace", class_alias, 1);
15946
7a697b8d 15947 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 15948Set a fast tracepoint at specified location.\n\
7a697b8d
SS
15949\n"
15950BREAK_ARGS_HELP ("ftrace") "\n\
15951Do \"help tracepoints\" for info on other tracepoint commands."));
15952 set_cmd_completer (c, location_completer);
15953
0fb4aa4b 15954 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 15955Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
15956\n\
15957strace [LOCATION] [if CONDITION]\n\
629500fa
KS
15958LOCATION may be a linespec, explicit, or address location (described below) \n\
15959or -m MARKER_ID.\n\n\
15960If a marker id is specified, probe the marker with that name. With\n\
15961no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
15962Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15963This collects arbitrary user data passed in the probe point call to the\n\
15964tracing library. You can inspect it when analyzing the trace buffer,\n\
15965by printing the $_sdata variable like any other convenience variable.\n\
15966\n\
15967CONDITION is a boolean expression.\n\
629500fa 15968\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15969Multiple tracepoints at one place are permitted, and useful if their\n\
15970conditions are different.\n\
0fb4aa4b
PA
15971\n\
15972Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15973Do \"help tracepoints\" for info on other tracepoint commands."));
15974 set_cmd_completer (c, location_completer);
15975
11db9430 15976 add_info ("tracepoints", info_tracepoints_command, _("\
e5a67952 15977Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
15978Convenience variable \"$tpnum\" contains the number of the\n\
15979last tracepoint set."));
15980
15981 add_info_alias ("tp", "tracepoints", 1);
15982
15983 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15984Delete specified tracepoints.\n\
15985Arguments are tracepoint numbers, separated by spaces.\n\
15986No argument means delete all tracepoints."),
15987 &deletelist);
7e20dfcd 15988 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
15989
15990 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15991Disable specified tracepoints.\n\
15992Arguments are tracepoint numbers, separated by spaces.\n\
15993No argument means disable all tracepoints."),
15994 &disablelist);
15995 deprecate_cmd (c, "disable");
15996
15997 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
15998Enable specified tracepoints.\n\
15999Arguments are tracepoint numbers, separated by spaces.\n\
16000No argument means enable all tracepoints."),
16001 &enablelist);
16002 deprecate_cmd (c, "enable");
16003
16004 add_com ("passcount", class_trace, trace_pass_command, _("\
16005Set the passcount for a tracepoint.\n\
16006The trace will end when the tracepoint has been passed 'count' times.\n\
16007Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16008if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16009
6149aea9
PA
16010 add_prefix_cmd ("save", class_breakpoint, save_command,
16011 _("Save breakpoint definitions as a script."),
16012 &save_cmdlist, "save ",
16013 0/*allow-unknown*/, &cmdlist);
16014
16015 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16016Save current breakpoint definitions as a script.\n\
cce7e648 16017This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16018catchpoints, tracepoints). Use the 'source' command in another debug\n\
16019session to restore them."),
16020 &save_cmdlist);
16021 set_cmd_completer (c, filename_completer);
16022
16023 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16024Save current tracepoint definitions as a script.\n\
6149aea9
PA
16025Use the 'source' command in another debug session to restore them."),
16026 &save_cmdlist);
1042e4c0
SS
16027 set_cmd_completer (c, filename_completer);
16028
6149aea9
PA
16029 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16030 deprecate_cmd (c, "save tracepoints");
16031
1bedd215 16032 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16033Breakpoint specific settings\n\
16034Configure various breakpoint-specific variables such as\n\
1bedd215 16035pending breakpoint behavior"),
fa8d40ab
JJ
16036 &breakpoint_set_cmdlist, "set breakpoint ",
16037 0/*allow-unknown*/, &setlist);
1bedd215 16038 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16039Breakpoint specific settings\n\
16040Configure various breakpoint-specific variables such as\n\
1bedd215 16041pending breakpoint behavior"),
fa8d40ab
JJ
16042 &breakpoint_show_cmdlist, "show breakpoint ",
16043 0/*allow-unknown*/, &showlist);
16044
7915a72c
AC
16045 add_setshow_auto_boolean_cmd ("pending", no_class,
16046 &pending_break_support, _("\
16047Set debugger's behavior regarding pending breakpoints."), _("\
16048Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16049If on, an unrecognized breakpoint location will cause gdb to create a\n\
16050pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16051an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16052user-query to see if a pending breakpoint should be created."),
2c5b56ce 16053 NULL,
920d2a44 16054 show_pending_break_support,
6e1d7d6c
AC
16055 &breakpoint_set_cmdlist,
16056 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16057
16058 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16059
16060 add_setshow_boolean_cmd ("auto-hw", no_class,
16061 &automatic_hardware_breakpoints, _("\
16062Set automatic usage of hardware breakpoints."), _("\
16063Show automatic usage of hardware breakpoints."), _("\
16064If set, the debugger will automatically use hardware breakpoints for\n\
16065breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16066a warning will be emitted for such breakpoints."),
16067 NULL,
16068 show_automatic_hardware_breakpoints,
16069 &breakpoint_set_cmdlist,
16070 &breakpoint_show_cmdlist);
74960c60 16071
a25a5a45
PA
16072 add_setshow_boolean_cmd ("always-inserted", class_support,
16073 &always_inserted_mode, _("\
74960c60
VP
16074Set mode for inserting breakpoints."), _("\
16075Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16076When this mode is on, breakpoints are inserted immediately as soon as\n\
16077they're created, kept inserted even when execution stops, and removed\n\
16078only when the user deletes them. When this mode is off (the default),\n\
16079breakpoints are inserted only when execution continues, and removed\n\
16080when execution stops."),
72d0e2c5
YQ
16081 NULL,
16082 &show_always_inserted_mode,
16083 &breakpoint_set_cmdlist,
16084 &breakpoint_show_cmdlist);
f1310107 16085
b775012e
LM
16086 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16087 condition_evaluation_enums,
16088 &condition_evaluation_mode_1, _("\
16089Set mode of breakpoint condition evaluation."), _("\
16090Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16091When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16092evaluated on the host's side by GDB. When it is set to \"target\",\n\
16093breakpoint conditions will be downloaded to the target (if the target\n\
16094supports such feature) and conditions will be evaluated on the target's side.\n\
16095If this is set to \"auto\" (default), this will be automatically set to\n\
16096\"target\" if it supports condition evaluation, otherwise it will\n\
16097be set to \"gdb\""),
16098 &set_condition_evaluation_mode,
16099 &show_condition_evaluation_mode,
16100 &breakpoint_set_cmdlist,
16101 &breakpoint_show_cmdlist);
16102
f1310107
TJB
16103 add_com ("break-range", class_breakpoint, break_range_command, _("\
16104Set a breakpoint for an address range.\n\
16105break-range START-LOCATION, END-LOCATION\n\
16106where START-LOCATION and END-LOCATION can be one of the following:\n\
16107 LINENUM, for that line in the current file,\n\
16108 FILE:LINENUM, for that line in that file,\n\
16109 +OFFSET, for that number of lines after the current line\n\
16110 or the start of the range\n\
16111 FUNCTION, for the first line in that function,\n\
16112 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16113 *ADDRESS, for the instruction at that address.\n\
16114\n\
16115The breakpoint will stop execution of the inferior whenever it executes\n\
16116an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16117range (including START-LOCATION and END-LOCATION)."));
16118
e7e0cddf 16119 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16120Set a dynamic printf at specified location.\n\
e7e0cddf 16121dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16122location may be a linespec, explicit, or address location.\n"
16123"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16124 set_cmd_completer (c, location_completer);
16125
16126 add_setshow_enum_cmd ("dprintf-style", class_support,
16127 dprintf_style_enums, &dprintf_style, _("\
16128Set the style of usage for dynamic printf."), _("\
16129Show the style of usage for dynamic printf."), _("\
16130This setting chooses how GDB will do a dynamic printf.\n\
16131If the value is \"gdb\", then the printing is done by GDB to its own\n\
16132console, as with the \"printf\" command.\n\
16133If the value is \"call\", the print is done by calling a function in your\n\
16134program; by default printf(), but you can choose a different function or\n\
16135output stream by setting dprintf-function and dprintf-channel."),
16136 update_dprintf_commands, NULL,
16137 &setlist, &showlist);
16138
16139 dprintf_function = xstrdup ("printf");
16140 add_setshow_string_cmd ("dprintf-function", class_support,
16141 &dprintf_function, _("\
16142Set the function to use for dynamic printf"), _("\
16143Show the function to use for dynamic printf"), NULL,
16144 update_dprintf_commands, NULL,
16145 &setlist, &showlist);
16146
16147 dprintf_channel = xstrdup ("");
16148 add_setshow_string_cmd ("dprintf-channel", class_support,
16149 &dprintf_channel, _("\
16150Set the channel to use for dynamic printf"), _("\
16151Show the channel to use for dynamic printf"), NULL,
16152 update_dprintf_commands, NULL,
16153 &setlist, &showlist);
16154
d3ce09f5
SS
16155 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16156 &disconnected_dprintf, _("\
16157Set whether dprintf continues after GDB disconnects."), _("\
16158Show whether dprintf continues after GDB disconnects."), _("\
16159Use this to let dprintf commands continue to hit and produce output\n\
16160even if GDB disconnects or detaches from the target."),
16161 NULL,
16162 NULL,
16163 &setlist, &showlist);
16164
16165 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16166agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16167(target agent only) This is useful for formatted output in user-defined commands."));
16168
765dc015 16169 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16170
16171 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16172 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16173}
This page took 3.221477 seconds and 4 git commands to generate.