Update comments to target_can_use_hardware_watchpoint
[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{
4f45d445 4574 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4575}
4576
4a64f543
MS
4577/* Execute all the commands associated with all the breakpoints at
4578 this location. Any of these commands could cause the process to
4579 proceed beyond this point, etc. We look out for such changes by
4580 checking the global "breakpoint_proceeded" after each command.
c906108c 4581
347bddb7
PA
4582 Returns true if a breakpoint command resumed the inferior. In that
4583 case, it is the caller's responsibility to recall it again with the
4584 bpstat of the current thread. */
4585
4586static int
4587bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4588{
4589 bpstat bs;
4590 struct cleanup *old_chain;
347bddb7 4591 int again = 0;
c906108c
SS
4592
4593 /* Avoid endless recursion if a `source' command is contained
4594 in bs->commands. */
4595 if (executing_breakpoint_commands)
347bddb7 4596 return 0;
c906108c
SS
4597
4598 executing_breakpoint_commands = 1;
4599 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4600
cf6c5ffb
TT
4601 prevent_dont_repeat ();
4602
4a64f543 4603 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4604 bs = *bsp;
4605
4606 breakpoint_proceeded = 0;
4607 for (; bs != NULL; bs = bs->next)
4608 {
9add0f1b 4609 struct counted_command_line *ccmd;
6c50ab1c
JB
4610 struct command_line *cmd;
4611 struct cleanup *this_cmd_tree_chain;
4612
4613 /* Take ownership of the BSP's command tree, if it has one.
4614
4615 The command tree could legitimately contain commands like
4616 'step' and 'next', which call clear_proceed_status, which
4617 frees stop_bpstat's command tree. To make sure this doesn't
4618 free the tree we're executing out from under us, we need to
4619 take ownership of the tree ourselves. Since a given bpstat's
4620 commands are only executed once, we don't need to copy it; we
4621 can clear the pointer in the bpstat, and make sure we free
4622 the tree when we're done. */
9add0f1b
TT
4623 ccmd = bs->commands;
4624 bs->commands = NULL;
abf85f46
JK
4625 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4626 cmd = ccmd ? ccmd->commands : NULL;
4627 if (command_line_is_silent (cmd))
4628 {
4629 /* The action has been already done by bpstat_stop_status. */
4630 cmd = cmd->next;
4631 }
6c50ab1c 4632
c906108c
SS
4633 while (cmd != NULL)
4634 {
4635 execute_control_command (cmd);
4636
4637 if (breakpoint_proceeded)
4638 break;
4639 else
4640 cmd = cmd->next;
4641 }
6c50ab1c
JB
4642
4643 /* We can free this command tree now. */
4644 do_cleanups (this_cmd_tree_chain);
4645
c906108c 4646 if (breakpoint_proceeded)
32c1e744 4647 {
5589af0e 4648 if (interpreter_async && target_can_async_p ())
347bddb7
PA
4649 /* If we are in async mode, then the target might be still
4650 running, not stopped at any breakpoint, so nothing for
4651 us to do here -- just return to the event loop. */
4652 ;
32c1e744
VP
4653 else
4654 /* In sync mode, when execute_control_command returns
4655 we're already standing on the next breakpoint.
347bddb7
PA
4656 Breakpoint commands for that stop were not run, since
4657 execute_command does not run breakpoint commands --
4658 only command_line_handler does, but that one is not
4659 involved in execution of breakpoint commands. So, we
4660 can now execute breakpoint commands. It should be
4661 noted that making execute_command do bpstat actions is
4662 not an option -- in this case we'll have recursive
4663 invocation of bpstat for each breakpoint with a
4664 command, and can easily blow up GDB stack. Instead, we
4665 return true, which will trigger the caller to recall us
4666 with the new stop_bpstat. */
4667 again = 1;
4668 break;
32c1e744 4669 }
c906108c 4670 }
c2b8ed2c 4671 do_cleanups (old_chain);
347bddb7
PA
4672 return again;
4673}
4674
4675void
4676bpstat_do_actions (void)
4677{
353d1d73
JK
4678 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4679
347bddb7
PA
4680 /* Do any commands attached to breakpoint we are stopped at. */
4681 while (!ptid_equal (inferior_ptid, null_ptid)
4682 && target_has_execution
4683 && !is_exited (inferior_ptid)
4684 && !is_executing (inferior_ptid))
4685 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4686 and only return when it is stopped at the next breakpoint, we
4687 keep doing breakpoint actions until it returns false to
4688 indicate the inferior was not resumed. */
16c381f0 4689 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4690 break;
353d1d73
JK
4691
4692 discard_cleanups (cleanup_if_error);
c906108c
SS
4693}
4694
fa4727a6
DJ
4695/* Print out the (old or new) value associated with a watchpoint. */
4696
4697static void
4698watchpoint_value_print (struct value *val, struct ui_file *stream)
4699{
4700 if (val == NULL)
4701 fprintf_unfiltered (stream, _("<unreadable>"));
4702 else
79a45b7d
TT
4703 {
4704 struct value_print_options opts;
4705 get_user_print_options (&opts);
4706 value_print (val, stream, &opts);
4707 }
fa4727a6
DJ
4708}
4709
e514a9d6 4710/* Generic routine for printing messages indicating why we
4a64f543 4711 stopped. The behavior of this function depends on the value
e514a9d6
JM
4712 'print_it' in the bpstat structure. Under some circumstances we
4713 may decide not to print anything here and delegate the task to
4a64f543 4714 normal_stop(). */
e514a9d6
JM
4715
4716static enum print_stop_action
4717print_bp_stop_message (bpstat bs)
4718{
4719 switch (bs->print_it)
4720 {
4721 case print_it_noop:
4a64f543 4722 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4723 return PRINT_UNKNOWN;
4724 break;
4725
4726 case print_it_done:
4727 /* We still want to print the frame, but we already printed the
4a64f543 4728 relevant messages. */
e514a9d6
JM
4729 return PRINT_SRC_AND_LOC;
4730 break;
4731
4732 case print_it_normal:
4f8d1dc6 4733 {
f431efe5
PA
4734 struct breakpoint *b = bs->breakpoint_at;
4735
1a6a67de
TJB
4736 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4737 which has since been deleted. */
4738 if (b == NULL)
4739 return PRINT_UNKNOWN;
4740
348d480f
PA
4741 /* Normal case. Call the breakpoint's print_it method. */
4742 return b->ops->print_it (bs);
4f8d1dc6 4743 }
348d480f 4744 break;
3086aeae 4745
e514a9d6 4746 default:
8e65ff28 4747 internal_error (__FILE__, __LINE__,
e2e0b3e5 4748 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4749 break;
c906108c 4750 }
c906108c
SS
4751}
4752
edcc5120
TT
4753/* A helper function that prints a shared library stopped event. */
4754
4755static void
4756print_solib_event (int is_catchpoint)
4757{
4758 int any_deleted
4759 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4760 int any_added
4761 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4762
4763 if (!is_catchpoint)
4764 {
4765 if (any_added || any_deleted)
4766 ui_out_text (current_uiout,
4767 _("Stopped due to shared library event:\n"));
4768 else
4769 ui_out_text (current_uiout,
4770 _("Stopped due to shared library event (no "
4771 "libraries added or removed)\n"));
4772 }
4773
4774 if (ui_out_is_mi_like_p (current_uiout))
4775 ui_out_field_string (current_uiout, "reason",
4776 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4777
4778 if (any_deleted)
4779 {
4780 struct cleanup *cleanup;
4781 char *name;
4782 int ix;
4783
4784 ui_out_text (current_uiout, _(" Inferior unloaded "));
4785 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4786 "removed");
4787 for (ix = 0;
4788 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4789 ix, name);
4790 ++ix)
4791 {
4792 if (ix > 0)
4793 ui_out_text (current_uiout, " ");
4794 ui_out_field_string (current_uiout, "library", name);
4795 ui_out_text (current_uiout, "\n");
4796 }
4797
4798 do_cleanups (cleanup);
4799 }
4800
4801 if (any_added)
4802 {
4803 struct so_list *iter;
4804 int ix;
4805 struct cleanup *cleanup;
4806
4807 ui_out_text (current_uiout, _(" Inferior loaded "));
4808 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4809 "added");
4810 for (ix = 0;
4811 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4812 ix, iter);
4813 ++ix)
4814 {
4815 if (ix > 0)
4816 ui_out_text (current_uiout, " ");
4817 ui_out_field_string (current_uiout, "library", iter->so_name);
4818 ui_out_text (current_uiout, "\n");
4819 }
4820
4821 do_cleanups (cleanup);
4822 }
4823}
4824
e514a9d6
JM
4825/* Print a message indicating what happened. This is called from
4826 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4827 list - a list of the eventpoints that caused this stop. KIND is
4828 the target_waitkind for the stopping event. This
e514a9d6
JM
4829 routine calls the generic print routine for printing a message
4830 about reasons for stopping. This will print (for example) the
4831 "Breakpoint n," part of the output. The return value of this
4832 routine is one of:
c906108c 4833
4a64f543 4834 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4835 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4836 code to print the location. An example is
c5aa993b
JM
4837 "Breakpoint 1, " which should be followed by
4838 the location.
917317f4 4839 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4840 to also print the location part of the message.
4841 An example is the catch/throw messages, which
4a64f543 4842 don't require a location appended to the end.
917317f4 4843 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4844 further info to be printed. */
c906108c 4845
917317f4 4846enum print_stop_action
36dfb11c 4847bpstat_print (bpstat bs, int kind)
c906108c
SS
4848{
4849 int val;
c5aa993b 4850
c906108c 4851 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4852 (Currently all watchpoints go on the bpstat whether hit or not.
4853 That probably could (should) be changed, provided care is taken
c906108c 4854 with respect to bpstat_explains_signal). */
e514a9d6
JM
4855 for (; bs; bs = bs->next)
4856 {
4857 val = print_bp_stop_message (bs);
4858 if (val == PRINT_SRC_ONLY
4859 || val == PRINT_SRC_AND_LOC
4860 || val == PRINT_NOTHING)
4861 return val;
4862 }
c906108c 4863
36dfb11c
TT
4864 /* If we had hit a shared library event breakpoint,
4865 print_bp_stop_message would print out this message. If we hit an
4866 OS-level shared library event, do the same thing. */
4867 if (kind == TARGET_WAITKIND_LOADED)
4868 {
edcc5120 4869 print_solib_event (0);
36dfb11c
TT
4870 return PRINT_NOTHING;
4871 }
4872
e514a9d6 4873 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4874 with and nothing was printed. */
917317f4 4875 return PRINT_UNKNOWN;
c906108c
SS
4876}
4877
c42bd95a
DE
4878/* Evaluate the expression EXP and return 1 if value is zero.
4879 This returns the inverse of the condition because it is called
4880 from catch_errors which returns 0 if an exception happened, and if an
4881 exception happens we want execution to stop.
4a64f543 4882 The argument is a "struct expression *" that has been cast to a
c42bd95a 4883 "void *" to make it pass through catch_errors. */
c906108c
SS
4884
4885static int
4efb68b1 4886breakpoint_cond_eval (void *exp)
c906108c 4887{
278cd55f 4888 struct value *mark = value_mark ();
c5aa993b 4889 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4890
c906108c
SS
4891 value_free_to_mark (mark);
4892 return i;
4893}
4894
5760d0ab 4895/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4896
4897static bpstat
5760d0ab 4898bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4899{
4900 bpstat bs;
4901
4902 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4903 bs->next = NULL;
4904 **bs_link_pointer = bs;
4905 *bs_link_pointer = &bs->next;
f431efe5
PA
4906 bs->breakpoint_at = bl->owner;
4907 bs->bp_location_at = bl;
4908 incref_bp_location (bl);
c906108c
SS
4909 /* If the condition is false, etc., don't do the commands. */
4910 bs->commands = NULL;
4911 bs->old_val = NULL;
4912 bs->print_it = print_it_normal;
4913 return bs;
4914}
4915\f
d983da9c
DJ
4916/* The target has stopped with waitstatus WS. Check if any hardware
4917 watchpoints have triggered, according to the target. */
4918
4919int
4920watchpoints_triggered (struct target_waitstatus *ws)
4921{
d92524f1 4922 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4923 CORE_ADDR addr;
4924 struct breakpoint *b;
4925
4926 if (!stopped_by_watchpoint)
4927 {
4928 /* We were not stopped by a watchpoint. Mark all watchpoints
4929 as not triggered. */
4930 ALL_BREAKPOINTS (b)
cc60f2e3 4931 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4932 {
4933 struct watchpoint *w = (struct watchpoint *) b;
4934
4935 w->watchpoint_triggered = watch_triggered_no;
4936 }
d983da9c
DJ
4937
4938 return 0;
4939 }
4940
4941 if (!target_stopped_data_address (&current_target, &addr))
4942 {
4943 /* We were stopped by a watchpoint, but we don't know where.
4944 Mark all watchpoints as unknown. */
4945 ALL_BREAKPOINTS (b)
cc60f2e3 4946 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4947 {
4948 struct watchpoint *w = (struct watchpoint *) b;
4949
4950 w->watchpoint_triggered = watch_triggered_unknown;
4951 }
d983da9c 4952
3c4797ba 4953 return 1;
d983da9c
DJ
4954 }
4955
4956 /* The target could report the data address. Mark watchpoints
4957 affected by this data address as triggered, and all others as not
4958 triggered. */
4959
4960 ALL_BREAKPOINTS (b)
cc60f2e3 4961 if (is_hardware_watchpoint (b))
d983da9c 4962 {
3a5c3e22 4963 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4964 struct bp_location *loc;
d983da9c 4965
3a5c3e22 4966 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4967 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4968 {
3a5c3e22 4969 if (is_masked_watchpoint (b))
9c06b0b4 4970 {
3a5c3e22
PA
4971 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4972 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4973
4974 if (newaddr == start)
4975 {
3a5c3e22 4976 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4977 break;
4978 }
4979 }
4980 /* Exact match not required. Within range is sufficient. */
4981 else if (target_watchpoint_addr_within_range (&current_target,
4982 addr, loc->address,
4983 loc->length))
4984 {
3a5c3e22 4985 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4986 break;
4987 }
4988 }
d983da9c
DJ
4989 }
4990
4991 return 1;
4992}
4993
c906108c
SS
4994/* Possible return values for watchpoint_check (this can't be an enum
4995 because of check_errors). */
4996/* The watchpoint has been deleted. */
4997#define WP_DELETED 1
4998/* The value has changed. */
4999#define WP_VALUE_CHANGED 2
5000/* The value has not changed. */
5001#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5002/* Ignore this watchpoint, no matter if the value changed or not. */
5003#define WP_IGNORE 4
c906108c
SS
5004
5005#define BP_TEMPFLAG 1
5006#define BP_HARDWAREFLAG 2
5007
4a64f543
MS
5008/* Evaluate watchpoint condition expression and check if its value
5009 changed.
553e4c11
JB
5010
5011 P should be a pointer to struct bpstat, but is defined as a void *
5012 in order for this function to be usable with catch_errors. */
c906108c
SS
5013
5014static int
4efb68b1 5015watchpoint_check (void *p)
c906108c
SS
5016{
5017 bpstat bs = (bpstat) p;
3a5c3e22 5018 struct watchpoint *b;
c906108c
SS
5019 struct frame_info *fr;
5020 int within_current_scope;
5021
f431efe5 5022 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5023 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5024 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5025
f6bc2008
PA
5026 /* If this is a local watchpoint, we only want to check if the
5027 watchpoint frame is in scope if the current thread is the thread
5028 that was used to create the watchpoint. */
5029 if (!watchpoint_in_thread_scope (b))
60e1c644 5030 return WP_IGNORE;
f6bc2008 5031
c906108c
SS
5032 if (b->exp_valid_block == NULL)
5033 within_current_scope = 1;
5034 else
5035 {
edb3359d
DJ
5036 struct frame_info *frame = get_current_frame ();
5037 struct gdbarch *frame_arch = get_frame_arch (frame);
5038 CORE_ADDR frame_pc = get_frame_pc (frame);
5039
4a64f543
MS
5040 /* in_function_epilogue_p() returns a non-zero value if we're
5041 still in the function but the stack frame has already been
5042 invalidated. Since we can't rely on the values of local
5043 variables after the stack has been destroyed, we are treating
5044 the watchpoint in that state as `not changed' without further
5045 checking. Don't mark watchpoints as changed if the current
5046 frame is in an epilogue - even if they are in some other
5047 frame, our view of the stack is likely to be wrong and
5048 frame_find_by_id could error out. */
a0f49112 5049 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 5050 return WP_IGNORE;
a0f49112 5051
101dcfbe 5052 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5053 within_current_scope = (fr != NULL);
69fbadd5
DJ
5054
5055 /* If we've gotten confused in the unwinder, we might have
5056 returned a frame that can't describe this variable. */
edb3359d
DJ
5057 if (within_current_scope)
5058 {
5059 struct symbol *function;
5060
5061 function = get_frame_function (fr);
5062 if (function == NULL
5063 || !contained_in (b->exp_valid_block,
5064 SYMBOL_BLOCK_VALUE (function)))
5065 within_current_scope = 0;
5066 }
69fbadd5 5067
edb3359d 5068 if (within_current_scope)
c906108c
SS
5069 /* If we end up stopping, the current frame will get selected
5070 in normal_stop. So this call to select_frame won't affect
5071 the user. */
0f7d239c 5072 select_frame (fr);
c906108c 5073 }
c5aa993b 5074
c906108c
SS
5075 if (within_current_scope)
5076 {
4a64f543
MS
5077 /* We use value_{,free_to_}mark because it could be a *long*
5078 time before we return to the command level and call
5079 free_all_values. We can't call free_all_values because we
5080 might be in the middle of evaluating a function call. */
c906108c 5081
0cf6dd15 5082 int pc = 0;
9c06b0b4 5083 struct value *mark;
fa4727a6
DJ
5084 struct value *new_val;
5085
3a5c3e22 5086 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5087 /* Since we don't know the exact trigger address (from
5088 stopped_data_address), just tell the user we've triggered
5089 a mask watchpoint. */
5090 return WP_VALUE_CHANGED;
5091
5092 mark = value_mark ();
3a1115a0 5093 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5094
bb9d5f81
PP
5095 if (b->val_bitsize != 0)
5096 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5097
4a64f543
MS
5098 /* We use value_equal_contents instead of value_equal because
5099 the latter coerces an array to a pointer, thus comparing just
5100 the address of the array instead of its contents. This is
5101 not what we want. */
fa4727a6 5102 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5103 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5104 {
fa4727a6
DJ
5105 if (new_val != NULL)
5106 {
5107 release_value (new_val);
5108 value_free_to_mark (mark);
5109 }
c906108c
SS
5110 bs->old_val = b->val;
5111 b->val = new_val;
fa4727a6 5112 b->val_valid = 1;
c906108c
SS
5113 return WP_VALUE_CHANGED;
5114 }
5115 else
5116 {
60e1c644 5117 /* Nothing changed. */
c906108c 5118 value_free_to_mark (mark);
c906108c
SS
5119 return WP_VALUE_NOT_CHANGED;
5120 }
5121 }
5122 else
5123 {
79a45e25
PA
5124 struct ui_out *uiout = current_uiout;
5125
c906108c 5126 /* This seems like the only logical thing to do because
c5aa993b
JM
5127 if we temporarily ignored the watchpoint, then when
5128 we reenter the block in which it is valid it contains
5129 garbage (in the case of a function, it may have two
5130 garbage values, one before and one after the prologue).
5131 So we can't even detect the first assignment to it and
5132 watch after that (since the garbage may or may not equal
5133 the first value assigned). */
348d480f
PA
5134 /* We print all the stop information in
5135 breakpoint_ops->print_it, but in this case, by the time we
5136 call breakpoint_ops->print_it this bp will be deleted
5137 already. So we have no choice but print the information
5138 here. */
9dc5e2a9 5139 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5140 ui_out_field_string
5141 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5142 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5143 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5144 ui_out_text (uiout,
5145 " deleted because the program has left the block in\n\
8b93c638 5146which its expression is valid.\n");
4ce44c66 5147
cdac0397 5148 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5149 decref_counted_command_line (&b->base.commands);
d0fb5eae 5150 watchpoint_del_at_next_stop (b);
c906108c
SS
5151
5152 return WP_DELETED;
5153 }
5154}
5155
18a18393 5156/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5157 breakpoint location BL. This function does not check if we should
5158 stop, only if BL explains the stop. */
5159
18a18393 5160static int
6c95b8df 5161bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5162 struct address_space *aspace, CORE_ADDR bp_addr,
5163 const struct target_waitstatus *ws)
18a18393
VP
5164{
5165 struct breakpoint *b = bl->owner;
5166
348d480f 5167 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5168 gdb_assert (b != NULL);
5169
09ac7c10 5170 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5171}
5172
3a5c3e22
PA
5173/* Determine if the watched values have actually changed, and we
5174 should stop. If not, set BS->stop to 0. */
5175
18a18393
VP
5176static void
5177bpstat_check_watchpoint (bpstat bs)
5178{
2bdf28a0 5179 const struct bp_location *bl;
3a5c3e22 5180 struct watchpoint *b;
2bdf28a0
JK
5181
5182 /* BS is built for existing struct breakpoint. */
f431efe5 5183 bl = bs->bp_location_at;
2bdf28a0 5184 gdb_assert (bl != NULL);
3a5c3e22 5185 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5186 gdb_assert (b != NULL);
18a18393 5187
18a18393 5188 {
18a18393
VP
5189 int must_check_value = 0;
5190
3a5c3e22 5191 if (b->base.type == bp_watchpoint)
18a18393
VP
5192 /* For a software watchpoint, we must always check the
5193 watched value. */
5194 must_check_value = 1;
5195 else if (b->watchpoint_triggered == watch_triggered_yes)
5196 /* We have a hardware watchpoint (read, write, or access)
5197 and the target earlier reported an address watched by
5198 this watchpoint. */
5199 must_check_value = 1;
5200 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5201 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5202 /* We were stopped by a hardware watchpoint, but the target could
5203 not report the data address. We must check the watchpoint's
5204 value. Access and read watchpoints are out of luck; without
5205 a data address, we can't figure it out. */
5206 must_check_value = 1;
3a5c3e22 5207
18a18393
VP
5208 if (must_check_value)
5209 {
3e43a32a
MS
5210 char *message
5211 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5212 b->base.number);
18a18393
VP
5213 struct cleanup *cleanups = make_cleanup (xfree, message);
5214 int e = catch_errors (watchpoint_check, bs, message,
5215 RETURN_MASK_ALL);
5216 do_cleanups (cleanups);
5217 switch (e)
5218 {
5219 case WP_DELETED:
5220 /* We've already printed what needs to be printed. */
5221 bs->print_it = print_it_done;
5222 /* Stop. */
5223 break;
60e1c644
PA
5224 case WP_IGNORE:
5225 bs->print_it = print_it_noop;
5226 bs->stop = 0;
5227 break;
18a18393 5228 case WP_VALUE_CHANGED:
3a5c3e22 5229 if (b->base.type == bp_read_watchpoint)
18a18393 5230 {
85d721b8
PA
5231 /* There are two cases to consider here:
5232
4a64f543 5233 1. We're watching the triggered memory for reads.
85d721b8
PA
5234 In that case, trust the target, and always report
5235 the watchpoint hit to the user. Even though
5236 reads don't cause value changes, the value may
5237 have changed since the last time it was read, and
5238 since we're not trapping writes, we will not see
5239 those, and as such we should ignore our notion of
5240 old value.
5241
4a64f543 5242 2. We're watching the triggered memory for both
85d721b8
PA
5243 reads and writes. There are two ways this may
5244 happen:
5245
4a64f543 5246 2.1. This is a target that can't break on data
85d721b8
PA
5247 reads only, but can break on accesses (reads or
5248 writes), such as e.g., x86. We detect this case
5249 at the time we try to insert read watchpoints.
5250
4a64f543 5251 2.2. Otherwise, the target supports read
85d721b8
PA
5252 watchpoints, but, the user set an access or write
5253 watchpoint watching the same memory as this read
5254 watchpoint.
5255
5256 If we're watching memory writes as well as reads,
5257 ignore watchpoint hits when we find that the
5258 value hasn't changed, as reads don't cause
5259 changes. This still gives false positives when
5260 the program writes the same value to memory as
5261 what there was already in memory (we will confuse
5262 it for a read), but it's much better than
5263 nothing. */
5264
5265 int other_write_watchpoint = 0;
5266
5267 if (bl->watchpoint_type == hw_read)
5268 {
5269 struct breakpoint *other_b;
5270
5271 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5272 if (other_b->type == bp_hardware_watchpoint
5273 || other_b->type == bp_access_watchpoint)
85d721b8 5274 {
3a5c3e22
PA
5275 struct watchpoint *other_w =
5276 (struct watchpoint *) other_b;
5277
5278 if (other_w->watchpoint_triggered
5279 == watch_triggered_yes)
5280 {
5281 other_write_watchpoint = 1;
5282 break;
5283 }
85d721b8
PA
5284 }
5285 }
5286
5287 if (other_write_watchpoint
5288 || bl->watchpoint_type == hw_access)
5289 {
5290 /* We're watching the same memory for writes,
5291 and the value changed since the last time we
5292 updated it, so this trap must be for a write.
5293 Ignore it. */
5294 bs->print_it = print_it_noop;
5295 bs->stop = 0;
5296 }
18a18393
VP
5297 }
5298 break;
5299 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5300 if (b->base.type == bp_hardware_watchpoint
5301 || b->base.type == bp_watchpoint)
18a18393
VP
5302 {
5303 /* Don't stop: write watchpoints shouldn't fire if
5304 the value hasn't changed. */
5305 bs->print_it = print_it_noop;
5306 bs->stop = 0;
5307 }
5308 /* Stop. */
5309 break;
5310 default:
5311 /* Can't happen. */
5312 case 0:
5313 /* Error from catch_errors. */
3a5c3e22 5314 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5315 watchpoint_del_at_next_stop (b);
18a18393
VP
5316 /* We've already printed what needs to be printed. */
5317 bs->print_it = print_it_done;
5318 break;
5319 }
5320 }
5321 else /* must_check_value == 0 */
5322 {
5323 /* This is a case where some watchpoint(s) triggered, but
5324 not at the address of this watchpoint, or else no
5325 watchpoint triggered after all. So don't print
5326 anything for this watchpoint. */
5327 bs->print_it = print_it_noop;
5328 bs->stop = 0;
5329 }
5330 }
5331}
5332
7d4df6a4
DE
5333/* For breakpoints that are currently marked as telling gdb to stop,
5334 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5335 of breakpoint referred to by BS. If we should not stop for this
5336 breakpoint, set BS->stop to 0. */
f431efe5 5337
18a18393
VP
5338static void
5339bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5340{
2bdf28a0
JK
5341 const struct bp_location *bl;
5342 struct breakpoint *b;
7d4df6a4
DE
5343 int value_is_zero = 0;
5344 struct expression *cond;
5345
5346 gdb_assert (bs->stop);
2bdf28a0
JK
5347
5348 /* BS is built for existing struct breakpoint. */
f431efe5 5349 bl = bs->bp_location_at;
2bdf28a0 5350 gdb_assert (bl != NULL);
f431efe5 5351 b = bs->breakpoint_at;
2bdf28a0 5352 gdb_assert (b != NULL);
18a18393 5353
b775012e
LM
5354 /* Even if the target evaluated the condition on its end and notified GDB, we
5355 need to do so again since GDB does not know if we stopped due to a
5356 breakpoint or a single step breakpoint. */
5357
18a18393 5358 if (frame_id_p (b->frame_id)
edb3359d 5359 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5360 {
7d4df6a4
DE
5361 bs->stop = 0;
5362 return;
5363 }
60e1c644 5364
12ab52e9
PA
5365 /* If this is a thread/task-specific breakpoint, don't waste cpu
5366 evaluating the condition if this isn't the specified
5367 thread/task. */
5368 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5369 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5370
6c1b0f7b
DE
5371 {
5372 bs->stop = 0;
5373 return;
5374 }
5375
6dddc817
DE
5376 /* Evaluate extension language breakpoints that have a "stop" method
5377 implemented. */
5378 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5379
7d4df6a4
DE
5380 if (is_watchpoint (b))
5381 {
5382 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5383
7d4df6a4
DE
5384 cond = w->cond_exp;
5385 }
5386 else
5387 cond = bl->cond;
60e1c644 5388
7d4df6a4
DE
5389 if (cond && b->disposition != disp_del_at_next_stop)
5390 {
5391 int within_current_scope = 1;
5392 struct watchpoint * w;
60e1c644 5393
7d4df6a4
DE
5394 /* We use value_mark and value_free_to_mark because it could
5395 be a long time before we return to the command level and
5396 call free_all_values. We can't call free_all_values
5397 because we might be in the middle of evaluating a
5398 function call. */
5399 struct value *mark = value_mark ();
5400
5401 if (is_watchpoint (b))
5402 w = (struct watchpoint *) b;
5403 else
5404 w = NULL;
5405
5406 /* Need to select the frame, with all that implies so that
5407 the conditions will have the right context. Because we
5408 use the frame, we will not see an inlined function's
5409 variables when we arrive at a breakpoint at the start
5410 of the inlined function; the current frame will be the
5411 call site. */
5412 if (w == NULL || w->cond_exp_valid_block == NULL)
5413 select_frame (get_current_frame ());
5414 else
18a18393 5415 {
7d4df6a4
DE
5416 struct frame_info *frame;
5417
5418 /* For local watchpoint expressions, which particular
5419 instance of a local is being watched matters, so we
5420 keep track of the frame to evaluate the expression
5421 in. To evaluate the condition however, it doesn't
5422 really matter which instantiation of the function
5423 where the condition makes sense triggers the
5424 watchpoint. This allows an expression like "watch
5425 global if q > 10" set in `func', catch writes to
5426 global on all threads that call `func', or catch
5427 writes on all recursive calls of `func' by a single
5428 thread. We simply always evaluate the condition in
5429 the innermost frame that's executing where it makes
5430 sense to evaluate the condition. It seems
5431 intuitive. */
5432 frame = block_innermost_frame (w->cond_exp_valid_block);
5433 if (frame != NULL)
5434 select_frame (frame);
5435 else
5436 within_current_scope = 0;
18a18393 5437 }
7d4df6a4
DE
5438 if (within_current_scope)
5439 value_is_zero
5440 = catch_errors (breakpoint_cond_eval, cond,
5441 "Error in testing breakpoint condition:\n",
5442 RETURN_MASK_ALL);
5443 else
18a18393 5444 {
7d4df6a4
DE
5445 warning (_("Watchpoint condition cannot be tested "
5446 "in the current scope"));
5447 /* If we failed to set the right context for this
5448 watchpoint, unconditionally report it. */
5449 value_is_zero = 0;
18a18393 5450 }
7d4df6a4
DE
5451 /* FIXME-someday, should give breakpoint #. */
5452 value_free_to_mark (mark);
18a18393 5453 }
7d4df6a4
DE
5454
5455 if (cond && value_is_zero)
5456 {
5457 bs->stop = 0;
5458 }
7d4df6a4
DE
5459 else if (b->ignore_count > 0)
5460 {
5461 b->ignore_count--;
5462 bs->stop = 0;
5463 /* Increase the hit count even though we don't stop. */
5464 ++(b->hit_count);
5465 observer_notify_breakpoint_modified (b);
5466 }
18a18393
VP
5467}
5468
1cf4d951
PA
5469/* Returns true if we need to track moribund locations of LOC's type
5470 on the current target. */
5471
5472static int
5473need_moribund_for_location_type (struct bp_location *loc)
5474{
5475 return ((loc->loc_type == bp_loc_software_breakpoint
5476 && !target_supports_stopped_by_sw_breakpoint ())
5477 || (loc->loc_type == bp_loc_hardware_breakpoint
5478 && !target_supports_stopped_by_hw_breakpoint ()));
5479}
5480
18a18393 5481
9709f61c 5482/* Get a bpstat associated with having just stopped at address
d983da9c 5483 BP_ADDR in thread PTID.
c906108c 5484
d983da9c 5485 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5486 don't understand this stop. Result is a chain of bpstat's such
5487 that:
c906108c 5488
c5aa993b 5489 if we don't understand the stop, the result is a null pointer.
c906108c 5490
c5aa993b 5491 if we understand why we stopped, the result is not null.
c906108c 5492
c5aa993b
JM
5493 Each element of the chain refers to a particular breakpoint or
5494 watchpoint at which we have stopped. (We may have stopped for
5495 several reasons concurrently.)
c906108c 5496
c5aa993b
JM
5497 Each element of the chain has valid next, breakpoint_at,
5498 commands, FIXME??? fields. */
c906108c
SS
5499
5500bpstat
6c95b8df 5501bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5502 CORE_ADDR bp_addr, ptid_t ptid,
5503 const struct target_waitstatus *ws)
c906108c 5504{
0d381245 5505 struct breakpoint *b = NULL;
afe38095 5506 struct bp_location *bl;
20874c92 5507 struct bp_location *loc;
5760d0ab
JK
5508 /* First item of allocated bpstat's. */
5509 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5510 /* Pointer to the last thing in the chain currently. */
5760d0ab 5511 bpstat bs;
20874c92 5512 int ix;
429374b8 5513 int need_remove_insert;
f431efe5 5514 int removed_any;
c906108c 5515
f431efe5
PA
5516 /* First, build the bpstat chain with locations that explain a
5517 target stop, while being careful to not set the target running,
5518 as that may invalidate locations (in particular watchpoint
5519 locations are recreated). Resuming will happen here with
5520 breakpoint conditions or watchpoint expressions that include
5521 inferior function calls. */
c5aa993b 5522
429374b8
JK
5523 ALL_BREAKPOINTS (b)
5524 {
1a853c52 5525 if (!breakpoint_enabled (b))
429374b8 5526 continue;
a5606eee 5527
429374b8
JK
5528 for (bl = b->loc; bl != NULL; bl = bl->next)
5529 {
4a64f543
MS
5530 /* For hardware watchpoints, we look only at the first
5531 location. The watchpoint_check function will work on the
5532 entire expression, not the individual locations. For
5533 read watchpoints, the watchpoints_triggered function has
5534 checked all locations already. */
429374b8
JK
5535 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5536 break;
18a18393 5537
f6592439 5538 if (!bl->enabled || bl->shlib_disabled)
429374b8 5539 continue;
c5aa993b 5540
09ac7c10 5541 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5542 continue;
c5aa993b 5543
4a64f543
MS
5544 /* Come here if it's a watchpoint, or if the break address
5545 matches. */
c5aa993b 5546
4a64f543
MS
5547 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5548 explain stop. */
c5aa993b 5549
f431efe5
PA
5550 /* Assume we stop. Should we find a watchpoint that is not
5551 actually triggered, or if the condition of the breakpoint
5552 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5553 bs->stop = 1;
5554 bs->print = 1;
d983da9c 5555
f431efe5
PA
5556 /* If this is a scope breakpoint, mark the associated
5557 watchpoint as triggered so that we will handle the
5558 out-of-scope event. We'll get to the watchpoint next
5559 iteration. */
d0fb5eae 5560 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5561 {
5562 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5563
5564 w->watchpoint_triggered = watch_triggered_yes;
5565 }
f431efe5
PA
5566 }
5567 }
5568
7c16b83e 5569 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5570 if (!target_supports_stopped_by_sw_breakpoint ()
5571 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5572 {
1cf4d951 5573 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5574 {
1cf4d951
PA
5575 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5576 && need_moribund_for_location_type (loc))
5577 {
5578 bs = bpstat_alloc (loc, &bs_link);
5579 /* For hits of moribund locations, we should just proceed. */
5580 bs->stop = 0;
5581 bs->print = 0;
5582 bs->print_it = print_it_noop;
5583 }
f431efe5
PA
5584 }
5585 }
5586
edcc5120
TT
5587 /* A bit of special processing for shlib breakpoints. We need to
5588 process solib loading here, so that the lists of loaded and
5589 unloaded libraries are correct before we handle "catch load" and
5590 "catch unload". */
5591 for (bs = bs_head; bs != NULL; bs = bs->next)
5592 {
5d268276 5593 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5594 {
5595 handle_solib_event ();
5596 break;
5597 }
5598 }
5599
f431efe5
PA
5600 /* Now go through the locations that caused the target to stop, and
5601 check whether we're interested in reporting this stop to higher
5602 layers, or whether we should resume the target transparently. */
5603
5604 removed_any = 0;
5605
5760d0ab 5606 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5607 {
5608 if (!bs->stop)
5609 continue;
5610
f431efe5 5611 b = bs->breakpoint_at;
348d480f
PA
5612 b->ops->check_status (bs);
5613 if (bs->stop)
28010a5d 5614 {
348d480f 5615 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5616
429374b8
JK
5617 if (bs->stop)
5618 {
5619 ++(b->hit_count);
8d3788bd 5620 observer_notify_breakpoint_modified (b);
c906108c 5621
4a64f543 5622 /* We will stop here. */
429374b8
JK
5623 if (b->disposition == disp_disable)
5624 {
816338b5 5625 --(b->enable_count);
1a853c52 5626 if (b->enable_count <= 0)
429374b8 5627 b->enable_state = bp_disabled;
f431efe5 5628 removed_any = 1;
429374b8
JK
5629 }
5630 if (b->silent)
5631 bs->print = 0;
5632 bs->commands = b->commands;
9add0f1b 5633 incref_counted_command_line (bs->commands);
abf85f46
JK
5634 if (command_line_is_silent (bs->commands
5635 ? bs->commands->commands : NULL))
5636 bs->print = 0;
9d6e6e84
HZ
5637
5638 b->ops->after_condition_true (bs);
429374b8
JK
5639 }
5640
348d480f 5641 }
a9b3a50f
PA
5642
5643 /* Print nothing for this entry if we don't stop or don't
5644 print. */
5645 if (!bs->stop || !bs->print)
5646 bs->print_it = print_it_noop;
429374b8 5647 }
876fa593 5648
d983da9c
DJ
5649 /* If we aren't stopping, the value of some hardware watchpoint may
5650 not have changed, but the intermediate memory locations we are
5651 watching may have. Don't bother if we're stopping; this will get
5652 done later. */
d832cb68 5653 need_remove_insert = 0;
5760d0ab
JK
5654 if (! bpstat_causes_stop (bs_head))
5655 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5656 if (!bs->stop
f431efe5
PA
5657 && bs->breakpoint_at
5658 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5659 {
3a5c3e22
PA
5660 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5661
5662 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5663 need_remove_insert = 1;
d983da9c
DJ
5664 }
5665
d832cb68 5666 if (need_remove_insert)
44702360 5667 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5668 else if (removed_any)
44702360 5669 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5670
5760d0ab 5671 return bs_head;
c906108c 5672}
628fe4e4
JK
5673
5674static void
5675handle_jit_event (void)
5676{
5677 struct frame_info *frame;
5678 struct gdbarch *gdbarch;
5679
5680 /* Switch terminal for any messages produced by
5681 breakpoint_re_set. */
5682 target_terminal_ours_for_output ();
5683
5684 frame = get_current_frame ();
5685 gdbarch = get_frame_arch (frame);
5686
5687 jit_event_handler (gdbarch);
5688
5689 target_terminal_inferior ();
5690}
5691
5692/* Prepare WHAT final decision for infrun. */
5693
5694/* Decide what infrun needs to do with this bpstat. */
5695
c906108c 5696struct bpstat_what
0e30163f 5697bpstat_what (bpstat bs_head)
c906108c 5698{
c906108c 5699 struct bpstat_what retval;
628fe4e4 5700 int jit_event = 0;
0e30163f 5701 bpstat bs;
c906108c 5702
628fe4e4 5703 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5704 retval.call_dummy = STOP_NONE;
186c406b 5705 retval.is_longjmp = 0;
628fe4e4 5706
0e30163f 5707 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5708 {
628fe4e4
JK
5709 /* Extract this BS's action. After processing each BS, we check
5710 if its action overrides all we've seem so far. */
5711 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5712 enum bptype bptype;
5713
c906108c 5714 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5715 {
5716 /* I suspect this can happen if it was a momentary
5717 breakpoint which has since been deleted. */
5718 bptype = bp_none;
5719 }
20874c92 5720 else
f431efe5 5721 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5722
5723 switch (bptype)
c906108c
SS
5724 {
5725 case bp_none:
628fe4e4 5726 break;
c906108c
SS
5727 case bp_breakpoint:
5728 case bp_hardware_breakpoint:
7c16b83e 5729 case bp_single_step:
c906108c
SS
5730 case bp_until:
5731 case bp_finish:
a9b3a50f 5732 case bp_shlib_event:
c906108c
SS
5733 if (bs->stop)
5734 {
5735 if (bs->print)
628fe4e4 5736 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5737 else
628fe4e4 5738 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5739 }
5740 else
628fe4e4 5741 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5742 break;
5743 case bp_watchpoint:
5744 case bp_hardware_watchpoint:
5745 case bp_read_watchpoint:
5746 case bp_access_watchpoint:
5747 if (bs->stop)
5748 {
5749 if (bs->print)
628fe4e4 5750 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5751 else
628fe4e4 5752 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5753 }
5754 else
628fe4e4
JK
5755 {
5756 /* There was a watchpoint, but we're not stopping.
5757 This requires no further action. */
5758 }
c906108c
SS
5759 break;
5760 case bp_longjmp:
e2e4d78b 5761 case bp_longjmp_call_dummy:
186c406b 5762 case bp_exception:
628fe4e4 5763 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5764 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5765 break;
5766 case bp_longjmp_resume:
186c406b 5767 case bp_exception_resume:
628fe4e4 5768 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5769 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5770 break;
5771 case bp_step_resume:
5772 if (bs->stop)
628fe4e4
JK
5773 this_action = BPSTAT_WHAT_STEP_RESUME;
5774 else
c906108c 5775 {
628fe4e4
JK
5776 /* It is for the wrong frame. */
5777 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5778 }
c906108c 5779 break;
2c03e5be
PA
5780 case bp_hp_step_resume:
5781 if (bs->stop)
5782 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5783 else
5784 {
5785 /* It is for the wrong frame. */
5786 this_action = BPSTAT_WHAT_SINGLE;
5787 }
5788 break;
c906108c 5789 case bp_watchpoint_scope:
c4093a6a 5790 case bp_thread_event:
1900040c 5791 case bp_overlay_event:
0fd8e87f 5792 case bp_longjmp_master:
aa7d318d 5793 case bp_std_terminate_master:
186c406b 5794 case bp_exception_master:
628fe4e4 5795 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5796 break;
ce78b96d 5797 case bp_catchpoint:
c5aa993b
JM
5798 if (bs->stop)
5799 {
5800 if (bs->print)
628fe4e4 5801 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5802 else
628fe4e4 5803 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5804 }
5805 else
628fe4e4
JK
5806 {
5807 /* There was a catchpoint, but we're not stopping.
5808 This requires no further action. */
5809 }
5810 break;
628fe4e4
JK
5811 case bp_jit_event:
5812 jit_event = 1;
5813 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5814 break;
c906108c 5815 case bp_call_dummy:
53a5351d
JM
5816 /* Make sure the action is stop (silent or noisy),
5817 so infrun.c pops the dummy frame. */
aa7d318d 5818 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5819 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5820 break;
5821 case bp_std_terminate:
5822 /* Make sure the action is stop (silent or noisy),
5823 so infrun.c pops the dummy frame. */
aa7d318d 5824 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5825 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5826 break;
1042e4c0 5827 case bp_tracepoint:
7a697b8d 5828 case bp_fast_tracepoint:
0fb4aa4b 5829 case bp_static_tracepoint:
1042e4c0
SS
5830 /* Tracepoint hits should not be reported back to GDB, and
5831 if one got through somehow, it should have been filtered
5832 out already. */
5833 internal_error (__FILE__, __LINE__,
7a697b8d 5834 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5835 break;
5836 case bp_gnu_ifunc_resolver:
5837 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5838 this_action = BPSTAT_WHAT_SINGLE;
5839 break;
5840 case bp_gnu_ifunc_resolver_return:
5841 /* The breakpoint will be removed, execution will restart from the
5842 PC of the former breakpoint. */
5843 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5844 break;
e7e0cddf
SS
5845
5846 case bp_dprintf:
a11cfd87
HZ
5847 if (bs->stop)
5848 this_action = BPSTAT_WHAT_STOP_SILENT;
5849 else
5850 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5851 break;
5852
628fe4e4
JK
5853 default:
5854 internal_error (__FILE__, __LINE__,
5855 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5856 }
628fe4e4
JK
5857
5858 retval.main_action = max (retval.main_action, this_action);
c906108c 5859 }
628fe4e4 5860
0e30163f
JK
5861 /* These operations may affect the bs->breakpoint_at state so they are
5862 delayed after MAIN_ACTION is decided above. */
5863
628fe4e4
JK
5864 if (jit_event)
5865 {
5866 if (debug_infrun)
5867 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5868
5869 handle_jit_event ();
5870 }
5871
0e30163f
JK
5872 for (bs = bs_head; bs != NULL; bs = bs->next)
5873 {
5874 struct breakpoint *b = bs->breakpoint_at;
5875
5876 if (b == NULL)
5877 continue;
5878 switch (b->type)
5879 {
5880 case bp_gnu_ifunc_resolver:
5881 gnu_ifunc_resolver_stop (b);
5882 break;
5883 case bp_gnu_ifunc_resolver_return:
5884 gnu_ifunc_resolver_return_stop (b);
5885 break;
5886 }
5887 }
5888
c906108c
SS
5889 return retval;
5890}
5891
5892/* Nonzero if we should step constantly (e.g. watchpoints on machines
5893 without hardware support). This isn't related to a specific bpstat,
5894 just to things like whether watchpoints are set. */
5895
c5aa993b 5896int
fba45db2 5897bpstat_should_step (void)
c906108c
SS
5898{
5899 struct breakpoint *b;
cc59ec59 5900
c906108c 5901 ALL_BREAKPOINTS (b)
717a8278 5902 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5903 return 1;
c906108c
SS
5904 return 0;
5905}
5906
67822962
PA
5907int
5908bpstat_causes_stop (bpstat bs)
5909{
5910 for (; bs != NULL; bs = bs->next)
5911 if (bs->stop)
5912 return 1;
5913
5914 return 0;
5915}
5916
c906108c 5917\f
c5aa993b 5918
170b53b2
UW
5919/* Compute a string of spaces suitable to indent the next line
5920 so it starts at the position corresponding to the table column
5921 named COL_NAME in the currently active table of UIOUT. */
5922
5923static char *
5924wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5925{
5926 static char wrap_indent[80];
5927 int i, total_width, width, align;
5928 char *text;
5929
5930 total_width = 0;
5931 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5932 {
5933 if (strcmp (text, col_name) == 0)
5934 {
5935 gdb_assert (total_width < sizeof wrap_indent);
5936 memset (wrap_indent, ' ', total_width);
5937 wrap_indent[total_width] = 0;
5938
5939 return wrap_indent;
5940 }
5941
5942 total_width += width + 1;
5943 }
5944
5945 return NULL;
5946}
5947
b775012e
LM
5948/* Determine if the locations of this breakpoint will have their conditions
5949 evaluated by the target, host or a mix of both. Returns the following:
5950
5951 "host": Host evals condition.
5952 "host or target": Host or Target evals condition.
5953 "target": Target evals condition.
5954*/
5955
5956static const char *
5957bp_condition_evaluator (struct breakpoint *b)
5958{
5959 struct bp_location *bl;
5960 char host_evals = 0;
5961 char target_evals = 0;
5962
5963 if (!b)
5964 return NULL;
5965
5966 if (!is_breakpoint (b))
5967 return NULL;
5968
5969 if (gdb_evaluates_breakpoint_condition_p ()
5970 || !target_supports_evaluation_of_breakpoint_conditions ())
5971 return condition_evaluation_host;
5972
5973 for (bl = b->loc; bl; bl = bl->next)
5974 {
5975 if (bl->cond_bytecode)
5976 target_evals++;
5977 else
5978 host_evals++;
5979 }
5980
5981 if (host_evals && target_evals)
5982 return condition_evaluation_both;
5983 else if (target_evals)
5984 return condition_evaluation_target;
5985 else
5986 return condition_evaluation_host;
5987}
5988
5989/* Determine the breakpoint location's condition evaluator. This is
5990 similar to bp_condition_evaluator, but for locations. */
5991
5992static const char *
5993bp_location_condition_evaluator (struct bp_location *bl)
5994{
5995 if (bl && !is_breakpoint (bl->owner))
5996 return NULL;
5997
5998 if (gdb_evaluates_breakpoint_condition_p ()
5999 || !target_supports_evaluation_of_breakpoint_conditions ())
6000 return condition_evaluation_host;
6001
6002 if (bl && bl->cond_bytecode)
6003 return condition_evaluation_target;
6004 else
6005 return condition_evaluation_host;
6006}
6007
859825b8
JK
6008/* Print the LOC location out of the list of B->LOC locations. */
6009
170b53b2
UW
6010static void
6011print_breakpoint_location (struct breakpoint *b,
6012 struct bp_location *loc)
0d381245 6013{
79a45e25 6014 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6015 struct cleanup *old_chain = save_current_program_space ();
6016
859825b8
JK
6017 if (loc != NULL && loc->shlib_disabled)
6018 loc = NULL;
6019
6c95b8df
PA
6020 if (loc != NULL)
6021 set_current_program_space (loc->pspace);
6022
56435ebe
TT
6023 if (b->display_canonical)
6024 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 6025 else if (loc && loc->symtab)
0d381245
VP
6026 {
6027 struct symbol *sym
6028 = find_pc_sect_function (loc->address, loc->section);
6029 if (sym)
6030 {
6031 ui_out_text (uiout, "in ");
6032 ui_out_field_string (uiout, "func",
6033 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6034 ui_out_text (uiout, " ");
6035 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6036 ui_out_text (uiout, "at ");
0d381245 6037 }
05cba821
JK
6038 ui_out_field_string (uiout, "file",
6039 symtab_to_filename_for_display (loc->symtab));
0d381245 6040 ui_out_text (uiout, ":");
05cba821 6041
0d381245 6042 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6043 ui_out_field_string (uiout, "fullname",
6044 symtab_to_fullname (loc->symtab));
0d381245 6045
f8eba3c6 6046 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6047 }
859825b8 6048 else if (loc)
0d381245 6049 {
f99d8bf4
PA
6050 struct ui_file *stb = mem_fileopen ();
6051 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6052
f99d8bf4 6053 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6054 demangle, "");
0d381245 6055 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6056
6057 do_cleanups (stb_chain);
0d381245 6058 }
859825b8
JK
6059 else
6060 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 6061
b775012e
LM
6062 if (loc && is_breakpoint (b)
6063 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6064 && bp_condition_evaluator (b) == condition_evaluation_both)
6065 {
6066 ui_out_text (uiout, " (");
6067 ui_out_field_string (uiout, "evaluated-by",
6068 bp_location_condition_evaluator (loc));
6069 ui_out_text (uiout, ")");
6070 }
6071
6c95b8df 6072 do_cleanups (old_chain);
0d381245
VP
6073}
6074
269b11a2
PA
6075static const char *
6076bptype_string (enum bptype type)
c906108c 6077{
c4093a6a
JM
6078 struct ep_type_description
6079 {
6080 enum bptype type;
6081 char *description;
6082 };
6083 static struct ep_type_description bptypes[] =
c906108c 6084 {
c5aa993b
JM
6085 {bp_none, "?deleted?"},
6086 {bp_breakpoint, "breakpoint"},
c906108c 6087 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6088 {bp_single_step, "sw single-step"},
c5aa993b
JM
6089 {bp_until, "until"},
6090 {bp_finish, "finish"},
6091 {bp_watchpoint, "watchpoint"},
c906108c 6092 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6093 {bp_read_watchpoint, "read watchpoint"},
6094 {bp_access_watchpoint, "acc watchpoint"},
6095 {bp_longjmp, "longjmp"},
6096 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6097 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6098 {bp_exception, "exception"},
6099 {bp_exception_resume, "exception resume"},
c5aa993b 6100 {bp_step_resume, "step resume"},
2c03e5be 6101 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6102 {bp_watchpoint_scope, "watchpoint scope"},
6103 {bp_call_dummy, "call dummy"},
aa7d318d 6104 {bp_std_terminate, "std::terminate"},
c5aa993b 6105 {bp_shlib_event, "shlib events"},
c4093a6a 6106 {bp_thread_event, "thread events"},
1900040c 6107 {bp_overlay_event, "overlay events"},
0fd8e87f 6108 {bp_longjmp_master, "longjmp master"},
aa7d318d 6109 {bp_std_terminate_master, "std::terminate master"},
186c406b 6110 {bp_exception_master, "exception master"},
ce78b96d 6111 {bp_catchpoint, "catchpoint"},
1042e4c0 6112 {bp_tracepoint, "tracepoint"},
7a697b8d 6113 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6114 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6115 {bp_dprintf, "dprintf"},
4efc6507 6116 {bp_jit_event, "jit events"},
0e30163f
JK
6117 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6118 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6119 };
269b11a2
PA
6120
6121 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6122 || ((int) type != bptypes[(int) type].type))
6123 internal_error (__FILE__, __LINE__,
6124 _("bptypes table does not describe type #%d."),
6125 (int) type);
6126
6127 return bptypes[(int) type].description;
6128}
6129
998580f1
MK
6130/* For MI, output a field named 'thread-groups' with a list as the value.
6131 For CLI, prefix the list with the string 'inf'. */
6132
6133static void
6134output_thread_groups (struct ui_out *uiout,
6135 const char *field_name,
6136 VEC(int) *inf_num,
6137 int mi_only)
6138{
752eb8b4 6139 struct cleanup *back_to;
998580f1
MK
6140 int is_mi = ui_out_is_mi_like_p (uiout);
6141 int inf;
6142 int i;
6143
6144 /* For backward compatibility, don't display inferiors in CLI unless
6145 there are several. Always display them for MI. */
6146 if (!is_mi && mi_only)
6147 return;
6148
752eb8b4
TT
6149 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6150
998580f1
MK
6151 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6152 {
6153 if (is_mi)
6154 {
6155 char mi_group[10];
6156
6157 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6158 ui_out_field_string (uiout, NULL, mi_group);
6159 }
6160 else
6161 {
6162 if (i == 0)
6163 ui_out_text (uiout, " inf ");
6164 else
6165 ui_out_text (uiout, ", ");
6166
6167 ui_out_text (uiout, plongest (inf));
6168 }
6169 }
6170
6171 do_cleanups (back_to);
6172}
6173
269b11a2
PA
6174/* Print B to gdb_stdout. */
6175
6176static void
6177print_one_breakpoint_location (struct breakpoint *b,
6178 struct bp_location *loc,
6179 int loc_number,
6180 struct bp_location **last_loc,
269b11a2
PA
6181 int allflag)
6182{
6183 struct command_line *l;
c2c6d25f 6184 static char bpenables[] = "nynny";
c906108c 6185
79a45e25 6186 struct ui_out *uiout = current_uiout;
0d381245
VP
6187 int header_of_multiple = 0;
6188 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6189 struct value_print_options opts;
6190
6191 get_user_print_options (&opts);
0d381245
VP
6192
6193 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6194 /* See comment in print_one_breakpoint concerning treatment of
6195 breakpoints with single disabled location. */
0d381245
VP
6196 if (loc == NULL
6197 && (b->loc != NULL
6198 && (b->loc->next != NULL || !b->loc->enabled)))
6199 header_of_multiple = 1;
6200 if (loc == NULL)
6201 loc = b->loc;
6202
c4093a6a
JM
6203 annotate_record ();
6204
6205 /* 1 */
6206 annotate_field (0);
0d381245
VP
6207 if (part_of_multiple)
6208 {
6209 char *formatted;
0c6773c1 6210 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6211 ui_out_field_string (uiout, "number", formatted);
6212 xfree (formatted);
6213 }
6214 else
6215 {
6216 ui_out_field_int (uiout, "number", b->number);
6217 }
c4093a6a
JM
6218
6219 /* 2 */
6220 annotate_field (1);
0d381245
VP
6221 if (part_of_multiple)
6222 ui_out_field_skip (uiout, "type");
269b11a2
PA
6223 else
6224 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6225
6226 /* 3 */
6227 annotate_field (2);
0d381245
VP
6228 if (part_of_multiple)
6229 ui_out_field_skip (uiout, "disp");
6230 else
2cec12e5 6231 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6232
c4093a6a
JM
6233
6234 /* 4 */
6235 annotate_field (3);
0d381245 6236 if (part_of_multiple)
54e52265 6237 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6238 else
4a64f543
MS
6239 ui_out_field_fmt (uiout, "enabled", "%c",
6240 bpenables[(int) b->enable_state]);
54e52265 6241 ui_out_spaces (uiout, 2);
0d381245 6242
c4093a6a
JM
6243
6244 /* 5 and 6 */
3086aeae 6245 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6246 {
4a64f543
MS
6247 /* Although the print_one can possibly print all locations,
6248 calling it here is not likely to get any nice result. So,
6249 make sure there's just one location. */
0d381245 6250 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6251 b->ops->print_one (b, last_loc);
0d381245 6252 }
3086aeae
DJ
6253 else
6254 switch (b->type)
6255 {
6256 case bp_none:
6257 internal_error (__FILE__, __LINE__,
e2e0b3e5 6258 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6259 break;
c906108c 6260
3086aeae
DJ
6261 case bp_watchpoint:
6262 case bp_hardware_watchpoint:
6263 case bp_read_watchpoint:
6264 case bp_access_watchpoint:
3a5c3e22
PA
6265 {
6266 struct watchpoint *w = (struct watchpoint *) b;
6267
6268 /* Field 4, the address, is omitted (which makes the columns
6269 not line up too nicely with the headers, but the effect
6270 is relatively readable). */
6271 if (opts.addressprint)
6272 ui_out_field_skip (uiout, "addr");
6273 annotate_field (5);
6274 ui_out_field_string (uiout, "what", w->exp_string);
6275 }
3086aeae
DJ
6276 break;
6277
3086aeae
DJ
6278 case bp_breakpoint:
6279 case bp_hardware_breakpoint:
7c16b83e 6280 case bp_single_step:
3086aeae
DJ
6281 case bp_until:
6282 case bp_finish:
6283 case bp_longjmp:
6284 case bp_longjmp_resume:
e2e4d78b 6285 case bp_longjmp_call_dummy:
186c406b
TT
6286 case bp_exception:
6287 case bp_exception_resume:
3086aeae 6288 case bp_step_resume:
2c03e5be 6289 case bp_hp_step_resume:
3086aeae
DJ
6290 case bp_watchpoint_scope:
6291 case bp_call_dummy:
aa7d318d 6292 case bp_std_terminate:
3086aeae
DJ
6293 case bp_shlib_event:
6294 case bp_thread_event:
6295 case bp_overlay_event:
0fd8e87f 6296 case bp_longjmp_master:
aa7d318d 6297 case bp_std_terminate_master:
186c406b 6298 case bp_exception_master:
1042e4c0 6299 case bp_tracepoint:
7a697b8d 6300 case bp_fast_tracepoint:
0fb4aa4b 6301 case bp_static_tracepoint:
e7e0cddf 6302 case bp_dprintf:
4efc6507 6303 case bp_jit_event:
0e30163f
JK
6304 case bp_gnu_ifunc_resolver:
6305 case bp_gnu_ifunc_resolver_return:
79a45b7d 6306 if (opts.addressprint)
3086aeae
DJ
6307 {
6308 annotate_field (4);
54e52265 6309 if (header_of_multiple)
0d381245 6310 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6311 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6312 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6313 else
5af949e3
UW
6314 ui_out_field_core_addr (uiout, "addr",
6315 loc->gdbarch, loc->address);
3086aeae
DJ
6316 }
6317 annotate_field (5);
0d381245 6318 if (!header_of_multiple)
170b53b2 6319 print_breakpoint_location (b, loc);
0d381245 6320 if (b->loc)
a6d9a66e 6321 *last_loc = b->loc;
3086aeae
DJ
6322 break;
6323 }
c906108c 6324
6c95b8df 6325
998580f1 6326 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6327 {
6328 struct inferior *inf;
998580f1
MK
6329 VEC(int) *inf_num = NULL;
6330 int mi_only = 1;
6c95b8df 6331
998580f1 6332 ALL_INFERIORS (inf)
6c95b8df
PA
6333 {
6334 if (inf->pspace == loc->pspace)
998580f1 6335 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6336 }
998580f1
MK
6337
6338 /* For backward compatibility, don't display inferiors in CLI unless
6339 there are several. Always display for MI. */
6340 if (allflag
6341 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6342 && (number_of_program_spaces () > 1
6343 || number_of_inferiors () > 1)
6344 /* LOC is for existing B, it cannot be in
6345 moribund_locations and thus having NULL OWNER. */
6346 && loc->owner->type != bp_catchpoint))
6347 mi_only = 0;
6348 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6349 VEC_free (int, inf_num);
6c95b8df
PA
6350 }
6351
4a306c9a 6352 if (!part_of_multiple)
c4093a6a 6353 {
4a306c9a
JB
6354 if (b->thread != -1)
6355 {
6356 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6357 "stop only in" line a little further down. */
4a306c9a
JB
6358 ui_out_text (uiout, " thread ");
6359 ui_out_field_int (uiout, "thread", b->thread);
6360 }
6361 else if (b->task != 0)
6362 {
6363 ui_out_text (uiout, " task ");
6364 ui_out_field_int (uiout, "task", b->task);
6365 }
c4093a6a 6366 }
f1310107 6367
8b93c638 6368 ui_out_text (uiout, "\n");
f1310107 6369
348d480f 6370 if (!part_of_multiple)
f1310107
TJB
6371 b->ops->print_one_detail (b, uiout);
6372
0d381245 6373 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6374 {
6375 annotate_field (6);
8b93c638 6376 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6377 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6378 the frame ID. */
5af949e3
UW
6379 ui_out_field_core_addr (uiout, "frame",
6380 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6381 ui_out_text (uiout, "\n");
c4093a6a
JM
6382 }
6383
28010a5d 6384 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6385 {
6386 annotate_field (7);
d77f58be 6387 if (is_tracepoint (b))
1042e4c0
SS
6388 ui_out_text (uiout, "\ttrace only if ");
6389 else
6390 ui_out_text (uiout, "\tstop only if ");
0101ce28 6391 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6392
6393 /* Print whether the target is doing the breakpoint's condition
6394 evaluation. If GDB is doing the evaluation, don't print anything. */
6395 if (is_breakpoint (b)
6396 && breakpoint_condition_evaluation_mode ()
6397 == condition_evaluation_target)
6398 {
6399 ui_out_text (uiout, " (");
6400 ui_out_field_string (uiout, "evaluated-by",
6401 bp_condition_evaluator (b));
6402 ui_out_text (uiout, " evals)");
6403 }
0101ce28
JJ
6404 ui_out_text (uiout, "\n");
6405 }
6406
0d381245 6407 if (!part_of_multiple && b->thread != -1)
c4093a6a 6408 {
4a64f543 6409 /* FIXME should make an annotation for this. */
8b93c638
JM
6410 ui_out_text (uiout, "\tstop only in thread ");
6411 ui_out_field_int (uiout, "thread", b->thread);
6412 ui_out_text (uiout, "\n");
c4093a6a
JM
6413 }
6414
556ec64d
YQ
6415 if (!part_of_multiple)
6416 {
6417 if (b->hit_count)
31f56a27
YQ
6418 {
6419 /* FIXME should make an annotation for this. */
6420 if (is_catchpoint (b))
6421 ui_out_text (uiout, "\tcatchpoint");
6422 else if (is_tracepoint (b))
6423 ui_out_text (uiout, "\ttracepoint");
6424 else
6425 ui_out_text (uiout, "\tbreakpoint");
6426 ui_out_text (uiout, " already hit ");
6427 ui_out_field_int (uiout, "times", b->hit_count);
6428 if (b->hit_count == 1)
6429 ui_out_text (uiout, " time\n");
6430 else
6431 ui_out_text (uiout, " times\n");
6432 }
556ec64d
YQ
6433 else
6434 {
31f56a27
YQ
6435 /* Output the count also if it is zero, but only if this is mi. */
6436 if (ui_out_is_mi_like_p (uiout))
6437 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6438 }
6439 }
8b93c638 6440
0d381245 6441 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6442 {
6443 annotate_field (8);
8b93c638
JM
6444 ui_out_text (uiout, "\tignore next ");
6445 ui_out_field_int (uiout, "ignore", b->ignore_count);
6446 ui_out_text (uiout, " hits\n");
c4093a6a 6447 }
059fb39f 6448
816338b5
SS
6449 /* Note that an enable count of 1 corresponds to "enable once"
6450 behavior, which is reported by the combination of enablement and
6451 disposition, so we don't need to mention it here. */
6452 if (!part_of_multiple && b->enable_count > 1)
6453 {
6454 annotate_field (8);
6455 ui_out_text (uiout, "\tdisable after ");
6456 /* Tweak the wording to clarify that ignore and enable counts
6457 are distinct, and have additive effect. */
6458 if (b->ignore_count)
6459 ui_out_text (uiout, "additional ");
6460 else
6461 ui_out_text (uiout, "next ");
6462 ui_out_field_int (uiout, "enable", b->enable_count);
6463 ui_out_text (uiout, " hits\n");
6464 }
6465
f196051f
SS
6466 if (!part_of_multiple && is_tracepoint (b))
6467 {
6468 struct tracepoint *tp = (struct tracepoint *) b;
6469
6470 if (tp->traceframe_usage)
6471 {
6472 ui_out_text (uiout, "\ttrace buffer usage ");
6473 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6474 ui_out_text (uiout, " bytes\n");
6475 }
6476 }
d3ce09f5 6477
9add0f1b 6478 l = b->commands ? b->commands->commands : NULL;
059fb39f 6479 if (!part_of_multiple && l)
c4093a6a 6480 {
3b31d625
EZ
6481 struct cleanup *script_chain;
6482
c4093a6a 6483 annotate_field (9);
3b31d625 6484 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6485 print_command_lines (uiout, l, 4);
3b31d625 6486 do_cleanups (script_chain);
c4093a6a 6487 }
d24317b4 6488
d9b3f62e 6489 if (is_tracepoint (b))
1042e4c0 6490 {
d9b3f62e
PA
6491 struct tracepoint *t = (struct tracepoint *) b;
6492
6493 if (!part_of_multiple && t->pass_count)
6494 {
6495 annotate_field (10);
6496 ui_out_text (uiout, "\tpass count ");
6497 ui_out_field_int (uiout, "pass", t->pass_count);
6498 ui_out_text (uiout, " \n");
6499 }
f2a8bc8a
YQ
6500
6501 /* Don't display it when tracepoint or tracepoint location is
6502 pending. */
6503 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6504 {
6505 annotate_field (11);
6506
6507 if (ui_out_is_mi_like_p (uiout))
6508 ui_out_field_string (uiout, "installed",
6509 loc->inserted ? "y" : "n");
6510 else
6511 {
6512 if (loc->inserted)
6513 ui_out_text (uiout, "\t");
6514 else
6515 ui_out_text (uiout, "\tnot ");
6516 ui_out_text (uiout, "installed on target\n");
6517 }
6518 }
1042e4c0
SS
6519 }
6520
d24317b4
VP
6521 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6522 {
3a5c3e22
PA
6523 if (is_watchpoint (b))
6524 {
6525 struct watchpoint *w = (struct watchpoint *) b;
6526
6527 ui_out_field_string (uiout, "original-location", w->exp_string);
6528 }
6529 else if (b->addr_string)
d24317b4 6530 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6531 }
c4093a6a 6532}
c5aa993b 6533
0d381245
VP
6534static void
6535print_one_breakpoint (struct breakpoint *b,
4a64f543 6536 struct bp_location **last_loc,
6c95b8df 6537 int allflag)
0d381245 6538{
8d3788bd 6539 struct cleanup *bkpt_chain;
79a45e25 6540 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6541
6542 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6543
12c5a436 6544 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6545 do_cleanups (bkpt_chain);
0d381245
VP
6546
6547 /* If this breakpoint has custom print function,
6548 it's already printed. Otherwise, print individual
6549 locations, if any. */
6550 if (b->ops == NULL || b->ops->print_one == NULL)
6551 {
4a64f543
MS
6552 /* If breakpoint has a single location that is disabled, we
6553 print it as if it had several locations, since otherwise it's
6554 hard to represent "breakpoint enabled, location disabled"
6555 situation.
6556
6557 Note that while hardware watchpoints have several locations
a3be7890 6558 internally, that's not a property exposed to user. */
0d381245 6559 if (b->loc
a5606eee 6560 && !is_hardware_watchpoint (b)
8d3788bd 6561 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6562 {
6563 struct bp_location *loc;
6564 int n = 1;
8d3788bd 6565
0d381245 6566 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6567 {
6568 struct cleanup *inner2 =
6569 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6570 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6571 do_cleanups (inner2);
6572 }
0d381245
VP
6573 }
6574 }
6575}
6576
a6d9a66e
UW
6577static int
6578breakpoint_address_bits (struct breakpoint *b)
6579{
6580 int print_address_bits = 0;
6581 struct bp_location *loc;
6582
6583 for (loc = b->loc; loc; loc = loc->next)
6584 {
c7437ca6
PA
6585 int addr_bit;
6586
6587 /* Software watchpoints that aren't watching memory don't have
6588 an address to print. */
6589 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6590 continue;
6591
6592 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6593 if (addr_bit > print_address_bits)
6594 print_address_bits = addr_bit;
6595 }
6596
6597 return print_address_bits;
6598}
0d381245 6599
c4093a6a
JM
6600struct captured_breakpoint_query_args
6601 {
6602 int bnum;
6603 };
c5aa993b 6604
c4093a6a 6605static int
2b65245e 6606do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6607{
6608 struct captured_breakpoint_query_args *args = data;
52f0bd74 6609 struct breakpoint *b;
a6d9a66e 6610 struct bp_location *dummy_loc = NULL;
cc59ec59 6611
c4093a6a
JM
6612 ALL_BREAKPOINTS (b)
6613 {
6614 if (args->bnum == b->number)
c5aa993b 6615 {
12c5a436 6616 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6617 return GDB_RC_OK;
c5aa993b 6618 }
c4093a6a
JM
6619 }
6620 return GDB_RC_NONE;
6621}
c5aa993b 6622
c4093a6a 6623enum gdb_rc
4a64f543
MS
6624gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6625 char **error_message)
c4093a6a
JM
6626{
6627 struct captured_breakpoint_query_args args;
cc59ec59 6628
c4093a6a
JM
6629 args.bnum = bnum;
6630 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6631 an error. */
b0b13bb4
DJ
6632 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6633 error_message, RETURN_MASK_ALL) < 0)
6634 return GDB_RC_FAIL;
6635 else
6636 return GDB_RC_OK;
c4093a6a 6637}
c5aa993b 6638
09d682a4
TT
6639/* Return true if this breakpoint was set by the user, false if it is
6640 internal or momentary. */
6641
6642int
6643user_breakpoint_p (struct breakpoint *b)
6644{
46c6471b 6645 return b->number > 0;
09d682a4
TT
6646}
6647
7f3b0473 6648/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6649 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6650 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6651 FILTER is non-NULL, call it on each breakpoint and only include the
6652 ones for which it returns non-zero. Return the total number of
6653 breakpoints listed. */
c906108c 6654
d77f58be 6655static int
e5a67952 6656breakpoint_1 (char *args, int allflag,
4a64f543 6657 int (*filter) (const struct breakpoint *))
c4093a6a 6658{
52f0bd74 6659 struct breakpoint *b;
a6d9a66e 6660 struct bp_location *last_loc = NULL;
7f3b0473 6661 int nr_printable_breakpoints;
3b31d625 6662 struct cleanup *bkpttbl_chain;
79a45b7d 6663 struct value_print_options opts;
a6d9a66e 6664 int print_address_bits = 0;
269b11a2 6665 int print_type_col_width = 14;
79a45e25 6666 struct ui_out *uiout = current_uiout;
269b11a2 6667
79a45b7d
TT
6668 get_user_print_options (&opts);
6669
4a64f543
MS
6670 /* Compute the number of rows in the table, as well as the size
6671 required for address fields. */
7f3b0473
AC
6672 nr_printable_breakpoints = 0;
6673 ALL_BREAKPOINTS (b)
e5a67952
MS
6674 {
6675 /* If we have a filter, only list the breakpoints it accepts. */
6676 if (filter && !filter (b))
6677 continue;
6678
6679 /* If we have an "args" string, it is a list of breakpoints to
6680 accept. Skip the others. */
6681 if (args != NULL && *args != '\0')
6682 {
6683 if (allflag && parse_and_eval_long (args) != b->number)
6684 continue;
6685 if (!allflag && !number_is_in_list (args, b->number))
6686 continue;
6687 }
269b11a2 6688
e5a67952
MS
6689 if (allflag || user_breakpoint_p (b))
6690 {
6691 int addr_bit, type_len;
a6d9a66e 6692
e5a67952
MS
6693 addr_bit = breakpoint_address_bits (b);
6694 if (addr_bit > print_address_bits)
6695 print_address_bits = addr_bit;
269b11a2 6696
e5a67952
MS
6697 type_len = strlen (bptype_string (b->type));
6698 if (type_len > print_type_col_width)
6699 print_type_col_width = type_len;
6700
6701 nr_printable_breakpoints++;
6702 }
6703 }
7f3b0473 6704
79a45b7d 6705 if (opts.addressprint)
3b31d625 6706 bkpttbl_chain
3e43a32a
MS
6707 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6708 nr_printable_breakpoints,
3b31d625 6709 "BreakpointTable");
8b93c638 6710 else
3b31d625 6711 bkpttbl_chain
3e43a32a
MS
6712 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6713 nr_printable_breakpoints,
3b31d625 6714 "BreakpointTable");
8b93c638 6715
7f3b0473 6716 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6717 annotate_breakpoints_headers ();
6718 if (nr_printable_breakpoints > 0)
6719 annotate_field (0);
4a64f543 6720 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6721 if (nr_printable_breakpoints > 0)
6722 annotate_field (1);
269b11a2 6723 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6724 "type", "Type"); /* 2 */
d7faa9e7
AC
6725 if (nr_printable_breakpoints > 0)
6726 annotate_field (2);
4a64f543 6727 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6728 if (nr_printable_breakpoints > 0)
6729 annotate_field (3);
54e52265 6730 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6731 if (opts.addressprint)
e5a67952
MS
6732 {
6733 if (nr_printable_breakpoints > 0)
6734 annotate_field (4);
6735 if (print_address_bits <= 32)
6736 ui_out_table_header (uiout, 10, ui_left,
6737 "addr", "Address"); /* 5 */
6738 else
6739 ui_out_table_header (uiout, 18, ui_left,
6740 "addr", "Address"); /* 5 */
6741 }
d7faa9e7
AC
6742 if (nr_printable_breakpoints > 0)
6743 annotate_field (5);
6744 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6745 ui_out_table_body (uiout);
6746 if (nr_printable_breakpoints > 0)
6747 annotate_breakpoints_table ();
7f3b0473 6748
c4093a6a 6749 ALL_BREAKPOINTS (b)
e5a67952
MS
6750 {
6751 QUIT;
6752 /* If we have a filter, only list the breakpoints it accepts. */
6753 if (filter && !filter (b))
6754 continue;
6755
6756 /* If we have an "args" string, it is a list of breakpoints to
6757 accept. Skip the others. */
6758
6759 if (args != NULL && *args != '\0')
6760 {
6761 if (allflag) /* maintenance info breakpoint */
6762 {
6763 if (parse_and_eval_long (args) != b->number)
6764 continue;
6765 }
6766 else /* all others */
6767 {
6768 if (!number_is_in_list (args, b->number))
6769 continue;
6770 }
6771 }
6772 /* We only print out user settable breakpoints unless the
6773 allflag is set. */
6774 if (allflag || user_breakpoint_p (b))
12c5a436 6775 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6776 }
6777
3b31d625 6778 do_cleanups (bkpttbl_chain);
698384cd 6779
7f3b0473 6780 if (nr_printable_breakpoints == 0)
c906108c 6781 {
4a64f543
MS
6782 /* If there's a filter, let the caller decide how to report
6783 empty list. */
d77f58be
SS
6784 if (!filter)
6785 {
e5a67952 6786 if (args == NULL || *args == '\0')
d77f58be
SS
6787 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6788 else
4a64f543 6789 ui_out_message (uiout, 0,
e5a67952
MS
6790 "No breakpoint or watchpoint matching '%s'.\n",
6791 args);
d77f58be 6792 }
c906108c
SS
6793 }
6794 else
c4093a6a 6795 {
a6d9a66e
UW
6796 if (last_loc && !server_command)
6797 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6798 }
c906108c 6799
4a64f543 6800 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6801 there have been breakpoints? */
c906108c 6802 annotate_breakpoints_table_end ();
d77f58be
SS
6803
6804 return nr_printable_breakpoints;
c906108c
SS
6805}
6806
ad443146
SS
6807/* Display the value of default-collect in a way that is generally
6808 compatible with the breakpoint list. */
6809
6810static void
6811default_collect_info (void)
6812{
79a45e25
PA
6813 struct ui_out *uiout = current_uiout;
6814
ad443146
SS
6815 /* If it has no value (which is frequently the case), say nothing; a
6816 message like "No default-collect." gets in user's face when it's
6817 not wanted. */
6818 if (!*default_collect)
6819 return;
6820
6821 /* The following phrase lines up nicely with per-tracepoint collect
6822 actions. */
6823 ui_out_text (uiout, "default collect ");
6824 ui_out_field_string (uiout, "default-collect", default_collect);
6825 ui_out_text (uiout, " \n");
6826}
6827
c906108c 6828static void
e5a67952 6829breakpoints_info (char *args, int from_tty)
c906108c 6830{
e5a67952 6831 breakpoint_1 (args, 0, NULL);
ad443146
SS
6832
6833 default_collect_info ();
d77f58be
SS
6834}
6835
6836static void
e5a67952 6837watchpoints_info (char *args, int from_tty)
d77f58be 6838{
e5a67952 6839 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6840 struct ui_out *uiout = current_uiout;
d77f58be
SS
6841
6842 if (num_printed == 0)
6843 {
e5a67952 6844 if (args == NULL || *args == '\0')
d77f58be
SS
6845 ui_out_message (uiout, 0, "No watchpoints.\n");
6846 else
e5a67952 6847 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6848 }
c906108c
SS
6849}
6850
7a292a7a 6851static void
e5a67952 6852maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6853{
e5a67952 6854 breakpoint_1 (args, 1, NULL);
ad443146
SS
6855
6856 default_collect_info ();
c906108c
SS
6857}
6858
0d381245 6859static int
714835d5 6860breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6861 struct program_space *pspace,
714835d5 6862 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6863{
6864 struct bp_location *bl = b->loc;
cc59ec59 6865
0d381245
VP
6866 for (; bl; bl = bl->next)
6867 {
6c95b8df
PA
6868 if (bl->pspace == pspace
6869 && bl->address == pc
0d381245
VP
6870 && (!overlay_debugging || bl->section == section))
6871 return 1;
6872 }
6873 return 0;
6874}
6875
672f9b60 6876/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6877 concerns with logical breakpoints, so we match program spaces, not
6878 address spaces. */
c906108c
SS
6879
6880static void
6c95b8df
PA
6881describe_other_breakpoints (struct gdbarch *gdbarch,
6882 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6883 struct obj_section *section, int thread)
c906108c 6884{
52f0bd74
AC
6885 int others = 0;
6886 struct breakpoint *b;
c906108c
SS
6887
6888 ALL_BREAKPOINTS (b)
672f9b60
KP
6889 others += (user_breakpoint_p (b)
6890 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6891 if (others > 0)
6892 {
a3f17187
AC
6893 if (others == 1)
6894 printf_filtered (_("Note: breakpoint "));
6895 else /* if (others == ???) */
6896 printf_filtered (_("Note: breakpoints "));
c906108c 6897 ALL_BREAKPOINTS (b)
672f9b60 6898 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6899 {
6900 others--;
6901 printf_filtered ("%d", b->number);
6902 if (b->thread == -1 && thread != -1)
6903 printf_filtered (" (all threads)");
6904 else if (b->thread != -1)
6905 printf_filtered (" (thread %d)", b->thread);
6906 printf_filtered ("%s%s ",
059fb39f 6907 ((b->enable_state == bp_disabled
f8eba3c6 6908 || b->enable_state == bp_call_disabled)
0d381245 6909 ? " (disabled)"
0d381245
VP
6910 : ""),
6911 (others > 1) ? ","
6912 : ((others == 1) ? " and" : ""));
6913 }
a3f17187 6914 printf_filtered (_("also set at pc "));
5af949e3 6915 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6916 printf_filtered (".\n");
6917 }
6918}
6919\f
c906108c 6920
e4f237da
KB
6921/* Return true iff it is meaningful to use the address member of
6922 BPT. For some breakpoint types, the address member is irrelevant
6923 and it makes no sense to attempt to compare it to other addresses
6924 (or use it for any other purpose either).
6925
4a64f543
MS
6926 More specifically, each of the following breakpoint types will
6927 always have a zero valued address and we don't want to mark
6928 breakpoints of any of these types to be a duplicate of an actual
6929 breakpoint at address zero:
e4f237da
KB
6930
6931 bp_watchpoint
2d134ed3
PA
6932 bp_catchpoint
6933
6934*/
e4f237da
KB
6935
6936static int
6937breakpoint_address_is_meaningful (struct breakpoint *bpt)
6938{
6939 enum bptype type = bpt->type;
6940
2d134ed3
PA
6941 return (type != bp_watchpoint && type != bp_catchpoint);
6942}
6943
6944/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6945 true if LOC1 and LOC2 represent the same watchpoint location. */
6946
6947static int
4a64f543
MS
6948watchpoint_locations_match (struct bp_location *loc1,
6949 struct bp_location *loc2)
2d134ed3 6950{
3a5c3e22
PA
6951 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6952 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6953
6954 /* Both of them must exist. */
6955 gdb_assert (w1 != NULL);
6956 gdb_assert (w2 != NULL);
2bdf28a0 6957
4a64f543
MS
6958 /* If the target can evaluate the condition expression in hardware,
6959 then we we need to insert both watchpoints even if they are at
6960 the same place. Otherwise the watchpoint will only trigger when
6961 the condition of whichever watchpoint was inserted evaluates to
6962 true, not giving a chance for GDB to check the condition of the
6963 other watchpoint. */
3a5c3e22 6964 if ((w1->cond_exp
4a64f543
MS
6965 && target_can_accel_watchpoint_condition (loc1->address,
6966 loc1->length,
0cf6dd15 6967 loc1->watchpoint_type,
3a5c3e22
PA
6968 w1->cond_exp))
6969 || (w2->cond_exp
4a64f543
MS
6970 && target_can_accel_watchpoint_condition (loc2->address,
6971 loc2->length,
0cf6dd15 6972 loc2->watchpoint_type,
3a5c3e22 6973 w2->cond_exp)))
0cf6dd15
TJB
6974 return 0;
6975
85d721b8
PA
6976 /* Note that this checks the owner's type, not the location's. In
6977 case the target does not support read watchpoints, but does
6978 support access watchpoints, we'll have bp_read_watchpoint
6979 watchpoints with hw_access locations. Those should be considered
6980 duplicates of hw_read locations. The hw_read locations will
6981 become hw_access locations later. */
2d134ed3
PA
6982 return (loc1->owner->type == loc2->owner->type
6983 && loc1->pspace->aspace == loc2->pspace->aspace
6984 && loc1->address == loc2->address
6985 && loc1->length == loc2->length);
e4f237da
KB
6986}
6987
31e77af2 6988/* See breakpoint.h. */
6c95b8df 6989
31e77af2 6990int
6c95b8df
PA
6991breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6992 struct address_space *aspace2, CORE_ADDR addr2)
6993{
f5656ead 6994 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6995 || aspace1 == aspace2)
6996 && addr1 == addr2);
6997}
6998
f1310107
TJB
6999/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7000 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7001 matches ASPACE2. On targets that have global breakpoints, the address
7002 space doesn't really matter. */
7003
7004static int
7005breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7006 int len1, struct address_space *aspace2,
7007 CORE_ADDR addr2)
7008{
f5656ead 7009 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7010 || aspace1 == aspace2)
7011 && addr2 >= addr1 && addr2 < addr1 + len1);
7012}
7013
7014/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7015 a ranged breakpoint. In most targets, a match happens only if ASPACE
7016 matches the breakpoint's address space. On targets that have global
7017 breakpoints, the address space doesn't really matter. */
7018
7019static int
7020breakpoint_location_address_match (struct bp_location *bl,
7021 struct address_space *aspace,
7022 CORE_ADDR addr)
7023{
7024 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7025 aspace, addr)
7026 || (bl->length
7027 && breakpoint_address_match_range (bl->pspace->aspace,
7028 bl->address, bl->length,
7029 aspace, addr)));
7030}
7031
1e4d1764
YQ
7032/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7033 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7034 true, otherwise returns false. */
7035
7036static int
7037tracepoint_locations_match (struct bp_location *loc1,
7038 struct bp_location *loc2)
7039{
7040 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7041 /* Since tracepoint locations are never duplicated with others', tracepoint
7042 locations at the same address of different tracepoints are regarded as
7043 different locations. */
7044 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7045 else
7046 return 0;
7047}
7048
2d134ed3
PA
7049/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7050 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7051 represent the same location. */
7052
7053static int
4a64f543
MS
7054breakpoint_locations_match (struct bp_location *loc1,
7055 struct bp_location *loc2)
2d134ed3 7056{
2bdf28a0
JK
7057 int hw_point1, hw_point2;
7058
7059 /* Both of them must not be in moribund_locations. */
7060 gdb_assert (loc1->owner != NULL);
7061 gdb_assert (loc2->owner != NULL);
7062
7063 hw_point1 = is_hardware_watchpoint (loc1->owner);
7064 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7065
7066 if (hw_point1 != hw_point2)
7067 return 0;
7068 else if (hw_point1)
7069 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7070 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7071 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7072 else
f1310107
TJB
7073 /* We compare bp_location.length in order to cover ranged breakpoints. */
7074 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7075 loc2->pspace->aspace, loc2->address)
7076 && loc1->length == loc2->length);
2d134ed3
PA
7077}
7078
76897487
KB
7079static void
7080breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7081 int bnum, int have_bnum)
7082{
f63fbe86
MS
7083 /* The longest string possibly returned by hex_string_custom
7084 is 50 chars. These must be at least that big for safety. */
7085 char astr1[64];
7086 char astr2[64];
76897487 7087
bb599908
PH
7088 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7089 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7090 if (have_bnum)
8a3fe4f8 7091 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7092 bnum, astr1, astr2);
7093 else
8a3fe4f8 7094 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7095}
7096
4a64f543
MS
7097/* Adjust a breakpoint's address to account for architectural
7098 constraints on breakpoint placement. Return the adjusted address.
7099 Note: Very few targets require this kind of adjustment. For most
7100 targets, this function is simply the identity function. */
76897487
KB
7101
7102static CORE_ADDR
a6d9a66e
UW
7103adjust_breakpoint_address (struct gdbarch *gdbarch,
7104 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7105{
a6d9a66e 7106 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7107 {
7108 /* Very few targets need any kind of breakpoint adjustment. */
7109 return bpaddr;
7110 }
88f7da05
KB
7111 else if (bptype == bp_watchpoint
7112 || bptype == bp_hardware_watchpoint
7113 || bptype == bp_read_watchpoint
7114 || bptype == bp_access_watchpoint
fe798b75 7115 || bptype == bp_catchpoint)
88f7da05
KB
7116 {
7117 /* Watchpoints and the various bp_catch_* eventpoints should not
7118 have their addresses modified. */
7119 return bpaddr;
7120 }
7c16b83e
PA
7121 else if (bptype == bp_single_step)
7122 {
7123 /* Single-step breakpoints should not have their addresses
7124 modified. If there's any architectural constrain that
7125 applies to this address, then it should have already been
7126 taken into account when the breakpoint was created in the
7127 first place. If we didn't do this, stepping through e.g.,
7128 Thumb-2 IT blocks would break. */
7129 return bpaddr;
7130 }
76897487
KB
7131 else
7132 {
7133 CORE_ADDR adjusted_bpaddr;
7134
7135 /* Some targets have architectural constraints on the placement
7136 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7137 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7138
7139 /* An adjusted breakpoint address can significantly alter
7140 a user's expectations. Print a warning if an adjustment
7141 is required. */
7142 if (adjusted_bpaddr != bpaddr)
7143 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7144
7145 return adjusted_bpaddr;
7146 }
7147}
7148
28010a5d
PA
7149void
7150init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7151 struct breakpoint *owner)
7cc221ef 7152{
7cc221ef
DJ
7153 memset (loc, 0, sizeof (*loc));
7154
348d480f
PA
7155 gdb_assert (ops != NULL);
7156
28010a5d
PA
7157 loc->ops = ops;
7158 loc->owner = owner;
511a6cd4 7159 loc->cond = NULL;
b775012e 7160 loc->cond_bytecode = NULL;
0d381245
VP
7161 loc->shlib_disabled = 0;
7162 loc->enabled = 1;
e049a4b5 7163
28010a5d 7164 switch (owner->type)
e049a4b5
DJ
7165 {
7166 case bp_breakpoint:
7c16b83e 7167 case bp_single_step:
e049a4b5
DJ
7168 case bp_until:
7169 case bp_finish:
7170 case bp_longjmp:
7171 case bp_longjmp_resume:
e2e4d78b 7172 case bp_longjmp_call_dummy:
186c406b
TT
7173 case bp_exception:
7174 case bp_exception_resume:
e049a4b5 7175 case bp_step_resume:
2c03e5be 7176 case bp_hp_step_resume:
e049a4b5
DJ
7177 case bp_watchpoint_scope:
7178 case bp_call_dummy:
aa7d318d 7179 case bp_std_terminate:
e049a4b5
DJ
7180 case bp_shlib_event:
7181 case bp_thread_event:
7182 case bp_overlay_event:
4efc6507 7183 case bp_jit_event:
0fd8e87f 7184 case bp_longjmp_master:
aa7d318d 7185 case bp_std_terminate_master:
186c406b 7186 case bp_exception_master:
0e30163f
JK
7187 case bp_gnu_ifunc_resolver:
7188 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7189 case bp_dprintf:
e049a4b5 7190 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7191 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7192 break;
7193 case bp_hardware_breakpoint:
7194 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7195 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7196 break;
7197 case bp_hardware_watchpoint:
7198 case bp_read_watchpoint:
7199 case bp_access_watchpoint:
7200 loc->loc_type = bp_loc_hardware_watchpoint;
7201 break;
7202 case bp_watchpoint:
ce78b96d 7203 case bp_catchpoint:
15c3d785
PA
7204 case bp_tracepoint:
7205 case bp_fast_tracepoint:
0fb4aa4b 7206 case bp_static_tracepoint:
e049a4b5
DJ
7207 loc->loc_type = bp_loc_other;
7208 break;
7209 default:
e2e0b3e5 7210 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7211 }
7212
f431efe5 7213 loc->refc = 1;
28010a5d
PA
7214}
7215
7216/* Allocate a struct bp_location. */
7217
7218static struct bp_location *
7219allocate_bp_location (struct breakpoint *bpt)
7220{
348d480f
PA
7221 return bpt->ops->allocate_location (bpt);
7222}
7cc221ef 7223
f431efe5
PA
7224static void
7225free_bp_location (struct bp_location *loc)
fe3f5fa8 7226{
348d480f 7227 loc->ops->dtor (loc);
fe3f5fa8
VP
7228 xfree (loc);
7229}
7230
f431efe5
PA
7231/* Increment reference count. */
7232
7233static void
7234incref_bp_location (struct bp_location *bl)
7235{
7236 ++bl->refc;
7237}
7238
7239/* Decrement reference count. If the reference count reaches 0,
7240 destroy the bp_location. Sets *BLP to NULL. */
7241
7242static void
7243decref_bp_location (struct bp_location **blp)
7244{
0807b50c
PA
7245 gdb_assert ((*blp)->refc > 0);
7246
f431efe5
PA
7247 if (--(*blp)->refc == 0)
7248 free_bp_location (*blp);
7249 *blp = NULL;
7250}
7251
346774a9 7252/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7253
346774a9
PA
7254static void
7255add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7256{
346774a9 7257 struct breakpoint *b1;
c906108c 7258
346774a9
PA
7259 /* Add this breakpoint to the end of the chain so that a list of
7260 breakpoints will come out in order of increasing numbers. */
7261
7262 b1 = breakpoint_chain;
7263 if (b1 == 0)
7264 breakpoint_chain = b;
7265 else
7266 {
7267 while (b1->next)
7268 b1 = b1->next;
7269 b1->next = b;
7270 }
7271}
7272
7273/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7274
7275static void
7276init_raw_breakpoint_without_location (struct breakpoint *b,
7277 struct gdbarch *gdbarch,
28010a5d 7278 enum bptype bptype,
c0a91b2b 7279 const struct breakpoint_ops *ops)
346774a9 7280{
c906108c 7281 memset (b, 0, sizeof (*b));
2219d63c 7282
348d480f
PA
7283 gdb_assert (ops != NULL);
7284
28010a5d 7285 b->ops = ops;
4d28f7a8 7286 b->type = bptype;
a6d9a66e 7287 b->gdbarch = gdbarch;
c906108c
SS
7288 b->language = current_language->la_language;
7289 b->input_radix = input_radix;
7290 b->thread = -1;
b5de0fa7 7291 b->enable_state = bp_enabled;
c906108c
SS
7292 b->next = 0;
7293 b->silent = 0;
7294 b->ignore_count = 0;
7295 b->commands = NULL;
818dd999 7296 b->frame_id = null_frame_id;
0d381245 7297 b->condition_not_parsed = 0;
84f4c1fe 7298 b->py_bp_object = NULL;
d0fb5eae 7299 b->related_breakpoint = b;
346774a9
PA
7300}
7301
7302/* Helper to set_raw_breakpoint below. Creates a breakpoint
7303 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7304
7305static struct breakpoint *
7306set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7307 enum bptype bptype,
c0a91b2b 7308 const struct breakpoint_ops *ops)
346774a9
PA
7309{
7310 struct breakpoint *b = XNEW (struct breakpoint);
7311
348d480f 7312 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7313 add_to_breakpoint_chain (b);
0d381245
VP
7314 return b;
7315}
7316
0e30163f
JK
7317/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7318 resolutions should be made as the user specified the location explicitly
7319 enough. */
7320
0d381245 7321static void
0e30163f 7322set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7323{
2bdf28a0
JK
7324 gdb_assert (loc->owner != NULL);
7325
0d381245 7326 if (loc->owner->type == bp_breakpoint
1042e4c0 7327 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7328 || is_tracepoint (loc->owner))
0d381245 7329 {
0e30163f 7330 int is_gnu_ifunc;
2c02bd72 7331 const char *function_name;
6a3a010b 7332 CORE_ADDR func_addr;
0e30163f 7333
2c02bd72 7334 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7335 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7336
7337 if (is_gnu_ifunc && !explicit_loc)
7338 {
7339 struct breakpoint *b = loc->owner;
7340
7341 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7342 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7343 &loc->requested_address))
7344 {
7345 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7346 loc->address = adjust_breakpoint_address (loc->gdbarch,
7347 loc->requested_address,
7348 b->type);
7349 }
7350 else if (b->type == bp_breakpoint && b->loc == loc
7351 && loc->next == NULL && b->related_breakpoint == b)
7352 {
7353 /* Create only the whole new breakpoint of this type but do not
7354 mess more complicated breakpoints with multiple locations. */
7355 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7356 /* Remember the resolver's address for use by the return
7357 breakpoint. */
7358 loc->related_address = func_addr;
0e30163f
JK
7359 }
7360 }
7361
2c02bd72
DE
7362 if (function_name)
7363 loc->function_name = xstrdup (function_name);
0d381245
VP
7364 }
7365}
7366
a6d9a66e 7367/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7368struct gdbarch *
a6d9a66e
UW
7369get_sal_arch (struct symtab_and_line sal)
7370{
7371 if (sal.section)
7372 return get_objfile_arch (sal.section->objfile);
7373 if (sal.symtab)
eb822aa6 7374 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7375
7376 return NULL;
7377}
7378
346774a9
PA
7379/* Low level routine for partially initializing a breakpoint of type
7380 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7381 file name, and line number are provided by SAL.
0d381245
VP
7382
7383 It is expected that the caller will complete the initialization of
7384 the newly created breakpoint struct as well as output any status
c56053d2 7385 information regarding the creation of a new breakpoint. */
0d381245 7386
346774a9
PA
7387static void
7388init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7389 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7390 const struct breakpoint_ops *ops)
0d381245 7391{
28010a5d 7392 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7393
3742cc8b 7394 add_location_to_breakpoint (b, &sal);
0d381245 7395
6c95b8df
PA
7396 if (bptype != bp_catchpoint)
7397 gdb_assert (sal.pspace != NULL);
7398
f8eba3c6
TT
7399 /* Store the program space that was used to set the breakpoint,
7400 except for ordinary breakpoints, which are independent of the
7401 program space. */
7402 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7403 b->pspace = sal.pspace;
346774a9 7404}
c906108c 7405
346774a9
PA
7406/* set_raw_breakpoint is a low level routine for allocating and
7407 partially initializing a breakpoint of type BPTYPE. The newly
7408 created breakpoint's address, section, source file name, and line
7409 number are provided by SAL. The newly created and partially
7410 initialized breakpoint is added to the breakpoint chain and
7411 is also returned as the value of this function.
7412
7413 It is expected that the caller will complete the initialization of
7414 the newly created breakpoint struct as well as output any status
7415 information regarding the creation of a new breakpoint. In
7416 particular, set_raw_breakpoint does NOT set the breakpoint
7417 number! Care should be taken to not allow an error to occur
7418 prior to completing the initialization of the breakpoint. If this
7419 should happen, a bogus breakpoint will be left on the chain. */
7420
7421struct breakpoint *
7422set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7423 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7424 const struct breakpoint_ops *ops)
346774a9
PA
7425{
7426 struct breakpoint *b = XNEW (struct breakpoint);
7427
348d480f 7428 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7429 add_to_breakpoint_chain (b);
c906108c
SS
7430 return b;
7431}
7432
c2c6d25f
JM
7433
7434/* Note that the breakpoint object B describes a permanent breakpoint
7435 instruction, hard-wired into the inferior's code. */
7436void
7437make_breakpoint_permanent (struct breakpoint *b)
7438{
0d381245 7439 struct bp_location *bl;
cc59ec59 7440
4a64f543
MS
7441 /* By definition, permanent breakpoints are already present in the
7442 code. Mark all locations as inserted. For now,
7443 make_breakpoint_permanent is called in just one place, so it's
7444 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7445 multiple locations or not, but it's easy to implement. */
0d381245 7446 for (bl = b->loc; bl; bl = bl->next)
1a853c52
PA
7447 {
7448 bl->permanent = 1;
7449 bl->inserted = 1;
7450 }
c2c6d25f
JM
7451}
7452
53a5351d 7453/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7454 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7455 initiated the operation. */
c906108c
SS
7456
7457void
186c406b 7458set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7459{
35df4500 7460 struct breakpoint *b, *b_tmp;
186c406b 7461 int thread = tp->num;
0fd8e87f
UW
7462
7463 /* To avoid having to rescan all objfile symbols at every step,
7464 we maintain a list of continually-inserted but always disabled
7465 longjmp "master" breakpoints. Here, we simply create momentary
7466 clones of those and enable them for the requested thread. */
35df4500 7467 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7468 if (b->pspace == current_program_space
186c406b
TT
7469 && (b->type == bp_longjmp_master
7470 || b->type == bp_exception_master))
0fd8e87f 7471 {
06edf0c0
PA
7472 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7473 struct breakpoint *clone;
cc59ec59 7474
e2e4d78b
JK
7475 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7476 after their removal. */
06edf0c0 7477 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7478 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7479 clone->thread = thread;
7480 }
186c406b
TT
7481
7482 tp->initiating_frame = frame;
c906108c
SS
7483}
7484
611c83ae 7485/* Delete all longjmp breakpoints from THREAD. */
c906108c 7486void
611c83ae 7487delete_longjmp_breakpoint (int thread)
c906108c 7488{
35df4500 7489 struct breakpoint *b, *b_tmp;
c906108c 7490
35df4500 7491 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7492 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7493 {
7494 if (b->thread == thread)
7495 delete_breakpoint (b);
7496 }
c906108c
SS
7497}
7498
f59f708a
PA
7499void
7500delete_longjmp_breakpoint_at_next_stop (int thread)
7501{
7502 struct breakpoint *b, *b_tmp;
7503
7504 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7505 if (b->type == bp_longjmp || b->type == bp_exception)
7506 {
7507 if (b->thread == thread)
7508 b->disposition = disp_del_at_next_stop;
7509 }
7510}
7511
e2e4d78b
JK
7512/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7513 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7514 pointer to any of them. Return NULL if this system cannot place longjmp
7515 breakpoints. */
7516
7517struct breakpoint *
7518set_longjmp_breakpoint_for_call_dummy (void)
7519{
7520 struct breakpoint *b, *retval = NULL;
7521
7522 ALL_BREAKPOINTS (b)
7523 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7524 {
7525 struct breakpoint *new_b;
7526
7527 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7528 &momentary_breakpoint_ops,
7529 1);
e2e4d78b
JK
7530 new_b->thread = pid_to_thread_id (inferior_ptid);
7531
7532 /* Link NEW_B into the chain of RETVAL breakpoints. */
7533
7534 gdb_assert (new_b->related_breakpoint == new_b);
7535 if (retval == NULL)
7536 retval = new_b;
7537 new_b->related_breakpoint = retval;
7538 while (retval->related_breakpoint != new_b->related_breakpoint)
7539 retval = retval->related_breakpoint;
7540 retval->related_breakpoint = new_b;
7541 }
7542
7543 return retval;
7544}
7545
7546/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7547 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7548 stack.
7549
7550 You should call this function only at places where it is safe to currently
7551 unwind the whole stack. Failed stack unwind would discard live dummy
7552 frames. */
7553
7554void
b67a2c6f 7555check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7556{
7557 struct breakpoint *b, *b_tmp;
7558
7559 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7560 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7561 {
7562 struct breakpoint *dummy_b = b->related_breakpoint;
7563
7564 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7565 dummy_b = dummy_b->related_breakpoint;
7566 if (dummy_b->type != bp_call_dummy
7567 || frame_find_by_id (dummy_b->frame_id) != NULL)
7568 continue;
7569
b67a2c6f 7570 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7571
7572 while (b->related_breakpoint != b)
7573 {
7574 if (b_tmp == b->related_breakpoint)
7575 b_tmp = b->related_breakpoint->next;
7576 delete_breakpoint (b->related_breakpoint);
7577 }
7578 delete_breakpoint (b);
7579 }
7580}
7581
1900040c
MS
7582void
7583enable_overlay_breakpoints (void)
7584{
52f0bd74 7585 struct breakpoint *b;
1900040c
MS
7586
7587 ALL_BREAKPOINTS (b)
7588 if (b->type == bp_overlay_event)
7589 {
7590 b->enable_state = bp_enabled;
44702360 7591 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7592 overlay_events_enabled = 1;
1900040c
MS
7593 }
7594}
7595
7596void
7597disable_overlay_breakpoints (void)
7598{
52f0bd74 7599 struct breakpoint *b;
1900040c
MS
7600
7601 ALL_BREAKPOINTS (b)
7602 if (b->type == bp_overlay_event)
7603 {
7604 b->enable_state = bp_disabled;
44702360 7605 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7606 overlay_events_enabled = 0;
1900040c
MS
7607 }
7608}
7609
aa7d318d
TT
7610/* Set an active std::terminate breakpoint for each std::terminate
7611 master breakpoint. */
7612void
7613set_std_terminate_breakpoint (void)
7614{
35df4500 7615 struct breakpoint *b, *b_tmp;
aa7d318d 7616
35df4500 7617 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7618 if (b->pspace == current_program_space
7619 && b->type == bp_std_terminate_master)
7620 {
06edf0c0 7621 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7622 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7623 }
7624}
7625
7626/* Delete all the std::terminate breakpoints. */
7627void
7628delete_std_terminate_breakpoint (void)
7629{
35df4500 7630 struct breakpoint *b, *b_tmp;
aa7d318d 7631
35df4500 7632 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7633 if (b->type == bp_std_terminate)
7634 delete_breakpoint (b);
7635}
7636
c4093a6a 7637struct breakpoint *
a6d9a66e 7638create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7639{
7640 struct breakpoint *b;
c4093a6a 7641
06edf0c0
PA
7642 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7643 &internal_breakpoint_ops);
7644
b5de0fa7 7645 b->enable_state = bp_enabled;
c4093a6a 7646 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7647 b->addr_string
7648 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7649
44702360 7650 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7651
c4093a6a
JM
7652 return b;
7653}
7654
7655void
7656remove_thread_event_breakpoints (void)
7657{
35df4500 7658 struct breakpoint *b, *b_tmp;
c4093a6a 7659
35df4500 7660 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7661 if (b->type == bp_thread_event
7662 && b->loc->pspace == current_program_space)
c4093a6a
JM
7663 delete_breakpoint (b);
7664}
7665
0101ce28
JJ
7666struct lang_and_radix
7667 {
7668 enum language lang;
7669 int radix;
7670 };
7671
4efc6507
DE
7672/* Create a breakpoint for JIT code registration and unregistration. */
7673
7674struct breakpoint *
7675create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7676{
7677 struct breakpoint *b;
7678
06edf0c0
PA
7679 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7680 &internal_breakpoint_ops);
44702360 7681 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7682 return b;
7683}
0101ce28 7684
03673fc7
PP
7685/* Remove JIT code registration and unregistration breakpoint(s). */
7686
7687void
7688remove_jit_event_breakpoints (void)
7689{
7690 struct breakpoint *b, *b_tmp;
7691
7692 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7693 if (b->type == bp_jit_event
7694 && b->loc->pspace == current_program_space)
7695 delete_breakpoint (b);
7696}
7697
cae688ec
JJ
7698void
7699remove_solib_event_breakpoints (void)
7700{
35df4500 7701 struct breakpoint *b, *b_tmp;
cae688ec 7702
35df4500 7703 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7704 if (b->type == bp_shlib_event
7705 && b->loc->pspace == current_program_space)
cae688ec
JJ
7706 delete_breakpoint (b);
7707}
7708
f37f681c
PA
7709/* See breakpoint.h. */
7710
7711void
7712remove_solib_event_breakpoints_at_next_stop (void)
7713{
7714 struct breakpoint *b, *b_tmp;
7715
7716 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7717 if (b->type == bp_shlib_event
7718 && b->loc->pspace == current_program_space)
7719 b->disposition = disp_del_at_next_stop;
7720}
7721
04086b45
PA
7722/* Helper for create_solib_event_breakpoint /
7723 create_and_insert_solib_event_breakpoint. Allows specifying which
7724 INSERT_MODE to pass through to update_global_location_list. */
7725
7726static struct breakpoint *
7727create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7728 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7729{
7730 struct breakpoint *b;
7731
06edf0c0
PA
7732 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7733 &internal_breakpoint_ops);
04086b45 7734 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7735 return b;
7736}
7737
04086b45
PA
7738struct breakpoint *
7739create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7740{
7741 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7742}
7743
f37f681c
PA
7744/* See breakpoint.h. */
7745
7746struct breakpoint *
7747create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7748{
7749 struct breakpoint *b;
7750
04086b45
PA
7751 /* Explicitly tell update_global_location_list to insert
7752 locations. */
7753 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7754 if (!b->loc->inserted)
7755 {
7756 delete_breakpoint (b);
7757 return NULL;
7758 }
7759 return b;
7760}
7761
cae688ec
JJ
7762/* Disable any breakpoints that are on code in shared libraries. Only
7763 apply to enabled breakpoints, disabled ones can just stay disabled. */
7764
7765void
cb851954 7766disable_breakpoints_in_shlibs (void)
cae688ec 7767{
876fa593 7768 struct bp_location *loc, **locp_tmp;
cae688ec 7769
876fa593 7770 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7771 {
2bdf28a0 7772 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7773 struct breakpoint *b = loc->owner;
2bdf28a0 7774
4a64f543
MS
7775 /* We apply the check to all breakpoints, including disabled for
7776 those with loc->duplicate set. This is so that when breakpoint
7777 becomes enabled, or the duplicate is removed, gdb will try to
7778 insert all breakpoints. If we don't set shlib_disabled here,
7779 we'll try to insert those breakpoints and fail. */
1042e4c0 7780 if (((b->type == bp_breakpoint)
508ccb1f 7781 || (b->type == bp_jit_event)
1042e4c0 7782 || (b->type == bp_hardware_breakpoint)
d77f58be 7783 || (is_tracepoint (b)))
6c95b8df 7784 && loc->pspace == current_program_space
0d381245 7785 && !loc->shlib_disabled
6c95b8df 7786 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7787 )
0d381245
VP
7788 {
7789 loc->shlib_disabled = 1;
7790 }
cae688ec
JJ
7791 }
7792}
7793
63644780
NB
7794/* Disable any breakpoints and tracepoints that are in SOLIB upon
7795 notification of unloaded_shlib. Only apply to enabled breakpoints,
7796 disabled ones can just stay disabled. */
84acb35a 7797
75149521 7798static void
84acb35a
JJ
7799disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7800{
876fa593 7801 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7802 int disabled_shlib_breaks = 0;
7803
c86cf029
VP
7804 /* SunOS a.out shared libraries are always mapped, so do not
7805 disable breakpoints; they will only be reported as unloaded
7806 through clear_solib when GDB discards its shared library
7807 list. See clear_solib for more information. */
7808 if (exec_bfd != NULL
7809 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7810 return;
7811
876fa593 7812 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7813 {
2bdf28a0 7814 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7815 struct breakpoint *b = loc->owner;
cc59ec59 7816
1e4d1764 7817 if (solib->pspace == loc->pspace
e2dd7057 7818 && !loc->shlib_disabled
1e4d1764
YQ
7819 && (((b->type == bp_breakpoint
7820 || b->type == bp_jit_event
7821 || b->type == bp_hardware_breakpoint)
7822 && (loc->loc_type == bp_loc_hardware_breakpoint
7823 || loc->loc_type == bp_loc_software_breakpoint))
7824 || is_tracepoint (b))
e2dd7057 7825 && solib_contains_address_p (solib, loc->address))
84acb35a 7826 {
e2dd7057
PP
7827 loc->shlib_disabled = 1;
7828 /* At this point, we cannot rely on remove_breakpoint
7829 succeeding so we must mark the breakpoint as not inserted
7830 to prevent future errors occurring in remove_breakpoints. */
7831 loc->inserted = 0;
8d3788bd
VP
7832
7833 /* This may cause duplicate notifications for the same breakpoint. */
7834 observer_notify_breakpoint_modified (b);
7835
e2dd7057
PP
7836 if (!disabled_shlib_breaks)
7837 {
7838 target_terminal_ours_for_output ();
3e43a32a
MS
7839 warning (_("Temporarily disabling breakpoints "
7840 "for unloaded shared library \"%s\""),
e2dd7057 7841 solib->so_name);
84acb35a 7842 }
e2dd7057 7843 disabled_shlib_breaks = 1;
84acb35a
JJ
7844 }
7845 }
84acb35a
JJ
7846}
7847
63644780
NB
7848/* Disable any breakpoints and tracepoints in OBJFILE upon
7849 notification of free_objfile. Only apply to enabled breakpoints,
7850 disabled ones can just stay disabled. */
7851
7852static void
7853disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7854{
7855 struct breakpoint *b;
7856
7857 if (objfile == NULL)
7858 return;
7859
d03de421
PA
7860 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7861 managed by the user with add-symbol-file/remove-symbol-file.
7862 Similarly to how breakpoints in shared libraries are handled in
7863 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7864 shlib_disabled so they end up uninserted on the next global
7865 location list update. Shared libraries not loaded by the user
7866 aren't handled here -- they're already handled in
7867 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7868 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7869 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7870 main objfile). */
7871 if ((objfile->flags & OBJF_SHARED) == 0
7872 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7873 return;
7874
7875 ALL_BREAKPOINTS (b)
7876 {
7877 struct bp_location *loc;
7878 int bp_modified = 0;
7879
7880 if (!is_breakpoint (b) && !is_tracepoint (b))
7881 continue;
7882
7883 for (loc = b->loc; loc != NULL; loc = loc->next)
7884 {
7885 CORE_ADDR loc_addr = loc->address;
7886
7887 if (loc->loc_type != bp_loc_hardware_breakpoint
7888 && loc->loc_type != bp_loc_software_breakpoint)
7889 continue;
7890
7891 if (loc->shlib_disabled != 0)
7892 continue;
7893
7894 if (objfile->pspace != loc->pspace)
7895 continue;
7896
7897 if (loc->loc_type != bp_loc_hardware_breakpoint
7898 && loc->loc_type != bp_loc_software_breakpoint)
7899 continue;
7900
7901 if (is_addr_in_objfile (loc_addr, objfile))
7902 {
7903 loc->shlib_disabled = 1;
08351840
PA
7904 /* At this point, we don't know whether the object was
7905 unmapped from the inferior or not, so leave the
7906 inserted flag alone. We'll handle failure to
7907 uninsert quietly, in case the object was indeed
7908 unmapped. */
63644780
NB
7909
7910 mark_breakpoint_location_modified (loc);
7911
7912 bp_modified = 1;
7913 }
7914 }
7915
7916 if (bp_modified)
7917 observer_notify_breakpoint_modified (b);
7918 }
7919}
7920
ce78b96d
JB
7921/* FORK & VFORK catchpoints. */
7922
e29a4733
PA
7923/* An instance of this type is used to represent a fork or vfork
7924 catchpoint. It includes a "struct breakpoint" as a kind of base
7925 class; users downcast to "struct breakpoint *" when needed. A
7926 breakpoint is really of this type iff its ops pointer points to
7927 CATCH_FORK_BREAKPOINT_OPS. */
7928
7929struct fork_catchpoint
7930{
7931 /* The base class. */
7932 struct breakpoint base;
7933
7934 /* Process id of a child process whose forking triggered this
7935 catchpoint. This field is only valid immediately after this
7936 catchpoint has triggered. */
7937 ptid_t forked_inferior_pid;
7938};
7939
4a64f543
MS
7940/* Implement the "insert" breakpoint_ops method for fork
7941 catchpoints. */
ce78b96d 7942
77b06cd7
TJB
7943static int
7944insert_catch_fork (struct bp_location *bl)
ce78b96d 7945{
dfd4cc63 7946 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7947}
7948
4a64f543
MS
7949/* Implement the "remove" breakpoint_ops method for fork
7950 catchpoints. */
ce78b96d
JB
7951
7952static int
77b06cd7 7953remove_catch_fork (struct bp_location *bl)
ce78b96d 7954{
dfd4cc63 7955 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7956}
7957
7958/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7959 catchpoints. */
7960
7961static int
f1310107 7962breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7963 struct address_space *aspace, CORE_ADDR bp_addr,
7964 const struct target_waitstatus *ws)
ce78b96d 7965{
e29a4733
PA
7966 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7967
f90263c1
TT
7968 if (ws->kind != TARGET_WAITKIND_FORKED)
7969 return 0;
7970
7971 c->forked_inferior_pid = ws->value.related_pid;
7972 return 1;
ce78b96d
JB
7973}
7974
4a64f543
MS
7975/* Implement the "print_it" breakpoint_ops method for fork
7976 catchpoints. */
ce78b96d
JB
7977
7978static enum print_stop_action
348d480f 7979print_it_catch_fork (bpstat bs)
ce78b96d 7980{
36dfb11c 7981 struct ui_out *uiout = current_uiout;
348d480f
PA
7982 struct breakpoint *b = bs->breakpoint_at;
7983 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7984
ce78b96d 7985 annotate_catchpoint (b->number);
36dfb11c
TT
7986 if (b->disposition == disp_del)
7987 ui_out_text (uiout, "\nTemporary catchpoint ");
7988 else
7989 ui_out_text (uiout, "\nCatchpoint ");
7990 if (ui_out_is_mi_like_p (uiout))
7991 {
7992 ui_out_field_string (uiout, "reason",
7993 async_reason_lookup (EXEC_ASYNC_FORK));
7994 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7995 }
7996 ui_out_field_int (uiout, "bkptno", b->number);
7997 ui_out_text (uiout, " (forked process ");
7998 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7999 ui_out_text (uiout, "), ");
ce78b96d
JB
8000 return PRINT_SRC_AND_LOC;
8001}
8002
4a64f543
MS
8003/* Implement the "print_one" breakpoint_ops method for fork
8004 catchpoints. */
ce78b96d
JB
8005
8006static void
a6d9a66e 8007print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8008{
e29a4733 8009 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8010 struct value_print_options opts;
79a45e25 8011 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8012
8013 get_user_print_options (&opts);
8014
4a64f543
MS
8015 /* Field 4, the address, is omitted (which makes the columns not
8016 line up too nicely with the headers, but the effect is relatively
8017 readable). */
79a45b7d 8018 if (opts.addressprint)
ce78b96d
JB
8019 ui_out_field_skip (uiout, "addr");
8020 annotate_field (5);
8021 ui_out_text (uiout, "fork");
e29a4733 8022 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8023 {
8024 ui_out_text (uiout, ", process ");
8025 ui_out_field_int (uiout, "what",
e29a4733 8026 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8027 ui_out_spaces (uiout, 1);
8028 }
8ac3646f
TT
8029
8030 if (ui_out_is_mi_like_p (uiout))
8031 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8032}
8033
8034/* Implement the "print_mention" breakpoint_ops method for fork
8035 catchpoints. */
8036
8037static void
8038print_mention_catch_fork (struct breakpoint *b)
8039{
8040 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8041}
8042
6149aea9
PA
8043/* Implement the "print_recreate" breakpoint_ops method for fork
8044 catchpoints. */
8045
8046static void
8047print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8048{
8049 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8050 print_recreate_thread (b, fp);
6149aea9
PA
8051}
8052
ce78b96d
JB
8053/* The breakpoint_ops structure to be used in fork catchpoints. */
8054
2060206e 8055static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8056
4a64f543
MS
8057/* Implement the "insert" breakpoint_ops method for vfork
8058 catchpoints. */
ce78b96d 8059
77b06cd7
TJB
8060static int
8061insert_catch_vfork (struct bp_location *bl)
ce78b96d 8062{
dfd4cc63 8063 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8064}
8065
4a64f543
MS
8066/* Implement the "remove" breakpoint_ops method for vfork
8067 catchpoints. */
ce78b96d
JB
8068
8069static int
77b06cd7 8070remove_catch_vfork (struct bp_location *bl)
ce78b96d 8071{
dfd4cc63 8072 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8073}
8074
8075/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8076 catchpoints. */
8077
8078static int
f1310107 8079breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8080 struct address_space *aspace, CORE_ADDR bp_addr,
8081 const struct target_waitstatus *ws)
ce78b96d 8082{
e29a4733
PA
8083 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8084
f90263c1
TT
8085 if (ws->kind != TARGET_WAITKIND_VFORKED)
8086 return 0;
8087
8088 c->forked_inferior_pid = ws->value.related_pid;
8089 return 1;
ce78b96d
JB
8090}
8091
4a64f543
MS
8092/* Implement the "print_it" breakpoint_ops method for vfork
8093 catchpoints. */
ce78b96d
JB
8094
8095static enum print_stop_action
348d480f 8096print_it_catch_vfork (bpstat bs)
ce78b96d 8097{
36dfb11c 8098 struct ui_out *uiout = current_uiout;
348d480f 8099 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8100 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8101
ce78b96d 8102 annotate_catchpoint (b->number);
36dfb11c
TT
8103 if (b->disposition == disp_del)
8104 ui_out_text (uiout, "\nTemporary catchpoint ");
8105 else
8106 ui_out_text (uiout, "\nCatchpoint ");
8107 if (ui_out_is_mi_like_p (uiout))
8108 {
8109 ui_out_field_string (uiout, "reason",
8110 async_reason_lookup (EXEC_ASYNC_VFORK));
8111 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8112 }
8113 ui_out_field_int (uiout, "bkptno", b->number);
8114 ui_out_text (uiout, " (vforked process ");
8115 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8116 ui_out_text (uiout, "), ");
ce78b96d
JB
8117 return PRINT_SRC_AND_LOC;
8118}
8119
4a64f543
MS
8120/* Implement the "print_one" breakpoint_ops method for vfork
8121 catchpoints. */
ce78b96d
JB
8122
8123static void
a6d9a66e 8124print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8125{
e29a4733 8126 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8127 struct value_print_options opts;
79a45e25 8128 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8129
8130 get_user_print_options (&opts);
4a64f543
MS
8131 /* Field 4, the address, is omitted (which makes the columns not
8132 line up too nicely with the headers, but the effect is relatively
8133 readable). */
79a45b7d 8134 if (opts.addressprint)
ce78b96d
JB
8135 ui_out_field_skip (uiout, "addr");
8136 annotate_field (5);
8137 ui_out_text (uiout, "vfork");
e29a4733 8138 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8139 {
8140 ui_out_text (uiout, ", process ");
8141 ui_out_field_int (uiout, "what",
e29a4733 8142 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8143 ui_out_spaces (uiout, 1);
8144 }
8ac3646f
TT
8145
8146 if (ui_out_is_mi_like_p (uiout))
8147 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8148}
8149
8150/* Implement the "print_mention" breakpoint_ops method for vfork
8151 catchpoints. */
8152
8153static void
8154print_mention_catch_vfork (struct breakpoint *b)
8155{
8156 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8157}
8158
6149aea9
PA
8159/* Implement the "print_recreate" breakpoint_ops method for vfork
8160 catchpoints. */
8161
8162static void
8163print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8164{
8165 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8166 print_recreate_thread (b, fp);
6149aea9
PA
8167}
8168
ce78b96d
JB
8169/* The breakpoint_ops structure to be used in vfork catchpoints. */
8170
2060206e 8171static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8172
edcc5120
TT
8173/* An instance of this type is used to represent an solib catchpoint.
8174 It includes a "struct breakpoint" as a kind of base class; users
8175 downcast to "struct breakpoint *" when needed. A breakpoint is
8176 really of this type iff its ops pointer points to
8177 CATCH_SOLIB_BREAKPOINT_OPS. */
8178
8179struct solib_catchpoint
8180{
8181 /* The base class. */
8182 struct breakpoint base;
8183
8184 /* True for "catch load", false for "catch unload". */
8185 unsigned char is_load;
8186
8187 /* Regular expression to match, if any. COMPILED is only valid when
8188 REGEX is non-NULL. */
8189 char *regex;
8190 regex_t compiled;
8191};
8192
8193static void
8194dtor_catch_solib (struct breakpoint *b)
8195{
8196 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8197
8198 if (self->regex)
8199 regfree (&self->compiled);
8200 xfree (self->regex);
8201
8202 base_breakpoint_ops.dtor (b);
8203}
8204
8205static int
8206insert_catch_solib (struct bp_location *ignore)
8207{
8208 return 0;
8209}
8210
8211static int
8212remove_catch_solib (struct bp_location *ignore)
8213{
8214 return 0;
8215}
8216
8217static int
8218breakpoint_hit_catch_solib (const struct bp_location *bl,
8219 struct address_space *aspace,
8220 CORE_ADDR bp_addr,
8221 const struct target_waitstatus *ws)
8222{
8223 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8224 struct breakpoint *other;
8225
8226 if (ws->kind == TARGET_WAITKIND_LOADED)
8227 return 1;
8228
8229 ALL_BREAKPOINTS (other)
8230 {
8231 struct bp_location *other_bl;
8232
8233 if (other == bl->owner)
8234 continue;
8235
8236 if (other->type != bp_shlib_event)
8237 continue;
8238
8239 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8240 continue;
8241
8242 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8243 {
8244 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8245 return 1;
8246 }
8247 }
8248
8249 return 0;
8250}
8251
8252static void
8253check_status_catch_solib (struct bpstats *bs)
8254{
8255 struct solib_catchpoint *self
8256 = (struct solib_catchpoint *) bs->breakpoint_at;
8257 int ix;
8258
8259 if (self->is_load)
8260 {
8261 struct so_list *iter;
8262
8263 for (ix = 0;
8264 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8265 ix, iter);
8266 ++ix)
8267 {
8268 if (!self->regex
8269 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8270 return;
8271 }
8272 }
8273 else
8274 {
8275 char *iter;
8276
8277 for (ix = 0;
8278 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8279 ix, iter);
8280 ++ix)
8281 {
8282 if (!self->regex
8283 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8284 return;
8285 }
8286 }
8287
8288 bs->stop = 0;
8289 bs->print_it = print_it_noop;
8290}
8291
8292static enum print_stop_action
8293print_it_catch_solib (bpstat bs)
8294{
8295 struct breakpoint *b = bs->breakpoint_at;
8296 struct ui_out *uiout = current_uiout;
8297
8298 annotate_catchpoint (b->number);
8299 if (b->disposition == disp_del)
8300 ui_out_text (uiout, "\nTemporary catchpoint ");
8301 else
8302 ui_out_text (uiout, "\nCatchpoint ");
8303 ui_out_field_int (uiout, "bkptno", b->number);
8304 ui_out_text (uiout, "\n");
8305 if (ui_out_is_mi_like_p (uiout))
8306 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8307 print_solib_event (1);
8308 return PRINT_SRC_AND_LOC;
8309}
8310
8311static void
8312print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8313{
8314 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8315 struct value_print_options opts;
8316 struct ui_out *uiout = current_uiout;
8317 char *msg;
8318
8319 get_user_print_options (&opts);
8320 /* Field 4, the address, is omitted (which makes the columns not
8321 line up too nicely with the headers, but the effect is relatively
8322 readable). */
8323 if (opts.addressprint)
8324 {
8325 annotate_field (4);
8326 ui_out_field_skip (uiout, "addr");
8327 }
8328
8329 annotate_field (5);
8330 if (self->is_load)
8331 {
8332 if (self->regex)
8333 msg = xstrprintf (_("load of library matching %s"), self->regex);
8334 else
8335 msg = xstrdup (_("load of library"));
8336 }
8337 else
8338 {
8339 if (self->regex)
8340 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8341 else
8342 msg = xstrdup (_("unload of library"));
8343 }
8344 ui_out_field_string (uiout, "what", msg);
8345 xfree (msg);
8ac3646f
TT
8346
8347 if (ui_out_is_mi_like_p (uiout))
8348 ui_out_field_string (uiout, "catch-type",
8349 self->is_load ? "load" : "unload");
edcc5120
TT
8350}
8351
8352static void
8353print_mention_catch_solib (struct breakpoint *b)
8354{
8355 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8356
8357 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8358 self->is_load ? "load" : "unload");
8359}
8360
8361static void
8362print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8363{
8364 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8365
8366 fprintf_unfiltered (fp, "%s %s",
8367 b->disposition == disp_del ? "tcatch" : "catch",
8368 self->is_load ? "load" : "unload");
8369 if (self->regex)
8370 fprintf_unfiltered (fp, " %s", self->regex);
8371 fprintf_unfiltered (fp, "\n");
8372}
8373
8374static struct breakpoint_ops catch_solib_breakpoint_ops;
8375
91985142
MG
8376/* Shared helper function (MI and CLI) for creating and installing
8377 a shared object event catchpoint. If IS_LOAD is non-zero then
8378 the events to be caught are load events, otherwise they are
8379 unload events. If IS_TEMP is non-zero the catchpoint is a
8380 temporary one. If ENABLED is non-zero the catchpoint is
8381 created in an enabled state. */
edcc5120 8382
91985142
MG
8383void
8384add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8385{
8386 struct solib_catchpoint *c;
8387 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8388 struct cleanup *cleanup;
8389
edcc5120
TT
8390 if (!arg)
8391 arg = "";
8392 arg = skip_spaces (arg);
8393
8394 c = XCNEW (struct solib_catchpoint);
8395 cleanup = make_cleanup (xfree, c);
8396
8397 if (*arg != '\0')
8398 {
8399 int errcode;
8400
8401 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8402 if (errcode != 0)
8403 {
8404 char *err = get_regcomp_error (errcode, &c->compiled);
8405
8406 make_cleanup (xfree, err);
8407 error (_("Invalid regexp (%s): %s"), err, arg);
8408 }
8409 c->regex = xstrdup (arg);
8410 }
8411
8412 c->is_load = is_load;
91985142 8413 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8414 &catch_solib_breakpoint_ops);
8415
91985142
MG
8416 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8417
edcc5120
TT
8418 discard_cleanups (cleanup);
8419 install_breakpoint (0, &c->base, 1);
8420}
8421
91985142
MG
8422/* A helper function that does all the work for "catch load" and
8423 "catch unload". */
8424
8425static void
8426catch_load_or_unload (char *arg, int from_tty, int is_load,
8427 struct cmd_list_element *command)
8428{
8429 int tempflag;
8430 const int enabled = 1;
8431
8432 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8433
8434 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8435}
8436
edcc5120
TT
8437static void
8438catch_load_command_1 (char *arg, int from_tty,
8439 struct cmd_list_element *command)
8440{
8441 catch_load_or_unload (arg, from_tty, 1, command);
8442}
8443
8444static void
8445catch_unload_command_1 (char *arg, int from_tty,
8446 struct cmd_list_element *command)
8447{
8448 catch_load_or_unload (arg, from_tty, 0, command);
8449}
8450
346774a9
PA
8451/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8452 is non-zero, then make the breakpoint temporary. If COND_STRING is
8453 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8454 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8455
ab04a2af 8456void
346774a9
PA
8457init_catchpoint (struct breakpoint *b,
8458 struct gdbarch *gdbarch, int tempflag,
8459 char *cond_string,
c0a91b2b 8460 const struct breakpoint_ops *ops)
c906108c 8461{
c5aa993b 8462 struct symtab_and_line sal;
346774a9 8463
fe39c653 8464 init_sal (&sal);
6c95b8df 8465 sal.pspace = current_program_space;
c5aa993b 8466
28010a5d 8467 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8468
1b36a34b 8469 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8470 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8471}
8472
28010a5d 8473void
3ea46bff 8474install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8475{
8476 add_to_breakpoint_chain (b);
3a5c3e22 8477 set_breakpoint_number (internal, b);
558a9d82
YQ
8478 if (is_tracepoint (b))
8479 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8480 if (!internal)
8481 mention (b);
c56053d2 8482 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8483
8484 if (update_gll)
44702360 8485 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8486}
8487
9b70b993 8488static void
a6d9a66e
UW
8489create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8490 int tempflag, char *cond_string,
c0a91b2b 8491 const struct breakpoint_ops *ops)
c906108c 8492{
e29a4733 8493 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8494
e29a4733
PA
8495 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8496
8497 c->forked_inferior_pid = null_ptid;
8498
3ea46bff 8499 install_breakpoint (0, &c->base, 1);
c906108c
SS
8500}
8501
fe798b75
JB
8502/* Exec catchpoints. */
8503
b4d90040
PA
8504/* An instance of this type is used to represent an exec catchpoint.
8505 It includes a "struct breakpoint" as a kind of base class; users
8506 downcast to "struct breakpoint *" when needed. A breakpoint is
8507 really of this type iff its ops pointer points to
8508 CATCH_EXEC_BREAKPOINT_OPS. */
8509
8510struct exec_catchpoint
8511{
8512 /* The base class. */
8513 struct breakpoint base;
8514
8515 /* Filename of a program whose exec triggered this catchpoint.
8516 This field is only valid immediately after this catchpoint has
8517 triggered. */
8518 char *exec_pathname;
8519};
8520
8521/* Implement the "dtor" breakpoint_ops method for exec
8522 catchpoints. */
8523
8524static void
8525dtor_catch_exec (struct breakpoint *b)
8526{
8527 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8528
8529 xfree (c->exec_pathname);
348d480f 8530
2060206e 8531 base_breakpoint_ops.dtor (b);
b4d90040
PA
8532}
8533
77b06cd7
TJB
8534static int
8535insert_catch_exec (struct bp_location *bl)
c906108c 8536{
dfd4cc63 8537 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8538}
c906108c 8539
fe798b75 8540static int
77b06cd7 8541remove_catch_exec (struct bp_location *bl)
fe798b75 8542{
dfd4cc63 8543 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8544}
c906108c 8545
fe798b75 8546static int
f1310107 8547breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8548 struct address_space *aspace, CORE_ADDR bp_addr,
8549 const struct target_waitstatus *ws)
fe798b75 8550{
b4d90040
PA
8551 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8552
f90263c1
TT
8553 if (ws->kind != TARGET_WAITKIND_EXECD)
8554 return 0;
8555
8556 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8557 return 1;
fe798b75 8558}
c906108c 8559
fe798b75 8560static enum print_stop_action
348d480f 8561print_it_catch_exec (bpstat bs)
fe798b75 8562{
36dfb11c 8563 struct ui_out *uiout = current_uiout;
348d480f 8564 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8565 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8566
fe798b75 8567 annotate_catchpoint (b->number);
36dfb11c
TT
8568 if (b->disposition == disp_del)
8569 ui_out_text (uiout, "\nTemporary catchpoint ");
8570 else
8571 ui_out_text (uiout, "\nCatchpoint ");
8572 if (ui_out_is_mi_like_p (uiout))
8573 {
8574 ui_out_field_string (uiout, "reason",
8575 async_reason_lookup (EXEC_ASYNC_EXEC));
8576 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8577 }
8578 ui_out_field_int (uiout, "bkptno", b->number);
8579 ui_out_text (uiout, " (exec'd ");
8580 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8581 ui_out_text (uiout, "), ");
8582
fe798b75 8583 return PRINT_SRC_AND_LOC;
c906108c
SS
8584}
8585
fe798b75 8586static void
a6d9a66e 8587print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8588{
b4d90040 8589 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8590 struct value_print_options opts;
79a45e25 8591 struct ui_out *uiout = current_uiout;
fe798b75
JB
8592
8593 get_user_print_options (&opts);
8594
8595 /* Field 4, the address, is omitted (which makes the columns
8596 not line up too nicely with the headers, but the effect
8597 is relatively readable). */
8598 if (opts.addressprint)
8599 ui_out_field_skip (uiout, "addr");
8600 annotate_field (5);
8601 ui_out_text (uiout, "exec");
b4d90040 8602 if (c->exec_pathname != NULL)
fe798b75
JB
8603 {
8604 ui_out_text (uiout, ", program \"");
b4d90040 8605 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8606 ui_out_text (uiout, "\" ");
8607 }
8ac3646f
TT
8608
8609 if (ui_out_is_mi_like_p (uiout))
8610 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8611}
8612
8613static void
8614print_mention_catch_exec (struct breakpoint *b)
8615{
8616 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8617}
8618
6149aea9
PA
8619/* Implement the "print_recreate" breakpoint_ops method for exec
8620 catchpoints. */
8621
8622static void
8623print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8624{
8625 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8626 print_recreate_thread (b, fp);
6149aea9
PA
8627}
8628
2060206e 8629static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8630
c906108c 8631static int
fba45db2 8632hw_breakpoint_used_count (void)
c906108c 8633{
c906108c 8634 int i = 0;
f1310107
TJB
8635 struct breakpoint *b;
8636 struct bp_location *bl;
c906108c
SS
8637
8638 ALL_BREAKPOINTS (b)
c5aa993b 8639 {
d6b74ac4 8640 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8641 for (bl = b->loc; bl; bl = bl->next)
8642 {
8643 /* Special types of hardware breakpoints may use more than
8644 one register. */
348d480f 8645 i += b->ops->resources_needed (bl);
f1310107 8646 }
c5aa993b 8647 }
c906108c
SS
8648
8649 return i;
8650}
8651
a1398e0c
PA
8652/* Returns the resources B would use if it were a hardware
8653 watchpoint. */
8654
c906108c 8655static int
a1398e0c 8656hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8657{
c906108c 8658 int i = 0;
e09342b5 8659 struct bp_location *bl;
c906108c 8660
a1398e0c
PA
8661 if (!breakpoint_enabled (b))
8662 return 0;
8663
8664 for (bl = b->loc; bl; bl = bl->next)
8665 {
8666 /* Special types of hardware watchpoints may use more than
8667 one register. */
8668 i += b->ops->resources_needed (bl);
8669 }
8670
8671 return i;
8672}
8673
8674/* Returns the sum the used resources of all hardware watchpoints of
8675 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8676 the sum of the used resources of all hardware watchpoints of other
8677 types _not_ TYPE. */
8678
8679static int
8680hw_watchpoint_used_count_others (struct breakpoint *except,
8681 enum bptype type, int *other_type_used)
8682{
8683 int i = 0;
8684 struct breakpoint *b;
8685
c906108c
SS
8686 *other_type_used = 0;
8687 ALL_BREAKPOINTS (b)
e09342b5 8688 {
a1398e0c
PA
8689 if (b == except)
8690 continue;
e09342b5
TJB
8691 if (!breakpoint_enabled (b))
8692 continue;
8693
a1398e0c
PA
8694 if (b->type == type)
8695 i += hw_watchpoint_use_count (b);
8696 else if (is_hardware_watchpoint (b))
8697 *other_type_used = 1;
e09342b5
TJB
8698 }
8699
c906108c
SS
8700 return i;
8701}
8702
c906108c 8703void
fba45db2 8704disable_watchpoints_before_interactive_call_start (void)
c906108c 8705{
c5aa993b 8706 struct breakpoint *b;
c906108c
SS
8707
8708 ALL_BREAKPOINTS (b)
c5aa993b 8709 {
cc60f2e3 8710 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8711 {
b5de0fa7 8712 b->enable_state = bp_call_disabled;
44702360 8713 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8714 }
8715 }
c906108c
SS
8716}
8717
8718void
fba45db2 8719enable_watchpoints_after_interactive_call_stop (void)
c906108c 8720{
c5aa993b 8721 struct breakpoint *b;
c906108c
SS
8722
8723 ALL_BREAKPOINTS (b)
c5aa993b 8724 {
cc60f2e3 8725 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8726 {
b5de0fa7 8727 b->enable_state = bp_enabled;
44702360 8728 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8729 }
8730 }
c906108c
SS
8731}
8732
8bea4e01
UW
8733void
8734disable_breakpoints_before_startup (void)
8735{
6c95b8df 8736 current_program_space->executing_startup = 1;
44702360 8737 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8738}
8739
8740void
8741enable_breakpoints_after_startup (void)
8742{
6c95b8df 8743 current_program_space->executing_startup = 0;
f8eba3c6 8744 breakpoint_re_set ();
8bea4e01
UW
8745}
8746
7c16b83e
PA
8747/* Create a new single-step breakpoint for thread THREAD, with no
8748 locations. */
c906108c 8749
7c16b83e
PA
8750static struct breakpoint *
8751new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8752{
8753 struct breakpoint *b = XNEW (struct breakpoint);
8754
8755 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8756 &momentary_breakpoint_ops);
8757
8758 b->disposition = disp_donttouch;
8759 b->frame_id = null_frame_id;
8760
8761 b->thread = thread;
8762 gdb_assert (b->thread != 0);
8763
8764 add_to_breakpoint_chain (b);
8765
8766 return b;
8767}
8768
8769/* Set a momentary breakpoint of type TYPE at address specified by
8770 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8771 frame. */
c906108c
SS
8772
8773struct breakpoint *
a6d9a66e
UW
8774set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8775 struct frame_id frame_id, enum bptype type)
c906108c 8776{
52f0bd74 8777 struct breakpoint *b;
edb3359d 8778
193facb3
JK
8779 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8780 tail-called one. */
8781 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8782
06edf0c0 8783 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8784 b->enable_state = bp_enabled;
8785 b->disposition = disp_donttouch;
818dd999 8786 b->frame_id = frame_id;
c906108c 8787
4a64f543
MS
8788 /* If we're debugging a multi-threaded program, then we want
8789 momentary breakpoints to be active in only a single thread of
8790 control. */
39f77062
KB
8791 if (in_thread_list (inferior_ptid))
8792 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 8793
44702360 8794 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8795
c906108c
SS
8796 return b;
8797}
611c83ae 8798
06edf0c0 8799/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8800 The new breakpoint will have type TYPE, use OPS as its
8801 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8802
06edf0c0
PA
8803static struct breakpoint *
8804momentary_breakpoint_from_master (struct breakpoint *orig,
8805 enum bptype type,
a1aa2221
LM
8806 const struct breakpoint_ops *ops,
8807 int loc_enabled)
e58b0e63
PA
8808{
8809 struct breakpoint *copy;
8810
06edf0c0 8811 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8812 copy->loc = allocate_bp_location (copy);
0e30163f 8813 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8814
a6d9a66e 8815 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8816 copy->loc->requested_address = orig->loc->requested_address;
8817 copy->loc->address = orig->loc->address;
8818 copy->loc->section = orig->loc->section;
6c95b8df 8819 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8820 copy->loc->probe = orig->loc->probe;
f8eba3c6 8821 copy->loc->line_number = orig->loc->line_number;
2f202fde 8822 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8823 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8824 copy->frame_id = orig->frame_id;
8825 copy->thread = orig->thread;
6c95b8df 8826 copy->pspace = orig->pspace;
e58b0e63
PA
8827
8828 copy->enable_state = bp_enabled;
8829 copy->disposition = disp_donttouch;
8830 copy->number = internal_breakpoint_number--;
8831
44702360 8832 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8833 return copy;
8834}
8835
06edf0c0
PA
8836/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8837 ORIG is NULL. */
8838
8839struct breakpoint *
8840clone_momentary_breakpoint (struct breakpoint *orig)
8841{
8842 /* If there's nothing to clone, then return nothing. */
8843 if (orig == NULL)
8844 return NULL;
8845
a1aa2221 8846 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8847}
8848
611c83ae 8849struct breakpoint *
a6d9a66e
UW
8850set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8851 enum bptype type)
611c83ae
PA
8852{
8853 struct symtab_and_line sal;
8854
8855 sal = find_pc_line (pc, 0);
8856 sal.pc = pc;
8857 sal.section = find_pc_overlay (pc);
8858 sal.explicit_pc = 1;
8859
a6d9a66e 8860 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8861}
c906108c 8862\f
c5aa993b 8863
c906108c
SS
8864/* Tell the user we have just set a breakpoint B. */
8865
8866static void
fba45db2 8867mention (struct breakpoint *b)
c906108c 8868{
348d480f 8869 b->ops->print_mention (b);
79a45e25 8870 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 8871 return;
c906108c
SS
8872 printf_filtered ("\n");
8873}
c906108c 8874\f
c5aa993b 8875
1a853c52
PA
8876static int bp_loc_is_permanent (struct bp_location *loc);
8877
0d381245 8878static struct bp_location *
39d61571 8879add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8880 const struct symtab_and_line *sal)
8881{
8882 struct bp_location *loc, **tmp;
3742cc8b
YQ
8883 CORE_ADDR adjusted_address;
8884 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8885
8886 if (loc_gdbarch == NULL)
8887 loc_gdbarch = b->gdbarch;
8888
8889 /* Adjust the breakpoint's address prior to allocating a location.
8890 Once we call allocate_bp_location(), that mostly uninitialized
8891 location will be placed on the location chain. Adjustment of the
8892 breakpoint may cause target_read_memory() to be called and we do
8893 not want its scan of the location chain to find a breakpoint and
8894 location that's only been partially initialized. */
8895 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8896 sal->pc, b->type);
0d381245 8897
d30113d4 8898 /* Sort the locations by their ADDRESS. */
39d61571 8899 loc = allocate_bp_location (b);
d30113d4
JK
8900 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8901 tmp = &((*tmp)->next))
0d381245 8902 ;
d30113d4 8903 loc->next = *tmp;
0d381245 8904 *tmp = loc;
3742cc8b 8905
0d381245 8906 loc->requested_address = sal->pc;
3742cc8b 8907 loc->address = adjusted_address;
6c95b8df 8908 loc->pspace = sal->pspace;
729662a5
TT
8909 loc->probe.probe = sal->probe;
8910 loc->probe.objfile = sal->objfile;
6c95b8df 8911 gdb_assert (loc->pspace != NULL);
0d381245 8912 loc->section = sal->section;
3742cc8b 8913 loc->gdbarch = loc_gdbarch;
f8eba3c6 8914 loc->line_number = sal->line;
2f202fde 8915 loc->symtab = sal->symtab;
f8eba3c6 8916
0e30163f
JK
8917 set_breakpoint_location_function (loc,
8918 sal->explicit_pc || sal->explicit_line);
1a853c52
PA
8919
8920 if (bp_loc_is_permanent (loc))
8921 {
8922 loc->inserted = 1;
8923 loc->permanent = 1;
8924 }
8925
0d381245
VP
8926 return loc;
8927}
514f746b
AR
8928\f
8929
1cf4d951 8930/* See breakpoint.h. */
514f746b 8931
1cf4d951
PA
8932int
8933program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8934{
8935 int len;
8936 CORE_ADDR addr;
1afeeb75 8937 const gdb_byte *bpoint;
514f746b 8938 gdb_byte *target_mem;
939c61fa
JK
8939 struct cleanup *cleanup;
8940 int retval = 0;
514f746b 8941
1cf4d951
PA
8942 addr = address;
8943 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8944
8945 /* Software breakpoints unsupported? */
8946 if (bpoint == NULL)
8947 return 0;
8948
8949 target_mem = alloca (len);
8950
8951 /* Enable the automatic memory restoration from breakpoints while
8952 we read the memory. Otherwise we could say about our temporary
8953 breakpoints they are permanent. */
8954 cleanup = make_show_memory_breakpoints_cleanup (0);
8955
8956 if (target_read_memory (address, target_mem, len) == 0
8957 && memcmp (target_mem, bpoint, len) == 0)
8958 retval = 1;
8959
8960 do_cleanups (cleanup);
8961
8962 return retval;
8963}
8964
8965/* Return 1 if LOC is pointing to a permanent breakpoint,
8966 return 0 otherwise. */
8967
8968static int
8969bp_loc_is_permanent (struct bp_location *loc)
8970{
8971 struct cleanup *cleanup;
8972 int retval;
8973
514f746b
AR
8974 gdb_assert (loc != NULL);
8975
e8af5d7a
JB
8976 /* bp_call_dummy breakpoint locations are usually memory locations
8977 where GDB just wrote a breakpoint instruction, making it look
8978 as if there is a permanent breakpoint at that location. Considering
8979 it permanent makes GDB rely on that breakpoint instruction to stop
8980 the program, thus removing the need to insert its own breakpoint
8981 there. This is normally expected to work, except that some versions
8982 of QEMU (Eg: QEMU 2.0.0 for SPARC) just report a fatal problem (Trap
8983 0x02 while interrupts disabled, Error state) instead of reporting
8984 a SIGTRAP. QEMU should probably be fixed, but in the interest of
8985 compatibility with versions that behave this way, we always consider
8986 bp_call_dummy breakpoint locations as non-permanent. */
8987 if (loc->owner->type == bp_call_dummy)
8988 return 0;
8989
6c95b8df 8990 cleanup = save_current_space_and_thread ();
6c95b8df 8991 switch_to_program_space_and_thread (loc->pspace);
939c61fa 8992
1cf4d951 8993 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b 8994
939c61fa
JK
8995 do_cleanups (cleanup);
8996
8997 return retval;
514f746b
AR
8998}
8999
e7e0cddf
SS
9000/* Build a command list for the dprintf corresponding to the current
9001 settings of the dprintf style options. */
9002
9003static void
9004update_dprintf_command_list (struct breakpoint *b)
9005{
9006 char *dprintf_args = b->extra_string;
9007 char *printf_line = NULL;
9008
9009 if (!dprintf_args)
9010 return;
9011
9012 dprintf_args = skip_spaces (dprintf_args);
9013
9014 /* Allow a comma, as it may have terminated a location, but don't
9015 insist on it. */
9016 if (*dprintf_args == ',')
9017 ++dprintf_args;
9018 dprintf_args = skip_spaces (dprintf_args);
9019
9020 if (*dprintf_args != '"')
9021 error (_("Bad format string, missing '\"'."));
9022
d3ce09f5 9023 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9024 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9025 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9026 {
9027 if (!dprintf_function)
9028 error (_("No function supplied for dprintf call"));
9029
9030 if (dprintf_channel && strlen (dprintf_channel) > 0)
9031 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9032 dprintf_function,
9033 dprintf_channel,
9034 dprintf_args);
9035 else
9036 printf_line = xstrprintf ("call (void) %s (%s)",
9037 dprintf_function,
9038 dprintf_args);
9039 }
d3ce09f5
SS
9040 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9041 {
9042 if (target_can_run_breakpoint_commands ())
9043 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9044 else
9045 {
9046 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9047 printf_line = xstrprintf ("printf %s", dprintf_args);
9048 }
9049 }
e7e0cddf
SS
9050 else
9051 internal_error (__FILE__, __LINE__,
9052 _("Invalid dprintf style."));
9053
f28045c2 9054 gdb_assert (printf_line != NULL);
9d6e6e84 9055 /* Manufacture a printf sequence. */
f28045c2 9056 {
9d6e6e84
HZ
9057 struct command_line *printf_cmd_line
9058 = xmalloc (sizeof (struct command_line));
e7e0cddf 9059
f28045c2
YQ
9060 printf_cmd_line = xmalloc (sizeof (struct command_line));
9061 printf_cmd_line->control_type = simple_control;
9062 printf_cmd_line->body_count = 0;
9063 printf_cmd_line->body_list = NULL;
9d6e6e84 9064 printf_cmd_line->next = NULL;
f28045c2 9065 printf_cmd_line->line = printf_line;
e7e0cddf 9066
f28045c2
YQ
9067 breakpoint_set_commands (b, printf_cmd_line);
9068 }
e7e0cddf
SS
9069}
9070
9071/* Update all dprintf commands, making their command lists reflect
9072 current style settings. */
9073
9074static void
9075update_dprintf_commands (char *args, int from_tty,
9076 struct cmd_list_element *c)
9077{
9078 struct breakpoint *b;
9079
9080 ALL_BREAKPOINTS (b)
9081 {
9082 if (b->type == bp_dprintf)
9083 update_dprintf_command_list (b);
9084 }
9085}
c3f6f71d 9086
018d34a4
VP
9087/* Create a breakpoint with SAL as location. Use ADDR_STRING
9088 as textual description of the location, and COND_STRING
db107f19 9089 as condition expression. */
018d34a4
VP
9090
9091static void
d9b3f62e
PA
9092init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9093 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9094 char *filter, char *cond_string,
e7e0cddf 9095 char *extra_string,
d9b3f62e
PA
9096 enum bptype type, enum bpdisp disposition,
9097 int thread, int task, int ignore_count,
c0a91b2b 9098 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9099 int enabled, int internal, unsigned flags,
9100 int display_canonical)
018d34a4 9101{
0d381245 9102 int i;
018d34a4
VP
9103
9104 if (type == bp_hardware_breakpoint)
9105 {
fbbd034e
AS
9106 int target_resources_ok;
9107
9108 i = hw_breakpoint_used_count ();
9109 target_resources_ok =
9110 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9111 i + 1, 0);
9112 if (target_resources_ok == 0)
9113 error (_("No hardware breakpoint support in the target."));
9114 else if (target_resources_ok < 0)
9115 error (_("Hardware breakpoints used exceeds limit."));
9116 }
9117
6c95b8df
PA
9118 gdb_assert (sals.nelts > 0);
9119
0d381245
VP
9120 for (i = 0; i < sals.nelts; ++i)
9121 {
9122 struct symtab_and_line sal = sals.sals[i];
9123 struct bp_location *loc;
9124
9125 if (from_tty)
5af949e3
UW
9126 {
9127 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9128 if (!loc_gdbarch)
9129 loc_gdbarch = gdbarch;
9130
9131 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9132 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9133 }
0d381245
VP
9134
9135 if (i == 0)
9136 {
d9b3f62e 9137 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9138 b->thread = thread;
4a306c9a 9139 b->task = task;
855a6e68 9140
0d381245 9141 b->cond_string = cond_string;
e7e0cddf 9142 b->extra_string = extra_string;
0d381245 9143 b->ignore_count = ignore_count;
41447f92 9144 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9145 b->disposition = disposition;
6c95b8df 9146
44f238bb
PA
9147 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9148 b->loc->inserted = 1;
9149
0fb4aa4b
PA
9150 if (type == bp_static_tracepoint)
9151 {
d9b3f62e 9152 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9153 struct static_tracepoint_marker marker;
9154
983af33b 9155 if (strace_marker_p (b))
0fb4aa4b
PA
9156 {
9157 /* We already know the marker exists, otherwise, we
9158 wouldn't see a sal for it. */
9159 char *p = &addr_string[3];
9160 char *endp;
9161 char *marker_str;
0fb4aa4b 9162
e9cafbcc 9163 p = skip_spaces (p);
0fb4aa4b 9164
e9cafbcc 9165 endp = skip_to_space (p);
0fb4aa4b
PA
9166
9167 marker_str = savestring (p, endp - p);
d9b3f62e 9168 t->static_trace_marker_id = marker_str;
0fb4aa4b 9169
3e43a32a
MS
9170 printf_filtered (_("Probed static tracepoint "
9171 "marker \"%s\"\n"),
d9b3f62e 9172 t->static_trace_marker_id);
0fb4aa4b
PA
9173 }
9174 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9175 {
d9b3f62e 9176 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9177 release_static_tracepoint_marker (&marker);
9178
3e43a32a
MS
9179 printf_filtered (_("Probed static tracepoint "
9180 "marker \"%s\"\n"),
d9b3f62e 9181 t->static_trace_marker_id);
0fb4aa4b
PA
9182 }
9183 else
3e43a32a
MS
9184 warning (_("Couldn't determine the static "
9185 "tracepoint marker to probe"));
0fb4aa4b
PA
9186 }
9187
0d381245
VP
9188 loc = b->loc;
9189 }
9190 else
018d34a4 9191 {
39d61571 9192 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9193 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9194 loc->inserted = 1;
0d381245
VP
9195 }
9196
9197 if (b->cond_string)
9198 {
bbc13ae3
KS
9199 const char *arg = b->cond_string;
9200
1bb9788d
TT
9201 loc->cond = parse_exp_1 (&arg, loc->address,
9202 block_for_pc (loc->address), 0);
0d381245 9203 if (*arg)
588ae58c 9204 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9205 }
e7e0cddf
SS
9206
9207 /* Dynamic printf requires and uses additional arguments on the
9208 command line, otherwise it's an error. */
9209 if (type == bp_dprintf)
9210 {
9211 if (b->extra_string)
9212 update_dprintf_command_list (b);
9213 else
9214 error (_("Format string required"));
9215 }
9216 else if (b->extra_string)
588ae58c 9217 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9218 }
018d34a4 9219
56435ebe 9220 b->display_canonical = display_canonical;
018d34a4
VP
9221 if (addr_string)
9222 b->addr_string = addr_string;
9223 else
9224 /* addr_string has to be used or breakpoint_re_set will delete
9225 me. */
5af949e3
UW
9226 b->addr_string
9227 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9228 b->filter = filter;
d9b3f62e 9229}
018d34a4 9230
d9b3f62e
PA
9231static void
9232create_breakpoint_sal (struct gdbarch *gdbarch,
9233 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9234 char *filter, char *cond_string,
e7e0cddf 9235 char *extra_string,
d9b3f62e
PA
9236 enum bptype type, enum bpdisp disposition,
9237 int thread, int task, int ignore_count,
c0a91b2b 9238 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9239 int enabled, int internal, unsigned flags,
9240 int display_canonical)
d9b3f62e
PA
9241{
9242 struct breakpoint *b;
9243 struct cleanup *old_chain;
9244
9245 if (is_tracepoint_type (type))
9246 {
9247 struct tracepoint *t;
9248
9249 t = XCNEW (struct tracepoint);
9250 b = &t->base;
9251 }
9252 else
9253 b = XNEW (struct breakpoint);
9254
9255 old_chain = make_cleanup (xfree, b);
9256
9257 init_breakpoint_sal (b, gdbarch,
9258 sals, addr_string,
e7e0cddf 9259 filter, cond_string, extra_string,
d9b3f62e
PA
9260 type, disposition,
9261 thread, task, ignore_count,
9262 ops, from_tty,
44f238bb
PA
9263 enabled, internal, flags,
9264 display_canonical);
d9b3f62e
PA
9265 discard_cleanups (old_chain);
9266
3ea46bff 9267 install_breakpoint (internal, b, 0);
018d34a4
VP
9268}
9269
9270/* Add SALS.nelts breakpoints to the breakpoint table. For each
9271 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9272 value. COND_STRING, if not NULL, specified the condition to be
9273 used for all breakpoints. Essentially the only case where
9274 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9275 function. In that case, it's still not possible to specify
9276 separate conditions for different overloaded functions, so
9277 we take just a single condition string.
9278
c3f6f71d 9279 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9280 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9281 array contents). If the function fails (error() is called), the
9282 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9283 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9284
9285static void
8cdf0e15 9286create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9287 struct linespec_result *canonical,
e7e0cddf 9288 char *cond_string, char *extra_string,
8cdf0e15
VP
9289 enum bptype type, enum bpdisp disposition,
9290 int thread, int task, int ignore_count,
c0a91b2b 9291 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9292 int enabled, int internal, unsigned flags)
c906108c 9293{
018d34a4 9294 int i;
f8eba3c6 9295 struct linespec_sals *lsal;
cc59ec59 9296
f8eba3c6
TT
9297 if (canonical->pre_expanded)
9298 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9299
9300 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9301 {
f8eba3c6
TT
9302 /* Note that 'addr_string' can be NULL in the case of a plain
9303 'break', without arguments. */
9304 char *addr_string = (canonical->addr_string
9305 ? xstrdup (canonical->addr_string)
9306 : NULL);
9307 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9308 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9309
f8eba3c6
TT
9310 make_cleanup (xfree, filter_string);
9311 create_breakpoint_sal (gdbarch, lsal->sals,
9312 addr_string,
9313 filter_string,
e7e0cddf
SS
9314 cond_string, extra_string,
9315 type, disposition,
84f4c1fe 9316 thread, task, ignore_count, ops,
44f238bb 9317 from_tty, enabled, internal, flags,
56435ebe 9318 canonical->special_display);
f8eba3c6 9319 discard_cleanups (inner);
c3f6f71d 9320 }
c3f6f71d 9321}
c906108c 9322
9998af43 9323/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9324 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9325 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9326 address strings. ADDRESS points to the end of the SAL.
9327
9328 The array and the line spec strings are allocated on the heap, it is
9329 the caller's responsibility to free them. */
c906108c 9330
b9362cc7 9331static void
c3f6f71d 9332parse_breakpoint_sals (char **address,
58438ac1 9333 struct linespec_result *canonical)
c3f6f71d 9334{
c3f6f71d 9335 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9336 breakpoint. */
0578b14e 9337 if ((*address) == NULL || linespec_lexer_lex_keyword (*address))
c906108c 9338 {
1bfeeb0f
JL
9339 /* The last displayed codepoint, if it's valid, is our default breakpoint
9340 address. */
9341 if (last_displayed_sal_is_valid ())
c906108c 9342 {
f8eba3c6 9343 struct linespec_sals lsal;
c3f6f71d 9344 struct symtab_and_line sal;
1c8cdcb1 9345 CORE_ADDR pc;
cc59ec59 9346
4a64f543 9347 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9348 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9349 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9350
9351 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9352 corresponding to the last call to print_frame_info.
9353 Be sure to reinitialize LINE with NOTCURRENT == 0
9354 as the breakpoint line number is inappropriate otherwise.
9355 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9356 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9357 pc = sal.pc;
9358 sal = find_pc_line (pc, 0);
00903456 9359
4a64f543 9360 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9361 where PC is the last displayed codepoint's address. So
9362 make sure to set sal.explicit_pc to prevent GDB from
9363 trying to expand the list of sals to include all other
9364 instances with the same symtab and line. */
1c8cdcb1 9365 sal.pc = pc;
00903456
JK
9366 sal.explicit_pc = 1;
9367
f8eba3c6
TT
9368 lsal.sals.sals[0] = sal;
9369 lsal.sals.nelts = 1;
9370 lsal.canonical = NULL;
9371
9372 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9373 }
9374 else
8a3fe4f8 9375 error (_("No default breakpoint address now."));
c906108c
SS
9376 }
9377 else
9378 {
cc80f267
JK
9379 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9380
c906108c 9381 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9382 current_source_symtab (which is decode_line_1's default).
9383 This should produce the results we want almost all of the
cc80f267
JK
9384 time while leaving default_breakpoint_* alone.
9385
9386 ObjC: However, don't match an Objective-C method name which
9387 may have a '+' or '-' succeeded by a '['. */
9388 if (last_displayed_sal_is_valid ()
9389 && (!cursal.symtab
9390 || ((strchr ("+-", (*address)[0]) != NULL)
9391 && ((*address)[1] != '['))))
f8eba3c6
TT
9392 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9393 get_last_displayed_symtab (),
9394 get_last_displayed_line (),
9395 canonical, NULL, NULL);
c906108c 9396 else
f8eba3c6 9397 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9398 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9399 }
c3f6f71d 9400}
c906108c 9401
c906108c 9402
c3f6f71d 9403/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9404 inserted as a breakpoint. If it can't throw an error. */
c906108c 9405
b9362cc7 9406static void
23e7acfb 9407breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9408{
9409 int i;
cc59ec59 9410
c3f6f71d 9411 for (i = 0; i < sals->nelts; i++)
ee53e872 9412 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9413}
9414
7a697b8d
SS
9415/* Fast tracepoints may have restrictions on valid locations. For
9416 instance, a fast tracepoint using a jump instead of a trap will
9417 likely have to overwrite more bytes than a trap would, and so can
9418 only be placed where the instruction is longer than the jump, or a
9419 multi-instruction sequence does not have a jump into the middle of
9420 it, etc. */
9421
9422static void
9423check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9424 struct symtabs_and_lines *sals)
9425{
9426 int i, rslt;
9427 struct symtab_and_line *sal;
9428 char *msg;
9429 struct cleanup *old_chain;
9430
9431 for (i = 0; i < sals->nelts; i++)
9432 {
f8eba3c6
TT
9433 struct gdbarch *sarch;
9434
7a697b8d
SS
9435 sal = &sals->sals[i];
9436
f8eba3c6
TT
9437 sarch = get_sal_arch (*sal);
9438 /* We fall back to GDBARCH if there is no architecture
9439 associated with SAL. */
9440 if (sarch == NULL)
9441 sarch = gdbarch;
9442 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9443 NULL, &msg);
9444 old_chain = make_cleanup (xfree, msg);
9445
9446 if (!rslt)
9447 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9448 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9449
9450 do_cleanups (old_chain);
9451 }
9452}
9453
af4908ba
KS
9454/* Issue an invalid thread ID error. */
9455
9456static void ATTRIBUTE_NORETURN
9457invalid_thread_id_error (int id)
9458{
9459 error (_("Unknown thread %d."), id);
9460}
9461
018d34a4
VP
9462/* Given TOK, a string specification of condition and thread, as
9463 accepted by the 'break' command, extract the condition
9464 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9465 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9466 If no condition is found, *COND_STRING is set to NULL.
9467 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9468
9469static void
bbc13ae3 9470find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9471 char **cond_string, int *thread, int *task,
9472 char **rest)
018d34a4
VP
9473{
9474 *cond_string = NULL;
9475 *thread = -1;
ed1d1739
KS
9476 *task = 0;
9477 *rest = NULL;
9478
018d34a4
VP
9479 while (tok && *tok)
9480 {
bbc13ae3 9481 const char *end_tok;
018d34a4 9482 int toklen;
bbc13ae3
KS
9483 const char *cond_start = NULL;
9484 const char *cond_end = NULL;
cc59ec59 9485
bbc13ae3 9486 tok = skip_spaces_const (tok);
e7e0cddf
SS
9487
9488 if ((*tok == '"' || *tok == ',') && rest)
9489 {
9490 *rest = savestring (tok, strlen (tok));
9491 return;
9492 }
9493
bbc13ae3 9494 end_tok = skip_to_space_const (tok);
d634f2de 9495
018d34a4 9496 toklen = end_tok - tok;
d634f2de 9497
018d34a4
VP
9498 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9499 {
f7545552
TT
9500 struct expression *expr;
9501
018d34a4 9502 tok = cond_start = end_tok + 1;
1bb9788d 9503 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9504 xfree (expr);
018d34a4 9505 cond_end = tok;
d634f2de 9506 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9507 }
9508 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9509 {
9510 char *tmptok;
d634f2de 9511
018d34a4 9512 tok = end_tok + 1;
bbc13ae3 9513 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9514 if (tok == tmptok)
9515 error (_("Junk after thread keyword."));
9516 if (!valid_thread_id (*thread))
af4908ba 9517 invalid_thread_id_error (*thread);
bbc13ae3 9518 tok = tmptok;
018d34a4 9519 }
4a306c9a
JB
9520 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9521 {
9522 char *tmptok;
9523
9524 tok = end_tok + 1;
bbc13ae3 9525 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9526 if (tok == tmptok)
9527 error (_("Junk after task keyword."));
9528 if (!valid_task_id (*task))
b6199126 9529 error (_("Unknown task %d."), *task);
bbc13ae3 9530 tok = tmptok;
4a306c9a 9531 }
e7e0cddf
SS
9532 else if (rest)
9533 {
9534 *rest = savestring (tok, strlen (tok));
ccab2054 9535 return;
e7e0cddf 9536 }
018d34a4
VP
9537 else
9538 error (_("Junk at end of arguments."));
9539 }
9540}
9541
0fb4aa4b
PA
9542/* Decode a static tracepoint marker spec. */
9543
9544static struct symtabs_and_lines
9545decode_static_tracepoint_spec (char **arg_p)
9546{
9547 VEC(static_tracepoint_marker_p) *markers = NULL;
9548 struct symtabs_and_lines sals;
0fb4aa4b
PA
9549 struct cleanup *old_chain;
9550 char *p = &(*arg_p)[3];
9551 char *endp;
9552 char *marker_str;
9553 int i;
9554
e9cafbcc 9555 p = skip_spaces (p);
0fb4aa4b 9556
e9cafbcc 9557 endp = skip_to_space (p);
0fb4aa4b
PA
9558
9559 marker_str = savestring (p, endp - p);
9560 old_chain = make_cleanup (xfree, marker_str);
9561
9562 markers = target_static_tracepoint_markers_by_strid (marker_str);
9563 if (VEC_empty(static_tracepoint_marker_p, markers))
9564 error (_("No known static tracepoint marker named %s"), marker_str);
9565
9566 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9567 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9568
9569 for (i = 0; i < sals.nelts; i++)
9570 {
9571 struct static_tracepoint_marker *marker;
9572
9573 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9574
9575 init_sal (&sals.sals[i]);
9576
9577 sals.sals[i] = find_pc_line (marker->address, 0);
9578 sals.sals[i].pc = marker->address;
9579
9580 release_static_tracepoint_marker (marker);
9581 }
9582
9583 do_cleanups (old_chain);
9584
9585 *arg_p = endp;
9586 return sals;
9587}
9588
fd9b8c24
PA
9589/* Set a breakpoint. This function is shared between CLI and MI
9590 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9591 modes of operations, selected by the PARSE_ARG parameter. If
9592 non-zero, the function will parse ARG, extracting location,
9593 condition, thread and extra string. Otherwise, ARG is just the
9594 breakpoint's location, with condition, thread, and extra string
9595 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9596 If INTERNAL is non-zero, the breakpoint number will be allocated
9597 from the internal breakpoint count. Returns true if any breakpoint
9598 was created; false otherwise. */
0101ce28 9599
8cdf0e15
VP
9600int
9601create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9602 char *arg, char *cond_string,
9603 int thread, char *extra_string,
f6de8ec2 9604 int parse_arg,
0fb4aa4b 9605 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9606 int ignore_count,
9607 enum auto_boolean pending_break_support,
c0a91b2b 9608 const struct breakpoint_ops *ops,
44f238bb
PA
9609 int from_tty, int enabled, int internal,
9610 unsigned flags)
c3f6f71d 9611{
f8eba3c6 9612 char *copy_arg = NULL;
c3f6f71d 9613 char *addr_start = arg;
7efd8fc2 9614 struct linespec_result canonical;
c3f6f71d 9615 struct cleanup *old_chain;
80c99de1 9616 struct cleanup *bkpt_chain = NULL;
0101ce28 9617 int pending = 0;
4a306c9a 9618 int task = 0;
86b17b60 9619 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9620
348d480f
PA
9621 gdb_assert (ops != NULL);
9622
7efd8fc2 9623 init_linespec_result (&canonical);
c3f6f71d 9624
492d29ea 9625 TRY
b78a6381 9626 {
983af33b
SDJ
9627 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9628 addr_start, &copy_arg);
b78a6381 9629 }
492d29ea 9630 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9631 {
492d29ea
PA
9632 /* If caller is interested in rc value from parse, set
9633 value. */
9634 if (e.error == NOT_FOUND_ERROR)
0101ce28 9635 {
05ff989b
AC
9636 /* If pending breakpoint support is turned off, throw
9637 error. */
fa8d40ab
JJ
9638
9639 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9640 throw_exception (e);
9641
9642 exception_print (gdb_stderr, e);
fa8d40ab 9643
05ff989b
AC
9644 /* If pending breakpoint support is auto query and the user
9645 selects no, then simply return the error code. */
059fb39f 9646 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9647 && !nquery (_("Make %s pending on future shared library load? "),
9648 bptype_string (type_wanted)))
fd9b8c24 9649 return 0;
fa8d40ab 9650
05ff989b
AC
9651 /* At this point, either the user was queried about setting
9652 a pending breakpoint and selected yes, or pending
9653 breakpoint behavior is on and thus a pending breakpoint
9654 is defaulted on behalf of the user. */
f8eba3c6
TT
9655 {
9656 struct linespec_sals lsal;
9657
9658 copy_arg = xstrdup (addr_start);
9659 lsal.canonical = xstrdup (copy_arg);
9660 lsal.sals.nelts = 1;
9661 lsal.sals.sals = XNEW (struct symtab_and_line);
9662 init_sal (&lsal.sals.sals[0]);
9663 pending = 1;
9664 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9665 }
0101ce28 9666 }
492d29ea
PA
9667 else
9668 throw_exception (e);
0101ce28 9669 }
492d29ea
PA
9670 END_CATCH
9671
9672 if (VEC_empty (linespec_sals, canonical.sals))
9673 return 0;
c3f6f71d 9674
4a64f543 9675 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9676 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9677
c3f6f71d
JM
9678 /* ----------------------------- SNIP -----------------------------
9679 Anything added to the cleanup chain beyond this point is assumed
9680 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9681 then the memory is not reclaimed. */
9682 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9683
c3f6f71d
JM
9684 /* Resolve all line numbers to PC's and verify that the addresses
9685 are ok for the target. */
0101ce28 9686 if (!pending)
f8eba3c6
TT
9687 {
9688 int ix;
9689 struct linespec_sals *iter;
9690
9691 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9692 breakpoint_sals_to_pc (&iter->sals);
9693 }
c3f6f71d 9694
7a697b8d 9695 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9696 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9697 {
9698 int ix;
9699 struct linespec_sals *iter;
9700
9701 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9702 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9703 }
7a697b8d 9704
c3f6f71d
JM
9705 /* Verify that condition can be parsed, before setting any
9706 breakpoints. Allocate a separate condition expression for each
4a64f543 9707 breakpoint. */
0101ce28 9708 if (!pending)
c3f6f71d 9709 {
f6de8ec2 9710 if (parse_arg)
72b2ff0e 9711 {
0878d0fa 9712 char *rest;
52d361e1
YQ
9713 struct linespec_sals *lsal;
9714
9715 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9716
0878d0fa
YQ
9717 /* Here we only parse 'arg' to separate condition
9718 from thread number, so parsing in context of first
9719 sal is OK. When setting the breakpoint we'll
9720 re-parse it in context of each sal. */
9721
9722 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9723 &thread, &task, &rest);
9724 if (cond_string)
9725 make_cleanup (xfree, cond_string);
9726 if (rest)
9727 make_cleanup (xfree, rest);
9728 if (rest)
9729 extra_string = rest;
72b2ff0e 9730 }
2f069f6f 9731 else
72b2ff0e 9732 {
0878d0fa
YQ
9733 if (*arg != '\0')
9734 error (_("Garbage '%s' at end of location"), arg);
9735
9736 /* Create a private copy of condition string. */
9737 if (cond_string)
9738 {
9739 cond_string = xstrdup (cond_string);
9740 make_cleanup (xfree, cond_string);
9741 }
9742 /* Create a private copy of any extra string. */
9743 if (extra_string)
9744 {
9745 extra_string = xstrdup (extra_string);
9746 make_cleanup (xfree, extra_string);
9747 }
72b2ff0e 9748 }
0fb4aa4b 9749
52d361e1 9750 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9751 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9752 tempflag ? disp_del : disp_donttouch,
9753 thread, task, ignore_count, ops,
44f238bb 9754 from_tty, enabled, internal, flags);
c906108c 9755 }
0101ce28
JJ
9756 else
9757 {
0101ce28
JJ
9758 struct breakpoint *b;
9759
0101ce28
JJ
9760 make_cleanup (xfree, copy_arg);
9761
bfccc43c
YQ
9762 if (is_tracepoint_type (type_wanted))
9763 {
9764 struct tracepoint *t;
9765
9766 t = XCNEW (struct tracepoint);
9767 b = &t->base;
9768 }
9769 else
9770 b = XNEW (struct breakpoint);
9771
9772 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9773
f8eba3c6 9774 b->addr_string = copy_arg;
f6de8ec2 9775 if (parse_arg)
e12c7713
MK
9776 b->cond_string = NULL;
9777 else
9778 {
9779 /* Create a private copy of condition string. */
9780 if (cond_string)
9781 {
9782 cond_string = xstrdup (cond_string);
9783 make_cleanup (xfree, cond_string);
9784 }
9785 b->cond_string = cond_string;
15630549 9786 b->thread = thread;
e12c7713 9787 }
e7e0cddf 9788 b->extra_string = NULL;
0101ce28 9789 b->ignore_count = ignore_count;
0101ce28 9790 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9791 b->condition_not_parsed = 1;
41447f92 9792 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9793 if ((type_wanted != bp_breakpoint
9794 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9795 b->pspace = current_program_space;
8bea4e01 9796
bfccc43c 9797 install_breakpoint (internal, b, 0);
0101ce28
JJ
9798 }
9799
f8eba3c6 9800 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9801 {
3e43a32a
MS
9802 warning (_("Multiple breakpoints were set.\nUse the "
9803 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9804 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9805 }
9806
80c99de1
PA
9807 /* That's it. Discard the cleanups for data inserted into the
9808 breakpoint. */
9809 discard_cleanups (bkpt_chain);
9810 /* But cleanup everything else. */
c3f6f71d 9811 do_cleanups (old_chain);
217dc9e2 9812
80c99de1 9813 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9814 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9815
9816 return 1;
c3f6f71d 9817}
c906108c 9818
348d480f 9819/* Set a breakpoint.
72b2ff0e
VP
9820 ARG is a string describing breakpoint address,
9821 condition, and thread.
9822 FLAG specifies if a breakpoint is hardware on,
9823 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9824 and BP_TEMPFLAG. */
348d480f 9825
98deb0da 9826static void
72b2ff0e 9827break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9828{
72b2ff0e 9829 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9830 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9831 ? bp_hardware_breakpoint
9832 : bp_breakpoint);
55aa24fb
SDJ
9833 struct breakpoint_ops *ops;
9834 const char *arg_cp = arg;
9835
9836 /* Matching breakpoints on probes. */
9837 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9838 ops = &bkpt_probe_breakpoint_ops;
9839 else
9840 ops = &bkpt_breakpoint_ops;
c3f6f71d 9841
8cdf0e15
VP
9842 create_breakpoint (get_current_arch (),
9843 arg,
e7e0cddf 9844 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 9845 tempflag, type_wanted,
8cdf0e15
VP
9846 0 /* Ignore count */,
9847 pending_break_support,
55aa24fb 9848 ops,
8cdf0e15 9849 from_tty,
84f4c1fe 9850 1 /* enabled */,
44f238bb
PA
9851 0 /* internal */,
9852 0);
c906108c
SS
9853}
9854
c906108c
SS
9855/* Helper function for break_command_1 and disassemble_command. */
9856
9857void
fba45db2 9858resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9859{
9860 CORE_ADDR pc;
9861
9862 if (sal->pc == 0 && sal->symtab != NULL)
9863 {
9864 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9865 error (_("No line %d in file \"%s\"."),
05cba821 9866 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9867 sal->pc = pc;
6a048695 9868
4a64f543
MS
9869 /* If this SAL corresponds to a breakpoint inserted using a line
9870 number, then skip the function prologue if necessary. */
6a048695 9871 if (sal->explicit_line)
059acae7 9872 skip_prologue_sal (sal);
c906108c
SS
9873 }
9874
9875 if (sal->section == 0 && sal->symtab != NULL)
9876 {
346d1dfe 9877 const struct blockvector *bv;
3977b71f 9878 const struct block *b;
c5aa993b 9879 struct symbol *sym;
c906108c 9880
43f3e411
DE
9881 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9882 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9883 if (bv != NULL)
9884 {
7f0df278 9885 sym = block_linkage_function (b);
c906108c
SS
9886 if (sym != NULL)
9887 {
eb822aa6
DE
9888 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9889 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9890 sym);
c906108c
SS
9891 }
9892 else
9893 {
4a64f543
MS
9894 /* It really is worthwhile to have the section, so we'll
9895 just have to look harder. This case can be executed
9896 if we have line numbers but no functions (as can
9897 happen in assembly source). */
c906108c 9898
7cbd4a93 9899 struct bound_minimal_symbol msym;
6c95b8df
PA
9900 struct cleanup *old_chain = save_current_space_and_thread ();
9901
9902 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
9903
9904 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9905 if (msym.minsym)
efd66ac6 9906 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
9907
9908 do_cleanups (old_chain);
c906108c
SS
9909 }
9910 }
9911 }
9912}
9913
9914void
fba45db2 9915break_command (char *arg, int from_tty)
c906108c 9916{
db107f19 9917 break_command_1 (arg, 0, from_tty);
c906108c
SS
9918}
9919
c906108c 9920void
fba45db2 9921tbreak_command (char *arg, int from_tty)
c906108c 9922{
db107f19 9923 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9924}
9925
c906108c 9926static void
fba45db2 9927hbreak_command (char *arg, int from_tty)
c906108c 9928{
db107f19 9929 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9930}
9931
9932static void
fba45db2 9933thbreak_command (char *arg, int from_tty)
c906108c 9934{
db107f19 9935 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9936}
9937
9938static void
fba45db2 9939stop_command (char *arg, int from_tty)
c906108c 9940{
a3f17187 9941 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9942Usage: stop in <function | address>\n\
a3f17187 9943 stop at <line>\n"));
c906108c
SS
9944}
9945
9946static void
fba45db2 9947stopin_command (char *arg, int from_tty)
c906108c
SS
9948{
9949 int badInput = 0;
9950
c5aa993b 9951 if (arg == (char *) NULL)
c906108c
SS
9952 badInput = 1;
9953 else if (*arg != '*')
9954 {
9955 char *argptr = arg;
9956 int hasColon = 0;
9957
4a64f543 9958 /* Look for a ':'. If this is a line number specification, then
53a5351d 9959 say it is bad, otherwise, it should be an address or
4a64f543 9960 function/method name. */
c906108c 9961 while (*argptr && !hasColon)
c5aa993b
JM
9962 {
9963 hasColon = (*argptr == ':');
9964 argptr++;
9965 }
c906108c
SS
9966
9967 if (hasColon)
c5aa993b 9968 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9969 else
c5aa993b 9970 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9971 }
9972
9973 if (badInput)
a3f17187 9974 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9975 else
db107f19 9976 break_command_1 (arg, 0, from_tty);
c906108c
SS
9977}
9978
9979static void
fba45db2 9980stopat_command (char *arg, int from_tty)
c906108c
SS
9981{
9982 int badInput = 0;
9983
c5aa993b 9984 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9985 badInput = 1;
9986 else
9987 {
9988 char *argptr = arg;
9989 int hasColon = 0;
9990
4a64f543
MS
9991 /* Look for a ':'. If there is a '::' then get out, otherwise
9992 it is probably a line number. */
c906108c 9993 while (*argptr && !hasColon)
c5aa993b
JM
9994 {
9995 hasColon = (*argptr == ':');
9996 argptr++;
9997 }
c906108c
SS
9998
9999 if (hasColon)
c5aa993b 10000 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10001 else
c5aa993b 10002 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10003 }
10004
10005 if (badInput)
a3f17187 10006 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10007 else
db107f19 10008 break_command_1 (arg, 0, from_tty);
c906108c
SS
10009}
10010
e7e0cddf
SS
10011/* The dynamic printf command is mostly like a regular breakpoint, but
10012 with a prewired command list consisting of a single output command,
10013 built from extra arguments supplied on the dprintf command
10014 line. */
10015
da821c7b 10016static void
e7e0cddf
SS
10017dprintf_command (char *arg, int from_tty)
10018{
10019 create_breakpoint (get_current_arch (),
10020 arg,
10021 NULL, 0, NULL, 1 /* parse arg */,
10022 0, bp_dprintf,
10023 0 /* Ignore count */,
10024 pending_break_support,
10025 &dprintf_breakpoint_ops,
10026 from_tty,
10027 1 /* enabled */,
10028 0 /* internal */,
10029 0);
10030}
10031
d3ce09f5
SS
10032static void
10033agent_printf_command (char *arg, int from_tty)
10034{
10035 error (_("May only run agent-printf on the target"));
10036}
10037
f1310107
TJB
10038/* Implement the "breakpoint_hit" breakpoint_ops method for
10039 ranged breakpoints. */
10040
10041static int
10042breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10043 struct address_space *aspace,
09ac7c10
TT
10044 CORE_ADDR bp_addr,
10045 const struct target_waitstatus *ws)
f1310107 10046{
09ac7c10 10047 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10048 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10049 return 0;
10050
f1310107
TJB
10051 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10052 bl->length, aspace, bp_addr);
10053}
10054
10055/* Implement the "resources_needed" breakpoint_ops method for
10056 ranged breakpoints. */
10057
10058static int
10059resources_needed_ranged_breakpoint (const struct bp_location *bl)
10060{
10061 return target_ranged_break_num_registers ();
10062}
10063
10064/* Implement the "print_it" breakpoint_ops method for
10065 ranged breakpoints. */
10066
10067static enum print_stop_action
348d480f 10068print_it_ranged_breakpoint (bpstat bs)
f1310107 10069{
348d480f 10070 struct breakpoint *b = bs->breakpoint_at;
f1310107 10071 struct bp_location *bl = b->loc;
79a45e25 10072 struct ui_out *uiout = current_uiout;
f1310107
TJB
10073
10074 gdb_assert (b->type == bp_hardware_breakpoint);
10075
10076 /* Ranged breakpoints have only one location. */
10077 gdb_assert (bl && bl->next == NULL);
10078
10079 annotate_breakpoint (b->number);
10080 if (b->disposition == disp_del)
10081 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10082 else
10083 ui_out_text (uiout, "\nRanged breakpoint ");
10084 if (ui_out_is_mi_like_p (uiout))
10085 {
10086 ui_out_field_string (uiout, "reason",
10087 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10088 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10089 }
10090 ui_out_field_int (uiout, "bkptno", b->number);
10091 ui_out_text (uiout, ", ");
10092
10093 return PRINT_SRC_AND_LOC;
10094}
10095
10096/* Implement the "print_one" breakpoint_ops method for
10097 ranged breakpoints. */
10098
10099static void
10100print_one_ranged_breakpoint (struct breakpoint *b,
10101 struct bp_location **last_loc)
10102{
10103 struct bp_location *bl = b->loc;
10104 struct value_print_options opts;
79a45e25 10105 struct ui_out *uiout = current_uiout;
f1310107
TJB
10106
10107 /* Ranged breakpoints have only one location. */
10108 gdb_assert (bl && bl->next == NULL);
10109
10110 get_user_print_options (&opts);
10111
10112 if (opts.addressprint)
10113 /* We don't print the address range here, it will be printed later
10114 by print_one_detail_ranged_breakpoint. */
10115 ui_out_field_skip (uiout, "addr");
10116 annotate_field (5);
10117 print_breakpoint_location (b, bl);
10118 *last_loc = bl;
10119}
10120
10121/* Implement the "print_one_detail" breakpoint_ops method for
10122 ranged breakpoints. */
10123
10124static void
10125print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10126 struct ui_out *uiout)
10127{
10128 CORE_ADDR address_start, address_end;
10129 struct bp_location *bl = b->loc;
f99d8bf4
PA
10130 struct ui_file *stb = mem_fileopen ();
10131 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10132
10133 gdb_assert (bl);
10134
10135 address_start = bl->address;
10136 address_end = address_start + bl->length - 1;
10137
10138 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10139 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10140 print_core_address (bl->gdbarch, address_start),
10141 print_core_address (bl->gdbarch, address_end));
10142 ui_out_field_stream (uiout, "addr", stb);
10143 ui_out_text (uiout, "\n");
10144
10145 do_cleanups (cleanup);
10146}
10147
10148/* Implement the "print_mention" breakpoint_ops method for
10149 ranged breakpoints. */
10150
10151static void
10152print_mention_ranged_breakpoint (struct breakpoint *b)
10153{
10154 struct bp_location *bl = b->loc;
79a45e25 10155 struct ui_out *uiout = current_uiout;
f1310107
TJB
10156
10157 gdb_assert (bl);
10158 gdb_assert (b->type == bp_hardware_breakpoint);
10159
10160 if (ui_out_is_mi_like_p (uiout))
10161 return;
10162
10163 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10164 b->number, paddress (bl->gdbarch, bl->address),
10165 paddress (bl->gdbarch, bl->address + bl->length - 1));
10166}
10167
10168/* Implement the "print_recreate" breakpoint_ops method for
10169 ranged breakpoints. */
10170
10171static void
10172print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10173{
10174 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10175 b->addr_string_range_end);
d9b3f62e 10176 print_recreate_thread (b, fp);
f1310107
TJB
10177}
10178
10179/* The breakpoint_ops structure to be used in ranged breakpoints. */
10180
2060206e 10181static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10182
10183/* Find the address where the end of the breakpoint range should be
10184 placed, given the SAL of the end of the range. This is so that if
10185 the user provides a line number, the end of the range is set to the
10186 last instruction of the given line. */
10187
10188static CORE_ADDR
10189find_breakpoint_range_end (struct symtab_and_line sal)
10190{
10191 CORE_ADDR end;
10192
10193 /* If the user provided a PC value, use it. Otherwise,
10194 find the address of the end of the given location. */
10195 if (sal.explicit_pc)
10196 end = sal.pc;
10197 else
10198 {
10199 int ret;
10200 CORE_ADDR start;
10201
10202 ret = find_line_pc_range (sal, &start, &end);
10203 if (!ret)
10204 error (_("Could not find location of the end of the range."));
10205
10206 /* find_line_pc_range returns the start of the next line. */
10207 end--;
10208 }
10209
10210 return end;
10211}
10212
10213/* Implement the "break-range" CLI command. */
10214
10215static void
10216break_range_command (char *arg, int from_tty)
10217{
10218 char *arg_start, *addr_string_start, *addr_string_end;
10219 struct linespec_result canonical_start, canonical_end;
10220 int bp_count, can_use_bp, length;
10221 CORE_ADDR end;
10222 struct breakpoint *b;
10223 struct symtab_and_line sal_start, sal_end;
f1310107 10224 struct cleanup *cleanup_bkpt;
f8eba3c6 10225 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10226
10227 /* We don't support software ranged breakpoints. */
10228 if (target_ranged_break_num_registers () < 0)
10229 error (_("This target does not support hardware ranged breakpoints."));
10230
10231 bp_count = hw_breakpoint_used_count ();
10232 bp_count += target_ranged_break_num_registers ();
10233 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10234 bp_count, 0);
10235 if (can_use_bp < 0)
10236 error (_("Hardware breakpoints used exceeds limit."));
10237
f8eba3c6 10238 arg = skip_spaces (arg);
f1310107
TJB
10239 if (arg == NULL || arg[0] == '\0')
10240 error(_("No address range specified."));
10241
f1310107
TJB
10242 init_linespec_result (&canonical_start);
10243
f8eba3c6
TT
10244 arg_start = arg;
10245 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10246
f8eba3c6 10247 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10248
10249 if (arg[0] != ',')
10250 error (_("Too few arguments."));
f8eba3c6 10251 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10252 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10253
10254 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10255
10256 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10257 || lsal_start->sals.nelts != 1)
f1310107
TJB
10258 error (_("Cannot create a ranged breakpoint with multiple locations."));
10259
f8eba3c6
TT
10260 sal_start = lsal_start->sals.sals[0];
10261 addr_string_start = savestring (arg_start, arg - arg_start);
10262 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10263
10264 arg++; /* Skip the comma. */
f8eba3c6 10265 arg = skip_spaces (arg);
f1310107
TJB
10266
10267 /* Parse the end location. */
10268
f1310107
TJB
10269 init_linespec_result (&canonical_end);
10270 arg_start = arg;
10271
f8eba3c6 10272 /* We call decode_line_full directly here instead of using
f1310107
TJB
10273 parse_breakpoint_sals because we need to specify the start location's
10274 symtab and line as the default symtab and line for the end of the
10275 range. This makes it possible to have ranges like "foo.c:27, +14",
10276 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10277 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10278 sal_start.symtab, sal_start.line,
10279 &canonical_end, NULL, NULL);
10280
10281 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10282
f8eba3c6 10283 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10284 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10285
10286 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10287 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10288 || lsal_end->sals.nelts != 1)
f1310107
TJB
10289 error (_("Cannot create a ranged breakpoint with multiple locations."));
10290
f8eba3c6
TT
10291 sal_end = lsal_end->sals.sals[0];
10292 addr_string_end = savestring (arg_start, arg - arg_start);
10293 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10294
10295 end = find_breakpoint_range_end (sal_end);
10296 if (sal_start.pc > end)
177b42fe 10297 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10298
10299 length = end - sal_start.pc + 1;
10300 if (length < 0)
10301 /* Length overflowed. */
10302 error (_("Address range too large."));
10303 else if (length == 1)
10304 {
10305 /* This range is simple enough to be handled by
10306 the `hbreak' command. */
10307 hbreak_command (addr_string_start, 1);
10308
10309 do_cleanups (cleanup_bkpt);
10310
10311 return;
10312 }
10313
10314 /* Now set up the breakpoint. */
10315 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10316 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10317 set_breakpoint_count (breakpoint_count + 1);
10318 b->number = breakpoint_count;
10319 b->disposition = disp_donttouch;
f8eba3c6
TT
10320 b->addr_string = xstrdup (addr_string_start);
10321 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10322 b->loc->length = length;
10323
f8eba3c6 10324 do_cleanups (cleanup_bkpt);
f1310107
TJB
10325
10326 mention (b);
8d3788bd 10327 observer_notify_breakpoint_created (b);
44702360 10328 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10329}
10330
4a64f543
MS
10331/* Return non-zero if EXP is verified as constant. Returned zero
10332 means EXP is variable. Also the constant detection may fail for
10333 some constant expressions and in such case still falsely return
10334 zero. */
2e6e3d9c 10335
65d79d4b
SDJ
10336static int
10337watchpoint_exp_is_const (const struct expression *exp)
10338{
10339 int i = exp->nelts;
10340
10341 while (i > 0)
10342 {
10343 int oplenp, argsp;
10344
10345 /* We are only interested in the descriptor of each element. */
10346 operator_length (exp, i, &oplenp, &argsp);
10347 i -= oplenp;
10348
10349 switch (exp->elts[i].opcode)
10350 {
10351 case BINOP_ADD:
10352 case BINOP_SUB:
10353 case BINOP_MUL:
10354 case BINOP_DIV:
10355 case BINOP_REM:
10356 case BINOP_MOD:
10357 case BINOP_LSH:
10358 case BINOP_RSH:
10359 case BINOP_LOGICAL_AND:
10360 case BINOP_LOGICAL_OR:
10361 case BINOP_BITWISE_AND:
10362 case BINOP_BITWISE_IOR:
10363 case BINOP_BITWISE_XOR:
10364 case BINOP_EQUAL:
10365 case BINOP_NOTEQUAL:
10366 case BINOP_LESS:
10367 case BINOP_GTR:
10368 case BINOP_LEQ:
10369 case BINOP_GEQ:
10370 case BINOP_REPEAT:
10371 case BINOP_COMMA:
10372 case BINOP_EXP:
10373 case BINOP_MIN:
10374 case BINOP_MAX:
10375 case BINOP_INTDIV:
10376 case BINOP_CONCAT:
65d79d4b
SDJ
10377 case TERNOP_COND:
10378 case TERNOP_SLICE:
65d79d4b
SDJ
10379
10380 case OP_LONG:
10381 case OP_DOUBLE:
10382 case OP_DECFLOAT:
10383 case OP_LAST:
10384 case OP_COMPLEX:
10385 case OP_STRING:
65d79d4b
SDJ
10386 case OP_ARRAY:
10387 case OP_TYPE:
608b4967
TT
10388 case OP_TYPEOF:
10389 case OP_DECLTYPE:
6e72ca20 10390 case OP_TYPEID:
65d79d4b
SDJ
10391 case OP_NAME:
10392 case OP_OBJC_NSSTRING:
10393
10394 case UNOP_NEG:
10395 case UNOP_LOGICAL_NOT:
10396 case UNOP_COMPLEMENT:
10397 case UNOP_ADDR:
10398 case UNOP_HIGH:
aeaa2474 10399 case UNOP_CAST:
9eaf6705
TT
10400
10401 case UNOP_CAST_TYPE:
10402 case UNOP_REINTERPRET_CAST:
10403 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10404 /* Unary, binary and ternary operators: We have to check
10405 their operands. If they are constant, then so is the
10406 result of that operation. For instance, if A and B are
10407 determined to be constants, then so is "A + B".
10408
10409 UNOP_IND is one exception to the rule above, because the
10410 value of *ADDR is not necessarily a constant, even when
10411 ADDR is. */
65d79d4b
SDJ
10412 break;
10413
10414 case OP_VAR_VALUE:
10415 /* Check whether the associated symbol is a constant.
4a64f543 10416
65d79d4b 10417 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10418 possible that a buggy compiler could mark a variable as
10419 constant even when it is not, and TYPE_CONST would return
10420 true in this case, while SYMBOL_CLASS wouldn't.
10421
10422 We also have to check for function symbols because they
10423 are always constant. */
65d79d4b
SDJ
10424 {
10425 struct symbol *s = exp->elts[i + 2].symbol;
10426
10427 if (SYMBOL_CLASS (s) != LOC_BLOCK
10428 && SYMBOL_CLASS (s) != LOC_CONST
10429 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10430 return 0;
10431 break;
10432 }
10433
10434 /* The default action is to return 0 because we are using
10435 the optimistic approach here: If we don't know something,
10436 then it is not a constant. */
10437 default:
10438 return 0;
10439 }
10440 }
10441
10442 return 1;
10443}
10444
3a5c3e22
PA
10445/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10446
10447static void
10448dtor_watchpoint (struct breakpoint *self)
10449{
10450 struct watchpoint *w = (struct watchpoint *) self;
10451
10452 xfree (w->cond_exp);
10453 xfree (w->exp);
10454 xfree (w->exp_string);
10455 xfree (w->exp_string_reparse);
10456 value_free (w->val);
10457
10458 base_breakpoint_ops.dtor (self);
10459}
10460
348d480f
PA
10461/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10462
10463static void
10464re_set_watchpoint (struct breakpoint *b)
10465{
3a5c3e22
PA
10466 struct watchpoint *w = (struct watchpoint *) b;
10467
348d480f
PA
10468 /* Watchpoint can be either on expression using entirely global
10469 variables, or it can be on local variables.
10470
10471 Watchpoints of the first kind are never auto-deleted, and even
10472 persist across program restarts. Since they can use variables
10473 from shared libraries, we need to reparse expression as libraries
10474 are loaded and unloaded.
10475
10476 Watchpoints on local variables can also change meaning as result
10477 of solib event. For example, if a watchpoint uses both a local
10478 and a global variables in expression, it's a local watchpoint,
10479 but unloading of a shared library will make the expression
10480 invalid. This is not a very common use case, but we still
10481 re-evaluate expression, to avoid surprises to the user.
10482
10483 Note that for local watchpoints, we re-evaluate it only if
10484 watchpoints frame id is still valid. If it's not, it means the
10485 watchpoint is out of scope and will be deleted soon. In fact,
10486 I'm not sure we'll ever be called in this case.
10487
10488 If a local watchpoint's frame id is still valid, then
3a5c3e22 10489 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10490
3a5c3e22
PA
10491 Don't do anything about disabled watchpoints, since they will be
10492 reevaluated again when enabled. */
10493 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10494}
10495
77b06cd7
TJB
10496/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10497
10498static int
10499insert_watchpoint (struct bp_location *bl)
10500{
3a5c3e22
PA
10501 struct watchpoint *w = (struct watchpoint *) bl->owner;
10502 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10503
10504 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10505 w->cond_exp);
77b06cd7
TJB
10506}
10507
10508/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10509
10510static int
10511remove_watchpoint (struct bp_location *bl)
10512{
3a5c3e22
PA
10513 struct watchpoint *w = (struct watchpoint *) bl->owner;
10514 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10515
10516 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10517 w->cond_exp);
e09342b5
TJB
10518}
10519
e09342b5 10520static int
348d480f 10521breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10522 struct address_space *aspace, CORE_ADDR bp_addr,
10523 const struct target_waitstatus *ws)
e09342b5 10524{
348d480f 10525 struct breakpoint *b = bl->owner;
3a5c3e22 10526 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10527
348d480f
PA
10528 /* Continuable hardware watchpoints are treated as non-existent if the
10529 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10530 some data address). Otherwise gdb won't stop on a break instruction
10531 in the code (not from a breakpoint) when a hardware watchpoint has
10532 been defined. Also skip watchpoints which we know did not trigger
10533 (did not match the data address). */
10534 if (is_hardware_watchpoint (b)
3a5c3e22 10535 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10536 return 0;
9c06b0b4 10537
348d480f 10538 return 1;
9c06b0b4
TJB
10539}
10540
348d480f
PA
10541static void
10542check_status_watchpoint (bpstat bs)
9c06b0b4 10543{
348d480f 10544 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10545
348d480f 10546 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10547}
10548
10549/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10550 hardware watchpoints. */
9c06b0b4
TJB
10551
10552static int
348d480f 10553resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10554{
3a5c3e22
PA
10555 struct watchpoint *w = (struct watchpoint *) bl->owner;
10556 int length = w->exact? 1 : bl->length;
348d480f
PA
10557
10558 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10559}
10560
10561/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10562 hardware watchpoints. */
9c06b0b4
TJB
10563
10564static int
348d480f 10565works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10566{
efa80663
PA
10567 /* Read and access watchpoints only work with hardware support. */
10568 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10569}
10570
9c06b0b4 10571static enum print_stop_action
348d480f 10572print_it_watchpoint (bpstat bs)
9c06b0b4 10573{
348d480f
PA
10574 struct cleanup *old_chain;
10575 struct breakpoint *b;
f99d8bf4 10576 struct ui_file *stb;
348d480f 10577 enum print_stop_action result;
3a5c3e22 10578 struct watchpoint *w;
79a45e25 10579 struct ui_out *uiout = current_uiout;
348d480f
PA
10580
10581 gdb_assert (bs->bp_location_at != NULL);
10582
348d480f 10583 b = bs->breakpoint_at;
3a5c3e22 10584 w = (struct watchpoint *) b;
348d480f 10585
f99d8bf4
PA
10586 stb = mem_fileopen ();
10587 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10588
10589 switch (b->type)
10590 {
348d480f 10591 case bp_watchpoint:
9c06b0b4
TJB
10592 case bp_hardware_watchpoint:
10593 annotate_watchpoint (b->number);
10594 if (ui_out_is_mi_like_p (uiout))
10595 ui_out_field_string
10596 (uiout, "reason",
10597 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10598 mention (b);
10599 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10600 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10601 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10602 ui_out_field_stream (uiout, "old", stb);
10603 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10604 watchpoint_value_print (w->val, stb);
348d480f
PA
10605 ui_out_field_stream (uiout, "new", stb);
10606 ui_out_text (uiout, "\n");
10607 /* More than one watchpoint may have been triggered. */
10608 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10609 break;
10610
10611 case bp_read_watchpoint:
10612 if (ui_out_is_mi_like_p (uiout))
10613 ui_out_field_string
10614 (uiout, "reason",
10615 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10616 mention (b);
10617 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10618 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10619 watchpoint_value_print (w->val, stb);
348d480f
PA
10620 ui_out_field_stream (uiout, "value", stb);
10621 ui_out_text (uiout, "\n");
10622 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10623 break;
10624
10625 case bp_access_watchpoint:
348d480f
PA
10626 if (bs->old_val != NULL)
10627 {
10628 annotate_watchpoint (b->number);
10629 if (ui_out_is_mi_like_p (uiout))
10630 ui_out_field_string
10631 (uiout, "reason",
10632 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10633 mention (b);
10634 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10635 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10636 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10637 ui_out_field_stream (uiout, "old", stb);
10638 ui_out_text (uiout, "\nNew value = ");
10639 }
10640 else
10641 {
10642 mention (b);
10643 if (ui_out_is_mi_like_p (uiout))
10644 ui_out_field_string
10645 (uiout, "reason",
10646 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10647 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10648 ui_out_text (uiout, "\nValue = ");
10649 }
f99d8bf4 10650 watchpoint_value_print (w->val, stb);
348d480f
PA
10651 ui_out_field_stream (uiout, "new", stb);
10652 ui_out_text (uiout, "\n");
10653 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10654 break;
10655 default:
348d480f 10656 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10657 }
10658
348d480f
PA
10659 do_cleanups (old_chain);
10660 return result;
10661}
10662
10663/* Implement the "print_mention" breakpoint_ops method for hardware
10664 watchpoints. */
10665
10666static void
10667print_mention_watchpoint (struct breakpoint *b)
10668{
10669 struct cleanup *ui_out_chain;
3a5c3e22 10670 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10671 struct ui_out *uiout = current_uiout;
348d480f
PA
10672
10673 switch (b->type)
10674 {
10675 case bp_watchpoint:
10676 ui_out_text (uiout, "Watchpoint ");
10677 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10678 break;
10679 case bp_hardware_watchpoint:
10680 ui_out_text (uiout, "Hardware watchpoint ");
10681 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10682 break;
10683 case bp_read_watchpoint:
10684 ui_out_text (uiout, "Hardware read watchpoint ");
10685 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10686 break;
10687 case bp_access_watchpoint:
10688 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10689 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10690 break;
10691 default:
10692 internal_error (__FILE__, __LINE__,
10693 _("Invalid hardware watchpoint type."));
10694 }
10695
10696 ui_out_field_int (uiout, "number", b->number);
10697 ui_out_text (uiout, ": ");
3a5c3e22 10698 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10699 do_cleanups (ui_out_chain);
10700}
10701
10702/* Implement the "print_recreate" breakpoint_ops method for
10703 watchpoints. */
10704
10705static void
10706print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10707{
3a5c3e22
PA
10708 struct watchpoint *w = (struct watchpoint *) b;
10709
348d480f
PA
10710 switch (b->type)
10711 {
10712 case bp_watchpoint:
10713 case bp_hardware_watchpoint:
10714 fprintf_unfiltered (fp, "watch");
10715 break;
10716 case bp_read_watchpoint:
10717 fprintf_unfiltered (fp, "rwatch");
10718 break;
10719 case bp_access_watchpoint:
10720 fprintf_unfiltered (fp, "awatch");
10721 break;
10722 default:
10723 internal_error (__FILE__, __LINE__,
10724 _("Invalid watchpoint type."));
10725 }
10726
3a5c3e22 10727 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10728 print_recreate_thread (b, fp);
348d480f
PA
10729}
10730
427cd150
TT
10731/* Implement the "explains_signal" breakpoint_ops method for
10732 watchpoints. */
10733
47591c29 10734static int
427cd150
TT
10735explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10736{
10737 /* A software watchpoint cannot cause a signal other than
10738 GDB_SIGNAL_TRAP. */
10739 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10740 return 0;
427cd150 10741
47591c29 10742 return 1;
427cd150
TT
10743}
10744
348d480f
PA
10745/* The breakpoint_ops structure to be used in hardware watchpoints. */
10746
2060206e 10747static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10748
10749/* Implement the "insert" breakpoint_ops method for
10750 masked hardware watchpoints. */
10751
10752static int
10753insert_masked_watchpoint (struct bp_location *bl)
10754{
3a5c3e22
PA
10755 struct watchpoint *w = (struct watchpoint *) bl->owner;
10756
10757 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10758 bl->watchpoint_type);
10759}
10760
10761/* Implement the "remove" breakpoint_ops method for
10762 masked hardware watchpoints. */
10763
10764static int
10765remove_masked_watchpoint (struct bp_location *bl)
10766{
3a5c3e22
PA
10767 struct watchpoint *w = (struct watchpoint *) bl->owner;
10768
10769 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10770 bl->watchpoint_type);
10771}
10772
10773/* Implement the "resources_needed" breakpoint_ops method for
10774 masked hardware watchpoints. */
10775
10776static int
10777resources_needed_masked_watchpoint (const struct bp_location *bl)
10778{
3a5c3e22
PA
10779 struct watchpoint *w = (struct watchpoint *) bl->owner;
10780
10781 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10782}
10783
10784/* Implement the "works_in_software_mode" breakpoint_ops method for
10785 masked hardware watchpoints. */
10786
10787static int
10788works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10789{
10790 return 0;
10791}
10792
10793/* Implement the "print_it" breakpoint_ops method for
10794 masked hardware watchpoints. */
10795
10796static enum print_stop_action
10797print_it_masked_watchpoint (bpstat bs)
10798{
10799 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10800 struct ui_out *uiout = current_uiout;
348d480f
PA
10801
10802 /* Masked watchpoints have only one location. */
10803 gdb_assert (b->loc && b->loc->next == NULL);
10804
10805 switch (b->type)
10806 {
10807 case bp_hardware_watchpoint:
10808 annotate_watchpoint (b->number);
10809 if (ui_out_is_mi_like_p (uiout))
10810 ui_out_field_string
10811 (uiout, "reason",
10812 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10813 break;
10814
10815 case bp_read_watchpoint:
10816 if (ui_out_is_mi_like_p (uiout))
10817 ui_out_field_string
10818 (uiout, "reason",
10819 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10820 break;
10821
10822 case bp_access_watchpoint:
10823 if (ui_out_is_mi_like_p (uiout))
10824 ui_out_field_string
10825 (uiout, "reason",
10826 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10827 break;
10828 default:
10829 internal_error (__FILE__, __LINE__,
10830 _("Invalid hardware watchpoint type."));
10831 }
10832
10833 mention (b);
9c06b0b4
TJB
10834 ui_out_text (uiout, _("\n\
10835Check the underlying instruction at PC for the memory\n\
10836address and value which triggered this watchpoint.\n"));
10837 ui_out_text (uiout, "\n");
10838
10839 /* More than one watchpoint may have been triggered. */
10840 return PRINT_UNKNOWN;
10841}
10842
10843/* Implement the "print_one_detail" breakpoint_ops method for
10844 masked hardware watchpoints. */
10845
10846static void
10847print_one_detail_masked_watchpoint (const struct breakpoint *b,
10848 struct ui_out *uiout)
10849{
3a5c3e22
PA
10850 struct watchpoint *w = (struct watchpoint *) b;
10851
9c06b0b4
TJB
10852 /* Masked watchpoints have only one location. */
10853 gdb_assert (b->loc && b->loc->next == NULL);
10854
10855 ui_out_text (uiout, "\tmask ");
3a5c3e22 10856 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
10857 ui_out_text (uiout, "\n");
10858}
10859
10860/* Implement the "print_mention" breakpoint_ops method for
10861 masked hardware watchpoints. */
10862
10863static void
10864print_mention_masked_watchpoint (struct breakpoint *b)
10865{
3a5c3e22 10866 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10867 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
10868 struct cleanup *ui_out_chain;
10869
10870 switch (b->type)
10871 {
10872 case bp_hardware_watchpoint:
10873 ui_out_text (uiout, "Masked hardware watchpoint ");
10874 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10875 break;
10876 case bp_read_watchpoint:
10877 ui_out_text (uiout, "Masked hardware read watchpoint ");
10878 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10879 break;
10880 case bp_access_watchpoint:
10881 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10882 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10883 break;
10884 default:
10885 internal_error (__FILE__, __LINE__,
10886 _("Invalid hardware watchpoint type."));
10887 }
10888
10889 ui_out_field_int (uiout, "number", b->number);
10890 ui_out_text (uiout, ": ");
3a5c3e22 10891 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
10892 do_cleanups (ui_out_chain);
10893}
10894
10895/* Implement the "print_recreate" breakpoint_ops method for
10896 masked hardware watchpoints. */
10897
10898static void
10899print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10900{
3a5c3e22 10901 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10902 char tmp[40];
10903
10904 switch (b->type)
10905 {
10906 case bp_hardware_watchpoint:
10907 fprintf_unfiltered (fp, "watch");
10908 break;
10909 case bp_read_watchpoint:
10910 fprintf_unfiltered (fp, "rwatch");
10911 break;
10912 case bp_access_watchpoint:
10913 fprintf_unfiltered (fp, "awatch");
10914 break;
10915 default:
10916 internal_error (__FILE__, __LINE__,
10917 _("Invalid hardware watchpoint type."));
10918 }
10919
3a5c3e22
PA
10920 sprintf_vma (tmp, w->hw_wp_mask);
10921 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10922 print_recreate_thread (b, fp);
9c06b0b4
TJB
10923}
10924
10925/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10926
2060206e 10927static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10928
10929/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10930
10931static int
10932is_masked_watchpoint (const struct breakpoint *b)
10933{
10934 return b->ops == &masked_watchpoint_breakpoint_ops;
10935}
10936
53a5351d
JM
10937/* accessflag: hw_write: watch write,
10938 hw_read: watch read,
10939 hw_access: watch access (read or write) */
c906108c 10940static void
bbc13ae3 10941watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10942 int just_location, int internal)
c906108c 10943{
d983da9c 10944 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 10945 struct expression *exp;
270140bd 10946 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10947 struct value *val, *mark, *result;
bb9d5f81 10948 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 10949 struct frame_info *frame;
bbc13ae3
KS
10950 const char *exp_start = NULL;
10951 const char *exp_end = NULL;
10952 const char *tok, *end_tok;
9c06b0b4 10953 int toklen = -1;
bbc13ae3
KS
10954 const char *cond_start = NULL;
10955 const char *cond_end = NULL;
c906108c 10956 enum bptype bp_type;
37e4754d 10957 int thread = -1;
0cf6dd15 10958 int pc = 0;
9c06b0b4
TJB
10959 /* Flag to indicate whether we are going to use masks for
10960 the hardware watchpoint. */
10961 int use_mask = 0;
10962 CORE_ADDR mask = 0;
3a5c3e22 10963 struct watchpoint *w;
bbc13ae3
KS
10964 char *expression;
10965 struct cleanup *back_to;
c906108c 10966
37e4754d
LM
10967 /* Make sure that we actually have parameters to parse. */
10968 if (arg != NULL && arg[0] != '\0')
10969 {
bbc13ae3
KS
10970 const char *value_start;
10971
10972 exp_end = arg + strlen (arg);
37e4754d 10973
9c06b0b4
TJB
10974 /* Look for "parameter value" pairs at the end
10975 of the arguments string. */
bbc13ae3 10976 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10977 {
10978 /* Skip whitespace at the end of the argument list. */
10979 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10980 tok--;
10981
10982 /* Find the beginning of the last token.
10983 This is the value of the parameter. */
10984 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10985 tok--;
10986 value_start = tok + 1;
10987
10988 /* Skip whitespace. */
10989 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10990 tok--;
10991
10992 end_tok = tok;
10993
10994 /* Find the beginning of the second to last token.
10995 This is the parameter itself. */
10996 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10997 tok--;
10998 tok++;
10999 toklen = end_tok - tok + 1;
11000
61012eef 11001 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4
TJB
11002 {
11003 /* At this point we've found a "thread" token, which means
11004 the user is trying to set a watchpoint that triggers
11005 only in a specific thread. */
11006 char *endp;
37e4754d 11007
9c06b0b4
TJB
11008 if (thread != -1)
11009 error(_("You can specify only one thread."));
37e4754d 11010
9c06b0b4
TJB
11011 /* Extract the thread ID from the next token. */
11012 thread = strtol (value_start, &endp, 0);
37e4754d 11013
9c06b0b4
TJB
11014 /* Check if the user provided a valid numeric value for the
11015 thread ID. */
11016 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11017 error (_("Invalid thread ID specification %s."), value_start);
11018
11019 /* Check if the thread actually exists. */
11020 if (!valid_thread_id (thread))
af4908ba 11021 invalid_thread_id_error (thread);
9c06b0b4 11022 }
61012eef 11023 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11024 {
11025 /* We've found a "mask" token, which means the user wants to
11026 create a hardware watchpoint that is going to have the mask
11027 facility. */
11028 struct value *mask_value, *mark;
37e4754d 11029
9c06b0b4
TJB
11030 if (use_mask)
11031 error(_("You can specify only one mask."));
37e4754d 11032
9c06b0b4 11033 use_mask = just_location = 1;
37e4754d 11034
9c06b0b4
TJB
11035 mark = value_mark ();
11036 mask_value = parse_to_comma_and_eval (&value_start);
11037 mask = value_as_address (mask_value);
11038 value_free_to_mark (mark);
11039 }
11040 else
11041 /* We didn't recognize what we found. We should stop here. */
11042 break;
37e4754d 11043
9c06b0b4
TJB
11044 /* Truncate the string and get rid of the "parameter value" pair before
11045 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11046 exp_end = tok;
9c06b0b4 11047 }
37e4754d 11048 }
bbc13ae3
KS
11049 else
11050 exp_end = arg;
37e4754d 11051
bbc13ae3
KS
11052 /* Parse the rest of the arguments. From here on out, everything
11053 is in terms of a newly allocated string instead of the original
11054 ARG. */
c906108c 11055 innermost_block = NULL;
bbc13ae3
KS
11056 expression = savestring (arg, exp_end - arg);
11057 back_to = make_cleanup (xfree, expression);
11058 exp_start = arg = expression;
1bb9788d 11059 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11060 exp_end = arg;
fa8a61dc
TT
11061 /* Remove trailing whitespace from the expression before saving it.
11062 This makes the eventual display of the expression string a bit
11063 prettier. */
11064 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11065 --exp_end;
11066
65d79d4b
SDJ
11067 /* Checking if the expression is not constant. */
11068 if (watchpoint_exp_is_const (exp))
11069 {
11070 int len;
11071
11072 len = exp_end - exp_start;
11073 while (len > 0 && isspace (exp_start[len - 1]))
11074 len--;
11075 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11076 }
11077
c906108c
SS
11078 exp_valid_block = innermost_block;
11079 mark = value_mark ();
3a1115a0 11080 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11081
bb9d5f81
PP
11082 if (val != NULL && just_location)
11083 {
11084 saved_bitpos = value_bitpos (val);
11085 saved_bitsize = value_bitsize (val);
11086 }
11087
06a64a0b
TT
11088 if (just_location)
11089 {
9c06b0b4
TJB
11090 int ret;
11091
06a64a0b 11092 exp_valid_block = NULL;
a1442452 11093 val = value_addr (result);
06a64a0b
TT
11094 release_value (val);
11095 value_free_to_mark (mark);
9c06b0b4
TJB
11096
11097 if (use_mask)
11098 {
11099 ret = target_masked_watch_num_registers (value_as_address (val),
11100 mask);
11101 if (ret == -1)
11102 error (_("This target does not support masked watchpoints."));
11103 else if (ret == -2)
11104 error (_("Invalid mask or memory region."));
11105 }
06a64a0b
TT
11106 }
11107 else if (val != NULL)
fa4727a6 11108 release_value (val);
c906108c 11109
bbc13ae3
KS
11110 tok = skip_spaces_const (arg);
11111 end_tok = skip_to_space_const (tok);
c906108c
SS
11112
11113 toklen = end_tok - tok;
11114 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11115 {
2d134ed3
PA
11116 struct expression *cond;
11117
60e1c644 11118 innermost_block = NULL;
c906108c 11119 tok = cond_start = end_tok + 1;
1bb9788d 11120 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11121
11122 /* The watchpoint expression may not be local, but the condition
11123 may still be. E.g.: `watch global if local > 0'. */
11124 cond_exp_valid_block = innermost_block;
11125
2d134ed3 11126 xfree (cond);
c906108c
SS
11127 cond_end = tok;
11128 }
11129 if (*tok)
8a3fe4f8 11130 error (_("Junk at end of command."));
c906108c 11131
d983da9c 11132 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11133
11134 /* If the expression is "local", then set up a "watchpoint scope"
11135 breakpoint at the point where we've left the scope of the watchpoint
11136 expression. Create the scope breakpoint before the watchpoint, so
11137 that we will encounter it first in bpstat_stop_status. */
60e1c644 11138 if (exp_valid_block && frame)
d983da9c 11139 {
edb3359d
DJ
11140 if (frame_id_p (frame_unwind_caller_id (frame)))
11141 {
11142 scope_breakpoint
a6d9a66e
UW
11143 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11144 frame_unwind_caller_pc (frame),
06edf0c0
PA
11145 bp_watchpoint_scope,
11146 &momentary_breakpoint_ops);
d983da9c 11147
edb3359d 11148 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11149
edb3359d
DJ
11150 /* Automatically delete the breakpoint when it hits. */
11151 scope_breakpoint->disposition = disp_del;
d983da9c 11152
edb3359d
DJ
11153 /* Only break in the proper frame (help with recursion). */
11154 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11155
edb3359d 11156 /* Set the address at which we will stop. */
a6d9a66e
UW
11157 scope_breakpoint->loc->gdbarch
11158 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11159 scope_breakpoint->loc->requested_address
11160 = frame_unwind_caller_pc (frame);
11161 scope_breakpoint->loc->address
a6d9a66e
UW
11162 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11163 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11164 scope_breakpoint->type);
11165 }
d983da9c
DJ
11166 }
11167
e8369a73
AB
11168 /* Now set up the breakpoint. We create all watchpoints as hardware
11169 watchpoints here even if hardware watchpoints are turned off, a call
11170 to update_watchpoint later in this function will cause the type to
11171 drop back to bp_watchpoint (software watchpoint) if required. */
11172
11173 if (accessflag == hw_read)
11174 bp_type = bp_read_watchpoint;
11175 else if (accessflag == hw_access)
11176 bp_type = bp_access_watchpoint;
11177 else
11178 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11179
11180 w = XCNEW (struct watchpoint);
11181 b = &w->base;
348d480f 11182 if (use_mask)
3a5c3e22
PA
11183 init_raw_breakpoint_without_location (b, NULL, bp_type,
11184 &masked_watchpoint_breakpoint_ops);
348d480f 11185 else
3a5c3e22
PA
11186 init_raw_breakpoint_without_location (b, NULL, bp_type,
11187 &watchpoint_breakpoint_ops);
37e4754d 11188 b->thread = thread;
b5de0fa7 11189 b->disposition = disp_donttouch;
348d480f 11190 b->pspace = current_program_space;
3a5c3e22
PA
11191 w->exp = exp;
11192 w->exp_valid_block = exp_valid_block;
11193 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11194 if (just_location)
11195 {
11196 struct type *t = value_type (val);
11197 CORE_ADDR addr = value_as_address (val);
11198 char *name;
11199
11200 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11201 name = type_to_string (t);
11202
3a5c3e22 11203 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11204 core_addr_to_string (addr));
06a64a0b
TT
11205 xfree (name);
11206
3a5c3e22 11207 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11208 (int) (exp_end - exp_start), exp_start);
11209
06a64a0b
TT
11210 /* The above expression is in C. */
11211 b->language = language_c;
11212 }
11213 else
3a5c3e22 11214 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11215
11216 if (use_mask)
11217 {
3a5c3e22 11218 w->hw_wp_mask = mask;
9c06b0b4
TJB
11219 }
11220 else
11221 {
3a5c3e22 11222 w->val = val;
bb9d5f81
PP
11223 w->val_bitpos = saved_bitpos;
11224 w->val_bitsize = saved_bitsize;
3a5c3e22 11225 w->val_valid = 1;
9c06b0b4 11226 }
77b06cd7 11227
c906108c
SS
11228 if (cond_start)
11229 b->cond_string = savestring (cond_start, cond_end - cond_start);
11230 else
11231 b->cond_string = 0;
c5aa993b 11232
c906108c 11233 if (frame)
f6bc2008 11234 {
3a5c3e22
PA
11235 w->watchpoint_frame = get_frame_id (frame);
11236 w->watchpoint_thread = inferior_ptid;
f6bc2008 11237 }
c906108c 11238 else
f6bc2008 11239 {
3a5c3e22
PA
11240 w->watchpoint_frame = null_frame_id;
11241 w->watchpoint_thread = null_ptid;
f6bc2008 11242 }
c906108c 11243
d983da9c 11244 if (scope_breakpoint != NULL)
c906108c 11245 {
d983da9c
DJ
11246 /* The scope breakpoint is related to the watchpoint. We will
11247 need to act on them together. */
11248 b->related_breakpoint = scope_breakpoint;
11249 scope_breakpoint->related_breakpoint = b;
c906108c 11250 }
d983da9c 11251
06a64a0b
TT
11252 if (!just_location)
11253 value_free_to_mark (mark);
2d134ed3 11254
492d29ea 11255 TRY
a9634178
TJB
11256 {
11257 /* Finally update the new watchpoint. This creates the locations
11258 that should be inserted. */
3a5c3e22 11259 update_watchpoint (w, 1);
a9634178 11260 }
492d29ea 11261 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11262 {
11263 delete_breakpoint (b);
11264 throw_exception (e);
11265 }
492d29ea 11266 END_CATCH
a9634178 11267
3ea46bff 11268 install_breakpoint (internal, b, 1);
bbc13ae3 11269 do_cleanups (back_to);
c906108c
SS
11270}
11271
e09342b5 11272/* Return count of debug registers needed to watch the given expression.
e09342b5 11273 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11274
c906108c 11275static int
a9634178 11276can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11277{
11278 int found_memory_cnt = 0;
2e70b7b9 11279 struct value *head = v;
c906108c
SS
11280
11281 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11282 if (!can_use_hw_watchpoints)
c906108c 11283 return 0;
c5aa993b 11284
5c44784c
JM
11285 /* Make sure that the value of the expression depends only upon
11286 memory contents, and values computed from them within GDB. If we
11287 find any register references or function calls, we can't use a
11288 hardware watchpoint.
11289
11290 The idea here is that evaluating an expression generates a series
11291 of values, one holding the value of every subexpression. (The
11292 expression a*b+c has five subexpressions: a, b, a*b, c, and
11293 a*b+c.) GDB's values hold almost enough information to establish
11294 the criteria given above --- they identify memory lvalues,
11295 register lvalues, computed values, etcetera. So we can evaluate
11296 the expression, and then scan the chain of values that leaves
11297 behind to decide whether we can detect any possible change to the
11298 expression's final value using only hardware watchpoints.
11299
11300 However, I don't think that the values returned by inferior
11301 function calls are special in any way. So this function may not
11302 notice that an expression involving an inferior function call
11303 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11304 for (; v; v = value_next (v))
c906108c 11305 {
5c44784c 11306 if (VALUE_LVAL (v) == lval_memory)
c906108c 11307 {
8464be76
DJ
11308 if (v != head && value_lazy (v))
11309 /* A lazy memory lvalue in the chain is one that GDB never
11310 needed to fetch; we either just used its address (e.g.,
11311 `a' in `a.b') or we never needed it at all (e.g., `a'
11312 in `a,b'). This doesn't apply to HEAD; if that is
11313 lazy then it was not readable, but watch it anyway. */
5c44784c 11314 ;
53a5351d 11315 else
5c44784c
JM
11316 {
11317 /* Ahh, memory we actually used! Check if we can cover
11318 it with hardware watchpoints. */
df407dfe 11319 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11320
11321 /* We only watch structs and arrays if user asked for it
11322 explicitly, never if they just happen to appear in a
11323 middle of some value chain. */
11324 if (v == head
11325 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11326 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11327 {
42ae5230 11328 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11329 int len;
11330 int num_regs;
11331
a9634178 11332 len = (target_exact_watchpoints
e09342b5
TJB
11333 && is_scalar_type_recursive (vtype))?
11334 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11335
e09342b5
TJB
11336 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11337 if (!num_regs)
2e70b7b9
MS
11338 return 0;
11339 else
e09342b5 11340 found_memory_cnt += num_regs;
2e70b7b9 11341 }
5c44784c 11342 }
c5aa993b 11343 }
5086187c
AC
11344 else if (VALUE_LVAL (v) != not_lval
11345 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11346 return 0; /* These are values from the history (e.g., $1). */
5086187c 11347 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11348 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11349 }
11350
11351 /* The expression itself looks suitable for using a hardware
11352 watchpoint, but give the target machine a chance to reject it. */
11353 return found_memory_cnt;
11354}
11355
8b93c638 11356void
84f4c1fe 11357watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11358{
84f4c1fe 11359 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11360}
11361
06a64a0b
TT
11362/* A helper function that looks for the "-location" argument and then
11363 calls watch_command_1. */
11364
11365static void
11366watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11367{
11368 int just_location = 0;
11369
11370 if (arg
11371 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11372 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11373 {
e9cafbcc 11374 arg = skip_spaces (arg);
06a64a0b
TT
11375 just_location = 1;
11376 }
11377
84f4c1fe 11378 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11379}
8926118c 11380
c5aa993b 11381static void
fba45db2 11382watch_command (char *arg, int from_tty)
c906108c 11383{
06a64a0b 11384 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11385}
11386
8b93c638 11387void
84f4c1fe 11388rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11389{
84f4c1fe 11390 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11391}
8926118c 11392
c5aa993b 11393static void
fba45db2 11394rwatch_command (char *arg, int from_tty)
c906108c 11395{
06a64a0b 11396 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11397}
11398
8b93c638 11399void
84f4c1fe 11400awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11401{
84f4c1fe 11402 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11403}
8926118c 11404
c5aa993b 11405static void
fba45db2 11406awatch_command (char *arg, int from_tty)
c906108c 11407{
06a64a0b 11408 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11409}
c906108c 11410\f
c5aa993b 11411
43ff13b4 11412/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11413 because it uses the mechanisms of breakpoints. */
11414
bfec99b2
PA
11415struct until_break_command_continuation_args
11416{
11417 struct breakpoint *breakpoint;
11418 struct breakpoint *breakpoint2;
186c406b 11419 int thread_num;
bfec99b2
PA
11420};
11421
43ff13b4 11422/* This function is called by fetch_inferior_event via the
4a64f543 11423 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11424 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11425 command. */
c2c6d25f 11426static void
fa4cd53f 11427until_break_command_continuation (void *arg, int err)
43ff13b4 11428{
bfec99b2
PA
11429 struct until_break_command_continuation_args *a = arg;
11430
11431 delete_breakpoint (a->breakpoint);
11432 if (a->breakpoint2)
11433 delete_breakpoint (a->breakpoint2);
186c406b 11434 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11435}
11436
c906108c 11437void
ae66c1fc 11438until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11439{
11440 struct symtabs_and_lines sals;
11441 struct symtab_and_line sal;
8556afb4
PA
11442 struct frame_info *frame;
11443 struct gdbarch *frame_gdbarch;
11444 struct frame_id stack_frame_id;
11445 struct frame_id caller_frame_id;
c906108c 11446 struct breakpoint *breakpoint;
f107f563 11447 struct breakpoint *breakpoint2 = NULL;
c906108c 11448 struct cleanup *old_chain;
186c406b
TT
11449 int thread;
11450 struct thread_info *tp;
c906108c 11451
70509625 11452 clear_proceed_status (0);
c906108c
SS
11453
11454 /* Set a breakpoint where the user wants it and at return from
4a64f543 11455 this function. */
c5aa993b 11456
1bfeeb0f 11457 if (last_displayed_sal_is_valid ())
f8eba3c6 11458 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11459 get_last_displayed_symtab (),
f8eba3c6 11460 get_last_displayed_line ());
c906108c 11461 else
f8eba3c6
TT
11462 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11463 (struct symtab *) NULL, 0);
c5aa993b 11464
c906108c 11465 if (sals.nelts != 1)
8a3fe4f8 11466 error (_("Couldn't get information on specified line."));
c5aa993b 11467
c906108c 11468 sal = sals.sals[0];
4a64f543 11469 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11470
c906108c 11471 if (*arg)
8a3fe4f8 11472 error (_("Junk at end of arguments."));
c5aa993b 11473
c906108c 11474 resolve_sal_pc (&sal);
c5aa993b 11475
186c406b
TT
11476 tp = inferior_thread ();
11477 thread = tp->num;
11478
883bc8d1
PA
11479 old_chain = make_cleanup (null_cleanup, NULL);
11480
8556afb4
PA
11481 /* Note linespec handling above invalidates the frame chain.
11482 Installing a breakpoint also invalidates the frame chain (as it
11483 may need to switch threads), so do any frame handling before
11484 that. */
11485
11486 frame = get_selected_frame (NULL);
11487 frame_gdbarch = get_frame_arch (frame);
11488 stack_frame_id = get_stack_frame_id (frame);
11489 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11490
ae66c1fc
EZ
11491 /* Keep within the current frame, or in frames called by the current
11492 one. */
edb3359d 11493
883bc8d1 11494 if (frame_id_p (caller_frame_id))
c906108c 11495 {
883bc8d1
PA
11496 struct symtab_and_line sal2;
11497
11498 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11499 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11500 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11501 sal2,
11502 caller_frame_id,
f107f563
VP
11503 bp_until);
11504 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11505
883bc8d1 11506 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11507 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11508 }
c5aa993b 11509
c70a6932
JK
11510 /* set_momentary_breakpoint could invalidate FRAME. */
11511 frame = NULL;
11512
883bc8d1
PA
11513 if (anywhere)
11514 /* If the user told us to continue until a specified location,
11515 we don't specify a frame at which we need to stop. */
11516 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11517 null_frame_id, bp_until);
11518 else
11519 /* Otherwise, specify the selected frame, because we want to stop
11520 only at the very same frame. */
11521 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11522 stack_frame_id, bp_until);
11523 make_cleanup_delete_breakpoint (breakpoint);
11524
64ce06e4 11525 proceed (-1, GDB_SIGNAL_DEFAULT);
f107f563 11526
4a64f543
MS
11527 /* If we are running asynchronously, and proceed call above has
11528 actually managed to start the target, arrange for breakpoints to
11529 be deleted when the target stops. Otherwise, we're already
11530 stopped and delete breakpoints via cleanup chain. */
f107f563 11531
8ea051c5 11532 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11533 {
bfec99b2
PA
11534 struct until_break_command_continuation_args *args;
11535 args = xmalloc (sizeof (*args));
f107f563 11536
bfec99b2
PA
11537 args->breakpoint = breakpoint;
11538 args->breakpoint2 = breakpoint2;
186c406b 11539 args->thread_num = thread;
f107f563
VP
11540
11541 discard_cleanups (old_chain);
95e54da7
PA
11542 add_continuation (inferior_thread (),
11543 until_break_command_continuation, args,
604ead4a 11544 xfree);
f107f563
VP
11545 }
11546 else
c5aa993b 11547 do_cleanups (old_chain);
c906108c 11548}
ae66c1fc 11549
c906108c
SS
11550/* This function attempts to parse an optional "if <cond>" clause
11551 from the arg string. If one is not found, it returns NULL.
c5aa993b 11552
c906108c
SS
11553 Else, it returns a pointer to the condition string. (It does not
11554 attempt to evaluate the string against a particular block.) And,
11555 it updates arg to point to the first character following the parsed
4a64f543 11556 if clause in the arg string. */
53a5351d 11557
916703c0 11558char *
fba45db2 11559ep_parse_optional_if_clause (char **arg)
c906108c 11560{
c5aa993b
JM
11561 char *cond_string;
11562
11563 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11564 return NULL;
c5aa993b 11565
4a64f543 11566 /* Skip the "if" keyword. */
c906108c 11567 (*arg) += 2;
c5aa993b 11568
c906108c 11569 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11570 condition string. */
e9cafbcc 11571 *arg = skip_spaces (*arg);
c906108c 11572 cond_string = *arg;
c5aa993b 11573
4a64f543
MS
11574 /* Assume that the condition occupies the remainder of the arg
11575 string. */
c906108c 11576 (*arg) += strlen (cond_string);
c5aa993b 11577
c906108c
SS
11578 return cond_string;
11579}
c5aa993b 11580
c906108c
SS
11581/* Commands to deal with catching events, such as signals, exceptions,
11582 process start/exit, etc. */
c5aa993b
JM
11583
11584typedef enum
11585{
44feb3ce
TT
11586 catch_fork_temporary, catch_vfork_temporary,
11587 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11588}
11589catch_fork_kind;
11590
c906108c 11591static void
cc59ec59
MS
11592catch_fork_command_1 (char *arg, int from_tty,
11593 struct cmd_list_element *command)
c906108c 11594{
a6d9a66e 11595 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11596 char *cond_string = NULL;
44feb3ce
TT
11597 catch_fork_kind fork_kind;
11598 int tempflag;
11599
11600 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11601 tempflag = (fork_kind == catch_fork_temporary
11602 || fork_kind == catch_vfork_temporary);
c5aa993b 11603
44feb3ce
TT
11604 if (!arg)
11605 arg = "";
e9cafbcc 11606 arg = skip_spaces (arg);
c5aa993b 11607
c906108c 11608 /* The allowed syntax is:
c5aa993b
JM
11609 catch [v]fork
11610 catch [v]fork if <cond>
11611
4a64f543 11612 First, check if there's an if clause. */
c906108c 11613 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11614
c906108c 11615 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11616 error (_("Junk at end of arguments."));
c5aa993b 11617
c906108c 11618 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11619 and enable reporting of such events. */
c5aa993b
JM
11620 switch (fork_kind)
11621 {
44feb3ce
TT
11622 case catch_fork_temporary:
11623 case catch_fork_permanent:
a6d9a66e 11624 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11625 &catch_fork_breakpoint_ops);
c906108c 11626 break;
44feb3ce
TT
11627 case catch_vfork_temporary:
11628 case catch_vfork_permanent:
a6d9a66e 11629 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11630 &catch_vfork_breakpoint_ops);
c906108c 11631 break;
c5aa993b 11632 default:
8a3fe4f8 11633 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11634 break;
c5aa993b 11635 }
c906108c
SS
11636}
11637
11638static void
cc59ec59
MS
11639catch_exec_command_1 (char *arg, int from_tty,
11640 struct cmd_list_element *command)
c906108c 11641{
b4d90040 11642 struct exec_catchpoint *c;
a6d9a66e 11643 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11644 int tempflag;
c5aa993b 11645 char *cond_string = NULL;
c906108c 11646
44feb3ce
TT
11647 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11648
11649 if (!arg)
11650 arg = "";
e9cafbcc 11651 arg = skip_spaces (arg);
c906108c
SS
11652
11653 /* The allowed syntax is:
c5aa993b
JM
11654 catch exec
11655 catch exec if <cond>
c906108c 11656
4a64f543 11657 First, check if there's an if clause. */
c906108c
SS
11658 cond_string = ep_parse_optional_if_clause (&arg);
11659
11660 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11661 error (_("Junk at end of arguments."));
c906108c 11662
b4d90040
PA
11663 c = XNEW (struct exec_catchpoint);
11664 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11665 &catch_exec_breakpoint_ops);
11666 c->exec_pathname = NULL;
11667
3ea46bff 11668 install_breakpoint (0, &c->base, 1);
c906108c 11669}
c5aa993b 11670
9ac4176b 11671void
28010a5d
PA
11672init_ada_exception_breakpoint (struct breakpoint *b,
11673 struct gdbarch *gdbarch,
11674 struct symtab_and_line sal,
11675 char *addr_string,
c0a91b2b 11676 const struct breakpoint_ops *ops,
28010a5d 11677 int tempflag,
349774ef 11678 int enabled,
28010a5d 11679 int from_tty)
f7f9143b 11680{
f7f9143b
JB
11681 if (from_tty)
11682 {
5af949e3
UW
11683 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11684 if (!loc_gdbarch)
11685 loc_gdbarch = gdbarch;
11686
6c95b8df
PA
11687 describe_other_breakpoints (loc_gdbarch,
11688 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11689 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11690 version for exception catchpoints, because two catchpoints
11691 used for different exception names will use the same address.
11692 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11693 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11694 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11695 the user what type of catchpoint it is. The above is good
11696 enough for now, though. */
11697 }
11698
28010a5d 11699 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11700
349774ef 11701 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11702 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
11703 b->addr_string = addr_string;
11704 b->language = language_ada;
f7f9143b
JB
11705}
11706
c906108c 11707static void
fba45db2 11708catch_command (char *arg, int from_tty)
c906108c 11709{
44feb3ce 11710 error (_("Catch requires an event name."));
c906108c
SS
11711}
11712\f
11713
11714static void
fba45db2 11715tcatch_command (char *arg, int from_tty)
c906108c 11716{
44feb3ce 11717 error (_("Catch requires an event name."));
c906108c
SS
11718}
11719
8a2c437b
TT
11720/* A qsort comparison function that sorts breakpoints in order. */
11721
11722static int
11723compare_breakpoints (const void *a, const void *b)
11724{
11725 const breakpoint_p *ba = a;
11726 uintptr_t ua = (uintptr_t) *ba;
11727 const breakpoint_p *bb = b;
11728 uintptr_t ub = (uintptr_t) *bb;
11729
11730 if ((*ba)->number < (*bb)->number)
11731 return -1;
11732 else if ((*ba)->number > (*bb)->number)
11733 return 1;
11734
11735 /* Now sort by address, in case we see, e..g, two breakpoints with
11736 the number 0. */
11737 if (ua < ub)
11738 return -1;
94b0e70d 11739 return ua > ub ? 1 : 0;
8a2c437b
TT
11740}
11741
80f8a6eb 11742/* Delete breakpoints by address or line. */
c906108c
SS
11743
11744static void
fba45db2 11745clear_command (char *arg, int from_tty)
c906108c 11746{
8a2c437b 11747 struct breakpoint *b, *prev;
d6e956e5
VP
11748 VEC(breakpoint_p) *found = 0;
11749 int ix;
c906108c
SS
11750 int default_match;
11751 struct symtabs_and_lines sals;
11752 struct symtab_and_line sal;
c906108c 11753 int i;
8a2c437b 11754 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11755
11756 if (arg)
11757 {
39cf75f7
DE
11758 sals = decode_line_with_current_source (arg,
11759 (DECODE_LINE_FUNFIRSTLINE
11760 | DECODE_LINE_LIST_MODE));
cf4ded82 11761 make_cleanup (xfree, sals.sals);
c906108c
SS
11762 default_match = 0;
11763 }
11764 else
11765 {
c5aa993b 11766 sals.sals = (struct symtab_and_line *)
c906108c 11767 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 11768 make_cleanup (xfree, sals.sals);
4a64f543 11769 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11770
11771 /* Set sal's line, symtab, pc, and pspace to the values
11772 corresponding to the last call to print_frame_info. If the
11773 codepoint is not valid, this will set all the fields to 0. */
11774 get_last_displayed_sal (&sal);
c906108c 11775 if (sal.symtab == 0)
8a3fe4f8 11776 error (_("No source file specified."));
c906108c
SS
11777
11778 sals.sals[0] = sal;
11779 sals.nelts = 1;
11780
11781 default_match = 1;
11782 }
11783
4a64f543
MS
11784 /* We don't call resolve_sal_pc here. That's not as bad as it
11785 seems, because all existing breakpoints typically have both
11786 file/line and pc set. So, if clear is given file/line, we can
11787 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11788
11789 We only support clearing given the address explicitly
11790 present in breakpoint table. Say, we've set breakpoint
4a64f543 11791 at file:line. There were several PC values for that file:line,
ed0616c6 11792 due to optimization, all in one block.
4a64f543
MS
11793
11794 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11795 PC corresponding to the same file:line, the breakpoint won't
11796 be cleared. We probably can still clear the breakpoint, but
11797 since the other PC value is never presented to user, user
11798 can only find it by guessing, and it does not seem important
11799 to support that. */
11800
4a64f543
MS
11801 /* For each line spec given, delete bps which correspond to it. Do
11802 it in two passes, solely to preserve the current behavior that
11803 from_tty is forced true if we delete more than one
11804 breakpoint. */
c906108c 11805
80f8a6eb 11806 found = NULL;
8a2c437b 11807 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11808 for (i = 0; i < sals.nelts; i++)
11809 {
05cba821
JK
11810 const char *sal_fullname;
11811
c906108c 11812 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11813 If line given (pc == 0), clear all bpts on specified line.
11814 If defaulting, clear all bpts on default line
c906108c 11815 or at default pc.
c5aa993b
JM
11816
11817 defaulting sal.pc != 0 tests to do
11818
11819 0 1 pc
11820 1 1 pc _and_ line
11821 0 0 line
11822 1 0 <can't happen> */
c906108c
SS
11823
11824 sal = sals.sals[i];
05cba821
JK
11825 sal_fullname = (sal.symtab == NULL
11826 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11827
4a64f543 11828 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11829 ALL_BREAKPOINTS (b)
c5aa993b 11830 {
0d381245 11831 int match = 0;
4a64f543 11832 /* Are we going to delete b? */
cc60f2e3 11833 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11834 {
11835 struct bp_location *loc = b->loc;
11836 for (; loc; loc = loc->next)
11837 {
f8eba3c6
TT
11838 /* If the user specified file:line, don't allow a PC
11839 match. This matches historical gdb behavior. */
11840 int pc_match = (!sal.explicit_line
11841 && sal.pc
11842 && (loc->pspace == sal.pspace)
11843 && (loc->address == sal.pc)
11844 && (!section_is_overlay (loc->section)
11845 || loc->section == sal.section));
4aac40c8
TT
11846 int line_match = 0;
11847
11848 if ((default_match || sal.explicit_line)
2f202fde 11849 && loc->symtab != NULL
05cba821 11850 && sal_fullname != NULL
4aac40c8 11851 && sal.pspace == loc->pspace
05cba821
JK
11852 && loc->line_number == sal.line
11853 && filename_cmp (symtab_to_fullname (loc->symtab),
11854 sal_fullname) == 0)
11855 line_match = 1;
4aac40c8 11856
0d381245
VP
11857 if (pc_match || line_match)
11858 {
11859 match = 1;
11860 break;
11861 }
11862 }
11863 }
11864
11865 if (match)
d6e956e5 11866 VEC_safe_push(breakpoint_p, found, b);
c906108c 11867 }
80f8a6eb 11868 }
8a2c437b 11869
80f8a6eb 11870 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11871 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11872 {
11873 if (arg)
8a3fe4f8 11874 error (_("No breakpoint at %s."), arg);
80f8a6eb 11875 else
8a3fe4f8 11876 error (_("No breakpoint at this line."));
80f8a6eb 11877 }
c906108c 11878
8a2c437b
TT
11879 /* Remove duplicates from the vec. */
11880 qsort (VEC_address (breakpoint_p, found),
11881 VEC_length (breakpoint_p, found),
11882 sizeof (breakpoint_p),
11883 compare_breakpoints);
11884 prev = VEC_index (breakpoint_p, found, 0);
11885 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11886 {
11887 if (b == prev)
11888 {
11889 VEC_ordered_remove (breakpoint_p, found, ix);
11890 --ix;
11891 }
11892 }
11893
d6e956e5 11894 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11895 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11896 if (from_tty)
a3f17187 11897 {
d6e956e5 11898 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11899 printf_unfiltered (_("Deleted breakpoint "));
11900 else
11901 printf_unfiltered (_("Deleted breakpoints "));
11902 }
d6e956e5
VP
11903
11904 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11905 {
c5aa993b 11906 if (from_tty)
d6e956e5
VP
11907 printf_unfiltered ("%d ", b->number);
11908 delete_breakpoint (b);
c906108c 11909 }
80f8a6eb
MS
11910 if (from_tty)
11911 putchar_unfiltered ('\n');
8a2c437b
TT
11912
11913 do_cleanups (cleanups);
c906108c
SS
11914}
11915\f
11916/* Delete breakpoint in BS if they are `delete' breakpoints and
11917 all breakpoints that are marked for deletion, whether hit or not.
11918 This is called after any breakpoint is hit, or after errors. */
11919
11920void
fba45db2 11921breakpoint_auto_delete (bpstat bs)
c906108c 11922{
35df4500 11923 struct breakpoint *b, *b_tmp;
c906108c
SS
11924
11925 for (; bs; bs = bs->next)
f431efe5
PA
11926 if (bs->breakpoint_at
11927 && bs->breakpoint_at->disposition == disp_del
c906108c 11928 && bs->stop)
f431efe5 11929 delete_breakpoint (bs->breakpoint_at);
c906108c 11930
35df4500 11931 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11932 {
b5de0fa7 11933 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11934 delete_breakpoint (b);
11935 }
c906108c
SS
11936}
11937
4a64f543
MS
11938/* A comparison function for bp_location AP and BP being interfaced to
11939 qsort. Sort elements primarily by their ADDRESS (no matter what
11940 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 11941 secondarily by ordering first permanent elements and
4a64f543 11942 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11943 qsort being an unstable algorithm. */
876fa593
JK
11944
11945static int
494cfb0f 11946bp_location_compare (const void *ap, const void *bp)
876fa593 11947{
494cfb0f
JK
11948 struct bp_location *a = *(void **) ap;
11949 struct bp_location *b = *(void **) bp;
876fa593
JK
11950
11951 if (a->address != b->address)
11952 return (a->address > b->address) - (a->address < b->address);
11953
dea2aa5f
LM
11954 /* Sort locations at the same address by their pspace number, keeping
11955 locations of the same inferior (in a multi-inferior environment)
11956 grouped. */
11957
11958 if (a->pspace->num != b->pspace->num)
11959 return ((a->pspace->num > b->pspace->num)
11960 - (a->pspace->num < b->pspace->num));
11961
876fa593 11962 /* Sort permanent breakpoints first. */
1a853c52
PA
11963 if (a->permanent != b->permanent)
11964 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 11965
c56a97f9
JK
11966 /* Make the internal GDB representation stable across GDB runs
11967 where A and B memory inside GDB can differ. Breakpoint locations of
11968 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11969
11970 if (a->owner->number != b->owner->number)
c56a97f9
JK
11971 return ((a->owner->number > b->owner->number)
11972 - (a->owner->number < b->owner->number));
876fa593
JK
11973
11974 return (a > b) - (a < b);
11975}
11976
876fa593 11977/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
11978 bp_location_shadow_len_after_address_max according to the current
11979 content of the bp_location array. */
f7545552
TT
11980
11981static void
876fa593 11982bp_location_target_extensions_update (void)
f7545552 11983{
876fa593
JK
11984 struct bp_location *bl, **blp_tmp;
11985
11986 bp_location_placed_address_before_address_max = 0;
11987 bp_location_shadow_len_after_address_max = 0;
11988
11989 ALL_BP_LOCATIONS (bl, blp_tmp)
11990 {
11991 CORE_ADDR start, end, addr;
11992
11993 if (!bp_location_has_shadow (bl))
11994 continue;
11995
11996 start = bl->target_info.placed_address;
11997 end = start + bl->target_info.shadow_len;
11998
11999 gdb_assert (bl->address >= start);
12000 addr = bl->address - start;
12001 if (addr > bp_location_placed_address_before_address_max)
12002 bp_location_placed_address_before_address_max = addr;
12003
12004 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12005
12006 gdb_assert (bl->address < end);
12007 addr = end - bl->address;
12008 if (addr > bp_location_shadow_len_after_address_max)
12009 bp_location_shadow_len_after_address_max = addr;
12010 }
f7545552
TT
12011}
12012
1e4d1764
YQ
12013/* Download tracepoint locations if they haven't been. */
12014
12015static void
12016download_tracepoint_locations (void)
12017{
7ed2c994 12018 struct breakpoint *b;
1e4d1764
YQ
12019 struct cleanup *old_chain;
12020
12021 if (!target_can_download_tracepoint ())
12022 return;
12023
12024 old_chain = save_current_space_and_thread ();
12025
7ed2c994 12026 ALL_TRACEPOINTS (b)
1e4d1764 12027 {
7ed2c994 12028 struct bp_location *bl;
1e4d1764 12029 struct tracepoint *t;
f2a8bc8a 12030 int bp_location_downloaded = 0;
1e4d1764 12031
7ed2c994 12032 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12033 ? !may_insert_fast_tracepoints
12034 : !may_insert_tracepoints))
12035 continue;
12036
7ed2c994
YQ
12037 for (bl = b->loc; bl; bl = bl->next)
12038 {
12039 /* In tracepoint, locations are _never_ duplicated, so
12040 should_be_inserted is equivalent to
12041 unduplicated_should_be_inserted. */
12042 if (!should_be_inserted (bl) || bl->inserted)
12043 continue;
1e4d1764 12044
7ed2c994 12045 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12046
7ed2c994 12047 target_download_tracepoint (bl);
1e4d1764 12048
7ed2c994 12049 bl->inserted = 1;
f2a8bc8a 12050 bp_location_downloaded = 1;
7ed2c994
YQ
12051 }
12052 t = (struct tracepoint *) b;
12053 t->number_on_target = b->number;
f2a8bc8a
YQ
12054 if (bp_location_downloaded)
12055 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12056 }
12057
12058 do_cleanups (old_chain);
12059}
12060
934709f0
PW
12061/* Swap the insertion/duplication state between two locations. */
12062
12063static void
12064swap_insertion (struct bp_location *left, struct bp_location *right)
12065{
12066 const int left_inserted = left->inserted;
12067 const int left_duplicate = left->duplicate;
b775012e 12068 const int left_needs_update = left->needs_update;
934709f0
PW
12069 const struct bp_target_info left_target_info = left->target_info;
12070
1e4d1764
YQ
12071 /* Locations of tracepoints can never be duplicated. */
12072 if (is_tracepoint (left->owner))
12073 gdb_assert (!left->duplicate);
12074 if (is_tracepoint (right->owner))
12075 gdb_assert (!right->duplicate);
12076
934709f0
PW
12077 left->inserted = right->inserted;
12078 left->duplicate = right->duplicate;
b775012e 12079 left->needs_update = right->needs_update;
934709f0
PW
12080 left->target_info = right->target_info;
12081 right->inserted = left_inserted;
12082 right->duplicate = left_duplicate;
b775012e 12083 right->needs_update = left_needs_update;
934709f0
PW
12084 right->target_info = left_target_info;
12085}
12086
b775012e
LM
12087/* Force the re-insertion of the locations at ADDRESS. This is called
12088 once a new/deleted/modified duplicate location is found and we are evaluating
12089 conditions on the target's side. Such conditions need to be updated on
12090 the target. */
12091
12092static void
12093force_breakpoint_reinsertion (struct bp_location *bl)
12094{
12095 struct bp_location **locp = NULL, **loc2p;
12096 struct bp_location *loc;
12097 CORE_ADDR address = 0;
12098 int pspace_num;
12099
12100 address = bl->address;
12101 pspace_num = bl->pspace->num;
12102
12103 /* This is only meaningful if the target is
12104 evaluating conditions and if the user has
12105 opted for condition evaluation on the target's
12106 side. */
12107 if (gdb_evaluates_breakpoint_condition_p ()
12108 || !target_supports_evaluation_of_breakpoint_conditions ())
12109 return;
12110
12111 /* Flag all breakpoint locations with this address and
12112 the same program space as the location
12113 as "its condition has changed". We need to
12114 update the conditions on the target's side. */
12115 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12116 {
12117 loc = *loc2p;
12118
12119 if (!is_breakpoint (loc->owner)
12120 || pspace_num != loc->pspace->num)
12121 continue;
12122
12123 /* Flag the location appropriately. We use a different state to
12124 let everyone know that we already updated the set of locations
12125 with addr bl->address and program space bl->pspace. This is so
12126 we don't have to keep calling these functions just to mark locations
12127 that have already been marked. */
12128 loc->condition_changed = condition_updated;
12129
12130 /* Free the agent expression bytecode as well. We will compute
12131 it later on. */
12132 if (loc->cond_bytecode)
12133 {
12134 free_agent_expr (loc->cond_bytecode);
12135 loc->cond_bytecode = NULL;
12136 }
12137 }
12138}
44702360
PA
12139/* Called whether new breakpoints are created, or existing breakpoints
12140 deleted, to update the global location list and recompute which
12141 locations are duplicate of which.
b775012e 12142
04086b45
PA
12143 The INSERT_MODE flag determines whether locations may not, may, or
12144 shall be inserted now. See 'enum ugll_insert_mode' for more
12145 info. */
b60e7edf 12146
0d381245 12147static void
44702360 12148update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12149{
74960c60 12150 struct breakpoint *b;
876fa593 12151 struct bp_location **locp, *loc;
f7545552 12152 struct cleanup *cleanups;
b775012e
LM
12153 /* Last breakpoint location address that was marked for update. */
12154 CORE_ADDR last_addr = 0;
12155 /* Last breakpoint location program space that was marked for update. */
12156 int last_pspace_num = -1;
f7545552 12157
2d134ed3
PA
12158 /* Used in the duplicates detection below. When iterating over all
12159 bp_locations, points to the first bp_location of a given address.
12160 Breakpoints and watchpoints of different types are never
12161 duplicates of each other. Keep one pointer for each type of
12162 breakpoint/watchpoint, so we only need to loop over all locations
12163 once. */
12164 struct bp_location *bp_loc_first; /* breakpoint */
12165 struct bp_location *wp_loc_first; /* hardware watchpoint */
12166 struct bp_location *awp_loc_first; /* access watchpoint */
12167 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12168
4a64f543
MS
12169 /* Saved former bp_location array which we compare against the newly
12170 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12171 struct bp_location **old_location, **old_locp;
12172 unsigned old_location_count;
12173
12174 old_location = bp_location;
12175 old_location_count = bp_location_count;
12176 bp_location = NULL;
12177 bp_location_count = 0;
12178 cleanups = make_cleanup (xfree, old_location);
0d381245 12179
74960c60 12180 ALL_BREAKPOINTS (b)
876fa593
JK
12181 for (loc = b->loc; loc; loc = loc->next)
12182 bp_location_count++;
12183
12184 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12185 locp = bp_location;
12186 ALL_BREAKPOINTS (b)
12187 for (loc = b->loc; loc; loc = loc->next)
12188 *locp++ = loc;
12189 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12190 bp_location_compare);
876fa593
JK
12191
12192 bp_location_target_extensions_update ();
74960c60 12193
4a64f543
MS
12194 /* Identify bp_location instances that are no longer present in the
12195 new list, and therefore should be freed. Note that it's not
12196 necessary that those locations should be removed from inferior --
12197 if there's another location at the same address (previously
12198 marked as duplicate), we don't need to remove/insert the
12199 location.
876fa593 12200
4a64f543
MS
12201 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12202 and former bp_location array state respectively. */
876fa593
JK
12203
12204 locp = bp_location;
12205 for (old_locp = old_location; old_locp < old_location + old_location_count;
12206 old_locp++)
74960c60 12207 {
876fa593 12208 struct bp_location *old_loc = *old_locp;
c7d46a38 12209 struct bp_location **loc2p;
876fa593 12210
e5dd4106 12211 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12212 not, we have to free it. */
c7d46a38 12213 int found_object = 0;
20874c92
VP
12214 /* Tells if the location should remain inserted in the target. */
12215 int keep_in_target = 0;
12216 int removed = 0;
876fa593 12217
4a64f543
MS
12218 /* Skip LOCP entries which will definitely never be needed.
12219 Stop either at or being the one matching OLD_LOC. */
876fa593 12220 while (locp < bp_location + bp_location_count
c7d46a38 12221 && (*locp)->address < old_loc->address)
876fa593 12222 locp++;
c7d46a38
PA
12223
12224 for (loc2p = locp;
12225 (loc2p < bp_location + bp_location_count
12226 && (*loc2p)->address == old_loc->address);
12227 loc2p++)
12228 {
b775012e
LM
12229 /* Check if this is a new/duplicated location or a duplicated
12230 location that had its condition modified. If so, we want to send
12231 its condition to the target if evaluation of conditions is taking
12232 place there. */
12233 if ((*loc2p)->condition_changed == condition_modified
12234 && (last_addr != old_loc->address
12235 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12236 {
b775012e
LM
12237 force_breakpoint_reinsertion (*loc2p);
12238 last_pspace_num = old_loc->pspace->num;
c7d46a38 12239 }
b775012e
LM
12240
12241 if (*loc2p == old_loc)
12242 found_object = 1;
c7d46a38 12243 }
74960c60 12244
b775012e
LM
12245 /* We have already handled this address, update it so that we don't
12246 have to go through updates again. */
12247 last_addr = old_loc->address;
12248
12249 /* Target-side condition evaluation: Handle deleted locations. */
12250 if (!found_object)
12251 force_breakpoint_reinsertion (old_loc);
12252
4a64f543
MS
12253 /* If this location is no longer present, and inserted, look if
12254 there's maybe a new location at the same address. If so,
12255 mark that one inserted, and don't remove this one. This is
12256 needed so that we don't have a time window where a breakpoint
12257 at certain location is not inserted. */
74960c60 12258
876fa593 12259 if (old_loc->inserted)
0d381245 12260 {
4a64f543
MS
12261 /* If the location is inserted now, we might have to remove
12262 it. */
74960c60 12263
876fa593 12264 if (found_object && should_be_inserted (old_loc))
74960c60 12265 {
4a64f543
MS
12266 /* The location is still present in the location list,
12267 and still should be inserted. Don't do anything. */
20874c92 12268 keep_in_target = 1;
74960c60
VP
12269 }
12270 else
12271 {
b775012e
LM
12272 /* This location still exists, but it won't be kept in the
12273 target since it may have been disabled. We proceed to
12274 remove its target-side condition. */
12275
4a64f543
MS
12276 /* The location is either no longer present, or got
12277 disabled. See if there's another location at the
12278 same address, in which case we don't need to remove
12279 this one from the target. */
876fa593 12280
2bdf28a0 12281 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12282 if (breakpoint_address_is_meaningful (old_loc->owner))
12283 {
876fa593 12284 for (loc2p = locp;
c7d46a38
PA
12285 (loc2p < bp_location + bp_location_count
12286 && (*loc2p)->address == old_loc->address);
876fa593
JK
12287 loc2p++)
12288 {
12289 struct bp_location *loc2 = *loc2p;
12290
2d134ed3 12291 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12292 {
85d721b8
PA
12293 /* Read watchpoint locations are switched to
12294 access watchpoints, if the former are not
12295 supported, but the latter are. */
12296 if (is_hardware_watchpoint (old_loc->owner))
12297 {
12298 gdb_assert (is_hardware_watchpoint (loc2->owner));
12299 loc2->watchpoint_type = old_loc->watchpoint_type;
12300 }
12301
934709f0
PW
12302 /* loc2 is a duplicated location. We need to check
12303 if it should be inserted in case it will be
12304 unduplicated. */
12305 if (loc2 != old_loc
12306 && unduplicated_should_be_inserted (loc2))
c7d46a38 12307 {
934709f0 12308 swap_insertion (old_loc, loc2);
c7d46a38
PA
12309 keep_in_target = 1;
12310 break;
12311 }
876fa593
JK
12312 }
12313 }
12314 }
74960c60
VP
12315 }
12316
20874c92
VP
12317 if (!keep_in_target)
12318 {
876fa593 12319 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12320 {
4a64f543
MS
12321 /* This is just about all we can do. We could keep
12322 this location on the global list, and try to
12323 remove it next time, but there's no particular
12324 reason why we will succeed next time.
20874c92 12325
4a64f543
MS
12326 Note that at this point, old_loc->owner is still
12327 valid, as delete_breakpoint frees the breakpoint
12328 only after calling us. */
3e43a32a
MS
12329 printf_filtered (_("warning: Error removing "
12330 "breakpoint %d\n"),
876fa593 12331 old_loc->owner->number);
20874c92
VP
12332 }
12333 removed = 1;
12334 }
0d381245 12335 }
74960c60
VP
12336
12337 if (!found_object)
1c5cfe86 12338 {
db82e815 12339 if (removed && non_stop
1cf4d951 12340 && need_moribund_for_location_type (old_loc))
20874c92 12341 {
db82e815
PA
12342 /* This location was removed from the target. In
12343 non-stop mode, a race condition is possible where
12344 we've removed a breakpoint, but stop events for that
12345 breakpoint are already queued and will arrive later.
12346 We apply an heuristic to be able to distinguish such
12347 SIGTRAPs from other random SIGTRAPs: we keep this
12348 breakpoint location for a bit, and will retire it
12349 after we see some number of events. The theory here
12350 is that reporting of events should, "on the average",
12351 be fair, so after a while we'll see events from all
12352 threads that have anything of interest, and no longer
12353 need to keep this breakpoint location around. We
12354 don't hold locations forever so to reduce chances of
12355 mistaking a non-breakpoint SIGTRAP for a breakpoint
12356 SIGTRAP.
12357
12358 The heuristic failing can be disastrous on
12359 decr_pc_after_break targets.
12360
12361 On decr_pc_after_break targets, like e.g., x86-linux,
12362 if we fail to recognize a late breakpoint SIGTRAP,
12363 because events_till_retirement has reached 0 too
12364 soon, we'll fail to do the PC adjustment, and report
12365 a random SIGTRAP to the user. When the user resumes
12366 the inferior, it will most likely immediately crash
2dec564e 12367 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12368 corrupted, because of being resumed e.g., in the
12369 middle of a multi-byte instruction, or skipped a
12370 one-byte instruction. This was actually seen happen
12371 on native x86-linux, and should be less rare on
12372 targets that do not support new thread events, like
12373 remote, due to the heuristic depending on
12374 thread_count.
12375
12376 Mistaking a random SIGTRAP for a breakpoint trap
12377 causes similar symptoms (PC adjustment applied when
12378 it shouldn't), but then again, playing with SIGTRAPs
12379 behind the debugger's back is asking for trouble.
12380
12381 Since hardware watchpoint traps are always
12382 distinguishable from other traps, so we don't need to
12383 apply keep hardware watchpoint moribund locations
12384 around. We simply always ignore hardware watchpoint
12385 traps we can no longer explain. */
12386
876fa593
JK
12387 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12388 old_loc->owner = NULL;
20874c92 12389
876fa593 12390 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12391 }
12392 else
f431efe5
PA
12393 {
12394 old_loc->owner = NULL;
12395 decref_bp_location (&old_loc);
12396 }
20874c92 12397 }
74960c60 12398 }
1c5cfe86 12399
348d480f
PA
12400 /* Rescan breakpoints at the same address and section, marking the
12401 first one as "first" and any others as "duplicates". This is so
12402 that the bpt instruction is only inserted once. If we have a
12403 permanent breakpoint at the same place as BPT, make that one the
12404 official one, and the rest as duplicates. Permanent breakpoints
12405 are sorted first for the same address.
12406
12407 Do the same for hardware watchpoints, but also considering the
12408 watchpoint's type (regular/access/read) and length. */
12409
12410 bp_loc_first = NULL;
12411 wp_loc_first = NULL;
12412 awp_loc_first = NULL;
12413 rwp_loc_first = NULL;
12414 ALL_BP_LOCATIONS (loc, locp)
12415 {
12416 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12417 non-NULL. */
348d480f 12418 struct bp_location **loc_first_p;
d3fbdd86 12419 b = loc->owner;
348d480f 12420
6f380991 12421 if (!unduplicated_should_be_inserted (loc)
348d480f 12422 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12423 /* Don't detect duplicate for tracepoint locations because they are
12424 never duplicated. See the comments in field `duplicate' of
12425 `struct bp_location'. */
348d480f 12426 || is_tracepoint (b))
b775012e
LM
12427 {
12428 /* Clear the condition modification flag. */
12429 loc->condition_changed = condition_unchanged;
12430 continue;
12431 }
348d480f
PA
12432
12433 /* Permanent breakpoint should always be inserted. */
1a853c52 12434 if (loc->permanent && ! loc->inserted)
348d480f
PA
12435 internal_error (__FILE__, __LINE__,
12436 _("allegedly permanent breakpoint is not "
12437 "actually inserted"));
12438
12439 if (b->type == bp_hardware_watchpoint)
12440 loc_first_p = &wp_loc_first;
12441 else if (b->type == bp_read_watchpoint)
12442 loc_first_p = &rwp_loc_first;
12443 else if (b->type == bp_access_watchpoint)
12444 loc_first_p = &awp_loc_first;
12445 else
12446 loc_first_p = &bp_loc_first;
12447
12448 if (*loc_first_p == NULL
12449 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12450 || !breakpoint_locations_match (loc, *loc_first_p))
12451 {
12452 *loc_first_p = loc;
12453 loc->duplicate = 0;
b775012e
LM
12454
12455 if (is_breakpoint (loc->owner) && loc->condition_changed)
12456 {
12457 loc->needs_update = 1;
12458 /* Clear the condition modification flag. */
12459 loc->condition_changed = condition_unchanged;
12460 }
348d480f
PA
12461 continue;
12462 }
12463
934709f0
PW
12464
12465 /* This and the above ensure the invariant that the first location
12466 is not duplicated, and is the inserted one.
12467 All following are marked as duplicated, and are not inserted. */
12468 if (loc->inserted)
12469 swap_insertion (loc, *loc_first_p);
348d480f
PA
12470 loc->duplicate = 1;
12471
b775012e
LM
12472 /* Clear the condition modification flag. */
12473 loc->condition_changed = condition_unchanged;
12474
1a853c52
PA
12475 if (loc->inserted && !loc->permanent
12476 && (*loc_first_p)->permanent)
348d480f
PA
12477 internal_error (__FILE__, __LINE__,
12478 _("another breakpoint was inserted on top of "
12479 "a permanent breakpoint"));
12480 }
12481
a25a5a45 12482 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12483 {
04086b45 12484 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12485 insert_breakpoint_locations ();
12486 else
12487 {
44702360
PA
12488 /* Even though the caller told us to not insert new
12489 locations, we may still need to update conditions on the
12490 target's side of breakpoints that were already inserted
12491 if the target is evaluating breakpoint conditions. We
b775012e
LM
12492 only update conditions for locations that are marked
12493 "needs_update". */
12494 update_inserted_breakpoint_locations ();
12495 }
12496 }
348d480f 12497
04086b45 12498 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12499 download_tracepoint_locations ();
12500
348d480f
PA
12501 do_cleanups (cleanups);
12502}
12503
12504void
12505breakpoint_retire_moribund (void)
12506{
12507 struct bp_location *loc;
12508 int ix;
12509
12510 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12511 if (--(loc->events_till_retirement) == 0)
12512 {
12513 decref_bp_location (&loc);
12514 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12515 --ix;
12516 }
12517}
12518
12519static void
44702360 12520update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12521{
348d480f 12522
492d29ea
PA
12523 TRY
12524 {
12525 update_global_location_list (insert_mode);
12526 }
12527 CATCH (e, RETURN_MASK_ERROR)
12528 {
12529 }
12530 END_CATCH
348d480f
PA
12531}
12532
12533/* Clear BKP from a BPS. */
12534
12535static void
12536bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12537{
12538 bpstat bs;
12539
12540 for (bs = bps; bs; bs = bs->next)
12541 if (bs->breakpoint_at == bpt)
12542 {
12543 bs->breakpoint_at = NULL;
12544 bs->old_val = NULL;
12545 /* bs->commands will be freed later. */
12546 }
12547}
12548
12549/* Callback for iterate_over_threads. */
12550static int
12551bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12552{
12553 struct breakpoint *bpt = data;
12554
12555 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12556 return 0;
12557}
12558
12559/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12560 callbacks. */
12561
12562static void
12563say_where (struct breakpoint *b)
12564{
12565 struct value_print_options opts;
12566
12567 get_user_print_options (&opts);
12568
12569 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12570 single string. */
12571 if (b->loc == NULL)
12572 {
12573 printf_filtered (_(" (%s) pending."), b->addr_string);
12574 }
12575 else
12576 {
2f202fde 12577 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12578 {
12579 printf_filtered (" at ");
12580 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12581 gdb_stdout);
12582 }
2f202fde 12583 if (b->loc->symtab != NULL)
f8eba3c6
TT
12584 {
12585 /* If there is a single location, we can print the location
12586 more nicely. */
12587 if (b->loc->next == NULL)
12588 printf_filtered (": file %s, line %d.",
05cba821
JK
12589 symtab_to_filename_for_display (b->loc->symtab),
12590 b->loc->line_number);
f8eba3c6
TT
12591 else
12592 /* This is not ideal, but each location may have a
12593 different file name, and this at least reflects the
12594 real situation somewhat. */
12595 printf_filtered (": %s.", b->addr_string);
12596 }
348d480f
PA
12597
12598 if (b->loc->next)
12599 {
12600 struct bp_location *loc = b->loc;
12601 int n = 0;
12602 for (; loc; loc = loc->next)
12603 ++n;
12604 printf_filtered (" (%d locations)", n);
12605 }
12606 }
12607}
12608
348d480f
PA
12609/* Default bp_location_ops methods. */
12610
12611static void
12612bp_location_dtor (struct bp_location *self)
12613{
12614 xfree (self->cond);
b775012e
LM
12615 if (self->cond_bytecode)
12616 free_agent_expr (self->cond_bytecode);
348d480f 12617 xfree (self->function_name);
8b4f3082
PA
12618
12619 VEC_free (agent_expr_p, self->target_info.conditions);
12620 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12621}
12622
12623static const struct bp_location_ops bp_location_ops =
12624{
12625 bp_location_dtor
12626};
12627
2060206e
PA
12628/* Default breakpoint_ops methods all breakpoint_ops ultimately
12629 inherit from. */
348d480f 12630
2060206e
PA
12631static void
12632base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12633{
12634 decref_counted_command_line (&self->commands);
12635 xfree (self->cond_string);
fb81d016 12636 xfree (self->extra_string);
348d480f 12637 xfree (self->addr_string);
f8eba3c6 12638 xfree (self->filter);
348d480f 12639 xfree (self->addr_string_range_end);
348d480f
PA
12640}
12641
2060206e
PA
12642static struct bp_location *
12643base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12644{
12645 struct bp_location *loc;
12646
12647 loc = XNEW (struct bp_location);
12648 init_bp_location (loc, &bp_location_ops, self);
12649 return loc;
12650}
12651
2060206e
PA
12652static void
12653base_breakpoint_re_set (struct breakpoint *b)
12654{
12655 /* Nothing to re-set. */
12656}
12657
12658#define internal_error_pure_virtual_called() \
12659 gdb_assert_not_reached ("pure virtual function called")
12660
12661static int
12662base_breakpoint_insert_location (struct bp_location *bl)
12663{
12664 internal_error_pure_virtual_called ();
12665}
12666
12667static int
12668base_breakpoint_remove_location (struct bp_location *bl)
12669{
12670 internal_error_pure_virtual_called ();
12671}
12672
12673static int
12674base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12675 struct address_space *aspace,
09ac7c10
TT
12676 CORE_ADDR bp_addr,
12677 const struct target_waitstatus *ws)
2060206e
PA
12678{
12679 internal_error_pure_virtual_called ();
12680}
12681
12682static void
12683base_breakpoint_check_status (bpstat bs)
12684{
12685 /* Always stop. */
12686}
12687
12688/* A "works_in_software_mode" breakpoint_ops method that just internal
12689 errors. */
12690
12691static int
12692base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12693{
12694 internal_error_pure_virtual_called ();
12695}
12696
12697/* A "resources_needed" breakpoint_ops method that just internal
12698 errors. */
12699
12700static int
12701base_breakpoint_resources_needed (const struct bp_location *bl)
12702{
12703 internal_error_pure_virtual_called ();
12704}
12705
12706static enum print_stop_action
12707base_breakpoint_print_it (bpstat bs)
12708{
12709 internal_error_pure_virtual_called ();
12710}
12711
12712static void
12713base_breakpoint_print_one_detail (const struct breakpoint *self,
12714 struct ui_out *uiout)
12715{
12716 /* nothing */
12717}
12718
12719static void
12720base_breakpoint_print_mention (struct breakpoint *b)
12721{
12722 internal_error_pure_virtual_called ();
12723}
12724
12725static void
12726base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12727{
12728 internal_error_pure_virtual_called ();
12729}
12730
983af33b
SDJ
12731static void
12732base_breakpoint_create_sals_from_address (char **arg,
12733 struct linespec_result *canonical,
12734 enum bptype type_wanted,
12735 char *addr_start,
12736 char **copy_arg)
12737{
12738 internal_error_pure_virtual_called ();
12739}
12740
12741static void
12742base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12743 struct linespec_result *c,
983af33b 12744 char *cond_string,
e7e0cddf 12745 char *extra_string,
983af33b
SDJ
12746 enum bptype type_wanted,
12747 enum bpdisp disposition,
12748 int thread,
12749 int task, int ignore_count,
12750 const struct breakpoint_ops *o,
12751 int from_tty, int enabled,
44f238bb 12752 int internal, unsigned flags)
983af33b
SDJ
12753{
12754 internal_error_pure_virtual_called ();
12755}
12756
12757static void
12758base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12759 struct symtabs_and_lines *sals)
12760{
12761 internal_error_pure_virtual_called ();
12762}
12763
ab04a2af
TT
12764/* The default 'explains_signal' method. */
12765
47591c29 12766static int
427cd150 12767base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12768{
47591c29 12769 return 1;
ab04a2af
TT
12770}
12771
9d6e6e84
HZ
12772/* The default "after_condition_true" method. */
12773
12774static void
12775base_breakpoint_after_condition_true (struct bpstats *bs)
12776{
12777 /* Nothing to do. */
12778}
12779
ab04a2af 12780struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
12781{
12782 base_breakpoint_dtor,
12783 base_breakpoint_allocate_location,
12784 base_breakpoint_re_set,
12785 base_breakpoint_insert_location,
12786 base_breakpoint_remove_location,
12787 base_breakpoint_breakpoint_hit,
12788 base_breakpoint_check_status,
12789 base_breakpoint_resources_needed,
12790 base_breakpoint_works_in_software_mode,
12791 base_breakpoint_print_it,
12792 NULL,
12793 base_breakpoint_print_one_detail,
12794 base_breakpoint_print_mention,
983af33b
SDJ
12795 base_breakpoint_print_recreate,
12796 base_breakpoint_create_sals_from_address,
12797 base_breakpoint_create_breakpoints_sal,
12798 base_breakpoint_decode_linespec,
9d6e6e84
HZ
12799 base_breakpoint_explains_signal,
12800 base_breakpoint_after_condition_true,
2060206e
PA
12801};
12802
12803/* Default breakpoint_ops methods. */
12804
12805static void
348d480f
PA
12806bkpt_re_set (struct breakpoint *b)
12807{
06edf0c0
PA
12808 /* FIXME: is this still reachable? */
12809 if (b->addr_string == NULL)
12810 {
12811 /* Anything without a string can't be re-set. */
348d480f 12812 delete_breakpoint (b);
06edf0c0 12813 return;
348d480f 12814 }
06edf0c0
PA
12815
12816 breakpoint_re_set_default (b);
348d480f
PA
12817}
12818
2060206e 12819static int
348d480f
PA
12820bkpt_insert_location (struct bp_location *bl)
12821{
12822 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12823 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12824 else
7c16b83e 12825 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12826}
12827
2060206e 12828static int
348d480f
PA
12829bkpt_remove_location (struct bp_location *bl)
12830{
12831 if (bl->loc_type == bp_loc_hardware_breakpoint)
12832 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12833 else
7c16b83e 12834 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12835}
12836
2060206e 12837static int
348d480f 12838bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12839 struct address_space *aspace, CORE_ADDR bp_addr,
12840 const struct target_waitstatus *ws)
348d480f 12841{
09ac7c10 12842 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12843 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12844 return 0;
12845
348d480f
PA
12846 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12847 aspace, bp_addr))
12848 return 0;
12849
12850 if (overlay_debugging /* unmapped overlay section */
12851 && section_is_overlay (bl->section)
12852 && !section_is_mapped (bl->section))
12853 return 0;
12854
12855 return 1;
12856}
12857
cd1608cc
PA
12858static int
12859dprintf_breakpoint_hit (const struct bp_location *bl,
12860 struct address_space *aspace, CORE_ADDR bp_addr,
12861 const struct target_waitstatus *ws)
12862{
12863 if (dprintf_style == dprintf_style_agent
12864 && target_can_run_breakpoint_commands ())
12865 {
12866 /* An agent-style dprintf never causes a stop. If we see a trap
12867 for this address it must be for a breakpoint that happens to
12868 be set at the same address. */
12869 return 0;
12870 }
12871
12872 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12873}
12874
2060206e 12875static int
348d480f
PA
12876bkpt_resources_needed (const struct bp_location *bl)
12877{
12878 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12879
12880 return 1;
12881}
12882
2060206e 12883static enum print_stop_action
348d480f
PA
12884bkpt_print_it (bpstat bs)
12885{
348d480f
PA
12886 struct breakpoint *b;
12887 const struct bp_location *bl;
001c8c33 12888 int bp_temp;
79a45e25 12889 struct ui_out *uiout = current_uiout;
348d480f
PA
12890
12891 gdb_assert (bs->bp_location_at != NULL);
12892
12893 bl = bs->bp_location_at;
12894 b = bs->breakpoint_at;
12895
001c8c33
PA
12896 bp_temp = b->disposition == disp_del;
12897 if (bl->address != bl->requested_address)
12898 breakpoint_adjustment_warning (bl->requested_address,
12899 bl->address,
12900 b->number, 1);
12901 annotate_breakpoint (b->number);
12902 if (bp_temp)
12903 ui_out_text (uiout, "\nTemporary breakpoint ");
12904 else
12905 ui_out_text (uiout, "\nBreakpoint ");
12906 if (ui_out_is_mi_like_p (uiout))
348d480f 12907 {
001c8c33
PA
12908 ui_out_field_string (uiout, "reason",
12909 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12910 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 12911 }
001c8c33
PA
12912 ui_out_field_int (uiout, "bkptno", b->number);
12913 ui_out_text (uiout, ", ");
06edf0c0 12914
001c8c33 12915 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12916}
12917
2060206e 12918static void
06edf0c0
PA
12919bkpt_print_mention (struct breakpoint *b)
12920{
79a45e25 12921 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
12922 return;
12923
12924 switch (b->type)
12925 {
12926 case bp_breakpoint:
12927 case bp_gnu_ifunc_resolver:
12928 if (b->disposition == disp_del)
12929 printf_filtered (_("Temporary breakpoint"));
12930 else
12931 printf_filtered (_("Breakpoint"));
12932 printf_filtered (_(" %d"), b->number);
12933 if (b->type == bp_gnu_ifunc_resolver)
12934 printf_filtered (_(" at gnu-indirect-function resolver"));
12935 break;
12936 case bp_hardware_breakpoint:
12937 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12938 break;
e7e0cddf
SS
12939 case bp_dprintf:
12940 printf_filtered (_("Dprintf %d"), b->number);
12941 break;
06edf0c0
PA
12942 }
12943
12944 say_where (b);
12945}
12946
2060206e 12947static void
06edf0c0
PA
12948bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12949{
12950 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12951 fprintf_unfiltered (fp, "tbreak");
12952 else if (tp->type == bp_breakpoint)
12953 fprintf_unfiltered (fp, "break");
12954 else if (tp->type == bp_hardware_breakpoint
12955 && tp->disposition == disp_del)
12956 fprintf_unfiltered (fp, "thbreak");
12957 else if (tp->type == bp_hardware_breakpoint)
12958 fprintf_unfiltered (fp, "hbreak");
12959 else
12960 internal_error (__FILE__, __LINE__,
12961 _("unhandled breakpoint type %d"), (int) tp->type);
12962
2060206e 12963 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 12964 print_recreate_thread (tp, fp);
06edf0c0
PA
12965}
12966
983af33b
SDJ
12967static void
12968bkpt_create_sals_from_address (char **arg,
12969 struct linespec_result *canonical,
12970 enum bptype type_wanted,
12971 char *addr_start, char **copy_arg)
12972{
12973 create_sals_from_address_default (arg, canonical, type_wanted,
12974 addr_start, copy_arg);
12975}
12976
12977static void
12978bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12979 struct linespec_result *canonical,
983af33b 12980 char *cond_string,
e7e0cddf 12981 char *extra_string,
983af33b
SDJ
12982 enum bptype type_wanted,
12983 enum bpdisp disposition,
12984 int thread,
12985 int task, int ignore_count,
12986 const struct breakpoint_ops *ops,
12987 int from_tty, int enabled,
44f238bb 12988 int internal, unsigned flags)
983af33b 12989{
023fa29b 12990 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
12991 cond_string, extra_string,
12992 type_wanted,
983af33b
SDJ
12993 disposition, thread, task,
12994 ignore_count, ops, from_tty,
44f238bb 12995 enabled, internal, flags);
983af33b
SDJ
12996}
12997
12998static void
12999bkpt_decode_linespec (struct breakpoint *b, char **s,
13000 struct symtabs_and_lines *sals)
13001{
13002 decode_linespec_default (b, s, sals);
13003}
13004
06edf0c0
PA
13005/* Virtual table for internal breakpoints. */
13006
13007static void
13008internal_bkpt_re_set (struct breakpoint *b)
13009{
13010 switch (b->type)
13011 {
13012 /* Delete overlay event and longjmp master breakpoints; they
13013 will be reset later by breakpoint_re_set. */
13014 case bp_overlay_event:
13015 case bp_longjmp_master:
13016 case bp_std_terminate_master:
13017 case bp_exception_master:
13018 delete_breakpoint (b);
13019 break;
13020
13021 /* This breakpoint is special, it's set up when the inferior
13022 starts and we really don't want to touch it. */
13023 case bp_shlib_event:
13024
13025 /* Like bp_shlib_event, this breakpoint type is special. Once
13026 it is set up, we do not want to touch it. */
13027 case bp_thread_event:
13028 break;
13029 }
13030}
13031
13032static void
13033internal_bkpt_check_status (bpstat bs)
13034{
a9b3a50f
PA
13035 if (bs->breakpoint_at->type == bp_shlib_event)
13036 {
13037 /* If requested, stop when the dynamic linker notifies GDB of
13038 events. This allows the user to get control and place
13039 breakpoints in initializer routines for dynamically loaded
13040 objects (among other things). */
13041 bs->stop = stop_on_solib_events;
13042 bs->print = stop_on_solib_events;
13043 }
13044 else
13045 bs->stop = 0;
06edf0c0
PA
13046}
13047
13048static enum print_stop_action
13049internal_bkpt_print_it (bpstat bs)
13050{
06edf0c0 13051 struct breakpoint *b;
06edf0c0 13052
06edf0c0
PA
13053 b = bs->breakpoint_at;
13054
06edf0c0
PA
13055 switch (b->type)
13056 {
348d480f
PA
13057 case bp_shlib_event:
13058 /* Did we stop because the user set the stop_on_solib_events
13059 variable? (If so, we report this as a generic, "Stopped due
13060 to shlib event" message.) */
edcc5120 13061 print_solib_event (0);
348d480f
PA
13062 break;
13063
13064 case bp_thread_event:
13065 /* Not sure how we will get here.
13066 GDB should not stop for these breakpoints. */
13067 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13068 break;
13069
13070 case bp_overlay_event:
13071 /* By analogy with the thread event, GDB should not stop for these. */
13072 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13073 break;
13074
13075 case bp_longjmp_master:
13076 /* These should never be enabled. */
13077 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13078 break;
13079
13080 case bp_std_terminate_master:
13081 /* These should never be enabled. */
13082 printf_filtered (_("std::terminate Master Breakpoint: "
13083 "gdb should not stop!\n"));
348d480f
PA
13084 break;
13085
13086 case bp_exception_master:
13087 /* These should never be enabled. */
13088 printf_filtered (_("Exception Master Breakpoint: "
13089 "gdb should not stop!\n"));
06edf0c0
PA
13090 break;
13091 }
13092
001c8c33 13093 return PRINT_NOTHING;
06edf0c0
PA
13094}
13095
13096static void
13097internal_bkpt_print_mention (struct breakpoint *b)
13098{
13099 /* Nothing to mention. These breakpoints are internal. */
13100}
13101
06edf0c0
PA
13102/* Virtual table for momentary breakpoints */
13103
13104static void
13105momentary_bkpt_re_set (struct breakpoint *b)
13106{
13107 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13108 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13109 Otherwise these should have been blown away via the cleanup chain
13110 or by breakpoint_init_inferior when we rerun the executable. */
13111}
13112
13113static void
13114momentary_bkpt_check_status (bpstat bs)
13115{
13116 /* Nothing. The point of these breakpoints is causing a stop. */
13117}
13118
13119static enum print_stop_action
13120momentary_bkpt_print_it (bpstat bs)
13121{
79a45e25
PA
13122 struct ui_out *uiout = current_uiout;
13123
001c8c33 13124 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13125 {
001c8c33 13126 struct breakpoint *b = bs->breakpoint_at;
348d480f 13127
001c8c33
PA
13128 switch (b->type)
13129 {
13130 case bp_finish:
13131 ui_out_field_string
13132 (uiout, "reason",
13133 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13134 break;
348d480f 13135
001c8c33
PA
13136 case bp_until:
13137 ui_out_field_string
13138 (uiout, "reason",
13139 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13140 break;
13141 }
348d480f
PA
13142 }
13143
001c8c33 13144 return PRINT_UNKNOWN;
348d480f
PA
13145}
13146
06edf0c0
PA
13147static void
13148momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13149{
06edf0c0 13150 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13151}
13152
e2e4d78b
JK
13153/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13154
13155 It gets cleared already on the removal of the first one of such placed
13156 breakpoints. This is OK as they get all removed altogether. */
13157
13158static void
13159longjmp_bkpt_dtor (struct breakpoint *self)
13160{
13161 struct thread_info *tp = find_thread_id (self->thread);
13162
13163 if (tp)
13164 tp->initiating_frame = null_frame_id;
13165
13166 momentary_breakpoint_ops.dtor (self);
13167}
13168
55aa24fb
SDJ
13169/* Specific methods for probe breakpoints. */
13170
13171static int
13172bkpt_probe_insert_location (struct bp_location *bl)
13173{
13174 int v = bkpt_insert_location (bl);
13175
13176 if (v == 0)
13177 {
13178 /* The insertion was successful, now let's set the probe's semaphore
13179 if needed. */
0ea5cda8
SDJ
13180 if (bl->probe.probe->pops->set_semaphore != NULL)
13181 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13182 bl->probe.objfile,
13183 bl->gdbarch);
55aa24fb
SDJ
13184 }
13185
13186 return v;
13187}
13188
13189static int
13190bkpt_probe_remove_location (struct bp_location *bl)
13191{
13192 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13193 if (bl->probe.probe->pops->clear_semaphore != NULL)
13194 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13195 bl->probe.objfile,
13196 bl->gdbarch);
55aa24fb
SDJ
13197
13198 return bkpt_remove_location (bl);
13199}
13200
13201static void
13202bkpt_probe_create_sals_from_address (char **arg,
13203 struct linespec_result *canonical,
13204 enum bptype type_wanted,
13205 char *addr_start, char **copy_arg)
13206{
13207 struct linespec_sals lsal;
13208
13209 lsal.sals = parse_probes (arg, canonical);
13210
13211 *copy_arg = xstrdup (canonical->addr_string);
13212 lsal.canonical = xstrdup (*copy_arg);
13213
13214 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13215}
13216
13217static void
13218bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13219 struct symtabs_and_lines *sals)
13220{
13221 *sals = parse_probes (s, NULL);
13222 if (!sals->sals)
13223 error (_("probe not found"));
13224}
13225
348d480f 13226/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13227
348d480f
PA
13228static void
13229tracepoint_re_set (struct breakpoint *b)
13230{
13231 breakpoint_re_set_default (b);
13232}
876fa593 13233
348d480f
PA
13234static int
13235tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13236 struct address_space *aspace, CORE_ADDR bp_addr,
13237 const struct target_waitstatus *ws)
348d480f
PA
13238{
13239 /* By definition, the inferior does not report stops at
13240 tracepoints. */
13241 return 0;
74960c60
VP
13242}
13243
13244static void
348d480f
PA
13245tracepoint_print_one_detail (const struct breakpoint *self,
13246 struct ui_out *uiout)
74960c60 13247{
d9b3f62e
PA
13248 struct tracepoint *tp = (struct tracepoint *) self;
13249 if (tp->static_trace_marker_id)
348d480f
PA
13250 {
13251 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13252
348d480f
PA
13253 ui_out_text (uiout, "\tmarker id is ");
13254 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13255 tp->static_trace_marker_id);
348d480f
PA
13256 ui_out_text (uiout, "\n");
13257 }
0d381245
VP
13258}
13259
a474d7c2 13260static void
348d480f 13261tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13262{
79a45e25 13263 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13264 return;
cc59ec59 13265
348d480f
PA
13266 switch (b->type)
13267 {
13268 case bp_tracepoint:
13269 printf_filtered (_("Tracepoint"));
13270 printf_filtered (_(" %d"), b->number);
13271 break;
13272 case bp_fast_tracepoint:
13273 printf_filtered (_("Fast tracepoint"));
13274 printf_filtered (_(" %d"), b->number);
13275 break;
13276 case bp_static_tracepoint:
13277 printf_filtered (_("Static tracepoint"));
13278 printf_filtered (_(" %d"), b->number);
13279 break;
13280 default:
13281 internal_error (__FILE__, __LINE__,
13282 _("unhandled tracepoint type %d"), (int) b->type);
13283 }
13284
13285 say_where (b);
a474d7c2
PA
13286}
13287
348d480f 13288static void
d9b3f62e 13289tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13290{
d9b3f62e
PA
13291 struct tracepoint *tp = (struct tracepoint *) self;
13292
13293 if (self->type == bp_fast_tracepoint)
348d480f 13294 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13295 if (self->type == bp_static_tracepoint)
348d480f 13296 fprintf_unfiltered (fp, "strace");
d9b3f62e 13297 else if (self->type == bp_tracepoint)
348d480f
PA
13298 fprintf_unfiltered (fp, "trace");
13299 else
13300 internal_error (__FILE__, __LINE__,
d9b3f62e 13301 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13302
d9b3f62e
PA
13303 fprintf_unfiltered (fp, " %s", self->addr_string);
13304 print_recreate_thread (self, fp);
13305
13306 if (tp->pass_count)
13307 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13308}
13309
983af33b
SDJ
13310static void
13311tracepoint_create_sals_from_address (char **arg,
13312 struct linespec_result *canonical,
13313 enum bptype type_wanted,
13314 char *addr_start, char **copy_arg)
13315{
13316 create_sals_from_address_default (arg, canonical, type_wanted,
13317 addr_start, copy_arg);
13318}
13319
13320static void
13321tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13322 struct linespec_result *canonical,
983af33b 13323 char *cond_string,
e7e0cddf 13324 char *extra_string,
983af33b
SDJ
13325 enum bptype type_wanted,
13326 enum bpdisp disposition,
13327 int thread,
13328 int task, int ignore_count,
13329 const struct breakpoint_ops *ops,
13330 int from_tty, int enabled,
44f238bb 13331 int internal, unsigned flags)
983af33b 13332{
023fa29b 13333 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13334 cond_string, extra_string,
13335 type_wanted,
983af33b
SDJ
13336 disposition, thread, task,
13337 ignore_count, ops, from_tty,
44f238bb 13338 enabled, internal, flags);
983af33b
SDJ
13339}
13340
13341static void
13342tracepoint_decode_linespec (struct breakpoint *b, char **s,
13343 struct symtabs_and_lines *sals)
13344{
13345 decode_linespec_default (b, s, sals);
13346}
13347
2060206e 13348struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13349
55aa24fb
SDJ
13350/* The breakpoint_ops structure to be use on tracepoints placed in a
13351 static probe. */
13352
13353static void
13354tracepoint_probe_create_sals_from_address (char **arg,
13355 struct linespec_result *canonical,
13356 enum bptype type_wanted,
13357 char *addr_start, char **copy_arg)
13358{
13359 /* We use the same method for breakpoint on probes. */
13360 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13361 addr_start, copy_arg);
13362}
13363
13364static void
13365tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13366 struct symtabs_and_lines *sals)
13367{
13368 /* We use the same method for breakpoint on probes. */
13369 bkpt_probe_decode_linespec (b, s, sals);
13370}
13371
13372static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13373
5c2b4418
HZ
13374/* Dprintf breakpoint_ops methods. */
13375
13376static void
13377dprintf_re_set (struct breakpoint *b)
13378{
13379 breakpoint_re_set_default (b);
13380
13381 /* This breakpoint could have been pending, and be resolved now, and
13382 if so, we should now have the extra string. If we don't, the
13383 dprintf was malformed when created, but we couldn't tell because
13384 we can't extract the extra string until the location is
13385 resolved. */
13386 if (b->loc != NULL && b->extra_string == NULL)
13387 error (_("Format string required"));
13388
13389 /* 1 - connect to target 1, that can run breakpoint commands.
13390 2 - create a dprintf, which resolves fine.
13391 3 - disconnect from target 1
13392 4 - connect to target 2, that can NOT run breakpoint commands.
13393
13394 After steps #3/#4, you'll want the dprintf command list to
13395 be updated, because target 1 and 2 may well return different
13396 answers for target_can_run_breakpoint_commands().
13397 Given absence of finer grained resetting, we get to do
13398 it all the time. */
13399 if (b->extra_string != NULL)
13400 update_dprintf_command_list (b);
13401}
13402
2d9442cc
HZ
13403/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13404
13405static void
13406dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13407{
13408 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13409 tp->extra_string);
13410 print_recreate_thread (tp, fp);
13411}
13412
9d6e6e84
HZ
13413/* Implement the "after_condition_true" breakpoint_ops method for
13414 dprintf.
13415
13416 dprintf's are implemented with regular commands in their command
13417 list, but we run the commands here instead of before presenting the
13418 stop to the user, as dprintf's don't actually cause a stop. This
13419 also makes it so that the commands of multiple dprintfs at the same
13420 address are all handled. */
13421
13422static void
13423dprintf_after_condition_true (struct bpstats *bs)
13424{
13425 struct cleanup *old_chain;
13426 struct bpstats tmp_bs = { NULL };
13427 struct bpstats *tmp_bs_p = &tmp_bs;
13428
13429 /* dprintf's never cause a stop. This wasn't set in the
13430 check_status hook instead because that would make the dprintf's
13431 condition not be evaluated. */
13432 bs->stop = 0;
13433
13434 /* Run the command list here. Take ownership of it instead of
13435 copying. We never want these commands to run later in
13436 bpstat_do_actions, if a breakpoint that causes a stop happens to
13437 be set at same address as this dprintf, or even if running the
13438 commands here throws. */
13439 tmp_bs.commands = bs->commands;
13440 bs->commands = NULL;
13441 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13442
13443 bpstat_do_actions_1 (&tmp_bs_p);
13444
13445 /* 'tmp_bs.commands' will usually be NULL by now, but
13446 bpstat_do_actions_1 may return early without processing the whole
13447 list. */
13448 do_cleanups (old_chain);
13449}
13450
983af33b
SDJ
13451/* The breakpoint_ops structure to be used on static tracepoints with
13452 markers (`-m'). */
13453
13454static void
13455strace_marker_create_sals_from_address (char **arg,
13456 struct linespec_result *canonical,
13457 enum bptype type_wanted,
13458 char *addr_start, char **copy_arg)
13459{
13460 struct linespec_sals lsal;
13461
13462 lsal.sals = decode_static_tracepoint_spec (arg);
13463
13464 *copy_arg = savestring (addr_start, *arg - addr_start);
13465
13466 canonical->addr_string = xstrdup (*copy_arg);
13467 lsal.canonical = xstrdup (*copy_arg);
13468 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13469}
13470
13471static void
13472strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13473 struct linespec_result *canonical,
983af33b 13474 char *cond_string,
e7e0cddf 13475 char *extra_string,
983af33b
SDJ
13476 enum bptype type_wanted,
13477 enum bpdisp disposition,
13478 int thread,
13479 int task, int ignore_count,
13480 const struct breakpoint_ops *ops,
13481 int from_tty, int enabled,
44f238bb 13482 int internal, unsigned flags)
983af33b
SDJ
13483{
13484 int i;
52d361e1
YQ
13485 struct linespec_sals *lsal = VEC_index (linespec_sals,
13486 canonical->sals, 0);
983af33b
SDJ
13487
13488 /* If the user is creating a static tracepoint by marker id
13489 (strace -m MARKER_ID), then store the sals index, so that
13490 breakpoint_re_set can try to match up which of the newly
13491 found markers corresponds to this one, and, don't try to
13492 expand multiple locations for each sal, given than SALS
13493 already should contain all sals for MARKER_ID. */
13494
13495 for (i = 0; i < lsal->sals.nelts; ++i)
13496 {
13497 struct symtabs_and_lines expanded;
13498 struct tracepoint *tp;
13499 struct cleanup *old_chain;
13500 char *addr_string;
13501
13502 expanded.nelts = 1;
13503 expanded.sals = &lsal->sals.sals[i];
13504
13505 addr_string = xstrdup (canonical->addr_string);
13506 old_chain = make_cleanup (xfree, addr_string);
13507
13508 tp = XCNEW (struct tracepoint);
13509 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13510 addr_string, NULL,
e7e0cddf
SS
13511 cond_string, extra_string,
13512 type_wanted, disposition,
983af33b 13513 thread, task, ignore_count, ops,
44f238bb 13514 from_tty, enabled, internal, flags,
983af33b
SDJ
13515 canonical->special_display);
13516 /* Given that its possible to have multiple markers with
13517 the same string id, if the user is creating a static
13518 tracepoint by marker id ("strace -m MARKER_ID"), then
13519 store the sals index, so that breakpoint_re_set can
13520 try to match up which of the newly found markers
13521 corresponds to this one */
13522 tp->static_trace_marker_id_idx = i;
13523
13524 install_breakpoint (internal, &tp->base, 0);
13525
13526 discard_cleanups (old_chain);
13527 }
13528}
13529
13530static void
13531strace_marker_decode_linespec (struct breakpoint *b, char **s,
13532 struct symtabs_and_lines *sals)
13533{
13534 struct tracepoint *tp = (struct tracepoint *) b;
13535
13536 *sals = decode_static_tracepoint_spec (s);
13537 if (sals->nelts > tp->static_trace_marker_id_idx)
13538 {
13539 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13540 sals->nelts = 1;
13541 }
13542 else
13543 error (_("marker %s not found"), tp->static_trace_marker_id);
13544}
13545
13546static struct breakpoint_ops strace_marker_breakpoint_ops;
13547
13548static int
13549strace_marker_p (struct breakpoint *b)
13550{
13551 return b->ops == &strace_marker_breakpoint_ops;
13552}
13553
53a5351d 13554/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13555 structures. */
c906108c
SS
13556
13557void
fba45db2 13558delete_breakpoint (struct breakpoint *bpt)
c906108c 13559{
52f0bd74 13560 struct breakpoint *b;
c906108c 13561
8a3fe4f8 13562 gdb_assert (bpt != NULL);
c906108c 13563
4a64f543
MS
13564 /* Has this bp already been deleted? This can happen because
13565 multiple lists can hold pointers to bp's. bpstat lists are
13566 especial culprits.
13567
13568 One example of this happening is a watchpoint's scope bp. When
13569 the scope bp triggers, we notice that the watchpoint is out of
13570 scope, and delete it. We also delete its scope bp. But the
13571 scope bp is marked "auto-deleting", and is already on a bpstat.
13572 That bpstat is then checked for auto-deleting bp's, which are
13573 deleted.
13574
13575 A real solution to this problem might involve reference counts in
13576 bp's, and/or giving them pointers back to their referencing
13577 bpstat's, and teaching delete_breakpoint to only free a bp's
13578 storage when no more references were extent. A cheaper bandaid
13579 was chosen. */
c906108c
SS
13580 if (bpt->type == bp_none)
13581 return;
13582
4a64f543
MS
13583 /* At least avoid this stale reference until the reference counting
13584 of breakpoints gets resolved. */
d0fb5eae 13585 if (bpt->related_breakpoint != bpt)
e5a0a904 13586 {
d0fb5eae 13587 struct breakpoint *related;
3a5c3e22 13588 struct watchpoint *w;
d0fb5eae
JK
13589
13590 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13591 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13592 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13593 w = (struct watchpoint *) bpt;
13594 else
13595 w = NULL;
13596 if (w != NULL)
13597 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13598
13599 /* Unlink bpt from the bpt->related_breakpoint ring. */
13600 for (related = bpt; related->related_breakpoint != bpt;
13601 related = related->related_breakpoint);
13602 related->related_breakpoint = bpt->related_breakpoint;
13603 bpt->related_breakpoint = bpt;
e5a0a904
JK
13604 }
13605
a9634178
TJB
13606 /* watch_command_1 creates a watchpoint but only sets its number if
13607 update_watchpoint succeeds in creating its bp_locations. If there's
13608 a problem in that process, we'll be asked to delete the half-created
13609 watchpoint. In that case, don't announce the deletion. */
13610 if (bpt->number)
13611 observer_notify_breakpoint_deleted (bpt);
c906108c 13612
c906108c
SS
13613 if (breakpoint_chain == bpt)
13614 breakpoint_chain = bpt->next;
13615
c906108c
SS
13616 ALL_BREAKPOINTS (b)
13617 if (b->next == bpt)
c5aa993b
JM
13618 {
13619 b->next = bpt->next;
13620 break;
13621 }
c906108c 13622
f431efe5
PA
13623 /* Be sure no bpstat's are pointing at the breakpoint after it's
13624 been freed. */
13625 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13626 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13627 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13628 commands are associated with the bpstat; if we remove it here,
13629 then the later call to bpstat_do_actions (&stop_bpstat); in
13630 event-top.c won't do anything, and temporary breakpoints with
13631 commands won't work. */
13632
13633 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13634
4a64f543
MS
13635 /* Now that breakpoint is removed from breakpoint list, update the
13636 global location list. This will remove locations that used to
13637 belong to this breakpoint. Do this before freeing the breakpoint
13638 itself, since remove_breakpoint looks at location's owner. It
13639 might be better design to have location completely
13640 self-contained, but it's not the case now. */
44702360 13641 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13642
348d480f 13643 bpt->ops->dtor (bpt);
4a64f543
MS
13644 /* On the chance that someone will soon try again to delete this
13645 same bp, we mark it as deleted before freeing its storage. */
c906108c 13646 bpt->type = bp_none;
b8c9b27d 13647 xfree (bpt);
c906108c
SS
13648}
13649
4d6140d9
AC
13650static void
13651do_delete_breakpoint_cleanup (void *b)
13652{
13653 delete_breakpoint (b);
13654}
13655
13656struct cleanup *
13657make_cleanup_delete_breakpoint (struct breakpoint *b)
13658{
13659 return make_cleanup (do_delete_breakpoint_cleanup, b);
13660}
13661
51be5b68
PA
13662/* Iterator function to call a user-provided callback function once
13663 for each of B and its related breakpoints. */
13664
13665static void
13666iterate_over_related_breakpoints (struct breakpoint *b,
13667 void (*function) (struct breakpoint *,
13668 void *),
13669 void *data)
13670{
13671 struct breakpoint *related;
13672
13673 related = b;
13674 do
13675 {
13676 struct breakpoint *next;
13677
13678 /* FUNCTION may delete RELATED. */
13679 next = related->related_breakpoint;
13680
13681 if (next == related)
13682 {
13683 /* RELATED is the last ring entry. */
13684 function (related, data);
13685
13686 /* FUNCTION may have deleted it, so we'd never reach back to
13687 B. There's nothing left to do anyway, so just break
13688 out. */
13689 break;
13690 }
13691 else
13692 function (related, data);
13693
13694 related = next;
13695 }
13696 while (related != b);
13697}
95a42b64
TT
13698
13699static void
13700do_delete_breakpoint (struct breakpoint *b, void *ignore)
13701{
13702 delete_breakpoint (b);
13703}
13704
51be5b68
PA
13705/* A callback for map_breakpoint_numbers that calls
13706 delete_breakpoint. */
13707
13708static void
13709do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13710{
13711 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13712}
13713
c906108c 13714void
fba45db2 13715delete_command (char *arg, int from_tty)
c906108c 13716{
35df4500 13717 struct breakpoint *b, *b_tmp;
c906108c 13718
ea9365bb
TT
13719 dont_repeat ();
13720
c906108c
SS
13721 if (arg == 0)
13722 {
13723 int breaks_to_delete = 0;
13724
46c6471b
PA
13725 /* Delete all breakpoints if no argument. Do not delete
13726 internal breakpoints, these have to be deleted with an
13727 explicit breakpoint number argument. */
c5aa993b 13728 ALL_BREAKPOINTS (b)
46c6471b 13729 if (user_breakpoint_p (b))
973d738b
DJ
13730 {
13731 breaks_to_delete = 1;
13732 break;
13733 }
c906108c
SS
13734
13735 /* Ask user only if there are some breakpoints to delete. */
13736 if (!from_tty
e2e0b3e5 13737 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13738 {
35df4500 13739 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13740 if (user_breakpoint_p (b))
c5aa993b 13741 delete_breakpoint (b);
c906108c
SS
13742 }
13743 }
13744 else
51be5b68 13745 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13746}
13747
0d381245
VP
13748static int
13749all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 13750{
0d381245 13751 for (; loc; loc = loc->next)
8645ff69
UW
13752 if (!loc->shlib_disabled
13753 && !loc->pspace->executing_startup)
0d381245
VP
13754 return 0;
13755 return 1;
fe3f5fa8
VP
13756}
13757
776592bf
DE
13758/* Subroutine of update_breakpoint_locations to simplify it.
13759 Return non-zero if multiple fns in list LOC have the same name.
13760 Null names are ignored. */
13761
13762static int
13763ambiguous_names_p (struct bp_location *loc)
13764{
13765 struct bp_location *l;
13766 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13767 (int (*) (const void *,
13768 const void *)) streq,
776592bf
DE
13769 NULL, xcalloc, xfree);
13770
13771 for (l = loc; l != NULL; l = l->next)
13772 {
13773 const char **slot;
13774 const char *name = l->function_name;
13775
13776 /* Allow for some names to be NULL, ignore them. */
13777 if (name == NULL)
13778 continue;
13779
13780 slot = (const char **) htab_find_slot (htab, (const void *) name,
13781 INSERT);
4a64f543
MS
13782 /* NOTE: We can assume slot != NULL here because xcalloc never
13783 returns NULL. */
776592bf
DE
13784 if (*slot != NULL)
13785 {
13786 htab_delete (htab);
13787 return 1;
13788 }
13789 *slot = name;
13790 }
13791
13792 htab_delete (htab);
13793 return 0;
13794}
13795
0fb4aa4b
PA
13796/* When symbols change, it probably means the sources changed as well,
13797 and it might mean the static tracepoint markers are no longer at
13798 the same address or line numbers they used to be at last we
13799 checked. Losing your static tracepoints whenever you rebuild is
13800 undesirable. This function tries to resync/rematch gdb static
13801 tracepoints with the markers on the target, for static tracepoints
13802 that have not been set by marker id. Static tracepoint that have
13803 been set by marker id are reset by marker id in breakpoint_re_set.
13804 The heuristic is:
13805
13806 1) For a tracepoint set at a specific address, look for a marker at
13807 the old PC. If one is found there, assume to be the same marker.
13808 If the name / string id of the marker found is different from the
13809 previous known name, assume that means the user renamed the marker
13810 in the sources, and output a warning.
13811
13812 2) For a tracepoint set at a given line number, look for a marker
13813 at the new address of the old line number. If one is found there,
13814 assume to be the same marker. If the name / string id of the
13815 marker found is different from the previous known name, assume that
13816 means the user renamed the marker in the sources, and output a
13817 warning.
13818
13819 3) If a marker is no longer found at the same address or line, it
13820 may mean the marker no longer exists. But it may also just mean
13821 the code changed a bit. Maybe the user added a few lines of code
13822 that made the marker move up or down (in line number terms). Ask
13823 the target for info about the marker with the string id as we knew
13824 it. If found, update line number and address in the matching
13825 static tracepoint. This will get confused if there's more than one
13826 marker with the same ID (possible in UST, although unadvised
13827 precisely because it confuses tools). */
13828
13829static struct symtab_and_line
13830update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13831{
d9b3f62e 13832 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13833 struct static_tracepoint_marker marker;
13834 CORE_ADDR pc;
0fb4aa4b
PA
13835
13836 pc = sal.pc;
13837 if (sal.line)
13838 find_line_pc (sal.symtab, sal.line, &pc);
13839
13840 if (target_static_tracepoint_marker_at (pc, &marker))
13841 {
d9b3f62e 13842 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13843 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13844 b->number,
d9b3f62e 13845 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13846
d9b3f62e
PA
13847 xfree (tp->static_trace_marker_id);
13848 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13849 release_static_tracepoint_marker (&marker);
13850
13851 return sal;
13852 }
13853
13854 /* Old marker wasn't found on target at lineno. Try looking it up
13855 by string ID. */
13856 if (!sal.explicit_pc
13857 && sal.line != 0
13858 && sal.symtab != NULL
d9b3f62e 13859 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13860 {
13861 VEC(static_tracepoint_marker_p) *markers;
13862
13863 markers
d9b3f62e 13864 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13865
13866 if (!VEC_empty(static_tracepoint_marker_p, markers))
13867 {
80e1d417 13868 struct symtab_and_line sal2;
0fb4aa4b 13869 struct symbol *sym;
80e1d417 13870 struct static_tracepoint_marker *tpmarker;
79a45e25 13871 struct ui_out *uiout = current_uiout;
0fb4aa4b 13872
80e1d417 13873 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13874
d9b3f62e 13875 xfree (tp->static_trace_marker_id);
80e1d417 13876 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13877
13878 warning (_("marker for static tracepoint %d (%s) not "
13879 "found at previous line number"),
d9b3f62e 13880 b->number, tp->static_trace_marker_id);
0fb4aa4b 13881
80e1d417 13882 init_sal (&sal2);
0fb4aa4b 13883
80e1d417 13884 sal2.pc = tpmarker->address;
0fb4aa4b 13885
80e1d417
AS
13886 sal2 = find_pc_line (tpmarker->address, 0);
13887 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
13888 ui_out_text (uiout, "Now in ");
13889 if (sym)
13890 {
13891 ui_out_field_string (uiout, "func",
13892 SYMBOL_PRINT_NAME (sym));
13893 ui_out_text (uiout, " at ");
13894 }
05cba821
JK
13895 ui_out_field_string (uiout, "file",
13896 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
13897 ui_out_text (uiout, ":");
13898
13899 if (ui_out_is_mi_like_p (uiout))
13900 {
0b0865da 13901 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13902
f35a17b5 13903 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
13904 }
13905
80e1d417 13906 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
13907 ui_out_text (uiout, "\n");
13908
80e1d417 13909 b->loc->line_number = sal2.line;
2f202fde 13910 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
13911
13912 xfree (b->addr_string);
13913 b->addr_string = xstrprintf ("%s:%d",
05cba821 13914 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 13915 b->loc->line_number);
0fb4aa4b
PA
13916
13917 /* Might be nice to check if function changed, and warn if
13918 so. */
13919
80e1d417 13920 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13921 }
13922 }
13923 return sal;
13924}
13925
8d3788bd
VP
13926/* Returns 1 iff locations A and B are sufficiently same that
13927 we don't need to report breakpoint as changed. */
13928
13929static int
13930locations_are_equal (struct bp_location *a, struct bp_location *b)
13931{
13932 while (a && b)
13933 {
13934 if (a->address != b->address)
13935 return 0;
13936
13937 if (a->shlib_disabled != b->shlib_disabled)
13938 return 0;
13939
13940 if (a->enabled != b->enabled)
13941 return 0;
13942
13943 a = a->next;
13944 b = b->next;
13945 }
13946
13947 if ((a == NULL) != (b == NULL))
13948 return 0;
13949
13950 return 1;
13951}
13952
f1310107
TJB
13953/* Create new breakpoint locations for B (a hardware or software breakpoint)
13954 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
13955 a ranged breakpoint. */
13956
0e30163f 13957void
0d381245 13958update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
13959 struct symtabs_and_lines sals,
13960 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
13961{
13962 int i;
0d381245
VP
13963 struct bp_location *existing_locations = b->loc;
13964
f8eba3c6
TT
13965 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
13966 {
13967 /* Ranged breakpoints have only one start location and one end
13968 location. */
13969 b->enable_state = bp_disabled;
44702360 13970 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
13971 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13972 "multiple locations found\n"),
13973 b->number);
13974 return;
13975 }
f1310107 13976
4a64f543
MS
13977 /* If there's no new locations, and all existing locations are
13978 pending, don't do anything. This optimizes the common case where
13979 all locations are in the same shared library, that was unloaded.
13980 We'd like to retain the location, so that when the library is
13981 loaded again, we don't loose the enabled/disabled status of the
13982 individual locations. */
0d381245 13983 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
13984 return;
13985
fe3f5fa8
VP
13986 b->loc = NULL;
13987
0d381245 13988 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 13989 {
f8eba3c6
TT
13990 struct bp_location *new_loc;
13991
13992 switch_to_program_space_and_thread (sals.sals[i].pspace);
13993
13994 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 13995
0d381245
VP
13996 /* Reparse conditions, they might contain references to the
13997 old symtab. */
13998 if (b->cond_string != NULL)
13999 {
bbc13ae3 14000 const char *s;
fe3f5fa8 14001
0d381245 14002 s = b->cond_string;
492d29ea 14003 TRY
0d381245 14004 {
1bb9788d
TT
14005 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14006 block_for_pc (sals.sals[i].pc),
0d381245
VP
14007 0);
14008 }
492d29ea 14009 CATCH (e, RETURN_MASK_ERROR)
0d381245 14010 {
3e43a32a
MS
14011 warning (_("failed to reevaluate condition "
14012 "for breakpoint %d: %s"),
0d381245
VP
14013 b->number, e.message);
14014 new_loc->enabled = 0;
14015 }
492d29ea 14016 END_CATCH
0d381245 14017 }
fe3f5fa8 14018
f1310107
TJB
14019 if (sals_end.nelts)
14020 {
14021 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14022
14023 new_loc->length = end - sals.sals[0].pc + 1;
14024 }
0d381245 14025 }
fe3f5fa8 14026
4a64f543
MS
14027 /* If possible, carry over 'disable' status from existing
14028 breakpoints. */
0d381245
VP
14029 {
14030 struct bp_location *e = existing_locations;
776592bf
DE
14031 /* If there are multiple breakpoints with the same function name,
14032 e.g. for inline functions, comparing function names won't work.
14033 Instead compare pc addresses; this is just a heuristic as things
14034 may have moved, but in practice it gives the correct answer
14035 often enough until a better solution is found. */
14036 int have_ambiguous_names = ambiguous_names_p (b->loc);
14037
0d381245
VP
14038 for (; e; e = e->next)
14039 {
14040 if (!e->enabled && e->function_name)
14041 {
14042 struct bp_location *l = b->loc;
776592bf
DE
14043 if (have_ambiguous_names)
14044 {
14045 for (; l; l = l->next)
f1310107 14046 if (breakpoint_locations_match (e, l))
776592bf
DE
14047 {
14048 l->enabled = 0;
14049 break;
14050 }
14051 }
14052 else
14053 {
14054 for (; l; l = l->next)
14055 if (l->function_name
14056 && strcmp (e->function_name, l->function_name) == 0)
14057 {
14058 l->enabled = 0;
14059 break;
14060 }
14061 }
0d381245
VP
14062 }
14063 }
14064 }
fe3f5fa8 14065
8d3788bd
VP
14066 if (!locations_are_equal (existing_locations, b->loc))
14067 observer_notify_breakpoint_modified (b);
14068
44702360 14069 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14070}
14071
ef23e705
TJB
14072/* Find the SaL locations corresponding to the given ADDR_STRING.
14073 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14074
14075static struct symtabs_and_lines
14076addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14077{
14078 char *s;
02d20e4a 14079 struct symtabs_and_lines sals = {0};
492d29ea 14080 struct gdb_exception exception = exception_none;
ef23e705 14081
983af33b 14082 gdb_assert (b->ops != NULL);
ef23e705 14083 s = addr_string;
ef23e705 14084
492d29ea 14085 TRY
ef23e705 14086 {
983af33b 14087 b->ops->decode_linespec (b, &s, &sals);
ef23e705 14088 }
492d29ea 14089 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14090 {
14091 int not_found_and_ok = 0;
492d29ea
PA
14092
14093 exception = e;
14094
ef23e705
TJB
14095 /* For pending breakpoints, it's expected that parsing will
14096 fail until the right shared library is loaded. User has
14097 already told to create pending breakpoints and don't need
14098 extra messages. If breakpoint is in bp_shlib_disabled
14099 state, then user already saw the message about that
14100 breakpoint being disabled, and don't want to see more
14101 errors. */
58438ac1 14102 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14103 && (b->condition_not_parsed
14104 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14105 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14106 || b->enable_state == bp_disabled))
14107 not_found_and_ok = 1;
14108
14109 if (!not_found_and_ok)
14110 {
14111 /* We surely don't want to warn about the same breakpoint
14112 10 times. One solution, implemented here, is disable
14113 the breakpoint on error. Another solution would be to
14114 have separate 'warning emitted' flag. Since this
14115 happens only when a binary has changed, I don't know
14116 which approach is better. */
14117 b->enable_state = bp_disabled;
14118 throw_exception (e);
14119 }
14120 }
492d29ea 14121 END_CATCH
ef23e705 14122
492d29ea 14123 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14124 {
f8eba3c6 14125 int i;
ef23e705 14126
f8eba3c6
TT
14127 for (i = 0; i < sals.nelts; ++i)
14128 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14129 if (b->condition_not_parsed && s && s[0])
14130 {
ed1d1739
KS
14131 char *cond_string, *extra_string;
14132 int thread, task;
ef23e705
TJB
14133
14134 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14135 &cond_string, &thread, &task,
14136 &extra_string);
ef23e705
TJB
14137 if (cond_string)
14138 b->cond_string = cond_string;
14139 b->thread = thread;
14140 b->task = task;
e7e0cddf
SS
14141 if (extra_string)
14142 b->extra_string = extra_string;
ef23e705
TJB
14143 b->condition_not_parsed = 0;
14144 }
14145
983af33b 14146 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14147 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14148
58438ac1
TT
14149 *found = 1;
14150 }
14151 else
14152 *found = 0;
ef23e705
TJB
14153
14154 return sals;
14155}
14156
348d480f
PA
14157/* The default re_set method, for typical hardware or software
14158 breakpoints. Reevaluate the breakpoint and recreate its
14159 locations. */
14160
14161static void
28010a5d 14162breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14163{
14164 int found;
f1310107 14165 struct symtabs_and_lines sals, sals_end;
ef23e705 14166 struct symtabs_and_lines expanded = {0};
f1310107 14167 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14168
14169 sals = addr_string_to_sals (b, b->addr_string, &found);
14170 if (found)
14171 {
14172 make_cleanup (xfree, sals.sals);
f8eba3c6 14173 expanded = sals;
ef23e705
TJB
14174 }
14175
f1310107
TJB
14176 if (b->addr_string_range_end)
14177 {
14178 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14179 if (found)
14180 {
14181 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14182 expanded_end = sals_end;
f1310107
TJB
14183 }
14184 }
14185
14186 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14187}
14188
983af33b
SDJ
14189/* Default method for creating SALs from an address string. It basically
14190 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14191
14192static void
14193create_sals_from_address_default (char **arg,
14194 struct linespec_result *canonical,
14195 enum bptype type_wanted,
14196 char *addr_start, char **copy_arg)
14197{
14198 parse_breakpoint_sals (arg, canonical);
14199}
14200
14201/* Call create_breakpoints_sal for the given arguments. This is the default
14202 function for the `create_breakpoints_sal' method of
14203 breakpoint_ops. */
14204
14205static void
14206create_breakpoints_sal_default (struct gdbarch *gdbarch,
14207 struct linespec_result *canonical,
983af33b 14208 char *cond_string,
e7e0cddf 14209 char *extra_string,
983af33b
SDJ
14210 enum bptype type_wanted,
14211 enum bpdisp disposition,
14212 int thread,
14213 int task, int ignore_count,
14214 const struct breakpoint_ops *ops,
14215 int from_tty, int enabled,
44f238bb 14216 int internal, unsigned flags)
983af33b
SDJ
14217{
14218 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14219 extra_string,
983af33b
SDJ
14220 type_wanted, disposition,
14221 thread, task, ignore_count, ops, from_tty,
44f238bb 14222 enabled, internal, flags);
983af33b
SDJ
14223}
14224
14225/* Decode the line represented by S by calling decode_line_full. This is the
14226 default function for the `decode_linespec' method of breakpoint_ops. */
14227
14228static void
14229decode_linespec_default (struct breakpoint *b, char **s,
14230 struct symtabs_and_lines *sals)
14231{
14232 struct linespec_result canonical;
14233
14234 init_linespec_result (&canonical);
14235 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14236 (struct symtab *) NULL, 0,
14237 &canonical, multiple_symbols_all,
14238 b->filter);
14239
14240 /* We should get 0 or 1 resulting SALs. */
14241 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14242
14243 if (VEC_length (linespec_sals, canonical.sals) > 0)
14244 {
14245 struct linespec_sals *lsal;
14246
14247 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14248 *sals = lsal->sals;
14249 /* Arrange it so the destructor does not free the
14250 contents. */
14251 lsal->sals.sals = NULL;
14252 }
14253
14254 destroy_linespec_result (&canonical);
14255}
14256
28010a5d
PA
14257/* Prepare the global context for a re-set of breakpoint B. */
14258
14259static struct cleanup *
14260prepare_re_set_context (struct breakpoint *b)
14261{
14262 struct cleanup *cleanups;
14263
14264 input_radix = b->input_radix;
14265 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14266 if (b->pspace != NULL)
14267 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14268 set_language (b->language);
14269
14270 return cleanups;
ef23e705
TJB
14271}
14272
c906108c
SS
14273/* Reset a breakpoint given it's struct breakpoint * BINT.
14274 The value we return ends up being the return value from catch_errors.
14275 Unused in this case. */
14276
14277static int
4efb68b1 14278breakpoint_re_set_one (void *bint)
c906108c 14279{
4a64f543 14280 /* Get past catch_errs. */
53a5351d 14281 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14282 struct cleanup *cleanups;
c906108c 14283
348d480f
PA
14284 cleanups = prepare_re_set_context (b);
14285 b->ops->re_set (b);
14286 do_cleanups (cleanups);
c906108c
SS
14287 return 0;
14288}
14289
69de3c6a 14290/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14291void
69de3c6a 14292breakpoint_re_set (void)
c906108c 14293{
35df4500 14294 struct breakpoint *b, *b_tmp;
c906108c
SS
14295 enum language save_language;
14296 int save_input_radix;
6c95b8df 14297 struct cleanup *old_chain;
c5aa993b 14298
c906108c
SS
14299 save_language = current_language->la_language;
14300 save_input_radix = input_radix;
6c95b8df
PA
14301 old_chain = save_current_program_space ();
14302
35df4500 14303 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14304 {
4a64f543 14305 /* Format possible error msg. */
fe3f5fa8 14306 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14307 b->number);
14308 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14309 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14310 do_cleanups (cleanups);
c5aa993b 14311 }
c906108c
SS
14312 set_language (save_language);
14313 input_radix = save_input_radix;
e62c965a 14314
0756c555 14315 jit_breakpoint_re_set ();
4efc6507 14316
6c95b8df
PA
14317 do_cleanups (old_chain);
14318
af02033e
PP
14319 create_overlay_event_breakpoint ();
14320 create_longjmp_master_breakpoint ();
14321 create_std_terminate_master_breakpoint ();
186c406b 14322 create_exception_master_breakpoint ();
c906108c
SS
14323}
14324\f
c906108c
SS
14325/* Reset the thread number of this breakpoint:
14326
14327 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14328 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14329void
fba45db2 14330breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14331{
14332 if (b->thread != -1)
14333 {
39f77062
KB
14334 if (in_thread_list (inferior_ptid))
14335 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14336
14337 /* We're being called after following a fork. The new fork is
14338 selected as current, and unless this was a vfork will have a
14339 different program space from the original thread. Reset that
14340 as well. */
14341 b->loc->pspace = current_program_space;
c906108c
SS
14342 }
14343}
14344
03ac34d5
MS
14345/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14346 If from_tty is nonzero, it prints a message to that effect,
14347 which ends with a period (no newline). */
14348
c906108c 14349void
fba45db2 14350set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14351{
52f0bd74 14352 struct breakpoint *b;
c906108c
SS
14353
14354 if (count < 0)
14355 count = 0;
14356
14357 ALL_BREAKPOINTS (b)
14358 if (b->number == bptnum)
c5aa993b 14359 {
d77f58be
SS
14360 if (is_tracepoint (b))
14361 {
14362 if (from_tty && count != 0)
14363 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14364 bptnum);
14365 return;
14366 }
14367
c5aa993b 14368 b->ignore_count = count;
221ea385
KS
14369 if (from_tty)
14370 {
14371 if (count == 0)
3e43a32a
MS
14372 printf_filtered (_("Will stop next time "
14373 "breakpoint %d is reached."),
221ea385
KS
14374 bptnum);
14375 else if (count == 1)
a3f17187 14376 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14377 bptnum);
14378 else
3e43a32a
MS
14379 printf_filtered (_("Will ignore next %d "
14380 "crossings of breakpoint %d."),
221ea385
KS
14381 count, bptnum);
14382 }
8d3788bd 14383 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14384 return;
14385 }
c906108c 14386
8a3fe4f8 14387 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14388}
14389
c906108c
SS
14390/* Command to set ignore-count of breakpoint N to COUNT. */
14391
14392static void
fba45db2 14393ignore_command (char *args, int from_tty)
c906108c
SS
14394{
14395 char *p = args;
52f0bd74 14396 int num;
c906108c
SS
14397
14398 if (p == 0)
e2e0b3e5 14399 error_no_arg (_("a breakpoint number"));
c5aa993b 14400
c906108c 14401 num = get_number (&p);
5c44784c 14402 if (num == 0)
8a3fe4f8 14403 error (_("bad breakpoint number: '%s'"), args);
c906108c 14404 if (*p == 0)
8a3fe4f8 14405 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14406
14407 set_ignore_count (num,
14408 longest_to_int (value_as_long (parse_and_eval (p))),
14409 from_tty);
221ea385
KS
14410 if (from_tty)
14411 printf_filtered ("\n");
c906108c
SS
14412}
14413\f
14414/* Call FUNCTION on each of the breakpoints
14415 whose numbers are given in ARGS. */
14416
14417static void
95a42b64
TT
14418map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14419 void *),
14420 void *data)
c906108c 14421{
52f0bd74
AC
14422 int num;
14423 struct breakpoint *b, *tmp;
11cf8741 14424 int match;
197f0a60 14425 struct get_number_or_range_state state;
c906108c 14426
b9d61307 14427 if (args == 0 || *args == '\0')
e2e0b3e5 14428 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14429
197f0a60
TT
14430 init_number_or_range (&state, args);
14431
14432 while (!state.finished)
c906108c 14433 {
e799154c 14434 const char *p = state.string;
197f0a60 14435
11cf8741 14436 match = 0;
c5aa993b 14437
197f0a60 14438 num = get_number_or_range (&state);
5c44784c 14439 if (num == 0)
c5aa993b 14440 {
8a3fe4f8 14441 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14442 }
14443 else
14444 {
14445 ALL_BREAKPOINTS_SAFE (b, tmp)
14446 if (b->number == num)
14447 {
11cf8741 14448 match = 1;
cdac0397 14449 function (b, data);
11cf8741 14450 break;
5c44784c 14451 }
11cf8741 14452 if (match == 0)
a3f17187 14453 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14454 }
c906108c
SS
14455 }
14456}
14457
0d381245
VP
14458static struct bp_location *
14459find_location_by_number (char *number)
14460{
14461 char *dot = strchr (number, '.');
14462 char *p1;
14463 int bp_num;
14464 int loc_num;
14465 struct breakpoint *b;
14466 struct bp_location *loc;
14467
14468 *dot = '\0';
14469
14470 p1 = number;
197f0a60 14471 bp_num = get_number (&p1);
0d381245
VP
14472 if (bp_num == 0)
14473 error (_("Bad breakpoint number '%s'"), number);
14474
14475 ALL_BREAKPOINTS (b)
14476 if (b->number == bp_num)
14477 {
14478 break;
14479 }
14480
14481 if (!b || b->number != bp_num)
14482 error (_("Bad breakpoint number '%s'"), number);
14483
14484 p1 = dot+1;
197f0a60 14485 loc_num = get_number (&p1);
0d381245
VP
14486 if (loc_num == 0)
14487 error (_("Bad breakpoint location number '%s'"), number);
14488
14489 --loc_num;
14490 loc = b->loc;
14491 for (;loc_num && loc; --loc_num, loc = loc->next)
14492 ;
14493 if (!loc)
14494 error (_("Bad breakpoint location number '%s'"), dot+1);
14495
14496 return loc;
14497}
14498
14499
1900040c
MS
14500/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14501 If from_tty is nonzero, it prints a message to that effect,
14502 which ends with a period (no newline). */
14503
c906108c 14504void
fba45db2 14505disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14506{
14507 /* Never disable a watchpoint scope breakpoint; we want to
14508 hit them when we leave scope so we can delete both the
14509 watchpoint and its scope breakpoint at that time. */
14510 if (bpt->type == bp_watchpoint_scope)
14511 return;
14512
b5de0fa7 14513 bpt->enable_state = bp_disabled;
c906108c 14514
b775012e
LM
14515 /* Mark breakpoint locations modified. */
14516 mark_breakpoint_modified (bpt);
14517
d248b706
KY
14518 if (target_supports_enable_disable_tracepoint ()
14519 && current_trace_status ()->running && is_tracepoint (bpt))
14520 {
14521 struct bp_location *location;
14522
14523 for (location = bpt->loc; location; location = location->next)
14524 target_disable_tracepoint (location);
14525 }
14526
44702360 14527 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14528
8d3788bd 14529 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14530}
14531
51be5b68
PA
14532/* A callback for iterate_over_related_breakpoints. */
14533
14534static void
14535do_disable_breakpoint (struct breakpoint *b, void *ignore)
14536{
14537 disable_breakpoint (b);
14538}
14539
95a42b64
TT
14540/* A callback for map_breakpoint_numbers that calls
14541 disable_breakpoint. */
14542
14543static void
14544do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14545{
51be5b68 14546 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14547}
14548
c906108c 14549static void
fba45db2 14550disable_command (char *args, int from_tty)
c906108c 14551{
c906108c 14552 if (args == 0)
46c6471b
PA
14553 {
14554 struct breakpoint *bpt;
14555
14556 ALL_BREAKPOINTS (bpt)
14557 if (user_breakpoint_p (bpt))
14558 disable_breakpoint (bpt);
14559 }
9eaabc75 14560 else
0d381245 14561 {
9eaabc75
MW
14562 char *num = extract_arg (&args);
14563
14564 while (num)
d248b706 14565 {
9eaabc75 14566 if (strchr (num, '.'))
b775012e 14567 {
9eaabc75
MW
14568 struct bp_location *loc = find_location_by_number (num);
14569
14570 if (loc)
14571 {
14572 if (loc->enabled)
14573 {
14574 loc->enabled = 0;
14575 mark_breakpoint_location_modified (loc);
14576 }
14577 if (target_supports_enable_disable_tracepoint ()
14578 && current_trace_status ()->running && loc->owner
14579 && is_tracepoint (loc->owner))
14580 target_disable_tracepoint (loc);
14581 }
44702360 14582 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14583 }
9eaabc75
MW
14584 else
14585 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14586 num = extract_arg (&args);
d248b706 14587 }
0d381245 14588 }
c906108c
SS
14589}
14590
14591static void
816338b5
SS
14592enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14593 int count)
c906108c 14594{
afe38095 14595 int target_resources_ok;
c906108c
SS
14596
14597 if (bpt->type == bp_hardware_breakpoint)
14598 {
14599 int i;
c5aa993b 14600 i = hw_breakpoint_used_count ();
53a5351d 14601 target_resources_ok =
d92524f1 14602 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14603 i + 1, 0);
c906108c 14604 if (target_resources_ok == 0)
8a3fe4f8 14605 error (_("No hardware breakpoint support in the target."));
c906108c 14606 else if (target_resources_ok < 0)
8a3fe4f8 14607 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14608 }
14609
cc60f2e3 14610 if (is_watchpoint (bpt))
c906108c 14611 {
d07205c2
JK
14612 /* Initialize it just to avoid a GCC false warning. */
14613 enum enable_state orig_enable_state = 0;
dde02812 14614
492d29ea 14615 TRY
c906108c 14616 {
3a5c3e22
PA
14617 struct watchpoint *w = (struct watchpoint *) bpt;
14618
1e718ff1
TJB
14619 orig_enable_state = bpt->enable_state;
14620 bpt->enable_state = bp_enabled;
3a5c3e22 14621 update_watchpoint (w, 1 /* reparse */);
c906108c 14622 }
492d29ea 14623 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14624 {
1e718ff1 14625 bpt->enable_state = orig_enable_state;
dde02812
ES
14626 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14627 bpt->number);
14628 return;
c5aa993b 14629 }
492d29ea 14630 END_CATCH
c906108c 14631 }
0101ce28 14632
b775012e
LM
14633 bpt->enable_state = bp_enabled;
14634
14635 /* Mark breakpoint locations modified. */
14636 mark_breakpoint_modified (bpt);
14637
d248b706
KY
14638 if (target_supports_enable_disable_tracepoint ()
14639 && current_trace_status ()->running && is_tracepoint (bpt))
14640 {
14641 struct bp_location *location;
14642
14643 for (location = bpt->loc; location; location = location->next)
14644 target_enable_tracepoint (location);
14645 }
14646
b4c291bb 14647 bpt->disposition = disposition;
816338b5 14648 bpt->enable_count = count;
44702360 14649 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14650
8d3788bd 14651 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14652}
14653
fe3f5fa8 14654
c906108c 14655void
fba45db2 14656enable_breakpoint (struct breakpoint *bpt)
c906108c 14657{
816338b5 14658 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14659}
14660
14661static void
14662do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14663{
14664 enable_breakpoint (bpt);
c906108c
SS
14665}
14666
95a42b64
TT
14667/* A callback for map_breakpoint_numbers that calls
14668 enable_breakpoint. */
14669
14670static void
14671do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14672{
51be5b68 14673 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14674}
14675
c906108c
SS
14676/* The enable command enables the specified breakpoints (or all defined
14677 breakpoints) so they once again become (or continue to be) effective
1272ad14 14678 in stopping the inferior. */
c906108c 14679
c906108c 14680static void
fba45db2 14681enable_command (char *args, int from_tty)
c906108c 14682{
c906108c 14683 if (args == 0)
46c6471b
PA
14684 {
14685 struct breakpoint *bpt;
14686
14687 ALL_BREAKPOINTS (bpt)
14688 if (user_breakpoint_p (bpt))
14689 enable_breakpoint (bpt);
14690 }
9eaabc75 14691 else
0d381245 14692 {
9eaabc75
MW
14693 char *num = extract_arg (&args);
14694
14695 while (num)
d248b706 14696 {
9eaabc75 14697 if (strchr (num, '.'))
b775012e 14698 {
9eaabc75
MW
14699 struct bp_location *loc = find_location_by_number (num);
14700
14701 if (loc)
14702 {
14703 if (!loc->enabled)
14704 {
14705 loc->enabled = 1;
14706 mark_breakpoint_location_modified (loc);
14707 }
14708 if (target_supports_enable_disable_tracepoint ()
14709 && current_trace_status ()->running && loc->owner
14710 && is_tracepoint (loc->owner))
14711 target_enable_tracepoint (loc);
14712 }
44702360 14713 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14714 }
9eaabc75
MW
14715 else
14716 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14717 num = extract_arg (&args);
d248b706 14718 }
0d381245 14719 }
c906108c
SS
14720}
14721
816338b5
SS
14722/* This struct packages up disposition data for application to multiple
14723 breakpoints. */
14724
14725struct disp_data
14726{
14727 enum bpdisp disp;
14728 int count;
14729};
14730
c906108c 14731static void
51be5b68
PA
14732do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14733{
816338b5 14734 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14735
816338b5 14736 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14737}
14738
14739static void
14740do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14741{
816338b5 14742 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14743
14744 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14745}
14746
c906108c 14747static void
fba45db2 14748enable_once_command (char *args, int from_tty)
c906108c 14749{
51be5b68 14750 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14751}
14752
816338b5
SS
14753static void
14754do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14755{
14756 struct disp_data disp = { disp_disable, *(int *) countptr };
14757
14758 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14759}
14760
14761static void
14762enable_count_command (char *args, int from_tty)
14763{
b9d61307
SM
14764 int count;
14765
14766 if (args == NULL)
14767 error_no_arg (_("hit count"));
14768
14769 count = get_number (&args);
816338b5
SS
14770
14771 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14772}
14773
c906108c 14774static void
51be5b68 14775do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14776{
816338b5 14777 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14778
14779 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14780}
14781
c906108c 14782static void
fba45db2 14783enable_delete_command (char *args, int from_tty)
c906108c 14784{
51be5b68 14785 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14786}
14787\f
fa8d40ab
JJ
14788static void
14789set_breakpoint_cmd (char *args, int from_tty)
14790{
14791}
14792
14793static void
14794show_breakpoint_cmd (char *args, int from_tty)
14795{
14796}
14797
1f3b5d1b
PP
14798/* Invalidate last known value of any hardware watchpoint if
14799 the memory which that value represents has been written to by
14800 GDB itself. */
14801
14802static void
8de0566d
YQ
14803invalidate_bp_value_on_memory_change (struct inferior *inferior,
14804 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14805 const bfd_byte *data)
14806{
14807 struct breakpoint *bp;
14808
14809 ALL_BREAKPOINTS (bp)
14810 if (bp->enable_state == bp_enabled
3a5c3e22 14811 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14812 {
3a5c3e22 14813 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14814
3a5c3e22
PA
14815 if (wp->val_valid && wp->val)
14816 {
14817 struct bp_location *loc;
14818
14819 for (loc = bp->loc; loc != NULL; loc = loc->next)
14820 if (loc->loc_type == bp_loc_hardware_watchpoint
14821 && loc->address + loc->length > addr
14822 && addr + len > loc->address)
14823 {
14824 value_free (wp->val);
14825 wp->val = NULL;
14826 wp->val_valid = 0;
14827 }
14828 }
1f3b5d1b
PP
14829 }
14830}
14831
8181d85f
DJ
14832/* Create and insert a breakpoint for software single step. */
14833
14834void
6c95b8df 14835insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14836 struct address_space *aspace,
14837 CORE_ADDR next_pc)
8181d85f 14838{
7c16b83e
PA
14839 struct thread_info *tp = inferior_thread ();
14840 struct symtab_and_line sal;
14841 CORE_ADDR pc = next_pc;
8181d85f 14842
34b7e8a6
PA
14843 if (tp->control.single_step_breakpoints == NULL)
14844 {
14845 tp->control.single_step_breakpoints
14846 = new_single_step_breakpoint (tp->num, gdbarch);
14847 }
8181d85f 14848
7c16b83e
PA
14849 sal = find_pc_line (pc, 0);
14850 sal.pc = pc;
14851 sal.section = find_pc_overlay (pc);
14852 sal.explicit_pc = 1;
34b7e8a6 14853 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14854
7c16b83e 14855 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14856}
14857
34b7e8a6 14858/* See breakpoint.h. */
f02253f1
HZ
14859
14860int
7c16b83e
PA
14861breakpoint_has_location_inserted_here (struct breakpoint *bp,
14862 struct address_space *aspace,
14863 CORE_ADDR pc)
1aafd4da 14864{
7c16b83e 14865 struct bp_location *loc;
1aafd4da 14866
7c16b83e
PA
14867 for (loc = bp->loc; loc != NULL; loc = loc->next)
14868 if (loc->inserted
14869 && breakpoint_location_address_match (loc, aspace, pc))
14870 return 1;
1aafd4da 14871
7c16b83e 14872 return 0;
ef370185
JB
14873}
14874
14875/* Check whether a software single-step breakpoint is inserted at
14876 PC. */
14877
14878int
14879single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14880 CORE_ADDR pc)
14881{
34b7e8a6
PA
14882 struct breakpoint *bpt;
14883
14884 ALL_BREAKPOINTS (bpt)
14885 {
14886 if (bpt->type == bp_single_step
14887 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14888 return 1;
14889 }
14890 return 0;
1aafd4da
UW
14891}
14892
1042e4c0
SS
14893/* Tracepoint-specific operations. */
14894
14895/* Set tracepoint count to NUM. */
14896static void
14897set_tracepoint_count (int num)
14898{
14899 tracepoint_count = num;
4fa62494 14900 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14901}
14902
70221824 14903static void
1042e4c0
SS
14904trace_command (char *arg, int from_tty)
14905{
55aa24fb
SDJ
14906 struct breakpoint_ops *ops;
14907 const char *arg_cp = arg;
14908
14909 if (arg && probe_linespec_to_ops (&arg_cp))
14910 ops = &tracepoint_probe_breakpoint_ops;
14911 else
14912 ops = &tracepoint_breakpoint_ops;
14913
558a9d82
YQ
14914 create_breakpoint (get_current_arch (),
14915 arg,
14916 NULL, 0, NULL, 1 /* parse arg */,
14917 0 /* tempflag */,
14918 bp_tracepoint /* type_wanted */,
14919 0 /* Ignore count */,
14920 pending_break_support,
14921 ops,
14922 from_tty,
14923 1 /* enabled */,
14924 0 /* internal */, 0);
1042e4c0
SS
14925}
14926
70221824 14927static void
7a697b8d
SS
14928ftrace_command (char *arg, int from_tty)
14929{
558a9d82
YQ
14930 create_breakpoint (get_current_arch (),
14931 arg,
14932 NULL, 0, NULL, 1 /* parse arg */,
14933 0 /* tempflag */,
14934 bp_fast_tracepoint /* type_wanted */,
14935 0 /* Ignore count */,
14936 pending_break_support,
14937 &tracepoint_breakpoint_ops,
14938 from_tty,
14939 1 /* enabled */,
14940 0 /* internal */, 0);
0fb4aa4b
PA
14941}
14942
14943/* strace command implementation. Creates a static tracepoint. */
14944
70221824 14945static void
0fb4aa4b
PA
14946strace_command (char *arg, int from_tty)
14947{
983af33b
SDJ
14948 struct breakpoint_ops *ops;
14949
14950 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14951 or with a normal static tracepoint. */
61012eef 14952 if (arg && startswith (arg, "-m") && isspace (arg[2]))
983af33b
SDJ
14953 ops = &strace_marker_breakpoint_ops;
14954 else
14955 ops = &tracepoint_breakpoint_ops;
14956
558a9d82
YQ
14957 create_breakpoint (get_current_arch (),
14958 arg,
14959 NULL, 0, NULL, 1 /* parse arg */,
14960 0 /* tempflag */,
14961 bp_static_tracepoint /* type_wanted */,
14962 0 /* Ignore count */,
14963 pending_break_support,
14964 ops,
14965 from_tty,
14966 1 /* enabled */,
14967 0 /* internal */, 0);
7a697b8d
SS
14968}
14969
409873ef
SS
14970/* Set up a fake reader function that gets command lines from a linked
14971 list that was acquired during tracepoint uploading. */
14972
14973static struct uploaded_tp *this_utp;
3149d8c1 14974static int next_cmd;
409873ef
SS
14975
14976static char *
14977read_uploaded_action (void)
14978{
14979 char *rslt;
14980
3149d8c1 14981 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 14982
3149d8c1 14983 next_cmd++;
409873ef
SS
14984
14985 return rslt;
14986}
14987
00bf0b85
SS
14988/* Given information about a tracepoint as recorded on a target (which
14989 can be either a live system or a trace file), attempt to create an
14990 equivalent GDB tracepoint. This is not a reliable process, since
14991 the target does not necessarily have all the information used when
14992 the tracepoint was originally defined. */
14993
d9b3f62e 14994struct tracepoint *
00bf0b85 14995create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14996{
409873ef 14997 char *addr_str, small_buf[100];
d9b3f62e 14998 struct tracepoint *tp;
fd9b8c24 14999
409873ef
SS
15000 if (utp->at_string)
15001 addr_str = utp->at_string;
15002 else
15003 {
15004 /* In the absence of a source location, fall back to raw
15005 address. Since there is no way to confirm that the address
15006 means the same thing as when the trace was started, warn the
15007 user. */
3e43a32a
MS
15008 warning (_("Uploaded tracepoint %d has no "
15009 "source location, using raw address"),
409873ef 15010 utp->number);
8c042590 15011 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15012 addr_str = small_buf;
15013 }
15014
15015 /* There's not much we can do with a sequence of bytecodes. */
15016 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15017 warning (_("Uploaded tracepoint %d condition "
15018 "has no source form, ignoring it"),
409873ef 15019 utp->number);
d5551862 15020
8cdf0e15 15021 if (!create_breakpoint (get_current_arch (),
409873ef 15022 addr_str,
e7e0cddf
SS
15023 utp->cond_string, -1, NULL,
15024 0 /* parse cond/thread */,
8cdf0e15 15025 0 /* tempflag */,
0fb4aa4b 15026 utp->type /* type_wanted */,
8cdf0e15
VP
15027 0 /* Ignore count */,
15028 pending_break_support,
348d480f 15029 &tracepoint_breakpoint_ops,
8cdf0e15 15030 0 /* from_tty */,
84f4c1fe 15031 utp->enabled /* enabled */,
44f238bb
PA
15032 0 /* internal */,
15033 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15034 return NULL;
15035
409873ef 15036 /* Get the tracepoint we just created. */
fd9b8c24
PA
15037 tp = get_tracepoint (tracepoint_count);
15038 gdb_assert (tp != NULL);
d5551862 15039
00bf0b85
SS
15040 if (utp->pass > 0)
15041 {
8c042590
PM
15042 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15043 tp->base.number);
00bf0b85 15044
409873ef 15045 trace_pass_command (small_buf, 0);
00bf0b85
SS
15046 }
15047
409873ef
SS
15048 /* If we have uploaded versions of the original commands, set up a
15049 special-purpose "reader" function and call the usual command line
15050 reader, then pass the result to the breakpoint command-setting
15051 function. */
3149d8c1 15052 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15053 {
409873ef 15054 struct command_line *cmd_list;
00bf0b85 15055
409873ef 15056 this_utp = utp;
3149d8c1 15057 next_cmd = 0;
d5551862 15058
409873ef
SS
15059 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15060
d9b3f62e 15061 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15062 }
3149d8c1
SS
15063 else if (!VEC_empty (char_ptr, utp->actions)
15064 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15065 warning (_("Uploaded tracepoint %d actions "
15066 "have no source form, ignoring them"),
409873ef 15067 utp->number);
00bf0b85 15068
f196051f
SS
15069 /* Copy any status information that might be available. */
15070 tp->base.hit_count = utp->hit_count;
15071 tp->traceframe_usage = utp->traceframe_usage;
15072
00bf0b85 15073 return tp;
d9b3f62e 15074}
00bf0b85 15075
1042e4c0
SS
15076/* Print information on tracepoint number TPNUM_EXP, or all if
15077 omitted. */
15078
15079static void
e5a67952 15080tracepoints_info (char *args, int from_tty)
1042e4c0 15081{
79a45e25 15082 struct ui_out *uiout = current_uiout;
e5a67952 15083 int num_printed;
1042e4c0 15084
e5a67952 15085 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15086
15087 if (num_printed == 0)
1042e4c0 15088 {
e5a67952 15089 if (args == NULL || *args == '\0')
d77f58be
SS
15090 ui_out_message (uiout, 0, "No tracepoints.\n");
15091 else
e5a67952 15092 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15093 }
ad443146
SS
15094
15095 default_collect_info ();
1042e4c0
SS
15096}
15097
4a64f543 15098/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15099 Not supported by all targets. */
15100static void
15101enable_trace_command (char *args, int from_tty)
15102{
15103 enable_command (args, from_tty);
15104}
15105
4a64f543 15106/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15107 Not supported by all targets. */
15108static void
15109disable_trace_command (char *args, int from_tty)
15110{
15111 disable_command (args, from_tty);
15112}
15113
4a64f543 15114/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15115static void
15116delete_trace_command (char *arg, int from_tty)
15117{
35df4500 15118 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15119
15120 dont_repeat ();
15121
15122 if (arg == 0)
15123 {
15124 int breaks_to_delete = 0;
15125
15126 /* Delete all breakpoints if no argument.
15127 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15128 have to be deleted with an explicit breakpoint number
15129 argument. */
1042e4c0 15130 ALL_TRACEPOINTS (b)
46c6471b 15131 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15132 {
15133 breaks_to_delete = 1;
15134 break;
15135 }
1042e4c0
SS
15136
15137 /* Ask user only if there are some breakpoints to delete. */
15138 if (!from_tty
15139 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15140 {
35df4500 15141 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15142 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15143 delete_breakpoint (b);
1042e4c0
SS
15144 }
15145 }
15146 else
51be5b68 15147 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15148}
15149
197f0a60
TT
15150/* Helper function for trace_pass_command. */
15151
15152static void
d9b3f62e 15153trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15154{
d9b3f62e 15155 tp->pass_count = count;
6f6484cd 15156 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15157 if (from_tty)
15158 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15159 tp->base.number, count);
197f0a60
TT
15160}
15161
1042e4c0
SS
15162/* Set passcount for tracepoint.
15163
15164 First command argument is passcount, second is tracepoint number.
15165 If tracepoint number omitted, apply to most recently defined.
15166 Also accepts special argument "all". */
15167
15168static void
15169trace_pass_command (char *args, int from_tty)
15170{
d9b3f62e 15171 struct tracepoint *t1;
1042e4c0 15172 unsigned int count;
1042e4c0
SS
15173
15174 if (args == 0 || *args == 0)
3e43a32a
MS
15175 error (_("passcount command requires an "
15176 "argument (count + optional TP num)"));
1042e4c0 15177
4a64f543 15178 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15179
529480d0 15180 args = skip_spaces (args);
1042e4c0
SS
15181 if (*args && strncasecmp (args, "all", 3) == 0)
15182 {
d9b3f62e
PA
15183 struct breakpoint *b;
15184
1042e4c0 15185 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15186 if (*args)
15187 error (_("Junk at end of arguments."));
1042e4c0 15188
d9b3f62e 15189 ALL_TRACEPOINTS (b)
197f0a60 15190 {
d9b3f62e 15191 t1 = (struct tracepoint *) b;
197f0a60
TT
15192 trace_pass_set_count (t1, count, from_tty);
15193 }
15194 }
15195 else if (*args == '\0')
1042e4c0 15196 {
5fa1d40e 15197 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15198 if (t1)
197f0a60
TT
15199 trace_pass_set_count (t1, count, from_tty);
15200 }
15201 else
15202 {
15203 struct get_number_or_range_state state;
15204
15205 init_number_or_range (&state, args);
15206 while (!state.finished)
1042e4c0 15207 {
5fa1d40e 15208 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15209 if (t1)
15210 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15211 }
15212 }
1042e4c0
SS
15213}
15214
d9b3f62e 15215struct tracepoint *
1042e4c0
SS
15216get_tracepoint (int num)
15217{
15218 struct breakpoint *t;
15219
15220 ALL_TRACEPOINTS (t)
15221 if (t->number == num)
d9b3f62e 15222 return (struct tracepoint *) t;
1042e4c0
SS
15223
15224 return NULL;
15225}
15226
d5551862
SS
15227/* Find the tracepoint with the given target-side number (which may be
15228 different from the tracepoint number after disconnecting and
15229 reconnecting). */
15230
d9b3f62e 15231struct tracepoint *
d5551862
SS
15232get_tracepoint_by_number_on_target (int num)
15233{
d9b3f62e 15234 struct breakpoint *b;
d5551862 15235
d9b3f62e
PA
15236 ALL_TRACEPOINTS (b)
15237 {
15238 struct tracepoint *t = (struct tracepoint *) b;
15239
15240 if (t->number_on_target == num)
15241 return t;
15242 }
d5551862
SS
15243
15244 return NULL;
15245}
15246
1042e4c0 15247/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15248 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15249 If the argument is missing, the most recent tracepoint
15250 (tracepoint_count) is returned. */
15251
d9b3f62e 15252struct tracepoint *
197f0a60 15253get_tracepoint_by_number (char **arg,
5fa1d40e 15254 struct get_number_or_range_state *state)
1042e4c0 15255{
1042e4c0
SS
15256 struct breakpoint *t;
15257 int tpnum;
15258 char *instring = arg == NULL ? NULL : *arg;
15259
197f0a60
TT
15260 if (state)
15261 {
15262 gdb_assert (!state->finished);
15263 tpnum = get_number_or_range (state);
15264 }
15265 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15266 tpnum = tracepoint_count;
1042e4c0 15267 else
197f0a60 15268 tpnum = get_number (arg);
1042e4c0
SS
15269
15270 if (tpnum <= 0)
15271 {
15272 if (instring && *instring)
15273 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15274 instring);
15275 else
5fa1d40e 15276 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15277 return NULL;
15278 }
15279
15280 ALL_TRACEPOINTS (t)
15281 if (t->number == tpnum)
15282 {
d9b3f62e 15283 return (struct tracepoint *) t;
1042e4c0
SS
15284 }
15285
1042e4c0
SS
15286 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15287 return NULL;
15288}
15289
d9b3f62e
PA
15290void
15291print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15292{
15293 if (b->thread != -1)
15294 fprintf_unfiltered (fp, " thread %d", b->thread);
15295
15296 if (b->task != 0)
15297 fprintf_unfiltered (fp, " task %d", b->task);
15298
15299 fprintf_unfiltered (fp, "\n");
15300}
15301
6149aea9
PA
15302/* Save information on user settable breakpoints (watchpoints, etc) to
15303 a new script file named FILENAME. If FILTER is non-NULL, call it
15304 on each breakpoint and only include the ones for which it returns
15305 non-zero. */
15306
1042e4c0 15307static void
6149aea9
PA
15308save_breakpoints (char *filename, int from_tty,
15309 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15310{
15311 struct breakpoint *tp;
6149aea9 15312 int any = 0;
1042e4c0 15313 struct cleanup *cleanup;
a7bdde9e 15314 struct ui_file *fp;
6149aea9 15315 int extra_trace_bits = 0;
1042e4c0 15316
6149aea9
PA
15317 if (filename == 0 || *filename == 0)
15318 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15319
15320 /* See if we have anything to save. */
6149aea9 15321 ALL_BREAKPOINTS (tp)
1042e4c0 15322 {
6149aea9 15323 /* Skip internal and momentary breakpoints. */
09d682a4 15324 if (!user_breakpoint_p (tp))
6149aea9
PA
15325 continue;
15326
15327 /* If we have a filter, only save the breakpoints it accepts. */
15328 if (filter && !filter (tp))
15329 continue;
15330
15331 any = 1;
15332
15333 if (is_tracepoint (tp))
15334 {
15335 extra_trace_bits = 1;
15336
15337 /* We can stop searching. */
15338 break;
15339 }
1042e4c0 15340 }
6149aea9
PA
15341
15342 if (!any)
1042e4c0 15343 {
6149aea9 15344 warning (_("Nothing to save."));
1042e4c0
SS
15345 return;
15346 }
15347
c718be47
PA
15348 filename = tilde_expand (filename);
15349 cleanup = make_cleanup (xfree, filename);
15350 fp = gdb_fopen (filename, "w");
059fb39f 15351 if (!fp)
6149aea9
PA
15352 error (_("Unable to open file '%s' for saving (%s)"),
15353 filename, safe_strerror (errno));
a7bdde9e 15354 make_cleanup_ui_file_delete (fp);
8bf6485c 15355
6149aea9
PA
15356 if (extra_trace_bits)
15357 save_trace_state_variables (fp);
8bf6485c 15358
6149aea9 15359 ALL_BREAKPOINTS (tp)
1042e4c0 15360 {
6149aea9 15361 /* Skip internal and momentary breakpoints. */
09d682a4 15362 if (!user_breakpoint_p (tp))
6149aea9 15363 continue;
8bf6485c 15364
6149aea9
PA
15365 /* If we have a filter, only save the breakpoints it accepts. */
15366 if (filter && !filter (tp))
15367 continue;
15368
348d480f 15369 tp->ops->print_recreate (tp, fp);
1042e4c0 15370
6149aea9
PA
15371 /* Note, we can't rely on tp->number for anything, as we can't
15372 assume the recreated breakpoint numbers will match. Use $bpnum
15373 instead. */
15374
15375 if (tp->cond_string)
15376 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15377
15378 if (tp->ignore_count)
15379 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15380
2d9442cc 15381 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15382 {
6c63c96a 15383 struct gdb_exception exception;
a7bdde9e 15384
6149aea9 15385 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15386
79a45e25 15387 ui_out_redirect (current_uiout, fp);
492d29ea 15388 TRY
1042e4c0 15389 {
79a45e25 15390 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15391 }
492d29ea
PA
15392 CATCH (ex, RETURN_MASK_ALL)
15393 {
6c63c96a 15394 ui_out_redirect (current_uiout, NULL);
492d29ea
PA
15395 throw_exception (ex);
15396 }
15397 END_CATCH
1042e4c0 15398
6c63c96a 15399 ui_out_redirect (current_uiout, NULL);
a7bdde9e 15400 fprintf_unfiltered (fp, " end\n");
1042e4c0 15401 }
6149aea9
PA
15402
15403 if (tp->enable_state == bp_disabled)
99894e11 15404 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15405
15406 /* If this is a multi-location breakpoint, check if the locations
15407 should be individually disabled. Watchpoint locations are
15408 special, and not user visible. */
15409 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15410 {
15411 struct bp_location *loc;
15412 int n = 1;
15413
15414 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15415 if (!loc->enabled)
15416 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15417 }
1042e4c0 15418 }
8bf6485c 15419
6149aea9 15420 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15421 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15422
1042e4c0 15423 if (from_tty)
6149aea9 15424 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15425 do_cleanups (cleanup);
6149aea9
PA
15426}
15427
15428/* The `save breakpoints' command. */
15429
15430static void
15431save_breakpoints_command (char *args, int from_tty)
15432{
15433 save_breakpoints (args, from_tty, NULL);
15434}
15435
15436/* The `save tracepoints' command. */
15437
15438static void
15439save_tracepoints_command (char *args, int from_tty)
15440{
15441 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15442}
15443
15444/* Create a vector of all tracepoints. */
15445
15446VEC(breakpoint_p) *
eeae04df 15447all_tracepoints (void)
1042e4c0
SS
15448{
15449 VEC(breakpoint_p) *tp_vec = 0;
15450 struct breakpoint *tp;
15451
15452 ALL_TRACEPOINTS (tp)
15453 {
15454 VEC_safe_push (breakpoint_p, tp_vec, tp);
15455 }
15456
15457 return tp_vec;
15458}
15459
c906108c 15460\f
4a64f543
MS
15461/* This help string is used for the break, hbreak, tbreak and thbreak
15462 commands. It is defined as a macro to prevent duplication.
15463 COMMAND should be a string constant containing the name of the
15464 command. */
31e2b00f 15465#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15466command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15467PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15468probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15469guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15470`-probe-dtrace' (for a DTrace probe).\n\
31e2b00f
AS
15471LOCATION may be a line number, function name, or \"*\" and an address.\n\
15472If a line number is specified, break at start of code for that line.\n\
15473If a function is specified, break at start of code for that function.\n\
15474If an address is specified, break at that exact address.\n\
dc10affe
PA
15475With no LOCATION, uses current execution address of the selected\n\
15476stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15477\n\
15478THREADNUM is the number from \"info threads\".\n\
15479CONDITION is a boolean expression.\n\
15480\n\
d41c0fc8
PA
15481Multiple breakpoints at one place are permitted, and useful if their\n\
15482conditions are different.\n\
31e2b00f
AS
15483\n\
15484Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15485
44feb3ce
TT
15486/* List of subcommands for "catch". */
15487static struct cmd_list_element *catch_cmdlist;
15488
15489/* List of subcommands for "tcatch". */
15490static struct cmd_list_element *tcatch_cmdlist;
15491
9ac4176b 15492void
44feb3ce 15493add_catch_command (char *name, char *docstring,
82ae6c8d 15494 cmd_sfunc_ftype *sfunc,
625e8578 15495 completer_ftype *completer,
44feb3ce
TT
15496 void *user_data_catch,
15497 void *user_data_tcatch)
15498{
15499 struct cmd_list_element *command;
15500
15501 command = add_cmd (name, class_breakpoint, NULL, docstring,
15502 &catch_cmdlist);
15503 set_cmd_sfunc (command, sfunc);
15504 set_cmd_context (command, user_data_catch);
a96d9b2e 15505 set_cmd_completer (command, completer);
44feb3ce
TT
15506
15507 command = add_cmd (name, class_breakpoint, NULL, docstring,
15508 &tcatch_cmdlist);
15509 set_cmd_sfunc (command, sfunc);
15510 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15511 set_cmd_completer (command, completer);
44feb3ce
TT
15512}
15513
6149aea9
PA
15514static void
15515save_command (char *arg, int from_tty)
15516{
3e43a32a
MS
15517 printf_unfiltered (_("\"save\" must be followed by "
15518 "the name of a save subcommand.\n"));
635c7e8a 15519 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15520}
15521
84f4c1fe
PM
15522struct breakpoint *
15523iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15524 void *data)
15525{
35df4500 15526 struct breakpoint *b, *b_tmp;
84f4c1fe 15527
35df4500 15528 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15529 {
15530 if ((*callback) (b, data))
15531 return b;
15532 }
15533
15534 return NULL;
15535}
15536
0574c78f
GB
15537/* Zero if any of the breakpoint's locations could be a location where
15538 functions have been inlined, nonzero otherwise. */
15539
15540static int
15541is_non_inline_function (struct breakpoint *b)
15542{
15543 /* The shared library event breakpoint is set on the address of a
15544 non-inline function. */
15545 if (b->type == bp_shlib_event)
15546 return 1;
15547
15548 return 0;
15549}
15550
15551/* Nonzero if the specified PC cannot be a location where functions
15552 have been inlined. */
15553
15554int
09ac7c10
TT
15555pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15556 const struct target_waitstatus *ws)
0574c78f
GB
15557{
15558 struct breakpoint *b;
15559 struct bp_location *bl;
15560
15561 ALL_BREAKPOINTS (b)
15562 {
15563 if (!is_non_inline_function (b))
15564 continue;
15565
15566 for (bl = b->loc; bl != NULL; bl = bl->next)
15567 {
15568 if (!bl->shlib_disabled
09ac7c10 15569 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15570 return 1;
15571 }
15572 }
15573
15574 return 0;
15575}
15576
2f202fde
JK
15577/* Remove any references to OBJFILE which is going to be freed. */
15578
15579void
15580breakpoint_free_objfile (struct objfile *objfile)
15581{
15582 struct bp_location **locp, *loc;
15583
15584 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15585 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15586 loc->symtab = NULL;
15587}
15588
2060206e
PA
15589void
15590initialize_breakpoint_ops (void)
15591{
15592 static int initialized = 0;
15593
15594 struct breakpoint_ops *ops;
15595
15596 if (initialized)
15597 return;
15598 initialized = 1;
15599
15600 /* The breakpoint_ops structure to be inherit by all kinds of
15601 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15602 internal and momentary breakpoints, etc.). */
15603 ops = &bkpt_base_breakpoint_ops;
15604 *ops = base_breakpoint_ops;
15605 ops->re_set = bkpt_re_set;
15606 ops->insert_location = bkpt_insert_location;
15607 ops->remove_location = bkpt_remove_location;
15608 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
15609 ops->create_sals_from_address = bkpt_create_sals_from_address;
15610 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15611 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
15612
15613 /* The breakpoint_ops structure to be used in regular breakpoints. */
15614 ops = &bkpt_breakpoint_ops;
15615 *ops = bkpt_base_breakpoint_ops;
15616 ops->re_set = bkpt_re_set;
15617 ops->resources_needed = bkpt_resources_needed;
15618 ops->print_it = bkpt_print_it;
15619 ops->print_mention = bkpt_print_mention;
15620 ops->print_recreate = bkpt_print_recreate;
15621
15622 /* Ranged breakpoints. */
15623 ops = &ranged_breakpoint_ops;
15624 *ops = bkpt_breakpoint_ops;
15625 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15626 ops->resources_needed = resources_needed_ranged_breakpoint;
15627 ops->print_it = print_it_ranged_breakpoint;
15628 ops->print_one = print_one_ranged_breakpoint;
15629 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15630 ops->print_mention = print_mention_ranged_breakpoint;
15631 ops->print_recreate = print_recreate_ranged_breakpoint;
15632
15633 /* Internal breakpoints. */
15634 ops = &internal_breakpoint_ops;
15635 *ops = bkpt_base_breakpoint_ops;
15636 ops->re_set = internal_bkpt_re_set;
15637 ops->check_status = internal_bkpt_check_status;
15638 ops->print_it = internal_bkpt_print_it;
15639 ops->print_mention = internal_bkpt_print_mention;
15640
15641 /* Momentary breakpoints. */
15642 ops = &momentary_breakpoint_ops;
15643 *ops = bkpt_base_breakpoint_ops;
15644 ops->re_set = momentary_bkpt_re_set;
15645 ops->check_status = momentary_bkpt_check_status;
15646 ops->print_it = momentary_bkpt_print_it;
15647 ops->print_mention = momentary_bkpt_print_mention;
15648
e2e4d78b
JK
15649 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15650 ops = &longjmp_breakpoint_ops;
15651 *ops = momentary_breakpoint_ops;
15652 ops->dtor = longjmp_bkpt_dtor;
15653
55aa24fb
SDJ
15654 /* Probe breakpoints. */
15655 ops = &bkpt_probe_breakpoint_ops;
15656 *ops = bkpt_breakpoint_ops;
15657 ops->insert_location = bkpt_probe_insert_location;
15658 ops->remove_location = bkpt_probe_remove_location;
15659 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15660 ops->decode_linespec = bkpt_probe_decode_linespec;
15661
2060206e
PA
15662 /* Watchpoints. */
15663 ops = &watchpoint_breakpoint_ops;
15664 *ops = base_breakpoint_ops;
3a5c3e22 15665 ops->dtor = dtor_watchpoint;
2060206e
PA
15666 ops->re_set = re_set_watchpoint;
15667 ops->insert_location = insert_watchpoint;
15668 ops->remove_location = remove_watchpoint;
15669 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15670 ops->check_status = check_status_watchpoint;
15671 ops->resources_needed = resources_needed_watchpoint;
15672 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15673 ops->print_it = print_it_watchpoint;
15674 ops->print_mention = print_mention_watchpoint;
15675 ops->print_recreate = print_recreate_watchpoint;
427cd150 15676 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15677
15678 /* Masked watchpoints. */
15679 ops = &masked_watchpoint_breakpoint_ops;
15680 *ops = watchpoint_breakpoint_ops;
15681 ops->insert_location = insert_masked_watchpoint;
15682 ops->remove_location = remove_masked_watchpoint;
15683 ops->resources_needed = resources_needed_masked_watchpoint;
15684 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15685 ops->print_it = print_it_masked_watchpoint;
15686 ops->print_one_detail = print_one_detail_masked_watchpoint;
15687 ops->print_mention = print_mention_masked_watchpoint;
15688 ops->print_recreate = print_recreate_masked_watchpoint;
15689
15690 /* Tracepoints. */
15691 ops = &tracepoint_breakpoint_ops;
15692 *ops = base_breakpoint_ops;
15693 ops->re_set = tracepoint_re_set;
15694 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15695 ops->print_one_detail = tracepoint_print_one_detail;
15696 ops->print_mention = tracepoint_print_mention;
15697 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
15698 ops->create_sals_from_address = tracepoint_create_sals_from_address;
15699 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15700 ops->decode_linespec = tracepoint_decode_linespec;
15701
55aa24fb
SDJ
15702 /* Probe tracepoints. */
15703 ops = &tracepoint_probe_breakpoint_ops;
15704 *ops = tracepoint_breakpoint_ops;
15705 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15706 ops->decode_linespec = tracepoint_probe_decode_linespec;
15707
983af33b
SDJ
15708 /* Static tracepoints with marker (`-m'). */
15709 ops = &strace_marker_breakpoint_ops;
15710 *ops = tracepoint_breakpoint_ops;
15711 ops->create_sals_from_address = strace_marker_create_sals_from_address;
15712 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15713 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
15714
15715 /* Fork catchpoints. */
15716 ops = &catch_fork_breakpoint_ops;
15717 *ops = base_breakpoint_ops;
15718 ops->insert_location = insert_catch_fork;
15719 ops->remove_location = remove_catch_fork;
15720 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15721 ops->print_it = print_it_catch_fork;
15722 ops->print_one = print_one_catch_fork;
15723 ops->print_mention = print_mention_catch_fork;
15724 ops->print_recreate = print_recreate_catch_fork;
15725
15726 /* Vfork catchpoints. */
15727 ops = &catch_vfork_breakpoint_ops;
15728 *ops = base_breakpoint_ops;
15729 ops->insert_location = insert_catch_vfork;
15730 ops->remove_location = remove_catch_vfork;
15731 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15732 ops->print_it = print_it_catch_vfork;
15733 ops->print_one = print_one_catch_vfork;
15734 ops->print_mention = print_mention_catch_vfork;
15735 ops->print_recreate = print_recreate_catch_vfork;
15736
15737 /* Exec catchpoints. */
15738 ops = &catch_exec_breakpoint_ops;
15739 *ops = base_breakpoint_ops;
15740 ops->dtor = dtor_catch_exec;
15741 ops->insert_location = insert_catch_exec;
15742 ops->remove_location = remove_catch_exec;
15743 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15744 ops->print_it = print_it_catch_exec;
15745 ops->print_one = print_one_catch_exec;
15746 ops->print_mention = print_mention_catch_exec;
15747 ops->print_recreate = print_recreate_catch_exec;
15748
edcc5120
TT
15749 /* Solib-related catchpoints. */
15750 ops = &catch_solib_breakpoint_ops;
15751 *ops = base_breakpoint_ops;
15752 ops->dtor = dtor_catch_solib;
15753 ops->insert_location = insert_catch_solib;
15754 ops->remove_location = remove_catch_solib;
15755 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15756 ops->check_status = check_status_catch_solib;
15757 ops->print_it = print_it_catch_solib;
15758 ops->print_one = print_one_catch_solib;
15759 ops->print_mention = print_mention_catch_solib;
15760 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15761
15762 ops = &dprintf_breakpoint_ops;
15763 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15764 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15765 ops->resources_needed = bkpt_resources_needed;
15766 ops->print_it = bkpt_print_it;
15767 ops->print_mention = bkpt_print_mention;
2d9442cc 15768 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15769 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15770 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15771}
15772
8bfd80db
YQ
15773/* Chain containing all defined "enable breakpoint" subcommands. */
15774
15775static struct cmd_list_element *enablebreaklist = NULL;
15776
c906108c 15777void
fba45db2 15778_initialize_breakpoint (void)
c906108c
SS
15779{
15780 struct cmd_list_element *c;
15781
2060206e
PA
15782 initialize_breakpoint_ops ();
15783
84acb35a 15784 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15785 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15786 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15787
55aa24fb
SDJ
15788 breakpoint_objfile_key
15789 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15790
c906108c
SS
15791 breakpoint_chain = 0;
15792 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15793 before a breakpoint is set. */
15794 breakpoint_count = 0;
15795
1042e4c0
SS
15796 tracepoint_count = 0;
15797
1bedd215
AC
15798 add_com ("ignore", class_breakpoint, ignore_command, _("\
15799Set ignore-count of breakpoint number N to COUNT.\n\
15800Usage is `ignore N COUNT'."));
c906108c 15801
1bedd215
AC
15802 add_com ("commands", class_breakpoint, commands_command, _("\
15803Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
15804Give breakpoint number as argument after \"commands\".\n\
15805With no argument, the targeted breakpoint is the last one set.\n\
15806The commands themselves follow starting on the next line.\n\
15807Type a line containing \"end\" to indicate the end of them.\n\
15808Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15809then no output is printed when it is hit, except what the commands print."));
c906108c 15810
d55637df 15811 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15812Specify breakpoint number N to break only if COND is true.\n\
c906108c 15813Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15814expression to be evaluated whenever breakpoint N is reached."));
d55637df 15815 set_cmd_completer (c, condition_completer);
c906108c 15816
1bedd215 15817 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15818Set a temporary breakpoint.\n\
c906108c
SS
15819Like \"break\" except the breakpoint is only temporary,\n\
15820so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15821by using \"enable delete\" on the breakpoint number.\n\
15822\n"
15823BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15824 set_cmd_completer (c, location_completer);
c94fdfd0 15825
1bedd215 15826 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15827Set a hardware assisted breakpoint.\n\
c906108c 15828Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15829some target hardware may not have this support.\n\
15830\n"
15831BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15832 set_cmd_completer (c, location_completer);
c906108c 15833
1bedd215 15834 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15835Set a temporary hardware assisted breakpoint.\n\
c906108c 15836Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15837so it will be deleted when hit.\n\
15838\n"
15839BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15840 set_cmd_completer (c, location_completer);
c906108c 15841
1bedd215
AC
15842 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15843Enable some breakpoints.\n\
c906108c
SS
15844Give breakpoint numbers (separated by spaces) as arguments.\n\
15845With no subcommand, breakpoints are enabled until you command otherwise.\n\
15846This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15847With a subcommand you can enable temporarily."),
c906108c 15848 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15849
15850 add_com_alias ("en", "enable", class_breakpoint, 1);
15851
84951ab5 15852 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15853Enable some breakpoints.\n\
c906108c
SS
15854Give breakpoint numbers (separated by spaces) as arguments.\n\
15855This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15856May be abbreviated to simply \"enable\".\n"),
c5aa993b 15857 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15858
1a966eab
AC
15859 add_cmd ("once", no_class, enable_once_command, _("\
15860Enable breakpoints for one hit. Give breakpoint numbers.\n\
15861If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15862 &enablebreaklist);
15863
1a966eab
AC
15864 add_cmd ("delete", no_class, enable_delete_command, _("\
15865Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15866If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15867 &enablebreaklist);
15868
816338b5
SS
15869 add_cmd ("count", no_class, enable_count_command, _("\
15870Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15871If a breakpoint is hit while enabled in this fashion,\n\
15872the count is decremented; when it reaches zero, the breakpoint is disabled."),
15873 &enablebreaklist);
15874
1a966eab
AC
15875 add_cmd ("delete", no_class, enable_delete_command, _("\
15876Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15877If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15878 &enablelist);
15879
1a966eab
AC
15880 add_cmd ("once", no_class, enable_once_command, _("\
15881Enable breakpoints for one hit. Give breakpoint numbers.\n\
15882If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15883 &enablelist);
15884
15885 add_cmd ("count", no_class, enable_count_command, _("\
15886Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15887If a breakpoint is hit while enabled in this fashion,\n\
15888the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15889 &enablelist);
15890
1bedd215
AC
15891 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15892Disable some breakpoints.\n\
c906108c
SS
15893Arguments are breakpoint numbers with spaces in between.\n\
15894To disable all breakpoints, give no argument.\n\
64b9b334 15895A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15896 &disablelist, "disable ", 1, &cmdlist);
15897 add_com_alias ("dis", "disable", class_breakpoint, 1);
15898 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15899
1a966eab
AC
15900 add_cmd ("breakpoints", class_alias, disable_command, _("\
15901Disable some breakpoints.\n\
c906108c
SS
15902Arguments are breakpoint numbers with spaces in between.\n\
15903To disable all breakpoints, give no argument.\n\
64b9b334 15904A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15905This command may be abbreviated \"disable\"."),
c906108c
SS
15906 &disablelist);
15907
1bedd215
AC
15908 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15909Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15910Arguments are breakpoint numbers with spaces in between.\n\
15911To delete all breakpoints, give no argument.\n\
15912\n\
15913Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15914The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15915 &deletelist, "delete ", 1, &cmdlist);
15916 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15917 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15918
1a966eab
AC
15919 add_cmd ("breakpoints", class_alias, delete_command, _("\
15920Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15921Arguments are breakpoint numbers with spaces in between.\n\
15922To delete all breakpoints, give no argument.\n\
1a966eab 15923This command may be abbreviated \"delete\"."),
c906108c
SS
15924 &deletelist);
15925
1bedd215
AC
15926 add_com ("clear", class_breakpoint, clear_command, _("\
15927Clear breakpoint at specified line or function.\n\
c906108c
SS
15928Argument may be line number, function name, or \"*\" and an address.\n\
15929If line number is specified, all breakpoints in that line are cleared.\n\
15930If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
15931If an address is specified, breakpoints at that address are cleared.\n\
15932\n\
15933With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
15934is executing in.\n\
15935\n\
1bedd215 15936See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15937 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15938
1bedd215 15939 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
15940Set breakpoint at specified line or function.\n"
15941BREAK_ARGS_HELP ("break")));
5ba2abeb 15942 set_cmd_completer (c, location_completer);
c94fdfd0 15943
c906108c
SS
15944 add_com_alias ("b", "break", class_run, 1);
15945 add_com_alias ("br", "break", class_run, 1);
15946 add_com_alias ("bre", "break", class_run, 1);
15947 add_com_alias ("brea", "break", class_run, 1);
15948
c906108c
SS
15949 if (dbx_commands)
15950 {
1bedd215
AC
15951 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15952Break in function/address or break at a line in the current file."),
c5aa993b
JM
15953 &stoplist, "stop ", 1, &cmdlist);
15954 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15955 _("Break in function or address."), &stoplist);
c5aa993b 15956 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15957 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
15958 add_com ("status", class_info, breakpoints_info, _("\
15959Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15960The \"Type\" column indicates one of:\n\
15961\tbreakpoint - normal breakpoint\n\
15962\twatchpoint - watchpoint\n\
15963The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15964the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15965breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15966address and file/line number respectively.\n\
15967\n\
15968Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15969are set to the address of the last breakpoint listed unless the command\n\
15970is prefixed with \"server \".\n\n\
c906108c 15971Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15972breakpoint set."));
c906108c
SS
15973 }
15974
1bedd215 15975 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 15976Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15977The \"Type\" column indicates one of:\n\
15978\tbreakpoint - normal breakpoint\n\
15979\twatchpoint - watchpoint\n\
15980The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15981the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15982breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15983address and file/line number respectively.\n\
15984\n\
15985Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15986are set to the address of the last breakpoint listed unless the command\n\
15987is prefixed with \"server \".\n\n\
c906108c 15988Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15989breakpoint set."));
c906108c 15990
6b04bdb7
MS
15991 add_info_alias ("b", "breakpoints", 1);
15992
1a966eab
AC
15993 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15994Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15995The \"Type\" column indicates one of:\n\
15996\tbreakpoint - normal breakpoint\n\
15997\twatchpoint - watchpoint\n\
15998\tlongjmp - internal breakpoint used to step through longjmp()\n\
15999\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16000\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16001\tfinish - internal breakpoint used by the \"finish\" command\n\
16002The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16003the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16004breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16005address and file/line number respectively.\n\
16006\n\
16007Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16008are set to the address of the last breakpoint listed unless the command\n\
16009is prefixed with \"server \".\n\n\
c906108c 16010Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16011breakpoint set."),
c906108c
SS
16012 &maintenanceinfolist);
16013
44feb3ce
TT
16014 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16015Set catchpoints to catch events."),
16016 &catch_cmdlist, "catch ",
16017 0/*allow-unknown*/, &cmdlist);
16018
16019 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16020Set temporary catchpoints to catch events."),
16021 &tcatch_cmdlist, "tcatch ",
16022 0/*allow-unknown*/, &cmdlist);
16023
44feb3ce
TT
16024 add_catch_command ("fork", _("Catch calls to fork."),
16025 catch_fork_command_1,
a96d9b2e 16026 NULL,
44feb3ce
TT
16027 (void *) (uintptr_t) catch_fork_permanent,
16028 (void *) (uintptr_t) catch_fork_temporary);
16029 add_catch_command ("vfork", _("Catch calls to vfork."),
16030 catch_fork_command_1,
a96d9b2e 16031 NULL,
44feb3ce
TT
16032 (void *) (uintptr_t) catch_vfork_permanent,
16033 (void *) (uintptr_t) catch_vfork_temporary);
16034 add_catch_command ("exec", _("Catch calls to exec."),
16035 catch_exec_command_1,
a96d9b2e
SDJ
16036 NULL,
16037 CATCH_PERMANENT,
16038 CATCH_TEMPORARY);
edcc5120
TT
16039 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16040Usage: catch load [REGEX]\n\
16041If REGEX is given, only stop for libraries matching the regular expression."),
16042 catch_load_command_1,
16043 NULL,
16044 CATCH_PERMANENT,
16045 CATCH_TEMPORARY);
16046 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16047Usage: catch unload [REGEX]\n\
16048If REGEX is given, only stop for libraries matching the regular expression."),
16049 catch_unload_command_1,
16050 NULL,
16051 CATCH_PERMANENT,
16052 CATCH_TEMPORARY);
c5aa993b 16053
1bedd215
AC
16054 c = add_com ("watch", class_breakpoint, watch_command, _("\
16055Set a watchpoint for an expression.\n\
06a64a0b 16056Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16057A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16058an expression changes.\n\
16059If -l or -location is given, this evaluates EXPRESSION and watches\n\
16060the memory to which it refers."));
65d12d83 16061 set_cmd_completer (c, expression_completer);
c906108c 16062
1bedd215
AC
16063 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16064Set a read watchpoint for an expression.\n\
06a64a0b 16065Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16066A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16067an expression is read.\n\
16068If -l or -location is given, this evaluates EXPRESSION and watches\n\
16069the memory to which it refers."));
65d12d83 16070 set_cmd_completer (c, expression_completer);
c906108c 16071
1bedd215
AC
16072 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16073Set a watchpoint for an expression.\n\
06a64a0b 16074Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16075A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16076an expression is either read or written.\n\
16077If -l or -location is given, this evaluates EXPRESSION and watches\n\
16078the memory to which it refers."));
65d12d83 16079 set_cmd_completer (c, expression_completer);
c906108c 16080
d77f58be 16081 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16082Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16083
920d2a44
AC
16084 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16085 respond to changes - contrary to the description. */
85c07804
AC
16086 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16087 &can_use_hw_watchpoints, _("\
16088Set debugger's willingness to use watchpoint hardware."), _("\
16089Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16090If zero, gdb will not use hardware for new watchpoints, even if\n\
16091such is available. (However, any hardware watchpoints that were\n\
16092created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16093hardware.)"),
16094 NULL,
920d2a44 16095 show_can_use_hw_watchpoints,
85c07804 16096 &setlist, &showlist);
c906108c
SS
16097
16098 can_use_hw_watchpoints = 1;
fa8d40ab 16099
1042e4c0
SS
16100 /* Tracepoint manipulation commands. */
16101
16102 c = add_com ("trace", class_breakpoint, trace_command, _("\
16103Set a tracepoint at specified line or function.\n\
16104\n"
16105BREAK_ARGS_HELP ("trace") "\n\
16106Do \"help tracepoints\" for info on other tracepoint commands."));
16107 set_cmd_completer (c, location_completer);
16108
16109 add_com_alias ("tp", "trace", class_alias, 0);
16110 add_com_alias ("tr", "trace", class_alias, 1);
16111 add_com_alias ("tra", "trace", class_alias, 1);
16112 add_com_alias ("trac", "trace", class_alias, 1);
16113
7a697b8d
SS
16114 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16115Set a fast tracepoint at specified line or function.\n\
16116\n"
16117BREAK_ARGS_HELP ("ftrace") "\n\
16118Do \"help tracepoints\" for info on other tracepoint commands."));
16119 set_cmd_completer (c, location_completer);
16120
0fb4aa4b
PA
16121 c = add_com ("strace", class_breakpoint, strace_command, _("\
16122Set a static tracepoint at specified line, function or marker.\n\
16123\n\
16124strace [LOCATION] [if CONDITION]\n\
16125LOCATION may be a line number, function name, \"*\" and an address,\n\
16126or -m MARKER_ID.\n\
16127If a line number is specified, probe the marker at start of code\n\
16128for that line. If a function is specified, probe the marker at start\n\
16129of code for that function. If an address is specified, probe the marker\n\
16130at that exact address. If a marker id is specified, probe the marker\n\
16131with that name. With no LOCATION, uses current execution address of\n\
16132the selected stack frame.\n\
16133Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16134This collects arbitrary user data passed in the probe point call to the\n\
16135tracing library. You can inspect it when analyzing the trace buffer,\n\
16136by printing the $_sdata variable like any other convenience variable.\n\
16137\n\
16138CONDITION is a boolean expression.\n\
16139\n\
d41c0fc8
PA
16140Multiple tracepoints at one place are permitted, and useful if their\n\
16141conditions are different.\n\
0fb4aa4b
PA
16142\n\
16143Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16144Do \"help tracepoints\" for info on other tracepoint commands."));
16145 set_cmd_completer (c, location_completer);
16146
1042e4c0 16147 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16148Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16149Convenience variable \"$tpnum\" contains the number of the\n\
16150last tracepoint set."));
16151
16152 add_info_alias ("tp", "tracepoints", 1);
16153
16154 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16155Delete specified tracepoints.\n\
16156Arguments are tracepoint numbers, separated by spaces.\n\
16157No argument means delete all tracepoints."),
16158 &deletelist);
7e20dfcd 16159 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16160
16161 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16162Disable specified tracepoints.\n\
16163Arguments are tracepoint numbers, separated by spaces.\n\
16164No argument means disable all tracepoints."),
16165 &disablelist);
16166 deprecate_cmd (c, "disable");
16167
16168 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16169Enable specified tracepoints.\n\
16170Arguments are tracepoint numbers, separated by spaces.\n\
16171No argument means enable all tracepoints."),
16172 &enablelist);
16173 deprecate_cmd (c, "enable");
16174
16175 add_com ("passcount", class_trace, trace_pass_command, _("\
16176Set the passcount for a tracepoint.\n\
16177The trace will end when the tracepoint has been passed 'count' times.\n\
16178Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16179if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16180
6149aea9
PA
16181 add_prefix_cmd ("save", class_breakpoint, save_command,
16182 _("Save breakpoint definitions as a script."),
16183 &save_cmdlist, "save ",
16184 0/*allow-unknown*/, &cmdlist);
16185
16186 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16187Save current breakpoint definitions as a script.\n\
cce7e648 16188This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16189catchpoints, tracepoints). Use the 'source' command in another debug\n\
16190session to restore them."),
16191 &save_cmdlist);
16192 set_cmd_completer (c, filename_completer);
16193
16194 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16195Save current tracepoint definitions as a script.\n\
6149aea9
PA
16196Use the 'source' command in another debug session to restore them."),
16197 &save_cmdlist);
1042e4c0
SS
16198 set_cmd_completer (c, filename_completer);
16199
6149aea9
PA
16200 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16201 deprecate_cmd (c, "save tracepoints");
16202
1bedd215 16203 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16204Breakpoint specific settings\n\
16205Configure various breakpoint-specific variables such as\n\
1bedd215 16206pending breakpoint behavior"),
fa8d40ab
JJ
16207 &breakpoint_set_cmdlist, "set breakpoint ",
16208 0/*allow-unknown*/, &setlist);
1bedd215 16209 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16210Breakpoint specific settings\n\
16211Configure various breakpoint-specific variables such as\n\
1bedd215 16212pending breakpoint behavior"),
fa8d40ab
JJ
16213 &breakpoint_show_cmdlist, "show breakpoint ",
16214 0/*allow-unknown*/, &showlist);
16215
7915a72c
AC
16216 add_setshow_auto_boolean_cmd ("pending", no_class,
16217 &pending_break_support, _("\
16218Set debugger's behavior regarding pending breakpoints."), _("\
16219Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16220If on, an unrecognized breakpoint location will cause gdb to create a\n\
16221pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16222an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16223user-query to see if a pending breakpoint should be created."),
2c5b56ce 16224 NULL,
920d2a44 16225 show_pending_break_support,
6e1d7d6c
AC
16226 &breakpoint_set_cmdlist,
16227 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16228
16229 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16230
16231 add_setshow_boolean_cmd ("auto-hw", no_class,
16232 &automatic_hardware_breakpoints, _("\
16233Set automatic usage of hardware breakpoints."), _("\
16234Show automatic usage of hardware breakpoints."), _("\
16235If set, the debugger will automatically use hardware breakpoints for\n\
16236breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16237a warning will be emitted for such breakpoints."),
16238 NULL,
16239 show_automatic_hardware_breakpoints,
16240 &breakpoint_set_cmdlist,
16241 &breakpoint_show_cmdlist);
74960c60 16242
a25a5a45
PA
16243 add_setshow_boolean_cmd ("always-inserted", class_support,
16244 &always_inserted_mode, _("\
74960c60
VP
16245Set mode for inserting breakpoints."), _("\
16246Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16247When this mode is on, breakpoints are inserted immediately as soon as\n\
16248they're created, kept inserted even when execution stops, and removed\n\
16249only when the user deletes them. When this mode is off (the default),\n\
16250breakpoints are inserted only when execution continues, and removed\n\
16251when execution stops."),
72d0e2c5
YQ
16252 NULL,
16253 &show_always_inserted_mode,
16254 &breakpoint_set_cmdlist,
16255 &breakpoint_show_cmdlist);
f1310107 16256
b775012e
LM
16257 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16258 condition_evaluation_enums,
16259 &condition_evaluation_mode_1, _("\
16260Set mode of breakpoint condition evaluation."), _("\
16261Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16262When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16263evaluated on the host's side by GDB. When it is set to \"target\",\n\
16264breakpoint conditions will be downloaded to the target (if the target\n\
16265supports such feature) and conditions will be evaluated on the target's side.\n\
16266If this is set to \"auto\" (default), this will be automatically set to\n\
16267\"target\" if it supports condition evaluation, otherwise it will\n\
16268be set to \"gdb\""),
16269 &set_condition_evaluation_mode,
16270 &show_condition_evaluation_mode,
16271 &breakpoint_set_cmdlist,
16272 &breakpoint_show_cmdlist);
16273
f1310107
TJB
16274 add_com ("break-range", class_breakpoint, break_range_command, _("\
16275Set a breakpoint for an address range.\n\
16276break-range START-LOCATION, END-LOCATION\n\
16277where START-LOCATION and END-LOCATION can be one of the following:\n\
16278 LINENUM, for that line in the current file,\n\
16279 FILE:LINENUM, for that line in that file,\n\
16280 +OFFSET, for that number of lines after the current line\n\
16281 or the start of the range\n\
16282 FUNCTION, for the first line in that function,\n\
16283 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16284 *ADDRESS, for the instruction at that address.\n\
16285\n\
16286The breakpoint will stop execution of the inferior whenever it executes\n\
16287an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16288range (including START-LOCATION and END-LOCATION)."));
16289
e7e0cddf
SS
16290 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16291Set a dynamic printf at specified line or function.\n\
16292dprintf location,format string,arg1,arg2,...\n\
16293location may be a line number, function name, or \"*\" and an address.\n\
16294If a line number is specified, break at start of code for that line.\n\
0e4777df 16295If a function is specified, break at start of code for that function."));
e7e0cddf
SS
16296 set_cmd_completer (c, location_completer);
16297
16298 add_setshow_enum_cmd ("dprintf-style", class_support,
16299 dprintf_style_enums, &dprintf_style, _("\
16300Set the style of usage for dynamic printf."), _("\
16301Show the style of usage for dynamic printf."), _("\
16302This setting chooses how GDB will do a dynamic printf.\n\
16303If the value is \"gdb\", then the printing is done by GDB to its own\n\
16304console, as with the \"printf\" command.\n\
16305If the value is \"call\", the print is done by calling a function in your\n\
16306program; by default printf(), but you can choose a different function or\n\
16307output stream by setting dprintf-function and dprintf-channel."),
16308 update_dprintf_commands, NULL,
16309 &setlist, &showlist);
16310
16311 dprintf_function = xstrdup ("printf");
16312 add_setshow_string_cmd ("dprintf-function", class_support,
16313 &dprintf_function, _("\
16314Set the function to use for dynamic printf"), _("\
16315Show the function to use for dynamic printf"), NULL,
16316 update_dprintf_commands, NULL,
16317 &setlist, &showlist);
16318
16319 dprintf_channel = xstrdup ("");
16320 add_setshow_string_cmd ("dprintf-channel", class_support,
16321 &dprintf_channel, _("\
16322Set the channel to use for dynamic printf"), _("\
16323Show the channel to use for dynamic printf"), NULL,
16324 update_dprintf_commands, NULL,
16325 &setlist, &showlist);
16326
d3ce09f5
SS
16327 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16328 &disconnected_dprintf, _("\
16329Set whether dprintf continues after GDB disconnects."), _("\
16330Show whether dprintf continues after GDB disconnects."), _("\
16331Use this to let dprintf commands continue to hit and produce output\n\
16332even if GDB disconnects or detaches from the target."),
16333 NULL,
16334 NULL,
16335 &setlist, &showlist);
16336
16337 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16338agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16339(target agent only) This is useful for formatted output in user-defined commands."));
16340
765dc015 16341 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16342
16343 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16344 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16345}
This page took 2.660486 seconds and 4 git commands to generate.