Fix PR threads/19422 - show which thread caused stop
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
618f726f 3 Copyright (C) 1986-2016 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
45741a9c 35#include "infrun.h"
c906108c
SS
36#include "gdbthread.h"
37#include "target.h"
38#include "language.h"
50f182aa 39#include "gdb-demangle.h"
0ba1096a 40#include "filenames.h"
c906108c
SS
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
5b7f31a4 47#include "gdb.h"
8b93c638 48#include "ui-out.h"
e1507482 49#include "cli/cli-script.h"
fe898f56 50#include "block.h"
a77053c2 51#include "solib.h"
84acb35a
JJ
52#include "solist.h"
53#include "observer.h"
765dc015 54#include "memattr.h"
f7f9143b 55#include "ada-lang.h"
d1aa2f50 56#include "top.h"
79a45b7d 57#include "valprint.h"
4efc6507 58#include "jit.h"
65d79d4b 59#include "parser-defs.h"
55aa24fb
SDJ
60#include "gdb_regex.h"
61#include "probe.h"
e9cafbcc 62#include "cli/cli-utils.h"
be34f849 63#include "continuations.h"
1bfeeb0f
JL
64#include "stack.h"
65#include "skip.h"
b775012e 66#include "ax-gdb.h"
e2e4d78b 67#include "dummy-frame.h"
5589af0e 68#include "interps.h"
d3ce09f5 69#include "format.h"
f00aae0f 70#include "location.h"
cfc31633 71#include "thread-fsm.h"
5d5658a1 72#include "tid-parse.h"
d3ce09f5 73
1042e4c0
SS
74/* readline include files */
75#include "readline/readline.h"
76#include "readline/history.h"
77
78/* readline defines this. */
79#undef savestring
80
034dad6f 81#include "mi/mi-common.h"
6dddc817 82#include "extension.h"
104c1213 83
e7e8980f
YQ
84/* Enums for exception-handling support. */
85enum exception_event_kind
86{
87 EX_EVENT_THROW,
591f19e8 88 EX_EVENT_RETHROW,
e7e8980f
YQ
89 EX_EVENT_CATCH
90};
91
4a64f543 92/* Prototypes for local functions. */
c906108c 93
a14ed312 94static void enable_delete_command (char *, int);
c906108c 95
a14ed312 96static void enable_once_command (char *, int);
c906108c 97
816338b5
SS
98static void enable_count_command (char *, int);
99
a14ed312 100static void disable_command (char *, int);
c906108c 101
a14ed312 102static void enable_command (char *, int);
c906108c 103
95a42b64
TT
104static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
105 void *),
106 void *);
c906108c 107
a14ed312 108static void ignore_command (char *, int);
c906108c 109
4efb68b1 110static int breakpoint_re_set_one (void *);
c906108c 111
348d480f
PA
112static void breakpoint_re_set_default (struct breakpoint *);
113
f00aae0f
KS
114static void
115 create_sals_from_location_default (const struct event_location *location,
116 struct linespec_result *canonical,
117 enum bptype type_wanted);
983af33b
SDJ
118
119static void create_breakpoints_sal_default (struct gdbarch *,
120 struct linespec_result *,
e7e0cddf 121 char *, char *, enum bptype,
983af33b
SDJ
122 enum bpdisp, int, int,
123 int,
124 const struct breakpoint_ops *,
44f238bb 125 int, int, int, unsigned);
983af33b 126
f00aae0f
KS
127static void decode_location_default (struct breakpoint *b,
128 const struct event_location *location,
129 struct symtabs_and_lines *sals);
983af33b 130
a14ed312 131static void clear_command (char *, int);
c906108c 132
a14ed312 133static void catch_command (char *, int);
c906108c 134
a9634178 135static int can_use_hardware_watchpoint (struct value *);
c906108c 136
98deb0da 137static void break_command_1 (char *, int, int);
c906108c 138
a14ed312 139static void mention (struct breakpoint *);
c906108c 140
348d480f
PA
141static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
142 enum bptype,
c0a91b2b 143 const struct breakpoint_ops *);
3742cc8b
YQ
144static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
145 const struct symtab_and_line *);
146
4a64f543
MS
147/* This function is used in gdbtk sources and thus can not be made
148 static. */
63c252f8 149struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 150 struct symtab_and_line,
c0a91b2b
TT
151 enum bptype,
152 const struct breakpoint_ops *);
c906108c 153
06edf0c0
PA
154static struct breakpoint *
155 momentary_breakpoint_from_master (struct breakpoint *orig,
156 enum bptype type,
a1aa2221
LM
157 const struct breakpoint_ops *ops,
158 int loc_enabled);
06edf0c0 159
76897487
KB
160static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
161
a6d9a66e
UW
162static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
163 CORE_ADDR bpaddr,
88f7da05 164 enum bptype bptype);
76897487 165
6c95b8df
PA
166static void describe_other_breakpoints (struct gdbarch *,
167 struct program_space *, CORE_ADDR,
5af949e3 168 struct obj_section *, int);
c906108c 169
85d721b8
PA
170static int watchpoint_locations_match (struct bp_location *loc1,
171 struct bp_location *loc2);
172
f1310107
TJB
173static int breakpoint_location_address_match (struct bp_location *bl,
174 struct address_space *aspace,
175 CORE_ADDR addr);
176
d35ae833
PA
177static int breakpoint_location_address_range_overlap (struct bp_location *,
178 struct address_space *,
179 CORE_ADDR, int);
180
a14ed312 181static void breakpoints_info (char *, int);
c906108c 182
d77f58be
SS
183static void watchpoints_info (char *, int);
184
e5a67952
MS
185static int breakpoint_1 (char *, int,
186 int (*) (const struct breakpoint *));
c906108c 187
4efb68b1 188static int breakpoint_cond_eval (void *);
c906108c 189
4efb68b1 190static void cleanup_executing_breakpoints (void *);
c906108c 191
a14ed312 192static void commands_command (char *, int);
c906108c 193
a14ed312 194static void condition_command (char *, int);
c906108c 195
c5aa993b
JM
196typedef enum
197 {
198 mark_inserted,
199 mark_uninserted
200 }
201insertion_state_t;
c906108c 202
0bde7532 203static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 204static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 205
e514a9d6 206static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 207
4efb68b1 208static int watchpoint_check (void *);
c906108c 209
a14ed312 210static void maintenance_info_breakpoints (char *, int);
c906108c 211
a14ed312 212static int hw_breakpoint_used_count (void);
c906108c 213
a1398e0c
PA
214static int hw_watchpoint_use_count (struct breakpoint *);
215
216static int hw_watchpoint_used_count_others (struct breakpoint *except,
217 enum bptype type,
218 int *other_type_used);
c906108c 219
a14ed312 220static void hbreak_command (char *, int);
c906108c 221
a14ed312 222static void thbreak_command (char *, int);
c906108c 223
816338b5
SS
224static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
225 int count);
c906108c 226
a14ed312 227static void stop_command (char *arg, int from_tty);
7a292a7a 228
a14ed312 229static void stopin_command (char *arg, int from_tty);
7a292a7a 230
a14ed312 231static void stopat_command (char *arg, int from_tty);
7a292a7a 232
a14ed312 233static void tcatch_command (char *arg, int from_tty);
7a292a7a 234
fe3f5fa8 235static void free_bp_location (struct bp_location *loc);
f431efe5
PA
236static void incref_bp_location (struct bp_location *loc);
237static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 238
39d61571 239static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 240
44702360
PA
241/* update_global_location_list's modes of operation wrt to whether to
242 insert locations now. */
243enum ugll_insert_mode
244{
245 /* Don't insert any breakpoint locations into the inferior, only
246 remove already-inserted locations that no longer should be
247 inserted. Functions that delete a breakpoint or breakpoints
248 should specify this mode, so that deleting a breakpoint doesn't
249 have the side effect of inserting the locations of other
250 breakpoints that are marked not-inserted, but should_be_inserted
251 returns true on them.
252
253 This behavior is useful is situations close to tear-down -- e.g.,
254 after an exec, while the target still has execution, but
255 breakpoint shadows of the previous executable image should *NOT*
256 be restored to the new image; or before detaching, where the
257 target still has execution and wants to delete breakpoints from
258 GDB's lists, and all breakpoints had already been removed from
259 the inferior. */
260 UGLL_DONT_INSERT,
261
a25a5a45
PA
262 /* May insert breakpoints iff breakpoints_should_be_inserted_now
263 claims breakpoints should be inserted now. */
04086b45
PA
264 UGLL_MAY_INSERT,
265
a25a5a45
PA
266 /* Insert locations now, irrespective of
267 breakpoints_should_be_inserted_now. E.g., say all threads are
268 stopped right now, and the user did "continue". We need to
269 insert breakpoints _before_ resuming the target, but
270 UGLL_MAY_INSERT wouldn't insert them, because
271 breakpoints_should_be_inserted_now returns false at that point,
272 as no thread is running yet. */
04086b45 273 UGLL_INSERT
44702360
PA
274};
275
276static void update_global_location_list (enum ugll_insert_mode);
a5606eee 277
44702360 278static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 279
d77f58be 280static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
281
282static void insert_breakpoint_locations (void);
a5606eee 283
1042e4c0
SS
284static void tracepoints_info (char *, int);
285
286static void delete_trace_command (char *, int);
287
288static void enable_trace_command (char *, int);
289
290static void disable_trace_command (char *, int);
291
292static void trace_pass_command (char *, int);
293
558a9d82
YQ
294static void set_tracepoint_count (int num);
295
9c06b0b4
TJB
296static int is_masked_watchpoint (const struct breakpoint *b);
297
b775012e
LM
298static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
299
983af33b
SDJ
300/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
301 otherwise. */
302
303static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 304
2060206e
PA
305/* The breakpoint_ops structure to be inherited by all breakpoint_ops
306 that are implemented on top of software or hardware breakpoints
307 (user breakpoints, internal and momentary breakpoints, etc.). */
308static struct breakpoint_ops bkpt_base_breakpoint_ops;
309
310/* Internal breakpoints class type. */
06edf0c0 311static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
312
313/* Momentary breakpoints class type. */
06edf0c0
PA
314static struct breakpoint_ops momentary_breakpoint_ops;
315
e2e4d78b
JK
316/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
317static struct breakpoint_ops longjmp_breakpoint_ops;
318
2060206e
PA
319/* The breakpoint_ops structure to be used in regular user created
320 breakpoints. */
321struct breakpoint_ops bkpt_breakpoint_ops;
322
55aa24fb
SDJ
323/* Breakpoints set on probes. */
324static struct breakpoint_ops bkpt_probe_breakpoint_ops;
325
e7e0cddf 326/* Dynamic printf class type. */
c5867ab6 327struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 328
d3ce09f5
SS
329/* The style in which to perform a dynamic printf. This is a user
330 option because different output options have different tradeoffs;
331 if GDB does the printing, there is better error handling if there
332 is a problem with any of the arguments, but using an inferior
333 function lets you have special-purpose printers and sending of
334 output to the same place as compiled-in print functions. */
335
336static const char dprintf_style_gdb[] = "gdb";
337static const char dprintf_style_call[] = "call";
338static const char dprintf_style_agent[] = "agent";
339static const char *const dprintf_style_enums[] = {
340 dprintf_style_gdb,
341 dprintf_style_call,
342 dprintf_style_agent,
343 NULL
344};
345static const char *dprintf_style = dprintf_style_gdb;
346
347/* The function to use for dynamic printf if the preferred style is to
348 call into the inferior. The value is simply a string that is
349 copied into the command, so it can be anything that GDB can
350 evaluate to a callable address, not necessarily a function name. */
351
352static char *dprintf_function = "";
353
354/* The channel to use for dynamic printf if the preferred style is to
355 call into the inferior; if a nonempty string, it will be passed to
356 the call as the first argument, with the format string as the
357 second. As with the dprintf function, this can be anything that
358 GDB knows how to evaluate, so in addition to common choices like
359 "stderr", this could be an app-specific expression like
360 "mystreams[curlogger]". */
361
362static char *dprintf_channel = "";
363
364/* True if dprintf commands should continue to operate even if GDB
365 has disconnected. */
366static int disconnected_dprintf = 1;
367
5cea2a26
PA
368/* A reference-counted struct command_line. This lets multiple
369 breakpoints share a single command list. */
370struct counted_command_line
371{
372 /* The reference count. */
373 int refc;
374
375 /* The command list. */
376 struct command_line *commands;
377};
378
379struct command_line *
380breakpoint_commands (struct breakpoint *b)
381{
382 return b->commands ? b->commands->commands : NULL;
383}
3daf8fe5 384
f3b1572e
PA
385/* Flag indicating that a command has proceeded the inferior past the
386 current breakpoint. */
387
388static int breakpoint_proceeded;
389
956a9fb9 390const char *
2cec12e5
AR
391bpdisp_text (enum bpdisp disp)
392{
4a64f543
MS
393 /* NOTE: the following values are a part of MI protocol and
394 represent values of 'disp' field returned when inferior stops at
395 a breakpoint. */
bc043ef3 396 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 397
2cec12e5
AR
398 return bpdisps[(int) disp];
399}
c906108c 400
4a64f543 401/* Prototypes for exported functions. */
c906108c 402/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 403 if such is available. */
c906108c
SS
404static int can_use_hw_watchpoints;
405
920d2a44
AC
406static void
407show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
408 struct cmd_list_element *c,
409 const char *value)
410{
3e43a32a
MS
411 fprintf_filtered (file,
412 _("Debugger's willingness to use "
413 "watchpoint hardware is %s.\n"),
920d2a44
AC
414 value);
415}
416
fa8d40ab
JJ
417/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
418 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 419 for unrecognized breakpoint locations.
fa8d40ab
JJ
420 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
421static enum auto_boolean pending_break_support;
920d2a44
AC
422static void
423show_pending_break_support (struct ui_file *file, int from_tty,
424 struct cmd_list_element *c,
425 const char *value)
426{
3e43a32a
MS
427 fprintf_filtered (file,
428 _("Debugger's behavior regarding "
429 "pending breakpoints is %s.\n"),
920d2a44
AC
430 value);
431}
fa8d40ab 432
765dc015 433/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 434 set with "break" but falling in read-only memory.
765dc015
VP
435 If 0, gdb will warn about such breakpoints, but won't automatically
436 use hardware breakpoints. */
437static int automatic_hardware_breakpoints;
438static void
439show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
440 struct cmd_list_element *c,
441 const char *value)
442{
3e43a32a
MS
443 fprintf_filtered (file,
444 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
445 value);
446}
447
a25a5a45
PA
448/* If on, GDB keeps breakpoints inserted even if the inferior is
449 stopped, and immediately inserts any new breakpoints as soon as
450 they're created. If off (default), GDB keeps breakpoints off of
451 the target as long as possible. That is, it delays inserting
452 breakpoints until the next resume, and removes them again when the
453 target fully stops. This is a bit safer in case GDB crashes while
454 processing user input. */
455static int always_inserted_mode = 0;
72d0e2c5 456
33e5cbd6 457static void
74960c60 458show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 459 struct cmd_list_element *c, const char *value)
74960c60 460{
a25a5a45
PA
461 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
462 value);
74960c60
VP
463}
464
b57bacec
PA
465/* See breakpoint.h. */
466
33e5cbd6 467int
a25a5a45 468breakpoints_should_be_inserted_now (void)
33e5cbd6 469{
a25a5a45
PA
470 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
471 {
472 /* If breakpoints are global, they should be inserted even if no
473 thread under gdb's control is running, or even if there are
474 no threads under GDB's control yet. */
475 return 1;
476 }
477 else if (target_has_execution)
478 {
372316f1
PA
479 struct thread_info *tp;
480
a25a5a45
PA
481 if (always_inserted_mode)
482 {
483 /* The user wants breakpoints inserted even if all threads
484 are stopped. */
485 return 1;
486 }
487
b57bacec
PA
488 if (threads_are_executing ())
489 return 1;
372316f1
PA
490
491 /* Don't remove breakpoints yet if, even though all threads are
492 stopped, we still have events to process. */
493 ALL_NON_EXITED_THREADS (tp)
494 if (tp->resumed
495 && tp->suspend.waitstatus_pending_p)
496 return 1;
a25a5a45
PA
497 }
498 return 0;
33e5cbd6 499}
765dc015 500
b775012e
LM
501static const char condition_evaluation_both[] = "host or target";
502
503/* Modes for breakpoint condition evaluation. */
504static const char condition_evaluation_auto[] = "auto";
505static const char condition_evaluation_host[] = "host";
506static const char condition_evaluation_target[] = "target";
507static const char *const condition_evaluation_enums[] = {
508 condition_evaluation_auto,
509 condition_evaluation_host,
510 condition_evaluation_target,
511 NULL
512};
513
514/* Global that holds the current mode for breakpoint condition evaluation. */
515static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
516
517/* Global that we use to display information to the user (gets its value from
518 condition_evaluation_mode_1. */
519static const char *condition_evaluation_mode = condition_evaluation_auto;
520
521/* Translate a condition evaluation mode MODE into either "host"
522 or "target". This is used mostly to translate from "auto" to the
523 real setting that is being used. It returns the translated
524 evaluation mode. */
525
526static const char *
527translate_condition_evaluation_mode (const char *mode)
528{
529 if (mode == condition_evaluation_auto)
530 {
531 if (target_supports_evaluation_of_breakpoint_conditions ())
532 return condition_evaluation_target;
533 else
534 return condition_evaluation_host;
535 }
536 else
537 return mode;
538}
539
540/* Discovers what condition_evaluation_auto translates to. */
541
542static const char *
543breakpoint_condition_evaluation_mode (void)
544{
545 return translate_condition_evaluation_mode (condition_evaluation_mode);
546}
547
548/* Return true if GDB should evaluate breakpoint conditions or false
549 otherwise. */
550
551static int
552gdb_evaluates_breakpoint_condition_p (void)
553{
554 const char *mode = breakpoint_condition_evaluation_mode ();
555
556 return (mode == condition_evaluation_host);
557}
558
a14ed312 559void _initialize_breakpoint (void);
c906108c 560
c906108c
SS
561/* Are we executing breakpoint commands? */
562static int executing_breakpoint_commands;
563
c02f5703
MS
564/* Are overlay event breakpoints enabled? */
565static int overlay_events_enabled;
566
e09342b5
TJB
567/* See description in breakpoint.h. */
568int target_exact_watchpoints = 0;
569
c906108c 570/* Walk the following statement or block through all breakpoints.
e5dd4106 571 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 572 current breakpoint. */
c906108c 573
5c44784c 574#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 575
5c44784c
JM
576#define ALL_BREAKPOINTS_SAFE(B,TMP) \
577 for (B = breakpoint_chain; \
578 B ? (TMP=B->next, 1): 0; \
579 B = TMP)
c906108c 580
4a64f543
MS
581/* Similar iterator for the low-level breakpoints. SAFE variant is
582 not provided so update_global_location_list must not be called
583 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 584
876fa593
JK
585#define ALL_BP_LOCATIONS(B,BP_TMP) \
586 for (BP_TMP = bp_location; \
587 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
588 BP_TMP++)
7cc221ef 589
b775012e
LM
590/* Iterates through locations with address ADDRESS for the currently selected
591 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
592 to where the loop should start from.
593 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
594 appropriate location to start with. */
595
596#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
597 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
598 BP_LOCP_TMP = BP_LOCP_START; \
599 BP_LOCP_START \
600 && (BP_LOCP_TMP < bp_location + bp_location_count \
601 && (*BP_LOCP_TMP)->address == ADDRESS); \
602 BP_LOCP_TMP++)
603
1042e4c0
SS
604/* Iterator for tracepoints only. */
605
606#define ALL_TRACEPOINTS(B) \
607 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 608 if (is_tracepoint (B))
1042e4c0 609
7cc221ef 610/* Chains of all breakpoints defined. */
c906108c
SS
611
612struct breakpoint *breakpoint_chain;
613
876fa593
JK
614/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
615
616static struct bp_location **bp_location;
617
618/* Number of elements of BP_LOCATION. */
619
620static unsigned bp_location_count;
621
4a64f543
MS
622/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
623 ADDRESS for the current elements of BP_LOCATION which get a valid
624 result from bp_location_has_shadow. You can use it for roughly
625 limiting the subrange of BP_LOCATION to scan for shadow bytes for
626 an address you need to read. */
876fa593
JK
627
628static CORE_ADDR bp_location_placed_address_before_address_max;
629
4a64f543
MS
630/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
631 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
632 BP_LOCATION which get a valid result from bp_location_has_shadow.
633 You can use it for roughly limiting the subrange of BP_LOCATION to
634 scan for shadow bytes for an address you need to read. */
876fa593
JK
635
636static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 637
4a64f543
MS
638/* The locations that no longer correspond to any breakpoint, unlinked
639 from bp_location array, but for which a hit may still be reported
640 by a target. */
20874c92
VP
641VEC(bp_location_p) *moribund_locations = NULL;
642
c906108c
SS
643/* Number of last breakpoint made. */
644
95a42b64
TT
645static int breakpoint_count;
646
86b17b60
PA
647/* The value of `breakpoint_count' before the last command that
648 created breakpoints. If the last (break-like) command created more
649 than one breakpoint, then the difference between BREAKPOINT_COUNT
650 and PREV_BREAKPOINT_COUNT is more than one. */
651static int prev_breakpoint_count;
c906108c 652
1042e4c0
SS
653/* Number of last tracepoint made. */
654
95a42b64 655static int tracepoint_count;
1042e4c0 656
6149aea9
PA
657static struct cmd_list_element *breakpoint_set_cmdlist;
658static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 659struct cmd_list_element *save_cmdlist;
6149aea9 660
badd37ce
SDJ
661/* See declaration at breakpoint.h. */
662
663struct breakpoint *
664breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
665 void *user_data)
666{
667 struct breakpoint *b = NULL;
668
669 ALL_BREAKPOINTS (b)
670 {
671 if (func (b, user_data) != 0)
672 break;
673 }
674
675 return b;
676}
677
468d015d
JJ
678/* Return whether a breakpoint is an active enabled breakpoint. */
679static int
680breakpoint_enabled (struct breakpoint *b)
681{
0d381245 682 return (b->enable_state == bp_enabled);
468d015d
JJ
683}
684
c906108c
SS
685/* Set breakpoint count to NUM. */
686
95a42b64 687static void
fba45db2 688set_breakpoint_count (int num)
c906108c 689{
86b17b60 690 prev_breakpoint_count = breakpoint_count;
c906108c 691 breakpoint_count = num;
4fa62494 692 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
693}
694
86b17b60
PA
695/* Used by `start_rbreak_breakpoints' below, to record the current
696 breakpoint count before "rbreak" creates any breakpoint. */
697static int rbreak_start_breakpoint_count;
698
95a42b64
TT
699/* Called at the start an "rbreak" command to record the first
700 breakpoint made. */
86b17b60 701
95a42b64
TT
702void
703start_rbreak_breakpoints (void)
704{
86b17b60 705 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
706}
707
708/* Called at the end of an "rbreak" command to record the last
709 breakpoint made. */
86b17b60 710
95a42b64
TT
711void
712end_rbreak_breakpoints (void)
713{
86b17b60 714 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
715}
716
4a64f543 717/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
718
719void
fba45db2 720clear_breakpoint_hit_counts (void)
c906108c
SS
721{
722 struct breakpoint *b;
723
724 ALL_BREAKPOINTS (b)
725 b->hit_count = 0;
726}
727
9add0f1b
TT
728/* Allocate a new counted_command_line with reference count of 1.
729 The new structure owns COMMANDS. */
730
731static struct counted_command_line *
732alloc_counted_command_line (struct command_line *commands)
733{
8d749320 734 struct counted_command_line *result = XNEW (struct counted_command_line);
cc59ec59 735
9add0f1b
TT
736 result->refc = 1;
737 result->commands = commands;
8d749320 738
9add0f1b
TT
739 return result;
740}
741
742/* Increment reference count. This does nothing if CMD is NULL. */
743
744static void
745incref_counted_command_line (struct counted_command_line *cmd)
746{
747 if (cmd)
748 ++cmd->refc;
749}
750
751/* Decrement reference count. If the reference count reaches 0,
752 destroy the counted_command_line. Sets *CMDP to NULL. This does
753 nothing if *CMDP is NULL. */
754
755static void
756decref_counted_command_line (struct counted_command_line **cmdp)
757{
758 if (*cmdp)
759 {
760 if (--(*cmdp)->refc == 0)
761 {
762 free_command_lines (&(*cmdp)->commands);
763 xfree (*cmdp);
764 }
765 *cmdp = NULL;
766 }
767}
768
769/* A cleanup function that calls decref_counted_command_line. */
770
771static void
772do_cleanup_counted_command_line (void *arg)
773{
9a3c8263 774 decref_counted_command_line ((struct counted_command_line **) arg);
9add0f1b
TT
775}
776
777/* Create a cleanup that calls decref_counted_command_line on the
778 argument. */
779
780static struct cleanup *
781make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
782{
783 return make_cleanup (do_cleanup_counted_command_line, cmdp);
784}
785
c906108c 786\f
48cb2d85
VP
787/* Return the breakpoint with the specified number, or NULL
788 if the number does not refer to an existing breakpoint. */
789
790struct breakpoint *
791get_breakpoint (int num)
792{
793 struct breakpoint *b;
794
795 ALL_BREAKPOINTS (b)
796 if (b->number == num)
797 return b;
798
799 return NULL;
800}
5c44784c 801
c906108c 802\f
adc36818 803
b775012e
LM
804/* Mark locations as "conditions have changed" in case the target supports
805 evaluating conditions on its side. */
806
807static void
808mark_breakpoint_modified (struct breakpoint *b)
809{
810 struct bp_location *loc;
811
812 /* This is only meaningful if the target is
813 evaluating conditions and if the user has
814 opted for condition evaluation on the target's
815 side. */
816 if (gdb_evaluates_breakpoint_condition_p ()
817 || !target_supports_evaluation_of_breakpoint_conditions ())
818 return;
819
820 if (!is_breakpoint (b))
821 return;
822
823 for (loc = b->loc; loc; loc = loc->next)
824 loc->condition_changed = condition_modified;
825}
826
827/* Mark location as "conditions have changed" in case the target supports
828 evaluating conditions on its side. */
829
830static void
831mark_breakpoint_location_modified (struct bp_location *loc)
832{
833 /* This is only meaningful if the target is
834 evaluating conditions and if the user has
835 opted for condition evaluation on the target's
836 side. */
837 if (gdb_evaluates_breakpoint_condition_p ()
838 || !target_supports_evaluation_of_breakpoint_conditions ())
839
840 return;
841
842 if (!is_breakpoint (loc->owner))
843 return;
844
845 loc->condition_changed = condition_modified;
846}
847
848/* Sets the condition-evaluation mode using the static global
849 condition_evaluation_mode. */
850
851static void
852set_condition_evaluation_mode (char *args, int from_tty,
853 struct cmd_list_element *c)
854{
b775012e
LM
855 const char *old_mode, *new_mode;
856
857 if ((condition_evaluation_mode_1 == condition_evaluation_target)
858 && !target_supports_evaluation_of_breakpoint_conditions ())
859 {
860 condition_evaluation_mode_1 = condition_evaluation_mode;
861 warning (_("Target does not support breakpoint condition evaluation.\n"
862 "Using host evaluation mode instead."));
863 return;
864 }
865
866 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
867 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
868
abf1152a
JK
869 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
870 settings was "auto". */
871 condition_evaluation_mode = condition_evaluation_mode_1;
872
b775012e
LM
873 /* Only update the mode if the user picked a different one. */
874 if (new_mode != old_mode)
875 {
876 struct bp_location *loc, **loc_tmp;
877 /* If the user switched to a different evaluation mode, we
878 need to synch the changes with the target as follows:
879
880 "host" -> "target": Send all (valid) conditions to the target.
881 "target" -> "host": Remove all the conditions from the target.
882 */
883
b775012e
LM
884 if (new_mode == condition_evaluation_target)
885 {
886 /* Mark everything modified and synch conditions with the
887 target. */
888 ALL_BP_LOCATIONS (loc, loc_tmp)
889 mark_breakpoint_location_modified (loc);
890 }
891 else
892 {
893 /* Manually mark non-duplicate locations to synch conditions
894 with the target. We do this to remove all the conditions the
895 target knows about. */
896 ALL_BP_LOCATIONS (loc, loc_tmp)
897 if (is_breakpoint (loc->owner) && loc->inserted)
898 loc->needs_update = 1;
899 }
900
901 /* Do the update. */
44702360 902 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
903 }
904
905 return;
906}
907
908/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
909 what "auto" is translating to. */
910
911static void
912show_condition_evaluation_mode (struct ui_file *file, int from_tty,
913 struct cmd_list_element *c, const char *value)
914{
915 if (condition_evaluation_mode == condition_evaluation_auto)
916 fprintf_filtered (file,
917 _("Breakpoint condition evaluation "
918 "mode is %s (currently %s).\n"),
919 value,
920 breakpoint_condition_evaluation_mode ());
921 else
922 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
923 value);
924}
925
926/* A comparison function for bp_location AP and BP that is used by
927 bsearch. This comparison function only cares about addresses, unlike
928 the more general bp_location_compare function. */
929
930static int
931bp_location_compare_addrs (const void *ap, const void *bp)
932{
9a3c8263
SM
933 const struct bp_location *a = *(const struct bp_location **) ap;
934 const struct bp_location *b = *(const struct bp_location **) bp;
b775012e
LM
935
936 if (a->address == b->address)
937 return 0;
938 else
939 return ((a->address > b->address) - (a->address < b->address));
940}
941
942/* Helper function to skip all bp_locations with addresses
943 less than ADDRESS. It returns the first bp_location that
944 is greater than or equal to ADDRESS. If none is found, just
945 return NULL. */
946
947static struct bp_location **
948get_first_locp_gte_addr (CORE_ADDR address)
949{
950 struct bp_location dummy_loc;
951 struct bp_location *dummy_locp = &dummy_loc;
952 struct bp_location **locp_found = NULL;
953
954 /* Initialize the dummy location's address field. */
955 memset (&dummy_loc, 0, sizeof (struct bp_location));
956 dummy_loc.address = address;
957
958 /* Find a close match to the first location at ADDRESS. */
9a3c8263
SM
959 locp_found = ((struct bp_location **)
960 bsearch (&dummy_locp, bp_location, bp_location_count,
961 sizeof (struct bp_location **),
962 bp_location_compare_addrs));
b775012e
LM
963
964 /* Nothing was found, nothing left to do. */
965 if (locp_found == NULL)
966 return NULL;
967
968 /* We may have found a location that is at ADDRESS but is not the first in the
969 location's list. Go backwards (if possible) and locate the first one. */
970 while ((locp_found - 1) >= bp_location
971 && (*(locp_found - 1))->address == address)
972 locp_found--;
973
974 return locp_found;
975}
976
adc36818 977void
7a26bd4d 978set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
979 int from_tty)
980{
3a5c3e22
PA
981 xfree (b->cond_string);
982 b->cond_string = NULL;
adc36818 983
3a5c3e22 984 if (is_watchpoint (b))
adc36818 985 {
3a5c3e22
PA
986 struct watchpoint *w = (struct watchpoint *) b;
987
988 xfree (w->cond_exp);
989 w->cond_exp = NULL;
990 }
991 else
992 {
993 struct bp_location *loc;
994
995 for (loc = b->loc; loc; loc = loc->next)
996 {
997 xfree (loc->cond);
998 loc->cond = NULL;
b775012e
LM
999
1000 /* No need to free the condition agent expression
1001 bytecode (if we have one). We will handle this
1002 when we go through update_global_location_list. */
3a5c3e22 1003 }
adc36818 1004 }
adc36818
PM
1005
1006 if (*exp == 0)
1007 {
1008 if (from_tty)
1009 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1010 }
1011 else
1012 {
bbc13ae3 1013 const char *arg = exp;
cc59ec59 1014
adc36818
PM
1015 /* I don't know if it matters whether this is the string the user
1016 typed in or the decompiled expression. */
1017 b->cond_string = xstrdup (arg);
1018 b->condition_not_parsed = 0;
1019
1020 if (is_watchpoint (b))
1021 {
3a5c3e22
PA
1022 struct watchpoint *w = (struct watchpoint *) b;
1023
adc36818
PM
1024 innermost_block = NULL;
1025 arg = exp;
1bb9788d 1026 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
1027 if (*arg)
1028 error (_("Junk at end of expression"));
3a5c3e22 1029 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1030 }
1031 else
1032 {
3a5c3e22
PA
1033 struct bp_location *loc;
1034
adc36818
PM
1035 for (loc = b->loc; loc; loc = loc->next)
1036 {
1037 arg = exp;
1038 loc->cond =
1bb9788d
TT
1039 parse_exp_1 (&arg, loc->address,
1040 block_for_pc (loc->address), 0);
adc36818
PM
1041 if (*arg)
1042 error (_("Junk at end of expression"));
1043 }
1044 }
1045 }
b775012e
LM
1046 mark_breakpoint_modified (b);
1047
8d3788bd 1048 observer_notify_breakpoint_modified (b);
adc36818
PM
1049}
1050
d55637df
TT
1051/* Completion for the "condition" command. */
1052
1053static VEC (char_ptr) *
6f937416
PA
1054condition_completer (struct cmd_list_element *cmd,
1055 const char *text, const char *word)
d55637df 1056{
6f937416 1057 const char *space;
d55637df 1058
6f937416
PA
1059 text = skip_spaces_const (text);
1060 space = skip_to_space_const (text);
d55637df
TT
1061 if (*space == '\0')
1062 {
1063 int len;
1064 struct breakpoint *b;
1065 VEC (char_ptr) *result = NULL;
1066
1067 if (text[0] == '$')
1068 {
1069 /* We don't support completion of history indices. */
1070 if (isdigit (text[1]))
1071 return NULL;
1072 return complete_internalvar (&text[1]);
1073 }
1074
1075 /* We're completing the breakpoint number. */
1076 len = strlen (text);
1077
1078 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1079 {
1080 char number[50];
1081
1082 xsnprintf (number, sizeof (number), "%d", b->number);
1083
1084 if (strncmp (number, text, len) == 0)
1085 VEC_safe_push (char_ptr, result, xstrdup (number));
1086 }
d55637df
TT
1087
1088 return result;
1089 }
1090
1091 /* We're completing the expression part. */
6f937416 1092 text = skip_spaces_const (space);
d55637df
TT
1093 return expression_completer (cmd, text, word);
1094}
1095
c906108c
SS
1096/* condition N EXP -- set break condition of breakpoint N to EXP. */
1097
1098static void
fba45db2 1099condition_command (char *arg, int from_tty)
c906108c 1100{
52f0bd74 1101 struct breakpoint *b;
c906108c 1102 char *p;
52f0bd74 1103 int bnum;
c906108c
SS
1104
1105 if (arg == 0)
e2e0b3e5 1106 error_no_arg (_("breakpoint number"));
c906108c
SS
1107
1108 p = arg;
1109 bnum = get_number (&p);
5c44784c 1110 if (bnum == 0)
8a3fe4f8 1111 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1112
1113 ALL_BREAKPOINTS (b)
1114 if (b->number == bnum)
2f069f6f 1115 {
6dddc817
DE
1116 /* Check if this breakpoint has a "stop" method implemented in an
1117 extension language. This method and conditions entered into GDB
1118 from the CLI are mutually exclusive. */
1119 const struct extension_language_defn *extlang
1120 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1121
1122 if (extlang != NULL)
1123 {
1124 error (_("Only one stop condition allowed. There is currently"
1125 " a %s stop condition defined for this breakpoint."),
1126 ext_lang_capitalized_name (extlang));
1127 }
2566ad2d 1128 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1129
1130 if (is_breakpoint (b))
44702360 1131 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1132
2f069f6f
JB
1133 return;
1134 }
c906108c 1135
8a3fe4f8 1136 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1137}
1138
a7bdde9e
VP
1139/* Check that COMMAND do not contain commands that are suitable
1140 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1141 Throw if any such commands is found. */
1142
a7bdde9e
VP
1143static void
1144check_no_tracepoint_commands (struct command_line *commands)
1145{
1146 struct command_line *c;
cc59ec59 1147
a7bdde9e
VP
1148 for (c = commands; c; c = c->next)
1149 {
1150 int i;
1151
1152 if (c->control_type == while_stepping_control)
3e43a32a
MS
1153 error (_("The 'while-stepping' command can "
1154 "only be used for tracepoints"));
a7bdde9e
VP
1155
1156 for (i = 0; i < c->body_count; ++i)
1157 check_no_tracepoint_commands ((c->body_list)[i]);
1158
1159 /* Not that command parsing removes leading whitespace and comment
4a64f543 1160 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1161 command directly. */
1162 if (strstr (c->line, "collect ") == c->line)
1163 error (_("The 'collect' command can only be used for tracepoints"));
1164
51661e93
VP
1165 if (strstr (c->line, "teval ") == c->line)
1166 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1167 }
1168}
1169
d77f58be
SS
1170/* Encapsulate tests for different types of tracepoints. */
1171
d9b3f62e
PA
1172static int
1173is_tracepoint_type (enum bptype type)
1174{
1175 return (type == bp_tracepoint
1176 || type == bp_fast_tracepoint
1177 || type == bp_static_tracepoint);
1178}
1179
a7bdde9e 1180int
d77f58be 1181is_tracepoint (const struct breakpoint *b)
a7bdde9e 1182{
d9b3f62e 1183 return is_tracepoint_type (b->type);
a7bdde9e 1184}
d9b3f62e 1185
e5dd4106 1186/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1187 breakpoint. This function will throw an exception if a problem is
1188 found. */
48cb2d85 1189
95a42b64
TT
1190static void
1191validate_commands_for_breakpoint (struct breakpoint *b,
1192 struct command_line *commands)
48cb2d85 1193{
d77f58be 1194 if (is_tracepoint (b))
a7bdde9e 1195 {
c9a6ce02 1196 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1197 struct command_line *c;
1198 struct command_line *while_stepping = 0;
c9a6ce02
PA
1199
1200 /* Reset the while-stepping step count. The previous commands
1201 might have included a while-stepping action, while the new
1202 ones might not. */
1203 t->step_count = 0;
1204
1205 /* We need to verify that each top-level element of commands is
1206 valid for tracepoints, that there's at most one
1207 while-stepping element, and that the while-stepping's body
1208 has valid tracing commands excluding nested while-stepping.
1209 We also need to validate the tracepoint action line in the
1210 context of the tracepoint --- validate_actionline actually
1211 has side effects, like setting the tracepoint's
1212 while-stepping STEP_COUNT, in addition to checking if the
1213 collect/teval actions parse and make sense in the
1214 tracepoint's context. */
a7bdde9e
VP
1215 for (c = commands; c; c = c->next)
1216 {
a7bdde9e
VP
1217 if (c->control_type == while_stepping_control)
1218 {
1219 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1220 error (_("The 'while-stepping' command "
1221 "cannot be used for fast tracepoint"));
0fb4aa4b 1222 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1223 error (_("The 'while-stepping' command "
1224 "cannot be used for static tracepoint"));
a7bdde9e
VP
1225
1226 if (while_stepping)
3e43a32a
MS
1227 error (_("The 'while-stepping' command "
1228 "can be used only once"));
a7bdde9e
VP
1229 else
1230 while_stepping = c;
1231 }
c9a6ce02
PA
1232
1233 validate_actionline (c->line, b);
a7bdde9e
VP
1234 }
1235 if (while_stepping)
1236 {
1237 struct command_line *c2;
1238
1239 gdb_assert (while_stepping->body_count == 1);
1240 c2 = while_stepping->body_list[0];
1241 for (; c2; c2 = c2->next)
1242 {
a7bdde9e
VP
1243 if (c2->control_type == while_stepping_control)
1244 error (_("The 'while-stepping' command cannot be nested"));
1245 }
1246 }
1247 }
1248 else
1249 {
1250 check_no_tracepoint_commands (commands);
1251 }
95a42b64
TT
1252}
1253
0fb4aa4b
PA
1254/* Return a vector of all the static tracepoints set at ADDR. The
1255 caller is responsible for releasing the vector. */
1256
1257VEC(breakpoint_p) *
1258static_tracepoints_here (CORE_ADDR addr)
1259{
1260 struct breakpoint *b;
1261 VEC(breakpoint_p) *found = 0;
1262 struct bp_location *loc;
1263
1264 ALL_BREAKPOINTS (b)
1265 if (b->type == bp_static_tracepoint)
1266 {
1267 for (loc = b->loc; loc; loc = loc->next)
1268 if (loc->address == addr)
1269 VEC_safe_push(breakpoint_p, found, b);
1270 }
1271
1272 return found;
1273}
1274
95a42b64 1275/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1276 validate that only allowed commands are included. */
95a42b64
TT
1277
1278void
4a64f543
MS
1279breakpoint_set_commands (struct breakpoint *b,
1280 struct command_line *commands)
95a42b64
TT
1281{
1282 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1283
9add0f1b
TT
1284 decref_counted_command_line (&b->commands);
1285 b->commands = alloc_counted_command_line (commands);
8d3788bd 1286 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1287}
1288
45a43567
TT
1289/* Set the internal `silent' flag on the breakpoint. Note that this
1290 is not the same as the "silent" that may appear in the breakpoint's
1291 commands. */
1292
1293void
1294breakpoint_set_silent (struct breakpoint *b, int silent)
1295{
1296 int old_silent = b->silent;
1297
1298 b->silent = silent;
1299 if (old_silent != silent)
8d3788bd 1300 observer_notify_breakpoint_modified (b);
45a43567
TT
1301}
1302
1303/* Set the thread for this breakpoint. If THREAD is -1, make the
1304 breakpoint work for any thread. */
1305
1306void
1307breakpoint_set_thread (struct breakpoint *b, int thread)
1308{
1309 int old_thread = b->thread;
1310
1311 b->thread = thread;
1312 if (old_thread != thread)
8d3788bd 1313 observer_notify_breakpoint_modified (b);
45a43567
TT
1314}
1315
1316/* Set the task for this breakpoint. If TASK is 0, make the
1317 breakpoint work for any task. */
1318
1319void
1320breakpoint_set_task (struct breakpoint *b, int task)
1321{
1322 int old_task = b->task;
1323
1324 b->task = task;
1325 if (old_task != task)
8d3788bd 1326 observer_notify_breakpoint_modified (b);
45a43567
TT
1327}
1328
95a42b64
TT
1329void
1330check_tracepoint_command (char *line, void *closure)
a7bdde9e 1331{
9a3c8263 1332 struct breakpoint *b = (struct breakpoint *) closure;
cc59ec59 1333
6f937416 1334 validate_actionline (line, b);
a7bdde9e
VP
1335}
1336
95a42b64
TT
1337/* A structure used to pass information through
1338 map_breakpoint_numbers. */
1339
1340struct commands_info
1341{
1342 /* True if the command was typed at a tty. */
1343 int from_tty;
86b17b60
PA
1344
1345 /* The breakpoint range spec. */
1346 char *arg;
1347
95a42b64
TT
1348 /* Non-NULL if the body of the commands are being read from this
1349 already-parsed command. */
1350 struct command_line *control;
86b17b60 1351
95a42b64
TT
1352 /* The command lines read from the user, or NULL if they have not
1353 yet been read. */
1354 struct counted_command_line *cmd;
1355};
1356
1357/* A callback for map_breakpoint_numbers that sets the commands for
1358 commands_command. */
1359
c906108c 1360static void
95a42b64 1361do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1362{
9a3c8263 1363 struct commands_info *info = (struct commands_info *) data;
c906108c 1364
95a42b64
TT
1365 if (info->cmd == NULL)
1366 {
1367 struct command_line *l;
5c44784c 1368
95a42b64
TT
1369 if (info->control != NULL)
1370 l = copy_command_lines (info->control->body_list[0]);
1371 else
86b17b60
PA
1372 {
1373 struct cleanup *old_chain;
1374 char *str;
c5aa993b 1375
3e43a32a
MS
1376 str = xstrprintf (_("Type commands for breakpoint(s) "
1377 "%s, one per line."),
86b17b60
PA
1378 info->arg);
1379
1380 old_chain = make_cleanup (xfree, str);
1381
1382 l = read_command_lines (str,
1383 info->from_tty, 1,
d77f58be 1384 (is_tracepoint (b)
86b17b60
PA
1385 ? check_tracepoint_command : 0),
1386 b);
1387
1388 do_cleanups (old_chain);
1389 }
a7bdde9e 1390
95a42b64
TT
1391 info->cmd = alloc_counted_command_line (l);
1392 }
1393
1394 /* If a breakpoint was on the list more than once, we don't need to
1395 do anything. */
1396 if (b->commands != info->cmd)
1397 {
1398 validate_commands_for_breakpoint (b, info->cmd->commands);
1399 incref_counted_command_line (info->cmd);
1400 decref_counted_command_line (&b->commands);
1401 b->commands = info->cmd;
8d3788bd 1402 observer_notify_breakpoint_modified (b);
c5aa993b 1403 }
95a42b64
TT
1404}
1405
1406static void
4a64f543
MS
1407commands_command_1 (char *arg, int from_tty,
1408 struct command_line *control)
95a42b64
TT
1409{
1410 struct cleanup *cleanups;
1411 struct commands_info info;
1412
1413 info.from_tty = from_tty;
1414 info.control = control;
1415 info.cmd = NULL;
1416 /* If we read command lines from the user, then `info' will hold an
1417 extra reference to the commands that we must clean up. */
1418 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1419
1420 if (arg == NULL || !*arg)
1421 {
86b17b60 1422 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1423 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1424 breakpoint_count);
95a42b64
TT
1425 else if (breakpoint_count > 0)
1426 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1427 else
1428 {
1429 /* So that we don't try to free the incoming non-NULL
1430 argument in the cleanup below. Mapping breakpoint
1431 numbers will fail in this case. */
1432 arg = NULL;
1433 }
95a42b64 1434 }
9766ced4
SS
1435 else
1436 /* The command loop has some static state, so we need to preserve
1437 our argument. */
1438 arg = xstrdup (arg);
86b17b60
PA
1439
1440 if (arg != NULL)
1441 make_cleanup (xfree, arg);
1442
1443 info.arg = arg;
95a42b64
TT
1444
1445 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1446
1447 if (info.cmd == NULL)
1448 error (_("No breakpoints specified."));
1449
1450 do_cleanups (cleanups);
1451}
1452
1453static void
1454commands_command (char *arg, int from_tty)
1455{
1456 commands_command_1 (arg, from_tty, NULL);
c906108c 1457}
40c03ae8
EZ
1458
1459/* Like commands_command, but instead of reading the commands from
1460 input stream, takes them from an already parsed command structure.
1461
1462 This is used by cli-script.c to DTRT with breakpoint commands
1463 that are part of if and while bodies. */
1464enum command_control_type
1465commands_from_control_command (char *arg, struct command_line *cmd)
1466{
95a42b64
TT
1467 commands_command_1 (arg, 0, cmd);
1468 return simple_control;
40c03ae8 1469}
876fa593
JK
1470
1471/* Return non-zero if BL->TARGET_INFO contains valid information. */
1472
1473static int
1474bp_location_has_shadow (struct bp_location *bl)
1475{
1476 if (bl->loc_type != bp_loc_software_breakpoint)
1477 return 0;
1478 if (!bl->inserted)
1479 return 0;
1480 if (bl->target_info.shadow_len == 0)
e5dd4106 1481 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1482 return 0;
1483 return 1;
1484}
1485
9d497a19
PA
1486/* Update BUF, which is LEN bytes read from the target address
1487 MEMADDR, by replacing a memory breakpoint with its shadowed
1488 contents.
1489
1490 If READBUF is not NULL, this buffer must not overlap with the of
1491 the breakpoint location's shadow_contents buffer. Otherwise, a
1492 failed assertion internal error will be raised. */
1493
1494static void
1495one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1496 const gdb_byte *writebuf_org,
1497 ULONGEST memaddr, LONGEST len,
1498 struct bp_target_info *target_info,
1499 struct gdbarch *gdbarch)
1500{
1501 /* Now do full processing of the found relevant range of elements. */
1502 CORE_ADDR bp_addr = 0;
1503 int bp_size = 0;
1504 int bptoffset = 0;
1505
1506 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1507 current_program_space->aspace, 0))
1508 {
1509 /* The breakpoint is inserted in a different address space. */
1510 return;
1511 }
1512
1513 /* Addresses and length of the part of the breakpoint that
1514 we need to copy. */
1515 bp_addr = target_info->placed_address;
1516 bp_size = target_info->shadow_len;
1517
1518 if (bp_addr + bp_size <= memaddr)
1519 {
1520 /* The breakpoint is entirely before the chunk of memory we are
1521 reading. */
1522 return;
1523 }
1524
1525 if (bp_addr >= memaddr + len)
1526 {
1527 /* The breakpoint is entirely after the chunk of memory we are
1528 reading. */
1529 return;
1530 }
1531
1532 /* Offset within shadow_contents. */
1533 if (bp_addr < memaddr)
1534 {
1535 /* Only copy the second part of the breakpoint. */
1536 bp_size -= memaddr - bp_addr;
1537 bptoffset = memaddr - bp_addr;
1538 bp_addr = memaddr;
1539 }
1540
1541 if (bp_addr + bp_size > memaddr + len)
1542 {
1543 /* Only copy the first part of the breakpoint. */
1544 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1545 }
1546
1547 if (readbuf != NULL)
1548 {
1549 /* Verify that the readbuf buffer does not overlap with the
1550 shadow_contents buffer. */
1551 gdb_assert (target_info->shadow_contents >= readbuf + len
1552 || readbuf >= (target_info->shadow_contents
1553 + target_info->shadow_len));
1554
1555 /* Update the read buffer with this inserted breakpoint's
1556 shadow. */
1557 memcpy (readbuf + bp_addr - memaddr,
1558 target_info->shadow_contents + bptoffset, bp_size);
1559 }
1560 else
1561 {
1562 const unsigned char *bp;
0d5ed153
MR
1563 CORE_ADDR addr = target_info->reqstd_address;
1564 int placed_size;
9d497a19
PA
1565
1566 /* Update the shadow with what we want to write to memory. */
1567 memcpy (target_info->shadow_contents + bptoffset,
1568 writebuf_org + bp_addr - memaddr, bp_size);
1569
1570 /* Determine appropriate breakpoint contents and size for this
1571 address. */
0d5ed153 1572 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1573
1574 /* Update the final write buffer with this inserted
1575 breakpoint's INSN. */
1576 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1577 }
1578}
1579
8defab1a 1580/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1581 by replacing any memory breakpoints with their shadowed contents.
1582
35c63cd8
JB
1583 If READBUF is not NULL, this buffer must not overlap with any of
1584 the breakpoint location's shadow_contents buffers. Otherwise,
1585 a failed assertion internal error will be raised.
1586
876fa593 1587 The range of shadowed area by each bp_location is:
35df4500
TJB
1588 bl->address - bp_location_placed_address_before_address_max
1589 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1590 The range we were requested to resolve shadows for is:
1591 memaddr ... memaddr + len
1592 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1593 memaddr + len <= (bl->address
1594 - bp_location_placed_address_before_address_max)
876fa593 1595 and:
35df4500 1596 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1597
8defab1a 1598void
f0ba3972
PA
1599breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1600 const gdb_byte *writebuf_org,
1601 ULONGEST memaddr, LONGEST len)
c906108c 1602{
4a64f543
MS
1603 /* Left boundary, right boundary and median element of our binary
1604 search. */
876fa593 1605 unsigned bc_l, bc_r, bc;
9d497a19 1606 size_t i;
876fa593 1607
4a64f543
MS
1608 /* Find BC_L which is a leftmost element which may affect BUF
1609 content. It is safe to report lower value but a failure to
1610 report higher one. */
876fa593
JK
1611
1612 bc_l = 0;
1613 bc_r = bp_location_count;
1614 while (bc_l + 1 < bc_r)
1615 {
35df4500 1616 struct bp_location *bl;
876fa593
JK
1617
1618 bc = (bc_l + bc_r) / 2;
35df4500 1619 bl = bp_location[bc];
876fa593 1620
4a64f543
MS
1621 /* Check first BL->ADDRESS will not overflow due to the added
1622 constant. Then advance the left boundary only if we are sure
1623 the BC element can in no way affect the BUF content (MEMADDR
1624 to MEMADDR + LEN range).
876fa593 1625
4a64f543
MS
1626 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1627 offset so that we cannot miss a breakpoint with its shadow
1628 range tail still reaching MEMADDR. */
c5aa993b 1629
35df4500
TJB
1630 if ((bl->address + bp_location_shadow_len_after_address_max
1631 >= bl->address)
1632 && (bl->address + bp_location_shadow_len_after_address_max
1633 <= memaddr))
876fa593
JK
1634 bc_l = bc;
1635 else
1636 bc_r = bc;
1637 }
1638
128070bb
PA
1639 /* Due to the binary search above, we need to make sure we pick the
1640 first location that's at BC_L's address. E.g., if there are
1641 multiple locations at the same address, BC_L may end up pointing
1642 at a duplicate location, and miss the "master"/"inserted"
1643 location. Say, given locations L1, L2 and L3 at addresses A and
1644 B:
1645
1646 L1@A, L2@A, L3@B, ...
1647
1648 BC_L could end up pointing at location L2, while the "master"
1649 location could be L1. Since the `loc->inserted' flag is only set
1650 on "master" locations, we'd forget to restore the shadow of L1
1651 and L2. */
1652 while (bc_l > 0
1653 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1654 bc_l--;
1655
876fa593
JK
1656 /* Now do full processing of the found relevant range of elements. */
1657
1658 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1659 {
35df4500 1660 struct bp_location *bl = bp_location[bc];
876fa593
JK
1661 CORE_ADDR bp_addr = 0;
1662 int bp_size = 0;
1663 int bptoffset = 0;
1664
35df4500
TJB
1665 /* bp_location array has BL->OWNER always non-NULL. */
1666 if (bl->owner->type == bp_none)
8a3fe4f8 1667 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1668 bl->owner->number);
ffce0d52 1669
e5dd4106 1670 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1671 content. */
1672
35df4500
TJB
1673 if (bl->address >= bp_location_placed_address_before_address_max
1674 && memaddr + len <= (bl->address
1675 - bp_location_placed_address_before_address_max))
876fa593
JK
1676 break;
1677
35df4500 1678 if (!bp_location_has_shadow (bl))
c5aa993b 1679 continue;
6c95b8df 1680
9d497a19
PA
1681 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1682 memaddr, len, &bl->target_info, bl->gdbarch);
1683 }
c906108c 1684}
9d497a19 1685
c906108c 1686\f
c5aa993b 1687
b775012e
LM
1688/* Return true if BPT is either a software breakpoint or a hardware
1689 breakpoint. */
1690
1691int
1692is_breakpoint (const struct breakpoint *bpt)
1693{
1694 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1695 || bpt->type == bp_hardware_breakpoint
1696 || bpt->type == bp_dprintf);
b775012e
LM
1697}
1698
60e1c644
PA
1699/* Return true if BPT is of any hardware watchpoint kind. */
1700
a5606eee 1701static int
d77f58be 1702is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1703{
1704 return (bpt->type == bp_hardware_watchpoint
1705 || bpt->type == bp_read_watchpoint
1706 || bpt->type == bp_access_watchpoint);
1707}
7270d8f2 1708
60e1c644
PA
1709/* Return true if BPT is of any watchpoint kind, hardware or
1710 software. */
1711
3a5c3e22 1712int
d77f58be 1713is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1714{
1715 return (is_hardware_watchpoint (bpt)
1716 || bpt->type == bp_watchpoint);
1717}
1718
3a5c3e22
PA
1719/* Returns true if the current thread and its running state are safe
1720 to evaluate or update watchpoint B. Watchpoints on local
1721 expressions need to be evaluated in the context of the thread that
1722 was current when the watchpoint was created, and, that thread needs
1723 to be stopped to be able to select the correct frame context.
1724 Watchpoints on global expressions can be evaluated on any thread,
1725 and in any state. It is presently left to the target allowing
1726 memory accesses when threads are running. */
f6bc2008
PA
1727
1728static int
3a5c3e22 1729watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1730{
d0d8b0c6
JK
1731 return (b->base.pspace == current_program_space
1732 && (ptid_equal (b->watchpoint_thread, null_ptid)
1733 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1734 && !is_executing (inferior_ptid))));
f6bc2008
PA
1735}
1736
d0fb5eae
JK
1737/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1738 associated bp_watchpoint_scope breakpoint. */
1739
1740static void
3a5c3e22 1741watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1742{
3a5c3e22 1743 struct breakpoint *b = &w->base;
d0fb5eae
JK
1744
1745 if (b->related_breakpoint != b)
1746 {
1747 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1748 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1749 b->related_breakpoint->disposition = disp_del_at_next_stop;
1750 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1751 b->related_breakpoint = b;
1752 }
1753 b->disposition = disp_del_at_next_stop;
1754}
1755
bb9d5f81
PP
1756/* Extract a bitfield value from value VAL using the bit parameters contained in
1757 watchpoint W. */
1758
1759static struct value *
1760extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1761{
1762 struct value *bit_val;
1763
1764 if (val == NULL)
1765 return NULL;
1766
1767 bit_val = allocate_value (value_type (val));
1768
1769 unpack_value_bitfield (bit_val,
1770 w->val_bitpos,
1771 w->val_bitsize,
1772 value_contents_for_printing (val),
1773 value_offset (val),
1774 val);
1775
1776 return bit_val;
1777}
1778
c6d81124
PA
1779/* Allocate a dummy location and add it to B, which must be a software
1780 watchpoint. This is required because even if a software watchpoint
1781 is not watching any memory, bpstat_stop_status requires a location
1782 to be able to report stops. */
1783
1784static void
1785software_watchpoint_add_no_memory_location (struct breakpoint *b,
1786 struct program_space *pspace)
1787{
1788 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1789
1790 b->loc = allocate_bp_location (b);
1791 b->loc->pspace = pspace;
1792 b->loc->address = -1;
1793 b->loc->length = -1;
1794}
1795
1796/* Returns true if B is a software watchpoint that is not watching any
1797 memory (e.g., "watch $pc"). */
1798
1799static int
1800is_no_memory_software_watchpoint (struct breakpoint *b)
1801{
1802 return (b->type == bp_watchpoint
1803 && b->loc != NULL
1804 && b->loc->next == NULL
1805 && b->loc->address == -1
1806 && b->loc->length == -1);
1807}
1808
567e1b4e
JB
1809/* Assuming that B is a watchpoint:
1810 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1811 - Evaluate expression and store the result in B->val
567e1b4e
JB
1812 - Evaluate the condition if there is one, and store the result
1813 in b->loc->cond.
a5606eee
VP
1814 - Update the list of values that must be watched in B->loc.
1815
4a64f543
MS
1816 If the watchpoint disposition is disp_del_at_next_stop, then do
1817 nothing. If this is local watchpoint that is out of scope, delete
1818 it.
1819
1820 Even with `set breakpoint always-inserted on' the watchpoints are
1821 removed + inserted on each stop here. Normal breakpoints must
1822 never be removed because they might be missed by a running thread
1823 when debugging in non-stop mode. On the other hand, hardware
1824 watchpoints (is_hardware_watchpoint; processed here) are specific
1825 to each LWP since they are stored in each LWP's hardware debug
1826 registers. Therefore, such LWP must be stopped first in order to
1827 be able to modify its hardware watchpoints.
1828
1829 Hardware watchpoints must be reset exactly once after being
1830 presented to the user. It cannot be done sooner, because it would
1831 reset the data used to present the watchpoint hit to the user. And
1832 it must not be done later because it could display the same single
1833 watchpoint hit during multiple GDB stops. Note that the latter is
1834 relevant only to the hardware watchpoint types bp_read_watchpoint
1835 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1836 not user-visible - its hit is suppressed if the memory content has
1837 not changed.
1838
1839 The following constraints influence the location where we can reset
1840 hardware watchpoints:
1841
1842 * target_stopped_by_watchpoint and target_stopped_data_address are
1843 called several times when GDB stops.
1844
1845 [linux]
1846 * Multiple hardware watchpoints can be hit at the same time,
1847 causing GDB to stop. GDB only presents one hardware watchpoint
1848 hit at a time as the reason for stopping, and all the other hits
1849 are presented later, one after the other, each time the user
1850 requests the execution to be resumed. Execution is not resumed
1851 for the threads still having pending hit event stored in
1852 LWP_INFO->STATUS. While the watchpoint is already removed from
1853 the inferior on the first stop the thread hit event is kept being
1854 reported from its cached value by linux_nat_stopped_data_address
1855 until the real thread resume happens after the watchpoint gets
1856 presented and thus its LWP_INFO->STATUS gets reset.
1857
1858 Therefore the hardware watchpoint hit can get safely reset on the
1859 watchpoint removal from inferior. */
a79d3c27 1860
b40ce68a 1861static void
3a5c3e22 1862update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1863{
a5606eee 1864 int within_current_scope;
a5606eee 1865 struct frame_id saved_frame_id;
66076460 1866 int frame_saved;
a5606eee 1867
f6bc2008
PA
1868 /* If this is a local watchpoint, we only want to check if the
1869 watchpoint frame is in scope if the current thread is the thread
1870 that was used to create the watchpoint. */
1871 if (!watchpoint_in_thread_scope (b))
1872 return;
1873
3a5c3e22 1874 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1875 return;
1876
66076460 1877 frame_saved = 0;
a5606eee
VP
1878
1879 /* Determine if the watchpoint is within scope. */
1880 if (b->exp_valid_block == NULL)
1881 within_current_scope = 1;
1882 else
1883 {
b5db5dfc
UW
1884 struct frame_info *fi = get_current_frame ();
1885 struct gdbarch *frame_arch = get_frame_arch (fi);
1886 CORE_ADDR frame_pc = get_frame_pc (fi);
1887
c9cf6e20
MG
1888 /* If we're at a point where the stack has been destroyed
1889 (e.g. in a function epilogue), unwinding may not work
1890 properly. Do not attempt to recreate locations at this
b5db5dfc 1891 point. See similar comments in watchpoint_check. */
c9cf6e20 1892 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1893 return;
66076460
DJ
1894
1895 /* Save the current frame's ID so we can restore it after
1896 evaluating the watchpoint expression on its own frame. */
1897 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1898 took a frame parameter, so that we didn't have to change the
1899 selected frame. */
1900 frame_saved = 1;
1901 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1902
a5606eee
VP
1903 fi = frame_find_by_id (b->watchpoint_frame);
1904 within_current_scope = (fi != NULL);
1905 if (within_current_scope)
1906 select_frame (fi);
1907 }
1908
b5db5dfc
UW
1909 /* We don't free locations. They are stored in the bp_location array
1910 and update_global_location_list will eventually delete them and
1911 remove breakpoints if needed. */
3a5c3e22 1912 b->base.loc = NULL;
b5db5dfc 1913
a5606eee
VP
1914 if (within_current_scope && reparse)
1915 {
bbc13ae3 1916 const char *s;
d63d0675 1917
a5606eee
VP
1918 if (b->exp)
1919 {
1920 xfree (b->exp);
1921 b->exp = NULL;
1922 }
d63d0675 1923 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1924 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1925 /* If the meaning of expression itself changed, the old value is
1926 no longer relevant. We don't want to report a watchpoint hit
1927 to the user when the old value and the new value may actually
1928 be completely different objects. */
1929 value_free (b->val);
fa4727a6
DJ
1930 b->val = NULL;
1931 b->val_valid = 0;
60e1c644
PA
1932
1933 /* Note that unlike with breakpoints, the watchpoint's condition
1934 expression is stored in the breakpoint object, not in the
1935 locations (re)created below. */
3a5c3e22 1936 if (b->base.cond_string != NULL)
60e1c644
PA
1937 {
1938 if (b->cond_exp != NULL)
1939 {
1940 xfree (b->cond_exp);
1941 b->cond_exp = NULL;
1942 }
1943
3a5c3e22 1944 s = b->base.cond_string;
1bb9788d 1945 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1946 }
a5606eee 1947 }
a5606eee
VP
1948
1949 /* If we failed to parse the expression, for example because
1950 it refers to a global variable in a not-yet-loaded shared library,
1951 don't try to insert watchpoint. We don't automatically delete
1952 such watchpoint, though, since failure to parse expression
1953 is different from out-of-scope watchpoint. */
e8369a73 1954 if (!target_has_execution)
2d134ed3
PA
1955 {
1956 /* Without execution, memory can't change. No use to try and
1957 set watchpoint locations. The watchpoint will be reset when
1958 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1959 if (!can_use_hw_watchpoints)
1960 {
1961 if (b->base.ops->works_in_software_mode (&b->base))
1962 b->base.type = bp_watchpoint;
1963 else
638aa5a1
AB
1964 error (_("Can't set read/access watchpoint when "
1965 "hardware watchpoints are disabled."));
e8369a73 1966 }
2d134ed3
PA
1967 }
1968 else if (within_current_scope && b->exp)
a5606eee 1969 {
0cf6dd15 1970 int pc = 0;
fa4727a6 1971 struct value *val_chain, *v, *result, *next;
2d134ed3 1972 struct program_space *frame_pspace;
a5606eee 1973
3a1115a0 1974 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1975
a5606eee
VP
1976 /* Avoid setting b->val if it's already set. The meaning of
1977 b->val is 'the last value' user saw, and we should update
1978 it only if we reported that last value to user. As it
9c06b0b4
TJB
1979 happens, the code that reports it updates b->val directly.
1980 We don't keep track of the memory value for masked
1981 watchpoints. */
3a5c3e22 1982 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1983 {
bb9d5f81
PP
1984 if (b->val_bitsize != 0)
1985 {
1986 v = extract_bitfield_from_watchpoint_value (b, v);
1987 if (v != NULL)
1988 release_value (v);
1989 }
fa4727a6
DJ
1990 b->val = v;
1991 b->val_valid = 1;
1992 }
a5606eee 1993
2d134ed3
PA
1994 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1995
a5606eee 1996 /* Look at each value on the value chain. */
9fa40276 1997 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1998 {
1999 /* If it's a memory location, and GDB actually needed
2000 its contents to evaluate the expression, then we
fa4727a6
DJ
2001 must watch it. If the first value returned is
2002 still lazy, that means an error occurred reading it;
2003 watch it anyway in case it becomes readable. */
a5606eee 2004 if (VALUE_LVAL (v) == lval_memory
fa4727a6 2005 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
2006 {
2007 struct type *vtype = check_typedef (value_type (v));
7270d8f2 2008
a5606eee
VP
2009 /* We only watch structs and arrays if user asked
2010 for it explicitly, never if they just happen to
2011 appear in the middle of some value chain. */
fa4727a6 2012 if (v == result
a5606eee
VP
2013 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2014 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2015 {
2016 CORE_ADDR addr;
f486487f 2017 enum target_hw_bp_type type;
a5606eee 2018 struct bp_location *loc, **tmp;
bb9d5f81
PP
2019 int bitpos = 0, bitsize = 0;
2020
2021 if (value_bitsize (v) != 0)
2022 {
2023 /* Extract the bit parameters out from the bitfield
2024 sub-expression. */
2025 bitpos = value_bitpos (v);
2026 bitsize = value_bitsize (v);
2027 }
2028 else if (v == result && b->val_bitsize != 0)
2029 {
2030 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2031 lvalue whose bit parameters are saved in the fields
2032 VAL_BITPOS and VAL_BITSIZE. */
2033 bitpos = b->val_bitpos;
2034 bitsize = b->val_bitsize;
2035 }
a5606eee 2036
42ae5230 2037 addr = value_address (v);
bb9d5f81
PP
2038 if (bitsize != 0)
2039 {
2040 /* Skip the bytes that don't contain the bitfield. */
2041 addr += bitpos / 8;
2042 }
2043
a5606eee 2044 type = hw_write;
3a5c3e22 2045 if (b->base.type == bp_read_watchpoint)
a5606eee 2046 type = hw_read;
3a5c3e22 2047 else if (b->base.type == bp_access_watchpoint)
a5606eee 2048 type = hw_access;
3a5c3e22
PA
2049
2050 loc = allocate_bp_location (&b->base);
2051 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2052 ;
2053 *tmp = loc;
a6d9a66e 2054 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2055
2056 loc->pspace = frame_pspace;
a5606eee 2057 loc->address = addr;
bb9d5f81
PP
2058
2059 if (bitsize != 0)
2060 {
2061 /* Just cover the bytes that make up the bitfield. */
2062 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2063 }
2064 else
2065 loc->length = TYPE_LENGTH (value_type (v));
2066
a5606eee
VP
2067 loc->watchpoint_type = type;
2068 }
2069 }
9fa40276
TJB
2070 }
2071
2072 /* Change the type of breakpoint between hardware assisted or
2073 an ordinary watchpoint depending on the hardware support
2074 and free hardware slots. REPARSE is set when the inferior
2075 is started. */
a9634178 2076 if (reparse)
9fa40276 2077 {
e09342b5 2078 int reg_cnt;
9fa40276
TJB
2079 enum bp_loc_type loc_type;
2080 struct bp_location *bl;
a5606eee 2081
a9634178 2082 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2083
2084 if (reg_cnt)
9fa40276
TJB
2085 {
2086 int i, target_resources_ok, other_type_used;
a1398e0c 2087 enum bptype type;
9fa40276 2088
a9634178
TJB
2089 /* Use an exact watchpoint when there's only one memory region to be
2090 watched, and only one debug register is needed to watch it. */
2091 b->exact = target_exact_watchpoints && reg_cnt == 1;
2092
9fa40276 2093 /* We need to determine how many resources are already
e09342b5
TJB
2094 used for all other hardware watchpoints plus this one
2095 to see if we still have enough resources to also fit
a1398e0c
PA
2096 this watchpoint in as well. */
2097
2098 /* If this is a software watchpoint, we try to turn it
2099 to a hardware one -- count resources as if B was of
2100 hardware watchpoint type. */
2101 type = b->base.type;
2102 if (type == bp_watchpoint)
2103 type = bp_hardware_watchpoint;
2104
2105 /* This watchpoint may or may not have been placed on
2106 the list yet at this point (it won't be in the list
2107 if we're trying to create it for the first time,
2108 through watch_command), so always account for it
2109 manually. */
2110
2111 /* Count resources used by all watchpoints except B. */
2112 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2113
2114 /* Add in the resources needed for B. */
2115 i += hw_watchpoint_use_count (&b->base);
2116
2117 target_resources_ok
2118 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2119 if (target_resources_ok <= 0)
a9634178 2120 {
3a5c3e22 2121 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2122
2123 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2124 error (_("Target does not support this type of "
2125 "hardware watchpoint."));
9c06b0b4
TJB
2126 else if (target_resources_ok < 0 && !sw_mode)
2127 error (_("There are not enough available hardware "
2128 "resources for this watchpoint."));
a1398e0c
PA
2129
2130 /* Downgrade to software watchpoint. */
2131 b->base.type = bp_watchpoint;
2132 }
2133 else
2134 {
2135 /* If this was a software watchpoint, we've just
2136 found we have enough resources to turn it to a
2137 hardware watchpoint. Otherwise, this is a
2138 nop. */
2139 b->base.type = type;
a9634178 2140 }
9fa40276 2141 }
3a5c3e22 2142 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2143 {
2144 if (!can_use_hw_watchpoints)
2145 error (_("Can't set read/access watchpoint when "
2146 "hardware watchpoints are disabled."));
2147 else
2148 error (_("Expression cannot be implemented with "
2149 "read/access watchpoint."));
2150 }
9fa40276 2151 else
3a5c3e22 2152 b->base.type = bp_watchpoint;
9fa40276 2153
3a5c3e22 2154 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2155 : bp_loc_hardware_watchpoint);
3a5c3e22 2156 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2157 bl->loc_type = loc_type;
2158 }
2159
2160 for (v = val_chain; v; v = next)
2161 {
a5606eee
VP
2162 next = value_next (v);
2163 if (v != b->val)
2164 value_free (v);
2165 }
2166
c7437ca6
PA
2167 /* If a software watchpoint is not watching any memory, then the
2168 above left it without any location set up. But,
2169 bpstat_stop_status requires a location to be able to report
2170 stops, so make sure there's at least a dummy one. */
3a5c3e22 2171 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c6d81124 2172 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
a5606eee
VP
2173 }
2174 else if (!within_current_scope)
7270d8f2 2175 {
ac74f770
MS
2176 printf_filtered (_("\
2177Watchpoint %d deleted because the program has left the block\n\
2178in which its expression is valid.\n"),
3a5c3e22 2179 b->base.number);
d0fb5eae 2180 watchpoint_del_at_next_stop (b);
7270d8f2 2181 }
a5606eee
VP
2182
2183 /* Restore the selected frame. */
66076460
DJ
2184 if (frame_saved)
2185 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2186}
2187
a5606eee 2188
74960c60 2189/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2190 inserted in the inferior. We don't differentiate the type of BL's owner
2191 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2192 breakpoint_ops is not defined, because in insert_bp_location,
2193 tracepoint's insert_location will not be called. */
74960c60 2194static int
35df4500 2195should_be_inserted (struct bp_location *bl)
74960c60 2196{
35df4500 2197 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2198 return 0;
2199
35df4500 2200 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2201 return 0;
2202
35df4500 2203 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2204 return 0;
2205
f8eba3c6
TT
2206 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2207 return 0;
2208
56710373
PA
2209 /* This is set for example, when we're attached to the parent of a
2210 vfork, and have detached from the child. The child is running
2211 free, and we expect it to do an exec or exit, at which point the
2212 OS makes the parent schedulable again (and the target reports
2213 that the vfork is done). Until the child is done with the shared
2214 memory region, do not insert breakpoints in the parent, otherwise
2215 the child could still trip on the parent's breakpoints. Since
2216 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2217 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2218 return 0;
2219
31e77af2
PA
2220 /* Don't insert a breakpoint if we're trying to step past its
2221 location. */
2222 if ((bl->loc_type == bp_loc_software_breakpoint
2223 || bl->loc_type == bp_loc_hardware_breakpoint)
2224 && stepping_past_instruction_at (bl->pspace->aspace,
2225 bl->address))
e558d7c1
PA
2226 {
2227 if (debug_infrun)
2228 {
2229 fprintf_unfiltered (gdb_stdlog,
2230 "infrun: skipping breakpoint: "
2231 "stepping past insn at: %s\n",
2232 paddress (bl->gdbarch, bl->address));
2233 }
2234 return 0;
2235 }
31e77af2 2236
963f9c80
PA
2237 /* Don't insert watchpoints if we're trying to step past the
2238 instruction that triggered one. */
2239 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2240 && stepping_past_nonsteppable_watchpoint ())
2241 {
2242 if (debug_infrun)
2243 {
2244 fprintf_unfiltered (gdb_stdlog,
2245 "infrun: stepping past non-steppable watchpoint. "
2246 "skipping watchpoint at %s:%d\n",
2247 paddress (bl->gdbarch, bl->address),
2248 bl->length);
2249 }
2250 return 0;
2251 }
2252
74960c60
VP
2253 return 1;
2254}
2255
934709f0
PW
2256/* Same as should_be_inserted but does the check assuming
2257 that the location is not duplicated. */
2258
2259static int
2260unduplicated_should_be_inserted (struct bp_location *bl)
2261{
2262 int result;
2263 const int save_duplicate = bl->duplicate;
2264
2265 bl->duplicate = 0;
2266 result = should_be_inserted (bl);
2267 bl->duplicate = save_duplicate;
2268 return result;
2269}
2270
b775012e
LM
2271/* Parses a conditional described by an expression COND into an
2272 agent expression bytecode suitable for evaluation
2273 by the bytecode interpreter. Return NULL if there was
2274 any error during parsing. */
2275
2276static struct agent_expr *
2277parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2278{
2279 struct agent_expr *aexpr = NULL;
b775012e
LM
2280
2281 if (!cond)
2282 return NULL;
2283
2284 /* We don't want to stop processing, so catch any errors
2285 that may show up. */
492d29ea 2286 TRY
b775012e
LM
2287 {
2288 aexpr = gen_eval_for_expr (scope, cond);
2289 }
2290
492d29ea 2291 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2292 {
2293 /* If we got here, it means the condition could not be parsed to a valid
2294 bytecode expression and thus can't be evaluated on the target's side.
2295 It's no use iterating through the conditions. */
2296 return NULL;
2297 }
492d29ea 2298 END_CATCH
b775012e
LM
2299
2300 /* We have a valid agent expression. */
2301 return aexpr;
2302}
2303
2304/* Based on location BL, create a list of breakpoint conditions to be
2305 passed on to the target. If we have duplicated locations with different
2306 conditions, we will add such conditions to the list. The idea is that the
2307 target will evaluate the list of conditions and will only notify GDB when
2308 one of them is true. */
2309
2310static void
2311build_target_condition_list (struct bp_location *bl)
2312{
2313 struct bp_location **locp = NULL, **loc2p;
2314 int null_condition_or_parse_error = 0;
2315 int modified = bl->needs_update;
2316 struct bp_location *loc;
2317
8b4f3082
PA
2318 /* Release conditions left over from a previous insert. */
2319 VEC_free (agent_expr_p, bl->target_info.conditions);
2320
b775012e
LM
2321 /* This is only meaningful if the target is
2322 evaluating conditions and if the user has
2323 opted for condition evaluation on the target's
2324 side. */
2325 if (gdb_evaluates_breakpoint_condition_p ()
2326 || !target_supports_evaluation_of_breakpoint_conditions ())
2327 return;
2328
2329 /* Do a first pass to check for locations with no assigned
2330 conditions or conditions that fail to parse to a valid agent expression
2331 bytecode. If any of these happen, then it's no use to send conditions
2332 to the target since this location will always trigger and generate a
2333 response back to GDB. */
2334 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2335 {
2336 loc = (*loc2p);
2337 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2338 {
2339 if (modified)
2340 {
2341 struct agent_expr *aexpr;
2342
2343 /* Re-parse the conditions since something changed. In that
2344 case we already freed the condition bytecodes (see
2345 force_breakpoint_reinsertion). We just
2346 need to parse the condition to bytecodes again. */
2347 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2348 loc->cond_bytecode = aexpr;
2349
2350 /* Check if we managed to parse the conditional expression
2351 correctly. If not, we will not send this condition
2352 to the target. */
2353 if (aexpr)
2354 continue;
2355 }
2356
2357 /* If we have a NULL bytecode expression, it means something
2358 went wrong or we have a null condition expression. */
2359 if (!loc->cond_bytecode)
2360 {
2361 null_condition_or_parse_error = 1;
2362 break;
2363 }
2364 }
2365 }
2366
2367 /* If any of these happened, it means we will have to evaluate the conditions
2368 for the location's address on gdb's side. It is no use keeping bytecodes
2369 for all the other duplicate locations, thus we free all of them here.
2370
2371 This is so we have a finer control over which locations' conditions are
2372 being evaluated by GDB or the remote stub. */
2373 if (null_condition_or_parse_error)
2374 {
2375 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2376 {
2377 loc = (*loc2p);
2378 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2379 {
2380 /* Only go as far as the first NULL bytecode is
2381 located. */
2382 if (!loc->cond_bytecode)
2383 return;
2384
2385 free_agent_expr (loc->cond_bytecode);
2386 loc->cond_bytecode = NULL;
2387 }
2388 }
2389 }
2390
2391 /* No NULL conditions or failed bytecode generation. Build a condition list
2392 for this location's address. */
2393 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2394 {
2395 loc = (*loc2p);
2396 if (loc->cond
2397 && is_breakpoint (loc->owner)
2398 && loc->pspace->num == bl->pspace->num
2399 && loc->owner->enable_state == bp_enabled
2400 && loc->enabled)
2401 /* Add the condition to the vector. This will be used later to send the
2402 conditions to the target. */
2403 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2404 loc->cond_bytecode);
2405 }
2406
2407 return;
2408}
2409
d3ce09f5
SS
2410/* Parses a command described by string CMD into an agent expression
2411 bytecode suitable for evaluation by the bytecode interpreter.
2412 Return NULL if there was any error during parsing. */
2413
2414static struct agent_expr *
2415parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2416{
2417 struct cleanup *old_cleanups = 0;
2418 struct expression *expr, **argvec;
2419 struct agent_expr *aexpr = NULL;
bbc13ae3
KS
2420 const char *cmdrest;
2421 const char *format_start, *format_end;
d3ce09f5
SS
2422 struct format_piece *fpieces;
2423 int nargs;
2424 struct gdbarch *gdbarch = get_current_arch ();
2425
2426 if (!cmd)
2427 return NULL;
2428
2429 cmdrest = cmd;
2430
2431 if (*cmdrest == ',')
2432 ++cmdrest;
bbc13ae3 2433 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2434
2435 if (*cmdrest++ != '"')
2436 error (_("No format string following the location"));
2437
2438 format_start = cmdrest;
2439
2440 fpieces = parse_format_string (&cmdrest);
2441
2442 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2443
2444 format_end = cmdrest;
2445
2446 if (*cmdrest++ != '"')
2447 error (_("Bad format string, non-terminated '\"'."));
2448
bbc13ae3 2449 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2450
2451 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2452 error (_("Invalid argument syntax"));
2453
2454 if (*cmdrest == ',')
2455 cmdrest++;
bbc13ae3 2456 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2457
2458 /* For each argument, make an expression. */
2459
2460 argvec = (struct expression **) alloca (strlen (cmd)
2461 * sizeof (struct expression *));
2462
2463 nargs = 0;
2464 while (*cmdrest != '\0')
2465 {
bbc13ae3 2466 const char *cmd1;
d3ce09f5
SS
2467
2468 cmd1 = cmdrest;
2469 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2470 argvec[nargs++] = expr;
2471 cmdrest = cmd1;
2472 if (*cmdrest == ',')
2473 ++cmdrest;
2474 }
2475
2476 /* We don't want to stop processing, so catch any errors
2477 that may show up. */
492d29ea 2478 TRY
d3ce09f5
SS
2479 {
2480 aexpr = gen_printf (scope, gdbarch, 0, 0,
2481 format_start, format_end - format_start,
2482 fpieces, nargs, argvec);
2483 }
492d29ea 2484 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2485 {
2486 /* If we got here, it means the command could not be parsed to a valid
2487 bytecode expression and thus can't be evaluated on the target's side.
2488 It's no use iterating through the other commands. */
492d29ea 2489 aexpr = NULL;
d3ce09f5 2490 }
492d29ea
PA
2491 END_CATCH
2492
2493 do_cleanups (old_cleanups);
d3ce09f5 2494
d3ce09f5
SS
2495 /* We have a valid agent expression, return it. */
2496 return aexpr;
2497}
2498
2499/* Based on location BL, create a list of breakpoint commands to be
2500 passed on to the target. If we have duplicated locations with
2501 different commands, we will add any such to the list. */
2502
2503static void
2504build_target_command_list (struct bp_location *bl)
2505{
2506 struct bp_location **locp = NULL, **loc2p;
2507 int null_command_or_parse_error = 0;
2508 int modified = bl->needs_update;
2509 struct bp_location *loc;
2510
8b4f3082
PA
2511 /* Release commands left over from a previous insert. */
2512 VEC_free (agent_expr_p, bl->target_info.tcommands);
2513
41fac0cf 2514 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2515 return;
2516
41fac0cf
PA
2517 /* For now, limit to agent-style dprintf breakpoints. */
2518 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2519 return;
2520
41fac0cf
PA
2521 /* For now, if we have any duplicate location that isn't a dprintf,
2522 don't install the target-side commands, as that would make the
2523 breakpoint not be reported to the core, and we'd lose
2524 control. */
2525 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2526 {
2527 loc = (*loc2p);
2528 if (is_breakpoint (loc->owner)
2529 && loc->pspace->num == bl->pspace->num
2530 && loc->owner->type != bp_dprintf)
2531 return;
2532 }
2533
d3ce09f5
SS
2534 /* Do a first pass to check for locations with no assigned
2535 conditions or conditions that fail to parse to a valid agent expression
2536 bytecode. If any of these happen, then it's no use to send conditions
2537 to the target since this location will always trigger and generate a
2538 response back to GDB. */
2539 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2540 {
2541 loc = (*loc2p);
2542 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2543 {
2544 if (modified)
2545 {
2546 struct agent_expr *aexpr;
2547
2548 /* Re-parse the commands since something changed. In that
2549 case we already freed the command bytecodes (see
2550 force_breakpoint_reinsertion). We just
2551 need to parse the command to bytecodes again. */
2552 aexpr = parse_cmd_to_aexpr (bl->address,
2553 loc->owner->extra_string);
2554 loc->cmd_bytecode = aexpr;
2555
2556 if (!aexpr)
2557 continue;
2558 }
2559
2560 /* If we have a NULL bytecode expression, it means something
2561 went wrong or we have a null command expression. */
2562 if (!loc->cmd_bytecode)
2563 {
2564 null_command_or_parse_error = 1;
2565 break;
2566 }
2567 }
2568 }
2569
2570 /* If anything failed, then we're not doing target-side commands,
2571 and so clean up. */
2572 if (null_command_or_parse_error)
2573 {
2574 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2575 {
2576 loc = (*loc2p);
2577 if (is_breakpoint (loc->owner)
2578 && loc->pspace->num == bl->pspace->num)
2579 {
2580 /* Only go as far as the first NULL bytecode is
2581 located. */
40fb6c5e 2582 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2583 return;
2584
40fb6c5e
HZ
2585 free_agent_expr (loc->cmd_bytecode);
2586 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2587 }
2588 }
2589 }
2590
2591 /* No NULL commands or failed bytecode generation. Build a command list
2592 for this location's address. */
2593 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2594 {
2595 loc = (*loc2p);
2596 if (loc->owner->extra_string
2597 && is_breakpoint (loc->owner)
2598 && loc->pspace->num == bl->pspace->num
2599 && loc->owner->enable_state == bp_enabled
2600 && loc->enabled)
2601 /* Add the command to the vector. This will be used later
2602 to send the commands to the target. */
2603 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2604 loc->cmd_bytecode);
2605 }
2606
2607 bl->target_info.persist = 0;
2608 /* Maybe flag this location as persistent. */
2609 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2610 bl->target_info.persist = 1;
2611}
2612
35df4500
TJB
2613/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2614 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2615 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2616 Returns 0 for success, 1 if the bp_location type is not supported or
2617 -1 for failure.
879bfdc2 2618
4a64f543
MS
2619 NOTE drow/2003-09-09: This routine could be broken down to an
2620 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2621static int
35df4500 2622insert_bp_location (struct bp_location *bl,
26bb91f3 2623 struct ui_file *tmp_error_stream,
3fbb6ffa 2624 int *disabled_breaks,
dd61ec5c
MW
2625 int *hw_breakpoint_error,
2626 int *hw_bp_error_explained_already)
879bfdc2 2627{
0000e5cc
PA
2628 enum errors bp_err = GDB_NO_ERROR;
2629 const char *bp_err_message = NULL;
879bfdc2 2630
b775012e 2631 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2632 return 0;
2633
35c63cd8
JB
2634 /* Note we don't initialize bl->target_info, as that wipes out
2635 the breakpoint location's shadow_contents if the breakpoint
2636 is still inserted at that location. This in turn breaks
2637 target_read_memory which depends on these buffers when
2638 a memory read is requested at the breakpoint location:
2639 Once the target_info has been wiped, we fail to see that
2640 we have a breakpoint inserted at that address and thus
2641 read the breakpoint instead of returning the data saved in
2642 the breakpoint location's shadow contents. */
0d5ed153 2643 bl->target_info.reqstd_address = bl->address;
35df4500 2644 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2645 bl->target_info.length = bl->length;
8181d85f 2646
b775012e
LM
2647 /* When working with target-side conditions, we must pass all the conditions
2648 for the same breakpoint address down to the target since GDB will not
2649 insert those locations. With a list of breakpoint conditions, the target
2650 can decide when to stop and notify GDB. */
2651
2652 if (is_breakpoint (bl->owner))
2653 {
2654 build_target_condition_list (bl);
d3ce09f5
SS
2655 build_target_command_list (bl);
2656 /* Reset the modification marker. */
b775012e
LM
2657 bl->needs_update = 0;
2658 }
2659
35df4500
TJB
2660 if (bl->loc_type == bp_loc_software_breakpoint
2661 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2662 {
35df4500 2663 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2664 {
2665 /* If the explicitly specified breakpoint type
2666 is not hardware breakpoint, check the memory map to see
2667 if the breakpoint address is in read only memory or not.
4a64f543 2668
765dc015
VP
2669 Two important cases are:
2670 - location type is not hardware breakpoint, memory
2671 is readonly. We change the type of the location to
2672 hardware breakpoint.
4a64f543
MS
2673 - location type is hardware breakpoint, memory is
2674 read-write. This means we've previously made the
2675 location hardware one, but then the memory map changed,
2676 so we undo.
765dc015 2677
4a64f543
MS
2678 When breakpoints are removed, remove_breakpoints will use
2679 location types we've just set here, the only possible
2680 problem is that memory map has changed during running
2681 program, but it's not going to work anyway with current
2682 gdb. */
765dc015 2683 struct mem_region *mr
0d5ed153 2684 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2685
2686 if (mr)
2687 {
2688 if (automatic_hardware_breakpoints)
2689 {
765dc015
VP
2690 enum bp_loc_type new_type;
2691
2692 if (mr->attrib.mode != MEM_RW)
2693 new_type = bp_loc_hardware_breakpoint;
2694 else
2695 new_type = bp_loc_software_breakpoint;
2696
35df4500 2697 if (new_type != bl->loc_type)
765dc015
VP
2698 {
2699 static int said = 0;
cc59ec59 2700
35df4500 2701 bl->loc_type = new_type;
765dc015
VP
2702 if (!said)
2703 {
3e43a32a
MS
2704 fprintf_filtered (gdb_stdout,
2705 _("Note: automatically using "
2706 "hardware breakpoints for "
2707 "read-only addresses.\n"));
765dc015
VP
2708 said = 1;
2709 }
2710 }
2711 }
35df4500 2712 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2713 && mr->attrib.mode != MEM_RW)
2714 {
2715 fprintf_unfiltered (tmp_error_stream,
2716 _("Cannot insert breakpoint %d.\n"
2717 "Cannot set software breakpoint "
2718 "at read-only address %s\n"),
2719 bl->owner->number,
2720 paddress (bl->gdbarch, bl->address));
2721 return 1;
2722 }
765dc015
VP
2723 }
2724 }
2725
879bfdc2
DJ
2726 /* First check to see if we have to handle an overlay. */
2727 if (overlay_debugging == ovly_off
35df4500
TJB
2728 || bl->section == NULL
2729 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2730 {
2731 /* No overlay handling: just set the breakpoint. */
492d29ea 2732 TRY
dd61ec5c 2733 {
0000e5cc
PA
2734 int val;
2735
dd61ec5c 2736 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2737 if (val)
2738 bp_err = GENERIC_ERROR;
dd61ec5c 2739 }
492d29ea 2740 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2741 {
0000e5cc
PA
2742 bp_err = e.error;
2743 bp_err_message = e.message;
dd61ec5c 2744 }
492d29ea 2745 END_CATCH
879bfdc2
DJ
2746 }
2747 else
2748 {
4a64f543 2749 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2750 Shall we set a breakpoint at the LMA? */
2751 if (!overlay_events_enabled)
2752 {
2753 /* Yes -- overlay event support is not active,
2754 so we must try to set a breakpoint at the LMA.
2755 This will not work for a hardware breakpoint. */
35df4500 2756 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2757 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2758 bl->owner->number);
879bfdc2
DJ
2759 else
2760 {
35df4500
TJB
2761 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2762 bl->section);
879bfdc2 2763 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2764 bl->overlay_target_info = bl->target_info;
0d5ed153 2765 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2766
2767 /* No overlay handling: just set the breakpoint. */
492d29ea 2768 TRY
0000e5cc
PA
2769 {
2770 int val;
2771
2772 val = target_insert_breakpoint (bl->gdbarch,
2773 &bl->overlay_target_info);
2774 if (val)
2775 bp_err = GENERIC_ERROR;
2776 }
492d29ea 2777 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2778 {
2779 bp_err = e.error;
2780 bp_err_message = e.message;
2781 }
492d29ea 2782 END_CATCH
0000e5cc
PA
2783
2784 if (bp_err != GDB_NO_ERROR)
99361f52 2785 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2786 "Overlay breakpoint %d "
2787 "failed: in ROM?\n",
35df4500 2788 bl->owner->number);
879bfdc2
DJ
2789 }
2790 }
2791 /* Shall we set a breakpoint at the VMA? */
35df4500 2792 if (section_is_mapped (bl->section))
879bfdc2
DJ
2793 {
2794 /* Yes. This overlay section is mapped into memory. */
492d29ea 2795 TRY
dd61ec5c 2796 {
0000e5cc
PA
2797 int val;
2798
dd61ec5c 2799 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2800 if (val)
2801 bp_err = GENERIC_ERROR;
dd61ec5c 2802 }
492d29ea 2803 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2804 {
0000e5cc
PA
2805 bp_err = e.error;
2806 bp_err_message = e.message;
dd61ec5c 2807 }
492d29ea 2808 END_CATCH
879bfdc2
DJ
2809 }
2810 else
2811 {
2812 /* No. This breakpoint will not be inserted.
2813 No error, but do not mark the bp as 'inserted'. */
2814 return 0;
2815 }
2816 }
2817
0000e5cc 2818 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2819 {
2820 /* Can't set the breakpoint. */
0000e5cc
PA
2821
2822 /* In some cases, we might not be able to insert a
2823 breakpoint in a shared library that has already been
2824 removed, but we have not yet processed the shlib unload
2825 event. Unfortunately, some targets that implement
076855f9
PA
2826 breakpoint insertion themselves can't tell why the
2827 breakpoint insertion failed (e.g., the remote target
2828 doesn't define error codes), so we must treat generic
2829 errors as memory errors. */
0000e5cc 2830 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2831 && bl->loc_type == bp_loc_software_breakpoint
08351840 2832 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2833 || shared_objfile_contains_address_p (bl->pspace,
2834 bl->address)))
879bfdc2 2835 {
4a64f543 2836 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2837 bl->shlib_disabled = 1;
8d3788bd 2838 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2839 if (!*disabled_breaks)
2840 {
2841 fprintf_unfiltered (tmp_error_stream,
2842 "Cannot insert breakpoint %d.\n",
2843 bl->owner->number);
2844 fprintf_unfiltered (tmp_error_stream,
2845 "Temporarily disabling shared "
2846 "library breakpoints:\n");
2847 }
2848 *disabled_breaks = 1;
879bfdc2 2849 fprintf_unfiltered (tmp_error_stream,
35df4500 2850 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2851 return 0;
879bfdc2
DJ
2852 }
2853 else
879bfdc2 2854 {
35df4500 2855 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2856 {
0000e5cc
PA
2857 *hw_breakpoint_error = 1;
2858 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2859 fprintf_unfiltered (tmp_error_stream,
2860 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2861 bl->owner->number, bp_err_message ? ":" : ".\n");
2862 if (bp_err_message != NULL)
2863 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2864 }
2865 else
2866 {
0000e5cc
PA
2867 if (bp_err_message == NULL)
2868 {
2869 char *message
2870 = memory_error_message (TARGET_XFER_E_IO,
2871 bl->gdbarch, bl->address);
2872 struct cleanup *old_chain = make_cleanup (xfree, message);
2873
2874 fprintf_unfiltered (tmp_error_stream,
2875 "Cannot insert breakpoint %d.\n"
2876 "%s\n",
2877 bl->owner->number, message);
2878 do_cleanups (old_chain);
2879 }
2880 else
2881 {
2882 fprintf_unfiltered (tmp_error_stream,
2883 "Cannot insert breakpoint %d: %s\n",
2884 bl->owner->number,
2885 bp_err_message);
2886 }
879bfdc2 2887 }
0000e5cc 2888 return 1;
879bfdc2
DJ
2889
2890 }
2891 }
2892 else
35df4500 2893 bl->inserted = 1;
879bfdc2 2894
0000e5cc 2895 return 0;
879bfdc2
DJ
2896 }
2897
35df4500 2898 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2899 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2900 watchpoints. It's not clear that it's necessary... */
35df4500 2901 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2902 {
0000e5cc
PA
2903 int val;
2904
77b06cd7
TJB
2905 gdb_assert (bl->owner->ops != NULL
2906 && bl->owner->ops->insert_location != NULL);
2907
2908 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2909
2910 /* If trying to set a read-watchpoint, and it turns out it's not
2911 supported, try emulating one with an access watchpoint. */
35df4500 2912 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2913 {
2914 struct bp_location *loc, **loc_temp;
2915
2916 /* But don't try to insert it, if there's already another
2917 hw_access location that would be considered a duplicate
2918 of this one. */
2919 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2920 if (loc != bl
85d721b8 2921 && loc->watchpoint_type == hw_access
35df4500 2922 && watchpoint_locations_match (bl, loc))
85d721b8 2923 {
35df4500
TJB
2924 bl->duplicate = 1;
2925 bl->inserted = 1;
2926 bl->target_info = loc->target_info;
2927 bl->watchpoint_type = hw_access;
85d721b8
PA
2928 val = 0;
2929 break;
2930 }
2931
2932 if (val == 1)
2933 {
77b06cd7
TJB
2934 bl->watchpoint_type = hw_access;
2935 val = bl->owner->ops->insert_location (bl);
2936
2937 if (val)
2938 /* Back to the original value. */
2939 bl->watchpoint_type = hw_read;
85d721b8
PA
2940 }
2941 }
2942
35df4500 2943 bl->inserted = (val == 0);
879bfdc2
DJ
2944 }
2945
35df4500 2946 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2947 {
0000e5cc
PA
2948 int val;
2949
77b06cd7
TJB
2950 gdb_assert (bl->owner->ops != NULL
2951 && bl->owner->ops->insert_location != NULL);
2952
2953 val = bl->owner->ops->insert_location (bl);
2954 if (val)
2955 {
2956 bl->owner->enable_state = bp_disabled;
2957
2958 if (val == 1)
2959 warning (_("\
2960Error inserting catchpoint %d: Your system does not support this type\n\
2961of catchpoint."), bl->owner->number);
2962 else
2963 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2964 }
2965
2966 bl->inserted = (val == 0);
1640b821
DJ
2967
2968 /* We've already printed an error message if there was a problem
2969 inserting this catchpoint, and we've disabled the catchpoint,
2970 so just return success. */
2971 return 0;
879bfdc2
DJ
2972 }
2973
2974 return 0;
2975}
2976
6c95b8df
PA
2977/* This function is called when program space PSPACE is about to be
2978 deleted. It takes care of updating breakpoints to not reference
2979 PSPACE anymore. */
2980
2981void
2982breakpoint_program_space_exit (struct program_space *pspace)
2983{
2984 struct breakpoint *b, *b_temp;
876fa593 2985 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2986
2987 /* Remove any breakpoint that was set through this program space. */
2988 ALL_BREAKPOINTS_SAFE (b, b_temp)
2989 {
2990 if (b->pspace == pspace)
2991 delete_breakpoint (b);
2992 }
2993
2994 /* Breakpoints set through other program spaces could have locations
2995 bound to PSPACE as well. Remove those. */
876fa593 2996 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2997 {
2998 struct bp_location *tmp;
2999
3000 if (loc->pspace == pspace)
3001 {
2bdf28a0 3002 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
3003 if (loc->owner->loc == loc)
3004 loc->owner->loc = loc->next;
3005 else
3006 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3007 if (tmp->next == loc)
3008 {
3009 tmp->next = loc->next;
3010 break;
3011 }
3012 }
3013 }
3014
3015 /* Now update the global location list to permanently delete the
3016 removed locations above. */
44702360 3017 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
3018}
3019
74960c60
VP
3020/* Make sure all breakpoints are inserted in inferior.
3021 Throws exception on any error.
3022 A breakpoint that is already inserted won't be inserted
3023 again, so calling this function twice is safe. */
3024void
3025insert_breakpoints (void)
3026{
3027 struct breakpoint *bpt;
3028
3029 ALL_BREAKPOINTS (bpt)
3030 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3031 {
3032 struct watchpoint *w = (struct watchpoint *) bpt;
3033
3034 update_watchpoint (w, 0 /* don't reparse. */);
3035 }
74960c60 3036
04086b45
PA
3037 /* Updating watchpoints creates new locations, so update the global
3038 location list. Explicitly tell ugll to insert locations and
3039 ignore breakpoints_always_inserted_mode. */
3040 update_global_location_list (UGLL_INSERT);
74960c60
VP
3041}
3042
20388dd6
YQ
3043/* Invoke CALLBACK for each of bp_location. */
3044
3045void
3046iterate_over_bp_locations (walk_bp_location_callback callback)
3047{
3048 struct bp_location *loc, **loc_tmp;
3049
3050 ALL_BP_LOCATIONS (loc, loc_tmp)
3051 {
3052 callback (loc, NULL);
3053 }
3054}
3055
b775012e
LM
3056/* This is used when we need to synch breakpoint conditions between GDB and the
3057 target. It is the case with deleting and disabling of breakpoints when using
3058 always-inserted mode. */
3059
3060static void
3061update_inserted_breakpoint_locations (void)
3062{
3063 struct bp_location *bl, **blp_tmp;
3064 int error_flag = 0;
3065 int val = 0;
3066 int disabled_breaks = 0;
3067 int hw_breakpoint_error = 0;
dd61ec5c 3068 int hw_bp_details_reported = 0;
b775012e
LM
3069
3070 struct ui_file *tmp_error_stream = mem_fileopen ();
3071 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3072
3073 /* Explicitly mark the warning -- this will only be printed if
3074 there was an error. */
3075 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3076
3077 save_current_space_and_thread ();
3078
3079 ALL_BP_LOCATIONS (bl, blp_tmp)
3080 {
3081 /* We only want to update software breakpoints and hardware
3082 breakpoints. */
3083 if (!is_breakpoint (bl->owner))
3084 continue;
3085
3086 /* We only want to update locations that are already inserted
3087 and need updating. This is to avoid unwanted insertion during
3088 deletion of breakpoints. */
3089 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3090 continue;
3091
3092 switch_to_program_space_and_thread (bl->pspace);
3093
3094 /* For targets that support global breakpoints, there's no need
3095 to select an inferior to insert breakpoint to. In fact, even
3096 if we aren't attached to any process yet, we should still
3097 insert breakpoints. */
f5656ead 3098 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3099 && ptid_equal (inferior_ptid, null_ptid))
3100 continue;
3101
3102 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3103 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3104 if (val)
3105 error_flag = val;
3106 }
3107
3108 if (error_flag)
3109 {
3110 target_terminal_ours_for_output ();
3111 error_stream (tmp_error_stream);
3112 }
3113
3114 do_cleanups (cleanups);
3115}
3116
c30eee59 3117/* Used when starting or continuing the program. */
c906108c 3118
74960c60
VP
3119static void
3120insert_breakpoint_locations (void)
c906108c 3121{
a5606eee 3122 struct breakpoint *bpt;
35df4500 3123 struct bp_location *bl, **blp_tmp;
eacd795a 3124 int error_flag = 0;
c906108c 3125 int val = 0;
3fbb6ffa 3126 int disabled_breaks = 0;
81d0cc19 3127 int hw_breakpoint_error = 0;
dd61ec5c 3128 int hw_bp_error_explained_already = 0;
c906108c 3129
81d0cc19 3130 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3131 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3132
81d0cc19
GS
3133 /* Explicitly mark the warning -- this will only be printed if
3134 there was an error. */
3135 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3136
3137 save_current_space_and_thread ();
3138
35df4500 3139 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3140 {
b775012e 3141 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3142 continue;
3143
4a64f543
MS
3144 /* There is no point inserting thread-specific breakpoints if
3145 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3146 has BL->OWNER always non-NULL. */
35df4500 3147 if (bl->owner->thread != -1
5d5658a1 3148 && !valid_global_thread_id (bl->owner->thread))
f365de73
AS
3149 continue;
3150
35df4500 3151 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3152
3153 /* For targets that support global breakpoints, there's no need
3154 to select an inferior to insert breakpoint to. In fact, even
3155 if we aren't attached to any process yet, we should still
3156 insert breakpoints. */
f5656ead 3157 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3158 && ptid_equal (inferior_ptid, null_ptid))
3159 continue;
3160
3fbb6ffa 3161 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3162 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3163 if (val)
eacd795a 3164 error_flag = val;
879bfdc2 3165 }
c906108c 3166
4a64f543
MS
3167 /* If we failed to insert all locations of a watchpoint, remove
3168 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3169 ALL_BREAKPOINTS (bpt)
3170 {
3171 int some_failed = 0;
3172 struct bp_location *loc;
3173
3174 if (!is_hardware_watchpoint (bpt))
3175 continue;
3176
d6b74ac4 3177 if (!breakpoint_enabled (bpt))
a5606eee 3178 continue;
74960c60
VP
3179
3180 if (bpt->disposition == disp_del_at_next_stop)
3181 continue;
a5606eee
VP
3182
3183 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3184 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3185 {
3186 some_failed = 1;
3187 break;
3188 }
3189 if (some_failed)
3190 {
3191 for (loc = bpt->loc; loc; loc = loc->next)
3192 if (loc->inserted)
3193 remove_breakpoint (loc, mark_uninserted);
3194
3195 hw_breakpoint_error = 1;
3196 fprintf_unfiltered (tmp_error_stream,
3197 "Could not insert hardware watchpoint %d.\n",
3198 bpt->number);
eacd795a 3199 error_flag = -1;
a5606eee
VP
3200 }
3201 }
3202
eacd795a 3203 if (error_flag)
81d0cc19
GS
3204 {
3205 /* If a hardware breakpoint or watchpoint was inserted, add a
3206 message about possibly exhausted resources. */
dd61ec5c 3207 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3208 {
c6510018
MS
3209 fprintf_unfiltered (tmp_error_stream,
3210 "Could not insert hardware breakpoints:\n\
3211You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3212 }
81d0cc19
GS
3213 target_terminal_ours_for_output ();
3214 error_stream (tmp_error_stream);
3215 }
f7545552
TT
3216
3217 do_cleanups (cleanups);
c906108c
SS
3218}
3219
c30eee59
TJB
3220/* Used when the program stops.
3221 Returns zero if successful, or non-zero if there was a problem
3222 removing a breakpoint location. */
3223
c906108c 3224int
fba45db2 3225remove_breakpoints (void)
c906108c 3226{
35df4500 3227 struct bp_location *bl, **blp_tmp;
3a1bae8e 3228 int val = 0;
c906108c 3229
35df4500 3230 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3231 {
1e4d1764 3232 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3233 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3234 }
3a1bae8e 3235 return val;
c906108c
SS
3236}
3237
49fa26b0
PA
3238/* When a thread exits, remove breakpoints that are related to
3239 that thread. */
3240
3241static void
3242remove_threaded_breakpoints (struct thread_info *tp, int silent)
3243{
3244 struct breakpoint *b, *b_tmp;
3245
3246 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3247 {
5d5658a1 3248 if (b->thread == tp->global_num && user_breakpoint_p (b))
49fa26b0
PA
3249 {
3250 b->disposition = disp_del_at_next_stop;
3251
3252 printf_filtered (_("\
43792cf0
PA
3253Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3254 b->number, print_thread_id (tp));
49fa26b0
PA
3255
3256 /* Hide it from the user. */
3257 b->number = 0;
3258 }
3259 }
3260}
3261
6c95b8df
PA
3262/* Remove breakpoints of process PID. */
3263
3264int
3265remove_breakpoints_pid (int pid)
3266{
35df4500 3267 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3268 int val;
3269 struct inferior *inf = find_inferior_pid (pid);
3270
35df4500 3271 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3272 {
35df4500 3273 if (bl->pspace != inf->pspace)
6c95b8df
PA
3274 continue;
3275
fc126975 3276 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3277 {
35df4500 3278 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3279 if (val != 0)
3280 return val;
3281 }
3282 }
3283 return 0;
3284}
3285
c906108c 3286int
fba45db2 3287reattach_breakpoints (int pid)
c906108c 3288{
6c95b8df 3289 struct cleanup *old_chain;
35df4500 3290 struct bp_location *bl, **blp_tmp;
c906108c 3291 int val;
86b887df 3292 struct ui_file *tmp_error_stream;
dd61ec5c 3293 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3294 struct inferior *inf;
3295 struct thread_info *tp;
3296
3297 tp = any_live_thread_of_process (pid);
3298 if (tp == NULL)
3299 return 1;
3300
3301 inf = find_inferior_pid (pid);
3302 old_chain = save_inferior_ptid ();
3303
3304 inferior_ptid = tp->ptid;
a4954f26 3305
86b887df 3306 tmp_error_stream = mem_fileopen ();
a4954f26 3307 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3308
35df4500 3309 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3310 {
35df4500 3311 if (bl->pspace != inf->pspace)
6c95b8df
PA
3312 continue;
3313
35df4500 3314 if (bl->inserted)
c5aa993b 3315 {
35df4500 3316 bl->inserted = 0;
dd61ec5c 3317 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3318 if (val != 0)
3319 {
ce696e05 3320 do_cleanups (old_chain);
c5aa993b
JM
3321 return val;
3322 }
3323 }
3324 }
ce696e05 3325 do_cleanups (old_chain);
c906108c
SS
3326 return 0;
3327}
3328
e58b0e63
PA
3329static int internal_breakpoint_number = -1;
3330
84f4c1fe
PM
3331/* Set the breakpoint number of B, depending on the value of INTERNAL.
3332 If INTERNAL is non-zero, the breakpoint number will be populated
3333 from internal_breakpoint_number and that variable decremented.
e5dd4106 3334 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3335 breakpoint_count and that value incremented. Internal breakpoints
3336 do not set the internal var bpnum. */
3337static void
3338set_breakpoint_number (int internal, struct breakpoint *b)
3339{
3340 if (internal)
3341 b->number = internal_breakpoint_number--;
3342 else
3343 {
3344 set_breakpoint_count (breakpoint_count + 1);
3345 b->number = breakpoint_count;
3346 }
3347}
3348
e62c965a 3349static struct breakpoint *
a6d9a66e 3350create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3351 CORE_ADDR address, enum bptype type,
c0a91b2b 3352 const struct breakpoint_ops *ops)
e62c965a 3353{
e62c965a
PP
3354 struct symtab_and_line sal;
3355 struct breakpoint *b;
3356
4a64f543 3357 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3358
3359 sal.pc = address;
3360 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3361 sal.pspace = current_program_space;
e62c965a 3362
06edf0c0 3363 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3364 b->number = internal_breakpoint_number--;
3365 b->disposition = disp_donttouch;
3366
3367 return b;
3368}
3369
17450429
PP
3370static const char *const longjmp_names[] =
3371 {
3372 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3373 };
3374#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3375
3376/* Per-objfile data private to breakpoint.c. */
3377struct breakpoint_objfile_data
3378{
3379 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3380 struct bound_minimal_symbol overlay_msym;
17450429
PP
3381
3382 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3383 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3384
28106bc2
SDJ
3385 /* True if we have looked for longjmp probes. */
3386 int longjmp_searched;
3387
3388 /* SystemTap probe points for longjmp (if any). */
3389 VEC (probe_p) *longjmp_probes;
3390
17450429 3391 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3392 struct bound_minimal_symbol terminate_msym;
17450429
PP
3393
3394 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3395 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3396
3397 /* True if we have looked for exception probes. */
3398 int exception_searched;
3399
3400 /* SystemTap probe points for unwinding (if any). */
3401 VEC (probe_p) *exception_probes;
17450429
PP
3402};
3403
3404static const struct objfile_data *breakpoint_objfile_key;
3405
3406/* Minimal symbol not found sentinel. */
3407static struct minimal_symbol msym_not_found;
3408
3409/* Returns TRUE if MSYM point to the "not found" sentinel. */
3410
3411static int
3412msym_not_found_p (const struct minimal_symbol *msym)
3413{
3414 return msym == &msym_not_found;
3415}
3416
3417/* Return per-objfile data needed by breakpoint.c.
3418 Allocate the data if necessary. */
3419
3420static struct breakpoint_objfile_data *
3421get_breakpoint_objfile_data (struct objfile *objfile)
3422{
3423 struct breakpoint_objfile_data *bp_objfile_data;
3424
9a3c8263
SM
3425 bp_objfile_data = ((struct breakpoint_objfile_data *)
3426 objfile_data (objfile, breakpoint_objfile_key));
17450429
PP
3427 if (bp_objfile_data == NULL)
3428 {
8d749320
SM
3429 bp_objfile_data =
3430 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3431
3432 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3433 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3434 }
3435 return bp_objfile_data;
3436}
3437
28106bc2
SDJ
3438static void
3439free_breakpoint_probes (struct objfile *obj, void *data)
3440{
9a3c8263
SM
3441 struct breakpoint_objfile_data *bp_objfile_data
3442 = (struct breakpoint_objfile_data *) data;
28106bc2
SDJ
3443
3444 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3445 VEC_free (probe_p, bp_objfile_data->exception_probes);
3446}
3447
e62c965a 3448static void
af02033e 3449create_overlay_event_breakpoint (void)
e62c965a 3450{
69de3c6a 3451 struct objfile *objfile;
af02033e 3452 const char *const func_name = "_ovly_debug_event";
e62c965a 3453
69de3c6a
PP
3454 ALL_OBJFILES (objfile)
3455 {
3456 struct breakpoint *b;
17450429
PP
3457 struct breakpoint_objfile_data *bp_objfile_data;
3458 CORE_ADDR addr;
67994074 3459 struct explicit_location explicit_loc;
69de3c6a 3460
17450429
PP
3461 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3462
3b7344d5 3463 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3464 continue;
3465
3b7344d5 3466 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3467 {
3b7344d5 3468 struct bound_minimal_symbol m;
17450429
PP
3469
3470 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3471 if (m.minsym == NULL)
17450429
PP
3472 {
3473 /* Avoid future lookups in this objfile. */
3b7344d5 3474 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3475 continue;
3476 }
3477 bp_objfile_data->overlay_msym = m;
3478 }
e62c965a 3479
77e371c0 3480 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3481 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3482 bp_overlay_event,
3483 &internal_breakpoint_ops);
67994074
KS
3484 initialize_explicit_location (&explicit_loc);
3485 explicit_loc.function_name = ASTRDUP (func_name);
3486 b->location = new_explicit_location (&explicit_loc);
e62c965a 3487
69de3c6a
PP
3488 if (overlay_debugging == ovly_auto)
3489 {
3490 b->enable_state = bp_enabled;
3491 overlay_events_enabled = 1;
3492 }
3493 else
3494 {
3495 b->enable_state = bp_disabled;
3496 overlay_events_enabled = 0;
3497 }
e62c965a 3498 }
44702360 3499 update_global_location_list (UGLL_MAY_INSERT);
e62c965a
PP
3500}
3501
0fd8e87f 3502static void
af02033e 3503create_longjmp_master_breakpoint (void)
0fd8e87f 3504{
6c95b8df 3505 struct program_space *pspace;
6c95b8df
PA
3506 struct cleanup *old_chain;
3507
3508 old_chain = save_current_program_space ();
0fd8e87f 3509
6c95b8df 3510 ALL_PSPACES (pspace)
af02033e
PP
3511 {
3512 struct objfile *objfile;
3513
3514 set_current_program_space (pspace);
3515
3516 ALL_OBJFILES (objfile)
0fd8e87f 3517 {
af02033e
PP
3518 int i;
3519 struct gdbarch *gdbarch;
17450429 3520 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3521
af02033e 3522 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3523
17450429
PP
3524 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3525
28106bc2
SDJ
3526 if (!bp_objfile_data->longjmp_searched)
3527 {
25f9533e
SDJ
3528 VEC (probe_p) *ret;
3529
3530 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3531 if (ret != NULL)
3532 {
3533 /* We are only interested in checking one element. */
3534 struct probe *p = VEC_index (probe_p, ret, 0);
3535
3536 if (!can_evaluate_probe_arguments (p))
3537 {
3538 /* We cannot use the probe interface here, because it does
3539 not know how to evaluate arguments. */
3540 VEC_free (probe_p, ret);
3541 ret = NULL;
3542 }
3543 }
3544 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3545 bp_objfile_data->longjmp_searched = 1;
3546 }
3547
3548 if (bp_objfile_data->longjmp_probes != NULL)
3549 {
3550 int i;
3551 struct probe *probe;
3552 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3553
3554 for (i = 0;
3555 VEC_iterate (probe_p,
3556 bp_objfile_data->longjmp_probes,
3557 i, probe);
3558 ++i)
3559 {
3560 struct breakpoint *b;
3561
729662a5
TT
3562 b = create_internal_breakpoint (gdbarch,
3563 get_probe_address (probe,
3564 objfile),
28106bc2
SDJ
3565 bp_longjmp_master,
3566 &internal_breakpoint_ops);
5b56227b
KS
3567 b->location
3568 = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3569 b->enable_state = bp_disabled;
3570 }
3571
3572 continue;
3573 }
3574
0569175e
TSD
3575 if (!gdbarch_get_longjmp_target_p (gdbarch))
3576 continue;
3577
17450429 3578 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3579 {
3580 struct breakpoint *b;
af02033e 3581 const char *func_name;
17450429 3582 CORE_ADDR addr;
67994074 3583 struct explicit_location explicit_loc;
6c95b8df 3584
3b7344d5 3585 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3586 continue;
0fd8e87f 3587
17450429 3588 func_name = longjmp_names[i];
3b7344d5 3589 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3590 {
3b7344d5 3591 struct bound_minimal_symbol m;
17450429
PP
3592
3593 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3594 if (m.minsym == NULL)
17450429
PP
3595 {
3596 /* Prevent future lookups in this objfile. */
3b7344d5 3597 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3598 continue;
3599 }
3600 bp_objfile_data->longjmp_msym[i] = m;
3601 }
3602
77e371c0 3603 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3604 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3605 &internal_breakpoint_ops);
67994074
KS
3606 initialize_explicit_location (&explicit_loc);
3607 explicit_loc.function_name = ASTRDUP (func_name);
3608 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3609 b->enable_state = bp_disabled;
3610 }
0fd8e87f 3611 }
af02033e 3612 }
44702360 3613 update_global_location_list (UGLL_MAY_INSERT);
6c95b8df
PA
3614
3615 do_cleanups (old_chain);
0fd8e87f
UW
3616}
3617
af02033e 3618/* Create a master std::terminate breakpoint. */
aa7d318d 3619static void
af02033e 3620create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3621{
3622 struct program_space *pspace;
aa7d318d 3623 struct cleanup *old_chain;
af02033e 3624 const char *const func_name = "std::terminate()";
aa7d318d
TT
3625
3626 old_chain = save_current_program_space ();
3627
3628 ALL_PSPACES (pspace)
17450429
PP
3629 {
3630 struct objfile *objfile;
3631 CORE_ADDR addr;
3632
3633 set_current_program_space (pspace);
3634
aa7d318d
TT
3635 ALL_OBJFILES (objfile)
3636 {
3637 struct breakpoint *b;
17450429 3638 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3639 struct explicit_location explicit_loc;
aa7d318d 3640
17450429 3641 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3642
3b7344d5 3643 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3644 continue;
3645
3b7344d5 3646 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3647 {
3b7344d5 3648 struct bound_minimal_symbol m;
17450429
PP
3649
3650 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3651 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3652 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3653 {
3654 /* Prevent future lookups in this objfile. */
3b7344d5 3655 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3656 continue;
3657 }
3658 bp_objfile_data->terminate_msym = m;
3659 }
aa7d318d 3660
77e371c0 3661 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3662 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3663 bp_std_terminate_master,
3664 &internal_breakpoint_ops);
67994074
KS
3665 initialize_explicit_location (&explicit_loc);
3666 explicit_loc.function_name = ASTRDUP (func_name);
3667 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3668 b->enable_state = bp_disabled;
3669 }
17450429
PP
3670 }
3671
44702360 3672 update_global_location_list (UGLL_MAY_INSERT);
aa7d318d
TT
3673
3674 do_cleanups (old_chain);
3675}
3676
186c406b
TT
3677/* Install a master breakpoint on the unwinder's debug hook. */
3678
70221824 3679static void
186c406b
TT
3680create_exception_master_breakpoint (void)
3681{
3682 struct objfile *objfile;
17450429 3683 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3684
3685 ALL_OBJFILES (objfile)
3686 {
17450429
PP
3687 struct breakpoint *b;
3688 struct gdbarch *gdbarch;
3689 struct breakpoint_objfile_data *bp_objfile_data;
3690 CORE_ADDR addr;
67994074 3691 struct explicit_location explicit_loc;
17450429
PP
3692
3693 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3694
28106bc2
SDJ
3695 /* We prefer the SystemTap probe point if it exists. */
3696 if (!bp_objfile_data->exception_searched)
3697 {
25f9533e
SDJ
3698 VEC (probe_p) *ret;
3699
3700 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3701
3702 if (ret != NULL)
3703 {
3704 /* We are only interested in checking one element. */
3705 struct probe *p = VEC_index (probe_p, ret, 0);
3706
3707 if (!can_evaluate_probe_arguments (p))
3708 {
3709 /* We cannot use the probe interface here, because it does
3710 not know how to evaluate arguments. */
3711 VEC_free (probe_p, ret);
3712 ret = NULL;
3713 }
3714 }
3715 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3716 bp_objfile_data->exception_searched = 1;
3717 }
3718
3719 if (bp_objfile_data->exception_probes != NULL)
3720 {
3721 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3722 int i;
3723 struct probe *probe;
3724
3725 for (i = 0;
3726 VEC_iterate (probe_p,
3727 bp_objfile_data->exception_probes,
3728 i, probe);
3729 ++i)
3730 {
3731 struct breakpoint *b;
3732
729662a5
TT
3733 b = create_internal_breakpoint (gdbarch,
3734 get_probe_address (probe,
3735 objfile),
28106bc2
SDJ
3736 bp_exception_master,
3737 &internal_breakpoint_ops);
5b56227b
KS
3738 b->location
3739 = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3740 b->enable_state = bp_disabled;
3741 }
3742
3743 continue;
3744 }
3745
3746 /* Otherwise, try the hook function. */
3747
3b7344d5 3748 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3749 continue;
3750
3751 gdbarch = get_objfile_arch (objfile);
186c406b 3752
3b7344d5 3753 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3754 {
3b7344d5 3755 struct bound_minimal_symbol debug_hook;
186c406b 3756
17450429 3757 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3758 if (debug_hook.minsym == NULL)
17450429 3759 {
3b7344d5 3760 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3761 continue;
3762 }
3763
3764 bp_objfile_data->exception_msym = debug_hook;
186c406b 3765 }
17450429 3766
77e371c0 3767 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3768 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3769 &current_target);
06edf0c0
PA
3770 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3771 &internal_breakpoint_ops);
67994074
KS
3772 initialize_explicit_location (&explicit_loc);
3773 explicit_loc.function_name = ASTRDUP (func_name);
3774 b->location = new_explicit_location (&explicit_loc);
17450429 3775 b->enable_state = bp_disabled;
186c406b
TT
3776 }
3777
44702360 3778 update_global_location_list (UGLL_MAY_INSERT);
186c406b
TT
3779}
3780
c906108c 3781void
fba45db2 3782update_breakpoints_after_exec (void)
c906108c 3783{
35df4500 3784 struct breakpoint *b, *b_tmp;
876fa593 3785 struct bp_location *bploc, **bplocp_tmp;
c906108c 3786
25b22b0a
PA
3787 /* We're about to delete breakpoints from GDB's lists. If the
3788 INSERTED flag is true, GDB will try to lift the breakpoints by
3789 writing the breakpoints' "shadow contents" back into memory. The
3790 "shadow contents" are NOT valid after an exec, so GDB should not
3791 do that. Instead, the target is responsible from marking
3792 breakpoints out as soon as it detects an exec. We don't do that
3793 here instead, because there may be other attempts to delete
3794 breakpoints after detecting an exec and before reaching here. */
876fa593 3795 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3796 if (bploc->pspace == current_program_space)
3797 gdb_assert (!bploc->inserted);
c906108c 3798
35df4500 3799 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3800 {
6c95b8df
PA
3801 if (b->pspace != current_program_space)
3802 continue;
3803
4a64f543 3804 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3805 if (b->type == bp_shlib_event)
3806 {
3807 delete_breakpoint (b);
3808 continue;
3809 }
c906108c 3810
4a64f543 3811 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3812 if (b->type == bp_jit_event)
3813 {
3814 delete_breakpoint (b);
3815 continue;
3816 }
3817
1900040c 3818 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3819 as must overlay event and longjmp master breakpoints. */
3820 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3821 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3822 || b->type == bp_exception_master)
c4093a6a
JM
3823 {
3824 delete_breakpoint (b);
3825 continue;
3826 }
3827
4a64f543 3828 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3829 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3830 {
3831 delete_breakpoint (b);
3832 continue;
3833 }
3834
7c16b83e
PA
3835 /* Just like single-step breakpoints. */
3836 if (b->type == bp_single_step)
3837 {
3838 delete_breakpoint (b);
3839 continue;
3840 }
3841
611c83ae
PA
3842 /* Longjmp and longjmp-resume breakpoints are also meaningless
3843 after an exec. */
186c406b 3844 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3845 || b->type == bp_longjmp_call_dummy
186c406b 3846 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3847 {
3848 delete_breakpoint (b);
3849 continue;
3850 }
3851
ce78b96d
JB
3852 if (b->type == bp_catchpoint)
3853 {
3854 /* For now, none of the bp_catchpoint breakpoints need to
3855 do anything at this point. In the future, if some of
3856 the catchpoints need to something, we will need to add
3857 a new method, and call this method from here. */
3858 continue;
3859 }
3860
c5aa993b
JM
3861 /* bp_finish is a special case. The only way we ought to be able
3862 to see one of these when an exec() has happened, is if the user
3863 caught a vfork, and then said "finish". Ordinarily a finish just
3864 carries them to the call-site of the current callee, by setting
3865 a temporary bp there and resuming. But in this case, the finish
3866 will carry them entirely through the vfork & exec.
3867
3868 We don't want to allow a bp_finish to remain inserted now. But
3869 we can't safely delete it, 'cause finish_command has a handle to
3870 the bp on a bpstat, and will later want to delete it. There's a
3871 chance (and I've seen it happen) that if we delete the bp_finish
3872 here, that its storage will get reused by the time finish_command
3873 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3874 We really must allow finish_command to delete a bp_finish.
3875
e5dd4106 3876 In the absence of a general solution for the "how do we know
53a5351d
JM
3877 it's safe to delete something others may have handles to?"
3878 problem, what we'll do here is just uninsert the bp_finish, and
3879 let finish_command delete it.
3880
3881 (We know the bp_finish is "doomed" in the sense that it's
3882 momentary, and will be deleted as soon as finish_command sees
3883 the inferior stopped. So it doesn't matter that the bp's
3884 address is probably bogus in the new a.out, unlike e.g., the
3885 solib breakpoints.) */
c5aa993b 3886
c5aa993b
JM
3887 if (b->type == bp_finish)
3888 {
3889 continue;
3890 }
3891
3892 /* Without a symbolic address, we have little hope of the
3893 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3894 a.out. */
f00aae0f 3895 if (event_location_empty_p (b->location))
c5aa993b
JM
3896 {
3897 delete_breakpoint (b);
3898 continue;
3899 }
c5aa993b 3900 }
c906108c
SS
3901}
3902
3903int
d80ee84f 3904detach_breakpoints (ptid_t ptid)
c906108c 3905{
35df4500 3906 struct bp_location *bl, **blp_tmp;
3a1bae8e 3907 int val = 0;
ce696e05 3908 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3909 struct inferior *inf = current_inferior ();
c5aa993b 3910
dfd4cc63 3911 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3912 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3913
6c95b8df 3914 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3915 inferior_ptid = ptid;
35df4500 3916 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3917 {
35df4500 3918 if (bl->pspace != inf->pspace)
6c95b8df
PA
3919 continue;
3920
bd9673a4
PW
3921 /* This function must physically remove breakpoints locations
3922 from the specified ptid, without modifying the breakpoint
3923 package's state. Locations of type bp_loc_other are only
3924 maintained at GDB side. So, there is no need to remove
3925 these bp_loc_other locations. Moreover, removing these
3926 would modify the breakpoint package's state. */
3927 if (bl->loc_type == bp_loc_other)
3928 continue;
3929
35df4500
TJB
3930 if (bl->inserted)
3931 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3932 }
d03285ec 3933
ce696e05 3934 do_cleanups (old_chain);
3a1bae8e 3935 return val;
c906108c
SS
3936}
3937
35df4500 3938/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3939 Note that this is used to detach breakpoints from a child fork.
3940 When we get here, the child isn't in the inferior list, and neither
3941 do we have objects to represent its address space --- we should
35df4500 3942 *not* look at bl->pspace->aspace here. */
6c95b8df 3943
c906108c 3944static int
35df4500 3945remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3946{
3947 int val;
c5aa993b 3948
35df4500
TJB
3949 /* BL is never in moribund_locations by our callers. */
3950 gdb_assert (bl->owner != NULL);
2bdf28a0 3951
74960c60
VP
3952 /* The type of none suggests that owner is actually deleted.
3953 This should not ever happen. */
35df4500 3954 gdb_assert (bl->owner->type != bp_none);
0bde7532 3955
35df4500
TJB
3956 if (bl->loc_type == bp_loc_software_breakpoint
3957 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3958 {
c02f5703
MS
3959 /* "Normal" instruction breakpoint: either the standard
3960 trap-instruction bp (bp_breakpoint), or a
3961 bp_hardware_breakpoint. */
3962
3963 /* First check to see if we have to handle an overlay. */
3964 if (overlay_debugging == ovly_off
35df4500
TJB
3965 || bl->section == NULL
3966 || !(section_is_overlay (bl->section)))
c02f5703
MS
3967 {
3968 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3969
3970 /* If we're trying to uninsert a memory breakpoint that we
3971 know is set in a dynamic object that is marked
3972 shlib_disabled, then either the dynamic object was
3973 removed with "remove-symbol-file" or with
3974 "nosharedlibrary". In the former case, we don't know
3975 whether another dynamic object might have loaded over the
3976 breakpoint's address -- the user might well let us know
3977 about it next with add-symbol-file (the whole point of
d03de421 3978 add-symbol-file is letting the user manually maintain a
08351840
PA
3979 list of dynamically loaded objects). If we have the
3980 breakpoint's shadow memory, that is, this is a software
3981 breakpoint managed by GDB, check whether the breakpoint
3982 is still inserted in memory, to avoid overwriting wrong
3983 code with stale saved shadow contents. Note that HW
3984 breakpoints don't have shadow memory, as they're
3985 implemented using a mechanism that is not dependent on
3986 being able to modify the target's memory, and as such
3987 they should always be removed. */
3988 if (bl->shlib_disabled
3989 && bl->target_info.shadow_len != 0
3990 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3991 val = 0;
3992 else
3993 val = bl->owner->ops->remove_location (bl);
c02f5703 3994 }
c906108c
SS
3995 else
3996 {
4a64f543 3997 /* This breakpoint is in an overlay section.
c02f5703
MS
3998 Did we set a breakpoint at the LMA? */
3999 if (!overlay_events_enabled)
4000 {
4001 /* Yes -- overlay event support is not active, so we
4002 should have set a breakpoint at the LMA. Remove it.
4003 */
c02f5703
MS
4004 /* Ignore any failures: if the LMA is in ROM, we will
4005 have already warned when we failed to insert it. */
35df4500
TJB
4006 if (bl->loc_type == bp_loc_hardware_breakpoint)
4007 target_remove_hw_breakpoint (bl->gdbarch,
4008 &bl->overlay_target_info);
c02f5703 4009 else
35df4500
TJB
4010 target_remove_breakpoint (bl->gdbarch,
4011 &bl->overlay_target_info);
c02f5703
MS
4012 }
4013 /* Did we set a breakpoint at the VMA?
4014 If so, we will have marked the breakpoint 'inserted'. */
35df4500 4015 if (bl->inserted)
c906108c 4016 {
c02f5703
MS
4017 /* Yes -- remove it. Previously we did not bother to
4018 remove the breakpoint if the section had been
4019 unmapped, but let's not rely on that being safe. We
4020 don't know what the overlay manager might do. */
aa67235e
UW
4021
4022 /* However, we should remove *software* breakpoints only
4023 if the section is still mapped, or else we overwrite
4024 wrong code with the saved shadow contents. */
348d480f
PA
4025 if (bl->loc_type == bp_loc_hardware_breakpoint
4026 || section_is_mapped (bl->section))
4027 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
4028 else
4029 val = 0;
c906108c 4030 }
c02f5703
MS
4031 else
4032 {
4033 /* No -- not inserted, so no need to remove. No error. */
4034 val = 0;
4035 }
c906108c 4036 }
879d1e6b 4037
08351840
PA
4038 /* In some cases, we might not be able to remove a breakpoint in
4039 a shared library that has already been removed, but we have
4040 not yet processed the shlib unload event. Similarly for an
4041 unloaded add-symbol-file object - the user might not yet have
4042 had the chance to remove-symbol-file it. shlib_disabled will
4043 be set if the library/object has already been removed, but
4044 the breakpoint hasn't been uninserted yet, e.g., after
4045 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4046 always-inserted mode. */
076855f9 4047 if (val
08351840
PA
4048 && (bl->loc_type == bp_loc_software_breakpoint
4049 && (bl->shlib_disabled
4050 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4051 || shared_objfile_contains_address_p (bl->pspace,
4052 bl->address))))
879d1e6b
UW
4053 val = 0;
4054
c906108c
SS
4055 if (val)
4056 return val;
35df4500 4057 bl->inserted = (is == mark_inserted);
c906108c 4058 }
35df4500 4059 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4060 {
77b06cd7
TJB
4061 gdb_assert (bl->owner->ops != NULL
4062 && bl->owner->ops->remove_location != NULL);
4063
35df4500 4064 bl->inserted = (is == mark_inserted);
77b06cd7 4065 bl->owner->ops->remove_location (bl);
2e70b7b9 4066
c906108c 4067 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 4068 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4069 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4070 bl->owner->number);
c906108c 4071 }
35df4500
TJB
4072 else if (bl->owner->type == bp_catchpoint
4073 && breakpoint_enabled (bl->owner)
4074 && !bl->duplicate)
ce78b96d 4075 {
77b06cd7
TJB
4076 gdb_assert (bl->owner->ops != NULL
4077 && bl->owner->ops->remove_location != NULL);
ce78b96d 4078
77b06cd7 4079 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4080 if (val)
4081 return val;
77b06cd7 4082
35df4500 4083 bl->inserted = (is == mark_inserted);
ce78b96d 4084 }
c906108c
SS
4085
4086 return 0;
4087}
4088
6c95b8df 4089static int
35df4500 4090remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4091{
4092 int ret;
4093 struct cleanup *old_chain;
4094
35df4500
TJB
4095 /* BL is never in moribund_locations by our callers. */
4096 gdb_assert (bl->owner != NULL);
2bdf28a0 4097
6c95b8df
PA
4098 /* The type of none suggests that owner is actually deleted.
4099 This should not ever happen. */
35df4500 4100 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4101
4102 old_chain = save_current_space_and_thread ();
4103
35df4500 4104 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4105
35df4500 4106 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4107
4108 do_cleanups (old_chain);
4109 return ret;
4110}
4111
c906108c
SS
4112/* Clear the "inserted" flag in all breakpoints. */
4113
25b22b0a 4114void
fba45db2 4115mark_breakpoints_out (void)
c906108c 4116{
35df4500 4117 struct bp_location *bl, **blp_tmp;
c906108c 4118
35df4500 4119 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4120 if (bl->pspace == current_program_space)
35df4500 4121 bl->inserted = 0;
c906108c
SS
4122}
4123
53a5351d
JM
4124/* Clear the "inserted" flag in all breakpoints and delete any
4125 breakpoints which should go away between runs of the program.
c906108c
SS
4126
4127 Plus other such housekeeping that has to be done for breakpoints
4128 between runs.
4129
53a5351d
JM
4130 Note: this function gets called at the end of a run (by
4131 generic_mourn_inferior) and when a run begins (by
4a64f543 4132 init_wait_for_inferior). */
c906108c
SS
4133
4134
4135
4136void
fba45db2 4137breakpoint_init_inferior (enum inf_context context)
c906108c 4138{
35df4500
TJB
4139 struct breakpoint *b, *b_tmp;
4140 struct bp_location *bl, **blp_tmp;
1c5cfe86 4141 int ix;
6c95b8df 4142 struct program_space *pspace = current_program_space;
c906108c 4143
50c71eaf
PA
4144 /* If breakpoint locations are shared across processes, then there's
4145 nothing to do. */
f5656ead 4146 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4147 return;
4148
1a853c52 4149 mark_breakpoints_out ();
075f6582 4150
35df4500 4151 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4152 {
6c95b8df
PA
4153 if (b->loc && b->loc->pspace != pspace)
4154 continue;
4155
c5aa993b
JM
4156 switch (b->type)
4157 {
4158 case bp_call_dummy:
e2e4d78b 4159 case bp_longjmp_call_dummy:
c906108c 4160
c5aa993b 4161 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4162 cause problems when the inferior is rerun, so we better get
4163 rid of it. */
4164
4165 case bp_watchpoint_scope:
4166
4167 /* Also get rid of scope breakpoints. */
4168
4169 case bp_shlib_event:
4170
4171 /* Also remove solib event breakpoints. Their addresses may
4172 have changed since the last time we ran the program.
4173 Actually we may now be debugging against different target;
4174 and so the solib backend that installed this breakpoint may
4175 not be used in by the target. E.g.,
4176
4177 (gdb) file prog-linux
4178 (gdb) run # native linux target
4179 ...
4180 (gdb) kill
4181 (gdb) file prog-win.exe
4182 (gdb) tar rem :9999 # remote Windows gdbserver.
4183 */
c906108c 4184
f59f708a
PA
4185 case bp_step_resume:
4186
4187 /* Also remove step-resume breakpoints. */
4188
7c16b83e
PA
4189 case bp_single_step:
4190
4191 /* Also remove single-step breakpoints. */
4192
c5aa993b
JM
4193 delete_breakpoint (b);
4194 break;
c906108c 4195
c5aa993b
JM
4196 case bp_watchpoint:
4197 case bp_hardware_watchpoint:
4198 case bp_read_watchpoint:
4199 case bp_access_watchpoint:
3a5c3e22
PA
4200 {
4201 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4202
3a5c3e22
PA
4203 /* Likewise for watchpoints on local expressions. */
4204 if (w->exp_valid_block != NULL)
4205 delete_breakpoint (b);
4206 else if (context == inf_starting)
4207 {
4208 /* Reset val field to force reread of starting value in
4209 insert_breakpoints. */
4210 if (w->val)
4211 value_free (w->val);
4212 w->val = NULL;
4213 w->val_valid = 0;
c860120c 4214 }
3a5c3e22 4215 }
c5aa993b
JM
4216 break;
4217 default:
c5aa993b
JM
4218 break;
4219 }
4220 }
1c5cfe86
PA
4221
4222 /* Get rid of the moribund locations. */
35df4500
TJB
4223 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4224 decref_bp_location (&bl);
1c5cfe86 4225 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4226}
4227
6c95b8df
PA
4228/* These functions concern about actual breakpoints inserted in the
4229 target --- to e.g. check if we need to do decr_pc adjustment or if
4230 we need to hop over the bkpt --- so we check for address space
4231 match, not program space. */
4232
c2c6d25f
JM
4233/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4234 exists at PC. It returns ordinary_breakpoint_here if it's an
4235 ordinary breakpoint, or permanent_breakpoint_here if it's a
4236 permanent breakpoint.
4237 - When continuing from a location with an ordinary breakpoint, we
4238 actually single step once before calling insert_breakpoints.
e5dd4106 4239 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4240 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4241 the target, to advance the PC past the breakpoint. */
c906108c 4242
c2c6d25f 4243enum breakpoint_here
6c95b8df 4244breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4245{
35df4500 4246 struct bp_location *bl, **blp_tmp;
c2c6d25f 4247 int any_breakpoint_here = 0;
c906108c 4248
35df4500 4249 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4250 {
35df4500
TJB
4251 if (bl->loc_type != bp_loc_software_breakpoint
4252 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4253 continue;
4254
f1310107 4255 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4256 if ((breakpoint_enabled (bl->owner)
1a853c52 4257 || bl->permanent)
f1310107 4258 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4259 {
4260 if (overlay_debugging
35df4500
TJB
4261 && section_is_overlay (bl->section)
4262 && !section_is_mapped (bl->section))
075f6582 4263 continue; /* unmapped overlay -- can't be a match */
1a853c52 4264 else if (bl->permanent)
075f6582
DJ
4265 return permanent_breakpoint_here;
4266 else
4267 any_breakpoint_here = 1;
4268 }
4269 }
c906108c 4270
f486487f 4271 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4272}
4273
d35ae833
PA
4274/* See breakpoint.h. */
4275
4276int
4277breakpoint_in_range_p (struct address_space *aspace,
4278 CORE_ADDR addr, ULONGEST len)
4279{
4280 struct bp_location *bl, **blp_tmp;
4281
4282 ALL_BP_LOCATIONS (bl, blp_tmp)
4283 {
4284 if (bl->loc_type != bp_loc_software_breakpoint
4285 && bl->loc_type != bp_loc_hardware_breakpoint)
4286 continue;
4287
4288 if ((breakpoint_enabled (bl->owner)
4289 || bl->permanent)
4290 && breakpoint_location_address_range_overlap (bl, aspace,
4291 addr, len))
4292 {
4293 if (overlay_debugging
4294 && section_is_overlay (bl->section)
4295 && !section_is_mapped (bl->section))
4296 {
4297 /* Unmapped overlay -- can't be a match. */
4298 continue;
4299 }
4300
4301 return 1;
4302 }
4303 }
4304
4305 return 0;
4306}
4307
1c5cfe86
PA
4308/* Return true if there's a moribund breakpoint at PC. */
4309
4310int
6c95b8df 4311moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4312{
4313 struct bp_location *loc;
4314 int ix;
4315
4316 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4317 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4318 return 1;
4319
4320 return 0;
4321}
c2c6d25f 4322
f7ce857f
PA
4323/* Returns non-zero iff BL is inserted at PC, in address space
4324 ASPACE. */
4325
4326static int
4327bp_location_inserted_here_p (struct bp_location *bl,
4328 struct address_space *aspace, CORE_ADDR pc)
4329{
4330 if (bl->inserted
4331 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4332 aspace, pc))
4333 {
4334 if (overlay_debugging
4335 && section_is_overlay (bl->section)
4336 && !section_is_mapped (bl->section))
4337 return 0; /* unmapped overlay -- can't be a match */
4338 else
4339 return 1;
4340 }
4341 return 0;
4342}
4343
a1fd2fa5 4344/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4345
4346int
a1fd2fa5 4347breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4348{
f7ce857f
PA
4349 struct bp_location **blp, **blp_tmp = NULL;
4350 struct bp_location *bl;
c906108c 4351
f7ce857f 4352 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4353 {
f7ce857f
PA
4354 struct bp_location *bl = *blp;
4355
35df4500
TJB
4356 if (bl->loc_type != bp_loc_software_breakpoint
4357 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4358 continue;
4359
f7ce857f
PA
4360 if (bp_location_inserted_here_p (bl, aspace, pc))
4361 return 1;
c5aa993b 4362 }
c36b740a
VP
4363 return 0;
4364}
4365
a1fd2fa5
PA
4366/* This function returns non-zero iff there is a software breakpoint
4367 inserted at PC. */
c36b740a
VP
4368
4369int
a1fd2fa5
PA
4370software_breakpoint_inserted_here_p (struct address_space *aspace,
4371 CORE_ADDR pc)
4fa8626c 4372{
f7ce857f
PA
4373 struct bp_location **blp, **blp_tmp = NULL;
4374 struct bp_location *bl;
4fa8626c 4375
f7ce857f 4376 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4377 {
f7ce857f
PA
4378 struct bp_location *bl = *blp;
4379
35df4500 4380 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4381 continue;
4382
f7ce857f
PA
4383 if (bp_location_inserted_here_p (bl, aspace, pc))
4384 return 1;
4fa8626c
DJ
4385 }
4386
4387 return 0;
9c02b525
PA
4388}
4389
4390/* See breakpoint.h. */
4391
4392int
4393hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4394 CORE_ADDR pc)
4395{
4396 struct bp_location **blp, **blp_tmp = NULL;
4397 struct bp_location *bl;
4398
4399 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4400 {
4401 struct bp_location *bl = *blp;
4402
4403 if (bl->loc_type != bp_loc_hardware_breakpoint)
4404 continue;
4405
4406 if (bp_location_inserted_here_p (bl, aspace, pc))
4407 return 1;
4408 }
4409
4410 return 0;
4fa8626c
DJ
4411}
4412
9093389c
PA
4413int
4414hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4415 CORE_ADDR addr, ULONGEST len)
4416{
4417 struct breakpoint *bpt;
4418
4419 ALL_BREAKPOINTS (bpt)
4420 {
4421 struct bp_location *loc;
4422
4423 if (bpt->type != bp_hardware_watchpoint
4424 && bpt->type != bp_access_watchpoint)
4425 continue;
4426
4427 if (!breakpoint_enabled (bpt))
4428 continue;
4429
4430 for (loc = bpt->loc; loc; loc = loc->next)
4431 if (loc->pspace->aspace == aspace && loc->inserted)
4432 {
4433 CORE_ADDR l, h;
4434
4435 /* Check for intersection. */
4436 l = max (loc->address, addr);
4437 h = min (loc->address + loc->length, addr + len);
4438 if (l < h)
4439 return 1;
4440 }
4441 }
4442 return 0;
4443}
c906108c 4444\f
c5aa993b 4445
c906108c
SS
4446/* bpstat stuff. External routines' interfaces are documented
4447 in breakpoint.h. */
4448
4449int
c326b90e 4450is_catchpoint (struct breakpoint *ep)
c906108c 4451{
533be4dd 4452 return (ep->type == bp_catchpoint);
c906108c
SS
4453}
4454
f431efe5
PA
4455/* Frees any storage that is part of a bpstat. Does not walk the
4456 'next' chain. */
4457
4458static void
198757a8
VP
4459bpstat_free (bpstat bs)
4460{
4461 if (bs->old_val != NULL)
4462 value_free (bs->old_val);
9add0f1b 4463 decref_counted_command_line (&bs->commands);
f431efe5 4464 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4465 xfree (bs);
4466}
4467
c906108c
SS
4468/* Clear a bpstat so that it says we are not at any breakpoint.
4469 Also free any storage that is part of a bpstat. */
4470
4471void
fba45db2 4472bpstat_clear (bpstat *bsp)
c906108c
SS
4473{
4474 bpstat p;
4475 bpstat q;
4476
4477 if (bsp == 0)
4478 return;
4479 p = *bsp;
4480 while (p != NULL)
4481 {
4482 q = p->next;
198757a8 4483 bpstat_free (p);
c906108c
SS
4484 p = q;
4485 }
4486 *bsp = NULL;
4487}
4488
4489/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4490 is part of the bpstat is copied as well. */
4491
4492bpstat
fba45db2 4493bpstat_copy (bpstat bs)
c906108c
SS
4494{
4495 bpstat p = NULL;
4496 bpstat tmp;
4497 bpstat retval = NULL;
4498
4499 if (bs == NULL)
4500 return bs;
4501
4502 for (; bs != NULL; bs = bs->next)
4503 {
4504 tmp = (bpstat) xmalloc (sizeof (*tmp));
4505 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4506 incref_counted_command_line (tmp->commands);
f431efe5 4507 incref_bp_location (tmp->bp_location_at);
31cc81e9 4508 if (bs->old_val != NULL)
3c3185ac
JK
4509 {
4510 tmp->old_val = value_copy (bs->old_val);
4511 release_value (tmp->old_val);
4512 }
31cc81e9 4513
c906108c
SS
4514 if (p == NULL)
4515 /* This is the first thing in the chain. */
4516 retval = tmp;
4517 else
4518 p->next = tmp;
4519 p = tmp;
4520 }
4521 p->next = NULL;
4522 return retval;
4523}
4524
4a64f543 4525/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4526
4527bpstat
fba45db2 4528bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4529{
c5aa993b
JM
4530 if (bsp == NULL)
4531 return NULL;
c906108c 4532
c5aa993b
JM
4533 for (; bsp != NULL; bsp = bsp->next)
4534 {
f431efe5 4535 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4536 return bsp;
4537 }
c906108c
SS
4538 return NULL;
4539}
4540
ab04a2af
TT
4541/* See breakpoint.h. */
4542
47591c29 4543int
427cd150 4544bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4545{
ab04a2af
TT
4546 for (; bsp != NULL; bsp = bsp->next)
4547 {
427cd150
TT
4548 if (bsp->breakpoint_at == NULL)
4549 {
4550 /* A moribund location can never explain a signal other than
4551 GDB_SIGNAL_TRAP. */
4552 if (sig == GDB_SIGNAL_TRAP)
47591c29 4553 return 1;
427cd150
TT
4554 }
4555 else
47591c29
PA
4556 {
4557 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4558 sig))
4559 return 1;
4560 }
ab04a2af
TT
4561 }
4562
47591c29 4563 return 0;
ab04a2af
TT
4564}
4565
4a64f543
MS
4566/* Put in *NUM the breakpoint number of the first breakpoint we are
4567 stopped at. *BSP upon return is a bpstat which points to the
4568 remaining breakpoints stopped at (but which is not guaranteed to be
4569 good for anything but further calls to bpstat_num).
4570
8671a17b
PA
4571 Return 0 if passed a bpstat which does not indicate any breakpoints.
4572 Return -1 if stopped at a breakpoint that has been deleted since
4573 we set it.
4574 Return 1 otherwise. */
c906108c
SS
4575
4576int
8671a17b 4577bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4578{
4579 struct breakpoint *b;
4580
4581 if ((*bsp) == NULL)
4582 return 0; /* No more breakpoint values */
8671a17b 4583
4a64f543
MS
4584 /* We assume we'll never have several bpstats that correspond to a
4585 single breakpoint -- otherwise, this function might return the
4586 same number more than once and this will look ugly. */
f431efe5 4587 b = (*bsp)->breakpoint_at;
8671a17b
PA
4588 *bsp = (*bsp)->next;
4589 if (b == NULL)
4590 return -1; /* breakpoint that's been deleted since */
4591
4592 *num = b->number; /* We have its number */
4593 return 1;
c906108c
SS
4594}
4595
e93ca019 4596/* See breakpoint.h. */
c906108c
SS
4597
4598void
e93ca019 4599bpstat_clear_actions (void)
c906108c 4600{
e93ca019
JK
4601 struct thread_info *tp;
4602 bpstat bs;
4603
4604 if (ptid_equal (inferior_ptid, null_ptid))
4605 return;
4606
4607 tp = find_thread_ptid (inferior_ptid);
4608 if (tp == NULL)
4609 return;
4610
4611 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4612 {
9add0f1b 4613 decref_counted_command_line (&bs->commands);
abf85f46 4614
c906108c
SS
4615 if (bs->old_val != NULL)
4616 {
4617 value_free (bs->old_val);
4618 bs->old_val = NULL;
4619 }
4620 }
4621}
4622
f3b1572e
PA
4623/* Called when a command is about to proceed the inferior. */
4624
4625static void
4626breakpoint_about_to_proceed (void)
4627{
4628 if (!ptid_equal (inferior_ptid, null_ptid))
4629 {
4630 struct thread_info *tp = inferior_thread ();
4631
4632 /* Allow inferior function calls in breakpoint commands to not
4633 interrupt the command list. When the call finishes
4634 successfully, the inferior will be standing at the same
4635 breakpoint as if nothing happened. */
16c381f0 4636 if (tp->control.in_infcall)
f3b1572e
PA
4637 return;
4638 }
4639
4640 breakpoint_proceeded = 1;
4641}
4642
4a64f543
MS
4643/* Stub for cleaning up our state if we error-out of a breakpoint
4644 command. */
c906108c 4645static void
4efb68b1 4646cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4647{
4648 executing_breakpoint_commands = 0;
4649}
4650
abf85f46
JK
4651/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4652 or its equivalent. */
4653
4654static int
4655command_line_is_silent (struct command_line *cmd)
4656{
4f45d445 4657 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4658}
4659
4a64f543
MS
4660/* Execute all the commands associated with all the breakpoints at
4661 this location. Any of these commands could cause the process to
4662 proceed beyond this point, etc. We look out for such changes by
4663 checking the global "breakpoint_proceeded" after each command.
c906108c 4664
347bddb7
PA
4665 Returns true if a breakpoint command resumed the inferior. In that
4666 case, it is the caller's responsibility to recall it again with the
4667 bpstat of the current thread. */
4668
4669static int
4670bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4671{
4672 bpstat bs;
4673 struct cleanup *old_chain;
347bddb7 4674 int again = 0;
c906108c
SS
4675
4676 /* Avoid endless recursion if a `source' command is contained
4677 in bs->commands. */
4678 if (executing_breakpoint_commands)
347bddb7 4679 return 0;
c906108c
SS
4680
4681 executing_breakpoint_commands = 1;
4682 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4683
cf6c5ffb
TT
4684 prevent_dont_repeat ();
4685
4a64f543 4686 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4687 bs = *bsp;
4688
4689 breakpoint_proceeded = 0;
4690 for (; bs != NULL; bs = bs->next)
4691 {
9add0f1b 4692 struct counted_command_line *ccmd;
6c50ab1c
JB
4693 struct command_line *cmd;
4694 struct cleanup *this_cmd_tree_chain;
4695
4696 /* Take ownership of the BSP's command tree, if it has one.
4697
4698 The command tree could legitimately contain commands like
4699 'step' and 'next', which call clear_proceed_status, which
4700 frees stop_bpstat's command tree. To make sure this doesn't
4701 free the tree we're executing out from under us, we need to
4702 take ownership of the tree ourselves. Since a given bpstat's
4703 commands are only executed once, we don't need to copy it; we
4704 can clear the pointer in the bpstat, and make sure we free
4705 the tree when we're done. */
9add0f1b
TT
4706 ccmd = bs->commands;
4707 bs->commands = NULL;
abf85f46
JK
4708 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4709 cmd = ccmd ? ccmd->commands : NULL;
4710 if (command_line_is_silent (cmd))
4711 {
4712 /* The action has been already done by bpstat_stop_status. */
4713 cmd = cmd->next;
4714 }
6c50ab1c 4715
c906108c
SS
4716 while (cmd != NULL)
4717 {
4718 execute_control_command (cmd);
4719
4720 if (breakpoint_proceeded)
4721 break;
4722 else
4723 cmd = cmd->next;
4724 }
6c50ab1c
JB
4725
4726 /* We can free this command tree now. */
4727 do_cleanups (this_cmd_tree_chain);
4728
c906108c 4729 if (breakpoint_proceeded)
32c1e744 4730 {
0b333c5e 4731 if (interpreter_async)
347bddb7
PA
4732 /* If we are in async mode, then the target might be still
4733 running, not stopped at any breakpoint, so nothing for
4734 us to do here -- just return to the event loop. */
4735 ;
32c1e744
VP
4736 else
4737 /* In sync mode, when execute_control_command returns
4738 we're already standing on the next breakpoint.
347bddb7
PA
4739 Breakpoint commands for that stop were not run, since
4740 execute_command does not run breakpoint commands --
4741 only command_line_handler does, but that one is not
4742 involved in execution of breakpoint commands. So, we
4743 can now execute breakpoint commands. It should be
4744 noted that making execute_command do bpstat actions is
4745 not an option -- in this case we'll have recursive
4746 invocation of bpstat for each breakpoint with a
4747 command, and can easily blow up GDB stack. Instead, we
4748 return true, which will trigger the caller to recall us
4749 with the new stop_bpstat. */
4750 again = 1;
4751 break;
32c1e744 4752 }
c906108c 4753 }
c2b8ed2c 4754 do_cleanups (old_chain);
347bddb7
PA
4755 return again;
4756}
4757
4758void
4759bpstat_do_actions (void)
4760{
353d1d73
JK
4761 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4762
347bddb7
PA
4763 /* Do any commands attached to breakpoint we are stopped at. */
4764 while (!ptid_equal (inferior_ptid, null_ptid)
4765 && target_has_execution
4766 && !is_exited (inferior_ptid)
4767 && !is_executing (inferior_ptid))
4768 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4769 and only return when it is stopped at the next breakpoint, we
4770 keep doing breakpoint actions until it returns false to
4771 indicate the inferior was not resumed. */
16c381f0 4772 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4773 break;
353d1d73
JK
4774
4775 discard_cleanups (cleanup_if_error);
c906108c
SS
4776}
4777
fa4727a6
DJ
4778/* Print out the (old or new) value associated with a watchpoint. */
4779
4780static void
4781watchpoint_value_print (struct value *val, struct ui_file *stream)
4782{
4783 if (val == NULL)
4784 fprintf_unfiltered (stream, _("<unreadable>"));
4785 else
79a45b7d
TT
4786 {
4787 struct value_print_options opts;
4788 get_user_print_options (&opts);
4789 value_print (val, stream, &opts);
4790 }
fa4727a6
DJ
4791}
4792
f303dbd6
PA
4793/* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4794 debugging multiple threads. */
4795
4796void
4797maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4798{
4799 if (ui_out_is_mi_like_p (uiout))
4800 return;
4801
4802 ui_out_text (uiout, "\n");
4803
4804 if (show_thread_that_caused_stop ())
4805 {
4806 const char *name;
4807 struct thread_info *thr = inferior_thread ();
4808
4809 ui_out_text (uiout, "Thread ");
4810 ui_out_field_fmt (uiout, "thread-id", "%s", print_thread_id (thr));
4811
4812 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4813 if (name != NULL)
4814 {
4815 ui_out_text (uiout, " \"");
4816 ui_out_field_fmt (uiout, "name", "%s", name);
4817 ui_out_text (uiout, "\"");
4818 }
4819
4820 ui_out_text (uiout, " hit ");
4821 }
4822}
4823
e514a9d6 4824/* Generic routine for printing messages indicating why we
4a64f543 4825 stopped. The behavior of this function depends on the value
e514a9d6
JM
4826 'print_it' in the bpstat structure. Under some circumstances we
4827 may decide not to print anything here and delegate the task to
4a64f543 4828 normal_stop(). */
e514a9d6
JM
4829
4830static enum print_stop_action
4831print_bp_stop_message (bpstat bs)
4832{
4833 switch (bs->print_it)
4834 {
4835 case print_it_noop:
4a64f543 4836 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4837 return PRINT_UNKNOWN;
4838 break;
4839
4840 case print_it_done:
4841 /* We still want to print the frame, but we already printed the
4a64f543 4842 relevant messages. */
e514a9d6
JM
4843 return PRINT_SRC_AND_LOC;
4844 break;
4845
4846 case print_it_normal:
4f8d1dc6 4847 {
f431efe5
PA
4848 struct breakpoint *b = bs->breakpoint_at;
4849
1a6a67de
TJB
4850 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4851 which has since been deleted. */
4852 if (b == NULL)
4853 return PRINT_UNKNOWN;
4854
348d480f
PA
4855 /* Normal case. Call the breakpoint's print_it method. */
4856 return b->ops->print_it (bs);
4f8d1dc6 4857 }
348d480f 4858 break;
3086aeae 4859
e514a9d6 4860 default:
8e65ff28 4861 internal_error (__FILE__, __LINE__,
e2e0b3e5 4862 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4863 break;
c906108c 4864 }
c906108c
SS
4865}
4866
edcc5120
TT
4867/* A helper function that prints a shared library stopped event. */
4868
4869static void
4870print_solib_event (int is_catchpoint)
4871{
4872 int any_deleted
4873 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4874 int any_added
4875 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4876
4877 if (!is_catchpoint)
4878 {
4879 if (any_added || any_deleted)
4880 ui_out_text (current_uiout,
4881 _("Stopped due to shared library event:\n"));
4882 else
4883 ui_out_text (current_uiout,
4884 _("Stopped due to shared library event (no "
4885 "libraries added or removed)\n"));
4886 }
4887
4888 if (ui_out_is_mi_like_p (current_uiout))
4889 ui_out_field_string (current_uiout, "reason",
4890 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4891
4892 if (any_deleted)
4893 {
4894 struct cleanup *cleanup;
4895 char *name;
4896 int ix;
4897
4898 ui_out_text (current_uiout, _(" Inferior unloaded "));
4899 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4900 "removed");
4901 for (ix = 0;
4902 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4903 ix, name);
4904 ++ix)
4905 {
4906 if (ix > 0)
4907 ui_out_text (current_uiout, " ");
4908 ui_out_field_string (current_uiout, "library", name);
4909 ui_out_text (current_uiout, "\n");
4910 }
4911
4912 do_cleanups (cleanup);
4913 }
4914
4915 if (any_added)
4916 {
4917 struct so_list *iter;
4918 int ix;
4919 struct cleanup *cleanup;
4920
4921 ui_out_text (current_uiout, _(" Inferior loaded "));
4922 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4923 "added");
4924 for (ix = 0;
4925 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4926 ix, iter);
4927 ++ix)
4928 {
4929 if (ix > 0)
4930 ui_out_text (current_uiout, " ");
4931 ui_out_field_string (current_uiout, "library", iter->so_name);
4932 ui_out_text (current_uiout, "\n");
4933 }
4934
4935 do_cleanups (cleanup);
4936 }
4937}
4938
e514a9d6
JM
4939/* Print a message indicating what happened. This is called from
4940 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4941 list - a list of the eventpoints that caused this stop. KIND is
4942 the target_waitkind for the stopping event. This
e514a9d6
JM
4943 routine calls the generic print routine for printing a message
4944 about reasons for stopping. This will print (for example) the
4945 "Breakpoint n," part of the output. The return value of this
4946 routine is one of:
c906108c 4947
4a64f543 4948 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4949 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4950 code to print the location. An example is
c5aa993b
JM
4951 "Breakpoint 1, " which should be followed by
4952 the location.
917317f4 4953 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4954 to also print the location part of the message.
4955 An example is the catch/throw messages, which
4a64f543 4956 don't require a location appended to the end.
917317f4 4957 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4958 further info to be printed. */
c906108c 4959
917317f4 4960enum print_stop_action
36dfb11c 4961bpstat_print (bpstat bs, int kind)
c906108c 4962{
f486487f 4963 enum print_stop_action val;
c5aa993b 4964
c906108c 4965 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4966 (Currently all watchpoints go on the bpstat whether hit or not.
4967 That probably could (should) be changed, provided care is taken
c906108c 4968 with respect to bpstat_explains_signal). */
e514a9d6
JM
4969 for (; bs; bs = bs->next)
4970 {
4971 val = print_bp_stop_message (bs);
4972 if (val == PRINT_SRC_ONLY
4973 || val == PRINT_SRC_AND_LOC
4974 || val == PRINT_NOTHING)
4975 return val;
4976 }
c906108c 4977
36dfb11c
TT
4978 /* If we had hit a shared library event breakpoint,
4979 print_bp_stop_message would print out this message. If we hit an
4980 OS-level shared library event, do the same thing. */
4981 if (kind == TARGET_WAITKIND_LOADED)
4982 {
edcc5120 4983 print_solib_event (0);
36dfb11c
TT
4984 return PRINT_NOTHING;
4985 }
4986
e514a9d6 4987 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4988 with and nothing was printed. */
917317f4 4989 return PRINT_UNKNOWN;
c906108c
SS
4990}
4991
c42bd95a
DE
4992/* Evaluate the expression EXP and return 1 if value is zero.
4993 This returns the inverse of the condition because it is called
4994 from catch_errors which returns 0 if an exception happened, and if an
4995 exception happens we want execution to stop.
4a64f543 4996 The argument is a "struct expression *" that has been cast to a
c42bd95a 4997 "void *" to make it pass through catch_errors. */
c906108c
SS
4998
4999static int
4efb68b1 5000breakpoint_cond_eval (void *exp)
c906108c 5001{
278cd55f 5002 struct value *mark = value_mark ();
c5aa993b 5003 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 5004
c906108c
SS
5005 value_free_to_mark (mark);
5006 return i;
5007}
5008
5760d0ab 5009/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
5010
5011static bpstat
5760d0ab 5012bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
5013{
5014 bpstat bs;
5015
5016 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
5017 bs->next = NULL;
5018 **bs_link_pointer = bs;
5019 *bs_link_pointer = &bs->next;
f431efe5
PA
5020 bs->breakpoint_at = bl->owner;
5021 bs->bp_location_at = bl;
5022 incref_bp_location (bl);
c906108c
SS
5023 /* If the condition is false, etc., don't do the commands. */
5024 bs->commands = NULL;
5025 bs->old_val = NULL;
5026 bs->print_it = print_it_normal;
5027 return bs;
5028}
5029\f
d983da9c
DJ
5030/* The target has stopped with waitstatus WS. Check if any hardware
5031 watchpoints have triggered, according to the target. */
5032
5033int
5034watchpoints_triggered (struct target_waitstatus *ws)
5035{
d92524f1 5036 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
5037 CORE_ADDR addr;
5038 struct breakpoint *b;
5039
5040 if (!stopped_by_watchpoint)
5041 {
5042 /* We were not stopped by a watchpoint. Mark all watchpoints
5043 as not triggered. */
5044 ALL_BREAKPOINTS (b)
cc60f2e3 5045 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5046 {
5047 struct watchpoint *w = (struct watchpoint *) b;
5048
5049 w->watchpoint_triggered = watch_triggered_no;
5050 }
d983da9c
DJ
5051
5052 return 0;
5053 }
5054
5055 if (!target_stopped_data_address (&current_target, &addr))
5056 {
5057 /* We were stopped by a watchpoint, but we don't know where.
5058 Mark all watchpoints as unknown. */
5059 ALL_BREAKPOINTS (b)
cc60f2e3 5060 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5061 {
5062 struct watchpoint *w = (struct watchpoint *) b;
5063
5064 w->watchpoint_triggered = watch_triggered_unknown;
5065 }
d983da9c 5066
3c4797ba 5067 return 1;
d983da9c
DJ
5068 }
5069
5070 /* The target could report the data address. Mark watchpoints
5071 affected by this data address as triggered, and all others as not
5072 triggered. */
5073
5074 ALL_BREAKPOINTS (b)
cc60f2e3 5075 if (is_hardware_watchpoint (b))
d983da9c 5076 {
3a5c3e22 5077 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 5078 struct bp_location *loc;
d983da9c 5079
3a5c3e22 5080 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5081 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5082 {
3a5c3e22 5083 if (is_masked_watchpoint (b))
9c06b0b4 5084 {
3a5c3e22
PA
5085 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5086 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5087
5088 if (newaddr == start)
5089 {
3a5c3e22 5090 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5091 break;
5092 }
5093 }
5094 /* Exact match not required. Within range is sufficient. */
5095 else if (target_watchpoint_addr_within_range (&current_target,
5096 addr, loc->address,
5097 loc->length))
5098 {
3a5c3e22 5099 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5100 break;
5101 }
5102 }
d983da9c
DJ
5103 }
5104
5105 return 1;
5106}
5107
c906108c
SS
5108/* Possible return values for watchpoint_check (this can't be an enum
5109 because of check_errors). */
5110/* The watchpoint has been deleted. */
5111#define WP_DELETED 1
5112/* The value has changed. */
5113#define WP_VALUE_CHANGED 2
5114/* The value has not changed. */
5115#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5116/* Ignore this watchpoint, no matter if the value changed or not. */
5117#define WP_IGNORE 4
c906108c
SS
5118
5119#define BP_TEMPFLAG 1
5120#define BP_HARDWAREFLAG 2
5121
4a64f543
MS
5122/* Evaluate watchpoint condition expression and check if its value
5123 changed.
553e4c11
JB
5124
5125 P should be a pointer to struct bpstat, but is defined as a void *
5126 in order for this function to be usable with catch_errors. */
c906108c
SS
5127
5128static int
4efb68b1 5129watchpoint_check (void *p)
c906108c
SS
5130{
5131 bpstat bs = (bpstat) p;
3a5c3e22 5132 struct watchpoint *b;
c906108c
SS
5133 struct frame_info *fr;
5134 int within_current_scope;
5135
f431efe5 5136 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5137 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5138 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5139
f6bc2008
PA
5140 /* If this is a local watchpoint, we only want to check if the
5141 watchpoint frame is in scope if the current thread is the thread
5142 that was used to create the watchpoint. */
5143 if (!watchpoint_in_thread_scope (b))
60e1c644 5144 return WP_IGNORE;
f6bc2008 5145
c906108c
SS
5146 if (b->exp_valid_block == NULL)
5147 within_current_scope = 1;
5148 else
5149 {
edb3359d
DJ
5150 struct frame_info *frame = get_current_frame ();
5151 struct gdbarch *frame_arch = get_frame_arch (frame);
5152 CORE_ADDR frame_pc = get_frame_pc (frame);
5153
c9cf6e20 5154 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5155 still in the function but the stack frame has already been
5156 invalidated. Since we can't rely on the values of local
5157 variables after the stack has been destroyed, we are treating
5158 the watchpoint in that state as `not changed' without further
5159 checking. Don't mark watchpoints as changed if the current
5160 frame is in an epilogue - even if they are in some other
5161 frame, our view of the stack is likely to be wrong and
5162 frame_find_by_id could error out. */
c9cf6e20 5163 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5164 return WP_IGNORE;
a0f49112 5165
101dcfbe 5166 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5167 within_current_scope = (fr != NULL);
69fbadd5
DJ
5168
5169 /* If we've gotten confused in the unwinder, we might have
5170 returned a frame that can't describe this variable. */
edb3359d
DJ
5171 if (within_current_scope)
5172 {
5173 struct symbol *function;
5174
5175 function = get_frame_function (fr);
5176 if (function == NULL
5177 || !contained_in (b->exp_valid_block,
5178 SYMBOL_BLOCK_VALUE (function)))
5179 within_current_scope = 0;
5180 }
69fbadd5 5181
edb3359d 5182 if (within_current_scope)
c906108c
SS
5183 /* If we end up stopping, the current frame will get selected
5184 in normal_stop. So this call to select_frame won't affect
5185 the user. */
0f7d239c 5186 select_frame (fr);
c906108c 5187 }
c5aa993b 5188
c906108c
SS
5189 if (within_current_scope)
5190 {
4a64f543
MS
5191 /* We use value_{,free_to_}mark because it could be a *long*
5192 time before we return to the command level and call
5193 free_all_values. We can't call free_all_values because we
5194 might be in the middle of evaluating a function call. */
c906108c 5195
0cf6dd15 5196 int pc = 0;
9c06b0b4 5197 struct value *mark;
fa4727a6
DJ
5198 struct value *new_val;
5199
3a5c3e22 5200 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5201 /* Since we don't know the exact trigger address (from
5202 stopped_data_address), just tell the user we've triggered
5203 a mask watchpoint. */
5204 return WP_VALUE_CHANGED;
5205
5206 mark = value_mark ();
3a1115a0 5207 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5208
bb9d5f81
PP
5209 if (b->val_bitsize != 0)
5210 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5211
4a64f543
MS
5212 /* We use value_equal_contents instead of value_equal because
5213 the latter coerces an array to a pointer, thus comparing just
5214 the address of the array instead of its contents. This is
5215 not what we want. */
fa4727a6 5216 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5217 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5218 {
fa4727a6
DJ
5219 if (new_val != NULL)
5220 {
5221 release_value (new_val);
5222 value_free_to_mark (mark);
5223 }
c906108c
SS
5224 bs->old_val = b->val;
5225 b->val = new_val;
fa4727a6 5226 b->val_valid = 1;
c906108c
SS
5227 return WP_VALUE_CHANGED;
5228 }
5229 else
5230 {
60e1c644 5231 /* Nothing changed. */
c906108c 5232 value_free_to_mark (mark);
c906108c
SS
5233 return WP_VALUE_NOT_CHANGED;
5234 }
5235 }
5236 else
5237 {
79a45e25
PA
5238 struct ui_out *uiout = current_uiout;
5239
c906108c 5240 /* This seems like the only logical thing to do because
c5aa993b
JM
5241 if we temporarily ignored the watchpoint, then when
5242 we reenter the block in which it is valid it contains
5243 garbage (in the case of a function, it may have two
5244 garbage values, one before and one after the prologue).
5245 So we can't even detect the first assignment to it and
5246 watch after that (since the garbage may or may not equal
5247 the first value assigned). */
348d480f
PA
5248 /* We print all the stop information in
5249 breakpoint_ops->print_it, but in this case, by the time we
5250 call breakpoint_ops->print_it this bp will be deleted
5251 already. So we have no choice but print the information
5252 here. */
9dc5e2a9 5253 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5254 ui_out_field_string
5255 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5256 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5257 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5258 ui_out_text (uiout,
5259 " deleted because the program has left the block in\n\
8b93c638 5260which its expression is valid.\n");
4ce44c66 5261
cdac0397 5262 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5263 decref_counted_command_line (&b->base.commands);
d0fb5eae 5264 watchpoint_del_at_next_stop (b);
c906108c
SS
5265
5266 return WP_DELETED;
5267 }
5268}
5269
18a18393 5270/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5271 breakpoint location BL. This function does not check if we should
5272 stop, only if BL explains the stop. */
5273
18a18393 5274static int
6c95b8df 5275bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5276 struct address_space *aspace, CORE_ADDR bp_addr,
5277 const struct target_waitstatus *ws)
18a18393
VP
5278{
5279 struct breakpoint *b = bl->owner;
5280
348d480f 5281 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5282 gdb_assert (b != NULL);
5283
09ac7c10 5284 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5285}
5286
3a5c3e22
PA
5287/* Determine if the watched values have actually changed, and we
5288 should stop. If not, set BS->stop to 0. */
5289
18a18393
VP
5290static void
5291bpstat_check_watchpoint (bpstat bs)
5292{
2bdf28a0 5293 const struct bp_location *bl;
3a5c3e22 5294 struct watchpoint *b;
2bdf28a0
JK
5295
5296 /* BS is built for existing struct breakpoint. */
f431efe5 5297 bl = bs->bp_location_at;
2bdf28a0 5298 gdb_assert (bl != NULL);
3a5c3e22 5299 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5300 gdb_assert (b != NULL);
18a18393 5301
18a18393 5302 {
18a18393
VP
5303 int must_check_value = 0;
5304
3a5c3e22 5305 if (b->base.type == bp_watchpoint)
18a18393
VP
5306 /* For a software watchpoint, we must always check the
5307 watched value. */
5308 must_check_value = 1;
5309 else if (b->watchpoint_triggered == watch_triggered_yes)
5310 /* We have a hardware watchpoint (read, write, or access)
5311 and the target earlier reported an address watched by
5312 this watchpoint. */
5313 must_check_value = 1;
5314 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5315 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5316 /* We were stopped by a hardware watchpoint, but the target could
5317 not report the data address. We must check the watchpoint's
5318 value. Access and read watchpoints are out of luck; without
5319 a data address, we can't figure it out. */
5320 must_check_value = 1;
3a5c3e22 5321
18a18393
VP
5322 if (must_check_value)
5323 {
3e43a32a
MS
5324 char *message
5325 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5326 b->base.number);
18a18393
VP
5327 struct cleanup *cleanups = make_cleanup (xfree, message);
5328 int e = catch_errors (watchpoint_check, bs, message,
5329 RETURN_MASK_ALL);
5330 do_cleanups (cleanups);
5331 switch (e)
5332 {
5333 case WP_DELETED:
5334 /* We've already printed what needs to be printed. */
5335 bs->print_it = print_it_done;
5336 /* Stop. */
5337 break;
60e1c644
PA
5338 case WP_IGNORE:
5339 bs->print_it = print_it_noop;
5340 bs->stop = 0;
5341 break;
18a18393 5342 case WP_VALUE_CHANGED:
3a5c3e22 5343 if (b->base.type == bp_read_watchpoint)
18a18393 5344 {
85d721b8
PA
5345 /* There are two cases to consider here:
5346
4a64f543 5347 1. We're watching the triggered memory for reads.
85d721b8
PA
5348 In that case, trust the target, and always report
5349 the watchpoint hit to the user. Even though
5350 reads don't cause value changes, the value may
5351 have changed since the last time it was read, and
5352 since we're not trapping writes, we will not see
5353 those, and as such we should ignore our notion of
5354 old value.
5355
4a64f543 5356 2. We're watching the triggered memory for both
85d721b8
PA
5357 reads and writes. There are two ways this may
5358 happen:
5359
4a64f543 5360 2.1. This is a target that can't break on data
85d721b8
PA
5361 reads only, but can break on accesses (reads or
5362 writes), such as e.g., x86. We detect this case
5363 at the time we try to insert read watchpoints.
5364
4a64f543 5365 2.2. Otherwise, the target supports read
85d721b8
PA
5366 watchpoints, but, the user set an access or write
5367 watchpoint watching the same memory as this read
5368 watchpoint.
5369
5370 If we're watching memory writes as well as reads,
5371 ignore watchpoint hits when we find that the
5372 value hasn't changed, as reads don't cause
5373 changes. This still gives false positives when
5374 the program writes the same value to memory as
5375 what there was already in memory (we will confuse
5376 it for a read), but it's much better than
5377 nothing. */
5378
5379 int other_write_watchpoint = 0;
5380
5381 if (bl->watchpoint_type == hw_read)
5382 {
5383 struct breakpoint *other_b;
5384
5385 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5386 if (other_b->type == bp_hardware_watchpoint
5387 || other_b->type == bp_access_watchpoint)
85d721b8 5388 {
3a5c3e22
PA
5389 struct watchpoint *other_w =
5390 (struct watchpoint *) other_b;
5391
5392 if (other_w->watchpoint_triggered
5393 == watch_triggered_yes)
5394 {
5395 other_write_watchpoint = 1;
5396 break;
5397 }
85d721b8
PA
5398 }
5399 }
5400
5401 if (other_write_watchpoint
5402 || bl->watchpoint_type == hw_access)
5403 {
5404 /* We're watching the same memory for writes,
5405 and the value changed since the last time we
5406 updated it, so this trap must be for a write.
5407 Ignore it. */
5408 bs->print_it = print_it_noop;
5409 bs->stop = 0;
5410 }
18a18393
VP
5411 }
5412 break;
5413 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5414 if (b->base.type == bp_hardware_watchpoint
5415 || b->base.type == bp_watchpoint)
18a18393
VP
5416 {
5417 /* Don't stop: write watchpoints shouldn't fire if
5418 the value hasn't changed. */
5419 bs->print_it = print_it_noop;
5420 bs->stop = 0;
5421 }
5422 /* Stop. */
5423 break;
5424 default:
5425 /* Can't happen. */
5426 case 0:
5427 /* Error from catch_errors. */
3a5c3e22 5428 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5429 watchpoint_del_at_next_stop (b);
18a18393
VP
5430 /* We've already printed what needs to be printed. */
5431 bs->print_it = print_it_done;
5432 break;
5433 }
5434 }
5435 else /* must_check_value == 0 */
5436 {
5437 /* This is a case where some watchpoint(s) triggered, but
5438 not at the address of this watchpoint, or else no
5439 watchpoint triggered after all. So don't print
5440 anything for this watchpoint. */
5441 bs->print_it = print_it_noop;
5442 bs->stop = 0;
5443 }
5444 }
5445}
5446
7d4df6a4
DE
5447/* For breakpoints that are currently marked as telling gdb to stop,
5448 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5449 of breakpoint referred to by BS. If we should not stop for this
5450 breakpoint, set BS->stop to 0. */
f431efe5 5451
18a18393
VP
5452static void
5453bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5454{
2bdf28a0
JK
5455 const struct bp_location *bl;
5456 struct breakpoint *b;
7d4df6a4
DE
5457 int value_is_zero = 0;
5458 struct expression *cond;
5459
5460 gdb_assert (bs->stop);
2bdf28a0
JK
5461
5462 /* BS is built for existing struct breakpoint. */
f431efe5 5463 bl = bs->bp_location_at;
2bdf28a0 5464 gdb_assert (bl != NULL);
f431efe5 5465 b = bs->breakpoint_at;
2bdf28a0 5466 gdb_assert (b != NULL);
18a18393 5467
b775012e
LM
5468 /* Even if the target evaluated the condition on its end and notified GDB, we
5469 need to do so again since GDB does not know if we stopped due to a
5470 breakpoint or a single step breakpoint. */
5471
18a18393 5472 if (frame_id_p (b->frame_id)
edb3359d 5473 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5474 {
7d4df6a4
DE
5475 bs->stop = 0;
5476 return;
5477 }
60e1c644 5478
12ab52e9
PA
5479 /* If this is a thread/task-specific breakpoint, don't waste cpu
5480 evaluating the condition if this isn't the specified
5481 thread/task. */
5d5658a1 5482 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
12ab52e9
PA
5483 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5484
6c1b0f7b
DE
5485 {
5486 bs->stop = 0;
5487 return;
5488 }
5489
6dddc817
DE
5490 /* Evaluate extension language breakpoints that have a "stop" method
5491 implemented. */
5492 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5493
7d4df6a4
DE
5494 if (is_watchpoint (b))
5495 {
5496 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5497
7d4df6a4
DE
5498 cond = w->cond_exp;
5499 }
5500 else
5501 cond = bl->cond;
60e1c644 5502
7d4df6a4
DE
5503 if (cond && b->disposition != disp_del_at_next_stop)
5504 {
5505 int within_current_scope = 1;
5506 struct watchpoint * w;
60e1c644 5507
7d4df6a4
DE
5508 /* We use value_mark and value_free_to_mark because it could
5509 be a long time before we return to the command level and
5510 call free_all_values. We can't call free_all_values
5511 because we might be in the middle of evaluating a
5512 function call. */
5513 struct value *mark = value_mark ();
5514
5515 if (is_watchpoint (b))
5516 w = (struct watchpoint *) b;
5517 else
5518 w = NULL;
5519
5520 /* Need to select the frame, with all that implies so that
5521 the conditions will have the right context. Because we
5522 use the frame, we will not see an inlined function's
5523 variables when we arrive at a breakpoint at the start
5524 of the inlined function; the current frame will be the
5525 call site. */
5526 if (w == NULL || w->cond_exp_valid_block == NULL)
5527 select_frame (get_current_frame ());
5528 else
18a18393 5529 {
7d4df6a4
DE
5530 struct frame_info *frame;
5531
5532 /* For local watchpoint expressions, which particular
5533 instance of a local is being watched matters, so we
5534 keep track of the frame to evaluate the expression
5535 in. To evaluate the condition however, it doesn't
5536 really matter which instantiation of the function
5537 where the condition makes sense triggers the
5538 watchpoint. This allows an expression like "watch
5539 global if q > 10" set in `func', catch writes to
5540 global on all threads that call `func', or catch
5541 writes on all recursive calls of `func' by a single
5542 thread. We simply always evaluate the condition in
5543 the innermost frame that's executing where it makes
5544 sense to evaluate the condition. It seems
5545 intuitive. */
5546 frame = block_innermost_frame (w->cond_exp_valid_block);
5547 if (frame != NULL)
5548 select_frame (frame);
5549 else
5550 within_current_scope = 0;
18a18393 5551 }
7d4df6a4
DE
5552 if (within_current_scope)
5553 value_is_zero
5554 = catch_errors (breakpoint_cond_eval, cond,
5555 "Error in testing breakpoint condition:\n",
5556 RETURN_MASK_ALL);
5557 else
18a18393 5558 {
7d4df6a4
DE
5559 warning (_("Watchpoint condition cannot be tested "
5560 "in the current scope"));
5561 /* If we failed to set the right context for this
5562 watchpoint, unconditionally report it. */
5563 value_is_zero = 0;
18a18393 5564 }
7d4df6a4
DE
5565 /* FIXME-someday, should give breakpoint #. */
5566 value_free_to_mark (mark);
18a18393 5567 }
7d4df6a4
DE
5568
5569 if (cond && value_is_zero)
5570 {
5571 bs->stop = 0;
5572 }
7d4df6a4
DE
5573 else if (b->ignore_count > 0)
5574 {
5575 b->ignore_count--;
5576 bs->stop = 0;
5577 /* Increase the hit count even though we don't stop. */
5578 ++(b->hit_count);
5579 observer_notify_breakpoint_modified (b);
5580 }
18a18393
VP
5581}
5582
1cf4d951
PA
5583/* Returns true if we need to track moribund locations of LOC's type
5584 on the current target. */
5585
5586static int
5587need_moribund_for_location_type (struct bp_location *loc)
5588{
5589 return ((loc->loc_type == bp_loc_software_breakpoint
5590 && !target_supports_stopped_by_sw_breakpoint ())
5591 || (loc->loc_type == bp_loc_hardware_breakpoint
5592 && !target_supports_stopped_by_hw_breakpoint ()));
5593}
5594
18a18393 5595
9709f61c 5596/* Get a bpstat associated with having just stopped at address
d983da9c 5597 BP_ADDR in thread PTID.
c906108c 5598
d983da9c 5599 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5600 don't understand this stop. Result is a chain of bpstat's such
5601 that:
c906108c 5602
c5aa993b 5603 if we don't understand the stop, the result is a null pointer.
c906108c 5604
c5aa993b 5605 if we understand why we stopped, the result is not null.
c906108c 5606
c5aa993b
JM
5607 Each element of the chain refers to a particular breakpoint or
5608 watchpoint at which we have stopped. (We may have stopped for
5609 several reasons concurrently.)
c906108c 5610
c5aa993b
JM
5611 Each element of the chain has valid next, breakpoint_at,
5612 commands, FIXME??? fields. */
c906108c
SS
5613
5614bpstat
6c95b8df 5615bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5616 CORE_ADDR bp_addr, ptid_t ptid,
5617 const struct target_waitstatus *ws)
c906108c 5618{
0d381245 5619 struct breakpoint *b = NULL;
afe38095 5620 struct bp_location *bl;
20874c92 5621 struct bp_location *loc;
5760d0ab
JK
5622 /* First item of allocated bpstat's. */
5623 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5624 /* Pointer to the last thing in the chain currently. */
5760d0ab 5625 bpstat bs;
20874c92 5626 int ix;
429374b8 5627 int need_remove_insert;
f431efe5 5628 int removed_any;
c906108c 5629
f431efe5
PA
5630 /* First, build the bpstat chain with locations that explain a
5631 target stop, while being careful to not set the target running,
5632 as that may invalidate locations (in particular watchpoint
5633 locations are recreated). Resuming will happen here with
5634 breakpoint conditions or watchpoint expressions that include
5635 inferior function calls. */
c5aa993b 5636
429374b8
JK
5637 ALL_BREAKPOINTS (b)
5638 {
1a853c52 5639 if (!breakpoint_enabled (b))
429374b8 5640 continue;
a5606eee 5641
429374b8
JK
5642 for (bl = b->loc; bl != NULL; bl = bl->next)
5643 {
4a64f543
MS
5644 /* For hardware watchpoints, we look only at the first
5645 location. The watchpoint_check function will work on the
5646 entire expression, not the individual locations. For
5647 read watchpoints, the watchpoints_triggered function has
5648 checked all locations already. */
429374b8
JK
5649 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5650 break;
18a18393 5651
f6592439 5652 if (!bl->enabled || bl->shlib_disabled)
429374b8 5653 continue;
c5aa993b 5654
09ac7c10 5655 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5656 continue;
c5aa993b 5657
4a64f543
MS
5658 /* Come here if it's a watchpoint, or if the break address
5659 matches. */
c5aa993b 5660
4a64f543
MS
5661 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5662 explain stop. */
c5aa993b 5663
f431efe5
PA
5664 /* Assume we stop. Should we find a watchpoint that is not
5665 actually triggered, or if the condition of the breakpoint
5666 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5667 bs->stop = 1;
5668 bs->print = 1;
d983da9c 5669
f431efe5
PA
5670 /* If this is a scope breakpoint, mark the associated
5671 watchpoint as triggered so that we will handle the
5672 out-of-scope event. We'll get to the watchpoint next
5673 iteration. */
d0fb5eae 5674 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5675 {
5676 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5677
5678 w->watchpoint_triggered = watch_triggered_yes;
5679 }
f431efe5
PA
5680 }
5681 }
5682
7c16b83e 5683 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5684 if (!target_supports_stopped_by_sw_breakpoint ()
5685 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5686 {
1cf4d951 5687 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5688 {
1cf4d951
PA
5689 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5690 && need_moribund_for_location_type (loc))
5691 {
5692 bs = bpstat_alloc (loc, &bs_link);
5693 /* For hits of moribund locations, we should just proceed. */
5694 bs->stop = 0;
5695 bs->print = 0;
5696 bs->print_it = print_it_noop;
5697 }
f431efe5
PA
5698 }
5699 }
5700
edcc5120
TT
5701 /* A bit of special processing for shlib breakpoints. We need to
5702 process solib loading here, so that the lists of loaded and
5703 unloaded libraries are correct before we handle "catch load" and
5704 "catch unload". */
5705 for (bs = bs_head; bs != NULL; bs = bs->next)
5706 {
5d268276 5707 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5708 {
5709 handle_solib_event ();
5710 break;
5711 }
5712 }
5713
f431efe5
PA
5714 /* Now go through the locations that caused the target to stop, and
5715 check whether we're interested in reporting this stop to higher
5716 layers, or whether we should resume the target transparently. */
5717
5718 removed_any = 0;
5719
5760d0ab 5720 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5721 {
5722 if (!bs->stop)
5723 continue;
5724
f431efe5 5725 b = bs->breakpoint_at;
348d480f
PA
5726 b->ops->check_status (bs);
5727 if (bs->stop)
28010a5d 5728 {
348d480f 5729 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5730
429374b8
JK
5731 if (bs->stop)
5732 {
5733 ++(b->hit_count);
8d3788bd 5734 observer_notify_breakpoint_modified (b);
c906108c 5735
4a64f543 5736 /* We will stop here. */
429374b8
JK
5737 if (b->disposition == disp_disable)
5738 {
816338b5 5739 --(b->enable_count);
1a853c52 5740 if (b->enable_count <= 0)
429374b8 5741 b->enable_state = bp_disabled;
f431efe5 5742 removed_any = 1;
429374b8
JK
5743 }
5744 if (b->silent)
5745 bs->print = 0;
5746 bs->commands = b->commands;
9add0f1b 5747 incref_counted_command_line (bs->commands);
abf85f46
JK
5748 if (command_line_is_silent (bs->commands
5749 ? bs->commands->commands : NULL))
5750 bs->print = 0;
9d6e6e84
HZ
5751
5752 b->ops->after_condition_true (bs);
429374b8
JK
5753 }
5754
348d480f 5755 }
a9b3a50f
PA
5756
5757 /* Print nothing for this entry if we don't stop or don't
5758 print. */
5759 if (!bs->stop || !bs->print)
5760 bs->print_it = print_it_noop;
429374b8 5761 }
876fa593 5762
d983da9c
DJ
5763 /* If we aren't stopping, the value of some hardware watchpoint may
5764 not have changed, but the intermediate memory locations we are
5765 watching may have. Don't bother if we're stopping; this will get
5766 done later. */
d832cb68 5767 need_remove_insert = 0;
5760d0ab
JK
5768 if (! bpstat_causes_stop (bs_head))
5769 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5770 if (!bs->stop
f431efe5
PA
5771 && bs->breakpoint_at
5772 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5773 {
3a5c3e22
PA
5774 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5775
5776 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5777 need_remove_insert = 1;
d983da9c
DJ
5778 }
5779
d832cb68 5780 if (need_remove_insert)
44702360 5781 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5782 else if (removed_any)
44702360 5783 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5784
5760d0ab 5785 return bs_head;
c906108c 5786}
628fe4e4
JK
5787
5788static void
5789handle_jit_event (void)
5790{
5791 struct frame_info *frame;
5792 struct gdbarch *gdbarch;
5793
243a9253
PA
5794 if (debug_infrun)
5795 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5796
628fe4e4
JK
5797 /* Switch terminal for any messages produced by
5798 breakpoint_re_set. */
5799 target_terminal_ours_for_output ();
5800
5801 frame = get_current_frame ();
5802 gdbarch = get_frame_arch (frame);
5803
5804 jit_event_handler (gdbarch);
5805
5806 target_terminal_inferior ();
5807}
5808
5809/* Prepare WHAT final decision for infrun. */
5810
5811/* Decide what infrun needs to do with this bpstat. */
5812
c906108c 5813struct bpstat_what
0e30163f 5814bpstat_what (bpstat bs_head)
c906108c 5815{
c906108c 5816 struct bpstat_what retval;
628fe4e4 5817 int jit_event = 0;
0e30163f 5818 bpstat bs;
c906108c 5819
628fe4e4 5820 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5821 retval.call_dummy = STOP_NONE;
186c406b 5822 retval.is_longjmp = 0;
628fe4e4 5823
0e30163f 5824 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5825 {
628fe4e4
JK
5826 /* Extract this BS's action. After processing each BS, we check
5827 if its action overrides all we've seem so far. */
5828 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5829 enum bptype bptype;
5830
c906108c 5831 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5832 {
5833 /* I suspect this can happen if it was a momentary
5834 breakpoint which has since been deleted. */
5835 bptype = bp_none;
5836 }
20874c92 5837 else
f431efe5 5838 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5839
5840 switch (bptype)
c906108c
SS
5841 {
5842 case bp_none:
628fe4e4 5843 break;
c906108c
SS
5844 case bp_breakpoint:
5845 case bp_hardware_breakpoint:
7c16b83e 5846 case bp_single_step:
c906108c
SS
5847 case bp_until:
5848 case bp_finish:
a9b3a50f 5849 case bp_shlib_event:
c906108c
SS
5850 if (bs->stop)
5851 {
5852 if (bs->print)
628fe4e4 5853 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5854 else
628fe4e4 5855 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5856 }
5857 else
628fe4e4 5858 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5859 break;
5860 case bp_watchpoint:
5861 case bp_hardware_watchpoint:
5862 case bp_read_watchpoint:
5863 case bp_access_watchpoint:
5864 if (bs->stop)
5865 {
5866 if (bs->print)
628fe4e4 5867 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5868 else
628fe4e4 5869 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5870 }
5871 else
628fe4e4
JK
5872 {
5873 /* There was a watchpoint, but we're not stopping.
5874 This requires no further action. */
5875 }
c906108c
SS
5876 break;
5877 case bp_longjmp:
e2e4d78b 5878 case bp_longjmp_call_dummy:
186c406b 5879 case bp_exception:
0a39bb32
PA
5880 if (bs->stop)
5881 {
5882 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5883 retval.is_longjmp = bptype != bp_exception;
5884 }
5885 else
5886 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5887 break;
5888 case bp_longjmp_resume:
186c406b 5889 case bp_exception_resume:
0a39bb32
PA
5890 if (bs->stop)
5891 {
5892 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5893 retval.is_longjmp = bptype == bp_longjmp_resume;
5894 }
5895 else
5896 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5897 break;
5898 case bp_step_resume:
5899 if (bs->stop)
628fe4e4
JK
5900 this_action = BPSTAT_WHAT_STEP_RESUME;
5901 else
c906108c 5902 {
628fe4e4
JK
5903 /* It is for the wrong frame. */
5904 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5905 }
c906108c 5906 break;
2c03e5be
PA
5907 case bp_hp_step_resume:
5908 if (bs->stop)
5909 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5910 else
5911 {
5912 /* It is for the wrong frame. */
5913 this_action = BPSTAT_WHAT_SINGLE;
5914 }
5915 break;
c906108c 5916 case bp_watchpoint_scope:
c4093a6a 5917 case bp_thread_event:
1900040c 5918 case bp_overlay_event:
0fd8e87f 5919 case bp_longjmp_master:
aa7d318d 5920 case bp_std_terminate_master:
186c406b 5921 case bp_exception_master:
628fe4e4 5922 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5923 break;
ce78b96d 5924 case bp_catchpoint:
c5aa993b
JM
5925 if (bs->stop)
5926 {
5927 if (bs->print)
628fe4e4 5928 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5929 else
628fe4e4 5930 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5931 }
5932 else
628fe4e4
JK
5933 {
5934 /* There was a catchpoint, but we're not stopping.
5935 This requires no further action. */
5936 }
5937 break;
628fe4e4
JK
5938 case bp_jit_event:
5939 jit_event = 1;
5940 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5941 break;
c906108c 5942 case bp_call_dummy:
53a5351d
JM
5943 /* Make sure the action is stop (silent or noisy),
5944 so infrun.c pops the dummy frame. */
aa7d318d 5945 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5946 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5947 break;
5948 case bp_std_terminate:
5949 /* Make sure the action is stop (silent or noisy),
5950 so infrun.c pops the dummy frame. */
aa7d318d 5951 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5952 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5953 break;
1042e4c0 5954 case bp_tracepoint:
7a697b8d 5955 case bp_fast_tracepoint:
0fb4aa4b 5956 case bp_static_tracepoint:
1042e4c0
SS
5957 /* Tracepoint hits should not be reported back to GDB, and
5958 if one got through somehow, it should have been filtered
5959 out already. */
5960 internal_error (__FILE__, __LINE__,
7a697b8d 5961 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5962 break;
5963 case bp_gnu_ifunc_resolver:
5964 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5965 this_action = BPSTAT_WHAT_SINGLE;
5966 break;
5967 case bp_gnu_ifunc_resolver_return:
5968 /* The breakpoint will be removed, execution will restart from the
5969 PC of the former breakpoint. */
5970 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5971 break;
e7e0cddf
SS
5972
5973 case bp_dprintf:
a11cfd87
HZ
5974 if (bs->stop)
5975 this_action = BPSTAT_WHAT_STOP_SILENT;
5976 else
5977 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5978 break;
5979
628fe4e4
JK
5980 default:
5981 internal_error (__FILE__, __LINE__,
5982 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5983 }
628fe4e4
JK
5984
5985 retval.main_action = max (retval.main_action, this_action);
c906108c 5986 }
628fe4e4 5987
243a9253
PA
5988 return retval;
5989}
628fe4e4 5990
243a9253
PA
5991void
5992bpstat_run_callbacks (bpstat bs_head)
5993{
5994 bpstat bs;
628fe4e4 5995
0e30163f
JK
5996 for (bs = bs_head; bs != NULL; bs = bs->next)
5997 {
5998 struct breakpoint *b = bs->breakpoint_at;
5999
6000 if (b == NULL)
6001 continue;
6002 switch (b->type)
6003 {
243a9253
PA
6004 case bp_jit_event:
6005 handle_jit_event ();
6006 break;
0e30163f
JK
6007 case bp_gnu_ifunc_resolver:
6008 gnu_ifunc_resolver_stop (b);
6009 break;
6010 case bp_gnu_ifunc_resolver_return:
6011 gnu_ifunc_resolver_return_stop (b);
6012 break;
6013 }
6014 }
c906108c
SS
6015}
6016
6017/* Nonzero if we should step constantly (e.g. watchpoints on machines
6018 without hardware support). This isn't related to a specific bpstat,
6019 just to things like whether watchpoints are set. */
6020
c5aa993b 6021int
fba45db2 6022bpstat_should_step (void)
c906108c
SS
6023{
6024 struct breakpoint *b;
cc59ec59 6025
c906108c 6026 ALL_BREAKPOINTS (b)
717a8278 6027 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 6028 return 1;
c906108c
SS
6029 return 0;
6030}
6031
67822962
PA
6032int
6033bpstat_causes_stop (bpstat bs)
6034{
6035 for (; bs != NULL; bs = bs->next)
6036 if (bs->stop)
6037 return 1;
6038
6039 return 0;
6040}
6041
c906108c 6042\f
c5aa993b 6043
170b53b2
UW
6044/* Compute a string of spaces suitable to indent the next line
6045 so it starts at the position corresponding to the table column
6046 named COL_NAME in the currently active table of UIOUT. */
6047
6048static char *
6049wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6050{
6051 static char wrap_indent[80];
6052 int i, total_width, width, align;
6053 char *text;
6054
6055 total_width = 0;
6056 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6057 {
6058 if (strcmp (text, col_name) == 0)
6059 {
6060 gdb_assert (total_width < sizeof wrap_indent);
6061 memset (wrap_indent, ' ', total_width);
6062 wrap_indent[total_width] = 0;
6063
6064 return wrap_indent;
6065 }
6066
6067 total_width += width + 1;
6068 }
6069
6070 return NULL;
6071}
6072
b775012e
LM
6073/* Determine if the locations of this breakpoint will have their conditions
6074 evaluated by the target, host or a mix of both. Returns the following:
6075
6076 "host": Host evals condition.
6077 "host or target": Host or Target evals condition.
6078 "target": Target evals condition.
6079*/
6080
6081static const char *
6082bp_condition_evaluator (struct breakpoint *b)
6083{
6084 struct bp_location *bl;
6085 char host_evals = 0;
6086 char target_evals = 0;
6087
6088 if (!b)
6089 return NULL;
6090
6091 if (!is_breakpoint (b))
6092 return NULL;
6093
6094 if (gdb_evaluates_breakpoint_condition_p ()
6095 || !target_supports_evaluation_of_breakpoint_conditions ())
6096 return condition_evaluation_host;
6097
6098 for (bl = b->loc; bl; bl = bl->next)
6099 {
6100 if (bl->cond_bytecode)
6101 target_evals++;
6102 else
6103 host_evals++;
6104 }
6105
6106 if (host_evals && target_evals)
6107 return condition_evaluation_both;
6108 else if (target_evals)
6109 return condition_evaluation_target;
6110 else
6111 return condition_evaluation_host;
6112}
6113
6114/* Determine the breakpoint location's condition evaluator. This is
6115 similar to bp_condition_evaluator, but for locations. */
6116
6117static const char *
6118bp_location_condition_evaluator (struct bp_location *bl)
6119{
6120 if (bl && !is_breakpoint (bl->owner))
6121 return NULL;
6122
6123 if (gdb_evaluates_breakpoint_condition_p ()
6124 || !target_supports_evaluation_of_breakpoint_conditions ())
6125 return condition_evaluation_host;
6126
6127 if (bl && bl->cond_bytecode)
6128 return condition_evaluation_target;
6129 else
6130 return condition_evaluation_host;
6131}
6132
859825b8
JK
6133/* Print the LOC location out of the list of B->LOC locations. */
6134
170b53b2
UW
6135static void
6136print_breakpoint_location (struct breakpoint *b,
6137 struct bp_location *loc)
0d381245 6138{
79a45e25 6139 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6140 struct cleanup *old_chain = save_current_program_space ();
6141
859825b8
JK
6142 if (loc != NULL && loc->shlib_disabled)
6143 loc = NULL;
6144
6c95b8df
PA
6145 if (loc != NULL)
6146 set_current_program_space (loc->pspace);
6147
56435ebe 6148 if (b->display_canonical)
f00aae0f
KS
6149 ui_out_field_string (uiout, "what",
6150 event_location_to_string (b->location));
2f202fde 6151 else if (loc && loc->symtab)
0d381245
VP
6152 {
6153 struct symbol *sym
6154 = find_pc_sect_function (loc->address, loc->section);
6155 if (sym)
6156 {
6157 ui_out_text (uiout, "in ");
6158 ui_out_field_string (uiout, "func",
6159 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6160 ui_out_text (uiout, " ");
6161 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6162 ui_out_text (uiout, "at ");
0d381245 6163 }
05cba821
JK
6164 ui_out_field_string (uiout, "file",
6165 symtab_to_filename_for_display (loc->symtab));
0d381245 6166 ui_out_text (uiout, ":");
05cba821 6167
0d381245 6168 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6169 ui_out_field_string (uiout, "fullname",
6170 symtab_to_fullname (loc->symtab));
0d381245 6171
f8eba3c6 6172 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6173 }
859825b8 6174 else if (loc)
0d381245 6175 {
f99d8bf4
PA
6176 struct ui_file *stb = mem_fileopen ();
6177 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6178
f99d8bf4 6179 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6180 demangle, "");
0d381245 6181 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6182
6183 do_cleanups (stb_chain);
0d381245 6184 }
859825b8 6185 else
f00aae0f
KS
6186 {
6187 ui_out_field_string (uiout, "pending",
6188 event_location_to_string (b->location));
6189 /* If extra_string is available, it could be holding a condition
6190 or dprintf arguments. In either case, make sure it is printed,
6191 too, but only for non-MI streams. */
6192 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6193 {
6194 if (b->type == bp_dprintf)
6195 ui_out_text (uiout, ",");
6196 else
6197 ui_out_text (uiout, " ");
6198 ui_out_text (uiout, b->extra_string);
6199 }
6200 }
6c95b8df 6201
b775012e
LM
6202 if (loc && is_breakpoint (b)
6203 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6204 && bp_condition_evaluator (b) == condition_evaluation_both)
6205 {
6206 ui_out_text (uiout, " (");
6207 ui_out_field_string (uiout, "evaluated-by",
6208 bp_location_condition_evaluator (loc));
6209 ui_out_text (uiout, ")");
6210 }
6211
6c95b8df 6212 do_cleanups (old_chain);
0d381245
VP
6213}
6214
269b11a2
PA
6215static const char *
6216bptype_string (enum bptype type)
c906108c 6217{
c4093a6a
JM
6218 struct ep_type_description
6219 {
6220 enum bptype type;
6221 char *description;
6222 };
6223 static struct ep_type_description bptypes[] =
c906108c 6224 {
c5aa993b
JM
6225 {bp_none, "?deleted?"},
6226 {bp_breakpoint, "breakpoint"},
c906108c 6227 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6228 {bp_single_step, "sw single-step"},
c5aa993b
JM
6229 {bp_until, "until"},
6230 {bp_finish, "finish"},
6231 {bp_watchpoint, "watchpoint"},
c906108c 6232 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6233 {bp_read_watchpoint, "read watchpoint"},
6234 {bp_access_watchpoint, "acc watchpoint"},
6235 {bp_longjmp, "longjmp"},
6236 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6237 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6238 {bp_exception, "exception"},
6239 {bp_exception_resume, "exception resume"},
c5aa993b 6240 {bp_step_resume, "step resume"},
2c03e5be 6241 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6242 {bp_watchpoint_scope, "watchpoint scope"},
6243 {bp_call_dummy, "call dummy"},
aa7d318d 6244 {bp_std_terminate, "std::terminate"},
c5aa993b 6245 {bp_shlib_event, "shlib events"},
c4093a6a 6246 {bp_thread_event, "thread events"},
1900040c 6247 {bp_overlay_event, "overlay events"},
0fd8e87f 6248 {bp_longjmp_master, "longjmp master"},
aa7d318d 6249 {bp_std_terminate_master, "std::terminate master"},
186c406b 6250 {bp_exception_master, "exception master"},
ce78b96d 6251 {bp_catchpoint, "catchpoint"},
1042e4c0 6252 {bp_tracepoint, "tracepoint"},
7a697b8d 6253 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6254 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6255 {bp_dprintf, "dprintf"},
4efc6507 6256 {bp_jit_event, "jit events"},
0e30163f
JK
6257 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6258 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6259 };
269b11a2
PA
6260
6261 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6262 || ((int) type != bptypes[(int) type].type))
6263 internal_error (__FILE__, __LINE__,
6264 _("bptypes table does not describe type #%d."),
6265 (int) type);
6266
6267 return bptypes[(int) type].description;
6268}
6269
998580f1
MK
6270/* For MI, output a field named 'thread-groups' with a list as the value.
6271 For CLI, prefix the list with the string 'inf'. */
6272
6273static void
6274output_thread_groups (struct ui_out *uiout,
6275 const char *field_name,
6276 VEC(int) *inf_num,
6277 int mi_only)
6278{
752eb8b4 6279 struct cleanup *back_to;
998580f1
MK
6280 int is_mi = ui_out_is_mi_like_p (uiout);
6281 int inf;
6282 int i;
6283
6284 /* For backward compatibility, don't display inferiors in CLI unless
6285 there are several. Always display them for MI. */
6286 if (!is_mi && mi_only)
6287 return;
6288
752eb8b4
TT
6289 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6290
998580f1
MK
6291 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6292 {
6293 if (is_mi)
6294 {
6295 char mi_group[10];
6296
6297 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6298 ui_out_field_string (uiout, NULL, mi_group);
6299 }
6300 else
6301 {
6302 if (i == 0)
6303 ui_out_text (uiout, " inf ");
6304 else
6305 ui_out_text (uiout, ", ");
6306
6307 ui_out_text (uiout, plongest (inf));
6308 }
6309 }
6310
6311 do_cleanups (back_to);
6312}
6313
269b11a2
PA
6314/* Print B to gdb_stdout. */
6315
6316static void
6317print_one_breakpoint_location (struct breakpoint *b,
6318 struct bp_location *loc,
6319 int loc_number,
6320 struct bp_location **last_loc,
269b11a2
PA
6321 int allflag)
6322{
6323 struct command_line *l;
c2c6d25f 6324 static char bpenables[] = "nynny";
c906108c 6325
79a45e25 6326 struct ui_out *uiout = current_uiout;
0d381245
VP
6327 int header_of_multiple = 0;
6328 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6329 struct value_print_options opts;
6330
6331 get_user_print_options (&opts);
0d381245
VP
6332
6333 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6334 /* See comment in print_one_breakpoint concerning treatment of
6335 breakpoints with single disabled location. */
0d381245
VP
6336 if (loc == NULL
6337 && (b->loc != NULL
6338 && (b->loc->next != NULL || !b->loc->enabled)))
6339 header_of_multiple = 1;
6340 if (loc == NULL)
6341 loc = b->loc;
6342
c4093a6a
JM
6343 annotate_record ();
6344
6345 /* 1 */
6346 annotate_field (0);
0d381245
VP
6347 if (part_of_multiple)
6348 {
6349 char *formatted;
0c6773c1 6350 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6351 ui_out_field_string (uiout, "number", formatted);
6352 xfree (formatted);
6353 }
6354 else
6355 {
6356 ui_out_field_int (uiout, "number", b->number);
6357 }
c4093a6a
JM
6358
6359 /* 2 */
6360 annotate_field (1);
0d381245
VP
6361 if (part_of_multiple)
6362 ui_out_field_skip (uiout, "type");
269b11a2
PA
6363 else
6364 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6365
6366 /* 3 */
6367 annotate_field (2);
0d381245
VP
6368 if (part_of_multiple)
6369 ui_out_field_skip (uiout, "disp");
6370 else
2cec12e5 6371 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6372
c4093a6a
JM
6373
6374 /* 4 */
6375 annotate_field (3);
0d381245 6376 if (part_of_multiple)
54e52265 6377 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6378 else
4a64f543
MS
6379 ui_out_field_fmt (uiout, "enabled", "%c",
6380 bpenables[(int) b->enable_state]);
54e52265 6381 ui_out_spaces (uiout, 2);
0d381245 6382
c4093a6a
JM
6383
6384 /* 5 and 6 */
3086aeae 6385 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6386 {
4a64f543
MS
6387 /* Although the print_one can possibly print all locations,
6388 calling it here is not likely to get any nice result. So,
6389 make sure there's just one location. */
0d381245 6390 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6391 b->ops->print_one (b, last_loc);
0d381245 6392 }
3086aeae
DJ
6393 else
6394 switch (b->type)
6395 {
6396 case bp_none:
6397 internal_error (__FILE__, __LINE__,
e2e0b3e5 6398 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6399 break;
c906108c 6400
3086aeae
DJ
6401 case bp_watchpoint:
6402 case bp_hardware_watchpoint:
6403 case bp_read_watchpoint:
6404 case bp_access_watchpoint:
3a5c3e22
PA
6405 {
6406 struct watchpoint *w = (struct watchpoint *) b;
6407
6408 /* Field 4, the address, is omitted (which makes the columns
6409 not line up too nicely with the headers, but the effect
6410 is relatively readable). */
6411 if (opts.addressprint)
6412 ui_out_field_skip (uiout, "addr");
6413 annotate_field (5);
6414 ui_out_field_string (uiout, "what", w->exp_string);
6415 }
3086aeae
DJ
6416 break;
6417
3086aeae
DJ
6418 case bp_breakpoint:
6419 case bp_hardware_breakpoint:
7c16b83e 6420 case bp_single_step:
3086aeae
DJ
6421 case bp_until:
6422 case bp_finish:
6423 case bp_longjmp:
6424 case bp_longjmp_resume:
e2e4d78b 6425 case bp_longjmp_call_dummy:
186c406b
TT
6426 case bp_exception:
6427 case bp_exception_resume:
3086aeae 6428 case bp_step_resume:
2c03e5be 6429 case bp_hp_step_resume:
3086aeae
DJ
6430 case bp_watchpoint_scope:
6431 case bp_call_dummy:
aa7d318d 6432 case bp_std_terminate:
3086aeae
DJ
6433 case bp_shlib_event:
6434 case bp_thread_event:
6435 case bp_overlay_event:
0fd8e87f 6436 case bp_longjmp_master:
aa7d318d 6437 case bp_std_terminate_master:
186c406b 6438 case bp_exception_master:
1042e4c0 6439 case bp_tracepoint:
7a697b8d 6440 case bp_fast_tracepoint:
0fb4aa4b 6441 case bp_static_tracepoint:
e7e0cddf 6442 case bp_dprintf:
4efc6507 6443 case bp_jit_event:
0e30163f
JK
6444 case bp_gnu_ifunc_resolver:
6445 case bp_gnu_ifunc_resolver_return:
79a45b7d 6446 if (opts.addressprint)
3086aeae
DJ
6447 {
6448 annotate_field (4);
54e52265 6449 if (header_of_multiple)
0d381245 6450 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6451 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6452 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6453 else
5af949e3
UW
6454 ui_out_field_core_addr (uiout, "addr",
6455 loc->gdbarch, loc->address);
3086aeae
DJ
6456 }
6457 annotate_field (5);
0d381245 6458 if (!header_of_multiple)
170b53b2 6459 print_breakpoint_location (b, loc);
0d381245 6460 if (b->loc)
a6d9a66e 6461 *last_loc = b->loc;
3086aeae
DJ
6462 break;
6463 }
c906108c 6464
6c95b8df 6465
998580f1 6466 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6467 {
6468 struct inferior *inf;
998580f1
MK
6469 VEC(int) *inf_num = NULL;
6470 int mi_only = 1;
6c95b8df 6471
998580f1 6472 ALL_INFERIORS (inf)
6c95b8df
PA
6473 {
6474 if (inf->pspace == loc->pspace)
998580f1 6475 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6476 }
998580f1
MK
6477
6478 /* For backward compatibility, don't display inferiors in CLI unless
6479 there are several. Always display for MI. */
6480 if (allflag
6481 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6482 && (number_of_program_spaces () > 1
6483 || number_of_inferiors () > 1)
6484 /* LOC is for existing B, it cannot be in
6485 moribund_locations and thus having NULL OWNER. */
6486 && loc->owner->type != bp_catchpoint))
6487 mi_only = 0;
6488 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6489 VEC_free (int, inf_num);
6c95b8df
PA
6490 }
6491
4a306c9a 6492 if (!part_of_multiple)
c4093a6a 6493 {
4a306c9a
JB
6494 if (b->thread != -1)
6495 {
6496 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6497 "stop only in" line a little further down. */
4a306c9a
JB
6498 ui_out_text (uiout, " thread ");
6499 ui_out_field_int (uiout, "thread", b->thread);
6500 }
6501 else if (b->task != 0)
6502 {
6503 ui_out_text (uiout, " task ");
6504 ui_out_field_int (uiout, "task", b->task);
6505 }
c4093a6a 6506 }
f1310107 6507
8b93c638 6508 ui_out_text (uiout, "\n");
f1310107 6509
348d480f 6510 if (!part_of_multiple)
f1310107
TJB
6511 b->ops->print_one_detail (b, uiout);
6512
0d381245 6513 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6514 {
6515 annotate_field (6);
8b93c638 6516 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6517 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6518 the frame ID. */
5af949e3
UW
6519 ui_out_field_core_addr (uiout, "frame",
6520 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6521 ui_out_text (uiout, "\n");
c4093a6a
JM
6522 }
6523
28010a5d 6524 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6525 {
6526 annotate_field (7);
d77f58be 6527 if (is_tracepoint (b))
1042e4c0
SS
6528 ui_out_text (uiout, "\ttrace only if ");
6529 else
6530 ui_out_text (uiout, "\tstop only if ");
0101ce28 6531 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6532
6533 /* Print whether the target is doing the breakpoint's condition
6534 evaluation. If GDB is doing the evaluation, don't print anything. */
6535 if (is_breakpoint (b)
6536 && breakpoint_condition_evaluation_mode ()
6537 == condition_evaluation_target)
6538 {
6539 ui_out_text (uiout, " (");
6540 ui_out_field_string (uiout, "evaluated-by",
6541 bp_condition_evaluator (b));
6542 ui_out_text (uiout, " evals)");
6543 }
0101ce28
JJ
6544 ui_out_text (uiout, "\n");
6545 }
6546
0d381245 6547 if (!part_of_multiple && b->thread != -1)
c4093a6a 6548 {
4a64f543 6549 /* FIXME should make an annotation for this. */
8b93c638 6550 ui_out_text (uiout, "\tstop only in thread ");
5d5658a1
PA
6551 if (ui_out_is_mi_like_p (uiout))
6552 ui_out_field_int (uiout, "thread", b->thread);
6553 else
6554 {
6555 struct thread_info *thr = find_thread_global_id (b->thread);
6556
6557 ui_out_field_string (uiout, "thread", print_thread_id (thr));
6558 }
8b93c638 6559 ui_out_text (uiout, "\n");
c4093a6a
JM
6560 }
6561
556ec64d
YQ
6562 if (!part_of_multiple)
6563 {
6564 if (b->hit_count)
31f56a27
YQ
6565 {
6566 /* FIXME should make an annotation for this. */
6567 if (is_catchpoint (b))
6568 ui_out_text (uiout, "\tcatchpoint");
6569 else if (is_tracepoint (b))
6570 ui_out_text (uiout, "\ttracepoint");
6571 else
6572 ui_out_text (uiout, "\tbreakpoint");
6573 ui_out_text (uiout, " already hit ");
6574 ui_out_field_int (uiout, "times", b->hit_count);
6575 if (b->hit_count == 1)
6576 ui_out_text (uiout, " time\n");
6577 else
6578 ui_out_text (uiout, " times\n");
6579 }
556ec64d
YQ
6580 else
6581 {
31f56a27
YQ
6582 /* Output the count also if it is zero, but only if this is mi. */
6583 if (ui_out_is_mi_like_p (uiout))
6584 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6585 }
6586 }
8b93c638 6587
0d381245 6588 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6589 {
6590 annotate_field (8);
8b93c638
JM
6591 ui_out_text (uiout, "\tignore next ");
6592 ui_out_field_int (uiout, "ignore", b->ignore_count);
6593 ui_out_text (uiout, " hits\n");
c4093a6a 6594 }
059fb39f 6595
816338b5
SS
6596 /* Note that an enable count of 1 corresponds to "enable once"
6597 behavior, which is reported by the combination of enablement and
6598 disposition, so we don't need to mention it here. */
6599 if (!part_of_multiple && b->enable_count > 1)
6600 {
6601 annotate_field (8);
6602 ui_out_text (uiout, "\tdisable after ");
6603 /* Tweak the wording to clarify that ignore and enable counts
6604 are distinct, and have additive effect. */
6605 if (b->ignore_count)
6606 ui_out_text (uiout, "additional ");
6607 else
6608 ui_out_text (uiout, "next ");
6609 ui_out_field_int (uiout, "enable", b->enable_count);
6610 ui_out_text (uiout, " hits\n");
6611 }
6612
f196051f
SS
6613 if (!part_of_multiple && is_tracepoint (b))
6614 {
6615 struct tracepoint *tp = (struct tracepoint *) b;
6616
6617 if (tp->traceframe_usage)
6618 {
6619 ui_out_text (uiout, "\ttrace buffer usage ");
6620 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6621 ui_out_text (uiout, " bytes\n");
6622 }
6623 }
d3ce09f5 6624
9add0f1b 6625 l = b->commands ? b->commands->commands : NULL;
059fb39f 6626 if (!part_of_multiple && l)
c4093a6a 6627 {
3b31d625
EZ
6628 struct cleanup *script_chain;
6629
c4093a6a 6630 annotate_field (9);
3b31d625 6631 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6632 print_command_lines (uiout, l, 4);
3b31d625 6633 do_cleanups (script_chain);
c4093a6a 6634 }
d24317b4 6635
d9b3f62e 6636 if (is_tracepoint (b))
1042e4c0 6637 {
d9b3f62e
PA
6638 struct tracepoint *t = (struct tracepoint *) b;
6639
6640 if (!part_of_multiple && t->pass_count)
6641 {
6642 annotate_field (10);
6643 ui_out_text (uiout, "\tpass count ");
6644 ui_out_field_int (uiout, "pass", t->pass_count);
6645 ui_out_text (uiout, " \n");
6646 }
f2a8bc8a
YQ
6647
6648 /* Don't display it when tracepoint or tracepoint location is
6649 pending. */
6650 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6651 {
6652 annotate_field (11);
6653
6654 if (ui_out_is_mi_like_p (uiout))
6655 ui_out_field_string (uiout, "installed",
6656 loc->inserted ? "y" : "n");
6657 else
6658 {
6659 if (loc->inserted)
6660 ui_out_text (uiout, "\t");
6661 else
6662 ui_out_text (uiout, "\tnot ");
6663 ui_out_text (uiout, "installed on target\n");
6664 }
6665 }
1042e4c0
SS
6666 }
6667
d24317b4
VP
6668 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6669 {
3a5c3e22
PA
6670 if (is_watchpoint (b))
6671 {
6672 struct watchpoint *w = (struct watchpoint *) b;
6673
6674 ui_out_field_string (uiout, "original-location", w->exp_string);
6675 }
f00aae0f
KS
6676 else if (b->location != NULL
6677 && event_location_to_string (b->location) != NULL)
6678 ui_out_field_string (uiout, "original-location",
6679 event_location_to_string (b->location));
d24317b4 6680 }
c4093a6a 6681}
c5aa993b 6682
0d381245
VP
6683static void
6684print_one_breakpoint (struct breakpoint *b,
4a64f543 6685 struct bp_location **last_loc,
6c95b8df 6686 int allflag)
0d381245 6687{
8d3788bd 6688 struct cleanup *bkpt_chain;
79a45e25 6689 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6690
6691 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6692
12c5a436 6693 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6694 do_cleanups (bkpt_chain);
0d381245
VP
6695
6696 /* If this breakpoint has custom print function,
6697 it's already printed. Otherwise, print individual
6698 locations, if any. */
6699 if (b->ops == NULL || b->ops->print_one == NULL)
6700 {
4a64f543
MS
6701 /* If breakpoint has a single location that is disabled, we
6702 print it as if it had several locations, since otherwise it's
6703 hard to represent "breakpoint enabled, location disabled"
6704 situation.
6705
6706 Note that while hardware watchpoints have several locations
a3be7890 6707 internally, that's not a property exposed to user. */
0d381245 6708 if (b->loc
a5606eee 6709 && !is_hardware_watchpoint (b)
8d3788bd 6710 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6711 {
6712 struct bp_location *loc;
6713 int n = 1;
8d3788bd 6714
0d381245 6715 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6716 {
6717 struct cleanup *inner2 =
6718 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6719 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6720 do_cleanups (inner2);
6721 }
0d381245
VP
6722 }
6723 }
6724}
6725
a6d9a66e
UW
6726static int
6727breakpoint_address_bits (struct breakpoint *b)
6728{
6729 int print_address_bits = 0;
6730 struct bp_location *loc;
6731
c6d81124
PA
6732 /* Software watchpoints that aren't watching memory don't have an
6733 address to print. */
6734 if (is_no_memory_software_watchpoint (b))
6735 return 0;
6736
a6d9a66e
UW
6737 for (loc = b->loc; loc; loc = loc->next)
6738 {
c7437ca6
PA
6739 int addr_bit;
6740
c7437ca6 6741 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6742 if (addr_bit > print_address_bits)
6743 print_address_bits = addr_bit;
6744 }
6745
6746 return print_address_bits;
6747}
0d381245 6748
c4093a6a
JM
6749struct captured_breakpoint_query_args
6750 {
6751 int bnum;
6752 };
c5aa993b 6753
c4093a6a 6754static int
2b65245e 6755do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a 6756{
9a3c8263
SM
6757 struct captured_breakpoint_query_args *args
6758 = (struct captured_breakpoint_query_args *) data;
52f0bd74 6759 struct breakpoint *b;
a6d9a66e 6760 struct bp_location *dummy_loc = NULL;
cc59ec59 6761
c4093a6a
JM
6762 ALL_BREAKPOINTS (b)
6763 {
6764 if (args->bnum == b->number)
c5aa993b 6765 {
12c5a436 6766 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6767 return GDB_RC_OK;
c5aa993b 6768 }
c4093a6a
JM
6769 }
6770 return GDB_RC_NONE;
6771}
c5aa993b 6772
c4093a6a 6773enum gdb_rc
4a64f543
MS
6774gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6775 char **error_message)
c4093a6a
JM
6776{
6777 struct captured_breakpoint_query_args args;
cc59ec59 6778
c4093a6a
JM
6779 args.bnum = bnum;
6780 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6781 an error. */
b0b13bb4
DJ
6782 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6783 error_message, RETURN_MASK_ALL) < 0)
6784 return GDB_RC_FAIL;
6785 else
6786 return GDB_RC_OK;
c4093a6a 6787}
c5aa993b 6788
09d682a4
TT
6789/* Return true if this breakpoint was set by the user, false if it is
6790 internal or momentary. */
6791
6792int
6793user_breakpoint_p (struct breakpoint *b)
6794{
46c6471b 6795 return b->number > 0;
09d682a4
TT
6796}
6797
7f3b0473 6798/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6799 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6800 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6801 FILTER is non-NULL, call it on each breakpoint and only include the
6802 ones for which it returns non-zero. Return the total number of
6803 breakpoints listed. */
c906108c 6804
d77f58be 6805static int
e5a67952 6806breakpoint_1 (char *args, int allflag,
4a64f543 6807 int (*filter) (const struct breakpoint *))
c4093a6a 6808{
52f0bd74 6809 struct breakpoint *b;
a6d9a66e 6810 struct bp_location *last_loc = NULL;
7f3b0473 6811 int nr_printable_breakpoints;
3b31d625 6812 struct cleanup *bkpttbl_chain;
79a45b7d 6813 struct value_print_options opts;
a6d9a66e 6814 int print_address_bits = 0;
269b11a2 6815 int print_type_col_width = 14;
79a45e25 6816 struct ui_out *uiout = current_uiout;
269b11a2 6817
79a45b7d
TT
6818 get_user_print_options (&opts);
6819
4a64f543
MS
6820 /* Compute the number of rows in the table, as well as the size
6821 required for address fields. */
7f3b0473
AC
6822 nr_printable_breakpoints = 0;
6823 ALL_BREAKPOINTS (b)
e5a67952
MS
6824 {
6825 /* If we have a filter, only list the breakpoints it accepts. */
6826 if (filter && !filter (b))
6827 continue;
6828
6829 /* If we have an "args" string, it is a list of breakpoints to
6830 accept. Skip the others. */
6831 if (args != NULL && *args != '\0')
6832 {
6833 if (allflag && parse_and_eval_long (args) != b->number)
6834 continue;
6835 if (!allflag && !number_is_in_list (args, b->number))
6836 continue;
6837 }
269b11a2 6838
e5a67952
MS
6839 if (allflag || user_breakpoint_p (b))
6840 {
6841 int addr_bit, type_len;
a6d9a66e 6842
e5a67952
MS
6843 addr_bit = breakpoint_address_bits (b);
6844 if (addr_bit > print_address_bits)
6845 print_address_bits = addr_bit;
269b11a2 6846
e5a67952
MS
6847 type_len = strlen (bptype_string (b->type));
6848 if (type_len > print_type_col_width)
6849 print_type_col_width = type_len;
6850
6851 nr_printable_breakpoints++;
6852 }
6853 }
7f3b0473 6854
79a45b7d 6855 if (opts.addressprint)
3b31d625 6856 bkpttbl_chain
3e43a32a
MS
6857 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6858 nr_printable_breakpoints,
3b31d625 6859 "BreakpointTable");
8b93c638 6860 else
3b31d625 6861 bkpttbl_chain
3e43a32a
MS
6862 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6863 nr_printable_breakpoints,
3b31d625 6864 "BreakpointTable");
8b93c638 6865
7f3b0473 6866 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6867 annotate_breakpoints_headers ();
6868 if (nr_printable_breakpoints > 0)
6869 annotate_field (0);
4a64f543 6870 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6871 if (nr_printable_breakpoints > 0)
6872 annotate_field (1);
269b11a2 6873 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6874 "type", "Type"); /* 2 */
d7faa9e7
AC
6875 if (nr_printable_breakpoints > 0)
6876 annotate_field (2);
4a64f543 6877 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6878 if (nr_printable_breakpoints > 0)
6879 annotate_field (3);
54e52265 6880 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6881 if (opts.addressprint)
e5a67952
MS
6882 {
6883 if (nr_printable_breakpoints > 0)
6884 annotate_field (4);
6885 if (print_address_bits <= 32)
6886 ui_out_table_header (uiout, 10, ui_left,
6887 "addr", "Address"); /* 5 */
6888 else
6889 ui_out_table_header (uiout, 18, ui_left,
6890 "addr", "Address"); /* 5 */
6891 }
d7faa9e7
AC
6892 if (nr_printable_breakpoints > 0)
6893 annotate_field (5);
6894 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6895 ui_out_table_body (uiout);
6896 if (nr_printable_breakpoints > 0)
6897 annotate_breakpoints_table ();
7f3b0473 6898
c4093a6a 6899 ALL_BREAKPOINTS (b)
e5a67952
MS
6900 {
6901 QUIT;
6902 /* If we have a filter, only list the breakpoints it accepts. */
6903 if (filter && !filter (b))
6904 continue;
6905
6906 /* If we have an "args" string, it is a list of breakpoints to
6907 accept. Skip the others. */
6908
6909 if (args != NULL && *args != '\0')
6910 {
6911 if (allflag) /* maintenance info breakpoint */
6912 {
6913 if (parse_and_eval_long (args) != b->number)
6914 continue;
6915 }
6916 else /* all others */
6917 {
6918 if (!number_is_in_list (args, b->number))
6919 continue;
6920 }
6921 }
6922 /* We only print out user settable breakpoints unless the
6923 allflag is set. */
6924 if (allflag || user_breakpoint_p (b))
12c5a436 6925 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6926 }
6927
3b31d625 6928 do_cleanups (bkpttbl_chain);
698384cd 6929
7f3b0473 6930 if (nr_printable_breakpoints == 0)
c906108c 6931 {
4a64f543
MS
6932 /* If there's a filter, let the caller decide how to report
6933 empty list. */
d77f58be
SS
6934 if (!filter)
6935 {
e5a67952 6936 if (args == NULL || *args == '\0')
d77f58be
SS
6937 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6938 else
4a64f543 6939 ui_out_message (uiout, 0,
e5a67952
MS
6940 "No breakpoint or watchpoint matching '%s'.\n",
6941 args);
d77f58be 6942 }
c906108c
SS
6943 }
6944 else
c4093a6a 6945 {
a6d9a66e
UW
6946 if (last_loc && !server_command)
6947 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6948 }
c906108c 6949
4a64f543 6950 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6951 there have been breakpoints? */
c906108c 6952 annotate_breakpoints_table_end ();
d77f58be
SS
6953
6954 return nr_printable_breakpoints;
c906108c
SS
6955}
6956
ad443146
SS
6957/* Display the value of default-collect in a way that is generally
6958 compatible with the breakpoint list. */
6959
6960static void
6961default_collect_info (void)
6962{
79a45e25
PA
6963 struct ui_out *uiout = current_uiout;
6964
ad443146
SS
6965 /* If it has no value (which is frequently the case), say nothing; a
6966 message like "No default-collect." gets in user's face when it's
6967 not wanted. */
6968 if (!*default_collect)
6969 return;
6970
6971 /* The following phrase lines up nicely with per-tracepoint collect
6972 actions. */
6973 ui_out_text (uiout, "default collect ");
6974 ui_out_field_string (uiout, "default-collect", default_collect);
6975 ui_out_text (uiout, " \n");
6976}
6977
c906108c 6978static void
e5a67952 6979breakpoints_info (char *args, int from_tty)
c906108c 6980{
e5a67952 6981 breakpoint_1 (args, 0, NULL);
ad443146
SS
6982
6983 default_collect_info ();
d77f58be
SS
6984}
6985
6986static void
e5a67952 6987watchpoints_info (char *args, int from_tty)
d77f58be 6988{
e5a67952 6989 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6990 struct ui_out *uiout = current_uiout;
d77f58be
SS
6991
6992 if (num_printed == 0)
6993 {
e5a67952 6994 if (args == NULL || *args == '\0')
d77f58be
SS
6995 ui_out_message (uiout, 0, "No watchpoints.\n");
6996 else
e5a67952 6997 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6998 }
c906108c
SS
6999}
7000
7a292a7a 7001static void
e5a67952 7002maintenance_info_breakpoints (char *args, int from_tty)
c906108c 7003{
e5a67952 7004 breakpoint_1 (args, 1, NULL);
ad443146
SS
7005
7006 default_collect_info ();
c906108c
SS
7007}
7008
0d381245 7009static int
714835d5 7010breakpoint_has_pc (struct breakpoint *b,
6c95b8df 7011 struct program_space *pspace,
714835d5 7012 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
7013{
7014 struct bp_location *bl = b->loc;
cc59ec59 7015
0d381245
VP
7016 for (; bl; bl = bl->next)
7017 {
6c95b8df
PA
7018 if (bl->pspace == pspace
7019 && bl->address == pc
0d381245
VP
7020 && (!overlay_debugging || bl->section == section))
7021 return 1;
7022 }
7023 return 0;
7024}
7025
672f9b60 7026/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
7027 concerns with logical breakpoints, so we match program spaces, not
7028 address spaces. */
c906108c
SS
7029
7030static void
6c95b8df
PA
7031describe_other_breakpoints (struct gdbarch *gdbarch,
7032 struct program_space *pspace, CORE_ADDR pc,
5af949e3 7033 struct obj_section *section, int thread)
c906108c 7034{
52f0bd74
AC
7035 int others = 0;
7036 struct breakpoint *b;
c906108c
SS
7037
7038 ALL_BREAKPOINTS (b)
672f9b60
KP
7039 others += (user_breakpoint_p (b)
7040 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
7041 if (others > 0)
7042 {
a3f17187
AC
7043 if (others == 1)
7044 printf_filtered (_("Note: breakpoint "));
7045 else /* if (others == ???) */
7046 printf_filtered (_("Note: breakpoints "));
c906108c 7047 ALL_BREAKPOINTS (b)
672f9b60 7048 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
7049 {
7050 others--;
7051 printf_filtered ("%d", b->number);
7052 if (b->thread == -1 && thread != -1)
7053 printf_filtered (" (all threads)");
7054 else if (b->thread != -1)
7055 printf_filtered (" (thread %d)", b->thread);
7056 printf_filtered ("%s%s ",
059fb39f 7057 ((b->enable_state == bp_disabled
f8eba3c6 7058 || b->enable_state == bp_call_disabled)
0d381245 7059 ? " (disabled)"
0d381245
VP
7060 : ""),
7061 (others > 1) ? ","
7062 : ((others == 1) ? " and" : ""));
7063 }
a3f17187 7064 printf_filtered (_("also set at pc "));
5af949e3 7065 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
7066 printf_filtered (".\n");
7067 }
7068}
7069\f
c906108c 7070
e4f237da 7071/* Return true iff it is meaningful to use the address member of
244558af
LM
7072 BPT locations. For some breakpoint types, the locations' address members
7073 are irrelevant and it makes no sense to attempt to compare them to other
7074 addresses (or use them for any other purpose either).
e4f237da 7075
4a64f543 7076 More specifically, each of the following breakpoint types will
244558af 7077 always have a zero valued location address and we don't want to mark
4a64f543 7078 breakpoints of any of these types to be a duplicate of an actual
244558af 7079 breakpoint location at address zero:
e4f237da
KB
7080
7081 bp_watchpoint
2d134ed3
PA
7082 bp_catchpoint
7083
7084*/
e4f237da
KB
7085
7086static int
7087breakpoint_address_is_meaningful (struct breakpoint *bpt)
7088{
7089 enum bptype type = bpt->type;
7090
2d134ed3
PA
7091 return (type != bp_watchpoint && type != bp_catchpoint);
7092}
7093
7094/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7095 true if LOC1 and LOC2 represent the same watchpoint location. */
7096
7097static int
4a64f543
MS
7098watchpoint_locations_match (struct bp_location *loc1,
7099 struct bp_location *loc2)
2d134ed3 7100{
3a5c3e22
PA
7101 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7102 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7103
7104 /* Both of them must exist. */
7105 gdb_assert (w1 != NULL);
7106 gdb_assert (w2 != NULL);
2bdf28a0 7107
4a64f543
MS
7108 /* If the target can evaluate the condition expression in hardware,
7109 then we we need to insert both watchpoints even if they are at
7110 the same place. Otherwise the watchpoint will only trigger when
7111 the condition of whichever watchpoint was inserted evaluates to
7112 true, not giving a chance for GDB to check the condition of the
7113 other watchpoint. */
3a5c3e22 7114 if ((w1->cond_exp
4a64f543
MS
7115 && target_can_accel_watchpoint_condition (loc1->address,
7116 loc1->length,
0cf6dd15 7117 loc1->watchpoint_type,
3a5c3e22
PA
7118 w1->cond_exp))
7119 || (w2->cond_exp
4a64f543
MS
7120 && target_can_accel_watchpoint_condition (loc2->address,
7121 loc2->length,
0cf6dd15 7122 loc2->watchpoint_type,
3a5c3e22 7123 w2->cond_exp)))
0cf6dd15
TJB
7124 return 0;
7125
85d721b8
PA
7126 /* Note that this checks the owner's type, not the location's. In
7127 case the target does not support read watchpoints, but does
7128 support access watchpoints, we'll have bp_read_watchpoint
7129 watchpoints with hw_access locations. Those should be considered
7130 duplicates of hw_read locations. The hw_read locations will
7131 become hw_access locations later. */
2d134ed3
PA
7132 return (loc1->owner->type == loc2->owner->type
7133 && loc1->pspace->aspace == loc2->pspace->aspace
7134 && loc1->address == loc2->address
7135 && loc1->length == loc2->length);
e4f237da
KB
7136}
7137
31e77af2 7138/* See breakpoint.h. */
6c95b8df 7139
31e77af2 7140int
6c95b8df
PA
7141breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7142 struct address_space *aspace2, CORE_ADDR addr2)
7143{
f5656ead 7144 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7145 || aspace1 == aspace2)
7146 && addr1 == addr2);
7147}
7148
f1310107
TJB
7149/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7150 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7151 matches ASPACE2. On targets that have global breakpoints, the address
7152 space doesn't really matter. */
7153
7154static int
7155breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7156 int len1, struct address_space *aspace2,
7157 CORE_ADDR addr2)
7158{
f5656ead 7159 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7160 || aspace1 == aspace2)
7161 && addr2 >= addr1 && addr2 < addr1 + len1);
7162}
7163
7164/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7165 a ranged breakpoint. In most targets, a match happens only if ASPACE
7166 matches the breakpoint's address space. On targets that have global
7167 breakpoints, the address space doesn't really matter. */
7168
7169static int
7170breakpoint_location_address_match (struct bp_location *bl,
7171 struct address_space *aspace,
7172 CORE_ADDR addr)
7173{
7174 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7175 aspace, addr)
7176 || (bl->length
7177 && breakpoint_address_match_range (bl->pspace->aspace,
7178 bl->address, bl->length,
7179 aspace, addr)));
7180}
7181
d35ae833
PA
7182/* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7183 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7184 match happens only if ASPACE matches the breakpoint's address
7185 space. On targets that have global breakpoints, the address space
7186 doesn't really matter. */
7187
7188static int
7189breakpoint_location_address_range_overlap (struct bp_location *bl,
7190 struct address_space *aspace,
7191 CORE_ADDR addr, int len)
7192{
7193 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7194 || bl->pspace->aspace == aspace)
7195 {
7196 int bl_len = bl->length != 0 ? bl->length : 1;
7197
7198 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7199 return 1;
7200 }
7201 return 0;
7202}
7203
1e4d1764
YQ
7204/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7205 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7206 true, otherwise returns false. */
7207
7208static int
7209tracepoint_locations_match (struct bp_location *loc1,
7210 struct bp_location *loc2)
7211{
7212 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7213 /* Since tracepoint locations are never duplicated with others', tracepoint
7214 locations at the same address of different tracepoints are regarded as
7215 different locations. */
7216 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7217 else
7218 return 0;
7219}
7220
2d134ed3
PA
7221/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7222 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7223 represent the same location. */
7224
7225static int
4a64f543
MS
7226breakpoint_locations_match (struct bp_location *loc1,
7227 struct bp_location *loc2)
2d134ed3 7228{
2bdf28a0
JK
7229 int hw_point1, hw_point2;
7230
7231 /* Both of them must not be in moribund_locations. */
7232 gdb_assert (loc1->owner != NULL);
7233 gdb_assert (loc2->owner != NULL);
7234
7235 hw_point1 = is_hardware_watchpoint (loc1->owner);
7236 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7237
7238 if (hw_point1 != hw_point2)
7239 return 0;
7240 else if (hw_point1)
7241 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7242 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7243 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7244 else
f1310107
TJB
7245 /* We compare bp_location.length in order to cover ranged breakpoints. */
7246 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7247 loc2->pspace->aspace, loc2->address)
7248 && loc1->length == loc2->length);
2d134ed3
PA
7249}
7250
76897487
KB
7251static void
7252breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7253 int bnum, int have_bnum)
7254{
f63fbe86
MS
7255 /* The longest string possibly returned by hex_string_custom
7256 is 50 chars. These must be at least that big for safety. */
7257 char astr1[64];
7258 char astr2[64];
76897487 7259
bb599908
PH
7260 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7261 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7262 if (have_bnum)
8a3fe4f8 7263 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7264 bnum, astr1, astr2);
7265 else
8a3fe4f8 7266 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7267}
7268
4a64f543
MS
7269/* Adjust a breakpoint's address to account for architectural
7270 constraints on breakpoint placement. Return the adjusted address.
7271 Note: Very few targets require this kind of adjustment. For most
7272 targets, this function is simply the identity function. */
76897487
KB
7273
7274static CORE_ADDR
a6d9a66e
UW
7275adjust_breakpoint_address (struct gdbarch *gdbarch,
7276 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7277{
a6d9a66e 7278 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7279 {
7280 /* Very few targets need any kind of breakpoint adjustment. */
7281 return bpaddr;
7282 }
88f7da05
KB
7283 else if (bptype == bp_watchpoint
7284 || bptype == bp_hardware_watchpoint
7285 || bptype == bp_read_watchpoint
7286 || bptype == bp_access_watchpoint
fe798b75 7287 || bptype == bp_catchpoint)
88f7da05
KB
7288 {
7289 /* Watchpoints and the various bp_catch_* eventpoints should not
7290 have their addresses modified. */
7291 return bpaddr;
7292 }
7c16b83e
PA
7293 else if (bptype == bp_single_step)
7294 {
7295 /* Single-step breakpoints should not have their addresses
7296 modified. If there's any architectural constrain that
7297 applies to this address, then it should have already been
7298 taken into account when the breakpoint was created in the
7299 first place. If we didn't do this, stepping through e.g.,
7300 Thumb-2 IT blocks would break. */
7301 return bpaddr;
7302 }
76897487
KB
7303 else
7304 {
7305 CORE_ADDR adjusted_bpaddr;
7306
7307 /* Some targets have architectural constraints on the placement
7308 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7309 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7310
7311 /* An adjusted breakpoint address can significantly alter
7312 a user's expectations. Print a warning if an adjustment
7313 is required. */
7314 if (adjusted_bpaddr != bpaddr)
7315 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7316
7317 return adjusted_bpaddr;
7318 }
7319}
7320
28010a5d
PA
7321void
7322init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7323 struct breakpoint *owner)
7cc221ef 7324{
7cc221ef
DJ
7325 memset (loc, 0, sizeof (*loc));
7326
348d480f
PA
7327 gdb_assert (ops != NULL);
7328
28010a5d
PA
7329 loc->ops = ops;
7330 loc->owner = owner;
511a6cd4 7331 loc->cond = NULL;
b775012e 7332 loc->cond_bytecode = NULL;
0d381245
VP
7333 loc->shlib_disabled = 0;
7334 loc->enabled = 1;
e049a4b5 7335
28010a5d 7336 switch (owner->type)
e049a4b5
DJ
7337 {
7338 case bp_breakpoint:
7c16b83e 7339 case bp_single_step:
e049a4b5
DJ
7340 case bp_until:
7341 case bp_finish:
7342 case bp_longjmp:
7343 case bp_longjmp_resume:
e2e4d78b 7344 case bp_longjmp_call_dummy:
186c406b
TT
7345 case bp_exception:
7346 case bp_exception_resume:
e049a4b5 7347 case bp_step_resume:
2c03e5be 7348 case bp_hp_step_resume:
e049a4b5
DJ
7349 case bp_watchpoint_scope:
7350 case bp_call_dummy:
aa7d318d 7351 case bp_std_terminate:
e049a4b5
DJ
7352 case bp_shlib_event:
7353 case bp_thread_event:
7354 case bp_overlay_event:
4efc6507 7355 case bp_jit_event:
0fd8e87f 7356 case bp_longjmp_master:
aa7d318d 7357 case bp_std_terminate_master:
186c406b 7358 case bp_exception_master:
0e30163f
JK
7359 case bp_gnu_ifunc_resolver:
7360 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7361 case bp_dprintf:
e049a4b5 7362 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7363 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7364 break;
7365 case bp_hardware_breakpoint:
7366 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7367 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7368 break;
7369 case bp_hardware_watchpoint:
7370 case bp_read_watchpoint:
7371 case bp_access_watchpoint:
7372 loc->loc_type = bp_loc_hardware_watchpoint;
7373 break;
7374 case bp_watchpoint:
ce78b96d 7375 case bp_catchpoint:
15c3d785
PA
7376 case bp_tracepoint:
7377 case bp_fast_tracepoint:
0fb4aa4b 7378 case bp_static_tracepoint:
e049a4b5
DJ
7379 loc->loc_type = bp_loc_other;
7380 break;
7381 default:
e2e0b3e5 7382 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7383 }
7384
f431efe5 7385 loc->refc = 1;
28010a5d
PA
7386}
7387
7388/* Allocate a struct bp_location. */
7389
7390static struct bp_location *
7391allocate_bp_location (struct breakpoint *bpt)
7392{
348d480f
PA
7393 return bpt->ops->allocate_location (bpt);
7394}
7cc221ef 7395
f431efe5
PA
7396static void
7397free_bp_location (struct bp_location *loc)
fe3f5fa8 7398{
348d480f 7399 loc->ops->dtor (loc);
fe3f5fa8
VP
7400 xfree (loc);
7401}
7402
f431efe5
PA
7403/* Increment reference count. */
7404
7405static void
7406incref_bp_location (struct bp_location *bl)
7407{
7408 ++bl->refc;
7409}
7410
7411/* Decrement reference count. If the reference count reaches 0,
7412 destroy the bp_location. Sets *BLP to NULL. */
7413
7414static void
7415decref_bp_location (struct bp_location **blp)
7416{
0807b50c
PA
7417 gdb_assert ((*blp)->refc > 0);
7418
f431efe5
PA
7419 if (--(*blp)->refc == 0)
7420 free_bp_location (*blp);
7421 *blp = NULL;
7422}
7423
346774a9 7424/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7425
346774a9
PA
7426static void
7427add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7428{
346774a9 7429 struct breakpoint *b1;
c906108c 7430
346774a9
PA
7431 /* Add this breakpoint to the end of the chain so that a list of
7432 breakpoints will come out in order of increasing numbers. */
7433
7434 b1 = breakpoint_chain;
7435 if (b1 == 0)
7436 breakpoint_chain = b;
7437 else
7438 {
7439 while (b1->next)
7440 b1 = b1->next;
7441 b1->next = b;
7442 }
7443}
7444
7445/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7446
7447static void
7448init_raw_breakpoint_without_location (struct breakpoint *b,
7449 struct gdbarch *gdbarch,
28010a5d 7450 enum bptype bptype,
c0a91b2b 7451 const struct breakpoint_ops *ops)
346774a9 7452{
c906108c 7453 memset (b, 0, sizeof (*b));
2219d63c 7454
348d480f
PA
7455 gdb_assert (ops != NULL);
7456
28010a5d 7457 b->ops = ops;
4d28f7a8 7458 b->type = bptype;
a6d9a66e 7459 b->gdbarch = gdbarch;
c906108c
SS
7460 b->language = current_language->la_language;
7461 b->input_radix = input_radix;
7462 b->thread = -1;
b5de0fa7 7463 b->enable_state = bp_enabled;
c906108c
SS
7464 b->next = 0;
7465 b->silent = 0;
7466 b->ignore_count = 0;
7467 b->commands = NULL;
818dd999 7468 b->frame_id = null_frame_id;
0d381245 7469 b->condition_not_parsed = 0;
84f4c1fe 7470 b->py_bp_object = NULL;
d0fb5eae 7471 b->related_breakpoint = b;
f00aae0f 7472 b->location = NULL;
346774a9
PA
7473}
7474
7475/* Helper to set_raw_breakpoint below. Creates a breakpoint
7476 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7477
7478static struct breakpoint *
7479set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7480 enum bptype bptype,
c0a91b2b 7481 const struct breakpoint_ops *ops)
346774a9
PA
7482{
7483 struct breakpoint *b = XNEW (struct breakpoint);
7484
348d480f 7485 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7486 add_to_breakpoint_chain (b);
0d381245
VP
7487 return b;
7488}
7489
0e30163f
JK
7490/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7491 resolutions should be made as the user specified the location explicitly
7492 enough. */
7493
0d381245 7494static void
0e30163f 7495set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7496{
2bdf28a0
JK
7497 gdb_assert (loc->owner != NULL);
7498
0d381245 7499 if (loc->owner->type == bp_breakpoint
1042e4c0 7500 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7501 || is_tracepoint (loc->owner))
0d381245 7502 {
0e30163f 7503 int is_gnu_ifunc;
2c02bd72 7504 const char *function_name;
6a3a010b 7505 CORE_ADDR func_addr;
0e30163f 7506
2c02bd72 7507 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7508 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7509
7510 if (is_gnu_ifunc && !explicit_loc)
7511 {
7512 struct breakpoint *b = loc->owner;
7513
7514 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7515 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7516 &loc->requested_address))
7517 {
7518 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7519 loc->address = adjust_breakpoint_address (loc->gdbarch,
7520 loc->requested_address,
7521 b->type);
7522 }
7523 else if (b->type == bp_breakpoint && b->loc == loc
7524 && loc->next == NULL && b->related_breakpoint == b)
7525 {
7526 /* Create only the whole new breakpoint of this type but do not
7527 mess more complicated breakpoints with multiple locations. */
7528 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7529 /* Remember the resolver's address for use by the return
7530 breakpoint. */
7531 loc->related_address = func_addr;
0e30163f
JK
7532 }
7533 }
7534
2c02bd72
DE
7535 if (function_name)
7536 loc->function_name = xstrdup (function_name);
0d381245
VP
7537 }
7538}
7539
a6d9a66e 7540/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7541struct gdbarch *
a6d9a66e
UW
7542get_sal_arch (struct symtab_and_line sal)
7543{
7544 if (sal.section)
7545 return get_objfile_arch (sal.section->objfile);
7546 if (sal.symtab)
eb822aa6 7547 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7548
7549 return NULL;
7550}
7551
346774a9
PA
7552/* Low level routine for partially initializing a breakpoint of type
7553 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7554 file name, and line number are provided by SAL.
0d381245
VP
7555
7556 It is expected that the caller will complete the initialization of
7557 the newly created breakpoint struct as well as output any status
c56053d2 7558 information regarding the creation of a new breakpoint. */
0d381245 7559
346774a9
PA
7560static void
7561init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7562 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7563 const struct breakpoint_ops *ops)
0d381245 7564{
28010a5d 7565 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7566
3742cc8b 7567 add_location_to_breakpoint (b, &sal);
0d381245 7568
6c95b8df
PA
7569 if (bptype != bp_catchpoint)
7570 gdb_assert (sal.pspace != NULL);
7571
f8eba3c6
TT
7572 /* Store the program space that was used to set the breakpoint,
7573 except for ordinary breakpoints, which are independent of the
7574 program space. */
7575 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7576 b->pspace = sal.pspace;
346774a9 7577}
c906108c 7578
346774a9
PA
7579/* set_raw_breakpoint is a low level routine for allocating and
7580 partially initializing a breakpoint of type BPTYPE. The newly
7581 created breakpoint's address, section, source file name, and line
7582 number are provided by SAL. The newly created and partially
7583 initialized breakpoint is added to the breakpoint chain and
7584 is also returned as the value of this function.
7585
7586 It is expected that the caller will complete the initialization of
7587 the newly created breakpoint struct as well as output any status
7588 information regarding the creation of a new breakpoint. In
7589 particular, set_raw_breakpoint does NOT set the breakpoint
7590 number! Care should be taken to not allow an error to occur
7591 prior to completing the initialization of the breakpoint. If this
7592 should happen, a bogus breakpoint will be left on the chain. */
7593
7594struct breakpoint *
7595set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7596 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7597 const struct breakpoint_ops *ops)
346774a9
PA
7598{
7599 struct breakpoint *b = XNEW (struct breakpoint);
7600
348d480f 7601 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7602 add_to_breakpoint_chain (b);
c906108c
SS
7603 return b;
7604}
7605
53a5351d 7606/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7607 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7608 initiated the operation. */
c906108c
SS
7609
7610void
186c406b 7611set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7612{
35df4500 7613 struct breakpoint *b, *b_tmp;
5d5658a1 7614 int thread = tp->global_num;
0fd8e87f
UW
7615
7616 /* To avoid having to rescan all objfile symbols at every step,
7617 we maintain a list of continually-inserted but always disabled
7618 longjmp "master" breakpoints. Here, we simply create momentary
7619 clones of those and enable them for the requested thread. */
35df4500 7620 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7621 if (b->pspace == current_program_space
186c406b
TT
7622 && (b->type == bp_longjmp_master
7623 || b->type == bp_exception_master))
0fd8e87f 7624 {
06edf0c0
PA
7625 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7626 struct breakpoint *clone;
cc59ec59 7627
e2e4d78b
JK
7628 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7629 after their removal. */
06edf0c0 7630 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7631 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7632 clone->thread = thread;
7633 }
186c406b
TT
7634
7635 tp->initiating_frame = frame;
c906108c
SS
7636}
7637
611c83ae 7638/* Delete all longjmp breakpoints from THREAD. */
c906108c 7639void
611c83ae 7640delete_longjmp_breakpoint (int thread)
c906108c 7641{
35df4500 7642 struct breakpoint *b, *b_tmp;
c906108c 7643
35df4500 7644 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7645 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7646 {
7647 if (b->thread == thread)
7648 delete_breakpoint (b);
7649 }
c906108c
SS
7650}
7651
f59f708a
PA
7652void
7653delete_longjmp_breakpoint_at_next_stop (int thread)
7654{
7655 struct breakpoint *b, *b_tmp;
7656
7657 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7658 if (b->type == bp_longjmp || b->type == bp_exception)
7659 {
7660 if (b->thread == thread)
7661 b->disposition = disp_del_at_next_stop;
7662 }
7663}
7664
e2e4d78b
JK
7665/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7666 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7667 pointer to any of them. Return NULL if this system cannot place longjmp
7668 breakpoints. */
7669
7670struct breakpoint *
7671set_longjmp_breakpoint_for_call_dummy (void)
7672{
7673 struct breakpoint *b, *retval = NULL;
7674
7675 ALL_BREAKPOINTS (b)
7676 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7677 {
7678 struct breakpoint *new_b;
7679
7680 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7681 &momentary_breakpoint_ops,
7682 1);
5d5658a1 7683 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
e2e4d78b
JK
7684
7685 /* Link NEW_B into the chain of RETVAL breakpoints. */
7686
7687 gdb_assert (new_b->related_breakpoint == new_b);
7688 if (retval == NULL)
7689 retval = new_b;
7690 new_b->related_breakpoint = retval;
7691 while (retval->related_breakpoint != new_b->related_breakpoint)
7692 retval = retval->related_breakpoint;
7693 retval->related_breakpoint = new_b;
7694 }
7695
7696 return retval;
7697}
7698
7699/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7700 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7701 stack.
7702
7703 You should call this function only at places where it is safe to currently
7704 unwind the whole stack. Failed stack unwind would discard live dummy
7705 frames. */
7706
7707void
b67a2c6f 7708check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7709{
7710 struct breakpoint *b, *b_tmp;
7711
7712 ALL_BREAKPOINTS_SAFE (b, b_tmp)
5d5658a1 7713 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
e2e4d78b
JK
7714 {
7715 struct breakpoint *dummy_b = b->related_breakpoint;
7716
7717 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7718 dummy_b = dummy_b->related_breakpoint;
7719 if (dummy_b->type != bp_call_dummy
7720 || frame_find_by_id (dummy_b->frame_id) != NULL)
7721 continue;
7722
b67a2c6f 7723 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7724
7725 while (b->related_breakpoint != b)
7726 {
7727 if (b_tmp == b->related_breakpoint)
7728 b_tmp = b->related_breakpoint->next;
7729 delete_breakpoint (b->related_breakpoint);
7730 }
7731 delete_breakpoint (b);
7732 }
7733}
7734
1900040c
MS
7735void
7736enable_overlay_breakpoints (void)
7737{
52f0bd74 7738 struct breakpoint *b;
1900040c
MS
7739
7740 ALL_BREAKPOINTS (b)
7741 if (b->type == bp_overlay_event)
7742 {
7743 b->enable_state = bp_enabled;
44702360 7744 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7745 overlay_events_enabled = 1;
1900040c
MS
7746 }
7747}
7748
7749void
7750disable_overlay_breakpoints (void)
7751{
52f0bd74 7752 struct breakpoint *b;
1900040c
MS
7753
7754 ALL_BREAKPOINTS (b)
7755 if (b->type == bp_overlay_event)
7756 {
7757 b->enable_state = bp_disabled;
44702360 7758 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7759 overlay_events_enabled = 0;
1900040c
MS
7760 }
7761}
7762
aa7d318d
TT
7763/* Set an active std::terminate breakpoint for each std::terminate
7764 master breakpoint. */
7765void
7766set_std_terminate_breakpoint (void)
7767{
35df4500 7768 struct breakpoint *b, *b_tmp;
aa7d318d 7769
35df4500 7770 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7771 if (b->pspace == current_program_space
7772 && b->type == bp_std_terminate_master)
7773 {
06edf0c0 7774 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7775 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7776 }
7777}
7778
7779/* Delete all the std::terminate breakpoints. */
7780void
7781delete_std_terminate_breakpoint (void)
7782{
35df4500 7783 struct breakpoint *b, *b_tmp;
aa7d318d 7784
35df4500 7785 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7786 if (b->type == bp_std_terminate)
7787 delete_breakpoint (b);
7788}
7789
c4093a6a 7790struct breakpoint *
a6d9a66e 7791create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7792{
7793 struct breakpoint *b;
c4093a6a 7794
06edf0c0
PA
7795 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7796 &internal_breakpoint_ops);
7797
b5de0fa7 7798 b->enable_state = bp_enabled;
f00aae0f 7799 /* location has to be used or breakpoint_re_set will delete me. */
a06efdd6 7800 b->location = new_address_location (b->loc->address);
c4093a6a 7801
44702360 7802 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7803
c4093a6a
JM
7804 return b;
7805}
7806
0101ce28
JJ
7807struct lang_and_radix
7808 {
7809 enum language lang;
7810 int radix;
7811 };
7812
4efc6507
DE
7813/* Create a breakpoint for JIT code registration and unregistration. */
7814
7815struct breakpoint *
7816create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7817{
7818 struct breakpoint *b;
7819
06edf0c0
PA
7820 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7821 &internal_breakpoint_ops);
44702360 7822 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7823 return b;
7824}
0101ce28 7825
03673fc7
PP
7826/* Remove JIT code registration and unregistration breakpoint(s). */
7827
7828void
7829remove_jit_event_breakpoints (void)
7830{
7831 struct breakpoint *b, *b_tmp;
7832
7833 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7834 if (b->type == bp_jit_event
7835 && b->loc->pspace == current_program_space)
7836 delete_breakpoint (b);
7837}
7838
cae688ec
JJ
7839void
7840remove_solib_event_breakpoints (void)
7841{
35df4500 7842 struct breakpoint *b, *b_tmp;
cae688ec 7843
35df4500 7844 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7845 if (b->type == bp_shlib_event
7846 && b->loc->pspace == current_program_space)
cae688ec
JJ
7847 delete_breakpoint (b);
7848}
7849
f37f681c
PA
7850/* See breakpoint.h. */
7851
7852void
7853remove_solib_event_breakpoints_at_next_stop (void)
7854{
7855 struct breakpoint *b, *b_tmp;
7856
7857 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7858 if (b->type == bp_shlib_event
7859 && b->loc->pspace == current_program_space)
7860 b->disposition = disp_del_at_next_stop;
7861}
7862
04086b45
PA
7863/* Helper for create_solib_event_breakpoint /
7864 create_and_insert_solib_event_breakpoint. Allows specifying which
7865 INSERT_MODE to pass through to update_global_location_list. */
7866
7867static struct breakpoint *
7868create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7869 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7870{
7871 struct breakpoint *b;
7872
06edf0c0
PA
7873 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7874 &internal_breakpoint_ops);
04086b45 7875 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7876 return b;
7877}
7878
04086b45
PA
7879struct breakpoint *
7880create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7881{
7882 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7883}
7884
f37f681c
PA
7885/* See breakpoint.h. */
7886
7887struct breakpoint *
7888create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7889{
7890 struct breakpoint *b;
7891
04086b45
PA
7892 /* Explicitly tell update_global_location_list to insert
7893 locations. */
7894 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7895 if (!b->loc->inserted)
7896 {
7897 delete_breakpoint (b);
7898 return NULL;
7899 }
7900 return b;
7901}
7902
cae688ec
JJ
7903/* Disable any breakpoints that are on code in shared libraries. Only
7904 apply to enabled breakpoints, disabled ones can just stay disabled. */
7905
7906void
cb851954 7907disable_breakpoints_in_shlibs (void)
cae688ec 7908{
876fa593 7909 struct bp_location *loc, **locp_tmp;
cae688ec 7910
876fa593 7911 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7912 {
2bdf28a0 7913 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7914 struct breakpoint *b = loc->owner;
2bdf28a0 7915
4a64f543
MS
7916 /* We apply the check to all breakpoints, including disabled for
7917 those with loc->duplicate set. This is so that when breakpoint
7918 becomes enabled, or the duplicate is removed, gdb will try to
7919 insert all breakpoints. If we don't set shlib_disabled here,
7920 we'll try to insert those breakpoints and fail. */
1042e4c0 7921 if (((b->type == bp_breakpoint)
508ccb1f 7922 || (b->type == bp_jit_event)
1042e4c0 7923 || (b->type == bp_hardware_breakpoint)
d77f58be 7924 || (is_tracepoint (b)))
6c95b8df 7925 && loc->pspace == current_program_space
0d381245 7926 && !loc->shlib_disabled
6c95b8df 7927 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7928 )
0d381245
VP
7929 {
7930 loc->shlib_disabled = 1;
7931 }
cae688ec
JJ
7932 }
7933}
7934
63644780
NB
7935/* Disable any breakpoints and tracepoints that are in SOLIB upon
7936 notification of unloaded_shlib. Only apply to enabled breakpoints,
7937 disabled ones can just stay disabled. */
84acb35a 7938
75149521 7939static void
84acb35a
JJ
7940disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7941{
876fa593 7942 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7943 int disabled_shlib_breaks = 0;
7944
c86cf029
VP
7945 /* SunOS a.out shared libraries are always mapped, so do not
7946 disable breakpoints; they will only be reported as unloaded
7947 through clear_solib when GDB discards its shared library
7948 list. See clear_solib for more information. */
7949 if (exec_bfd != NULL
7950 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7951 return;
7952
876fa593 7953 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7954 {
2bdf28a0 7955 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7956 struct breakpoint *b = loc->owner;
cc59ec59 7957
1e4d1764 7958 if (solib->pspace == loc->pspace
e2dd7057 7959 && !loc->shlib_disabled
1e4d1764
YQ
7960 && (((b->type == bp_breakpoint
7961 || b->type == bp_jit_event
7962 || b->type == bp_hardware_breakpoint)
7963 && (loc->loc_type == bp_loc_hardware_breakpoint
7964 || loc->loc_type == bp_loc_software_breakpoint))
7965 || is_tracepoint (b))
e2dd7057 7966 && solib_contains_address_p (solib, loc->address))
84acb35a 7967 {
e2dd7057
PP
7968 loc->shlib_disabled = 1;
7969 /* At this point, we cannot rely on remove_breakpoint
7970 succeeding so we must mark the breakpoint as not inserted
7971 to prevent future errors occurring in remove_breakpoints. */
7972 loc->inserted = 0;
8d3788bd
VP
7973
7974 /* This may cause duplicate notifications for the same breakpoint. */
7975 observer_notify_breakpoint_modified (b);
7976
e2dd7057
PP
7977 if (!disabled_shlib_breaks)
7978 {
7979 target_terminal_ours_for_output ();
3e43a32a
MS
7980 warning (_("Temporarily disabling breakpoints "
7981 "for unloaded shared library \"%s\""),
e2dd7057 7982 solib->so_name);
84acb35a 7983 }
e2dd7057 7984 disabled_shlib_breaks = 1;
84acb35a
JJ
7985 }
7986 }
84acb35a
JJ
7987}
7988
63644780
NB
7989/* Disable any breakpoints and tracepoints in OBJFILE upon
7990 notification of free_objfile. Only apply to enabled breakpoints,
7991 disabled ones can just stay disabled. */
7992
7993static void
7994disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7995{
7996 struct breakpoint *b;
7997
7998 if (objfile == NULL)
7999 return;
8000
d03de421
PA
8001 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
8002 managed by the user with add-symbol-file/remove-symbol-file.
8003 Similarly to how breakpoints in shared libraries are handled in
8004 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
8005 shlib_disabled so they end up uninserted on the next global
8006 location list update. Shared libraries not loaded by the user
8007 aren't handled here -- they're already handled in
8008 disable_breakpoints_in_unloaded_shlib, called by solib.c's
8009 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
8010 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8011 main objfile). */
8012 if ((objfile->flags & OBJF_SHARED) == 0
8013 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
8014 return;
8015
8016 ALL_BREAKPOINTS (b)
8017 {
8018 struct bp_location *loc;
8019 int bp_modified = 0;
8020
8021 if (!is_breakpoint (b) && !is_tracepoint (b))
8022 continue;
8023
8024 for (loc = b->loc; loc != NULL; loc = loc->next)
8025 {
8026 CORE_ADDR loc_addr = loc->address;
8027
8028 if (loc->loc_type != bp_loc_hardware_breakpoint
8029 && loc->loc_type != bp_loc_software_breakpoint)
8030 continue;
8031
8032 if (loc->shlib_disabled != 0)
8033 continue;
8034
8035 if (objfile->pspace != loc->pspace)
8036 continue;
8037
8038 if (loc->loc_type != bp_loc_hardware_breakpoint
8039 && loc->loc_type != bp_loc_software_breakpoint)
8040 continue;
8041
8042 if (is_addr_in_objfile (loc_addr, objfile))
8043 {
8044 loc->shlib_disabled = 1;
08351840
PA
8045 /* At this point, we don't know whether the object was
8046 unmapped from the inferior or not, so leave the
8047 inserted flag alone. We'll handle failure to
8048 uninsert quietly, in case the object was indeed
8049 unmapped. */
63644780
NB
8050
8051 mark_breakpoint_location_modified (loc);
8052
8053 bp_modified = 1;
8054 }
8055 }
8056
8057 if (bp_modified)
8058 observer_notify_breakpoint_modified (b);
8059 }
8060}
8061
ce78b96d
JB
8062/* FORK & VFORK catchpoints. */
8063
e29a4733
PA
8064/* An instance of this type is used to represent a fork or vfork
8065 catchpoint. It includes a "struct breakpoint" as a kind of base
8066 class; users downcast to "struct breakpoint *" when needed. A
8067 breakpoint is really of this type iff its ops pointer points to
8068 CATCH_FORK_BREAKPOINT_OPS. */
8069
8070struct fork_catchpoint
8071{
8072 /* The base class. */
8073 struct breakpoint base;
8074
8075 /* Process id of a child process whose forking triggered this
8076 catchpoint. This field is only valid immediately after this
8077 catchpoint has triggered. */
8078 ptid_t forked_inferior_pid;
8079};
8080
4a64f543
MS
8081/* Implement the "insert" breakpoint_ops method for fork
8082 catchpoints. */
ce78b96d 8083
77b06cd7
TJB
8084static int
8085insert_catch_fork (struct bp_location *bl)
ce78b96d 8086{
dfd4cc63 8087 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8088}
8089
4a64f543
MS
8090/* Implement the "remove" breakpoint_ops method for fork
8091 catchpoints. */
ce78b96d
JB
8092
8093static int
77b06cd7 8094remove_catch_fork (struct bp_location *bl)
ce78b96d 8095{
dfd4cc63 8096 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8097}
8098
8099/* Implement the "breakpoint_hit" breakpoint_ops method for fork
8100 catchpoints. */
8101
8102static int
f1310107 8103breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
8104 struct address_space *aspace, CORE_ADDR bp_addr,
8105 const struct target_waitstatus *ws)
ce78b96d 8106{
e29a4733
PA
8107 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8108
f90263c1
TT
8109 if (ws->kind != TARGET_WAITKIND_FORKED)
8110 return 0;
8111
8112 c->forked_inferior_pid = ws->value.related_pid;
8113 return 1;
ce78b96d
JB
8114}
8115
4a64f543
MS
8116/* Implement the "print_it" breakpoint_ops method for fork
8117 catchpoints. */
ce78b96d
JB
8118
8119static enum print_stop_action
348d480f 8120print_it_catch_fork (bpstat bs)
ce78b96d 8121{
36dfb11c 8122 struct ui_out *uiout = current_uiout;
348d480f
PA
8123 struct breakpoint *b = bs->breakpoint_at;
8124 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8125
ce78b96d 8126 annotate_catchpoint (b->number);
f303dbd6 8127 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8128 if (b->disposition == disp_del)
f303dbd6 8129 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8130 else
f303dbd6 8131 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8132 if (ui_out_is_mi_like_p (uiout))
8133 {
8134 ui_out_field_string (uiout, "reason",
8135 async_reason_lookup (EXEC_ASYNC_FORK));
8136 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8137 }
8138 ui_out_field_int (uiout, "bkptno", b->number);
8139 ui_out_text (uiout, " (forked process ");
8140 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8141 ui_out_text (uiout, "), ");
ce78b96d
JB
8142 return PRINT_SRC_AND_LOC;
8143}
8144
4a64f543
MS
8145/* Implement the "print_one" breakpoint_ops method for fork
8146 catchpoints. */
ce78b96d
JB
8147
8148static void
a6d9a66e 8149print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8150{
e29a4733 8151 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8152 struct value_print_options opts;
79a45e25 8153 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8154
8155 get_user_print_options (&opts);
8156
4a64f543
MS
8157 /* Field 4, the address, is omitted (which makes the columns not
8158 line up too nicely with the headers, but the effect is relatively
8159 readable). */
79a45b7d 8160 if (opts.addressprint)
ce78b96d
JB
8161 ui_out_field_skip (uiout, "addr");
8162 annotate_field (5);
8163 ui_out_text (uiout, "fork");
e29a4733 8164 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8165 {
8166 ui_out_text (uiout, ", process ");
8167 ui_out_field_int (uiout, "what",
e29a4733 8168 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8169 ui_out_spaces (uiout, 1);
8170 }
8ac3646f
TT
8171
8172 if (ui_out_is_mi_like_p (uiout))
8173 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8174}
8175
8176/* Implement the "print_mention" breakpoint_ops method for fork
8177 catchpoints. */
8178
8179static void
8180print_mention_catch_fork (struct breakpoint *b)
8181{
8182 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8183}
8184
6149aea9
PA
8185/* Implement the "print_recreate" breakpoint_ops method for fork
8186 catchpoints. */
8187
8188static void
8189print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8190{
8191 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8192 print_recreate_thread (b, fp);
6149aea9
PA
8193}
8194
ce78b96d
JB
8195/* The breakpoint_ops structure to be used in fork catchpoints. */
8196
2060206e 8197static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8198
4a64f543
MS
8199/* Implement the "insert" breakpoint_ops method for vfork
8200 catchpoints. */
ce78b96d 8201
77b06cd7
TJB
8202static int
8203insert_catch_vfork (struct bp_location *bl)
ce78b96d 8204{
dfd4cc63 8205 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8206}
8207
4a64f543
MS
8208/* Implement the "remove" breakpoint_ops method for vfork
8209 catchpoints. */
ce78b96d
JB
8210
8211static int
77b06cd7 8212remove_catch_vfork (struct bp_location *bl)
ce78b96d 8213{
dfd4cc63 8214 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8215}
8216
8217/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8218 catchpoints. */
8219
8220static int
f1310107 8221breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8222 struct address_space *aspace, CORE_ADDR bp_addr,
8223 const struct target_waitstatus *ws)
ce78b96d 8224{
e29a4733
PA
8225 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8226
f90263c1
TT
8227 if (ws->kind != TARGET_WAITKIND_VFORKED)
8228 return 0;
8229
8230 c->forked_inferior_pid = ws->value.related_pid;
8231 return 1;
ce78b96d
JB
8232}
8233
4a64f543
MS
8234/* Implement the "print_it" breakpoint_ops method for vfork
8235 catchpoints. */
ce78b96d
JB
8236
8237static enum print_stop_action
348d480f 8238print_it_catch_vfork (bpstat bs)
ce78b96d 8239{
36dfb11c 8240 struct ui_out *uiout = current_uiout;
348d480f 8241 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8242 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8243
ce78b96d 8244 annotate_catchpoint (b->number);
f303dbd6 8245 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8246 if (b->disposition == disp_del)
f303dbd6 8247 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8248 else
f303dbd6 8249 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8250 if (ui_out_is_mi_like_p (uiout))
8251 {
8252 ui_out_field_string (uiout, "reason",
8253 async_reason_lookup (EXEC_ASYNC_VFORK));
8254 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8255 }
8256 ui_out_field_int (uiout, "bkptno", b->number);
8257 ui_out_text (uiout, " (vforked process ");
8258 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8259 ui_out_text (uiout, "), ");
ce78b96d
JB
8260 return PRINT_SRC_AND_LOC;
8261}
8262
4a64f543
MS
8263/* Implement the "print_one" breakpoint_ops method for vfork
8264 catchpoints. */
ce78b96d
JB
8265
8266static void
a6d9a66e 8267print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8268{
e29a4733 8269 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8270 struct value_print_options opts;
79a45e25 8271 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8272
8273 get_user_print_options (&opts);
4a64f543
MS
8274 /* Field 4, the address, is omitted (which makes the columns not
8275 line up too nicely with the headers, but the effect is relatively
8276 readable). */
79a45b7d 8277 if (opts.addressprint)
ce78b96d
JB
8278 ui_out_field_skip (uiout, "addr");
8279 annotate_field (5);
8280 ui_out_text (uiout, "vfork");
e29a4733 8281 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8282 {
8283 ui_out_text (uiout, ", process ");
8284 ui_out_field_int (uiout, "what",
e29a4733 8285 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8286 ui_out_spaces (uiout, 1);
8287 }
8ac3646f
TT
8288
8289 if (ui_out_is_mi_like_p (uiout))
8290 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8291}
8292
8293/* Implement the "print_mention" breakpoint_ops method for vfork
8294 catchpoints. */
8295
8296static void
8297print_mention_catch_vfork (struct breakpoint *b)
8298{
8299 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8300}
8301
6149aea9
PA
8302/* Implement the "print_recreate" breakpoint_ops method for vfork
8303 catchpoints. */
8304
8305static void
8306print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8307{
8308 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8309 print_recreate_thread (b, fp);
6149aea9
PA
8310}
8311
ce78b96d
JB
8312/* The breakpoint_ops structure to be used in vfork catchpoints. */
8313
2060206e 8314static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8315
edcc5120
TT
8316/* An instance of this type is used to represent an solib catchpoint.
8317 It includes a "struct breakpoint" as a kind of base class; users
8318 downcast to "struct breakpoint *" when needed. A breakpoint is
8319 really of this type iff its ops pointer points to
8320 CATCH_SOLIB_BREAKPOINT_OPS. */
8321
8322struct solib_catchpoint
8323{
8324 /* The base class. */
8325 struct breakpoint base;
8326
8327 /* True for "catch load", false for "catch unload". */
8328 unsigned char is_load;
8329
8330 /* Regular expression to match, if any. COMPILED is only valid when
8331 REGEX is non-NULL. */
8332 char *regex;
8333 regex_t compiled;
8334};
8335
8336static void
8337dtor_catch_solib (struct breakpoint *b)
8338{
8339 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8340
8341 if (self->regex)
8342 regfree (&self->compiled);
8343 xfree (self->regex);
8344
8345 base_breakpoint_ops.dtor (b);
8346}
8347
8348static int
8349insert_catch_solib (struct bp_location *ignore)
8350{
8351 return 0;
8352}
8353
8354static int
8355remove_catch_solib (struct bp_location *ignore)
8356{
8357 return 0;
8358}
8359
8360static int
8361breakpoint_hit_catch_solib (const struct bp_location *bl,
8362 struct address_space *aspace,
8363 CORE_ADDR bp_addr,
8364 const struct target_waitstatus *ws)
8365{
8366 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8367 struct breakpoint *other;
8368
8369 if (ws->kind == TARGET_WAITKIND_LOADED)
8370 return 1;
8371
8372 ALL_BREAKPOINTS (other)
8373 {
8374 struct bp_location *other_bl;
8375
8376 if (other == bl->owner)
8377 continue;
8378
8379 if (other->type != bp_shlib_event)
8380 continue;
8381
8382 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8383 continue;
8384
8385 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8386 {
8387 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8388 return 1;
8389 }
8390 }
8391
8392 return 0;
8393}
8394
8395static void
8396check_status_catch_solib (struct bpstats *bs)
8397{
8398 struct solib_catchpoint *self
8399 = (struct solib_catchpoint *) bs->breakpoint_at;
8400 int ix;
8401
8402 if (self->is_load)
8403 {
8404 struct so_list *iter;
8405
8406 for (ix = 0;
8407 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8408 ix, iter);
8409 ++ix)
8410 {
8411 if (!self->regex
8412 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8413 return;
8414 }
8415 }
8416 else
8417 {
8418 char *iter;
8419
8420 for (ix = 0;
8421 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8422 ix, iter);
8423 ++ix)
8424 {
8425 if (!self->regex
8426 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8427 return;
8428 }
8429 }
8430
8431 bs->stop = 0;
8432 bs->print_it = print_it_noop;
8433}
8434
8435static enum print_stop_action
8436print_it_catch_solib (bpstat bs)
8437{
8438 struct breakpoint *b = bs->breakpoint_at;
8439 struct ui_out *uiout = current_uiout;
8440
8441 annotate_catchpoint (b->number);
f303dbd6 8442 maybe_print_thread_hit_breakpoint (uiout);
edcc5120 8443 if (b->disposition == disp_del)
f303dbd6 8444 ui_out_text (uiout, "Temporary catchpoint ");
edcc5120 8445 else
f303dbd6 8446 ui_out_text (uiout, "Catchpoint ");
edcc5120
TT
8447 ui_out_field_int (uiout, "bkptno", b->number);
8448 ui_out_text (uiout, "\n");
8449 if (ui_out_is_mi_like_p (uiout))
8450 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8451 print_solib_event (1);
8452 return PRINT_SRC_AND_LOC;
8453}
8454
8455static void
8456print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8457{
8458 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8459 struct value_print_options opts;
8460 struct ui_out *uiout = current_uiout;
8461 char *msg;
8462
8463 get_user_print_options (&opts);
8464 /* Field 4, the address, is omitted (which makes the columns not
8465 line up too nicely with the headers, but the effect is relatively
8466 readable). */
8467 if (opts.addressprint)
8468 {
8469 annotate_field (4);
8470 ui_out_field_skip (uiout, "addr");
8471 }
8472
8473 annotate_field (5);
8474 if (self->is_load)
8475 {
8476 if (self->regex)
8477 msg = xstrprintf (_("load of library matching %s"), self->regex);
8478 else
8479 msg = xstrdup (_("load of library"));
8480 }
8481 else
8482 {
8483 if (self->regex)
8484 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8485 else
8486 msg = xstrdup (_("unload of library"));
8487 }
8488 ui_out_field_string (uiout, "what", msg);
8489 xfree (msg);
8ac3646f
TT
8490
8491 if (ui_out_is_mi_like_p (uiout))
8492 ui_out_field_string (uiout, "catch-type",
8493 self->is_load ? "load" : "unload");
edcc5120
TT
8494}
8495
8496static void
8497print_mention_catch_solib (struct breakpoint *b)
8498{
8499 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8500
8501 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8502 self->is_load ? "load" : "unload");
8503}
8504
8505static void
8506print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8507{
8508 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8509
8510 fprintf_unfiltered (fp, "%s %s",
8511 b->disposition == disp_del ? "tcatch" : "catch",
8512 self->is_load ? "load" : "unload");
8513 if (self->regex)
8514 fprintf_unfiltered (fp, " %s", self->regex);
8515 fprintf_unfiltered (fp, "\n");
8516}
8517
8518static struct breakpoint_ops catch_solib_breakpoint_ops;
8519
91985142
MG
8520/* Shared helper function (MI and CLI) for creating and installing
8521 a shared object event catchpoint. If IS_LOAD is non-zero then
8522 the events to be caught are load events, otherwise they are
8523 unload events. If IS_TEMP is non-zero the catchpoint is a
8524 temporary one. If ENABLED is non-zero the catchpoint is
8525 created in an enabled state. */
edcc5120 8526
91985142
MG
8527void
8528add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8529{
8530 struct solib_catchpoint *c;
8531 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8532 struct cleanup *cleanup;
8533
edcc5120
TT
8534 if (!arg)
8535 arg = "";
8536 arg = skip_spaces (arg);
8537
8538 c = XCNEW (struct solib_catchpoint);
8539 cleanup = make_cleanup (xfree, c);
8540
8541 if (*arg != '\0')
8542 {
8543 int errcode;
8544
8545 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8546 if (errcode != 0)
8547 {
8548 char *err = get_regcomp_error (errcode, &c->compiled);
8549
8550 make_cleanup (xfree, err);
8551 error (_("Invalid regexp (%s): %s"), err, arg);
8552 }
8553 c->regex = xstrdup (arg);
8554 }
8555
8556 c->is_load = is_load;
91985142 8557 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8558 &catch_solib_breakpoint_ops);
8559
91985142
MG
8560 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8561
edcc5120
TT
8562 discard_cleanups (cleanup);
8563 install_breakpoint (0, &c->base, 1);
8564}
8565
91985142
MG
8566/* A helper function that does all the work for "catch load" and
8567 "catch unload". */
8568
8569static void
8570catch_load_or_unload (char *arg, int from_tty, int is_load,
8571 struct cmd_list_element *command)
8572{
8573 int tempflag;
8574 const int enabled = 1;
8575
8576 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8577
8578 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8579}
8580
edcc5120
TT
8581static void
8582catch_load_command_1 (char *arg, int from_tty,
8583 struct cmd_list_element *command)
8584{
8585 catch_load_or_unload (arg, from_tty, 1, command);
8586}
8587
8588static void
8589catch_unload_command_1 (char *arg, int from_tty,
8590 struct cmd_list_element *command)
8591{
8592 catch_load_or_unload (arg, from_tty, 0, command);
8593}
8594
346774a9
PA
8595/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8596 is non-zero, then make the breakpoint temporary. If COND_STRING is
8597 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8598 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8599
ab04a2af 8600void
346774a9
PA
8601init_catchpoint (struct breakpoint *b,
8602 struct gdbarch *gdbarch, int tempflag,
8603 char *cond_string,
c0a91b2b 8604 const struct breakpoint_ops *ops)
c906108c 8605{
c5aa993b 8606 struct symtab_and_line sal;
346774a9 8607
fe39c653 8608 init_sal (&sal);
6c95b8df 8609 sal.pspace = current_program_space;
c5aa993b 8610
28010a5d 8611 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8612
1b36a34b 8613 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8614 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8615}
8616
28010a5d 8617void
3ea46bff 8618install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8619{
8620 add_to_breakpoint_chain (b);
3a5c3e22 8621 set_breakpoint_number (internal, b);
558a9d82
YQ
8622 if (is_tracepoint (b))
8623 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8624 if (!internal)
8625 mention (b);
c56053d2 8626 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8627
8628 if (update_gll)
44702360 8629 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8630}
8631
9b70b993 8632static void
a6d9a66e
UW
8633create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8634 int tempflag, char *cond_string,
c0a91b2b 8635 const struct breakpoint_ops *ops)
c906108c 8636{
e29a4733 8637 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8638
e29a4733
PA
8639 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8640
8641 c->forked_inferior_pid = null_ptid;
8642
3ea46bff 8643 install_breakpoint (0, &c->base, 1);
c906108c
SS
8644}
8645
fe798b75
JB
8646/* Exec catchpoints. */
8647
b4d90040
PA
8648/* An instance of this type is used to represent an exec catchpoint.
8649 It includes a "struct breakpoint" as a kind of base class; users
8650 downcast to "struct breakpoint *" when needed. A breakpoint is
8651 really of this type iff its ops pointer points to
8652 CATCH_EXEC_BREAKPOINT_OPS. */
8653
8654struct exec_catchpoint
8655{
8656 /* The base class. */
8657 struct breakpoint base;
8658
8659 /* Filename of a program whose exec triggered this catchpoint.
8660 This field is only valid immediately after this catchpoint has
8661 triggered. */
8662 char *exec_pathname;
8663};
8664
8665/* Implement the "dtor" breakpoint_ops method for exec
8666 catchpoints. */
8667
8668static void
8669dtor_catch_exec (struct breakpoint *b)
8670{
8671 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8672
8673 xfree (c->exec_pathname);
348d480f 8674
2060206e 8675 base_breakpoint_ops.dtor (b);
b4d90040
PA
8676}
8677
77b06cd7
TJB
8678static int
8679insert_catch_exec (struct bp_location *bl)
c906108c 8680{
dfd4cc63 8681 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8682}
c906108c 8683
fe798b75 8684static int
77b06cd7 8685remove_catch_exec (struct bp_location *bl)
fe798b75 8686{
dfd4cc63 8687 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8688}
c906108c 8689
fe798b75 8690static int
f1310107 8691breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8692 struct address_space *aspace, CORE_ADDR bp_addr,
8693 const struct target_waitstatus *ws)
fe798b75 8694{
b4d90040
PA
8695 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8696
f90263c1
TT
8697 if (ws->kind != TARGET_WAITKIND_EXECD)
8698 return 0;
8699
8700 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8701 return 1;
fe798b75 8702}
c906108c 8703
fe798b75 8704static enum print_stop_action
348d480f 8705print_it_catch_exec (bpstat bs)
fe798b75 8706{
36dfb11c 8707 struct ui_out *uiout = current_uiout;
348d480f 8708 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8709 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8710
fe798b75 8711 annotate_catchpoint (b->number);
f303dbd6 8712 maybe_print_thread_hit_breakpoint (uiout);
36dfb11c 8713 if (b->disposition == disp_del)
f303dbd6 8714 ui_out_text (uiout, "Temporary catchpoint ");
36dfb11c 8715 else
f303dbd6 8716 ui_out_text (uiout, "Catchpoint ");
36dfb11c
TT
8717 if (ui_out_is_mi_like_p (uiout))
8718 {
8719 ui_out_field_string (uiout, "reason",
8720 async_reason_lookup (EXEC_ASYNC_EXEC));
8721 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8722 }
8723 ui_out_field_int (uiout, "bkptno", b->number);
8724 ui_out_text (uiout, " (exec'd ");
8725 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8726 ui_out_text (uiout, "), ");
8727
fe798b75 8728 return PRINT_SRC_AND_LOC;
c906108c
SS
8729}
8730
fe798b75 8731static void
a6d9a66e 8732print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8733{
b4d90040 8734 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8735 struct value_print_options opts;
79a45e25 8736 struct ui_out *uiout = current_uiout;
fe798b75
JB
8737
8738 get_user_print_options (&opts);
8739
8740 /* Field 4, the address, is omitted (which makes the columns
8741 not line up too nicely with the headers, but the effect
8742 is relatively readable). */
8743 if (opts.addressprint)
8744 ui_out_field_skip (uiout, "addr");
8745 annotate_field (5);
8746 ui_out_text (uiout, "exec");
b4d90040 8747 if (c->exec_pathname != NULL)
fe798b75
JB
8748 {
8749 ui_out_text (uiout, ", program \"");
b4d90040 8750 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8751 ui_out_text (uiout, "\" ");
8752 }
8ac3646f
TT
8753
8754 if (ui_out_is_mi_like_p (uiout))
8755 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8756}
8757
8758static void
8759print_mention_catch_exec (struct breakpoint *b)
8760{
8761 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8762}
8763
6149aea9
PA
8764/* Implement the "print_recreate" breakpoint_ops method for exec
8765 catchpoints. */
8766
8767static void
8768print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8769{
8770 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8771 print_recreate_thread (b, fp);
6149aea9
PA
8772}
8773
2060206e 8774static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8775
c906108c 8776static int
fba45db2 8777hw_breakpoint_used_count (void)
c906108c 8778{
c906108c 8779 int i = 0;
f1310107
TJB
8780 struct breakpoint *b;
8781 struct bp_location *bl;
c906108c
SS
8782
8783 ALL_BREAKPOINTS (b)
c5aa993b 8784 {
d6b74ac4 8785 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8786 for (bl = b->loc; bl; bl = bl->next)
8787 {
8788 /* Special types of hardware breakpoints may use more than
8789 one register. */
348d480f 8790 i += b->ops->resources_needed (bl);
f1310107 8791 }
c5aa993b 8792 }
c906108c
SS
8793
8794 return i;
8795}
8796
a1398e0c
PA
8797/* Returns the resources B would use if it were a hardware
8798 watchpoint. */
8799
c906108c 8800static int
a1398e0c 8801hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8802{
c906108c 8803 int i = 0;
e09342b5 8804 struct bp_location *bl;
c906108c 8805
a1398e0c
PA
8806 if (!breakpoint_enabled (b))
8807 return 0;
8808
8809 for (bl = b->loc; bl; bl = bl->next)
8810 {
8811 /* Special types of hardware watchpoints may use more than
8812 one register. */
8813 i += b->ops->resources_needed (bl);
8814 }
8815
8816 return i;
8817}
8818
8819/* Returns the sum the used resources of all hardware watchpoints of
8820 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8821 the sum of the used resources of all hardware watchpoints of other
8822 types _not_ TYPE. */
8823
8824static int
8825hw_watchpoint_used_count_others (struct breakpoint *except,
8826 enum bptype type, int *other_type_used)
8827{
8828 int i = 0;
8829 struct breakpoint *b;
8830
c906108c
SS
8831 *other_type_used = 0;
8832 ALL_BREAKPOINTS (b)
e09342b5 8833 {
a1398e0c
PA
8834 if (b == except)
8835 continue;
e09342b5
TJB
8836 if (!breakpoint_enabled (b))
8837 continue;
8838
a1398e0c
PA
8839 if (b->type == type)
8840 i += hw_watchpoint_use_count (b);
8841 else if (is_hardware_watchpoint (b))
8842 *other_type_used = 1;
e09342b5
TJB
8843 }
8844
c906108c
SS
8845 return i;
8846}
8847
c906108c 8848void
fba45db2 8849disable_watchpoints_before_interactive_call_start (void)
c906108c 8850{
c5aa993b 8851 struct breakpoint *b;
c906108c
SS
8852
8853 ALL_BREAKPOINTS (b)
c5aa993b 8854 {
cc60f2e3 8855 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8856 {
b5de0fa7 8857 b->enable_state = bp_call_disabled;
44702360 8858 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8859 }
8860 }
c906108c
SS
8861}
8862
8863void
fba45db2 8864enable_watchpoints_after_interactive_call_stop (void)
c906108c 8865{
c5aa993b 8866 struct breakpoint *b;
c906108c
SS
8867
8868 ALL_BREAKPOINTS (b)
c5aa993b 8869 {
cc60f2e3 8870 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8871 {
b5de0fa7 8872 b->enable_state = bp_enabled;
44702360 8873 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8874 }
8875 }
c906108c
SS
8876}
8877
8bea4e01
UW
8878void
8879disable_breakpoints_before_startup (void)
8880{
6c95b8df 8881 current_program_space->executing_startup = 1;
44702360 8882 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8883}
8884
8885void
8886enable_breakpoints_after_startup (void)
8887{
6c95b8df 8888 current_program_space->executing_startup = 0;
f8eba3c6 8889 breakpoint_re_set ();
8bea4e01
UW
8890}
8891
7c16b83e
PA
8892/* Create a new single-step breakpoint for thread THREAD, with no
8893 locations. */
c906108c 8894
7c16b83e
PA
8895static struct breakpoint *
8896new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8897{
8898 struct breakpoint *b = XNEW (struct breakpoint);
8899
8900 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8901 &momentary_breakpoint_ops);
8902
8903 b->disposition = disp_donttouch;
8904 b->frame_id = null_frame_id;
8905
8906 b->thread = thread;
8907 gdb_assert (b->thread != 0);
8908
8909 add_to_breakpoint_chain (b);
8910
8911 return b;
8912}
8913
8914/* Set a momentary breakpoint of type TYPE at address specified by
8915 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8916 frame. */
c906108c
SS
8917
8918struct breakpoint *
a6d9a66e
UW
8919set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8920 struct frame_id frame_id, enum bptype type)
c906108c 8921{
52f0bd74 8922 struct breakpoint *b;
edb3359d 8923
193facb3
JK
8924 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8925 tail-called one. */
8926 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8927
06edf0c0 8928 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8929 b->enable_state = bp_enabled;
8930 b->disposition = disp_donttouch;
818dd999 8931 b->frame_id = frame_id;
c906108c 8932
4a64f543
MS
8933 /* If we're debugging a multi-threaded program, then we want
8934 momentary breakpoints to be active in only a single thread of
8935 control. */
39f77062 8936 if (in_thread_list (inferior_ptid))
5d5658a1 8937 b->thread = ptid_to_global_thread_id (inferior_ptid);
c906108c 8938
44702360 8939 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8940
c906108c
SS
8941 return b;
8942}
611c83ae 8943
06edf0c0 8944/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8945 The new breakpoint will have type TYPE, use OPS as its
8946 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8947
06edf0c0
PA
8948static struct breakpoint *
8949momentary_breakpoint_from_master (struct breakpoint *orig,
8950 enum bptype type,
a1aa2221
LM
8951 const struct breakpoint_ops *ops,
8952 int loc_enabled)
e58b0e63
PA
8953{
8954 struct breakpoint *copy;
8955
06edf0c0 8956 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8957 copy->loc = allocate_bp_location (copy);
0e30163f 8958 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8959
a6d9a66e 8960 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8961 copy->loc->requested_address = orig->loc->requested_address;
8962 copy->loc->address = orig->loc->address;
8963 copy->loc->section = orig->loc->section;
6c95b8df 8964 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8965 copy->loc->probe = orig->loc->probe;
f8eba3c6 8966 copy->loc->line_number = orig->loc->line_number;
2f202fde 8967 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8968 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8969 copy->frame_id = orig->frame_id;
8970 copy->thread = orig->thread;
6c95b8df 8971 copy->pspace = orig->pspace;
e58b0e63
PA
8972
8973 copy->enable_state = bp_enabled;
8974 copy->disposition = disp_donttouch;
8975 copy->number = internal_breakpoint_number--;
8976
44702360 8977 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8978 return copy;
8979}
8980
06edf0c0
PA
8981/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8982 ORIG is NULL. */
8983
8984struct breakpoint *
8985clone_momentary_breakpoint (struct breakpoint *orig)
8986{
8987 /* If there's nothing to clone, then return nothing. */
8988 if (orig == NULL)
8989 return NULL;
8990
a1aa2221 8991 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8992}
8993
611c83ae 8994struct breakpoint *
a6d9a66e
UW
8995set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8996 enum bptype type)
611c83ae
PA
8997{
8998 struct symtab_and_line sal;
8999
9000 sal = find_pc_line (pc, 0);
9001 sal.pc = pc;
9002 sal.section = find_pc_overlay (pc);
9003 sal.explicit_pc = 1;
9004
a6d9a66e 9005 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9006}
c906108c 9007\f
c5aa993b 9008
c906108c
SS
9009/* Tell the user we have just set a breakpoint B. */
9010
9011static void
fba45db2 9012mention (struct breakpoint *b)
c906108c 9013{
348d480f 9014 b->ops->print_mention (b);
79a45e25 9015 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9016 return;
c906108c
SS
9017 printf_filtered ("\n");
9018}
c906108c 9019\f
c5aa993b 9020
1a853c52
PA
9021static int bp_loc_is_permanent (struct bp_location *loc);
9022
0d381245 9023static struct bp_location *
39d61571 9024add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9025 const struct symtab_and_line *sal)
9026{
9027 struct bp_location *loc, **tmp;
3742cc8b
YQ
9028 CORE_ADDR adjusted_address;
9029 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9030
9031 if (loc_gdbarch == NULL)
9032 loc_gdbarch = b->gdbarch;
9033
9034 /* Adjust the breakpoint's address prior to allocating a location.
9035 Once we call allocate_bp_location(), that mostly uninitialized
9036 location will be placed on the location chain. Adjustment of the
9037 breakpoint may cause target_read_memory() to be called and we do
9038 not want its scan of the location chain to find a breakpoint and
9039 location that's only been partially initialized. */
9040 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9041 sal->pc, b->type);
0d381245 9042
d30113d4 9043 /* Sort the locations by their ADDRESS. */
39d61571 9044 loc = allocate_bp_location (b);
d30113d4
JK
9045 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9046 tmp = &((*tmp)->next))
0d381245 9047 ;
d30113d4 9048 loc->next = *tmp;
0d381245 9049 *tmp = loc;
3742cc8b 9050
0d381245 9051 loc->requested_address = sal->pc;
3742cc8b 9052 loc->address = adjusted_address;
6c95b8df 9053 loc->pspace = sal->pspace;
729662a5
TT
9054 loc->probe.probe = sal->probe;
9055 loc->probe.objfile = sal->objfile;
6c95b8df 9056 gdb_assert (loc->pspace != NULL);
0d381245 9057 loc->section = sal->section;
3742cc8b 9058 loc->gdbarch = loc_gdbarch;
f8eba3c6 9059 loc->line_number = sal->line;
2f202fde 9060 loc->symtab = sal->symtab;
f8eba3c6 9061
0e30163f
JK
9062 set_breakpoint_location_function (loc,
9063 sal->explicit_pc || sal->explicit_line);
1a853c52 9064
6ae88661
LM
9065 /* While by definition, permanent breakpoints are already present in the
9066 code, we don't mark the location as inserted. Normally one would expect
9067 that GDB could rely on that breakpoint instruction to stop the program,
9068 thus removing the need to insert its own breakpoint, except that executing
9069 the breakpoint instruction can kill the target instead of reporting a
9070 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9071 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9072 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9073 breakpoint be inserted normally results in QEMU knowing about the GDB
9074 breakpoint, and thus trap before the breakpoint instruction is executed.
9075 (If GDB later needs to continue execution past the permanent breakpoint,
9076 it manually increments the PC, thus avoiding executing the breakpoint
9077 instruction.) */
1a853c52 9078 if (bp_loc_is_permanent (loc))
6ae88661 9079 loc->permanent = 1;
1a853c52 9080
0d381245
VP
9081 return loc;
9082}
514f746b
AR
9083\f
9084
1cf4d951 9085/* See breakpoint.h. */
514f746b 9086
1cf4d951
PA
9087int
9088program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
9089{
9090 int len;
9091 CORE_ADDR addr;
1afeeb75 9092 const gdb_byte *bpoint;
514f746b 9093 gdb_byte *target_mem;
939c61fa
JK
9094 struct cleanup *cleanup;
9095 int retval = 0;
514f746b 9096
1cf4d951
PA
9097 addr = address;
9098 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9099
9100 /* Software breakpoints unsupported? */
9101 if (bpoint == NULL)
9102 return 0;
9103
224c3ddb 9104 target_mem = (gdb_byte *) alloca (len);
1cf4d951
PA
9105
9106 /* Enable the automatic memory restoration from breakpoints while
9107 we read the memory. Otherwise we could say about our temporary
9108 breakpoints they are permanent. */
9109 cleanup = make_show_memory_breakpoints_cleanup (0);
9110
9111 if (target_read_memory (address, target_mem, len) == 0
9112 && memcmp (target_mem, bpoint, len) == 0)
9113 retval = 1;
9114
9115 do_cleanups (cleanup);
9116
9117 return retval;
9118}
9119
9120/* Return 1 if LOC is pointing to a permanent breakpoint,
9121 return 0 otherwise. */
9122
9123static int
9124bp_loc_is_permanent (struct bp_location *loc)
9125{
9126 struct cleanup *cleanup;
9127 int retval;
9128
514f746b
AR
9129 gdb_assert (loc != NULL);
9130
244558af
LM
9131 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9132 attempt to read from the addresses the locations of these breakpoint types
9133 point to. program_breakpoint_here_p, below, will attempt to read
9134 memory. */
9135 if (!breakpoint_address_is_meaningful (loc->owner))
9136 return 0;
9137
6c95b8df 9138 cleanup = save_current_space_and_thread ();
6c95b8df 9139 switch_to_program_space_and_thread (loc->pspace);
939c61fa 9140
1cf4d951 9141 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b 9142
939c61fa
JK
9143 do_cleanups (cleanup);
9144
9145 return retval;
514f746b
AR
9146}
9147
e7e0cddf
SS
9148/* Build a command list for the dprintf corresponding to the current
9149 settings of the dprintf style options. */
9150
9151static void
9152update_dprintf_command_list (struct breakpoint *b)
9153{
9154 char *dprintf_args = b->extra_string;
9155 char *printf_line = NULL;
9156
9157 if (!dprintf_args)
9158 return;
9159
9160 dprintf_args = skip_spaces (dprintf_args);
9161
9162 /* Allow a comma, as it may have terminated a location, but don't
9163 insist on it. */
9164 if (*dprintf_args == ',')
9165 ++dprintf_args;
9166 dprintf_args = skip_spaces (dprintf_args);
9167
9168 if (*dprintf_args != '"')
9169 error (_("Bad format string, missing '\"'."));
9170
d3ce09f5 9171 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9172 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9173 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9174 {
9175 if (!dprintf_function)
9176 error (_("No function supplied for dprintf call"));
9177
9178 if (dprintf_channel && strlen (dprintf_channel) > 0)
9179 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9180 dprintf_function,
9181 dprintf_channel,
9182 dprintf_args);
9183 else
9184 printf_line = xstrprintf ("call (void) %s (%s)",
9185 dprintf_function,
9186 dprintf_args);
9187 }
d3ce09f5
SS
9188 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9189 {
9190 if (target_can_run_breakpoint_commands ())
9191 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9192 else
9193 {
9194 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9195 printf_line = xstrprintf ("printf %s", dprintf_args);
9196 }
9197 }
e7e0cddf
SS
9198 else
9199 internal_error (__FILE__, __LINE__,
9200 _("Invalid dprintf style."));
9201
f28045c2 9202 gdb_assert (printf_line != NULL);
9d6e6e84 9203 /* Manufacture a printf sequence. */
f28045c2 9204 {
8d749320 9205 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 9206
f28045c2
YQ
9207 printf_cmd_line->control_type = simple_control;
9208 printf_cmd_line->body_count = 0;
9209 printf_cmd_line->body_list = NULL;
9d6e6e84 9210 printf_cmd_line->next = NULL;
f28045c2 9211 printf_cmd_line->line = printf_line;
e7e0cddf 9212
f28045c2
YQ
9213 breakpoint_set_commands (b, printf_cmd_line);
9214 }
e7e0cddf
SS
9215}
9216
9217/* Update all dprintf commands, making their command lists reflect
9218 current style settings. */
9219
9220static void
9221update_dprintf_commands (char *args, int from_tty,
9222 struct cmd_list_element *c)
9223{
9224 struct breakpoint *b;
9225
9226 ALL_BREAKPOINTS (b)
9227 {
9228 if (b->type == bp_dprintf)
9229 update_dprintf_command_list (b);
9230 }
9231}
c3f6f71d 9232
f00aae0f
KS
9233/* Create a breakpoint with SAL as location. Use LOCATION
9234 as a description of the location, and COND_STRING
db107f19 9235 as condition expression. */
018d34a4
VP
9236
9237static void
d9b3f62e 9238init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
f00aae0f
KS
9239 struct symtabs_and_lines sals,
9240 struct event_location *location,
f8eba3c6 9241 char *filter, char *cond_string,
e7e0cddf 9242 char *extra_string,
d9b3f62e
PA
9243 enum bptype type, enum bpdisp disposition,
9244 int thread, int task, int ignore_count,
c0a91b2b 9245 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9246 int enabled, int internal, unsigned flags,
9247 int display_canonical)
018d34a4 9248{
0d381245 9249 int i;
018d34a4
VP
9250
9251 if (type == bp_hardware_breakpoint)
9252 {
fbbd034e
AS
9253 int target_resources_ok;
9254
9255 i = hw_breakpoint_used_count ();
9256 target_resources_ok =
9257 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9258 i + 1, 0);
9259 if (target_resources_ok == 0)
9260 error (_("No hardware breakpoint support in the target."));
9261 else if (target_resources_ok < 0)
9262 error (_("Hardware breakpoints used exceeds limit."));
9263 }
9264
6c95b8df
PA
9265 gdb_assert (sals.nelts > 0);
9266
0d381245
VP
9267 for (i = 0; i < sals.nelts; ++i)
9268 {
9269 struct symtab_and_line sal = sals.sals[i];
9270 struct bp_location *loc;
9271
9272 if (from_tty)
5af949e3
UW
9273 {
9274 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9275 if (!loc_gdbarch)
9276 loc_gdbarch = gdbarch;
9277
9278 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9279 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9280 }
0d381245
VP
9281
9282 if (i == 0)
9283 {
d9b3f62e 9284 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9285 b->thread = thread;
4a306c9a 9286 b->task = task;
855a6e68 9287
0d381245 9288 b->cond_string = cond_string;
e7e0cddf 9289 b->extra_string = extra_string;
0d381245 9290 b->ignore_count = ignore_count;
41447f92 9291 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9292 b->disposition = disposition;
6c95b8df 9293
44f238bb
PA
9294 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9295 b->loc->inserted = 1;
9296
0fb4aa4b
PA
9297 if (type == bp_static_tracepoint)
9298 {
d9b3f62e 9299 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9300 struct static_tracepoint_marker marker;
9301
983af33b 9302 if (strace_marker_p (b))
0fb4aa4b
PA
9303 {
9304 /* We already know the marker exists, otherwise, we
9305 wouldn't see a sal for it. */
f00aae0f
KS
9306 const char *p = &event_location_to_string (b->location)[3];
9307 const char *endp;
0fb4aa4b 9308 char *marker_str;
0fb4aa4b 9309
f00aae0f 9310 p = skip_spaces_const (p);
0fb4aa4b 9311
f00aae0f 9312 endp = skip_to_space_const (p);
0fb4aa4b
PA
9313
9314 marker_str = savestring (p, endp - p);
d9b3f62e 9315 t->static_trace_marker_id = marker_str;
0fb4aa4b 9316
3e43a32a
MS
9317 printf_filtered (_("Probed static tracepoint "
9318 "marker \"%s\"\n"),
d9b3f62e 9319 t->static_trace_marker_id);
0fb4aa4b
PA
9320 }
9321 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9322 {
d9b3f62e 9323 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9324 release_static_tracepoint_marker (&marker);
9325
3e43a32a
MS
9326 printf_filtered (_("Probed static tracepoint "
9327 "marker \"%s\"\n"),
d9b3f62e 9328 t->static_trace_marker_id);
0fb4aa4b
PA
9329 }
9330 else
3e43a32a
MS
9331 warning (_("Couldn't determine the static "
9332 "tracepoint marker to probe"));
0fb4aa4b
PA
9333 }
9334
0d381245
VP
9335 loc = b->loc;
9336 }
9337 else
018d34a4 9338 {
39d61571 9339 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9340 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9341 loc->inserted = 1;
0d381245
VP
9342 }
9343
9344 if (b->cond_string)
9345 {
bbc13ae3
KS
9346 const char *arg = b->cond_string;
9347
1bb9788d
TT
9348 loc->cond = parse_exp_1 (&arg, loc->address,
9349 block_for_pc (loc->address), 0);
0d381245 9350 if (*arg)
588ae58c 9351 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9352 }
e7e0cddf
SS
9353
9354 /* Dynamic printf requires and uses additional arguments on the
9355 command line, otherwise it's an error. */
9356 if (type == bp_dprintf)
9357 {
9358 if (b->extra_string)
9359 update_dprintf_command_list (b);
9360 else
9361 error (_("Format string required"));
9362 }
9363 else if (b->extra_string)
588ae58c 9364 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9365 }
018d34a4 9366
56435ebe 9367 b->display_canonical = display_canonical;
f00aae0f
KS
9368 if (location != NULL)
9369 b->location = location;
018d34a4 9370 else
a06efdd6 9371 b->location = new_address_location (b->loc->address);
f8eba3c6 9372 b->filter = filter;
d9b3f62e 9373}
018d34a4 9374
d9b3f62e
PA
9375static void
9376create_breakpoint_sal (struct gdbarch *gdbarch,
f00aae0f
KS
9377 struct symtabs_and_lines sals,
9378 struct event_location *location,
f8eba3c6 9379 char *filter, char *cond_string,
e7e0cddf 9380 char *extra_string,
d9b3f62e
PA
9381 enum bptype type, enum bpdisp disposition,
9382 int thread, int task, int ignore_count,
c0a91b2b 9383 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9384 int enabled, int internal, unsigned flags,
9385 int display_canonical)
d9b3f62e
PA
9386{
9387 struct breakpoint *b;
9388 struct cleanup *old_chain;
9389
9390 if (is_tracepoint_type (type))
9391 {
9392 struct tracepoint *t;
9393
9394 t = XCNEW (struct tracepoint);
9395 b = &t->base;
9396 }
9397 else
9398 b = XNEW (struct breakpoint);
9399
9400 old_chain = make_cleanup (xfree, b);
9401
9402 init_breakpoint_sal (b, gdbarch,
f00aae0f 9403 sals, location,
e7e0cddf 9404 filter, cond_string, extra_string,
d9b3f62e
PA
9405 type, disposition,
9406 thread, task, ignore_count,
9407 ops, from_tty,
44f238bb
PA
9408 enabled, internal, flags,
9409 display_canonical);
d9b3f62e
PA
9410 discard_cleanups (old_chain);
9411
3ea46bff 9412 install_breakpoint (internal, b, 0);
018d34a4
VP
9413}
9414
9415/* Add SALS.nelts breakpoints to the breakpoint table. For each
9416 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9417 value. COND_STRING, if not NULL, specified the condition to be
9418 used for all breakpoints. Essentially the only case where
9419 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9420 function. In that case, it's still not possible to specify
9421 separate conditions for different overloaded functions, so
9422 we take just a single condition string.
9423
c3f6f71d 9424 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9425 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9426 array contents). If the function fails (error() is called), the
9427 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9428 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9429
9430static void
8cdf0e15 9431create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9432 struct linespec_result *canonical,
e7e0cddf 9433 char *cond_string, char *extra_string,
8cdf0e15
VP
9434 enum bptype type, enum bpdisp disposition,
9435 int thread, int task, int ignore_count,
c0a91b2b 9436 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9437 int enabled, int internal, unsigned flags)
c906108c 9438{
018d34a4 9439 int i;
f8eba3c6 9440 struct linespec_sals *lsal;
cc59ec59 9441
f8eba3c6
TT
9442 if (canonical->pre_expanded)
9443 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9444
9445 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9446 {
f00aae0f 9447 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9448 'break', without arguments. */
f00aae0f
KS
9449 struct event_location *location
9450 = (canonical->location != NULL
9451 ? copy_event_location (canonical->location) : NULL);
f8eba3c6 9452 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
f00aae0f 9453 struct cleanup *inner = make_cleanup_delete_event_location (location);
0d381245 9454
f8eba3c6
TT
9455 make_cleanup (xfree, filter_string);
9456 create_breakpoint_sal (gdbarch, lsal->sals,
f00aae0f 9457 location,
f8eba3c6 9458 filter_string,
e7e0cddf
SS
9459 cond_string, extra_string,
9460 type, disposition,
84f4c1fe 9461 thread, task, ignore_count, ops,
44f238bb 9462 from_tty, enabled, internal, flags,
56435ebe 9463 canonical->special_display);
f8eba3c6 9464 discard_cleanups (inner);
c3f6f71d 9465 }
c3f6f71d 9466}
c906108c 9467
f00aae0f 9468/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9469 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9470 addresses found. LOCATION points to the end of the SAL (for
9471 linespec locations).
9998af43
TJB
9472
9473 The array and the line spec strings are allocated on the heap, it is
9474 the caller's responsibility to free them. */
c906108c 9475
b9362cc7 9476static void
f00aae0f 9477parse_breakpoint_sals (const struct event_location *location,
58438ac1 9478 struct linespec_result *canonical)
c3f6f71d 9479{
f00aae0f
KS
9480 struct symtab_and_line cursal;
9481
9482 if (event_location_type (location) == LINESPEC_LOCATION)
9483 {
9484 const char *address = get_linespec_location (location);
9485
9486 if (address == NULL)
9487 {
9488 /* The last displayed codepoint, if it's valid, is our default
9489 breakpoint address. */
9490 if (last_displayed_sal_is_valid ())
9491 {
9492 struct linespec_sals lsal;
9493 struct symtab_and_line sal;
9494 CORE_ADDR pc;
9495
9496 init_sal (&sal); /* Initialize to zeroes. */
8d749320 9497 lsal.sals.sals = XNEW (struct symtab_and_line);
f00aae0f
KS
9498
9499 /* Set sal's pspace, pc, symtab, and line to the values
9500 corresponding to the last call to print_frame_info.
9501 Be sure to reinitialize LINE with NOTCURRENT == 0
9502 as the breakpoint line number is inappropriate otherwise.
9503 find_pc_line would adjust PC, re-set it back. */
9504 get_last_displayed_sal (&sal);
9505 pc = sal.pc;
9506 sal = find_pc_line (pc, 0);
9507
9508 /* "break" without arguments is equivalent to "break *PC"
9509 where PC is the last displayed codepoint's address. So
9510 make sure to set sal.explicit_pc to prevent GDB from
9511 trying to expand the list of sals to include all other
9512 instances with the same symtab and line. */
9513 sal.pc = pc;
9514 sal.explicit_pc = 1;
9515
9516 lsal.sals.sals[0] = sal;
9517 lsal.sals.nelts = 1;
9518 lsal.canonical = NULL;
9519
9520 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9521 return;
9522 }
9523 else
9524 error (_("No default breakpoint address now."));
c906108c 9525 }
c906108c 9526 }
f00aae0f
KS
9527
9528 /* Force almost all breakpoints to be in terms of the
9529 current_source_symtab (which is decode_line_1's default).
9530 This should produce the results we want almost all of the
9531 time while leaving default_breakpoint_* alone.
9532
9533 ObjC: However, don't match an Objective-C method name which
9534 may have a '+' or '-' succeeded by a '['. */
9535 cursal = get_current_source_symtab_and_line ();
9536 if (last_displayed_sal_is_valid ())
c906108c 9537 {
f00aae0f 9538 const char *address = NULL;
cc80f267 9539
f00aae0f
KS
9540 if (event_location_type (location) == LINESPEC_LOCATION)
9541 address = get_linespec_location (location);
cc80f267 9542
f00aae0f
KS
9543 if (!cursal.symtab
9544 || (address != NULL
9545 && strchr ("+-", address[0]) != NULL
9546 && address[1] != '['))
9547 {
9548 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
9549 get_last_displayed_symtab (),
9550 get_last_displayed_line (),
9551 canonical, NULL, NULL);
9552 return;
9553 }
c906108c 9554 }
f00aae0f
KS
9555
9556 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
9557 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9558}
c906108c 9559
c906108c 9560
c3f6f71d 9561/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9562 inserted as a breakpoint. If it can't throw an error. */
c906108c 9563
b9362cc7 9564static void
23e7acfb 9565breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9566{
9567 int i;
cc59ec59 9568
c3f6f71d 9569 for (i = 0; i < sals->nelts; i++)
ee53e872 9570 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9571}
9572
7a697b8d
SS
9573/* Fast tracepoints may have restrictions on valid locations. For
9574 instance, a fast tracepoint using a jump instead of a trap will
9575 likely have to overwrite more bytes than a trap would, and so can
9576 only be placed where the instruction is longer than the jump, or a
9577 multi-instruction sequence does not have a jump into the middle of
9578 it, etc. */
9579
9580static void
9581check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9582 struct symtabs_and_lines *sals)
9583{
9584 int i, rslt;
9585 struct symtab_and_line *sal;
9586 char *msg;
9587 struct cleanup *old_chain;
9588
9589 for (i = 0; i < sals->nelts; i++)
9590 {
f8eba3c6
TT
9591 struct gdbarch *sarch;
9592
7a697b8d
SS
9593 sal = &sals->sals[i];
9594
f8eba3c6
TT
9595 sarch = get_sal_arch (*sal);
9596 /* We fall back to GDBARCH if there is no architecture
9597 associated with SAL. */
9598 if (sarch == NULL)
9599 sarch = gdbarch;
6b940e6a 9600 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
7a697b8d
SS
9601 old_chain = make_cleanup (xfree, msg);
9602
9603 if (!rslt)
9604 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9605 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9606
9607 do_cleanups (old_chain);
9608 }
9609}
9610
018d34a4
VP
9611/* Given TOK, a string specification of condition and thread, as
9612 accepted by the 'break' command, extract the condition
9613 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9614 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9615 If no condition is found, *COND_STRING is set to NULL.
9616 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9617
9618static void
bbc13ae3 9619find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9620 char **cond_string, int *thread, int *task,
9621 char **rest)
018d34a4
VP
9622{
9623 *cond_string = NULL;
9624 *thread = -1;
ed1d1739
KS
9625 *task = 0;
9626 *rest = NULL;
9627
018d34a4
VP
9628 while (tok && *tok)
9629 {
bbc13ae3 9630 const char *end_tok;
018d34a4 9631 int toklen;
bbc13ae3
KS
9632 const char *cond_start = NULL;
9633 const char *cond_end = NULL;
cc59ec59 9634
bbc13ae3 9635 tok = skip_spaces_const (tok);
e7e0cddf
SS
9636
9637 if ((*tok == '"' || *tok == ',') && rest)
9638 {
9639 *rest = savestring (tok, strlen (tok));
9640 return;
9641 }
9642
bbc13ae3 9643 end_tok = skip_to_space_const (tok);
d634f2de 9644
018d34a4 9645 toklen = end_tok - tok;
d634f2de 9646
018d34a4
VP
9647 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9648 {
f7545552
TT
9649 struct expression *expr;
9650
018d34a4 9651 tok = cond_start = end_tok + 1;
1bb9788d 9652 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9653 xfree (expr);
018d34a4 9654 cond_end = tok;
d634f2de 9655 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9656 }
9657 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9658 {
5d5658a1
PA
9659 const char *tmptok;
9660 struct thread_info *thr;
d634f2de 9661
018d34a4 9662 tok = end_tok + 1;
5d5658a1 9663 thr = parse_thread_id (tok, &tmptok);
018d34a4
VP
9664 if (tok == tmptok)
9665 error (_("Junk after thread keyword."));
5d5658a1 9666 *thread = thr->global_num;
bbc13ae3 9667 tok = tmptok;
018d34a4 9668 }
4a306c9a
JB
9669 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9670 {
9671 char *tmptok;
9672
9673 tok = end_tok + 1;
bbc13ae3 9674 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9675 if (tok == tmptok)
9676 error (_("Junk after task keyword."));
9677 if (!valid_task_id (*task))
b6199126 9678 error (_("Unknown task %d."), *task);
bbc13ae3 9679 tok = tmptok;
4a306c9a 9680 }
e7e0cddf
SS
9681 else if (rest)
9682 {
9683 *rest = savestring (tok, strlen (tok));
ccab2054 9684 return;
e7e0cddf 9685 }
018d34a4
VP
9686 else
9687 error (_("Junk at end of arguments."));
9688 }
9689}
9690
0fb4aa4b
PA
9691/* Decode a static tracepoint marker spec. */
9692
9693static struct symtabs_and_lines
f00aae0f 9694decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9695{
9696 VEC(static_tracepoint_marker_p) *markers = NULL;
9697 struct symtabs_and_lines sals;
0fb4aa4b 9698 struct cleanup *old_chain;
f00aae0f
KS
9699 const char *p = &(*arg_p)[3];
9700 const char *endp;
0fb4aa4b
PA
9701 char *marker_str;
9702 int i;
9703
f00aae0f 9704 p = skip_spaces_const (p);
0fb4aa4b 9705
f00aae0f 9706 endp = skip_to_space_const (p);
0fb4aa4b
PA
9707
9708 marker_str = savestring (p, endp - p);
9709 old_chain = make_cleanup (xfree, marker_str);
9710
9711 markers = target_static_tracepoint_markers_by_strid (marker_str);
9712 if (VEC_empty(static_tracepoint_marker_p, markers))
9713 error (_("No known static tracepoint marker named %s"), marker_str);
9714
9715 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8d749320 9716 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
0fb4aa4b
PA
9717
9718 for (i = 0; i < sals.nelts; i++)
9719 {
9720 struct static_tracepoint_marker *marker;
9721
9722 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9723
9724 init_sal (&sals.sals[i]);
9725
9726 sals.sals[i] = find_pc_line (marker->address, 0);
9727 sals.sals[i].pc = marker->address;
9728
9729 release_static_tracepoint_marker (marker);
9730 }
9731
9732 do_cleanups (old_chain);
9733
9734 *arg_p = endp;
9735 return sals;
9736}
9737
f00aae0f 9738/* See breakpoint.h. */
0101ce28 9739
8cdf0e15
VP
9740int
9741create_breakpoint (struct gdbarch *gdbarch,
f00aae0f 9742 const struct event_location *location, char *cond_string,
e7e0cddf 9743 int thread, char *extra_string,
f00aae0f 9744 int parse_extra,
0fb4aa4b 9745 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9746 int ignore_count,
9747 enum auto_boolean pending_break_support,
c0a91b2b 9748 const struct breakpoint_ops *ops,
44f238bb
PA
9749 int from_tty, int enabled, int internal,
9750 unsigned flags)
c3f6f71d 9751{
7efd8fc2 9752 struct linespec_result canonical;
c3f6f71d 9753 struct cleanup *old_chain;
80c99de1 9754 struct cleanup *bkpt_chain = NULL;
0101ce28 9755 int pending = 0;
4a306c9a 9756 int task = 0;
86b17b60 9757 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9758
348d480f
PA
9759 gdb_assert (ops != NULL);
9760
f00aae0f
KS
9761 /* If extra_string isn't useful, set it to NULL. */
9762 if (extra_string != NULL && *extra_string == '\0')
9763 extra_string = NULL;
9764
7efd8fc2 9765 init_linespec_result (&canonical);
c3f6f71d 9766
492d29ea 9767 TRY
b78a6381 9768 {
f00aae0f 9769 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9770 }
492d29ea 9771 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9772 {
492d29ea
PA
9773 /* If caller is interested in rc value from parse, set
9774 value. */
9775 if (e.error == NOT_FOUND_ERROR)
0101ce28 9776 {
05ff989b
AC
9777 /* If pending breakpoint support is turned off, throw
9778 error. */
fa8d40ab
JJ
9779
9780 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9781 throw_exception (e);
9782
9783 exception_print (gdb_stderr, e);
fa8d40ab 9784
05ff989b
AC
9785 /* If pending breakpoint support is auto query and the user
9786 selects no, then simply return the error code. */
059fb39f 9787 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9788 && !nquery (_("Make %s pending on future shared library load? "),
9789 bptype_string (type_wanted)))
fd9b8c24 9790 return 0;
fa8d40ab 9791
05ff989b
AC
9792 /* At this point, either the user was queried about setting
9793 a pending breakpoint and selected yes, or pending
9794 breakpoint behavior is on and thus a pending breakpoint
9795 is defaulted on behalf of the user. */
f00aae0f 9796 pending = 1;
0101ce28 9797 }
492d29ea
PA
9798 else
9799 throw_exception (e);
0101ce28 9800 }
492d29ea
PA
9801 END_CATCH
9802
f00aae0f 9803 if (!pending && VEC_empty (linespec_sals, canonical.sals))
492d29ea 9804 return 0;
c3f6f71d 9805
4a64f543 9806 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9807 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9808
c3f6f71d
JM
9809 /* ----------------------------- SNIP -----------------------------
9810 Anything added to the cleanup chain beyond this point is assumed
9811 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9812 then the memory is not reclaimed. */
9813 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9814
c3f6f71d
JM
9815 /* Resolve all line numbers to PC's and verify that the addresses
9816 are ok for the target. */
0101ce28 9817 if (!pending)
f8eba3c6
TT
9818 {
9819 int ix;
9820 struct linespec_sals *iter;
9821
9822 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9823 breakpoint_sals_to_pc (&iter->sals);
9824 }
c3f6f71d 9825
7a697b8d 9826 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9827 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9828 {
9829 int ix;
9830 struct linespec_sals *iter;
9831
9832 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9833 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9834 }
7a697b8d 9835
c3f6f71d
JM
9836 /* Verify that condition can be parsed, before setting any
9837 breakpoints. Allocate a separate condition expression for each
4a64f543 9838 breakpoint. */
0101ce28 9839 if (!pending)
c3f6f71d 9840 {
f00aae0f 9841 if (parse_extra)
72b2ff0e 9842 {
0878d0fa 9843 char *rest;
52d361e1
YQ
9844 struct linespec_sals *lsal;
9845
9846 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9847
0878d0fa
YQ
9848 /* Here we only parse 'arg' to separate condition
9849 from thread number, so parsing in context of first
9850 sal is OK. When setting the breakpoint we'll
9851 re-parse it in context of each sal. */
9852
f00aae0f
KS
9853 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9854 &cond_string, &thread, &task, &rest);
0878d0fa
YQ
9855 if (cond_string)
9856 make_cleanup (xfree, cond_string);
9857 if (rest)
9858 make_cleanup (xfree, rest);
9859 if (rest)
9860 extra_string = rest;
f00aae0f
KS
9861 else
9862 extra_string = NULL;
72b2ff0e 9863 }
2f069f6f 9864 else
72b2ff0e 9865 {
f00aae0f
KS
9866 if (type_wanted != bp_dprintf
9867 && extra_string != NULL && *extra_string != '\0')
9868 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9869
9870 /* Create a private copy of condition string. */
9871 if (cond_string)
9872 {
9873 cond_string = xstrdup (cond_string);
9874 make_cleanup (xfree, cond_string);
9875 }
9876 /* Create a private copy of any extra string. */
9877 if (extra_string)
9878 {
9879 extra_string = xstrdup (extra_string);
9880 make_cleanup (xfree, extra_string);
9881 }
72b2ff0e 9882 }
0fb4aa4b 9883
52d361e1 9884 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9885 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9886 tempflag ? disp_del : disp_donttouch,
9887 thread, task, ignore_count, ops,
44f238bb 9888 from_tty, enabled, internal, flags);
c906108c 9889 }
0101ce28
JJ
9890 else
9891 {
0101ce28
JJ
9892 struct breakpoint *b;
9893
bfccc43c
YQ
9894 if (is_tracepoint_type (type_wanted))
9895 {
9896 struct tracepoint *t;
9897
9898 t = XCNEW (struct tracepoint);
9899 b = &t->base;
9900 }
9901 else
9902 b = XNEW (struct breakpoint);
9903
9904 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
f00aae0f 9905 b->location = copy_event_location (location);
bfccc43c 9906
f00aae0f
KS
9907 if (parse_extra)
9908 b->cond_string = NULL;
e12c7713
MK
9909 else
9910 {
9911 /* Create a private copy of condition string. */
9912 if (cond_string)
9913 {
9914 cond_string = xstrdup (cond_string);
9915 make_cleanup (xfree, cond_string);
9916 }
9917 b->cond_string = cond_string;
15630549 9918 b->thread = thread;
e12c7713 9919 }
f00aae0f
KS
9920
9921 /* Create a private copy of any extra string. */
9922 if (extra_string != NULL)
9923 {
9924 extra_string = xstrdup (extra_string);
9925 make_cleanup (xfree, extra_string);
9926 }
9927 b->extra_string = extra_string;
0101ce28 9928 b->ignore_count = ignore_count;
0101ce28 9929 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9930 b->condition_not_parsed = 1;
41447f92 9931 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9932 if ((type_wanted != bp_breakpoint
9933 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9934 b->pspace = current_program_space;
8bea4e01 9935
bfccc43c 9936 install_breakpoint (internal, b, 0);
0101ce28
JJ
9937 }
9938
f8eba3c6 9939 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9940 {
3e43a32a
MS
9941 warning (_("Multiple breakpoints were set.\nUse the "
9942 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9943 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9944 }
9945
80c99de1
PA
9946 /* That's it. Discard the cleanups for data inserted into the
9947 breakpoint. */
9948 discard_cleanups (bkpt_chain);
9949 /* But cleanup everything else. */
c3f6f71d 9950 do_cleanups (old_chain);
217dc9e2 9951
80c99de1 9952 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9953 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9954
9955 return 1;
c3f6f71d 9956}
c906108c 9957
348d480f 9958/* Set a breakpoint.
72b2ff0e
VP
9959 ARG is a string describing breakpoint address,
9960 condition, and thread.
9961 FLAG specifies if a breakpoint is hardware on,
9962 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9963 and BP_TEMPFLAG. */
348d480f 9964
98deb0da 9965static void
72b2ff0e 9966break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9967{
72b2ff0e 9968 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9969 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9970 ? bp_hardware_breakpoint
9971 : bp_breakpoint);
55aa24fb 9972 struct breakpoint_ops *ops;
f00aae0f
KS
9973 struct event_location *location;
9974 struct cleanup *cleanup;
9975
9976 location = string_to_event_location (&arg, current_language);
9977 cleanup = make_cleanup_delete_event_location (location);
55aa24fb
SDJ
9978
9979 /* Matching breakpoints on probes. */
5b56227b
KS
9980 if (location != NULL
9981 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
9982 ops = &bkpt_probe_breakpoint_ops;
9983 else
9984 ops = &bkpt_breakpoint_ops;
c3f6f71d 9985
8cdf0e15 9986 create_breakpoint (get_current_arch (),
f00aae0f
KS
9987 location,
9988 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9989 tempflag, type_wanted,
8cdf0e15
VP
9990 0 /* Ignore count */,
9991 pending_break_support,
55aa24fb 9992 ops,
8cdf0e15 9993 from_tty,
84f4c1fe 9994 1 /* enabled */,
44f238bb
PA
9995 0 /* internal */,
9996 0);
f00aae0f 9997 do_cleanups (cleanup);
c906108c
SS
9998}
9999
c906108c
SS
10000/* Helper function for break_command_1 and disassemble_command. */
10001
10002void
fba45db2 10003resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10004{
10005 CORE_ADDR pc;
10006
10007 if (sal->pc == 0 && sal->symtab != NULL)
10008 {
10009 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10010 error (_("No line %d in file \"%s\"."),
05cba821 10011 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10012 sal->pc = pc;
6a048695 10013
4a64f543
MS
10014 /* If this SAL corresponds to a breakpoint inserted using a line
10015 number, then skip the function prologue if necessary. */
6a048695 10016 if (sal->explicit_line)
059acae7 10017 skip_prologue_sal (sal);
c906108c
SS
10018 }
10019
10020 if (sal->section == 0 && sal->symtab != NULL)
10021 {
346d1dfe 10022 const struct blockvector *bv;
3977b71f 10023 const struct block *b;
c5aa993b 10024 struct symbol *sym;
c906108c 10025
43f3e411
DE
10026 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10027 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
10028 if (bv != NULL)
10029 {
7f0df278 10030 sym = block_linkage_function (b);
c906108c
SS
10031 if (sym != NULL)
10032 {
eb822aa6
DE
10033 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10034 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10035 sym);
c906108c
SS
10036 }
10037 else
10038 {
4a64f543
MS
10039 /* It really is worthwhile to have the section, so we'll
10040 just have to look harder. This case can be executed
10041 if we have line numbers but no functions (as can
10042 happen in assembly source). */
c906108c 10043
7cbd4a93 10044 struct bound_minimal_symbol msym;
6c95b8df
PA
10045 struct cleanup *old_chain = save_current_space_and_thread ();
10046
10047 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10048
10049 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10050 if (msym.minsym)
efd66ac6 10051 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10052
10053 do_cleanups (old_chain);
c906108c
SS
10054 }
10055 }
10056 }
10057}
10058
10059void
fba45db2 10060break_command (char *arg, int from_tty)
c906108c 10061{
db107f19 10062 break_command_1 (arg, 0, from_tty);
c906108c
SS
10063}
10064
c906108c 10065void
fba45db2 10066tbreak_command (char *arg, int from_tty)
c906108c 10067{
db107f19 10068 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10069}
10070
c906108c 10071static void
fba45db2 10072hbreak_command (char *arg, int from_tty)
c906108c 10073{
db107f19 10074 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10075}
10076
10077static void
fba45db2 10078thbreak_command (char *arg, int from_tty)
c906108c 10079{
db107f19 10080 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10081}
10082
10083static void
fba45db2 10084stop_command (char *arg, int from_tty)
c906108c 10085{
a3f17187 10086 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10087Usage: stop in <function | address>\n\
a3f17187 10088 stop at <line>\n"));
c906108c
SS
10089}
10090
10091static void
fba45db2 10092stopin_command (char *arg, int from_tty)
c906108c
SS
10093{
10094 int badInput = 0;
10095
c5aa993b 10096 if (arg == (char *) NULL)
c906108c
SS
10097 badInput = 1;
10098 else if (*arg != '*')
10099 {
10100 char *argptr = arg;
10101 int hasColon = 0;
10102
4a64f543 10103 /* Look for a ':'. If this is a line number specification, then
53a5351d 10104 say it is bad, otherwise, it should be an address or
4a64f543 10105 function/method name. */
c906108c 10106 while (*argptr && !hasColon)
c5aa993b
JM
10107 {
10108 hasColon = (*argptr == ':');
10109 argptr++;
10110 }
c906108c
SS
10111
10112 if (hasColon)
c5aa993b 10113 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10114 else
c5aa993b 10115 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10116 }
10117
10118 if (badInput)
a3f17187 10119 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10120 else
db107f19 10121 break_command_1 (arg, 0, from_tty);
c906108c
SS
10122}
10123
10124static void
fba45db2 10125stopat_command (char *arg, int from_tty)
c906108c
SS
10126{
10127 int badInput = 0;
10128
c5aa993b 10129 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10130 badInput = 1;
10131 else
10132 {
10133 char *argptr = arg;
10134 int hasColon = 0;
10135
4a64f543
MS
10136 /* Look for a ':'. If there is a '::' then get out, otherwise
10137 it is probably a line number. */
c906108c 10138 while (*argptr && !hasColon)
c5aa993b
JM
10139 {
10140 hasColon = (*argptr == ':');
10141 argptr++;
10142 }
c906108c
SS
10143
10144 if (hasColon)
c5aa993b 10145 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10146 else
c5aa993b 10147 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10148 }
10149
10150 if (badInput)
a3f17187 10151 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10152 else
db107f19 10153 break_command_1 (arg, 0, from_tty);
c906108c
SS
10154}
10155
e7e0cddf
SS
10156/* The dynamic printf command is mostly like a regular breakpoint, but
10157 with a prewired command list consisting of a single output command,
10158 built from extra arguments supplied on the dprintf command
10159 line. */
10160
da821c7b 10161static void
e7e0cddf
SS
10162dprintf_command (char *arg, int from_tty)
10163{
f00aae0f
KS
10164 struct event_location *location;
10165 struct cleanup *cleanup;
10166
10167 location = string_to_event_location (&arg, current_language);
10168 cleanup = make_cleanup_delete_event_location (location);
10169
10170 /* If non-NULL, ARG should have been advanced past the location;
10171 the next character must be ','. */
10172 if (arg != NULL)
10173 {
10174 if (arg[0] != ',' || arg[1] == '\0')
10175 error (_("Format string required"));
10176 else
10177 {
10178 /* Skip the comma. */
10179 ++arg;
10180 }
10181 }
10182
e7e0cddf 10183 create_breakpoint (get_current_arch (),
f00aae0f
KS
10184 location,
10185 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
10186 0, bp_dprintf,
10187 0 /* Ignore count */,
10188 pending_break_support,
10189 &dprintf_breakpoint_ops,
10190 from_tty,
10191 1 /* enabled */,
10192 0 /* internal */,
10193 0);
f00aae0f 10194 do_cleanups (cleanup);
e7e0cddf
SS
10195}
10196
d3ce09f5
SS
10197static void
10198agent_printf_command (char *arg, int from_tty)
10199{
10200 error (_("May only run agent-printf on the target"));
10201}
10202
f1310107
TJB
10203/* Implement the "breakpoint_hit" breakpoint_ops method for
10204 ranged breakpoints. */
10205
10206static int
10207breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10208 struct address_space *aspace,
09ac7c10
TT
10209 CORE_ADDR bp_addr,
10210 const struct target_waitstatus *ws)
f1310107 10211{
09ac7c10 10212 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10213 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10214 return 0;
10215
f1310107
TJB
10216 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10217 bl->length, aspace, bp_addr);
10218}
10219
10220/* Implement the "resources_needed" breakpoint_ops method for
10221 ranged breakpoints. */
10222
10223static int
10224resources_needed_ranged_breakpoint (const struct bp_location *bl)
10225{
10226 return target_ranged_break_num_registers ();
10227}
10228
10229/* Implement the "print_it" breakpoint_ops method for
10230 ranged breakpoints. */
10231
10232static enum print_stop_action
348d480f 10233print_it_ranged_breakpoint (bpstat bs)
f1310107 10234{
348d480f 10235 struct breakpoint *b = bs->breakpoint_at;
f1310107 10236 struct bp_location *bl = b->loc;
79a45e25 10237 struct ui_out *uiout = current_uiout;
f1310107
TJB
10238
10239 gdb_assert (b->type == bp_hardware_breakpoint);
10240
10241 /* Ranged breakpoints have only one location. */
10242 gdb_assert (bl && bl->next == NULL);
10243
10244 annotate_breakpoint (b->number);
f303dbd6
PA
10245
10246 maybe_print_thread_hit_breakpoint (uiout);
10247
f1310107 10248 if (b->disposition == disp_del)
f303dbd6 10249 ui_out_text (uiout, "Temporary ranged breakpoint ");
f1310107 10250 else
f303dbd6 10251 ui_out_text (uiout, "Ranged breakpoint ");
f1310107
TJB
10252 if (ui_out_is_mi_like_p (uiout))
10253 {
10254 ui_out_field_string (uiout, "reason",
10255 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10256 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10257 }
10258 ui_out_field_int (uiout, "bkptno", b->number);
10259 ui_out_text (uiout, ", ");
10260
10261 return PRINT_SRC_AND_LOC;
10262}
10263
10264/* Implement the "print_one" breakpoint_ops method for
10265 ranged breakpoints. */
10266
10267static void
10268print_one_ranged_breakpoint (struct breakpoint *b,
10269 struct bp_location **last_loc)
10270{
10271 struct bp_location *bl = b->loc;
10272 struct value_print_options opts;
79a45e25 10273 struct ui_out *uiout = current_uiout;
f1310107
TJB
10274
10275 /* Ranged breakpoints have only one location. */
10276 gdb_assert (bl && bl->next == NULL);
10277
10278 get_user_print_options (&opts);
10279
10280 if (opts.addressprint)
10281 /* We don't print the address range here, it will be printed later
10282 by print_one_detail_ranged_breakpoint. */
10283 ui_out_field_skip (uiout, "addr");
10284 annotate_field (5);
10285 print_breakpoint_location (b, bl);
10286 *last_loc = bl;
10287}
10288
10289/* Implement the "print_one_detail" breakpoint_ops method for
10290 ranged breakpoints. */
10291
10292static void
10293print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10294 struct ui_out *uiout)
10295{
10296 CORE_ADDR address_start, address_end;
10297 struct bp_location *bl = b->loc;
f99d8bf4
PA
10298 struct ui_file *stb = mem_fileopen ();
10299 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10300
10301 gdb_assert (bl);
10302
10303 address_start = bl->address;
10304 address_end = address_start + bl->length - 1;
10305
10306 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10307 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10308 print_core_address (bl->gdbarch, address_start),
10309 print_core_address (bl->gdbarch, address_end));
10310 ui_out_field_stream (uiout, "addr", stb);
10311 ui_out_text (uiout, "\n");
10312
10313 do_cleanups (cleanup);
10314}
10315
10316/* Implement the "print_mention" breakpoint_ops method for
10317 ranged breakpoints. */
10318
10319static void
10320print_mention_ranged_breakpoint (struct breakpoint *b)
10321{
10322 struct bp_location *bl = b->loc;
79a45e25 10323 struct ui_out *uiout = current_uiout;
f1310107
TJB
10324
10325 gdb_assert (bl);
10326 gdb_assert (b->type == bp_hardware_breakpoint);
10327
10328 if (ui_out_is_mi_like_p (uiout))
10329 return;
10330
10331 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10332 b->number, paddress (bl->gdbarch, bl->address),
10333 paddress (bl->gdbarch, bl->address + bl->length - 1));
10334}
10335
10336/* Implement the "print_recreate" breakpoint_ops method for
10337 ranged breakpoints. */
10338
10339static void
10340print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10341{
f00aae0f
KS
10342 fprintf_unfiltered (fp, "break-range %s, %s",
10343 event_location_to_string (b->location),
10344 event_location_to_string (b->location_range_end));
d9b3f62e 10345 print_recreate_thread (b, fp);
f1310107
TJB
10346}
10347
10348/* The breakpoint_ops structure to be used in ranged breakpoints. */
10349
2060206e 10350static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10351
10352/* Find the address where the end of the breakpoint range should be
10353 placed, given the SAL of the end of the range. This is so that if
10354 the user provides a line number, the end of the range is set to the
10355 last instruction of the given line. */
10356
10357static CORE_ADDR
10358find_breakpoint_range_end (struct symtab_and_line sal)
10359{
10360 CORE_ADDR end;
10361
10362 /* If the user provided a PC value, use it. Otherwise,
10363 find the address of the end of the given location. */
10364 if (sal.explicit_pc)
10365 end = sal.pc;
10366 else
10367 {
10368 int ret;
10369 CORE_ADDR start;
10370
10371 ret = find_line_pc_range (sal, &start, &end);
10372 if (!ret)
10373 error (_("Could not find location of the end of the range."));
10374
10375 /* find_line_pc_range returns the start of the next line. */
10376 end--;
10377 }
10378
10379 return end;
10380}
10381
10382/* Implement the "break-range" CLI command. */
10383
10384static void
10385break_range_command (char *arg, int from_tty)
10386{
10387 char *arg_start, *addr_string_start, *addr_string_end;
10388 struct linespec_result canonical_start, canonical_end;
10389 int bp_count, can_use_bp, length;
10390 CORE_ADDR end;
10391 struct breakpoint *b;
10392 struct symtab_and_line sal_start, sal_end;
f1310107 10393 struct cleanup *cleanup_bkpt;
f8eba3c6 10394 struct linespec_sals *lsal_start, *lsal_end;
f00aae0f 10395 struct event_location *start_location, *end_location;
f1310107
TJB
10396
10397 /* We don't support software ranged breakpoints. */
10398 if (target_ranged_break_num_registers () < 0)
10399 error (_("This target does not support hardware ranged breakpoints."));
10400
10401 bp_count = hw_breakpoint_used_count ();
10402 bp_count += target_ranged_break_num_registers ();
10403 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10404 bp_count, 0);
10405 if (can_use_bp < 0)
10406 error (_("Hardware breakpoints used exceeds limit."));
10407
f8eba3c6 10408 arg = skip_spaces (arg);
f1310107
TJB
10409 if (arg == NULL || arg[0] == '\0')
10410 error(_("No address range specified."));
10411
f1310107
TJB
10412 init_linespec_result (&canonical_start);
10413
f8eba3c6 10414 arg_start = arg;
f00aae0f
KS
10415 start_location = string_to_event_location (&arg, current_language);
10416 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10417 parse_breakpoint_sals (start_location, &canonical_start);
10418 make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10419
10420 if (arg[0] != ',')
10421 error (_("Too few arguments."));
f8eba3c6 10422 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10423 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10424
10425 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10426
10427 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10428 || lsal_start->sals.nelts != 1)
f1310107
TJB
10429 error (_("Cannot create a ranged breakpoint with multiple locations."));
10430
f8eba3c6
TT
10431 sal_start = lsal_start->sals.sals[0];
10432 addr_string_start = savestring (arg_start, arg - arg_start);
10433 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10434
10435 arg++; /* Skip the comma. */
f8eba3c6 10436 arg = skip_spaces (arg);
f1310107
TJB
10437
10438 /* Parse the end location. */
10439
f1310107
TJB
10440 init_linespec_result (&canonical_end);
10441 arg_start = arg;
10442
f8eba3c6 10443 /* We call decode_line_full directly here instead of using
f1310107
TJB
10444 parse_breakpoint_sals because we need to specify the start location's
10445 symtab and line as the default symtab and line for the end of the
10446 range. This makes it possible to have ranges like "foo.c:27, +14",
10447 where +14 means 14 lines from the start location. */
f00aae0f
KS
10448 end_location = string_to_event_location (&arg, current_language);
10449 make_cleanup_delete_event_location (end_location);
10450 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE,
f8eba3c6
TT
10451 sal_start.symtab, sal_start.line,
10452 &canonical_end, NULL, NULL);
10453
10454 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10455
f8eba3c6 10456 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10457 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10458
10459 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10460 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10461 || lsal_end->sals.nelts != 1)
f1310107
TJB
10462 error (_("Cannot create a ranged breakpoint with multiple locations."));
10463
f8eba3c6 10464 sal_end = lsal_end->sals.sals[0];
f1310107
TJB
10465
10466 end = find_breakpoint_range_end (sal_end);
10467 if (sal_start.pc > end)
177b42fe 10468 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10469
10470 length = end - sal_start.pc + 1;
10471 if (length < 0)
10472 /* Length overflowed. */
10473 error (_("Address range too large."));
10474 else if (length == 1)
10475 {
10476 /* This range is simple enough to be handled by
10477 the `hbreak' command. */
10478 hbreak_command (addr_string_start, 1);
10479
10480 do_cleanups (cleanup_bkpt);
10481
10482 return;
10483 }
10484
10485 /* Now set up the breakpoint. */
10486 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10487 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10488 set_breakpoint_count (breakpoint_count + 1);
10489 b->number = breakpoint_count;
10490 b->disposition = disp_donttouch;
f00aae0f
KS
10491 b->location = copy_event_location (start_location);
10492 b->location_range_end = copy_event_location (end_location);
f1310107
TJB
10493 b->loc->length = length;
10494
f8eba3c6 10495 do_cleanups (cleanup_bkpt);
f1310107
TJB
10496
10497 mention (b);
8d3788bd 10498 observer_notify_breakpoint_created (b);
44702360 10499 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10500}
10501
4a64f543
MS
10502/* Return non-zero if EXP is verified as constant. Returned zero
10503 means EXP is variable. Also the constant detection may fail for
10504 some constant expressions and in such case still falsely return
10505 zero. */
2e6e3d9c 10506
65d79d4b
SDJ
10507static int
10508watchpoint_exp_is_const (const struct expression *exp)
10509{
10510 int i = exp->nelts;
10511
10512 while (i > 0)
10513 {
10514 int oplenp, argsp;
10515
10516 /* We are only interested in the descriptor of each element. */
10517 operator_length (exp, i, &oplenp, &argsp);
10518 i -= oplenp;
10519
10520 switch (exp->elts[i].opcode)
10521 {
10522 case BINOP_ADD:
10523 case BINOP_SUB:
10524 case BINOP_MUL:
10525 case BINOP_DIV:
10526 case BINOP_REM:
10527 case BINOP_MOD:
10528 case BINOP_LSH:
10529 case BINOP_RSH:
10530 case BINOP_LOGICAL_AND:
10531 case BINOP_LOGICAL_OR:
10532 case BINOP_BITWISE_AND:
10533 case BINOP_BITWISE_IOR:
10534 case BINOP_BITWISE_XOR:
10535 case BINOP_EQUAL:
10536 case BINOP_NOTEQUAL:
10537 case BINOP_LESS:
10538 case BINOP_GTR:
10539 case BINOP_LEQ:
10540 case BINOP_GEQ:
10541 case BINOP_REPEAT:
10542 case BINOP_COMMA:
10543 case BINOP_EXP:
10544 case BINOP_MIN:
10545 case BINOP_MAX:
10546 case BINOP_INTDIV:
10547 case BINOP_CONCAT:
65d79d4b
SDJ
10548 case TERNOP_COND:
10549 case TERNOP_SLICE:
65d79d4b
SDJ
10550
10551 case OP_LONG:
10552 case OP_DOUBLE:
10553 case OP_DECFLOAT:
10554 case OP_LAST:
10555 case OP_COMPLEX:
10556 case OP_STRING:
65d79d4b
SDJ
10557 case OP_ARRAY:
10558 case OP_TYPE:
608b4967
TT
10559 case OP_TYPEOF:
10560 case OP_DECLTYPE:
6e72ca20 10561 case OP_TYPEID:
65d79d4b
SDJ
10562 case OP_NAME:
10563 case OP_OBJC_NSSTRING:
10564
10565 case UNOP_NEG:
10566 case UNOP_LOGICAL_NOT:
10567 case UNOP_COMPLEMENT:
10568 case UNOP_ADDR:
10569 case UNOP_HIGH:
aeaa2474 10570 case UNOP_CAST:
9eaf6705
TT
10571
10572 case UNOP_CAST_TYPE:
10573 case UNOP_REINTERPRET_CAST:
10574 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10575 /* Unary, binary and ternary operators: We have to check
10576 their operands. If they are constant, then so is the
10577 result of that operation. For instance, if A and B are
10578 determined to be constants, then so is "A + B".
10579
10580 UNOP_IND is one exception to the rule above, because the
10581 value of *ADDR is not necessarily a constant, even when
10582 ADDR is. */
65d79d4b
SDJ
10583 break;
10584
10585 case OP_VAR_VALUE:
10586 /* Check whether the associated symbol is a constant.
4a64f543 10587
65d79d4b 10588 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10589 possible that a buggy compiler could mark a variable as
10590 constant even when it is not, and TYPE_CONST would return
10591 true in this case, while SYMBOL_CLASS wouldn't.
10592
10593 We also have to check for function symbols because they
10594 are always constant. */
65d79d4b
SDJ
10595 {
10596 struct symbol *s = exp->elts[i + 2].symbol;
10597
10598 if (SYMBOL_CLASS (s) != LOC_BLOCK
10599 && SYMBOL_CLASS (s) != LOC_CONST
10600 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10601 return 0;
10602 break;
10603 }
10604
10605 /* The default action is to return 0 because we are using
10606 the optimistic approach here: If we don't know something,
10607 then it is not a constant. */
10608 default:
10609 return 0;
10610 }
10611 }
10612
10613 return 1;
10614}
10615
3a5c3e22
PA
10616/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10617
10618static void
10619dtor_watchpoint (struct breakpoint *self)
10620{
10621 struct watchpoint *w = (struct watchpoint *) self;
10622
10623 xfree (w->cond_exp);
10624 xfree (w->exp);
10625 xfree (w->exp_string);
10626 xfree (w->exp_string_reparse);
10627 value_free (w->val);
10628
10629 base_breakpoint_ops.dtor (self);
10630}
10631
348d480f
PA
10632/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10633
10634static void
10635re_set_watchpoint (struct breakpoint *b)
10636{
3a5c3e22
PA
10637 struct watchpoint *w = (struct watchpoint *) b;
10638
348d480f
PA
10639 /* Watchpoint can be either on expression using entirely global
10640 variables, or it can be on local variables.
10641
10642 Watchpoints of the first kind are never auto-deleted, and even
10643 persist across program restarts. Since they can use variables
10644 from shared libraries, we need to reparse expression as libraries
10645 are loaded and unloaded.
10646
10647 Watchpoints on local variables can also change meaning as result
10648 of solib event. For example, if a watchpoint uses both a local
10649 and a global variables in expression, it's a local watchpoint,
10650 but unloading of a shared library will make the expression
10651 invalid. This is not a very common use case, but we still
10652 re-evaluate expression, to avoid surprises to the user.
10653
10654 Note that for local watchpoints, we re-evaluate it only if
10655 watchpoints frame id is still valid. If it's not, it means the
10656 watchpoint is out of scope and will be deleted soon. In fact,
10657 I'm not sure we'll ever be called in this case.
10658
10659 If a local watchpoint's frame id is still valid, then
3a5c3e22 10660 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10661
3a5c3e22
PA
10662 Don't do anything about disabled watchpoints, since they will be
10663 reevaluated again when enabled. */
10664 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10665}
10666
77b06cd7
TJB
10667/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10668
10669static int
10670insert_watchpoint (struct bp_location *bl)
10671{
3a5c3e22
PA
10672 struct watchpoint *w = (struct watchpoint *) bl->owner;
10673 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10674
10675 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10676 w->cond_exp);
77b06cd7
TJB
10677}
10678
10679/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10680
10681static int
10682remove_watchpoint (struct bp_location *bl)
10683{
3a5c3e22
PA
10684 struct watchpoint *w = (struct watchpoint *) bl->owner;
10685 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10686
10687 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10688 w->cond_exp);
e09342b5
TJB
10689}
10690
e09342b5 10691static int
348d480f 10692breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10693 struct address_space *aspace, CORE_ADDR bp_addr,
10694 const struct target_waitstatus *ws)
e09342b5 10695{
348d480f 10696 struct breakpoint *b = bl->owner;
3a5c3e22 10697 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10698
348d480f
PA
10699 /* Continuable hardware watchpoints are treated as non-existent if the
10700 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10701 some data address). Otherwise gdb won't stop on a break instruction
10702 in the code (not from a breakpoint) when a hardware watchpoint has
10703 been defined. Also skip watchpoints which we know did not trigger
10704 (did not match the data address). */
10705 if (is_hardware_watchpoint (b)
3a5c3e22 10706 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10707 return 0;
9c06b0b4 10708
348d480f 10709 return 1;
9c06b0b4
TJB
10710}
10711
348d480f
PA
10712static void
10713check_status_watchpoint (bpstat bs)
9c06b0b4 10714{
348d480f 10715 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10716
348d480f 10717 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10718}
10719
10720/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10721 hardware watchpoints. */
9c06b0b4
TJB
10722
10723static int
348d480f 10724resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10725{
3a5c3e22
PA
10726 struct watchpoint *w = (struct watchpoint *) bl->owner;
10727 int length = w->exact? 1 : bl->length;
348d480f
PA
10728
10729 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10730}
10731
10732/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10733 hardware watchpoints. */
9c06b0b4
TJB
10734
10735static int
348d480f 10736works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10737{
efa80663
PA
10738 /* Read and access watchpoints only work with hardware support. */
10739 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10740}
10741
9c06b0b4 10742static enum print_stop_action
348d480f 10743print_it_watchpoint (bpstat bs)
9c06b0b4 10744{
348d480f
PA
10745 struct cleanup *old_chain;
10746 struct breakpoint *b;
f99d8bf4 10747 struct ui_file *stb;
348d480f 10748 enum print_stop_action result;
3a5c3e22 10749 struct watchpoint *w;
79a45e25 10750 struct ui_out *uiout = current_uiout;
348d480f
PA
10751
10752 gdb_assert (bs->bp_location_at != NULL);
10753
348d480f 10754 b = bs->breakpoint_at;
3a5c3e22 10755 w = (struct watchpoint *) b;
348d480f 10756
f99d8bf4
PA
10757 stb = mem_fileopen ();
10758 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4 10759
f303dbd6
PA
10760 annotate_watchpoint (b->number);
10761 maybe_print_thread_hit_breakpoint (uiout);
10762
9c06b0b4
TJB
10763 switch (b->type)
10764 {
348d480f 10765 case bp_watchpoint:
9c06b0b4 10766 case bp_hardware_watchpoint:
9c06b0b4
TJB
10767 if (ui_out_is_mi_like_p (uiout))
10768 ui_out_field_string
10769 (uiout, "reason",
10770 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10771 mention (b);
10772 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10773 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10774 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10775 ui_out_field_stream (uiout, "old", stb);
10776 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10777 watchpoint_value_print (w->val, stb);
348d480f
PA
10778 ui_out_field_stream (uiout, "new", stb);
10779 ui_out_text (uiout, "\n");
10780 /* More than one watchpoint may have been triggered. */
10781 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10782 break;
10783
10784 case bp_read_watchpoint:
10785 if (ui_out_is_mi_like_p (uiout))
10786 ui_out_field_string
10787 (uiout, "reason",
10788 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10789 mention (b);
10790 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10791 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10792 watchpoint_value_print (w->val, stb);
348d480f
PA
10793 ui_out_field_stream (uiout, "value", stb);
10794 ui_out_text (uiout, "\n");
10795 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10796 break;
10797
10798 case bp_access_watchpoint:
348d480f
PA
10799 if (bs->old_val != NULL)
10800 {
348d480f
PA
10801 if (ui_out_is_mi_like_p (uiout))
10802 ui_out_field_string
10803 (uiout, "reason",
10804 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10805 mention (b);
10806 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10807 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10808 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10809 ui_out_field_stream (uiout, "old", stb);
10810 ui_out_text (uiout, "\nNew value = ");
10811 }
10812 else
10813 {
10814 mention (b);
10815 if (ui_out_is_mi_like_p (uiout))
10816 ui_out_field_string
10817 (uiout, "reason",
10818 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10819 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10820 ui_out_text (uiout, "\nValue = ");
10821 }
f99d8bf4 10822 watchpoint_value_print (w->val, stb);
348d480f
PA
10823 ui_out_field_stream (uiout, "new", stb);
10824 ui_out_text (uiout, "\n");
10825 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10826 break;
10827 default:
348d480f 10828 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10829 }
10830
348d480f
PA
10831 do_cleanups (old_chain);
10832 return result;
10833}
10834
10835/* Implement the "print_mention" breakpoint_ops method for hardware
10836 watchpoints. */
10837
10838static void
10839print_mention_watchpoint (struct breakpoint *b)
10840{
10841 struct cleanup *ui_out_chain;
3a5c3e22 10842 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10843 struct ui_out *uiout = current_uiout;
348d480f
PA
10844
10845 switch (b->type)
10846 {
10847 case bp_watchpoint:
10848 ui_out_text (uiout, "Watchpoint ");
10849 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10850 break;
10851 case bp_hardware_watchpoint:
10852 ui_out_text (uiout, "Hardware watchpoint ");
10853 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10854 break;
10855 case bp_read_watchpoint:
10856 ui_out_text (uiout, "Hardware read watchpoint ");
10857 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10858 break;
10859 case bp_access_watchpoint:
10860 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10861 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10862 break;
10863 default:
10864 internal_error (__FILE__, __LINE__,
10865 _("Invalid hardware watchpoint type."));
10866 }
10867
10868 ui_out_field_int (uiout, "number", b->number);
10869 ui_out_text (uiout, ": ");
3a5c3e22 10870 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10871 do_cleanups (ui_out_chain);
10872}
10873
10874/* Implement the "print_recreate" breakpoint_ops method for
10875 watchpoints. */
10876
10877static void
10878print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10879{
3a5c3e22
PA
10880 struct watchpoint *w = (struct watchpoint *) b;
10881
348d480f
PA
10882 switch (b->type)
10883 {
10884 case bp_watchpoint:
10885 case bp_hardware_watchpoint:
10886 fprintf_unfiltered (fp, "watch");
10887 break;
10888 case bp_read_watchpoint:
10889 fprintf_unfiltered (fp, "rwatch");
10890 break;
10891 case bp_access_watchpoint:
10892 fprintf_unfiltered (fp, "awatch");
10893 break;
10894 default:
10895 internal_error (__FILE__, __LINE__,
10896 _("Invalid watchpoint type."));
10897 }
10898
3a5c3e22 10899 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10900 print_recreate_thread (b, fp);
348d480f
PA
10901}
10902
427cd150
TT
10903/* Implement the "explains_signal" breakpoint_ops method for
10904 watchpoints. */
10905
47591c29 10906static int
427cd150
TT
10907explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10908{
10909 /* A software watchpoint cannot cause a signal other than
10910 GDB_SIGNAL_TRAP. */
10911 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10912 return 0;
427cd150 10913
47591c29 10914 return 1;
427cd150
TT
10915}
10916
348d480f
PA
10917/* The breakpoint_ops structure to be used in hardware watchpoints. */
10918
2060206e 10919static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10920
10921/* Implement the "insert" breakpoint_ops method for
10922 masked hardware watchpoints. */
10923
10924static int
10925insert_masked_watchpoint (struct bp_location *bl)
10926{
3a5c3e22
PA
10927 struct watchpoint *w = (struct watchpoint *) bl->owner;
10928
10929 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10930 bl->watchpoint_type);
10931}
10932
10933/* Implement the "remove" breakpoint_ops method for
10934 masked hardware watchpoints. */
10935
10936static int
10937remove_masked_watchpoint (struct bp_location *bl)
10938{
3a5c3e22
PA
10939 struct watchpoint *w = (struct watchpoint *) bl->owner;
10940
10941 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10942 bl->watchpoint_type);
10943}
10944
10945/* Implement the "resources_needed" breakpoint_ops method for
10946 masked hardware watchpoints. */
10947
10948static int
10949resources_needed_masked_watchpoint (const struct bp_location *bl)
10950{
3a5c3e22
PA
10951 struct watchpoint *w = (struct watchpoint *) bl->owner;
10952
10953 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10954}
10955
10956/* Implement the "works_in_software_mode" breakpoint_ops method for
10957 masked hardware watchpoints. */
10958
10959static int
10960works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10961{
10962 return 0;
10963}
10964
10965/* Implement the "print_it" breakpoint_ops method for
10966 masked hardware watchpoints. */
10967
10968static enum print_stop_action
10969print_it_masked_watchpoint (bpstat bs)
10970{
10971 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10972 struct ui_out *uiout = current_uiout;
348d480f
PA
10973
10974 /* Masked watchpoints have only one location. */
10975 gdb_assert (b->loc && b->loc->next == NULL);
10976
f303dbd6
PA
10977 annotate_watchpoint (b->number);
10978 maybe_print_thread_hit_breakpoint (uiout);
10979
348d480f
PA
10980 switch (b->type)
10981 {
10982 case bp_hardware_watchpoint:
348d480f
PA
10983 if (ui_out_is_mi_like_p (uiout))
10984 ui_out_field_string
10985 (uiout, "reason",
10986 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10987 break;
10988
10989 case bp_read_watchpoint:
10990 if (ui_out_is_mi_like_p (uiout))
10991 ui_out_field_string
10992 (uiout, "reason",
10993 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10994 break;
10995
10996 case bp_access_watchpoint:
10997 if (ui_out_is_mi_like_p (uiout))
10998 ui_out_field_string
10999 (uiout, "reason",
11000 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11001 break;
11002 default:
11003 internal_error (__FILE__, __LINE__,
11004 _("Invalid hardware watchpoint type."));
11005 }
11006
11007 mention (b);
9c06b0b4
TJB
11008 ui_out_text (uiout, _("\n\
11009Check the underlying instruction at PC for the memory\n\
11010address and value which triggered this watchpoint.\n"));
11011 ui_out_text (uiout, "\n");
11012
11013 /* More than one watchpoint may have been triggered. */
11014 return PRINT_UNKNOWN;
11015}
11016
11017/* Implement the "print_one_detail" breakpoint_ops method for
11018 masked hardware watchpoints. */
11019
11020static void
11021print_one_detail_masked_watchpoint (const struct breakpoint *b,
11022 struct ui_out *uiout)
11023{
3a5c3e22
PA
11024 struct watchpoint *w = (struct watchpoint *) b;
11025
9c06b0b4
TJB
11026 /* Masked watchpoints have only one location. */
11027 gdb_assert (b->loc && b->loc->next == NULL);
11028
11029 ui_out_text (uiout, "\tmask ");
3a5c3e22 11030 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11031 ui_out_text (uiout, "\n");
11032}
11033
11034/* Implement the "print_mention" breakpoint_ops method for
11035 masked hardware watchpoints. */
11036
11037static void
11038print_mention_masked_watchpoint (struct breakpoint *b)
11039{
3a5c3e22 11040 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11041 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11042 struct cleanup *ui_out_chain;
11043
11044 switch (b->type)
11045 {
11046 case bp_hardware_watchpoint:
11047 ui_out_text (uiout, "Masked hardware watchpoint ");
11048 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11049 break;
11050 case bp_read_watchpoint:
11051 ui_out_text (uiout, "Masked hardware read watchpoint ");
11052 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11053 break;
11054 case bp_access_watchpoint:
11055 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11056 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11057 break;
11058 default:
11059 internal_error (__FILE__, __LINE__,
11060 _("Invalid hardware watchpoint type."));
11061 }
11062
11063 ui_out_field_int (uiout, "number", b->number);
11064 ui_out_text (uiout, ": ");
3a5c3e22 11065 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11066 do_cleanups (ui_out_chain);
11067}
11068
11069/* Implement the "print_recreate" breakpoint_ops method for
11070 masked hardware watchpoints. */
11071
11072static void
11073print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11074{
3a5c3e22 11075 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11076 char tmp[40];
11077
11078 switch (b->type)
11079 {
11080 case bp_hardware_watchpoint:
11081 fprintf_unfiltered (fp, "watch");
11082 break;
11083 case bp_read_watchpoint:
11084 fprintf_unfiltered (fp, "rwatch");
11085 break;
11086 case bp_access_watchpoint:
11087 fprintf_unfiltered (fp, "awatch");
11088 break;
11089 default:
11090 internal_error (__FILE__, __LINE__,
11091 _("Invalid hardware watchpoint type."));
11092 }
11093
3a5c3e22
PA
11094 sprintf_vma (tmp, w->hw_wp_mask);
11095 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11096 print_recreate_thread (b, fp);
9c06b0b4
TJB
11097}
11098
11099/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11100
2060206e 11101static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11102
11103/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11104
11105static int
11106is_masked_watchpoint (const struct breakpoint *b)
11107{
11108 return b->ops == &masked_watchpoint_breakpoint_ops;
11109}
11110
53a5351d
JM
11111/* accessflag: hw_write: watch write,
11112 hw_read: watch read,
11113 hw_access: watch access (read or write) */
c906108c 11114static void
bbc13ae3 11115watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11116 int just_location, int internal)
c906108c 11117{
d983da9c 11118 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11119 struct expression *exp;
270140bd 11120 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11121 struct value *val, *mark, *result;
bb9d5f81 11122 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11123 struct frame_info *frame;
bbc13ae3
KS
11124 const char *exp_start = NULL;
11125 const char *exp_end = NULL;
11126 const char *tok, *end_tok;
9c06b0b4 11127 int toklen = -1;
bbc13ae3
KS
11128 const char *cond_start = NULL;
11129 const char *cond_end = NULL;
c906108c 11130 enum bptype bp_type;
37e4754d 11131 int thread = -1;
0cf6dd15 11132 int pc = 0;
9c06b0b4
TJB
11133 /* Flag to indicate whether we are going to use masks for
11134 the hardware watchpoint. */
11135 int use_mask = 0;
11136 CORE_ADDR mask = 0;
3a5c3e22 11137 struct watchpoint *w;
bbc13ae3
KS
11138 char *expression;
11139 struct cleanup *back_to;
c906108c 11140
37e4754d
LM
11141 /* Make sure that we actually have parameters to parse. */
11142 if (arg != NULL && arg[0] != '\0')
11143 {
bbc13ae3
KS
11144 const char *value_start;
11145
11146 exp_end = arg + strlen (arg);
37e4754d 11147
9c06b0b4
TJB
11148 /* Look for "parameter value" pairs at the end
11149 of the arguments string. */
bbc13ae3 11150 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11151 {
11152 /* Skip whitespace at the end of the argument list. */
11153 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11154 tok--;
11155
11156 /* Find the beginning of the last token.
11157 This is the value of the parameter. */
11158 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11159 tok--;
11160 value_start = tok + 1;
11161
11162 /* Skip whitespace. */
11163 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11164 tok--;
11165
11166 end_tok = tok;
11167
11168 /* Find the beginning of the second to last token.
11169 This is the parameter itself. */
11170 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11171 tok--;
11172 tok++;
11173 toklen = end_tok - tok + 1;
11174
61012eef 11175 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4 11176 {
5d5658a1 11177 struct thread_info *thr;
9c06b0b4
TJB
11178 /* At this point we've found a "thread" token, which means
11179 the user is trying to set a watchpoint that triggers
11180 only in a specific thread. */
5d5658a1 11181 const char *endp;
37e4754d 11182
9c06b0b4
TJB
11183 if (thread != -1)
11184 error(_("You can specify only one thread."));
37e4754d 11185
9c06b0b4 11186 /* Extract the thread ID from the next token. */
5d5658a1 11187 thr = parse_thread_id (value_start, &endp);
37e4754d 11188
5d5658a1 11189 /* Check if the user provided a valid thread ID. */
9c06b0b4 11190 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
5d5658a1 11191 invalid_thread_id_error (value_start);
9c06b0b4 11192
5d5658a1 11193 thread = thr->global_num;
9c06b0b4 11194 }
61012eef 11195 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11196 {
11197 /* We've found a "mask" token, which means the user wants to
11198 create a hardware watchpoint that is going to have the mask
11199 facility. */
11200 struct value *mask_value, *mark;
37e4754d 11201
9c06b0b4
TJB
11202 if (use_mask)
11203 error(_("You can specify only one mask."));
37e4754d 11204
9c06b0b4 11205 use_mask = just_location = 1;
37e4754d 11206
9c06b0b4
TJB
11207 mark = value_mark ();
11208 mask_value = parse_to_comma_and_eval (&value_start);
11209 mask = value_as_address (mask_value);
11210 value_free_to_mark (mark);
11211 }
11212 else
11213 /* We didn't recognize what we found. We should stop here. */
11214 break;
37e4754d 11215
9c06b0b4
TJB
11216 /* Truncate the string and get rid of the "parameter value" pair before
11217 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11218 exp_end = tok;
9c06b0b4 11219 }
37e4754d 11220 }
bbc13ae3
KS
11221 else
11222 exp_end = arg;
37e4754d 11223
bbc13ae3
KS
11224 /* Parse the rest of the arguments. From here on out, everything
11225 is in terms of a newly allocated string instead of the original
11226 ARG. */
c906108c 11227 innermost_block = NULL;
bbc13ae3
KS
11228 expression = savestring (arg, exp_end - arg);
11229 back_to = make_cleanup (xfree, expression);
11230 exp_start = arg = expression;
1bb9788d 11231 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11232 exp_end = arg;
fa8a61dc
TT
11233 /* Remove trailing whitespace from the expression before saving it.
11234 This makes the eventual display of the expression string a bit
11235 prettier. */
11236 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11237 --exp_end;
11238
65d79d4b
SDJ
11239 /* Checking if the expression is not constant. */
11240 if (watchpoint_exp_is_const (exp))
11241 {
11242 int len;
11243
11244 len = exp_end - exp_start;
11245 while (len > 0 && isspace (exp_start[len - 1]))
11246 len--;
11247 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11248 }
11249
c906108c
SS
11250 exp_valid_block = innermost_block;
11251 mark = value_mark ();
3a1115a0 11252 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11253
bb9d5f81
PP
11254 if (val != NULL && just_location)
11255 {
11256 saved_bitpos = value_bitpos (val);
11257 saved_bitsize = value_bitsize (val);
11258 }
11259
06a64a0b
TT
11260 if (just_location)
11261 {
9c06b0b4
TJB
11262 int ret;
11263
06a64a0b 11264 exp_valid_block = NULL;
a1442452 11265 val = value_addr (result);
06a64a0b
TT
11266 release_value (val);
11267 value_free_to_mark (mark);
9c06b0b4
TJB
11268
11269 if (use_mask)
11270 {
11271 ret = target_masked_watch_num_registers (value_as_address (val),
11272 mask);
11273 if (ret == -1)
11274 error (_("This target does not support masked watchpoints."));
11275 else if (ret == -2)
11276 error (_("Invalid mask or memory region."));
11277 }
06a64a0b
TT
11278 }
11279 else if (val != NULL)
fa4727a6 11280 release_value (val);
c906108c 11281
bbc13ae3
KS
11282 tok = skip_spaces_const (arg);
11283 end_tok = skip_to_space_const (tok);
c906108c
SS
11284
11285 toklen = end_tok - tok;
11286 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11287 {
2d134ed3
PA
11288 struct expression *cond;
11289
60e1c644 11290 innermost_block = NULL;
c906108c 11291 tok = cond_start = end_tok + 1;
1bb9788d 11292 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11293
11294 /* The watchpoint expression may not be local, but the condition
11295 may still be. E.g.: `watch global if local > 0'. */
11296 cond_exp_valid_block = innermost_block;
11297
2d134ed3 11298 xfree (cond);
c906108c
SS
11299 cond_end = tok;
11300 }
11301 if (*tok)
8a3fe4f8 11302 error (_("Junk at end of command."));
c906108c 11303
d983da9c 11304 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11305
11306 /* If the expression is "local", then set up a "watchpoint scope"
11307 breakpoint at the point where we've left the scope of the watchpoint
11308 expression. Create the scope breakpoint before the watchpoint, so
11309 that we will encounter it first in bpstat_stop_status. */
60e1c644 11310 if (exp_valid_block && frame)
d983da9c 11311 {
edb3359d
DJ
11312 if (frame_id_p (frame_unwind_caller_id (frame)))
11313 {
11314 scope_breakpoint
a6d9a66e
UW
11315 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11316 frame_unwind_caller_pc (frame),
06edf0c0
PA
11317 bp_watchpoint_scope,
11318 &momentary_breakpoint_ops);
d983da9c 11319
edb3359d 11320 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11321
edb3359d
DJ
11322 /* Automatically delete the breakpoint when it hits. */
11323 scope_breakpoint->disposition = disp_del;
d983da9c 11324
edb3359d
DJ
11325 /* Only break in the proper frame (help with recursion). */
11326 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11327
edb3359d 11328 /* Set the address at which we will stop. */
a6d9a66e
UW
11329 scope_breakpoint->loc->gdbarch
11330 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11331 scope_breakpoint->loc->requested_address
11332 = frame_unwind_caller_pc (frame);
11333 scope_breakpoint->loc->address
a6d9a66e
UW
11334 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11335 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11336 scope_breakpoint->type);
11337 }
d983da9c
DJ
11338 }
11339
e8369a73
AB
11340 /* Now set up the breakpoint. We create all watchpoints as hardware
11341 watchpoints here even if hardware watchpoints are turned off, a call
11342 to update_watchpoint later in this function will cause the type to
11343 drop back to bp_watchpoint (software watchpoint) if required. */
11344
11345 if (accessflag == hw_read)
11346 bp_type = bp_read_watchpoint;
11347 else if (accessflag == hw_access)
11348 bp_type = bp_access_watchpoint;
11349 else
11350 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11351
11352 w = XCNEW (struct watchpoint);
11353 b = &w->base;
348d480f 11354 if (use_mask)
3a5c3e22
PA
11355 init_raw_breakpoint_without_location (b, NULL, bp_type,
11356 &masked_watchpoint_breakpoint_ops);
348d480f 11357 else
3a5c3e22
PA
11358 init_raw_breakpoint_without_location (b, NULL, bp_type,
11359 &watchpoint_breakpoint_ops);
37e4754d 11360 b->thread = thread;
b5de0fa7 11361 b->disposition = disp_donttouch;
348d480f 11362 b->pspace = current_program_space;
3a5c3e22
PA
11363 w->exp = exp;
11364 w->exp_valid_block = exp_valid_block;
11365 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11366 if (just_location)
11367 {
11368 struct type *t = value_type (val);
11369 CORE_ADDR addr = value_as_address (val);
11370 char *name;
11371
11372 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11373 name = type_to_string (t);
11374
3a5c3e22 11375 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11376 core_addr_to_string (addr));
06a64a0b
TT
11377 xfree (name);
11378
3a5c3e22 11379 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11380 (int) (exp_end - exp_start), exp_start);
11381
06a64a0b
TT
11382 /* The above expression is in C. */
11383 b->language = language_c;
11384 }
11385 else
3a5c3e22 11386 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11387
11388 if (use_mask)
11389 {
3a5c3e22 11390 w->hw_wp_mask = mask;
9c06b0b4
TJB
11391 }
11392 else
11393 {
3a5c3e22 11394 w->val = val;
bb9d5f81
PP
11395 w->val_bitpos = saved_bitpos;
11396 w->val_bitsize = saved_bitsize;
3a5c3e22 11397 w->val_valid = 1;
9c06b0b4 11398 }
77b06cd7 11399
c906108c
SS
11400 if (cond_start)
11401 b->cond_string = savestring (cond_start, cond_end - cond_start);
11402 else
11403 b->cond_string = 0;
c5aa993b 11404
c906108c 11405 if (frame)
f6bc2008 11406 {
3a5c3e22
PA
11407 w->watchpoint_frame = get_frame_id (frame);
11408 w->watchpoint_thread = inferior_ptid;
f6bc2008 11409 }
c906108c 11410 else
f6bc2008 11411 {
3a5c3e22
PA
11412 w->watchpoint_frame = null_frame_id;
11413 w->watchpoint_thread = null_ptid;
f6bc2008 11414 }
c906108c 11415
d983da9c 11416 if (scope_breakpoint != NULL)
c906108c 11417 {
d983da9c
DJ
11418 /* The scope breakpoint is related to the watchpoint. We will
11419 need to act on them together. */
11420 b->related_breakpoint = scope_breakpoint;
11421 scope_breakpoint->related_breakpoint = b;
c906108c 11422 }
d983da9c 11423
06a64a0b
TT
11424 if (!just_location)
11425 value_free_to_mark (mark);
2d134ed3 11426
492d29ea 11427 TRY
a9634178
TJB
11428 {
11429 /* Finally update the new watchpoint. This creates the locations
11430 that should be inserted. */
3a5c3e22 11431 update_watchpoint (w, 1);
a9634178 11432 }
492d29ea 11433 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11434 {
11435 delete_breakpoint (b);
11436 throw_exception (e);
11437 }
492d29ea 11438 END_CATCH
a9634178 11439
3ea46bff 11440 install_breakpoint (internal, b, 1);
bbc13ae3 11441 do_cleanups (back_to);
c906108c
SS
11442}
11443
e09342b5 11444/* Return count of debug registers needed to watch the given expression.
e09342b5 11445 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11446
c906108c 11447static int
a9634178 11448can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11449{
11450 int found_memory_cnt = 0;
2e70b7b9 11451 struct value *head = v;
c906108c
SS
11452
11453 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11454 if (!can_use_hw_watchpoints)
c906108c 11455 return 0;
c5aa993b 11456
5c44784c
JM
11457 /* Make sure that the value of the expression depends only upon
11458 memory contents, and values computed from them within GDB. If we
11459 find any register references or function calls, we can't use a
11460 hardware watchpoint.
11461
11462 The idea here is that evaluating an expression generates a series
11463 of values, one holding the value of every subexpression. (The
11464 expression a*b+c has five subexpressions: a, b, a*b, c, and
11465 a*b+c.) GDB's values hold almost enough information to establish
11466 the criteria given above --- they identify memory lvalues,
11467 register lvalues, computed values, etcetera. So we can evaluate
11468 the expression, and then scan the chain of values that leaves
11469 behind to decide whether we can detect any possible change to the
11470 expression's final value using only hardware watchpoints.
11471
11472 However, I don't think that the values returned by inferior
11473 function calls are special in any way. So this function may not
11474 notice that an expression involving an inferior function call
11475 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11476 for (; v; v = value_next (v))
c906108c 11477 {
5c44784c 11478 if (VALUE_LVAL (v) == lval_memory)
c906108c 11479 {
8464be76
DJ
11480 if (v != head && value_lazy (v))
11481 /* A lazy memory lvalue in the chain is one that GDB never
11482 needed to fetch; we either just used its address (e.g.,
11483 `a' in `a.b') or we never needed it at all (e.g., `a'
11484 in `a,b'). This doesn't apply to HEAD; if that is
11485 lazy then it was not readable, but watch it anyway. */
5c44784c 11486 ;
53a5351d 11487 else
5c44784c
JM
11488 {
11489 /* Ahh, memory we actually used! Check if we can cover
11490 it with hardware watchpoints. */
df407dfe 11491 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11492
11493 /* We only watch structs and arrays if user asked for it
11494 explicitly, never if they just happen to appear in a
11495 middle of some value chain. */
11496 if (v == head
11497 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11498 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11499 {
42ae5230 11500 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11501 int len;
11502 int num_regs;
11503
a9634178 11504 len = (target_exact_watchpoints
e09342b5
TJB
11505 && is_scalar_type_recursive (vtype))?
11506 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11507
e09342b5
TJB
11508 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11509 if (!num_regs)
2e70b7b9
MS
11510 return 0;
11511 else
e09342b5 11512 found_memory_cnt += num_regs;
2e70b7b9 11513 }
5c44784c 11514 }
c5aa993b 11515 }
5086187c
AC
11516 else if (VALUE_LVAL (v) != not_lval
11517 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11518 return 0; /* These are values from the history (e.g., $1). */
5086187c 11519 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11520 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11521 }
11522
11523 /* The expression itself looks suitable for using a hardware
11524 watchpoint, but give the target machine a chance to reject it. */
11525 return found_memory_cnt;
11526}
11527
8b93c638 11528void
84f4c1fe 11529watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11530{
84f4c1fe 11531 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11532}
11533
06a64a0b
TT
11534/* A helper function that looks for the "-location" argument and then
11535 calls watch_command_1. */
11536
11537static void
11538watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11539{
11540 int just_location = 0;
11541
11542 if (arg
11543 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11544 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11545 {
e9cafbcc 11546 arg = skip_spaces (arg);
06a64a0b
TT
11547 just_location = 1;
11548 }
11549
84f4c1fe 11550 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11551}
8926118c 11552
c5aa993b 11553static void
fba45db2 11554watch_command (char *arg, int from_tty)
c906108c 11555{
06a64a0b 11556 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11557}
11558
8b93c638 11559void
84f4c1fe 11560rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11561{
84f4c1fe 11562 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11563}
8926118c 11564
c5aa993b 11565static void
fba45db2 11566rwatch_command (char *arg, int from_tty)
c906108c 11567{
06a64a0b 11568 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11569}
11570
8b93c638 11571void
84f4c1fe 11572awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11573{
84f4c1fe 11574 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11575}
8926118c 11576
c5aa993b 11577static void
fba45db2 11578awatch_command (char *arg, int from_tty)
c906108c 11579{
06a64a0b 11580 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11581}
c906108c 11582\f
c5aa993b 11583
cfc31633
PA
11584/* Data for the FSM that manages the until(location)/advance commands
11585 in infcmd.c. Here because it uses the mechanisms of
11586 breakpoints. */
c906108c 11587
cfc31633 11588struct until_break_fsm
bfec99b2 11589{
cfc31633
PA
11590 /* The base class. */
11591 struct thread_fsm thread_fsm;
11592
11593 /* The thread that as current when the command was executed. */
11594 int thread;
11595
11596 /* The breakpoint set at the destination location. */
11597 struct breakpoint *location_breakpoint;
11598
11599 /* Breakpoint set at the return address in the caller frame. May be
11600 NULL. */
11601 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11602};
11603
cfc31633
PA
11604static void until_break_fsm_clean_up (struct thread_fsm *self);
11605static int until_break_fsm_should_stop (struct thread_fsm *self);
11606static enum async_reply_reason
11607 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11608
11609/* until_break_fsm's vtable. */
11610
11611static struct thread_fsm_ops until_break_fsm_ops =
11612{
11613 NULL, /* dtor */
11614 until_break_fsm_clean_up,
11615 until_break_fsm_should_stop,
11616 NULL, /* return_value */
11617 until_break_fsm_async_reply_reason,
11618};
11619
11620/* Allocate a new until_break_command_fsm. */
11621
11622static struct until_break_fsm *
11623new_until_break_fsm (int thread,
11624 struct breakpoint *location_breakpoint,
11625 struct breakpoint *caller_breakpoint)
11626{
11627 struct until_break_fsm *sm;
11628
11629 sm = XCNEW (struct until_break_fsm);
11630 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops);
11631
11632 sm->thread = thread;
11633 sm->location_breakpoint = location_breakpoint;
11634 sm->caller_breakpoint = caller_breakpoint;
11635
11636 return sm;
11637}
11638
11639/* Implementation of the 'should_stop' FSM method for the
11640 until(location)/advance commands. */
11641
11642static int
11643until_break_fsm_should_stop (struct thread_fsm *self)
11644{
11645 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11646 struct thread_info *tp = inferior_thread ();
11647
11648 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11649 sm->location_breakpoint) != NULL
11650 || (sm->caller_breakpoint != NULL
11651 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11652 sm->caller_breakpoint) != NULL))
11653 thread_fsm_set_finished (self);
11654
11655 return 1;
11656}
11657
11658/* Implementation of the 'clean_up' FSM method for the
11659 until(location)/advance commands. */
11660
c2c6d25f 11661static void
cfc31633 11662until_break_fsm_clean_up (struct thread_fsm *self)
43ff13b4 11663{
cfc31633 11664 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11665
cfc31633
PA
11666 /* Clean up our temporary breakpoints. */
11667 if (sm->location_breakpoint != NULL)
11668 {
11669 delete_breakpoint (sm->location_breakpoint);
11670 sm->location_breakpoint = NULL;
11671 }
11672 if (sm->caller_breakpoint != NULL)
11673 {
11674 delete_breakpoint (sm->caller_breakpoint);
11675 sm->caller_breakpoint = NULL;
11676 }
11677 delete_longjmp_breakpoint (sm->thread);
11678}
11679
11680/* Implementation of the 'async_reply_reason' FSM method for the
11681 until(location)/advance commands. */
11682
11683static enum async_reply_reason
11684until_break_fsm_async_reply_reason (struct thread_fsm *self)
11685{
11686 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11687}
11688
c906108c 11689void
ae66c1fc 11690until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11691{
11692 struct symtabs_and_lines sals;
11693 struct symtab_and_line sal;
8556afb4
PA
11694 struct frame_info *frame;
11695 struct gdbarch *frame_gdbarch;
11696 struct frame_id stack_frame_id;
11697 struct frame_id caller_frame_id;
cfc31633
PA
11698 struct breakpoint *location_breakpoint;
11699 struct breakpoint *caller_breakpoint = NULL;
f00aae0f 11700 struct cleanup *old_chain, *cleanup;
186c406b
TT
11701 int thread;
11702 struct thread_info *tp;
f00aae0f 11703 struct event_location *location;
cfc31633 11704 struct until_break_fsm *sm;
c906108c 11705
70509625 11706 clear_proceed_status (0);
c906108c
SS
11707
11708 /* Set a breakpoint where the user wants it and at return from
4a64f543 11709 this function. */
c5aa993b 11710
f00aae0f
KS
11711 location = string_to_event_location (&arg, current_language);
11712 cleanup = make_cleanup_delete_event_location (location);
11713
1bfeeb0f 11714 if (last_displayed_sal_is_valid ())
f00aae0f 11715 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11716 get_last_displayed_symtab (),
f8eba3c6 11717 get_last_displayed_line ());
c906108c 11718 else
f00aae0f 11719 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
f8eba3c6 11720 (struct symtab *) NULL, 0);
c5aa993b 11721
c906108c 11722 if (sals.nelts != 1)
8a3fe4f8 11723 error (_("Couldn't get information on specified line."));
c5aa993b 11724
c906108c 11725 sal = sals.sals[0];
4a64f543 11726 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11727
c906108c 11728 if (*arg)
8a3fe4f8 11729 error (_("Junk at end of arguments."));
c5aa993b 11730
c906108c 11731 resolve_sal_pc (&sal);
c5aa993b 11732
186c406b 11733 tp = inferior_thread ();
5d5658a1 11734 thread = tp->global_num;
186c406b 11735
883bc8d1
PA
11736 old_chain = make_cleanup (null_cleanup, NULL);
11737
8556afb4
PA
11738 /* Note linespec handling above invalidates the frame chain.
11739 Installing a breakpoint also invalidates the frame chain (as it
11740 may need to switch threads), so do any frame handling before
11741 that. */
11742
11743 frame = get_selected_frame (NULL);
11744 frame_gdbarch = get_frame_arch (frame);
11745 stack_frame_id = get_stack_frame_id (frame);
11746 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11747
ae66c1fc
EZ
11748 /* Keep within the current frame, or in frames called by the current
11749 one. */
edb3359d 11750
883bc8d1 11751 if (frame_id_p (caller_frame_id))
c906108c 11752 {
883bc8d1 11753 struct symtab_and_line sal2;
cfc31633 11754 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11755
11756 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11757 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11758 caller_gdbarch = frame_unwind_caller_arch (frame);
11759 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11760 sal2,
11761 caller_frame_id,
11762 bp_until);
11763 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11764
883bc8d1 11765 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11766 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11767 }
c5aa993b 11768
c70a6932
JK
11769 /* set_momentary_breakpoint could invalidate FRAME. */
11770 frame = NULL;
11771
883bc8d1
PA
11772 if (anywhere)
11773 /* If the user told us to continue until a specified location,
11774 we don't specify a frame at which we need to stop. */
cfc31633
PA
11775 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11776 null_frame_id, bp_until);
883bc8d1
PA
11777 else
11778 /* Otherwise, specify the selected frame, because we want to stop
11779 only at the very same frame. */
cfc31633
PA
11780 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11781 stack_frame_id, bp_until);
11782 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11783
5d5658a1
PA
11784 sm = new_until_break_fsm (tp->global_num,
11785 location_breakpoint, caller_breakpoint);
cfc31633 11786 tp->thread_fsm = &sm->thread_fsm;
f107f563 11787
cfc31633 11788 discard_cleanups (old_chain);
f107f563 11789
cfc31633 11790 proceed (-1, GDB_SIGNAL_DEFAULT);
f00aae0f
KS
11791
11792 do_cleanups (cleanup);
c906108c 11793}
ae66c1fc 11794
c906108c
SS
11795/* This function attempts to parse an optional "if <cond>" clause
11796 from the arg string. If one is not found, it returns NULL.
c5aa993b 11797
c906108c
SS
11798 Else, it returns a pointer to the condition string. (It does not
11799 attempt to evaluate the string against a particular block.) And,
11800 it updates arg to point to the first character following the parsed
4a64f543 11801 if clause in the arg string. */
53a5351d 11802
916703c0 11803char *
fba45db2 11804ep_parse_optional_if_clause (char **arg)
c906108c 11805{
c5aa993b
JM
11806 char *cond_string;
11807
11808 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11809 return NULL;
c5aa993b 11810
4a64f543 11811 /* Skip the "if" keyword. */
c906108c 11812 (*arg) += 2;
c5aa993b 11813
c906108c 11814 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11815 condition string. */
e9cafbcc 11816 *arg = skip_spaces (*arg);
c906108c 11817 cond_string = *arg;
c5aa993b 11818
4a64f543
MS
11819 /* Assume that the condition occupies the remainder of the arg
11820 string. */
c906108c 11821 (*arg) += strlen (cond_string);
c5aa993b 11822
c906108c
SS
11823 return cond_string;
11824}
c5aa993b 11825
c906108c
SS
11826/* Commands to deal with catching events, such as signals, exceptions,
11827 process start/exit, etc. */
c5aa993b
JM
11828
11829typedef enum
11830{
44feb3ce
TT
11831 catch_fork_temporary, catch_vfork_temporary,
11832 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11833}
11834catch_fork_kind;
11835
c906108c 11836static void
cc59ec59
MS
11837catch_fork_command_1 (char *arg, int from_tty,
11838 struct cmd_list_element *command)
c906108c 11839{
a6d9a66e 11840 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11841 char *cond_string = NULL;
44feb3ce
TT
11842 catch_fork_kind fork_kind;
11843 int tempflag;
11844
11845 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11846 tempflag = (fork_kind == catch_fork_temporary
11847 || fork_kind == catch_vfork_temporary);
c5aa993b 11848
44feb3ce
TT
11849 if (!arg)
11850 arg = "";
e9cafbcc 11851 arg = skip_spaces (arg);
c5aa993b 11852
c906108c 11853 /* The allowed syntax is:
c5aa993b
JM
11854 catch [v]fork
11855 catch [v]fork if <cond>
11856
4a64f543 11857 First, check if there's an if clause. */
c906108c 11858 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11859
c906108c 11860 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11861 error (_("Junk at end of arguments."));
c5aa993b 11862
c906108c 11863 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11864 and enable reporting of such events. */
c5aa993b
JM
11865 switch (fork_kind)
11866 {
44feb3ce
TT
11867 case catch_fork_temporary:
11868 case catch_fork_permanent:
a6d9a66e 11869 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11870 &catch_fork_breakpoint_ops);
c906108c 11871 break;
44feb3ce
TT
11872 case catch_vfork_temporary:
11873 case catch_vfork_permanent:
a6d9a66e 11874 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11875 &catch_vfork_breakpoint_ops);
c906108c 11876 break;
c5aa993b 11877 default:
8a3fe4f8 11878 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11879 break;
c5aa993b 11880 }
c906108c
SS
11881}
11882
11883static void
cc59ec59
MS
11884catch_exec_command_1 (char *arg, int from_tty,
11885 struct cmd_list_element *command)
c906108c 11886{
b4d90040 11887 struct exec_catchpoint *c;
a6d9a66e 11888 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11889 int tempflag;
c5aa993b 11890 char *cond_string = NULL;
c906108c 11891
44feb3ce
TT
11892 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11893
11894 if (!arg)
11895 arg = "";
e9cafbcc 11896 arg = skip_spaces (arg);
c906108c
SS
11897
11898 /* The allowed syntax is:
c5aa993b
JM
11899 catch exec
11900 catch exec if <cond>
c906108c 11901
4a64f543 11902 First, check if there's an if clause. */
c906108c
SS
11903 cond_string = ep_parse_optional_if_clause (&arg);
11904
11905 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11906 error (_("Junk at end of arguments."));
c906108c 11907
b4d90040
PA
11908 c = XNEW (struct exec_catchpoint);
11909 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11910 &catch_exec_breakpoint_ops);
11911 c->exec_pathname = NULL;
11912
3ea46bff 11913 install_breakpoint (0, &c->base, 1);
c906108c 11914}
c5aa993b 11915
9ac4176b 11916void
28010a5d
PA
11917init_ada_exception_breakpoint (struct breakpoint *b,
11918 struct gdbarch *gdbarch,
11919 struct symtab_and_line sal,
11920 char *addr_string,
c0a91b2b 11921 const struct breakpoint_ops *ops,
28010a5d 11922 int tempflag,
349774ef 11923 int enabled,
28010a5d 11924 int from_tty)
f7f9143b 11925{
f7f9143b
JB
11926 if (from_tty)
11927 {
5af949e3
UW
11928 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11929 if (!loc_gdbarch)
11930 loc_gdbarch = gdbarch;
11931
6c95b8df
PA
11932 describe_other_breakpoints (loc_gdbarch,
11933 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11934 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11935 version for exception catchpoints, because two catchpoints
11936 used for different exception names will use the same address.
11937 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11938 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11939 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11940 the user what type of catchpoint it is. The above is good
11941 enough for now, though. */
11942 }
11943
28010a5d 11944 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11945
349774ef 11946 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11947 b->disposition = tempflag ? disp_del : disp_donttouch;
f00aae0f
KS
11948 b->location = string_to_event_location (&addr_string,
11949 language_def (language_ada));
f7f9143b 11950 b->language = language_ada;
f7f9143b
JB
11951}
11952
c906108c 11953static void
fba45db2 11954catch_command (char *arg, int from_tty)
c906108c 11955{
44feb3ce 11956 error (_("Catch requires an event name."));
c906108c
SS
11957}
11958\f
11959
11960static void
fba45db2 11961tcatch_command (char *arg, int from_tty)
c906108c 11962{
44feb3ce 11963 error (_("Catch requires an event name."));
c906108c
SS
11964}
11965
8a2c437b
TT
11966/* A qsort comparison function that sorts breakpoints in order. */
11967
11968static int
11969compare_breakpoints (const void *a, const void *b)
11970{
9a3c8263 11971 const breakpoint_p *ba = (const breakpoint_p *) a;
8a2c437b 11972 uintptr_t ua = (uintptr_t) *ba;
9a3c8263 11973 const breakpoint_p *bb = (const breakpoint_p *) b;
8a2c437b
TT
11974 uintptr_t ub = (uintptr_t) *bb;
11975
11976 if ((*ba)->number < (*bb)->number)
11977 return -1;
11978 else if ((*ba)->number > (*bb)->number)
11979 return 1;
11980
11981 /* Now sort by address, in case we see, e..g, two breakpoints with
11982 the number 0. */
11983 if (ua < ub)
11984 return -1;
94b0e70d 11985 return ua > ub ? 1 : 0;
8a2c437b
TT
11986}
11987
80f8a6eb 11988/* Delete breakpoints by address or line. */
c906108c
SS
11989
11990static void
fba45db2 11991clear_command (char *arg, int from_tty)
c906108c 11992{
8a2c437b 11993 struct breakpoint *b, *prev;
d6e956e5
VP
11994 VEC(breakpoint_p) *found = 0;
11995 int ix;
c906108c
SS
11996 int default_match;
11997 struct symtabs_and_lines sals;
11998 struct symtab_and_line sal;
c906108c 11999 int i;
8a2c437b 12000 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12001
12002 if (arg)
12003 {
39cf75f7
DE
12004 sals = decode_line_with_current_source (arg,
12005 (DECODE_LINE_FUNFIRSTLINE
12006 | DECODE_LINE_LIST_MODE));
cf4ded82 12007 make_cleanup (xfree, sals.sals);
c906108c
SS
12008 default_match = 0;
12009 }
12010 else
12011 {
8d749320 12012 sals.sals = XNEW (struct symtab_and_line);
80f8a6eb 12013 make_cleanup (xfree, sals.sals);
4a64f543 12014 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12015
12016 /* Set sal's line, symtab, pc, and pspace to the values
12017 corresponding to the last call to print_frame_info. If the
12018 codepoint is not valid, this will set all the fields to 0. */
12019 get_last_displayed_sal (&sal);
c906108c 12020 if (sal.symtab == 0)
8a3fe4f8 12021 error (_("No source file specified."));
c906108c
SS
12022
12023 sals.sals[0] = sal;
12024 sals.nelts = 1;
12025
12026 default_match = 1;
12027 }
12028
4a64f543
MS
12029 /* We don't call resolve_sal_pc here. That's not as bad as it
12030 seems, because all existing breakpoints typically have both
12031 file/line and pc set. So, if clear is given file/line, we can
12032 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12033
12034 We only support clearing given the address explicitly
12035 present in breakpoint table. Say, we've set breakpoint
4a64f543 12036 at file:line. There were several PC values for that file:line,
ed0616c6 12037 due to optimization, all in one block.
4a64f543
MS
12038
12039 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12040 PC corresponding to the same file:line, the breakpoint won't
12041 be cleared. We probably can still clear the breakpoint, but
12042 since the other PC value is never presented to user, user
12043 can only find it by guessing, and it does not seem important
12044 to support that. */
12045
4a64f543
MS
12046 /* For each line spec given, delete bps which correspond to it. Do
12047 it in two passes, solely to preserve the current behavior that
12048 from_tty is forced true if we delete more than one
12049 breakpoint. */
c906108c 12050
80f8a6eb 12051 found = NULL;
8a2c437b 12052 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12053 for (i = 0; i < sals.nelts; i++)
12054 {
05cba821
JK
12055 const char *sal_fullname;
12056
c906108c 12057 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12058 If line given (pc == 0), clear all bpts on specified line.
12059 If defaulting, clear all bpts on default line
c906108c 12060 or at default pc.
c5aa993b
JM
12061
12062 defaulting sal.pc != 0 tests to do
12063
12064 0 1 pc
12065 1 1 pc _and_ line
12066 0 0 line
12067 1 0 <can't happen> */
c906108c
SS
12068
12069 sal = sals.sals[i];
05cba821
JK
12070 sal_fullname = (sal.symtab == NULL
12071 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12072
4a64f543 12073 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12074 ALL_BREAKPOINTS (b)
c5aa993b 12075 {
0d381245 12076 int match = 0;
4a64f543 12077 /* Are we going to delete b? */
cc60f2e3 12078 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12079 {
12080 struct bp_location *loc = b->loc;
12081 for (; loc; loc = loc->next)
12082 {
f8eba3c6
TT
12083 /* If the user specified file:line, don't allow a PC
12084 match. This matches historical gdb behavior. */
12085 int pc_match = (!sal.explicit_line
12086 && sal.pc
12087 && (loc->pspace == sal.pspace)
12088 && (loc->address == sal.pc)
12089 && (!section_is_overlay (loc->section)
12090 || loc->section == sal.section));
4aac40c8
TT
12091 int line_match = 0;
12092
12093 if ((default_match || sal.explicit_line)
2f202fde 12094 && loc->symtab != NULL
05cba821 12095 && sal_fullname != NULL
4aac40c8 12096 && sal.pspace == loc->pspace
05cba821
JK
12097 && loc->line_number == sal.line
12098 && filename_cmp (symtab_to_fullname (loc->symtab),
12099 sal_fullname) == 0)
12100 line_match = 1;
4aac40c8 12101
0d381245
VP
12102 if (pc_match || line_match)
12103 {
12104 match = 1;
12105 break;
12106 }
12107 }
12108 }
12109
12110 if (match)
d6e956e5 12111 VEC_safe_push(breakpoint_p, found, b);
c906108c 12112 }
80f8a6eb 12113 }
8a2c437b 12114
80f8a6eb 12115 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12116 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12117 {
12118 if (arg)
8a3fe4f8 12119 error (_("No breakpoint at %s."), arg);
80f8a6eb 12120 else
8a3fe4f8 12121 error (_("No breakpoint at this line."));
80f8a6eb 12122 }
c906108c 12123
8a2c437b
TT
12124 /* Remove duplicates from the vec. */
12125 qsort (VEC_address (breakpoint_p, found),
12126 VEC_length (breakpoint_p, found),
12127 sizeof (breakpoint_p),
12128 compare_breakpoints);
12129 prev = VEC_index (breakpoint_p, found, 0);
12130 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12131 {
12132 if (b == prev)
12133 {
12134 VEC_ordered_remove (breakpoint_p, found, ix);
12135 --ix;
12136 }
12137 }
12138
d6e956e5 12139 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12140 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12141 if (from_tty)
a3f17187 12142 {
d6e956e5 12143 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12144 printf_unfiltered (_("Deleted breakpoint "));
12145 else
12146 printf_unfiltered (_("Deleted breakpoints "));
12147 }
d6e956e5
VP
12148
12149 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12150 {
c5aa993b 12151 if (from_tty)
d6e956e5
VP
12152 printf_unfiltered ("%d ", b->number);
12153 delete_breakpoint (b);
c906108c 12154 }
80f8a6eb
MS
12155 if (from_tty)
12156 putchar_unfiltered ('\n');
8a2c437b
TT
12157
12158 do_cleanups (cleanups);
c906108c
SS
12159}
12160\f
12161/* Delete breakpoint in BS if they are `delete' breakpoints and
12162 all breakpoints that are marked for deletion, whether hit or not.
12163 This is called after any breakpoint is hit, or after errors. */
12164
12165void
fba45db2 12166breakpoint_auto_delete (bpstat bs)
c906108c 12167{
35df4500 12168 struct breakpoint *b, *b_tmp;
c906108c
SS
12169
12170 for (; bs; bs = bs->next)
f431efe5
PA
12171 if (bs->breakpoint_at
12172 && bs->breakpoint_at->disposition == disp_del
c906108c 12173 && bs->stop)
f431efe5 12174 delete_breakpoint (bs->breakpoint_at);
c906108c 12175
35df4500 12176 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12177 {
b5de0fa7 12178 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12179 delete_breakpoint (b);
12180 }
c906108c
SS
12181}
12182
4a64f543
MS
12183/* A comparison function for bp_location AP and BP being interfaced to
12184 qsort. Sort elements primarily by their ADDRESS (no matter what
12185 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 12186 secondarily by ordering first permanent elements and
4a64f543 12187 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12188 qsort being an unstable algorithm. */
876fa593
JK
12189
12190static int
494cfb0f 12191bp_location_compare (const void *ap, const void *bp)
876fa593 12192{
9a3c8263
SM
12193 const struct bp_location *a = *(const struct bp_location **) ap;
12194 const struct bp_location *b = *(const struct bp_location **) bp;
876fa593
JK
12195
12196 if (a->address != b->address)
12197 return (a->address > b->address) - (a->address < b->address);
12198
dea2aa5f
LM
12199 /* Sort locations at the same address by their pspace number, keeping
12200 locations of the same inferior (in a multi-inferior environment)
12201 grouped. */
12202
12203 if (a->pspace->num != b->pspace->num)
12204 return ((a->pspace->num > b->pspace->num)
12205 - (a->pspace->num < b->pspace->num));
12206
876fa593 12207 /* Sort permanent breakpoints first. */
1a853c52
PA
12208 if (a->permanent != b->permanent)
12209 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12210
c56a97f9
JK
12211 /* Make the internal GDB representation stable across GDB runs
12212 where A and B memory inside GDB can differ. Breakpoint locations of
12213 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12214
12215 if (a->owner->number != b->owner->number)
c56a97f9
JK
12216 return ((a->owner->number > b->owner->number)
12217 - (a->owner->number < b->owner->number));
876fa593
JK
12218
12219 return (a > b) - (a < b);
12220}
12221
876fa593 12222/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12223 bp_location_shadow_len_after_address_max according to the current
12224 content of the bp_location array. */
f7545552
TT
12225
12226static void
876fa593 12227bp_location_target_extensions_update (void)
f7545552 12228{
876fa593
JK
12229 struct bp_location *bl, **blp_tmp;
12230
12231 bp_location_placed_address_before_address_max = 0;
12232 bp_location_shadow_len_after_address_max = 0;
12233
12234 ALL_BP_LOCATIONS (bl, blp_tmp)
12235 {
12236 CORE_ADDR start, end, addr;
12237
12238 if (!bp_location_has_shadow (bl))
12239 continue;
12240
12241 start = bl->target_info.placed_address;
12242 end = start + bl->target_info.shadow_len;
12243
12244 gdb_assert (bl->address >= start);
12245 addr = bl->address - start;
12246 if (addr > bp_location_placed_address_before_address_max)
12247 bp_location_placed_address_before_address_max = addr;
12248
12249 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12250
12251 gdb_assert (bl->address < end);
12252 addr = end - bl->address;
12253 if (addr > bp_location_shadow_len_after_address_max)
12254 bp_location_shadow_len_after_address_max = addr;
12255 }
f7545552
TT
12256}
12257
1e4d1764
YQ
12258/* Download tracepoint locations if they haven't been. */
12259
12260static void
12261download_tracepoint_locations (void)
12262{
7ed2c994 12263 struct breakpoint *b;
1e4d1764 12264 struct cleanup *old_chain;
dd2e65cc 12265 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764
YQ
12266
12267 old_chain = save_current_space_and_thread ();
12268
7ed2c994 12269 ALL_TRACEPOINTS (b)
1e4d1764 12270 {
7ed2c994 12271 struct bp_location *bl;
1e4d1764 12272 struct tracepoint *t;
f2a8bc8a 12273 int bp_location_downloaded = 0;
1e4d1764 12274
7ed2c994 12275 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12276 ? !may_insert_fast_tracepoints
12277 : !may_insert_tracepoints))
12278 continue;
12279
dd2e65cc
YQ
12280 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12281 {
12282 if (target_can_download_tracepoint ())
12283 can_download_tracepoint = TRIBOOL_TRUE;
12284 else
12285 can_download_tracepoint = TRIBOOL_FALSE;
12286 }
12287
12288 if (can_download_tracepoint == TRIBOOL_FALSE)
12289 break;
12290
7ed2c994
YQ
12291 for (bl = b->loc; bl; bl = bl->next)
12292 {
12293 /* In tracepoint, locations are _never_ duplicated, so
12294 should_be_inserted is equivalent to
12295 unduplicated_should_be_inserted. */
12296 if (!should_be_inserted (bl) || bl->inserted)
12297 continue;
1e4d1764 12298
7ed2c994 12299 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12300
7ed2c994 12301 target_download_tracepoint (bl);
1e4d1764 12302
7ed2c994 12303 bl->inserted = 1;
f2a8bc8a 12304 bp_location_downloaded = 1;
7ed2c994
YQ
12305 }
12306 t = (struct tracepoint *) b;
12307 t->number_on_target = b->number;
f2a8bc8a
YQ
12308 if (bp_location_downloaded)
12309 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12310 }
12311
12312 do_cleanups (old_chain);
12313}
12314
934709f0
PW
12315/* Swap the insertion/duplication state between two locations. */
12316
12317static void
12318swap_insertion (struct bp_location *left, struct bp_location *right)
12319{
12320 const int left_inserted = left->inserted;
12321 const int left_duplicate = left->duplicate;
b775012e 12322 const int left_needs_update = left->needs_update;
934709f0
PW
12323 const struct bp_target_info left_target_info = left->target_info;
12324
1e4d1764
YQ
12325 /* Locations of tracepoints can never be duplicated. */
12326 if (is_tracepoint (left->owner))
12327 gdb_assert (!left->duplicate);
12328 if (is_tracepoint (right->owner))
12329 gdb_assert (!right->duplicate);
12330
934709f0
PW
12331 left->inserted = right->inserted;
12332 left->duplicate = right->duplicate;
b775012e 12333 left->needs_update = right->needs_update;
934709f0
PW
12334 left->target_info = right->target_info;
12335 right->inserted = left_inserted;
12336 right->duplicate = left_duplicate;
b775012e 12337 right->needs_update = left_needs_update;
934709f0
PW
12338 right->target_info = left_target_info;
12339}
12340
b775012e
LM
12341/* Force the re-insertion of the locations at ADDRESS. This is called
12342 once a new/deleted/modified duplicate location is found and we are evaluating
12343 conditions on the target's side. Such conditions need to be updated on
12344 the target. */
12345
12346static void
12347force_breakpoint_reinsertion (struct bp_location *bl)
12348{
12349 struct bp_location **locp = NULL, **loc2p;
12350 struct bp_location *loc;
12351 CORE_ADDR address = 0;
12352 int pspace_num;
12353
12354 address = bl->address;
12355 pspace_num = bl->pspace->num;
12356
12357 /* This is only meaningful if the target is
12358 evaluating conditions and if the user has
12359 opted for condition evaluation on the target's
12360 side. */
12361 if (gdb_evaluates_breakpoint_condition_p ()
12362 || !target_supports_evaluation_of_breakpoint_conditions ())
12363 return;
12364
12365 /* Flag all breakpoint locations with this address and
12366 the same program space as the location
12367 as "its condition has changed". We need to
12368 update the conditions on the target's side. */
12369 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12370 {
12371 loc = *loc2p;
12372
12373 if (!is_breakpoint (loc->owner)
12374 || pspace_num != loc->pspace->num)
12375 continue;
12376
12377 /* Flag the location appropriately. We use a different state to
12378 let everyone know that we already updated the set of locations
12379 with addr bl->address and program space bl->pspace. This is so
12380 we don't have to keep calling these functions just to mark locations
12381 that have already been marked. */
12382 loc->condition_changed = condition_updated;
12383
12384 /* Free the agent expression bytecode as well. We will compute
12385 it later on. */
12386 if (loc->cond_bytecode)
12387 {
12388 free_agent_expr (loc->cond_bytecode);
12389 loc->cond_bytecode = NULL;
12390 }
12391 }
12392}
44702360
PA
12393/* Called whether new breakpoints are created, or existing breakpoints
12394 deleted, to update the global location list and recompute which
12395 locations are duplicate of which.
b775012e 12396
04086b45
PA
12397 The INSERT_MODE flag determines whether locations may not, may, or
12398 shall be inserted now. See 'enum ugll_insert_mode' for more
12399 info. */
b60e7edf 12400
0d381245 12401static void
44702360 12402update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12403{
74960c60 12404 struct breakpoint *b;
876fa593 12405 struct bp_location **locp, *loc;
f7545552 12406 struct cleanup *cleanups;
b775012e
LM
12407 /* Last breakpoint location address that was marked for update. */
12408 CORE_ADDR last_addr = 0;
12409 /* Last breakpoint location program space that was marked for update. */
12410 int last_pspace_num = -1;
f7545552 12411
2d134ed3
PA
12412 /* Used in the duplicates detection below. When iterating over all
12413 bp_locations, points to the first bp_location of a given address.
12414 Breakpoints and watchpoints of different types are never
12415 duplicates of each other. Keep one pointer for each type of
12416 breakpoint/watchpoint, so we only need to loop over all locations
12417 once. */
12418 struct bp_location *bp_loc_first; /* breakpoint */
12419 struct bp_location *wp_loc_first; /* hardware watchpoint */
12420 struct bp_location *awp_loc_first; /* access watchpoint */
12421 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12422
4a64f543
MS
12423 /* Saved former bp_location array which we compare against the newly
12424 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12425 struct bp_location **old_location, **old_locp;
12426 unsigned old_location_count;
12427
12428 old_location = bp_location;
12429 old_location_count = bp_location_count;
12430 bp_location = NULL;
12431 bp_location_count = 0;
12432 cleanups = make_cleanup (xfree, old_location);
0d381245 12433
74960c60 12434 ALL_BREAKPOINTS (b)
876fa593
JK
12435 for (loc = b->loc; loc; loc = loc->next)
12436 bp_location_count++;
12437
8d749320 12438 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
876fa593
JK
12439 locp = bp_location;
12440 ALL_BREAKPOINTS (b)
12441 for (loc = b->loc; loc; loc = loc->next)
12442 *locp++ = loc;
12443 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12444 bp_location_compare);
876fa593
JK
12445
12446 bp_location_target_extensions_update ();
74960c60 12447
4a64f543
MS
12448 /* Identify bp_location instances that are no longer present in the
12449 new list, and therefore should be freed. Note that it's not
12450 necessary that those locations should be removed from inferior --
12451 if there's another location at the same address (previously
12452 marked as duplicate), we don't need to remove/insert the
12453 location.
876fa593 12454
4a64f543
MS
12455 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12456 and former bp_location array state respectively. */
876fa593
JK
12457
12458 locp = bp_location;
12459 for (old_locp = old_location; old_locp < old_location + old_location_count;
12460 old_locp++)
74960c60 12461 {
876fa593 12462 struct bp_location *old_loc = *old_locp;
c7d46a38 12463 struct bp_location **loc2p;
876fa593 12464
e5dd4106 12465 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12466 not, we have to free it. */
c7d46a38 12467 int found_object = 0;
20874c92
VP
12468 /* Tells if the location should remain inserted in the target. */
12469 int keep_in_target = 0;
12470 int removed = 0;
876fa593 12471
4a64f543
MS
12472 /* Skip LOCP entries which will definitely never be needed.
12473 Stop either at or being the one matching OLD_LOC. */
876fa593 12474 while (locp < bp_location + bp_location_count
c7d46a38 12475 && (*locp)->address < old_loc->address)
876fa593 12476 locp++;
c7d46a38
PA
12477
12478 for (loc2p = locp;
12479 (loc2p < bp_location + bp_location_count
12480 && (*loc2p)->address == old_loc->address);
12481 loc2p++)
12482 {
b775012e
LM
12483 /* Check if this is a new/duplicated location or a duplicated
12484 location that had its condition modified. If so, we want to send
12485 its condition to the target if evaluation of conditions is taking
12486 place there. */
12487 if ((*loc2p)->condition_changed == condition_modified
12488 && (last_addr != old_loc->address
12489 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12490 {
b775012e
LM
12491 force_breakpoint_reinsertion (*loc2p);
12492 last_pspace_num = old_loc->pspace->num;
c7d46a38 12493 }
b775012e
LM
12494
12495 if (*loc2p == old_loc)
12496 found_object = 1;
c7d46a38 12497 }
74960c60 12498
b775012e
LM
12499 /* We have already handled this address, update it so that we don't
12500 have to go through updates again. */
12501 last_addr = old_loc->address;
12502
12503 /* Target-side condition evaluation: Handle deleted locations. */
12504 if (!found_object)
12505 force_breakpoint_reinsertion (old_loc);
12506
4a64f543
MS
12507 /* If this location is no longer present, and inserted, look if
12508 there's maybe a new location at the same address. If so,
12509 mark that one inserted, and don't remove this one. This is
12510 needed so that we don't have a time window where a breakpoint
12511 at certain location is not inserted. */
74960c60 12512
876fa593 12513 if (old_loc->inserted)
0d381245 12514 {
4a64f543
MS
12515 /* If the location is inserted now, we might have to remove
12516 it. */
74960c60 12517
876fa593 12518 if (found_object && should_be_inserted (old_loc))
74960c60 12519 {
4a64f543
MS
12520 /* The location is still present in the location list,
12521 and still should be inserted. Don't do anything. */
20874c92 12522 keep_in_target = 1;
74960c60
VP
12523 }
12524 else
12525 {
b775012e
LM
12526 /* This location still exists, but it won't be kept in the
12527 target since it may have been disabled. We proceed to
12528 remove its target-side condition. */
12529
4a64f543
MS
12530 /* The location is either no longer present, or got
12531 disabled. See if there's another location at the
12532 same address, in which case we don't need to remove
12533 this one from the target. */
876fa593 12534
2bdf28a0 12535 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12536 if (breakpoint_address_is_meaningful (old_loc->owner))
12537 {
876fa593 12538 for (loc2p = locp;
c7d46a38
PA
12539 (loc2p < bp_location + bp_location_count
12540 && (*loc2p)->address == old_loc->address);
876fa593
JK
12541 loc2p++)
12542 {
12543 struct bp_location *loc2 = *loc2p;
12544
2d134ed3 12545 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12546 {
85d721b8
PA
12547 /* Read watchpoint locations are switched to
12548 access watchpoints, if the former are not
12549 supported, but the latter are. */
12550 if (is_hardware_watchpoint (old_loc->owner))
12551 {
12552 gdb_assert (is_hardware_watchpoint (loc2->owner));
12553 loc2->watchpoint_type = old_loc->watchpoint_type;
12554 }
12555
934709f0
PW
12556 /* loc2 is a duplicated location. We need to check
12557 if it should be inserted in case it will be
12558 unduplicated. */
12559 if (loc2 != old_loc
12560 && unduplicated_should_be_inserted (loc2))
c7d46a38 12561 {
934709f0 12562 swap_insertion (old_loc, loc2);
c7d46a38
PA
12563 keep_in_target = 1;
12564 break;
12565 }
876fa593
JK
12566 }
12567 }
12568 }
74960c60
VP
12569 }
12570
20874c92
VP
12571 if (!keep_in_target)
12572 {
876fa593 12573 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12574 {
4a64f543
MS
12575 /* This is just about all we can do. We could keep
12576 this location on the global list, and try to
12577 remove it next time, but there's no particular
12578 reason why we will succeed next time.
20874c92 12579
4a64f543
MS
12580 Note that at this point, old_loc->owner is still
12581 valid, as delete_breakpoint frees the breakpoint
12582 only after calling us. */
3e43a32a
MS
12583 printf_filtered (_("warning: Error removing "
12584 "breakpoint %d\n"),
876fa593 12585 old_loc->owner->number);
20874c92
VP
12586 }
12587 removed = 1;
12588 }
0d381245 12589 }
74960c60
VP
12590
12591 if (!found_object)
1c5cfe86 12592 {
fbea99ea 12593 if (removed && target_is_non_stop_p ()
1cf4d951 12594 && need_moribund_for_location_type (old_loc))
20874c92 12595 {
db82e815
PA
12596 /* This location was removed from the target. In
12597 non-stop mode, a race condition is possible where
12598 we've removed a breakpoint, but stop events for that
12599 breakpoint are already queued and will arrive later.
12600 We apply an heuristic to be able to distinguish such
12601 SIGTRAPs from other random SIGTRAPs: we keep this
12602 breakpoint location for a bit, and will retire it
12603 after we see some number of events. The theory here
12604 is that reporting of events should, "on the average",
12605 be fair, so after a while we'll see events from all
12606 threads that have anything of interest, and no longer
12607 need to keep this breakpoint location around. We
12608 don't hold locations forever so to reduce chances of
12609 mistaking a non-breakpoint SIGTRAP for a breakpoint
12610 SIGTRAP.
12611
12612 The heuristic failing can be disastrous on
12613 decr_pc_after_break targets.
12614
12615 On decr_pc_after_break targets, like e.g., x86-linux,
12616 if we fail to recognize a late breakpoint SIGTRAP,
12617 because events_till_retirement has reached 0 too
12618 soon, we'll fail to do the PC adjustment, and report
12619 a random SIGTRAP to the user. When the user resumes
12620 the inferior, it will most likely immediately crash
2dec564e 12621 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12622 corrupted, because of being resumed e.g., in the
12623 middle of a multi-byte instruction, or skipped a
12624 one-byte instruction. This was actually seen happen
12625 on native x86-linux, and should be less rare on
12626 targets that do not support new thread events, like
12627 remote, due to the heuristic depending on
12628 thread_count.
12629
12630 Mistaking a random SIGTRAP for a breakpoint trap
12631 causes similar symptoms (PC adjustment applied when
12632 it shouldn't), but then again, playing with SIGTRAPs
12633 behind the debugger's back is asking for trouble.
12634
12635 Since hardware watchpoint traps are always
12636 distinguishable from other traps, so we don't need to
12637 apply keep hardware watchpoint moribund locations
12638 around. We simply always ignore hardware watchpoint
12639 traps we can no longer explain. */
12640
876fa593
JK
12641 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12642 old_loc->owner = NULL;
20874c92 12643
876fa593 12644 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12645 }
12646 else
f431efe5
PA
12647 {
12648 old_loc->owner = NULL;
12649 decref_bp_location (&old_loc);
12650 }
20874c92 12651 }
74960c60 12652 }
1c5cfe86 12653
348d480f
PA
12654 /* Rescan breakpoints at the same address and section, marking the
12655 first one as "first" and any others as "duplicates". This is so
12656 that the bpt instruction is only inserted once. If we have a
12657 permanent breakpoint at the same place as BPT, make that one the
12658 official one, and the rest as duplicates. Permanent breakpoints
12659 are sorted first for the same address.
12660
12661 Do the same for hardware watchpoints, but also considering the
12662 watchpoint's type (regular/access/read) and length. */
12663
12664 bp_loc_first = NULL;
12665 wp_loc_first = NULL;
12666 awp_loc_first = NULL;
12667 rwp_loc_first = NULL;
12668 ALL_BP_LOCATIONS (loc, locp)
12669 {
12670 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12671 non-NULL. */
348d480f 12672 struct bp_location **loc_first_p;
d3fbdd86 12673 b = loc->owner;
348d480f 12674
6f380991 12675 if (!unduplicated_should_be_inserted (loc)
348d480f 12676 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12677 /* Don't detect duplicate for tracepoint locations because they are
12678 never duplicated. See the comments in field `duplicate' of
12679 `struct bp_location'. */
348d480f 12680 || is_tracepoint (b))
b775012e
LM
12681 {
12682 /* Clear the condition modification flag. */
12683 loc->condition_changed = condition_unchanged;
12684 continue;
12685 }
348d480f 12686
348d480f
PA
12687 if (b->type == bp_hardware_watchpoint)
12688 loc_first_p = &wp_loc_first;
12689 else if (b->type == bp_read_watchpoint)
12690 loc_first_p = &rwp_loc_first;
12691 else if (b->type == bp_access_watchpoint)
12692 loc_first_p = &awp_loc_first;
12693 else
12694 loc_first_p = &bp_loc_first;
12695
12696 if (*loc_first_p == NULL
12697 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12698 || !breakpoint_locations_match (loc, *loc_first_p))
12699 {
12700 *loc_first_p = loc;
12701 loc->duplicate = 0;
b775012e
LM
12702
12703 if (is_breakpoint (loc->owner) && loc->condition_changed)
12704 {
12705 loc->needs_update = 1;
12706 /* Clear the condition modification flag. */
12707 loc->condition_changed = condition_unchanged;
12708 }
348d480f
PA
12709 continue;
12710 }
12711
934709f0
PW
12712
12713 /* This and the above ensure the invariant that the first location
12714 is not duplicated, and is the inserted one.
12715 All following are marked as duplicated, and are not inserted. */
12716 if (loc->inserted)
12717 swap_insertion (loc, *loc_first_p);
348d480f
PA
12718 loc->duplicate = 1;
12719
b775012e
LM
12720 /* Clear the condition modification flag. */
12721 loc->condition_changed = condition_unchanged;
348d480f
PA
12722 }
12723
a25a5a45 12724 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12725 {
04086b45 12726 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12727 insert_breakpoint_locations ();
12728 else
12729 {
44702360
PA
12730 /* Even though the caller told us to not insert new
12731 locations, we may still need to update conditions on the
12732 target's side of breakpoints that were already inserted
12733 if the target is evaluating breakpoint conditions. We
b775012e
LM
12734 only update conditions for locations that are marked
12735 "needs_update". */
12736 update_inserted_breakpoint_locations ();
12737 }
12738 }
348d480f 12739
04086b45 12740 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12741 download_tracepoint_locations ();
12742
348d480f
PA
12743 do_cleanups (cleanups);
12744}
12745
12746void
12747breakpoint_retire_moribund (void)
12748{
12749 struct bp_location *loc;
12750 int ix;
12751
12752 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12753 if (--(loc->events_till_retirement) == 0)
12754 {
12755 decref_bp_location (&loc);
12756 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12757 --ix;
12758 }
12759}
12760
12761static void
44702360 12762update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12763{
348d480f 12764
492d29ea
PA
12765 TRY
12766 {
12767 update_global_location_list (insert_mode);
12768 }
12769 CATCH (e, RETURN_MASK_ERROR)
12770 {
12771 }
12772 END_CATCH
348d480f
PA
12773}
12774
12775/* Clear BKP from a BPS. */
12776
12777static void
12778bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12779{
12780 bpstat bs;
12781
12782 for (bs = bps; bs; bs = bs->next)
12783 if (bs->breakpoint_at == bpt)
12784 {
12785 bs->breakpoint_at = NULL;
12786 bs->old_val = NULL;
12787 /* bs->commands will be freed later. */
12788 }
12789}
12790
12791/* Callback for iterate_over_threads. */
12792static int
12793bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12794{
9a3c8263 12795 struct breakpoint *bpt = (struct breakpoint *) data;
348d480f
PA
12796
12797 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12798 return 0;
12799}
12800
12801/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12802 callbacks. */
12803
12804static void
12805say_where (struct breakpoint *b)
12806{
12807 struct value_print_options opts;
12808
12809 get_user_print_options (&opts);
12810
12811 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12812 single string. */
12813 if (b->loc == NULL)
12814 {
f00aae0f
KS
12815 /* For pending locations, the output differs slightly based
12816 on b->extra_string. If this is non-NULL, it contains either
12817 a condition or dprintf arguments. */
12818 if (b->extra_string == NULL)
12819 {
12820 printf_filtered (_(" (%s) pending."),
12821 event_location_to_string (b->location));
12822 }
12823 else if (b->type == bp_dprintf)
12824 {
12825 printf_filtered (_(" (%s,%s) pending."),
12826 event_location_to_string (b->location),
12827 b->extra_string);
12828 }
12829 else
12830 {
12831 printf_filtered (_(" (%s %s) pending."),
12832 event_location_to_string (b->location),
12833 b->extra_string);
12834 }
348d480f
PA
12835 }
12836 else
12837 {
2f202fde 12838 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12839 {
12840 printf_filtered (" at ");
12841 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12842 gdb_stdout);
12843 }
2f202fde 12844 if (b->loc->symtab != NULL)
f8eba3c6
TT
12845 {
12846 /* If there is a single location, we can print the location
12847 more nicely. */
12848 if (b->loc->next == NULL)
12849 printf_filtered (": file %s, line %d.",
05cba821
JK
12850 symtab_to_filename_for_display (b->loc->symtab),
12851 b->loc->line_number);
f8eba3c6
TT
12852 else
12853 /* This is not ideal, but each location may have a
12854 different file name, and this at least reflects the
12855 real situation somewhat. */
f00aae0f
KS
12856 printf_filtered (": %s.",
12857 event_location_to_string (b->location));
f8eba3c6 12858 }
348d480f
PA
12859
12860 if (b->loc->next)
12861 {
12862 struct bp_location *loc = b->loc;
12863 int n = 0;
12864 for (; loc; loc = loc->next)
12865 ++n;
12866 printf_filtered (" (%d locations)", n);
12867 }
12868 }
12869}
12870
348d480f
PA
12871/* Default bp_location_ops methods. */
12872
12873static void
12874bp_location_dtor (struct bp_location *self)
12875{
12876 xfree (self->cond);
b775012e
LM
12877 if (self->cond_bytecode)
12878 free_agent_expr (self->cond_bytecode);
348d480f 12879 xfree (self->function_name);
8b4f3082
PA
12880
12881 VEC_free (agent_expr_p, self->target_info.conditions);
12882 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12883}
12884
12885static const struct bp_location_ops bp_location_ops =
12886{
12887 bp_location_dtor
12888};
12889
2060206e
PA
12890/* Default breakpoint_ops methods all breakpoint_ops ultimately
12891 inherit from. */
348d480f 12892
2060206e
PA
12893static void
12894base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12895{
12896 decref_counted_command_line (&self->commands);
12897 xfree (self->cond_string);
fb81d016 12898 xfree (self->extra_string);
f8eba3c6 12899 xfree (self->filter);
f00aae0f
KS
12900 delete_event_location (self->location);
12901 delete_event_location (self->location_range_end);
348d480f
PA
12902}
12903
2060206e
PA
12904static struct bp_location *
12905base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12906{
12907 struct bp_location *loc;
12908
12909 loc = XNEW (struct bp_location);
12910 init_bp_location (loc, &bp_location_ops, self);
12911 return loc;
12912}
12913
2060206e
PA
12914static void
12915base_breakpoint_re_set (struct breakpoint *b)
12916{
12917 /* Nothing to re-set. */
12918}
12919
12920#define internal_error_pure_virtual_called() \
12921 gdb_assert_not_reached ("pure virtual function called")
12922
12923static int
12924base_breakpoint_insert_location (struct bp_location *bl)
12925{
12926 internal_error_pure_virtual_called ();
12927}
12928
12929static int
12930base_breakpoint_remove_location (struct bp_location *bl)
12931{
12932 internal_error_pure_virtual_called ();
12933}
12934
12935static int
12936base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12937 struct address_space *aspace,
09ac7c10
TT
12938 CORE_ADDR bp_addr,
12939 const struct target_waitstatus *ws)
2060206e
PA
12940{
12941 internal_error_pure_virtual_called ();
12942}
12943
12944static void
12945base_breakpoint_check_status (bpstat bs)
12946{
12947 /* Always stop. */
12948}
12949
12950/* A "works_in_software_mode" breakpoint_ops method that just internal
12951 errors. */
12952
12953static int
12954base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12955{
12956 internal_error_pure_virtual_called ();
12957}
12958
12959/* A "resources_needed" breakpoint_ops method that just internal
12960 errors. */
12961
12962static int
12963base_breakpoint_resources_needed (const struct bp_location *bl)
12964{
12965 internal_error_pure_virtual_called ();
12966}
12967
12968static enum print_stop_action
12969base_breakpoint_print_it (bpstat bs)
12970{
12971 internal_error_pure_virtual_called ();
12972}
12973
12974static void
12975base_breakpoint_print_one_detail (const struct breakpoint *self,
12976 struct ui_out *uiout)
12977{
12978 /* nothing */
12979}
12980
12981static void
12982base_breakpoint_print_mention (struct breakpoint *b)
12983{
12984 internal_error_pure_virtual_called ();
12985}
12986
12987static void
12988base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12989{
12990 internal_error_pure_virtual_called ();
12991}
12992
983af33b 12993static void
f00aae0f
KS
12994base_breakpoint_create_sals_from_location
12995 (const struct event_location *location,
12996 struct linespec_result *canonical,
12997 enum bptype type_wanted)
983af33b
SDJ
12998{
12999 internal_error_pure_virtual_called ();
13000}
13001
13002static void
13003base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13004 struct linespec_result *c,
983af33b 13005 char *cond_string,
e7e0cddf 13006 char *extra_string,
983af33b
SDJ
13007 enum bptype type_wanted,
13008 enum bpdisp disposition,
13009 int thread,
13010 int task, int ignore_count,
13011 const struct breakpoint_ops *o,
13012 int from_tty, int enabled,
44f238bb 13013 int internal, unsigned flags)
983af33b
SDJ
13014{
13015 internal_error_pure_virtual_called ();
13016}
13017
13018static void
f00aae0f
KS
13019base_breakpoint_decode_location (struct breakpoint *b,
13020 const struct event_location *location,
983af33b
SDJ
13021 struct symtabs_and_lines *sals)
13022{
13023 internal_error_pure_virtual_called ();
13024}
13025
ab04a2af
TT
13026/* The default 'explains_signal' method. */
13027
47591c29 13028static int
427cd150 13029base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13030{
47591c29 13031 return 1;
ab04a2af
TT
13032}
13033
9d6e6e84
HZ
13034/* The default "after_condition_true" method. */
13035
13036static void
13037base_breakpoint_after_condition_true (struct bpstats *bs)
13038{
13039 /* Nothing to do. */
13040}
13041
ab04a2af 13042struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13043{
13044 base_breakpoint_dtor,
13045 base_breakpoint_allocate_location,
13046 base_breakpoint_re_set,
13047 base_breakpoint_insert_location,
13048 base_breakpoint_remove_location,
13049 base_breakpoint_breakpoint_hit,
13050 base_breakpoint_check_status,
13051 base_breakpoint_resources_needed,
13052 base_breakpoint_works_in_software_mode,
13053 base_breakpoint_print_it,
13054 NULL,
13055 base_breakpoint_print_one_detail,
13056 base_breakpoint_print_mention,
983af33b 13057 base_breakpoint_print_recreate,
5f700d83 13058 base_breakpoint_create_sals_from_location,
983af33b 13059 base_breakpoint_create_breakpoints_sal,
5f700d83 13060 base_breakpoint_decode_location,
9d6e6e84
HZ
13061 base_breakpoint_explains_signal,
13062 base_breakpoint_after_condition_true,
2060206e
PA
13063};
13064
13065/* Default breakpoint_ops methods. */
13066
13067static void
348d480f
PA
13068bkpt_re_set (struct breakpoint *b)
13069{
06edf0c0 13070 /* FIXME: is this still reachable? */
f00aae0f 13071 if (event_location_empty_p (b->location))
06edf0c0 13072 {
f00aae0f 13073 /* Anything without a location can't be re-set. */
348d480f 13074 delete_breakpoint (b);
06edf0c0 13075 return;
348d480f 13076 }
06edf0c0
PA
13077
13078 breakpoint_re_set_default (b);
348d480f
PA
13079}
13080
2060206e 13081static int
348d480f
PA
13082bkpt_insert_location (struct bp_location *bl)
13083{
13084 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 13085 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 13086 else
7c16b83e 13087 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13088}
13089
2060206e 13090static int
348d480f
PA
13091bkpt_remove_location (struct bp_location *bl)
13092{
13093 if (bl->loc_type == bp_loc_hardware_breakpoint)
13094 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13095 else
7c16b83e 13096 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
13097}
13098
2060206e 13099static int
348d480f 13100bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13101 struct address_space *aspace, CORE_ADDR bp_addr,
13102 const struct target_waitstatus *ws)
348d480f 13103{
09ac7c10 13104 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13105 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13106 return 0;
13107
348d480f
PA
13108 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13109 aspace, bp_addr))
13110 return 0;
13111
13112 if (overlay_debugging /* unmapped overlay section */
13113 && section_is_overlay (bl->section)
13114 && !section_is_mapped (bl->section))
13115 return 0;
13116
13117 return 1;
13118}
13119
cd1608cc
PA
13120static int
13121dprintf_breakpoint_hit (const struct bp_location *bl,
13122 struct address_space *aspace, CORE_ADDR bp_addr,
13123 const struct target_waitstatus *ws)
13124{
13125 if (dprintf_style == dprintf_style_agent
13126 && target_can_run_breakpoint_commands ())
13127 {
13128 /* An agent-style dprintf never causes a stop. If we see a trap
13129 for this address it must be for a breakpoint that happens to
13130 be set at the same address. */
13131 return 0;
13132 }
13133
13134 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13135}
13136
2060206e 13137static int
348d480f
PA
13138bkpt_resources_needed (const struct bp_location *bl)
13139{
13140 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13141
13142 return 1;
13143}
13144
2060206e 13145static enum print_stop_action
348d480f
PA
13146bkpt_print_it (bpstat bs)
13147{
348d480f
PA
13148 struct breakpoint *b;
13149 const struct bp_location *bl;
001c8c33 13150 int bp_temp;
79a45e25 13151 struct ui_out *uiout = current_uiout;
348d480f
PA
13152
13153 gdb_assert (bs->bp_location_at != NULL);
13154
13155 bl = bs->bp_location_at;
13156 b = bs->breakpoint_at;
13157
001c8c33
PA
13158 bp_temp = b->disposition == disp_del;
13159 if (bl->address != bl->requested_address)
13160 breakpoint_adjustment_warning (bl->requested_address,
13161 bl->address,
13162 b->number, 1);
13163 annotate_breakpoint (b->number);
f303dbd6
PA
13164 maybe_print_thread_hit_breakpoint (uiout);
13165
001c8c33 13166 if (bp_temp)
f303dbd6 13167 ui_out_text (uiout, "Temporary breakpoint ");
001c8c33 13168 else
f303dbd6 13169 ui_out_text (uiout, "Breakpoint ");
001c8c33 13170 if (ui_out_is_mi_like_p (uiout))
348d480f 13171 {
001c8c33
PA
13172 ui_out_field_string (uiout, "reason",
13173 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13174 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13175 }
001c8c33
PA
13176 ui_out_field_int (uiout, "bkptno", b->number);
13177 ui_out_text (uiout, ", ");
06edf0c0 13178
001c8c33 13179 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13180}
13181
2060206e 13182static void
06edf0c0
PA
13183bkpt_print_mention (struct breakpoint *b)
13184{
79a45e25 13185 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13186 return;
13187
13188 switch (b->type)
13189 {
13190 case bp_breakpoint:
13191 case bp_gnu_ifunc_resolver:
13192 if (b->disposition == disp_del)
13193 printf_filtered (_("Temporary breakpoint"));
13194 else
13195 printf_filtered (_("Breakpoint"));
13196 printf_filtered (_(" %d"), b->number);
13197 if (b->type == bp_gnu_ifunc_resolver)
13198 printf_filtered (_(" at gnu-indirect-function resolver"));
13199 break;
13200 case bp_hardware_breakpoint:
13201 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13202 break;
e7e0cddf
SS
13203 case bp_dprintf:
13204 printf_filtered (_("Dprintf %d"), b->number);
13205 break;
06edf0c0
PA
13206 }
13207
13208 say_where (b);
13209}
13210
2060206e 13211static void
06edf0c0
PA
13212bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13213{
13214 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13215 fprintf_unfiltered (fp, "tbreak");
13216 else if (tp->type == bp_breakpoint)
13217 fprintf_unfiltered (fp, "break");
13218 else if (tp->type == bp_hardware_breakpoint
13219 && tp->disposition == disp_del)
13220 fprintf_unfiltered (fp, "thbreak");
13221 else if (tp->type == bp_hardware_breakpoint)
13222 fprintf_unfiltered (fp, "hbreak");
13223 else
13224 internal_error (__FILE__, __LINE__,
13225 _("unhandled breakpoint type %d"), (int) tp->type);
13226
f00aae0f
KS
13227 fprintf_unfiltered (fp, " %s",
13228 event_location_to_string (tp->location));
13229
13230 /* Print out extra_string if this breakpoint is pending. It might
13231 contain, for example, conditions that were set by the user. */
13232 if (tp->loc == NULL && tp->extra_string != NULL)
13233 fprintf_unfiltered (fp, " %s", tp->extra_string);
13234
dd11a36c 13235 print_recreate_thread (tp, fp);
06edf0c0
PA
13236}
13237
983af33b 13238static void
f00aae0f
KS
13239bkpt_create_sals_from_location (const struct event_location *location,
13240 struct linespec_result *canonical,
13241 enum bptype type_wanted)
983af33b 13242{
f00aae0f 13243 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13244}
13245
13246static void
13247bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13248 struct linespec_result *canonical,
983af33b 13249 char *cond_string,
e7e0cddf 13250 char *extra_string,
983af33b
SDJ
13251 enum bptype type_wanted,
13252 enum bpdisp disposition,
13253 int thread,
13254 int task, int ignore_count,
13255 const struct breakpoint_ops *ops,
13256 int from_tty, int enabled,
44f238bb 13257 int internal, unsigned flags)
983af33b 13258{
023fa29b 13259 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13260 cond_string, extra_string,
13261 type_wanted,
983af33b
SDJ
13262 disposition, thread, task,
13263 ignore_count, ops, from_tty,
44f238bb 13264 enabled, internal, flags);
983af33b
SDJ
13265}
13266
13267static void
f00aae0f
KS
13268bkpt_decode_location (struct breakpoint *b,
13269 const struct event_location *location,
983af33b
SDJ
13270 struct symtabs_and_lines *sals)
13271{
f00aae0f 13272 decode_location_default (b, location, sals);
983af33b
SDJ
13273}
13274
06edf0c0
PA
13275/* Virtual table for internal breakpoints. */
13276
13277static void
13278internal_bkpt_re_set (struct breakpoint *b)
13279{
13280 switch (b->type)
13281 {
13282 /* Delete overlay event and longjmp master breakpoints; they
13283 will be reset later by breakpoint_re_set. */
13284 case bp_overlay_event:
13285 case bp_longjmp_master:
13286 case bp_std_terminate_master:
13287 case bp_exception_master:
13288 delete_breakpoint (b);
13289 break;
13290
13291 /* This breakpoint is special, it's set up when the inferior
13292 starts and we really don't want to touch it. */
13293 case bp_shlib_event:
13294
13295 /* Like bp_shlib_event, this breakpoint type is special. Once
13296 it is set up, we do not want to touch it. */
13297 case bp_thread_event:
13298 break;
13299 }
13300}
13301
13302static void
13303internal_bkpt_check_status (bpstat bs)
13304{
a9b3a50f
PA
13305 if (bs->breakpoint_at->type == bp_shlib_event)
13306 {
13307 /* If requested, stop when the dynamic linker notifies GDB of
13308 events. This allows the user to get control and place
13309 breakpoints in initializer routines for dynamically loaded
13310 objects (among other things). */
13311 bs->stop = stop_on_solib_events;
13312 bs->print = stop_on_solib_events;
13313 }
13314 else
13315 bs->stop = 0;
06edf0c0
PA
13316}
13317
13318static enum print_stop_action
13319internal_bkpt_print_it (bpstat bs)
13320{
06edf0c0 13321 struct breakpoint *b;
06edf0c0 13322
06edf0c0
PA
13323 b = bs->breakpoint_at;
13324
06edf0c0
PA
13325 switch (b->type)
13326 {
348d480f
PA
13327 case bp_shlib_event:
13328 /* Did we stop because the user set the stop_on_solib_events
13329 variable? (If so, we report this as a generic, "Stopped due
13330 to shlib event" message.) */
edcc5120 13331 print_solib_event (0);
348d480f
PA
13332 break;
13333
13334 case bp_thread_event:
13335 /* Not sure how we will get here.
13336 GDB should not stop for these breakpoints. */
13337 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13338 break;
13339
13340 case bp_overlay_event:
13341 /* By analogy with the thread event, GDB should not stop for these. */
13342 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13343 break;
13344
13345 case bp_longjmp_master:
13346 /* These should never be enabled. */
13347 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13348 break;
13349
13350 case bp_std_terminate_master:
13351 /* These should never be enabled. */
13352 printf_filtered (_("std::terminate Master Breakpoint: "
13353 "gdb should not stop!\n"));
348d480f
PA
13354 break;
13355
13356 case bp_exception_master:
13357 /* These should never be enabled. */
13358 printf_filtered (_("Exception Master Breakpoint: "
13359 "gdb should not stop!\n"));
06edf0c0
PA
13360 break;
13361 }
13362
001c8c33 13363 return PRINT_NOTHING;
06edf0c0
PA
13364}
13365
13366static void
13367internal_bkpt_print_mention (struct breakpoint *b)
13368{
13369 /* Nothing to mention. These breakpoints are internal. */
13370}
13371
06edf0c0
PA
13372/* Virtual table for momentary breakpoints */
13373
13374static void
13375momentary_bkpt_re_set (struct breakpoint *b)
13376{
13377 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13378 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13379 Otherwise these should have been blown away via the cleanup chain
13380 or by breakpoint_init_inferior when we rerun the executable. */
13381}
13382
13383static void
13384momentary_bkpt_check_status (bpstat bs)
13385{
13386 /* Nothing. The point of these breakpoints is causing a stop. */
13387}
13388
13389static enum print_stop_action
13390momentary_bkpt_print_it (bpstat bs)
13391{
001c8c33 13392 return PRINT_UNKNOWN;
348d480f
PA
13393}
13394
06edf0c0
PA
13395static void
13396momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13397{
06edf0c0 13398 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13399}
13400
e2e4d78b
JK
13401/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13402
13403 It gets cleared already on the removal of the first one of such placed
13404 breakpoints. This is OK as they get all removed altogether. */
13405
13406static void
13407longjmp_bkpt_dtor (struct breakpoint *self)
13408{
5d5658a1 13409 struct thread_info *tp = find_thread_global_id (self->thread);
e2e4d78b
JK
13410
13411 if (tp)
13412 tp->initiating_frame = null_frame_id;
13413
13414 momentary_breakpoint_ops.dtor (self);
13415}
13416
55aa24fb
SDJ
13417/* Specific methods for probe breakpoints. */
13418
13419static int
13420bkpt_probe_insert_location (struct bp_location *bl)
13421{
13422 int v = bkpt_insert_location (bl);
13423
13424 if (v == 0)
13425 {
13426 /* The insertion was successful, now let's set the probe's semaphore
13427 if needed. */
0ea5cda8
SDJ
13428 if (bl->probe.probe->pops->set_semaphore != NULL)
13429 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13430 bl->probe.objfile,
13431 bl->gdbarch);
55aa24fb
SDJ
13432 }
13433
13434 return v;
13435}
13436
13437static int
13438bkpt_probe_remove_location (struct bp_location *bl)
13439{
13440 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13441 if (bl->probe.probe->pops->clear_semaphore != NULL)
13442 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13443 bl->probe.objfile,
13444 bl->gdbarch);
55aa24fb
SDJ
13445
13446 return bkpt_remove_location (bl);
13447}
13448
13449static void
f00aae0f 13450bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13451 struct linespec_result *canonical,
f00aae0f 13452 enum bptype type_wanted)
55aa24fb
SDJ
13453{
13454 struct linespec_sals lsal;
13455
f00aae0f
KS
13456 lsal.sals = parse_probes (location, canonical);
13457 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
55aa24fb
SDJ
13458 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13459}
13460
13461static void
f00aae0f
KS
13462bkpt_probe_decode_location (struct breakpoint *b,
13463 const struct event_location *location,
55aa24fb
SDJ
13464 struct symtabs_and_lines *sals)
13465{
f00aae0f 13466 *sals = parse_probes (location, NULL);
55aa24fb
SDJ
13467 if (!sals->sals)
13468 error (_("probe not found"));
13469}
13470
348d480f 13471/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13472
348d480f
PA
13473static void
13474tracepoint_re_set (struct breakpoint *b)
13475{
13476 breakpoint_re_set_default (b);
13477}
876fa593 13478
348d480f
PA
13479static int
13480tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13481 struct address_space *aspace, CORE_ADDR bp_addr,
13482 const struct target_waitstatus *ws)
348d480f
PA
13483{
13484 /* By definition, the inferior does not report stops at
13485 tracepoints. */
13486 return 0;
74960c60
VP
13487}
13488
13489static void
348d480f
PA
13490tracepoint_print_one_detail (const struct breakpoint *self,
13491 struct ui_out *uiout)
74960c60 13492{
d9b3f62e
PA
13493 struct tracepoint *tp = (struct tracepoint *) self;
13494 if (tp->static_trace_marker_id)
348d480f
PA
13495 {
13496 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13497
348d480f
PA
13498 ui_out_text (uiout, "\tmarker id is ");
13499 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13500 tp->static_trace_marker_id);
348d480f
PA
13501 ui_out_text (uiout, "\n");
13502 }
0d381245
VP
13503}
13504
a474d7c2 13505static void
348d480f 13506tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13507{
79a45e25 13508 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13509 return;
cc59ec59 13510
348d480f
PA
13511 switch (b->type)
13512 {
13513 case bp_tracepoint:
13514 printf_filtered (_("Tracepoint"));
13515 printf_filtered (_(" %d"), b->number);
13516 break;
13517 case bp_fast_tracepoint:
13518 printf_filtered (_("Fast tracepoint"));
13519 printf_filtered (_(" %d"), b->number);
13520 break;
13521 case bp_static_tracepoint:
13522 printf_filtered (_("Static tracepoint"));
13523 printf_filtered (_(" %d"), b->number);
13524 break;
13525 default:
13526 internal_error (__FILE__, __LINE__,
13527 _("unhandled tracepoint type %d"), (int) b->type);
13528 }
13529
13530 say_where (b);
a474d7c2
PA
13531}
13532
348d480f 13533static void
d9b3f62e 13534tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13535{
d9b3f62e
PA
13536 struct tracepoint *tp = (struct tracepoint *) self;
13537
13538 if (self->type == bp_fast_tracepoint)
348d480f 13539 fprintf_unfiltered (fp, "ftrace");
c93e8391 13540 else if (self->type == bp_static_tracepoint)
348d480f 13541 fprintf_unfiltered (fp, "strace");
d9b3f62e 13542 else if (self->type == bp_tracepoint)
348d480f
PA
13543 fprintf_unfiltered (fp, "trace");
13544 else
13545 internal_error (__FILE__, __LINE__,
d9b3f62e 13546 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13547
f00aae0f
KS
13548 fprintf_unfiltered (fp, " %s",
13549 event_location_to_string (self->location));
d9b3f62e
PA
13550 print_recreate_thread (self, fp);
13551
13552 if (tp->pass_count)
13553 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13554}
13555
983af33b 13556static void
f00aae0f
KS
13557tracepoint_create_sals_from_location (const struct event_location *location,
13558 struct linespec_result *canonical,
13559 enum bptype type_wanted)
983af33b 13560{
f00aae0f 13561 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13562}
13563
13564static void
13565tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13566 struct linespec_result *canonical,
983af33b 13567 char *cond_string,
e7e0cddf 13568 char *extra_string,
983af33b
SDJ
13569 enum bptype type_wanted,
13570 enum bpdisp disposition,
13571 int thread,
13572 int task, int ignore_count,
13573 const struct breakpoint_ops *ops,
13574 int from_tty, int enabled,
44f238bb 13575 int internal, unsigned flags)
983af33b 13576{
023fa29b 13577 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13578 cond_string, extra_string,
13579 type_wanted,
983af33b
SDJ
13580 disposition, thread, task,
13581 ignore_count, ops, from_tty,
44f238bb 13582 enabled, internal, flags);
983af33b
SDJ
13583}
13584
13585static void
f00aae0f
KS
13586tracepoint_decode_location (struct breakpoint *b,
13587 const struct event_location *location,
983af33b
SDJ
13588 struct symtabs_and_lines *sals)
13589{
f00aae0f 13590 decode_location_default (b, location, sals);
983af33b
SDJ
13591}
13592
2060206e 13593struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13594
55aa24fb
SDJ
13595/* The breakpoint_ops structure to be use on tracepoints placed in a
13596 static probe. */
13597
13598static void
f00aae0f
KS
13599tracepoint_probe_create_sals_from_location
13600 (const struct event_location *location,
13601 struct linespec_result *canonical,
13602 enum bptype type_wanted)
55aa24fb
SDJ
13603{
13604 /* We use the same method for breakpoint on probes. */
f00aae0f 13605 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13606}
13607
13608static void
f00aae0f
KS
13609tracepoint_probe_decode_location (struct breakpoint *b,
13610 const struct event_location *location,
55aa24fb
SDJ
13611 struct symtabs_and_lines *sals)
13612{
13613 /* We use the same method for breakpoint on probes. */
f00aae0f 13614 bkpt_probe_decode_location (b, location, sals);
55aa24fb
SDJ
13615}
13616
13617static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13618
5c2b4418
HZ
13619/* Dprintf breakpoint_ops methods. */
13620
13621static void
13622dprintf_re_set (struct breakpoint *b)
13623{
13624 breakpoint_re_set_default (b);
13625
f00aae0f
KS
13626 /* extra_string should never be non-NULL for dprintf. */
13627 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13628
13629 /* 1 - connect to target 1, that can run breakpoint commands.
13630 2 - create a dprintf, which resolves fine.
13631 3 - disconnect from target 1
13632 4 - connect to target 2, that can NOT run breakpoint commands.
13633
13634 After steps #3/#4, you'll want the dprintf command list to
13635 be updated, because target 1 and 2 may well return different
13636 answers for target_can_run_breakpoint_commands().
13637 Given absence of finer grained resetting, we get to do
13638 it all the time. */
13639 if (b->extra_string != NULL)
13640 update_dprintf_command_list (b);
13641}
13642
2d9442cc
HZ
13643/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13644
13645static void
13646dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13647{
f00aae0f
KS
13648 fprintf_unfiltered (fp, "dprintf %s,%s",
13649 event_location_to_string (tp->location),
2d9442cc
HZ
13650 tp->extra_string);
13651 print_recreate_thread (tp, fp);
13652}
13653
9d6e6e84
HZ
13654/* Implement the "after_condition_true" breakpoint_ops method for
13655 dprintf.
13656
13657 dprintf's are implemented with regular commands in their command
13658 list, but we run the commands here instead of before presenting the
13659 stop to the user, as dprintf's don't actually cause a stop. This
13660 also makes it so that the commands of multiple dprintfs at the same
13661 address are all handled. */
13662
13663static void
13664dprintf_after_condition_true (struct bpstats *bs)
13665{
13666 struct cleanup *old_chain;
13667 struct bpstats tmp_bs = { NULL };
13668 struct bpstats *tmp_bs_p = &tmp_bs;
13669
13670 /* dprintf's never cause a stop. This wasn't set in the
13671 check_status hook instead because that would make the dprintf's
13672 condition not be evaluated. */
13673 bs->stop = 0;
13674
13675 /* Run the command list here. Take ownership of it instead of
13676 copying. We never want these commands to run later in
13677 bpstat_do_actions, if a breakpoint that causes a stop happens to
13678 be set at same address as this dprintf, or even if running the
13679 commands here throws. */
13680 tmp_bs.commands = bs->commands;
13681 bs->commands = NULL;
13682 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13683
13684 bpstat_do_actions_1 (&tmp_bs_p);
13685
13686 /* 'tmp_bs.commands' will usually be NULL by now, but
13687 bpstat_do_actions_1 may return early without processing the whole
13688 list. */
13689 do_cleanups (old_chain);
13690}
13691
983af33b
SDJ
13692/* The breakpoint_ops structure to be used on static tracepoints with
13693 markers (`-m'). */
13694
13695static void
f00aae0f 13696strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13697 struct linespec_result *canonical,
f00aae0f 13698 enum bptype type_wanted)
983af33b
SDJ
13699{
13700 struct linespec_sals lsal;
f00aae0f
KS
13701 const char *arg_start, *arg;
13702 char *str;
13703 struct cleanup *cleanup;
983af33b 13704
f00aae0f
KS
13705 arg = arg_start = get_linespec_location (location);
13706 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13707
f00aae0f
KS
13708 str = savestring (arg_start, arg - arg_start);
13709 cleanup = make_cleanup (xfree, str);
13710 canonical->location = new_linespec_location (&str);
13711 do_cleanups (cleanup);
983af33b 13712
f00aae0f 13713 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
983af33b
SDJ
13714 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13715}
13716
13717static void
13718strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13719 struct linespec_result *canonical,
983af33b 13720 char *cond_string,
e7e0cddf 13721 char *extra_string,
983af33b
SDJ
13722 enum bptype type_wanted,
13723 enum bpdisp disposition,
13724 int thread,
13725 int task, int ignore_count,
13726 const struct breakpoint_ops *ops,
13727 int from_tty, int enabled,
44f238bb 13728 int internal, unsigned flags)
983af33b
SDJ
13729{
13730 int i;
52d361e1
YQ
13731 struct linespec_sals *lsal = VEC_index (linespec_sals,
13732 canonical->sals, 0);
983af33b
SDJ
13733
13734 /* If the user is creating a static tracepoint by marker id
13735 (strace -m MARKER_ID), then store the sals index, so that
13736 breakpoint_re_set can try to match up which of the newly
13737 found markers corresponds to this one, and, don't try to
13738 expand multiple locations for each sal, given than SALS
13739 already should contain all sals for MARKER_ID. */
13740
13741 for (i = 0; i < lsal->sals.nelts; ++i)
13742 {
13743 struct symtabs_and_lines expanded;
13744 struct tracepoint *tp;
13745 struct cleanup *old_chain;
f00aae0f 13746 struct event_location *location;
983af33b
SDJ
13747
13748 expanded.nelts = 1;
13749 expanded.sals = &lsal->sals.sals[i];
13750
f00aae0f
KS
13751 location = copy_event_location (canonical->location);
13752 old_chain = make_cleanup_delete_event_location (location);
983af33b
SDJ
13753
13754 tp = XCNEW (struct tracepoint);
13755 init_breakpoint_sal (&tp->base, gdbarch, expanded,
f00aae0f 13756 location, NULL,
e7e0cddf
SS
13757 cond_string, extra_string,
13758 type_wanted, disposition,
983af33b 13759 thread, task, ignore_count, ops,
44f238bb 13760 from_tty, enabled, internal, flags,
983af33b
SDJ
13761 canonical->special_display);
13762 /* Given that its possible to have multiple markers with
13763 the same string id, if the user is creating a static
13764 tracepoint by marker id ("strace -m MARKER_ID"), then
13765 store the sals index, so that breakpoint_re_set can
13766 try to match up which of the newly found markers
13767 corresponds to this one */
13768 tp->static_trace_marker_id_idx = i;
13769
13770 install_breakpoint (internal, &tp->base, 0);
13771
13772 discard_cleanups (old_chain);
13773 }
13774}
13775
13776static void
f00aae0f
KS
13777strace_marker_decode_location (struct breakpoint *b,
13778 const struct event_location *location,
983af33b
SDJ
13779 struct symtabs_and_lines *sals)
13780{
13781 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13782 const char *s = get_linespec_location (location);
983af33b 13783
f00aae0f 13784 *sals = decode_static_tracepoint_spec (&s);
983af33b
SDJ
13785 if (sals->nelts > tp->static_trace_marker_id_idx)
13786 {
13787 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13788 sals->nelts = 1;
13789 }
13790 else
13791 error (_("marker %s not found"), tp->static_trace_marker_id);
13792}
13793
13794static struct breakpoint_ops strace_marker_breakpoint_ops;
13795
13796static int
13797strace_marker_p (struct breakpoint *b)
13798{
13799 return b->ops == &strace_marker_breakpoint_ops;
13800}
13801
53a5351d 13802/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13803 structures. */
c906108c
SS
13804
13805void
fba45db2 13806delete_breakpoint (struct breakpoint *bpt)
c906108c 13807{
52f0bd74 13808 struct breakpoint *b;
c906108c 13809
8a3fe4f8 13810 gdb_assert (bpt != NULL);
c906108c 13811
4a64f543
MS
13812 /* Has this bp already been deleted? This can happen because
13813 multiple lists can hold pointers to bp's. bpstat lists are
13814 especial culprits.
13815
13816 One example of this happening is a watchpoint's scope bp. When
13817 the scope bp triggers, we notice that the watchpoint is out of
13818 scope, and delete it. We also delete its scope bp. But the
13819 scope bp is marked "auto-deleting", and is already on a bpstat.
13820 That bpstat is then checked for auto-deleting bp's, which are
13821 deleted.
13822
13823 A real solution to this problem might involve reference counts in
13824 bp's, and/or giving them pointers back to their referencing
13825 bpstat's, and teaching delete_breakpoint to only free a bp's
13826 storage when no more references were extent. A cheaper bandaid
13827 was chosen. */
c906108c
SS
13828 if (bpt->type == bp_none)
13829 return;
13830
4a64f543
MS
13831 /* At least avoid this stale reference until the reference counting
13832 of breakpoints gets resolved. */
d0fb5eae 13833 if (bpt->related_breakpoint != bpt)
e5a0a904 13834 {
d0fb5eae 13835 struct breakpoint *related;
3a5c3e22 13836 struct watchpoint *w;
d0fb5eae
JK
13837
13838 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13839 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13840 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13841 w = (struct watchpoint *) bpt;
13842 else
13843 w = NULL;
13844 if (w != NULL)
13845 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13846
13847 /* Unlink bpt from the bpt->related_breakpoint ring. */
13848 for (related = bpt; related->related_breakpoint != bpt;
13849 related = related->related_breakpoint);
13850 related->related_breakpoint = bpt->related_breakpoint;
13851 bpt->related_breakpoint = bpt;
e5a0a904
JK
13852 }
13853
a9634178
TJB
13854 /* watch_command_1 creates a watchpoint but only sets its number if
13855 update_watchpoint succeeds in creating its bp_locations. If there's
13856 a problem in that process, we'll be asked to delete the half-created
13857 watchpoint. In that case, don't announce the deletion. */
13858 if (bpt->number)
13859 observer_notify_breakpoint_deleted (bpt);
c906108c 13860
c906108c
SS
13861 if (breakpoint_chain == bpt)
13862 breakpoint_chain = bpt->next;
13863
c906108c
SS
13864 ALL_BREAKPOINTS (b)
13865 if (b->next == bpt)
c5aa993b
JM
13866 {
13867 b->next = bpt->next;
13868 break;
13869 }
c906108c 13870
f431efe5
PA
13871 /* Be sure no bpstat's are pointing at the breakpoint after it's
13872 been freed. */
13873 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13874 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13875 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13876 commands are associated with the bpstat; if we remove it here,
13877 then the later call to bpstat_do_actions (&stop_bpstat); in
13878 event-top.c won't do anything, and temporary breakpoints with
13879 commands won't work. */
13880
13881 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13882
4a64f543
MS
13883 /* Now that breakpoint is removed from breakpoint list, update the
13884 global location list. This will remove locations that used to
13885 belong to this breakpoint. Do this before freeing the breakpoint
13886 itself, since remove_breakpoint looks at location's owner. It
13887 might be better design to have location completely
13888 self-contained, but it's not the case now. */
44702360 13889 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13890
348d480f 13891 bpt->ops->dtor (bpt);
4a64f543
MS
13892 /* On the chance that someone will soon try again to delete this
13893 same bp, we mark it as deleted before freeing its storage. */
c906108c 13894 bpt->type = bp_none;
b8c9b27d 13895 xfree (bpt);
c906108c
SS
13896}
13897
4d6140d9
AC
13898static void
13899do_delete_breakpoint_cleanup (void *b)
13900{
9a3c8263 13901 delete_breakpoint ((struct breakpoint *) b);
4d6140d9
AC
13902}
13903
13904struct cleanup *
13905make_cleanup_delete_breakpoint (struct breakpoint *b)
13906{
13907 return make_cleanup (do_delete_breakpoint_cleanup, b);
13908}
13909
51be5b68
PA
13910/* Iterator function to call a user-provided callback function once
13911 for each of B and its related breakpoints. */
13912
13913static void
13914iterate_over_related_breakpoints (struct breakpoint *b,
13915 void (*function) (struct breakpoint *,
13916 void *),
13917 void *data)
13918{
13919 struct breakpoint *related;
13920
13921 related = b;
13922 do
13923 {
13924 struct breakpoint *next;
13925
13926 /* FUNCTION may delete RELATED. */
13927 next = related->related_breakpoint;
13928
13929 if (next == related)
13930 {
13931 /* RELATED is the last ring entry. */
13932 function (related, data);
13933
13934 /* FUNCTION may have deleted it, so we'd never reach back to
13935 B. There's nothing left to do anyway, so just break
13936 out. */
13937 break;
13938 }
13939 else
13940 function (related, data);
13941
13942 related = next;
13943 }
13944 while (related != b);
13945}
95a42b64
TT
13946
13947static void
13948do_delete_breakpoint (struct breakpoint *b, void *ignore)
13949{
13950 delete_breakpoint (b);
13951}
13952
51be5b68
PA
13953/* A callback for map_breakpoint_numbers that calls
13954 delete_breakpoint. */
13955
13956static void
13957do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13958{
13959 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13960}
13961
c906108c 13962void
fba45db2 13963delete_command (char *arg, int from_tty)
c906108c 13964{
35df4500 13965 struct breakpoint *b, *b_tmp;
c906108c 13966
ea9365bb
TT
13967 dont_repeat ();
13968
c906108c
SS
13969 if (arg == 0)
13970 {
13971 int breaks_to_delete = 0;
13972
46c6471b
PA
13973 /* Delete all breakpoints if no argument. Do not delete
13974 internal breakpoints, these have to be deleted with an
13975 explicit breakpoint number argument. */
c5aa993b 13976 ALL_BREAKPOINTS (b)
46c6471b 13977 if (user_breakpoint_p (b))
973d738b
DJ
13978 {
13979 breaks_to_delete = 1;
13980 break;
13981 }
c906108c
SS
13982
13983 /* Ask user only if there are some breakpoints to delete. */
13984 if (!from_tty
e2e0b3e5 13985 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13986 {
35df4500 13987 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13988 if (user_breakpoint_p (b))
c5aa993b 13989 delete_breakpoint (b);
c906108c
SS
13990 }
13991 }
13992 else
51be5b68 13993 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13994}
13995
0d381245
VP
13996static int
13997all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 13998{
0d381245 13999 for (; loc; loc = loc->next)
8645ff69
UW
14000 if (!loc->shlib_disabled
14001 && !loc->pspace->executing_startup)
0d381245
VP
14002 return 0;
14003 return 1;
fe3f5fa8
VP
14004}
14005
776592bf
DE
14006/* Subroutine of update_breakpoint_locations to simplify it.
14007 Return non-zero if multiple fns in list LOC have the same name.
14008 Null names are ignored. */
14009
14010static int
14011ambiguous_names_p (struct bp_location *loc)
14012{
14013 struct bp_location *l;
14014 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14015 (int (*) (const void *,
14016 const void *)) streq,
776592bf
DE
14017 NULL, xcalloc, xfree);
14018
14019 for (l = loc; l != NULL; l = l->next)
14020 {
14021 const char **slot;
14022 const char *name = l->function_name;
14023
14024 /* Allow for some names to be NULL, ignore them. */
14025 if (name == NULL)
14026 continue;
14027
14028 slot = (const char **) htab_find_slot (htab, (const void *) name,
14029 INSERT);
4a64f543
MS
14030 /* NOTE: We can assume slot != NULL here because xcalloc never
14031 returns NULL. */
776592bf
DE
14032 if (*slot != NULL)
14033 {
14034 htab_delete (htab);
14035 return 1;
14036 }
14037 *slot = name;
14038 }
14039
14040 htab_delete (htab);
14041 return 0;
14042}
14043
0fb4aa4b
PA
14044/* When symbols change, it probably means the sources changed as well,
14045 and it might mean the static tracepoint markers are no longer at
14046 the same address or line numbers they used to be at last we
14047 checked. Losing your static tracepoints whenever you rebuild is
14048 undesirable. This function tries to resync/rematch gdb static
14049 tracepoints with the markers on the target, for static tracepoints
14050 that have not been set by marker id. Static tracepoint that have
14051 been set by marker id are reset by marker id in breakpoint_re_set.
14052 The heuristic is:
14053
14054 1) For a tracepoint set at a specific address, look for a marker at
14055 the old PC. If one is found there, assume to be the same marker.
14056 If the name / string id of the marker found is different from the
14057 previous known name, assume that means the user renamed the marker
14058 in the sources, and output a warning.
14059
14060 2) For a tracepoint set at a given line number, look for a marker
14061 at the new address of the old line number. If one is found there,
14062 assume to be the same marker. If the name / string id of the
14063 marker found is different from the previous known name, assume that
14064 means the user renamed the marker in the sources, and output a
14065 warning.
14066
14067 3) If a marker is no longer found at the same address or line, it
14068 may mean the marker no longer exists. But it may also just mean
14069 the code changed a bit. Maybe the user added a few lines of code
14070 that made the marker move up or down (in line number terms). Ask
14071 the target for info about the marker with the string id as we knew
14072 it. If found, update line number and address in the matching
14073 static tracepoint. This will get confused if there's more than one
14074 marker with the same ID (possible in UST, although unadvised
14075 precisely because it confuses tools). */
14076
14077static struct symtab_and_line
14078update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14079{
d9b3f62e 14080 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14081 struct static_tracepoint_marker marker;
14082 CORE_ADDR pc;
0fb4aa4b
PA
14083
14084 pc = sal.pc;
14085 if (sal.line)
14086 find_line_pc (sal.symtab, sal.line, &pc);
14087
14088 if (target_static_tracepoint_marker_at (pc, &marker))
14089 {
d9b3f62e 14090 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14091 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14092 b->number,
d9b3f62e 14093 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14094
d9b3f62e
PA
14095 xfree (tp->static_trace_marker_id);
14096 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14097 release_static_tracepoint_marker (&marker);
14098
14099 return sal;
14100 }
14101
14102 /* Old marker wasn't found on target at lineno. Try looking it up
14103 by string ID. */
14104 if (!sal.explicit_pc
14105 && sal.line != 0
14106 && sal.symtab != NULL
d9b3f62e 14107 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14108 {
14109 VEC(static_tracepoint_marker_p) *markers;
14110
14111 markers
d9b3f62e 14112 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14113
14114 if (!VEC_empty(static_tracepoint_marker_p, markers))
14115 {
80e1d417 14116 struct symtab_and_line sal2;
0fb4aa4b 14117 struct symbol *sym;
80e1d417 14118 struct static_tracepoint_marker *tpmarker;
79a45e25 14119 struct ui_out *uiout = current_uiout;
67994074 14120 struct explicit_location explicit_loc;
0fb4aa4b 14121
80e1d417 14122 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14123
d9b3f62e 14124 xfree (tp->static_trace_marker_id);
80e1d417 14125 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14126
14127 warning (_("marker for static tracepoint %d (%s) not "
14128 "found at previous line number"),
d9b3f62e 14129 b->number, tp->static_trace_marker_id);
0fb4aa4b 14130
80e1d417 14131 init_sal (&sal2);
0fb4aa4b 14132
80e1d417 14133 sal2.pc = tpmarker->address;
0fb4aa4b 14134
80e1d417
AS
14135 sal2 = find_pc_line (tpmarker->address, 0);
14136 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14137 ui_out_text (uiout, "Now in ");
14138 if (sym)
14139 {
14140 ui_out_field_string (uiout, "func",
14141 SYMBOL_PRINT_NAME (sym));
14142 ui_out_text (uiout, " at ");
14143 }
05cba821
JK
14144 ui_out_field_string (uiout, "file",
14145 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14146 ui_out_text (uiout, ":");
14147
14148 if (ui_out_is_mi_like_p (uiout))
14149 {
0b0865da 14150 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14151
f35a17b5 14152 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14153 }
14154
80e1d417 14155 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14156 ui_out_text (uiout, "\n");
14157
80e1d417 14158 b->loc->line_number = sal2.line;
2f202fde 14159 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 14160
f00aae0f 14161 delete_event_location (b->location);
67994074
KS
14162 initialize_explicit_location (&explicit_loc);
14163 explicit_loc.source_filename
00e52e53 14164 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
14165 explicit_loc.line_offset.offset = b->loc->line_number;
14166 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14167 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
14168
14169 /* Might be nice to check if function changed, and warn if
14170 so. */
14171
80e1d417 14172 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14173 }
14174 }
14175 return sal;
14176}
14177
8d3788bd
VP
14178/* Returns 1 iff locations A and B are sufficiently same that
14179 we don't need to report breakpoint as changed. */
14180
14181static int
14182locations_are_equal (struct bp_location *a, struct bp_location *b)
14183{
14184 while (a && b)
14185 {
14186 if (a->address != b->address)
14187 return 0;
14188
14189 if (a->shlib_disabled != b->shlib_disabled)
14190 return 0;
14191
14192 if (a->enabled != b->enabled)
14193 return 0;
14194
14195 a = a->next;
14196 b = b->next;
14197 }
14198
14199 if ((a == NULL) != (b == NULL))
14200 return 0;
14201
14202 return 1;
14203}
14204
f1310107
TJB
14205/* Create new breakpoint locations for B (a hardware or software breakpoint)
14206 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14207 a ranged breakpoint. */
14208
0e30163f 14209void
0d381245 14210update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14211 struct symtabs_and_lines sals,
14212 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14213{
14214 int i;
0d381245
VP
14215 struct bp_location *existing_locations = b->loc;
14216
f8eba3c6
TT
14217 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14218 {
14219 /* Ranged breakpoints have only one start location and one end
14220 location. */
14221 b->enable_state = bp_disabled;
44702360 14222 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
14223 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14224 "multiple locations found\n"),
14225 b->number);
14226 return;
14227 }
f1310107 14228
4a64f543
MS
14229 /* If there's no new locations, and all existing locations are
14230 pending, don't do anything. This optimizes the common case where
14231 all locations are in the same shared library, that was unloaded.
14232 We'd like to retain the location, so that when the library is
14233 loaded again, we don't loose the enabled/disabled status of the
14234 individual locations. */
0d381245 14235 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14236 return;
14237
fe3f5fa8
VP
14238 b->loc = NULL;
14239
0d381245 14240 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14241 {
f8eba3c6
TT
14242 struct bp_location *new_loc;
14243
14244 switch_to_program_space_and_thread (sals.sals[i].pspace);
14245
14246 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14247
0d381245
VP
14248 /* Reparse conditions, they might contain references to the
14249 old symtab. */
14250 if (b->cond_string != NULL)
14251 {
bbc13ae3 14252 const char *s;
fe3f5fa8 14253
0d381245 14254 s = b->cond_string;
492d29ea 14255 TRY
0d381245 14256 {
1bb9788d
TT
14257 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14258 block_for_pc (sals.sals[i].pc),
0d381245
VP
14259 0);
14260 }
492d29ea 14261 CATCH (e, RETURN_MASK_ERROR)
0d381245 14262 {
3e43a32a
MS
14263 warning (_("failed to reevaluate condition "
14264 "for breakpoint %d: %s"),
0d381245
VP
14265 b->number, e.message);
14266 new_loc->enabled = 0;
14267 }
492d29ea 14268 END_CATCH
0d381245 14269 }
fe3f5fa8 14270
f1310107
TJB
14271 if (sals_end.nelts)
14272 {
14273 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14274
14275 new_loc->length = end - sals.sals[0].pc + 1;
14276 }
0d381245 14277 }
fe3f5fa8 14278
4a64f543
MS
14279 /* If possible, carry over 'disable' status from existing
14280 breakpoints. */
0d381245
VP
14281 {
14282 struct bp_location *e = existing_locations;
776592bf
DE
14283 /* If there are multiple breakpoints with the same function name,
14284 e.g. for inline functions, comparing function names won't work.
14285 Instead compare pc addresses; this is just a heuristic as things
14286 may have moved, but in practice it gives the correct answer
14287 often enough until a better solution is found. */
14288 int have_ambiguous_names = ambiguous_names_p (b->loc);
14289
0d381245
VP
14290 for (; e; e = e->next)
14291 {
14292 if (!e->enabled && e->function_name)
14293 {
14294 struct bp_location *l = b->loc;
776592bf
DE
14295 if (have_ambiguous_names)
14296 {
14297 for (; l; l = l->next)
f1310107 14298 if (breakpoint_locations_match (e, l))
776592bf
DE
14299 {
14300 l->enabled = 0;
14301 break;
14302 }
14303 }
14304 else
14305 {
14306 for (; l; l = l->next)
14307 if (l->function_name
14308 && strcmp (e->function_name, l->function_name) == 0)
14309 {
14310 l->enabled = 0;
14311 break;
14312 }
14313 }
0d381245
VP
14314 }
14315 }
14316 }
fe3f5fa8 14317
8d3788bd
VP
14318 if (!locations_are_equal (existing_locations, b->loc))
14319 observer_notify_breakpoint_modified (b);
14320
44702360 14321 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14322}
14323
f00aae0f 14324/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
14325 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14326
14327static struct symtabs_and_lines
f00aae0f
KS
14328location_to_sals (struct breakpoint *b, struct event_location *location,
14329 int *found)
ef23e705 14330{
02d20e4a 14331 struct symtabs_and_lines sals = {0};
492d29ea 14332 struct gdb_exception exception = exception_none;
ef23e705 14333
983af33b 14334 gdb_assert (b->ops != NULL);
ef23e705 14335
492d29ea 14336 TRY
ef23e705 14337 {
f00aae0f 14338 b->ops->decode_location (b, location, &sals);
ef23e705 14339 }
492d29ea 14340 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14341 {
14342 int not_found_and_ok = 0;
492d29ea
PA
14343
14344 exception = e;
14345
ef23e705
TJB
14346 /* For pending breakpoints, it's expected that parsing will
14347 fail until the right shared library is loaded. User has
14348 already told to create pending breakpoints and don't need
14349 extra messages. If breakpoint is in bp_shlib_disabled
14350 state, then user already saw the message about that
14351 breakpoint being disabled, and don't want to see more
14352 errors. */
58438ac1 14353 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14354 && (b->condition_not_parsed
14355 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14356 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14357 || b->enable_state == bp_disabled))
14358 not_found_and_ok = 1;
14359
14360 if (!not_found_and_ok)
14361 {
14362 /* We surely don't want to warn about the same breakpoint
14363 10 times. One solution, implemented here, is disable
14364 the breakpoint on error. Another solution would be to
14365 have separate 'warning emitted' flag. Since this
14366 happens only when a binary has changed, I don't know
14367 which approach is better. */
14368 b->enable_state = bp_disabled;
14369 throw_exception (e);
14370 }
14371 }
492d29ea 14372 END_CATCH
ef23e705 14373
492d29ea 14374 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14375 {
f8eba3c6 14376 int i;
ef23e705 14377
f8eba3c6
TT
14378 for (i = 0; i < sals.nelts; ++i)
14379 resolve_sal_pc (&sals.sals[i]);
f00aae0f 14380 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14381 {
ed1d1739
KS
14382 char *cond_string, *extra_string;
14383 int thread, task;
ef23e705 14384
f00aae0f 14385 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
e7e0cddf
SS
14386 &cond_string, &thread, &task,
14387 &extra_string);
f00aae0f 14388 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14389 if (cond_string)
14390 b->cond_string = cond_string;
14391 b->thread = thread;
14392 b->task = task;
e7e0cddf 14393 if (extra_string)
f00aae0f
KS
14394 {
14395 xfree (b->extra_string);
14396 b->extra_string = extra_string;
14397 }
ef23e705
TJB
14398 b->condition_not_parsed = 0;
14399 }
14400
983af33b 14401 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14402 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14403
58438ac1
TT
14404 *found = 1;
14405 }
14406 else
14407 *found = 0;
ef23e705
TJB
14408
14409 return sals;
14410}
14411
348d480f
PA
14412/* The default re_set method, for typical hardware or software
14413 breakpoints. Reevaluate the breakpoint and recreate its
14414 locations. */
14415
14416static void
28010a5d 14417breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14418{
14419 int found;
f1310107 14420 struct symtabs_and_lines sals, sals_end;
ef23e705 14421 struct symtabs_and_lines expanded = {0};
f1310107 14422 struct symtabs_and_lines expanded_end = {0};
ef23e705 14423
f00aae0f 14424 sals = location_to_sals (b, b->location, &found);
ef23e705
TJB
14425 if (found)
14426 {
14427 make_cleanup (xfree, sals.sals);
f8eba3c6 14428 expanded = sals;
ef23e705
TJB
14429 }
14430
f00aae0f 14431 if (b->location_range_end != NULL)
f1310107 14432 {
f00aae0f 14433 sals_end = location_to_sals (b, b->location_range_end, &found);
f1310107
TJB
14434 if (found)
14435 {
14436 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14437 expanded_end = sals_end;
f1310107
TJB
14438 }
14439 }
14440
14441 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14442}
14443
983af33b
SDJ
14444/* Default method for creating SALs from an address string. It basically
14445 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14446
14447static void
f00aae0f
KS
14448create_sals_from_location_default (const struct event_location *location,
14449 struct linespec_result *canonical,
14450 enum bptype type_wanted)
983af33b 14451{
f00aae0f 14452 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14453}
14454
14455/* Call create_breakpoints_sal for the given arguments. This is the default
14456 function for the `create_breakpoints_sal' method of
14457 breakpoint_ops. */
14458
14459static void
14460create_breakpoints_sal_default (struct gdbarch *gdbarch,
14461 struct linespec_result *canonical,
983af33b 14462 char *cond_string,
e7e0cddf 14463 char *extra_string,
983af33b
SDJ
14464 enum bptype type_wanted,
14465 enum bpdisp disposition,
14466 int thread,
14467 int task, int ignore_count,
14468 const struct breakpoint_ops *ops,
14469 int from_tty, int enabled,
44f238bb 14470 int internal, unsigned flags)
983af33b
SDJ
14471{
14472 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14473 extra_string,
983af33b
SDJ
14474 type_wanted, disposition,
14475 thread, task, ignore_count, ops, from_tty,
44f238bb 14476 enabled, internal, flags);
983af33b
SDJ
14477}
14478
14479/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14480 default function for the `decode_location' method of breakpoint_ops. */
983af33b
SDJ
14481
14482static void
f00aae0f
KS
14483decode_location_default (struct breakpoint *b,
14484 const struct event_location *location,
983af33b
SDJ
14485 struct symtabs_and_lines *sals)
14486{
14487 struct linespec_result canonical;
14488
14489 init_linespec_result (&canonical);
f00aae0f 14490 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
983af33b
SDJ
14491 (struct symtab *) NULL, 0,
14492 &canonical, multiple_symbols_all,
14493 b->filter);
14494
14495 /* We should get 0 or 1 resulting SALs. */
14496 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14497
14498 if (VEC_length (linespec_sals, canonical.sals) > 0)
14499 {
14500 struct linespec_sals *lsal;
14501
14502 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14503 *sals = lsal->sals;
14504 /* Arrange it so the destructor does not free the
14505 contents. */
14506 lsal->sals.sals = NULL;
14507 }
14508
14509 destroy_linespec_result (&canonical);
14510}
14511
28010a5d
PA
14512/* Prepare the global context for a re-set of breakpoint B. */
14513
14514static struct cleanup *
14515prepare_re_set_context (struct breakpoint *b)
14516{
14517 struct cleanup *cleanups;
14518
14519 input_radix = b->input_radix;
14520 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14521 if (b->pspace != NULL)
14522 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14523 set_language (b->language);
14524
14525 return cleanups;
ef23e705
TJB
14526}
14527
c906108c
SS
14528/* Reset a breakpoint given it's struct breakpoint * BINT.
14529 The value we return ends up being the return value from catch_errors.
14530 Unused in this case. */
14531
14532static int
4efb68b1 14533breakpoint_re_set_one (void *bint)
c906108c 14534{
4a64f543 14535 /* Get past catch_errs. */
53a5351d 14536 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14537 struct cleanup *cleanups;
c906108c 14538
348d480f
PA
14539 cleanups = prepare_re_set_context (b);
14540 b->ops->re_set (b);
14541 do_cleanups (cleanups);
c906108c
SS
14542 return 0;
14543}
14544
69de3c6a 14545/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14546void
69de3c6a 14547breakpoint_re_set (void)
c906108c 14548{
35df4500 14549 struct breakpoint *b, *b_tmp;
c906108c
SS
14550 enum language save_language;
14551 int save_input_radix;
6c95b8df 14552 struct cleanup *old_chain;
c5aa993b 14553
c906108c
SS
14554 save_language = current_language->la_language;
14555 save_input_radix = input_radix;
6c95b8df
PA
14556 old_chain = save_current_program_space ();
14557
35df4500 14558 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14559 {
4a64f543 14560 /* Format possible error msg. */
fe3f5fa8 14561 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14562 b->number);
14563 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14564 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14565 do_cleanups (cleanups);
c5aa993b 14566 }
c906108c
SS
14567 set_language (save_language);
14568 input_radix = save_input_radix;
e62c965a 14569
0756c555 14570 jit_breakpoint_re_set ();
4efc6507 14571
6c95b8df
PA
14572 do_cleanups (old_chain);
14573
af02033e
PP
14574 create_overlay_event_breakpoint ();
14575 create_longjmp_master_breakpoint ();
14576 create_std_terminate_master_breakpoint ();
186c406b 14577 create_exception_master_breakpoint ();
c906108c
SS
14578}
14579\f
c906108c
SS
14580/* Reset the thread number of this breakpoint:
14581
14582 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14583 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14584void
fba45db2 14585breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14586{
14587 if (b->thread != -1)
14588 {
39f77062 14589 if (in_thread_list (inferior_ptid))
5d5658a1 14590 b->thread = ptid_to_global_thread_id (inferior_ptid);
6c95b8df
PA
14591
14592 /* We're being called after following a fork. The new fork is
14593 selected as current, and unless this was a vfork will have a
14594 different program space from the original thread. Reset that
14595 as well. */
14596 b->loc->pspace = current_program_space;
c906108c
SS
14597 }
14598}
14599
03ac34d5
MS
14600/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14601 If from_tty is nonzero, it prints a message to that effect,
14602 which ends with a period (no newline). */
14603
c906108c 14604void
fba45db2 14605set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14606{
52f0bd74 14607 struct breakpoint *b;
c906108c
SS
14608
14609 if (count < 0)
14610 count = 0;
14611
14612 ALL_BREAKPOINTS (b)
14613 if (b->number == bptnum)
c5aa993b 14614 {
d77f58be
SS
14615 if (is_tracepoint (b))
14616 {
14617 if (from_tty && count != 0)
14618 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14619 bptnum);
14620 return;
14621 }
14622
c5aa993b 14623 b->ignore_count = count;
221ea385
KS
14624 if (from_tty)
14625 {
14626 if (count == 0)
3e43a32a
MS
14627 printf_filtered (_("Will stop next time "
14628 "breakpoint %d is reached."),
221ea385
KS
14629 bptnum);
14630 else if (count == 1)
a3f17187 14631 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14632 bptnum);
14633 else
3e43a32a
MS
14634 printf_filtered (_("Will ignore next %d "
14635 "crossings of breakpoint %d."),
221ea385
KS
14636 count, bptnum);
14637 }
8d3788bd 14638 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14639 return;
14640 }
c906108c 14641
8a3fe4f8 14642 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14643}
14644
c906108c
SS
14645/* Command to set ignore-count of breakpoint N to COUNT. */
14646
14647static void
fba45db2 14648ignore_command (char *args, int from_tty)
c906108c
SS
14649{
14650 char *p = args;
52f0bd74 14651 int num;
c906108c
SS
14652
14653 if (p == 0)
e2e0b3e5 14654 error_no_arg (_("a breakpoint number"));
c5aa993b 14655
c906108c 14656 num = get_number (&p);
5c44784c 14657 if (num == 0)
8a3fe4f8 14658 error (_("bad breakpoint number: '%s'"), args);
c906108c 14659 if (*p == 0)
8a3fe4f8 14660 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14661
14662 set_ignore_count (num,
14663 longest_to_int (value_as_long (parse_and_eval (p))),
14664 from_tty);
221ea385
KS
14665 if (from_tty)
14666 printf_filtered ("\n");
c906108c
SS
14667}
14668\f
14669/* Call FUNCTION on each of the breakpoints
14670 whose numbers are given in ARGS. */
14671
14672static void
95a42b64
TT
14673map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14674 void *),
14675 void *data)
c906108c 14676{
52f0bd74
AC
14677 int num;
14678 struct breakpoint *b, *tmp;
11cf8741 14679 int match;
197f0a60 14680 struct get_number_or_range_state state;
c906108c 14681
b9d61307 14682 if (args == 0 || *args == '\0')
e2e0b3e5 14683 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14684
197f0a60
TT
14685 init_number_or_range (&state, args);
14686
14687 while (!state.finished)
c906108c 14688 {
e799154c 14689 const char *p = state.string;
197f0a60 14690
11cf8741 14691 match = 0;
c5aa993b 14692
197f0a60 14693 num = get_number_or_range (&state);
5c44784c 14694 if (num == 0)
c5aa993b 14695 {
8a3fe4f8 14696 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14697 }
14698 else
14699 {
14700 ALL_BREAKPOINTS_SAFE (b, tmp)
14701 if (b->number == num)
14702 {
11cf8741 14703 match = 1;
cdac0397 14704 function (b, data);
11cf8741 14705 break;
5c44784c 14706 }
11cf8741 14707 if (match == 0)
a3f17187 14708 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14709 }
c906108c
SS
14710 }
14711}
14712
0d381245
VP
14713static struct bp_location *
14714find_location_by_number (char *number)
14715{
14716 char *dot = strchr (number, '.');
14717 char *p1;
14718 int bp_num;
14719 int loc_num;
14720 struct breakpoint *b;
14721 struct bp_location *loc;
14722
14723 *dot = '\0';
14724
14725 p1 = number;
197f0a60 14726 bp_num = get_number (&p1);
0d381245
VP
14727 if (bp_num == 0)
14728 error (_("Bad breakpoint number '%s'"), number);
14729
14730 ALL_BREAKPOINTS (b)
14731 if (b->number == bp_num)
14732 {
14733 break;
14734 }
14735
14736 if (!b || b->number != bp_num)
14737 error (_("Bad breakpoint number '%s'"), number);
14738
14739 p1 = dot+1;
197f0a60 14740 loc_num = get_number (&p1);
0d381245
VP
14741 if (loc_num == 0)
14742 error (_("Bad breakpoint location number '%s'"), number);
14743
14744 --loc_num;
14745 loc = b->loc;
14746 for (;loc_num && loc; --loc_num, loc = loc->next)
14747 ;
14748 if (!loc)
14749 error (_("Bad breakpoint location number '%s'"), dot+1);
14750
14751 return loc;
14752}
14753
14754
1900040c
MS
14755/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14756 If from_tty is nonzero, it prints a message to that effect,
14757 which ends with a period (no newline). */
14758
c906108c 14759void
fba45db2 14760disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14761{
14762 /* Never disable a watchpoint scope breakpoint; we want to
14763 hit them when we leave scope so we can delete both the
14764 watchpoint and its scope breakpoint at that time. */
14765 if (bpt->type == bp_watchpoint_scope)
14766 return;
14767
b5de0fa7 14768 bpt->enable_state = bp_disabled;
c906108c 14769
b775012e
LM
14770 /* Mark breakpoint locations modified. */
14771 mark_breakpoint_modified (bpt);
14772
d248b706
KY
14773 if (target_supports_enable_disable_tracepoint ()
14774 && current_trace_status ()->running && is_tracepoint (bpt))
14775 {
14776 struct bp_location *location;
14777
14778 for (location = bpt->loc; location; location = location->next)
14779 target_disable_tracepoint (location);
14780 }
14781
44702360 14782 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14783
8d3788bd 14784 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14785}
14786
51be5b68
PA
14787/* A callback for iterate_over_related_breakpoints. */
14788
14789static void
14790do_disable_breakpoint (struct breakpoint *b, void *ignore)
14791{
14792 disable_breakpoint (b);
14793}
14794
95a42b64
TT
14795/* A callback for map_breakpoint_numbers that calls
14796 disable_breakpoint. */
14797
14798static void
14799do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14800{
51be5b68 14801 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14802}
14803
c906108c 14804static void
fba45db2 14805disable_command (char *args, int from_tty)
c906108c 14806{
c906108c 14807 if (args == 0)
46c6471b
PA
14808 {
14809 struct breakpoint *bpt;
14810
14811 ALL_BREAKPOINTS (bpt)
14812 if (user_breakpoint_p (bpt))
14813 disable_breakpoint (bpt);
14814 }
9eaabc75 14815 else
0d381245 14816 {
9eaabc75
MW
14817 char *num = extract_arg (&args);
14818
14819 while (num)
d248b706 14820 {
9eaabc75 14821 if (strchr (num, '.'))
b775012e 14822 {
9eaabc75
MW
14823 struct bp_location *loc = find_location_by_number (num);
14824
14825 if (loc)
14826 {
14827 if (loc->enabled)
14828 {
14829 loc->enabled = 0;
14830 mark_breakpoint_location_modified (loc);
14831 }
14832 if (target_supports_enable_disable_tracepoint ()
14833 && current_trace_status ()->running && loc->owner
14834 && is_tracepoint (loc->owner))
14835 target_disable_tracepoint (loc);
14836 }
44702360 14837 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14838 }
9eaabc75
MW
14839 else
14840 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14841 num = extract_arg (&args);
d248b706 14842 }
0d381245 14843 }
c906108c
SS
14844}
14845
14846static void
816338b5
SS
14847enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14848 int count)
c906108c 14849{
afe38095 14850 int target_resources_ok;
c906108c
SS
14851
14852 if (bpt->type == bp_hardware_breakpoint)
14853 {
14854 int i;
c5aa993b 14855 i = hw_breakpoint_used_count ();
53a5351d 14856 target_resources_ok =
d92524f1 14857 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14858 i + 1, 0);
c906108c 14859 if (target_resources_ok == 0)
8a3fe4f8 14860 error (_("No hardware breakpoint support in the target."));
c906108c 14861 else if (target_resources_ok < 0)
8a3fe4f8 14862 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14863 }
14864
cc60f2e3 14865 if (is_watchpoint (bpt))
c906108c 14866 {
d07205c2 14867 /* Initialize it just to avoid a GCC false warning. */
f486487f 14868 enum enable_state orig_enable_state = bp_disabled;
dde02812 14869
492d29ea 14870 TRY
c906108c 14871 {
3a5c3e22
PA
14872 struct watchpoint *w = (struct watchpoint *) bpt;
14873
1e718ff1
TJB
14874 orig_enable_state = bpt->enable_state;
14875 bpt->enable_state = bp_enabled;
3a5c3e22 14876 update_watchpoint (w, 1 /* reparse */);
c906108c 14877 }
492d29ea 14878 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14879 {
1e718ff1 14880 bpt->enable_state = orig_enable_state;
dde02812
ES
14881 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14882 bpt->number);
14883 return;
c5aa993b 14884 }
492d29ea 14885 END_CATCH
c906108c 14886 }
0101ce28 14887
b775012e
LM
14888 bpt->enable_state = bp_enabled;
14889
14890 /* Mark breakpoint locations modified. */
14891 mark_breakpoint_modified (bpt);
14892
d248b706
KY
14893 if (target_supports_enable_disable_tracepoint ()
14894 && current_trace_status ()->running && is_tracepoint (bpt))
14895 {
14896 struct bp_location *location;
14897
14898 for (location = bpt->loc; location; location = location->next)
14899 target_enable_tracepoint (location);
14900 }
14901
b4c291bb 14902 bpt->disposition = disposition;
816338b5 14903 bpt->enable_count = count;
44702360 14904 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14905
8d3788bd 14906 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14907}
14908
fe3f5fa8 14909
c906108c 14910void
fba45db2 14911enable_breakpoint (struct breakpoint *bpt)
c906108c 14912{
816338b5 14913 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14914}
14915
14916static void
14917do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14918{
14919 enable_breakpoint (bpt);
c906108c
SS
14920}
14921
95a42b64
TT
14922/* A callback for map_breakpoint_numbers that calls
14923 enable_breakpoint. */
14924
14925static void
14926do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14927{
51be5b68 14928 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14929}
14930
c906108c
SS
14931/* The enable command enables the specified breakpoints (or all defined
14932 breakpoints) so they once again become (or continue to be) effective
1272ad14 14933 in stopping the inferior. */
c906108c 14934
c906108c 14935static void
fba45db2 14936enable_command (char *args, int from_tty)
c906108c 14937{
c906108c 14938 if (args == 0)
46c6471b
PA
14939 {
14940 struct breakpoint *bpt;
14941
14942 ALL_BREAKPOINTS (bpt)
14943 if (user_breakpoint_p (bpt))
14944 enable_breakpoint (bpt);
14945 }
9eaabc75 14946 else
0d381245 14947 {
9eaabc75
MW
14948 char *num = extract_arg (&args);
14949
14950 while (num)
d248b706 14951 {
9eaabc75 14952 if (strchr (num, '.'))
b775012e 14953 {
9eaabc75
MW
14954 struct bp_location *loc = find_location_by_number (num);
14955
14956 if (loc)
14957 {
14958 if (!loc->enabled)
14959 {
14960 loc->enabled = 1;
14961 mark_breakpoint_location_modified (loc);
14962 }
14963 if (target_supports_enable_disable_tracepoint ()
14964 && current_trace_status ()->running && loc->owner
14965 && is_tracepoint (loc->owner))
14966 target_enable_tracepoint (loc);
14967 }
44702360 14968 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14969 }
9eaabc75
MW
14970 else
14971 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14972 num = extract_arg (&args);
d248b706 14973 }
0d381245 14974 }
c906108c
SS
14975}
14976
816338b5
SS
14977/* This struct packages up disposition data for application to multiple
14978 breakpoints. */
14979
14980struct disp_data
14981{
14982 enum bpdisp disp;
14983 int count;
14984};
14985
c906108c 14986static void
51be5b68
PA
14987do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14988{
816338b5 14989 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14990
816338b5 14991 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14992}
14993
14994static void
14995do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14996{
816338b5 14997 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14998
14999 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15000}
15001
c906108c 15002static void
fba45db2 15003enable_once_command (char *args, int from_tty)
c906108c 15004{
51be5b68 15005 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15006}
15007
816338b5
SS
15008static void
15009do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15010{
15011 struct disp_data disp = { disp_disable, *(int *) countptr };
15012
15013 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15014}
15015
15016static void
15017enable_count_command (char *args, int from_tty)
15018{
b9d61307
SM
15019 int count;
15020
15021 if (args == NULL)
15022 error_no_arg (_("hit count"));
15023
15024 count = get_number (&args);
816338b5
SS
15025
15026 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15027}
15028
c906108c 15029static void
51be5b68 15030do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15031{
816338b5 15032 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15033
15034 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15035}
15036
c906108c 15037static void
fba45db2 15038enable_delete_command (char *args, int from_tty)
c906108c 15039{
51be5b68 15040 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15041}
15042\f
fa8d40ab
JJ
15043static void
15044set_breakpoint_cmd (char *args, int from_tty)
15045{
15046}
15047
15048static void
15049show_breakpoint_cmd (char *args, int from_tty)
15050{
15051}
15052
1f3b5d1b
PP
15053/* Invalidate last known value of any hardware watchpoint if
15054 the memory which that value represents has been written to by
15055 GDB itself. */
15056
15057static void
8de0566d
YQ
15058invalidate_bp_value_on_memory_change (struct inferior *inferior,
15059 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15060 const bfd_byte *data)
15061{
15062 struct breakpoint *bp;
15063
15064 ALL_BREAKPOINTS (bp)
15065 if (bp->enable_state == bp_enabled
3a5c3e22 15066 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15067 {
3a5c3e22 15068 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15069
3a5c3e22
PA
15070 if (wp->val_valid && wp->val)
15071 {
15072 struct bp_location *loc;
15073
15074 for (loc = bp->loc; loc != NULL; loc = loc->next)
15075 if (loc->loc_type == bp_loc_hardware_watchpoint
15076 && loc->address + loc->length > addr
15077 && addr + len > loc->address)
15078 {
15079 value_free (wp->val);
15080 wp->val = NULL;
15081 wp->val_valid = 0;
15082 }
15083 }
1f3b5d1b
PP
15084 }
15085}
15086
8181d85f
DJ
15087/* Create and insert a breakpoint for software single step. */
15088
15089void
6c95b8df 15090insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15091 struct address_space *aspace,
15092 CORE_ADDR next_pc)
8181d85f 15093{
7c16b83e
PA
15094 struct thread_info *tp = inferior_thread ();
15095 struct symtab_and_line sal;
15096 CORE_ADDR pc = next_pc;
8181d85f 15097
34b7e8a6
PA
15098 if (tp->control.single_step_breakpoints == NULL)
15099 {
15100 tp->control.single_step_breakpoints
5d5658a1 15101 = new_single_step_breakpoint (tp->global_num, gdbarch);
34b7e8a6 15102 }
8181d85f 15103
7c16b83e
PA
15104 sal = find_pc_line (pc, 0);
15105 sal.pc = pc;
15106 sal.section = find_pc_overlay (pc);
15107 sal.explicit_pc = 1;
34b7e8a6 15108 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 15109
7c16b83e 15110 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
15111}
15112
34b7e8a6 15113/* See breakpoint.h. */
f02253f1
HZ
15114
15115int
7c16b83e
PA
15116breakpoint_has_location_inserted_here (struct breakpoint *bp,
15117 struct address_space *aspace,
15118 CORE_ADDR pc)
1aafd4da 15119{
7c16b83e 15120 struct bp_location *loc;
1aafd4da 15121
7c16b83e
PA
15122 for (loc = bp->loc; loc != NULL; loc = loc->next)
15123 if (loc->inserted
15124 && breakpoint_location_address_match (loc, aspace, pc))
15125 return 1;
1aafd4da 15126
7c16b83e 15127 return 0;
ef370185
JB
15128}
15129
15130/* Check whether a software single-step breakpoint is inserted at
15131 PC. */
15132
15133int
15134single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15135 CORE_ADDR pc)
15136{
34b7e8a6
PA
15137 struct breakpoint *bpt;
15138
15139 ALL_BREAKPOINTS (bpt)
15140 {
15141 if (bpt->type == bp_single_step
15142 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15143 return 1;
15144 }
15145 return 0;
1aafd4da
UW
15146}
15147
1042e4c0
SS
15148/* Tracepoint-specific operations. */
15149
15150/* Set tracepoint count to NUM. */
15151static void
15152set_tracepoint_count (int num)
15153{
15154 tracepoint_count = num;
4fa62494 15155 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15156}
15157
70221824 15158static void
1042e4c0
SS
15159trace_command (char *arg, int from_tty)
15160{
55aa24fb 15161 struct breakpoint_ops *ops;
f00aae0f
KS
15162 struct event_location *location;
15163 struct cleanup *back_to;
55aa24fb 15164
f00aae0f
KS
15165 location = string_to_event_location (&arg, current_language);
15166 back_to = make_cleanup_delete_event_location (location);
5b56227b
KS
15167 if (location != NULL
15168 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
15169 ops = &tracepoint_probe_breakpoint_ops;
15170 else
15171 ops = &tracepoint_breakpoint_ops;
15172
558a9d82 15173 create_breakpoint (get_current_arch (),
f00aae0f
KS
15174 location,
15175 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15176 0 /* tempflag */,
15177 bp_tracepoint /* type_wanted */,
15178 0 /* Ignore count */,
15179 pending_break_support,
15180 ops,
15181 from_tty,
15182 1 /* enabled */,
15183 0 /* internal */, 0);
f00aae0f 15184 do_cleanups (back_to);
1042e4c0
SS
15185}
15186
70221824 15187static void
7a697b8d
SS
15188ftrace_command (char *arg, int from_tty)
15189{
f00aae0f
KS
15190 struct event_location *location;
15191 struct cleanup *back_to;
15192
15193 location = string_to_event_location (&arg, current_language);
15194 back_to = make_cleanup_delete_event_location (location);
558a9d82 15195 create_breakpoint (get_current_arch (),
f00aae0f
KS
15196 location,
15197 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15198 0 /* tempflag */,
15199 bp_fast_tracepoint /* type_wanted */,
15200 0 /* Ignore count */,
15201 pending_break_support,
15202 &tracepoint_breakpoint_ops,
15203 from_tty,
15204 1 /* enabled */,
15205 0 /* internal */, 0);
f00aae0f 15206 do_cleanups (back_to);
0fb4aa4b
PA
15207}
15208
15209/* strace command implementation. Creates a static tracepoint. */
15210
70221824 15211static void
0fb4aa4b
PA
15212strace_command (char *arg, int from_tty)
15213{
983af33b 15214 struct breakpoint_ops *ops;
f00aae0f
KS
15215 struct event_location *location;
15216 struct cleanup *back_to;
983af33b
SDJ
15217
15218 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15219 or with a normal static tracepoint. */
61012eef 15220 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
15221 {
15222 ops = &strace_marker_breakpoint_ops;
15223 location = new_linespec_location (&arg);
15224 }
983af33b 15225 else
f00aae0f
KS
15226 {
15227 ops = &tracepoint_breakpoint_ops;
15228 location = string_to_event_location (&arg, current_language);
15229 }
983af33b 15230
f00aae0f 15231 back_to = make_cleanup_delete_event_location (location);
558a9d82 15232 create_breakpoint (get_current_arch (),
f00aae0f
KS
15233 location,
15234 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15235 0 /* tempflag */,
15236 bp_static_tracepoint /* type_wanted */,
15237 0 /* Ignore count */,
15238 pending_break_support,
15239 ops,
15240 from_tty,
15241 1 /* enabled */,
15242 0 /* internal */, 0);
f00aae0f 15243 do_cleanups (back_to);
7a697b8d
SS
15244}
15245
409873ef
SS
15246/* Set up a fake reader function that gets command lines from a linked
15247 list that was acquired during tracepoint uploading. */
15248
15249static struct uploaded_tp *this_utp;
3149d8c1 15250static int next_cmd;
409873ef
SS
15251
15252static char *
15253read_uploaded_action (void)
15254{
15255 char *rslt;
15256
3149d8c1 15257 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15258
3149d8c1 15259 next_cmd++;
409873ef
SS
15260
15261 return rslt;
15262}
15263
00bf0b85
SS
15264/* Given information about a tracepoint as recorded on a target (which
15265 can be either a live system or a trace file), attempt to create an
15266 equivalent GDB tracepoint. This is not a reliable process, since
15267 the target does not necessarily have all the information used when
15268 the tracepoint was originally defined. */
15269
d9b3f62e 15270struct tracepoint *
00bf0b85 15271create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15272{
409873ef 15273 char *addr_str, small_buf[100];
d9b3f62e 15274 struct tracepoint *tp;
f00aae0f
KS
15275 struct event_location *location;
15276 struct cleanup *cleanup;
fd9b8c24 15277
409873ef
SS
15278 if (utp->at_string)
15279 addr_str = utp->at_string;
15280 else
15281 {
15282 /* In the absence of a source location, fall back to raw
15283 address. Since there is no way to confirm that the address
15284 means the same thing as when the trace was started, warn the
15285 user. */
3e43a32a
MS
15286 warning (_("Uploaded tracepoint %d has no "
15287 "source location, using raw address"),
409873ef 15288 utp->number);
8c042590 15289 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15290 addr_str = small_buf;
15291 }
15292
15293 /* There's not much we can do with a sequence of bytecodes. */
15294 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15295 warning (_("Uploaded tracepoint %d condition "
15296 "has no source form, ignoring it"),
409873ef 15297 utp->number);
d5551862 15298
f00aae0f
KS
15299 location = string_to_event_location (&addr_str, current_language);
15300 cleanup = make_cleanup_delete_event_location (location);
8cdf0e15 15301 if (!create_breakpoint (get_current_arch (),
f00aae0f
KS
15302 location,
15303 utp->cond_string, -1, addr_str,
e7e0cddf 15304 0 /* parse cond/thread */,
8cdf0e15 15305 0 /* tempflag */,
0fb4aa4b 15306 utp->type /* type_wanted */,
8cdf0e15
VP
15307 0 /* Ignore count */,
15308 pending_break_support,
348d480f 15309 &tracepoint_breakpoint_ops,
8cdf0e15 15310 0 /* from_tty */,
84f4c1fe 15311 utp->enabled /* enabled */,
44f238bb
PA
15312 0 /* internal */,
15313 CREATE_BREAKPOINT_FLAGS_INSERTED))
f00aae0f
KS
15314 {
15315 do_cleanups (cleanup);
15316 return NULL;
15317 }
15318
15319 do_cleanups (cleanup);
fd9b8c24 15320
409873ef 15321 /* Get the tracepoint we just created. */
fd9b8c24
PA
15322 tp = get_tracepoint (tracepoint_count);
15323 gdb_assert (tp != NULL);
d5551862 15324
00bf0b85
SS
15325 if (utp->pass > 0)
15326 {
8c042590
PM
15327 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15328 tp->base.number);
00bf0b85 15329
409873ef 15330 trace_pass_command (small_buf, 0);
00bf0b85
SS
15331 }
15332
409873ef
SS
15333 /* If we have uploaded versions of the original commands, set up a
15334 special-purpose "reader" function and call the usual command line
15335 reader, then pass the result to the breakpoint command-setting
15336 function. */
3149d8c1 15337 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15338 {
409873ef 15339 struct command_line *cmd_list;
00bf0b85 15340
409873ef 15341 this_utp = utp;
3149d8c1 15342 next_cmd = 0;
d5551862 15343
409873ef
SS
15344 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15345
d9b3f62e 15346 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15347 }
3149d8c1
SS
15348 else if (!VEC_empty (char_ptr, utp->actions)
15349 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15350 warning (_("Uploaded tracepoint %d actions "
15351 "have no source form, ignoring them"),
409873ef 15352 utp->number);
00bf0b85 15353
f196051f
SS
15354 /* Copy any status information that might be available. */
15355 tp->base.hit_count = utp->hit_count;
15356 tp->traceframe_usage = utp->traceframe_usage;
15357
00bf0b85 15358 return tp;
d9b3f62e 15359}
00bf0b85 15360
1042e4c0
SS
15361/* Print information on tracepoint number TPNUM_EXP, or all if
15362 omitted. */
15363
15364static void
e5a67952 15365tracepoints_info (char *args, int from_tty)
1042e4c0 15366{
79a45e25 15367 struct ui_out *uiout = current_uiout;
e5a67952 15368 int num_printed;
1042e4c0 15369
e5a67952 15370 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15371
15372 if (num_printed == 0)
1042e4c0 15373 {
e5a67952 15374 if (args == NULL || *args == '\0')
d77f58be
SS
15375 ui_out_message (uiout, 0, "No tracepoints.\n");
15376 else
e5a67952 15377 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15378 }
ad443146
SS
15379
15380 default_collect_info ();
1042e4c0
SS
15381}
15382
4a64f543 15383/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15384 Not supported by all targets. */
15385static void
15386enable_trace_command (char *args, int from_tty)
15387{
15388 enable_command (args, from_tty);
15389}
15390
4a64f543 15391/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15392 Not supported by all targets. */
15393static void
15394disable_trace_command (char *args, int from_tty)
15395{
15396 disable_command (args, from_tty);
15397}
15398
4a64f543 15399/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15400static void
15401delete_trace_command (char *arg, int from_tty)
15402{
35df4500 15403 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15404
15405 dont_repeat ();
15406
15407 if (arg == 0)
15408 {
15409 int breaks_to_delete = 0;
15410
15411 /* Delete all breakpoints if no argument.
15412 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15413 have to be deleted with an explicit breakpoint number
15414 argument. */
1042e4c0 15415 ALL_TRACEPOINTS (b)
46c6471b 15416 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15417 {
15418 breaks_to_delete = 1;
15419 break;
15420 }
1042e4c0
SS
15421
15422 /* Ask user only if there are some breakpoints to delete. */
15423 if (!from_tty
15424 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15425 {
35df4500 15426 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15427 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15428 delete_breakpoint (b);
1042e4c0
SS
15429 }
15430 }
15431 else
51be5b68 15432 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15433}
15434
197f0a60
TT
15435/* Helper function for trace_pass_command. */
15436
15437static void
d9b3f62e 15438trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15439{
d9b3f62e 15440 tp->pass_count = count;
6f6484cd 15441 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15442 if (from_tty)
15443 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15444 tp->base.number, count);
197f0a60
TT
15445}
15446
1042e4c0
SS
15447/* Set passcount for tracepoint.
15448
15449 First command argument is passcount, second is tracepoint number.
15450 If tracepoint number omitted, apply to most recently defined.
15451 Also accepts special argument "all". */
15452
15453static void
15454trace_pass_command (char *args, int from_tty)
15455{
d9b3f62e 15456 struct tracepoint *t1;
1042e4c0 15457 unsigned int count;
1042e4c0
SS
15458
15459 if (args == 0 || *args == 0)
3e43a32a
MS
15460 error (_("passcount command requires an "
15461 "argument (count + optional TP num)"));
1042e4c0 15462
4a64f543 15463 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15464
529480d0 15465 args = skip_spaces (args);
1042e4c0
SS
15466 if (*args && strncasecmp (args, "all", 3) == 0)
15467 {
d9b3f62e
PA
15468 struct breakpoint *b;
15469
1042e4c0 15470 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15471 if (*args)
15472 error (_("Junk at end of arguments."));
1042e4c0 15473
d9b3f62e 15474 ALL_TRACEPOINTS (b)
197f0a60 15475 {
d9b3f62e 15476 t1 = (struct tracepoint *) b;
197f0a60
TT
15477 trace_pass_set_count (t1, count, from_tty);
15478 }
15479 }
15480 else if (*args == '\0')
1042e4c0 15481 {
5fa1d40e 15482 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15483 if (t1)
197f0a60
TT
15484 trace_pass_set_count (t1, count, from_tty);
15485 }
15486 else
15487 {
15488 struct get_number_or_range_state state;
15489
15490 init_number_or_range (&state, args);
15491 while (!state.finished)
1042e4c0 15492 {
5fa1d40e 15493 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15494 if (t1)
15495 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15496 }
15497 }
1042e4c0
SS
15498}
15499
d9b3f62e 15500struct tracepoint *
1042e4c0
SS
15501get_tracepoint (int num)
15502{
15503 struct breakpoint *t;
15504
15505 ALL_TRACEPOINTS (t)
15506 if (t->number == num)
d9b3f62e 15507 return (struct tracepoint *) t;
1042e4c0
SS
15508
15509 return NULL;
15510}
15511
d5551862
SS
15512/* Find the tracepoint with the given target-side number (which may be
15513 different from the tracepoint number after disconnecting and
15514 reconnecting). */
15515
d9b3f62e 15516struct tracepoint *
d5551862
SS
15517get_tracepoint_by_number_on_target (int num)
15518{
d9b3f62e 15519 struct breakpoint *b;
d5551862 15520
d9b3f62e
PA
15521 ALL_TRACEPOINTS (b)
15522 {
15523 struct tracepoint *t = (struct tracepoint *) b;
15524
15525 if (t->number_on_target == num)
15526 return t;
15527 }
d5551862
SS
15528
15529 return NULL;
15530}
15531
1042e4c0 15532/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15533 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15534 If the argument is missing, the most recent tracepoint
15535 (tracepoint_count) is returned. */
15536
d9b3f62e 15537struct tracepoint *
197f0a60 15538get_tracepoint_by_number (char **arg,
5fa1d40e 15539 struct get_number_or_range_state *state)
1042e4c0 15540{
1042e4c0
SS
15541 struct breakpoint *t;
15542 int tpnum;
15543 char *instring = arg == NULL ? NULL : *arg;
15544
197f0a60
TT
15545 if (state)
15546 {
15547 gdb_assert (!state->finished);
15548 tpnum = get_number_or_range (state);
15549 }
15550 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15551 tpnum = tracepoint_count;
1042e4c0 15552 else
197f0a60 15553 tpnum = get_number (arg);
1042e4c0
SS
15554
15555 if (tpnum <= 0)
15556 {
15557 if (instring && *instring)
15558 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15559 instring);
15560 else
5fa1d40e 15561 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15562 return NULL;
15563 }
15564
15565 ALL_TRACEPOINTS (t)
15566 if (t->number == tpnum)
15567 {
d9b3f62e 15568 return (struct tracepoint *) t;
1042e4c0
SS
15569 }
15570
1042e4c0
SS
15571 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15572 return NULL;
15573}
15574
d9b3f62e
PA
15575void
15576print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15577{
15578 if (b->thread != -1)
15579 fprintf_unfiltered (fp, " thread %d", b->thread);
15580
15581 if (b->task != 0)
15582 fprintf_unfiltered (fp, " task %d", b->task);
15583
15584 fprintf_unfiltered (fp, "\n");
15585}
15586
6149aea9
PA
15587/* Save information on user settable breakpoints (watchpoints, etc) to
15588 a new script file named FILENAME. If FILTER is non-NULL, call it
15589 on each breakpoint and only include the ones for which it returns
15590 non-zero. */
15591
1042e4c0 15592static void
6149aea9
PA
15593save_breakpoints (char *filename, int from_tty,
15594 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15595{
15596 struct breakpoint *tp;
6149aea9 15597 int any = 0;
1042e4c0 15598 struct cleanup *cleanup;
a7bdde9e 15599 struct ui_file *fp;
6149aea9 15600 int extra_trace_bits = 0;
1042e4c0 15601
6149aea9
PA
15602 if (filename == 0 || *filename == 0)
15603 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15604
15605 /* See if we have anything to save. */
6149aea9 15606 ALL_BREAKPOINTS (tp)
1042e4c0 15607 {
6149aea9 15608 /* Skip internal and momentary breakpoints. */
09d682a4 15609 if (!user_breakpoint_p (tp))
6149aea9
PA
15610 continue;
15611
15612 /* If we have a filter, only save the breakpoints it accepts. */
15613 if (filter && !filter (tp))
15614 continue;
15615
15616 any = 1;
15617
15618 if (is_tracepoint (tp))
15619 {
15620 extra_trace_bits = 1;
15621
15622 /* We can stop searching. */
15623 break;
15624 }
1042e4c0 15625 }
6149aea9
PA
15626
15627 if (!any)
1042e4c0 15628 {
6149aea9 15629 warning (_("Nothing to save."));
1042e4c0
SS
15630 return;
15631 }
15632
c718be47
PA
15633 filename = tilde_expand (filename);
15634 cleanup = make_cleanup (xfree, filename);
15635 fp = gdb_fopen (filename, "w");
059fb39f 15636 if (!fp)
6149aea9
PA
15637 error (_("Unable to open file '%s' for saving (%s)"),
15638 filename, safe_strerror (errno));
a7bdde9e 15639 make_cleanup_ui_file_delete (fp);
8bf6485c 15640
6149aea9
PA
15641 if (extra_trace_bits)
15642 save_trace_state_variables (fp);
8bf6485c 15643
6149aea9 15644 ALL_BREAKPOINTS (tp)
1042e4c0 15645 {
6149aea9 15646 /* Skip internal and momentary breakpoints. */
09d682a4 15647 if (!user_breakpoint_p (tp))
6149aea9 15648 continue;
8bf6485c 15649
6149aea9
PA
15650 /* If we have a filter, only save the breakpoints it accepts. */
15651 if (filter && !filter (tp))
15652 continue;
15653
348d480f 15654 tp->ops->print_recreate (tp, fp);
1042e4c0 15655
6149aea9
PA
15656 /* Note, we can't rely on tp->number for anything, as we can't
15657 assume the recreated breakpoint numbers will match. Use $bpnum
15658 instead. */
15659
15660 if (tp->cond_string)
15661 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15662
15663 if (tp->ignore_count)
15664 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15665
2d9442cc 15666 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15667 {
6c63c96a 15668 struct gdb_exception exception;
a7bdde9e 15669
6149aea9 15670 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15671
79a45e25 15672 ui_out_redirect (current_uiout, fp);
492d29ea 15673 TRY
1042e4c0 15674 {
79a45e25 15675 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15676 }
492d29ea
PA
15677 CATCH (ex, RETURN_MASK_ALL)
15678 {
6c63c96a 15679 ui_out_redirect (current_uiout, NULL);
492d29ea
PA
15680 throw_exception (ex);
15681 }
15682 END_CATCH
1042e4c0 15683
6c63c96a 15684 ui_out_redirect (current_uiout, NULL);
a7bdde9e 15685 fprintf_unfiltered (fp, " end\n");
1042e4c0 15686 }
6149aea9
PA
15687
15688 if (tp->enable_state == bp_disabled)
99894e11 15689 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15690
15691 /* If this is a multi-location breakpoint, check if the locations
15692 should be individually disabled. Watchpoint locations are
15693 special, and not user visible. */
15694 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15695 {
15696 struct bp_location *loc;
15697 int n = 1;
15698
15699 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15700 if (!loc->enabled)
15701 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15702 }
1042e4c0 15703 }
8bf6485c 15704
6149aea9 15705 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15706 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15707
1042e4c0 15708 if (from_tty)
6149aea9 15709 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15710 do_cleanups (cleanup);
6149aea9
PA
15711}
15712
15713/* The `save breakpoints' command. */
15714
15715static void
15716save_breakpoints_command (char *args, int from_tty)
15717{
15718 save_breakpoints (args, from_tty, NULL);
15719}
15720
15721/* The `save tracepoints' command. */
15722
15723static void
15724save_tracepoints_command (char *args, int from_tty)
15725{
15726 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15727}
15728
15729/* Create a vector of all tracepoints. */
15730
15731VEC(breakpoint_p) *
eeae04df 15732all_tracepoints (void)
1042e4c0
SS
15733{
15734 VEC(breakpoint_p) *tp_vec = 0;
15735 struct breakpoint *tp;
15736
15737 ALL_TRACEPOINTS (tp)
15738 {
15739 VEC_safe_push (breakpoint_p, tp_vec, tp);
15740 }
15741
15742 return tp_vec;
15743}
15744
c906108c 15745\f
629500fa
KS
15746/* This help string is used to consolidate all the help string for specifying
15747 locations used by several commands. */
15748
15749#define LOCATION_HELP_STRING \
15750"Linespecs are colon-separated lists of location parameters, such as\n\
15751source filename, function name, label name, and line number.\n\
15752Example: To specify the start of a label named \"the_top\" in the\n\
15753function \"fact\" in the file \"factorial.c\", use\n\
15754\"factorial.c:fact:the_top\".\n\
15755\n\
15756Address locations begin with \"*\" and specify an exact address in the\n\
15757program. Example: To specify the fourth byte past the start function\n\
15758\"main\", use \"*main + 4\".\n\
15759\n\
15760Explicit locations are similar to linespecs but use an option/argument\n\
15761syntax to specify location parameters.\n\
15762Example: To specify the start of the label named \"the_top\" in the\n\
15763function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15764-function fact -label the_top\".\n"
15765
4a64f543
MS
15766/* This help string is used for the break, hbreak, tbreak and thbreak
15767 commands. It is defined as a macro to prevent duplication.
15768 COMMAND should be a string constant containing the name of the
15769 command. */
629500fa 15770
31e2b00f 15771#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15772command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15773PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15774probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15775guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15776`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15777LOCATION may be a linespec, address, or explicit location as described\n\
15778below.\n\
15779\n\
dc10affe
PA
15780With no LOCATION, uses current execution address of the selected\n\
15781stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15782\n\
15783THREADNUM is the number from \"info threads\".\n\
15784CONDITION is a boolean expression.\n\
629500fa 15785\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15786Multiple breakpoints at one place are permitted, and useful if their\n\
15787conditions are different.\n\
31e2b00f
AS
15788\n\
15789Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15790
44feb3ce
TT
15791/* List of subcommands for "catch". */
15792static struct cmd_list_element *catch_cmdlist;
15793
15794/* List of subcommands for "tcatch". */
15795static struct cmd_list_element *tcatch_cmdlist;
15796
9ac4176b 15797void
44feb3ce 15798add_catch_command (char *name, char *docstring,
82ae6c8d 15799 cmd_sfunc_ftype *sfunc,
625e8578 15800 completer_ftype *completer,
44feb3ce
TT
15801 void *user_data_catch,
15802 void *user_data_tcatch)
15803{
15804 struct cmd_list_element *command;
15805
15806 command = add_cmd (name, class_breakpoint, NULL, docstring,
15807 &catch_cmdlist);
15808 set_cmd_sfunc (command, sfunc);
15809 set_cmd_context (command, user_data_catch);
a96d9b2e 15810 set_cmd_completer (command, completer);
44feb3ce
TT
15811
15812 command = add_cmd (name, class_breakpoint, NULL, docstring,
15813 &tcatch_cmdlist);
15814 set_cmd_sfunc (command, sfunc);
15815 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15816 set_cmd_completer (command, completer);
44feb3ce
TT
15817}
15818
6149aea9
PA
15819static void
15820save_command (char *arg, int from_tty)
15821{
3e43a32a
MS
15822 printf_unfiltered (_("\"save\" must be followed by "
15823 "the name of a save subcommand.\n"));
635c7e8a 15824 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15825}
15826
84f4c1fe
PM
15827struct breakpoint *
15828iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15829 void *data)
15830{
35df4500 15831 struct breakpoint *b, *b_tmp;
84f4c1fe 15832
35df4500 15833 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15834 {
15835 if ((*callback) (b, data))
15836 return b;
15837 }
15838
15839 return NULL;
15840}
15841
0574c78f
GB
15842/* Zero if any of the breakpoint's locations could be a location where
15843 functions have been inlined, nonzero otherwise. */
15844
15845static int
15846is_non_inline_function (struct breakpoint *b)
15847{
15848 /* The shared library event breakpoint is set on the address of a
15849 non-inline function. */
15850 if (b->type == bp_shlib_event)
15851 return 1;
15852
15853 return 0;
15854}
15855
15856/* Nonzero if the specified PC cannot be a location where functions
15857 have been inlined. */
15858
15859int
09ac7c10
TT
15860pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15861 const struct target_waitstatus *ws)
0574c78f
GB
15862{
15863 struct breakpoint *b;
15864 struct bp_location *bl;
15865
15866 ALL_BREAKPOINTS (b)
15867 {
15868 if (!is_non_inline_function (b))
15869 continue;
15870
15871 for (bl = b->loc; bl != NULL; bl = bl->next)
15872 {
15873 if (!bl->shlib_disabled
09ac7c10 15874 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15875 return 1;
15876 }
15877 }
15878
15879 return 0;
15880}
15881
2f202fde
JK
15882/* Remove any references to OBJFILE which is going to be freed. */
15883
15884void
15885breakpoint_free_objfile (struct objfile *objfile)
15886{
15887 struct bp_location **locp, *loc;
15888
15889 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15890 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15891 loc->symtab = NULL;
15892}
15893
2060206e
PA
15894void
15895initialize_breakpoint_ops (void)
15896{
15897 static int initialized = 0;
15898
15899 struct breakpoint_ops *ops;
15900
15901 if (initialized)
15902 return;
15903 initialized = 1;
15904
15905 /* The breakpoint_ops structure to be inherit by all kinds of
15906 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15907 internal and momentary breakpoints, etc.). */
15908 ops = &bkpt_base_breakpoint_ops;
15909 *ops = base_breakpoint_ops;
15910 ops->re_set = bkpt_re_set;
15911 ops->insert_location = bkpt_insert_location;
15912 ops->remove_location = bkpt_remove_location;
15913 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15914 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15915 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15916 ops->decode_location = bkpt_decode_location;
2060206e
PA
15917
15918 /* The breakpoint_ops structure to be used in regular breakpoints. */
15919 ops = &bkpt_breakpoint_ops;
15920 *ops = bkpt_base_breakpoint_ops;
15921 ops->re_set = bkpt_re_set;
15922 ops->resources_needed = bkpt_resources_needed;
15923 ops->print_it = bkpt_print_it;
15924 ops->print_mention = bkpt_print_mention;
15925 ops->print_recreate = bkpt_print_recreate;
15926
15927 /* Ranged breakpoints. */
15928 ops = &ranged_breakpoint_ops;
15929 *ops = bkpt_breakpoint_ops;
15930 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15931 ops->resources_needed = resources_needed_ranged_breakpoint;
15932 ops->print_it = print_it_ranged_breakpoint;
15933 ops->print_one = print_one_ranged_breakpoint;
15934 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15935 ops->print_mention = print_mention_ranged_breakpoint;
15936 ops->print_recreate = print_recreate_ranged_breakpoint;
15937
15938 /* Internal breakpoints. */
15939 ops = &internal_breakpoint_ops;
15940 *ops = bkpt_base_breakpoint_ops;
15941 ops->re_set = internal_bkpt_re_set;
15942 ops->check_status = internal_bkpt_check_status;
15943 ops->print_it = internal_bkpt_print_it;
15944 ops->print_mention = internal_bkpt_print_mention;
15945
15946 /* Momentary breakpoints. */
15947 ops = &momentary_breakpoint_ops;
15948 *ops = bkpt_base_breakpoint_ops;
15949 ops->re_set = momentary_bkpt_re_set;
15950 ops->check_status = momentary_bkpt_check_status;
15951 ops->print_it = momentary_bkpt_print_it;
15952 ops->print_mention = momentary_bkpt_print_mention;
15953
e2e4d78b
JK
15954 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15955 ops = &longjmp_breakpoint_ops;
15956 *ops = momentary_breakpoint_ops;
15957 ops->dtor = longjmp_bkpt_dtor;
15958
55aa24fb
SDJ
15959 /* Probe breakpoints. */
15960 ops = &bkpt_probe_breakpoint_ops;
15961 *ops = bkpt_breakpoint_ops;
15962 ops->insert_location = bkpt_probe_insert_location;
15963 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15964 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15965 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15966
2060206e
PA
15967 /* Watchpoints. */
15968 ops = &watchpoint_breakpoint_ops;
15969 *ops = base_breakpoint_ops;
3a5c3e22 15970 ops->dtor = dtor_watchpoint;
2060206e
PA
15971 ops->re_set = re_set_watchpoint;
15972 ops->insert_location = insert_watchpoint;
15973 ops->remove_location = remove_watchpoint;
15974 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15975 ops->check_status = check_status_watchpoint;
15976 ops->resources_needed = resources_needed_watchpoint;
15977 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15978 ops->print_it = print_it_watchpoint;
15979 ops->print_mention = print_mention_watchpoint;
15980 ops->print_recreate = print_recreate_watchpoint;
427cd150 15981 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15982
15983 /* Masked watchpoints. */
15984 ops = &masked_watchpoint_breakpoint_ops;
15985 *ops = watchpoint_breakpoint_ops;
15986 ops->insert_location = insert_masked_watchpoint;
15987 ops->remove_location = remove_masked_watchpoint;
15988 ops->resources_needed = resources_needed_masked_watchpoint;
15989 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15990 ops->print_it = print_it_masked_watchpoint;
15991 ops->print_one_detail = print_one_detail_masked_watchpoint;
15992 ops->print_mention = print_mention_masked_watchpoint;
15993 ops->print_recreate = print_recreate_masked_watchpoint;
15994
15995 /* Tracepoints. */
15996 ops = &tracepoint_breakpoint_ops;
15997 *ops = base_breakpoint_ops;
15998 ops->re_set = tracepoint_re_set;
15999 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16000 ops->print_one_detail = tracepoint_print_one_detail;
16001 ops->print_mention = tracepoint_print_mention;
16002 ops->print_recreate = tracepoint_print_recreate;
5f700d83 16003 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 16004 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 16005 ops->decode_location = tracepoint_decode_location;
983af33b 16006
55aa24fb
SDJ
16007 /* Probe tracepoints. */
16008 ops = &tracepoint_probe_breakpoint_ops;
16009 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
16010 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
16011 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 16012
983af33b
SDJ
16013 /* Static tracepoints with marker (`-m'). */
16014 ops = &strace_marker_breakpoint_ops;
16015 *ops = tracepoint_breakpoint_ops;
5f700d83 16016 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 16017 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 16018 ops->decode_location = strace_marker_decode_location;
2060206e
PA
16019
16020 /* Fork catchpoints. */
16021 ops = &catch_fork_breakpoint_ops;
16022 *ops = base_breakpoint_ops;
16023 ops->insert_location = insert_catch_fork;
16024 ops->remove_location = remove_catch_fork;
16025 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16026 ops->print_it = print_it_catch_fork;
16027 ops->print_one = print_one_catch_fork;
16028 ops->print_mention = print_mention_catch_fork;
16029 ops->print_recreate = print_recreate_catch_fork;
16030
16031 /* Vfork catchpoints. */
16032 ops = &catch_vfork_breakpoint_ops;
16033 *ops = base_breakpoint_ops;
16034 ops->insert_location = insert_catch_vfork;
16035 ops->remove_location = remove_catch_vfork;
16036 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16037 ops->print_it = print_it_catch_vfork;
16038 ops->print_one = print_one_catch_vfork;
16039 ops->print_mention = print_mention_catch_vfork;
16040 ops->print_recreate = print_recreate_catch_vfork;
16041
16042 /* Exec catchpoints. */
16043 ops = &catch_exec_breakpoint_ops;
16044 *ops = base_breakpoint_ops;
16045 ops->dtor = dtor_catch_exec;
16046 ops->insert_location = insert_catch_exec;
16047 ops->remove_location = remove_catch_exec;
16048 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16049 ops->print_it = print_it_catch_exec;
16050 ops->print_one = print_one_catch_exec;
16051 ops->print_mention = print_mention_catch_exec;
16052 ops->print_recreate = print_recreate_catch_exec;
16053
edcc5120
TT
16054 /* Solib-related catchpoints. */
16055 ops = &catch_solib_breakpoint_ops;
16056 *ops = base_breakpoint_ops;
16057 ops->dtor = dtor_catch_solib;
16058 ops->insert_location = insert_catch_solib;
16059 ops->remove_location = remove_catch_solib;
16060 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16061 ops->check_status = check_status_catch_solib;
16062 ops->print_it = print_it_catch_solib;
16063 ops->print_one = print_one_catch_solib;
16064 ops->print_mention = print_mention_catch_solib;
16065 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16066
16067 ops = &dprintf_breakpoint_ops;
16068 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16069 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16070 ops->resources_needed = bkpt_resources_needed;
16071 ops->print_it = bkpt_print_it;
16072 ops->print_mention = bkpt_print_mention;
2d9442cc 16073 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16074 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16075 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16076}
16077
8bfd80db
YQ
16078/* Chain containing all defined "enable breakpoint" subcommands. */
16079
16080static struct cmd_list_element *enablebreaklist = NULL;
16081
c906108c 16082void
fba45db2 16083_initialize_breakpoint (void)
c906108c
SS
16084{
16085 struct cmd_list_element *c;
16086
2060206e
PA
16087 initialize_breakpoint_ops ();
16088
84acb35a 16089 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16090 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 16091 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16092
55aa24fb
SDJ
16093 breakpoint_objfile_key
16094 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16095
c906108c
SS
16096 breakpoint_chain = 0;
16097 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16098 before a breakpoint is set. */
16099 breakpoint_count = 0;
16100
1042e4c0
SS
16101 tracepoint_count = 0;
16102
1bedd215
AC
16103 add_com ("ignore", class_breakpoint, ignore_command, _("\
16104Set ignore-count of breakpoint number N to COUNT.\n\
16105Usage is `ignore N COUNT'."));
c906108c 16106
1bedd215
AC
16107 add_com ("commands", class_breakpoint, commands_command, _("\
16108Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16109Give breakpoint number as argument after \"commands\".\n\
16110With no argument, the targeted breakpoint is the last one set.\n\
16111The commands themselves follow starting on the next line.\n\
16112Type a line containing \"end\" to indicate the end of them.\n\
16113Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16114then no output is printed when it is hit, except what the commands print."));
c906108c 16115
d55637df 16116 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16117Specify breakpoint number N to break only if COND is true.\n\
c906108c 16118Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16119expression to be evaluated whenever breakpoint N is reached."));
d55637df 16120 set_cmd_completer (c, condition_completer);
c906108c 16121
1bedd215 16122 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16123Set a temporary breakpoint.\n\
c906108c
SS
16124Like \"break\" except the breakpoint is only temporary,\n\
16125so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16126by using \"enable delete\" on the breakpoint number.\n\
16127\n"
16128BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16129 set_cmd_completer (c, location_completer);
c94fdfd0 16130
1bedd215 16131 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16132Set a hardware assisted breakpoint.\n\
c906108c 16133Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16134some target hardware may not have this support.\n\
16135\n"
16136BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16137 set_cmd_completer (c, location_completer);
c906108c 16138
1bedd215 16139 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16140Set a temporary hardware assisted breakpoint.\n\
c906108c 16141Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16142so it will be deleted when hit.\n\
16143\n"
16144BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16145 set_cmd_completer (c, location_completer);
c906108c 16146
1bedd215
AC
16147 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16148Enable some breakpoints.\n\
c906108c
SS
16149Give breakpoint numbers (separated by spaces) as arguments.\n\
16150With no subcommand, breakpoints are enabled until you command otherwise.\n\
16151This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16152With a subcommand you can enable temporarily."),
c906108c 16153 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
16154
16155 add_com_alias ("en", "enable", class_breakpoint, 1);
16156
84951ab5 16157 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16158Enable some breakpoints.\n\
c906108c
SS
16159Give breakpoint numbers (separated by spaces) as arguments.\n\
16160This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16161May be abbreviated to simply \"enable\".\n"),
c5aa993b 16162 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16163
1a966eab
AC
16164 add_cmd ("once", no_class, enable_once_command, _("\
16165Enable breakpoints for one hit. Give breakpoint numbers.\n\
16166If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16167 &enablebreaklist);
16168
1a966eab
AC
16169 add_cmd ("delete", no_class, enable_delete_command, _("\
16170Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16171If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16172 &enablebreaklist);
16173
816338b5
SS
16174 add_cmd ("count", no_class, enable_count_command, _("\
16175Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16176If a breakpoint is hit while enabled in this fashion,\n\
16177the count is decremented; when it reaches zero, the breakpoint is disabled."),
16178 &enablebreaklist);
16179
1a966eab
AC
16180 add_cmd ("delete", no_class, enable_delete_command, _("\
16181Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16182If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16183 &enablelist);
16184
1a966eab
AC
16185 add_cmd ("once", no_class, enable_once_command, _("\
16186Enable breakpoints for one hit. Give breakpoint numbers.\n\
16187If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16188 &enablelist);
16189
16190 add_cmd ("count", no_class, enable_count_command, _("\
16191Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16192If a breakpoint is hit while enabled in this fashion,\n\
16193the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16194 &enablelist);
16195
1bedd215
AC
16196 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16197Disable some breakpoints.\n\
c906108c
SS
16198Arguments are breakpoint numbers with spaces in between.\n\
16199To disable all breakpoints, give no argument.\n\
64b9b334 16200A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16201 &disablelist, "disable ", 1, &cmdlist);
16202 add_com_alias ("dis", "disable", class_breakpoint, 1);
16203 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 16204
1a966eab
AC
16205 add_cmd ("breakpoints", class_alias, disable_command, _("\
16206Disable some breakpoints.\n\
c906108c
SS
16207Arguments are breakpoint numbers with spaces in between.\n\
16208To disable all breakpoints, give no argument.\n\
64b9b334 16209A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16210This command may be abbreviated \"disable\"."),
c906108c
SS
16211 &disablelist);
16212
1bedd215
AC
16213 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16214Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16215Arguments are breakpoint numbers with spaces in between.\n\
16216To delete all breakpoints, give no argument.\n\
16217\n\
16218Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16219The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16220 &deletelist, "delete ", 1, &cmdlist);
16221 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16222 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16223
1a966eab
AC
16224 add_cmd ("breakpoints", class_alias, delete_command, _("\
16225Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16226Arguments are breakpoint numbers with spaces in between.\n\
16227To delete all breakpoints, give no argument.\n\
1a966eab 16228This command may be abbreviated \"delete\"."),
c906108c
SS
16229 &deletelist);
16230
1bedd215 16231 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
16232Clear breakpoint at specified location.\n\
16233Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
16234\n\
16235With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
16236is executing in.\n"
16237"\n" LOCATION_HELP_STRING "\n\
1bedd215 16238See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16239 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16240
1bedd215 16241 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 16242Set breakpoint at specified location.\n"
31e2b00f 16243BREAK_ARGS_HELP ("break")));
5ba2abeb 16244 set_cmd_completer (c, location_completer);
c94fdfd0 16245
c906108c
SS
16246 add_com_alias ("b", "break", class_run, 1);
16247 add_com_alias ("br", "break", class_run, 1);
16248 add_com_alias ("bre", "break", class_run, 1);
16249 add_com_alias ("brea", "break", class_run, 1);
16250
c906108c
SS
16251 if (dbx_commands)
16252 {
1bedd215
AC
16253 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16254Break in function/address or break at a line in the current file."),
c5aa993b
JM
16255 &stoplist, "stop ", 1, &cmdlist);
16256 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16257 _("Break in function or address."), &stoplist);
c5aa993b 16258 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16259 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16260 add_com ("status", class_info, breakpoints_info, _("\
16261Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16262The \"Type\" column indicates one of:\n\
16263\tbreakpoint - normal breakpoint\n\
16264\twatchpoint - watchpoint\n\
16265The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16266the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16267breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16268address and file/line number respectively.\n\
16269\n\
16270Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16271are set to the address of the last breakpoint listed unless the command\n\
16272is prefixed with \"server \".\n\n\
c906108c 16273Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16274breakpoint set."));
c906108c
SS
16275 }
16276
1bedd215 16277 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16278Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16279The \"Type\" column indicates one of:\n\
16280\tbreakpoint - normal breakpoint\n\
16281\twatchpoint - watchpoint\n\
16282The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16283the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16284breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16285address and file/line number respectively.\n\
16286\n\
16287Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16288are set to the address of the last breakpoint listed unless the command\n\
16289is prefixed with \"server \".\n\n\
c906108c 16290Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16291breakpoint set."));
c906108c 16292
6b04bdb7
MS
16293 add_info_alias ("b", "breakpoints", 1);
16294
1a966eab
AC
16295 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16296Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16297The \"Type\" column indicates one of:\n\
16298\tbreakpoint - normal breakpoint\n\
16299\twatchpoint - watchpoint\n\
16300\tlongjmp - internal breakpoint used to step through longjmp()\n\
16301\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16302\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16303\tfinish - internal breakpoint used by the \"finish\" command\n\
16304The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16305the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16306breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16307address and file/line number respectively.\n\
16308\n\
16309Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16310are set to the address of the last breakpoint listed unless the command\n\
16311is prefixed with \"server \".\n\n\
c906108c 16312Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16313breakpoint set."),
c906108c
SS
16314 &maintenanceinfolist);
16315
44feb3ce
TT
16316 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16317Set catchpoints to catch events."),
16318 &catch_cmdlist, "catch ",
16319 0/*allow-unknown*/, &cmdlist);
16320
16321 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16322Set temporary catchpoints to catch events."),
16323 &tcatch_cmdlist, "tcatch ",
16324 0/*allow-unknown*/, &cmdlist);
16325
44feb3ce
TT
16326 add_catch_command ("fork", _("Catch calls to fork."),
16327 catch_fork_command_1,
a96d9b2e 16328 NULL,
44feb3ce
TT
16329 (void *) (uintptr_t) catch_fork_permanent,
16330 (void *) (uintptr_t) catch_fork_temporary);
16331 add_catch_command ("vfork", _("Catch calls to vfork."),
16332 catch_fork_command_1,
a96d9b2e 16333 NULL,
44feb3ce
TT
16334 (void *) (uintptr_t) catch_vfork_permanent,
16335 (void *) (uintptr_t) catch_vfork_temporary);
16336 add_catch_command ("exec", _("Catch calls to exec."),
16337 catch_exec_command_1,
a96d9b2e
SDJ
16338 NULL,
16339 CATCH_PERMANENT,
16340 CATCH_TEMPORARY);
edcc5120
TT
16341 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16342Usage: catch load [REGEX]\n\
16343If REGEX is given, only stop for libraries matching the regular expression."),
16344 catch_load_command_1,
16345 NULL,
16346 CATCH_PERMANENT,
16347 CATCH_TEMPORARY);
16348 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16349Usage: catch unload [REGEX]\n\
16350If REGEX is given, only stop for libraries matching the regular expression."),
16351 catch_unload_command_1,
16352 NULL,
16353 CATCH_PERMANENT,
16354 CATCH_TEMPORARY);
c5aa993b 16355
1bedd215
AC
16356 c = add_com ("watch", class_breakpoint, watch_command, _("\
16357Set a watchpoint for an expression.\n\
06a64a0b 16358Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16359A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16360an expression changes.\n\
16361If -l or -location is given, this evaluates EXPRESSION and watches\n\
16362the memory to which it refers."));
65d12d83 16363 set_cmd_completer (c, expression_completer);
c906108c 16364
1bedd215
AC
16365 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16366Set a read watchpoint for an expression.\n\
06a64a0b 16367Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16368A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16369an expression is read.\n\
16370If -l or -location is given, this evaluates EXPRESSION and watches\n\
16371the memory to which it refers."));
65d12d83 16372 set_cmd_completer (c, expression_completer);
c906108c 16373
1bedd215
AC
16374 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16375Set a watchpoint for an expression.\n\
06a64a0b 16376Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16377A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16378an expression is either read or written.\n\
16379If -l or -location is given, this evaluates EXPRESSION and watches\n\
16380the memory to which it refers."));
65d12d83 16381 set_cmd_completer (c, expression_completer);
c906108c 16382
d77f58be 16383 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16384Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16385
920d2a44
AC
16386 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16387 respond to changes - contrary to the description. */
85c07804
AC
16388 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16389 &can_use_hw_watchpoints, _("\
16390Set debugger's willingness to use watchpoint hardware."), _("\
16391Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16392If zero, gdb will not use hardware for new watchpoints, even if\n\
16393such is available. (However, any hardware watchpoints that were\n\
16394created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16395hardware.)"),
16396 NULL,
920d2a44 16397 show_can_use_hw_watchpoints,
85c07804 16398 &setlist, &showlist);
c906108c
SS
16399
16400 can_use_hw_watchpoints = 1;
fa8d40ab 16401
1042e4c0
SS
16402 /* Tracepoint manipulation commands. */
16403
16404 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16405Set a tracepoint at specified location.\n\
1042e4c0
SS
16406\n"
16407BREAK_ARGS_HELP ("trace") "\n\
16408Do \"help tracepoints\" for info on other tracepoint commands."));
16409 set_cmd_completer (c, location_completer);
16410
16411 add_com_alias ("tp", "trace", class_alias, 0);
16412 add_com_alias ("tr", "trace", class_alias, 1);
16413 add_com_alias ("tra", "trace", class_alias, 1);
16414 add_com_alias ("trac", "trace", class_alias, 1);
16415
7a697b8d 16416 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16417Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16418\n"
16419BREAK_ARGS_HELP ("ftrace") "\n\
16420Do \"help tracepoints\" for info on other tracepoint commands."));
16421 set_cmd_completer (c, location_completer);
16422
0fb4aa4b 16423 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16424Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16425\n\
16426strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16427LOCATION may be a linespec, explicit, or address location (described below) \n\
16428or -m MARKER_ID.\n\n\
16429If a marker id is specified, probe the marker with that name. With\n\
16430no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16431Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16432This collects arbitrary user data passed in the probe point call to the\n\
16433tracing library. You can inspect it when analyzing the trace buffer,\n\
16434by printing the $_sdata variable like any other convenience variable.\n\
16435\n\
16436CONDITION is a boolean expression.\n\
629500fa 16437\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16438Multiple tracepoints at one place are permitted, and useful if their\n\
16439conditions are different.\n\
0fb4aa4b
PA
16440\n\
16441Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16442Do \"help tracepoints\" for info on other tracepoint commands."));
16443 set_cmd_completer (c, location_completer);
16444
1042e4c0 16445 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16446Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16447Convenience variable \"$tpnum\" contains the number of the\n\
16448last tracepoint set."));
16449
16450 add_info_alias ("tp", "tracepoints", 1);
16451
16452 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16453Delete specified tracepoints.\n\
16454Arguments are tracepoint numbers, separated by spaces.\n\
16455No argument means delete all tracepoints."),
16456 &deletelist);
7e20dfcd 16457 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16458
16459 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16460Disable specified tracepoints.\n\
16461Arguments are tracepoint numbers, separated by spaces.\n\
16462No argument means disable all tracepoints."),
16463 &disablelist);
16464 deprecate_cmd (c, "disable");
16465
16466 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16467Enable specified tracepoints.\n\
16468Arguments are tracepoint numbers, separated by spaces.\n\
16469No argument means enable all tracepoints."),
16470 &enablelist);
16471 deprecate_cmd (c, "enable");
16472
16473 add_com ("passcount", class_trace, trace_pass_command, _("\
16474Set the passcount for a tracepoint.\n\
16475The trace will end when the tracepoint has been passed 'count' times.\n\
16476Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16477if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16478
6149aea9
PA
16479 add_prefix_cmd ("save", class_breakpoint, save_command,
16480 _("Save breakpoint definitions as a script."),
16481 &save_cmdlist, "save ",
16482 0/*allow-unknown*/, &cmdlist);
16483
16484 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16485Save current breakpoint definitions as a script.\n\
cce7e648 16486This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16487catchpoints, tracepoints). Use the 'source' command in another debug\n\
16488session to restore them."),
16489 &save_cmdlist);
16490 set_cmd_completer (c, filename_completer);
16491
16492 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16493Save current tracepoint definitions as a script.\n\
6149aea9
PA
16494Use the 'source' command in another debug session to restore them."),
16495 &save_cmdlist);
1042e4c0
SS
16496 set_cmd_completer (c, filename_completer);
16497
6149aea9
PA
16498 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16499 deprecate_cmd (c, "save tracepoints");
16500
1bedd215 16501 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16502Breakpoint specific settings\n\
16503Configure various breakpoint-specific variables such as\n\
1bedd215 16504pending breakpoint behavior"),
fa8d40ab
JJ
16505 &breakpoint_set_cmdlist, "set breakpoint ",
16506 0/*allow-unknown*/, &setlist);
1bedd215 16507 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16508Breakpoint specific settings\n\
16509Configure various breakpoint-specific variables such as\n\
1bedd215 16510pending breakpoint behavior"),
fa8d40ab
JJ
16511 &breakpoint_show_cmdlist, "show breakpoint ",
16512 0/*allow-unknown*/, &showlist);
16513
7915a72c
AC
16514 add_setshow_auto_boolean_cmd ("pending", no_class,
16515 &pending_break_support, _("\
16516Set debugger's behavior regarding pending breakpoints."), _("\
16517Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16518If on, an unrecognized breakpoint location will cause gdb to create a\n\
16519pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16520an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16521user-query to see if a pending breakpoint should be created."),
2c5b56ce 16522 NULL,
920d2a44 16523 show_pending_break_support,
6e1d7d6c
AC
16524 &breakpoint_set_cmdlist,
16525 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16526
16527 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16528
16529 add_setshow_boolean_cmd ("auto-hw", no_class,
16530 &automatic_hardware_breakpoints, _("\
16531Set automatic usage of hardware breakpoints."), _("\
16532Show automatic usage of hardware breakpoints."), _("\
16533If set, the debugger will automatically use hardware breakpoints for\n\
16534breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16535a warning will be emitted for such breakpoints."),
16536 NULL,
16537 show_automatic_hardware_breakpoints,
16538 &breakpoint_set_cmdlist,
16539 &breakpoint_show_cmdlist);
74960c60 16540
a25a5a45
PA
16541 add_setshow_boolean_cmd ("always-inserted", class_support,
16542 &always_inserted_mode, _("\
74960c60
VP
16543Set mode for inserting breakpoints."), _("\
16544Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16545When this mode is on, breakpoints are inserted immediately as soon as\n\
16546they're created, kept inserted even when execution stops, and removed\n\
16547only when the user deletes them. When this mode is off (the default),\n\
16548breakpoints are inserted only when execution continues, and removed\n\
16549when execution stops."),
72d0e2c5
YQ
16550 NULL,
16551 &show_always_inserted_mode,
16552 &breakpoint_set_cmdlist,
16553 &breakpoint_show_cmdlist);
f1310107 16554
b775012e
LM
16555 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16556 condition_evaluation_enums,
16557 &condition_evaluation_mode_1, _("\
16558Set mode of breakpoint condition evaluation."), _("\
16559Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16560When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16561evaluated on the host's side by GDB. When it is set to \"target\",\n\
16562breakpoint conditions will be downloaded to the target (if the target\n\
16563supports such feature) and conditions will be evaluated on the target's side.\n\
16564If this is set to \"auto\" (default), this will be automatically set to\n\
16565\"target\" if it supports condition evaluation, otherwise it will\n\
16566be set to \"gdb\""),
16567 &set_condition_evaluation_mode,
16568 &show_condition_evaluation_mode,
16569 &breakpoint_set_cmdlist,
16570 &breakpoint_show_cmdlist);
16571
f1310107
TJB
16572 add_com ("break-range", class_breakpoint, break_range_command, _("\
16573Set a breakpoint for an address range.\n\
16574break-range START-LOCATION, END-LOCATION\n\
16575where START-LOCATION and END-LOCATION can be one of the following:\n\
16576 LINENUM, for that line in the current file,\n\
16577 FILE:LINENUM, for that line in that file,\n\
16578 +OFFSET, for that number of lines after the current line\n\
16579 or the start of the range\n\
16580 FUNCTION, for the first line in that function,\n\
16581 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16582 *ADDRESS, for the instruction at that address.\n\
16583\n\
16584The breakpoint will stop execution of the inferior whenever it executes\n\
16585an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16586range (including START-LOCATION and END-LOCATION)."));
16587
e7e0cddf 16588 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16589Set a dynamic printf at specified location.\n\
e7e0cddf 16590dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16591location may be a linespec, explicit, or address location.\n"
16592"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16593 set_cmd_completer (c, location_completer);
16594
16595 add_setshow_enum_cmd ("dprintf-style", class_support,
16596 dprintf_style_enums, &dprintf_style, _("\
16597Set the style of usage for dynamic printf."), _("\
16598Show the style of usage for dynamic printf."), _("\
16599This setting chooses how GDB will do a dynamic printf.\n\
16600If the value is \"gdb\", then the printing is done by GDB to its own\n\
16601console, as with the \"printf\" command.\n\
16602If the value is \"call\", the print is done by calling a function in your\n\
16603program; by default printf(), but you can choose a different function or\n\
16604output stream by setting dprintf-function and dprintf-channel."),
16605 update_dprintf_commands, NULL,
16606 &setlist, &showlist);
16607
16608 dprintf_function = xstrdup ("printf");
16609 add_setshow_string_cmd ("dprintf-function", class_support,
16610 &dprintf_function, _("\
16611Set the function to use for dynamic printf"), _("\
16612Show the function to use for dynamic printf"), NULL,
16613 update_dprintf_commands, NULL,
16614 &setlist, &showlist);
16615
16616 dprintf_channel = xstrdup ("");
16617 add_setshow_string_cmd ("dprintf-channel", class_support,
16618 &dprintf_channel, _("\
16619Set the channel to use for dynamic printf"), _("\
16620Show the channel to use for dynamic printf"), NULL,
16621 update_dprintf_commands, NULL,
16622 &setlist, &showlist);
16623
d3ce09f5
SS
16624 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16625 &disconnected_dprintf, _("\
16626Set whether dprintf continues after GDB disconnects."), _("\
16627Show whether dprintf continues after GDB disconnects."), _("\
16628Use this to let dprintf commands continue to hit and produce output\n\
16629even if GDB disconnects or detaches from the target."),
16630 NULL,
16631 NULL,
16632 &setlist, &showlist);
16633
16634 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16635agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16636(target agent only) This is useful for formatted output in user-defined commands."));
16637
765dc015 16638 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16639
16640 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16641 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16642}
This page took 2.869918 seconds and 4 git commands to generate.