Replace SYMBOLIC_BIND with SYMBOL_REFERENCES_LOCAL
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
32d0add0 3 Copyright (C) 1986-2015 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
a6d9a66e 21#include "arch-utils.h"
c906108c 22#include <ctype.h>
776592bf 23#include "hashtab.h"
c906108c
SS
24#include "symtab.h"
25#include "frame.h"
26#include "breakpoint.h"
1042e4c0 27#include "tracepoint.h"
c906108c
SS
28#include "gdbtypes.h"
29#include "expression.h"
30#include "gdbcore.h"
31#include "gdbcmd.h"
32#include "value.h"
33#include "command.h"
34#include "inferior.h"
45741a9c 35#include "infrun.h"
c906108c
SS
36#include "gdbthread.h"
37#include "target.h"
38#include "language.h"
50f182aa 39#include "gdb-demangle.h"
0ba1096a 40#include "filenames.h"
c906108c
SS
41#include "annotate.h"
42#include "symfile.h"
43#include "objfiles.h"
0378c332 44#include "source.h"
c5f0f3d0 45#include "linespec.h"
c94fdfd0 46#include "completer.h"
5b7f31a4 47#include "gdb.h"
8b93c638 48#include "ui-out.h"
e1507482 49#include "cli/cli-script.h"
fe898f56 50#include "block.h"
a77053c2 51#include "solib.h"
84acb35a
JJ
52#include "solist.h"
53#include "observer.h"
765dc015 54#include "memattr.h"
f7f9143b 55#include "ada-lang.h"
d1aa2f50 56#include "top.h"
79a45b7d 57#include "valprint.h"
4efc6507 58#include "jit.h"
65d79d4b 59#include "parser-defs.h"
55aa24fb
SDJ
60#include "gdb_regex.h"
61#include "probe.h"
e9cafbcc 62#include "cli/cli-utils.h"
be34f849 63#include "continuations.h"
1bfeeb0f
JL
64#include "stack.h"
65#include "skip.h"
b775012e 66#include "ax-gdb.h"
e2e4d78b 67#include "dummy-frame.h"
5589af0e 68#include "interps.h"
d3ce09f5
SS
69#include "format.h"
70
1042e4c0
SS
71/* readline include files */
72#include "readline/readline.h"
73#include "readline/history.h"
74
75/* readline defines this. */
76#undef savestring
77
034dad6f 78#include "mi/mi-common.h"
6dddc817 79#include "extension.h"
104c1213 80
e7e8980f
YQ
81/* Enums for exception-handling support. */
82enum exception_event_kind
83{
84 EX_EVENT_THROW,
591f19e8 85 EX_EVENT_RETHROW,
e7e8980f
YQ
86 EX_EVENT_CATCH
87};
88
4a64f543 89/* Prototypes for local functions. */
c906108c 90
a14ed312 91static void enable_delete_command (char *, int);
c906108c 92
a14ed312 93static void enable_once_command (char *, int);
c906108c 94
816338b5
SS
95static void enable_count_command (char *, int);
96
a14ed312 97static void disable_command (char *, int);
c906108c 98
a14ed312 99static void enable_command (char *, int);
c906108c 100
95a42b64
TT
101static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
102 void *),
103 void *);
c906108c 104
a14ed312 105static void ignore_command (char *, int);
c906108c 106
4efb68b1 107static int breakpoint_re_set_one (void *);
c906108c 108
348d480f
PA
109static void breakpoint_re_set_default (struct breakpoint *);
110
983af33b
SDJ
111static void create_sals_from_address_default (char **,
112 struct linespec_result *,
113 enum bptype, char *,
114 char **);
115
116static void create_breakpoints_sal_default (struct gdbarch *,
117 struct linespec_result *,
e7e0cddf 118 char *, char *, enum bptype,
983af33b
SDJ
119 enum bpdisp, int, int,
120 int,
121 const struct breakpoint_ops *,
44f238bb 122 int, int, int, unsigned);
983af33b
SDJ
123
124static void decode_linespec_default (struct breakpoint *, char **,
125 struct symtabs_and_lines *);
126
a14ed312 127static void clear_command (char *, int);
c906108c 128
a14ed312 129static void catch_command (char *, int);
c906108c 130
a9634178 131static int can_use_hardware_watchpoint (struct value *);
c906108c 132
98deb0da 133static void break_command_1 (char *, int, int);
c906108c 134
a14ed312 135static void mention (struct breakpoint *);
c906108c 136
348d480f
PA
137static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
138 enum bptype,
c0a91b2b 139 const struct breakpoint_ops *);
3742cc8b
YQ
140static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
141 const struct symtab_and_line *);
142
4a64f543
MS
143/* This function is used in gdbtk sources and thus can not be made
144 static. */
63c252f8 145struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 146 struct symtab_and_line,
c0a91b2b
TT
147 enum bptype,
148 const struct breakpoint_ops *);
c906108c 149
06edf0c0
PA
150static struct breakpoint *
151 momentary_breakpoint_from_master (struct breakpoint *orig,
152 enum bptype type,
a1aa2221
LM
153 const struct breakpoint_ops *ops,
154 int loc_enabled);
06edf0c0 155
76897487
KB
156static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
157
a6d9a66e
UW
158static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
159 CORE_ADDR bpaddr,
88f7da05 160 enum bptype bptype);
76897487 161
6c95b8df
PA
162static void describe_other_breakpoints (struct gdbarch *,
163 struct program_space *, CORE_ADDR,
5af949e3 164 struct obj_section *, int);
c906108c 165
85d721b8
PA
166static int watchpoint_locations_match (struct bp_location *loc1,
167 struct bp_location *loc2);
168
f1310107
TJB
169static int breakpoint_location_address_match (struct bp_location *bl,
170 struct address_space *aspace,
171 CORE_ADDR addr);
172
a14ed312 173static void breakpoints_info (char *, int);
c906108c 174
d77f58be
SS
175static void watchpoints_info (char *, int);
176
e5a67952
MS
177static int breakpoint_1 (char *, int,
178 int (*) (const struct breakpoint *));
c906108c 179
4efb68b1 180static int breakpoint_cond_eval (void *);
c906108c 181
4efb68b1 182static void cleanup_executing_breakpoints (void *);
c906108c 183
a14ed312 184static void commands_command (char *, int);
c906108c 185
a14ed312 186static void condition_command (char *, int);
c906108c 187
c5aa993b
JM
188typedef enum
189 {
190 mark_inserted,
191 mark_uninserted
192 }
193insertion_state_t;
c906108c 194
0bde7532 195static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 196static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 197
e514a9d6 198static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 199
4efb68b1 200static int watchpoint_check (void *);
c906108c 201
a14ed312 202static void maintenance_info_breakpoints (char *, int);
c906108c 203
a14ed312 204static int hw_breakpoint_used_count (void);
c906108c 205
a1398e0c
PA
206static int hw_watchpoint_use_count (struct breakpoint *);
207
208static int hw_watchpoint_used_count_others (struct breakpoint *except,
209 enum bptype type,
210 int *other_type_used);
c906108c 211
a14ed312 212static void hbreak_command (char *, int);
c906108c 213
a14ed312 214static void thbreak_command (char *, int);
c906108c 215
816338b5
SS
216static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
217 int count);
c906108c 218
a14ed312 219static void stop_command (char *arg, int from_tty);
7a292a7a 220
a14ed312 221static void stopin_command (char *arg, int from_tty);
7a292a7a 222
a14ed312 223static void stopat_command (char *arg, int from_tty);
7a292a7a 224
a14ed312 225static void tcatch_command (char *arg, int from_tty);
7a292a7a 226
fe3f5fa8 227static void free_bp_location (struct bp_location *loc);
f431efe5
PA
228static void incref_bp_location (struct bp_location *loc);
229static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 230
39d61571 231static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 232
44702360
PA
233/* update_global_location_list's modes of operation wrt to whether to
234 insert locations now. */
235enum ugll_insert_mode
236{
237 /* Don't insert any breakpoint locations into the inferior, only
238 remove already-inserted locations that no longer should be
239 inserted. Functions that delete a breakpoint or breakpoints
240 should specify this mode, so that deleting a breakpoint doesn't
241 have the side effect of inserting the locations of other
242 breakpoints that are marked not-inserted, but should_be_inserted
243 returns true on them.
244
245 This behavior is useful is situations close to tear-down -- e.g.,
246 after an exec, while the target still has execution, but
247 breakpoint shadows of the previous executable image should *NOT*
248 be restored to the new image; or before detaching, where the
249 target still has execution and wants to delete breakpoints from
250 GDB's lists, and all breakpoints had already been removed from
251 the inferior. */
252 UGLL_DONT_INSERT,
253
a25a5a45
PA
254 /* May insert breakpoints iff breakpoints_should_be_inserted_now
255 claims breakpoints should be inserted now. */
04086b45
PA
256 UGLL_MAY_INSERT,
257
a25a5a45
PA
258 /* Insert locations now, irrespective of
259 breakpoints_should_be_inserted_now. E.g., say all threads are
260 stopped right now, and the user did "continue". We need to
261 insert breakpoints _before_ resuming the target, but
262 UGLL_MAY_INSERT wouldn't insert them, because
263 breakpoints_should_be_inserted_now returns false at that point,
264 as no thread is running yet. */
04086b45 265 UGLL_INSERT
44702360
PA
266};
267
268static void update_global_location_list (enum ugll_insert_mode);
a5606eee 269
44702360 270static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 271
d77f58be 272static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
273
274static void insert_breakpoint_locations (void);
a5606eee 275
1042e4c0
SS
276static void tracepoints_info (char *, int);
277
278static void delete_trace_command (char *, int);
279
280static void enable_trace_command (char *, int);
281
282static void disable_trace_command (char *, int);
283
284static void trace_pass_command (char *, int);
285
558a9d82
YQ
286static void set_tracepoint_count (int num);
287
9c06b0b4
TJB
288static int is_masked_watchpoint (const struct breakpoint *b);
289
b775012e
LM
290static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
291
983af33b
SDJ
292/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
293 otherwise. */
294
295static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 296
2060206e
PA
297/* The breakpoint_ops structure to be inherited by all breakpoint_ops
298 that are implemented on top of software or hardware breakpoints
299 (user breakpoints, internal and momentary breakpoints, etc.). */
300static struct breakpoint_ops bkpt_base_breakpoint_ops;
301
302/* Internal breakpoints class type. */
06edf0c0 303static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
304
305/* Momentary breakpoints class type. */
06edf0c0
PA
306static struct breakpoint_ops momentary_breakpoint_ops;
307
e2e4d78b
JK
308/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
309static struct breakpoint_ops longjmp_breakpoint_ops;
310
2060206e
PA
311/* The breakpoint_ops structure to be used in regular user created
312 breakpoints. */
313struct breakpoint_ops bkpt_breakpoint_ops;
314
55aa24fb
SDJ
315/* Breakpoints set on probes. */
316static struct breakpoint_ops bkpt_probe_breakpoint_ops;
317
e7e0cddf 318/* Dynamic printf class type. */
c5867ab6 319struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 320
d3ce09f5
SS
321/* The style in which to perform a dynamic printf. This is a user
322 option because different output options have different tradeoffs;
323 if GDB does the printing, there is better error handling if there
324 is a problem with any of the arguments, but using an inferior
325 function lets you have special-purpose printers and sending of
326 output to the same place as compiled-in print functions. */
327
328static const char dprintf_style_gdb[] = "gdb";
329static const char dprintf_style_call[] = "call";
330static const char dprintf_style_agent[] = "agent";
331static const char *const dprintf_style_enums[] = {
332 dprintf_style_gdb,
333 dprintf_style_call,
334 dprintf_style_agent,
335 NULL
336};
337static const char *dprintf_style = dprintf_style_gdb;
338
339/* The function to use for dynamic printf if the preferred style is to
340 call into the inferior. The value is simply a string that is
341 copied into the command, so it can be anything that GDB can
342 evaluate to a callable address, not necessarily a function name. */
343
344static char *dprintf_function = "";
345
346/* The channel to use for dynamic printf if the preferred style is to
347 call into the inferior; if a nonempty string, it will be passed to
348 the call as the first argument, with the format string as the
349 second. As with the dprintf function, this can be anything that
350 GDB knows how to evaluate, so in addition to common choices like
351 "stderr", this could be an app-specific expression like
352 "mystreams[curlogger]". */
353
354static char *dprintf_channel = "";
355
356/* True if dprintf commands should continue to operate even if GDB
357 has disconnected. */
358static int disconnected_dprintf = 1;
359
5cea2a26
PA
360/* A reference-counted struct command_line. This lets multiple
361 breakpoints share a single command list. */
362struct counted_command_line
363{
364 /* The reference count. */
365 int refc;
366
367 /* The command list. */
368 struct command_line *commands;
369};
370
371struct command_line *
372breakpoint_commands (struct breakpoint *b)
373{
374 return b->commands ? b->commands->commands : NULL;
375}
3daf8fe5 376
f3b1572e
PA
377/* Flag indicating that a command has proceeded the inferior past the
378 current breakpoint. */
379
380static int breakpoint_proceeded;
381
956a9fb9 382const char *
2cec12e5
AR
383bpdisp_text (enum bpdisp disp)
384{
4a64f543
MS
385 /* NOTE: the following values are a part of MI protocol and
386 represent values of 'disp' field returned when inferior stops at
387 a breakpoint. */
bc043ef3 388 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 389
2cec12e5
AR
390 return bpdisps[(int) disp];
391}
c906108c 392
4a64f543 393/* Prototypes for exported functions. */
c906108c 394/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 395 if such is available. */
c906108c
SS
396static int can_use_hw_watchpoints;
397
920d2a44
AC
398static void
399show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
400 struct cmd_list_element *c,
401 const char *value)
402{
3e43a32a
MS
403 fprintf_filtered (file,
404 _("Debugger's willingness to use "
405 "watchpoint hardware is %s.\n"),
920d2a44
AC
406 value);
407}
408
fa8d40ab
JJ
409/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
410 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 411 for unrecognized breakpoint locations.
fa8d40ab
JJ
412 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
413static enum auto_boolean pending_break_support;
920d2a44
AC
414static void
415show_pending_break_support (struct ui_file *file, int from_tty,
416 struct cmd_list_element *c,
417 const char *value)
418{
3e43a32a
MS
419 fprintf_filtered (file,
420 _("Debugger's behavior regarding "
421 "pending breakpoints is %s.\n"),
920d2a44
AC
422 value);
423}
fa8d40ab 424
765dc015 425/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 426 set with "break" but falling in read-only memory.
765dc015
VP
427 If 0, gdb will warn about such breakpoints, but won't automatically
428 use hardware breakpoints. */
429static int automatic_hardware_breakpoints;
430static void
431show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
432 struct cmd_list_element *c,
433 const char *value)
434{
3e43a32a
MS
435 fprintf_filtered (file,
436 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
437 value);
438}
439
a25a5a45
PA
440/* If on, GDB keeps breakpoints inserted even if the inferior is
441 stopped, and immediately inserts any new breakpoints as soon as
442 they're created. If off (default), GDB keeps breakpoints off of
443 the target as long as possible. That is, it delays inserting
444 breakpoints until the next resume, and removes them again when the
445 target fully stops. This is a bit safer in case GDB crashes while
446 processing user input. */
447static int always_inserted_mode = 0;
72d0e2c5 448
33e5cbd6 449static void
74960c60 450show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 451 struct cmd_list_element *c, const char *value)
74960c60 452{
a25a5a45
PA
453 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
454 value);
74960c60
VP
455}
456
b57bacec
PA
457/* See breakpoint.h. */
458
33e5cbd6 459int
a25a5a45 460breakpoints_should_be_inserted_now (void)
33e5cbd6 461{
a25a5a45
PA
462 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
463 {
464 /* If breakpoints are global, they should be inserted even if no
465 thread under gdb's control is running, or even if there are
466 no threads under GDB's control yet. */
467 return 1;
468 }
469 else if (target_has_execution)
470 {
a25a5a45
PA
471 if (always_inserted_mode)
472 {
473 /* The user wants breakpoints inserted even if all threads
474 are stopped. */
475 return 1;
476 }
477
b57bacec
PA
478 if (threads_are_executing ())
479 return 1;
a25a5a45
PA
480 }
481 return 0;
33e5cbd6 482}
765dc015 483
b775012e
LM
484static const char condition_evaluation_both[] = "host or target";
485
486/* Modes for breakpoint condition evaluation. */
487static const char condition_evaluation_auto[] = "auto";
488static const char condition_evaluation_host[] = "host";
489static const char condition_evaluation_target[] = "target";
490static const char *const condition_evaluation_enums[] = {
491 condition_evaluation_auto,
492 condition_evaluation_host,
493 condition_evaluation_target,
494 NULL
495};
496
497/* Global that holds the current mode for breakpoint condition evaluation. */
498static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
499
500/* Global that we use to display information to the user (gets its value from
501 condition_evaluation_mode_1. */
502static const char *condition_evaluation_mode = condition_evaluation_auto;
503
504/* Translate a condition evaluation mode MODE into either "host"
505 or "target". This is used mostly to translate from "auto" to the
506 real setting that is being used. It returns the translated
507 evaluation mode. */
508
509static const char *
510translate_condition_evaluation_mode (const char *mode)
511{
512 if (mode == condition_evaluation_auto)
513 {
514 if (target_supports_evaluation_of_breakpoint_conditions ())
515 return condition_evaluation_target;
516 else
517 return condition_evaluation_host;
518 }
519 else
520 return mode;
521}
522
523/* Discovers what condition_evaluation_auto translates to. */
524
525static const char *
526breakpoint_condition_evaluation_mode (void)
527{
528 return translate_condition_evaluation_mode (condition_evaluation_mode);
529}
530
531/* Return true if GDB should evaluate breakpoint conditions or false
532 otherwise. */
533
534static int
535gdb_evaluates_breakpoint_condition_p (void)
536{
537 const char *mode = breakpoint_condition_evaluation_mode ();
538
539 return (mode == condition_evaluation_host);
540}
541
a14ed312 542void _initialize_breakpoint (void);
c906108c 543
c906108c
SS
544/* Are we executing breakpoint commands? */
545static int executing_breakpoint_commands;
546
c02f5703
MS
547/* Are overlay event breakpoints enabled? */
548static int overlay_events_enabled;
549
e09342b5
TJB
550/* See description in breakpoint.h. */
551int target_exact_watchpoints = 0;
552
c906108c 553/* Walk the following statement or block through all breakpoints.
e5dd4106 554 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 555 current breakpoint. */
c906108c 556
5c44784c 557#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 558
5c44784c
JM
559#define ALL_BREAKPOINTS_SAFE(B,TMP) \
560 for (B = breakpoint_chain; \
561 B ? (TMP=B->next, 1): 0; \
562 B = TMP)
c906108c 563
4a64f543
MS
564/* Similar iterator for the low-level breakpoints. SAFE variant is
565 not provided so update_global_location_list must not be called
566 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 567
876fa593
JK
568#define ALL_BP_LOCATIONS(B,BP_TMP) \
569 for (BP_TMP = bp_location; \
570 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
571 BP_TMP++)
7cc221ef 572
b775012e
LM
573/* Iterates through locations with address ADDRESS for the currently selected
574 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
575 to where the loop should start from.
576 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
577 appropriate location to start with. */
578
579#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
580 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
581 BP_LOCP_TMP = BP_LOCP_START; \
582 BP_LOCP_START \
583 && (BP_LOCP_TMP < bp_location + bp_location_count \
584 && (*BP_LOCP_TMP)->address == ADDRESS); \
585 BP_LOCP_TMP++)
586
1042e4c0
SS
587/* Iterator for tracepoints only. */
588
589#define ALL_TRACEPOINTS(B) \
590 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 591 if (is_tracepoint (B))
1042e4c0 592
7cc221ef 593/* Chains of all breakpoints defined. */
c906108c
SS
594
595struct breakpoint *breakpoint_chain;
596
876fa593
JK
597/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
598
599static struct bp_location **bp_location;
600
601/* Number of elements of BP_LOCATION. */
602
603static unsigned bp_location_count;
604
4a64f543
MS
605/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
606 ADDRESS for the current elements of BP_LOCATION which get a valid
607 result from bp_location_has_shadow. You can use it for roughly
608 limiting the subrange of BP_LOCATION to scan for shadow bytes for
609 an address you need to read. */
876fa593
JK
610
611static CORE_ADDR bp_location_placed_address_before_address_max;
612
4a64f543
MS
613/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
614 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
615 BP_LOCATION which get a valid result from bp_location_has_shadow.
616 You can use it for roughly limiting the subrange of BP_LOCATION to
617 scan for shadow bytes for an address you need to read. */
876fa593
JK
618
619static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 620
4a64f543
MS
621/* The locations that no longer correspond to any breakpoint, unlinked
622 from bp_location array, but for which a hit may still be reported
623 by a target. */
20874c92
VP
624VEC(bp_location_p) *moribund_locations = NULL;
625
c906108c
SS
626/* Number of last breakpoint made. */
627
95a42b64
TT
628static int breakpoint_count;
629
86b17b60
PA
630/* The value of `breakpoint_count' before the last command that
631 created breakpoints. If the last (break-like) command created more
632 than one breakpoint, then the difference between BREAKPOINT_COUNT
633 and PREV_BREAKPOINT_COUNT is more than one. */
634static int prev_breakpoint_count;
c906108c 635
1042e4c0
SS
636/* Number of last tracepoint made. */
637
95a42b64 638static int tracepoint_count;
1042e4c0 639
6149aea9
PA
640static struct cmd_list_element *breakpoint_set_cmdlist;
641static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 642struct cmd_list_element *save_cmdlist;
6149aea9 643
badd37ce
SDJ
644/* See declaration at breakpoint.h. */
645
646struct breakpoint *
647breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
648 void *user_data)
649{
650 struct breakpoint *b = NULL;
651
652 ALL_BREAKPOINTS (b)
653 {
654 if (func (b, user_data) != 0)
655 break;
656 }
657
658 return b;
659}
660
468d015d
JJ
661/* Return whether a breakpoint is an active enabled breakpoint. */
662static int
663breakpoint_enabled (struct breakpoint *b)
664{
0d381245 665 return (b->enable_state == bp_enabled);
468d015d
JJ
666}
667
c906108c
SS
668/* Set breakpoint count to NUM. */
669
95a42b64 670static void
fba45db2 671set_breakpoint_count (int num)
c906108c 672{
86b17b60 673 prev_breakpoint_count = breakpoint_count;
c906108c 674 breakpoint_count = num;
4fa62494 675 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
676}
677
86b17b60
PA
678/* Used by `start_rbreak_breakpoints' below, to record the current
679 breakpoint count before "rbreak" creates any breakpoint. */
680static int rbreak_start_breakpoint_count;
681
95a42b64
TT
682/* Called at the start an "rbreak" command to record the first
683 breakpoint made. */
86b17b60 684
95a42b64
TT
685void
686start_rbreak_breakpoints (void)
687{
86b17b60 688 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
689}
690
691/* Called at the end of an "rbreak" command to record the last
692 breakpoint made. */
86b17b60 693
95a42b64
TT
694void
695end_rbreak_breakpoints (void)
696{
86b17b60 697 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
698}
699
4a64f543 700/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
701
702void
fba45db2 703clear_breakpoint_hit_counts (void)
c906108c
SS
704{
705 struct breakpoint *b;
706
707 ALL_BREAKPOINTS (b)
708 b->hit_count = 0;
709}
710
9add0f1b
TT
711/* Allocate a new counted_command_line with reference count of 1.
712 The new structure owns COMMANDS. */
713
714static struct counted_command_line *
715alloc_counted_command_line (struct command_line *commands)
716{
717 struct counted_command_line *result
718 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 719
9add0f1b
TT
720 result->refc = 1;
721 result->commands = commands;
722 return result;
723}
724
725/* Increment reference count. This does nothing if CMD is NULL. */
726
727static void
728incref_counted_command_line (struct counted_command_line *cmd)
729{
730 if (cmd)
731 ++cmd->refc;
732}
733
734/* Decrement reference count. If the reference count reaches 0,
735 destroy the counted_command_line. Sets *CMDP to NULL. This does
736 nothing if *CMDP is NULL. */
737
738static void
739decref_counted_command_line (struct counted_command_line **cmdp)
740{
741 if (*cmdp)
742 {
743 if (--(*cmdp)->refc == 0)
744 {
745 free_command_lines (&(*cmdp)->commands);
746 xfree (*cmdp);
747 }
748 *cmdp = NULL;
749 }
750}
751
752/* A cleanup function that calls decref_counted_command_line. */
753
754static void
755do_cleanup_counted_command_line (void *arg)
756{
757 decref_counted_command_line (arg);
758}
759
760/* Create a cleanup that calls decref_counted_command_line on the
761 argument. */
762
763static struct cleanup *
764make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
765{
766 return make_cleanup (do_cleanup_counted_command_line, cmdp);
767}
768
c906108c 769\f
48cb2d85
VP
770/* Return the breakpoint with the specified number, or NULL
771 if the number does not refer to an existing breakpoint. */
772
773struct breakpoint *
774get_breakpoint (int num)
775{
776 struct breakpoint *b;
777
778 ALL_BREAKPOINTS (b)
779 if (b->number == num)
780 return b;
781
782 return NULL;
783}
5c44784c 784
c906108c 785\f
adc36818 786
b775012e
LM
787/* Mark locations as "conditions have changed" in case the target supports
788 evaluating conditions on its side. */
789
790static void
791mark_breakpoint_modified (struct breakpoint *b)
792{
793 struct bp_location *loc;
794
795 /* This is only meaningful if the target is
796 evaluating conditions and if the user has
797 opted for condition evaluation on the target's
798 side. */
799 if (gdb_evaluates_breakpoint_condition_p ()
800 || !target_supports_evaluation_of_breakpoint_conditions ())
801 return;
802
803 if (!is_breakpoint (b))
804 return;
805
806 for (loc = b->loc; loc; loc = loc->next)
807 loc->condition_changed = condition_modified;
808}
809
810/* Mark location as "conditions have changed" in case the target supports
811 evaluating conditions on its side. */
812
813static void
814mark_breakpoint_location_modified (struct bp_location *loc)
815{
816 /* This is only meaningful if the target is
817 evaluating conditions and if the user has
818 opted for condition evaluation on the target's
819 side. */
820 if (gdb_evaluates_breakpoint_condition_p ()
821 || !target_supports_evaluation_of_breakpoint_conditions ())
822
823 return;
824
825 if (!is_breakpoint (loc->owner))
826 return;
827
828 loc->condition_changed = condition_modified;
829}
830
831/* Sets the condition-evaluation mode using the static global
832 condition_evaluation_mode. */
833
834static void
835set_condition_evaluation_mode (char *args, int from_tty,
836 struct cmd_list_element *c)
837{
b775012e
LM
838 const char *old_mode, *new_mode;
839
840 if ((condition_evaluation_mode_1 == condition_evaluation_target)
841 && !target_supports_evaluation_of_breakpoint_conditions ())
842 {
843 condition_evaluation_mode_1 = condition_evaluation_mode;
844 warning (_("Target does not support breakpoint condition evaluation.\n"
845 "Using host evaluation mode instead."));
846 return;
847 }
848
849 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
850 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
851
abf1152a
JK
852 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
853 settings was "auto". */
854 condition_evaluation_mode = condition_evaluation_mode_1;
855
b775012e
LM
856 /* Only update the mode if the user picked a different one. */
857 if (new_mode != old_mode)
858 {
859 struct bp_location *loc, **loc_tmp;
860 /* If the user switched to a different evaluation mode, we
861 need to synch the changes with the target as follows:
862
863 "host" -> "target": Send all (valid) conditions to the target.
864 "target" -> "host": Remove all the conditions from the target.
865 */
866
b775012e
LM
867 if (new_mode == condition_evaluation_target)
868 {
869 /* Mark everything modified and synch conditions with the
870 target. */
871 ALL_BP_LOCATIONS (loc, loc_tmp)
872 mark_breakpoint_location_modified (loc);
873 }
874 else
875 {
876 /* Manually mark non-duplicate locations to synch conditions
877 with the target. We do this to remove all the conditions the
878 target knows about. */
879 ALL_BP_LOCATIONS (loc, loc_tmp)
880 if (is_breakpoint (loc->owner) && loc->inserted)
881 loc->needs_update = 1;
882 }
883
884 /* Do the update. */
44702360 885 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
886 }
887
888 return;
889}
890
891/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
892 what "auto" is translating to. */
893
894static void
895show_condition_evaluation_mode (struct ui_file *file, int from_tty,
896 struct cmd_list_element *c, const char *value)
897{
898 if (condition_evaluation_mode == condition_evaluation_auto)
899 fprintf_filtered (file,
900 _("Breakpoint condition evaluation "
901 "mode is %s (currently %s).\n"),
902 value,
903 breakpoint_condition_evaluation_mode ());
904 else
905 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
906 value);
907}
908
909/* A comparison function for bp_location AP and BP that is used by
910 bsearch. This comparison function only cares about addresses, unlike
911 the more general bp_location_compare function. */
912
913static int
914bp_location_compare_addrs (const void *ap, const void *bp)
915{
916 struct bp_location *a = *(void **) ap;
917 struct bp_location *b = *(void **) bp;
918
919 if (a->address == b->address)
920 return 0;
921 else
922 return ((a->address > b->address) - (a->address < b->address));
923}
924
925/* Helper function to skip all bp_locations with addresses
926 less than ADDRESS. It returns the first bp_location that
927 is greater than or equal to ADDRESS. If none is found, just
928 return NULL. */
929
930static struct bp_location **
931get_first_locp_gte_addr (CORE_ADDR address)
932{
933 struct bp_location dummy_loc;
934 struct bp_location *dummy_locp = &dummy_loc;
935 struct bp_location **locp_found = NULL;
936
937 /* Initialize the dummy location's address field. */
938 memset (&dummy_loc, 0, sizeof (struct bp_location));
939 dummy_loc.address = address;
940
941 /* Find a close match to the first location at ADDRESS. */
942 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
943 sizeof (struct bp_location **),
944 bp_location_compare_addrs);
945
946 /* Nothing was found, nothing left to do. */
947 if (locp_found == NULL)
948 return NULL;
949
950 /* We may have found a location that is at ADDRESS but is not the first in the
951 location's list. Go backwards (if possible) and locate the first one. */
952 while ((locp_found - 1) >= bp_location
953 && (*(locp_found - 1))->address == address)
954 locp_found--;
955
956 return locp_found;
957}
958
adc36818 959void
7a26bd4d 960set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
961 int from_tty)
962{
3a5c3e22
PA
963 xfree (b->cond_string);
964 b->cond_string = NULL;
adc36818 965
3a5c3e22 966 if (is_watchpoint (b))
adc36818 967 {
3a5c3e22
PA
968 struct watchpoint *w = (struct watchpoint *) b;
969
970 xfree (w->cond_exp);
971 w->cond_exp = NULL;
972 }
973 else
974 {
975 struct bp_location *loc;
976
977 for (loc = b->loc; loc; loc = loc->next)
978 {
979 xfree (loc->cond);
980 loc->cond = NULL;
b775012e
LM
981
982 /* No need to free the condition agent expression
983 bytecode (if we have one). We will handle this
984 when we go through update_global_location_list. */
3a5c3e22 985 }
adc36818 986 }
adc36818
PM
987
988 if (*exp == 0)
989 {
990 if (from_tty)
991 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
992 }
993 else
994 {
bbc13ae3 995 const char *arg = exp;
cc59ec59 996
adc36818
PM
997 /* I don't know if it matters whether this is the string the user
998 typed in or the decompiled expression. */
999 b->cond_string = xstrdup (arg);
1000 b->condition_not_parsed = 0;
1001
1002 if (is_watchpoint (b))
1003 {
3a5c3e22
PA
1004 struct watchpoint *w = (struct watchpoint *) b;
1005
adc36818
PM
1006 innermost_block = NULL;
1007 arg = exp;
1bb9788d 1008 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
1009 if (*arg)
1010 error (_("Junk at end of expression"));
3a5c3e22 1011 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1012 }
1013 else
1014 {
3a5c3e22
PA
1015 struct bp_location *loc;
1016
adc36818
PM
1017 for (loc = b->loc; loc; loc = loc->next)
1018 {
1019 arg = exp;
1020 loc->cond =
1bb9788d
TT
1021 parse_exp_1 (&arg, loc->address,
1022 block_for_pc (loc->address), 0);
adc36818
PM
1023 if (*arg)
1024 error (_("Junk at end of expression"));
1025 }
1026 }
1027 }
b775012e
LM
1028 mark_breakpoint_modified (b);
1029
8d3788bd 1030 observer_notify_breakpoint_modified (b);
adc36818
PM
1031}
1032
d55637df
TT
1033/* Completion for the "condition" command. */
1034
1035static VEC (char_ptr) *
6f937416
PA
1036condition_completer (struct cmd_list_element *cmd,
1037 const char *text, const char *word)
d55637df 1038{
6f937416 1039 const char *space;
d55637df 1040
6f937416
PA
1041 text = skip_spaces_const (text);
1042 space = skip_to_space_const (text);
d55637df
TT
1043 if (*space == '\0')
1044 {
1045 int len;
1046 struct breakpoint *b;
1047 VEC (char_ptr) *result = NULL;
1048
1049 if (text[0] == '$')
1050 {
1051 /* We don't support completion of history indices. */
1052 if (isdigit (text[1]))
1053 return NULL;
1054 return complete_internalvar (&text[1]);
1055 }
1056
1057 /* We're completing the breakpoint number. */
1058 len = strlen (text);
1059
1060 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1061 {
1062 char number[50];
1063
1064 xsnprintf (number, sizeof (number), "%d", b->number);
1065
1066 if (strncmp (number, text, len) == 0)
1067 VEC_safe_push (char_ptr, result, xstrdup (number));
1068 }
d55637df
TT
1069
1070 return result;
1071 }
1072
1073 /* We're completing the expression part. */
6f937416 1074 text = skip_spaces_const (space);
d55637df
TT
1075 return expression_completer (cmd, text, word);
1076}
1077
c906108c
SS
1078/* condition N EXP -- set break condition of breakpoint N to EXP. */
1079
1080static void
fba45db2 1081condition_command (char *arg, int from_tty)
c906108c 1082{
52f0bd74 1083 struct breakpoint *b;
c906108c 1084 char *p;
52f0bd74 1085 int bnum;
c906108c
SS
1086
1087 if (arg == 0)
e2e0b3e5 1088 error_no_arg (_("breakpoint number"));
c906108c
SS
1089
1090 p = arg;
1091 bnum = get_number (&p);
5c44784c 1092 if (bnum == 0)
8a3fe4f8 1093 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1094
1095 ALL_BREAKPOINTS (b)
1096 if (b->number == bnum)
2f069f6f 1097 {
6dddc817
DE
1098 /* Check if this breakpoint has a "stop" method implemented in an
1099 extension language. This method and conditions entered into GDB
1100 from the CLI are mutually exclusive. */
1101 const struct extension_language_defn *extlang
1102 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1103
1104 if (extlang != NULL)
1105 {
1106 error (_("Only one stop condition allowed. There is currently"
1107 " a %s stop condition defined for this breakpoint."),
1108 ext_lang_capitalized_name (extlang));
1109 }
2566ad2d 1110 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1111
1112 if (is_breakpoint (b))
44702360 1113 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1114
2f069f6f
JB
1115 return;
1116 }
c906108c 1117
8a3fe4f8 1118 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1119}
1120
a7bdde9e
VP
1121/* Check that COMMAND do not contain commands that are suitable
1122 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1123 Throw if any such commands is found. */
1124
a7bdde9e
VP
1125static void
1126check_no_tracepoint_commands (struct command_line *commands)
1127{
1128 struct command_line *c;
cc59ec59 1129
a7bdde9e
VP
1130 for (c = commands; c; c = c->next)
1131 {
1132 int i;
1133
1134 if (c->control_type == while_stepping_control)
3e43a32a
MS
1135 error (_("The 'while-stepping' command can "
1136 "only be used for tracepoints"));
a7bdde9e
VP
1137
1138 for (i = 0; i < c->body_count; ++i)
1139 check_no_tracepoint_commands ((c->body_list)[i]);
1140
1141 /* Not that command parsing removes leading whitespace and comment
4a64f543 1142 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1143 command directly. */
1144 if (strstr (c->line, "collect ") == c->line)
1145 error (_("The 'collect' command can only be used for tracepoints"));
1146
51661e93
VP
1147 if (strstr (c->line, "teval ") == c->line)
1148 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1149 }
1150}
1151
d77f58be
SS
1152/* Encapsulate tests for different types of tracepoints. */
1153
d9b3f62e
PA
1154static int
1155is_tracepoint_type (enum bptype type)
1156{
1157 return (type == bp_tracepoint
1158 || type == bp_fast_tracepoint
1159 || type == bp_static_tracepoint);
1160}
1161
a7bdde9e 1162int
d77f58be 1163is_tracepoint (const struct breakpoint *b)
a7bdde9e 1164{
d9b3f62e 1165 return is_tracepoint_type (b->type);
a7bdde9e 1166}
d9b3f62e 1167
e5dd4106 1168/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1169 breakpoint. This function will throw an exception if a problem is
1170 found. */
48cb2d85 1171
95a42b64
TT
1172static void
1173validate_commands_for_breakpoint (struct breakpoint *b,
1174 struct command_line *commands)
48cb2d85 1175{
d77f58be 1176 if (is_tracepoint (b))
a7bdde9e 1177 {
c9a6ce02 1178 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1179 struct command_line *c;
1180 struct command_line *while_stepping = 0;
c9a6ce02
PA
1181
1182 /* Reset the while-stepping step count. The previous commands
1183 might have included a while-stepping action, while the new
1184 ones might not. */
1185 t->step_count = 0;
1186
1187 /* We need to verify that each top-level element of commands is
1188 valid for tracepoints, that there's at most one
1189 while-stepping element, and that the while-stepping's body
1190 has valid tracing commands excluding nested while-stepping.
1191 We also need to validate the tracepoint action line in the
1192 context of the tracepoint --- validate_actionline actually
1193 has side effects, like setting the tracepoint's
1194 while-stepping STEP_COUNT, in addition to checking if the
1195 collect/teval actions parse and make sense in the
1196 tracepoint's context. */
a7bdde9e
VP
1197 for (c = commands; c; c = c->next)
1198 {
a7bdde9e
VP
1199 if (c->control_type == while_stepping_control)
1200 {
1201 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1202 error (_("The 'while-stepping' command "
1203 "cannot be used for fast tracepoint"));
0fb4aa4b 1204 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1205 error (_("The 'while-stepping' command "
1206 "cannot be used for static tracepoint"));
a7bdde9e
VP
1207
1208 if (while_stepping)
3e43a32a
MS
1209 error (_("The 'while-stepping' command "
1210 "can be used only once"));
a7bdde9e
VP
1211 else
1212 while_stepping = c;
1213 }
c9a6ce02
PA
1214
1215 validate_actionline (c->line, b);
a7bdde9e
VP
1216 }
1217 if (while_stepping)
1218 {
1219 struct command_line *c2;
1220
1221 gdb_assert (while_stepping->body_count == 1);
1222 c2 = while_stepping->body_list[0];
1223 for (; c2; c2 = c2->next)
1224 {
a7bdde9e
VP
1225 if (c2->control_type == while_stepping_control)
1226 error (_("The 'while-stepping' command cannot be nested"));
1227 }
1228 }
1229 }
1230 else
1231 {
1232 check_no_tracepoint_commands (commands);
1233 }
95a42b64
TT
1234}
1235
0fb4aa4b
PA
1236/* Return a vector of all the static tracepoints set at ADDR. The
1237 caller is responsible for releasing the vector. */
1238
1239VEC(breakpoint_p) *
1240static_tracepoints_here (CORE_ADDR addr)
1241{
1242 struct breakpoint *b;
1243 VEC(breakpoint_p) *found = 0;
1244 struct bp_location *loc;
1245
1246 ALL_BREAKPOINTS (b)
1247 if (b->type == bp_static_tracepoint)
1248 {
1249 for (loc = b->loc; loc; loc = loc->next)
1250 if (loc->address == addr)
1251 VEC_safe_push(breakpoint_p, found, b);
1252 }
1253
1254 return found;
1255}
1256
95a42b64 1257/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1258 validate that only allowed commands are included. */
95a42b64
TT
1259
1260void
4a64f543
MS
1261breakpoint_set_commands (struct breakpoint *b,
1262 struct command_line *commands)
95a42b64
TT
1263{
1264 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1265
9add0f1b
TT
1266 decref_counted_command_line (&b->commands);
1267 b->commands = alloc_counted_command_line (commands);
8d3788bd 1268 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1269}
1270
45a43567
TT
1271/* Set the internal `silent' flag on the breakpoint. Note that this
1272 is not the same as the "silent" that may appear in the breakpoint's
1273 commands. */
1274
1275void
1276breakpoint_set_silent (struct breakpoint *b, int silent)
1277{
1278 int old_silent = b->silent;
1279
1280 b->silent = silent;
1281 if (old_silent != silent)
8d3788bd 1282 observer_notify_breakpoint_modified (b);
45a43567
TT
1283}
1284
1285/* Set the thread for this breakpoint. If THREAD is -1, make the
1286 breakpoint work for any thread. */
1287
1288void
1289breakpoint_set_thread (struct breakpoint *b, int thread)
1290{
1291 int old_thread = b->thread;
1292
1293 b->thread = thread;
1294 if (old_thread != thread)
8d3788bd 1295 observer_notify_breakpoint_modified (b);
45a43567
TT
1296}
1297
1298/* Set the task for this breakpoint. If TASK is 0, make the
1299 breakpoint work for any task. */
1300
1301void
1302breakpoint_set_task (struct breakpoint *b, int task)
1303{
1304 int old_task = b->task;
1305
1306 b->task = task;
1307 if (old_task != task)
8d3788bd 1308 observer_notify_breakpoint_modified (b);
45a43567
TT
1309}
1310
95a42b64
TT
1311void
1312check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1313{
1314 struct breakpoint *b = closure;
cc59ec59 1315
6f937416 1316 validate_actionline (line, b);
a7bdde9e
VP
1317}
1318
95a42b64
TT
1319/* A structure used to pass information through
1320 map_breakpoint_numbers. */
1321
1322struct commands_info
1323{
1324 /* True if the command was typed at a tty. */
1325 int from_tty;
86b17b60
PA
1326
1327 /* The breakpoint range spec. */
1328 char *arg;
1329
95a42b64
TT
1330 /* Non-NULL if the body of the commands are being read from this
1331 already-parsed command. */
1332 struct command_line *control;
86b17b60 1333
95a42b64
TT
1334 /* The command lines read from the user, or NULL if they have not
1335 yet been read. */
1336 struct counted_command_line *cmd;
1337};
1338
1339/* A callback for map_breakpoint_numbers that sets the commands for
1340 commands_command. */
1341
c906108c 1342static void
95a42b64 1343do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1344{
95a42b64 1345 struct commands_info *info = data;
c906108c 1346
95a42b64
TT
1347 if (info->cmd == NULL)
1348 {
1349 struct command_line *l;
5c44784c 1350
95a42b64
TT
1351 if (info->control != NULL)
1352 l = copy_command_lines (info->control->body_list[0]);
1353 else
86b17b60
PA
1354 {
1355 struct cleanup *old_chain;
1356 char *str;
c5aa993b 1357
3e43a32a
MS
1358 str = xstrprintf (_("Type commands for breakpoint(s) "
1359 "%s, one per line."),
86b17b60
PA
1360 info->arg);
1361
1362 old_chain = make_cleanup (xfree, str);
1363
1364 l = read_command_lines (str,
1365 info->from_tty, 1,
d77f58be 1366 (is_tracepoint (b)
86b17b60
PA
1367 ? check_tracepoint_command : 0),
1368 b);
1369
1370 do_cleanups (old_chain);
1371 }
a7bdde9e 1372
95a42b64
TT
1373 info->cmd = alloc_counted_command_line (l);
1374 }
1375
1376 /* If a breakpoint was on the list more than once, we don't need to
1377 do anything. */
1378 if (b->commands != info->cmd)
1379 {
1380 validate_commands_for_breakpoint (b, info->cmd->commands);
1381 incref_counted_command_line (info->cmd);
1382 decref_counted_command_line (&b->commands);
1383 b->commands = info->cmd;
8d3788bd 1384 observer_notify_breakpoint_modified (b);
c5aa993b 1385 }
95a42b64
TT
1386}
1387
1388static void
4a64f543
MS
1389commands_command_1 (char *arg, int from_tty,
1390 struct command_line *control)
95a42b64
TT
1391{
1392 struct cleanup *cleanups;
1393 struct commands_info info;
1394
1395 info.from_tty = from_tty;
1396 info.control = control;
1397 info.cmd = NULL;
1398 /* If we read command lines from the user, then `info' will hold an
1399 extra reference to the commands that we must clean up. */
1400 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1401
1402 if (arg == NULL || !*arg)
1403 {
86b17b60 1404 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1405 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1406 breakpoint_count);
95a42b64
TT
1407 else if (breakpoint_count > 0)
1408 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1409 else
1410 {
1411 /* So that we don't try to free the incoming non-NULL
1412 argument in the cleanup below. Mapping breakpoint
1413 numbers will fail in this case. */
1414 arg = NULL;
1415 }
95a42b64 1416 }
9766ced4
SS
1417 else
1418 /* The command loop has some static state, so we need to preserve
1419 our argument. */
1420 arg = xstrdup (arg);
86b17b60
PA
1421
1422 if (arg != NULL)
1423 make_cleanup (xfree, arg);
1424
1425 info.arg = arg;
95a42b64
TT
1426
1427 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1428
1429 if (info.cmd == NULL)
1430 error (_("No breakpoints specified."));
1431
1432 do_cleanups (cleanups);
1433}
1434
1435static void
1436commands_command (char *arg, int from_tty)
1437{
1438 commands_command_1 (arg, from_tty, NULL);
c906108c 1439}
40c03ae8
EZ
1440
1441/* Like commands_command, but instead of reading the commands from
1442 input stream, takes them from an already parsed command structure.
1443
1444 This is used by cli-script.c to DTRT with breakpoint commands
1445 that are part of if and while bodies. */
1446enum command_control_type
1447commands_from_control_command (char *arg, struct command_line *cmd)
1448{
95a42b64
TT
1449 commands_command_1 (arg, 0, cmd);
1450 return simple_control;
40c03ae8 1451}
876fa593
JK
1452
1453/* Return non-zero if BL->TARGET_INFO contains valid information. */
1454
1455static int
1456bp_location_has_shadow (struct bp_location *bl)
1457{
1458 if (bl->loc_type != bp_loc_software_breakpoint)
1459 return 0;
1460 if (!bl->inserted)
1461 return 0;
1462 if (bl->target_info.shadow_len == 0)
e5dd4106 1463 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1464 return 0;
1465 return 1;
1466}
1467
9d497a19
PA
1468/* Update BUF, which is LEN bytes read from the target address
1469 MEMADDR, by replacing a memory breakpoint with its shadowed
1470 contents.
1471
1472 If READBUF is not NULL, this buffer must not overlap with the of
1473 the breakpoint location's shadow_contents buffer. Otherwise, a
1474 failed assertion internal error will be raised. */
1475
1476static void
1477one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1478 const gdb_byte *writebuf_org,
1479 ULONGEST memaddr, LONGEST len,
1480 struct bp_target_info *target_info,
1481 struct gdbarch *gdbarch)
1482{
1483 /* Now do full processing of the found relevant range of elements. */
1484 CORE_ADDR bp_addr = 0;
1485 int bp_size = 0;
1486 int bptoffset = 0;
1487
1488 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1489 current_program_space->aspace, 0))
1490 {
1491 /* The breakpoint is inserted in a different address space. */
1492 return;
1493 }
1494
1495 /* Addresses and length of the part of the breakpoint that
1496 we need to copy. */
1497 bp_addr = target_info->placed_address;
1498 bp_size = target_info->shadow_len;
1499
1500 if (bp_addr + bp_size <= memaddr)
1501 {
1502 /* The breakpoint is entirely before the chunk of memory we are
1503 reading. */
1504 return;
1505 }
1506
1507 if (bp_addr >= memaddr + len)
1508 {
1509 /* The breakpoint is entirely after the chunk of memory we are
1510 reading. */
1511 return;
1512 }
1513
1514 /* Offset within shadow_contents. */
1515 if (bp_addr < memaddr)
1516 {
1517 /* Only copy the second part of the breakpoint. */
1518 bp_size -= memaddr - bp_addr;
1519 bptoffset = memaddr - bp_addr;
1520 bp_addr = memaddr;
1521 }
1522
1523 if (bp_addr + bp_size > memaddr + len)
1524 {
1525 /* Only copy the first part of the breakpoint. */
1526 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1527 }
1528
1529 if (readbuf != NULL)
1530 {
1531 /* Verify that the readbuf buffer does not overlap with the
1532 shadow_contents buffer. */
1533 gdb_assert (target_info->shadow_contents >= readbuf + len
1534 || readbuf >= (target_info->shadow_contents
1535 + target_info->shadow_len));
1536
1537 /* Update the read buffer with this inserted breakpoint's
1538 shadow. */
1539 memcpy (readbuf + bp_addr - memaddr,
1540 target_info->shadow_contents + bptoffset, bp_size);
1541 }
1542 else
1543 {
1544 const unsigned char *bp;
0d5ed153
MR
1545 CORE_ADDR addr = target_info->reqstd_address;
1546 int placed_size;
9d497a19
PA
1547
1548 /* Update the shadow with what we want to write to memory. */
1549 memcpy (target_info->shadow_contents + bptoffset,
1550 writebuf_org + bp_addr - memaddr, bp_size);
1551
1552 /* Determine appropriate breakpoint contents and size for this
1553 address. */
0d5ed153 1554 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1555
1556 /* Update the final write buffer with this inserted
1557 breakpoint's INSN. */
1558 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1559 }
1560}
1561
8defab1a 1562/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1563 by replacing any memory breakpoints with their shadowed contents.
1564
35c63cd8
JB
1565 If READBUF is not NULL, this buffer must not overlap with any of
1566 the breakpoint location's shadow_contents buffers. Otherwise,
1567 a failed assertion internal error will be raised.
1568
876fa593 1569 The range of shadowed area by each bp_location is:
35df4500
TJB
1570 bl->address - bp_location_placed_address_before_address_max
1571 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1572 The range we were requested to resolve shadows for is:
1573 memaddr ... memaddr + len
1574 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1575 memaddr + len <= (bl->address
1576 - bp_location_placed_address_before_address_max)
876fa593 1577 and:
35df4500 1578 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1579
8defab1a 1580void
f0ba3972
PA
1581breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1582 const gdb_byte *writebuf_org,
1583 ULONGEST memaddr, LONGEST len)
c906108c 1584{
4a64f543
MS
1585 /* Left boundary, right boundary and median element of our binary
1586 search. */
876fa593 1587 unsigned bc_l, bc_r, bc;
9d497a19 1588 size_t i;
876fa593 1589
4a64f543
MS
1590 /* Find BC_L which is a leftmost element which may affect BUF
1591 content. It is safe to report lower value but a failure to
1592 report higher one. */
876fa593
JK
1593
1594 bc_l = 0;
1595 bc_r = bp_location_count;
1596 while (bc_l + 1 < bc_r)
1597 {
35df4500 1598 struct bp_location *bl;
876fa593
JK
1599
1600 bc = (bc_l + bc_r) / 2;
35df4500 1601 bl = bp_location[bc];
876fa593 1602
4a64f543
MS
1603 /* Check first BL->ADDRESS will not overflow due to the added
1604 constant. Then advance the left boundary only if we are sure
1605 the BC element can in no way affect the BUF content (MEMADDR
1606 to MEMADDR + LEN range).
876fa593 1607
4a64f543
MS
1608 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1609 offset so that we cannot miss a breakpoint with its shadow
1610 range tail still reaching MEMADDR. */
c5aa993b 1611
35df4500
TJB
1612 if ((bl->address + bp_location_shadow_len_after_address_max
1613 >= bl->address)
1614 && (bl->address + bp_location_shadow_len_after_address_max
1615 <= memaddr))
876fa593
JK
1616 bc_l = bc;
1617 else
1618 bc_r = bc;
1619 }
1620
128070bb
PA
1621 /* Due to the binary search above, we need to make sure we pick the
1622 first location that's at BC_L's address. E.g., if there are
1623 multiple locations at the same address, BC_L may end up pointing
1624 at a duplicate location, and miss the "master"/"inserted"
1625 location. Say, given locations L1, L2 and L3 at addresses A and
1626 B:
1627
1628 L1@A, L2@A, L3@B, ...
1629
1630 BC_L could end up pointing at location L2, while the "master"
1631 location could be L1. Since the `loc->inserted' flag is only set
1632 on "master" locations, we'd forget to restore the shadow of L1
1633 and L2. */
1634 while (bc_l > 0
1635 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1636 bc_l--;
1637
876fa593
JK
1638 /* Now do full processing of the found relevant range of elements. */
1639
1640 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1641 {
35df4500 1642 struct bp_location *bl = bp_location[bc];
876fa593
JK
1643 CORE_ADDR bp_addr = 0;
1644 int bp_size = 0;
1645 int bptoffset = 0;
1646
35df4500
TJB
1647 /* bp_location array has BL->OWNER always non-NULL. */
1648 if (bl->owner->type == bp_none)
8a3fe4f8 1649 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1650 bl->owner->number);
ffce0d52 1651
e5dd4106 1652 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1653 content. */
1654
35df4500
TJB
1655 if (bl->address >= bp_location_placed_address_before_address_max
1656 && memaddr + len <= (bl->address
1657 - bp_location_placed_address_before_address_max))
876fa593
JK
1658 break;
1659
35df4500 1660 if (!bp_location_has_shadow (bl))
c5aa993b 1661 continue;
6c95b8df 1662
9d497a19
PA
1663 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1664 memaddr, len, &bl->target_info, bl->gdbarch);
1665 }
c906108c 1666}
9d497a19 1667
c906108c 1668\f
c5aa993b 1669
b775012e
LM
1670/* Return true if BPT is either a software breakpoint or a hardware
1671 breakpoint. */
1672
1673int
1674is_breakpoint (const struct breakpoint *bpt)
1675{
1676 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1677 || bpt->type == bp_hardware_breakpoint
1678 || bpt->type == bp_dprintf);
b775012e
LM
1679}
1680
60e1c644
PA
1681/* Return true if BPT is of any hardware watchpoint kind. */
1682
a5606eee 1683static int
d77f58be 1684is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1685{
1686 return (bpt->type == bp_hardware_watchpoint
1687 || bpt->type == bp_read_watchpoint
1688 || bpt->type == bp_access_watchpoint);
1689}
7270d8f2 1690
60e1c644
PA
1691/* Return true if BPT is of any watchpoint kind, hardware or
1692 software. */
1693
3a5c3e22 1694int
d77f58be 1695is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1696{
1697 return (is_hardware_watchpoint (bpt)
1698 || bpt->type == bp_watchpoint);
1699}
1700
3a5c3e22
PA
1701/* Returns true if the current thread and its running state are safe
1702 to evaluate or update watchpoint B. Watchpoints on local
1703 expressions need to be evaluated in the context of the thread that
1704 was current when the watchpoint was created, and, that thread needs
1705 to be stopped to be able to select the correct frame context.
1706 Watchpoints on global expressions can be evaluated on any thread,
1707 and in any state. It is presently left to the target allowing
1708 memory accesses when threads are running. */
f6bc2008
PA
1709
1710static int
3a5c3e22 1711watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1712{
d0d8b0c6
JK
1713 return (b->base.pspace == current_program_space
1714 && (ptid_equal (b->watchpoint_thread, null_ptid)
1715 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1716 && !is_executing (inferior_ptid))));
f6bc2008
PA
1717}
1718
d0fb5eae
JK
1719/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1720 associated bp_watchpoint_scope breakpoint. */
1721
1722static void
3a5c3e22 1723watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1724{
3a5c3e22 1725 struct breakpoint *b = &w->base;
d0fb5eae
JK
1726
1727 if (b->related_breakpoint != b)
1728 {
1729 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1730 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1731 b->related_breakpoint->disposition = disp_del_at_next_stop;
1732 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1733 b->related_breakpoint = b;
1734 }
1735 b->disposition = disp_del_at_next_stop;
1736}
1737
bb9d5f81
PP
1738/* Extract a bitfield value from value VAL using the bit parameters contained in
1739 watchpoint W. */
1740
1741static struct value *
1742extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1743{
1744 struct value *bit_val;
1745
1746 if (val == NULL)
1747 return NULL;
1748
1749 bit_val = allocate_value (value_type (val));
1750
1751 unpack_value_bitfield (bit_val,
1752 w->val_bitpos,
1753 w->val_bitsize,
1754 value_contents_for_printing (val),
1755 value_offset (val),
1756 val);
1757
1758 return bit_val;
1759}
1760
567e1b4e
JB
1761/* Assuming that B is a watchpoint:
1762 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1763 - Evaluate expression and store the result in B->val
567e1b4e
JB
1764 - Evaluate the condition if there is one, and store the result
1765 in b->loc->cond.
a5606eee
VP
1766 - Update the list of values that must be watched in B->loc.
1767
4a64f543
MS
1768 If the watchpoint disposition is disp_del_at_next_stop, then do
1769 nothing. If this is local watchpoint that is out of scope, delete
1770 it.
1771
1772 Even with `set breakpoint always-inserted on' the watchpoints are
1773 removed + inserted on each stop here. Normal breakpoints must
1774 never be removed because they might be missed by a running thread
1775 when debugging in non-stop mode. On the other hand, hardware
1776 watchpoints (is_hardware_watchpoint; processed here) are specific
1777 to each LWP since they are stored in each LWP's hardware debug
1778 registers. Therefore, such LWP must be stopped first in order to
1779 be able to modify its hardware watchpoints.
1780
1781 Hardware watchpoints must be reset exactly once after being
1782 presented to the user. It cannot be done sooner, because it would
1783 reset the data used to present the watchpoint hit to the user. And
1784 it must not be done later because it could display the same single
1785 watchpoint hit during multiple GDB stops. Note that the latter is
1786 relevant only to the hardware watchpoint types bp_read_watchpoint
1787 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1788 not user-visible - its hit is suppressed if the memory content has
1789 not changed.
1790
1791 The following constraints influence the location where we can reset
1792 hardware watchpoints:
1793
1794 * target_stopped_by_watchpoint and target_stopped_data_address are
1795 called several times when GDB stops.
1796
1797 [linux]
1798 * Multiple hardware watchpoints can be hit at the same time,
1799 causing GDB to stop. GDB only presents one hardware watchpoint
1800 hit at a time as the reason for stopping, and all the other hits
1801 are presented later, one after the other, each time the user
1802 requests the execution to be resumed. Execution is not resumed
1803 for the threads still having pending hit event stored in
1804 LWP_INFO->STATUS. While the watchpoint is already removed from
1805 the inferior on the first stop the thread hit event is kept being
1806 reported from its cached value by linux_nat_stopped_data_address
1807 until the real thread resume happens after the watchpoint gets
1808 presented and thus its LWP_INFO->STATUS gets reset.
1809
1810 Therefore the hardware watchpoint hit can get safely reset on the
1811 watchpoint removal from inferior. */
a79d3c27 1812
b40ce68a 1813static void
3a5c3e22 1814update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1815{
a5606eee 1816 int within_current_scope;
a5606eee 1817 struct frame_id saved_frame_id;
66076460 1818 int frame_saved;
a5606eee 1819
f6bc2008
PA
1820 /* If this is a local watchpoint, we only want to check if the
1821 watchpoint frame is in scope if the current thread is the thread
1822 that was used to create the watchpoint. */
1823 if (!watchpoint_in_thread_scope (b))
1824 return;
1825
3a5c3e22 1826 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1827 return;
1828
66076460 1829 frame_saved = 0;
a5606eee
VP
1830
1831 /* Determine if the watchpoint is within scope. */
1832 if (b->exp_valid_block == NULL)
1833 within_current_scope = 1;
1834 else
1835 {
b5db5dfc
UW
1836 struct frame_info *fi = get_current_frame ();
1837 struct gdbarch *frame_arch = get_frame_arch (fi);
1838 CORE_ADDR frame_pc = get_frame_pc (fi);
1839
1840 /* If we're in a function epilogue, unwinding may not work
1841 properly, so do not attempt to recreate locations at this
1842 point. See similar comments in watchpoint_check. */
1843 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1844 return;
66076460
DJ
1845
1846 /* Save the current frame's ID so we can restore it after
1847 evaluating the watchpoint expression on its own frame. */
1848 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1849 took a frame parameter, so that we didn't have to change the
1850 selected frame. */
1851 frame_saved = 1;
1852 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1853
a5606eee
VP
1854 fi = frame_find_by_id (b->watchpoint_frame);
1855 within_current_scope = (fi != NULL);
1856 if (within_current_scope)
1857 select_frame (fi);
1858 }
1859
b5db5dfc
UW
1860 /* We don't free locations. They are stored in the bp_location array
1861 and update_global_location_list will eventually delete them and
1862 remove breakpoints if needed. */
3a5c3e22 1863 b->base.loc = NULL;
b5db5dfc 1864
a5606eee
VP
1865 if (within_current_scope && reparse)
1866 {
bbc13ae3 1867 const char *s;
d63d0675 1868
a5606eee
VP
1869 if (b->exp)
1870 {
1871 xfree (b->exp);
1872 b->exp = NULL;
1873 }
d63d0675 1874 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1875 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1876 /* If the meaning of expression itself changed, the old value is
1877 no longer relevant. We don't want to report a watchpoint hit
1878 to the user when the old value and the new value may actually
1879 be completely different objects. */
1880 value_free (b->val);
fa4727a6
DJ
1881 b->val = NULL;
1882 b->val_valid = 0;
60e1c644
PA
1883
1884 /* Note that unlike with breakpoints, the watchpoint's condition
1885 expression is stored in the breakpoint object, not in the
1886 locations (re)created below. */
3a5c3e22 1887 if (b->base.cond_string != NULL)
60e1c644
PA
1888 {
1889 if (b->cond_exp != NULL)
1890 {
1891 xfree (b->cond_exp);
1892 b->cond_exp = NULL;
1893 }
1894
3a5c3e22 1895 s = b->base.cond_string;
1bb9788d 1896 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1897 }
a5606eee 1898 }
a5606eee
VP
1899
1900 /* If we failed to parse the expression, for example because
1901 it refers to a global variable in a not-yet-loaded shared library,
1902 don't try to insert watchpoint. We don't automatically delete
1903 such watchpoint, though, since failure to parse expression
1904 is different from out-of-scope watchpoint. */
e8369a73 1905 if (!target_has_execution)
2d134ed3
PA
1906 {
1907 /* Without execution, memory can't change. No use to try and
1908 set watchpoint locations. The watchpoint will be reset when
1909 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1910 if (!can_use_hw_watchpoints)
1911 {
1912 if (b->base.ops->works_in_software_mode (&b->base))
1913 b->base.type = bp_watchpoint;
1914 else
638aa5a1
AB
1915 error (_("Can't set read/access watchpoint when "
1916 "hardware watchpoints are disabled."));
e8369a73 1917 }
2d134ed3
PA
1918 }
1919 else if (within_current_scope && b->exp)
a5606eee 1920 {
0cf6dd15 1921 int pc = 0;
fa4727a6 1922 struct value *val_chain, *v, *result, *next;
2d134ed3 1923 struct program_space *frame_pspace;
a5606eee 1924
3a1115a0 1925 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1926
a5606eee
VP
1927 /* Avoid setting b->val if it's already set. The meaning of
1928 b->val is 'the last value' user saw, and we should update
1929 it only if we reported that last value to user. As it
9c06b0b4
TJB
1930 happens, the code that reports it updates b->val directly.
1931 We don't keep track of the memory value for masked
1932 watchpoints. */
3a5c3e22 1933 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1934 {
bb9d5f81
PP
1935 if (b->val_bitsize != 0)
1936 {
1937 v = extract_bitfield_from_watchpoint_value (b, v);
1938 if (v != NULL)
1939 release_value (v);
1940 }
fa4727a6
DJ
1941 b->val = v;
1942 b->val_valid = 1;
1943 }
a5606eee 1944
2d134ed3
PA
1945 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1946
a5606eee 1947 /* Look at each value on the value chain. */
9fa40276 1948 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1949 {
1950 /* If it's a memory location, and GDB actually needed
1951 its contents to evaluate the expression, then we
fa4727a6
DJ
1952 must watch it. If the first value returned is
1953 still lazy, that means an error occurred reading it;
1954 watch it anyway in case it becomes readable. */
a5606eee 1955 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1956 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1957 {
1958 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1959
a5606eee
VP
1960 /* We only watch structs and arrays if user asked
1961 for it explicitly, never if they just happen to
1962 appear in the middle of some value chain. */
fa4727a6 1963 if (v == result
a5606eee
VP
1964 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1965 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1966 {
1967 CORE_ADDR addr;
744a8059 1968 int type;
a5606eee 1969 struct bp_location *loc, **tmp;
bb9d5f81
PP
1970 int bitpos = 0, bitsize = 0;
1971
1972 if (value_bitsize (v) != 0)
1973 {
1974 /* Extract the bit parameters out from the bitfield
1975 sub-expression. */
1976 bitpos = value_bitpos (v);
1977 bitsize = value_bitsize (v);
1978 }
1979 else if (v == result && b->val_bitsize != 0)
1980 {
1981 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1982 lvalue whose bit parameters are saved in the fields
1983 VAL_BITPOS and VAL_BITSIZE. */
1984 bitpos = b->val_bitpos;
1985 bitsize = b->val_bitsize;
1986 }
a5606eee 1987
42ae5230 1988 addr = value_address (v);
bb9d5f81
PP
1989 if (bitsize != 0)
1990 {
1991 /* Skip the bytes that don't contain the bitfield. */
1992 addr += bitpos / 8;
1993 }
1994
a5606eee 1995 type = hw_write;
3a5c3e22 1996 if (b->base.type == bp_read_watchpoint)
a5606eee 1997 type = hw_read;
3a5c3e22 1998 else if (b->base.type == bp_access_watchpoint)
a5606eee 1999 type = hw_access;
3a5c3e22
PA
2000
2001 loc = allocate_bp_location (&b->base);
2002 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2003 ;
2004 *tmp = loc;
a6d9a66e 2005 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2006
2007 loc->pspace = frame_pspace;
a5606eee 2008 loc->address = addr;
bb9d5f81
PP
2009
2010 if (bitsize != 0)
2011 {
2012 /* Just cover the bytes that make up the bitfield. */
2013 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2014 }
2015 else
2016 loc->length = TYPE_LENGTH (value_type (v));
2017
a5606eee
VP
2018 loc->watchpoint_type = type;
2019 }
2020 }
9fa40276
TJB
2021 }
2022
2023 /* Change the type of breakpoint between hardware assisted or
2024 an ordinary watchpoint depending on the hardware support
2025 and free hardware slots. REPARSE is set when the inferior
2026 is started. */
a9634178 2027 if (reparse)
9fa40276 2028 {
e09342b5 2029 int reg_cnt;
9fa40276
TJB
2030 enum bp_loc_type loc_type;
2031 struct bp_location *bl;
a5606eee 2032
a9634178 2033 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2034
2035 if (reg_cnt)
9fa40276
TJB
2036 {
2037 int i, target_resources_ok, other_type_used;
a1398e0c 2038 enum bptype type;
9fa40276 2039
a9634178
TJB
2040 /* Use an exact watchpoint when there's only one memory region to be
2041 watched, and only one debug register is needed to watch it. */
2042 b->exact = target_exact_watchpoints && reg_cnt == 1;
2043
9fa40276 2044 /* We need to determine how many resources are already
e09342b5
TJB
2045 used for all other hardware watchpoints plus this one
2046 to see if we still have enough resources to also fit
a1398e0c
PA
2047 this watchpoint in as well. */
2048
2049 /* If this is a software watchpoint, we try to turn it
2050 to a hardware one -- count resources as if B was of
2051 hardware watchpoint type. */
2052 type = b->base.type;
2053 if (type == bp_watchpoint)
2054 type = bp_hardware_watchpoint;
2055
2056 /* This watchpoint may or may not have been placed on
2057 the list yet at this point (it won't be in the list
2058 if we're trying to create it for the first time,
2059 through watch_command), so always account for it
2060 manually. */
2061
2062 /* Count resources used by all watchpoints except B. */
2063 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2064
2065 /* Add in the resources needed for B. */
2066 i += hw_watchpoint_use_count (&b->base);
2067
2068 target_resources_ok
2069 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2070 if (target_resources_ok <= 0)
a9634178 2071 {
3a5c3e22 2072 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2073
2074 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2075 error (_("Target does not support this type of "
2076 "hardware watchpoint."));
9c06b0b4
TJB
2077 else if (target_resources_ok < 0 && !sw_mode)
2078 error (_("There are not enough available hardware "
2079 "resources for this watchpoint."));
a1398e0c
PA
2080
2081 /* Downgrade to software watchpoint. */
2082 b->base.type = bp_watchpoint;
2083 }
2084 else
2085 {
2086 /* If this was a software watchpoint, we've just
2087 found we have enough resources to turn it to a
2088 hardware watchpoint. Otherwise, this is a
2089 nop. */
2090 b->base.type = type;
a9634178 2091 }
9fa40276 2092 }
3a5c3e22 2093 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2094 {
2095 if (!can_use_hw_watchpoints)
2096 error (_("Can't set read/access watchpoint when "
2097 "hardware watchpoints are disabled."));
2098 else
2099 error (_("Expression cannot be implemented with "
2100 "read/access watchpoint."));
2101 }
9fa40276 2102 else
3a5c3e22 2103 b->base.type = bp_watchpoint;
9fa40276 2104
3a5c3e22 2105 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2106 : bp_loc_hardware_watchpoint);
3a5c3e22 2107 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2108 bl->loc_type = loc_type;
2109 }
2110
2111 for (v = val_chain; v; v = next)
2112 {
a5606eee
VP
2113 next = value_next (v);
2114 if (v != b->val)
2115 value_free (v);
2116 }
2117
c7437ca6
PA
2118 /* If a software watchpoint is not watching any memory, then the
2119 above left it without any location set up. But,
2120 bpstat_stop_status requires a location to be able to report
2121 stops, so make sure there's at least a dummy one. */
3a5c3e22 2122 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2123 {
3a5c3e22
PA
2124 struct breakpoint *base = &b->base;
2125 base->loc = allocate_bp_location (base);
2126 base->loc->pspace = frame_pspace;
2127 base->loc->address = -1;
2128 base->loc->length = -1;
2129 base->loc->watchpoint_type = -1;
c7437ca6 2130 }
a5606eee
VP
2131 }
2132 else if (!within_current_scope)
7270d8f2 2133 {
ac74f770
MS
2134 printf_filtered (_("\
2135Watchpoint %d deleted because the program has left the block\n\
2136in which its expression is valid.\n"),
3a5c3e22 2137 b->base.number);
d0fb5eae 2138 watchpoint_del_at_next_stop (b);
7270d8f2 2139 }
a5606eee
VP
2140
2141 /* Restore the selected frame. */
66076460
DJ
2142 if (frame_saved)
2143 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2144}
2145
a5606eee 2146
74960c60 2147/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2148 inserted in the inferior. We don't differentiate the type of BL's owner
2149 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2150 breakpoint_ops is not defined, because in insert_bp_location,
2151 tracepoint's insert_location will not be called. */
74960c60 2152static int
35df4500 2153should_be_inserted (struct bp_location *bl)
74960c60 2154{
35df4500 2155 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2156 return 0;
2157
35df4500 2158 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2159 return 0;
2160
35df4500 2161 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2162 return 0;
2163
f8eba3c6
TT
2164 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2165 return 0;
2166
56710373
PA
2167 /* This is set for example, when we're attached to the parent of a
2168 vfork, and have detached from the child. The child is running
2169 free, and we expect it to do an exec or exit, at which point the
2170 OS makes the parent schedulable again (and the target reports
2171 that the vfork is done). Until the child is done with the shared
2172 memory region, do not insert breakpoints in the parent, otherwise
2173 the child could still trip on the parent's breakpoints. Since
2174 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2175 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2176 return 0;
2177
31e77af2
PA
2178 /* Don't insert a breakpoint if we're trying to step past its
2179 location. */
2180 if ((bl->loc_type == bp_loc_software_breakpoint
2181 || bl->loc_type == bp_loc_hardware_breakpoint)
2182 && stepping_past_instruction_at (bl->pspace->aspace,
2183 bl->address))
e558d7c1
PA
2184 {
2185 if (debug_infrun)
2186 {
2187 fprintf_unfiltered (gdb_stdlog,
2188 "infrun: skipping breakpoint: "
2189 "stepping past insn at: %s\n",
2190 paddress (bl->gdbarch, bl->address));
2191 }
2192 return 0;
2193 }
31e77af2 2194
963f9c80
PA
2195 /* Don't insert watchpoints if we're trying to step past the
2196 instruction that triggered one. */
2197 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2198 && stepping_past_nonsteppable_watchpoint ())
2199 {
2200 if (debug_infrun)
2201 {
2202 fprintf_unfiltered (gdb_stdlog,
2203 "infrun: stepping past non-steppable watchpoint. "
2204 "skipping watchpoint at %s:%d\n",
2205 paddress (bl->gdbarch, bl->address),
2206 bl->length);
2207 }
2208 return 0;
2209 }
2210
74960c60
VP
2211 return 1;
2212}
2213
934709f0
PW
2214/* Same as should_be_inserted but does the check assuming
2215 that the location is not duplicated. */
2216
2217static int
2218unduplicated_should_be_inserted (struct bp_location *bl)
2219{
2220 int result;
2221 const int save_duplicate = bl->duplicate;
2222
2223 bl->duplicate = 0;
2224 result = should_be_inserted (bl);
2225 bl->duplicate = save_duplicate;
2226 return result;
2227}
2228
b775012e
LM
2229/* Parses a conditional described by an expression COND into an
2230 agent expression bytecode suitable for evaluation
2231 by the bytecode interpreter. Return NULL if there was
2232 any error during parsing. */
2233
2234static struct agent_expr *
2235parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2236{
2237 struct agent_expr *aexpr = NULL;
b775012e
LM
2238
2239 if (!cond)
2240 return NULL;
2241
2242 /* We don't want to stop processing, so catch any errors
2243 that may show up. */
492d29ea 2244 TRY
b775012e
LM
2245 {
2246 aexpr = gen_eval_for_expr (scope, cond);
2247 }
2248
492d29ea 2249 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2250 {
2251 /* If we got here, it means the condition could not be parsed to a valid
2252 bytecode expression and thus can't be evaluated on the target's side.
2253 It's no use iterating through the conditions. */
2254 return NULL;
2255 }
492d29ea 2256 END_CATCH
b775012e
LM
2257
2258 /* We have a valid agent expression. */
2259 return aexpr;
2260}
2261
2262/* Based on location BL, create a list of breakpoint conditions to be
2263 passed on to the target. If we have duplicated locations with different
2264 conditions, we will add such conditions to the list. The idea is that the
2265 target will evaluate the list of conditions and will only notify GDB when
2266 one of them is true. */
2267
2268static void
2269build_target_condition_list (struct bp_location *bl)
2270{
2271 struct bp_location **locp = NULL, **loc2p;
2272 int null_condition_or_parse_error = 0;
2273 int modified = bl->needs_update;
2274 struct bp_location *loc;
2275
8b4f3082
PA
2276 /* Release conditions left over from a previous insert. */
2277 VEC_free (agent_expr_p, bl->target_info.conditions);
2278
b775012e
LM
2279 /* This is only meaningful if the target is
2280 evaluating conditions and if the user has
2281 opted for condition evaluation on the target's
2282 side. */
2283 if (gdb_evaluates_breakpoint_condition_p ()
2284 || !target_supports_evaluation_of_breakpoint_conditions ())
2285 return;
2286
2287 /* Do a first pass to check for locations with no assigned
2288 conditions or conditions that fail to parse to a valid agent expression
2289 bytecode. If any of these happen, then it's no use to send conditions
2290 to the target since this location will always trigger and generate a
2291 response back to GDB. */
2292 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2293 {
2294 loc = (*loc2p);
2295 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2296 {
2297 if (modified)
2298 {
2299 struct agent_expr *aexpr;
2300
2301 /* Re-parse the conditions since something changed. In that
2302 case we already freed the condition bytecodes (see
2303 force_breakpoint_reinsertion). We just
2304 need to parse the condition to bytecodes again. */
2305 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2306 loc->cond_bytecode = aexpr;
2307
2308 /* Check if we managed to parse the conditional expression
2309 correctly. If not, we will not send this condition
2310 to the target. */
2311 if (aexpr)
2312 continue;
2313 }
2314
2315 /* If we have a NULL bytecode expression, it means something
2316 went wrong or we have a null condition expression. */
2317 if (!loc->cond_bytecode)
2318 {
2319 null_condition_or_parse_error = 1;
2320 break;
2321 }
2322 }
2323 }
2324
2325 /* If any of these happened, it means we will have to evaluate the conditions
2326 for the location's address on gdb's side. It is no use keeping bytecodes
2327 for all the other duplicate locations, thus we free all of them here.
2328
2329 This is so we have a finer control over which locations' conditions are
2330 being evaluated by GDB or the remote stub. */
2331 if (null_condition_or_parse_error)
2332 {
2333 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2334 {
2335 loc = (*loc2p);
2336 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2337 {
2338 /* Only go as far as the first NULL bytecode is
2339 located. */
2340 if (!loc->cond_bytecode)
2341 return;
2342
2343 free_agent_expr (loc->cond_bytecode);
2344 loc->cond_bytecode = NULL;
2345 }
2346 }
2347 }
2348
2349 /* No NULL conditions or failed bytecode generation. Build a condition list
2350 for this location's address. */
2351 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2352 {
2353 loc = (*loc2p);
2354 if (loc->cond
2355 && is_breakpoint (loc->owner)
2356 && loc->pspace->num == bl->pspace->num
2357 && loc->owner->enable_state == bp_enabled
2358 && loc->enabled)
2359 /* Add the condition to the vector. This will be used later to send the
2360 conditions to the target. */
2361 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2362 loc->cond_bytecode);
2363 }
2364
2365 return;
2366}
2367
d3ce09f5
SS
2368/* Parses a command described by string CMD into an agent expression
2369 bytecode suitable for evaluation by the bytecode interpreter.
2370 Return NULL if there was any error during parsing. */
2371
2372static struct agent_expr *
2373parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2374{
2375 struct cleanup *old_cleanups = 0;
2376 struct expression *expr, **argvec;
2377 struct agent_expr *aexpr = NULL;
bbc13ae3
KS
2378 const char *cmdrest;
2379 const char *format_start, *format_end;
d3ce09f5
SS
2380 struct format_piece *fpieces;
2381 int nargs;
2382 struct gdbarch *gdbarch = get_current_arch ();
2383
2384 if (!cmd)
2385 return NULL;
2386
2387 cmdrest = cmd;
2388
2389 if (*cmdrest == ',')
2390 ++cmdrest;
bbc13ae3 2391 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2392
2393 if (*cmdrest++ != '"')
2394 error (_("No format string following the location"));
2395
2396 format_start = cmdrest;
2397
2398 fpieces = parse_format_string (&cmdrest);
2399
2400 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2401
2402 format_end = cmdrest;
2403
2404 if (*cmdrest++ != '"')
2405 error (_("Bad format string, non-terminated '\"'."));
2406
bbc13ae3 2407 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2408
2409 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2410 error (_("Invalid argument syntax"));
2411
2412 if (*cmdrest == ',')
2413 cmdrest++;
bbc13ae3 2414 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2415
2416 /* For each argument, make an expression. */
2417
2418 argvec = (struct expression **) alloca (strlen (cmd)
2419 * sizeof (struct expression *));
2420
2421 nargs = 0;
2422 while (*cmdrest != '\0')
2423 {
bbc13ae3 2424 const char *cmd1;
d3ce09f5
SS
2425
2426 cmd1 = cmdrest;
2427 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2428 argvec[nargs++] = expr;
2429 cmdrest = cmd1;
2430 if (*cmdrest == ',')
2431 ++cmdrest;
2432 }
2433
2434 /* We don't want to stop processing, so catch any errors
2435 that may show up. */
492d29ea 2436 TRY
d3ce09f5
SS
2437 {
2438 aexpr = gen_printf (scope, gdbarch, 0, 0,
2439 format_start, format_end - format_start,
2440 fpieces, nargs, argvec);
2441 }
492d29ea 2442 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2443 {
2444 /* If we got here, it means the command could not be parsed to a valid
2445 bytecode expression and thus can't be evaluated on the target's side.
2446 It's no use iterating through the other commands. */
492d29ea 2447 aexpr = NULL;
d3ce09f5 2448 }
492d29ea
PA
2449 END_CATCH
2450
2451 do_cleanups (old_cleanups);
d3ce09f5 2452
d3ce09f5
SS
2453 /* We have a valid agent expression, return it. */
2454 return aexpr;
2455}
2456
2457/* Based on location BL, create a list of breakpoint commands to be
2458 passed on to the target. If we have duplicated locations with
2459 different commands, we will add any such to the list. */
2460
2461static void
2462build_target_command_list (struct bp_location *bl)
2463{
2464 struct bp_location **locp = NULL, **loc2p;
2465 int null_command_or_parse_error = 0;
2466 int modified = bl->needs_update;
2467 struct bp_location *loc;
2468
8b4f3082
PA
2469 /* Release commands left over from a previous insert. */
2470 VEC_free (agent_expr_p, bl->target_info.tcommands);
2471
41fac0cf 2472 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2473 return;
2474
41fac0cf
PA
2475 /* For now, limit to agent-style dprintf breakpoints. */
2476 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2477 return;
2478
41fac0cf
PA
2479 /* For now, if we have any duplicate location that isn't a dprintf,
2480 don't install the target-side commands, as that would make the
2481 breakpoint not be reported to the core, and we'd lose
2482 control. */
2483 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2484 {
2485 loc = (*loc2p);
2486 if (is_breakpoint (loc->owner)
2487 && loc->pspace->num == bl->pspace->num
2488 && loc->owner->type != bp_dprintf)
2489 return;
2490 }
2491
d3ce09f5
SS
2492 /* Do a first pass to check for locations with no assigned
2493 conditions or conditions that fail to parse to a valid agent expression
2494 bytecode. If any of these happen, then it's no use to send conditions
2495 to the target since this location will always trigger and generate a
2496 response back to GDB. */
2497 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2498 {
2499 loc = (*loc2p);
2500 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2501 {
2502 if (modified)
2503 {
2504 struct agent_expr *aexpr;
2505
2506 /* Re-parse the commands since something changed. In that
2507 case we already freed the command bytecodes (see
2508 force_breakpoint_reinsertion). We just
2509 need to parse the command to bytecodes again. */
2510 aexpr = parse_cmd_to_aexpr (bl->address,
2511 loc->owner->extra_string);
2512 loc->cmd_bytecode = aexpr;
2513
2514 if (!aexpr)
2515 continue;
2516 }
2517
2518 /* If we have a NULL bytecode expression, it means something
2519 went wrong or we have a null command expression. */
2520 if (!loc->cmd_bytecode)
2521 {
2522 null_command_or_parse_error = 1;
2523 break;
2524 }
2525 }
2526 }
2527
2528 /* If anything failed, then we're not doing target-side commands,
2529 and so clean up. */
2530 if (null_command_or_parse_error)
2531 {
2532 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2533 {
2534 loc = (*loc2p);
2535 if (is_breakpoint (loc->owner)
2536 && loc->pspace->num == bl->pspace->num)
2537 {
2538 /* Only go as far as the first NULL bytecode is
2539 located. */
40fb6c5e 2540 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2541 return;
2542
40fb6c5e
HZ
2543 free_agent_expr (loc->cmd_bytecode);
2544 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2545 }
2546 }
2547 }
2548
2549 /* No NULL commands or failed bytecode generation. Build a command list
2550 for this location's address. */
2551 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2552 {
2553 loc = (*loc2p);
2554 if (loc->owner->extra_string
2555 && is_breakpoint (loc->owner)
2556 && loc->pspace->num == bl->pspace->num
2557 && loc->owner->enable_state == bp_enabled
2558 && loc->enabled)
2559 /* Add the command to the vector. This will be used later
2560 to send the commands to the target. */
2561 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2562 loc->cmd_bytecode);
2563 }
2564
2565 bl->target_info.persist = 0;
2566 /* Maybe flag this location as persistent. */
2567 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2568 bl->target_info.persist = 1;
2569}
2570
35df4500
TJB
2571/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2572 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2573 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2574 Returns 0 for success, 1 if the bp_location type is not supported or
2575 -1 for failure.
879bfdc2 2576
4a64f543
MS
2577 NOTE drow/2003-09-09: This routine could be broken down to an
2578 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2579static int
35df4500 2580insert_bp_location (struct bp_location *bl,
26bb91f3 2581 struct ui_file *tmp_error_stream,
3fbb6ffa 2582 int *disabled_breaks,
dd61ec5c
MW
2583 int *hw_breakpoint_error,
2584 int *hw_bp_error_explained_already)
879bfdc2 2585{
0000e5cc
PA
2586 enum errors bp_err = GDB_NO_ERROR;
2587 const char *bp_err_message = NULL;
879bfdc2 2588
b775012e 2589 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2590 return 0;
2591
35c63cd8
JB
2592 /* Note we don't initialize bl->target_info, as that wipes out
2593 the breakpoint location's shadow_contents if the breakpoint
2594 is still inserted at that location. This in turn breaks
2595 target_read_memory which depends on these buffers when
2596 a memory read is requested at the breakpoint location:
2597 Once the target_info has been wiped, we fail to see that
2598 we have a breakpoint inserted at that address and thus
2599 read the breakpoint instead of returning the data saved in
2600 the breakpoint location's shadow contents. */
0d5ed153 2601 bl->target_info.reqstd_address = bl->address;
35df4500 2602 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2603 bl->target_info.length = bl->length;
8181d85f 2604
b775012e
LM
2605 /* When working with target-side conditions, we must pass all the conditions
2606 for the same breakpoint address down to the target since GDB will not
2607 insert those locations. With a list of breakpoint conditions, the target
2608 can decide when to stop and notify GDB. */
2609
2610 if (is_breakpoint (bl->owner))
2611 {
2612 build_target_condition_list (bl);
d3ce09f5
SS
2613 build_target_command_list (bl);
2614 /* Reset the modification marker. */
b775012e
LM
2615 bl->needs_update = 0;
2616 }
2617
35df4500
TJB
2618 if (bl->loc_type == bp_loc_software_breakpoint
2619 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2620 {
35df4500 2621 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2622 {
2623 /* If the explicitly specified breakpoint type
2624 is not hardware breakpoint, check the memory map to see
2625 if the breakpoint address is in read only memory or not.
4a64f543 2626
765dc015
VP
2627 Two important cases are:
2628 - location type is not hardware breakpoint, memory
2629 is readonly. We change the type of the location to
2630 hardware breakpoint.
4a64f543
MS
2631 - location type is hardware breakpoint, memory is
2632 read-write. This means we've previously made the
2633 location hardware one, but then the memory map changed,
2634 so we undo.
765dc015 2635
4a64f543
MS
2636 When breakpoints are removed, remove_breakpoints will use
2637 location types we've just set here, the only possible
2638 problem is that memory map has changed during running
2639 program, but it's not going to work anyway with current
2640 gdb. */
765dc015 2641 struct mem_region *mr
0d5ed153 2642 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2643
2644 if (mr)
2645 {
2646 if (automatic_hardware_breakpoints)
2647 {
765dc015
VP
2648 enum bp_loc_type new_type;
2649
2650 if (mr->attrib.mode != MEM_RW)
2651 new_type = bp_loc_hardware_breakpoint;
2652 else
2653 new_type = bp_loc_software_breakpoint;
2654
35df4500 2655 if (new_type != bl->loc_type)
765dc015
VP
2656 {
2657 static int said = 0;
cc59ec59 2658
35df4500 2659 bl->loc_type = new_type;
765dc015
VP
2660 if (!said)
2661 {
3e43a32a
MS
2662 fprintf_filtered (gdb_stdout,
2663 _("Note: automatically using "
2664 "hardware breakpoints for "
2665 "read-only addresses.\n"));
765dc015
VP
2666 said = 1;
2667 }
2668 }
2669 }
35df4500 2670 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2671 && mr->attrib.mode != MEM_RW)
2672 {
2673 fprintf_unfiltered (tmp_error_stream,
2674 _("Cannot insert breakpoint %d.\n"
2675 "Cannot set software breakpoint "
2676 "at read-only address %s\n"),
2677 bl->owner->number,
2678 paddress (bl->gdbarch, bl->address));
2679 return 1;
2680 }
765dc015
VP
2681 }
2682 }
2683
879bfdc2
DJ
2684 /* First check to see if we have to handle an overlay. */
2685 if (overlay_debugging == ovly_off
35df4500
TJB
2686 || bl->section == NULL
2687 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2688 {
2689 /* No overlay handling: just set the breakpoint. */
492d29ea 2690 TRY
dd61ec5c 2691 {
0000e5cc
PA
2692 int val;
2693
dd61ec5c 2694 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2695 if (val)
2696 bp_err = GENERIC_ERROR;
dd61ec5c 2697 }
492d29ea 2698 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2699 {
0000e5cc
PA
2700 bp_err = e.error;
2701 bp_err_message = e.message;
dd61ec5c 2702 }
492d29ea 2703 END_CATCH
879bfdc2
DJ
2704 }
2705 else
2706 {
4a64f543 2707 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2708 Shall we set a breakpoint at the LMA? */
2709 if (!overlay_events_enabled)
2710 {
2711 /* Yes -- overlay event support is not active,
2712 so we must try to set a breakpoint at the LMA.
2713 This will not work for a hardware breakpoint. */
35df4500 2714 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2715 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2716 bl->owner->number);
879bfdc2
DJ
2717 else
2718 {
35df4500
TJB
2719 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2720 bl->section);
879bfdc2 2721 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2722 bl->overlay_target_info = bl->target_info;
0d5ed153 2723 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2724
2725 /* No overlay handling: just set the breakpoint. */
492d29ea 2726 TRY
0000e5cc
PA
2727 {
2728 int val;
2729
2730 val = target_insert_breakpoint (bl->gdbarch,
2731 &bl->overlay_target_info);
2732 if (val)
2733 bp_err = GENERIC_ERROR;
2734 }
492d29ea 2735 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2736 {
2737 bp_err = e.error;
2738 bp_err_message = e.message;
2739 }
492d29ea 2740 END_CATCH
0000e5cc
PA
2741
2742 if (bp_err != GDB_NO_ERROR)
99361f52 2743 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2744 "Overlay breakpoint %d "
2745 "failed: in ROM?\n",
35df4500 2746 bl->owner->number);
879bfdc2
DJ
2747 }
2748 }
2749 /* Shall we set a breakpoint at the VMA? */
35df4500 2750 if (section_is_mapped (bl->section))
879bfdc2
DJ
2751 {
2752 /* Yes. This overlay section is mapped into memory. */
492d29ea 2753 TRY
dd61ec5c 2754 {
0000e5cc
PA
2755 int val;
2756
dd61ec5c 2757 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2758 if (val)
2759 bp_err = GENERIC_ERROR;
dd61ec5c 2760 }
492d29ea 2761 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2762 {
0000e5cc
PA
2763 bp_err = e.error;
2764 bp_err_message = e.message;
dd61ec5c 2765 }
492d29ea 2766 END_CATCH
879bfdc2
DJ
2767 }
2768 else
2769 {
2770 /* No. This breakpoint will not be inserted.
2771 No error, but do not mark the bp as 'inserted'. */
2772 return 0;
2773 }
2774 }
2775
0000e5cc 2776 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2777 {
2778 /* Can't set the breakpoint. */
0000e5cc
PA
2779
2780 /* In some cases, we might not be able to insert a
2781 breakpoint in a shared library that has already been
2782 removed, but we have not yet processed the shlib unload
2783 event. Unfortunately, some targets that implement
076855f9
PA
2784 breakpoint insertion themselves can't tell why the
2785 breakpoint insertion failed (e.g., the remote target
2786 doesn't define error codes), so we must treat generic
2787 errors as memory errors. */
0000e5cc 2788 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2789 && bl->loc_type == bp_loc_software_breakpoint
08351840 2790 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2791 || shared_objfile_contains_address_p (bl->pspace,
2792 bl->address)))
879bfdc2 2793 {
4a64f543 2794 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2795 bl->shlib_disabled = 1;
8d3788bd 2796 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2797 if (!*disabled_breaks)
2798 {
2799 fprintf_unfiltered (tmp_error_stream,
2800 "Cannot insert breakpoint %d.\n",
2801 bl->owner->number);
2802 fprintf_unfiltered (tmp_error_stream,
2803 "Temporarily disabling shared "
2804 "library breakpoints:\n");
2805 }
2806 *disabled_breaks = 1;
879bfdc2 2807 fprintf_unfiltered (tmp_error_stream,
35df4500 2808 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2809 return 0;
879bfdc2
DJ
2810 }
2811 else
879bfdc2 2812 {
35df4500 2813 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2814 {
0000e5cc
PA
2815 *hw_breakpoint_error = 1;
2816 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2817 fprintf_unfiltered (tmp_error_stream,
2818 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2819 bl->owner->number, bp_err_message ? ":" : ".\n");
2820 if (bp_err_message != NULL)
2821 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2822 }
2823 else
2824 {
0000e5cc
PA
2825 if (bp_err_message == NULL)
2826 {
2827 char *message
2828 = memory_error_message (TARGET_XFER_E_IO,
2829 bl->gdbarch, bl->address);
2830 struct cleanup *old_chain = make_cleanup (xfree, message);
2831
2832 fprintf_unfiltered (tmp_error_stream,
2833 "Cannot insert breakpoint %d.\n"
2834 "%s\n",
2835 bl->owner->number, message);
2836 do_cleanups (old_chain);
2837 }
2838 else
2839 {
2840 fprintf_unfiltered (tmp_error_stream,
2841 "Cannot insert breakpoint %d: %s\n",
2842 bl->owner->number,
2843 bp_err_message);
2844 }
879bfdc2 2845 }
0000e5cc 2846 return 1;
879bfdc2
DJ
2847
2848 }
2849 }
2850 else
35df4500 2851 bl->inserted = 1;
879bfdc2 2852
0000e5cc 2853 return 0;
879bfdc2
DJ
2854 }
2855
35df4500 2856 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2857 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2858 watchpoints. It's not clear that it's necessary... */
35df4500 2859 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2860 {
0000e5cc
PA
2861 int val;
2862
77b06cd7
TJB
2863 gdb_assert (bl->owner->ops != NULL
2864 && bl->owner->ops->insert_location != NULL);
2865
2866 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2867
2868 /* If trying to set a read-watchpoint, and it turns out it's not
2869 supported, try emulating one with an access watchpoint. */
35df4500 2870 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2871 {
2872 struct bp_location *loc, **loc_temp;
2873
2874 /* But don't try to insert it, if there's already another
2875 hw_access location that would be considered a duplicate
2876 of this one. */
2877 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2878 if (loc != bl
85d721b8 2879 && loc->watchpoint_type == hw_access
35df4500 2880 && watchpoint_locations_match (bl, loc))
85d721b8 2881 {
35df4500
TJB
2882 bl->duplicate = 1;
2883 bl->inserted = 1;
2884 bl->target_info = loc->target_info;
2885 bl->watchpoint_type = hw_access;
85d721b8
PA
2886 val = 0;
2887 break;
2888 }
2889
2890 if (val == 1)
2891 {
77b06cd7
TJB
2892 bl->watchpoint_type = hw_access;
2893 val = bl->owner->ops->insert_location (bl);
2894
2895 if (val)
2896 /* Back to the original value. */
2897 bl->watchpoint_type = hw_read;
85d721b8
PA
2898 }
2899 }
2900
35df4500 2901 bl->inserted = (val == 0);
879bfdc2
DJ
2902 }
2903
35df4500 2904 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2905 {
0000e5cc
PA
2906 int val;
2907
77b06cd7
TJB
2908 gdb_assert (bl->owner->ops != NULL
2909 && bl->owner->ops->insert_location != NULL);
2910
2911 val = bl->owner->ops->insert_location (bl);
2912 if (val)
2913 {
2914 bl->owner->enable_state = bp_disabled;
2915
2916 if (val == 1)
2917 warning (_("\
2918Error inserting catchpoint %d: Your system does not support this type\n\
2919of catchpoint."), bl->owner->number);
2920 else
2921 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2922 }
2923
2924 bl->inserted = (val == 0);
1640b821
DJ
2925
2926 /* We've already printed an error message if there was a problem
2927 inserting this catchpoint, and we've disabled the catchpoint,
2928 so just return success. */
2929 return 0;
879bfdc2
DJ
2930 }
2931
2932 return 0;
2933}
2934
6c95b8df
PA
2935/* This function is called when program space PSPACE is about to be
2936 deleted. It takes care of updating breakpoints to not reference
2937 PSPACE anymore. */
2938
2939void
2940breakpoint_program_space_exit (struct program_space *pspace)
2941{
2942 struct breakpoint *b, *b_temp;
876fa593 2943 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2944
2945 /* Remove any breakpoint that was set through this program space. */
2946 ALL_BREAKPOINTS_SAFE (b, b_temp)
2947 {
2948 if (b->pspace == pspace)
2949 delete_breakpoint (b);
2950 }
2951
2952 /* Breakpoints set through other program spaces could have locations
2953 bound to PSPACE as well. Remove those. */
876fa593 2954 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2955 {
2956 struct bp_location *tmp;
2957
2958 if (loc->pspace == pspace)
2959 {
2bdf28a0 2960 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2961 if (loc->owner->loc == loc)
2962 loc->owner->loc = loc->next;
2963 else
2964 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2965 if (tmp->next == loc)
2966 {
2967 tmp->next = loc->next;
2968 break;
2969 }
2970 }
2971 }
2972
2973 /* Now update the global location list to permanently delete the
2974 removed locations above. */
44702360 2975 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2976}
2977
74960c60
VP
2978/* Make sure all breakpoints are inserted in inferior.
2979 Throws exception on any error.
2980 A breakpoint that is already inserted won't be inserted
2981 again, so calling this function twice is safe. */
2982void
2983insert_breakpoints (void)
2984{
2985 struct breakpoint *bpt;
2986
2987 ALL_BREAKPOINTS (bpt)
2988 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2989 {
2990 struct watchpoint *w = (struct watchpoint *) bpt;
2991
2992 update_watchpoint (w, 0 /* don't reparse. */);
2993 }
74960c60 2994
04086b45
PA
2995 /* Updating watchpoints creates new locations, so update the global
2996 location list. Explicitly tell ugll to insert locations and
2997 ignore breakpoints_always_inserted_mode. */
2998 update_global_location_list (UGLL_INSERT);
74960c60
VP
2999}
3000
20388dd6
YQ
3001/* Invoke CALLBACK for each of bp_location. */
3002
3003void
3004iterate_over_bp_locations (walk_bp_location_callback callback)
3005{
3006 struct bp_location *loc, **loc_tmp;
3007
3008 ALL_BP_LOCATIONS (loc, loc_tmp)
3009 {
3010 callback (loc, NULL);
3011 }
3012}
3013
b775012e
LM
3014/* This is used when we need to synch breakpoint conditions between GDB and the
3015 target. It is the case with deleting and disabling of breakpoints when using
3016 always-inserted mode. */
3017
3018static void
3019update_inserted_breakpoint_locations (void)
3020{
3021 struct bp_location *bl, **blp_tmp;
3022 int error_flag = 0;
3023 int val = 0;
3024 int disabled_breaks = 0;
3025 int hw_breakpoint_error = 0;
dd61ec5c 3026 int hw_bp_details_reported = 0;
b775012e
LM
3027
3028 struct ui_file *tmp_error_stream = mem_fileopen ();
3029 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3030
3031 /* Explicitly mark the warning -- this will only be printed if
3032 there was an error. */
3033 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3034
3035 save_current_space_and_thread ();
3036
3037 ALL_BP_LOCATIONS (bl, blp_tmp)
3038 {
3039 /* We only want to update software breakpoints and hardware
3040 breakpoints. */
3041 if (!is_breakpoint (bl->owner))
3042 continue;
3043
3044 /* We only want to update locations that are already inserted
3045 and need updating. This is to avoid unwanted insertion during
3046 deletion of breakpoints. */
3047 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3048 continue;
3049
3050 switch_to_program_space_and_thread (bl->pspace);
3051
3052 /* For targets that support global breakpoints, there's no need
3053 to select an inferior to insert breakpoint to. In fact, even
3054 if we aren't attached to any process yet, we should still
3055 insert breakpoints. */
f5656ead 3056 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3057 && ptid_equal (inferior_ptid, null_ptid))
3058 continue;
3059
3060 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3061 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3062 if (val)
3063 error_flag = val;
3064 }
3065
3066 if (error_flag)
3067 {
3068 target_terminal_ours_for_output ();
3069 error_stream (tmp_error_stream);
3070 }
3071
3072 do_cleanups (cleanups);
3073}
3074
c30eee59 3075/* Used when starting or continuing the program. */
c906108c 3076
74960c60
VP
3077static void
3078insert_breakpoint_locations (void)
c906108c 3079{
a5606eee 3080 struct breakpoint *bpt;
35df4500 3081 struct bp_location *bl, **blp_tmp;
eacd795a 3082 int error_flag = 0;
c906108c 3083 int val = 0;
3fbb6ffa 3084 int disabled_breaks = 0;
81d0cc19 3085 int hw_breakpoint_error = 0;
dd61ec5c 3086 int hw_bp_error_explained_already = 0;
c906108c 3087
81d0cc19 3088 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3089 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3090
81d0cc19
GS
3091 /* Explicitly mark the warning -- this will only be printed if
3092 there was an error. */
3093 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3094
3095 save_current_space_and_thread ();
3096
35df4500 3097 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3098 {
b775012e 3099 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3100 continue;
3101
4a64f543
MS
3102 /* There is no point inserting thread-specific breakpoints if
3103 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3104 has BL->OWNER always non-NULL. */
35df4500
TJB
3105 if (bl->owner->thread != -1
3106 && !valid_thread_id (bl->owner->thread))
f365de73
AS
3107 continue;
3108
35df4500 3109 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3110
3111 /* For targets that support global breakpoints, there's no need
3112 to select an inferior to insert breakpoint to. In fact, even
3113 if we aren't attached to any process yet, we should still
3114 insert breakpoints. */
f5656ead 3115 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3116 && ptid_equal (inferior_ptid, null_ptid))
3117 continue;
3118
3fbb6ffa 3119 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3120 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3121 if (val)
eacd795a 3122 error_flag = val;
879bfdc2 3123 }
c906108c 3124
4a64f543
MS
3125 /* If we failed to insert all locations of a watchpoint, remove
3126 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3127 ALL_BREAKPOINTS (bpt)
3128 {
3129 int some_failed = 0;
3130 struct bp_location *loc;
3131
3132 if (!is_hardware_watchpoint (bpt))
3133 continue;
3134
d6b74ac4 3135 if (!breakpoint_enabled (bpt))
a5606eee 3136 continue;
74960c60
VP
3137
3138 if (bpt->disposition == disp_del_at_next_stop)
3139 continue;
a5606eee
VP
3140
3141 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3142 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3143 {
3144 some_failed = 1;
3145 break;
3146 }
3147 if (some_failed)
3148 {
3149 for (loc = bpt->loc; loc; loc = loc->next)
3150 if (loc->inserted)
3151 remove_breakpoint (loc, mark_uninserted);
3152
3153 hw_breakpoint_error = 1;
3154 fprintf_unfiltered (tmp_error_stream,
3155 "Could not insert hardware watchpoint %d.\n",
3156 bpt->number);
eacd795a 3157 error_flag = -1;
a5606eee
VP
3158 }
3159 }
3160
eacd795a 3161 if (error_flag)
81d0cc19
GS
3162 {
3163 /* If a hardware breakpoint or watchpoint was inserted, add a
3164 message about possibly exhausted resources. */
dd61ec5c 3165 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3166 {
c6510018
MS
3167 fprintf_unfiltered (tmp_error_stream,
3168 "Could not insert hardware breakpoints:\n\
3169You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3170 }
81d0cc19
GS
3171 target_terminal_ours_for_output ();
3172 error_stream (tmp_error_stream);
3173 }
f7545552
TT
3174
3175 do_cleanups (cleanups);
c906108c
SS
3176}
3177
c30eee59
TJB
3178/* Used when the program stops.
3179 Returns zero if successful, or non-zero if there was a problem
3180 removing a breakpoint location. */
3181
c906108c 3182int
fba45db2 3183remove_breakpoints (void)
c906108c 3184{
35df4500 3185 struct bp_location *bl, **blp_tmp;
3a1bae8e 3186 int val = 0;
c906108c 3187
35df4500 3188 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3189 {
1e4d1764 3190 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3191 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3192 }
3a1bae8e 3193 return val;
c906108c
SS
3194}
3195
49fa26b0
PA
3196/* When a thread exits, remove breakpoints that are related to
3197 that thread. */
3198
3199static void
3200remove_threaded_breakpoints (struct thread_info *tp, int silent)
3201{
3202 struct breakpoint *b, *b_tmp;
3203
3204 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3205 {
96181529 3206 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3207 {
3208 b->disposition = disp_del_at_next_stop;
3209
3210 printf_filtered (_("\
46ecd527 3211Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3212 b->number, tp->num);
3213
3214 /* Hide it from the user. */
3215 b->number = 0;
3216 }
3217 }
3218}
3219
6c95b8df
PA
3220/* Remove breakpoints of process PID. */
3221
3222int
3223remove_breakpoints_pid (int pid)
3224{
35df4500 3225 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3226 int val;
3227 struct inferior *inf = find_inferior_pid (pid);
3228
35df4500 3229 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3230 {
35df4500 3231 if (bl->pspace != inf->pspace)
6c95b8df
PA
3232 continue;
3233
fc126975 3234 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3235 {
35df4500 3236 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3237 if (val != 0)
3238 return val;
3239 }
3240 }
3241 return 0;
3242}
3243
c906108c 3244int
fba45db2 3245reattach_breakpoints (int pid)
c906108c 3246{
6c95b8df 3247 struct cleanup *old_chain;
35df4500 3248 struct bp_location *bl, **blp_tmp;
c906108c 3249 int val;
86b887df 3250 struct ui_file *tmp_error_stream;
dd61ec5c 3251 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3252 struct inferior *inf;
3253 struct thread_info *tp;
3254
3255 tp = any_live_thread_of_process (pid);
3256 if (tp == NULL)
3257 return 1;
3258
3259 inf = find_inferior_pid (pid);
3260 old_chain = save_inferior_ptid ();
3261
3262 inferior_ptid = tp->ptid;
a4954f26 3263
86b887df 3264 tmp_error_stream = mem_fileopen ();
a4954f26 3265 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3266
35df4500 3267 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3268 {
35df4500 3269 if (bl->pspace != inf->pspace)
6c95b8df
PA
3270 continue;
3271
35df4500 3272 if (bl->inserted)
c5aa993b 3273 {
35df4500 3274 bl->inserted = 0;
dd61ec5c 3275 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3276 if (val != 0)
3277 {
ce696e05 3278 do_cleanups (old_chain);
c5aa993b
JM
3279 return val;
3280 }
3281 }
3282 }
ce696e05 3283 do_cleanups (old_chain);
c906108c
SS
3284 return 0;
3285}
3286
e58b0e63
PA
3287static int internal_breakpoint_number = -1;
3288
84f4c1fe
PM
3289/* Set the breakpoint number of B, depending on the value of INTERNAL.
3290 If INTERNAL is non-zero, the breakpoint number will be populated
3291 from internal_breakpoint_number and that variable decremented.
e5dd4106 3292 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3293 breakpoint_count and that value incremented. Internal breakpoints
3294 do not set the internal var bpnum. */
3295static void
3296set_breakpoint_number (int internal, struct breakpoint *b)
3297{
3298 if (internal)
3299 b->number = internal_breakpoint_number--;
3300 else
3301 {
3302 set_breakpoint_count (breakpoint_count + 1);
3303 b->number = breakpoint_count;
3304 }
3305}
3306
e62c965a 3307static struct breakpoint *
a6d9a66e 3308create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3309 CORE_ADDR address, enum bptype type,
c0a91b2b 3310 const struct breakpoint_ops *ops)
e62c965a 3311{
e62c965a
PP
3312 struct symtab_and_line sal;
3313 struct breakpoint *b;
3314
4a64f543 3315 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3316
3317 sal.pc = address;
3318 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3319 sal.pspace = current_program_space;
e62c965a 3320
06edf0c0 3321 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3322 b->number = internal_breakpoint_number--;
3323 b->disposition = disp_donttouch;
3324
3325 return b;
3326}
3327
17450429
PP
3328static const char *const longjmp_names[] =
3329 {
3330 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3331 };
3332#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3333
3334/* Per-objfile data private to breakpoint.c. */
3335struct breakpoint_objfile_data
3336{
3337 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3338 struct bound_minimal_symbol overlay_msym;
17450429
PP
3339
3340 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3341 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3342
28106bc2
SDJ
3343 /* True if we have looked for longjmp probes. */
3344 int longjmp_searched;
3345
3346 /* SystemTap probe points for longjmp (if any). */
3347 VEC (probe_p) *longjmp_probes;
3348
17450429 3349 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3350 struct bound_minimal_symbol terminate_msym;
17450429
PP
3351
3352 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3353 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3354
3355 /* True if we have looked for exception probes. */
3356 int exception_searched;
3357
3358 /* SystemTap probe points for unwinding (if any). */
3359 VEC (probe_p) *exception_probes;
17450429
PP
3360};
3361
3362static const struct objfile_data *breakpoint_objfile_key;
3363
3364/* Minimal symbol not found sentinel. */
3365static struct minimal_symbol msym_not_found;
3366
3367/* Returns TRUE if MSYM point to the "not found" sentinel. */
3368
3369static int
3370msym_not_found_p (const struct minimal_symbol *msym)
3371{
3372 return msym == &msym_not_found;
3373}
3374
3375/* Return per-objfile data needed by breakpoint.c.
3376 Allocate the data if necessary. */
3377
3378static struct breakpoint_objfile_data *
3379get_breakpoint_objfile_data (struct objfile *objfile)
3380{
3381 struct breakpoint_objfile_data *bp_objfile_data;
3382
3383 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3384 if (bp_objfile_data == NULL)
3385 {
3386 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3387 sizeof (*bp_objfile_data));
3388
3389 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3390 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3391 }
3392 return bp_objfile_data;
3393}
3394
28106bc2
SDJ
3395static void
3396free_breakpoint_probes (struct objfile *obj, void *data)
3397{
3398 struct breakpoint_objfile_data *bp_objfile_data = data;
3399
3400 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3401 VEC_free (probe_p, bp_objfile_data->exception_probes);
3402}
3403
e62c965a 3404static void
af02033e 3405create_overlay_event_breakpoint (void)
e62c965a 3406{
69de3c6a 3407 struct objfile *objfile;
af02033e 3408 const char *const func_name = "_ovly_debug_event";
e62c965a 3409
69de3c6a
PP
3410 ALL_OBJFILES (objfile)
3411 {
3412 struct breakpoint *b;
17450429
PP
3413 struct breakpoint_objfile_data *bp_objfile_data;
3414 CORE_ADDR addr;
69de3c6a 3415
17450429
PP
3416 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3417
3b7344d5 3418 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3419 continue;
3420
3b7344d5 3421 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3422 {
3b7344d5 3423 struct bound_minimal_symbol m;
17450429
PP
3424
3425 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3426 if (m.minsym == NULL)
17450429
PP
3427 {
3428 /* Avoid future lookups in this objfile. */
3b7344d5 3429 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3430 continue;
3431 }
3432 bp_objfile_data->overlay_msym = m;
3433 }
e62c965a 3434
77e371c0 3435 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3436 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3437 bp_overlay_event,
3438 &internal_breakpoint_ops);
69de3c6a 3439 b->addr_string = xstrdup (func_name);
e62c965a 3440
69de3c6a
PP
3441 if (overlay_debugging == ovly_auto)
3442 {
3443 b->enable_state = bp_enabled;
3444 overlay_events_enabled = 1;
3445 }
3446 else
3447 {
3448 b->enable_state = bp_disabled;
3449 overlay_events_enabled = 0;
3450 }
e62c965a 3451 }
44702360 3452 update_global_location_list (UGLL_MAY_INSERT);
e62c965a
PP
3453}
3454
0fd8e87f 3455static void
af02033e 3456create_longjmp_master_breakpoint (void)
0fd8e87f 3457{
6c95b8df 3458 struct program_space *pspace;
6c95b8df
PA
3459 struct cleanup *old_chain;
3460
3461 old_chain = save_current_program_space ();
0fd8e87f 3462
6c95b8df 3463 ALL_PSPACES (pspace)
af02033e
PP
3464 {
3465 struct objfile *objfile;
3466
3467 set_current_program_space (pspace);
3468
3469 ALL_OBJFILES (objfile)
0fd8e87f 3470 {
af02033e
PP
3471 int i;
3472 struct gdbarch *gdbarch;
17450429 3473 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3474
af02033e 3475 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3476
17450429
PP
3477 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3478
28106bc2
SDJ
3479 if (!bp_objfile_data->longjmp_searched)
3480 {
25f9533e
SDJ
3481 VEC (probe_p) *ret;
3482
3483 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3484 if (ret != NULL)
3485 {
3486 /* We are only interested in checking one element. */
3487 struct probe *p = VEC_index (probe_p, ret, 0);
3488
3489 if (!can_evaluate_probe_arguments (p))
3490 {
3491 /* We cannot use the probe interface here, because it does
3492 not know how to evaluate arguments. */
3493 VEC_free (probe_p, ret);
3494 ret = NULL;
3495 }
3496 }
3497 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3498 bp_objfile_data->longjmp_searched = 1;
3499 }
3500
3501 if (bp_objfile_data->longjmp_probes != NULL)
3502 {
3503 int i;
3504 struct probe *probe;
3505 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3506
3507 for (i = 0;
3508 VEC_iterate (probe_p,
3509 bp_objfile_data->longjmp_probes,
3510 i, probe);
3511 ++i)
3512 {
3513 struct breakpoint *b;
3514
729662a5
TT
3515 b = create_internal_breakpoint (gdbarch,
3516 get_probe_address (probe,
3517 objfile),
28106bc2
SDJ
3518 bp_longjmp_master,
3519 &internal_breakpoint_ops);
3520 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3521 b->enable_state = bp_disabled;
3522 }
3523
3524 continue;
3525 }
3526
0569175e
TSD
3527 if (!gdbarch_get_longjmp_target_p (gdbarch))
3528 continue;
3529
17450429 3530 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3531 {
3532 struct breakpoint *b;
af02033e 3533 const char *func_name;
17450429 3534 CORE_ADDR addr;
6c95b8df 3535
3b7344d5 3536 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3537 continue;
0fd8e87f 3538
17450429 3539 func_name = longjmp_names[i];
3b7344d5 3540 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3541 {
3b7344d5 3542 struct bound_minimal_symbol m;
17450429
PP
3543
3544 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3545 if (m.minsym == NULL)
17450429
PP
3546 {
3547 /* Prevent future lookups in this objfile. */
3b7344d5 3548 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3549 continue;
3550 }
3551 bp_objfile_data->longjmp_msym[i] = m;
3552 }
3553
77e371c0 3554 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3555 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3556 &internal_breakpoint_ops);
af02033e
PP
3557 b->addr_string = xstrdup (func_name);
3558 b->enable_state = bp_disabled;
3559 }
0fd8e87f 3560 }
af02033e 3561 }
44702360 3562 update_global_location_list (UGLL_MAY_INSERT);
6c95b8df
PA
3563
3564 do_cleanups (old_chain);
0fd8e87f
UW
3565}
3566
af02033e 3567/* Create a master std::terminate breakpoint. */
aa7d318d 3568static void
af02033e 3569create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3570{
3571 struct program_space *pspace;
aa7d318d 3572 struct cleanup *old_chain;
af02033e 3573 const char *const func_name = "std::terminate()";
aa7d318d
TT
3574
3575 old_chain = save_current_program_space ();
3576
3577 ALL_PSPACES (pspace)
17450429
PP
3578 {
3579 struct objfile *objfile;
3580 CORE_ADDR addr;
3581
3582 set_current_program_space (pspace);
3583
aa7d318d
TT
3584 ALL_OBJFILES (objfile)
3585 {
3586 struct breakpoint *b;
17450429 3587 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3588
17450429 3589 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3590
3b7344d5 3591 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3592 continue;
3593
3b7344d5 3594 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3595 {
3b7344d5 3596 struct bound_minimal_symbol m;
17450429
PP
3597
3598 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3599 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3600 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3601 {
3602 /* Prevent future lookups in this objfile. */
3b7344d5 3603 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3604 continue;
3605 }
3606 bp_objfile_data->terminate_msym = m;
3607 }
aa7d318d 3608
77e371c0 3609 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3610 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3611 bp_std_terminate_master,
3612 &internal_breakpoint_ops);
aa7d318d
TT
3613 b->addr_string = xstrdup (func_name);
3614 b->enable_state = bp_disabled;
3615 }
17450429
PP
3616 }
3617
44702360 3618 update_global_location_list (UGLL_MAY_INSERT);
aa7d318d
TT
3619
3620 do_cleanups (old_chain);
3621}
3622
186c406b
TT
3623/* Install a master breakpoint on the unwinder's debug hook. */
3624
70221824 3625static void
186c406b
TT
3626create_exception_master_breakpoint (void)
3627{
3628 struct objfile *objfile;
17450429 3629 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3630
3631 ALL_OBJFILES (objfile)
3632 {
17450429
PP
3633 struct breakpoint *b;
3634 struct gdbarch *gdbarch;
3635 struct breakpoint_objfile_data *bp_objfile_data;
3636 CORE_ADDR addr;
3637
3638 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3639
28106bc2
SDJ
3640 /* We prefer the SystemTap probe point if it exists. */
3641 if (!bp_objfile_data->exception_searched)
3642 {
25f9533e
SDJ
3643 VEC (probe_p) *ret;
3644
3645 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3646
3647 if (ret != NULL)
3648 {
3649 /* We are only interested in checking one element. */
3650 struct probe *p = VEC_index (probe_p, ret, 0);
3651
3652 if (!can_evaluate_probe_arguments (p))
3653 {
3654 /* We cannot use the probe interface here, because it does
3655 not know how to evaluate arguments. */
3656 VEC_free (probe_p, ret);
3657 ret = NULL;
3658 }
3659 }
3660 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3661 bp_objfile_data->exception_searched = 1;
3662 }
3663
3664 if (bp_objfile_data->exception_probes != NULL)
3665 {
3666 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3667 int i;
3668 struct probe *probe;
3669
3670 for (i = 0;
3671 VEC_iterate (probe_p,
3672 bp_objfile_data->exception_probes,
3673 i, probe);
3674 ++i)
3675 {
3676 struct breakpoint *b;
3677
729662a5
TT
3678 b = create_internal_breakpoint (gdbarch,
3679 get_probe_address (probe,
3680 objfile),
28106bc2
SDJ
3681 bp_exception_master,
3682 &internal_breakpoint_ops);
3683 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3684 b->enable_state = bp_disabled;
3685 }
3686
3687 continue;
3688 }
3689
3690 /* Otherwise, try the hook function. */
3691
3b7344d5 3692 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3693 continue;
3694
3695 gdbarch = get_objfile_arch (objfile);
186c406b 3696
3b7344d5 3697 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3698 {
3b7344d5 3699 struct bound_minimal_symbol debug_hook;
186c406b 3700
17450429 3701 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3702 if (debug_hook.minsym == NULL)
17450429 3703 {
3b7344d5 3704 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3705 continue;
3706 }
3707
3708 bp_objfile_data->exception_msym = debug_hook;
186c406b 3709 }
17450429 3710
77e371c0 3711 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3712 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3713 &current_target);
06edf0c0
PA
3714 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3715 &internal_breakpoint_ops);
17450429
PP
3716 b->addr_string = xstrdup (func_name);
3717 b->enable_state = bp_disabled;
186c406b
TT
3718 }
3719
44702360 3720 update_global_location_list (UGLL_MAY_INSERT);
186c406b
TT
3721}
3722
c906108c 3723void
fba45db2 3724update_breakpoints_after_exec (void)
c906108c 3725{
35df4500 3726 struct breakpoint *b, *b_tmp;
876fa593 3727 struct bp_location *bploc, **bplocp_tmp;
c906108c 3728
25b22b0a
PA
3729 /* We're about to delete breakpoints from GDB's lists. If the
3730 INSERTED flag is true, GDB will try to lift the breakpoints by
3731 writing the breakpoints' "shadow contents" back into memory. The
3732 "shadow contents" are NOT valid after an exec, so GDB should not
3733 do that. Instead, the target is responsible from marking
3734 breakpoints out as soon as it detects an exec. We don't do that
3735 here instead, because there may be other attempts to delete
3736 breakpoints after detecting an exec and before reaching here. */
876fa593 3737 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3738 if (bploc->pspace == current_program_space)
3739 gdb_assert (!bploc->inserted);
c906108c 3740
35df4500 3741 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3742 {
6c95b8df
PA
3743 if (b->pspace != current_program_space)
3744 continue;
3745
4a64f543 3746 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3747 if (b->type == bp_shlib_event)
3748 {
3749 delete_breakpoint (b);
3750 continue;
3751 }
c906108c 3752
4a64f543 3753 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3754 if (b->type == bp_jit_event)
3755 {
3756 delete_breakpoint (b);
3757 continue;
3758 }
3759
1900040c 3760 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3761 as must overlay event and longjmp master breakpoints. */
3762 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3763 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3764 || b->type == bp_exception_master)
c4093a6a
JM
3765 {
3766 delete_breakpoint (b);
3767 continue;
3768 }
3769
4a64f543 3770 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3771 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3772 {
3773 delete_breakpoint (b);
3774 continue;
3775 }
3776
7c16b83e
PA
3777 /* Just like single-step breakpoints. */
3778 if (b->type == bp_single_step)
3779 {
3780 delete_breakpoint (b);
3781 continue;
3782 }
3783
611c83ae
PA
3784 /* Longjmp and longjmp-resume breakpoints are also meaningless
3785 after an exec. */
186c406b 3786 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3787 || b->type == bp_longjmp_call_dummy
186c406b 3788 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3789 {
3790 delete_breakpoint (b);
3791 continue;
3792 }
3793
ce78b96d
JB
3794 if (b->type == bp_catchpoint)
3795 {
3796 /* For now, none of the bp_catchpoint breakpoints need to
3797 do anything at this point. In the future, if some of
3798 the catchpoints need to something, we will need to add
3799 a new method, and call this method from here. */
3800 continue;
3801 }
3802
c5aa993b
JM
3803 /* bp_finish is a special case. The only way we ought to be able
3804 to see one of these when an exec() has happened, is if the user
3805 caught a vfork, and then said "finish". Ordinarily a finish just
3806 carries them to the call-site of the current callee, by setting
3807 a temporary bp there and resuming. But in this case, the finish
3808 will carry them entirely through the vfork & exec.
3809
3810 We don't want to allow a bp_finish to remain inserted now. But
3811 we can't safely delete it, 'cause finish_command has a handle to
3812 the bp on a bpstat, and will later want to delete it. There's a
3813 chance (and I've seen it happen) that if we delete the bp_finish
3814 here, that its storage will get reused by the time finish_command
3815 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3816 We really must allow finish_command to delete a bp_finish.
3817
e5dd4106 3818 In the absence of a general solution for the "how do we know
53a5351d
JM
3819 it's safe to delete something others may have handles to?"
3820 problem, what we'll do here is just uninsert the bp_finish, and
3821 let finish_command delete it.
3822
3823 (We know the bp_finish is "doomed" in the sense that it's
3824 momentary, and will be deleted as soon as finish_command sees
3825 the inferior stopped. So it doesn't matter that the bp's
3826 address is probably bogus in the new a.out, unlike e.g., the
3827 solib breakpoints.) */
c5aa993b 3828
c5aa993b
JM
3829 if (b->type == bp_finish)
3830 {
3831 continue;
3832 }
3833
3834 /* Without a symbolic address, we have little hope of the
3835 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3836 a.out. */
c5aa993b
JM
3837 if (b->addr_string == NULL)
3838 {
3839 delete_breakpoint (b);
3840 continue;
3841 }
c5aa993b 3842 }
c906108c
SS
3843}
3844
3845int
d80ee84f 3846detach_breakpoints (ptid_t ptid)
c906108c 3847{
35df4500 3848 struct bp_location *bl, **blp_tmp;
3a1bae8e 3849 int val = 0;
ce696e05 3850 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3851 struct inferior *inf = current_inferior ();
c5aa993b 3852
dfd4cc63 3853 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3854 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3855
6c95b8df 3856 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3857 inferior_ptid = ptid;
35df4500 3858 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3859 {
35df4500 3860 if (bl->pspace != inf->pspace)
6c95b8df
PA
3861 continue;
3862
bd9673a4
PW
3863 /* This function must physically remove breakpoints locations
3864 from the specified ptid, without modifying the breakpoint
3865 package's state. Locations of type bp_loc_other are only
3866 maintained at GDB side. So, there is no need to remove
3867 these bp_loc_other locations. Moreover, removing these
3868 would modify the breakpoint package's state. */
3869 if (bl->loc_type == bp_loc_other)
3870 continue;
3871
35df4500
TJB
3872 if (bl->inserted)
3873 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3874 }
d03285ec 3875
ce696e05 3876 do_cleanups (old_chain);
3a1bae8e 3877 return val;
c906108c
SS
3878}
3879
35df4500 3880/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3881 Note that this is used to detach breakpoints from a child fork.
3882 When we get here, the child isn't in the inferior list, and neither
3883 do we have objects to represent its address space --- we should
35df4500 3884 *not* look at bl->pspace->aspace here. */
6c95b8df 3885
c906108c 3886static int
35df4500 3887remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3888{
3889 int val;
c5aa993b 3890
35df4500
TJB
3891 /* BL is never in moribund_locations by our callers. */
3892 gdb_assert (bl->owner != NULL);
2bdf28a0 3893
1a853c52 3894 if (bl->permanent)
c2c6d25f
JM
3895 /* Permanent breakpoints cannot be inserted or removed. */
3896 return 0;
3897
74960c60
VP
3898 /* The type of none suggests that owner is actually deleted.
3899 This should not ever happen. */
35df4500 3900 gdb_assert (bl->owner->type != bp_none);
0bde7532 3901
35df4500
TJB
3902 if (bl->loc_type == bp_loc_software_breakpoint
3903 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3904 {
c02f5703
MS
3905 /* "Normal" instruction breakpoint: either the standard
3906 trap-instruction bp (bp_breakpoint), or a
3907 bp_hardware_breakpoint. */
3908
3909 /* First check to see if we have to handle an overlay. */
3910 if (overlay_debugging == ovly_off
35df4500
TJB
3911 || bl->section == NULL
3912 || !(section_is_overlay (bl->section)))
c02f5703
MS
3913 {
3914 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3915
3916 /* If we're trying to uninsert a memory breakpoint that we
3917 know is set in a dynamic object that is marked
3918 shlib_disabled, then either the dynamic object was
3919 removed with "remove-symbol-file" or with
3920 "nosharedlibrary". In the former case, we don't know
3921 whether another dynamic object might have loaded over the
3922 breakpoint's address -- the user might well let us know
3923 about it next with add-symbol-file (the whole point of
d03de421 3924 add-symbol-file is letting the user manually maintain a
08351840
PA
3925 list of dynamically loaded objects). If we have the
3926 breakpoint's shadow memory, that is, this is a software
3927 breakpoint managed by GDB, check whether the breakpoint
3928 is still inserted in memory, to avoid overwriting wrong
3929 code with stale saved shadow contents. Note that HW
3930 breakpoints don't have shadow memory, as they're
3931 implemented using a mechanism that is not dependent on
3932 being able to modify the target's memory, and as such
3933 they should always be removed. */
3934 if (bl->shlib_disabled
3935 && bl->target_info.shadow_len != 0
3936 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3937 val = 0;
3938 else
3939 val = bl->owner->ops->remove_location (bl);
c02f5703 3940 }
c906108c
SS
3941 else
3942 {
4a64f543 3943 /* This breakpoint is in an overlay section.
c02f5703
MS
3944 Did we set a breakpoint at the LMA? */
3945 if (!overlay_events_enabled)
3946 {
3947 /* Yes -- overlay event support is not active, so we
3948 should have set a breakpoint at the LMA. Remove it.
3949 */
c02f5703
MS
3950 /* Ignore any failures: if the LMA is in ROM, we will
3951 have already warned when we failed to insert it. */
35df4500
TJB
3952 if (bl->loc_type == bp_loc_hardware_breakpoint)
3953 target_remove_hw_breakpoint (bl->gdbarch,
3954 &bl->overlay_target_info);
c02f5703 3955 else
35df4500
TJB
3956 target_remove_breakpoint (bl->gdbarch,
3957 &bl->overlay_target_info);
c02f5703
MS
3958 }
3959 /* Did we set a breakpoint at the VMA?
3960 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3961 if (bl->inserted)
c906108c 3962 {
c02f5703
MS
3963 /* Yes -- remove it. Previously we did not bother to
3964 remove the breakpoint if the section had been
3965 unmapped, but let's not rely on that being safe. We
3966 don't know what the overlay manager might do. */
aa67235e
UW
3967
3968 /* However, we should remove *software* breakpoints only
3969 if the section is still mapped, or else we overwrite
3970 wrong code with the saved shadow contents. */
348d480f
PA
3971 if (bl->loc_type == bp_loc_hardware_breakpoint
3972 || section_is_mapped (bl->section))
3973 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3974 else
3975 val = 0;
c906108c 3976 }
c02f5703
MS
3977 else
3978 {
3979 /* No -- not inserted, so no need to remove. No error. */
3980 val = 0;
3981 }
c906108c 3982 }
879d1e6b 3983
08351840
PA
3984 /* In some cases, we might not be able to remove a breakpoint in
3985 a shared library that has already been removed, but we have
3986 not yet processed the shlib unload event. Similarly for an
3987 unloaded add-symbol-file object - the user might not yet have
3988 had the chance to remove-symbol-file it. shlib_disabled will
3989 be set if the library/object has already been removed, but
3990 the breakpoint hasn't been uninserted yet, e.g., after
3991 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3992 always-inserted mode. */
076855f9 3993 if (val
08351840
PA
3994 && (bl->loc_type == bp_loc_software_breakpoint
3995 && (bl->shlib_disabled
3996 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3997 || shared_objfile_contains_address_p (bl->pspace,
3998 bl->address))))
879d1e6b
UW
3999 val = 0;
4000
c906108c
SS
4001 if (val)
4002 return val;
35df4500 4003 bl->inserted = (is == mark_inserted);
c906108c 4004 }
35df4500 4005 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4006 {
77b06cd7
TJB
4007 gdb_assert (bl->owner->ops != NULL
4008 && bl->owner->ops->remove_location != NULL);
4009
35df4500 4010 bl->inserted = (is == mark_inserted);
77b06cd7 4011 bl->owner->ops->remove_location (bl);
2e70b7b9 4012
c906108c 4013 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 4014 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4015 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4016 bl->owner->number);
c906108c 4017 }
35df4500
TJB
4018 else if (bl->owner->type == bp_catchpoint
4019 && breakpoint_enabled (bl->owner)
4020 && !bl->duplicate)
ce78b96d 4021 {
77b06cd7
TJB
4022 gdb_assert (bl->owner->ops != NULL
4023 && bl->owner->ops->remove_location != NULL);
ce78b96d 4024
77b06cd7 4025 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4026 if (val)
4027 return val;
77b06cd7 4028
35df4500 4029 bl->inserted = (is == mark_inserted);
ce78b96d 4030 }
c906108c
SS
4031
4032 return 0;
4033}
4034
6c95b8df 4035static int
35df4500 4036remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4037{
4038 int ret;
4039 struct cleanup *old_chain;
4040
35df4500
TJB
4041 /* BL is never in moribund_locations by our callers. */
4042 gdb_assert (bl->owner != NULL);
2bdf28a0 4043
1a853c52 4044 if (bl->permanent)
6c95b8df
PA
4045 /* Permanent breakpoints cannot be inserted or removed. */
4046 return 0;
4047
4048 /* The type of none suggests that owner is actually deleted.
4049 This should not ever happen. */
35df4500 4050 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4051
4052 old_chain = save_current_space_and_thread ();
4053
35df4500 4054 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4055
35df4500 4056 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4057
4058 do_cleanups (old_chain);
4059 return ret;
4060}
4061
c906108c
SS
4062/* Clear the "inserted" flag in all breakpoints. */
4063
25b22b0a 4064void
fba45db2 4065mark_breakpoints_out (void)
c906108c 4066{
35df4500 4067 struct bp_location *bl, **blp_tmp;
c906108c 4068
35df4500 4069 ALL_BP_LOCATIONS (bl, blp_tmp)
1a853c52
PA
4070 if (bl->pspace == current_program_space
4071 && !bl->permanent)
35df4500 4072 bl->inserted = 0;
c906108c
SS
4073}
4074
53a5351d
JM
4075/* Clear the "inserted" flag in all breakpoints and delete any
4076 breakpoints which should go away between runs of the program.
c906108c
SS
4077
4078 Plus other such housekeeping that has to be done for breakpoints
4079 between runs.
4080
53a5351d
JM
4081 Note: this function gets called at the end of a run (by
4082 generic_mourn_inferior) and when a run begins (by
4a64f543 4083 init_wait_for_inferior). */
c906108c
SS
4084
4085
4086
4087void
fba45db2 4088breakpoint_init_inferior (enum inf_context context)
c906108c 4089{
35df4500
TJB
4090 struct breakpoint *b, *b_tmp;
4091 struct bp_location *bl, **blp_tmp;
1c5cfe86 4092 int ix;
6c95b8df 4093 struct program_space *pspace = current_program_space;
c906108c 4094
50c71eaf
PA
4095 /* If breakpoint locations are shared across processes, then there's
4096 nothing to do. */
f5656ead 4097 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4098 return;
4099
1a853c52 4100 mark_breakpoints_out ();
075f6582 4101
35df4500 4102 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4103 {
6c95b8df
PA
4104 if (b->loc && b->loc->pspace != pspace)
4105 continue;
4106
c5aa993b
JM
4107 switch (b->type)
4108 {
4109 case bp_call_dummy:
e2e4d78b 4110 case bp_longjmp_call_dummy:
c906108c 4111
c5aa993b 4112 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4113 cause problems when the inferior is rerun, so we better get
4114 rid of it. */
4115
4116 case bp_watchpoint_scope:
4117
4118 /* Also get rid of scope breakpoints. */
4119
4120 case bp_shlib_event:
4121
4122 /* Also remove solib event breakpoints. Their addresses may
4123 have changed since the last time we ran the program.
4124 Actually we may now be debugging against different target;
4125 and so the solib backend that installed this breakpoint may
4126 not be used in by the target. E.g.,
4127
4128 (gdb) file prog-linux
4129 (gdb) run # native linux target
4130 ...
4131 (gdb) kill
4132 (gdb) file prog-win.exe
4133 (gdb) tar rem :9999 # remote Windows gdbserver.
4134 */
c906108c 4135
f59f708a
PA
4136 case bp_step_resume:
4137
4138 /* Also remove step-resume breakpoints. */
4139
7c16b83e
PA
4140 case bp_single_step:
4141
4142 /* Also remove single-step breakpoints. */
4143
c5aa993b
JM
4144 delete_breakpoint (b);
4145 break;
c906108c 4146
c5aa993b
JM
4147 case bp_watchpoint:
4148 case bp_hardware_watchpoint:
4149 case bp_read_watchpoint:
4150 case bp_access_watchpoint:
3a5c3e22
PA
4151 {
4152 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4153
3a5c3e22
PA
4154 /* Likewise for watchpoints on local expressions. */
4155 if (w->exp_valid_block != NULL)
4156 delete_breakpoint (b);
4157 else if (context == inf_starting)
4158 {
4159 /* Reset val field to force reread of starting value in
4160 insert_breakpoints. */
4161 if (w->val)
4162 value_free (w->val);
4163 w->val = NULL;
4164 w->val_valid = 0;
c860120c 4165 }
3a5c3e22 4166 }
c5aa993b
JM
4167 break;
4168 default:
c5aa993b
JM
4169 break;
4170 }
4171 }
1c5cfe86
PA
4172
4173 /* Get rid of the moribund locations. */
35df4500
TJB
4174 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4175 decref_bp_location (&bl);
1c5cfe86 4176 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4177}
4178
6c95b8df
PA
4179/* These functions concern about actual breakpoints inserted in the
4180 target --- to e.g. check if we need to do decr_pc adjustment or if
4181 we need to hop over the bkpt --- so we check for address space
4182 match, not program space. */
4183
c2c6d25f
JM
4184/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4185 exists at PC. It returns ordinary_breakpoint_here if it's an
4186 ordinary breakpoint, or permanent_breakpoint_here if it's a
4187 permanent breakpoint.
4188 - When continuing from a location with an ordinary breakpoint, we
4189 actually single step once before calling insert_breakpoints.
e5dd4106 4190 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4191 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4192 the target, to advance the PC past the breakpoint. */
c906108c 4193
c2c6d25f 4194enum breakpoint_here
6c95b8df 4195breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4196{
35df4500 4197 struct bp_location *bl, **blp_tmp;
c2c6d25f 4198 int any_breakpoint_here = 0;
c906108c 4199
35df4500 4200 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4201 {
35df4500
TJB
4202 if (bl->loc_type != bp_loc_software_breakpoint
4203 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4204 continue;
4205
f1310107 4206 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4207 if ((breakpoint_enabled (bl->owner)
1a853c52 4208 || bl->permanent)
f1310107 4209 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4210 {
4211 if (overlay_debugging
35df4500
TJB
4212 && section_is_overlay (bl->section)
4213 && !section_is_mapped (bl->section))
075f6582 4214 continue; /* unmapped overlay -- can't be a match */
1a853c52 4215 else if (bl->permanent)
075f6582
DJ
4216 return permanent_breakpoint_here;
4217 else
4218 any_breakpoint_here = 1;
4219 }
4220 }
c906108c 4221
c2c6d25f 4222 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
4223}
4224
1c5cfe86
PA
4225/* Return true if there's a moribund breakpoint at PC. */
4226
4227int
6c95b8df 4228moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4229{
4230 struct bp_location *loc;
4231 int ix;
4232
4233 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4234 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4235 return 1;
4236
4237 return 0;
4238}
c2c6d25f 4239
f7ce857f
PA
4240/* Returns non-zero iff BL is inserted at PC, in address space
4241 ASPACE. */
4242
4243static int
4244bp_location_inserted_here_p (struct bp_location *bl,
4245 struct address_space *aspace, CORE_ADDR pc)
4246{
4247 if (bl->inserted
4248 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4249 aspace, pc))
4250 {
4251 if (overlay_debugging
4252 && section_is_overlay (bl->section)
4253 && !section_is_mapped (bl->section))
4254 return 0; /* unmapped overlay -- can't be a match */
4255 else
4256 return 1;
4257 }
4258 return 0;
4259}
4260
a1fd2fa5 4261/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4262
4263int
a1fd2fa5 4264breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4265{
f7ce857f
PA
4266 struct bp_location **blp, **blp_tmp = NULL;
4267 struct bp_location *bl;
c906108c 4268
f7ce857f 4269 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4270 {
f7ce857f
PA
4271 struct bp_location *bl = *blp;
4272
35df4500
TJB
4273 if (bl->loc_type != bp_loc_software_breakpoint
4274 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4275 continue;
4276
f7ce857f
PA
4277 if (bp_location_inserted_here_p (bl, aspace, pc))
4278 return 1;
c5aa993b 4279 }
c36b740a
VP
4280 return 0;
4281}
4282
a1fd2fa5
PA
4283/* This function returns non-zero iff there is a software breakpoint
4284 inserted at PC. */
c36b740a
VP
4285
4286int
a1fd2fa5
PA
4287software_breakpoint_inserted_here_p (struct address_space *aspace,
4288 CORE_ADDR pc)
4fa8626c 4289{
f7ce857f
PA
4290 struct bp_location **blp, **blp_tmp = NULL;
4291 struct bp_location *bl;
4fa8626c 4292
f7ce857f 4293 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4294 {
f7ce857f
PA
4295 struct bp_location *bl = *blp;
4296
35df4500 4297 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4298 continue;
4299
f7ce857f
PA
4300 if (bp_location_inserted_here_p (bl, aspace, pc))
4301 return 1;
4fa8626c
DJ
4302 }
4303
4304 return 0;
9c02b525
PA
4305}
4306
4307/* See breakpoint.h. */
4308
4309int
4310hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4311 CORE_ADDR pc)
4312{
4313 struct bp_location **blp, **blp_tmp = NULL;
4314 struct bp_location *bl;
4315
4316 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4317 {
4318 struct bp_location *bl = *blp;
4319
4320 if (bl->loc_type != bp_loc_hardware_breakpoint)
4321 continue;
4322
4323 if (bp_location_inserted_here_p (bl, aspace, pc))
4324 return 1;
4325 }
4326
4327 return 0;
4fa8626c
DJ
4328}
4329
9093389c
PA
4330int
4331hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4332 CORE_ADDR addr, ULONGEST len)
4333{
4334 struct breakpoint *bpt;
4335
4336 ALL_BREAKPOINTS (bpt)
4337 {
4338 struct bp_location *loc;
4339
4340 if (bpt->type != bp_hardware_watchpoint
4341 && bpt->type != bp_access_watchpoint)
4342 continue;
4343
4344 if (!breakpoint_enabled (bpt))
4345 continue;
4346
4347 for (loc = bpt->loc; loc; loc = loc->next)
4348 if (loc->pspace->aspace == aspace && loc->inserted)
4349 {
4350 CORE_ADDR l, h;
4351
4352 /* Check for intersection. */
4353 l = max (loc->address, addr);
4354 h = min (loc->address + loc->length, addr + len);
4355 if (l < h)
4356 return 1;
4357 }
4358 }
4359 return 0;
4360}
c906108c 4361\f
c5aa993b 4362
c906108c
SS
4363/* bpstat stuff. External routines' interfaces are documented
4364 in breakpoint.h. */
4365
4366int
c326b90e 4367is_catchpoint (struct breakpoint *ep)
c906108c 4368{
533be4dd 4369 return (ep->type == bp_catchpoint);
c906108c
SS
4370}
4371
f431efe5
PA
4372/* Frees any storage that is part of a bpstat. Does not walk the
4373 'next' chain. */
4374
4375static void
198757a8
VP
4376bpstat_free (bpstat bs)
4377{
4378 if (bs->old_val != NULL)
4379 value_free (bs->old_val);
9add0f1b 4380 decref_counted_command_line (&bs->commands);
f431efe5 4381 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4382 xfree (bs);
4383}
4384
c906108c
SS
4385/* Clear a bpstat so that it says we are not at any breakpoint.
4386 Also free any storage that is part of a bpstat. */
4387
4388void
fba45db2 4389bpstat_clear (bpstat *bsp)
c906108c
SS
4390{
4391 bpstat p;
4392 bpstat q;
4393
4394 if (bsp == 0)
4395 return;
4396 p = *bsp;
4397 while (p != NULL)
4398 {
4399 q = p->next;
198757a8 4400 bpstat_free (p);
c906108c
SS
4401 p = q;
4402 }
4403 *bsp = NULL;
4404}
4405
4406/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4407 is part of the bpstat is copied as well. */
4408
4409bpstat
fba45db2 4410bpstat_copy (bpstat bs)
c906108c
SS
4411{
4412 bpstat p = NULL;
4413 bpstat tmp;
4414 bpstat retval = NULL;
4415
4416 if (bs == NULL)
4417 return bs;
4418
4419 for (; bs != NULL; bs = bs->next)
4420 {
4421 tmp = (bpstat) xmalloc (sizeof (*tmp));
4422 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4423 incref_counted_command_line (tmp->commands);
f431efe5 4424 incref_bp_location (tmp->bp_location_at);
31cc81e9 4425 if (bs->old_val != NULL)
3c3185ac
JK
4426 {
4427 tmp->old_val = value_copy (bs->old_val);
4428 release_value (tmp->old_val);
4429 }
31cc81e9 4430
c906108c
SS
4431 if (p == NULL)
4432 /* This is the first thing in the chain. */
4433 retval = tmp;
4434 else
4435 p->next = tmp;
4436 p = tmp;
4437 }
4438 p->next = NULL;
4439 return retval;
4440}
4441
4a64f543 4442/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4443
4444bpstat
fba45db2 4445bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4446{
c5aa993b
JM
4447 if (bsp == NULL)
4448 return NULL;
c906108c 4449
c5aa993b
JM
4450 for (; bsp != NULL; bsp = bsp->next)
4451 {
f431efe5 4452 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4453 return bsp;
4454 }
c906108c
SS
4455 return NULL;
4456}
4457
ab04a2af
TT
4458/* See breakpoint.h. */
4459
47591c29 4460int
427cd150 4461bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4462{
ab04a2af
TT
4463 for (; bsp != NULL; bsp = bsp->next)
4464 {
427cd150
TT
4465 if (bsp->breakpoint_at == NULL)
4466 {
4467 /* A moribund location can never explain a signal other than
4468 GDB_SIGNAL_TRAP. */
4469 if (sig == GDB_SIGNAL_TRAP)
47591c29 4470 return 1;
427cd150
TT
4471 }
4472 else
47591c29
PA
4473 {
4474 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4475 sig))
4476 return 1;
4477 }
ab04a2af
TT
4478 }
4479
47591c29 4480 return 0;
ab04a2af
TT
4481}
4482
4a64f543
MS
4483/* Put in *NUM the breakpoint number of the first breakpoint we are
4484 stopped at. *BSP upon return is a bpstat which points to the
4485 remaining breakpoints stopped at (but which is not guaranteed to be
4486 good for anything but further calls to bpstat_num).
4487
8671a17b
PA
4488 Return 0 if passed a bpstat which does not indicate any breakpoints.
4489 Return -1 if stopped at a breakpoint that has been deleted since
4490 we set it.
4491 Return 1 otherwise. */
c906108c
SS
4492
4493int
8671a17b 4494bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4495{
4496 struct breakpoint *b;
4497
4498 if ((*bsp) == NULL)
4499 return 0; /* No more breakpoint values */
8671a17b 4500
4a64f543
MS
4501 /* We assume we'll never have several bpstats that correspond to a
4502 single breakpoint -- otherwise, this function might return the
4503 same number more than once and this will look ugly. */
f431efe5 4504 b = (*bsp)->breakpoint_at;
8671a17b
PA
4505 *bsp = (*bsp)->next;
4506 if (b == NULL)
4507 return -1; /* breakpoint that's been deleted since */
4508
4509 *num = b->number; /* We have its number */
4510 return 1;
c906108c
SS
4511}
4512
e93ca019 4513/* See breakpoint.h. */
c906108c
SS
4514
4515void
e93ca019 4516bpstat_clear_actions (void)
c906108c 4517{
e93ca019
JK
4518 struct thread_info *tp;
4519 bpstat bs;
4520
4521 if (ptid_equal (inferior_ptid, null_ptid))
4522 return;
4523
4524 tp = find_thread_ptid (inferior_ptid);
4525 if (tp == NULL)
4526 return;
4527
4528 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4529 {
9add0f1b 4530 decref_counted_command_line (&bs->commands);
abf85f46 4531
c906108c
SS
4532 if (bs->old_val != NULL)
4533 {
4534 value_free (bs->old_val);
4535 bs->old_val = NULL;
4536 }
4537 }
4538}
4539
f3b1572e
PA
4540/* Called when a command is about to proceed the inferior. */
4541
4542static void
4543breakpoint_about_to_proceed (void)
4544{
4545 if (!ptid_equal (inferior_ptid, null_ptid))
4546 {
4547 struct thread_info *tp = inferior_thread ();
4548
4549 /* Allow inferior function calls in breakpoint commands to not
4550 interrupt the command list. When the call finishes
4551 successfully, the inferior will be standing at the same
4552 breakpoint as if nothing happened. */
16c381f0 4553 if (tp->control.in_infcall)
f3b1572e
PA
4554 return;
4555 }
4556
4557 breakpoint_proceeded = 1;
4558}
4559
4a64f543
MS
4560/* Stub for cleaning up our state if we error-out of a breakpoint
4561 command. */
c906108c 4562static void
4efb68b1 4563cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4564{
4565 executing_breakpoint_commands = 0;
4566}
4567
abf85f46
JK
4568/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4569 or its equivalent. */
4570
4571static int
4572command_line_is_silent (struct command_line *cmd)
4573{
4574 return cmd && (strcmp ("silent", cmd->line) == 0
4575 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4576}
4577
4a64f543
MS
4578/* Execute all the commands associated with all the breakpoints at
4579 this location. Any of these commands could cause the process to
4580 proceed beyond this point, etc. We look out for such changes by
4581 checking the global "breakpoint_proceeded" after each command.
c906108c 4582
347bddb7
PA
4583 Returns true if a breakpoint command resumed the inferior. In that
4584 case, it is the caller's responsibility to recall it again with the
4585 bpstat of the current thread. */
4586
4587static int
4588bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4589{
4590 bpstat bs;
4591 struct cleanup *old_chain;
347bddb7 4592 int again = 0;
c906108c
SS
4593
4594 /* Avoid endless recursion if a `source' command is contained
4595 in bs->commands. */
4596 if (executing_breakpoint_commands)
347bddb7 4597 return 0;
c906108c
SS
4598
4599 executing_breakpoint_commands = 1;
4600 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4601
cf6c5ffb
TT
4602 prevent_dont_repeat ();
4603
4a64f543 4604 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4605 bs = *bsp;
4606
4607 breakpoint_proceeded = 0;
4608 for (; bs != NULL; bs = bs->next)
4609 {
9add0f1b 4610 struct counted_command_line *ccmd;
6c50ab1c
JB
4611 struct command_line *cmd;
4612 struct cleanup *this_cmd_tree_chain;
4613
4614 /* Take ownership of the BSP's command tree, if it has one.
4615
4616 The command tree could legitimately contain commands like
4617 'step' and 'next', which call clear_proceed_status, which
4618 frees stop_bpstat's command tree. To make sure this doesn't
4619 free the tree we're executing out from under us, we need to
4620 take ownership of the tree ourselves. Since a given bpstat's
4621 commands are only executed once, we don't need to copy it; we
4622 can clear the pointer in the bpstat, and make sure we free
4623 the tree when we're done. */
9add0f1b
TT
4624 ccmd = bs->commands;
4625 bs->commands = NULL;
abf85f46
JK
4626 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4627 cmd = ccmd ? ccmd->commands : NULL;
4628 if (command_line_is_silent (cmd))
4629 {
4630 /* The action has been already done by bpstat_stop_status. */
4631 cmd = cmd->next;
4632 }
6c50ab1c 4633
c906108c
SS
4634 while (cmd != NULL)
4635 {
4636 execute_control_command (cmd);
4637
4638 if (breakpoint_proceeded)
4639 break;
4640 else
4641 cmd = cmd->next;
4642 }
6c50ab1c
JB
4643
4644 /* We can free this command tree now. */
4645 do_cleanups (this_cmd_tree_chain);
4646
c906108c 4647 if (breakpoint_proceeded)
32c1e744 4648 {
5589af0e 4649 if (interpreter_async && target_can_async_p ())
347bddb7
PA
4650 /* If we are in async mode, then the target might be still
4651 running, not stopped at any breakpoint, so nothing for
4652 us to do here -- just return to the event loop. */
4653 ;
32c1e744
VP
4654 else
4655 /* In sync mode, when execute_control_command returns
4656 we're already standing on the next breakpoint.
347bddb7
PA
4657 Breakpoint commands for that stop were not run, since
4658 execute_command does not run breakpoint commands --
4659 only command_line_handler does, but that one is not
4660 involved in execution of breakpoint commands. So, we
4661 can now execute breakpoint commands. It should be
4662 noted that making execute_command do bpstat actions is
4663 not an option -- in this case we'll have recursive
4664 invocation of bpstat for each breakpoint with a
4665 command, and can easily blow up GDB stack. Instead, we
4666 return true, which will trigger the caller to recall us
4667 with the new stop_bpstat. */
4668 again = 1;
4669 break;
32c1e744 4670 }
c906108c 4671 }
c2b8ed2c 4672 do_cleanups (old_chain);
347bddb7
PA
4673 return again;
4674}
4675
4676void
4677bpstat_do_actions (void)
4678{
353d1d73
JK
4679 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4680
347bddb7
PA
4681 /* Do any commands attached to breakpoint we are stopped at. */
4682 while (!ptid_equal (inferior_ptid, null_ptid)
4683 && target_has_execution
4684 && !is_exited (inferior_ptid)
4685 && !is_executing (inferior_ptid))
4686 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4687 and only return when it is stopped at the next breakpoint, we
4688 keep doing breakpoint actions until it returns false to
4689 indicate the inferior was not resumed. */
16c381f0 4690 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4691 break;
353d1d73
JK
4692
4693 discard_cleanups (cleanup_if_error);
c906108c
SS
4694}
4695
fa4727a6
DJ
4696/* Print out the (old or new) value associated with a watchpoint. */
4697
4698static void
4699watchpoint_value_print (struct value *val, struct ui_file *stream)
4700{
4701 if (val == NULL)
4702 fprintf_unfiltered (stream, _("<unreadable>"));
4703 else
79a45b7d
TT
4704 {
4705 struct value_print_options opts;
4706 get_user_print_options (&opts);
4707 value_print (val, stream, &opts);
4708 }
fa4727a6
DJ
4709}
4710
e514a9d6 4711/* Generic routine for printing messages indicating why we
4a64f543 4712 stopped. The behavior of this function depends on the value
e514a9d6
JM
4713 'print_it' in the bpstat structure. Under some circumstances we
4714 may decide not to print anything here and delegate the task to
4a64f543 4715 normal_stop(). */
e514a9d6
JM
4716
4717static enum print_stop_action
4718print_bp_stop_message (bpstat bs)
4719{
4720 switch (bs->print_it)
4721 {
4722 case print_it_noop:
4a64f543 4723 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4724 return PRINT_UNKNOWN;
4725 break;
4726
4727 case print_it_done:
4728 /* We still want to print the frame, but we already printed the
4a64f543 4729 relevant messages. */
e514a9d6
JM
4730 return PRINT_SRC_AND_LOC;
4731 break;
4732
4733 case print_it_normal:
4f8d1dc6 4734 {
f431efe5
PA
4735 struct breakpoint *b = bs->breakpoint_at;
4736
1a6a67de
TJB
4737 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4738 which has since been deleted. */
4739 if (b == NULL)
4740 return PRINT_UNKNOWN;
4741
348d480f
PA
4742 /* Normal case. Call the breakpoint's print_it method. */
4743 return b->ops->print_it (bs);
4f8d1dc6 4744 }
348d480f 4745 break;
3086aeae 4746
e514a9d6 4747 default:
8e65ff28 4748 internal_error (__FILE__, __LINE__,
e2e0b3e5 4749 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4750 break;
c906108c 4751 }
c906108c
SS
4752}
4753
edcc5120
TT
4754/* A helper function that prints a shared library stopped event. */
4755
4756static void
4757print_solib_event (int is_catchpoint)
4758{
4759 int any_deleted
4760 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4761 int any_added
4762 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4763
4764 if (!is_catchpoint)
4765 {
4766 if (any_added || any_deleted)
4767 ui_out_text (current_uiout,
4768 _("Stopped due to shared library event:\n"));
4769 else
4770 ui_out_text (current_uiout,
4771 _("Stopped due to shared library event (no "
4772 "libraries added or removed)\n"));
4773 }
4774
4775 if (ui_out_is_mi_like_p (current_uiout))
4776 ui_out_field_string (current_uiout, "reason",
4777 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4778
4779 if (any_deleted)
4780 {
4781 struct cleanup *cleanup;
4782 char *name;
4783 int ix;
4784
4785 ui_out_text (current_uiout, _(" Inferior unloaded "));
4786 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4787 "removed");
4788 for (ix = 0;
4789 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4790 ix, name);
4791 ++ix)
4792 {
4793 if (ix > 0)
4794 ui_out_text (current_uiout, " ");
4795 ui_out_field_string (current_uiout, "library", name);
4796 ui_out_text (current_uiout, "\n");
4797 }
4798
4799 do_cleanups (cleanup);
4800 }
4801
4802 if (any_added)
4803 {
4804 struct so_list *iter;
4805 int ix;
4806 struct cleanup *cleanup;
4807
4808 ui_out_text (current_uiout, _(" Inferior loaded "));
4809 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4810 "added");
4811 for (ix = 0;
4812 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4813 ix, iter);
4814 ++ix)
4815 {
4816 if (ix > 0)
4817 ui_out_text (current_uiout, " ");
4818 ui_out_field_string (current_uiout, "library", iter->so_name);
4819 ui_out_text (current_uiout, "\n");
4820 }
4821
4822 do_cleanups (cleanup);
4823 }
4824}
4825
e514a9d6
JM
4826/* Print a message indicating what happened. This is called from
4827 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4828 list - a list of the eventpoints that caused this stop. KIND is
4829 the target_waitkind for the stopping event. This
e514a9d6
JM
4830 routine calls the generic print routine for printing a message
4831 about reasons for stopping. This will print (for example) the
4832 "Breakpoint n," part of the output. The return value of this
4833 routine is one of:
c906108c 4834
4a64f543 4835 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4836 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4837 code to print the location. An example is
c5aa993b
JM
4838 "Breakpoint 1, " which should be followed by
4839 the location.
917317f4 4840 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4841 to also print the location part of the message.
4842 An example is the catch/throw messages, which
4a64f543 4843 don't require a location appended to the end.
917317f4 4844 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4845 further info to be printed. */
c906108c 4846
917317f4 4847enum print_stop_action
36dfb11c 4848bpstat_print (bpstat bs, int kind)
c906108c
SS
4849{
4850 int val;
c5aa993b 4851
c906108c 4852 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4853 (Currently all watchpoints go on the bpstat whether hit or not.
4854 That probably could (should) be changed, provided care is taken
c906108c 4855 with respect to bpstat_explains_signal). */
e514a9d6
JM
4856 for (; bs; bs = bs->next)
4857 {
4858 val = print_bp_stop_message (bs);
4859 if (val == PRINT_SRC_ONLY
4860 || val == PRINT_SRC_AND_LOC
4861 || val == PRINT_NOTHING)
4862 return val;
4863 }
c906108c 4864
36dfb11c
TT
4865 /* If we had hit a shared library event breakpoint,
4866 print_bp_stop_message would print out this message. If we hit an
4867 OS-level shared library event, do the same thing. */
4868 if (kind == TARGET_WAITKIND_LOADED)
4869 {
edcc5120 4870 print_solib_event (0);
36dfb11c
TT
4871 return PRINT_NOTHING;
4872 }
4873
e514a9d6 4874 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4875 with and nothing was printed. */
917317f4 4876 return PRINT_UNKNOWN;
c906108c
SS
4877}
4878
c42bd95a
DE
4879/* Evaluate the expression EXP and return 1 if value is zero.
4880 This returns the inverse of the condition because it is called
4881 from catch_errors which returns 0 if an exception happened, and if an
4882 exception happens we want execution to stop.
4a64f543 4883 The argument is a "struct expression *" that has been cast to a
c42bd95a 4884 "void *" to make it pass through catch_errors. */
c906108c
SS
4885
4886static int
4efb68b1 4887breakpoint_cond_eval (void *exp)
c906108c 4888{
278cd55f 4889 struct value *mark = value_mark ();
c5aa993b 4890 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4891
c906108c
SS
4892 value_free_to_mark (mark);
4893 return i;
4894}
4895
5760d0ab 4896/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4897
4898static bpstat
5760d0ab 4899bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4900{
4901 bpstat bs;
4902
4903 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4904 bs->next = NULL;
4905 **bs_link_pointer = bs;
4906 *bs_link_pointer = &bs->next;
f431efe5
PA
4907 bs->breakpoint_at = bl->owner;
4908 bs->bp_location_at = bl;
4909 incref_bp_location (bl);
c906108c
SS
4910 /* If the condition is false, etc., don't do the commands. */
4911 bs->commands = NULL;
4912 bs->old_val = NULL;
4913 bs->print_it = print_it_normal;
4914 return bs;
4915}
4916\f
d983da9c
DJ
4917/* The target has stopped with waitstatus WS. Check if any hardware
4918 watchpoints have triggered, according to the target. */
4919
4920int
4921watchpoints_triggered (struct target_waitstatus *ws)
4922{
d92524f1 4923 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4924 CORE_ADDR addr;
4925 struct breakpoint *b;
4926
4927 if (!stopped_by_watchpoint)
4928 {
4929 /* We were not stopped by a watchpoint. Mark all watchpoints
4930 as not triggered. */
4931 ALL_BREAKPOINTS (b)
cc60f2e3 4932 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4933 {
4934 struct watchpoint *w = (struct watchpoint *) b;
4935
4936 w->watchpoint_triggered = watch_triggered_no;
4937 }
d983da9c
DJ
4938
4939 return 0;
4940 }
4941
4942 if (!target_stopped_data_address (&current_target, &addr))
4943 {
4944 /* We were stopped by a watchpoint, but we don't know where.
4945 Mark all watchpoints as unknown. */
4946 ALL_BREAKPOINTS (b)
cc60f2e3 4947 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4948 {
4949 struct watchpoint *w = (struct watchpoint *) b;
4950
4951 w->watchpoint_triggered = watch_triggered_unknown;
4952 }
d983da9c 4953
3c4797ba 4954 return 1;
d983da9c
DJ
4955 }
4956
4957 /* The target could report the data address. Mark watchpoints
4958 affected by this data address as triggered, and all others as not
4959 triggered. */
4960
4961 ALL_BREAKPOINTS (b)
cc60f2e3 4962 if (is_hardware_watchpoint (b))
d983da9c 4963 {
3a5c3e22 4964 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4965 struct bp_location *loc;
d983da9c 4966
3a5c3e22 4967 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4968 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4969 {
3a5c3e22 4970 if (is_masked_watchpoint (b))
9c06b0b4 4971 {
3a5c3e22
PA
4972 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4973 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4974
4975 if (newaddr == start)
4976 {
3a5c3e22 4977 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4978 break;
4979 }
4980 }
4981 /* Exact match not required. Within range is sufficient. */
4982 else if (target_watchpoint_addr_within_range (&current_target,
4983 addr, loc->address,
4984 loc->length))
4985 {
3a5c3e22 4986 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4987 break;
4988 }
4989 }
d983da9c
DJ
4990 }
4991
4992 return 1;
4993}
4994
c906108c
SS
4995/* Possible return values for watchpoint_check (this can't be an enum
4996 because of check_errors). */
4997/* The watchpoint has been deleted. */
4998#define WP_DELETED 1
4999/* The value has changed. */
5000#define WP_VALUE_CHANGED 2
5001/* The value has not changed. */
5002#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5003/* Ignore this watchpoint, no matter if the value changed or not. */
5004#define WP_IGNORE 4
c906108c
SS
5005
5006#define BP_TEMPFLAG 1
5007#define BP_HARDWAREFLAG 2
5008
4a64f543
MS
5009/* Evaluate watchpoint condition expression and check if its value
5010 changed.
553e4c11
JB
5011
5012 P should be a pointer to struct bpstat, but is defined as a void *
5013 in order for this function to be usable with catch_errors. */
c906108c
SS
5014
5015static int
4efb68b1 5016watchpoint_check (void *p)
c906108c
SS
5017{
5018 bpstat bs = (bpstat) p;
3a5c3e22 5019 struct watchpoint *b;
c906108c
SS
5020 struct frame_info *fr;
5021 int within_current_scope;
5022
f431efe5 5023 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5024 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5025 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5026
f6bc2008
PA
5027 /* If this is a local watchpoint, we only want to check if the
5028 watchpoint frame is in scope if the current thread is the thread
5029 that was used to create the watchpoint. */
5030 if (!watchpoint_in_thread_scope (b))
60e1c644 5031 return WP_IGNORE;
f6bc2008 5032
c906108c
SS
5033 if (b->exp_valid_block == NULL)
5034 within_current_scope = 1;
5035 else
5036 {
edb3359d
DJ
5037 struct frame_info *frame = get_current_frame ();
5038 struct gdbarch *frame_arch = get_frame_arch (frame);
5039 CORE_ADDR frame_pc = get_frame_pc (frame);
5040
4a64f543
MS
5041 /* in_function_epilogue_p() returns a non-zero value if we're
5042 still in the function but the stack frame has already been
5043 invalidated. Since we can't rely on the values of local
5044 variables after the stack has been destroyed, we are treating
5045 the watchpoint in that state as `not changed' without further
5046 checking. Don't mark watchpoints as changed if the current
5047 frame is in an epilogue - even if they are in some other
5048 frame, our view of the stack is likely to be wrong and
5049 frame_find_by_id could error out. */
a0f49112 5050 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 5051 return WP_IGNORE;
a0f49112 5052
101dcfbe 5053 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5054 within_current_scope = (fr != NULL);
69fbadd5
DJ
5055
5056 /* If we've gotten confused in the unwinder, we might have
5057 returned a frame that can't describe this variable. */
edb3359d
DJ
5058 if (within_current_scope)
5059 {
5060 struct symbol *function;
5061
5062 function = get_frame_function (fr);
5063 if (function == NULL
5064 || !contained_in (b->exp_valid_block,
5065 SYMBOL_BLOCK_VALUE (function)))
5066 within_current_scope = 0;
5067 }
69fbadd5 5068
edb3359d 5069 if (within_current_scope)
c906108c
SS
5070 /* If we end up stopping, the current frame will get selected
5071 in normal_stop. So this call to select_frame won't affect
5072 the user. */
0f7d239c 5073 select_frame (fr);
c906108c 5074 }
c5aa993b 5075
c906108c
SS
5076 if (within_current_scope)
5077 {
4a64f543
MS
5078 /* We use value_{,free_to_}mark because it could be a *long*
5079 time before we return to the command level and call
5080 free_all_values. We can't call free_all_values because we
5081 might be in the middle of evaluating a function call. */
c906108c 5082
0cf6dd15 5083 int pc = 0;
9c06b0b4 5084 struct value *mark;
fa4727a6
DJ
5085 struct value *new_val;
5086
3a5c3e22 5087 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5088 /* Since we don't know the exact trigger address (from
5089 stopped_data_address), just tell the user we've triggered
5090 a mask watchpoint. */
5091 return WP_VALUE_CHANGED;
5092
5093 mark = value_mark ();
3a1115a0 5094 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5095
bb9d5f81
PP
5096 if (b->val_bitsize != 0)
5097 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5098
4a64f543
MS
5099 /* We use value_equal_contents instead of value_equal because
5100 the latter coerces an array to a pointer, thus comparing just
5101 the address of the array instead of its contents. This is
5102 not what we want. */
fa4727a6 5103 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5104 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5105 {
fa4727a6
DJ
5106 if (new_val != NULL)
5107 {
5108 release_value (new_val);
5109 value_free_to_mark (mark);
5110 }
c906108c
SS
5111 bs->old_val = b->val;
5112 b->val = new_val;
fa4727a6 5113 b->val_valid = 1;
c906108c
SS
5114 return WP_VALUE_CHANGED;
5115 }
5116 else
5117 {
60e1c644 5118 /* Nothing changed. */
c906108c 5119 value_free_to_mark (mark);
c906108c
SS
5120 return WP_VALUE_NOT_CHANGED;
5121 }
5122 }
5123 else
5124 {
79a45e25
PA
5125 struct ui_out *uiout = current_uiout;
5126
c906108c 5127 /* This seems like the only logical thing to do because
c5aa993b
JM
5128 if we temporarily ignored the watchpoint, then when
5129 we reenter the block in which it is valid it contains
5130 garbage (in the case of a function, it may have two
5131 garbage values, one before and one after the prologue).
5132 So we can't even detect the first assignment to it and
5133 watch after that (since the garbage may or may not equal
5134 the first value assigned). */
348d480f
PA
5135 /* We print all the stop information in
5136 breakpoint_ops->print_it, but in this case, by the time we
5137 call breakpoint_ops->print_it this bp will be deleted
5138 already. So we have no choice but print the information
5139 here. */
9dc5e2a9 5140 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5141 ui_out_field_string
5142 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5143 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5144 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5145 ui_out_text (uiout,
5146 " deleted because the program has left the block in\n\
8b93c638 5147which its expression is valid.\n");
4ce44c66 5148
cdac0397 5149 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5150 decref_counted_command_line (&b->base.commands);
d0fb5eae 5151 watchpoint_del_at_next_stop (b);
c906108c
SS
5152
5153 return WP_DELETED;
5154 }
5155}
5156
18a18393 5157/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5158 breakpoint location BL. This function does not check if we should
5159 stop, only if BL explains the stop. */
5160
18a18393 5161static int
6c95b8df 5162bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5163 struct address_space *aspace, CORE_ADDR bp_addr,
5164 const struct target_waitstatus *ws)
18a18393
VP
5165{
5166 struct breakpoint *b = bl->owner;
5167
348d480f 5168 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5169 gdb_assert (b != NULL);
5170
09ac7c10 5171 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5172}
5173
3a5c3e22
PA
5174/* Determine if the watched values have actually changed, and we
5175 should stop. If not, set BS->stop to 0. */
5176
18a18393
VP
5177static void
5178bpstat_check_watchpoint (bpstat bs)
5179{
2bdf28a0 5180 const struct bp_location *bl;
3a5c3e22 5181 struct watchpoint *b;
2bdf28a0
JK
5182
5183 /* BS is built for existing struct breakpoint. */
f431efe5 5184 bl = bs->bp_location_at;
2bdf28a0 5185 gdb_assert (bl != NULL);
3a5c3e22 5186 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5187 gdb_assert (b != NULL);
18a18393 5188
18a18393 5189 {
18a18393
VP
5190 int must_check_value = 0;
5191
3a5c3e22 5192 if (b->base.type == bp_watchpoint)
18a18393
VP
5193 /* For a software watchpoint, we must always check the
5194 watched value. */
5195 must_check_value = 1;
5196 else if (b->watchpoint_triggered == watch_triggered_yes)
5197 /* We have a hardware watchpoint (read, write, or access)
5198 and the target earlier reported an address watched by
5199 this watchpoint. */
5200 must_check_value = 1;
5201 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5202 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5203 /* We were stopped by a hardware watchpoint, but the target could
5204 not report the data address. We must check the watchpoint's
5205 value. Access and read watchpoints are out of luck; without
5206 a data address, we can't figure it out. */
5207 must_check_value = 1;
3a5c3e22 5208
18a18393
VP
5209 if (must_check_value)
5210 {
3e43a32a
MS
5211 char *message
5212 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5213 b->base.number);
18a18393
VP
5214 struct cleanup *cleanups = make_cleanup (xfree, message);
5215 int e = catch_errors (watchpoint_check, bs, message,
5216 RETURN_MASK_ALL);
5217 do_cleanups (cleanups);
5218 switch (e)
5219 {
5220 case WP_DELETED:
5221 /* We've already printed what needs to be printed. */
5222 bs->print_it = print_it_done;
5223 /* Stop. */
5224 break;
60e1c644
PA
5225 case WP_IGNORE:
5226 bs->print_it = print_it_noop;
5227 bs->stop = 0;
5228 break;
18a18393 5229 case WP_VALUE_CHANGED:
3a5c3e22 5230 if (b->base.type == bp_read_watchpoint)
18a18393 5231 {
85d721b8
PA
5232 /* There are two cases to consider here:
5233
4a64f543 5234 1. We're watching the triggered memory for reads.
85d721b8
PA
5235 In that case, trust the target, and always report
5236 the watchpoint hit to the user. Even though
5237 reads don't cause value changes, the value may
5238 have changed since the last time it was read, and
5239 since we're not trapping writes, we will not see
5240 those, and as such we should ignore our notion of
5241 old value.
5242
4a64f543 5243 2. We're watching the triggered memory for both
85d721b8
PA
5244 reads and writes. There are two ways this may
5245 happen:
5246
4a64f543 5247 2.1. This is a target that can't break on data
85d721b8
PA
5248 reads only, but can break on accesses (reads or
5249 writes), such as e.g., x86. We detect this case
5250 at the time we try to insert read watchpoints.
5251
4a64f543 5252 2.2. Otherwise, the target supports read
85d721b8
PA
5253 watchpoints, but, the user set an access or write
5254 watchpoint watching the same memory as this read
5255 watchpoint.
5256
5257 If we're watching memory writes as well as reads,
5258 ignore watchpoint hits when we find that the
5259 value hasn't changed, as reads don't cause
5260 changes. This still gives false positives when
5261 the program writes the same value to memory as
5262 what there was already in memory (we will confuse
5263 it for a read), but it's much better than
5264 nothing. */
5265
5266 int other_write_watchpoint = 0;
5267
5268 if (bl->watchpoint_type == hw_read)
5269 {
5270 struct breakpoint *other_b;
5271
5272 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5273 if (other_b->type == bp_hardware_watchpoint
5274 || other_b->type == bp_access_watchpoint)
85d721b8 5275 {
3a5c3e22
PA
5276 struct watchpoint *other_w =
5277 (struct watchpoint *) other_b;
5278
5279 if (other_w->watchpoint_triggered
5280 == watch_triggered_yes)
5281 {
5282 other_write_watchpoint = 1;
5283 break;
5284 }
85d721b8
PA
5285 }
5286 }
5287
5288 if (other_write_watchpoint
5289 || bl->watchpoint_type == hw_access)
5290 {
5291 /* We're watching the same memory for writes,
5292 and the value changed since the last time we
5293 updated it, so this trap must be for a write.
5294 Ignore it. */
5295 bs->print_it = print_it_noop;
5296 bs->stop = 0;
5297 }
18a18393
VP
5298 }
5299 break;
5300 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5301 if (b->base.type == bp_hardware_watchpoint
5302 || b->base.type == bp_watchpoint)
18a18393
VP
5303 {
5304 /* Don't stop: write watchpoints shouldn't fire if
5305 the value hasn't changed. */
5306 bs->print_it = print_it_noop;
5307 bs->stop = 0;
5308 }
5309 /* Stop. */
5310 break;
5311 default:
5312 /* Can't happen. */
5313 case 0:
5314 /* Error from catch_errors. */
3a5c3e22 5315 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5316 watchpoint_del_at_next_stop (b);
18a18393
VP
5317 /* We've already printed what needs to be printed. */
5318 bs->print_it = print_it_done;
5319 break;
5320 }
5321 }
5322 else /* must_check_value == 0 */
5323 {
5324 /* This is a case where some watchpoint(s) triggered, but
5325 not at the address of this watchpoint, or else no
5326 watchpoint triggered after all. So don't print
5327 anything for this watchpoint. */
5328 bs->print_it = print_it_noop;
5329 bs->stop = 0;
5330 }
5331 }
5332}
5333
7d4df6a4
DE
5334/* For breakpoints that are currently marked as telling gdb to stop,
5335 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5336 of breakpoint referred to by BS. If we should not stop for this
5337 breakpoint, set BS->stop to 0. */
f431efe5 5338
18a18393
VP
5339static void
5340bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5341{
2bdf28a0
JK
5342 const struct bp_location *bl;
5343 struct breakpoint *b;
7d4df6a4
DE
5344 int value_is_zero = 0;
5345 struct expression *cond;
5346
5347 gdb_assert (bs->stop);
2bdf28a0
JK
5348
5349 /* BS is built for existing struct breakpoint. */
f431efe5 5350 bl = bs->bp_location_at;
2bdf28a0 5351 gdb_assert (bl != NULL);
f431efe5 5352 b = bs->breakpoint_at;
2bdf28a0 5353 gdb_assert (b != NULL);
18a18393 5354
b775012e
LM
5355 /* Even if the target evaluated the condition on its end and notified GDB, we
5356 need to do so again since GDB does not know if we stopped due to a
5357 breakpoint or a single step breakpoint. */
5358
18a18393 5359 if (frame_id_p (b->frame_id)
edb3359d 5360 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5361 {
7d4df6a4
DE
5362 bs->stop = 0;
5363 return;
5364 }
60e1c644 5365
12ab52e9
PA
5366 /* If this is a thread/task-specific breakpoint, don't waste cpu
5367 evaluating the condition if this isn't the specified
5368 thread/task. */
5369 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5370 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5371
6c1b0f7b
DE
5372 {
5373 bs->stop = 0;
5374 return;
5375 }
5376
6dddc817
DE
5377 /* Evaluate extension language breakpoints that have a "stop" method
5378 implemented. */
5379 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5380
7d4df6a4
DE
5381 if (is_watchpoint (b))
5382 {
5383 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5384
7d4df6a4
DE
5385 cond = w->cond_exp;
5386 }
5387 else
5388 cond = bl->cond;
60e1c644 5389
7d4df6a4
DE
5390 if (cond && b->disposition != disp_del_at_next_stop)
5391 {
5392 int within_current_scope = 1;
5393 struct watchpoint * w;
60e1c644 5394
7d4df6a4
DE
5395 /* We use value_mark and value_free_to_mark because it could
5396 be a long time before we return to the command level and
5397 call free_all_values. We can't call free_all_values
5398 because we might be in the middle of evaluating a
5399 function call. */
5400 struct value *mark = value_mark ();
5401
5402 if (is_watchpoint (b))
5403 w = (struct watchpoint *) b;
5404 else
5405 w = NULL;
5406
5407 /* Need to select the frame, with all that implies so that
5408 the conditions will have the right context. Because we
5409 use the frame, we will not see an inlined function's
5410 variables when we arrive at a breakpoint at the start
5411 of the inlined function; the current frame will be the
5412 call site. */
5413 if (w == NULL || w->cond_exp_valid_block == NULL)
5414 select_frame (get_current_frame ());
5415 else
18a18393 5416 {
7d4df6a4
DE
5417 struct frame_info *frame;
5418
5419 /* For local watchpoint expressions, which particular
5420 instance of a local is being watched matters, so we
5421 keep track of the frame to evaluate the expression
5422 in. To evaluate the condition however, it doesn't
5423 really matter which instantiation of the function
5424 where the condition makes sense triggers the
5425 watchpoint. This allows an expression like "watch
5426 global if q > 10" set in `func', catch writes to
5427 global on all threads that call `func', or catch
5428 writes on all recursive calls of `func' by a single
5429 thread. We simply always evaluate the condition in
5430 the innermost frame that's executing where it makes
5431 sense to evaluate the condition. It seems
5432 intuitive. */
5433 frame = block_innermost_frame (w->cond_exp_valid_block);
5434 if (frame != NULL)
5435 select_frame (frame);
5436 else
5437 within_current_scope = 0;
18a18393 5438 }
7d4df6a4
DE
5439 if (within_current_scope)
5440 value_is_zero
5441 = catch_errors (breakpoint_cond_eval, cond,
5442 "Error in testing breakpoint condition:\n",
5443 RETURN_MASK_ALL);
5444 else
18a18393 5445 {
7d4df6a4
DE
5446 warning (_("Watchpoint condition cannot be tested "
5447 "in the current scope"));
5448 /* If we failed to set the right context for this
5449 watchpoint, unconditionally report it. */
5450 value_is_zero = 0;
18a18393 5451 }
7d4df6a4
DE
5452 /* FIXME-someday, should give breakpoint #. */
5453 value_free_to_mark (mark);
18a18393 5454 }
7d4df6a4
DE
5455
5456 if (cond && value_is_zero)
5457 {
5458 bs->stop = 0;
5459 }
7d4df6a4
DE
5460 else if (b->ignore_count > 0)
5461 {
5462 b->ignore_count--;
5463 bs->stop = 0;
5464 /* Increase the hit count even though we don't stop. */
5465 ++(b->hit_count);
5466 observer_notify_breakpoint_modified (b);
5467 }
18a18393
VP
5468}
5469
1cf4d951
PA
5470/* Returns true if we need to track moribund locations of LOC's type
5471 on the current target. */
5472
5473static int
5474need_moribund_for_location_type (struct bp_location *loc)
5475{
5476 return ((loc->loc_type == bp_loc_software_breakpoint
5477 && !target_supports_stopped_by_sw_breakpoint ())
5478 || (loc->loc_type == bp_loc_hardware_breakpoint
5479 && !target_supports_stopped_by_hw_breakpoint ()));
5480}
5481
18a18393 5482
9709f61c 5483/* Get a bpstat associated with having just stopped at address
d983da9c 5484 BP_ADDR in thread PTID.
c906108c 5485
d983da9c 5486 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5487 don't understand this stop. Result is a chain of bpstat's such
5488 that:
c906108c 5489
c5aa993b 5490 if we don't understand the stop, the result is a null pointer.
c906108c 5491
c5aa993b 5492 if we understand why we stopped, the result is not null.
c906108c 5493
c5aa993b
JM
5494 Each element of the chain refers to a particular breakpoint or
5495 watchpoint at which we have stopped. (We may have stopped for
5496 several reasons concurrently.)
c906108c 5497
c5aa993b
JM
5498 Each element of the chain has valid next, breakpoint_at,
5499 commands, FIXME??? fields. */
c906108c
SS
5500
5501bpstat
6c95b8df 5502bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5503 CORE_ADDR bp_addr, ptid_t ptid,
5504 const struct target_waitstatus *ws)
c906108c 5505{
0d381245 5506 struct breakpoint *b = NULL;
afe38095 5507 struct bp_location *bl;
20874c92 5508 struct bp_location *loc;
5760d0ab
JK
5509 /* First item of allocated bpstat's. */
5510 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5511 /* Pointer to the last thing in the chain currently. */
5760d0ab 5512 bpstat bs;
20874c92 5513 int ix;
429374b8 5514 int need_remove_insert;
f431efe5 5515 int removed_any;
c906108c 5516
f431efe5
PA
5517 /* First, build the bpstat chain with locations that explain a
5518 target stop, while being careful to not set the target running,
5519 as that may invalidate locations (in particular watchpoint
5520 locations are recreated). Resuming will happen here with
5521 breakpoint conditions or watchpoint expressions that include
5522 inferior function calls. */
c5aa993b 5523
429374b8
JK
5524 ALL_BREAKPOINTS (b)
5525 {
1a853c52 5526 if (!breakpoint_enabled (b))
429374b8 5527 continue;
a5606eee 5528
429374b8
JK
5529 for (bl = b->loc; bl != NULL; bl = bl->next)
5530 {
4a64f543
MS
5531 /* For hardware watchpoints, we look only at the first
5532 location. The watchpoint_check function will work on the
5533 entire expression, not the individual locations. For
5534 read watchpoints, the watchpoints_triggered function has
5535 checked all locations already. */
429374b8
JK
5536 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5537 break;
18a18393 5538
f6592439 5539 if (!bl->enabled || bl->shlib_disabled)
429374b8 5540 continue;
c5aa993b 5541
09ac7c10 5542 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5543 continue;
c5aa993b 5544
4a64f543
MS
5545 /* Come here if it's a watchpoint, or if the break address
5546 matches. */
c5aa993b 5547
4a64f543
MS
5548 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5549 explain stop. */
c5aa993b 5550
f431efe5
PA
5551 /* Assume we stop. Should we find a watchpoint that is not
5552 actually triggered, or if the condition of the breakpoint
5553 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5554 bs->stop = 1;
5555 bs->print = 1;
d983da9c 5556
f431efe5
PA
5557 /* If this is a scope breakpoint, mark the associated
5558 watchpoint as triggered so that we will handle the
5559 out-of-scope event. We'll get to the watchpoint next
5560 iteration. */
d0fb5eae 5561 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5562 {
5563 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5564
5565 w->watchpoint_triggered = watch_triggered_yes;
5566 }
f431efe5
PA
5567 }
5568 }
5569
7c16b83e 5570 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5571 if (!target_supports_stopped_by_sw_breakpoint ()
5572 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5573 {
1cf4d951 5574 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5575 {
1cf4d951
PA
5576 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5577 && need_moribund_for_location_type (loc))
5578 {
5579 bs = bpstat_alloc (loc, &bs_link);
5580 /* For hits of moribund locations, we should just proceed. */
5581 bs->stop = 0;
5582 bs->print = 0;
5583 bs->print_it = print_it_noop;
5584 }
f431efe5
PA
5585 }
5586 }
5587
edcc5120
TT
5588 /* A bit of special processing for shlib breakpoints. We need to
5589 process solib loading here, so that the lists of loaded and
5590 unloaded libraries are correct before we handle "catch load" and
5591 "catch unload". */
5592 for (bs = bs_head; bs != NULL; bs = bs->next)
5593 {
5d268276 5594 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5595 {
5596 handle_solib_event ();
5597 break;
5598 }
5599 }
5600
f431efe5
PA
5601 /* Now go through the locations that caused the target to stop, and
5602 check whether we're interested in reporting this stop to higher
5603 layers, or whether we should resume the target transparently. */
5604
5605 removed_any = 0;
5606
5760d0ab 5607 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5608 {
5609 if (!bs->stop)
5610 continue;
5611
f431efe5 5612 b = bs->breakpoint_at;
348d480f
PA
5613 b->ops->check_status (bs);
5614 if (bs->stop)
28010a5d 5615 {
348d480f 5616 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5617
429374b8
JK
5618 if (bs->stop)
5619 {
5620 ++(b->hit_count);
8d3788bd 5621 observer_notify_breakpoint_modified (b);
c906108c 5622
4a64f543 5623 /* We will stop here. */
429374b8
JK
5624 if (b->disposition == disp_disable)
5625 {
816338b5 5626 --(b->enable_count);
1a853c52 5627 if (b->enable_count <= 0)
429374b8 5628 b->enable_state = bp_disabled;
f431efe5 5629 removed_any = 1;
429374b8
JK
5630 }
5631 if (b->silent)
5632 bs->print = 0;
5633 bs->commands = b->commands;
9add0f1b 5634 incref_counted_command_line (bs->commands);
abf85f46
JK
5635 if (command_line_is_silent (bs->commands
5636 ? bs->commands->commands : NULL))
5637 bs->print = 0;
9d6e6e84
HZ
5638
5639 b->ops->after_condition_true (bs);
429374b8
JK
5640 }
5641
348d480f 5642 }
a9b3a50f
PA
5643
5644 /* Print nothing for this entry if we don't stop or don't
5645 print. */
5646 if (!bs->stop || !bs->print)
5647 bs->print_it = print_it_noop;
429374b8 5648 }
876fa593 5649
d983da9c
DJ
5650 /* If we aren't stopping, the value of some hardware watchpoint may
5651 not have changed, but the intermediate memory locations we are
5652 watching may have. Don't bother if we're stopping; this will get
5653 done later. */
d832cb68 5654 need_remove_insert = 0;
5760d0ab
JK
5655 if (! bpstat_causes_stop (bs_head))
5656 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5657 if (!bs->stop
f431efe5
PA
5658 && bs->breakpoint_at
5659 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5660 {
3a5c3e22
PA
5661 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5662
5663 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5664 need_remove_insert = 1;
d983da9c
DJ
5665 }
5666
d832cb68 5667 if (need_remove_insert)
44702360 5668 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5669 else if (removed_any)
44702360 5670 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5671
5760d0ab 5672 return bs_head;
c906108c 5673}
628fe4e4
JK
5674
5675static void
5676handle_jit_event (void)
5677{
5678 struct frame_info *frame;
5679 struct gdbarch *gdbarch;
5680
5681 /* Switch terminal for any messages produced by
5682 breakpoint_re_set. */
5683 target_terminal_ours_for_output ();
5684
5685 frame = get_current_frame ();
5686 gdbarch = get_frame_arch (frame);
5687
5688 jit_event_handler (gdbarch);
5689
5690 target_terminal_inferior ();
5691}
5692
5693/* Prepare WHAT final decision for infrun. */
5694
5695/* Decide what infrun needs to do with this bpstat. */
5696
c906108c 5697struct bpstat_what
0e30163f 5698bpstat_what (bpstat bs_head)
c906108c 5699{
c906108c 5700 struct bpstat_what retval;
628fe4e4 5701 int jit_event = 0;
0e30163f 5702 bpstat bs;
c906108c 5703
628fe4e4 5704 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5705 retval.call_dummy = STOP_NONE;
186c406b 5706 retval.is_longjmp = 0;
628fe4e4 5707
0e30163f 5708 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5709 {
628fe4e4
JK
5710 /* Extract this BS's action. After processing each BS, we check
5711 if its action overrides all we've seem so far. */
5712 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5713 enum bptype bptype;
5714
c906108c 5715 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5716 {
5717 /* I suspect this can happen if it was a momentary
5718 breakpoint which has since been deleted. */
5719 bptype = bp_none;
5720 }
20874c92 5721 else
f431efe5 5722 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5723
5724 switch (bptype)
c906108c
SS
5725 {
5726 case bp_none:
628fe4e4 5727 break;
c906108c
SS
5728 case bp_breakpoint:
5729 case bp_hardware_breakpoint:
7c16b83e 5730 case bp_single_step:
c906108c
SS
5731 case bp_until:
5732 case bp_finish:
a9b3a50f 5733 case bp_shlib_event:
c906108c
SS
5734 if (bs->stop)
5735 {
5736 if (bs->print)
628fe4e4 5737 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5738 else
628fe4e4 5739 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5740 }
5741 else
628fe4e4 5742 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5743 break;
5744 case bp_watchpoint:
5745 case bp_hardware_watchpoint:
5746 case bp_read_watchpoint:
5747 case bp_access_watchpoint:
5748 if (bs->stop)
5749 {
5750 if (bs->print)
628fe4e4 5751 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5752 else
628fe4e4 5753 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5754 }
5755 else
628fe4e4
JK
5756 {
5757 /* There was a watchpoint, but we're not stopping.
5758 This requires no further action. */
5759 }
c906108c
SS
5760 break;
5761 case bp_longjmp:
e2e4d78b 5762 case bp_longjmp_call_dummy:
186c406b 5763 case bp_exception:
628fe4e4 5764 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5765 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5766 break;
5767 case bp_longjmp_resume:
186c406b 5768 case bp_exception_resume:
628fe4e4 5769 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5770 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5771 break;
5772 case bp_step_resume:
5773 if (bs->stop)
628fe4e4
JK
5774 this_action = BPSTAT_WHAT_STEP_RESUME;
5775 else
c906108c 5776 {
628fe4e4
JK
5777 /* It is for the wrong frame. */
5778 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5779 }
c906108c 5780 break;
2c03e5be
PA
5781 case bp_hp_step_resume:
5782 if (bs->stop)
5783 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5784 else
5785 {
5786 /* It is for the wrong frame. */
5787 this_action = BPSTAT_WHAT_SINGLE;
5788 }
5789 break;
c906108c 5790 case bp_watchpoint_scope:
c4093a6a 5791 case bp_thread_event:
1900040c 5792 case bp_overlay_event:
0fd8e87f 5793 case bp_longjmp_master:
aa7d318d 5794 case bp_std_terminate_master:
186c406b 5795 case bp_exception_master:
628fe4e4 5796 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5797 break;
ce78b96d 5798 case bp_catchpoint:
c5aa993b
JM
5799 if (bs->stop)
5800 {
5801 if (bs->print)
628fe4e4 5802 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5803 else
628fe4e4 5804 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5805 }
5806 else
628fe4e4
JK
5807 {
5808 /* There was a catchpoint, but we're not stopping.
5809 This requires no further action. */
5810 }
5811 break;
628fe4e4
JK
5812 case bp_jit_event:
5813 jit_event = 1;
5814 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5815 break;
c906108c 5816 case bp_call_dummy:
53a5351d
JM
5817 /* Make sure the action is stop (silent or noisy),
5818 so infrun.c pops the dummy frame. */
aa7d318d 5819 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5820 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5821 break;
5822 case bp_std_terminate:
5823 /* Make sure the action is stop (silent or noisy),
5824 so infrun.c pops the dummy frame. */
aa7d318d 5825 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5826 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5827 break;
1042e4c0 5828 case bp_tracepoint:
7a697b8d 5829 case bp_fast_tracepoint:
0fb4aa4b 5830 case bp_static_tracepoint:
1042e4c0
SS
5831 /* Tracepoint hits should not be reported back to GDB, and
5832 if one got through somehow, it should have been filtered
5833 out already. */
5834 internal_error (__FILE__, __LINE__,
7a697b8d 5835 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5836 break;
5837 case bp_gnu_ifunc_resolver:
5838 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5839 this_action = BPSTAT_WHAT_SINGLE;
5840 break;
5841 case bp_gnu_ifunc_resolver_return:
5842 /* The breakpoint will be removed, execution will restart from the
5843 PC of the former breakpoint. */
5844 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5845 break;
e7e0cddf
SS
5846
5847 case bp_dprintf:
a11cfd87
HZ
5848 if (bs->stop)
5849 this_action = BPSTAT_WHAT_STOP_SILENT;
5850 else
5851 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5852 break;
5853
628fe4e4
JK
5854 default:
5855 internal_error (__FILE__, __LINE__,
5856 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5857 }
628fe4e4
JK
5858
5859 retval.main_action = max (retval.main_action, this_action);
c906108c 5860 }
628fe4e4 5861
0e30163f
JK
5862 /* These operations may affect the bs->breakpoint_at state so they are
5863 delayed after MAIN_ACTION is decided above. */
5864
628fe4e4
JK
5865 if (jit_event)
5866 {
5867 if (debug_infrun)
5868 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5869
5870 handle_jit_event ();
5871 }
5872
0e30163f
JK
5873 for (bs = bs_head; bs != NULL; bs = bs->next)
5874 {
5875 struct breakpoint *b = bs->breakpoint_at;
5876
5877 if (b == NULL)
5878 continue;
5879 switch (b->type)
5880 {
5881 case bp_gnu_ifunc_resolver:
5882 gnu_ifunc_resolver_stop (b);
5883 break;
5884 case bp_gnu_ifunc_resolver_return:
5885 gnu_ifunc_resolver_return_stop (b);
5886 break;
5887 }
5888 }
5889
c906108c
SS
5890 return retval;
5891}
5892
5893/* Nonzero if we should step constantly (e.g. watchpoints on machines
5894 without hardware support). This isn't related to a specific bpstat,
5895 just to things like whether watchpoints are set. */
5896
c5aa993b 5897int
fba45db2 5898bpstat_should_step (void)
c906108c
SS
5899{
5900 struct breakpoint *b;
cc59ec59 5901
c906108c 5902 ALL_BREAKPOINTS (b)
717a8278 5903 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5904 return 1;
c906108c
SS
5905 return 0;
5906}
5907
67822962
PA
5908int
5909bpstat_causes_stop (bpstat bs)
5910{
5911 for (; bs != NULL; bs = bs->next)
5912 if (bs->stop)
5913 return 1;
5914
5915 return 0;
5916}
5917
c906108c 5918\f
c5aa993b 5919
170b53b2
UW
5920/* Compute a string of spaces suitable to indent the next line
5921 so it starts at the position corresponding to the table column
5922 named COL_NAME in the currently active table of UIOUT. */
5923
5924static char *
5925wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5926{
5927 static char wrap_indent[80];
5928 int i, total_width, width, align;
5929 char *text;
5930
5931 total_width = 0;
5932 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5933 {
5934 if (strcmp (text, col_name) == 0)
5935 {
5936 gdb_assert (total_width < sizeof wrap_indent);
5937 memset (wrap_indent, ' ', total_width);
5938 wrap_indent[total_width] = 0;
5939
5940 return wrap_indent;
5941 }
5942
5943 total_width += width + 1;
5944 }
5945
5946 return NULL;
5947}
5948
b775012e
LM
5949/* Determine if the locations of this breakpoint will have their conditions
5950 evaluated by the target, host or a mix of both. Returns the following:
5951
5952 "host": Host evals condition.
5953 "host or target": Host or Target evals condition.
5954 "target": Target evals condition.
5955*/
5956
5957static const char *
5958bp_condition_evaluator (struct breakpoint *b)
5959{
5960 struct bp_location *bl;
5961 char host_evals = 0;
5962 char target_evals = 0;
5963
5964 if (!b)
5965 return NULL;
5966
5967 if (!is_breakpoint (b))
5968 return NULL;
5969
5970 if (gdb_evaluates_breakpoint_condition_p ()
5971 || !target_supports_evaluation_of_breakpoint_conditions ())
5972 return condition_evaluation_host;
5973
5974 for (bl = b->loc; bl; bl = bl->next)
5975 {
5976 if (bl->cond_bytecode)
5977 target_evals++;
5978 else
5979 host_evals++;
5980 }
5981
5982 if (host_evals && target_evals)
5983 return condition_evaluation_both;
5984 else if (target_evals)
5985 return condition_evaluation_target;
5986 else
5987 return condition_evaluation_host;
5988}
5989
5990/* Determine the breakpoint location's condition evaluator. This is
5991 similar to bp_condition_evaluator, but for locations. */
5992
5993static const char *
5994bp_location_condition_evaluator (struct bp_location *bl)
5995{
5996 if (bl && !is_breakpoint (bl->owner))
5997 return NULL;
5998
5999 if (gdb_evaluates_breakpoint_condition_p ()
6000 || !target_supports_evaluation_of_breakpoint_conditions ())
6001 return condition_evaluation_host;
6002
6003 if (bl && bl->cond_bytecode)
6004 return condition_evaluation_target;
6005 else
6006 return condition_evaluation_host;
6007}
6008
859825b8
JK
6009/* Print the LOC location out of the list of B->LOC locations. */
6010
170b53b2
UW
6011static void
6012print_breakpoint_location (struct breakpoint *b,
6013 struct bp_location *loc)
0d381245 6014{
79a45e25 6015 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6016 struct cleanup *old_chain = save_current_program_space ();
6017
859825b8
JK
6018 if (loc != NULL && loc->shlib_disabled)
6019 loc = NULL;
6020
6c95b8df
PA
6021 if (loc != NULL)
6022 set_current_program_space (loc->pspace);
6023
56435ebe
TT
6024 if (b->display_canonical)
6025 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 6026 else if (loc && loc->symtab)
0d381245
VP
6027 {
6028 struct symbol *sym
6029 = find_pc_sect_function (loc->address, loc->section);
6030 if (sym)
6031 {
6032 ui_out_text (uiout, "in ");
6033 ui_out_field_string (uiout, "func",
6034 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6035 ui_out_text (uiout, " ");
6036 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6037 ui_out_text (uiout, "at ");
0d381245 6038 }
05cba821
JK
6039 ui_out_field_string (uiout, "file",
6040 symtab_to_filename_for_display (loc->symtab));
0d381245 6041 ui_out_text (uiout, ":");
05cba821 6042
0d381245 6043 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6044 ui_out_field_string (uiout, "fullname",
6045 symtab_to_fullname (loc->symtab));
0d381245 6046
f8eba3c6 6047 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6048 }
859825b8 6049 else if (loc)
0d381245 6050 {
f99d8bf4
PA
6051 struct ui_file *stb = mem_fileopen ();
6052 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6053
f99d8bf4 6054 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6055 demangle, "");
0d381245 6056 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6057
6058 do_cleanups (stb_chain);
0d381245 6059 }
859825b8
JK
6060 else
6061 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 6062
b775012e
LM
6063 if (loc && is_breakpoint (b)
6064 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6065 && bp_condition_evaluator (b) == condition_evaluation_both)
6066 {
6067 ui_out_text (uiout, " (");
6068 ui_out_field_string (uiout, "evaluated-by",
6069 bp_location_condition_evaluator (loc));
6070 ui_out_text (uiout, ")");
6071 }
6072
6c95b8df 6073 do_cleanups (old_chain);
0d381245
VP
6074}
6075
269b11a2
PA
6076static const char *
6077bptype_string (enum bptype type)
c906108c 6078{
c4093a6a
JM
6079 struct ep_type_description
6080 {
6081 enum bptype type;
6082 char *description;
6083 };
6084 static struct ep_type_description bptypes[] =
c906108c 6085 {
c5aa993b
JM
6086 {bp_none, "?deleted?"},
6087 {bp_breakpoint, "breakpoint"},
c906108c 6088 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6089 {bp_single_step, "sw single-step"},
c5aa993b
JM
6090 {bp_until, "until"},
6091 {bp_finish, "finish"},
6092 {bp_watchpoint, "watchpoint"},
c906108c 6093 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6094 {bp_read_watchpoint, "read watchpoint"},
6095 {bp_access_watchpoint, "acc watchpoint"},
6096 {bp_longjmp, "longjmp"},
6097 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6098 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6099 {bp_exception, "exception"},
6100 {bp_exception_resume, "exception resume"},
c5aa993b 6101 {bp_step_resume, "step resume"},
2c03e5be 6102 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6103 {bp_watchpoint_scope, "watchpoint scope"},
6104 {bp_call_dummy, "call dummy"},
aa7d318d 6105 {bp_std_terminate, "std::terminate"},
c5aa993b 6106 {bp_shlib_event, "shlib events"},
c4093a6a 6107 {bp_thread_event, "thread events"},
1900040c 6108 {bp_overlay_event, "overlay events"},
0fd8e87f 6109 {bp_longjmp_master, "longjmp master"},
aa7d318d 6110 {bp_std_terminate_master, "std::terminate master"},
186c406b 6111 {bp_exception_master, "exception master"},
ce78b96d 6112 {bp_catchpoint, "catchpoint"},
1042e4c0 6113 {bp_tracepoint, "tracepoint"},
7a697b8d 6114 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6115 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6116 {bp_dprintf, "dprintf"},
4efc6507 6117 {bp_jit_event, "jit events"},
0e30163f
JK
6118 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6119 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6120 };
269b11a2
PA
6121
6122 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6123 || ((int) type != bptypes[(int) type].type))
6124 internal_error (__FILE__, __LINE__,
6125 _("bptypes table does not describe type #%d."),
6126 (int) type);
6127
6128 return bptypes[(int) type].description;
6129}
6130
998580f1
MK
6131/* For MI, output a field named 'thread-groups' with a list as the value.
6132 For CLI, prefix the list with the string 'inf'. */
6133
6134static void
6135output_thread_groups (struct ui_out *uiout,
6136 const char *field_name,
6137 VEC(int) *inf_num,
6138 int mi_only)
6139{
752eb8b4 6140 struct cleanup *back_to;
998580f1
MK
6141 int is_mi = ui_out_is_mi_like_p (uiout);
6142 int inf;
6143 int i;
6144
6145 /* For backward compatibility, don't display inferiors in CLI unless
6146 there are several. Always display them for MI. */
6147 if (!is_mi && mi_only)
6148 return;
6149
752eb8b4
TT
6150 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6151
998580f1
MK
6152 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6153 {
6154 if (is_mi)
6155 {
6156 char mi_group[10];
6157
6158 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6159 ui_out_field_string (uiout, NULL, mi_group);
6160 }
6161 else
6162 {
6163 if (i == 0)
6164 ui_out_text (uiout, " inf ");
6165 else
6166 ui_out_text (uiout, ", ");
6167
6168 ui_out_text (uiout, plongest (inf));
6169 }
6170 }
6171
6172 do_cleanups (back_to);
6173}
6174
269b11a2
PA
6175/* Print B to gdb_stdout. */
6176
6177static void
6178print_one_breakpoint_location (struct breakpoint *b,
6179 struct bp_location *loc,
6180 int loc_number,
6181 struct bp_location **last_loc,
269b11a2
PA
6182 int allflag)
6183{
6184 struct command_line *l;
c2c6d25f 6185 static char bpenables[] = "nynny";
c906108c 6186
79a45e25 6187 struct ui_out *uiout = current_uiout;
0d381245
VP
6188 int header_of_multiple = 0;
6189 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6190 struct value_print_options opts;
6191
6192 get_user_print_options (&opts);
0d381245
VP
6193
6194 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6195 /* See comment in print_one_breakpoint concerning treatment of
6196 breakpoints with single disabled location. */
0d381245
VP
6197 if (loc == NULL
6198 && (b->loc != NULL
6199 && (b->loc->next != NULL || !b->loc->enabled)))
6200 header_of_multiple = 1;
6201 if (loc == NULL)
6202 loc = b->loc;
6203
c4093a6a
JM
6204 annotate_record ();
6205
6206 /* 1 */
6207 annotate_field (0);
0d381245
VP
6208 if (part_of_multiple)
6209 {
6210 char *formatted;
0c6773c1 6211 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6212 ui_out_field_string (uiout, "number", formatted);
6213 xfree (formatted);
6214 }
6215 else
6216 {
6217 ui_out_field_int (uiout, "number", b->number);
6218 }
c4093a6a
JM
6219
6220 /* 2 */
6221 annotate_field (1);
0d381245
VP
6222 if (part_of_multiple)
6223 ui_out_field_skip (uiout, "type");
269b11a2
PA
6224 else
6225 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6226
6227 /* 3 */
6228 annotate_field (2);
0d381245
VP
6229 if (part_of_multiple)
6230 ui_out_field_skip (uiout, "disp");
6231 else
2cec12e5 6232 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6233
c4093a6a
JM
6234
6235 /* 4 */
6236 annotate_field (3);
0d381245 6237 if (part_of_multiple)
54e52265 6238 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6239 else
4a64f543
MS
6240 ui_out_field_fmt (uiout, "enabled", "%c",
6241 bpenables[(int) b->enable_state]);
54e52265 6242 ui_out_spaces (uiout, 2);
0d381245 6243
c4093a6a
JM
6244
6245 /* 5 and 6 */
3086aeae 6246 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6247 {
4a64f543
MS
6248 /* Although the print_one can possibly print all locations,
6249 calling it here is not likely to get any nice result. So,
6250 make sure there's just one location. */
0d381245 6251 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6252 b->ops->print_one (b, last_loc);
0d381245 6253 }
3086aeae
DJ
6254 else
6255 switch (b->type)
6256 {
6257 case bp_none:
6258 internal_error (__FILE__, __LINE__,
e2e0b3e5 6259 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6260 break;
c906108c 6261
3086aeae
DJ
6262 case bp_watchpoint:
6263 case bp_hardware_watchpoint:
6264 case bp_read_watchpoint:
6265 case bp_access_watchpoint:
3a5c3e22
PA
6266 {
6267 struct watchpoint *w = (struct watchpoint *) b;
6268
6269 /* Field 4, the address, is omitted (which makes the columns
6270 not line up too nicely with the headers, but the effect
6271 is relatively readable). */
6272 if (opts.addressprint)
6273 ui_out_field_skip (uiout, "addr");
6274 annotate_field (5);
6275 ui_out_field_string (uiout, "what", w->exp_string);
6276 }
3086aeae
DJ
6277 break;
6278
3086aeae
DJ
6279 case bp_breakpoint:
6280 case bp_hardware_breakpoint:
7c16b83e 6281 case bp_single_step:
3086aeae
DJ
6282 case bp_until:
6283 case bp_finish:
6284 case bp_longjmp:
6285 case bp_longjmp_resume:
e2e4d78b 6286 case bp_longjmp_call_dummy:
186c406b
TT
6287 case bp_exception:
6288 case bp_exception_resume:
3086aeae 6289 case bp_step_resume:
2c03e5be 6290 case bp_hp_step_resume:
3086aeae
DJ
6291 case bp_watchpoint_scope:
6292 case bp_call_dummy:
aa7d318d 6293 case bp_std_terminate:
3086aeae
DJ
6294 case bp_shlib_event:
6295 case bp_thread_event:
6296 case bp_overlay_event:
0fd8e87f 6297 case bp_longjmp_master:
aa7d318d 6298 case bp_std_terminate_master:
186c406b 6299 case bp_exception_master:
1042e4c0 6300 case bp_tracepoint:
7a697b8d 6301 case bp_fast_tracepoint:
0fb4aa4b 6302 case bp_static_tracepoint:
e7e0cddf 6303 case bp_dprintf:
4efc6507 6304 case bp_jit_event:
0e30163f
JK
6305 case bp_gnu_ifunc_resolver:
6306 case bp_gnu_ifunc_resolver_return:
79a45b7d 6307 if (opts.addressprint)
3086aeae
DJ
6308 {
6309 annotate_field (4);
54e52265 6310 if (header_of_multiple)
0d381245 6311 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6312 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6313 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6314 else
5af949e3
UW
6315 ui_out_field_core_addr (uiout, "addr",
6316 loc->gdbarch, loc->address);
3086aeae
DJ
6317 }
6318 annotate_field (5);
0d381245 6319 if (!header_of_multiple)
170b53b2 6320 print_breakpoint_location (b, loc);
0d381245 6321 if (b->loc)
a6d9a66e 6322 *last_loc = b->loc;
3086aeae
DJ
6323 break;
6324 }
c906108c 6325
6c95b8df 6326
998580f1 6327 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6328 {
6329 struct inferior *inf;
998580f1
MK
6330 VEC(int) *inf_num = NULL;
6331 int mi_only = 1;
6c95b8df 6332
998580f1 6333 ALL_INFERIORS (inf)
6c95b8df
PA
6334 {
6335 if (inf->pspace == loc->pspace)
998580f1 6336 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6337 }
998580f1
MK
6338
6339 /* For backward compatibility, don't display inferiors in CLI unless
6340 there are several. Always display for MI. */
6341 if (allflag
6342 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6343 && (number_of_program_spaces () > 1
6344 || number_of_inferiors () > 1)
6345 /* LOC is for existing B, it cannot be in
6346 moribund_locations and thus having NULL OWNER. */
6347 && loc->owner->type != bp_catchpoint))
6348 mi_only = 0;
6349 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6350 VEC_free (int, inf_num);
6c95b8df
PA
6351 }
6352
4a306c9a 6353 if (!part_of_multiple)
c4093a6a 6354 {
4a306c9a
JB
6355 if (b->thread != -1)
6356 {
6357 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6358 "stop only in" line a little further down. */
4a306c9a
JB
6359 ui_out_text (uiout, " thread ");
6360 ui_out_field_int (uiout, "thread", b->thread);
6361 }
6362 else if (b->task != 0)
6363 {
6364 ui_out_text (uiout, " task ");
6365 ui_out_field_int (uiout, "task", b->task);
6366 }
c4093a6a 6367 }
f1310107 6368
8b93c638 6369 ui_out_text (uiout, "\n");
f1310107 6370
348d480f 6371 if (!part_of_multiple)
f1310107
TJB
6372 b->ops->print_one_detail (b, uiout);
6373
0d381245 6374 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6375 {
6376 annotate_field (6);
8b93c638 6377 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6378 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6379 the frame ID. */
5af949e3
UW
6380 ui_out_field_core_addr (uiout, "frame",
6381 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6382 ui_out_text (uiout, "\n");
c4093a6a
JM
6383 }
6384
28010a5d 6385 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6386 {
6387 annotate_field (7);
d77f58be 6388 if (is_tracepoint (b))
1042e4c0
SS
6389 ui_out_text (uiout, "\ttrace only if ");
6390 else
6391 ui_out_text (uiout, "\tstop only if ");
0101ce28 6392 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6393
6394 /* Print whether the target is doing the breakpoint's condition
6395 evaluation. If GDB is doing the evaluation, don't print anything. */
6396 if (is_breakpoint (b)
6397 && breakpoint_condition_evaluation_mode ()
6398 == condition_evaluation_target)
6399 {
6400 ui_out_text (uiout, " (");
6401 ui_out_field_string (uiout, "evaluated-by",
6402 bp_condition_evaluator (b));
6403 ui_out_text (uiout, " evals)");
6404 }
0101ce28
JJ
6405 ui_out_text (uiout, "\n");
6406 }
6407
0d381245 6408 if (!part_of_multiple && b->thread != -1)
c4093a6a 6409 {
4a64f543 6410 /* FIXME should make an annotation for this. */
8b93c638
JM
6411 ui_out_text (uiout, "\tstop only in thread ");
6412 ui_out_field_int (uiout, "thread", b->thread);
6413 ui_out_text (uiout, "\n");
c4093a6a
JM
6414 }
6415
556ec64d
YQ
6416 if (!part_of_multiple)
6417 {
6418 if (b->hit_count)
31f56a27
YQ
6419 {
6420 /* FIXME should make an annotation for this. */
6421 if (is_catchpoint (b))
6422 ui_out_text (uiout, "\tcatchpoint");
6423 else if (is_tracepoint (b))
6424 ui_out_text (uiout, "\ttracepoint");
6425 else
6426 ui_out_text (uiout, "\tbreakpoint");
6427 ui_out_text (uiout, " already hit ");
6428 ui_out_field_int (uiout, "times", b->hit_count);
6429 if (b->hit_count == 1)
6430 ui_out_text (uiout, " time\n");
6431 else
6432 ui_out_text (uiout, " times\n");
6433 }
556ec64d
YQ
6434 else
6435 {
31f56a27
YQ
6436 /* Output the count also if it is zero, but only if this is mi. */
6437 if (ui_out_is_mi_like_p (uiout))
6438 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6439 }
6440 }
8b93c638 6441
0d381245 6442 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6443 {
6444 annotate_field (8);
8b93c638
JM
6445 ui_out_text (uiout, "\tignore next ");
6446 ui_out_field_int (uiout, "ignore", b->ignore_count);
6447 ui_out_text (uiout, " hits\n");
c4093a6a 6448 }
059fb39f 6449
816338b5
SS
6450 /* Note that an enable count of 1 corresponds to "enable once"
6451 behavior, which is reported by the combination of enablement and
6452 disposition, so we don't need to mention it here. */
6453 if (!part_of_multiple && b->enable_count > 1)
6454 {
6455 annotate_field (8);
6456 ui_out_text (uiout, "\tdisable after ");
6457 /* Tweak the wording to clarify that ignore and enable counts
6458 are distinct, and have additive effect. */
6459 if (b->ignore_count)
6460 ui_out_text (uiout, "additional ");
6461 else
6462 ui_out_text (uiout, "next ");
6463 ui_out_field_int (uiout, "enable", b->enable_count);
6464 ui_out_text (uiout, " hits\n");
6465 }
6466
f196051f
SS
6467 if (!part_of_multiple && is_tracepoint (b))
6468 {
6469 struct tracepoint *tp = (struct tracepoint *) b;
6470
6471 if (tp->traceframe_usage)
6472 {
6473 ui_out_text (uiout, "\ttrace buffer usage ");
6474 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6475 ui_out_text (uiout, " bytes\n");
6476 }
6477 }
d3ce09f5 6478
9add0f1b 6479 l = b->commands ? b->commands->commands : NULL;
059fb39f 6480 if (!part_of_multiple && l)
c4093a6a 6481 {
3b31d625
EZ
6482 struct cleanup *script_chain;
6483
c4093a6a 6484 annotate_field (9);
3b31d625 6485 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6486 print_command_lines (uiout, l, 4);
3b31d625 6487 do_cleanups (script_chain);
c4093a6a 6488 }
d24317b4 6489
d9b3f62e 6490 if (is_tracepoint (b))
1042e4c0 6491 {
d9b3f62e
PA
6492 struct tracepoint *t = (struct tracepoint *) b;
6493
6494 if (!part_of_multiple && t->pass_count)
6495 {
6496 annotate_field (10);
6497 ui_out_text (uiout, "\tpass count ");
6498 ui_out_field_int (uiout, "pass", t->pass_count);
6499 ui_out_text (uiout, " \n");
6500 }
f2a8bc8a
YQ
6501
6502 /* Don't display it when tracepoint or tracepoint location is
6503 pending. */
6504 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6505 {
6506 annotate_field (11);
6507
6508 if (ui_out_is_mi_like_p (uiout))
6509 ui_out_field_string (uiout, "installed",
6510 loc->inserted ? "y" : "n");
6511 else
6512 {
6513 if (loc->inserted)
6514 ui_out_text (uiout, "\t");
6515 else
6516 ui_out_text (uiout, "\tnot ");
6517 ui_out_text (uiout, "installed on target\n");
6518 }
6519 }
1042e4c0
SS
6520 }
6521
d24317b4
VP
6522 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6523 {
3a5c3e22
PA
6524 if (is_watchpoint (b))
6525 {
6526 struct watchpoint *w = (struct watchpoint *) b;
6527
6528 ui_out_field_string (uiout, "original-location", w->exp_string);
6529 }
6530 else if (b->addr_string)
d24317b4 6531 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6532 }
c4093a6a 6533}
c5aa993b 6534
0d381245
VP
6535static void
6536print_one_breakpoint (struct breakpoint *b,
4a64f543 6537 struct bp_location **last_loc,
6c95b8df 6538 int allflag)
0d381245 6539{
8d3788bd 6540 struct cleanup *bkpt_chain;
79a45e25 6541 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6542
6543 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6544
12c5a436 6545 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6546 do_cleanups (bkpt_chain);
0d381245
VP
6547
6548 /* If this breakpoint has custom print function,
6549 it's already printed. Otherwise, print individual
6550 locations, if any. */
6551 if (b->ops == NULL || b->ops->print_one == NULL)
6552 {
4a64f543
MS
6553 /* If breakpoint has a single location that is disabled, we
6554 print it as if it had several locations, since otherwise it's
6555 hard to represent "breakpoint enabled, location disabled"
6556 situation.
6557
6558 Note that while hardware watchpoints have several locations
a3be7890 6559 internally, that's not a property exposed to user. */
0d381245 6560 if (b->loc
a5606eee 6561 && !is_hardware_watchpoint (b)
8d3788bd 6562 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6563 {
6564 struct bp_location *loc;
6565 int n = 1;
8d3788bd 6566
0d381245 6567 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6568 {
6569 struct cleanup *inner2 =
6570 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6571 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6572 do_cleanups (inner2);
6573 }
0d381245
VP
6574 }
6575 }
6576}
6577
a6d9a66e
UW
6578static int
6579breakpoint_address_bits (struct breakpoint *b)
6580{
6581 int print_address_bits = 0;
6582 struct bp_location *loc;
6583
6584 for (loc = b->loc; loc; loc = loc->next)
6585 {
c7437ca6
PA
6586 int addr_bit;
6587
6588 /* Software watchpoints that aren't watching memory don't have
6589 an address to print. */
6590 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6591 continue;
6592
6593 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6594 if (addr_bit > print_address_bits)
6595 print_address_bits = addr_bit;
6596 }
6597
6598 return print_address_bits;
6599}
0d381245 6600
c4093a6a
JM
6601struct captured_breakpoint_query_args
6602 {
6603 int bnum;
6604 };
c5aa993b 6605
c4093a6a 6606static int
2b65245e 6607do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6608{
6609 struct captured_breakpoint_query_args *args = data;
52f0bd74 6610 struct breakpoint *b;
a6d9a66e 6611 struct bp_location *dummy_loc = NULL;
cc59ec59 6612
c4093a6a
JM
6613 ALL_BREAKPOINTS (b)
6614 {
6615 if (args->bnum == b->number)
c5aa993b 6616 {
12c5a436 6617 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6618 return GDB_RC_OK;
c5aa993b 6619 }
c4093a6a
JM
6620 }
6621 return GDB_RC_NONE;
6622}
c5aa993b 6623
c4093a6a 6624enum gdb_rc
4a64f543
MS
6625gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6626 char **error_message)
c4093a6a
JM
6627{
6628 struct captured_breakpoint_query_args args;
cc59ec59 6629
c4093a6a
JM
6630 args.bnum = bnum;
6631 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6632 an error. */
b0b13bb4
DJ
6633 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6634 error_message, RETURN_MASK_ALL) < 0)
6635 return GDB_RC_FAIL;
6636 else
6637 return GDB_RC_OK;
c4093a6a 6638}
c5aa993b 6639
09d682a4
TT
6640/* Return true if this breakpoint was set by the user, false if it is
6641 internal or momentary. */
6642
6643int
6644user_breakpoint_p (struct breakpoint *b)
6645{
46c6471b 6646 return b->number > 0;
09d682a4
TT
6647}
6648
7f3b0473 6649/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6650 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6651 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6652 FILTER is non-NULL, call it on each breakpoint and only include the
6653 ones for which it returns non-zero. Return the total number of
6654 breakpoints listed. */
c906108c 6655
d77f58be 6656static int
e5a67952 6657breakpoint_1 (char *args, int allflag,
4a64f543 6658 int (*filter) (const struct breakpoint *))
c4093a6a 6659{
52f0bd74 6660 struct breakpoint *b;
a6d9a66e 6661 struct bp_location *last_loc = NULL;
7f3b0473 6662 int nr_printable_breakpoints;
3b31d625 6663 struct cleanup *bkpttbl_chain;
79a45b7d 6664 struct value_print_options opts;
a6d9a66e 6665 int print_address_bits = 0;
269b11a2 6666 int print_type_col_width = 14;
79a45e25 6667 struct ui_out *uiout = current_uiout;
269b11a2 6668
79a45b7d
TT
6669 get_user_print_options (&opts);
6670
4a64f543
MS
6671 /* Compute the number of rows in the table, as well as the size
6672 required for address fields. */
7f3b0473
AC
6673 nr_printable_breakpoints = 0;
6674 ALL_BREAKPOINTS (b)
e5a67952
MS
6675 {
6676 /* If we have a filter, only list the breakpoints it accepts. */
6677 if (filter && !filter (b))
6678 continue;
6679
6680 /* If we have an "args" string, it is a list of breakpoints to
6681 accept. Skip the others. */
6682 if (args != NULL && *args != '\0')
6683 {
6684 if (allflag && parse_and_eval_long (args) != b->number)
6685 continue;
6686 if (!allflag && !number_is_in_list (args, b->number))
6687 continue;
6688 }
269b11a2 6689
e5a67952
MS
6690 if (allflag || user_breakpoint_p (b))
6691 {
6692 int addr_bit, type_len;
a6d9a66e 6693
e5a67952
MS
6694 addr_bit = breakpoint_address_bits (b);
6695 if (addr_bit > print_address_bits)
6696 print_address_bits = addr_bit;
269b11a2 6697
e5a67952
MS
6698 type_len = strlen (bptype_string (b->type));
6699 if (type_len > print_type_col_width)
6700 print_type_col_width = type_len;
6701
6702 nr_printable_breakpoints++;
6703 }
6704 }
7f3b0473 6705
79a45b7d 6706 if (opts.addressprint)
3b31d625 6707 bkpttbl_chain
3e43a32a
MS
6708 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6709 nr_printable_breakpoints,
3b31d625 6710 "BreakpointTable");
8b93c638 6711 else
3b31d625 6712 bkpttbl_chain
3e43a32a
MS
6713 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6714 nr_printable_breakpoints,
3b31d625 6715 "BreakpointTable");
8b93c638 6716
7f3b0473 6717 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6718 annotate_breakpoints_headers ();
6719 if (nr_printable_breakpoints > 0)
6720 annotate_field (0);
4a64f543 6721 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6722 if (nr_printable_breakpoints > 0)
6723 annotate_field (1);
269b11a2 6724 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6725 "type", "Type"); /* 2 */
d7faa9e7
AC
6726 if (nr_printable_breakpoints > 0)
6727 annotate_field (2);
4a64f543 6728 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6729 if (nr_printable_breakpoints > 0)
6730 annotate_field (3);
54e52265 6731 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6732 if (opts.addressprint)
e5a67952
MS
6733 {
6734 if (nr_printable_breakpoints > 0)
6735 annotate_field (4);
6736 if (print_address_bits <= 32)
6737 ui_out_table_header (uiout, 10, ui_left,
6738 "addr", "Address"); /* 5 */
6739 else
6740 ui_out_table_header (uiout, 18, ui_left,
6741 "addr", "Address"); /* 5 */
6742 }
d7faa9e7
AC
6743 if (nr_printable_breakpoints > 0)
6744 annotate_field (5);
6745 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6746 ui_out_table_body (uiout);
6747 if (nr_printable_breakpoints > 0)
6748 annotate_breakpoints_table ();
7f3b0473 6749
c4093a6a 6750 ALL_BREAKPOINTS (b)
e5a67952
MS
6751 {
6752 QUIT;
6753 /* If we have a filter, only list the breakpoints it accepts. */
6754 if (filter && !filter (b))
6755 continue;
6756
6757 /* If we have an "args" string, it is a list of breakpoints to
6758 accept. Skip the others. */
6759
6760 if (args != NULL && *args != '\0')
6761 {
6762 if (allflag) /* maintenance info breakpoint */
6763 {
6764 if (parse_and_eval_long (args) != b->number)
6765 continue;
6766 }
6767 else /* all others */
6768 {
6769 if (!number_is_in_list (args, b->number))
6770 continue;
6771 }
6772 }
6773 /* We only print out user settable breakpoints unless the
6774 allflag is set. */
6775 if (allflag || user_breakpoint_p (b))
12c5a436 6776 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6777 }
6778
3b31d625 6779 do_cleanups (bkpttbl_chain);
698384cd 6780
7f3b0473 6781 if (nr_printable_breakpoints == 0)
c906108c 6782 {
4a64f543
MS
6783 /* If there's a filter, let the caller decide how to report
6784 empty list. */
d77f58be
SS
6785 if (!filter)
6786 {
e5a67952 6787 if (args == NULL || *args == '\0')
d77f58be
SS
6788 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6789 else
4a64f543 6790 ui_out_message (uiout, 0,
e5a67952
MS
6791 "No breakpoint or watchpoint matching '%s'.\n",
6792 args);
d77f58be 6793 }
c906108c
SS
6794 }
6795 else
c4093a6a 6796 {
a6d9a66e
UW
6797 if (last_loc && !server_command)
6798 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6799 }
c906108c 6800
4a64f543 6801 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6802 there have been breakpoints? */
c906108c 6803 annotate_breakpoints_table_end ();
d77f58be
SS
6804
6805 return nr_printable_breakpoints;
c906108c
SS
6806}
6807
ad443146
SS
6808/* Display the value of default-collect in a way that is generally
6809 compatible with the breakpoint list. */
6810
6811static void
6812default_collect_info (void)
6813{
79a45e25
PA
6814 struct ui_out *uiout = current_uiout;
6815
ad443146
SS
6816 /* If it has no value (which is frequently the case), say nothing; a
6817 message like "No default-collect." gets in user's face when it's
6818 not wanted. */
6819 if (!*default_collect)
6820 return;
6821
6822 /* The following phrase lines up nicely with per-tracepoint collect
6823 actions. */
6824 ui_out_text (uiout, "default collect ");
6825 ui_out_field_string (uiout, "default-collect", default_collect);
6826 ui_out_text (uiout, " \n");
6827}
6828
c906108c 6829static void
e5a67952 6830breakpoints_info (char *args, int from_tty)
c906108c 6831{
e5a67952 6832 breakpoint_1 (args, 0, NULL);
ad443146
SS
6833
6834 default_collect_info ();
d77f58be
SS
6835}
6836
6837static void
e5a67952 6838watchpoints_info (char *args, int from_tty)
d77f58be 6839{
e5a67952 6840 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6841 struct ui_out *uiout = current_uiout;
d77f58be
SS
6842
6843 if (num_printed == 0)
6844 {
e5a67952 6845 if (args == NULL || *args == '\0')
d77f58be
SS
6846 ui_out_message (uiout, 0, "No watchpoints.\n");
6847 else
e5a67952 6848 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6849 }
c906108c
SS
6850}
6851
7a292a7a 6852static void
e5a67952 6853maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6854{
e5a67952 6855 breakpoint_1 (args, 1, NULL);
ad443146
SS
6856
6857 default_collect_info ();
c906108c
SS
6858}
6859
0d381245 6860static int
714835d5 6861breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6862 struct program_space *pspace,
714835d5 6863 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6864{
6865 struct bp_location *bl = b->loc;
cc59ec59 6866
0d381245
VP
6867 for (; bl; bl = bl->next)
6868 {
6c95b8df
PA
6869 if (bl->pspace == pspace
6870 && bl->address == pc
0d381245
VP
6871 && (!overlay_debugging || bl->section == section))
6872 return 1;
6873 }
6874 return 0;
6875}
6876
672f9b60 6877/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6878 concerns with logical breakpoints, so we match program spaces, not
6879 address spaces. */
c906108c
SS
6880
6881static void
6c95b8df
PA
6882describe_other_breakpoints (struct gdbarch *gdbarch,
6883 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6884 struct obj_section *section, int thread)
c906108c 6885{
52f0bd74
AC
6886 int others = 0;
6887 struct breakpoint *b;
c906108c
SS
6888
6889 ALL_BREAKPOINTS (b)
672f9b60
KP
6890 others += (user_breakpoint_p (b)
6891 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6892 if (others > 0)
6893 {
a3f17187
AC
6894 if (others == 1)
6895 printf_filtered (_("Note: breakpoint "));
6896 else /* if (others == ???) */
6897 printf_filtered (_("Note: breakpoints "));
c906108c 6898 ALL_BREAKPOINTS (b)
672f9b60 6899 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6900 {
6901 others--;
6902 printf_filtered ("%d", b->number);
6903 if (b->thread == -1 && thread != -1)
6904 printf_filtered (" (all threads)");
6905 else if (b->thread != -1)
6906 printf_filtered (" (thread %d)", b->thread);
6907 printf_filtered ("%s%s ",
059fb39f 6908 ((b->enable_state == bp_disabled
f8eba3c6 6909 || b->enable_state == bp_call_disabled)
0d381245 6910 ? " (disabled)"
0d381245
VP
6911 : ""),
6912 (others > 1) ? ","
6913 : ((others == 1) ? " and" : ""));
6914 }
a3f17187 6915 printf_filtered (_("also set at pc "));
5af949e3 6916 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6917 printf_filtered (".\n");
6918 }
6919}
6920\f
c906108c 6921
e4f237da
KB
6922/* Return true iff it is meaningful to use the address member of
6923 BPT. For some breakpoint types, the address member is irrelevant
6924 and it makes no sense to attempt to compare it to other addresses
6925 (or use it for any other purpose either).
6926
4a64f543
MS
6927 More specifically, each of the following breakpoint types will
6928 always have a zero valued address and we don't want to mark
6929 breakpoints of any of these types to be a duplicate of an actual
6930 breakpoint at address zero:
e4f237da
KB
6931
6932 bp_watchpoint
2d134ed3
PA
6933 bp_catchpoint
6934
6935*/
e4f237da
KB
6936
6937static int
6938breakpoint_address_is_meaningful (struct breakpoint *bpt)
6939{
6940 enum bptype type = bpt->type;
6941
2d134ed3
PA
6942 return (type != bp_watchpoint && type != bp_catchpoint);
6943}
6944
6945/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6946 true if LOC1 and LOC2 represent the same watchpoint location. */
6947
6948static int
4a64f543
MS
6949watchpoint_locations_match (struct bp_location *loc1,
6950 struct bp_location *loc2)
2d134ed3 6951{
3a5c3e22
PA
6952 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6953 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6954
6955 /* Both of them must exist. */
6956 gdb_assert (w1 != NULL);
6957 gdb_assert (w2 != NULL);
2bdf28a0 6958
4a64f543
MS
6959 /* If the target can evaluate the condition expression in hardware,
6960 then we we need to insert both watchpoints even if they are at
6961 the same place. Otherwise the watchpoint will only trigger when
6962 the condition of whichever watchpoint was inserted evaluates to
6963 true, not giving a chance for GDB to check the condition of the
6964 other watchpoint. */
3a5c3e22 6965 if ((w1->cond_exp
4a64f543
MS
6966 && target_can_accel_watchpoint_condition (loc1->address,
6967 loc1->length,
0cf6dd15 6968 loc1->watchpoint_type,
3a5c3e22
PA
6969 w1->cond_exp))
6970 || (w2->cond_exp
4a64f543
MS
6971 && target_can_accel_watchpoint_condition (loc2->address,
6972 loc2->length,
0cf6dd15 6973 loc2->watchpoint_type,
3a5c3e22 6974 w2->cond_exp)))
0cf6dd15
TJB
6975 return 0;
6976
85d721b8
PA
6977 /* Note that this checks the owner's type, not the location's. In
6978 case the target does not support read watchpoints, but does
6979 support access watchpoints, we'll have bp_read_watchpoint
6980 watchpoints with hw_access locations. Those should be considered
6981 duplicates of hw_read locations. The hw_read locations will
6982 become hw_access locations later. */
2d134ed3
PA
6983 return (loc1->owner->type == loc2->owner->type
6984 && loc1->pspace->aspace == loc2->pspace->aspace
6985 && loc1->address == loc2->address
6986 && loc1->length == loc2->length);
e4f237da
KB
6987}
6988
31e77af2 6989/* See breakpoint.h. */
6c95b8df 6990
31e77af2 6991int
6c95b8df
PA
6992breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6993 struct address_space *aspace2, CORE_ADDR addr2)
6994{
f5656ead 6995 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6996 || aspace1 == aspace2)
6997 && addr1 == addr2);
6998}
6999
f1310107
TJB
7000/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7001 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7002 matches ASPACE2. On targets that have global breakpoints, the address
7003 space doesn't really matter. */
7004
7005static int
7006breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7007 int len1, struct address_space *aspace2,
7008 CORE_ADDR addr2)
7009{
f5656ead 7010 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7011 || aspace1 == aspace2)
7012 && addr2 >= addr1 && addr2 < addr1 + len1);
7013}
7014
7015/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7016 a ranged breakpoint. In most targets, a match happens only if ASPACE
7017 matches the breakpoint's address space. On targets that have global
7018 breakpoints, the address space doesn't really matter. */
7019
7020static int
7021breakpoint_location_address_match (struct bp_location *bl,
7022 struct address_space *aspace,
7023 CORE_ADDR addr)
7024{
7025 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7026 aspace, addr)
7027 || (bl->length
7028 && breakpoint_address_match_range (bl->pspace->aspace,
7029 bl->address, bl->length,
7030 aspace, addr)));
7031}
7032
1e4d1764
YQ
7033/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7034 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7035 true, otherwise returns false. */
7036
7037static int
7038tracepoint_locations_match (struct bp_location *loc1,
7039 struct bp_location *loc2)
7040{
7041 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7042 /* Since tracepoint locations are never duplicated with others', tracepoint
7043 locations at the same address of different tracepoints are regarded as
7044 different locations. */
7045 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7046 else
7047 return 0;
7048}
7049
2d134ed3
PA
7050/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7051 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7052 represent the same location. */
7053
7054static int
4a64f543
MS
7055breakpoint_locations_match (struct bp_location *loc1,
7056 struct bp_location *loc2)
2d134ed3 7057{
2bdf28a0
JK
7058 int hw_point1, hw_point2;
7059
7060 /* Both of them must not be in moribund_locations. */
7061 gdb_assert (loc1->owner != NULL);
7062 gdb_assert (loc2->owner != NULL);
7063
7064 hw_point1 = is_hardware_watchpoint (loc1->owner);
7065 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7066
7067 if (hw_point1 != hw_point2)
7068 return 0;
7069 else if (hw_point1)
7070 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7071 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7072 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7073 else
f1310107
TJB
7074 /* We compare bp_location.length in order to cover ranged breakpoints. */
7075 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7076 loc2->pspace->aspace, loc2->address)
7077 && loc1->length == loc2->length);
2d134ed3
PA
7078}
7079
76897487
KB
7080static void
7081breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7082 int bnum, int have_bnum)
7083{
f63fbe86
MS
7084 /* The longest string possibly returned by hex_string_custom
7085 is 50 chars. These must be at least that big for safety. */
7086 char astr1[64];
7087 char astr2[64];
76897487 7088
bb599908
PH
7089 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7090 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7091 if (have_bnum)
8a3fe4f8 7092 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7093 bnum, astr1, astr2);
7094 else
8a3fe4f8 7095 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7096}
7097
4a64f543
MS
7098/* Adjust a breakpoint's address to account for architectural
7099 constraints on breakpoint placement. Return the adjusted address.
7100 Note: Very few targets require this kind of adjustment. For most
7101 targets, this function is simply the identity function. */
76897487
KB
7102
7103static CORE_ADDR
a6d9a66e
UW
7104adjust_breakpoint_address (struct gdbarch *gdbarch,
7105 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7106{
a6d9a66e 7107 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7108 {
7109 /* Very few targets need any kind of breakpoint adjustment. */
7110 return bpaddr;
7111 }
88f7da05
KB
7112 else if (bptype == bp_watchpoint
7113 || bptype == bp_hardware_watchpoint
7114 || bptype == bp_read_watchpoint
7115 || bptype == bp_access_watchpoint
fe798b75 7116 || bptype == bp_catchpoint)
88f7da05
KB
7117 {
7118 /* Watchpoints and the various bp_catch_* eventpoints should not
7119 have their addresses modified. */
7120 return bpaddr;
7121 }
7c16b83e
PA
7122 else if (bptype == bp_single_step)
7123 {
7124 /* Single-step breakpoints should not have their addresses
7125 modified. If there's any architectural constrain that
7126 applies to this address, then it should have already been
7127 taken into account when the breakpoint was created in the
7128 first place. If we didn't do this, stepping through e.g.,
7129 Thumb-2 IT blocks would break. */
7130 return bpaddr;
7131 }
76897487
KB
7132 else
7133 {
7134 CORE_ADDR adjusted_bpaddr;
7135
7136 /* Some targets have architectural constraints on the placement
7137 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7138 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7139
7140 /* An adjusted breakpoint address can significantly alter
7141 a user's expectations. Print a warning if an adjustment
7142 is required. */
7143 if (adjusted_bpaddr != bpaddr)
7144 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7145
7146 return adjusted_bpaddr;
7147 }
7148}
7149
28010a5d
PA
7150void
7151init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7152 struct breakpoint *owner)
7cc221ef 7153{
7cc221ef
DJ
7154 memset (loc, 0, sizeof (*loc));
7155
348d480f
PA
7156 gdb_assert (ops != NULL);
7157
28010a5d
PA
7158 loc->ops = ops;
7159 loc->owner = owner;
511a6cd4 7160 loc->cond = NULL;
b775012e 7161 loc->cond_bytecode = NULL;
0d381245
VP
7162 loc->shlib_disabled = 0;
7163 loc->enabled = 1;
e049a4b5 7164
28010a5d 7165 switch (owner->type)
e049a4b5
DJ
7166 {
7167 case bp_breakpoint:
7c16b83e 7168 case bp_single_step:
e049a4b5
DJ
7169 case bp_until:
7170 case bp_finish:
7171 case bp_longjmp:
7172 case bp_longjmp_resume:
e2e4d78b 7173 case bp_longjmp_call_dummy:
186c406b
TT
7174 case bp_exception:
7175 case bp_exception_resume:
e049a4b5 7176 case bp_step_resume:
2c03e5be 7177 case bp_hp_step_resume:
e049a4b5
DJ
7178 case bp_watchpoint_scope:
7179 case bp_call_dummy:
aa7d318d 7180 case bp_std_terminate:
e049a4b5
DJ
7181 case bp_shlib_event:
7182 case bp_thread_event:
7183 case bp_overlay_event:
4efc6507 7184 case bp_jit_event:
0fd8e87f 7185 case bp_longjmp_master:
aa7d318d 7186 case bp_std_terminate_master:
186c406b 7187 case bp_exception_master:
0e30163f
JK
7188 case bp_gnu_ifunc_resolver:
7189 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7190 case bp_dprintf:
e049a4b5 7191 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7192 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7193 break;
7194 case bp_hardware_breakpoint:
7195 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7196 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7197 break;
7198 case bp_hardware_watchpoint:
7199 case bp_read_watchpoint:
7200 case bp_access_watchpoint:
7201 loc->loc_type = bp_loc_hardware_watchpoint;
7202 break;
7203 case bp_watchpoint:
ce78b96d 7204 case bp_catchpoint:
15c3d785
PA
7205 case bp_tracepoint:
7206 case bp_fast_tracepoint:
0fb4aa4b 7207 case bp_static_tracepoint:
e049a4b5
DJ
7208 loc->loc_type = bp_loc_other;
7209 break;
7210 default:
e2e0b3e5 7211 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7212 }
7213
f431efe5 7214 loc->refc = 1;
28010a5d
PA
7215}
7216
7217/* Allocate a struct bp_location. */
7218
7219static struct bp_location *
7220allocate_bp_location (struct breakpoint *bpt)
7221{
348d480f
PA
7222 return bpt->ops->allocate_location (bpt);
7223}
7cc221ef 7224
f431efe5
PA
7225static void
7226free_bp_location (struct bp_location *loc)
fe3f5fa8 7227{
348d480f 7228 loc->ops->dtor (loc);
fe3f5fa8
VP
7229 xfree (loc);
7230}
7231
f431efe5
PA
7232/* Increment reference count. */
7233
7234static void
7235incref_bp_location (struct bp_location *bl)
7236{
7237 ++bl->refc;
7238}
7239
7240/* Decrement reference count. If the reference count reaches 0,
7241 destroy the bp_location. Sets *BLP to NULL. */
7242
7243static void
7244decref_bp_location (struct bp_location **blp)
7245{
0807b50c
PA
7246 gdb_assert ((*blp)->refc > 0);
7247
f431efe5
PA
7248 if (--(*blp)->refc == 0)
7249 free_bp_location (*blp);
7250 *blp = NULL;
7251}
7252
346774a9 7253/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7254
346774a9
PA
7255static void
7256add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7257{
346774a9 7258 struct breakpoint *b1;
c906108c 7259
346774a9
PA
7260 /* Add this breakpoint to the end of the chain so that a list of
7261 breakpoints will come out in order of increasing numbers. */
7262
7263 b1 = breakpoint_chain;
7264 if (b1 == 0)
7265 breakpoint_chain = b;
7266 else
7267 {
7268 while (b1->next)
7269 b1 = b1->next;
7270 b1->next = b;
7271 }
7272}
7273
7274/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7275
7276static void
7277init_raw_breakpoint_without_location (struct breakpoint *b,
7278 struct gdbarch *gdbarch,
28010a5d 7279 enum bptype bptype,
c0a91b2b 7280 const struct breakpoint_ops *ops)
346774a9 7281{
c906108c 7282 memset (b, 0, sizeof (*b));
2219d63c 7283
348d480f
PA
7284 gdb_assert (ops != NULL);
7285
28010a5d 7286 b->ops = ops;
4d28f7a8 7287 b->type = bptype;
a6d9a66e 7288 b->gdbarch = gdbarch;
c906108c
SS
7289 b->language = current_language->la_language;
7290 b->input_radix = input_radix;
7291 b->thread = -1;
b5de0fa7 7292 b->enable_state = bp_enabled;
c906108c
SS
7293 b->next = 0;
7294 b->silent = 0;
7295 b->ignore_count = 0;
7296 b->commands = NULL;
818dd999 7297 b->frame_id = null_frame_id;
0d381245 7298 b->condition_not_parsed = 0;
84f4c1fe 7299 b->py_bp_object = NULL;
d0fb5eae 7300 b->related_breakpoint = b;
346774a9
PA
7301}
7302
7303/* Helper to set_raw_breakpoint below. Creates a breakpoint
7304 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7305
7306static struct breakpoint *
7307set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7308 enum bptype bptype,
c0a91b2b 7309 const struct breakpoint_ops *ops)
346774a9
PA
7310{
7311 struct breakpoint *b = XNEW (struct breakpoint);
7312
348d480f 7313 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7314 add_to_breakpoint_chain (b);
0d381245
VP
7315 return b;
7316}
7317
0e30163f
JK
7318/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7319 resolutions should be made as the user specified the location explicitly
7320 enough. */
7321
0d381245 7322static void
0e30163f 7323set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7324{
2bdf28a0
JK
7325 gdb_assert (loc->owner != NULL);
7326
0d381245 7327 if (loc->owner->type == bp_breakpoint
1042e4c0 7328 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7329 || is_tracepoint (loc->owner))
0d381245 7330 {
0e30163f 7331 int is_gnu_ifunc;
2c02bd72 7332 const char *function_name;
6a3a010b 7333 CORE_ADDR func_addr;
0e30163f 7334
2c02bd72 7335 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7336 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7337
7338 if (is_gnu_ifunc && !explicit_loc)
7339 {
7340 struct breakpoint *b = loc->owner;
7341
7342 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7343 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7344 &loc->requested_address))
7345 {
7346 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7347 loc->address = adjust_breakpoint_address (loc->gdbarch,
7348 loc->requested_address,
7349 b->type);
7350 }
7351 else if (b->type == bp_breakpoint && b->loc == loc
7352 && loc->next == NULL && b->related_breakpoint == b)
7353 {
7354 /* Create only the whole new breakpoint of this type but do not
7355 mess more complicated breakpoints with multiple locations. */
7356 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7357 /* Remember the resolver's address for use by the return
7358 breakpoint. */
7359 loc->related_address = func_addr;
0e30163f
JK
7360 }
7361 }
7362
2c02bd72
DE
7363 if (function_name)
7364 loc->function_name = xstrdup (function_name);
0d381245
VP
7365 }
7366}
7367
a6d9a66e 7368/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7369struct gdbarch *
a6d9a66e
UW
7370get_sal_arch (struct symtab_and_line sal)
7371{
7372 if (sal.section)
7373 return get_objfile_arch (sal.section->objfile);
7374 if (sal.symtab)
eb822aa6 7375 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7376
7377 return NULL;
7378}
7379
346774a9
PA
7380/* Low level routine for partially initializing a breakpoint of type
7381 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7382 file name, and line number are provided by SAL.
0d381245
VP
7383
7384 It is expected that the caller will complete the initialization of
7385 the newly created breakpoint struct as well as output any status
c56053d2 7386 information regarding the creation of a new breakpoint. */
0d381245 7387
346774a9
PA
7388static void
7389init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7390 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7391 const struct breakpoint_ops *ops)
0d381245 7392{
28010a5d 7393 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7394
3742cc8b 7395 add_location_to_breakpoint (b, &sal);
0d381245 7396
6c95b8df
PA
7397 if (bptype != bp_catchpoint)
7398 gdb_assert (sal.pspace != NULL);
7399
f8eba3c6
TT
7400 /* Store the program space that was used to set the breakpoint,
7401 except for ordinary breakpoints, which are independent of the
7402 program space. */
7403 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7404 b->pspace = sal.pspace;
346774a9 7405}
c906108c 7406
346774a9
PA
7407/* set_raw_breakpoint is a low level routine for allocating and
7408 partially initializing a breakpoint of type BPTYPE. The newly
7409 created breakpoint's address, section, source file name, and line
7410 number are provided by SAL. The newly created and partially
7411 initialized breakpoint is added to the breakpoint chain and
7412 is also returned as the value of this function.
7413
7414 It is expected that the caller will complete the initialization of
7415 the newly created breakpoint struct as well as output any status
7416 information regarding the creation of a new breakpoint. In
7417 particular, set_raw_breakpoint does NOT set the breakpoint
7418 number! Care should be taken to not allow an error to occur
7419 prior to completing the initialization of the breakpoint. If this
7420 should happen, a bogus breakpoint will be left on the chain. */
7421
7422struct breakpoint *
7423set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7424 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7425 const struct breakpoint_ops *ops)
346774a9
PA
7426{
7427 struct breakpoint *b = XNEW (struct breakpoint);
7428
348d480f 7429 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7430 add_to_breakpoint_chain (b);
c906108c
SS
7431 return b;
7432}
7433
c2c6d25f
JM
7434
7435/* Note that the breakpoint object B describes a permanent breakpoint
7436 instruction, hard-wired into the inferior's code. */
7437void
7438make_breakpoint_permanent (struct breakpoint *b)
7439{
0d381245 7440 struct bp_location *bl;
cc59ec59 7441
4a64f543
MS
7442 /* By definition, permanent breakpoints are already present in the
7443 code. Mark all locations as inserted. For now,
7444 make_breakpoint_permanent is called in just one place, so it's
7445 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7446 multiple locations or not, but it's easy to implement. */
0d381245 7447 for (bl = b->loc; bl; bl = bl->next)
1a853c52
PA
7448 {
7449 bl->permanent = 1;
7450 bl->inserted = 1;
7451 }
c2c6d25f
JM
7452}
7453
53a5351d 7454/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7455 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7456 initiated the operation. */
c906108c
SS
7457
7458void
186c406b 7459set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7460{
35df4500 7461 struct breakpoint *b, *b_tmp;
186c406b 7462 int thread = tp->num;
0fd8e87f
UW
7463
7464 /* To avoid having to rescan all objfile symbols at every step,
7465 we maintain a list of continually-inserted but always disabled
7466 longjmp "master" breakpoints. Here, we simply create momentary
7467 clones of those and enable them for the requested thread. */
35df4500 7468 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7469 if (b->pspace == current_program_space
186c406b
TT
7470 && (b->type == bp_longjmp_master
7471 || b->type == bp_exception_master))
0fd8e87f 7472 {
06edf0c0
PA
7473 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7474 struct breakpoint *clone;
cc59ec59 7475
e2e4d78b
JK
7476 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7477 after their removal. */
06edf0c0 7478 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7479 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7480 clone->thread = thread;
7481 }
186c406b
TT
7482
7483 tp->initiating_frame = frame;
c906108c
SS
7484}
7485
611c83ae 7486/* Delete all longjmp breakpoints from THREAD. */
c906108c 7487void
611c83ae 7488delete_longjmp_breakpoint (int thread)
c906108c 7489{
35df4500 7490 struct breakpoint *b, *b_tmp;
c906108c 7491
35df4500 7492 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7493 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7494 {
7495 if (b->thread == thread)
7496 delete_breakpoint (b);
7497 }
c906108c
SS
7498}
7499
f59f708a
PA
7500void
7501delete_longjmp_breakpoint_at_next_stop (int thread)
7502{
7503 struct breakpoint *b, *b_tmp;
7504
7505 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7506 if (b->type == bp_longjmp || b->type == bp_exception)
7507 {
7508 if (b->thread == thread)
7509 b->disposition = disp_del_at_next_stop;
7510 }
7511}
7512
e2e4d78b
JK
7513/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7514 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7515 pointer to any of them. Return NULL if this system cannot place longjmp
7516 breakpoints. */
7517
7518struct breakpoint *
7519set_longjmp_breakpoint_for_call_dummy (void)
7520{
7521 struct breakpoint *b, *retval = NULL;
7522
7523 ALL_BREAKPOINTS (b)
7524 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7525 {
7526 struct breakpoint *new_b;
7527
7528 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7529 &momentary_breakpoint_ops,
7530 1);
e2e4d78b
JK
7531 new_b->thread = pid_to_thread_id (inferior_ptid);
7532
7533 /* Link NEW_B into the chain of RETVAL breakpoints. */
7534
7535 gdb_assert (new_b->related_breakpoint == new_b);
7536 if (retval == NULL)
7537 retval = new_b;
7538 new_b->related_breakpoint = retval;
7539 while (retval->related_breakpoint != new_b->related_breakpoint)
7540 retval = retval->related_breakpoint;
7541 retval->related_breakpoint = new_b;
7542 }
7543
7544 return retval;
7545}
7546
7547/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7548 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7549 stack.
7550
7551 You should call this function only at places where it is safe to currently
7552 unwind the whole stack. Failed stack unwind would discard live dummy
7553 frames. */
7554
7555void
b67a2c6f 7556check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7557{
7558 struct breakpoint *b, *b_tmp;
7559
7560 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7561 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7562 {
7563 struct breakpoint *dummy_b = b->related_breakpoint;
7564
7565 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7566 dummy_b = dummy_b->related_breakpoint;
7567 if (dummy_b->type != bp_call_dummy
7568 || frame_find_by_id (dummy_b->frame_id) != NULL)
7569 continue;
7570
b67a2c6f 7571 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7572
7573 while (b->related_breakpoint != b)
7574 {
7575 if (b_tmp == b->related_breakpoint)
7576 b_tmp = b->related_breakpoint->next;
7577 delete_breakpoint (b->related_breakpoint);
7578 }
7579 delete_breakpoint (b);
7580 }
7581}
7582
1900040c
MS
7583void
7584enable_overlay_breakpoints (void)
7585{
52f0bd74 7586 struct breakpoint *b;
1900040c
MS
7587
7588 ALL_BREAKPOINTS (b)
7589 if (b->type == bp_overlay_event)
7590 {
7591 b->enable_state = bp_enabled;
44702360 7592 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7593 overlay_events_enabled = 1;
1900040c
MS
7594 }
7595}
7596
7597void
7598disable_overlay_breakpoints (void)
7599{
52f0bd74 7600 struct breakpoint *b;
1900040c
MS
7601
7602 ALL_BREAKPOINTS (b)
7603 if (b->type == bp_overlay_event)
7604 {
7605 b->enable_state = bp_disabled;
44702360 7606 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7607 overlay_events_enabled = 0;
1900040c
MS
7608 }
7609}
7610
aa7d318d
TT
7611/* Set an active std::terminate breakpoint for each std::terminate
7612 master breakpoint. */
7613void
7614set_std_terminate_breakpoint (void)
7615{
35df4500 7616 struct breakpoint *b, *b_tmp;
aa7d318d 7617
35df4500 7618 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7619 if (b->pspace == current_program_space
7620 && b->type == bp_std_terminate_master)
7621 {
06edf0c0 7622 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7623 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7624 }
7625}
7626
7627/* Delete all the std::terminate breakpoints. */
7628void
7629delete_std_terminate_breakpoint (void)
7630{
35df4500 7631 struct breakpoint *b, *b_tmp;
aa7d318d 7632
35df4500 7633 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7634 if (b->type == bp_std_terminate)
7635 delete_breakpoint (b);
7636}
7637
c4093a6a 7638struct breakpoint *
a6d9a66e 7639create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7640{
7641 struct breakpoint *b;
c4093a6a 7642
06edf0c0
PA
7643 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7644 &internal_breakpoint_ops);
7645
b5de0fa7 7646 b->enable_state = bp_enabled;
c4093a6a 7647 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7648 b->addr_string
7649 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7650
44702360 7651 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7652
c4093a6a
JM
7653 return b;
7654}
7655
7656void
7657remove_thread_event_breakpoints (void)
7658{
35df4500 7659 struct breakpoint *b, *b_tmp;
c4093a6a 7660
35df4500 7661 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7662 if (b->type == bp_thread_event
7663 && b->loc->pspace == current_program_space)
c4093a6a
JM
7664 delete_breakpoint (b);
7665}
7666
0101ce28
JJ
7667struct lang_and_radix
7668 {
7669 enum language lang;
7670 int radix;
7671 };
7672
4efc6507
DE
7673/* Create a breakpoint for JIT code registration and unregistration. */
7674
7675struct breakpoint *
7676create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7677{
7678 struct breakpoint *b;
7679
06edf0c0
PA
7680 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7681 &internal_breakpoint_ops);
44702360 7682 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7683 return b;
7684}
0101ce28 7685
03673fc7
PP
7686/* Remove JIT code registration and unregistration breakpoint(s). */
7687
7688void
7689remove_jit_event_breakpoints (void)
7690{
7691 struct breakpoint *b, *b_tmp;
7692
7693 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7694 if (b->type == bp_jit_event
7695 && b->loc->pspace == current_program_space)
7696 delete_breakpoint (b);
7697}
7698
cae688ec
JJ
7699void
7700remove_solib_event_breakpoints (void)
7701{
35df4500 7702 struct breakpoint *b, *b_tmp;
cae688ec 7703
35df4500 7704 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7705 if (b->type == bp_shlib_event
7706 && b->loc->pspace == current_program_space)
cae688ec
JJ
7707 delete_breakpoint (b);
7708}
7709
f37f681c
PA
7710/* See breakpoint.h. */
7711
7712void
7713remove_solib_event_breakpoints_at_next_stop (void)
7714{
7715 struct breakpoint *b, *b_tmp;
7716
7717 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7718 if (b->type == bp_shlib_event
7719 && b->loc->pspace == current_program_space)
7720 b->disposition = disp_del_at_next_stop;
7721}
7722
04086b45
PA
7723/* Helper for create_solib_event_breakpoint /
7724 create_and_insert_solib_event_breakpoint. Allows specifying which
7725 INSERT_MODE to pass through to update_global_location_list. */
7726
7727static struct breakpoint *
7728create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7729 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7730{
7731 struct breakpoint *b;
7732
06edf0c0
PA
7733 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7734 &internal_breakpoint_ops);
04086b45 7735 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7736 return b;
7737}
7738
04086b45
PA
7739struct breakpoint *
7740create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7741{
7742 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7743}
7744
f37f681c
PA
7745/* See breakpoint.h. */
7746
7747struct breakpoint *
7748create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7749{
7750 struct breakpoint *b;
7751
04086b45
PA
7752 /* Explicitly tell update_global_location_list to insert
7753 locations. */
7754 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7755 if (!b->loc->inserted)
7756 {
7757 delete_breakpoint (b);
7758 return NULL;
7759 }
7760 return b;
7761}
7762
cae688ec
JJ
7763/* Disable any breakpoints that are on code in shared libraries. Only
7764 apply to enabled breakpoints, disabled ones can just stay disabled. */
7765
7766void
cb851954 7767disable_breakpoints_in_shlibs (void)
cae688ec 7768{
876fa593 7769 struct bp_location *loc, **locp_tmp;
cae688ec 7770
876fa593 7771 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7772 {
2bdf28a0 7773 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7774 struct breakpoint *b = loc->owner;
2bdf28a0 7775
4a64f543
MS
7776 /* We apply the check to all breakpoints, including disabled for
7777 those with loc->duplicate set. This is so that when breakpoint
7778 becomes enabled, or the duplicate is removed, gdb will try to
7779 insert all breakpoints. If we don't set shlib_disabled here,
7780 we'll try to insert those breakpoints and fail. */
1042e4c0 7781 if (((b->type == bp_breakpoint)
508ccb1f 7782 || (b->type == bp_jit_event)
1042e4c0 7783 || (b->type == bp_hardware_breakpoint)
d77f58be 7784 || (is_tracepoint (b)))
6c95b8df 7785 && loc->pspace == current_program_space
0d381245 7786 && !loc->shlib_disabled
6c95b8df 7787 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7788 )
0d381245
VP
7789 {
7790 loc->shlib_disabled = 1;
7791 }
cae688ec
JJ
7792 }
7793}
7794
63644780
NB
7795/* Disable any breakpoints and tracepoints that are in SOLIB upon
7796 notification of unloaded_shlib. Only apply to enabled breakpoints,
7797 disabled ones can just stay disabled. */
84acb35a 7798
75149521 7799static void
84acb35a
JJ
7800disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7801{
876fa593 7802 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7803 int disabled_shlib_breaks = 0;
7804
c86cf029
VP
7805 /* SunOS a.out shared libraries are always mapped, so do not
7806 disable breakpoints; they will only be reported as unloaded
7807 through clear_solib when GDB discards its shared library
7808 list. See clear_solib for more information. */
7809 if (exec_bfd != NULL
7810 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7811 return;
7812
876fa593 7813 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7814 {
2bdf28a0 7815 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7816 struct breakpoint *b = loc->owner;
cc59ec59 7817
1e4d1764 7818 if (solib->pspace == loc->pspace
e2dd7057 7819 && !loc->shlib_disabled
1e4d1764
YQ
7820 && (((b->type == bp_breakpoint
7821 || b->type == bp_jit_event
7822 || b->type == bp_hardware_breakpoint)
7823 && (loc->loc_type == bp_loc_hardware_breakpoint
7824 || loc->loc_type == bp_loc_software_breakpoint))
7825 || is_tracepoint (b))
e2dd7057 7826 && solib_contains_address_p (solib, loc->address))
84acb35a 7827 {
e2dd7057
PP
7828 loc->shlib_disabled = 1;
7829 /* At this point, we cannot rely on remove_breakpoint
7830 succeeding so we must mark the breakpoint as not inserted
7831 to prevent future errors occurring in remove_breakpoints. */
7832 loc->inserted = 0;
8d3788bd
VP
7833
7834 /* This may cause duplicate notifications for the same breakpoint. */
7835 observer_notify_breakpoint_modified (b);
7836
e2dd7057
PP
7837 if (!disabled_shlib_breaks)
7838 {
7839 target_terminal_ours_for_output ();
3e43a32a
MS
7840 warning (_("Temporarily disabling breakpoints "
7841 "for unloaded shared library \"%s\""),
e2dd7057 7842 solib->so_name);
84acb35a 7843 }
e2dd7057 7844 disabled_shlib_breaks = 1;
84acb35a
JJ
7845 }
7846 }
84acb35a
JJ
7847}
7848
63644780
NB
7849/* Disable any breakpoints and tracepoints in OBJFILE upon
7850 notification of free_objfile. Only apply to enabled breakpoints,
7851 disabled ones can just stay disabled. */
7852
7853static void
7854disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7855{
7856 struct breakpoint *b;
7857
7858 if (objfile == NULL)
7859 return;
7860
d03de421
PA
7861 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7862 managed by the user with add-symbol-file/remove-symbol-file.
7863 Similarly to how breakpoints in shared libraries are handled in
7864 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7865 shlib_disabled so they end up uninserted on the next global
7866 location list update. Shared libraries not loaded by the user
7867 aren't handled here -- they're already handled in
7868 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7869 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7870 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7871 main objfile). */
7872 if ((objfile->flags & OBJF_SHARED) == 0
7873 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7874 return;
7875
7876 ALL_BREAKPOINTS (b)
7877 {
7878 struct bp_location *loc;
7879 int bp_modified = 0;
7880
7881 if (!is_breakpoint (b) && !is_tracepoint (b))
7882 continue;
7883
7884 for (loc = b->loc; loc != NULL; loc = loc->next)
7885 {
7886 CORE_ADDR loc_addr = loc->address;
7887
7888 if (loc->loc_type != bp_loc_hardware_breakpoint
7889 && loc->loc_type != bp_loc_software_breakpoint)
7890 continue;
7891
7892 if (loc->shlib_disabled != 0)
7893 continue;
7894
7895 if (objfile->pspace != loc->pspace)
7896 continue;
7897
7898 if (loc->loc_type != bp_loc_hardware_breakpoint
7899 && loc->loc_type != bp_loc_software_breakpoint)
7900 continue;
7901
7902 if (is_addr_in_objfile (loc_addr, objfile))
7903 {
7904 loc->shlib_disabled = 1;
08351840
PA
7905 /* At this point, we don't know whether the object was
7906 unmapped from the inferior or not, so leave the
7907 inserted flag alone. We'll handle failure to
7908 uninsert quietly, in case the object was indeed
7909 unmapped. */
63644780
NB
7910
7911 mark_breakpoint_location_modified (loc);
7912
7913 bp_modified = 1;
7914 }
7915 }
7916
7917 if (bp_modified)
7918 observer_notify_breakpoint_modified (b);
7919 }
7920}
7921
ce78b96d
JB
7922/* FORK & VFORK catchpoints. */
7923
e29a4733
PA
7924/* An instance of this type is used to represent a fork or vfork
7925 catchpoint. It includes a "struct breakpoint" as a kind of base
7926 class; users downcast to "struct breakpoint *" when needed. A
7927 breakpoint is really of this type iff its ops pointer points to
7928 CATCH_FORK_BREAKPOINT_OPS. */
7929
7930struct fork_catchpoint
7931{
7932 /* The base class. */
7933 struct breakpoint base;
7934
7935 /* Process id of a child process whose forking triggered this
7936 catchpoint. This field is only valid immediately after this
7937 catchpoint has triggered. */
7938 ptid_t forked_inferior_pid;
7939};
7940
4a64f543
MS
7941/* Implement the "insert" breakpoint_ops method for fork
7942 catchpoints. */
ce78b96d 7943
77b06cd7
TJB
7944static int
7945insert_catch_fork (struct bp_location *bl)
ce78b96d 7946{
dfd4cc63 7947 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7948}
7949
4a64f543
MS
7950/* Implement the "remove" breakpoint_ops method for fork
7951 catchpoints. */
ce78b96d
JB
7952
7953static int
77b06cd7 7954remove_catch_fork (struct bp_location *bl)
ce78b96d 7955{
dfd4cc63 7956 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7957}
7958
7959/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7960 catchpoints. */
7961
7962static int
f1310107 7963breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7964 struct address_space *aspace, CORE_ADDR bp_addr,
7965 const struct target_waitstatus *ws)
ce78b96d 7966{
e29a4733
PA
7967 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7968
f90263c1
TT
7969 if (ws->kind != TARGET_WAITKIND_FORKED)
7970 return 0;
7971
7972 c->forked_inferior_pid = ws->value.related_pid;
7973 return 1;
ce78b96d
JB
7974}
7975
4a64f543
MS
7976/* Implement the "print_it" breakpoint_ops method for fork
7977 catchpoints. */
ce78b96d
JB
7978
7979static enum print_stop_action
348d480f 7980print_it_catch_fork (bpstat bs)
ce78b96d 7981{
36dfb11c 7982 struct ui_out *uiout = current_uiout;
348d480f
PA
7983 struct breakpoint *b = bs->breakpoint_at;
7984 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7985
ce78b96d 7986 annotate_catchpoint (b->number);
36dfb11c
TT
7987 if (b->disposition == disp_del)
7988 ui_out_text (uiout, "\nTemporary catchpoint ");
7989 else
7990 ui_out_text (uiout, "\nCatchpoint ");
7991 if (ui_out_is_mi_like_p (uiout))
7992 {
7993 ui_out_field_string (uiout, "reason",
7994 async_reason_lookup (EXEC_ASYNC_FORK));
7995 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7996 }
7997 ui_out_field_int (uiout, "bkptno", b->number);
7998 ui_out_text (uiout, " (forked process ");
7999 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8000 ui_out_text (uiout, "), ");
ce78b96d
JB
8001 return PRINT_SRC_AND_LOC;
8002}
8003
4a64f543
MS
8004/* Implement the "print_one" breakpoint_ops method for fork
8005 catchpoints. */
ce78b96d
JB
8006
8007static void
a6d9a66e 8008print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8009{
e29a4733 8010 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8011 struct value_print_options opts;
79a45e25 8012 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8013
8014 get_user_print_options (&opts);
8015
4a64f543
MS
8016 /* Field 4, the address, is omitted (which makes the columns not
8017 line up too nicely with the headers, but the effect is relatively
8018 readable). */
79a45b7d 8019 if (opts.addressprint)
ce78b96d
JB
8020 ui_out_field_skip (uiout, "addr");
8021 annotate_field (5);
8022 ui_out_text (uiout, "fork");
e29a4733 8023 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8024 {
8025 ui_out_text (uiout, ", process ");
8026 ui_out_field_int (uiout, "what",
e29a4733 8027 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8028 ui_out_spaces (uiout, 1);
8029 }
8ac3646f
TT
8030
8031 if (ui_out_is_mi_like_p (uiout))
8032 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8033}
8034
8035/* Implement the "print_mention" breakpoint_ops method for fork
8036 catchpoints. */
8037
8038static void
8039print_mention_catch_fork (struct breakpoint *b)
8040{
8041 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8042}
8043
6149aea9
PA
8044/* Implement the "print_recreate" breakpoint_ops method for fork
8045 catchpoints. */
8046
8047static void
8048print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8049{
8050 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8051 print_recreate_thread (b, fp);
6149aea9
PA
8052}
8053
ce78b96d
JB
8054/* The breakpoint_ops structure to be used in fork catchpoints. */
8055
2060206e 8056static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8057
4a64f543
MS
8058/* Implement the "insert" breakpoint_ops method for vfork
8059 catchpoints. */
ce78b96d 8060
77b06cd7
TJB
8061static int
8062insert_catch_vfork (struct bp_location *bl)
ce78b96d 8063{
dfd4cc63 8064 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8065}
8066
4a64f543
MS
8067/* Implement the "remove" breakpoint_ops method for vfork
8068 catchpoints. */
ce78b96d
JB
8069
8070static int
77b06cd7 8071remove_catch_vfork (struct bp_location *bl)
ce78b96d 8072{
dfd4cc63 8073 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8074}
8075
8076/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8077 catchpoints. */
8078
8079static int
f1310107 8080breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8081 struct address_space *aspace, CORE_ADDR bp_addr,
8082 const struct target_waitstatus *ws)
ce78b96d 8083{
e29a4733
PA
8084 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8085
f90263c1
TT
8086 if (ws->kind != TARGET_WAITKIND_VFORKED)
8087 return 0;
8088
8089 c->forked_inferior_pid = ws->value.related_pid;
8090 return 1;
ce78b96d
JB
8091}
8092
4a64f543
MS
8093/* Implement the "print_it" breakpoint_ops method for vfork
8094 catchpoints. */
ce78b96d
JB
8095
8096static enum print_stop_action
348d480f 8097print_it_catch_vfork (bpstat bs)
ce78b96d 8098{
36dfb11c 8099 struct ui_out *uiout = current_uiout;
348d480f 8100 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8101 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8102
ce78b96d 8103 annotate_catchpoint (b->number);
36dfb11c
TT
8104 if (b->disposition == disp_del)
8105 ui_out_text (uiout, "\nTemporary catchpoint ");
8106 else
8107 ui_out_text (uiout, "\nCatchpoint ");
8108 if (ui_out_is_mi_like_p (uiout))
8109 {
8110 ui_out_field_string (uiout, "reason",
8111 async_reason_lookup (EXEC_ASYNC_VFORK));
8112 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8113 }
8114 ui_out_field_int (uiout, "bkptno", b->number);
8115 ui_out_text (uiout, " (vforked process ");
8116 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8117 ui_out_text (uiout, "), ");
ce78b96d
JB
8118 return PRINT_SRC_AND_LOC;
8119}
8120
4a64f543
MS
8121/* Implement the "print_one" breakpoint_ops method for vfork
8122 catchpoints. */
ce78b96d
JB
8123
8124static void
a6d9a66e 8125print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8126{
e29a4733 8127 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8128 struct value_print_options opts;
79a45e25 8129 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8130
8131 get_user_print_options (&opts);
4a64f543
MS
8132 /* Field 4, the address, is omitted (which makes the columns not
8133 line up too nicely with the headers, but the effect is relatively
8134 readable). */
79a45b7d 8135 if (opts.addressprint)
ce78b96d
JB
8136 ui_out_field_skip (uiout, "addr");
8137 annotate_field (5);
8138 ui_out_text (uiout, "vfork");
e29a4733 8139 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8140 {
8141 ui_out_text (uiout, ", process ");
8142 ui_out_field_int (uiout, "what",
e29a4733 8143 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8144 ui_out_spaces (uiout, 1);
8145 }
8ac3646f
TT
8146
8147 if (ui_out_is_mi_like_p (uiout))
8148 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8149}
8150
8151/* Implement the "print_mention" breakpoint_ops method for vfork
8152 catchpoints. */
8153
8154static void
8155print_mention_catch_vfork (struct breakpoint *b)
8156{
8157 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8158}
8159
6149aea9
PA
8160/* Implement the "print_recreate" breakpoint_ops method for vfork
8161 catchpoints. */
8162
8163static void
8164print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8165{
8166 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8167 print_recreate_thread (b, fp);
6149aea9
PA
8168}
8169
ce78b96d
JB
8170/* The breakpoint_ops structure to be used in vfork catchpoints. */
8171
2060206e 8172static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8173
edcc5120
TT
8174/* An instance of this type is used to represent an solib catchpoint.
8175 It includes a "struct breakpoint" as a kind of base class; users
8176 downcast to "struct breakpoint *" when needed. A breakpoint is
8177 really of this type iff its ops pointer points to
8178 CATCH_SOLIB_BREAKPOINT_OPS. */
8179
8180struct solib_catchpoint
8181{
8182 /* The base class. */
8183 struct breakpoint base;
8184
8185 /* True for "catch load", false for "catch unload". */
8186 unsigned char is_load;
8187
8188 /* Regular expression to match, if any. COMPILED is only valid when
8189 REGEX is non-NULL. */
8190 char *regex;
8191 regex_t compiled;
8192};
8193
8194static void
8195dtor_catch_solib (struct breakpoint *b)
8196{
8197 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8198
8199 if (self->regex)
8200 regfree (&self->compiled);
8201 xfree (self->regex);
8202
8203 base_breakpoint_ops.dtor (b);
8204}
8205
8206static int
8207insert_catch_solib (struct bp_location *ignore)
8208{
8209 return 0;
8210}
8211
8212static int
8213remove_catch_solib (struct bp_location *ignore)
8214{
8215 return 0;
8216}
8217
8218static int
8219breakpoint_hit_catch_solib (const struct bp_location *bl,
8220 struct address_space *aspace,
8221 CORE_ADDR bp_addr,
8222 const struct target_waitstatus *ws)
8223{
8224 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8225 struct breakpoint *other;
8226
8227 if (ws->kind == TARGET_WAITKIND_LOADED)
8228 return 1;
8229
8230 ALL_BREAKPOINTS (other)
8231 {
8232 struct bp_location *other_bl;
8233
8234 if (other == bl->owner)
8235 continue;
8236
8237 if (other->type != bp_shlib_event)
8238 continue;
8239
8240 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8241 continue;
8242
8243 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8244 {
8245 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8246 return 1;
8247 }
8248 }
8249
8250 return 0;
8251}
8252
8253static void
8254check_status_catch_solib (struct bpstats *bs)
8255{
8256 struct solib_catchpoint *self
8257 = (struct solib_catchpoint *) bs->breakpoint_at;
8258 int ix;
8259
8260 if (self->is_load)
8261 {
8262 struct so_list *iter;
8263
8264 for (ix = 0;
8265 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8266 ix, iter);
8267 ++ix)
8268 {
8269 if (!self->regex
8270 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8271 return;
8272 }
8273 }
8274 else
8275 {
8276 char *iter;
8277
8278 for (ix = 0;
8279 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8280 ix, iter);
8281 ++ix)
8282 {
8283 if (!self->regex
8284 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8285 return;
8286 }
8287 }
8288
8289 bs->stop = 0;
8290 bs->print_it = print_it_noop;
8291}
8292
8293static enum print_stop_action
8294print_it_catch_solib (bpstat bs)
8295{
8296 struct breakpoint *b = bs->breakpoint_at;
8297 struct ui_out *uiout = current_uiout;
8298
8299 annotate_catchpoint (b->number);
8300 if (b->disposition == disp_del)
8301 ui_out_text (uiout, "\nTemporary catchpoint ");
8302 else
8303 ui_out_text (uiout, "\nCatchpoint ");
8304 ui_out_field_int (uiout, "bkptno", b->number);
8305 ui_out_text (uiout, "\n");
8306 if (ui_out_is_mi_like_p (uiout))
8307 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8308 print_solib_event (1);
8309 return PRINT_SRC_AND_LOC;
8310}
8311
8312static void
8313print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8314{
8315 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8316 struct value_print_options opts;
8317 struct ui_out *uiout = current_uiout;
8318 char *msg;
8319
8320 get_user_print_options (&opts);
8321 /* Field 4, the address, is omitted (which makes the columns not
8322 line up too nicely with the headers, but the effect is relatively
8323 readable). */
8324 if (opts.addressprint)
8325 {
8326 annotate_field (4);
8327 ui_out_field_skip (uiout, "addr");
8328 }
8329
8330 annotate_field (5);
8331 if (self->is_load)
8332 {
8333 if (self->regex)
8334 msg = xstrprintf (_("load of library matching %s"), self->regex);
8335 else
8336 msg = xstrdup (_("load of library"));
8337 }
8338 else
8339 {
8340 if (self->regex)
8341 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8342 else
8343 msg = xstrdup (_("unload of library"));
8344 }
8345 ui_out_field_string (uiout, "what", msg);
8346 xfree (msg);
8ac3646f
TT
8347
8348 if (ui_out_is_mi_like_p (uiout))
8349 ui_out_field_string (uiout, "catch-type",
8350 self->is_load ? "load" : "unload");
edcc5120
TT
8351}
8352
8353static void
8354print_mention_catch_solib (struct breakpoint *b)
8355{
8356 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8357
8358 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8359 self->is_load ? "load" : "unload");
8360}
8361
8362static void
8363print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8364{
8365 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8366
8367 fprintf_unfiltered (fp, "%s %s",
8368 b->disposition == disp_del ? "tcatch" : "catch",
8369 self->is_load ? "load" : "unload");
8370 if (self->regex)
8371 fprintf_unfiltered (fp, " %s", self->regex);
8372 fprintf_unfiltered (fp, "\n");
8373}
8374
8375static struct breakpoint_ops catch_solib_breakpoint_ops;
8376
91985142
MG
8377/* Shared helper function (MI and CLI) for creating and installing
8378 a shared object event catchpoint. If IS_LOAD is non-zero then
8379 the events to be caught are load events, otherwise they are
8380 unload events. If IS_TEMP is non-zero the catchpoint is a
8381 temporary one. If ENABLED is non-zero the catchpoint is
8382 created in an enabled state. */
edcc5120 8383
91985142
MG
8384void
8385add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8386{
8387 struct solib_catchpoint *c;
8388 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8389 struct cleanup *cleanup;
8390
edcc5120
TT
8391 if (!arg)
8392 arg = "";
8393 arg = skip_spaces (arg);
8394
8395 c = XCNEW (struct solib_catchpoint);
8396 cleanup = make_cleanup (xfree, c);
8397
8398 if (*arg != '\0')
8399 {
8400 int errcode;
8401
8402 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8403 if (errcode != 0)
8404 {
8405 char *err = get_regcomp_error (errcode, &c->compiled);
8406
8407 make_cleanup (xfree, err);
8408 error (_("Invalid regexp (%s): %s"), err, arg);
8409 }
8410 c->regex = xstrdup (arg);
8411 }
8412
8413 c->is_load = is_load;
91985142 8414 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8415 &catch_solib_breakpoint_ops);
8416
91985142
MG
8417 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8418
edcc5120
TT
8419 discard_cleanups (cleanup);
8420 install_breakpoint (0, &c->base, 1);
8421}
8422
91985142
MG
8423/* A helper function that does all the work for "catch load" and
8424 "catch unload". */
8425
8426static void
8427catch_load_or_unload (char *arg, int from_tty, int is_load,
8428 struct cmd_list_element *command)
8429{
8430 int tempflag;
8431 const int enabled = 1;
8432
8433 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8434
8435 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8436}
8437
edcc5120
TT
8438static void
8439catch_load_command_1 (char *arg, int from_tty,
8440 struct cmd_list_element *command)
8441{
8442 catch_load_or_unload (arg, from_tty, 1, command);
8443}
8444
8445static void
8446catch_unload_command_1 (char *arg, int from_tty,
8447 struct cmd_list_element *command)
8448{
8449 catch_load_or_unload (arg, from_tty, 0, command);
8450}
8451
346774a9
PA
8452/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8453 is non-zero, then make the breakpoint temporary. If COND_STRING is
8454 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8455 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8456
ab04a2af 8457void
346774a9
PA
8458init_catchpoint (struct breakpoint *b,
8459 struct gdbarch *gdbarch, int tempflag,
8460 char *cond_string,
c0a91b2b 8461 const struct breakpoint_ops *ops)
c906108c 8462{
c5aa993b 8463 struct symtab_and_line sal;
346774a9 8464
fe39c653 8465 init_sal (&sal);
6c95b8df 8466 sal.pspace = current_program_space;
c5aa993b 8467
28010a5d 8468 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8469
1b36a34b 8470 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8471 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8472}
8473
28010a5d 8474void
3ea46bff 8475install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8476{
8477 add_to_breakpoint_chain (b);
3a5c3e22 8478 set_breakpoint_number (internal, b);
558a9d82
YQ
8479 if (is_tracepoint (b))
8480 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8481 if (!internal)
8482 mention (b);
c56053d2 8483 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8484
8485 if (update_gll)
44702360 8486 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8487}
8488
9b70b993 8489static void
a6d9a66e
UW
8490create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8491 int tempflag, char *cond_string,
c0a91b2b 8492 const struct breakpoint_ops *ops)
c906108c 8493{
e29a4733 8494 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8495
e29a4733
PA
8496 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8497
8498 c->forked_inferior_pid = null_ptid;
8499
3ea46bff 8500 install_breakpoint (0, &c->base, 1);
c906108c
SS
8501}
8502
fe798b75
JB
8503/* Exec catchpoints. */
8504
b4d90040
PA
8505/* An instance of this type is used to represent an exec catchpoint.
8506 It includes a "struct breakpoint" as a kind of base class; users
8507 downcast to "struct breakpoint *" when needed. A breakpoint is
8508 really of this type iff its ops pointer points to
8509 CATCH_EXEC_BREAKPOINT_OPS. */
8510
8511struct exec_catchpoint
8512{
8513 /* The base class. */
8514 struct breakpoint base;
8515
8516 /* Filename of a program whose exec triggered this catchpoint.
8517 This field is only valid immediately after this catchpoint has
8518 triggered. */
8519 char *exec_pathname;
8520};
8521
8522/* Implement the "dtor" breakpoint_ops method for exec
8523 catchpoints. */
8524
8525static void
8526dtor_catch_exec (struct breakpoint *b)
8527{
8528 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8529
8530 xfree (c->exec_pathname);
348d480f 8531
2060206e 8532 base_breakpoint_ops.dtor (b);
b4d90040
PA
8533}
8534
77b06cd7
TJB
8535static int
8536insert_catch_exec (struct bp_location *bl)
c906108c 8537{
dfd4cc63 8538 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8539}
c906108c 8540
fe798b75 8541static int
77b06cd7 8542remove_catch_exec (struct bp_location *bl)
fe798b75 8543{
dfd4cc63 8544 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8545}
c906108c 8546
fe798b75 8547static int
f1310107 8548breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8549 struct address_space *aspace, CORE_ADDR bp_addr,
8550 const struct target_waitstatus *ws)
fe798b75 8551{
b4d90040
PA
8552 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8553
f90263c1
TT
8554 if (ws->kind != TARGET_WAITKIND_EXECD)
8555 return 0;
8556
8557 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8558 return 1;
fe798b75 8559}
c906108c 8560
fe798b75 8561static enum print_stop_action
348d480f 8562print_it_catch_exec (bpstat bs)
fe798b75 8563{
36dfb11c 8564 struct ui_out *uiout = current_uiout;
348d480f 8565 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8566 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8567
fe798b75 8568 annotate_catchpoint (b->number);
36dfb11c
TT
8569 if (b->disposition == disp_del)
8570 ui_out_text (uiout, "\nTemporary catchpoint ");
8571 else
8572 ui_out_text (uiout, "\nCatchpoint ");
8573 if (ui_out_is_mi_like_p (uiout))
8574 {
8575 ui_out_field_string (uiout, "reason",
8576 async_reason_lookup (EXEC_ASYNC_EXEC));
8577 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8578 }
8579 ui_out_field_int (uiout, "bkptno", b->number);
8580 ui_out_text (uiout, " (exec'd ");
8581 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8582 ui_out_text (uiout, "), ");
8583
fe798b75 8584 return PRINT_SRC_AND_LOC;
c906108c
SS
8585}
8586
fe798b75 8587static void
a6d9a66e 8588print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8589{
b4d90040 8590 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8591 struct value_print_options opts;
79a45e25 8592 struct ui_out *uiout = current_uiout;
fe798b75
JB
8593
8594 get_user_print_options (&opts);
8595
8596 /* Field 4, the address, is omitted (which makes the columns
8597 not line up too nicely with the headers, but the effect
8598 is relatively readable). */
8599 if (opts.addressprint)
8600 ui_out_field_skip (uiout, "addr");
8601 annotate_field (5);
8602 ui_out_text (uiout, "exec");
b4d90040 8603 if (c->exec_pathname != NULL)
fe798b75
JB
8604 {
8605 ui_out_text (uiout, ", program \"");
b4d90040 8606 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8607 ui_out_text (uiout, "\" ");
8608 }
8ac3646f
TT
8609
8610 if (ui_out_is_mi_like_p (uiout))
8611 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8612}
8613
8614static void
8615print_mention_catch_exec (struct breakpoint *b)
8616{
8617 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8618}
8619
6149aea9
PA
8620/* Implement the "print_recreate" breakpoint_ops method for exec
8621 catchpoints. */
8622
8623static void
8624print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8625{
8626 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8627 print_recreate_thread (b, fp);
6149aea9
PA
8628}
8629
2060206e 8630static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8631
c906108c 8632static int
fba45db2 8633hw_breakpoint_used_count (void)
c906108c 8634{
c906108c 8635 int i = 0;
f1310107
TJB
8636 struct breakpoint *b;
8637 struct bp_location *bl;
c906108c
SS
8638
8639 ALL_BREAKPOINTS (b)
c5aa993b 8640 {
d6b74ac4 8641 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8642 for (bl = b->loc; bl; bl = bl->next)
8643 {
8644 /* Special types of hardware breakpoints may use more than
8645 one register. */
348d480f 8646 i += b->ops->resources_needed (bl);
f1310107 8647 }
c5aa993b 8648 }
c906108c
SS
8649
8650 return i;
8651}
8652
a1398e0c
PA
8653/* Returns the resources B would use if it were a hardware
8654 watchpoint. */
8655
c906108c 8656static int
a1398e0c 8657hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8658{
c906108c 8659 int i = 0;
e09342b5 8660 struct bp_location *bl;
c906108c 8661
a1398e0c
PA
8662 if (!breakpoint_enabled (b))
8663 return 0;
8664
8665 for (bl = b->loc; bl; bl = bl->next)
8666 {
8667 /* Special types of hardware watchpoints may use more than
8668 one register. */
8669 i += b->ops->resources_needed (bl);
8670 }
8671
8672 return i;
8673}
8674
8675/* Returns the sum the used resources of all hardware watchpoints of
8676 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8677 the sum of the used resources of all hardware watchpoints of other
8678 types _not_ TYPE. */
8679
8680static int
8681hw_watchpoint_used_count_others (struct breakpoint *except,
8682 enum bptype type, int *other_type_used)
8683{
8684 int i = 0;
8685 struct breakpoint *b;
8686
c906108c
SS
8687 *other_type_used = 0;
8688 ALL_BREAKPOINTS (b)
e09342b5 8689 {
a1398e0c
PA
8690 if (b == except)
8691 continue;
e09342b5
TJB
8692 if (!breakpoint_enabled (b))
8693 continue;
8694
a1398e0c
PA
8695 if (b->type == type)
8696 i += hw_watchpoint_use_count (b);
8697 else if (is_hardware_watchpoint (b))
8698 *other_type_used = 1;
e09342b5
TJB
8699 }
8700
c906108c
SS
8701 return i;
8702}
8703
c906108c 8704void
fba45db2 8705disable_watchpoints_before_interactive_call_start (void)
c906108c 8706{
c5aa993b 8707 struct breakpoint *b;
c906108c
SS
8708
8709 ALL_BREAKPOINTS (b)
c5aa993b 8710 {
cc60f2e3 8711 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8712 {
b5de0fa7 8713 b->enable_state = bp_call_disabled;
44702360 8714 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8715 }
8716 }
c906108c
SS
8717}
8718
8719void
fba45db2 8720enable_watchpoints_after_interactive_call_stop (void)
c906108c 8721{
c5aa993b 8722 struct breakpoint *b;
c906108c
SS
8723
8724 ALL_BREAKPOINTS (b)
c5aa993b 8725 {
cc60f2e3 8726 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8727 {
b5de0fa7 8728 b->enable_state = bp_enabled;
44702360 8729 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8730 }
8731 }
c906108c
SS
8732}
8733
8bea4e01
UW
8734void
8735disable_breakpoints_before_startup (void)
8736{
6c95b8df 8737 current_program_space->executing_startup = 1;
44702360 8738 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8739}
8740
8741void
8742enable_breakpoints_after_startup (void)
8743{
6c95b8df 8744 current_program_space->executing_startup = 0;
f8eba3c6 8745 breakpoint_re_set ();
8bea4e01
UW
8746}
8747
7c16b83e
PA
8748/* Create a new single-step breakpoint for thread THREAD, with no
8749 locations. */
c906108c 8750
7c16b83e
PA
8751static struct breakpoint *
8752new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8753{
8754 struct breakpoint *b = XNEW (struct breakpoint);
8755
8756 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8757 &momentary_breakpoint_ops);
8758
8759 b->disposition = disp_donttouch;
8760 b->frame_id = null_frame_id;
8761
8762 b->thread = thread;
8763 gdb_assert (b->thread != 0);
8764
8765 add_to_breakpoint_chain (b);
8766
8767 return b;
8768}
8769
8770/* Set a momentary breakpoint of type TYPE at address specified by
8771 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8772 frame. */
c906108c
SS
8773
8774struct breakpoint *
a6d9a66e
UW
8775set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8776 struct frame_id frame_id, enum bptype type)
c906108c 8777{
52f0bd74 8778 struct breakpoint *b;
edb3359d 8779
193facb3
JK
8780 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8781 tail-called one. */
8782 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8783
06edf0c0 8784 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8785 b->enable_state = bp_enabled;
8786 b->disposition = disp_donttouch;
818dd999 8787 b->frame_id = frame_id;
c906108c 8788
4a64f543
MS
8789 /* If we're debugging a multi-threaded program, then we want
8790 momentary breakpoints to be active in only a single thread of
8791 control. */
39f77062
KB
8792 if (in_thread_list (inferior_ptid))
8793 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 8794
44702360 8795 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8796
c906108c
SS
8797 return b;
8798}
611c83ae 8799
06edf0c0 8800/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8801 The new breakpoint will have type TYPE, use OPS as its
8802 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8803
06edf0c0
PA
8804static struct breakpoint *
8805momentary_breakpoint_from_master (struct breakpoint *orig,
8806 enum bptype type,
a1aa2221
LM
8807 const struct breakpoint_ops *ops,
8808 int loc_enabled)
e58b0e63
PA
8809{
8810 struct breakpoint *copy;
8811
06edf0c0 8812 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8813 copy->loc = allocate_bp_location (copy);
0e30163f 8814 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8815
a6d9a66e 8816 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8817 copy->loc->requested_address = orig->loc->requested_address;
8818 copy->loc->address = orig->loc->address;
8819 copy->loc->section = orig->loc->section;
6c95b8df 8820 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8821 copy->loc->probe = orig->loc->probe;
f8eba3c6 8822 copy->loc->line_number = orig->loc->line_number;
2f202fde 8823 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8824 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8825 copy->frame_id = orig->frame_id;
8826 copy->thread = orig->thread;
6c95b8df 8827 copy->pspace = orig->pspace;
e58b0e63
PA
8828
8829 copy->enable_state = bp_enabled;
8830 copy->disposition = disp_donttouch;
8831 copy->number = internal_breakpoint_number--;
8832
44702360 8833 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8834 return copy;
8835}
8836
06edf0c0
PA
8837/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8838 ORIG is NULL. */
8839
8840struct breakpoint *
8841clone_momentary_breakpoint (struct breakpoint *orig)
8842{
8843 /* If there's nothing to clone, then return nothing. */
8844 if (orig == NULL)
8845 return NULL;
8846
a1aa2221 8847 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8848}
8849
611c83ae 8850struct breakpoint *
a6d9a66e
UW
8851set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8852 enum bptype type)
611c83ae
PA
8853{
8854 struct symtab_and_line sal;
8855
8856 sal = find_pc_line (pc, 0);
8857 sal.pc = pc;
8858 sal.section = find_pc_overlay (pc);
8859 sal.explicit_pc = 1;
8860
a6d9a66e 8861 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8862}
c906108c 8863\f
c5aa993b 8864
c906108c
SS
8865/* Tell the user we have just set a breakpoint B. */
8866
8867static void
fba45db2 8868mention (struct breakpoint *b)
c906108c 8869{
348d480f 8870 b->ops->print_mention (b);
79a45e25 8871 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 8872 return;
c906108c
SS
8873 printf_filtered ("\n");
8874}
c906108c 8875\f
c5aa993b 8876
1a853c52
PA
8877static int bp_loc_is_permanent (struct bp_location *loc);
8878
0d381245 8879static struct bp_location *
39d61571 8880add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8881 const struct symtab_and_line *sal)
8882{
8883 struct bp_location *loc, **tmp;
3742cc8b
YQ
8884 CORE_ADDR adjusted_address;
8885 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8886
8887 if (loc_gdbarch == NULL)
8888 loc_gdbarch = b->gdbarch;
8889
8890 /* Adjust the breakpoint's address prior to allocating a location.
8891 Once we call allocate_bp_location(), that mostly uninitialized
8892 location will be placed on the location chain. Adjustment of the
8893 breakpoint may cause target_read_memory() to be called and we do
8894 not want its scan of the location chain to find a breakpoint and
8895 location that's only been partially initialized. */
8896 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8897 sal->pc, b->type);
0d381245 8898
d30113d4 8899 /* Sort the locations by their ADDRESS. */
39d61571 8900 loc = allocate_bp_location (b);
d30113d4
JK
8901 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8902 tmp = &((*tmp)->next))
0d381245 8903 ;
d30113d4 8904 loc->next = *tmp;
0d381245 8905 *tmp = loc;
3742cc8b 8906
0d381245 8907 loc->requested_address = sal->pc;
3742cc8b 8908 loc->address = adjusted_address;
6c95b8df 8909 loc->pspace = sal->pspace;
729662a5
TT
8910 loc->probe.probe = sal->probe;
8911 loc->probe.objfile = sal->objfile;
6c95b8df 8912 gdb_assert (loc->pspace != NULL);
0d381245 8913 loc->section = sal->section;
3742cc8b 8914 loc->gdbarch = loc_gdbarch;
f8eba3c6 8915 loc->line_number = sal->line;
2f202fde 8916 loc->symtab = sal->symtab;
f8eba3c6 8917
0e30163f
JK
8918 set_breakpoint_location_function (loc,
8919 sal->explicit_pc || sal->explicit_line);
1a853c52
PA
8920
8921 if (bp_loc_is_permanent (loc))
8922 {
8923 loc->inserted = 1;
8924 loc->permanent = 1;
8925 }
8926
0d381245
VP
8927 return loc;
8928}
514f746b
AR
8929\f
8930
1cf4d951 8931/* See breakpoint.h. */
514f746b 8932
1cf4d951
PA
8933int
8934program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8935{
8936 int len;
8937 CORE_ADDR addr;
1afeeb75 8938 const gdb_byte *bpoint;
514f746b 8939 gdb_byte *target_mem;
939c61fa
JK
8940 struct cleanup *cleanup;
8941 int retval = 0;
514f746b 8942
1cf4d951
PA
8943 addr = address;
8944 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8945
8946 /* Software breakpoints unsupported? */
8947 if (bpoint == NULL)
8948 return 0;
8949
8950 target_mem = alloca (len);
8951
8952 /* Enable the automatic memory restoration from breakpoints while
8953 we read the memory. Otherwise we could say about our temporary
8954 breakpoints they are permanent. */
8955 cleanup = make_show_memory_breakpoints_cleanup (0);
8956
8957 if (target_read_memory (address, target_mem, len) == 0
8958 && memcmp (target_mem, bpoint, len) == 0)
8959 retval = 1;
8960
8961 do_cleanups (cleanup);
8962
8963 return retval;
8964}
8965
8966/* Return 1 if LOC is pointing to a permanent breakpoint,
8967 return 0 otherwise. */
8968
8969static int
8970bp_loc_is_permanent (struct bp_location *loc)
8971{
8972 struct cleanup *cleanup;
8973 int retval;
8974
514f746b
AR
8975 gdb_assert (loc != NULL);
8976
e8af5d7a
JB
8977 /* bp_call_dummy breakpoint locations are usually memory locations
8978 where GDB just wrote a breakpoint instruction, making it look
8979 as if there is a permanent breakpoint at that location. Considering
8980 it permanent makes GDB rely on that breakpoint instruction to stop
8981 the program, thus removing the need to insert its own breakpoint
8982 there. This is normally expected to work, except that some versions
8983 of QEMU (Eg: QEMU 2.0.0 for SPARC) just report a fatal problem (Trap
8984 0x02 while interrupts disabled, Error state) instead of reporting
8985 a SIGTRAP. QEMU should probably be fixed, but in the interest of
8986 compatibility with versions that behave this way, we always consider
8987 bp_call_dummy breakpoint locations as non-permanent. */
8988 if (loc->owner->type == bp_call_dummy)
8989 return 0;
8990
6c95b8df 8991 cleanup = save_current_space_and_thread ();
6c95b8df 8992 switch_to_program_space_and_thread (loc->pspace);
939c61fa 8993
1cf4d951 8994 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b 8995
939c61fa
JK
8996 do_cleanups (cleanup);
8997
8998 return retval;
514f746b
AR
8999}
9000
e7e0cddf
SS
9001/* Build a command list for the dprintf corresponding to the current
9002 settings of the dprintf style options. */
9003
9004static void
9005update_dprintf_command_list (struct breakpoint *b)
9006{
9007 char *dprintf_args = b->extra_string;
9008 char *printf_line = NULL;
9009
9010 if (!dprintf_args)
9011 return;
9012
9013 dprintf_args = skip_spaces (dprintf_args);
9014
9015 /* Allow a comma, as it may have terminated a location, but don't
9016 insist on it. */
9017 if (*dprintf_args == ',')
9018 ++dprintf_args;
9019 dprintf_args = skip_spaces (dprintf_args);
9020
9021 if (*dprintf_args != '"')
9022 error (_("Bad format string, missing '\"'."));
9023
d3ce09f5 9024 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9025 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9026 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9027 {
9028 if (!dprintf_function)
9029 error (_("No function supplied for dprintf call"));
9030
9031 if (dprintf_channel && strlen (dprintf_channel) > 0)
9032 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9033 dprintf_function,
9034 dprintf_channel,
9035 dprintf_args);
9036 else
9037 printf_line = xstrprintf ("call (void) %s (%s)",
9038 dprintf_function,
9039 dprintf_args);
9040 }
d3ce09f5
SS
9041 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9042 {
9043 if (target_can_run_breakpoint_commands ())
9044 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9045 else
9046 {
9047 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9048 printf_line = xstrprintf ("printf %s", dprintf_args);
9049 }
9050 }
e7e0cddf
SS
9051 else
9052 internal_error (__FILE__, __LINE__,
9053 _("Invalid dprintf style."));
9054
f28045c2 9055 gdb_assert (printf_line != NULL);
9d6e6e84 9056 /* Manufacture a printf sequence. */
f28045c2 9057 {
9d6e6e84
HZ
9058 struct command_line *printf_cmd_line
9059 = xmalloc (sizeof (struct command_line));
e7e0cddf 9060
f28045c2
YQ
9061 printf_cmd_line = xmalloc (sizeof (struct command_line));
9062 printf_cmd_line->control_type = simple_control;
9063 printf_cmd_line->body_count = 0;
9064 printf_cmd_line->body_list = NULL;
9d6e6e84 9065 printf_cmd_line->next = NULL;
f28045c2 9066 printf_cmd_line->line = printf_line;
e7e0cddf 9067
f28045c2
YQ
9068 breakpoint_set_commands (b, printf_cmd_line);
9069 }
e7e0cddf
SS
9070}
9071
9072/* Update all dprintf commands, making their command lists reflect
9073 current style settings. */
9074
9075static void
9076update_dprintf_commands (char *args, int from_tty,
9077 struct cmd_list_element *c)
9078{
9079 struct breakpoint *b;
9080
9081 ALL_BREAKPOINTS (b)
9082 {
9083 if (b->type == bp_dprintf)
9084 update_dprintf_command_list (b);
9085 }
9086}
c3f6f71d 9087
018d34a4
VP
9088/* Create a breakpoint with SAL as location. Use ADDR_STRING
9089 as textual description of the location, and COND_STRING
db107f19 9090 as condition expression. */
018d34a4
VP
9091
9092static void
d9b3f62e
PA
9093init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9094 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9095 char *filter, char *cond_string,
e7e0cddf 9096 char *extra_string,
d9b3f62e
PA
9097 enum bptype type, enum bpdisp disposition,
9098 int thread, int task, int ignore_count,
c0a91b2b 9099 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9100 int enabled, int internal, unsigned flags,
9101 int display_canonical)
018d34a4 9102{
0d381245 9103 int i;
018d34a4
VP
9104
9105 if (type == bp_hardware_breakpoint)
9106 {
fbbd034e
AS
9107 int target_resources_ok;
9108
9109 i = hw_breakpoint_used_count ();
9110 target_resources_ok =
9111 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9112 i + 1, 0);
9113 if (target_resources_ok == 0)
9114 error (_("No hardware breakpoint support in the target."));
9115 else if (target_resources_ok < 0)
9116 error (_("Hardware breakpoints used exceeds limit."));
9117 }
9118
6c95b8df
PA
9119 gdb_assert (sals.nelts > 0);
9120
0d381245
VP
9121 for (i = 0; i < sals.nelts; ++i)
9122 {
9123 struct symtab_and_line sal = sals.sals[i];
9124 struct bp_location *loc;
9125
9126 if (from_tty)
5af949e3
UW
9127 {
9128 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9129 if (!loc_gdbarch)
9130 loc_gdbarch = gdbarch;
9131
9132 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9133 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9134 }
0d381245
VP
9135
9136 if (i == 0)
9137 {
d9b3f62e 9138 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9139 b->thread = thread;
4a306c9a 9140 b->task = task;
855a6e68 9141
0d381245 9142 b->cond_string = cond_string;
e7e0cddf 9143 b->extra_string = extra_string;
0d381245 9144 b->ignore_count = ignore_count;
41447f92 9145 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9146 b->disposition = disposition;
6c95b8df 9147
44f238bb
PA
9148 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9149 b->loc->inserted = 1;
9150
0fb4aa4b
PA
9151 if (type == bp_static_tracepoint)
9152 {
d9b3f62e 9153 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9154 struct static_tracepoint_marker marker;
9155
983af33b 9156 if (strace_marker_p (b))
0fb4aa4b
PA
9157 {
9158 /* We already know the marker exists, otherwise, we
9159 wouldn't see a sal for it. */
9160 char *p = &addr_string[3];
9161 char *endp;
9162 char *marker_str;
0fb4aa4b 9163
e9cafbcc 9164 p = skip_spaces (p);
0fb4aa4b 9165
e9cafbcc 9166 endp = skip_to_space (p);
0fb4aa4b
PA
9167
9168 marker_str = savestring (p, endp - p);
d9b3f62e 9169 t->static_trace_marker_id = marker_str;
0fb4aa4b 9170
3e43a32a
MS
9171 printf_filtered (_("Probed static tracepoint "
9172 "marker \"%s\"\n"),
d9b3f62e 9173 t->static_trace_marker_id);
0fb4aa4b
PA
9174 }
9175 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9176 {
d9b3f62e 9177 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9178 release_static_tracepoint_marker (&marker);
9179
3e43a32a
MS
9180 printf_filtered (_("Probed static tracepoint "
9181 "marker \"%s\"\n"),
d9b3f62e 9182 t->static_trace_marker_id);
0fb4aa4b
PA
9183 }
9184 else
3e43a32a
MS
9185 warning (_("Couldn't determine the static "
9186 "tracepoint marker to probe"));
0fb4aa4b
PA
9187 }
9188
0d381245
VP
9189 loc = b->loc;
9190 }
9191 else
018d34a4 9192 {
39d61571 9193 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9194 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9195 loc->inserted = 1;
0d381245
VP
9196 }
9197
9198 if (b->cond_string)
9199 {
bbc13ae3
KS
9200 const char *arg = b->cond_string;
9201
1bb9788d
TT
9202 loc->cond = parse_exp_1 (&arg, loc->address,
9203 block_for_pc (loc->address), 0);
0d381245 9204 if (*arg)
588ae58c 9205 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9206 }
e7e0cddf
SS
9207
9208 /* Dynamic printf requires and uses additional arguments on the
9209 command line, otherwise it's an error. */
9210 if (type == bp_dprintf)
9211 {
9212 if (b->extra_string)
9213 update_dprintf_command_list (b);
9214 else
9215 error (_("Format string required"));
9216 }
9217 else if (b->extra_string)
588ae58c 9218 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9219 }
018d34a4 9220
56435ebe 9221 b->display_canonical = display_canonical;
018d34a4
VP
9222 if (addr_string)
9223 b->addr_string = addr_string;
9224 else
9225 /* addr_string has to be used or breakpoint_re_set will delete
9226 me. */
5af949e3
UW
9227 b->addr_string
9228 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9229 b->filter = filter;
d9b3f62e 9230}
018d34a4 9231
d9b3f62e
PA
9232static void
9233create_breakpoint_sal (struct gdbarch *gdbarch,
9234 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9235 char *filter, char *cond_string,
e7e0cddf 9236 char *extra_string,
d9b3f62e
PA
9237 enum bptype type, enum bpdisp disposition,
9238 int thread, int task, int ignore_count,
c0a91b2b 9239 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9240 int enabled, int internal, unsigned flags,
9241 int display_canonical)
d9b3f62e
PA
9242{
9243 struct breakpoint *b;
9244 struct cleanup *old_chain;
9245
9246 if (is_tracepoint_type (type))
9247 {
9248 struct tracepoint *t;
9249
9250 t = XCNEW (struct tracepoint);
9251 b = &t->base;
9252 }
9253 else
9254 b = XNEW (struct breakpoint);
9255
9256 old_chain = make_cleanup (xfree, b);
9257
9258 init_breakpoint_sal (b, gdbarch,
9259 sals, addr_string,
e7e0cddf 9260 filter, cond_string, extra_string,
d9b3f62e
PA
9261 type, disposition,
9262 thread, task, ignore_count,
9263 ops, from_tty,
44f238bb
PA
9264 enabled, internal, flags,
9265 display_canonical);
d9b3f62e
PA
9266 discard_cleanups (old_chain);
9267
3ea46bff 9268 install_breakpoint (internal, b, 0);
018d34a4
VP
9269}
9270
9271/* Add SALS.nelts breakpoints to the breakpoint table. For each
9272 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9273 value. COND_STRING, if not NULL, specified the condition to be
9274 used for all breakpoints. Essentially the only case where
9275 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9276 function. In that case, it's still not possible to specify
9277 separate conditions for different overloaded functions, so
9278 we take just a single condition string.
9279
c3f6f71d 9280 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9281 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9282 array contents). If the function fails (error() is called), the
9283 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9284 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9285
9286static void
8cdf0e15 9287create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9288 struct linespec_result *canonical,
e7e0cddf 9289 char *cond_string, char *extra_string,
8cdf0e15
VP
9290 enum bptype type, enum bpdisp disposition,
9291 int thread, int task, int ignore_count,
c0a91b2b 9292 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9293 int enabled, int internal, unsigned flags)
c906108c 9294{
018d34a4 9295 int i;
f8eba3c6 9296 struct linespec_sals *lsal;
cc59ec59 9297
f8eba3c6
TT
9298 if (canonical->pre_expanded)
9299 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9300
9301 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9302 {
f8eba3c6
TT
9303 /* Note that 'addr_string' can be NULL in the case of a plain
9304 'break', without arguments. */
9305 char *addr_string = (canonical->addr_string
9306 ? xstrdup (canonical->addr_string)
9307 : NULL);
9308 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9309 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9310
f8eba3c6
TT
9311 make_cleanup (xfree, filter_string);
9312 create_breakpoint_sal (gdbarch, lsal->sals,
9313 addr_string,
9314 filter_string,
e7e0cddf
SS
9315 cond_string, extra_string,
9316 type, disposition,
84f4c1fe 9317 thread, task, ignore_count, ops,
44f238bb 9318 from_tty, enabled, internal, flags,
56435ebe 9319 canonical->special_display);
f8eba3c6 9320 discard_cleanups (inner);
c3f6f71d 9321 }
c3f6f71d 9322}
c906108c 9323
9998af43 9324/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9325 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9326 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9327 address strings. ADDRESS points to the end of the SAL.
9328
9329 The array and the line spec strings are allocated on the heap, it is
9330 the caller's responsibility to free them. */
c906108c 9331
b9362cc7 9332static void
c3f6f71d 9333parse_breakpoint_sals (char **address,
58438ac1 9334 struct linespec_result *canonical)
c3f6f71d 9335{
c3f6f71d 9336 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9337 breakpoint. */
0578b14e 9338 if ((*address) == NULL || linespec_lexer_lex_keyword (*address))
c906108c 9339 {
1bfeeb0f
JL
9340 /* The last displayed codepoint, if it's valid, is our default breakpoint
9341 address. */
9342 if (last_displayed_sal_is_valid ())
c906108c 9343 {
f8eba3c6 9344 struct linespec_sals lsal;
c3f6f71d 9345 struct symtab_and_line sal;
1c8cdcb1 9346 CORE_ADDR pc;
cc59ec59 9347
4a64f543 9348 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9349 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9350 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9351
9352 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9353 corresponding to the last call to print_frame_info.
9354 Be sure to reinitialize LINE with NOTCURRENT == 0
9355 as the breakpoint line number is inappropriate otherwise.
9356 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9357 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9358 pc = sal.pc;
9359 sal = find_pc_line (pc, 0);
00903456 9360
4a64f543 9361 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9362 where PC is the last displayed codepoint's address. So
9363 make sure to set sal.explicit_pc to prevent GDB from
9364 trying to expand the list of sals to include all other
9365 instances with the same symtab and line. */
1c8cdcb1 9366 sal.pc = pc;
00903456
JK
9367 sal.explicit_pc = 1;
9368
f8eba3c6
TT
9369 lsal.sals.sals[0] = sal;
9370 lsal.sals.nelts = 1;
9371 lsal.canonical = NULL;
9372
9373 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9374 }
9375 else
8a3fe4f8 9376 error (_("No default breakpoint address now."));
c906108c
SS
9377 }
9378 else
9379 {
cc80f267
JK
9380 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9381
c906108c 9382 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9383 current_source_symtab (which is decode_line_1's default).
9384 This should produce the results we want almost all of the
cc80f267
JK
9385 time while leaving default_breakpoint_* alone.
9386
9387 ObjC: However, don't match an Objective-C method name which
9388 may have a '+' or '-' succeeded by a '['. */
9389 if (last_displayed_sal_is_valid ()
9390 && (!cursal.symtab
9391 || ((strchr ("+-", (*address)[0]) != NULL)
9392 && ((*address)[1] != '['))))
f8eba3c6
TT
9393 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9394 get_last_displayed_symtab (),
9395 get_last_displayed_line (),
9396 canonical, NULL, NULL);
c906108c 9397 else
f8eba3c6 9398 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9399 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9400 }
c3f6f71d 9401}
c906108c 9402
c906108c 9403
c3f6f71d 9404/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9405 inserted as a breakpoint. If it can't throw an error. */
c906108c 9406
b9362cc7 9407static void
23e7acfb 9408breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9409{
9410 int i;
cc59ec59 9411
c3f6f71d 9412 for (i = 0; i < sals->nelts; i++)
ee53e872 9413 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9414}
9415
7a697b8d
SS
9416/* Fast tracepoints may have restrictions on valid locations. For
9417 instance, a fast tracepoint using a jump instead of a trap will
9418 likely have to overwrite more bytes than a trap would, and so can
9419 only be placed where the instruction is longer than the jump, or a
9420 multi-instruction sequence does not have a jump into the middle of
9421 it, etc. */
9422
9423static void
9424check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9425 struct symtabs_and_lines *sals)
9426{
9427 int i, rslt;
9428 struct symtab_and_line *sal;
9429 char *msg;
9430 struct cleanup *old_chain;
9431
9432 for (i = 0; i < sals->nelts; i++)
9433 {
f8eba3c6
TT
9434 struct gdbarch *sarch;
9435
7a697b8d
SS
9436 sal = &sals->sals[i];
9437
f8eba3c6
TT
9438 sarch = get_sal_arch (*sal);
9439 /* We fall back to GDBARCH if there is no architecture
9440 associated with SAL. */
9441 if (sarch == NULL)
9442 sarch = gdbarch;
9443 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9444 NULL, &msg);
9445 old_chain = make_cleanup (xfree, msg);
9446
9447 if (!rslt)
9448 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9449 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9450
9451 do_cleanups (old_chain);
9452 }
9453}
9454
af4908ba
KS
9455/* Issue an invalid thread ID error. */
9456
9457static void ATTRIBUTE_NORETURN
9458invalid_thread_id_error (int id)
9459{
9460 error (_("Unknown thread %d."), id);
9461}
9462
018d34a4
VP
9463/* Given TOK, a string specification of condition and thread, as
9464 accepted by the 'break' command, extract the condition
9465 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9466 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9467 If no condition is found, *COND_STRING is set to NULL.
9468 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9469
9470static void
bbc13ae3 9471find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9472 char **cond_string, int *thread, int *task,
9473 char **rest)
018d34a4
VP
9474{
9475 *cond_string = NULL;
9476 *thread = -1;
ed1d1739
KS
9477 *task = 0;
9478 *rest = NULL;
9479
018d34a4
VP
9480 while (tok && *tok)
9481 {
bbc13ae3 9482 const char *end_tok;
018d34a4 9483 int toklen;
bbc13ae3
KS
9484 const char *cond_start = NULL;
9485 const char *cond_end = NULL;
cc59ec59 9486
bbc13ae3 9487 tok = skip_spaces_const (tok);
e7e0cddf
SS
9488
9489 if ((*tok == '"' || *tok == ',') && rest)
9490 {
9491 *rest = savestring (tok, strlen (tok));
9492 return;
9493 }
9494
bbc13ae3 9495 end_tok = skip_to_space_const (tok);
d634f2de 9496
018d34a4 9497 toklen = end_tok - tok;
d634f2de 9498
018d34a4
VP
9499 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9500 {
f7545552
TT
9501 struct expression *expr;
9502
018d34a4 9503 tok = cond_start = end_tok + 1;
1bb9788d 9504 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9505 xfree (expr);
018d34a4 9506 cond_end = tok;
d634f2de 9507 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9508 }
9509 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9510 {
9511 char *tmptok;
d634f2de 9512
018d34a4 9513 tok = end_tok + 1;
bbc13ae3 9514 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9515 if (tok == tmptok)
9516 error (_("Junk after thread keyword."));
9517 if (!valid_thread_id (*thread))
af4908ba 9518 invalid_thread_id_error (*thread);
bbc13ae3 9519 tok = tmptok;
018d34a4 9520 }
4a306c9a
JB
9521 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9522 {
9523 char *tmptok;
9524
9525 tok = end_tok + 1;
bbc13ae3 9526 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9527 if (tok == tmptok)
9528 error (_("Junk after task keyword."));
9529 if (!valid_task_id (*task))
b6199126 9530 error (_("Unknown task %d."), *task);
bbc13ae3 9531 tok = tmptok;
4a306c9a 9532 }
e7e0cddf
SS
9533 else if (rest)
9534 {
9535 *rest = savestring (tok, strlen (tok));
ccab2054 9536 return;
e7e0cddf 9537 }
018d34a4
VP
9538 else
9539 error (_("Junk at end of arguments."));
9540 }
9541}
9542
0fb4aa4b
PA
9543/* Decode a static tracepoint marker spec. */
9544
9545static struct symtabs_and_lines
9546decode_static_tracepoint_spec (char **arg_p)
9547{
9548 VEC(static_tracepoint_marker_p) *markers = NULL;
9549 struct symtabs_and_lines sals;
0fb4aa4b
PA
9550 struct cleanup *old_chain;
9551 char *p = &(*arg_p)[3];
9552 char *endp;
9553 char *marker_str;
9554 int i;
9555
e9cafbcc 9556 p = skip_spaces (p);
0fb4aa4b 9557
e9cafbcc 9558 endp = skip_to_space (p);
0fb4aa4b
PA
9559
9560 marker_str = savestring (p, endp - p);
9561 old_chain = make_cleanup (xfree, marker_str);
9562
9563 markers = target_static_tracepoint_markers_by_strid (marker_str);
9564 if (VEC_empty(static_tracepoint_marker_p, markers))
9565 error (_("No known static tracepoint marker named %s"), marker_str);
9566
9567 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9568 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9569
9570 for (i = 0; i < sals.nelts; i++)
9571 {
9572 struct static_tracepoint_marker *marker;
9573
9574 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9575
9576 init_sal (&sals.sals[i]);
9577
9578 sals.sals[i] = find_pc_line (marker->address, 0);
9579 sals.sals[i].pc = marker->address;
9580
9581 release_static_tracepoint_marker (marker);
9582 }
9583
9584 do_cleanups (old_chain);
9585
9586 *arg_p = endp;
9587 return sals;
9588}
9589
fd9b8c24
PA
9590/* Set a breakpoint. This function is shared between CLI and MI
9591 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9592 modes of operations, selected by the PARSE_ARG parameter. If
9593 non-zero, the function will parse ARG, extracting location,
9594 condition, thread and extra string. Otherwise, ARG is just the
9595 breakpoint's location, with condition, thread, and extra string
9596 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9597 If INTERNAL is non-zero, the breakpoint number will be allocated
9598 from the internal breakpoint count. Returns true if any breakpoint
9599 was created; false otherwise. */
0101ce28 9600
8cdf0e15
VP
9601int
9602create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9603 char *arg, char *cond_string,
9604 int thread, char *extra_string,
f6de8ec2 9605 int parse_arg,
0fb4aa4b 9606 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9607 int ignore_count,
9608 enum auto_boolean pending_break_support,
c0a91b2b 9609 const struct breakpoint_ops *ops,
44f238bb
PA
9610 int from_tty, int enabled, int internal,
9611 unsigned flags)
c3f6f71d 9612{
f8eba3c6 9613 char *copy_arg = NULL;
c3f6f71d 9614 char *addr_start = arg;
7efd8fc2 9615 struct linespec_result canonical;
c3f6f71d 9616 struct cleanup *old_chain;
80c99de1 9617 struct cleanup *bkpt_chain = NULL;
0101ce28 9618 int pending = 0;
4a306c9a 9619 int task = 0;
86b17b60 9620 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9621
348d480f
PA
9622 gdb_assert (ops != NULL);
9623
7efd8fc2 9624 init_linespec_result (&canonical);
c3f6f71d 9625
492d29ea 9626 TRY
b78a6381 9627 {
983af33b
SDJ
9628 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9629 addr_start, &copy_arg);
b78a6381 9630 }
492d29ea 9631 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9632 {
492d29ea
PA
9633 /* If caller is interested in rc value from parse, set
9634 value. */
9635 if (e.error == NOT_FOUND_ERROR)
0101ce28 9636 {
05ff989b
AC
9637 /* If pending breakpoint support is turned off, throw
9638 error. */
fa8d40ab
JJ
9639
9640 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9641 throw_exception (e);
9642
9643 exception_print (gdb_stderr, e);
fa8d40ab 9644
05ff989b
AC
9645 /* If pending breakpoint support is auto query and the user
9646 selects no, then simply return the error code. */
059fb39f 9647 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9648 && !nquery (_("Make %s pending on future shared library load? "),
9649 bptype_string (type_wanted)))
fd9b8c24 9650 return 0;
fa8d40ab 9651
05ff989b
AC
9652 /* At this point, either the user was queried about setting
9653 a pending breakpoint and selected yes, or pending
9654 breakpoint behavior is on and thus a pending breakpoint
9655 is defaulted on behalf of the user. */
f8eba3c6
TT
9656 {
9657 struct linespec_sals lsal;
9658
9659 copy_arg = xstrdup (addr_start);
9660 lsal.canonical = xstrdup (copy_arg);
9661 lsal.sals.nelts = 1;
9662 lsal.sals.sals = XNEW (struct symtab_and_line);
9663 init_sal (&lsal.sals.sals[0]);
9664 pending = 1;
9665 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9666 }
0101ce28 9667 }
492d29ea
PA
9668 else
9669 throw_exception (e);
0101ce28 9670 }
492d29ea
PA
9671 END_CATCH
9672
9673 if (VEC_empty (linespec_sals, canonical.sals))
9674 return 0;
c3f6f71d 9675
4a64f543 9676 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9677 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9678
c3f6f71d
JM
9679 /* ----------------------------- SNIP -----------------------------
9680 Anything added to the cleanup chain beyond this point is assumed
9681 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9682 then the memory is not reclaimed. */
9683 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9684
c3f6f71d
JM
9685 /* Resolve all line numbers to PC's and verify that the addresses
9686 are ok for the target. */
0101ce28 9687 if (!pending)
f8eba3c6
TT
9688 {
9689 int ix;
9690 struct linespec_sals *iter;
9691
9692 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9693 breakpoint_sals_to_pc (&iter->sals);
9694 }
c3f6f71d 9695
7a697b8d 9696 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9697 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9698 {
9699 int ix;
9700 struct linespec_sals *iter;
9701
9702 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9703 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9704 }
7a697b8d 9705
c3f6f71d
JM
9706 /* Verify that condition can be parsed, before setting any
9707 breakpoints. Allocate a separate condition expression for each
4a64f543 9708 breakpoint. */
0101ce28 9709 if (!pending)
c3f6f71d 9710 {
f6de8ec2 9711 if (parse_arg)
72b2ff0e 9712 {
0878d0fa 9713 char *rest;
52d361e1
YQ
9714 struct linespec_sals *lsal;
9715
9716 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9717
0878d0fa
YQ
9718 /* Here we only parse 'arg' to separate condition
9719 from thread number, so parsing in context of first
9720 sal is OK. When setting the breakpoint we'll
9721 re-parse it in context of each sal. */
9722
9723 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9724 &thread, &task, &rest);
9725 if (cond_string)
9726 make_cleanup (xfree, cond_string);
9727 if (rest)
9728 make_cleanup (xfree, rest);
9729 if (rest)
9730 extra_string = rest;
72b2ff0e 9731 }
2f069f6f 9732 else
72b2ff0e 9733 {
0878d0fa
YQ
9734 if (*arg != '\0')
9735 error (_("Garbage '%s' at end of location"), arg);
9736
9737 /* Create a private copy of condition string. */
9738 if (cond_string)
9739 {
9740 cond_string = xstrdup (cond_string);
9741 make_cleanup (xfree, cond_string);
9742 }
9743 /* Create a private copy of any extra string. */
9744 if (extra_string)
9745 {
9746 extra_string = xstrdup (extra_string);
9747 make_cleanup (xfree, extra_string);
9748 }
72b2ff0e 9749 }
0fb4aa4b 9750
52d361e1 9751 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9752 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9753 tempflag ? disp_del : disp_donttouch,
9754 thread, task, ignore_count, ops,
44f238bb 9755 from_tty, enabled, internal, flags);
c906108c 9756 }
0101ce28
JJ
9757 else
9758 {
0101ce28
JJ
9759 struct breakpoint *b;
9760
0101ce28
JJ
9761 make_cleanup (xfree, copy_arg);
9762
bfccc43c
YQ
9763 if (is_tracepoint_type (type_wanted))
9764 {
9765 struct tracepoint *t;
9766
9767 t = XCNEW (struct tracepoint);
9768 b = &t->base;
9769 }
9770 else
9771 b = XNEW (struct breakpoint);
9772
9773 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9774
f8eba3c6 9775 b->addr_string = copy_arg;
f6de8ec2 9776 if (parse_arg)
e12c7713
MK
9777 b->cond_string = NULL;
9778 else
9779 {
9780 /* Create a private copy of condition string. */
9781 if (cond_string)
9782 {
9783 cond_string = xstrdup (cond_string);
9784 make_cleanup (xfree, cond_string);
9785 }
9786 b->cond_string = cond_string;
15630549 9787 b->thread = thread;
e12c7713 9788 }
e7e0cddf 9789 b->extra_string = NULL;
0101ce28 9790 b->ignore_count = ignore_count;
0101ce28 9791 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9792 b->condition_not_parsed = 1;
41447f92 9793 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9794 if ((type_wanted != bp_breakpoint
9795 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9796 b->pspace = current_program_space;
8bea4e01 9797
bfccc43c 9798 install_breakpoint (internal, b, 0);
0101ce28
JJ
9799 }
9800
f8eba3c6 9801 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9802 {
3e43a32a
MS
9803 warning (_("Multiple breakpoints were set.\nUse the "
9804 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9805 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9806 }
9807
80c99de1
PA
9808 /* That's it. Discard the cleanups for data inserted into the
9809 breakpoint. */
9810 discard_cleanups (bkpt_chain);
9811 /* But cleanup everything else. */
c3f6f71d 9812 do_cleanups (old_chain);
217dc9e2 9813
80c99de1 9814 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9815 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9816
9817 return 1;
c3f6f71d 9818}
c906108c 9819
348d480f 9820/* Set a breakpoint.
72b2ff0e
VP
9821 ARG is a string describing breakpoint address,
9822 condition, and thread.
9823 FLAG specifies if a breakpoint is hardware on,
9824 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9825 and BP_TEMPFLAG. */
348d480f 9826
98deb0da 9827static void
72b2ff0e 9828break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9829{
72b2ff0e 9830 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9831 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9832 ? bp_hardware_breakpoint
9833 : bp_breakpoint);
55aa24fb
SDJ
9834 struct breakpoint_ops *ops;
9835 const char *arg_cp = arg;
9836
9837 /* Matching breakpoints on probes. */
9838 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9839 ops = &bkpt_probe_breakpoint_ops;
9840 else
9841 ops = &bkpt_breakpoint_ops;
c3f6f71d 9842
8cdf0e15
VP
9843 create_breakpoint (get_current_arch (),
9844 arg,
e7e0cddf 9845 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 9846 tempflag, type_wanted,
8cdf0e15
VP
9847 0 /* Ignore count */,
9848 pending_break_support,
55aa24fb 9849 ops,
8cdf0e15 9850 from_tty,
84f4c1fe 9851 1 /* enabled */,
44f238bb
PA
9852 0 /* internal */,
9853 0);
c906108c
SS
9854}
9855
c906108c
SS
9856/* Helper function for break_command_1 and disassemble_command. */
9857
9858void
fba45db2 9859resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9860{
9861 CORE_ADDR pc;
9862
9863 if (sal->pc == 0 && sal->symtab != NULL)
9864 {
9865 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9866 error (_("No line %d in file \"%s\"."),
05cba821 9867 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9868 sal->pc = pc;
6a048695 9869
4a64f543
MS
9870 /* If this SAL corresponds to a breakpoint inserted using a line
9871 number, then skip the function prologue if necessary. */
6a048695 9872 if (sal->explicit_line)
059acae7 9873 skip_prologue_sal (sal);
c906108c
SS
9874 }
9875
9876 if (sal->section == 0 && sal->symtab != NULL)
9877 {
346d1dfe 9878 const struct blockvector *bv;
3977b71f 9879 const struct block *b;
c5aa993b 9880 struct symbol *sym;
c906108c 9881
43f3e411
DE
9882 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9883 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9884 if (bv != NULL)
9885 {
7f0df278 9886 sym = block_linkage_function (b);
c906108c
SS
9887 if (sym != NULL)
9888 {
eb822aa6
DE
9889 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9890 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9891 sym);
c906108c
SS
9892 }
9893 else
9894 {
4a64f543
MS
9895 /* It really is worthwhile to have the section, so we'll
9896 just have to look harder. This case can be executed
9897 if we have line numbers but no functions (as can
9898 happen in assembly source). */
c906108c 9899
7cbd4a93 9900 struct bound_minimal_symbol msym;
6c95b8df
PA
9901 struct cleanup *old_chain = save_current_space_and_thread ();
9902
9903 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
9904
9905 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9906 if (msym.minsym)
efd66ac6 9907 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
9908
9909 do_cleanups (old_chain);
c906108c
SS
9910 }
9911 }
9912 }
9913}
9914
9915void
fba45db2 9916break_command (char *arg, int from_tty)
c906108c 9917{
db107f19 9918 break_command_1 (arg, 0, from_tty);
c906108c
SS
9919}
9920
c906108c 9921void
fba45db2 9922tbreak_command (char *arg, int from_tty)
c906108c 9923{
db107f19 9924 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9925}
9926
c906108c 9927static void
fba45db2 9928hbreak_command (char *arg, int from_tty)
c906108c 9929{
db107f19 9930 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9931}
9932
9933static void
fba45db2 9934thbreak_command (char *arg, int from_tty)
c906108c 9935{
db107f19 9936 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9937}
9938
9939static void
fba45db2 9940stop_command (char *arg, int from_tty)
c906108c 9941{
a3f17187 9942 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9943Usage: stop in <function | address>\n\
a3f17187 9944 stop at <line>\n"));
c906108c
SS
9945}
9946
9947static void
fba45db2 9948stopin_command (char *arg, int from_tty)
c906108c
SS
9949{
9950 int badInput = 0;
9951
c5aa993b 9952 if (arg == (char *) NULL)
c906108c
SS
9953 badInput = 1;
9954 else if (*arg != '*')
9955 {
9956 char *argptr = arg;
9957 int hasColon = 0;
9958
4a64f543 9959 /* Look for a ':'. If this is a line number specification, then
53a5351d 9960 say it is bad, otherwise, it should be an address or
4a64f543 9961 function/method name. */
c906108c 9962 while (*argptr && !hasColon)
c5aa993b
JM
9963 {
9964 hasColon = (*argptr == ':');
9965 argptr++;
9966 }
c906108c
SS
9967
9968 if (hasColon)
c5aa993b 9969 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9970 else
c5aa993b 9971 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9972 }
9973
9974 if (badInput)
a3f17187 9975 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9976 else
db107f19 9977 break_command_1 (arg, 0, from_tty);
c906108c
SS
9978}
9979
9980static void
fba45db2 9981stopat_command (char *arg, int from_tty)
c906108c
SS
9982{
9983 int badInput = 0;
9984
c5aa993b 9985 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9986 badInput = 1;
9987 else
9988 {
9989 char *argptr = arg;
9990 int hasColon = 0;
9991
4a64f543
MS
9992 /* Look for a ':'. If there is a '::' then get out, otherwise
9993 it is probably a line number. */
c906108c 9994 while (*argptr && !hasColon)
c5aa993b
JM
9995 {
9996 hasColon = (*argptr == ':');
9997 argptr++;
9998 }
c906108c
SS
9999
10000 if (hasColon)
c5aa993b 10001 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10002 else
c5aa993b 10003 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10004 }
10005
10006 if (badInput)
a3f17187 10007 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10008 else
db107f19 10009 break_command_1 (arg, 0, from_tty);
c906108c
SS
10010}
10011
e7e0cddf
SS
10012/* The dynamic printf command is mostly like a regular breakpoint, but
10013 with a prewired command list consisting of a single output command,
10014 built from extra arguments supplied on the dprintf command
10015 line. */
10016
da821c7b 10017static void
e7e0cddf
SS
10018dprintf_command (char *arg, int from_tty)
10019{
10020 create_breakpoint (get_current_arch (),
10021 arg,
10022 NULL, 0, NULL, 1 /* parse arg */,
10023 0, bp_dprintf,
10024 0 /* Ignore count */,
10025 pending_break_support,
10026 &dprintf_breakpoint_ops,
10027 from_tty,
10028 1 /* enabled */,
10029 0 /* internal */,
10030 0);
10031}
10032
d3ce09f5
SS
10033static void
10034agent_printf_command (char *arg, int from_tty)
10035{
10036 error (_("May only run agent-printf on the target"));
10037}
10038
f1310107
TJB
10039/* Implement the "breakpoint_hit" breakpoint_ops method for
10040 ranged breakpoints. */
10041
10042static int
10043breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10044 struct address_space *aspace,
09ac7c10
TT
10045 CORE_ADDR bp_addr,
10046 const struct target_waitstatus *ws)
f1310107 10047{
09ac7c10 10048 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10049 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10050 return 0;
10051
f1310107
TJB
10052 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10053 bl->length, aspace, bp_addr);
10054}
10055
10056/* Implement the "resources_needed" breakpoint_ops method for
10057 ranged breakpoints. */
10058
10059static int
10060resources_needed_ranged_breakpoint (const struct bp_location *bl)
10061{
10062 return target_ranged_break_num_registers ();
10063}
10064
10065/* Implement the "print_it" breakpoint_ops method for
10066 ranged breakpoints. */
10067
10068static enum print_stop_action
348d480f 10069print_it_ranged_breakpoint (bpstat bs)
f1310107 10070{
348d480f 10071 struct breakpoint *b = bs->breakpoint_at;
f1310107 10072 struct bp_location *bl = b->loc;
79a45e25 10073 struct ui_out *uiout = current_uiout;
f1310107
TJB
10074
10075 gdb_assert (b->type == bp_hardware_breakpoint);
10076
10077 /* Ranged breakpoints have only one location. */
10078 gdb_assert (bl && bl->next == NULL);
10079
10080 annotate_breakpoint (b->number);
10081 if (b->disposition == disp_del)
10082 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10083 else
10084 ui_out_text (uiout, "\nRanged breakpoint ");
10085 if (ui_out_is_mi_like_p (uiout))
10086 {
10087 ui_out_field_string (uiout, "reason",
10088 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10089 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10090 }
10091 ui_out_field_int (uiout, "bkptno", b->number);
10092 ui_out_text (uiout, ", ");
10093
10094 return PRINT_SRC_AND_LOC;
10095}
10096
10097/* Implement the "print_one" breakpoint_ops method for
10098 ranged breakpoints. */
10099
10100static void
10101print_one_ranged_breakpoint (struct breakpoint *b,
10102 struct bp_location **last_loc)
10103{
10104 struct bp_location *bl = b->loc;
10105 struct value_print_options opts;
79a45e25 10106 struct ui_out *uiout = current_uiout;
f1310107
TJB
10107
10108 /* Ranged breakpoints have only one location. */
10109 gdb_assert (bl && bl->next == NULL);
10110
10111 get_user_print_options (&opts);
10112
10113 if (opts.addressprint)
10114 /* We don't print the address range here, it will be printed later
10115 by print_one_detail_ranged_breakpoint. */
10116 ui_out_field_skip (uiout, "addr");
10117 annotate_field (5);
10118 print_breakpoint_location (b, bl);
10119 *last_loc = bl;
10120}
10121
10122/* Implement the "print_one_detail" breakpoint_ops method for
10123 ranged breakpoints. */
10124
10125static void
10126print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10127 struct ui_out *uiout)
10128{
10129 CORE_ADDR address_start, address_end;
10130 struct bp_location *bl = b->loc;
f99d8bf4
PA
10131 struct ui_file *stb = mem_fileopen ();
10132 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10133
10134 gdb_assert (bl);
10135
10136 address_start = bl->address;
10137 address_end = address_start + bl->length - 1;
10138
10139 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10140 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10141 print_core_address (bl->gdbarch, address_start),
10142 print_core_address (bl->gdbarch, address_end));
10143 ui_out_field_stream (uiout, "addr", stb);
10144 ui_out_text (uiout, "\n");
10145
10146 do_cleanups (cleanup);
10147}
10148
10149/* Implement the "print_mention" breakpoint_ops method for
10150 ranged breakpoints. */
10151
10152static void
10153print_mention_ranged_breakpoint (struct breakpoint *b)
10154{
10155 struct bp_location *bl = b->loc;
79a45e25 10156 struct ui_out *uiout = current_uiout;
f1310107
TJB
10157
10158 gdb_assert (bl);
10159 gdb_assert (b->type == bp_hardware_breakpoint);
10160
10161 if (ui_out_is_mi_like_p (uiout))
10162 return;
10163
10164 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10165 b->number, paddress (bl->gdbarch, bl->address),
10166 paddress (bl->gdbarch, bl->address + bl->length - 1));
10167}
10168
10169/* Implement the "print_recreate" breakpoint_ops method for
10170 ranged breakpoints. */
10171
10172static void
10173print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10174{
10175 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10176 b->addr_string_range_end);
d9b3f62e 10177 print_recreate_thread (b, fp);
f1310107
TJB
10178}
10179
10180/* The breakpoint_ops structure to be used in ranged breakpoints. */
10181
2060206e 10182static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10183
10184/* Find the address where the end of the breakpoint range should be
10185 placed, given the SAL of the end of the range. This is so that if
10186 the user provides a line number, the end of the range is set to the
10187 last instruction of the given line. */
10188
10189static CORE_ADDR
10190find_breakpoint_range_end (struct symtab_and_line sal)
10191{
10192 CORE_ADDR end;
10193
10194 /* If the user provided a PC value, use it. Otherwise,
10195 find the address of the end of the given location. */
10196 if (sal.explicit_pc)
10197 end = sal.pc;
10198 else
10199 {
10200 int ret;
10201 CORE_ADDR start;
10202
10203 ret = find_line_pc_range (sal, &start, &end);
10204 if (!ret)
10205 error (_("Could not find location of the end of the range."));
10206
10207 /* find_line_pc_range returns the start of the next line. */
10208 end--;
10209 }
10210
10211 return end;
10212}
10213
10214/* Implement the "break-range" CLI command. */
10215
10216static void
10217break_range_command (char *arg, int from_tty)
10218{
10219 char *arg_start, *addr_string_start, *addr_string_end;
10220 struct linespec_result canonical_start, canonical_end;
10221 int bp_count, can_use_bp, length;
10222 CORE_ADDR end;
10223 struct breakpoint *b;
10224 struct symtab_and_line sal_start, sal_end;
f1310107 10225 struct cleanup *cleanup_bkpt;
f8eba3c6 10226 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10227
10228 /* We don't support software ranged breakpoints. */
10229 if (target_ranged_break_num_registers () < 0)
10230 error (_("This target does not support hardware ranged breakpoints."));
10231
10232 bp_count = hw_breakpoint_used_count ();
10233 bp_count += target_ranged_break_num_registers ();
10234 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10235 bp_count, 0);
10236 if (can_use_bp < 0)
10237 error (_("Hardware breakpoints used exceeds limit."));
10238
f8eba3c6 10239 arg = skip_spaces (arg);
f1310107
TJB
10240 if (arg == NULL || arg[0] == '\0')
10241 error(_("No address range specified."));
10242
f1310107
TJB
10243 init_linespec_result (&canonical_start);
10244
f8eba3c6
TT
10245 arg_start = arg;
10246 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10247
f8eba3c6 10248 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10249
10250 if (arg[0] != ',')
10251 error (_("Too few arguments."));
f8eba3c6 10252 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10253 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10254
10255 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10256
10257 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10258 || lsal_start->sals.nelts != 1)
f1310107
TJB
10259 error (_("Cannot create a ranged breakpoint with multiple locations."));
10260
f8eba3c6
TT
10261 sal_start = lsal_start->sals.sals[0];
10262 addr_string_start = savestring (arg_start, arg - arg_start);
10263 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10264
10265 arg++; /* Skip the comma. */
f8eba3c6 10266 arg = skip_spaces (arg);
f1310107
TJB
10267
10268 /* Parse the end location. */
10269
f1310107
TJB
10270 init_linespec_result (&canonical_end);
10271 arg_start = arg;
10272
f8eba3c6 10273 /* We call decode_line_full directly here instead of using
f1310107
TJB
10274 parse_breakpoint_sals because we need to specify the start location's
10275 symtab and line as the default symtab and line for the end of the
10276 range. This makes it possible to have ranges like "foo.c:27, +14",
10277 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10278 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10279 sal_start.symtab, sal_start.line,
10280 &canonical_end, NULL, NULL);
10281
10282 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10283
f8eba3c6 10284 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10285 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10286
10287 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10288 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10289 || lsal_end->sals.nelts != 1)
f1310107
TJB
10290 error (_("Cannot create a ranged breakpoint with multiple locations."));
10291
f8eba3c6
TT
10292 sal_end = lsal_end->sals.sals[0];
10293 addr_string_end = savestring (arg_start, arg - arg_start);
10294 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10295
10296 end = find_breakpoint_range_end (sal_end);
10297 if (sal_start.pc > end)
177b42fe 10298 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10299
10300 length = end - sal_start.pc + 1;
10301 if (length < 0)
10302 /* Length overflowed. */
10303 error (_("Address range too large."));
10304 else if (length == 1)
10305 {
10306 /* This range is simple enough to be handled by
10307 the `hbreak' command. */
10308 hbreak_command (addr_string_start, 1);
10309
10310 do_cleanups (cleanup_bkpt);
10311
10312 return;
10313 }
10314
10315 /* Now set up the breakpoint. */
10316 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10317 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10318 set_breakpoint_count (breakpoint_count + 1);
10319 b->number = breakpoint_count;
10320 b->disposition = disp_donttouch;
f8eba3c6
TT
10321 b->addr_string = xstrdup (addr_string_start);
10322 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10323 b->loc->length = length;
10324
f8eba3c6 10325 do_cleanups (cleanup_bkpt);
f1310107
TJB
10326
10327 mention (b);
8d3788bd 10328 observer_notify_breakpoint_created (b);
44702360 10329 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10330}
10331
4a64f543
MS
10332/* Return non-zero if EXP is verified as constant. Returned zero
10333 means EXP is variable. Also the constant detection may fail for
10334 some constant expressions and in such case still falsely return
10335 zero. */
2e6e3d9c 10336
65d79d4b
SDJ
10337static int
10338watchpoint_exp_is_const (const struct expression *exp)
10339{
10340 int i = exp->nelts;
10341
10342 while (i > 0)
10343 {
10344 int oplenp, argsp;
10345
10346 /* We are only interested in the descriptor of each element. */
10347 operator_length (exp, i, &oplenp, &argsp);
10348 i -= oplenp;
10349
10350 switch (exp->elts[i].opcode)
10351 {
10352 case BINOP_ADD:
10353 case BINOP_SUB:
10354 case BINOP_MUL:
10355 case BINOP_DIV:
10356 case BINOP_REM:
10357 case BINOP_MOD:
10358 case BINOP_LSH:
10359 case BINOP_RSH:
10360 case BINOP_LOGICAL_AND:
10361 case BINOP_LOGICAL_OR:
10362 case BINOP_BITWISE_AND:
10363 case BINOP_BITWISE_IOR:
10364 case BINOP_BITWISE_XOR:
10365 case BINOP_EQUAL:
10366 case BINOP_NOTEQUAL:
10367 case BINOP_LESS:
10368 case BINOP_GTR:
10369 case BINOP_LEQ:
10370 case BINOP_GEQ:
10371 case BINOP_REPEAT:
10372 case BINOP_COMMA:
10373 case BINOP_EXP:
10374 case BINOP_MIN:
10375 case BINOP_MAX:
10376 case BINOP_INTDIV:
10377 case BINOP_CONCAT:
65d79d4b
SDJ
10378 case TERNOP_COND:
10379 case TERNOP_SLICE:
65d79d4b
SDJ
10380
10381 case OP_LONG:
10382 case OP_DOUBLE:
10383 case OP_DECFLOAT:
10384 case OP_LAST:
10385 case OP_COMPLEX:
10386 case OP_STRING:
65d79d4b
SDJ
10387 case OP_ARRAY:
10388 case OP_TYPE:
608b4967
TT
10389 case OP_TYPEOF:
10390 case OP_DECLTYPE:
6e72ca20 10391 case OP_TYPEID:
65d79d4b
SDJ
10392 case OP_NAME:
10393 case OP_OBJC_NSSTRING:
10394
10395 case UNOP_NEG:
10396 case UNOP_LOGICAL_NOT:
10397 case UNOP_COMPLEMENT:
10398 case UNOP_ADDR:
10399 case UNOP_HIGH:
aeaa2474 10400 case UNOP_CAST:
9eaf6705
TT
10401
10402 case UNOP_CAST_TYPE:
10403 case UNOP_REINTERPRET_CAST:
10404 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10405 /* Unary, binary and ternary operators: We have to check
10406 their operands. If they are constant, then so is the
10407 result of that operation. For instance, if A and B are
10408 determined to be constants, then so is "A + B".
10409
10410 UNOP_IND is one exception to the rule above, because the
10411 value of *ADDR is not necessarily a constant, even when
10412 ADDR is. */
65d79d4b
SDJ
10413 break;
10414
10415 case OP_VAR_VALUE:
10416 /* Check whether the associated symbol is a constant.
4a64f543 10417
65d79d4b 10418 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10419 possible that a buggy compiler could mark a variable as
10420 constant even when it is not, and TYPE_CONST would return
10421 true in this case, while SYMBOL_CLASS wouldn't.
10422
10423 We also have to check for function symbols because they
10424 are always constant. */
65d79d4b
SDJ
10425 {
10426 struct symbol *s = exp->elts[i + 2].symbol;
10427
10428 if (SYMBOL_CLASS (s) != LOC_BLOCK
10429 && SYMBOL_CLASS (s) != LOC_CONST
10430 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10431 return 0;
10432 break;
10433 }
10434
10435 /* The default action is to return 0 because we are using
10436 the optimistic approach here: If we don't know something,
10437 then it is not a constant. */
10438 default:
10439 return 0;
10440 }
10441 }
10442
10443 return 1;
10444}
10445
3a5c3e22
PA
10446/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10447
10448static void
10449dtor_watchpoint (struct breakpoint *self)
10450{
10451 struct watchpoint *w = (struct watchpoint *) self;
10452
10453 xfree (w->cond_exp);
10454 xfree (w->exp);
10455 xfree (w->exp_string);
10456 xfree (w->exp_string_reparse);
10457 value_free (w->val);
10458
10459 base_breakpoint_ops.dtor (self);
10460}
10461
348d480f
PA
10462/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10463
10464static void
10465re_set_watchpoint (struct breakpoint *b)
10466{
3a5c3e22
PA
10467 struct watchpoint *w = (struct watchpoint *) b;
10468
348d480f
PA
10469 /* Watchpoint can be either on expression using entirely global
10470 variables, or it can be on local variables.
10471
10472 Watchpoints of the first kind are never auto-deleted, and even
10473 persist across program restarts. Since they can use variables
10474 from shared libraries, we need to reparse expression as libraries
10475 are loaded and unloaded.
10476
10477 Watchpoints on local variables can also change meaning as result
10478 of solib event. For example, if a watchpoint uses both a local
10479 and a global variables in expression, it's a local watchpoint,
10480 but unloading of a shared library will make the expression
10481 invalid. This is not a very common use case, but we still
10482 re-evaluate expression, to avoid surprises to the user.
10483
10484 Note that for local watchpoints, we re-evaluate it only if
10485 watchpoints frame id is still valid. If it's not, it means the
10486 watchpoint is out of scope and will be deleted soon. In fact,
10487 I'm not sure we'll ever be called in this case.
10488
10489 If a local watchpoint's frame id is still valid, then
3a5c3e22 10490 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10491
3a5c3e22
PA
10492 Don't do anything about disabled watchpoints, since they will be
10493 reevaluated again when enabled. */
10494 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10495}
10496
77b06cd7
TJB
10497/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10498
10499static int
10500insert_watchpoint (struct bp_location *bl)
10501{
3a5c3e22
PA
10502 struct watchpoint *w = (struct watchpoint *) bl->owner;
10503 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10504
10505 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10506 w->cond_exp);
77b06cd7
TJB
10507}
10508
10509/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10510
10511static int
10512remove_watchpoint (struct bp_location *bl)
10513{
3a5c3e22
PA
10514 struct watchpoint *w = (struct watchpoint *) bl->owner;
10515 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10516
10517 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10518 w->cond_exp);
e09342b5
TJB
10519}
10520
e09342b5 10521static int
348d480f 10522breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10523 struct address_space *aspace, CORE_ADDR bp_addr,
10524 const struct target_waitstatus *ws)
e09342b5 10525{
348d480f 10526 struct breakpoint *b = bl->owner;
3a5c3e22 10527 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10528
348d480f
PA
10529 /* Continuable hardware watchpoints are treated as non-existent if the
10530 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10531 some data address). Otherwise gdb won't stop on a break instruction
10532 in the code (not from a breakpoint) when a hardware watchpoint has
10533 been defined. Also skip watchpoints which we know did not trigger
10534 (did not match the data address). */
10535 if (is_hardware_watchpoint (b)
3a5c3e22 10536 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10537 return 0;
9c06b0b4 10538
348d480f 10539 return 1;
9c06b0b4
TJB
10540}
10541
348d480f
PA
10542static void
10543check_status_watchpoint (bpstat bs)
9c06b0b4 10544{
348d480f 10545 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10546
348d480f 10547 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10548}
10549
10550/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10551 hardware watchpoints. */
9c06b0b4
TJB
10552
10553static int
348d480f 10554resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10555{
3a5c3e22
PA
10556 struct watchpoint *w = (struct watchpoint *) bl->owner;
10557 int length = w->exact? 1 : bl->length;
348d480f
PA
10558
10559 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10560}
10561
10562/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10563 hardware watchpoints. */
9c06b0b4
TJB
10564
10565static int
348d480f 10566works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10567{
efa80663
PA
10568 /* Read and access watchpoints only work with hardware support. */
10569 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10570}
10571
9c06b0b4 10572static enum print_stop_action
348d480f 10573print_it_watchpoint (bpstat bs)
9c06b0b4 10574{
348d480f
PA
10575 struct cleanup *old_chain;
10576 struct breakpoint *b;
f99d8bf4 10577 struct ui_file *stb;
348d480f 10578 enum print_stop_action result;
3a5c3e22 10579 struct watchpoint *w;
79a45e25 10580 struct ui_out *uiout = current_uiout;
348d480f
PA
10581
10582 gdb_assert (bs->bp_location_at != NULL);
10583
348d480f 10584 b = bs->breakpoint_at;
3a5c3e22 10585 w = (struct watchpoint *) b;
348d480f 10586
f99d8bf4
PA
10587 stb = mem_fileopen ();
10588 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10589
10590 switch (b->type)
10591 {
348d480f 10592 case bp_watchpoint:
9c06b0b4
TJB
10593 case bp_hardware_watchpoint:
10594 annotate_watchpoint (b->number);
10595 if (ui_out_is_mi_like_p (uiout))
10596 ui_out_field_string
10597 (uiout, "reason",
10598 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10599 mention (b);
10600 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10601 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10602 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10603 ui_out_field_stream (uiout, "old", stb);
10604 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10605 watchpoint_value_print (w->val, stb);
348d480f
PA
10606 ui_out_field_stream (uiout, "new", stb);
10607 ui_out_text (uiout, "\n");
10608 /* More than one watchpoint may have been triggered. */
10609 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10610 break;
10611
10612 case bp_read_watchpoint:
10613 if (ui_out_is_mi_like_p (uiout))
10614 ui_out_field_string
10615 (uiout, "reason",
10616 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10617 mention (b);
10618 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10619 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10620 watchpoint_value_print (w->val, stb);
348d480f
PA
10621 ui_out_field_stream (uiout, "value", stb);
10622 ui_out_text (uiout, "\n");
10623 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10624 break;
10625
10626 case bp_access_watchpoint:
348d480f
PA
10627 if (bs->old_val != NULL)
10628 {
10629 annotate_watchpoint (b->number);
10630 if (ui_out_is_mi_like_p (uiout))
10631 ui_out_field_string
10632 (uiout, "reason",
10633 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10634 mention (b);
10635 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10636 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10637 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10638 ui_out_field_stream (uiout, "old", stb);
10639 ui_out_text (uiout, "\nNew value = ");
10640 }
10641 else
10642 {
10643 mention (b);
10644 if (ui_out_is_mi_like_p (uiout))
10645 ui_out_field_string
10646 (uiout, "reason",
10647 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10648 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10649 ui_out_text (uiout, "\nValue = ");
10650 }
f99d8bf4 10651 watchpoint_value_print (w->val, stb);
348d480f
PA
10652 ui_out_field_stream (uiout, "new", stb);
10653 ui_out_text (uiout, "\n");
10654 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10655 break;
10656 default:
348d480f 10657 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10658 }
10659
348d480f
PA
10660 do_cleanups (old_chain);
10661 return result;
10662}
10663
10664/* Implement the "print_mention" breakpoint_ops method for hardware
10665 watchpoints. */
10666
10667static void
10668print_mention_watchpoint (struct breakpoint *b)
10669{
10670 struct cleanup *ui_out_chain;
3a5c3e22 10671 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10672 struct ui_out *uiout = current_uiout;
348d480f
PA
10673
10674 switch (b->type)
10675 {
10676 case bp_watchpoint:
10677 ui_out_text (uiout, "Watchpoint ");
10678 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10679 break;
10680 case bp_hardware_watchpoint:
10681 ui_out_text (uiout, "Hardware watchpoint ");
10682 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10683 break;
10684 case bp_read_watchpoint:
10685 ui_out_text (uiout, "Hardware read watchpoint ");
10686 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10687 break;
10688 case bp_access_watchpoint:
10689 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10690 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10691 break;
10692 default:
10693 internal_error (__FILE__, __LINE__,
10694 _("Invalid hardware watchpoint type."));
10695 }
10696
10697 ui_out_field_int (uiout, "number", b->number);
10698 ui_out_text (uiout, ": ");
3a5c3e22 10699 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10700 do_cleanups (ui_out_chain);
10701}
10702
10703/* Implement the "print_recreate" breakpoint_ops method for
10704 watchpoints. */
10705
10706static void
10707print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10708{
3a5c3e22
PA
10709 struct watchpoint *w = (struct watchpoint *) b;
10710
348d480f
PA
10711 switch (b->type)
10712 {
10713 case bp_watchpoint:
10714 case bp_hardware_watchpoint:
10715 fprintf_unfiltered (fp, "watch");
10716 break;
10717 case bp_read_watchpoint:
10718 fprintf_unfiltered (fp, "rwatch");
10719 break;
10720 case bp_access_watchpoint:
10721 fprintf_unfiltered (fp, "awatch");
10722 break;
10723 default:
10724 internal_error (__FILE__, __LINE__,
10725 _("Invalid watchpoint type."));
10726 }
10727
3a5c3e22 10728 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10729 print_recreate_thread (b, fp);
348d480f
PA
10730}
10731
427cd150
TT
10732/* Implement the "explains_signal" breakpoint_ops method for
10733 watchpoints. */
10734
47591c29 10735static int
427cd150
TT
10736explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10737{
10738 /* A software watchpoint cannot cause a signal other than
10739 GDB_SIGNAL_TRAP. */
10740 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10741 return 0;
427cd150 10742
47591c29 10743 return 1;
427cd150
TT
10744}
10745
348d480f
PA
10746/* The breakpoint_ops structure to be used in hardware watchpoints. */
10747
2060206e 10748static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10749
10750/* Implement the "insert" breakpoint_ops method for
10751 masked hardware watchpoints. */
10752
10753static int
10754insert_masked_watchpoint (struct bp_location *bl)
10755{
3a5c3e22
PA
10756 struct watchpoint *w = (struct watchpoint *) bl->owner;
10757
10758 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10759 bl->watchpoint_type);
10760}
10761
10762/* Implement the "remove" breakpoint_ops method for
10763 masked hardware watchpoints. */
10764
10765static int
10766remove_masked_watchpoint (struct bp_location *bl)
10767{
3a5c3e22
PA
10768 struct watchpoint *w = (struct watchpoint *) bl->owner;
10769
10770 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10771 bl->watchpoint_type);
10772}
10773
10774/* Implement the "resources_needed" breakpoint_ops method for
10775 masked hardware watchpoints. */
10776
10777static int
10778resources_needed_masked_watchpoint (const struct bp_location *bl)
10779{
3a5c3e22
PA
10780 struct watchpoint *w = (struct watchpoint *) bl->owner;
10781
10782 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10783}
10784
10785/* Implement the "works_in_software_mode" breakpoint_ops method for
10786 masked hardware watchpoints. */
10787
10788static int
10789works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10790{
10791 return 0;
10792}
10793
10794/* Implement the "print_it" breakpoint_ops method for
10795 masked hardware watchpoints. */
10796
10797static enum print_stop_action
10798print_it_masked_watchpoint (bpstat bs)
10799{
10800 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10801 struct ui_out *uiout = current_uiout;
348d480f
PA
10802
10803 /* Masked watchpoints have only one location. */
10804 gdb_assert (b->loc && b->loc->next == NULL);
10805
10806 switch (b->type)
10807 {
10808 case bp_hardware_watchpoint:
10809 annotate_watchpoint (b->number);
10810 if (ui_out_is_mi_like_p (uiout))
10811 ui_out_field_string
10812 (uiout, "reason",
10813 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10814 break;
10815
10816 case bp_read_watchpoint:
10817 if (ui_out_is_mi_like_p (uiout))
10818 ui_out_field_string
10819 (uiout, "reason",
10820 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10821 break;
10822
10823 case bp_access_watchpoint:
10824 if (ui_out_is_mi_like_p (uiout))
10825 ui_out_field_string
10826 (uiout, "reason",
10827 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10828 break;
10829 default:
10830 internal_error (__FILE__, __LINE__,
10831 _("Invalid hardware watchpoint type."));
10832 }
10833
10834 mention (b);
9c06b0b4
TJB
10835 ui_out_text (uiout, _("\n\
10836Check the underlying instruction at PC for the memory\n\
10837address and value which triggered this watchpoint.\n"));
10838 ui_out_text (uiout, "\n");
10839
10840 /* More than one watchpoint may have been triggered. */
10841 return PRINT_UNKNOWN;
10842}
10843
10844/* Implement the "print_one_detail" breakpoint_ops method for
10845 masked hardware watchpoints. */
10846
10847static void
10848print_one_detail_masked_watchpoint (const struct breakpoint *b,
10849 struct ui_out *uiout)
10850{
3a5c3e22
PA
10851 struct watchpoint *w = (struct watchpoint *) b;
10852
9c06b0b4
TJB
10853 /* Masked watchpoints have only one location. */
10854 gdb_assert (b->loc && b->loc->next == NULL);
10855
10856 ui_out_text (uiout, "\tmask ");
3a5c3e22 10857 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
10858 ui_out_text (uiout, "\n");
10859}
10860
10861/* Implement the "print_mention" breakpoint_ops method for
10862 masked hardware watchpoints. */
10863
10864static void
10865print_mention_masked_watchpoint (struct breakpoint *b)
10866{
3a5c3e22 10867 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10868 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
10869 struct cleanup *ui_out_chain;
10870
10871 switch (b->type)
10872 {
10873 case bp_hardware_watchpoint:
10874 ui_out_text (uiout, "Masked hardware watchpoint ");
10875 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10876 break;
10877 case bp_read_watchpoint:
10878 ui_out_text (uiout, "Masked hardware read watchpoint ");
10879 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10880 break;
10881 case bp_access_watchpoint:
10882 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10883 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10884 break;
10885 default:
10886 internal_error (__FILE__, __LINE__,
10887 _("Invalid hardware watchpoint type."));
10888 }
10889
10890 ui_out_field_int (uiout, "number", b->number);
10891 ui_out_text (uiout, ": ");
3a5c3e22 10892 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
10893 do_cleanups (ui_out_chain);
10894}
10895
10896/* Implement the "print_recreate" breakpoint_ops method for
10897 masked hardware watchpoints. */
10898
10899static void
10900print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10901{
3a5c3e22 10902 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10903 char tmp[40];
10904
10905 switch (b->type)
10906 {
10907 case bp_hardware_watchpoint:
10908 fprintf_unfiltered (fp, "watch");
10909 break;
10910 case bp_read_watchpoint:
10911 fprintf_unfiltered (fp, "rwatch");
10912 break;
10913 case bp_access_watchpoint:
10914 fprintf_unfiltered (fp, "awatch");
10915 break;
10916 default:
10917 internal_error (__FILE__, __LINE__,
10918 _("Invalid hardware watchpoint type."));
10919 }
10920
3a5c3e22
PA
10921 sprintf_vma (tmp, w->hw_wp_mask);
10922 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10923 print_recreate_thread (b, fp);
9c06b0b4
TJB
10924}
10925
10926/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10927
2060206e 10928static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10929
10930/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10931
10932static int
10933is_masked_watchpoint (const struct breakpoint *b)
10934{
10935 return b->ops == &masked_watchpoint_breakpoint_ops;
10936}
10937
53a5351d
JM
10938/* accessflag: hw_write: watch write,
10939 hw_read: watch read,
10940 hw_access: watch access (read or write) */
c906108c 10941static void
bbc13ae3 10942watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10943 int just_location, int internal)
c906108c 10944{
d983da9c 10945 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 10946 struct expression *exp;
270140bd 10947 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10948 struct value *val, *mark, *result;
bb9d5f81 10949 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 10950 struct frame_info *frame;
bbc13ae3
KS
10951 const char *exp_start = NULL;
10952 const char *exp_end = NULL;
10953 const char *tok, *end_tok;
9c06b0b4 10954 int toklen = -1;
bbc13ae3
KS
10955 const char *cond_start = NULL;
10956 const char *cond_end = NULL;
c906108c 10957 enum bptype bp_type;
37e4754d 10958 int thread = -1;
0cf6dd15 10959 int pc = 0;
9c06b0b4
TJB
10960 /* Flag to indicate whether we are going to use masks for
10961 the hardware watchpoint. */
10962 int use_mask = 0;
10963 CORE_ADDR mask = 0;
3a5c3e22 10964 struct watchpoint *w;
bbc13ae3
KS
10965 char *expression;
10966 struct cleanup *back_to;
c906108c 10967
37e4754d
LM
10968 /* Make sure that we actually have parameters to parse. */
10969 if (arg != NULL && arg[0] != '\0')
10970 {
bbc13ae3
KS
10971 const char *value_start;
10972
10973 exp_end = arg + strlen (arg);
37e4754d 10974
9c06b0b4
TJB
10975 /* Look for "parameter value" pairs at the end
10976 of the arguments string. */
bbc13ae3 10977 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10978 {
10979 /* Skip whitespace at the end of the argument list. */
10980 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10981 tok--;
10982
10983 /* Find the beginning of the last token.
10984 This is the value of the parameter. */
10985 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10986 tok--;
10987 value_start = tok + 1;
10988
10989 /* Skip whitespace. */
10990 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10991 tok--;
10992
10993 end_tok = tok;
10994
10995 /* Find the beginning of the second to last token.
10996 This is the parameter itself. */
10997 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10998 tok--;
10999 tok++;
11000 toklen = end_tok - tok + 1;
11001
61012eef 11002 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4
TJB
11003 {
11004 /* At this point we've found a "thread" token, which means
11005 the user is trying to set a watchpoint that triggers
11006 only in a specific thread. */
11007 char *endp;
37e4754d 11008
9c06b0b4
TJB
11009 if (thread != -1)
11010 error(_("You can specify only one thread."));
37e4754d 11011
9c06b0b4
TJB
11012 /* Extract the thread ID from the next token. */
11013 thread = strtol (value_start, &endp, 0);
37e4754d 11014
9c06b0b4
TJB
11015 /* Check if the user provided a valid numeric value for the
11016 thread ID. */
11017 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11018 error (_("Invalid thread ID specification %s."), value_start);
11019
11020 /* Check if the thread actually exists. */
11021 if (!valid_thread_id (thread))
af4908ba 11022 invalid_thread_id_error (thread);
9c06b0b4 11023 }
61012eef 11024 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11025 {
11026 /* We've found a "mask" token, which means the user wants to
11027 create a hardware watchpoint that is going to have the mask
11028 facility. */
11029 struct value *mask_value, *mark;
37e4754d 11030
9c06b0b4
TJB
11031 if (use_mask)
11032 error(_("You can specify only one mask."));
37e4754d 11033
9c06b0b4 11034 use_mask = just_location = 1;
37e4754d 11035
9c06b0b4
TJB
11036 mark = value_mark ();
11037 mask_value = parse_to_comma_and_eval (&value_start);
11038 mask = value_as_address (mask_value);
11039 value_free_to_mark (mark);
11040 }
11041 else
11042 /* We didn't recognize what we found. We should stop here. */
11043 break;
37e4754d 11044
9c06b0b4
TJB
11045 /* Truncate the string and get rid of the "parameter value" pair before
11046 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11047 exp_end = tok;
9c06b0b4 11048 }
37e4754d 11049 }
bbc13ae3
KS
11050 else
11051 exp_end = arg;
37e4754d 11052
bbc13ae3
KS
11053 /* Parse the rest of the arguments. From here on out, everything
11054 is in terms of a newly allocated string instead of the original
11055 ARG. */
c906108c 11056 innermost_block = NULL;
bbc13ae3
KS
11057 expression = savestring (arg, exp_end - arg);
11058 back_to = make_cleanup (xfree, expression);
11059 exp_start = arg = expression;
1bb9788d 11060 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11061 exp_end = arg;
fa8a61dc
TT
11062 /* Remove trailing whitespace from the expression before saving it.
11063 This makes the eventual display of the expression string a bit
11064 prettier. */
11065 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11066 --exp_end;
11067
65d79d4b
SDJ
11068 /* Checking if the expression is not constant. */
11069 if (watchpoint_exp_is_const (exp))
11070 {
11071 int len;
11072
11073 len = exp_end - exp_start;
11074 while (len > 0 && isspace (exp_start[len - 1]))
11075 len--;
11076 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11077 }
11078
c906108c
SS
11079 exp_valid_block = innermost_block;
11080 mark = value_mark ();
3a1115a0 11081 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11082
bb9d5f81
PP
11083 if (val != NULL && just_location)
11084 {
11085 saved_bitpos = value_bitpos (val);
11086 saved_bitsize = value_bitsize (val);
11087 }
11088
06a64a0b
TT
11089 if (just_location)
11090 {
9c06b0b4
TJB
11091 int ret;
11092
06a64a0b 11093 exp_valid_block = NULL;
a1442452 11094 val = value_addr (result);
06a64a0b
TT
11095 release_value (val);
11096 value_free_to_mark (mark);
9c06b0b4
TJB
11097
11098 if (use_mask)
11099 {
11100 ret = target_masked_watch_num_registers (value_as_address (val),
11101 mask);
11102 if (ret == -1)
11103 error (_("This target does not support masked watchpoints."));
11104 else if (ret == -2)
11105 error (_("Invalid mask or memory region."));
11106 }
06a64a0b
TT
11107 }
11108 else if (val != NULL)
fa4727a6 11109 release_value (val);
c906108c 11110
bbc13ae3
KS
11111 tok = skip_spaces_const (arg);
11112 end_tok = skip_to_space_const (tok);
c906108c
SS
11113
11114 toklen = end_tok - tok;
11115 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11116 {
2d134ed3
PA
11117 struct expression *cond;
11118
60e1c644 11119 innermost_block = NULL;
c906108c 11120 tok = cond_start = end_tok + 1;
1bb9788d 11121 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11122
11123 /* The watchpoint expression may not be local, but the condition
11124 may still be. E.g.: `watch global if local > 0'. */
11125 cond_exp_valid_block = innermost_block;
11126
2d134ed3 11127 xfree (cond);
c906108c
SS
11128 cond_end = tok;
11129 }
11130 if (*tok)
8a3fe4f8 11131 error (_("Junk at end of command."));
c906108c 11132
d983da9c 11133 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11134
11135 /* If the expression is "local", then set up a "watchpoint scope"
11136 breakpoint at the point where we've left the scope of the watchpoint
11137 expression. Create the scope breakpoint before the watchpoint, so
11138 that we will encounter it first in bpstat_stop_status. */
60e1c644 11139 if (exp_valid_block && frame)
d983da9c 11140 {
edb3359d
DJ
11141 if (frame_id_p (frame_unwind_caller_id (frame)))
11142 {
11143 scope_breakpoint
a6d9a66e
UW
11144 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11145 frame_unwind_caller_pc (frame),
06edf0c0
PA
11146 bp_watchpoint_scope,
11147 &momentary_breakpoint_ops);
d983da9c 11148
edb3359d 11149 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11150
edb3359d
DJ
11151 /* Automatically delete the breakpoint when it hits. */
11152 scope_breakpoint->disposition = disp_del;
d983da9c 11153
edb3359d
DJ
11154 /* Only break in the proper frame (help with recursion). */
11155 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11156
edb3359d 11157 /* Set the address at which we will stop. */
a6d9a66e
UW
11158 scope_breakpoint->loc->gdbarch
11159 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11160 scope_breakpoint->loc->requested_address
11161 = frame_unwind_caller_pc (frame);
11162 scope_breakpoint->loc->address
a6d9a66e
UW
11163 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11164 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11165 scope_breakpoint->type);
11166 }
d983da9c
DJ
11167 }
11168
e8369a73
AB
11169 /* Now set up the breakpoint. We create all watchpoints as hardware
11170 watchpoints here even if hardware watchpoints are turned off, a call
11171 to update_watchpoint later in this function will cause the type to
11172 drop back to bp_watchpoint (software watchpoint) if required. */
11173
11174 if (accessflag == hw_read)
11175 bp_type = bp_read_watchpoint;
11176 else if (accessflag == hw_access)
11177 bp_type = bp_access_watchpoint;
11178 else
11179 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11180
11181 w = XCNEW (struct watchpoint);
11182 b = &w->base;
348d480f 11183 if (use_mask)
3a5c3e22
PA
11184 init_raw_breakpoint_without_location (b, NULL, bp_type,
11185 &masked_watchpoint_breakpoint_ops);
348d480f 11186 else
3a5c3e22
PA
11187 init_raw_breakpoint_without_location (b, NULL, bp_type,
11188 &watchpoint_breakpoint_ops);
37e4754d 11189 b->thread = thread;
b5de0fa7 11190 b->disposition = disp_donttouch;
348d480f 11191 b->pspace = current_program_space;
3a5c3e22
PA
11192 w->exp = exp;
11193 w->exp_valid_block = exp_valid_block;
11194 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11195 if (just_location)
11196 {
11197 struct type *t = value_type (val);
11198 CORE_ADDR addr = value_as_address (val);
11199 char *name;
11200
11201 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11202 name = type_to_string (t);
11203
3a5c3e22 11204 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11205 core_addr_to_string (addr));
06a64a0b
TT
11206 xfree (name);
11207
3a5c3e22 11208 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11209 (int) (exp_end - exp_start), exp_start);
11210
06a64a0b
TT
11211 /* The above expression is in C. */
11212 b->language = language_c;
11213 }
11214 else
3a5c3e22 11215 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11216
11217 if (use_mask)
11218 {
3a5c3e22 11219 w->hw_wp_mask = mask;
9c06b0b4
TJB
11220 }
11221 else
11222 {
3a5c3e22 11223 w->val = val;
bb9d5f81
PP
11224 w->val_bitpos = saved_bitpos;
11225 w->val_bitsize = saved_bitsize;
3a5c3e22 11226 w->val_valid = 1;
9c06b0b4 11227 }
77b06cd7 11228
c906108c
SS
11229 if (cond_start)
11230 b->cond_string = savestring (cond_start, cond_end - cond_start);
11231 else
11232 b->cond_string = 0;
c5aa993b 11233
c906108c 11234 if (frame)
f6bc2008 11235 {
3a5c3e22
PA
11236 w->watchpoint_frame = get_frame_id (frame);
11237 w->watchpoint_thread = inferior_ptid;
f6bc2008 11238 }
c906108c 11239 else
f6bc2008 11240 {
3a5c3e22
PA
11241 w->watchpoint_frame = null_frame_id;
11242 w->watchpoint_thread = null_ptid;
f6bc2008 11243 }
c906108c 11244
d983da9c 11245 if (scope_breakpoint != NULL)
c906108c 11246 {
d983da9c
DJ
11247 /* The scope breakpoint is related to the watchpoint. We will
11248 need to act on them together. */
11249 b->related_breakpoint = scope_breakpoint;
11250 scope_breakpoint->related_breakpoint = b;
c906108c 11251 }
d983da9c 11252
06a64a0b
TT
11253 if (!just_location)
11254 value_free_to_mark (mark);
2d134ed3 11255
492d29ea 11256 TRY
a9634178
TJB
11257 {
11258 /* Finally update the new watchpoint. This creates the locations
11259 that should be inserted. */
3a5c3e22 11260 update_watchpoint (w, 1);
a9634178 11261 }
492d29ea 11262 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11263 {
11264 delete_breakpoint (b);
11265 throw_exception (e);
11266 }
492d29ea 11267 END_CATCH
a9634178 11268
3ea46bff 11269 install_breakpoint (internal, b, 1);
bbc13ae3 11270 do_cleanups (back_to);
c906108c
SS
11271}
11272
e09342b5 11273/* Return count of debug registers needed to watch the given expression.
e09342b5 11274 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11275
c906108c 11276static int
a9634178 11277can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11278{
11279 int found_memory_cnt = 0;
2e70b7b9 11280 struct value *head = v;
c906108c
SS
11281
11282 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11283 if (!can_use_hw_watchpoints)
c906108c 11284 return 0;
c5aa993b 11285
5c44784c
JM
11286 /* Make sure that the value of the expression depends only upon
11287 memory contents, and values computed from them within GDB. If we
11288 find any register references or function calls, we can't use a
11289 hardware watchpoint.
11290
11291 The idea here is that evaluating an expression generates a series
11292 of values, one holding the value of every subexpression. (The
11293 expression a*b+c has five subexpressions: a, b, a*b, c, and
11294 a*b+c.) GDB's values hold almost enough information to establish
11295 the criteria given above --- they identify memory lvalues,
11296 register lvalues, computed values, etcetera. So we can evaluate
11297 the expression, and then scan the chain of values that leaves
11298 behind to decide whether we can detect any possible change to the
11299 expression's final value using only hardware watchpoints.
11300
11301 However, I don't think that the values returned by inferior
11302 function calls are special in any way. So this function may not
11303 notice that an expression involving an inferior function call
11304 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11305 for (; v; v = value_next (v))
c906108c 11306 {
5c44784c 11307 if (VALUE_LVAL (v) == lval_memory)
c906108c 11308 {
8464be76
DJ
11309 if (v != head && value_lazy (v))
11310 /* A lazy memory lvalue in the chain is one that GDB never
11311 needed to fetch; we either just used its address (e.g.,
11312 `a' in `a.b') or we never needed it at all (e.g., `a'
11313 in `a,b'). This doesn't apply to HEAD; if that is
11314 lazy then it was not readable, but watch it anyway. */
5c44784c 11315 ;
53a5351d 11316 else
5c44784c
JM
11317 {
11318 /* Ahh, memory we actually used! Check if we can cover
11319 it with hardware watchpoints. */
df407dfe 11320 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11321
11322 /* We only watch structs and arrays if user asked for it
11323 explicitly, never if they just happen to appear in a
11324 middle of some value chain. */
11325 if (v == head
11326 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11327 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11328 {
42ae5230 11329 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11330 int len;
11331 int num_regs;
11332
a9634178 11333 len = (target_exact_watchpoints
e09342b5
TJB
11334 && is_scalar_type_recursive (vtype))?
11335 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11336
e09342b5
TJB
11337 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11338 if (!num_regs)
2e70b7b9
MS
11339 return 0;
11340 else
e09342b5 11341 found_memory_cnt += num_regs;
2e70b7b9 11342 }
5c44784c 11343 }
c5aa993b 11344 }
5086187c
AC
11345 else if (VALUE_LVAL (v) != not_lval
11346 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11347 return 0; /* These are values from the history (e.g., $1). */
5086187c 11348 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11349 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11350 }
11351
11352 /* The expression itself looks suitable for using a hardware
11353 watchpoint, but give the target machine a chance to reject it. */
11354 return found_memory_cnt;
11355}
11356
8b93c638 11357void
84f4c1fe 11358watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11359{
84f4c1fe 11360 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11361}
11362
06a64a0b
TT
11363/* A helper function that looks for the "-location" argument and then
11364 calls watch_command_1. */
11365
11366static void
11367watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11368{
11369 int just_location = 0;
11370
11371 if (arg
11372 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11373 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11374 {
e9cafbcc 11375 arg = skip_spaces (arg);
06a64a0b
TT
11376 just_location = 1;
11377 }
11378
84f4c1fe 11379 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11380}
8926118c 11381
c5aa993b 11382static void
fba45db2 11383watch_command (char *arg, int from_tty)
c906108c 11384{
06a64a0b 11385 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11386}
11387
8b93c638 11388void
84f4c1fe 11389rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11390{
84f4c1fe 11391 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11392}
8926118c 11393
c5aa993b 11394static void
fba45db2 11395rwatch_command (char *arg, int from_tty)
c906108c 11396{
06a64a0b 11397 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11398}
11399
8b93c638 11400void
84f4c1fe 11401awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11402{
84f4c1fe 11403 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11404}
8926118c 11405
c5aa993b 11406static void
fba45db2 11407awatch_command (char *arg, int from_tty)
c906108c 11408{
06a64a0b 11409 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11410}
c906108c 11411\f
c5aa993b 11412
43ff13b4 11413/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11414 because it uses the mechanisms of breakpoints. */
11415
bfec99b2
PA
11416struct until_break_command_continuation_args
11417{
11418 struct breakpoint *breakpoint;
11419 struct breakpoint *breakpoint2;
186c406b 11420 int thread_num;
bfec99b2
PA
11421};
11422
43ff13b4 11423/* This function is called by fetch_inferior_event via the
4a64f543 11424 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11425 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11426 command. */
c2c6d25f 11427static void
fa4cd53f 11428until_break_command_continuation (void *arg, int err)
43ff13b4 11429{
bfec99b2
PA
11430 struct until_break_command_continuation_args *a = arg;
11431
11432 delete_breakpoint (a->breakpoint);
11433 if (a->breakpoint2)
11434 delete_breakpoint (a->breakpoint2);
186c406b 11435 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11436}
11437
c906108c 11438void
ae66c1fc 11439until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11440{
11441 struct symtabs_and_lines sals;
11442 struct symtab_and_line sal;
8556afb4
PA
11443 struct frame_info *frame;
11444 struct gdbarch *frame_gdbarch;
11445 struct frame_id stack_frame_id;
11446 struct frame_id caller_frame_id;
c906108c 11447 struct breakpoint *breakpoint;
f107f563 11448 struct breakpoint *breakpoint2 = NULL;
c906108c 11449 struct cleanup *old_chain;
186c406b
TT
11450 int thread;
11451 struct thread_info *tp;
c906108c 11452
70509625 11453 clear_proceed_status (0);
c906108c
SS
11454
11455 /* Set a breakpoint where the user wants it and at return from
4a64f543 11456 this function. */
c5aa993b 11457
1bfeeb0f 11458 if (last_displayed_sal_is_valid ())
f8eba3c6 11459 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11460 get_last_displayed_symtab (),
f8eba3c6 11461 get_last_displayed_line ());
c906108c 11462 else
f8eba3c6
TT
11463 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11464 (struct symtab *) NULL, 0);
c5aa993b 11465
c906108c 11466 if (sals.nelts != 1)
8a3fe4f8 11467 error (_("Couldn't get information on specified line."));
c5aa993b 11468
c906108c 11469 sal = sals.sals[0];
4a64f543 11470 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11471
c906108c 11472 if (*arg)
8a3fe4f8 11473 error (_("Junk at end of arguments."));
c5aa993b 11474
c906108c 11475 resolve_sal_pc (&sal);
c5aa993b 11476
186c406b
TT
11477 tp = inferior_thread ();
11478 thread = tp->num;
11479
883bc8d1
PA
11480 old_chain = make_cleanup (null_cleanup, NULL);
11481
8556afb4
PA
11482 /* Note linespec handling above invalidates the frame chain.
11483 Installing a breakpoint also invalidates the frame chain (as it
11484 may need to switch threads), so do any frame handling before
11485 that. */
11486
11487 frame = get_selected_frame (NULL);
11488 frame_gdbarch = get_frame_arch (frame);
11489 stack_frame_id = get_stack_frame_id (frame);
11490 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11491
ae66c1fc
EZ
11492 /* Keep within the current frame, or in frames called by the current
11493 one. */
edb3359d 11494
883bc8d1 11495 if (frame_id_p (caller_frame_id))
c906108c 11496 {
883bc8d1
PA
11497 struct symtab_and_line sal2;
11498
11499 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11500 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11501 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11502 sal2,
11503 caller_frame_id,
f107f563
VP
11504 bp_until);
11505 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11506
883bc8d1 11507 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11508 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11509 }
c5aa993b 11510
c70a6932
JK
11511 /* set_momentary_breakpoint could invalidate FRAME. */
11512 frame = NULL;
11513
883bc8d1
PA
11514 if (anywhere)
11515 /* If the user told us to continue until a specified location,
11516 we don't specify a frame at which we need to stop. */
11517 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11518 null_frame_id, bp_until);
11519 else
11520 /* Otherwise, specify the selected frame, because we want to stop
11521 only at the very same frame. */
11522 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11523 stack_frame_id, bp_until);
11524 make_cleanup_delete_breakpoint (breakpoint);
11525
64ce06e4 11526 proceed (-1, GDB_SIGNAL_DEFAULT);
f107f563 11527
4a64f543
MS
11528 /* If we are running asynchronously, and proceed call above has
11529 actually managed to start the target, arrange for breakpoints to
11530 be deleted when the target stops. Otherwise, we're already
11531 stopped and delete breakpoints via cleanup chain. */
f107f563 11532
8ea051c5 11533 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11534 {
bfec99b2
PA
11535 struct until_break_command_continuation_args *args;
11536 args = xmalloc (sizeof (*args));
f107f563 11537
bfec99b2
PA
11538 args->breakpoint = breakpoint;
11539 args->breakpoint2 = breakpoint2;
186c406b 11540 args->thread_num = thread;
f107f563
VP
11541
11542 discard_cleanups (old_chain);
95e54da7
PA
11543 add_continuation (inferior_thread (),
11544 until_break_command_continuation, args,
604ead4a 11545 xfree);
f107f563
VP
11546 }
11547 else
c5aa993b 11548 do_cleanups (old_chain);
c906108c 11549}
ae66c1fc 11550
c906108c
SS
11551/* This function attempts to parse an optional "if <cond>" clause
11552 from the arg string. If one is not found, it returns NULL.
c5aa993b 11553
c906108c
SS
11554 Else, it returns a pointer to the condition string. (It does not
11555 attempt to evaluate the string against a particular block.) And,
11556 it updates arg to point to the first character following the parsed
4a64f543 11557 if clause in the arg string. */
53a5351d 11558
916703c0 11559char *
fba45db2 11560ep_parse_optional_if_clause (char **arg)
c906108c 11561{
c5aa993b
JM
11562 char *cond_string;
11563
11564 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11565 return NULL;
c5aa993b 11566
4a64f543 11567 /* Skip the "if" keyword. */
c906108c 11568 (*arg) += 2;
c5aa993b 11569
c906108c 11570 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11571 condition string. */
e9cafbcc 11572 *arg = skip_spaces (*arg);
c906108c 11573 cond_string = *arg;
c5aa993b 11574
4a64f543
MS
11575 /* Assume that the condition occupies the remainder of the arg
11576 string. */
c906108c 11577 (*arg) += strlen (cond_string);
c5aa993b 11578
c906108c
SS
11579 return cond_string;
11580}
c5aa993b 11581
c906108c
SS
11582/* Commands to deal with catching events, such as signals, exceptions,
11583 process start/exit, etc. */
c5aa993b
JM
11584
11585typedef enum
11586{
44feb3ce
TT
11587 catch_fork_temporary, catch_vfork_temporary,
11588 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11589}
11590catch_fork_kind;
11591
c906108c 11592static void
cc59ec59
MS
11593catch_fork_command_1 (char *arg, int from_tty,
11594 struct cmd_list_element *command)
c906108c 11595{
a6d9a66e 11596 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11597 char *cond_string = NULL;
44feb3ce
TT
11598 catch_fork_kind fork_kind;
11599 int tempflag;
11600
11601 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11602 tempflag = (fork_kind == catch_fork_temporary
11603 || fork_kind == catch_vfork_temporary);
c5aa993b 11604
44feb3ce
TT
11605 if (!arg)
11606 arg = "";
e9cafbcc 11607 arg = skip_spaces (arg);
c5aa993b 11608
c906108c 11609 /* The allowed syntax is:
c5aa993b
JM
11610 catch [v]fork
11611 catch [v]fork if <cond>
11612
4a64f543 11613 First, check if there's an if clause. */
c906108c 11614 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11615
c906108c 11616 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11617 error (_("Junk at end of arguments."));
c5aa993b 11618
c906108c 11619 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11620 and enable reporting of such events. */
c5aa993b
JM
11621 switch (fork_kind)
11622 {
44feb3ce
TT
11623 case catch_fork_temporary:
11624 case catch_fork_permanent:
a6d9a66e 11625 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11626 &catch_fork_breakpoint_ops);
c906108c 11627 break;
44feb3ce
TT
11628 case catch_vfork_temporary:
11629 case catch_vfork_permanent:
a6d9a66e 11630 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11631 &catch_vfork_breakpoint_ops);
c906108c 11632 break;
c5aa993b 11633 default:
8a3fe4f8 11634 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11635 break;
c5aa993b 11636 }
c906108c
SS
11637}
11638
11639static void
cc59ec59
MS
11640catch_exec_command_1 (char *arg, int from_tty,
11641 struct cmd_list_element *command)
c906108c 11642{
b4d90040 11643 struct exec_catchpoint *c;
a6d9a66e 11644 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11645 int tempflag;
c5aa993b 11646 char *cond_string = NULL;
c906108c 11647
44feb3ce
TT
11648 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11649
11650 if (!arg)
11651 arg = "";
e9cafbcc 11652 arg = skip_spaces (arg);
c906108c
SS
11653
11654 /* The allowed syntax is:
c5aa993b
JM
11655 catch exec
11656 catch exec if <cond>
c906108c 11657
4a64f543 11658 First, check if there's an if clause. */
c906108c
SS
11659 cond_string = ep_parse_optional_if_clause (&arg);
11660
11661 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11662 error (_("Junk at end of arguments."));
c906108c 11663
b4d90040
PA
11664 c = XNEW (struct exec_catchpoint);
11665 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11666 &catch_exec_breakpoint_ops);
11667 c->exec_pathname = NULL;
11668
3ea46bff 11669 install_breakpoint (0, &c->base, 1);
c906108c 11670}
c5aa993b 11671
9ac4176b 11672void
28010a5d
PA
11673init_ada_exception_breakpoint (struct breakpoint *b,
11674 struct gdbarch *gdbarch,
11675 struct symtab_and_line sal,
11676 char *addr_string,
c0a91b2b 11677 const struct breakpoint_ops *ops,
28010a5d 11678 int tempflag,
349774ef 11679 int enabled,
28010a5d 11680 int from_tty)
f7f9143b 11681{
f7f9143b
JB
11682 if (from_tty)
11683 {
5af949e3
UW
11684 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11685 if (!loc_gdbarch)
11686 loc_gdbarch = gdbarch;
11687
6c95b8df
PA
11688 describe_other_breakpoints (loc_gdbarch,
11689 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11690 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11691 version for exception catchpoints, because two catchpoints
11692 used for different exception names will use the same address.
11693 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11694 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11695 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11696 the user what type of catchpoint it is. The above is good
11697 enough for now, though. */
11698 }
11699
28010a5d 11700 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11701
349774ef 11702 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11703 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
11704 b->addr_string = addr_string;
11705 b->language = language_ada;
f7f9143b
JB
11706}
11707
c906108c 11708static void
fba45db2 11709catch_command (char *arg, int from_tty)
c906108c 11710{
44feb3ce 11711 error (_("Catch requires an event name."));
c906108c
SS
11712}
11713\f
11714
11715static void
fba45db2 11716tcatch_command (char *arg, int from_tty)
c906108c 11717{
44feb3ce 11718 error (_("Catch requires an event name."));
c906108c
SS
11719}
11720
8a2c437b
TT
11721/* A qsort comparison function that sorts breakpoints in order. */
11722
11723static int
11724compare_breakpoints (const void *a, const void *b)
11725{
11726 const breakpoint_p *ba = a;
11727 uintptr_t ua = (uintptr_t) *ba;
11728 const breakpoint_p *bb = b;
11729 uintptr_t ub = (uintptr_t) *bb;
11730
11731 if ((*ba)->number < (*bb)->number)
11732 return -1;
11733 else if ((*ba)->number > (*bb)->number)
11734 return 1;
11735
11736 /* Now sort by address, in case we see, e..g, two breakpoints with
11737 the number 0. */
11738 if (ua < ub)
11739 return -1;
94b0e70d 11740 return ua > ub ? 1 : 0;
8a2c437b
TT
11741}
11742
80f8a6eb 11743/* Delete breakpoints by address or line. */
c906108c
SS
11744
11745static void
fba45db2 11746clear_command (char *arg, int from_tty)
c906108c 11747{
8a2c437b 11748 struct breakpoint *b, *prev;
d6e956e5
VP
11749 VEC(breakpoint_p) *found = 0;
11750 int ix;
c906108c
SS
11751 int default_match;
11752 struct symtabs_and_lines sals;
11753 struct symtab_and_line sal;
c906108c 11754 int i;
8a2c437b 11755 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11756
11757 if (arg)
11758 {
39cf75f7
DE
11759 sals = decode_line_with_current_source (arg,
11760 (DECODE_LINE_FUNFIRSTLINE
11761 | DECODE_LINE_LIST_MODE));
cf4ded82 11762 make_cleanup (xfree, sals.sals);
c906108c
SS
11763 default_match = 0;
11764 }
11765 else
11766 {
c5aa993b 11767 sals.sals = (struct symtab_and_line *)
c906108c 11768 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 11769 make_cleanup (xfree, sals.sals);
4a64f543 11770 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11771
11772 /* Set sal's line, symtab, pc, and pspace to the values
11773 corresponding to the last call to print_frame_info. If the
11774 codepoint is not valid, this will set all the fields to 0. */
11775 get_last_displayed_sal (&sal);
c906108c 11776 if (sal.symtab == 0)
8a3fe4f8 11777 error (_("No source file specified."));
c906108c
SS
11778
11779 sals.sals[0] = sal;
11780 sals.nelts = 1;
11781
11782 default_match = 1;
11783 }
11784
4a64f543
MS
11785 /* We don't call resolve_sal_pc here. That's not as bad as it
11786 seems, because all existing breakpoints typically have both
11787 file/line and pc set. So, if clear is given file/line, we can
11788 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11789
11790 We only support clearing given the address explicitly
11791 present in breakpoint table. Say, we've set breakpoint
4a64f543 11792 at file:line. There were several PC values for that file:line,
ed0616c6 11793 due to optimization, all in one block.
4a64f543
MS
11794
11795 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11796 PC corresponding to the same file:line, the breakpoint won't
11797 be cleared. We probably can still clear the breakpoint, but
11798 since the other PC value is never presented to user, user
11799 can only find it by guessing, and it does not seem important
11800 to support that. */
11801
4a64f543
MS
11802 /* For each line spec given, delete bps which correspond to it. Do
11803 it in two passes, solely to preserve the current behavior that
11804 from_tty is forced true if we delete more than one
11805 breakpoint. */
c906108c 11806
80f8a6eb 11807 found = NULL;
8a2c437b 11808 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11809 for (i = 0; i < sals.nelts; i++)
11810 {
05cba821
JK
11811 const char *sal_fullname;
11812
c906108c 11813 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11814 If line given (pc == 0), clear all bpts on specified line.
11815 If defaulting, clear all bpts on default line
c906108c 11816 or at default pc.
c5aa993b
JM
11817
11818 defaulting sal.pc != 0 tests to do
11819
11820 0 1 pc
11821 1 1 pc _and_ line
11822 0 0 line
11823 1 0 <can't happen> */
c906108c
SS
11824
11825 sal = sals.sals[i];
05cba821
JK
11826 sal_fullname = (sal.symtab == NULL
11827 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11828
4a64f543 11829 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11830 ALL_BREAKPOINTS (b)
c5aa993b 11831 {
0d381245 11832 int match = 0;
4a64f543 11833 /* Are we going to delete b? */
cc60f2e3 11834 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11835 {
11836 struct bp_location *loc = b->loc;
11837 for (; loc; loc = loc->next)
11838 {
f8eba3c6
TT
11839 /* If the user specified file:line, don't allow a PC
11840 match. This matches historical gdb behavior. */
11841 int pc_match = (!sal.explicit_line
11842 && sal.pc
11843 && (loc->pspace == sal.pspace)
11844 && (loc->address == sal.pc)
11845 && (!section_is_overlay (loc->section)
11846 || loc->section == sal.section));
4aac40c8
TT
11847 int line_match = 0;
11848
11849 if ((default_match || sal.explicit_line)
2f202fde 11850 && loc->symtab != NULL
05cba821 11851 && sal_fullname != NULL
4aac40c8 11852 && sal.pspace == loc->pspace
05cba821
JK
11853 && loc->line_number == sal.line
11854 && filename_cmp (symtab_to_fullname (loc->symtab),
11855 sal_fullname) == 0)
11856 line_match = 1;
4aac40c8 11857
0d381245
VP
11858 if (pc_match || line_match)
11859 {
11860 match = 1;
11861 break;
11862 }
11863 }
11864 }
11865
11866 if (match)
d6e956e5 11867 VEC_safe_push(breakpoint_p, found, b);
c906108c 11868 }
80f8a6eb 11869 }
8a2c437b 11870
80f8a6eb 11871 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11872 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11873 {
11874 if (arg)
8a3fe4f8 11875 error (_("No breakpoint at %s."), arg);
80f8a6eb 11876 else
8a3fe4f8 11877 error (_("No breakpoint at this line."));
80f8a6eb 11878 }
c906108c 11879
8a2c437b
TT
11880 /* Remove duplicates from the vec. */
11881 qsort (VEC_address (breakpoint_p, found),
11882 VEC_length (breakpoint_p, found),
11883 sizeof (breakpoint_p),
11884 compare_breakpoints);
11885 prev = VEC_index (breakpoint_p, found, 0);
11886 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11887 {
11888 if (b == prev)
11889 {
11890 VEC_ordered_remove (breakpoint_p, found, ix);
11891 --ix;
11892 }
11893 }
11894
d6e956e5 11895 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11896 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11897 if (from_tty)
a3f17187 11898 {
d6e956e5 11899 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11900 printf_unfiltered (_("Deleted breakpoint "));
11901 else
11902 printf_unfiltered (_("Deleted breakpoints "));
11903 }
d6e956e5
VP
11904
11905 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11906 {
c5aa993b 11907 if (from_tty)
d6e956e5
VP
11908 printf_unfiltered ("%d ", b->number);
11909 delete_breakpoint (b);
c906108c 11910 }
80f8a6eb
MS
11911 if (from_tty)
11912 putchar_unfiltered ('\n');
8a2c437b
TT
11913
11914 do_cleanups (cleanups);
c906108c
SS
11915}
11916\f
11917/* Delete breakpoint in BS if they are `delete' breakpoints and
11918 all breakpoints that are marked for deletion, whether hit or not.
11919 This is called after any breakpoint is hit, or after errors. */
11920
11921void
fba45db2 11922breakpoint_auto_delete (bpstat bs)
c906108c 11923{
35df4500 11924 struct breakpoint *b, *b_tmp;
c906108c
SS
11925
11926 for (; bs; bs = bs->next)
f431efe5
PA
11927 if (bs->breakpoint_at
11928 && bs->breakpoint_at->disposition == disp_del
c906108c 11929 && bs->stop)
f431efe5 11930 delete_breakpoint (bs->breakpoint_at);
c906108c 11931
35df4500 11932 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11933 {
b5de0fa7 11934 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11935 delete_breakpoint (b);
11936 }
c906108c
SS
11937}
11938
4a64f543
MS
11939/* A comparison function for bp_location AP and BP being interfaced to
11940 qsort. Sort elements primarily by their ADDRESS (no matter what
11941 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 11942 secondarily by ordering first permanent elements and
4a64f543 11943 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11944 qsort being an unstable algorithm. */
876fa593
JK
11945
11946static int
494cfb0f 11947bp_location_compare (const void *ap, const void *bp)
876fa593 11948{
494cfb0f
JK
11949 struct bp_location *a = *(void **) ap;
11950 struct bp_location *b = *(void **) bp;
876fa593
JK
11951
11952 if (a->address != b->address)
11953 return (a->address > b->address) - (a->address < b->address);
11954
dea2aa5f
LM
11955 /* Sort locations at the same address by their pspace number, keeping
11956 locations of the same inferior (in a multi-inferior environment)
11957 grouped. */
11958
11959 if (a->pspace->num != b->pspace->num)
11960 return ((a->pspace->num > b->pspace->num)
11961 - (a->pspace->num < b->pspace->num));
11962
876fa593 11963 /* Sort permanent breakpoints first. */
1a853c52
PA
11964 if (a->permanent != b->permanent)
11965 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 11966
c56a97f9
JK
11967 /* Make the internal GDB representation stable across GDB runs
11968 where A and B memory inside GDB can differ. Breakpoint locations of
11969 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11970
11971 if (a->owner->number != b->owner->number)
c56a97f9
JK
11972 return ((a->owner->number > b->owner->number)
11973 - (a->owner->number < b->owner->number));
876fa593
JK
11974
11975 return (a > b) - (a < b);
11976}
11977
876fa593 11978/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
11979 bp_location_shadow_len_after_address_max according to the current
11980 content of the bp_location array. */
f7545552
TT
11981
11982static void
876fa593 11983bp_location_target_extensions_update (void)
f7545552 11984{
876fa593
JK
11985 struct bp_location *bl, **blp_tmp;
11986
11987 bp_location_placed_address_before_address_max = 0;
11988 bp_location_shadow_len_after_address_max = 0;
11989
11990 ALL_BP_LOCATIONS (bl, blp_tmp)
11991 {
11992 CORE_ADDR start, end, addr;
11993
11994 if (!bp_location_has_shadow (bl))
11995 continue;
11996
11997 start = bl->target_info.placed_address;
11998 end = start + bl->target_info.shadow_len;
11999
12000 gdb_assert (bl->address >= start);
12001 addr = bl->address - start;
12002 if (addr > bp_location_placed_address_before_address_max)
12003 bp_location_placed_address_before_address_max = addr;
12004
12005 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12006
12007 gdb_assert (bl->address < end);
12008 addr = end - bl->address;
12009 if (addr > bp_location_shadow_len_after_address_max)
12010 bp_location_shadow_len_after_address_max = addr;
12011 }
f7545552
TT
12012}
12013
1e4d1764
YQ
12014/* Download tracepoint locations if they haven't been. */
12015
12016static void
12017download_tracepoint_locations (void)
12018{
7ed2c994 12019 struct breakpoint *b;
1e4d1764
YQ
12020 struct cleanup *old_chain;
12021
12022 if (!target_can_download_tracepoint ())
12023 return;
12024
12025 old_chain = save_current_space_and_thread ();
12026
7ed2c994 12027 ALL_TRACEPOINTS (b)
1e4d1764 12028 {
7ed2c994 12029 struct bp_location *bl;
1e4d1764 12030 struct tracepoint *t;
f2a8bc8a 12031 int bp_location_downloaded = 0;
1e4d1764 12032
7ed2c994 12033 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12034 ? !may_insert_fast_tracepoints
12035 : !may_insert_tracepoints))
12036 continue;
12037
7ed2c994
YQ
12038 for (bl = b->loc; bl; bl = bl->next)
12039 {
12040 /* In tracepoint, locations are _never_ duplicated, so
12041 should_be_inserted is equivalent to
12042 unduplicated_should_be_inserted. */
12043 if (!should_be_inserted (bl) || bl->inserted)
12044 continue;
1e4d1764 12045
7ed2c994 12046 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12047
7ed2c994 12048 target_download_tracepoint (bl);
1e4d1764 12049
7ed2c994 12050 bl->inserted = 1;
f2a8bc8a 12051 bp_location_downloaded = 1;
7ed2c994
YQ
12052 }
12053 t = (struct tracepoint *) b;
12054 t->number_on_target = b->number;
f2a8bc8a
YQ
12055 if (bp_location_downloaded)
12056 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12057 }
12058
12059 do_cleanups (old_chain);
12060}
12061
934709f0
PW
12062/* Swap the insertion/duplication state between two locations. */
12063
12064static void
12065swap_insertion (struct bp_location *left, struct bp_location *right)
12066{
12067 const int left_inserted = left->inserted;
12068 const int left_duplicate = left->duplicate;
b775012e 12069 const int left_needs_update = left->needs_update;
934709f0
PW
12070 const struct bp_target_info left_target_info = left->target_info;
12071
1e4d1764
YQ
12072 /* Locations of tracepoints can never be duplicated. */
12073 if (is_tracepoint (left->owner))
12074 gdb_assert (!left->duplicate);
12075 if (is_tracepoint (right->owner))
12076 gdb_assert (!right->duplicate);
12077
934709f0
PW
12078 left->inserted = right->inserted;
12079 left->duplicate = right->duplicate;
b775012e 12080 left->needs_update = right->needs_update;
934709f0
PW
12081 left->target_info = right->target_info;
12082 right->inserted = left_inserted;
12083 right->duplicate = left_duplicate;
b775012e 12084 right->needs_update = left_needs_update;
934709f0
PW
12085 right->target_info = left_target_info;
12086}
12087
b775012e
LM
12088/* Force the re-insertion of the locations at ADDRESS. This is called
12089 once a new/deleted/modified duplicate location is found and we are evaluating
12090 conditions on the target's side. Such conditions need to be updated on
12091 the target. */
12092
12093static void
12094force_breakpoint_reinsertion (struct bp_location *bl)
12095{
12096 struct bp_location **locp = NULL, **loc2p;
12097 struct bp_location *loc;
12098 CORE_ADDR address = 0;
12099 int pspace_num;
12100
12101 address = bl->address;
12102 pspace_num = bl->pspace->num;
12103
12104 /* This is only meaningful if the target is
12105 evaluating conditions and if the user has
12106 opted for condition evaluation on the target's
12107 side. */
12108 if (gdb_evaluates_breakpoint_condition_p ()
12109 || !target_supports_evaluation_of_breakpoint_conditions ())
12110 return;
12111
12112 /* Flag all breakpoint locations with this address and
12113 the same program space as the location
12114 as "its condition has changed". We need to
12115 update the conditions on the target's side. */
12116 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12117 {
12118 loc = *loc2p;
12119
12120 if (!is_breakpoint (loc->owner)
12121 || pspace_num != loc->pspace->num)
12122 continue;
12123
12124 /* Flag the location appropriately. We use a different state to
12125 let everyone know that we already updated the set of locations
12126 with addr bl->address and program space bl->pspace. This is so
12127 we don't have to keep calling these functions just to mark locations
12128 that have already been marked. */
12129 loc->condition_changed = condition_updated;
12130
12131 /* Free the agent expression bytecode as well. We will compute
12132 it later on. */
12133 if (loc->cond_bytecode)
12134 {
12135 free_agent_expr (loc->cond_bytecode);
12136 loc->cond_bytecode = NULL;
12137 }
12138 }
12139}
44702360
PA
12140/* Called whether new breakpoints are created, or existing breakpoints
12141 deleted, to update the global location list and recompute which
12142 locations are duplicate of which.
b775012e 12143
04086b45
PA
12144 The INSERT_MODE flag determines whether locations may not, may, or
12145 shall be inserted now. See 'enum ugll_insert_mode' for more
12146 info. */
b60e7edf 12147
0d381245 12148static void
44702360 12149update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12150{
74960c60 12151 struct breakpoint *b;
876fa593 12152 struct bp_location **locp, *loc;
f7545552 12153 struct cleanup *cleanups;
b775012e
LM
12154 /* Last breakpoint location address that was marked for update. */
12155 CORE_ADDR last_addr = 0;
12156 /* Last breakpoint location program space that was marked for update. */
12157 int last_pspace_num = -1;
f7545552 12158
2d134ed3
PA
12159 /* Used in the duplicates detection below. When iterating over all
12160 bp_locations, points to the first bp_location of a given address.
12161 Breakpoints and watchpoints of different types are never
12162 duplicates of each other. Keep one pointer for each type of
12163 breakpoint/watchpoint, so we only need to loop over all locations
12164 once. */
12165 struct bp_location *bp_loc_first; /* breakpoint */
12166 struct bp_location *wp_loc_first; /* hardware watchpoint */
12167 struct bp_location *awp_loc_first; /* access watchpoint */
12168 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12169
4a64f543
MS
12170 /* Saved former bp_location array which we compare against the newly
12171 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12172 struct bp_location **old_location, **old_locp;
12173 unsigned old_location_count;
12174
12175 old_location = bp_location;
12176 old_location_count = bp_location_count;
12177 bp_location = NULL;
12178 bp_location_count = 0;
12179 cleanups = make_cleanup (xfree, old_location);
0d381245 12180
74960c60 12181 ALL_BREAKPOINTS (b)
876fa593
JK
12182 for (loc = b->loc; loc; loc = loc->next)
12183 bp_location_count++;
12184
12185 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12186 locp = bp_location;
12187 ALL_BREAKPOINTS (b)
12188 for (loc = b->loc; loc; loc = loc->next)
12189 *locp++ = loc;
12190 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12191 bp_location_compare);
876fa593
JK
12192
12193 bp_location_target_extensions_update ();
74960c60 12194
4a64f543
MS
12195 /* Identify bp_location instances that are no longer present in the
12196 new list, and therefore should be freed. Note that it's not
12197 necessary that those locations should be removed from inferior --
12198 if there's another location at the same address (previously
12199 marked as duplicate), we don't need to remove/insert the
12200 location.
876fa593 12201
4a64f543
MS
12202 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12203 and former bp_location array state respectively. */
876fa593
JK
12204
12205 locp = bp_location;
12206 for (old_locp = old_location; old_locp < old_location + old_location_count;
12207 old_locp++)
74960c60 12208 {
876fa593 12209 struct bp_location *old_loc = *old_locp;
c7d46a38 12210 struct bp_location **loc2p;
876fa593 12211
e5dd4106 12212 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12213 not, we have to free it. */
c7d46a38 12214 int found_object = 0;
20874c92
VP
12215 /* Tells if the location should remain inserted in the target. */
12216 int keep_in_target = 0;
12217 int removed = 0;
876fa593 12218
4a64f543
MS
12219 /* Skip LOCP entries which will definitely never be needed.
12220 Stop either at or being the one matching OLD_LOC. */
876fa593 12221 while (locp < bp_location + bp_location_count
c7d46a38 12222 && (*locp)->address < old_loc->address)
876fa593 12223 locp++;
c7d46a38
PA
12224
12225 for (loc2p = locp;
12226 (loc2p < bp_location + bp_location_count
12227 && (*loc2p)->address == old_loc->address);
12228 loc2p++)
12229 {
b775012e
LM
12230 /* Check if this is a new/duplicated location or a duplicated
12231 location that had its condition modified. If so, we want to send
12232 its condition to the target if evaluation of conditions is taking
12233 place there. */
12234 if ((*loc2p)->condition_changed == condition_modified
12235 && (last_addr != old_loc->address
12236 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12237 {
b775012e
LM
12238 force_breakpoint_reinsertion (*loc2p);
12239 last_pspace_num = old_loc->pspace->num;
c7d46a38 12240 }
b775012e
LM
12241
12242 if (*loc2p == old_loc)
12243 found_object = 1;
c7d46a38 12244 }
74960c60 12245
b775012e
LM
12246 /* We have already handled this address, update it so that we don't
12247 have to go through updates again. */
12248 last_addr = old_loc->address;
12249
12250 /* Target-side condition evaluation: Handle deleted locations. */
12251 if (!found_object)
12252 force_breakpoint_reinsertion (old_loc);
12253
4a64f543
MS
12254 /* If this location is no longer present, and inserted, look if
12255 there's maybe a new location at the same address. If so,
12256 mark that one inserted, and don't remove this one. This is
12257 needed so that we don't have a time window where a breakpoint
12258 at certain location is not inserted. */
74960c60 12259
876fa593 12260 if (old_loc->inserted)
0d381245 12261 {
4a64f543
MS
12262 /* If the location is inserted now, we might have to remove
12263 it. */
74960c60 12264
876fa593 12265 if (found_object && should_be_inserted (old_loc))
74960c60 12266 {
4a64f543
MS
12267 /* The location is still present in the location list,
12268 and still should be inserted. Don't do anything. */
20874c92 12269 keep_in_target = 1;
74960c60
VP
12270 }
12271 else
12272 {
b775012e
LM
12273 /* This location still exists, but it won't be kept in the
12274 target since it may have been disabled. We proceed to
12275 remove its target-side condition. */
12276
4a64f543
MS
12277 /* The location is either no longer present, or got
12278 disabled. See if there's another location at the
12279 same address, in which case we don't need to remove
12280 this one from the target. */
876fa593 12281
2bdf28a0 12282 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12283 if (breakpoint_address_is_meaningful (old_loc->owner))
12284 {
876fa593 12285 for (loc2p = locp;
c7d46a38
PA
12286 (loc2p < bp_location + bp_location_count
12287 && (*loc2p)->address == old_loc->address);
876fa593
JK
12288 loc2p++)
12289 {
12290 struct bp_location *loc2 = *loc2p;
12291
2d134ed3 12292 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12293 {
85d721b8
PA
12294 /* Read watchpoint locations are switched to
12295 access watchpoints, if the former are not
12296 supported, but the latter are. */
12297 if (is_hardware_watchpoint (old_loc->owner))
12298 {
12299 gdb_assert (is_hardware_watchpoint (loc2->owner));
12300 loc2->watchpoint_type = old_loc->watchpoint_type;
12301 }
12302
934709f0
PW
12303 /* loc2 is a duplicated location. We need to check
12304 if it should be inserted in case it will be
12305 unduplicated. */
12306 if (loc2 != old_loc
12307 && unduplicated_should_be_inserted (loc2))
c7d46a38 12308 {
934709f0 12309 swap_insertion (old_loc, loc2);
c7d46a38
PA
12310 keep_in_target = 1;
12311 break;
12312 }
876fa593
JK
12313 }
12314 }
12315 }
74960c60
VP
12316 }
12317
20874c92
VP
12318 if (!keep_in_target)
12319 {
876fa593 12320 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12321 {
4a64f543
MS
12322 /* This is just about all we can do. We could keep
12323 this location on the global list, and try to
12324 remove it next time, but there's no particular
12325 reason why we will succeed next time.
20874c92 12326
4a64f543
MS
12327 Note that at this point, old_loc->owner is still
12328 valid, as delete_breakpoint frees the breakpoint
12329 only after calling us. */
3e43a32a
MS
12330 printf_filtered (_("warning: Error removing "
12331 "breakpoint %d\n"),
876fa593 12332 old_loc->owner->number);
20874c92
VP
12333 }
12334 removed = 1;
12335 }
0d381245 12336 }
74960c60
VP
12337
12338 if (!found_object)
1c5cfe86 12339 {
db82e815 12340 if (removed && non_stop
1cf4d951 12341 && need_moribund_for_location_type (old_loc))
20874c92 12342 {
db82e815
PA
12343 /* This location was removed from the target. In
12344 non-stop mode, a race condition is possible where
12345 we've removed a breakpoint, but stop events for that
12346 breakpoint are already queued and will arrive later.
12347 We apply an heuristic to be able to distinguish such
12348 SIGTRAPs from other random SIGTRAPs: we keep this
12349 breakpoint location for a bit, and will retire it
12350 after we see some number of events. The theory here
12351 is that reporting of events should, "on the average",
12352 be fair, so after a while we'll see events from all
12353 threads that have anything of interest, and no longer
12354 need to keep this breakpoint location around. We
12355 don't hold locations forever so to reduce chances of
12356 mistaking a non-breakpoint SIGTRAP for a breakpoint
12357 SIGTRAP.
12358
12359 The heuristic failing can be disastrous on
12360 decr_pc_after_break targets.
12361
12362 On decr_pc_after_break targets, like e.g., x86-linux,
12363 if we fail to recognize a late breakpoint SIGTRAP,
12364 because events_till_retirement has reached 0 too
12365 soon, we'll fail to do the PC adjustment, and report
12366 a random SIGTRAP to the user. When the user resumes
12367 the inferior, it will most likely immediately crash
2dec564e 12368 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12369 corrupted, because of being resumed e.g., in the
12370 middle of a multi-byte instruction, or skipped a
12371 one-byte instruction. This was actually seen happen
12372 on native x86-linux, and should be less rare on
12373 targets that do not support new thread events, like
12374 remote, due to the heuristic depending on
12375 thread_count.
12376
12377 Mistaking a random SIGTRAP for a breakpoint trap
12378 causes similar symptoms (PC adjustment applied when
12379 it shouldn't), but then again, playing with SIGTRAPs
12380 behind the debugger's back is asking for trouble.
12381
12382 Since hardware watchpoint traps are always
12383 distinguishable from other traps, so we don't need to
12384 apply keep hardware watchpoint moribund locations
12385 around. We simply always ignore hardware watchpoint
12386 traps we can no longer explain. */
12387
876fa593
JK
12388 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12389 old_loc->owner = NULL;
20874c92 12390
876fa593 12391 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12392 }
12393 else
f431efe5
PA
12394 {
12395 old_loc->owner = NULL;
12396 decref_bp_location (&old_loc);
12397 }
20874c92 12398 }
74960c60 12399 }
1c5cfe86 12400
348d480f
PA
12401 /* Rescan breakpoints at the same address and section, marking the
12402 first one as "first" and any others as "duplicates". This is so
12403 that the bpt instruction is only inserted once. If we have a
12404 permanent breakpoint at the same place as BPT, make that one the
12405 official one, and the rest as duplicates. Permanent breakpoints
12406 are sorted first for the same address.
12407
12408 Do the same for hardware watchpoints, but also considering the
12409 watchpoint's type (regular/access/read) and length. */
12410
12411 bp_loc_first = NULL;
12412 wp_loc_first = NULL;
12413 awp_loc_first = NULL;
12414 rwp_loc_first = NULL;
12415 ALL_BP_LOCATIONS (loc, locp)
12416 {
12417 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12418 non-NULL. */
348d480f 12419 struct bp_location **loc_first_p;
d3fbdd86 12420 b = loc->owner;
348d480f 12421
6f380991 12422 if (!unduplicated_should_be_inserted (loc)
348d480f 12423 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12424 /* Don't detect duplicate for tracepoint locations because they are
12425 never duplicated. See the comments in field `duplicate' of
12426 `struct bp_location'. */
348d480f 12427 || is_tracepoint (b))
b775012e
LM
12428 {
12429 /* Clear the condition modification flag. */
12430 loc->condition_changed = condition_unchanged;
12431 continue;
12432 }
348d480f
PA
12433
12434 /* Permanent breakpoint should always be inserted. */
1a853c52 12435 if (loc->permanent && ! loc->inserted)
348d480f
PA
12436 internal_error (__FILE__, __LINE__,
12437 _("allegedly permanent breakpoint is not "
12438 "actually inserted"));
12439
12440 if (b->type == bp_hardware_watchpoint)
12441 loc_first_p = &wp_loc_first;
12442 else if (b->type == bp_read_watchpoint)
12443 loc_first_p = &rwp_loc_first;
12444 else if (b->type == bp_access_watchpoint)
12445 loc_first_p = &awp_loc_first;
12446 else
12447 loc_first_p = &bp_loc_first;
12448
12449 if (*loc_first_p == NULL
12450 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12451 || !breakpoint_locations_match (loc, *loc_first_p))
12452 {
12453 *loc_first_p = loc;
12454 loc->duplicate = 0;
b775012e
LM
12455
12456 if (is_breakpoint (loc->owner) && loc->condition_changed)
12457 {
12458 loc->needs_update = 1;
12459 /* Clear the condition modification flag. */
12460 loc->condition_changed = condition_unchanged;
12461 }
348d480f
PA
12462 continue;
12463 }
12464
934709f0
PW
12465
12466 /* This and the above ensure the invariant that the first location
12467 is not duplicated, and is the inserted one.
12468 All following are marked as duplicated, and are not inserted. */
12469 if (loc->inserted)
12470 swap_insertion (loc, *loc_first_p);
348d480f
PA
12471 loc->duplicate = 1;
12472
b775012e
LM
12473 /* Clear the condition modification flag. */
12474 loc->condition_changed = condition_unchanged;
12475
1a853c52
PA
12476 if (loc->inserted && !loc->permanent
12477 && (*loc_first_p)->permanent)
348d480f
PA
12478 internal_error (__FILE__, __LINE__,
12479 _("another breakpoint was inserted on top of "
12480 "a permanent breakpoint"));
12481 }
12482
a25a5a45 12483 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12484 {
04086b45 12485 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12486 insert_breakpoint_locations ();
12487 else
12488 {
44702360
PA
12489 /* Even though the caller told us to not insert new
12490 locations, we may still need to update conditions on the
12491 target's side of breakpoints that were already inserted
12492 if the target is evaluating breakpoint conditions. We
b775012e
LM
12493 only update conditions for locations that are marked
12494 "needs_update". */
12495 update_inserted_breakpoint_locations ();
12496 }
12497 }
348d480f 12498
04086b45 12499 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12500 download_tracepoint_locations ();
12501
348d480f
PA
12502 do_cleanups (cleanups);
12503}
12504
12505void
12506breakpoint_retire_moribund (void)
12507{
12508 struct bp_location *loc;
12509 int ix;
12510
12511 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12512 if (--(loc->events_till_retirement) == 0)
12513 {
12514 decref_bp_location (&loc);
12515 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12516 --ix;
12517 }
12518}
12519
12520static void
44702360 12521update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12522{
348d480f 12523
492d29ea
PA
12524 TRY
12525 {
12526 update_global_location_list (insert_mode);
12527 }
12528 CATCH (e, RETURN_MASK_ERROR)
12529 {
12530 }
12531 END_CATCH
348d480f
PA
12532}
12533
12534/* Clear BKP from a BPS. */
12535
12536static void
12537bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12538{
12539 bpstat bs;
12540
12541 for (bs = bps; bs; bs = bs->next)
12542 if (bs->breakpoint_at == bpt)
12543 {
12544 bs->breakpoint_at = NULL;
12545 bs->old_val = NULL;
12546 /* bs->commands will be freed later. */
12547 }
12548}
12549
12550/* Callback for iterate_over_threads. */
12551static int
12552bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12553{
12554 struct breakpoint *bpt = data;
12555
12556 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12557 return 0;
12558}
12559
12560/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12561 callbacks. */
12562
12563static void
12564say_where (struct breakpoint *b)
12565{
12566 struct value_print_options opts;
12567
12568 get_user_print_options (&opts);
12569
12570 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12571 single string. */
12572 if (b->loc == NULL)
12573 {
12574 printf_filtered (_(" (%s) pending."), b->addr_string);
12575 }
12576 else
12577 {
2f202fde 12578 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12579 {
12580 printf_filtered (" at ");
12581 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12582 gdb_stdout);
12583 }
2f202fde 12584 if (b->loc->symtab != NULL)
f8eba3c6
TT
12585 {
12586 /* If there is a single location, we can print the location
12587 more nicely. */
12588 if (b->loc->next == NULL)
12589 printf_filtered (": file %s, line %d.",
05cba821
JK
12590 symtab_to_filename_for_display (b->loc->symtab),
12591 b->loc->line_number);
f8eba3c6
TT
12592 else
12593 /* This is not ideal, but each location may have a
12594 different file name, and this at least reflects the
12595 real situation somewhat. */
12596 printf_filtered (": %s.", b->addr_string);
12597 }
348d480f
PA
12598
12599 if (b->loc->next)
12600 {
12601 struct bp_location *loc = b->loc;
12602 int n = 0;
12603 for (; loc; loc = loc->next)
12604 ++n;
12605 printf_filtered (" (%d locations)", n);
12606 }
12607 }
12608}
12609
348d480f
PA
12610/* Default bp_location_ops methods. */
12611
12612static void
12613bp_location_dtor (struct bp_location *self)
12614{
12615 xfree (self->cond);
b775012e
LM
12616 if (self->cond_bytecode)
12617 free_agent_expr (self->cond_bytecode);
348d480f 12618 xfree (self->function_name);
8b4f3082
PA
12619
12620 VEC_free (agent_expr_p, self->target_info.conditions);
12621 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12622}
12623
12624static const struct bp_location_ops bp_location_ops =
12625{
12626 bp_location_dtor
12627};
12628
2060206e
PA
12629/* Default breakpoint_ops methods all breakpoint_ops ultimately
12630 inherit from. */
348d480f 12631
2060206e
PA
12632static void
12633base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12634{
12635 decref_counted_command_line (&self->commands);
12636 xfree (self->cond_string);
fb81d016 12637 xfree (self->extra_string);
348d480f 12638 xfree (self->addr_string);
f8eba3c6 12639 xfree (self->filter);
348d480f 12640 xfree (self->addr_string_range_end);
348d480f
PA
12641}
12642
2060206e
PA
12643static struct bp_location *
12644base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12645{
12646 struct bp_location *loc;
12647
12648 loc = XNEW (struct bp_location);
12649 init_bp_location (loc, &bp_location_ops, self);
12650 return loc;
12651}
12652
2060206e
PA
12653static void
12654base_breakpoint_re_set (struct breakpoint *b)
12655{
12656 /* Nothing to re-set. */
12657}
12658
12659#define internal_error_pure_virtual_called() \
12660 gdb_assert_not_reached ("pure virtual function called")
12661
12662static int
12663base_breakpoint_insert_location (struct bp_location *bl)
12664{
12665 internal_error_pure_virtual_called ();
12666}
12667
12668static int
12669base_breakpoint_remove_location (struct bp_location *bl)
12670{
12671 internal_error_pure_virtual_called ();
12672}
12673
12674static int
12675base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12676 struct address_space *aspace,
09ac7c10
TT
12677 CORE_ADDR bp_addr,
12678 const struct target_waitstatus *ws)
2060206e
PA
12679{
12680 internal_error_pure_virtual_called ();
12681}
12682
12683static void
12684base_breakpoint_check_status (bpstat bs)
12685{
12686 /* Always stop. */
12687}
12688
12689/* A "works_in_software_mode" breakpoint_ops method that just internal
12690 errors. */
12691
12692static int
12693base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12694{
12695 internal_error_pure_virtual_called ();
12696}
12697
12698/* A "resources_needed" breakpoint_ops method that just internal
12699 errors. */
12700
12701static int
12702base_breakpoint_resources_needed (const struct bp_location *bl)
12703{
12704 internal_error_pure_virtual_called ();
12705}
12706
12707static enum print_stop_action
12708base_breakpoint_print_it (bpstat bs)
12709{
12710 internal_error_pure_virtual_called ();
12711}
12712
12713static void
12714base_breakpoint_print_one_detail (const struct breakpoint *self,
12715 struct ui_out *uiout)
12716{
12717 /* nothing */
12718}
12719
12720static void
12721base_breakpoint_print_mention (struct breakpoint *b)
12722{
12723 internal_error_pure_virtual_called ();
12724}
12725
12726static void
12727base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12728{
12729 internal_error_pure_virtual_called ();
12730}
12731
983af33b
SDJ
12732static void
12733base_breakpoint_create_sals_from_address (char **arg,
12734 struct linespec_result *canonical,
12735 enum bptype type_wanted,
12736 char *addr_start,
12737 char **copy_arg)
12738{
12739 internal_error_pure_virtual_called ();
12740}
12741
12742static void
12743base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12744 struct linespec_result *c,
983af33b 12745 char *cond_string,
e7e0cddf 12746 char *extra_string,
983af33b
SDJ
12747 enum bptype type_wanted,
12748 enum bpdisp disposition,
12749 int thread,
12750 int task, int ignore_count,
12751 const struct breakpoint_ops *o,
12752 int from_tty, int enabled,
44f238bb 12753 int internal, unsigned flags)
983af33b
SDJ
12754{
12755 internal_error_pure_virtual_called ();
12756}
12757
12758static void
12759base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12760 struct symtabs_and_lines *sals)
12761{
12762 internal_error_pure_virtual_called ();
12763}
12764
ab04a2af
TT
12765/* The default 'explains_signal' method. */
12766
47591c29 12767static int
427cd150 12768base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12769{
47591c29 12770 return 1;
ab04a2af
TT
12771}
12772
9d6e6e84
HZ
12773/* The default "after_condition_true" method. */
12774
12775static void
12776base_breakpoint_after_condition_true (struct bpstats *bs)
12777{
12778 /* Nothing to do. */
12779}
12780
ab04a2af 12781struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
12782{
12783 base_breakpoint_dtor,
12784 base_breakpoint_allocate_location,
12785 base_breakpoint_re_set,
12786 base_breakpoint_insert_location,
12787 base_breakpoint_remove_location,
12788 base_breakpoint_breakpoint_hit,
12789 base_breakpoint_check_status,
12790 base_breakpoint_resources_needed,
12791 base_breakpoint_works_in_software_mode,
12792 base_breakpoint_print_it,
12793 NULL,
12794 base_breakpoint_print_one_detail,
12795 base_breakpoint_print_mention,
983af33b
SDJ
12796 base_breakpoint_print_recreate,
12797 base_breakpoint_create_sals_from_address,
12798 base_breakpoint_create_breakpoints_sal,
12799 base_breakpoint_decode_linespec,
9d6e6e84
HZ
12800 base_breakpoint_explains_signal,
12801 base_breakpoint_after_condition_true,
2060206e
PA
12802};
12803
12804/* Default breakpoint_ops methods. */
12805
12806static void
348d480f
PA
12807bkpt_re_set (struct breakpoint *b)
12808{
06edf0c0
PA
12809 /* FIXME: is this still reachable? */
12810 if (b->addr_string == NULL)
12811 {
12812 /* Anything without a string can't be re-set. */
348d480f 12813 delete_breakpoint (b);
06edf0c0 12814 return;
348d480f 12815 }
06edf0c0
PA
12816
12817 breakpoint_re_set_default (b);
348d480f
PA
12818}
12819
2060206e 12820static int
348d480f
PA
12821bkpt_insert_location (struct bp_location *bl)
12822{
12823 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12824 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12825 else
7c16b83e 12826 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12827}
12828
2060206e 12829static int
348d480f
PA
12830bkpt_remove_location (struct bp_location *bl)
12831{
12832 if (bl->loc_type == bp_loc_hardware_breakpoint)
12833 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12834 else
7c16b83e 12835 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12836}
12837
2060206e 12838static int
348d480f 12839bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12840 struct address_space *aspace, CORE_ADDR bp_addr,
12841 const struct target_waitstatus *ws)
348d480f 12842{
09ac7c10 12843 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12844 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12845 return 0;
12846
348d480f
PA
12847 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12848 aspace, bp_addr))
12849 return 0;
12850
12851 if (overlay_debugging /* unmapped overlay section */
12852 && section_is_overlay (bl->section)
12853 && !section_is_mapped (bl->section))
12854 return 0;
12855
12856 return 1;
12857}
12858
cd1608cc
PA
12859static int
12860dprintf_breakpoint_hit (const struct bp_location *bl,
12861 struct address_space *aspace, CORE_ADDR bp_addr,
12862 const struct target_waitstatus *ws)
12863{
12864 if (dprintf_style == dprintf_style_agent
12865 && target_can_run_breakpoint_commands ())
12866 {
12867 /* An agent-style dprintf never causes a stop. If we see a trap
12868 for this address it must be for a breakpoint that happens to
12869 be set at the same address. */
12870 return 0;
12871 }
12872
12873 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12874}
12875
2060206e 12876static int
348d480f
PA
12877bkpt_resources_needed (const struct bp_location *bl)
12878{
12879 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12880
12881 return 1;
12882}
12883
2060206e 12884static enum print_stop_action
348d480f
PA
12885bkpt_print_it (bpstat bs)
12886{
348d480f
PA
12887 struct breakpoint *b;
12888 const struct bp_location *bl;
001c8c33 12889 int bp_temp;
79a45e25 12890 struct ui_out *uiout = current_uiout;
348d480f
PA
12891
12892 gdb_assert (bs->bp_location_at != NULL);
12893
12894 bl = bs->bp_location_at;
12895 b = bs->breakpoint_at;
12896
001c8c33
PA
12897 bp_temp = b->disposition == disp_del;
12898 if (bl->address != bl->requested_address)
12899 breakpoint_adjustment_warning (bl->requested_address,
12900 bl->address,
12901 b->number, 1);
12902 annotate_breakpoint (b->number);
12903 if (bp_temp)
12904 ui_out_text (uiout, "\nTemporary breakpoint ");
12905 else
12906 ui_out_text (uiout, "\nBreakpoint ");
12907 if (ui_out_is_mi_like_p (uiout))
348d480f 12908 {
001c8c33
PA
12909 ui_out_field_string (uiout, "reason",
12910 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12911 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 12912 }
001c8c33
PA
12913 ui_out_field_int (uiout, "bkptno", b->number);
12914 ui_out_text (uiout, ", ");
06edf0c0 12915
001c8c33 12916 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12917}
12918
2060206e 12919static void
06edf0c0
PA
12920bkpt_print_mention (struct breakpoint *b)
12921{
79a45e25 12922 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
12923 return;
12924
12925 switch (b->type)
12926 {
12927 case bp_breakpoint:
12928 case bp_gnu_ifunc_resolver:
12929 if (b->disposition == disp_del)
12930 printf_filtered (_("Temporary breakpoint"));
12931 else
12932 printf_filtered (_("Breakpoint"));
12933 printf_filtered (_(" %d"), b->number);
12934 if (b->type == bp_gnu_ifunc_resolver)
12935 printf_filtered (_(" at gnu-indirect-function resolver"));
12936 break;
12937 case bp_hardware_breakpoint:
12938 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12939 break;
e7e0cddf
SS
12940 case bp_dprintf:
12941 printf_filtered (_("Dprintf %d"), b->number);
12942 break;
06edf0c0
PA
12943 }
12944
12945 say_where (b);
12946}
12947
2060206e 12948static void
06edf0c0
PA
12949bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12950{
12951 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12952 fprintf_unfiltered (fp, "tbreak");
12953 else if (tp->type == bp_breakpoint)
12954 fprintf_unfiltered (fp, "break");
12955 else if (tp->type == bp_hardware_breakpoint
12956 && tp->disposition == disp_del)
12957 fprintf_unfiltered (fp, "thbreak");
12958 else if (tp->type == bp_hardware_breakpoint)
12959 fprintf_unfiltered (fp, "hbreak");
12960 else
12961 internal_error (__FILE__, __LINE__,
12962 _("unhandled breakpoint type %d"), (int) tp->type);
12963
2060206e 12964 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 12965 print_recreate_thread (tp, fp);
06edf0c0
PA
12966}
12967
983af33b
SDJ
12968static void
12969bkpt_create_sals_from_address (char **arg,
12970 struct linespec_result *canonical,
12971 enum bptype type_wanted,
12972 char *addr_start, char **copy_arg)
12973{
12974 create_sals_from_address_default (arg, canonical, type_wanted,
12975 addr_start, copy_arg);
12976}
12977
12978static void
12979bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12980 struct linespec_result *canonical,
983af33b 12981 char *cond_string,
e7e0cddf 12982 char *extra_string,
983af33b
SDJ
12983 enum bptype type_wanted,
12984 enum bpdisp disposition,
12985 int thread,
12986 int task, int ignore_count,
12987 const struct breakpoint_ops *ops,
12988 int from_tty, int enabled,
44f238bb 12989 int internal, unsigned flags)
983af33b 12990{
023fa29b 12991 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
12992 cond_string, extra_string,
12993 type_wanted,
983af33b
SDJ
12994 disposition, thread, task,
12995 ignore_count, ops, from_tty,
44f238bb 12996 enabled, internal, flags);
983af33b
SDJ
12997}
12998
12999static void
13000bkpt_decode_linespec (struct breakpoint *b, char **s,
13001 struct symtabs_and_lines *sals)
13002{
13003 decode_linespec_default (b, s, sals);
13004}
13005
06edf0c0
PA
13006/* Virtual table for internal breakpoints. */
13007
13008static void
13009internal_bkpt_re_set (struct breakpoint *b)
13010{
13011 switch (b->type)
13012 {
13013 /* Delete overlay event and longjmp master breakpoints; they
13014 will be reset later by breakpoint_re_set. */
13015 case bp_overlay_event:
13016 case bp_longjmp_master:
13017 case bp_std_terminate_master:
13018 case bp_exception_master:
13019 delete_breakpoint (b);
13020 break;
13021
13022 /* This breakpoint is special, it's set up when the inferior
13023 starts and we really don't want to touch it. */
13024 case bp_shlib_event:
13025
13026 /* Like bp_shlib_event, this breakpoint type is special. Once
13027 it is set up, we do not want to touch it. */
13028 case bp_thread_event:
13029 break;
13030 }
13031}
13032
13033static void
13034internal_bkpt_check_status (bpstat bs)
13035{
a9b3a50f
PA
13036 if (bs->breakpoint_at->type == bp_shlib_event)
13037 {
13038 /* If requested, stop when the dynamic linker notifies GDB of
13039 events. This allows the user to get control and place
13040 breakpoints in initializer routines for dynamically loaded
13041 objects (among other things). */
13042 bs->stop = stop_on_solib_events;
13043 bs->print = stop_on_solib_events;
13044 }
13045 else
13046 bs->stop = 0;
06edf0c0
PA
13047}
13048
13049static enum print_stop_action
13050internal_bkpt_print_it (bpstat bs)
13051{
06edf0c0 13052 struct breakpoint *b;
06edf0c0 13053
06edf0c0
PA
13054 b = bs->breakpoint_at;
13055
06edf0c0
PA
13056 switch (b->type)
13057 {
348d480f
PA
13058 case bp_shlib_event:
13059 /* Did we stop because the user set the stop_on_solib_events
13060 variable? (If so, we report this as a generic, "Stopped due
13061 to shlib event" message.) */
edcc5120 13062 print_solib_event (0);
348d480f
PA
13063 break;
13064
13065 case bp_thread_event:
13066 /* Not sure how we will get here.
13067 GDB should not stop for these breakpoints. */
13068 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13069 break;
13070
13071 case bp_overlay_event:
13072 /* By analogy with the thread event, GDB should not stop for these. */
13073 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13074 break;
13075
13076 case bp_longjmp_master:
13077 /* These should never be enabled. */
13078 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13079 break;
13080
13081 case bp_std_terminate_master:
13082 /* These should never be enabled. */
13083 printf_filtered (_("std::terminate Master Breakpoint: "
13084 "gdb should not stop!\n"));
348d480f
PA
13085 break;
13086
13087 case bp_exception_master:
13088 /* These should never be enabled. */
13089 printf_filtered (_("Exception Master Breakpoint: "
13090 "gdb should not stop!\n"));
06edf0c0
PA
13091 break;
13092 }
13093
001c8c33 13094 return PRINT_NOTHING;
06edf0c0
PA
13095}
13096
13097static void
13098internal_bkpt_print_mention (struct breakpoint *b)
13099{
13100 /* Nothing to mention. These breakpoints are internal. */
13101}
13102
06edf0c0
PA
13103/* Virtual table for momentary breakpoints */
13104
13105static void
13106momentary_bkpt_re_set (struct breakpoint *b)
13107{
13108 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13109 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13110 Otherwise these should have been blown away via the cleanup chain
13111 or by breakpoint_init_inferior when we rerun the executable. */
13112}
13113
13114static void
13115momentary_bkpt_check_status (bpstat bs)
13116{
13117 /* Nothing. The point of these breakpoints is causing a stop. */
13118}
13119
13120static enum print_stop_action
13121momentary_bkpt_print_it (bpstat bs)
13122{
79a45e25
PA
13123 struct ui_out *uiout = current_uiout;
13124
001c8c33 13125 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13126 {
001c8c33 13127 struct breakpoint *b = bs->breakpoint_at;
348d480f 13128
001c8c33
PA
13129 switch (b->type)
13130 {
13131 case bp_finish:
13132 ui_out_field_string
13133 (uiout, "reason",
13134 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13135 break;
348d480f 13136
001c8c33
PA
13137 case bp_until:
13138 ui_out_field_string
13139 (uiout, "reason",
13140 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13141 break;
13142 }
348d480f
PA
13143 }
13144
001c8c33 13145 return PRINT_UNKNOWN;
348d480f
PA
13146}
13147
06edf0c0
PA
13148static void
13149momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13150{
06edf0c0 13151 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13152}
13153
e2e4d78b
JK
13154/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13155
13156 It gets cleared already on the removal of the first one of such placed
13157 breakpoints. This is OK as they get all removed altogether. */
13158
13159static void
13160longjmp_bkpt_dtor (struct breakpoint *self)
13161{
13162 struct thread_info *tp = find_thread_id (self->thread);
13163
13164 if (tp)
13165 tp->initiating_frame = null_frame_id;
13166
13167 momentary_breakpoint_ops.dtor (self);
13168}
13169
55aa24fb
SDJ
13170/* Specific methods for probe breakpoints. */
13171
13172static int
13173bkpt_probe_insert_location (struct bp_location *bl)
13174{
13175 int v = bkpt_insert_location (bl);
13176
13177 if (v == 0)
13178 {
13179 /* The insertion was successful, now let's set the probe's semaphore
13180 if needed. */
0ea5cda8
SDJ
13181 if (bl->probe.probe->pops->set_semaphore != NULL)
13182 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13183 bl->probe.objfile,
13184 bl->gdbarch);
55aa24fb
SDJ
13185 }
13186
13187 return v;
13188}
13189
13190static int
13191bkpt_probe_remove_location (struct bp_location *bl)
13192{
13193 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13194 if (bl->probe.probe->pops->clear_semaphore != NULL)
13195 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13196 bl->probe.objfile,
13197 bl->gdbarch);
55aa24fb
SDJ
13198
13199 return bkpt_remove_location (bl);
13200}
13201
13202static void
13203bkpt_probe_create_sals_from_address (char **arg,
13204 struct linespec_result *canonical,
13205 enum bptype type_wanted,
13206 char *addr_start, char **copy_arg)
13207{
13208 struct linespec_sals lsal;
13209
13210 lsal.sals = parse_probes (arg, canonical);
13211
13212 *copy_arg = xstrdup (canonical->addr_string);
13213 lsal.canonical = xstrdup (*copy_arg);
13214
13215 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13216}
13217
13218static void
13219bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13220 struct symtabs_and_lines *sals)
13221{
13222 *sals = parse_probes (s, NULL);
13223 if (!sals->sals)
13224 error (_("probe not found"));
13225}
13226
348d480f 13227/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13228
348d480f
PA
13229static void
13230tracepoint_re_set (struct breakpoint *b)
13231{
13232 breakpoint_re_set_default (b);
13233}
876fa593 13234
348d480f
PA
13235static int
13236tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13237 struct address_space *aspace, CORE_ADDR bp_addr,
13238 const struct target_waitstatus *ws)
348d480f
PA
13239{
13240 /* By definition, the inferior does not report stops at
13241 tracepoints. */
13242 return 0;
74960c60
VP
13243}
13244
13245static void
348d480f
PA
13246tracepoint_print_one_detail (const struct breakpoint *self,
13247 struct ui_out *uiout)
74960c60 13248{
d9b3f62e
PA
13249 struct tracepoint *tp = (struct tracepoint *) self;
13250 if (tp->static_trace_marker_id)
348d480f
PA
13251 {
13252 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13253
348d480f
PA
13254 ui_out_text (uiout, "\tmarker id is ");
13255 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13256 tp->static_trace_marker_id);
348d480f
PA
13257 ui_out_text (uiout, "\n");
13258 }
0d381245
VP
13259}
13260
a474d7c2 13261static void
348d480f 13262tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13263{
79a45e25 13264 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13265 return;
cc59ec59 13266
348d480f
PA
13267 switch (b->type)
13268 {
13269 case bp_tracepoint:
13270 printf_filtered (_("Tracepoint"));
13271 printf_filtered (_(" %d"), b->number);
13272 break;
13273 case bp_fast_tracepoint:
13274 printf_filtered (_("Fast tracepoint"));
13275 printf_filtered (_(" %d"), b->number);
13276 break;
13277 case bp_static_tracepoint:
13278 printf_filtered (_("Static tracepoint"));
13279 printf_filtered (_(" %d"), b->number);
13280 break;
13281 default:
13282 internal_error (__FILE__, __LINE__,
13283 _("unhandled tracepoint type %d"), (int) b->type);
13284 }
13285
13286 say_where (b);
a474d7c2
PA
13287}
13288
348d480f 13289static void
d9b3f62e 13290tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13291{
d9b3f62e
PA
13292 struct tracepoint *tp = (struct tracepoint *) self;
13293
13294 if (self->type == bp_fast_tracepoint)
348d480f 13295 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13296 if (self->type == bp_static_tracepoint)
348d480f 13297 fprintf_unfiltered (fp, "strace");
d9b3f62e 13298 else if (self->type == bp_tracepoint)
348d480f
PA
13299 fprintf_unfiltered (fp, "trace");
13300 else
13301 internal_error (__FILE__, __LINE__,
d9b3f62e 13302 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13303
d9b3f62e
PA
13304 fprintf_unfiltered (fp, " %s", self->addr_string);
13305 print_recreate_thread (self, fp);
13306
13307 if (tp->pass_count)
13308 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13309}
13310
983af33b
SDJ
13311static void
13312tracepoint_create_sals_from_address (char **arg,
13313 struct linespec_result *canonical,
13314 enum bptype type_wanted,
13315 char *addr_start, char **copy_arg)
13316{
13317 create_sals_from_address_default (arg, canonical, type_wanted,
13318 addr_start, copy_arg);
13319}
13320
13321static void
13322tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13323 struct linespec_result *canonical,
983af33b 13324 char *cond_string,
e7e0cddf 13325 char *extra_string,
983af33b
SDJ
13326 enum bptype type_wanted,
13327 enum bpdisp disposition,
13328 int thread,
13329 int task, int ignore_count,
13330 const struct breakpoint_ops *ops,
13331 int from_tty, int enabled,
44f238bb 13332 int internal, unsigned flags)
983af33b 13333{
023fa29b 13334 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13335 cond_string, extra_string,
13336 type_wanted,
983af33b
SDJ
13337 disposition, thread, task,
13338 ignore_count, ops, from_tty,
44f238bb 13339 enabled, internal, flags);
983af33b
SDJ
13340}
13341
13342static void
13343tracepoint_decode_linespec (struct breakpoint *b, char **s,
13344 struct symtabs_and_lines *sals)
13345{
13346 decode_linespec_default (b, s, sals);
13347}
13348
2060206e 13349struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13350
55aa24fb
SDJ
13351/* The breakpoint_ops structure to be use on tracepoints placed in a
13352 static probe. */
13353
13354static void
13355tracepoint_probe_create_sals_from_address (char **arg,
13356 struct linespec_result *canonical,
13357 enum bptype type_wanted,
13358 char *addr_start, char **copy_arg)
13359{
13360 /* We use the same method for breakpoint on probes. */
13361 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13362 addr_start, copy_arg);
13363}
13364
13365static void
13366tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13367 struct symtabs_and_lines *sals)
13368{
13369 /* We use the same method for breakpoint on probes. */
13370 bkpt_probe_decode_linespec (b, s, sals);
13371}
13372
13373static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13374
5c2b4418
HZ
13375/* Dprintf breakpoint_ops methods. */
13376
13377static void
13378dprintf_re_set (struct breakpoint *b)
13379{
13380 breakpoint_re_set_default (b);
13381
13382 /* This breakpoint could have been pending, and be resolved now, and
13383 if so, we should now have the extra string. If we don't, the
13384 dprintf was malformed when created, but we couldn't tell because
13385 we can't extract the extra string until the location is
13386 resolved. */
13387 if (b->loc != NULL && b->extra_string == NULL)
13388 error (_("Format string required"));
13389
13390 /* 1 - connect to target 1, that can run breakpoint commands.
13391 2 - create a dprintf, which resolves fine.
13392 3 - disconnect from target 1
13393 4 - connect to target 2, that can NOT run breakpoint commands.
13394
13395 After steps #3/#4, you'll want the dprintf command list to
13396 be updated, because target 1 and 2 may well return different
13397 answers for target_can_run_breakpoint_commands().
13398 Given absence of finer grained resetting, we get to do
13399 it all the time. */
13400 if (b->extra_string != NULL)
13401 update_dprintf_command_list (b);
13402}
13403
2d9442cc
HZ
13404/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13405
13406static void
13407dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13408{
13409 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13410 tp->extra_string);
13411 print_recreate_thread (tp, fp);
13412}
13413
9d6e6e84
HZ
13414/* Implement the "after_condition_true" breakpoint_ops method for
13415 dprintf.
13416
13417 dprintf's are implemented with regular commands in their command
13418 list, but we run the commands here instead of before presenting the
13419 stop to the user, as dprintf's don't actually cause a stop. This
13420 also makes it so that the commands of multiple dprintfs at the same
13421 address are all handled. */
13422
13423static void
13424dprintf_after_condition_true (struct bpstats *bs)
13425{
13426 struct cleanup *old_chain;
13427 struct bpstats tmp_bs = { NULL };
13428 struct bpstats *tmp_bs_p = &tmp_bs;
13429
13430 /* dprintf's never cause a stop. This wasn't set in the
13431 check_status hook instead because that would make the dprintf's
13432 condition not be evaluated. */
13433 bs->stop = 0;
13434
13435 /* Run the command list here. Take ownership of it instead of
13436 copying. We never want these commands to run later in
13437 bpstat_do_actions, if a breakpoint that causes a stop happens to
13438 be set at same address as this dprintf, or even if running the
13439 commands here throws. */
13440 tmp_bs.commands = bs->commands;
13441 bs->commands = NULL;
13442 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13443
13444 bpstat_do_actions_1 (&tmp_bs_p);
13445
13446 /* 'tmp_bs.commands' will usually be NULL by now, but
13447 bpstat_do_actions_1 may return early without processing the whole
13448 list. */
13449 do_cleanups (old_chain);
13450}
13451
983af33b
SDJ
13452/* The breakpoint_ops structure to be used on static tracepoints with
13453 markers (`-m'). */
13454
13455static void
13456strace_marker_create_sals_from_address (char **arg,
13457 struct linespec_result *canonical,
13458 enum bptype type_wanted,
13459 char *addr_start, char **copy_arg)
13460{
13461 struct linespec_sals lsal;
13462
13463 lsal.sals = decode_static_tracepoint_spec (arg);
13464
13465 *copy_arg = savestring (addr_start, *arg - addr_start);
13466
13467 canonical->addr_string = xstrdup (*copy_arg);
13468 lsal.canonical = xstrdup (*copy_arg);
13469 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13470}
13471
13472static void
13473strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13474 struct linespec_result *canonical,
983af33b 13475 char *cond_string,
e7e0cddf 13476 char *extra_string,
983af33b
SDJ
13477 enum bptype type_wanted,
13478 enum bpdisp disposition,
13479 int thread,
13480 int task, int ignore_count,
13481 const struct breakpoint_ops *ops,
13482 int from_tty, int enabled,
44f238bb 13483 int internal, unsigned flags)
983af33b
SDJ
13484{
13485 int i;
52d361e1
YQ
13486 struct linespec_sals *lsal = VEC_index (linespec_sals,
13487 canonical->sals, 0);
983af33b
SDJ
13488
13489 /* If the user is creating a static tracepoint by marker id
13490 (strace -m MARKER_ID), then store the sals index, so that
13491 breakpoint_re_set can try to match up which of the newly
13492 found markers corresponds to this one, and, don't try to
13493 expand multiple locations for each sal, given than SALS
13494 already should contain all sals for MARKER_ID. */
13495
13496 for (i = 0; i < lsal->sals.nelts; ++i)
13497 {
13498 struct symtabs_and_lines expanded;
13499 struct tracepoint *tp;
13500 struct cleanup *old_chain;
13501 char *addr_string;
13502
13503 expanded.nelts = 1;
13504 expanded.sals = &lsal->sals.sals[i];
13505
13506 addr_string = xstrdup (canonical->addr_string);
13507 old_chain = make_cleanup (xfree, addr_string);
13508
13509 tp = XCNEW (struct tracepoint);
13510 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13511 addr_string, NULL,
e7e0cddf
SS
13512 cond_string, extra_string,
13513 type_wanted, disposition,
983af33b 13514 thread, task, ignore_count, ops,
44f238bb 13515 from_tty, enabled, internal, flags,
983af33b
SDJ
13516 canonical->special_display);
13517 /* Given that its possible to have multiple markers with
13518 the same string id, if the user is creating a static
13519 tracepoint by marker id ("strace -m MARKER_ID"), then
13520 store the sals index, so that breakpoint_re_set can
13521 try to match up which of the newly found markers
13522 corresponds to this one */
13523 tp->static_trace_marker_id_idx = i;
13524
13525 install_breakpoint (internal, &tp->base, 0);
13526
13527 discard_cleanups (old_chain);
13528 }
13529}
13530
13531static void
13532strace_marker_decode_linespec (struct breakpoint *b, char **s,
13533 struct symtabs_and_lines *sals)
13534{
13535 struct tracepoint *tp = (struct tracepoint *) b;
13536
13537 *sals = decode_static_tracepoint_spec (s);
13538 if (sals->nelts > tp->static_trace_marker_id_idx)
13539 {
13540 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13541 sals->nelts = 1;
13542 }
13543 else
13544 error (_("marker %s not found"), tp->static_trace_marker_id);
13545}
13546
13547static struct breakpoint_ops strace_marker_breakpoint_ops;
13548
13549static int
13550strace_marker_p (struct breakpoint *b)
13551{
13552 return b->ops == &strace_marker_breakpoint_ops;
13553}
13554
53a5351d 13555/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13556 structures. */
c906108c
SS
13557
13558void
fba45db2 13559delete_breakpoint (struct breakpoint *bpt)
c906108c 13560{
52f0bd74 13561 struct breakpoint *b;
c906108c 13562
8a3fe4f8 13563 gdb_assert (bpt != NULL);
c906108c 13564
4a64f543
MS
13565 /* Has this bp already been deleted? This can happen because
13566 multiple lists can hold pointers to bp's. bpstat lists are
13567 especial culprits.
13568
13569 One example of this happening is a watchpoint's scope bp. When
13570 the scope bp triggers, we notice that the watchpoint is out of
13571 scope, and delete it. We also delete its scope bp. But the
13572 scope bp is marked "auto-deleting", and is already on a bpstat.
13573 That bpstat is then checked for auto-deleting bp's, which are
13574 deleted.
13575
13576 A real solution to this problem might involve reference counts in
13577 bp's, and/or giving them pointers back to their referencing
13578 bpstat's, and teaching delete_breakpoint to only free a bp's
13579 storage when no more references were extent. A cheaper bandaid
13580 was chosen. */
c906108c
SS
13581 if (bpt->type == bp_none)
13582 return;
13583
4a64f543
MS
13584 /* At least avoid this stale reference until the reference counting
13585 of breakpoints gets resolved. */
d0fb5eae 13586 if (bpt->related_breakpoint != bpt)
e5a0a904 13587 {
d0fb5eae 13588 struct breakpoint *related;
3a5c3e22 13589 struct watchpoint *w;
d0fb5eae
JK
13590
13591 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13592 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13593 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13594 w = (struct watchpoint *) bpt;
13595 else
13596 w = NULL;
13597 if (w != NULL)
13598 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13599
13600 /* Unlink bpt from the bpt->related_breakpoint ring. */
13601 for (related = bpt; related->related_breakpoint != bpt;
13602 related = related->related_breakpoint);
13603 related->related_breakpoint = bpt->related_breakpoint;
13604 bpt->related_breakpoint = bpt;
e5a0a904
JK
13605 }
13606
a9634178
TJB
13607 /* watch_command_1 creates a watchpoint but only sets its number if
13608 update_watchpoint succeeds in creating its bp_locations. If there's
13609 a problem in that process, we'll be asked to delete the half-created
13610 watchpoint. In that case, don't announce the deletion. */
13611 if (bpt->number)
13612 observer_notify_breakpoint_deleted (bpt);
c906108c 13613
c906108c
SS
13614 if (breakpoint_chain == bpt)
13615 breakpoint_chain = bpt->next;
13616
c906108c
SS
13617 ALL_BREAKPOINTS (b)
13618 if (b->next == bpt)
c5aa993b
JM
13619 {
13620 b->next = bpt->next;
13621 break;
13622 }
c906108c 13623
f431efe5
PA
13624 /* Be sure no bpstat's are pointing at the breakpoint after it's
13625 been freed. */
13626 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13627 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13628 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13629 commands are associated with the bpstat; if we remove it here,
13630 then the later call to bpstat_do_actions (&stop_bpstat); in
13631 event-top.c won't do anything, and temporary breakpoints with
13632 commands won't work. */
13633
13634 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13635
4a64f543
MS
13636 /* Now that breakpoint is removed from breakpoint list, update the
13637 global location list. This will remove locations that used to
13638 belong to this breakpoint. Do this before freeing the breakpoint
13639 itself, since remove_breakpoint looks at location's owner. It
13640 might be better design to have location completely
13641 self-contained, but it's not the case now. */
44702360 13642 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13643
348d480f 13644 bpt->ops->dtor (bpt);
4a64f543
MS
13645 /* On the chance that someone will soon try again to delete this
13646 same bp, we mark it as deleted before freeing its storage. */
c906108c 13647 bpt->type = bp_none;
b8c9b27d 13648 xfree (bpt);
c906108c
SS
13649}
13650
4d6140d9
AC
13651static void
13652do_delete_breakpoint_cleanup (void *b)
13653{
13654 delete_breakpoint (b);
13655}
13656
13657struct cleanup *
13658make_cleanup_delete_breakpoint (struct breakpoint *b)
13659{
13660 return make_cleanup (do_delete_breakpoint_cleanup, b);
13661}
13662
51be5b68
PA
13663/* Iterator function to call a user-provided callback function once
13664 for each of B and its related breakpoints. */
13665
13666static void
13667iterate_over_related_breakpoints (struct breakpoint *b,
13668 void (*function) (struct breakpoint *,
13669 void *),
13670 void *data)
13671{
13672 struct breakpoint *related;
13673
13674 related = b;
13675 do
13676 {
13677 struct breakpoint *next;
13678
13679 /* FUNCTION may delete RELATED. */
13680 next = related->related_breakpoint;
13681
13682 if (next == related)
13683 {
13684 /* RELATED is the last ring entry. */
13685 function (related, data);
13686
13687 /* FUNCTION may have deleted it, so we'd never reach back to
13688 B. There's nothing left to do anyway, so just break
13689 out. */
13690 break;
13691 }
13692 else
13693 function (related, data);
13694
13695 related = next;
13696 }
13697 while (related != b);
13698}
95a42b64
TT
13699
13700static void
13701do_delete_breakpoint (struct breakpoint *b, void *ignore)
13702{
13703 delete_breakpoint (b);
13704}
13705
51be5b68
PA
13706/* A callback for map_breakpoint_numbers that calls
13707 delete_breakpoint. */
13708
13709static void
13710do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13711{
13712 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13713}
13714
c906108c 13715void
fba45db2 13716delete_command (char *arg, int from_tty)
c906108c 13717{
35df4500 13718 struct breakpoint *b, *b_tmp;
c906108c 13719
ea9365bb
TT
13720 dont_repeat ();
13721
c906108c
SS
13722 if (arg == 0)
13723 {
13724 int breaks_to_delete = 0;
13725
46c6471b
PA
13726 /* Delete all breakpoints if no argument. Do not delete
13727 internal breakpoints, these have to be deleted with an
13728 explicit breakpoint number argument. */
c5aa993b 13729 ALL_BREAKPOINTS (b)
46c6471b 13730 if (user_breakpoint_p (b))
973d738b
DJ
13731 {
13732 breaks_to_delete = 1;
13733 break;
13734 }
c906108c
SS
13735
13736 /* Ask user only if there are some breakpoints to delete. */
13737 if (!from_tty
e2e0b3e5 13738 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13739 {
35df4500 13740 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13741 if (user_breakpoint_p (b))
c5aa993b 13742 delete_breakpoint (b);
c906108c
SS
13743 }
13744 }
13745 else
51be5b68 13746 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13747}
13748
0d381245
VP
13749static int
13750all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 13751{
0d381245 13752 for (; loc; loc = loc->next)
8645ff69
UW
13753 if (!loc->shlib_disabled
13754 && !loc->pspace->executing_startup)
0d381245
VP
13755 return 0;
13756 return 1;
fe3f5fa8
VP
13757}
13758
776592bf
DE
13759/* Subroutine of update_breakpoint_locations to simplify it.
13760 Return non-zero if multiple fns in list LOC have the same name.
13761 Null names are ignored. */
13762
13763static int
13764ambiguous_names_p (struct bp_location *loc)
13765{
13766 struct bp_location *l;
13767 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13768 (int (*) (const void *,
13769 const void *)) streq,
776592bf
DE
13770 NULL, xcalloc, xfree);
13771
13772 for (l = loc; l != NULL; l = l->next)
13773 {
13774 const char **slot;
13775 const char *name = l->function_name;
13776
13777 /* Allow for some names to be NULL, ignore them. */
13778 if (name == NULL)
13779 continue;
13780
13781 slot = (const char **) htab_find_slot (htab, (const void *) name,
13782 INSERT);
4a64f543
MS
13783 /* NOTE: We can assume slot != NULL here because xcalloc never
13784 returns NULL. */
776592bf
DE
13785 if (*slot != NULL)
13786 {
13787 htab_delete (htab);
13788 return 1;
13789 }
13790 *slot = name;
13791 }
13792
13793 htab_delete (htab);
13794 return 0;
13795}
13796
0fb4aa4b
PA
13797/* When symbols change, it probably means the sources changed as well,
13798 and it might mean the static tracepoint markers are no longer at
13799 the same address or line numbers they used to be at last we
13800 checked. Losing your static tracepoints whenever you rebuild is
13801 undesirable. This function tries to resync/rematch gdb static
13802 tracepoints with the markers on the target, for static tracepoints
13803 that have not been set by marker id. Static tracepoint that have
13804 been set by marker id are reset by marker id in breakpoint_re_set.
13805 The heuristic is:
13806
13807 1) For a tracepoint set at a specific address, look for a marker at
13808 the old PC. If one is found there, assume to be the same marker.
13809 If the name / string id of the marker found is different from the
13810 previous known name, assume that means the user renamed the marker
13811 in the sources, and output a warning.
13812
13813 2) For a tracepoint set at a given line number, look for a marker
13814 at the new address of the old line number. If one is found there,
13815 assume to be the same marker. If the name / string id of the
13816 marker found is different from the previous known name, assume that
13817 means the user renamed the marker in the sources, and output a
13818 warning.
13819
13820 3) If a marker is no longer found at the same address or line, it
13821 may mean the marker no longer exists. But it may also just mean
13822 the code changed a bit. Maybe the user added a few lines of code
13823 that made the marker move up or down (in line number terms). Ask
13824 the target for info about the marker with the string id as we knew
13825 it. If found, update line number and address in the matching
13826 static tracepoint. This will get confused if there's more than one
13827 marker with the same ID (possible in UST, although unadvised
13828 precisely because it confuses tools). */
13829
13830static struct symtab_and_line
13831update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13832{
d9b3f62e 13833 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13834 struct static_tracepoint_marker marker;
13835 CORE_ADDR pc;
0fb4aa4b
PA
13836
13837 pc = sal.pc;
13838 if (sal.line)
13839 find_line_pc (sal.symtab, sal.line, &pc);
13840
13841 if (target_static_tracepoint_marker_at (pc, &marker))
13842 {
d9b3f62e 13843 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13844 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13845 b->number,
d9b3f62e 13846 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13847
d9b3f62e
PA
13848 xfree (tp->static_trace_marker_id);
13849 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13850 release_static_tracepoint_marker (&marker);
13851
13852 return sal;
13853 }
13854
13855 /* Old marker wasn't found on target at lineno. Try looking it up
13856 by string ID. */
13857 if (!sal.explicit_pc
13858 && sal.line != 0
13859 && sal.symtab != NULL
d9b3f62e 13860 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13861 {
13862 VEC(static_tracepoint_marker_p) *markers;
13863
13864 markers
d9b3f62e 13865 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13866
13867 if (!VEC_empty(static_tracepoint_marker_p, markers))
13868 {
80e1d417 13869 struct symtab_and_line sal2;
0fb4aa4b 13870 struct symbol *sym;
80e1d417 13871 struct static_tracepoint_marker *tpmarker;
79a45e25 13872 struct ui_out *uiout = current_uiout;
0fb4aa4b 13873
80e1d417 13874 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13875
d9b3f62e 13876 xfree (tp->static_trace_marker_id);
80e1d417 13877 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13878
13879 warning (_("marker for static tracepoint %d (%s) not "
13880 "found at previous line number"),
d9b3f62e 13881 b->number, tp->static_trace_marker_id);
0fb4aa4b 13882
80e1d417 13883 init_sal (&sal2);
0fb4aa4b 13884
80e1d417 13885 sal2.pc = tpmarker->address;
0fb4aa4b 13886
80e1d417
AS
13887 sal2 = find_pc_line (tpmarker->address, 0);
13888 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
13889 ui_out_text (uiout, "Now in ");
13890 if (sym)
13891 {
13892 ui_out_field_string (uiout, "func",
13893 SYMBOL_PRINT_NAME (sym));
13894 ui_out_text (uiout, " at ");
13895 }
05cba821
JK
13896 ui_out_field_string (uiout, "file",
13897 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
13898 ui_out_text (uiout, ":");
13899
13900 if (ui_out_is_mi_like_p (uiout))
13901 {
0b0865da 13902 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13903
f35a17b5 13904 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
13905 }
13906
80e1d417 13907 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
13908 ui_out_text (uiout, "\n");
13909
80e1d417 13910 b->loc->line_number = sal2.line;
2f202fde 13911 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
13912
13913 xfree (b->addr_string);
13914 b->addr_string = xstrprintf ("%s:%d",
05cba821 13915 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 13916 b->loc->line_number);
0fb4aa4b
PA
13917
13918 /* Might be nice to check if function changed, and warn if
13919 so. */
13920
80e1d417 13921 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13922 }
13923 }
13924 return sal;
13925}
13926
8d3788bd
VP
13927/* Returns 1 iff locations A and B are sufficiently same that
13928 we don't need to report breakpoint as changed. */
13929
13930static int
13931locations_are_equal (struct bp_location *a, struct bp_location *b)
13932{
13933 while (a && b)
13934 {
13935 if (a->address != b->address)
13936 return 0;
13937
13938 if (a->shlib_disabled != b->shlib_disabled)
13939 return 0;
13940
13941 if (a->enabled != b->enabled)
13942 return 0;
13943
13944 a = a->next;
13945 b = b->next;
13946 }
13947
13948 if ((a == NULL) != (b == NULL))
13949 return 0;
13950
13951 return 1;
13952}
13953
f1310107
TJB
13954/* Create new breakpoint locations for B (a hardware or software breakpoint)
13955 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
13956 a ranged breakpoint. */
13957
0e30163f 13958void
0d381245 13959update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
13960 struct symtabs_and_lines sals,
13961 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
13962{
13963 int i;
0d381245
VP
13964 struct bp_location *existing_locations = b->loc;
13965
f8eba3c6
TT
13966 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
13967 {
13968 /* Ranged breakpoints have only one start location and one end
13969 location. */
13970 b->enable_state = bp_disabled;
44702360 13971 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
13972 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13973 "multiple locations found\n"),
13974 b->number);
13975 return;
13976 }
f1310107 13977
4a64f543
MS
13978 /* If there's no new locations, and all existing locations are
13979 pending, don't do anything. This optimizes the common case where
13980 all locations are in the same shared library, that was unloaded.
13981 We'd like to retain the location, so that when the library is
13982 loaded again, we don't loose the enabled/disabled status of the
13983 individual locations. */
0d381245 13984 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
13985 return;
13986
fe3f5fa8
VP
13987 b->loc = NULL;
13988
0d381245 13989 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 13990 {
f8eba3c6
TT
13991 struct bp_location *new_loc;
13992
13993 switch_to_program_space_and_thread (sals.sals[i].pspace);
13994
13995 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 13996
0d381245
VP
13997 /* Reparse conditions, they might contain references to the
13998 old symtab. */
13999 if (b->cond_string != NULL)
14000 {
bbc13ae3 14001 const char *s;
fe3f5fa8 14002
0d381245 14003 s = b->cond_string;
492d29ea 14004 TRY
0d381245 14005 {
1bb9788d
TT
14006 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14007 block_for_pc (sals.sals[i].pc),
0d381245
VP
14008 0);
14009 }
492d29ea 14010 CATCH (e, RETURN_MASK_ERROR)
0d381245 14011 {
3e43a32a
MS
14012 warning (_("failed to reevaluate condition "
14013 "for breakpoint %d: %s"),
0d381245
VP
14014 b->number, e.message);
14015 new_loc->enabled = 0;
14016 }
492d29ea 14017 END_CATCH
0d381245 14018 }
fe3f5fa8 14019
f1310107
TJB
14020 if (sals_end.nelts)
14021 {
14022 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14023
14024 new_loc->length = end - sals.sals[0].pc + 1;
14025 }
0d381245 14026 }
fe3f5fa8 14027
4a64f543
MS
14028 /* If possible, carry over 'disable' status from existing
14029 breakpoints. */
0d381245
VP
14030 {
14031 struct bp_location *e = existing_locations;
776592bf
DE
14032 /* If there are multiple breakpoints with the same function name,
14033 e.g. for inline functions, comparing function names won't work.
14034 Instead compare pc addresses; this is just a heuristic as things
14035 may have moved, but in practice it gives the correct answer
14036 often enough until a better solution is found. */
14037 int have_ambiguous_names = ambiguous_names_p (b->loc);
14038
0d381245
VP
14039 for (; e; e = e->next)
14040 {
14041 if (!e->enabled && e->function_name)
14042 {
14043 struct bp_location *l = b->loc;
776592bf
DE
14044 if (have_ambiguous_names)
14045 {
14046 for (; l; l = l->next)
f1310107 14047 if (breakpoint_locations_match (e, l))
776592bf
DE
14048 {
14049 l->enabled = 0;
14050 break;
14051 }
14052 }
14053 else
14054 {
14055 for (; l; l = l->next)
14056 if (l->function_name
14057 && strcmp (e->function_name, l->function_name) == 0)
14058 {
14059 l->enabled = 0;
14060 break;
14061 }
14062 }
0d381245
VP
14063 }
14064 }
14065 }
fe3f5fa8 14066
8d3788bd
VP
14067 if (!locations_are_equal (existing_locations, b->loc))
14068 observer_notify_breakpoint_modified (b);
14069
44702360 14070 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14071}
14072
ef23e705
TJB
14073/* Find the SaL locations corresponding to the given ADDR_STRING.
14074 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14075
14076static struct symtabs_and_lines
14077addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14078{
14079 char *s;
02d20e4a 14080 struct symtabs_and_lines sals = {0};
492d29ea 14081 struct gdb_exception exception = exception_none;
ef23e705 14082
983af33b 14083 gdb_assert (b->ops != NULL);
ef23e705 14084 s = addr_string;
ef23e705 14085
492d29ea 14086 TRY
ef23e705 14087 {
983af33b 14088 b->ops->decode_linespec (b, &s, &sals);
ef23e705 14089 }
492d29ea 14090 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14091 {
14092 int not_found_and_ok = 0;
492d29ea
PA
14093
14094 exception = e;
14095
ef23e705
TJB
14096 /* For pending breakpoints, it's expected that parsing will
14097 fail until the right shared library is loaded. User has
14098 already told to create pending breakpoints and don't need
14099 extra messages. If breakpoint is in bp_shlib_disabled
14100 state, then user already saw the message about that
14101 breakpoint being disabled, and don't want to see more
14102 errors. */
58438ac1 14103 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14104 && (b->condition_not_parsed
14105 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14106 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14107 || b->enable_state == bp_disabled))
14108 not_found_and_ok = 1;
14109
14110 if (!not_found_and_ok)
14111 {
14112 /* We surely don't want to warn about the same breakpoint
14113 10 times. One solution, implemented here, is disable
14114 the breakpoint on error. Another solution would be to
14115 have separate 'warning emitted' flag. Since this
14116 happens only when a binary has changed, I don't know
14117 which approach is better. */
14118 b->enable_state = bp_disabled;
14119 throw_exception (e);
14120 }
14121 }
492d29ea 14122 END_CATCH
ef23e705 14123
492d29ea 14124 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14125 {
f8eba3c6 14126 int i;
ef23e705 14127
f8eba3c6
TT
14128 for (i = 0; i < sals.nelts; ++i)
14129 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14130 if (b->condition_not_parsed && s && s[0])
14131 {
ed1d1739
KS
14132 char *cond_string, *extra_string;
14133 int thread, task;
ef23e705
TJB
14134
14135 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14136 &cond_string, &thread, &task,
14137 &extra_string);
ef23e705
TJB
14138 if (cond_string)
14139 b->cond_string = cond_string;
14140 b->thread = thread;
14141 b->task = task;
e7e0cddf
SS
14142 if (extra_string)
14143 b->extra_string = extra_string;
ef23e705
TJB
14144 b->condition_not_parsed = 0;
14145 }
14146
983af33b 14147 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14148 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14149
58438ac1
TT
14150 *found = 1;
14151 }
14152 else
14153 *found = 0;
ef23e705
TJB
14154
14155 return sals;
14156}
14157
348d480f
PA
14158/* The default re_set method, for typical hardware or software
14159 breakpoints. Reevaluate the breakpoint and recreate its
14160 locations. */
14161
14162static void
28010a5d 14163breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14164{
14165 int found;
f1310107 14166 struct symtabs_and_lines sals, sals_end;
ef23e705 14167 struct symtabs_and_lines expanded = {0};
f1310107 14168 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14169
14170 sals = addr_string_to_sals (b, b->addr_string, &found);
14171 if (found)
14172 {
14173 make_cleanup (xfree, sals.sals);
f8eba3c6 14174 expanded = sals;
ef23e705
TJB
14175 }
14176
f1310107
TJB
14177 if (b->addr_string_range_end)
14178 {
14179 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14180 if (found)
14181 {
14182 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14183 expanded_end = sals_end;
f1310107
TJB
14184 }
14185 }
14186
14187 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14188}
14189
983af33b
SDJ
14190/* Default method for creating SALs from an address string. It basically
14191 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14192
14193static void
14194create_sals_from_address_default (char **arg,
14195 struct linespec_result *canonical,
14196 enum bptype type_wanted,
14197 char *addr_start, char **copy_arg)
14198{
14199 parse_breakpoint_sals (arg, canonical);
14200}
14201
14202/* Call create_breakpoints_sal for the given arguments. This is the default
14203 function for the `create_breakpoints_sal' method of
14204 breakpoint_ops. */
14205
14206static void
14207create_breakpoints_sal_default (struct gdbarch *gdbarch,
14208 struct linespec_result *canonical,
983af33b 14209 char *cond_string,
e7e0cddf 14210 char *extra_string,
983af33b
SDJ
14211 enum bptype type_wanted,
14212 enum bpdisp disposition,
14213 int thread,
14214 int task, int ignore_count,
14215 const struct breakpoint_ops *ops,
14216 int from_tty, int enabled,
44f238bb 14217 int internal, unsigned flags)
983af33b
SDJ
14218{
14219 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14220 extra_string,
983af33b
SDJ
14221 type_wanted, disposition,
14222 thread, task, ignore_count, ops, from_tty,
44f238bb 14223 enabled, internal, flags);
983af33b
SDJ
14224}
14225
14226/* Decode the line represented by S by calling decode_line_full. This is the
14227 default function for the `decode_linespec' method of breakpoint_ops. */
14228
14229static void
14230decode_linespec_default (struct breakpoint *b, char **s,
14231 struct symtabs_and_lines *sals)
14232{
14233 struct linespec_result canonical;
14234
14235 init_linespec_result (&canonical);
14236 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14237 (struct symtab *) NULL, 0,
14238 &canonical, multiple_symbols_all,
14239 b->filter);
14240
14241 /* We should get 0 or 1 resulting SALs. */
14242 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14243
14244 if (VEC_length (linespec_sals, canonical.sals) > 0)
14245 {
14246 struct linespec_sals *lsal;
14247
14248 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14249 *sals = lsal->sals;
14250 /* Arrange it so the destructor does not free the
14251 contents. */
14252 lsal->sals.sals = NULL;
14253 }
14254
14255 destroy_linespec_result (&canonical);
14256}
14257
28010a5d
PA
14258/* Prepare the global context for a re-set of breakpoint B. */
14259
14260static struct cleanup *
14261prepare_re_set_context (struct breakpoint *b)
14262{
14263 struct cleanup *cleanups;
14264
14265 input_radix = b->input_radix;
14266 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14267 if (b->pspace != NULL)
14268 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14269 set_language (b->language);
14270
14271 return cleanups;
ef23e705
TJB
14272}
14273
c906108c
SS
14274/* Reset a breakpoint given it's struct breakpoint * BINT.
14275 The value we return ends up being the return value from catch_errors.
14276 Unused in this case. */
14277
14278static int
4efb68b1 14279breakpoint_re_set_one (void *bint)
c906108c 14280{
4a64f543 14281 /* Get past catch_errs. */
53a5351d 14282 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14283 struct cleanup *cleanups;
c906108c 14284
348d480f
PA
14285 cleanups = prepare_re_set_context (b);
14286 b->ops->re_set (b);
14287 do_cleanups (cleanups);
c906108c
SS
14288 return 0;
14289}
14290
69de3c6a 14291/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14292void
69de3c6a 14293breakpoint_re_set (void)
c906108c 14294{
35df4500 14295 struct breakpoint *b, *b_tmp;
c906108c
SS
14296 enum language save_language;
14297 int save_input_radix;
6c95b8df 14298 struct cleanup *old_chain;
c5aa993b 14299
c906108c
SS
14300 save_language = current_language->la_language;
14301 save_input_radix = input_radix;
6c95b8df
PA
14302 old_chain = save_current_program_space ();
14303
35df4500 14304 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14305 {
4a64f543 14306 /* Format possible error msg. */
fe3f5fa8 14307 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14308 b->number);
14309 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14310 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14311 do_cleanups (cleanups);
c5aa993b 14312 }
c906108c
SS
14313 set_language (save_language);
14314 input_radix = save_input_radix;
e62c965a 14315
0756c555 14316 jit_breakpoint_re_set ();
4efc6507 14317
6c95b8df
PA
14318 do_cleanups (old_chain);
14319
af02033e
PP
14320 create_overlay_event_breakpoint ();
14321 create_longjmp_master_breakpoint ();
14322 create_std_terminate_master_breakpoint ();
186c406b 14323 create_exception_master_breakpoint ();
c906108c
SS
14324}
14325\f
c906108c
SS
14326/* Reset the thread number of this breakpoint:
14327
14328 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14329 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14330void
fba45db2 14331breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14332{
14333 if (b->thread != -1)
14334 {
39f77062
KB
14335 if (in_thread_list (inferior_ptid))
14336 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14337
14338 /* We're being called after following a fork. The new fork is
14339 selected as current, and unless this was a vfork will have a
14340 different program space from the original thread. Reset that
14341 as well. */
14342 b->loc->pspace = current_program_space;
c906108c
SS
14343 }
14344}
14345
03ac34d5
MS
14346/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14347 If from_tty is nonzero, it prints a message to that effect,
14348 which ends with a period (no newline). */
14349
c906108c 14350void
fba45db2 14351set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14352{
52f0bd74 14353 struct breakpoint *b;
c906108c
SS
14354
14355 if (count < 0)
14356 count = 0;
14357
14358 ALL_BREAKPOINTS (b)
14359 if (b->number == bptnum)
c5aa993b 14360 {
d77f58be
SS
14361 if (is_tracepoint (b))
14362 {
14363 if (from_tty && count != 0)
14364 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14365 bptnum);
14366 return;
14367 }
14368
c5aa993b 14369 b->ignore_count = count;
221ea385
KS
14370 if (from_tty)
14371 {
14372 if (count == 0)
3e43a32a
MS
14373 printf_filtered (_("Will stop next time "
14374 "breakpoint %d is reached."),
221ea385
KS
14375 bptnum);
14376 else if (count == 1)
a3f17187 14377 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14378 bptnum);
14379 else
3e43a32a
MS
14380 printf_filtered (_("Will ignore next %d "
14381 "crossings of breakpoint %d."),
221ea385
KS
14382 count, bptnum);
14383 }
8d3788bd 14384 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14385 return;
14386 }
c906108c 14387
8a3fe4f8 14388 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14389}
14390
c906108c
SS
14391/* Command to set ignore-count of breakpoint N to COUNT. */
14392
14393static void
fba45db2 14394ignore_command (char *args, int from_tty)
c906108c
SS
14395{
14396 char *p = args;
52f0bd74 14397 int num;
c906108c
SS
14398
14399 if (p == 0)
e2e0b3e5 14400 error_no_arg (_("a breakpoint number"));
c5aa993b 14401
c906108c 14402 num = get_number (&p);
5c44784c 14403 if (num == 0)
8a3fe4f8 14404 error (_("bad breakpoint number: '%s'"), args);
c906108c 14405 if (*p == 0)
8a3fe4f8 14406 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14407
14408 set_ignore_count (num,
14409 longest_to_int (value_as_long (parse_and_eval (p))),
14410 from_tty);
221ea385
KS
14411 if (from_tty)
14412 printf_filtered ("\n");
c906108c
SS
14413}
14414\f
14415/* Call FUNCTION on each of the breakpoints
14416 whose numbers are given in ARGS. */
14417
14418static void
95a42b64
TT
14419map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14420 void *),
14421 void *data)
c906108c 14422{
52f0bd74
AC
14423 int num;
14424 struct breakpoint *b, *tmp;
11cf8741 14425 int match;
197f0a60 14426 struct get_number_or_range_state state;
c906108c 14427
b9d61307 14428 if (args == 0 || *args == '\0')
e2e0b3e5 14429 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14430
197f0a60
TT
14431 init_number_or_range (&state, args);
14432
14433 while (!state.finished)
c906108c 14434 {
e799154c 14435 const char *p = state.string;
197f0a60 14436
11cf8741 14437 match = 0;
c5aa993b 14438
197f0a60 14439 num = get_number_or_range (&state);
5c44784c 14440 if (num == 0)
c5aa993b 14441 {
8a3fe4f8 14442 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14443 }
14444 else
14445 {
14446 ALL_BREAKPOINTS_SAFE (b, tmp)
14447 if (b->number == num)
14448 {
11cf8741 14449 match = 1;
cdac0397 14450 function (b, data);
11cf8741 14451 break;
5c44784c 14452 }
11cf8741 14453 if (match == 0)
a3f17187 14454 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14455 }
c906108c
SS
14456 }
14457}
14458
0d381245
VP
14459static struct bp_location *
14460find_location_by_number (char *number)
14461{
14462 char *dot = strchr (number, '.');
14463 char *p1;
14464 int bp_num;
14465 int loc_num;
14466 struct breakpoint *b;
14467 struct bp_location *loc;
14468
14469 *dot = '\0';
14470
14471 p1 = number;
197f0a60 14472 bp_num = get_number (&p1);
0d381245
VP
14473 if (bp_num == 0)
14474 error (_("Bad breakpoint number '%s'"), number);
14475
14476 ALL_BREAKPOINTS (b)
14477 if (b->number == bp_num)
14478 {
14479 break;
14480 }
14481
14482 if (!b || b->number != bp_num)
14483 error (_("Bad breakpoint number '%s'"), number);
14484
14485 p1 = dot+1;
197f0a60 14486 loc_num = get_number (&p1);
0d381245
VP
14487 if (loc_num == 0)
14488 error (_("Bad breakpoint location number '%s'"), number);
14489
14490 --loc_num;
14491 loc = b->loc;
14492 for (;loc_num && loc; --loc_num, loc = loc->next)
14493 ;
14494 if (!loc)
14495 error (_("Bad breakpoint location number '%s'"), dot+1);
14496
14497 return loc;
14498}
14499
14500
1900040c
MS
14501/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14502 If from_tty is nonzero, it prints a message to that effect,
14503 which ends with a period (no newline). */
14504
c906108c 14505void
fba45db2 14506disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14507{
14508 /* Never disable a watchpoint scope breakpoint; we want to
14509 hit them when we leave scope so we can delete both the
14510 watchpoint and its scope breakpoint at that time. */
14511 if (bpt->type == bp_watchpoint_scope)
14512 return;
14513
b5de0fa7 14514 bpt->enable_state = bp_disabled;
c906108c 14515
b775012e
LM
14516 /* Mark breakpoint locations modified. */
14517 mark_breakpoint_modified (bpt);
14518
d248b706
KY
14519 if (target_supports_enable_disable_tracepoint ()
14520 && current_trace_status ()->running && is_tracepoint (bpt))
14521 {
14522 struct bp_location *location;
14523
14524 for (location = bpt->loc; location; location = location->next)
14525 target_disable_tracepoint (location);
14526 }
14527
44702360 14528 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14529
8d3788bd 14530 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14531}
14532
51be5b68
PA
14533/* A callback for iterate_over_related_breakpoints. */
14534
14535static void
14536do_disable_breakpoint (struct breakpoint *b, void *ignore)
14537{
14538 disable_breakpoint (b);
14539}
14540
95a42b64
TT
14541/* A callback for map_breakpoint_numbers that calls
14542 disable_breakpoint. */
14543
14544static void
14545do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14546{
51be5b68 14547 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14548}
14549
c906108c 14550static void
fba45db2 14551disable_command (char *args, int from_tty)
c906108c 14552{
c906108c 14553 if (args == 0)
46c6471b
PA
14554 {
14555 struct breakpoint *bpt;
14556
14557 ALL_BREAKPOINTS (bpt)
14558 if (user_breakpoint_p (bpt))
14559 disable_breakpoint (bpt);
14560 }
9eaabc75 14561 else
0d381245 14562 {
9eaabc75
MW
14563 char *num = extract_arg (&args);
14564
14565 while (num)
d248b706 14566 {
9eaabc75 14567 if (strchr (num, '.'))
b775012e 14568 {
9eaabc75
MW
14569 struct bp_location *loc = find_location_by_number (num);
14570
14571 if (loc)
14572 {
14573 if (loc->enabled)
14574 {
14575 loc->enabled = 0;
14576 mark_breakpoint_location_modified (loc);
14577 }
14578 if (target_supports_enable_disable_tracepoint ()
14579 && current_trace_status ()->running && loc->owner
14580 && is_tracepoint (loc->owner))
14581 target_disable_tracepoint (loc);
14582 }
44702360 14583 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14584 }
9eaabc75
MW
14585 else
14586 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14587 num = extract_arg (&args);
d248b706 14588 }
0d381245 14589 }
c906108c
SS
14590}
14591
14592static void
816338b5
SS
14593enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14594 int count)
c906108c 14595{
afe38095 14596 int target_resources_ok;
c906108c
SS
14597
14598 if (bpt->type == bp_hardware_breakpoint)
14599 {
14600 int i;
c5aa993b 14601 i = hw_breakpoint_used_count ();
53a5351d 14602 target_resources_ok =
d92524f1 14603 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14604 i + 1, 0);
c906108c 14605 if (target_resources_ok == 0)
8a3fe4f8 14606 error (_("No hardware breakpoint support in the target."));
c906108c 14607 else if (target_resources_ok < 0)
8a3fe4f8 14608 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14609 }
14610
cc60f2e3 14611 if (is_watchpoint (bpt))
c906108c 14612 {
d07205c2
JK
14613 /* Initialize it just to avoid a GCC false warning. */
14614 enum enable_state orig_enable_state = 0;
dde02812 14615
492d29ea 14616 TRY
c906108c 14617 {
3a5c3e22
PA
14618 struct watchpoint *w = (struct watchpoint *) bpt;
14619
1e718ff1
TJB
14620 orig_enable_state = bpt->enable_state;
14621 bpt->enable_state = bp_enabled;
3a5c3e22 14622 update_watchpoint (w, 1 /* reparse */);
c906108c 14623 }
492d29ea 14624 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14625 {
1e718ff1 14626 bpt->enable_state = orig_enable_state;
dde02812
ES
14627 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14628 bpt->number);
14629 return;
c5aa993b 14630 }
492d29ea 14631 END_CATCH
c906108c 14632 }
0101ce28 14633
b775012e
LM
14634 bpt->enable_state = bp_enabled;
14635
14636 /* Mark breakpoint locations modified. */
14637 mark_breakpoint_modified (bpt);
14638
d248b706
KY
14639 if (target_supports_enable_disable_tracepoint ()
14640 && current_trace_status ()->running && is_tracepoint (bpt))
14641 {
14642 struct bp_location *location;
14643
14644 for (location = bpt->loc; location; location = location->next)
14645 target_enable_tracepoint (location);
14646 }
14647
b4c291bb 14648 bpt->disposition = disposition;
816338b5 14649 bpt->enable_count = count;
44702360 14650 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14651
8d3788bd 14652 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14653}
14654
fe3f5fa8 14655
c906108c 14656void
fba45db2 14657enable_breakpoint (struct breakpoint *bpt)
c906108c 14658{
816338b5 14659 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14660}
14661
14662static void
14663do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14664{
14665 enable_breakpoint (bpt);
c906108c
SS
14666}
14667
95a42b64
TT
14668/* A callback for map_breakpoint_numbers that calls
14669 enable_breakpoint. */
14670
14671static void
14672do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14673{
51be5b68 14674 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14675}
14676
c906108c
SS
14677/* The enable command enables the specified breakpoints (or all defined
14678 breakpoints) so they once again become (or continue to be) effective
1272ad14 14679 in stopping the inferior. */
c906108c 14680
c906108c 14681static void
fba45db2 14682enable_command (char *args, int from_tty)
c906108c 14683{
c906108c 14684 if (args == 0)
46c6471b
PA
14685 {
14686 struct breakpoint *bpt;
14687
14688 ALL_BREAKPOINTS (bpt)
14689 if (user_breakpoint_p (bpt))
14690 enable_breakpoint (bpt);
14691 }
9eaabc75 14692 else
0d381245 14693 {
9eaabc75
MW
14694 char *num = extract_arg (&args);
14695
14696 while (num)
d248b706 14697 {
9eaabc75 14698 if (strchr (num, '.'))
b775012e 14699 {
9eaabc75
MW
14700 struct bp_location *loc = find_location_by_number (num);
14701
14702 if (loc)
14703 {
14704 if (!loc->enabled)
14705 {
14706 loc->enabled = 1;
14707 mark_breakpoint_location_modified (loc);
14708 }
14709 if (target_supports_enable_disable_tracepoint ()
14710 && current_trace_status ()->running && loc->owner
14711 && is_tracepoint (loc->owner))
14712 target_enable_tracepoint (loc);
14713 }
44702360 14714 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14715 }
9eaabc75
MW
14716 else
14717 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14718 num = extract_arg (&args);
d248b706 14719 }
0d381245 14720 }
c906108c
SS
14721}
14722
816338b5
SS
14723/* This struct packages up disposition data for application to multiple
14724 breakpoints. */
14725
14726struct disp_data
14727{
14728 enum bpdisp disp;
14729 int count;
14730};
14731
c906108c 14732static void
51be5b68
PA
14733do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14734{
816338b5 14735 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14736
816338b5 14737 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14738}
14739
14740static void
14741do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14742{
816338b5 14743 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14744
14745 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14746}
14747
c906108c 14748static void
fba45db2 14749enable_once_command (char *args, int from_tty)
c906108c 14750{
51be5b68 14751 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14752}
14753
816338b5
SS
14754static void
14755do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14756{
14757 struct disp_data disp = { disp_disable, *(int *) countptr };
14758
14759 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14760}
14761
14762static void
14763enable_count_command (char *args, int from_tty)
14764{
b9d61307
SM
14765 int count;
14766
14767 if (args == NULL)
14768 error_no_arg (_("hit count"));
14769
14770 count = get_number (&args);
816338b5
SS
14771
14772 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14773}
14774
c906108c 14775static void
51be5b68 14776do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14777{
816338b5 14778 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14779
14780 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14781}
14782
c906108c 14783static void
fba45db2 14784enable_delete_command (char *args, int from_tty)
c906108c 14785{
51be5b68 14786 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14787}
14788\f
fa8d40ab
JJ
14789static void
14790set_breakpoint_cmd (char *args, int from_tty)
14791{
14792}
14793
14794static void
14795show_breakpoint_cmd (char *args, int from_tty)
14796{
14797}
14798
1f3b5d1b
PP
14799/* Invalidate last known value of any hardware watchpoint if
14800 the memory which that value represents has been written to by
14801 GDB itself. */
14802
14803static void
8de0566d
YQ
14804invalidate_bp_value_on_memory_change (struct inferior *inferior,
14805 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14806 const bfd_byte *data)
14807{
14808 struct breakpoint *bp;
14809
14810 ALL_BREAKPOINTS (bp)
14811 if (bp->enable_state == bp_enabled
3a5c3e22 14812 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14813 {
3a5c3e22 14814 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14815
3a5c3e22
PA
14816 if (wp->val_valid && wp->val)
14817 {
14818 struct bp_location *loc;
14819
14820 for (loc = bp->loc; loc != NULL; loc = loc->next)
14821 if (loc->loc_type == bp_loc_hardware_watchpoint
14822 && loc->address + loc->length > addr
14823 && addr + len > loc->address)
14824 {
14825 value_free (wp->val);
14826 wp->val = NULL;
14827 wp->val_valid = 0;
14828 }
14829 }
1f3b5d1b
PP
14830 }
14831}
14832
8181d85f
DJ
14833/* Create and insert a breakpoint for software single step. */
14834
14835void
6c95b8df 14836insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14837 struct address_space *aspace,
14838 CORE_ADDR next_pc)
8181d85f 14839{
7c16b83e
PA
14840 struct thread_info *tp = inferior_thread ();
14841 struct symtab_and_line sal;
14842 CORE_ADDR pc = next_pc;
8181d85f 14843
34b7e8a6
PA
14844 if (tp->control.single_step_breakpoints == NULL)
14845 {
14846 tp->control.single_step_breakpoints
14847 = new_single_step_breakpoint (tp->num, gdbarch);
14848 }
8181d85f 14849
7c16b83e
PA
14850 sal = find_pc_line (pc, 0);
14851 sal.pc = pc;
14852 sal.section = find_pc_overlay (pc);
14853 sal.explicit_pc = 1;
34b7e8a6 14854 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14855
7c16b83e 14856 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14857}
14858
34b7e8a6 14859/* See breakpoint.h. */
f02253f1
HZ
14860
14861int
7c16b83e
PA
14862breakpoint_has_location_inserted_here (struct breakpoint *bp,
14863 struct address_space *aspace,
14864 CORE_ADDR pc)
1aafd4da 14865{
7c16b83e 14866 struct bp_location *loc;
1aafd4da 14867
7c16b83e
PA
14868 for (loc = bp->loc; loc != NULL; loc = loc->next)
14869 if (loc->inserted
14870 && breakpoint_location_address_match (loc, aspace, pc))
14871 return 1;
1aafd4da 14872
7c16b83e 14873 return 0;
ef370185
JB
14874}
14875
14876/* Check whether a software single-step breakpoint is inserted at
14877 PC. */
14878
14879int
14880single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14881 CORE_ADDR pc)
14882{
34b7e8a6
PA
14883 struct breakpoint *bpt;
14884
14885 ALL_BREAKPOINTS (bpt)
14886 {
14887 if (bpt->type == bp_single_step
14888 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14889 return 1;
14890 }
14891 return 0;
1aafd4da
UW
14892}
14893
1042e4c0
SS
14894/* Tracepoint-specific operations. */
14895
14896/* Set tracepoint count to NUM. */
14897static void
14898set_tracepoint_count (int num)
14899{
14900 tracepoint_count = num;
4fa62494 14901 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14902}
14903
70221824 14904static void
1042e4c0
SS
14905trace_command (char *arg, int from_tty)
14906{
55aa24fb
SDJ
14907 struct breakpoint_ops *ops;
14908 const char *arg_cp = arg;
14909
14910 if (arg && probe_linespec_to_ops (&arg_cp))
14911 ops = &tracepoint_probe_breakpoint_ops;
14912 else
14913 ops = &tracepoint_breakpoint_ops;
14914
558a9d82
YQ
14915 create_breakpoint (get_current_arch (),
14916 arg,
14917 NULL, 0, NULL, 1 /* parse arg */,
14918 0 /* tempflag */,
14919 bp_tracepoint /* type_wanted */,
14920 0 /* Ignore count */,
14921 pending_break_support,
14922 ops,
14923 from_tty,
14924 1 /* enabled */,
14925 0 /* internal */, 0);
1042e4c0
SS
14926}
14927
70221824 14928static void
7a697b8d
SS
14929ftrace_command (char *arg, int from_tty)
14930{
558a9d82
YQ
14931 create_breakpoint (get_current_arch (),
14932 arg,
14933 NULL, 0, NULL, 1 /* parse arg */,
14934 0 /* tempflag */,
14935 bp_fast_tracepoint /* type_wanted */,
14936 0 /* Ignore count */,
14937 pending_break_support,
14938 &tracepoint_breakpoint_ops,
14939 from_tty,
14940 1 /* enabled */,
14941 0 /* internal */, 0);
0fb4aa4b
PA
14942}
14943
14944/* strace command implementation. Creates a static tracepoint. */
14945
70221824 14946static void
0fb4aa4b
PA
14947strace_command (char *arg, int from_tty)
14948{
983af33b
SDJ
14949 struct breakpoint_ops *ops;
14950
14951 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14952 or with a normal static tracepoint. */
61012eef 14953 if (arg && startswith (arg, "-m") && isspace (arg[2]))
983af33b
SDJ
14954 ops = &strace_marker_breakpoint_ops;
14955 else
14956 ops = &tracepoint_breakpoint_ops;
14957
558a9d82
YQ
14958 create_breakpoint (get_current_arch (),
14959 arg,
14960 NULL, 0, NULL, 1 /* parse arg */,
14961 0 /* tempflag */,
14962 bp_static_tracepoint /* type_wanted */,
14963 0 /* Ignore count */,
14964 pending_break_support,
14965 ops,
14966 from_tty,
14967 1 /* enabled */,
14968 0 /* internal */, 0);
7a697b8d
SS
14969}
14970
409873ef
SS
14971/* Set up a fake reader function that gets command lines from a linked
14972 list that was acquired during tracepoint uploading. */
14973
14974static struct uploaded_tp *this_utp;
3149d8c1 14975static int next_cmd;
409873ef
SS
14976
14977static char *
14978read_uploaded_action (void)
14979{
14980 char *rslt;
14981
3149d8c1 14982 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 14983
3149d8c1 14984 next_cmd++;
409873ef
SS
14985
14986 return rslt;
14987}
14988
00bf0b85
SS
14989/* Given information about a tracepoint as recorded on a target (which
14990 can be either a live system or a trace file), attempt to create an
14991 equivalent GDB tracepoint. This is not a reliable process, since
14992 the target does not necessarily have all the information used when
14993 the tracepoint was originally defined. */
14994
d9b3f62e 14995struct tracepoint *
00bf0b85 14996create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14997{
409873ef 14998 char *addr_str, small_buf[100];
d9b3f62e 14999 struct tracepoint *tp;
fd9b8c24 15000
409873ef
SS
15001 if (utp->at_string)
15002 addr_str = utp->at_string;
15003 else
15004 {
15005 /* In the absence of a source location, fall back to raw
15006 address. Since there is no way to confirm that the address
15007 means the same thing as when the trace was started, warn the
15008 user. */
3e43a32a
MS
15009 warning (_("Uploaded tracepoint %d has no "
15010 "source location, using raw address"),
409873ef 15011 utp->number);
8c042590 15012 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15013 addr_str = small_buf;
15014 }
15015
15016 /* There's not much we can do with a sequence of bytecodes. */
15017 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15018 warning (_("Uploaded tracepoint %d condition "
15019 "has no source form, ignoring it"),
409873ef 15020 utp->number);
d5551862 15021
8cdf0e15 15022 if (!create_breakpoint (get_current_arch (),
409873ef 15023 addr_str,
e7e0cddf
SS
15024 utp->cond_string, -1, NULL,
15025 0 /* parse cond/thread */,
8cdf0e15 15026 0 /* tempflag */,
0fb4aa4b 15027 utp->type /* type_wanted */,
8cdf0e15
VP
15028 0 /* Ignore count */,
15029 pending_break_support,
348d480f 15030 &tracepoint_breakpoint_ops,
8cdf0e15 15031 0 /* from_tty */,
84f4c1fe 15032 utp->enabled /* enabled */,
44f238bb
PA
15033 0 /* internal */,
15034 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15035 return NULL;
15036
409873ef 15037 /* Get the tracepoint we just created. */
fd9b8c24
PA
15038 tp = get_tracepoint (tracepoint_count);
15039 gdb_assert (tp != NULL);
d5551862 15040
00bf0b85
SS
15041 if (utp->pass > 0)
15042 {
8c042590
PM
15043 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15044 tp->base.number);
00bf0b85 15045
409873ef 15046 trace_pass_command (small_buf, 0);
00bf0b85
SS
15047 }
15048
409873ef
SS
15049 /* If we have uploaded versions of the original commands, set up a
15050 special-purpose "reader" function and call the usual command line
15051 reader, then pass the result to the breakpoint command-setting
15052 function. */
3149d8c1 15053 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15054 {
409873ef 15055 struct command_line *cmd_list;
00bf0b85 15056
409873ef 15057 this_utp = utp;
3149d8c1 15058 next_cmd = 0;
d5551862 15059
409873ef
SS
15060 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15061
d9b3f62e 15062 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15063 }
3149d8c1
SS
15064 else if (!VEC_empty (char_ptr, utp->actions)
15065 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15066 warning (_("Uploaded tracepoint %d actions "
15067 "have no source form, ignoring them"),
409873ef 15068 utp->number);
00bf0b85 15069
f196051f
SS
15070 /* Copy any status information that might be available. */
15071 tp->base.hit_count = utp->hit_count;
15072 tp->traceframe_usage = utp->traceframe_usage;
15073
00bf0b85 15074 return tp;
d9b3f62e 15075}
00bf0b85 15076
1042e4c0
SS
15077/* Print information on tracepoint number TPNUM_EXP, or all if
15078 omitted. */
15079
15080static void
e5a67952 15081tracepoints_info (char *args, int from_tty)
1042e4c0 15082{
79a45e25 15083 struct ui_out *uiout = current_uiout;
e5a67952 15084 int num_printed;
1042e4c0 15085
e5a67952 15086 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15087
15088 if (num_printed == 0)
1042e4c0 15089 {
e5a67952 15090 if (args == NULL || *args == '\0')
d77f58be
SS
15091 ui_out_message (uiout, 0, "No tracepoints.\n");
15092 else
e5a67952 15093 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15094 }
ad443146
SS
15095
15096 default_collect_info ();
1042e4c0
SS
15097}
15098
4a64f543 15099/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15100 Not supported by all targets. */
15101static void
15102enable_trace_command (char *args, int from_tty)
15103{
15104 enable_command (args, from_tty);
15105}
15106
4a64f543 15107/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15108 Not supported by all targets. */
15109static void
15110disable_trace_command (char *args, int from_tty)
15111{
15112 disable_command (args, from_tty);
15113}
15114
4a64f543 15115/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15116static void
15117delete_trace_command (char *arg, int from_tty)
15118{
35df4500 15119 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15120
15121 dont_repeat ();
15122
15123 if (arg == 0)
15124 {
15125 int breaks_to_delete = 0;
15126
15127 /* Delete all breakpoints if no argument.
15128 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15129 have to be deleted with an explicit breakpoint number
15130 argument. */
1042e4c0 15131 ALL_TRACEPOINTS (b)
46c6471b 15132 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15133 {
15134 breaks_to_delete = 1;
15135 break;
15136 }
1042e4c0
SS
15137
15138 /* Ask user only if there are some breakpoints to delete. */
15139 if (!from_tty
15140 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15141 {
35df4500 15142 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15143 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15144 delete_breakpoint (b);
1042e4c0
SS
15145 }
15146 }
15147 else
51be5b68 15148 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15149}
15150
197f0a60
TT
15151/* Helper function for trace_pass_command. */
15152
15153static void
d9b3f62e 15154trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15155{
d9b3f62e 15156 tp->pass_count = count;
6f6484cd 15157 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15158 if (from_tty)
15159 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15160 tp->base.number, count);
197f0a60
TT
15161}
15162
1042e4c0
SS
15163/* Set passcount for tracepoint.
15164
15165 First command argument is passcount, second is tracepoint number.
15166 If tracepoint number omitted, apply to most recently defined.
15167 Also accepts special argument "all". */
15168
15169static void
15170trace_pass_command (char *args, int from_tty)
15171{
d9b3f62e 15172 struct tracepoint *t1;
1042e4c0 15173 unsigned int count;
1042e4c0
SS
15174
15175 if (args == 0 || *args == 0)
3e43a32a
MS
15176 error (_("passcount command requires an "
15177 "argument (count + optional TP num)"));
1042e4c0 15178
4a64f543 15179 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15180
529480d0 15181 args = skip_spaces (args);
1042e4c0
SS
15182 if (*args && strncasecmp (args, "all", 3) == 0)
15183 {
d9b3f62e
PA
15184 struct breakpoint *b;
15185
1042e4c0 15186 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15187 if (*args)
15188 error (_("Junk at end of arguments."));
1042e4c0 15189
d9b3f62e 15190 ALL_TRACEPOINTS (b)
197f0a60 15191 {
d9b3f62e 15192 t1 = (struct tracepoint *) b;
197f0a60
TT
15193 trace_pass_set_count (t1, count, from_tty);
15194 }
15195 }
15196 else if (*args == '\0')
1042e4c0 15197 {
5fa1d40e 15198 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15199 if (t1)
197f0a60
TT
15200 trace_pass_set_count (t1, count, from_tty);
15201 }
15202 else
15203 {
15204 struct get_number_or_range_state state;
15205
15206 init_number_or_range (&state, args);
15207 while (!state.finished)
1042e4c0 15208 {
5fa1d40e 15209 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15210 if (t1)
15211 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15212 }
15213 }
1042e4c0
SS
15214}
15215
d9b3f62e 15216struct tracepoint *
1042e4c0
SS
15217get_tracepoint (int num)
15218{
15219 struct breakpoint *t;
15220
15221 ALL_TRACEPOINTS (t)
15222 if (t->number == num)
d9b3f62e 15223 return (struct tracepoint *) t;
1042e4c0
SS
15224
15225 return NULL;
15226}
15227
d5551862
SS
15228/* Find the tracepoint with the given target-side number (which may be
15229 different from the tracepoint number after disconnecting and
15230 reconnecting). */
15231
d9b3f62e 15232struct tracepoint *
d5551862
SS
15233get_tracepoint_by_number_on_target (int num)
15234{
d9b3f62e 15235 struct breakpoint *b;
d5551862 15236
d9b3f62e
PA
15237 ALL_TRACEPOINTS (b)
15238 {
15239 struct tracepoint *t = (struct tracepoint *) b;
15240
15241 if (t->number_on_target == num)
15242 return t;
15243 }
d5551862
SS
15244
15245 return NULL;
15246}
15247
1042e4c0 15248/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15249 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15250 If the argument is missing, the most recent tracepoint
15251 (tracepoint_count) is returned. */
15252
d9b3f62e 15253struct tracepoint *
197f0a60 15254get_tracepoint_by_number (char **arg,
5fa1d40e 15255 struct get_number_or_range_state *state)
1042e4c0 15256{
1042e4c0
SS
15257 struct breakpoint *t;
15258 int tpnum;
15259 char *instring = arg == NULL ? NULL : *arg;
15260
197f0a60
TT
15261 if (state)
15262 {
15263 gdb_assert (!state->finished);
15264 tpnum = get_number_or_range (state);
15265 }
15266 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15267 tpnum = tracepoint_count;
1042e4c0 15268 else
197f0a60 15269 tpnum = get_number (arg);
1042e4c0
SS
15270
15271 if (tpnum <= 0)
15272 {
15273 if (instring && *instring)
15274 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15275 instring);
15276 else
5fa1d40e 15277 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15278 return NULL;
15279 }
15280
15281 ALL_TRACEPOINTS (t)
15282 if (t->number == tpnum)
15283 {
d9b3f62e 15284 return (struct tracepoint *) t;
1042e4c0
SS
15285 }
15286
1042e4c0
SS
15287 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15288 return NULL;
15289}
15290
d9b3f62e
PA
15291void
15292print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15293{
15294 if (b->thread != -1)
15295 fprintf_unfiltered (fp, " thread %d", b->thread);
15296
15297 if (b->task != 0)
15298 fprintf_unfiltered (fp, " task %d", b->task);
15299
15300 fprintf_unfiltered (fp, "\n");
15301}
15302
6149aea9
PA
15303/* Save information on user settable breakpoints (watchpoints, etc) to
15304 a new script file named FILENAME. If FILTER is non-NULL, call it
15305 on each breakpoint and only include the ones for which it returns
15306 non-zero. */
15307
1042e4c0 15308static void
6149aea9
PA
15309save_breakpoints (char *filename, int from_tty,
15310 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15311{
15312 struct breakpoint *tp;
6149aea9 15313 int any = 0;
1042e4c0 15314 struct cleanup *cleanup;
a7bdde9e 15315 struct ui_file *fp;
6149aea9 15316 int extra_trace_bits = 0;
1042e4c0 15317
6149aea9
PA
15318 if (filename == 0 || *filename == 0)
15319 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15320
15321 /* See if we have anything to save. */
6149aea9 15322 ALL_BREAKPOINTS (tp)
1042e4c0 15323 {
6149aea9 15324 /* Skip internal and momentary breakpoints. */
09d682a4 15325 if (!user_breakpoint_p (tp))
6149aea9
PA
15326 continue;
15327
15328 /* If we have a filter, only save the breakpoints it accepts. */
15329 if (filter && !filter (tp))
15330 continue;
15331
15332 any = 1;
15333
15334 if (is_tracepoint (tp))
15335 {
15336 extra_trace_bits = 1;
15337
15338 /* We can stop searching. */
15339 break;
15340 }
1042e4c0 15341 }
6149aea9
PA
15342
15343 if (!any)
1042e4c0 15344 {
6149aea9 15345 warning (_("Nothing to save."));
1042e4c0
SS
15346 return;
15347 }
15348
c718be47
PA
15349 filename = tilde_expand (filename);
15350 cleanup = make_cleanup (xfree, filename);
15351 fp = gdb_fopen (filename, "w");
059fb39f 15352 if (!fp)
6149aea9
PA
15353 error (_("Unable to open file '%s' for saving (%s)"),
15354 filename, safe_strerror (errno));
a7bdde9e 15355 make_cleanup_ui_file_delete (fp);
8bf6485c 15356
6149aea9
PA
15357 if (extra_trace_bits)
15358 save_trace_state_variables (fp);
8bf6485c 15359
6149aea9 15360 ALL_BREAKPOINTS (tp)
1042e4c0 15361 {
6149aea9 15362 /* Skip internal and momentary breakpoints. */
09d682a4 15363 if (!user_breakpoint_p (tp))
6149aea9 15364 continue;
8bf6485c 15365
6149aea9
PA
15366 /* If we have a filter, only save the breakpoints it accepts. */
15367 if (filter && !filter (tp))
15368 continue;
15369
348d480f 15370 tp->ops->print_recreate (tp, fp);
1042e4c0 15371
6149aea9
PA
15372 /* Note, we can't rely on tp->number for anything, as we can't
15373 assume the recreated breakpoint numbers will match. Use $bpnum
15374 instead. */
15375
15376 if (tp->cond_string)
15377 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15378
15379 if (tp->ignore_count)
15380 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15381
2d9442cc 15382 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15383 {
6c63c96a 15384 struct gdb_exception exception;
a7bdde9e 15385
6149aea9 15386 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15387
79a45e25 15388 ui_out_redirect (current_uiout, fp);
492d29ea 15389 TRY
1042e4c0 15390 {
79a45e25 15391 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15392 }
492d29ea
PA
15393 CATCH (ex, RETURN_MASK_ALL)
15394 {
6c63c96a 15395 ui_out_redirect (current_uiout, NULL);
492d29ea
PA
15396 throw_exception (ex);
15397 }
15398 END_CATCH
1042e4c0 15399
6c63c96a 15400 ui_out_redirect (current_uiout, NULL);
a7bdde9e 15401 fprintf_unfiltered (fp, " end\n");
1042e4c0 15402 }
6149aea9
PA
15403
15404 if (tp->enable_state == bp_disabled)
99894e11 15405 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15406
15407 /* If this is a multi-location breakpoint, check if the locations
15408 should be individually disabled. Watchpoint locations are
15409 special, and not user visible. */
15410 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15411 {
15412 struct bp_location *loc;
15413 int n = 1;
15414
15415 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15416 if (!loc->enabled)
15417 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15418 }
1042e4c0 15419 }
8bf6485c 15420
6149aea9 15421 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15422 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15423
1042e4c0 15424 if (from_tty)
6149aea9 15425 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15426 do_cleanups (cleanup);
6149aea9
PA
15427}
15428
15429/* The `save breakpoints' command. */
15430
15431static void
15432save_breakpoints_command (char *args, int from_tty)
15433{
15434 save_breakpoints (args, from_tty, NULL);
15435}
15436
15437/* The `save tracepoints' command. */
15438
15439static void
15440save_tracepoints_command (char *args, int from_tty)
15441{
15442 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15443}
15444
15445/* Create a vector of all tracepoints. */
15446
15447VEC(breakpoint_p) *
eeae04df 15448all_tracepoints (void)
1042e4c0
SS
15449{
15450 VEC(breakpoint_p) *tp_vec = 0;
15451 struct breakpoint *tp;
15452
15453 ALL_TRACEPOINTS (tp)
15454 {
15455 VEC_safe_push (breakpoint_p, tp_vec, tp);
15456 }
15457
15458 return tp_vec;
15459}
15460
c906108c 15461\f
4a64f543
MS
15462/* This help string is used for the break, hbreak, tbreak and thbreak
15463 commands. It is defined as a macro to prevent duplication.
15464 COMMAND should be a string constant containing the name of the
15465 command. */
31e2b00f 15466#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15467command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15468PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15469probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15470guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15471`-probe-dtrace' (for a DTrace probe).\n\
31e2b00f
AS
15472LOCATION may be a line number, function name, or \"*\" and an address.\n\
15473If a line number is specified, break at start of code for that line.\n\
15474If a function is specified, break at start of code for that function.\n\
15475If an address is specified, break at that exact address.\n\
dc10affe
PA
15476With no LOCATION, uses current execution address of the selected\n\
15477stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15478\n\
15479THREADNUM is the number from \"info threads\".\n\
15480CONDITION is a boolean expression.\n\
15481\n\
d41c0fc8
PA
15482Multiple breakpoints at one place are permitted, and useful if their\n\
15483conditions are different.\n\
31e2b00f
AS
15484\n\
15485Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15486
44feb3ce
TT
15487/* List of subcommands for "catch". */
15488static struct cmd_list_element *catch_cmdlist;
15489
15490/* List of subcommands for "tcatch". */
15491static struct cmd_list_element *tcatch_cmdlist;
15492
9ac4176b 15493void
44feb3ce 15494add_catch_command (char *name, char *docstring,
82ae6c8d 15495 cmd_sfunc_ftype *sfunc,
625e8578 15496 completer_ftype *completer,
44feb3ce
TT
15497 void *user_data_catch,
15498 void *user_data_tcatch)
15499{
15500 struct cmd_list_element *command;
15501
15502 command = add_cmd (name, class_breakpoint, NULL, docstring,
15503 &catch_cmdlist);
15504 set_cmd_sfunc (command, sfunc);
15505 set_cmd_context (command, user_data_catch);
a96d9b2e 15506 set_cmd_completer (command, completer);
44feb3ce
TT
15507
15508 command = add_cmd (name, class_breakpoint, NULL, docstring,
15509 &tcatch_cmdlist);
15510 set_cmd_sfunc (command, sfunc);
15511 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15512 set_cmd_completer (command, completer);
44feb3ce
TT
15513}
15514
6149aea9
PA
15515static void
15516save_command (char *arg, int from_tty)
15517{
3e43a32a
MS
15518 printf_unfiltered (_("\"save\" must be followed by "
15519 "the name of a save subcommand.\n"));
635c7e8a 15520 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15521}
15522
84f4c1fe
PM
15523struct breakpoint *
15524iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15525 void *data)
15526{
35df4500 15527 struct breakpoint *b, *b_tmp;
84f4c1fe 15528
35df4500 15529 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15530 {
15531 if ((*callback) (b, data))
15532 return b;
15533 }
15534
15535 return NULL;
15536}
15537
0574c78f
GB
15538/* Zero if any of the breakpoint's locations could be a location where
15539 functions have been inlined, nonzero otherwise. */
15540
15541static int
15542is_non_inline_function (struct breakpoint *b)
15543{
15544 /* The shared library event breakpoint is set on the address of a
15545 non-inline function. */
15546 if (b->type == bp_shlib_event)
15547 return 1;
15548
15549 return 0;
15550}
15551
15552/* Nonzero if the specified PC cannot be a location where functions
15553 have been inlined. */
15554
15555int
09ac7c10
TT
15556pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15557 const struct target_waitstatus *ws)
0574c78f
GB
15558{
15559 struct breakpoint *b;
15560 struct bp_location *bl;
15561
15562 ALL_BREAKPOINTS (b)
15563 {
15564 if (!is_non_inline_function (b))
15565 continue;
15566
15567 for (bl = b->loc; bl != NULL; bl = bl->next)
15568 {
15569 if (!bl->shlib_disabled
09ac7c10 15570 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15571 return 1;
15572 }
15573 }
15574
15575 return 0;
15576}
15577
2f202fde
JK
15578/* Remove any references to OBJFILE which is going to be freed. */
15579
15580void
15581breakpoint_free_objfile (struct objfile *objfile)
15582{
15583 struct bp_location **locp, *loc;
15584
15585 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15586 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15587 loc->symtab = NULL;
15588}
15589
2060206e
PA
15590void
15591initialize_breakpoint_ops (void)
15592{
15593 static int initialized = 0;
15594
15595 struct breakpoint_ops *ops;
15596
15597 if (initialized)
15598 return;
15599 initialized = 1;
15600
15601 /* The breakpoint_ops structure to be inherit by all kinds of
15602 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15603 internal and momentary breakpoints, etc.). */
15604 ops = &bkpt_base_breakpoint_ops;
15605 *ops = base_breakpoint_ops;
15606 ops->re_set = bkpt_re_set;
15607 ops->insert_location = bkpt_insert_location;
15608 ops->remove_location = bkpt_remove_location;
15609 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
15610 ops->create_sals_from_address = bkpt_create_sals_from_address;
15611 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15612 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
15613
15614 /* The breakpoint_ops structure to be used in regular breakpoints. */
15615 ops = &bkpt_breakpoint_ops;
15616 *ops = bkpt_base_breakpoint_ops;
15617 ops->re_set = bkpt_re_set;
15618 ops->resources_needed = bkpt_resources_needed;
15619 ops->print_it = bkpt_print_it;
15620 ops->print_mention = bkpt_print_mention;
15621 ops->print_recreate = bkpt_print_recreate;
15622
15623 /* Ranged breakpoints. */
15624 ops = &ranged_breakpoint_ops;
15625 *ops = bkpt_breakpoint_ops;
15626 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15627 ops->resources_needed = resources_needed_ranged_breakpoint;
15628 ops->print_it = print_it_ranged_breakpoint;
15629 ops->print_one = print_one_ranged_breakpoint;
15630 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15631 ops->print_mention = print_mention_ranged_breakpoint;
15632 ops->print_recreate = print_recreate_ranged_breakpoint;
15633
15634 /* Internal breakpoints. */
15635 ops = &internal_breakpoint_ops;
15636 *ops = bkpt_base_breakpoint_ops;
15637 ops->re_set = internal_bkpt_re_set;
15638 ops->check_status = internal_bkpt_check_status;
15639 ops->print_it = internal_bkpt_print_it;
15640 ops->print_mention = internal_bkpt_print_mention;
15641
15642 /* Momentary breakpoints. */
15643 ops = &momentary_breakpoint_ops;
15644 *ops = bkpt_base_breakpoint_ops;
15645 ops->re_set = momentary_bkpt_re_set;
15646 ops->check_status = momentary_bkpt_check_status;
15647 ops->print_it = momentary_bkpt_print_it;
15648 ops->print_mention = momentary_bkpt_print_mention;
15649
e2e4d78b
JK
15650 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15651 ops = &longjmp_breakpoint_ops;
15652 *ops = momentary_breakpoint_ops;
15653 ops->dtor = longjmp_bkpt_dtor;
15654
55aa24fb
SDJ
15655 /* Probe breakpoints. */
15656 ops = &bkpt_probe_breakpoint_ops;
15657 *ops = bkpt_breakpoint_ops;
15658 ops->insert_location = bkpt_probe_insert_location;
15659 ops->remove_location = bkpt_probe_remove_location;
15660 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15661 ops->decode_linespec = bkpt_probe_decode_linespec;
15662
2060206e
PA
15663 /* Watchpoints. */
15664 ops = &watchpoint_breakpoint_ops;
15665 *ops = base_breakpoint_ops;
3a5c3e22 15666 ops->dtor = dtor_watchpoint;
2060206e
PA
15667 ops->re_set = re_set_watchpoint;
15668 ops->insert_location = insert_watchpoint;
15669 ops->remove_location = remove_watchpoint;
15670 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15671 ops->check_status = check_status_watchpoint;
15672 ops->resources_needed = resources_needed_watchpoint;
15673 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15674 ops->print_it = print_it_watchpoint;
15675 ops->print_mention = print_mention_watchpoint;
15676 ops->print_recreate = print_recreate_watchpoint;
427cd150 15677 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15678
15679 /* Masked watchpoints. */
15680 ops = &masked_watchpoint_breakpoint_ops;
15681 *ops = watchpoint_breakpoint_ops;
15682 ops->insert_location = insert_masked_watchpoint;
15683 ops->remove_location = remove_masked_watchpoint;
15684 ops->resources_needed = resources_needed_masked_watchpoint;
15685 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15686 ops->print_it = print_it_masked_watchpoint;
15687 ops->print_one_detail = print_one_detail_masked_watchpoint;
15688 ops->print_mention = print_mention_masked_watchpoint;
15689 ops->print_recreate = print_recreate_masked_watchpoint;
15690
15691 /* Tracepoints. */
15692 ops = &tracepoint_breakpoint_ops;
15693 *ops = base_breakpoint_ops;
15694 ops->re_set = tracepoint_re_set;
15695 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15696 ops->print_one_detail = tracepoint_print_one_detail;
15697 ops->print_mention = tracepoint_print_mention;
15698 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
15699 ops->create_sals_from_address = tracepoint_create_sals_from_address;
15700 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15701 ops->decode_linespec = tracepoint_decode_linespec;
15702
55aa24fb
SDJ
15703 /* Probe tracepoints. */
15704 ops = &tracepoint_probe_breakpoint_ops;
15705 *ops = tracepoint_breakpoint_ops;
15706 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15707 ops->decode_linespec = tracepoint_probe_decode_linespec;
15708
983af33b
SDJ
15709 /* Static tracepoints with marker (`-m'). */
15710 ops = &strace_marker_breakpoint_ops;
15711 *ops = tracepoint_breakpoint_ops;
15712 ops->create_sals_from_address = strace_marker_create_sals_from_address;
15713 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15714 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
15715
15716 /* Fork catchpoints. */
15717 ops = &catch_fork_breakpoint_ops;
15718 *ops = base_breakpoint_ops;
15719 ops->insert_location = insert_catch_fork;
15720 ops->remove_location = remove_catch_fork;
15721 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15722 ops->print_it = print_it_catch_fork;
15723 ops->print_one = print_one_catch_fork;
15724 ops->print_mention = print_mention_catch_fork;
15725 ops->print_recreate = print_recreate_catch_fork;
15726
15727 /* Vfork catchpoints. */
15728 ops = &catch_vfork_breakpoint_ops;
15729 *ops = base_breakpoint_ops;
15730 ops->insert_location = insert_catch_vfork;
15731 ops->remove_location = remove_catch_vfork;
15732 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15733 ops->print_it = print_it_catch_vfork;
15734 ops->print_one = print_one_catch_vfork;
15735 ops->print_mention = print_mention_catch_vfork;
15736 ops->print_recreate = print_recreate_catch_vfork;
15737
15738 /* Exec catchpoints. */
15739 ops = &catch_exec_breakpoint_ops;
15740 *ops = base_breakpoint_ops;
15741 ops->dtor = dtor_catch_exec;
15742 ops->insert_location = insert_catch_exec;
15743 ops->remove_location = remove_catch_exec;
15744 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15745 ops->print_it = print_it_catch_exec;
15746 ops->print_one = print_one_catch_exec;
15747 ops->print_mention = print_mention_catch_exec;
15748 ops->print_recreate = print_recreate_catch_exec;
15749
edcc5120
TT
15750 /* Solib-related catchpoints. */
15751 ops = &catch_solib_breakpoint_ops;
15752 *ops = base_breakpoint_ops;
15753 ops->dtor = dtor_catch_solib;
15754 ops->insert_location = insert_catch_solib;
15755 ops->remove_location = remove_catch_solib;
15756 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15757 ops->check_status = check_status_catch_solib;
15758 ops->print_it = print_it_catch_solib;
15759 ops->print_one = print_one_catch_solib;
15760 ops->print_mention = print_mention_catch_solib;
15761 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15762
15763 ops = &dprintf_breakpoint_ops;
15764 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15765 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15766 ops->resources_needed = bkpt_resources_needed;
15767 ops->print_it = bkpt_print_it;
15768 ops->print_mention = bkpt_print_mention;
2d9442cc 15769 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15770 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15771 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15772}
15773
8bfd80db
YQ
15774/* Chain containing all defined "enable breakpoint" subcommands. */
15775
15776static struct cmd_list_element *enablebreaklist = NULL;
15777
c906108c 15778void
fba45db2 15779_initialize_breakpoint (void)
c906108c
SS
15780{
15781 struct cmd_list_element *c;
15782
2060206e
PA
15783 initialize_breakpoint_ops ();
15784
84acb35a 15785 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15786 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15787 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15788
55aa24fb
SDJ
15789 breakpoint_objfile_key
15790 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15791
c906108c
SS
15792 breakpoint_chain = 0;
15793 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15794 before a breakpoint is set. */
15795 breakpoint_count = 0;
15796
1042e4c0
SS
15797 tracepoint_count = 0;
15798
1bedd215
AC
15799 add_com ("ignore", class_breakpoint, ignore_command, _("\
15800Set ignore-count of breakpoint number N to COUNT.\n\
15801Usage is `ignore N COUNT'."));
c906108c 15802 if (xdb_commands)
c5aa993b 15803 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 15804
1bedd215
AC
15805 add_com ("commands", class_breakpoint, commands_command, _("\
15806Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
15807Give breakpoint number as argument after \"commands\".\n\
15808With no argument, the targeted breakpoint is the last one set.\n\
15809The commands themselves follow starting on the next line.\n\
15810Type a line containing \"end\" to indicate the end of them.\n\
15811Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15812then no output is printed when it is hit, except what the commands print."));
c906108c 15813
d55637df 15814 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15815Specify breakpoint number N to break only if COND is true.\n\
c906108c 15816Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15817expression to be evaluated whenever breakpoint N is reached."));
d55637df 15818 set_cmd_completer (c, condition_completer);
c906108c 15819
1bedd215 15820 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15821Set a temporary breakpoint.\n\
c906108c
SS
15822Like \"break\" except the breakpoint is only temporary,\n\
15823so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15824by using \"enable delete\" on the breakpoint number.\n\
15825\n"
15826BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15827 set_cmd_completer (c, location_completer);
c94fdfd0 15828
1bedd215 15829 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15830Set a hardware assisted breakpoint.\n\
c906108c 15831Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15832some target hardware may not have this support.\n\
15833\n"
15834BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15835 set_cmd_completer (c, location_completer);
c906108c 15836
1bedd215 15837 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15838Set a temporary hardware assisted breakpoint.\n\
c906108c 15839Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15840so it will be deleted when hit.\n\
15841\n"
15842BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15843 set_cmd_completer (c, location_completer);
c906108c 15844
1bedd215
AC
15845 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15846Enable some breakpoints.\n\
c906108c
SS
15847Give breakpoint numbers (separated by spaces) as arguments.\n\
15848With no subcommand, breakpoints are enabled until you command otherwise.\n\
15849This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15850With a subcommand you can enable temporarily."),
c906108c
SS
15851 &enablelist, "enable ", 1, &cmdlist);
15852 if (xdb_commands)
1bedd215
AC
15853 add_com ("ab", class_breakpoint, enable_command, _("\
15854Enable some breakpoints.\n\
c906108c
SS
15855Give breakpoint numbers (separated by spaces) as arguments.\n\
15856With no subcommand, breakpoints are enabled until you command otherwise.\n\
15857This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15858With a subcommand you can enable temporarily."));
c906108c
SS
15859
15860 add_com_alias ("en", "enable", class_breakpoint, 1);
15861
84951ab5 15862 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15863Enable some breakpoints.\n\
c906108c
SS
15864Give breakpoint numbers (separated by spaces) as arguments.\n\
15865This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15866May be abbreviated to simply \"enable\".\n"),
c5aa993b 15867 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15868
1a966eab
AC
15869 add_cmd ("once", no_class, enable_once_command, _("\
15870Enable breakpoints for one hit. Give breakpoint numbers.\n\
15871If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15872 &enablebreaklist);
15873
1a966eab
AC
15874 add_cmd ("delete", no_class, enable_delete_command, _("\
15875Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15876If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15877 &enablebreaklist);
15878
816338b5
SS
15879 add_cmd ("count", no_class, enable_count_command, _("\
15880Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15881If a breakpoint is hit while enabled in this fashion,\n\
15882the count is decremented; when it reaches zero, the breakpoint is disabled."),
15883 &enablebreaklist);
15884
1a966eab
AC
15885 add_cmd ("delete", no_class, enable_delete_command, _("\
15886Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15887If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15888 &enablelist);
15889
1a966eab
AC
15890 add_cmd ("once", no_class, enable_once_command, _("\
15891Enable breakpoints for one hit. Give breakpoint numbers.\n\
15892If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15893 &enablelist);
15894
15895 add_cmd ("count", no_class, enable_count_command, _("\
15896Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15897If a breakpoint is hit while enabled in this fashion,\n\
15898the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15899 &enablelist);
15900
1bedd215
AC
15901 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15902Disable some breakpoints.\n\
c906108c
SS
15903Arguments are breakpoint numbers with spaces in between.\n\
15904To disable all breakpoints, give no argument.\n\
64b9b334 15905A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15906 &disablelist, "disable ", 1, &cmdlist);
15907 add_com_alias ("dis", "disable", class_breakpoint, 1);
15908 add_com_alias ("disa", "disable", class_breakpoint, 1);
15909 if (xdb_commands)
1bedd215
AC
15910 add_com ("sb", class_breakpoint, disable_command, _("\
15911Disable some breakpoints.\n\
c906108c
SS
15912Arguments are breakpoint numbers with spaces in between.\n\
15913To disable all breakpoints, give no argument.\n\
64b9b334 15914A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 15915
1a966eab
AC
15916 add_cmd ("breakpoints", class_alias, disable_command, _("\
15917Disable some breakpoints.\n\
c906108c
SS
15918Arguments are breakpoint numbers with spaces in between.\n\
15919To disable all breakpoints, give no argument.\n\
64b9b334 15920A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15921This command may be abbreviated \"disable\"."),
c906108c
SS
15922 &disablelist);
15923
1bedd215
AC
15924 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15925Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15926Arguments are breakpoint numbers with spaces in between.\n\
15927To delete all breakpoints, give no argument.\n\
15928\n\
15929Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15930The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15931 &deletelist, "delete ", 1, &cmdlist);
15932 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15933 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15934 if (xdb_commands)
1bedd215
AC
15935 add_com ("db", class_breakpoint, delete_command, _("\
15936Delete some breakpoints.\n\
c906108c 15937Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 15938To delete all breakpoints, give no argument.\n"));
c906108c 15939
1a966eab
AC
15940 add_cmd ("breakpoints", class_alias, delete_command, _("\
15941Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15942Arguments are breakpoint numbers with spaces in between.\n\
15943To delete all breakpoints, give no argument.\n\
1a966eab 15944This command may be abbreviated \"delete\"."),
c906108c
SS
15945 &deletelist);
15946
1bedd215
AC
15947 add_com ("clear", class_breakpoint, clear_command, _("\
15948Clear breakpoint at specified line or function.\n\
c906108c
SS
15949Argument may be line number, function name, or \"*\" and an address.\n\
15950If line number is specified, all breakpoints in that line are cleared.\n\
15951If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
15952If an address is specified, breakpoints at that address are cleared.\n\
15953\n\
15954With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
15955is executing in.\n\
15956\n\
1bedd215 15957See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15958 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15959
1bedd215 15960 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
15961Set breakpoint at specified line or function.\n"
15962BREAK_ARGS_HELP ("break")));
5ba2abeb 15963 set_cmd_completer (c, location_completer);
c94fdfd0 15964
c906108c
SS
15965 add_com_alias ("b", "break", class_run, 1);
15966 add_com_alias ("br", "break", class_run, 1);
15967 add_com_alias ("bre", "break", class_run, 1);
15968 add_com_alias ("brea", "break", class_run, 1);
15969
7681d515
PM
15970 if (xdb_commands)
15971 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
15972
15973 if (dbx_commands)
15974 {
1bedd215
AC
15975 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15976Break in function/address or break at a line in the current file."),
c5aa993b
JM
15977 &stoplist, "stop ", 1, &cmdlist);
15978 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15979 _("Break in function or address."), &stoplist);
c5aa993b 15980 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15981 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
15982 add_com ("status", class_info, breakpoints_info, _("\
15983Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15984The \"Type\" column indicates one of:\n\
15985\tbreakpoint - normal breakpoint\n\
15986\twatchpoint - watchpoint\n\
15987The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15988the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15989breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15990address and file/line number respectively.\n\
15991\n\
15992Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15993are set to the address of the last breakpoint listed unless the command\n\
15994is prefixed with \"server \".\n\n\
c906108c 15995Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15996breakpoint set."));
c906108c
SS
15997 }
15998
1bedd215 15999 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16000Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16001The \"Type\" column indicates one of:\n\
16002\tbreakpoint - normal breakpoint\n\
16003\twatchpoint - watchpoint\n\
16004The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16005the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16006breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16007address and file/line number respectively.\n\
16008\n\
16009Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16010are set to the address of the last breakpoint listed unless the command\n\
16011is prefixed with \"server \".\n\n\
c906108c 16012Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16013breakpoint set."));
c906108c 16014
6b04bdb7
MS
16015 add_info_alias ("b", "breakpoints", 1);
16016
c906108c 16017 if (xdb_commands)
1bedd215
AC
16018 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16019Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16020The \"Type\" column indicates one of:\n\
16021\tbreakpoint - normal breakpoint\n\
16022\twatchpoint - watchpoint\n\
16023The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16024the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16025breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16026address and file/line number respectively.\n\
16027\n\
16028Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16029are set to the address of the last breakpoint listed unless the command\n\
16030is prefixed with \"server \".\n\n\
c906108c 16031Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16032breakpoint set."));
c906108c 16033
1a966eab
AC
16034 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16035Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16036The \"Type\" column indicates one of:\n\
16037\tbreakpoint - normal breakpoint\n\
16038\twatchpoint - watchpoint\n\
16039\tlongjmp - internal breakpoint used to step through longjmp()\n\
16040\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16041\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16042\tfinish - internal breakpoint used by the \"finish\" command\n\
16043The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16044the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16045breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16046address and file/line number respectively.\n\
16047\n\
16048Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16049are set to the address of the last breakpoint listed unless the command\n\
16050is prefixed with \"server \".\n\n\
c906108c 16051Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16052breakpoint set."),
c906108c
SS
16053 &maintenanceinfolist);
16054
44feb3ce
TT
16055 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16056Set catchpoints to catch events."),
16057 &catch_cmdlist, "catch ",
16058 0/*allow-unknown*/, &cmdlist);
16059
16060 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16061Set temporary catchpoints to catch events."),
16062 &tcatch_cmdlist, "tcatch ",
16063 0/*allow-unknown*/, &cmdlist);
16064
44feb3ce
TT
16065 add_catch_command ("fork", _("Catch calls to fork."),
16066 catch_fork_command_1,
a96d9b2e 16067 NULL,
44feb3ce
TT
16068 (void *) (uintptr_t) catch_fork_permanent,
16069 (void *) (uintptr_t) catch_fork_temporary);
16070 add_catch_command ("vfork", _("Catch calls to vfork."),
16071 catch_fork_command_1,
a96d9b2e 16072 NULL,
44feb3ce
TT
16073 (void *) (uintptr_t) catch_vfork_permanent,
16074 (void *) (uintptr_t) catch_vfork_temporary);
16075 add_catch_command ("exec", _("Catch calls to exec."),
16076 catch_exec_command_1,
a96d9b2e
SDJ
16077 NULL,
16078 CATCH_PERMANENT,
16079 CATCH_TEMPORARY);
edcc5120
TT
16080 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16081Usage: catch load [REGEX]\n\
16082If REGEX is given, only stop for libraries matching the regular expression."),
16083 catch_load_command_1,
16084 NULL,
16085 CATCH_PERMANENT,
16086 CATCH_TEMPORARY);
16087 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16088Usage: catch unload [REGEX]\n\
16089If REGEX is given, only stop for libraries matching the regular expression."),
16090 catch_unload_command_1,
16091 NULL,
16092 CATCH_PERMANENT,
16093 CATCH_TEMPORARY);
c5aa993b 16094
1bedd215
AC
16095 c = add_com ("watch", class_breakpoint, watch_command, _("\
16096Set a watchpoint for an expression.\n\
06a64a0b 16097Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16098A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16099an expression changes.\n\
16100If -l or -location is given, this evaluates EXPRESSION and watches\n\
16101the memory to which it refers."));
65d12d83 16102 set_cmd_completer (c, expression_completer);
c906108c 16103
1bedd215
AC
16104 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16105Set a read watchpoint for an expression.\n\
06a64a0b 16106Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16107A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16108an expression is read.\n\
16109If -l or -location is given, this evaluates EXPRESSION and watches\n\
16110the memory to which it refers."));
65d12d83 16111 set_cmd_completer (c, expression_completer);
c906108c 16112
1bedd215
AC
16113 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16114Set a watchpoint for an expression.\n\
06a64a0b 16115Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16116A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16117an expression is either read or written.\n\
16118If -l or -location is given, this evaluates EXPRESSION and watches\n\
16119the memory to which it refers."));
65d12d83 16120 set_cmd_completer (c, expression_completer);
c906108c 16121
d77f58be 16122 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16123Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16124
920d2a44
AC
16125 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16126 respond to changes - contrary to the description. */
85c07804
AC
16127 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16128 &can_use_hw_watchpoints, _("\
16129Set debugger's willingness to use watchpoint hardware."), _("\
16130Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16131If zero, gdb will not use hardware for new watchpoints, even if\n\
16132such is available. (However, any hardware watchpoints that were\n\
16133created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16134hardware.)"),
16135 NULL,
920d2a44 16136 show_can_use_hw_watchpoints,
85c07804 16137 &setlist, &showlist);
c906108c
SS
16138
16139 can_use_hw_watchpoints = 1;
fa8d40ab 16140
1042e4c0
SS
16141 /* Tracepoint manipulation commands. */
16142
16143 c = add_com ("trace", class_breakpoint, trace_command, _("\
16144Set a tracepoint at specified line or function.\n\
16145\n"
16146BREAK_ARGS_HELP ("trace") "\n\
16147Do \"help tracepoints\" for info on other tracepoint commands."));
16148 set_cmd_completer (c, location_completer);
16149
16150 add_com_alias ("tp", "trace", class_alias, 0);
16151 add_com_alias ("tr", "trace", class_alias, 1);
16152 add_com_alias ("tra", "trace", class_alias, 1);
16153 add_com_alias ("trac", "trace", class_alias, 1);
16154
7a697b8d
SS
16155 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16156Set a fast tracepoint at specified line or function.\n\
16157\n"
16158BREAK_ARGS_HELP ("ftrace") "\n\
16159Do \"help tracepoints\" for info on other tracepoint commands."));
16160 set_cmd_completer (c, location_completer);
16161
0fb4aa4b
PA
16162 c = add_com ("strace", class_breakpoint, strace_command, _("\
16163Set a static tracepoint at specified line, function or marker.\n\
16164\n\
16165strace [LOCATION] [if CONDITION]\n\
16166LOCATION may be a line number, function name, \"*\" and an address,\n\
16167or -m MARKER_ID.\n\
16168If a line number is specified, probe the marker at start of code\n\
16169for that line. If a function is specified, probe the marker at start\n\
16170of code for that function. If an address is specified, probe the marker\n\
16171at that exact address. If a marker id is specified, probe the marker\n\
16172with that name. With no LOCATION, uses current execution address of\n\
16173the selected stack frame.\n\
16174Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16175This collects arbitrary user data passed in the probe point call to the\n\
16176tracing library. You can inspect it when analyzing the trace buffer,\n\
16177by printing the $_sdata variable like any other convenience variable.\n\
16178\n\
16179CONDITION is a boolean expression.\n\
16180\n\
d41c0fc8
PA
16181Multiple tracepoints at one place are permitted, and useful if their\n\
16182conditions are different.\n\
0fb4aa4b
PA
16183\n\
16184Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16185Do \"help tracepoints\" for info on other tracepoint commands."));
16186 set_cmd_completer (c, location_completer);
16187
1042e4c0 16188 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16189Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16190Convenience variable \"$tpnum\" contains the number of the\n\
16191last tracepoint set."));
16192
16193 add_info_alias ("tp", "tracepoints", 1);
16194
16195 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16196Delete specified tracepoints.\n\
16197Arguments are tracepoint numbers, separated by spaces.\n\
16198No argument means delete all tracepoints."),
16199 &deletelist);
7e20dfcd 16200 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16201
16202 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16203Disable specified tracepoints.\n\
16204Arguments are tracepoint numbers, separated by spaces.\n\
16205No argument means disable all tracepoints."),
16206 &disablelist);
16207 deprecate_cmd (c, "disable");
16208
16209 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16210Enable specified tracepoints.\n\
16211Arguments are tracepoint numbers, separated by spaces.\n\
16212No argument means enable all tracepoints."),
16213 &enablelist);
16214 deprecate_cmd (c, "enable");
16215
16216 add_com ("passcount", class_trace, trace_pass_command, _("\
16217Set the passcount for a tracepoint.\n\
16218The trace will end when the tracepoint has been passed 'count' times.\n\
16219Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16220if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16221
6149aea9
PA
16222 add_prefix_cmd ("save", class_breakpoint, save_command,
16223 _("Save breakpoint definitions as a script."),
16224 &save_cmdlist, "save ",
16225 0/*allow-unknown*/, &cmdlist);
16226
16227 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16228Save current breakpoint definitions as a script.\n\
cce7e648 16229This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16230catchpoints, tracepoints). Use the 'source' command in another debug\n\
16231session to restore them."),
16232 &save_cmdlist);
16233 set_cmd_completer (c, filename_completer);
16234
16235 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16236Save current tracepoint definitions as a script.\n\
6149aea9
PA
16237Use the 'source' command in another debug session to restore them."),
16238 &save_cmdlist);
1042e4c0
SS
16239 set_cmd_completer (c, filename_completer);
16240
6149aea9
PA
16241 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16242 deprecate_cmd (c, "save tracepoints");
16243
1bedd215 16244 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16245Breakpoint specific settings\n\
16246Configure various breakpoint-specific variables such as\n\
1bedd215 16247pending breakpoint behavior"),
fa8d40ab
JJ
16248 &breakpoint_set_cmdlist, "set breakpoint ",
16249 0/*allow-unknown*/, &setlist);
1bedd215 16250 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16251Breakpoint specific settings\n\
16252Configure various breakpoint-specific variables such as\n\
1bedd215 16253pending breakpoint behavior"),
fa8d40ab
JJ
16254 &breakpoint_show_cmdlist, "show breakpoint ",
16255 0/*allow-unknown*/, &showlist);
16256
7915a72c
AC
16257 add_setshow_auto_boolean_cmd ("pending", no_class,
16258 &pending_break_support, _("\
16259Set debugger's behavior regarding pending breakpoints."), _("\
16260Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16261If on, an unrecognized breakpoint location will cause gdb to create a\n\
16262pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16263an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16264user-query to see if a pending breakpoint should be created."),
2c5b56ce 16265 NULL,
920d2a44 16266 show_pending_break_support,
6e1d7d6c
AC
16267 &breakpoint_set_cmdlist,
16268 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16269
16270 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16271
16272 add_setshow_boolean_cmd ("auto-hw", no_class,
16273 &automatic_hardware_breakpoints, _("\
16274Set automatic usage of hardware breakpoints."), _("\
16275Show automatic usage of hardware breakpoints."), _("\
16276If set, the debugger will automatically use hardware breakpoints for\n\
16277breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16278a warning will be emitted for such breakpoints."),
16279 NULL,
16280 show_automatic_hardware_breakpoints,
16281 &breakpoint_set_cmdlist,
16282 &breakpoint_show_cmdlist);
74960c60 16283
a25a5a45
PA
16284 add_setshow_boolean_cmd ("always-inserted", class_support,
16285 &always_inserted_mode, _("\
74960c60
VP
16286Set mode for inserting breakpoints."), _("\
16287Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16288When this mode is on, breakpoints are inserted immediately as soon as\n\
16289they're created, kept inserted even when execution stops, and removed\n\
16290only when the user deletes them. When this mode is off (the default),\n\
16291breakpoints are inserted only when execution continues, and removed\n\
16292when execution stops."),
72d0e2c5
YQ
16293 NULL,
16294 &show_always_inserted_mode,
16295 &breakpoint_set_cmdlist,
16296 &breakpoint_show_cmdlist);
f1310107 16297
b775012e
LM
16298 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16299 condition_evaluation_enums,
16300 &condition_evaluation_mode_1, _("\
16301Set mode of breakpoint condition evaluation."), _("\
16302Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16303When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16304evaluated on the host's side by GDB. When it is set to \"target\",\n\
16305breakpoint conditions will be downloaded to the target (if the target\n\
16306supports such feature) and conditions will be evaluated on the target's side.\n\
16307If this is set to \"auto\" (default), this will be automatically set to\n\
16308\"target\" if it supports condition evaluation, otherwise it will\n\
16309be set to \"gdb\""),
16310 &set_condition_evaluation_mode,
16311 &show_condition_evaluation_mode,
16312 &breakpoint_set_cmdlist,
16313 &breakpoint_show_cmdlist);
16314
f1310107
TJB
16315 add_com ("break-range", class_breakpoint, break_range_command, _("\
16316Set a breakpoint for an address range.\n\
16317break-range START-LOCATION, END-LOCATION\n\
16318where START-LOCATION and END-LOCATION can be one of the following:\n\
16319 LINENUM, for that line in the current file,\n\
16320 FILE:LINENUM, for that line in that file,\n\
16321 +OFFSET, for that number of lines after the current line\n\
16322 or the start of the range\n\
16323 FUNCTION, for the first line in that function,\n\
16324 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16325 *ADDRESS, for the instruction at that address.\n\
16326\n\
16327The breakpoint will stop execution of the inferior whenever it executes\n\
16328an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16329range (including START-LOCATION and END-LOCATION)."));
16330
e7e0cddf
SS
16331 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16332Set a dynamic printf at specified line or function.\n\
16333dprintf location,format string,arg1,arg2,...\n\
16334location may be a line number, function name, or \"*\" and an address.\n\
16335If a line number is specified, break at start of code for that line.\n\
0e4777df 16336If a function is specified, break at start of code for that function."));
e7e0cddf
SS
16337 set_cmd_completer (c, location_completer);
16338
16339 add_setshow_enum_cmd ("dprintf-style", class_support,
16340 dprintf_style_enums, &dprintf_style, _("\
16341Set the style of usage for dynamic printf."), _("\
16342Show the style of usage for dynamic printf."), _("\
16343This setting chooses how GDB will do a dynamic printf.\n\
16344If the value is \"gdb\", then the printing is done by GDB to its own\n\
16345console, as with the \"printf\" command.\n\
16346If the value is \"call\", the print is done by calling a function in your\n\
16347program; by default printf(), but you can choose a different function or\n\
16348output stream by setting dprintf-function and dprintf-channel."),
16349 update_dprintf_commands, NULL,
16350 &setlist, &showlist);
16351
16352 dprintf_function = xstrdup ("printf");
16353 add_setshow_string_cmd ("dprintf-function", class_support,
16354 &dprintf_function, _("\
16355Set the function to use for dynamic printf"), _("\
16356Show the function to use for dynamic printf"), NULL,
16357 update_dprintf_commands, NULL,
16358 &setlist, &showlist);
16359
16360 dprintf_channel = xstrdup ("");
16361 add_setshow_string_cmd ("dprintf-channel", class_support,
16362 &dprintf_channel, _("\
16363Set the channel to use for dynamic printf"), _("\
16364Show the channel to use for dynamic printf"), NULL,
16365 update_dprintf_commands, NULL,
16366 &setlist, &showlist);
16367
d3ce09f5
SS
16368 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16369 &disconnected_dprintf, _("\
16370Set whether dprintf continues after GDB disconnects."), _("\
16371Show whether dprintf continues after GDB disconnects."), _("\
16372Use this to let dprintf commands continue to hit and produce output\n\
16373even if GDB disconnects or detaches from the target."),
16374 NULL,
16375 NULL,
16376 &setlist, &showlist);
16377
16378 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16379agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16380(target agent only) This is useful for formatted output in user-defined commands."));
16381
765dc015 16382 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16383
16384 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16385 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16386}
This page took 3.639606 seconds and 4 git commands to generate.