Fix problems with finishing a dummy function call on simulators.
[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
c9cf6e20
MG
1840 /* If we're at a point where the stack has been destroyed
1841 (e.g. in a function epilogue), unwinding may not work
1842 properly. Do not attempt to recreate locations at this
b5db5dfc 1843 point. See similar comments in watchpoint_check. */
c9cf6e20 1844 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1845 return;
66076460
DJ
1846
1847 /* Save the current frame's ID so we can restore it after
1848 evaluating the watchpoint expression on its own frame. */
1849 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1850 took a frame parameter, so that we didn't have to change the
1851 selected frame. */
1852 frame_saved = 1;
1853 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1854
a5606eee
VP
1855 fi = frame_find_by_id (b->watchpoint_frame);
1856 within_current_scope = (fi != NULL);
1857 if (within_current_scope)
1858 select_frame (fi);
1859 }
1860
b5db5dfc
UW
1861 /* We don't free locations. They are stored in the bp_location array
1862 and update_global_location_list will eventually delete them and
1863 remove breakpoints if needed. */
3a5c3e22 1864 b->base.loc = NULL;
b5db5dfc 1865
a5606eee
VP
1866 if (within_current_scope && reparse)
1867 {
bbc13ae3 1868 const char *s;
d63d0675 1869
a5606eee
VP
1870 if (b->exp)
1871 {
1872 xfree (b->exp);
1873 b->exp = NULL;
1874 }
d63d0675 1875 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1876 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1877 /* If the meaning of expression itself changed, the old value is
1878 no longer relevant. We don't want to report a watchpoint hit
1879 to the user when the old value and the new value may actually
1880 be completely different objects. */
1881 value_free (b->val);
fa4727a6
DJ
1882 b->val = NULL;
1883 b->val_valid = 0;
60e1c644
PA
1884
1885 /* Note that unlike with breakpoints, the watchpoint's condition
1886 expression is stored in the breakpoint object, not in the
1887 locations (re)created below. */
3a5c3e22 1888 if (b->base.cond_string != NULL)
60e1c644
PA
1889 {
1890 if (b->cond_exp != NULL)
1891 {
1892 xfree (b->cond_exp);
1893 b->cond_exp = NULL;
1894 }
1895
3a5c3e22 1896 s = b->base.cond_string;
1bb9788d 1897 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1898 }
a5606eee 1899 }
a5606eee
VP
1900
1901 /* If we failed to parse the expression, for example because
1902 it refers to a global variable in a not-yet-loaded shared library,
1903 don't try to insert watchpoint. We don't automatically delete
1904 such watchpoint, though, since failure to parse expression
1905 is different from out-of-scope watchpoint. */
e8369a73 1906 if (!target_has_execution)
2d134ed3
PA
1907 {
1908 /* Without execution, memory can't change. No use to try and
1909 set watchpoint locations. The watchpoint will be reset when
1910 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1911 if (!can_use_hw_watchpoints)
1912 {
1913 if (b->base.ops->works_in_software_mode (&b->base))
1914 b->base.type = bp_watchpoint;
1915 else
638aa5a1
AB
1916 error (_("Can't set read/access watchpoint when "
1917 "hardware watchpoints are disabled."));
e8369a73 1918 }
2d134ed3
PA
1919 }
1920 else if (within_current_scope && b->exp)
a5606eee 1921 {
0cf6dd15 1922 int pc = 0;
fa4727a6 1923 struct value *val_chain, *v, *result, *next;
2d134ed3 1924 struct program_space *frame_pspace;
a5606eee 1925
3a1115a0 1926 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1927
a5606eee
VP
1928 /* Avoid setting b->val if it's already set. The meaning of
1929 b->val is 'the last value' user saw, and we should update
1930 it only if we reported that last value to user. As it
9c06b0b4
TJB
1931 happens, the code that reports it updates b->val directly.
1932 We don't keep track of the memory value for masked
1933 watchpoints. */
3a5c3e22 1934 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1935 {
bb9d5f81
PP
1936 if (b->val_bitsize != 0)
1937 {
1938 v = extract_bitfield_from_watchpoint_value (b, v);
1939 if (v != NULL)
1940 release_value (v);
1941 }
fa4727a6
DJ
1942 b->val = v;
1943 b->val_valid = 1;
1944 }
a5606eee 1945
2d134ed3
PA
1946 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1947
a5606eee 1948 /* Look at each value on the value chain. */
9fa40276 1949 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1950 {
1951 /* If it's a memory location, and GDB actually needed
1952 its contents to evaluate the expression, then we
fa4727a6
DJ
1953 must watch it. If the first value returned is
1954 still lazy, that means an error occurred reading it;
1955 watch it anyway in case it becomes readable. */
a5606eee 1956 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1957 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1958 {
1959 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1960
a5606eee
VP
1961 /* We only watch structs and arrays if user asked
1962 for it explicitly, never if they just happen to
1963 appear in the middle of some value chain. */
fa4727a6 1964 if (v == result
a5606eee
VP
1965 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1966 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1967 {
1968 CORE_ADDR addr;
744a8059 1969 int type;
a5606eee 1970 struct bp_location *loc, **tmp;
bb9d5f81
PP
1971 int bitpos = 0, bitsize = 0;
1972
1973 if (value_bitsize (v) != 0)
1974 {
1975 /* Extract the bit parameters out from the bitfield
1976 sub-expression. */
1977 bitpos = value_bitpos (v);
1978 bitsize = value_bitsize (v);
1979 }
1980 else if (v == result && b->val_bitsize != 0)
1981 {
1982 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1983 lvalue whose bit parameters are saved in the fields
1984 VAL_BITPOS and VAL_BITSIZE. */
1985 bitpos = b->val_bitpos;
1986 bitsize = b->val_bitsize;
1987 }
a5606eee 1988
42ae5230 1989 addr = value_address (v);
bb9d5f81
PP
1990 if (bitsize != 0)
1991 {
1992 /* Skip the bytes that don't contain the bitfield. */
1993 addr += bitpos / 8;
1994 }
1995
a5606eee 1996 type = hw_write;
3a5c3e22 1997 if (b->base.type == bp_read_watchpoint)
a5606eee 1998 type = hw_read;
3a5c3e22 1999 else if (b->base.type == bp_access_watchpoint)
a5606eee 2000 type = hw_access;
3a5c3e22
PA
2001
2002 loc = allocate_bp_location (&b->base);
2003 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2004 ;
2005 *tmp = loc;
a6d9a66e 2006 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2007
2008 loc->pspace = frame_pspace;
a5606eee 2009 loc->address = addr;
bb9d5f81
PP
2010
2011 if (bitsize != 0)
2012 {
2013 /* Just cover the bytes that make up the bitfield. */
2014 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2015 }
2016 else
2017 loc->length = TYPE_LENGTH (value_type (v));
2018
a5606eee
VP
2019 loc->watchpoint_type = type;
2020 }
2021 }
9fa40276
TJB
2022 }
2023
2024 /* Change the type of breakpoint between hardware assisted or
2025 an ordinary watchpoint depending on the hardware support
2026 and free hardware slots. REPARSE is set when the inferior
2027 is started. */
a9634178 2028 if (reparse)
9fa40276 2029 {
e09342b5 2030 int reg_cnt;
9fa40276
TJB
2031 enum bp_loc_type loc_type;
2032 struct bp_location *bl;
a5606eee 2033
a9634178 2034 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2035
2036 if (reg_cnt)
9fa40276
TJB
2037 {
2038 int i, target_resources_ok, other_type_used;
a1398e0c 2039 enum bptype type;
9fa40276 2040
a9634178
TJB
2041 /* Use an exact watchpoint when there's only one memory region to be
2042 watched, and only one debug register is needed to watch it. */
2043 b->exact = target_exact_watchpoints && reg_cnt == 1;
2044
9fa40276 2045 /* We need to determine how many resources are already
e09342b5
TJB
2046 used for all other hardware watchpoints plus this one
2047 to see if we still have enough resources to also fit
a1398e0c
PA
2048 this watchpoint in as well. */
2049
2050 /* If this is a software watchpoint, we try to turn it
2051 to a hardware one -- count resources as if B was of
2052 hardware watchpoint type. */
2053 type = b->base.type;
2054 if (type == bp_watchpoint)
2055 type = bp_hardware_watchpoint;
2056
2057 /* This watchpoint may or may not have been placed on
2058 the list yet at this point (it won't be in the list
2059 if we're trying to create it for the first time,
2060 through watch_command), so always account for it
2061 manually. */
2062
2063 /* Count resources used by all watchpoints except B. */
2064 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2065
2066 /* Add in the resources needed for B. */
2067 i += hw_watchpoint_use_count (&b->base);
2068
2069 target_resources_ok
2070 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2071 if (target_resources_ok <= 0)
a9634178 2072 {
3a5c3e22 2073 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2074
2075 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2076 error (_("Target does not support this type of "
2077 "hardware watchpoint."));
9c06b0b4
TJB
2078 else if (target_resources_ok < 0 && !sw_mode)
2079 error (_("There are not enough available hardware "
2080 "resources for this watchpoint."));
a1398e0c
PA
2081
2082 /* Downgrade to software watchpoint. */
2083 b->base.type = bp_watchpoint;
2084 }
2085 else
2086 {
2087 /* If this was a software watchpoint, we've just
2088 found we have enough resources to turn it to a
2089 hardware watchpoint. Otherwise, this is a
2090 nop. */
2091 b->base.type = type;
a9634178 2092 }
9fa40276 2093 }
3a5c3e22 2094 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2095 {
2096 if (!can_use_hw_watchpoints)
2097 error (_("Can't set read/access watchpoint when "
2098 "hardware watchpoints are disabled."));
2099 else
2100 error (_("Expression cannot be implemented with "
2101 "read/access watchpoint."));
2102 }
9fa40276 2103 else
3a5c3e22 2104 b->base.type = bp_watchpoint;
9fa40276 2105
3a5c3e22 2106 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2107 : bp_loc_hardware_watchpoint);
3a5c3e22 2108 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2109 bl->loc_type = loc_type;
2110 }
2111
2112 for (v = val_chain; v; v = next)
2113 {
a5606eee
VP
2114 next = value_next (v);
2115 if (v != b->val)
2116 value_free (v);
2117 }
2118
c7437ca6
PA
2119 /* If a software watchpoint is not watching any memory, then the
2120 above left it without any location set up. But,
2121 bpstat_stop_status requires a location to be able to report
2122 stops, so make sure there's at least a dummy one. */
3a5c3e22 2123 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2124 {
3a5c3e22
PA
2125 struct breakpoint *base = &b->base;
2126 base->loc = allocate_bp_location (base);
2127 base->loc->pspace = frame_pspace;
2128 base->loc->address = -1;
2129 base->loc->length = -1;
2130 base->loc->watchpoint_type = -1;
c7437ca6 2131 }
a5606eee
VP
2132 }
2133 else if (!within_current_scope)
7270d8f2 2134 {
ac74f770
MS
2135 printf_filtered (_("\
2136Watchpoint %d deleted because the program has left the block\n\
2137in which its expression is valid.\n"),
3a5c3e22 2138 b->base.number);
d0fb5eae 2139 watchpoint_del_at_next_stop (b);
7270d8f2 2140 }
a5606eee
VP
2141
2142 /* Restore the selected frame. */
66076460
DJ
2143 if (frame_saved)
2144 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2145}
2146
a5606eee 2147
74960c60 2148/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2149 inserted in the inferior. We don't differentiate the type of BL's owner
2150 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2151 breakpoint_ops is not defined, because in insert_bp_location,
2152 tracepoint's insert_location will not be called. */
74960c60 2153static int
35df4500 2154should_be_inserted (struct bp_location *bl)
74960c60 2155{
35df4500 2156 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2157 return 0;
2158
35df4500 2159 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2160 return 0;
2161
35df4500 2162 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2163 return 0;
2164
f8eba3c6
TT
2165 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2166 return 0;
2167
56710373
PA
2168 /* This is set for example, when we're attached to the parent of a
2169 vfork, and have detached from the child. The child is running
2170 free, and we expect it to do an exec or exit, at which point the
2171 OS makes the parent schedulable again (and the target reports
2172 that the vfork is done). Until the child is done with the shared
2173 memory region, do not insert breakpoints in the parent, otherwise
2174 the child could still trip on the parent's breakpoints. Since
2175 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2176 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2177 return 0;
2178
31e77af2
PA
2179 /* Don't insert a breakpoint if we're trying to step past its
2180 location. */
2181 if ((bl->loc_type == bp_loc_software_breakpoint
2182 || bl->loc_type == bp_loc_hardware_breakpoint)
2183 && stepping_past_instruction_at (bl->pspace->aspace,
2184 bl->address))
e558d7c1
PA
2185 {
2186 if (debug_infrun)
2187 {
2188 fprintf_unfiltered (gdb_stdlog,
2189 "infrun: skipping breakpoint: "
2190 "stepping past insn at: %s\n",
2191 paddress (bl->gdbarch, bl->address));
2192 }
2193 return 0;
2194 }
31e77af2 2195
963f9c80
PA
2196 /* Don't insert watchpoints if we're trying to step past the
2197 instruction that triggered one. */
2198 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2199 && stepping_past_nonsteppable_watchpoint ())
2200 {
2201 if (debug_infrun)
2202 {
2203 fprintf_unfiltered (gdb_stdlog,
2204 "infrun: stepping past non-steppable watchpoint. "
2205 "skipping watchpoint at %s:%d\n",
2206 paddress (bl->gdbarch, bl->address),
2207 bl->length);
2208 }
2209 return 0;
2210 }
2211
74960c60
VP
2212 return 1;
2213}
2214
934709f0
PW
2215/* Same as should_be_inserted but does the check assuming
2216 that the location is not duplicated. */
2217
2218static int
2219unduplicated_should_be_inserted (struct bp_location *bl)
2220{
2221 int result;
2222 const int save_duplicate = bl->duplicate;
2223
2224 bl->duplicate = 0;
2225 result = should_be_inserted (bl);
2226 bl->duplicate = save_duplicate;
2227 return result;
2228}
2229
b775012e
LM
2230/* Parses a conditional described by an expression COND into an
2231 agent expression bytecode suitable for evaluation
2232 by the bytecode interpreter. Return NULL if there was
2233 any error during parsing. */
2234
2235static struct agent_expr *
2236parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2237{
2238 struct agent_expr *aexpr = NULL;
b775012e
LM
2239
2240 if (!cond)
2241 return NULL;
2242
2243 /* We don't want to stop processing, so catch any errors
2244 that may show up. */
492d29ea 2245 TRY
b775012e
LM
2246 {
2247 aexpr = gen_eval_for_expr (scope, cond);
2248 }
2249
492d29ea 2250 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2251 {
2252 /* If we got here, it means the condition could not be parsed to a valid
2253 bytecode expression and thus can't be evaluated on the target's side.
2254 It's no use iterating through the conditions. */
2255 return NULL;
2256 }
492d29ea 2257 END_CATCH
b775012e
LM
2258
2259 /* We have a valid agent expression. */
2260 return aexpr;
2261}
2262
2263/* Based on location BL, create a list of breakpoint conditions to be
2264 passed on to the target. If we have duplicated locations with different
2265 conditions, we will add such conditions to the list. The idea is that the
2266 target will evaluate the list of conditions and will only notify GDB when
2267 one of them is true. */
2268
2269static void
2270build_target_condition_list (struct bp_location *bl)
2271{
2272 struct bp_location **locp = NULL, **loc2p;
2273 int null_condition_or_parse_error = 0;
2274 int modified = bl->needs_update;
2275 struct bp_location *loc;
2276
8b4f3082
PA
2277 /* Release conditions left over from a previous insert. */
2278 VEC_free (agent_expr_p, bl->target_info.conditions);
2279
b775012e
LM
2280 /* This is only meaningful if the target is
2281 evaluating conditions and if the user has
2282 opted for condition evaluation on the target's
2283 side. */
2284 if (gdb_evaluates_breakpoint_condition_p ()
2285 || !target_supports_evaluation_of_breakpoint_conditions ())
2286 return;
2287
2288 /* Do a first pass to check for locations with no assigned
2289 conditions or conditions that fail to parse to a valid agent expression
2290 bytecode. If any of these happen, then it's no use to send conditions
2291 to the target since this location will always trigger and generate a
2292 response back to GDB. */
2293 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2294 {
2295 loc = (*loc2p);
2296 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2297 {
2298 if (modified)
2299 {
2300 struct agent_expr *aexpr;
2301
2302 /* Re-parse the conditions since something changed. In that
2303 case we already freed the condition bytecodes (see
2304 force_breakpoint_reinsertion). We just
2305 need to parse the condition to bytecodes again. */
2306 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2307 loc->cond_bytecode = aexpr;
2308
2309 /* Check if we managed to parse the conditional expression
2310 correctly. If not, we will not send this condition
2311 to the target. */
2312 if (aexpr)
2313 continue;
2314 }
2315
2316 /* If we have a NULL bytecode expression, it means something
2317 went wrong or we have a null condition expression. */
2318 if (!loc->cond_bytecode)
2319 {
2320 null_condition_or_parse_error = 1;
2321 break;
2322 }
2323 }
2324 }
2325
2326 /* If any of these happened, it means we will have to evaluate the conditions
2327 for the location's address on gdb's side. It is no use keeping bytecodes
2328 for all the other duplicate locations, thus we free all of them here.
2329
2330 This is so we have a finer control over which locations' conditions are
2331 being evaluated by GDB or the remote stub. */
2332 if (null_condition_or_parse_error)
2333 {
2334 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2335 {
2336 loc = (*loc2p);
2337 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2338 {
2339 /* Only go as far as the first NULL bytecode is
2340 located. */
2341 if (!loc->cond_bytecode)
2342 return;
2343
2344 free_agent_expr (loc->cond_bytecode);
2345 loc->cond_bytecode = NULL;
2346 }
2347 }
2348 }
2349
2350 /* No NULL conditions or failed bytecode generation. Build a condition list
2351 for this location's address. */
2352 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2353 {
2354 loc = (*loc2p);
2355 if (loc->cond
2356 && is_breakpoint (loc->owner)
2357 && loc->pspace->num == bl->pspace->num
2358 && loc->owner->enable_state == bp_enabled
2359 && loc->enabled)
2360 /* Add the condition to the vector. This will be used later to send the
2361 conditions to the target. */
2362 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2363 loc->cond_bytecode);
2364 }
2365
2366 return;
2367}
2368
d3ce09f5
SS
2369/* Parses a command described by string CMD into an agent expression
2370 bytecode suitable for evaluation by the bytecode interpreter.
2371 Return NULL if there was any error during parsing. */
2372
2373static struct agent_expr *
2374parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2375{
2376 struct cleanup *old_cleanups = 0;
2377 struct expression *expr, **argvec;
2378 struct agent_expr *aexpr = NULL;
bbc13ae3
KS
2379 const char *cmdrest;
2380 const char *format_start, *format_end;
d3ce09f5
SS
2381 struct format_piece *fpieces;
2382 int nargs;
2383 struct gdbarch *gdbarch = get_current_arch ();
2384
2385 if (!cmd)
2386 return NULL;
2387
2388 cmdrest = cmd;
2389
2390 if (*cmdrest == ',')
2391 ++cmdrest;
bbc13ae3 2392 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2393
2394 if (*cmdrest++ != '"')
2395 error (_("No format string following the location"));
2396
2397 format_start = cmdrest;
2398
2399 fpieces = parse_format_string (&cmdrest);
2400
2401 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2402
2403 format_end = cmdrest;
2404
2405 if (*cmdrest++ != '"')
2406 error (_("Bad format string, non-terminated '\"'."));
2407
bbc13ae3 2408 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2409
2410 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2411 error (_("Invalid argument syntax"));
2412
2413 if (*cmdrest == ',')
2414 cmdrest++;
bbc13ae3 2415 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2416
2417 /* For each argument, make an expression. */
2418
2419 argvec = (struct expression **) alloca (strlen (cmd)
2420 * sizeof (struct expression *));
2421
2422 nargs = 0;
2423 while (*cmdrest != '\0')
2424 {
bbc13ae3 2425 const char *cmd1;
d3ce09f5
SS
2426
2427 cmd1 = cmdrest;
2428 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2429 argvec[nargs++] = expr;
2430 cmdrest = cmd1;
2431 if (*cmdrest == ',')
2432 ++cmdrest;
2433 }
2434
2435 /* We don't want to stop processing, so catch any errors
2436 that may show up. */
492d29ea 2437 TRY
d3ce09f5
SS
2438 {
2439 aexpr = gen_printf (scope, gdbarch, 0, 0,
2440 format_start, format_end - format_start,
2441 fpieces, nargs, argvec);
2442 }
492d29ea 2443 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2444 {
2445 /* If we got here, it means the command could not be parsed to a valid
2446 bytecode expression and thus can't be evaluated on the target's side.
2447 It's no use iterating through the other commands. */
492d29ea 2448 aexpr = NULL;
d3ce09f5 2449 }
492d29ea
PA
2450 END_CATCH
2451
2452 do_cleanups (old_cleanups);
d3ce09f5 2453
d3ce09f5
SS
2454 /* We have a valid agent expression, return it. */
2455 return aexpr;
2456}
2457
2458/* Based on location BL, create a list of breakpoint commands to be
2459 passed on to the target. If we have duplicated locations with
2460 different commands, we will add any such to the list. */
2461
2462static void
2463build_target_command_list (struct bp_location *bl)
2464{
2465 struct bp_location **locp = NULL, **loc2p;
2466 int null_command_or_parse_error = 0;
2467 int modified = bl->needs_update;
2468 struct bp_location *loc;
2469
8b4f3082
PA
2470 /* Release commands left over from a previous insert. */
2471 VEC_free (agent_expr_p, bl->target_info.tcommands);
2472
41fac0cf 2473 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2474 return;
2475
41fac0cf
PA
2476 /* For now, limit to agent-style dprintf breakpoints. */
2477 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2478 return;
2479
41fac0cf
PA
2480 /* For now, if we have any duplicate location that isn't a dprintf,
2481 don't install the target-side commands, as that would make the
2482 breakpoint not be reported to the core, and we'd lose
2483 control. */
2484 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2485 {
2486 loc = (*loc2p);
2487 if (is_breakpoint (loc->owner)
2488 && loc->pspace->num == bl->pspace->num
2489 && loc->owner->type != bp_dprintf)
2490 return;
2491 }
2492
d3ce09f5
SS
2493 /* Do a first pass to check for locations with no assigned
2494 conditions or conditions that fail to parse to a valid agent expression
2495 bytecode. If any of these happen, then it's no use to send conditions
2496 to the target since this location will always trigger and generate a
2497 response back to GDB. */
2498 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2499 {
2500 loc = (*loc2p);
2501 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2502 {
2503 if (modified)
2504 {
2505 struct agent_expr *aexpr;
2506
2507 /* Re-parse the commands since something changed. In that
2508 case we already freed the command bytecodes (see
2509 force_breakpoint_reinsertion). We just
2510 need to parse the command to bytecodes again. */
2511 aexpr = parse_cmd_to_aexpr (bl->address,
2512 loc->owner->extra_string);
2513 loc->cmd_bytecode = aexpr;
2514
2515 if (!aexpr)
2516 continue;
2517 }
2518
2519 /* If we have a NULL bytecode expression, it means something
2520 went wrong or we have a null command expression. */
2521 if (!loc->cmd_bytecode)
2522 {
2523 null_command_or_parse_error = 1;
2524 break;
2525 }
2526 }
2527 }
2528
2529 /* If anything failed, then we're not doing target-side commands,
2530 and so clean up. */
2531 if (null_command_or_parse_error)
2532 {
2533 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2534 {
2535 loc = (*loc2p);
2536 if (is_breakpoint (loc->owner)
2537 && loc->pspace->num == bl->pspace->num)
2538 {
2539 /* Only go as far as the first NULL bytecode is
2540 located. */
40fb6c5e 2541 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2542 return;
2543
40fb6c5e
HZ
2544 free_agent_expr (loc->cmd_bytecode);
2545 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2546 }
2547 }
2548 }
2549
2550 /* No NULL commands or failed bytecode generation. Build a command list
2551 for this location's address. */
2552 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2553 {
2554 loc = (*loc2p);
2555 if (loc->owner->extra_string
2556 && is_breakpoint (loc->owner)
2557 && loc->pspace->num == bl->pspace->num
2558 && loc->owner->enable_state == bp_enabled
2559 && loc->enabled)
2560 /* Add the command to the vector. This will be used later
2561 to send the commands to the target. */
2562 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2563 loc->cmd_bytecode);
2564 }
2565
2566 bl->target_info.persist = 0;
2567 /* Maybe flag this location as persistent. */
2568 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2569 bl->target_info.persist = 1;
2570}
2571
35df4500
TJB
2572/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2573 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2574 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2575 Returns 0 for success, 1 if the bp_location type is not supported or
2576 -1 for failure.
879bfdc2 2577
4a64f543
MS
2578 NOTE drow/2003-09-09: This routine could be broken down to an
2579 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2580static int
35df4500 2581insert_bp_location (struct bp_location *bl,
26bb91f3 2582 struct ui_file *tmp_error_stream,
3fbb6ffa 2583 int *disabled_breaks,
dd61ec5c
MW
2584 int *hw_breakpoint_error,
2585 int *hw_bp_error_explained_already)
879bfdc2 2586{
0000e5cc
PA
2587 enum errors bp_err = GDB_NO_ERROR;
2588 const char *bp_err_message = NULL;
879bfdc2 2589
b775012e 2590 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2591 return 0;
2592
35c63cd8
JB
2593 /* Note we don't initialize bl->target_info, as that wipes out
2594 the breakpoint location's shadow_contents if the breakpoint
2595 is still inserted at that location. This in turn breaks
2596 target_read_memory which depends on these buffers when
2597 a memory read is requested at the breakpoint location:
2598 Once the target_info has been wiped, we fail to see that
2599 we have a breakpoint inserted at that address and thus
2600 read the breakpoint instead of returning the data saved in
2601 the breakpoint location's shadow contents. */
0d5ed153 2602 bl->target_info.reqstd_address = bl->address;
35df4500 2603 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2604 bl->target_info.length = bl->length;
8181d85f 2605
b775012e
LM
2606 /* When working with target-side conditions, we must pass all the conditions
2607 for the same breakpoint address down to the target since GDB will not
2608 insert those locations. With a list of breakpoint conditions, the target
2609 can decide when to stop and notify GDB. */
2610
2611 if (is_breakpoint (bl->owner))
2612 {
2613 build_target_condition_list (bl);
d3ce09f5
SS
2614 build_target_command_list (bl);
2615 /* Reset the modification marker. */
b775012e
LM
2616 bl->needs_update = 0;
2617 }
2618
35df4500
TJB
2619 if (bl->loc_type == bp_loc_software_breakpoint
2620 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2621 {
35df4500 2622 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2623 {
2624 /* If the explicitly specified breakpoint type
2625 is not hardware breakpoint, check the memory map to see
2626 if the breakpoint address is in read only memory or not.
4a64f543 2627
765dc015
VP
2628 Two important cases are:
2629 - location type is not hardware breakpoint, memory
2630 is readonly. We change the type of the location to
2631 hardware breakpoint.
4a64f543
MS
2632 - location type is hardware breakpoint, memory is
2633 read-write. This means we've previously made the
2634 location hardware one, but then the memory map changed,
2635 so we undo.
765dc015 2636
4a64f543
MS
2637 When breakpoints are removed, remove_breakpoints will use
2638 location types we've just set here, the only possible
2639 problem is that memory map has changed during running
2640 program, but it's not going to work anyway with current
2641 gdb. */
765dc015 2642 struct mem_region *mr
0d5ed153 2643 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2644
2645 if (mr)
2646 {
2647 if (automatic_hardware_breakpoints)
2648 {
765dc015
VP
2649 enum bp_loc_type new_type;
2650
2651 if (mr->attrib.mode != MEM_RW)
2652 new_type = bp_loc_hardware_breakpoint;
2653 else
2654 new_type = bp_loc_software_breakpoint;
2655
35df4500 2656 if (new_type != bl->loc_type)
765dc015
VP
2657 {
2658 static int said = 0;
cc59ec59 2659
35df4500 2660 bl->loc_type = new_type;
765dc015
VP
2661 if (!said)
2662 {
3e43a32a
MS
2663 fprintf_filtered (gdb_stdout,
2664 _("Note: automatically using "
2665 "hardware breakpoints for "
2666 "read-only addresses.\n"));
765dc015
VP
2667 said = 1;
2668 }
2669 }
2670 }
35df4500 2671 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2672 && mr->attrib.mode != MEM_RW)
2673 {
2674 fprintf_unfiltered (tmp_error_stream,
2675 _("Cannot insert breakpoint %d.\n"
2676 "Cannot set software breakpoint "
2677 "at read-only address %s\n"),
2678 bl->owner->number,
2679 paddress (bl->gdbarch, bl->address));
2680 return 1;
2681 }
765dc015
VP
2682 }
2683 }
2684
879bfdc2
DJ
2685 /* First check to see if we have to handle an overlay. */
2686 if (overlay_debugging == ovly_off
35df4500
TJB
2687 || bl->section == NULL
2688 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2689 {
2690 /* No overlay handling: just set the breakpoint. */
492d29ea 2691 TRY
dd61ec5c 2692 {
0000e5cc
PA
2693 int val;
2694
dd61ec5c 2695 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2696 if (val)
2697 bp_err = GENERIC_ERROR;
dd61ec5c 2698 }
492d29ea 2699 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2700 {
0000e5cc
PA
2701 bp_err = e.error;
2702 bp_err_message = e.message;
dd61ec5c 2703 }
492d29ea 2704 END_CATCH
879bfdc2
DJ
2705 }
2706 else
2707 {
4a64f543 2708 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2709 Shall we set a breakpoint at the LMA? */
2710 if (!overlay_events_enabled)
2711 {
2712 /* Yes -- overlay event support is not active,
2713 so we must try to set a breakpoint at the LMA.
2714 This will not work for a hardware breakpoint. */
35df4500 2715 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2716 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2717 bl->owner->number);
879bfdc2
DJ
2718 else
2719 {
35df4500
TJB
2720 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2721 bl->section);
879bfdc2 2722 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2723 bl->overlay_target_info = bl->target_info;
0d5ed153 2724 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2725
2726 /* No overlay handling: just set the breakpoint. */
492d29ea 2727 TRY
0000e5cc
PA
2728 {
2729 int val;
2730
2731 val = target_insert_breakpoint (bl->gdbarch,
2732 &bl->overlay_target_info);
2733 if (val)
2734 bp_err = GENERIC_ERROR;
2735 }
492d29ea 2736 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2737 {
2738 bp_err = e.error;
2739 bp_err_message = e.message;
2740 }
492d29ea 2741 END_CATCH
0000e5cc
PA
2742
2743 if (bp_err != GDB_NO_ERROR)
99361f52 2744 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2745 "Overlay breakpoint %d "
2746 "failed: in ROM?\n",
35df4500 2747 bl->owner->number);
879bfdc2
DJ
2748 }
2749 }
2750 /* Shall we set a breakpoint at the VMA? */
35df4500 2751 if (section_is_mapped (bl->section))
879bfdc2
DJ
2752 {
2753 /* Yes. This overlay section is mapped into memory. */
492d29ea 2754 TRY
dd61ec5c 2755 {
0000e5cc
PA
2756 int val;
2757
dd61ec5c 2758 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2759 if (val)
2760 bp_err = GENERIC_ERROR;
dd61ec5c 2761 }
492d29ea 2762 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2763 {
0000e5cc
PA
2764 bp_err = e.error;
2765 bp_err_message = e.message;
dd61ec5c 2766 }
492d29ea 2767 END_CATCH
879bfdc2
DJ
2768 }
2769 else
2770 {
2771 /* No. This breakpoint will not be inserted.
2772 No error, but do not mark the bp as 'inserted'. */
2773 return 0;
2774 }
2775 }
2776
0000e5cc 2777 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2778 {
2779 /* Can't set the breakpoint. */
0000e5cc
PA
2780
2781 /* In some cases, we might not be able to insert a
2782 breakpoint in a shared library that has already been
2783 removed, but we have not yet processed the shlib unload
2784 event. Unfortunately, some targets that implement
076855f9
PA
2785 breakpoint insertion themselves can't tell why the
2786 breakpoint insertion failed (e.g., the remote target
2787 doesn't define error codes), so we must treat generic
2788 errors as memory errors. */
0000e5cc 2789 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2790 && bl->loc_type == bp_loc_software_breakpoint
08351840 2791 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2792 || shared_objfile_contains_address_p (bl->pspace,
2793 bl->address)))
879bfdc2 2794 {
4a64f543 2795 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2796 bl->shlib_disabled = 1;
8d3788bd 2797 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2798 if (!*disabled_breaks)
2799 {
2800 fprintf_unfiltered (tmp_error_stream,
2801 "Cannot insert breakpoint %d.\n",
2802 bl->owner->number);
2803 fprintf_unfiltered (tmp_error_stream,
2804 "Temporarily disabling shared "
2805 "library breakpoints:\n");
2806 }
2807 *disabled_breaks = 1;
879bfdc2 2808 fprintf_unfiltered (tmp_error_stream,
35df4500 2809 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2810 return 0;
879bfdc2
DJ
2811 }
2812 else
879bfdc2 2813 {
35df4500 2814 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2815 {
0000e5cc
PA
2816 *hw_breakpoint_error = 1;
2817 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2818 fprintf_unfiltered (tmp_error_stream,
2819 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2820 bl->owner->number, bp_err_message ? ":" : ".\n");
2821 if (bp_err_message != NULL)
2822 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2823 }
2824 else
2825 {
0000e5cc
PA
2826 if (bp_err_message == NULL)
2827 {
2828 char *message
2829 = memory_error_message (TARGET_XFER_E_IO,
2830 bl->gdbarch, bl->address);
2831 struct cleanup *old_chain = make_cleanup (xfree, message);
2832
2833 fprintf_unfiltered (tmp_error_stream,
2834 "Cannot insert breakpoint %d.\n"
2835 "%s\n",
2836 bl->owner->number, message);
2837 do_cleanups (old_chain);
2838 }
2839 else
2840 {
2841 fprintf_unfiltered (tmp_error_stream,
2842 "Cannot insert breakpoint %d: %s\n",
2843 bl->owner->number,
2844 bp_err_message);
2845 }
879bfdc2 2846 }
0000e5cc 2847 return 1;
879bfdc2
DJ
2848
2849 }
2850 }
2851 else
35df4500 2852 bl->inserted = 1;
879bfdc2 2853
0000e5cc 2854 return 0;
879bfdc2
DJ
2855 }
2856
35df4500 2857 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2858 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2859 watchpoints. It's not clear that it's necessary... */
35df4500 2860 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2861 {
0000e5cc
PA
2862 int val;
2863
77b06cd7
TJB
2864 gdb_assert (bl->owner->ops != NULL
2865 && bl->owner->ops->insert_location != NULL);
2866
2867 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2868
2869 /* If trying to set a read-watchpoint, and it turns out it's not
2870 supported, try emulating one with an access watchpoint. */
35df4500 2871 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2872 {
2873 struct bp_location *loc, **loc_temp;
2874
2875 /* But don't try to insert it, if there's already another
2876 hw_access location that would be considered a duplicate
2877 of this one. */
2878 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2879 if (loc != bl
85d721b8 2880 && loc->watchpoint_type == hw_access
35df4500 2881 && watchpoint_locations_match (bl, loc))
85d721b8 2882 {
35df4500
TJB
2883 bl->duplicate = 1;
2884 bl->inserted = 1;
2885 bl->target_info = loc->target_info;
2886 bl->watchpoint_type = hw_access;
85d721b8
PA
2887 val = 0;
2888 break;
2889 }
2890
2891 if (val == 1)
2892 {
77b06cd7
TJB
2893 bl->watchpoint_type = hw_access;
2894 val = bl->owner->ops->insert_location (bl);
2895
2896 if (val)
2897 /* Back to the original value. */
2898 bl->watchpoint_type = hw_read;
85d721b8
PA
2899 }
2900 }
2901
35df4500 2902 bl->inserted = (val == 0);
879bfdc2
DJ
2903 }
2904
35df4500 2905 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2906 {
0000e5cc
PA
2907 int val;
2908
77b06cd7
TJB
2909 gdb_assert (bl->owner->ops != NULL
2910 && bl->owner->ops->insert_location != NULL);
2911
2912 val = bl->owner->ops->insert_location (bl);
2913 if (val)
2914 {
2915 bl->owner->enable_state = bp_disabled;
2916
2917 if (val == 1)
2918 warning (_("\
2919Error inserting catchpoint %d: Your system does not support this type\n\
2920of catchpoint."), bl->owner->number);
2921 else
2922 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2923 }
2924
2925 bl->inserted = (val == 0);
1640b821
DJ
2926
2927 /* We've already printed an error message if there was a problem
2928 inserting this catchpoint, and we've disabled the catchpoint,
2929 so just return success. */
2930 return 0;
879bfdc2
DJ
2931 }
2932
2933 return 0;
2934}
2935
6c95b8df
PA
2936/* This function is called when program space PSPACE is about to be
2937 deleted. It takes care of updating breakpoints to not reference
2938 PSPACE anymore. */
2939
2940void
2941breakpoint_program_space_exit (struct program_space *pspace)
2942{
2943 struct breakpoint *b, *b_temp;
876fa593 2944 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2945
2946 /* Remove any breakpoint that was set through this program space. */
2947 ALL_BREAKPOINTS_SAFE (b, b_temp)
2948 {
2949 if (b->pspace == pspace)
2950 delete_breakpoint (b);
2951 }
2952
2953 /* Breakpoints set through other program spaces could have locations
2954 bound to PSPACE as well. Remove those. */
876fa593 2955 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2956 {
2957 struct bp_location *tmp;
2958
2959 if (loc->pspace == pspace)
2960 {
2bdf28a0 2961 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2962 if (loc->owner->loc == loc)
2963 loc->owner->loc = loc->next;
2964 else
2965 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2966 if (tmp->next == loc)
2967 {
2968 tmp->next = loc->next;
2969 break;
2970 }
2971 }
2972 }
2973
2974 /* Now update the global location list to permanently delete the
2975 removed locations above. */
44702360 2976 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2977}
2978
74960c60
VP
2979/* Make sure all breakpoints are inserted in inferior.
2980 Throws exception on any error.
2981 A breakpoint that is already inserted won't be inserted
2982 again, so calling this function twice is safe. */
2983void
2984insert_breakpoints (void)
2985{
2986 struct breakpoint *bpt;
2987
2988 ALL_BREAKPOINTS (bpt)
2989 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2990 {
2991 struct watchpoint *w = (struct watchpoint *) bpt;
2992
2993 update_watchpoint (w, 0 /* don't reparse. */);
2994 }
74960c60 2995
04086b45
PA
2996 /* Updating watchpoints creates new locations, so update the global
2997 location list. Explicitly tell ugll to insert locations and
2998 ignore breakpoints_always_inserted_mode. */
2999 update_global_location_list (UGLL_INSERT);
74960c60
VP
3000}
3001
20388dd6
YQ
3002/* Invoke CALLBACK for each of bp_location. */
3003
3004void
3005iterate_over_bp_locations (walk_bp_location_callback callback)
3006{
3007 struct bp_location *loc, **loc_tmp;
3008
3009 ALL_BP_LOCATIONS (loc, loc_tmp)
3010 {
3011 callback (loc, NULL);
3012 }
3013}
3014
b775012e
LM
3015/* This is used when we need to synch breakpoint conditions between GDB and the
3016 target. It is the case with deleting and disabling of breakpoints when using
3017 always-inserted mode. */
3018
3019static void
3020update_inserted_breakpoint_locations (void)
3021{
3022 struct bp_location *bl, **blp_tmp;
3023 int error_flag = 0;
3024 int val = 0;
3025 int disabled_breaks = 0;
3026 int hw_breakpoint_error = 0;
dd61ec5c 3027 int hw_bp_details_reported = 0;
b775012e
LM
3028
3029 struct ui_file *tmp_error_stream = mem_fileopen ();
3030 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3031
3032 /* Explicitly mark the warning -- this will only be printed if
3033 there was an error. */
3034 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3035
3036 save_current_space_and_thread ();
3037
3038 ALL_BP_LOCATIONS (bl, blp_tmp)
3039 {
3040 /* We only want to update software breakpoints and hardware
3041 breakpoints. */
3042 if (!is_breakpoint (bl->owner))
3043 continue;
3044
3045 /* We only want to update locations that are already inserted
3046 and need updating. This is to avoid unwanted insertion during
3047 deletion of breakpoints. */
3048 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3049 continue;
3050
3051 switch_to_program_space_and_thread (bl->pspace);
3052
3053 /* For targets that support global breakpoints, there's no need
3054 to select an inferior to insert breakpoint to. In fact, even
3055 if we aren't attached to any process yet, we should still
3056 insert breakpoints. */
f5656ead 3057 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3058 && ptid_equal (inferior_ptid, null_ptid))
3059 continue;
3060
3061 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3062 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3063 if (val)
3064 error_flag = val;
3065 }
3066
3067 if (error_flag)
3068 {
3069 target_terminal_ours_for_output ();
3070 error_stream (tmp_error_stream);
3071 }
3072
3073 do_cleanups (cleanups);
3074}
3075
c30eee59 3076/* Used when starting or continuing the program. */
c906108c 3077
74960c60
VP
3078static void
3079insert_breakpoint_locations (void)
c906108c 3080{
a5606eee 3081 struct breakpoint *bpt;
35df4500 3082 struct bp_location *bl, **blp_tmp;
eacd795a 3083 int error_flag = 0;
c906108c 3084 int val = 0;
3fbb6ffa 3085 int disabled_breaks = 0;
81d0cc19 3086 int hw_breakpoint_error = 0;
dd61ec5c 3087 int hw_bp_error_explained_already = 0;
c906108c 3088
81d0cc19 3089 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3090 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3091
81d0cc19
GS
3092 /* Explicitly mark the warning -- this will only be printed if
3093 there was an error. */
3094 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3095
3096 save_current_space_and_thread ();
3097
35df4500 3098 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3099 {
b775012e 3100 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3101 continue;
3102
4a64f543
MS
3103 /* There is no point inserting thread-specific breakpoints if
3104 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3105 has BL->OWNER always non-NULL. */
35df4500
TJB
3106 if (bl->owner->thread != -1
3107 && !valid_thread_id (bl->owner->thread))
f365de73
AS
3108 continue;
3109
35df4500 3110 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3111
3112 /* For targets that support global breakpoints, there's no need
3113 to select an inferior to insert breakpoint to. In fact, even
3114 if we aren't attached to any process yet, we should still
3115 insert breakpoints. */
f5656ead 3116 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3117 && ptid_equal (inferior_ptid, null_ptid))
3118 continue;
3119
3fbb6ffa 3120 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3121 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3122 if (val)
eacd795a 3123 error_flag = val;
879bfdc2 3124 }
c906108c 3125
4a64f543
MS
3126 /* If we failed to insert all locations of a watchpoint, remove
3127 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3128 ALL_BREAKPOINTS (bpt)
3129 {
3130 int some_failed = 0;
3131 struct bp_location *loc;
3132
3133 if (!is_hardware_watchpoint (bpt))
3134 continue;
3135
d6b74ac4 3136 if (!breakpoint_enabled (bpt))
a5606eee 3137 continue;
74960c60
VP
3138
3139 if (bpt->disposition == disp_del_at_next_stop)
3140 continue;
a5606eee
VP
3141
3142 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3143 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3144 {
3145 some_failed = 1;
3146 break;
3147 }
3148 if (some_failed)
3149 {
3150 for (loc = bpt->loc; loc; loc = loc->next)
3151 if (loc->inserted)
3152 remove_breakpoint (loc, mark_uninserted);
3153
3154 hw_breakpoint_error = 1;
3155 fprintf_unfiltered (tmp_error_stream,
3156 "Could not insert hardware watchpoint %d.\n",
3157 bpt->number);
eacd795a 3158 error_flag = -1;
a5606eee
VP
3159 }
3160 }
3161
eacd795a 3162 if (error_flag)
81d0cc19
GS
3163 {
3164 /* If a hardware breakpoint or watchpoint was inserted, add a
3165 message about possibly exhausted resources. */
dd61ec5c 3166 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3167 {
c6510018
MS
3168 fprintf_unfiltered (tmp_error_stream,
3169 "Could not insert hardware breakpoints:\n\
3170You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3171 }
81d0cc19
GS
3172 target_terminal_ours_for_output ();
3173 error_stream (tmp_error_stream);
3174 }
f7545552
TT
3175
3176 do_cleanups (cleanups);
c906108c
SS
3177}
3178
c30eee59
TJB
3179/* Used when the program stops.
3180 Returns zero if successful, or non-zero if there was a problem
3181 removing a breakpoint location. */
3182
c906108c 3183int
fba45db2 3184remove_breakpoints (void)
c906108c 3185{
35df4500 3186 struct bp_location *bl, **blp_tmp;
3a1bae8e 3187 int val = 0;
c906108c 3188
35df4500 3189 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3190 {
1e4d1764 3191 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3192 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3193 }
3a1bae8e 3194 return val;
c906108c
SS
3195}
3196
49fa26b0
PA
3197/* When a thread exits, remove breakpoints that are related to
3198 that thread. */
3199
3200static void
3201remove_threaded_breakpoints (struct thread_info *tp, int silent)
3202{
3203 struct breakpoint *b, *b_tmp;
3204
3205 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3206 {
96181529 3207 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3208 {
3209 b->disposition = disp_del_at_next_stop;
3210
3211 printf_filtered (_("\
46ecd527 3212Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3213 b->number, tp->num);
3214
3215 /* Hide it from the user. */
3216 b->number = 0;
3217 }
3218 }
3219}
3220
6c95b8df
PA
3221/* Remove breakpoints of process PID. */
3222
3223int
3224remove_breakpoints_pid (int pid)
3225{
35df4500 3226 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3227 int val;
3228 struct inferior *inf = find_inferior_pid (pid);
3229
35df4500 3230 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3231 {
35df4500 3232 if (bl->pspace != inf->pspace)
6c95b8df
PA
3233 continue;
3234
fc126975 3235 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3236 {
35df4500 3237 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3238 if (val != 0)
3239 return val;
3240 }
3241 }
3242 return 0;
3243}
3244
c906108c 3245int
fba45db2 3246reattach_breakpoints (int pid)
c906108c 3247{
6c95b8df 3248 struct cleanup *old_chain;
35df4500 3249 struct bp_location *bl, **blp_tmp;
c906108c 3250 int val;
86b887df 3251 struct ui_file *tmp_error_stream;
dd61ec5c 3252 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3253 struct inferior *inf;
3254 struct thread_info *tp;
3255
3256 tp = any_live_thread_of_process (pid);
3257 if (tp == NULL)
3258 return 1;
3259
3260 inf = find_inferior_pid (pid);
3261 old_chain = save_inferior_ptid ();
3262
3263 inferior_ptid = tp->ptid;
a4954f26 3264
86b887df 3265 tmp_error_stream = mem_fileopen ();
a4954f26 3266 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3267
35df4500 3268 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3269 {
35df4500 3270 if (bl->pspace != inf->pspace)
6c95b8df
PA
3271 continue;
3272
35df4500 3273 if (bl->inserted)
c5aa993b 3274 {
35df4500 3275 bl->inserted = 0;
dd61ec5c 3276 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3277 if (val != 0)
3278 {
ce696e05 3279 do_cleanups (old_chain);
c5aa993b
JM
3280 return val;
3281 }
3282 }
3283 }
ce696e05 3284 do_cleanups (old_chain);
c906108c
SS
3285 return 0;
3286}
3287
e58b0e63
PA
3288static int internal_breakpoint_number = -1;
3289
84f4c1fe
PM
3290/* Set the breakpoint number of B, depending on the value of INTERNAL.
3291 If INTERNAL is non-zero, the breakpoint number will be populated
3292 from internal_breakpoint_number and that variable decremented.
e5dd4106 3293 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3294 breakpoint_count and that value incremented. Internal breakpoints
3295 do not set the internal var bpnum. */
3296static void
3297set_breakpoint_number (int internal, struct breakpoint *b)
3298{
3299 if (internal)
3300 b->number = internal_breakpoint_number--;
3301 else
3302 {
3303 set_breakpoint_count (breakpoint_count + 1);
3304 b->number = breakpoint_count;
3305 }
3306}
3307
e62c965a 3308static struct breakpoint *
a6d9a66e 3309create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3310 CORE_ADDR address, enum bptype type,
c0a91b2b 3311 const struct breakpoint_ops *ops)
e62c965a 3312{
e62c965a
PP
3313 struct symtab_and_line sal;
3314 struct breakpoint *b;
3315
4a64f543 3316 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3317
3318 sal.pc = address;
3319 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3320 sal.pspace = current_program_space;
e62c965a 3321
06edf0c0 3322 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3323 b->number = internal_breakpoint_number--;
3324 b->disposition = disp_donttouch;
3325
3326 return b;
3327}
3328
17450429
PP
3329static const char *const longjmp_names[] =
3330 {
3331 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3332 };
3333#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3334
3335/* Per-objfile data private to breakpoint.c. */
3336struct breakpoint_objfile_data
3337{
3338 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3339 struct bound_minimal_symbol overlay_msym;
17450429
PP
3340
3341 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3342 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3343
28106bc2
SDJ
3344 /* True if we have looked for longjmp probes. */
3345 int longjmp_searched;
3346
3347 /* SystemTap probe points for longjmp (if any). */
3348 VEC (probe_p) *longjmp_probes;
3349
17450429 3350 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3351 struct bound_minimal_symbol terminate_msym;
17450429
PP
3352
3353 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3354 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3355
3356 /* True if we have looked for exception probes. */
3357 int exception_searched;
3358
3359 /* SystemTap probe points for unwinding (if any). */
3360 VEC (probe_p) *exception_probes;
17450429
PP
3361};
3362
3363static const struct objfile_data *breakpoint_objfile_key;
3364
3365/* Minimal symbol not found sentinel. */
3366static struct minimal_symbol msym_not_found;
3367
3368/* Returns TRUE if MSYM point to the "not found" sentinel. */
3369
3370static int
3371msym_not_found_p (const struct minimal_symbol *msym)
3372{
3373 return msym == &msym_not_found;
3374}
3375
3376/* Return per-objfile data needed by breakpoint.c.
3377 Allocate the data if necessary. */
3378
3379static struct breakpoint_objfile_data *
3380get_breakpoint_objfile_data (struct objfile *objfile)
3381{
3382 struct breakpoint_objfile_data *bp_objfile_data;
3383
3384 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3385 if (bp_objfile_data == NULL)
3386 {
3387 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3388 sizeof (*bp_objfile_data));
3389
3390 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3391 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3392 }
3393 return bp_objfile_data;
3394}
3395
28106bc2
SDJ
3396static void
3397free_breakpoint_probes (struct objfile *obj, void *data)
3398{
3399 struct breakpoint_objfile_data *bp_objfile_data = data;
3400
3401 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3402 VEC_free (probe_p, bp_objfile_data->exception_probes);
3403}
3404
e62c965a 3405static void
af02033e 3406create_overlay_event_breakpoint (void)
e62c965a 3407{
69de3c6a 3408 struct objfile *objfile;
af02033e 3409 const char *const func_name = "_ovly_debug_event";
e62c965a 3410
69de3c6a
PP
3411 ALL_OBJFILES (objfile)
3412 {
3413 struct breakpoint *b;
17450429
PP
3414 struct breakpoint_objfile_data *bp_objfile_data;
3415 CORE_ADDR addr;
69de3c6a 3416
17450429
PP
3417 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3418
3b7344d5 3419 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3420 continue;
3421
3b7344d5 3422 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3423 {
3b7344d5 3424 struct bound_minimal_symbol m;
17450429
PP
3425
3426 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3427 if (m.minsym == NULL)
17450429
PP
3428 {
3429 /* Avoid future lookups in this objfile. */
3b7344d5 3430 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3431 continue;
3432 }
3433 bp_objfile_data->overlay_msym = m;
3434 }
e62c965a 3435
77e371c0 3436 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3437 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3438 bp_overlay_event,
3439 &internal_breakpoint_ops);
69de3c6a 3440 b->addr_string = xstrdup (func_name);
e62c965a 3441
69de3c6a
PP
3442 if (overlay_debugging == ovly_auto)
3443 {
3444 b->enable_state = bp_enabled;
3445 overlay_events_enabled = 1;
3446 }
3447 else
3448 {
3449 b->enable_state = bp_disabled;
3450 overlay_events_enabled = 0;
3451 }
e62c965a 3452 }
44702360 3453 update_global_location_list (UGLL_MAY_INSERT);
e62c965a
PP
3454}
3455
0fd8e87f 3456static void
af02033e 3457create_longjmp_master_breakpoint (void)
0fd8e87f 3458{
6c95b8df 3459 struct program_space *pspace;
6c95b8df
PA
3460 struct cleanup *old_chain;
3461
3462 old_chain = save_current_program_space ();
0fd8e87f 3463
6c95b8df 3464 ALL_PSPACES (pspace)
af02033e
PP
3465 {
3466 struct objfile *objfile;
3467
3468 set_current_program_space (pspace);
3469
3470 ALL_OBJFILES (objfile)
0fd8e87f 3471 {
af02033e
PP
3472 int i;
3473 struct gdbarch *gdbarch;
17450429 3474 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3475
af02033e 3476 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3477
17450429
PP
3478 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3479
28106bc2
SDJ
3480 if (!bp_objfile_data->longjmp_searched)
3481 {
25f9533e
SDJ
3482 VEC (probe_p) *ret;
3483
3484 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3485 if (ret != NULL)
3486 {
3487 /* We are only interested in checking one element. */
3488 struct probe *p = VEC_index (probe_p, ret, 0);
3489
3490 if (!can_evaluate_probe_arguments (p))
3491 {
3492 /* We cannot use the probe interface here, because it does
3493 not know how to evaluate arguments. */
3494 VEC_free (probe_p, ret);
3495 ret = NULL;
3496 }
3497 }
3498 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3499 bp_objfile_data->longjmp_searched = 1;
3500 }
3501
3502 if (bp_objfile_data->longjmp_probes != NULL)
3503 {
3504 int i;
3505 struct probe *probe;
3506 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3507
3508 for (i = 0;
3509 VEC_iterate (probe_p,
3510 bp_objfile_data->longjmp_probes,
3511 i, probe);
3512 ++i)
3513 {
3514 struct breakpoint *b;
3515
729662a5
TT
3516 b = create_internal_breakpoint (gdbarch,
3517 get_probe_address (probe,
3518 objfile),
28106bc2
SDJ
3519 bp_longjmp_master,
3520 &internal_breakpoint_ops);
3521 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3522 b->enable_state = bp_disabled;
3523 }
3524
3525 continue;
3526 }
3527
0569175e
TSD
3528 if (!gdbarch_get_longjmp_target_p (gdbarch))
3529 continue;
3530
17450429 3531 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3532 {
3533 struct breakpoint *b;
af02033e 3534 const char *func_name;
17450429 3535 CORE_ADDR addr;
6c95b8df 3536
3b7344d5 3537 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3538 continue;
0fd8e87f 3539
17450429 3540 func_name = longjmp_names[i];
3b7344d5 3541 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3542 {
3b7344d5 3543 struct bound_minimal_symbol m;
17450429
PP
3544
3545 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3546 if (m.minsym == NULL)
17450429
PP
3547 {
3548 /* Prevent future lookups in this objfile. */
3b7344d5 3549 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3550 continue;
3551 }
3552 bp_objfile_data->longjmp_msym[i] = m;
3553 }
3554
77e371c0 3555 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3556 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3557 &internal_breakpoint_ops);
af02033e
PP
3558 b->addr_string = xstrdup (func_name);
3559 b->enable_state = bp_disabled;
3560 }
0fd8e87f 3561 }
af02033e 3562 }
44702360 3563 update_global_location_list (UGLL_MAY_INSERT);
6c95b8df
PA
3564
3565 do_cleanups (old_chain);
0fd8e87f
UW
3566}
3567
af02033e 3568/* Create a master std::terminate breakpoint. */
aa7d318d 3569static void
af02033e 3570create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3571{
3572 struct program_space *pspace;
aa7d318d 3573 struct cleanup *old_chain;
af02033e 3574 const char *const func_name = "std::terminate()";
aa7d318d
TT
3575
3576 old_chain = save_current_program_space ();
3577
3578 ALL_PSPACES (pspace)
17450429
PP
3579 {
3580 struct objfile *objfile;
3581 CORE_ADDR addr;
3582
3583 set_current_program_space (pspace);
3584
aa7d318d
TT
3585 ALL_OBJFILES (objfile)
3586 {
3587 struct breakpoint *b;
17450429 3588 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3589
17450429 3590 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3591
3b7344d5 3592 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3593 continue;
3594
3b7344d5 3595 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3596 {
3b7344d5 3597 struct bound_minimal_symbol m;
17450429
PP
3598
3599 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3600 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3601 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3602 {
3603 /* Prevent future lookups in this objfile. */
3b7344d5 3604 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3605 continue;
3606 }
3607 bp_objfile_data->terminate_msym = m;
3608 }
aa7d318d 3609
77e371c0 3610 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3611 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3612 bp_std_terminate_master,
3613 &internal_breakpoint_ops);
aa7d318d
TT
3614 b->addr_string = xstrdup (func_name);
3615 b->enable_state = bp_disabled;
3616 }
17450429
PP
3617 }
3618
44702360 3619 update_global_location_list (UGLL_MAY_INSERT);
aa7d318d
TT
3620
3621 do_cleanups (old_chain);
3622}
3623
186c406b
TT
3624/* Install a master breakpoint on the unwinder's debug hook. */
3625
70221824 3626static void
186c406b
TT
3627create_exception_master_breakpoint (void)
3628{
3629 struct objfile *objfile;
17450429 3630 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3631
3632 ALL_OBJFILES (objfile)
3633 {
17450429
PP
3634 struct breakpoint *b;
3635 struct gdbarch *gdbarch;
3636 struct breakpoint_objfile_data *bp_objfile_data;
3637 CORE_ADDR addr;
3638
3639 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3640
28106bc2
SDJ
3641 /* We prefer the SystemTap probe point if it exists. */
3642 if (!bp_objfile_data->exception_searched)
3643 {
25f9533e
SDJ
3644 VEC (probe_p) *ret;
3645
3646 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3647
3648 if (ret != NULL)
3649 {
3650 /* We are only interested in checking one element. */
3651 struct probe *p = VEC_index (probe_p, ret, 0);
3652
3653 if (!can_evaluate_probe_arguments (p))
3654 {
3655 /* We cannot use the probe interface here, because it does
3656 not know how to evaluate arguments. */
3657 VEC_free (probe_p, ret);
3658 ret = NULL;
3659 }
3660 }
3661 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3662 bp_objfile_data->exception_searched = 1;
3663 }
3664
3665 if (bp_objfile_data->exception_probes != NULL)
3666 {
3667 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3668 int i;
3669 struct probe *probe;
3670
3671 for (i = 0;
3672 VEC_iterate (probe_p,
3673 bp_objfile_data->exception_probes,
3674 i, probe);
3675 ++i)
3676 {
3677 struct breakpoint *b;
3678
729662a5
TT
3679 b = create_internal_breakpoint (gdbarch,
3680 get_probe_address (probe,
3681 objfile),
28106bc2
SDJ
3682 bp_exception_master,
3683 &internal_breakpoint_ops);
3684 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3685 b->enable_state = bp_disabled;
3686 }
3687
3688 continue;
3689 }
3690
3691 /* Otherwise, try the hook function. */
3692
3b7344d5 3693 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3694 continue;
3695
3696 gdbarch = get_objfile_arch (objfile);
186c406b 3697
3b7344d5 3698 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3699 {
3b7344d5 3700 struct bound_minimal_symbol debug_hook;
186c406b 3701
17450429 3702 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3703 if (debug_hook.minsym == NULL)
17450429 3704 {
3b7344d5 3705 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3706 continue;
3707 }
3708
3709 bp_objfile_data->exception_msym = debug_hook;
186c406b 3710 }
17450429 3711
77e371c0 3712 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3713 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3714 &current_target);
06edf0c0
PA
3715 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3716 &internal_breakpoint_ops);
17450429
PP
3717 b->addr_string = xstrdup (func_name);
3718 b->enable_state = bp_disabled;
186c406b
TT
3719 }
3720
44702360 3721 update_global_location_list (UGLL_MAY_INSERT);
186c406b
TT
3722}
3723
c906108c 3724void
fba45db2 3725update_breakpoints_after_exec (void)
c906108c 3726{
35df4500 3727 struct breakpoint *b, *b_tmp;
876fa593 3728 struct bp_location *bploc, **bplocp_tmp;
c906108c 3729
25b22b0a
PA
3730 /* We're about to delete breakpoints from GDB's lists. If the
3731 INSERTED flag is true, GDB will try to lift the breakpoints by
3732 writing the breakpoints' "shadow contents" back into memory. The
3733 "shadow contents" are NOT valid after an exec, so GDB should not
3734 do that. Instead, the target is responsible from marking
3735 breakpoints out as soon as it detects an exec. We don't do that
3736 here instead, because there may be other attempts to delete
3737 breakpoints after detecting an exec and before reaching here. */
876fa593 3738 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3739 if (bploc->pspace == current_program_space)
3740 gdb_assert (!bploc->inserted);
c906108c 3741
35df4500 3742 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3743 {
6c95b8df
PA
3744 if (b->pspace != current_program_space)
3745 continue;
3746
4a64f543 3747 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3748 if (b->type == bp_shlib_event)
3749 {
3750 delete_breakpoint (b);
3751 continue;
3752 }
c906108c 3753
4a64f543 3754 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3755 if (b->type == bp_jit_event)
3756 {
3757 delete_breakpoint (b);
3758 continue;
3759 }
3760
1900040c 3761 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3762 as must overlay event and longjmp master breakpoints. */
3763 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3764 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3765 || b->type == bp_exception_master)
c4093a6a
JM
3766 {
3767 delete_breakpoint (b);
3768 continue;
3769 }
3770
4a64f543 3771 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3772 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3773 {
3774 delete_breakpoint (b);
3775 continue;
3776 }
3777
7c16b83e
PA
3778 /* Just like single-step breakpoints. */
3779 if (b->type == bp_single_step)
3780 {
3781 delete_breakpoint (b);
3782 continue;
3783 }
3784
611c83ae
PA
3785 /* Longjmp and longjmp-resume breakpoints are also meaningless
3786 after an exec. */
186c406b 3787 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3788 || b->type == bp_longjmp_call_dummy
186c406b 3789 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3790 {
3791 delete_breakpoint (b);
3792 continue;
3793 }
3794
ce78b96d
JB
3795 if (b->type == bp_catchpoint)
3796 {
3797 /* For now, none of the bp_catchpoint breakpoints need to
3798 do anything at this point. In the future, if some of
3799 the catchpoints need to something, we will need to add
3800 a new method, and call this method from here. */
3801 continue;
3802 }
3803
c5aa993b
JM
3804 /* bp_finish is a special case. The only way we ought to be able
3805 to see one of these when an exec() has happened, is if the user
3806 caught a vfork, and then said "finish". Ordinarily a finish just
3807 carries them to the call-site of the current callee, by setting
3808 a temporary bp there and resuming. But in this case, the finish
3809 will carry them entirely through the vfork & exec.
3810
3811 We don't want to allow a bp_finish to remain inserted now. But
3812 we can't safely delete it, 'cause finish_command has a handle to
3813 the bp on a bpstat, and will later want to delete it. There's a
3814 chance (and I've seen it happen) that if we delete the bp_finish
3815 here, that its storage will get reused by the time finish_command
3816 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3817 We really must allow finish_command to delete a bp_finish.
3818
e5dd4106 3819 In the absence of a general solution for the "how do we know
53a5351d
JM
3820 it's safe to delete something others may have handles to?"
3821 problem, what we'll do here is just uninsert the bp_finish, and
3822 let finish_command delete it.
3823
3824 (We know the bp_finish is "doomed" in the sense that it's
3825 momentary, and will be deleted as soon as finish_command sees
3826 the inferior stopped. So it doesn't matter that the bp's
3827 address is probably bogus in the new a.out, unlike e.g., the
3828 solib breakpoints.) */
c5aa993b 3829
c5aa993b
JM
3830 if (b->type == bp_finish)
3831 {
3832 continue;
3833 }
3834
3835 /* Without a symbolic address, we have little hope of the
3836 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3837 a.out. */
c5aa993b
JM
3838 if (b->addr_string == NULL)
3839 {
3840 delete_breakpoint (b);
3841 continue;
3842 }
c5aa993b 3843 }
c906108c
SS
3844}
3845
3846int
d80ee84f 3847detach_breakpoints (ptid_t ptid)
c906108c 3848{
35df4500 3849 struct bp_location *bl, **blp_tmp;
3a1bae8e 3850 int val = 0;
ce696e05 3851 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3852 struct inferior *inf = current_inferior ();
c5aa993b 3853
dfd4cc63 3854 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3855 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3856
6c95b8df 3857 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3858 inferior_ptid = ptid;
35df4500 3859 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3860 {
35df4500 3861 if (bl->pspace != inf->pspace)
6c95b8df
PA
3862 continue;
3863
bd9673a4
PW
3864 /* This function must physically remove breakpoints locations
3865 from the specified ptid, without modifying the breakpoint
3866 package's state. Locations of type bp_loc_other are only
3867 maintained at GDB side. So, there is no need to remove
3868 these bp_loc_other locations. Moreover, removing these
3869 would modify the breakpoint package's state. */
3870 if (bl->loc_type == bp_loc_other)
3871 continue;
3872
35df4500
TJB
3873 if (bl->inserted)
3874 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3875 }
d03285ec 3876
ce696e05 3877 do_cleanups (old_chain);
3a1bae8e 3878 return val;
c906108c
SS
3879}
3880
35df4500 3881/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3882 Note that this is used to detach breakpoints from a child fork.
3883 When we get here, the child isn't in the inferior list, and neither
3884 do we have objects to represent its address space --- we should
35df4500 3885 *not* look at bl->pspace->aspace here. */
6c95b8df 3886
c906108c 3887static int
35df4500 3888remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3889{
3890 int val;
c5aa993b 3891
35df4500
TJB
3892 /* BL is never in moribund_locations by our callers. */
3893 gdb_assert (bl->owner != NULL);
2bdf28a0 3894
74960c60
VP
3895 /* The type of none suggests that owner is actually deleted.
3896 This should not ever happen. */
35df4500 3897 gdb_assert (bl->owner->type != bp_none);
0bde7532 3898
35df4500
TJB
3899 if (bl->loc_type == bp_loc_software_breakpoint
3900 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3901 {
c02f5703
MS
3902 /* "Normal" instruction breakpoint: either the standard
3903 trap-instruction bp (bp_breakpoint), or a
3904 bp_hardware_breakpoint. */
3905
3906 /* First check to see if we have to handle an overlay. */
3907 if (overlay_debugging == ovly_off
35df4500
TJB
3908 || bl->section == NULL
3909 || !(section_is_overlay (bl->section)))
c02f5703
MS
3910 {
3911 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3912
3913 /* If we're trying to uninsert a memory breakpoint that we
3914 know is set in a dynamic object that is marked
3915 shlib_disabled, then either the dynamic object was
3916 removed with "remove-symbol-file" or with
3917 "nosharedlibrary". In the former case, we don't know
3918 whether another dynamic object might have loaded over the
3919 breakpoint's address -- the user might well let us know
3920 about it next with add-symbol-file (the whole point of
d03de421 3921 add-symbol-file is letting the user manually maintain a
08351840
PA
3922 list of dynamically loaded objects). If we have the
3923 breakpoint's shadow memory, that is, this is a software
3924 breakpoint managed by GDB, check whether the breakpoint
3925 is still inserted in memory, to avoid overwriting wrong
3926 code with stale saved shadow contents. Note that HW
3927 breakpoints don't have shadow memory, as they're
3928 implemented using a mechanism that is not dependent on
3929 being able to modify the target's memory, and as such
3930 they should always be removed. */
3931 if (bl->shlib_disabled
3932 && bl->target_info.shadow_len != 0
3933 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3934 val = 0;
3935 else
3936 val = bl->owner->ops->remove_location (bl);
c02f5703 3937 }
c906108c
SS
3938 else
3939 {
4a64f543 3940 /* This breakpoint is in an overlay section.
c02f5703
MS
3941 Did we set a breakpoint at the LMA? */
3942 if (!overlay_events_enabled)
3943 {
3944 /* Yes -- overlay event support is not active, so we
3945 should have set a breakpoint at the LMA. Remove it.
3946 */
c02f5703
MS
3947 /* Ignore any failures: if the LMA is in ROM, we will
3948 have already warned when we failed to insert it. */
35df4500
TJB
3949 if (bl->loc_type == bp_loc_hardware_breakpoint)
3950 target_remove_hw_breakpoint (bl->gdbarch,
3951 &bl->overlay_target_info);
c02f5703 3952 else
35df4500
TJB
3953 target_remove_breakpoint (bl->gdbarch,
3954 &bl->overlay_target_info);
c02f5703
MS
3955 }
3956 /* Did we set a breakpoint at the VMA?
3957 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3958 if (bl->inserted)
c906108c 3959 {
c02f5703
MS
3960 /* Yes -- remove it. Previously we did not bother to
3961 remove the breakpoint if the section had been
3962 unmapped, but let's not rely on that being safe. We
3963 don't know what the overlay manager might do. */
aa67235e
UW
3964
3965 /* However, we should remove *software* breakpoints only
3966 if the section is still mapped, or else we overwrite
3967 wrong code with the saved shadow contents. */
348d480f
PA
3968 if (bl->loc_type == bp_loc_hardware_breakpoint
3969 || section_is_mapped (bl->section))
3970 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3971 else
3972 val = 0;
c906108c 3973 }
c02f5703
MS
3974 else
3975 {
3976 /* No -- not inserted, so no need to remove. No error. */
3977 val = 0;
3978 }
c906108c 3979 }
879d1e6b 3980
08351840
PA
3981 /* In some cases, we might not be able to remove a breakpoint in
3982 a shared library that has already been removed, but we have
3983 not yet processed the shlib unload event. Similarly for an
3984 unloaded add-symbol-file object - the user might not yet have
3985 had the chance to remove-symbol-file it. shlib_disabled will
3986 be set if the library/object has already been removed, but
3987 the breakpoint hasn't been uninserted yet, e.g., after
3988 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3989 always-inserted mode. */
076855f9 3990 if (val
08351840
PA
3991 && (bl->loc_type == bp_loc_software_breakpoint
3992 && (bl->shlib_disabled
3993 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3994 || shared_objfile_contains_address_p (bl->pspace,
3995 bl->address))))
879d1e6b
UW
3996 val = 0;
3997
c906108c
SS
3998 if (val)
3999 return val;
35df4500 4000 bl->inserted = (is == mark_inserted);
c906108c 4001 }
35df4500 4002 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4003 {
77b06cd7
TJB
4004 gdb_assert (bl->owner->ops != NULL
4005 && bl->owner->ops->remove_location != NULL);
4006
35df4500 4007 bl->inserted = (is == mark_inserted);
77b06cd7 4008 bl->owner->ops->remove_location (bl);
2e70b7b9 4009
c906108c 4010 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 4011 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4012 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4013 bl->owner->number);
c906108c 4014 }
35df4500
TJB
4015 else if (bl->owner->type == bp_catchpoint
4016 && breakpoint_enabled (bl->owner)
4017 && !bl->duplicate)
ce78b96d 4018 {
77b06cd7
TJB
4019 gdb_assert (bl->owner->ops != NULL
4020 && bl->owner->ops->remove_location != NULL);
ce78b96d 4021
77b06cd7 4022 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4023 if (val)
4024 return val;
77b06cd7 4025
35df4500 4026 bl->inserted = (is == mark_inserted);
ce78b96d 4027 }
c906108c
SS
4028
4029 return 0;
4030}
4031
6c95b8df 4032static int
35df4500 4033remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4034{
4035 int ret;
4036 struct cleanup *old_chain;
4037
35df4500
TJB
4038 /* BL is never in moribund_locations by our callers. */
4039 gdb_assert (bl->owner != NULL);
2bdf28a0 4040
6c95b8df
PA
4041 /* The type of none suggests that owner is actually deleted.
4042 This should not ever happen. */
35df4500 4043 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4044
4045 old_chain = save_current_space_and_thread ();
4046
35df4500 4047 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4048
35df4500 4049 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4050
4051 do_cleanups (old_chain);
4052 return ret;
4053}
4054
c906108c
SS
4055/* Clear the "inserted" flag in all breakpoints. */
4056
25b22b0a 4057void
fba45db2 4058mark_breakpoints_out (void)
c906108c 4059{
35df4500 4060 struct bp_location *bl, **blp_tmp;
c906108c 4061
35df4500 4062 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4063 if (bl->pspace == current_program_space)
35df4500 4064 bl->inserted = 0;
c906108c
SS
4065}
4066
53a5351d
JM
4067/* Clear the "inserted" flag in all breakpoints and delete any
4068 breakpoints which should go away between runs of the program.
c906108c
SS
4069
4070 Plus other such housekeeping that has to be done for breakpoints
4071 between runs.
4072
53a5351d
JM
4073 Note: this function gets called at the end of a run (by
4074 generic_mourn_inferior) and when a run begins (by
4a64f543 4075 init_wait_for_inferior). */
c906108c
SS
4076
4077
4078
4079void
fba45db2 4080breakpoint_init_inferior (enum inf_context context)
c906108c 4081{
35df4500
TJB
4082 struct breakpoint *b, *b_tmp;
4083 struct bp_location *bl, **blp_tmp;
1c5cfe86 4084 int ix;
6c95b8df 4085 struct program_space *pspace = current_program_space;
c906108c 4086
50c71eaf
PA
4087 /* If breakpoint locations are shared across processes, then there's
4088 nothing to do. */
f5656ead 4089 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4090 return;
4091
1a853c52 4092 mark_breakpoints_out ();
075f6582 4093
35df4500 4094 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4095 {
6c95b8df
PA
4096 if (b->loc && b->loc->pspace != pspace)
4097 continue;
4098
c5aa993b
JM
4099 switch (b->type)
4100 {
4101 case bp_call_dummy:
e2e4d78b 4102 case bp_longjmp_call_dummy:
c906108c 4103
c5aa993b 4104 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4105 cause problems when the inferior is rerun, so we better get
4106 rid of it. */
4107
4108 case bp_watchpoint_scope:
4109
4110 /* Also get rid of scope breakpoints. */
4111
4112 case bp_shlib_event:
4113
4114 /* Also remove solib event breakpoints. Their addresses may
4115 have changed since the last time we ran the program.
4116 Actually we may now be debugging against different target;
4117 and so the solib backend that installed this breakpoint may
4118 not be used in by the target. E.g.,
4119
4120 (gdb) file prog-linux
4121 (gdb) run # native linux target
4122 ...
4123 (gdb) kill
4124 (gdb) file prog-win.exe
4125 (gdb) tar rem :9999 # remote Windows gdbserver.
4126 */
c906108c 4127
f59f708a
PA
4128 case bp_step_resume:
4129
4130 /* Also remove step-resume breakpoints. */
4131
7c16b83e
PA
4132 case bp_single_step:
4133
4134 /* Also remove single-step breakpoints. */
4135
c5aa993b
JM
4136 delete_breakpoint (b);
4137 break;
c906108c 4138
c5aa993b
JM
4139 case bp_watchpoint:
4140 case bp_hardware_watchpoint:
4141 case bp_read_watchpoint:
4142 case bp_access_watchpoint:
3a5c3e22
PA
4143 {
4144 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4145
3a5c3e22
PA
4146 /* Likewise for watchpoints on local expressions. */
4147 if (w->exp_valid_block != NULL)
4148 delete_breakpoint (b);
4149 else if (context == inf_starting)
4150 {
4151 /* Reset val field to force reread of starting value in
4152 insert_breakpoints. */
4153 if (w->val)
4154 value_free (w->val);
4155 w->val = NULL;
4156 w->val_valid = 0;
c860120c 4157 }
3a5c3e22 4158 }
c5aa993b
JM
4159 break;
4160 default:
c5aa993b
JM
4161 break;
4162 }
4163 }
1c5cfe86
PA
4164
4165 /* Get rid of the moribund locations. */
35df4500
TJB
4166 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4167 decref_bp_location (&bl);
1c5cfe86 4168 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4169}
4170
6c95b8df
PA
4171/* These functions concern about actual breakpoints inserted in the
4172 target --- to e.g. check if we need to do decr_pc adjustment or if
4173 we need to hop over the bkpt --- so we check for address space
4174 match, not program space. */
4175
c2c6d25f
JM
4176/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4177 exists at PC. It returns ordinary_breakpoint_here if it's an
4178 ordinary breakpoint, or permanent_breakpoint_here if it's a
4179 permanent breakpoint.
4180 - When continuing from a location with an ordinary breakpoint, we
4181 actually single step once before calling insert_breakpoints.
e5dd4106 4182 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4183 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4184 the target, to advance the PC past the breakpoint. */
c906108c 4185
c2c6d25f 4186enum breakpoint_here
6c95b8df 4187breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4188{
35df4500 4189 struct bp_location *bl, **blp_tmp;
c2c6d25f 4190 int any_breakpoint_here = 0;
c906108c 4191
35df4500 4192 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4193 {
35df4500
TJB
4194 if (bl->loc_type != bp_loc_software_breakpoint
4195 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4196 continue;
4197
f1310107 4198 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4199 if ((breakpoint_enabled (bl->owner)
1a853c52 4200 || bl->permanent)
f1310107 4201 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4202 {
4203 if (overlay_debugging
35df4500
TJB
4204 && section_is_overlay (bl->section)
4205 && !section_is_mapped (bl->section))
075f6582 4206 continue; /* unmapped overlay -- can't be a match */
1a853c52 4207 else if (bl->permanent)
075f6582
DJ
4208 return permanent_breakpoint_here;
4209 else
4210 any_breakpoint_here = 1;
4211 }
4212 }
c906108c 4213
c2c6d25f 4214 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
4215}
4216
1c5cfe86
PA
4217/* Return true if there's a moribund breakpoint at PC. */
4218
4219int
6c95b8df 4220moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4221{
4222 struct bp_location *loc;
4223 int ix;
4224
4225 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4226 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4227 return 1;
4228
4229 return 0;
4230}
c2c6d25f 4231
f7ce857f
PA
4232/* Returns non-zero iff BL is inserted at PC, in address space
4233 ASPACE. */
4234
4235static int
4236bp_location_inserted_here_p (struct bp_location *bl,
4237 struct address_space *aspace, CORE_ADDR pc)
4238{
4239 if (bl->inserted
4240 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4241 aspace, pc))
4242 {
4243 if (overlay_debugging
4244 && section_is_overlay (bl->section)
4245 && !section_is_mapped (bl->section))
4246 return 0; /* unmapped overlay -- can't be a match */
4247 else
4248 return 1;
4249 }
4250 return 0;
4251}
4252
a1fd2fa5 4253/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4254
4255int
a1fd2fa5 4256breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4257{
f7ce857f
PA
4258 struct bp_location **blp, **blp_tmp = NULL;
4259 struct bp_location *bl;
c906108c 4260
f7ce857f 4261 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4262 {
f7ce857f
PA
4263 struct bp_location *bl = *blp;
4264
35df4500
TJB
4265 if (bl->loc_type != bp_loc_software_breakpoint
4266 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4267 continue;
4268
f7ce857f
PA
4269 if (bp_location_inserted_here_p (bl, aspace, pc))
4270 return 1;
c5aa993b 4271 }
c36b740a
VP
4272 return 0;
4273}
4274
a1fd2fa5
PA
4275/* This function returns non-zero iff there is a software breakpoint
4276 inserted at PC. */
c36b740a
VP
4277
4278int
a1fd2fa5
PA
4279software_breakpoint_inserted_here_p (struct address_space *aspace,
4280 CORE_ADDR pc)
4fa8626c 4281{
f7ce857f
PA
4282 struct bp_location **blp, **blp_tmp = NULL;
4283 struct bp_location *bl;
4fa8626c 4284
f7ce857f 4285 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4286 {
f7ce857f
PA
4287 struct bp_location *bl = *blp;
4288
35df4500 4289 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4290 continue;
4291
f7ce857f
PA
4292 if (bp_location_inserted_here_p (bl, aspace, pc))
4293 return 1;
4fa8626c
DJ
4294 }
4295
4296 return 0;
9c02b525
PA
4297}
4298
4299/* See breakpoint.h. */
4300
4301int
4302hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4303 CORE_ADDR pc)
4304{
4305 struct bp_location **blp, **blp_tmp = NULL;
4306 struct bp_location *bl;
4307
4308 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4309 {
4310 struct bp_location *bl = *blp;
4311
4312 if (bl->loc_type != bp_loc_hardware_breakpoint)
4313 continue;
4314
4315 if (bp_location_inserted_here_p (bl, aspace, pc))
4316 return 1;
4317 }
4318
4319 return 0;
4fa8626c
DJ
4320}
4321
9093389c
PA
4322int
4323hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4324 CORE_ADDR addr, ULONGEST len)
4325{
4326 struct breakpoint *bpt;
4327
4328 ALL_BREAKPOINTS (bpt)
4329 {
4330 struct bp_location *loc;
4331
4332 if (bpt->type != bp_hardware_watchpoint
4333 && bpt->type != bp_access_watchpoint)
4334 continue;
4335
4336 if (!breakpoint_enabled (bpt))
4337 continue;
4338
4339 for (loc = bpt->loc; loc; loc = loc->next)
4340 if (loc->pspace->aspace == aspace && loc->inserted)
4341 {
4342 CORE_ADDR l, h;
4343
4344 /* Check for intersection. */
4345 l = max (loc->address, addr);
4346 h = min (loc->address + loc->length, addr + len);
4347 if (l < h)
4348 return 1;
4349 }
4350 }
4351 return 0;
4352}
c906108c 4353\f
c5aa993b 4354
c906108c
SS
4355/* bpstat stuff. External routines' interfaces are documented
4356 in breakpoint.h. */
4357
4358int
c326b90e 4359is_catchpoint (struct breakpoint *ep)
c906108c 4360{
533be4dd 4361 return (ep->type == bp_catchpoint);
c906108c
SS
4362}
4363
f431efe5
PA
4364/* Frees any storage that is part of a bpstat. Does not walk the
4365 'next' chain. */
4366
4367static void
198757a8
VP
4368bpstat_free (bpstat bs)
4369{
4370 if (bs->old_val != NULL)
4371 value_free (bs->old_val);
9add0f1b 4372 decref_counted_command_line (&bs->commands);
f431efe5 4373 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4374 xfree (bs);
4375}
4376
c906108c
SS
4377/* Clear a bpstat so that it says we are not at any breakpoint.
4378 Also free any storage that is part of a bpstat. */
4379
4380void
fba45db2 4381bpstat_clear (bpstat *bsp)
c906108c
SS
4382{
4383 bpstat p;
4384 bpstat q;
4385
4386 if (bsp == 0)
4387 return;
4388 p = *bsp;
4389 while (p != NULL)
4390 {
4391 q = p->next;
198757a8 4392 bpstat_free (p);
c906108c
SS
4393 p = q;
4394 }
4395 *bsp = NULL;
4396}
4397
4398/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4399 is part of the bpstat is copied as well. */
4400
4401bpstat
fba45db2 4402bpstat_copy (bpstat bs)
c906108c
SS
4403{
4404 bpstat p = NULL;
4405 bpstat tmp;
4406 bpstat retval = NULL;
4407
4408 if (bs == NULL)
4409 return bs;
4410
4411 for (; bs != NULL; bs = bs->next)
4412 {
4413 tmp = (bpstat) xmalloc (sizeof (*tmp));
4414 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4415 incref_counted_command_line (tmp->commands);
f431efe5 4416 incref_bp_location (tmp->bp_location_at);
31cc81e9 4417 if (bs->old_val != NULL)
3c3185ac
JK
4418 {
4419 tmp->old_val = value_copy (bs->old_val);
4420 release_value (tmp->old_val);
4421 }
31cc81e9 4422
c906108c
SS
4423 if (p == NULL)
4424 /* This is the first thing in the chain. */
4425 retval = tmp;
4426 else
4427 p->next = tmp;
4428 p = tmp;
4429 }
4430 p->next = NULL;
4431 return retval;
4432}
4433
4a64f543 4434/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4435
4436bpstat
fba45db2 4437bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4438{
c5aa993b
JM
4439 if (bsp == NULL)
4440 return NULL;
c906108c 4441
c5aa993b
JM
4442 for (; bsp != NULL; bsp = bsp->next)
4443 {
f431efe5 4444 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4445 return bsp;
4446 }
c906108c
SS
4447 return NULL;
4448}
4449
ab04a2af
TT
4450/* See breakpoint.h. */
4451
47591c29 4452int
427cd150 4453bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4454{
ab04a2af
TT
4455 for (; bsp != NULL; bsp = bsp->next)
4456 {
427cd150
TT
4457 if (bsp->breakpoint_at == NULL)
4458 {
4459 /* A moribund location can never explain a signal other than
4460 GDB_SIGNAL_TRAP. */
4461 if (sig == GDB_SIGNAL_TRAP)
47591c29 4462 return 1;
427cd150
TT
4463 }
4464 else
47591c29
PA
4465 {
4466 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4467 sig))
4468 return 1;
4469 }
ab04a2af
TT
4470 }
4471
47591c29 4472 return 0;
ab04a2af
TT
4473}
4474
4a64f543
MS
4475/* Put in *NUM the breakpoint number of the first breakpoint we are
4476 stopped at. *BSP upon return is a bpstat which points to the
4477 remaining breakpoints stopped at (but which is not guaranteed to be
4478 good for anything but further calls to bpstat_num).
4479
8671a17b
PA
4480 Return 0 if passed a bpstat which does not indicate any breakpoints.
4481 Return -1 if stopped at a breakpoint that has been deleted since
4482 we set it.
4483 Return 1 otherwise. */
c906108c
SS
4484
4485int
8671a17b 4486bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4487{
4488 struct breakpoint *b;
4489
4490 if ((*bsp) == NULL)
4491 return 0; /* No more breakpoint values */
8671a17b 4492
4a64f543
MS
4493 /* We assume we'll never have several bpstats that correspond to a
4494 single breakpoint -- otherwise, this function might return the
4495 same number more than once and this will look ugly. */
f431efe5 4496 b = (*bsp)->breakpoint_at;
8671a17b
PA
4497 *bsp = (*bsp)->next;
4498 if (b == NULL)
4499 return -1; /* breakpoint that's been deleted since */
4500
4501 *num = b->number; /* We have its number */
4502 return 1;
c906108c
SS
4503}
4504
e93ca019 4505/* See breakpoint.h. */
c906108c
SS
4506
4507void
e93ca019 4508bpstat_clear_actions (void)
c906108c 4509{
e93ca019
JK
4510 struct thread_info *tp;
4511 bpstat bs;
4512
4513 if (ptid_equal (inferior_ptid, null_ptid))
4514 return;
4515
4516 tp = find_thread_ptid (inferior_ptid);
4517 if (tp == NULL)
4518 return;
4519
4520 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4521 {
9add0f1b 4522 decref_counted_command_line (&bs->commands);
abf85f46 4523
c906108c
SS
4524 if (bs->old_val != NULL)
4525 {
4526 value_free (bs->old_val);
4527 bs->old_val = NULL;
4528 }
4529 }
4530}
4531
f3b1572e
PA
4532/* Called when a command is about to proceed the inferior. */
4533
4534static void
4535breakpoint_about_to_proceed (void)
4536{
4537 if (!ptid_equal (inferior_ptid, null_ptid))
4538 {
4539 struct thread_info *tp = inferior_thread ();
4540
4541 /* Allow inferior function calls in breakpoint commands to not
4542 interrupt the command list. When the call finishes
4543 successfully, the inferior will be standing at the same
4544 breakpoint as if nothing happened. */
16c381f0 4545 if (tp->control.in_infcall)
f3b1572e
PA
4546 return;
4547 }
4548
4549 breakpoint_proceeded = 1;
4550}
4551
4a64f543
MS
4552/* Stub for cleaning up our state if we error-out of a breakpoint
4553 command. */
c906108c 4554static void
4efb68b1 4555cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4556{
4557 executing_breakpoint_commands = 0;
4558}
4559
abf85f46
JK
4560/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4561 or its equivalent. */
4562
4563static int
4564command_line_is_silent (struct command_line *cmd)
4565{
4f45d445 4566 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4567}
4568
4a64f543
MS
4569/* Execute all the commands associated with all the breakpoints at
4570 this location. Any of these commands could cause the process to
4571 proceed beyond this point, etc. We look out for such changes by
4572 checking the global "breakpoint_proceeded" after each command.
c906108c 4573
347bddb7
PA
4574 Returns true if a breakpoint command resumed the inferior. In that
4575 case, it is the caller's responsibility to recall it again with the
4576 bpstat of the current thread. */
4577
4578static int
4579bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4580{
4581 bpstat bs;
4582 struct cleanup *old_chain;
347bddb7 4583 int again = 0;
c906108c
SS
4584
4585 /* Avoid endless recursion if a `source' command is contained
4586 in bs->commands. */
4587 if (executing_breakpoint_commands)
347bddb7 4588 return 0;
c906108c
SS
4589
4590 executing_breakpoint_commands = 1;
4591 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4592
cf6c5ffb
TT
4593 prevent_dont_repeat ();
4594
4a64f543 4595 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4596 bs = *bsp;
4597
4598 breakpoint_proceeded = 0;
4599 for (; bs != NULL; bs = bs->next)
4600 {
9add0f1b 4601 struct counted_command_line *ccmd;
6c50ab1c
JB
4602 struct command_line *cmd;
4603 struct cleanup *this_cmd_tree_chain;
4604
4605 /* Take ownership of the BSP's command tree, if it has one.
4606
4607 The command tree could legitimately contain commands like
4608 'step' and 'next', which call clear_proceed_status, which
4609 frees stop_bpstat's command tree. To make sure this doesn't
4610 free the tree we're executing out from under us, we need to
4611 take ownership of the tree ourselves. Since a given bpstat's
4612 commands are only executed once, we don't need to copy it; we
4613 can clear the pointer in the bpstat, and make sure we free
4614 the tree when we're done. */
9add0f1b
TT
4615 ccmd = bs->commands;
4616 bs->commands = NULL;
abf85f46
JK
4617 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4618 cmd = ccmd ? ccmd->commands : NULL;
4619 if (command_line_is_silent (cmd))
4620 {
4621 /* The action has been already done by bpstat_stop_status. */
4622 cmd = cmd->next;
4623 }
6c50ab1c 4624
c906108c
SS
4625 while (cmd != NULL)
4626 {
4627 execute_control_command (cmd);
4628
4629 if (breakpoint_proceeded)
4630 break;
4631 else
4632 cmd = cmd->next;
4633 }
6c50ab1c
JB
4634
4635 /* We can free this command tree now. */
4636 do_cleanups (this_cmd_tree_chain);
4637
c906108c 4638 if (breakpoint_proceeded)
32c1e744 4639 {
5589af0e 4640 if (interpreter_async && target_can_async_p ())
347bddb7
PA
4641 /* If we are in async mode, then the target might be still
4642 running, not stopped at any breakpoint, so nothing for
4643 us to do here -- just return to the event loop. */
4644 ;
32c1e744
VP
4645 else
4646 /* In sync mode, when execute_control_command returns
4647 we're already standing on the next breakpoint.
347bddb7
PA
4648 Breakpoint commands for that stop were not run, since
4649 execute_command does not run breakpoint commands --
4650 only command_line_handler does, but that one is not
4651 involved in execution of breakpoint commands. So, we
4652 can now execute breakpoint commands. It should be
4653 noted that making execute_command do bpstat actions is
4654 not an option -- in this case we'll have recursive
4655 invocation of bpstat for each breakpoint with a
4656 command, and can easily blow up GDB stack. Instead, we
4657 return true, which will trigger the caller to recall us
4658 with the new stop_bpstat. */
4659 again = 1;
4660 break;
32c1e744 4661 }
c906108c 4662 }
c2b8ed2c 4663 do_cleanups (old_chain);
347bddb7
PA
4664 return again;
4665}
4666
4667void
4668bpstat_do_actions (void)
4669{
353d1d73
JK
4670 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4671
347bddb7
PA
4672 /* Do any commands attached to breakpoint we are stopped at. */
4673 while (!ptid_equal (inferior_ptid, null_ptid)
4674 && target_has_execution
4675 && !is_exited (inferior_ptid)
4676 && !is_executing (inferior_ptid))
4677 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4678 and only return when it is stopped at the next breakpoint, we
4679 keep doing breakpoint actions until it returns false to
4680 indicate the inferior was not resumed. */
16c381f0 4681 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4682 break;
353d1d73
JK
4683
4684 discard_cleanups (cleanup_if_error);
c906108c
SS
4685}
4686
fa4727a6
DJ
4687/* Print out the (old or new) value associated with a watchpoint. */
4688
4689static void
4690watchpoint_value_print (struct value *val, struct ui_file *stream)
4691{
4692 if (val == NULL)
4693 fprintf_unfiltered (stream, _("<unreadable>"));
4694 else
79a45b7d
TT
4695 {
4696 struct value_print_options opts;
4697 get_user_print_options (&opts);
4698 value_print (val, stream, &opts);
4699 }
fa4727a6
DJ
4700}
4701
e514a9d6 4702/* Generic routine for printing messages indicating why we
4a64f543 4703 stopped. The behavior of this function depends on the value
e514a9d6
JM
4704 'print_it' in the bpstat structure. Under some circumstances we
4705 may decide not to print anything here and delegate the task to
4a64f543 4706 normal_stop(). */
e514a9d6
JM
4707
4708static enum print_stop_action
4709print_bp_stop_message (bpstat bs)
4710{
4711 switch (bs->print_it)
4712 {
4713 case print_it_noop:
4a64f543 4714 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4715 return PRINT_UNKNOWN;
4716 break;
4717
4718 case print_it_done:
4719 /* We still want to print the frame, but we already printed the
4a64f543 4720 relevant messages. */
e514a9d6
JM
4721 return PRINT_SRC_AND_LOC;
4722 break;
4723
4724 case print_it_normal:
4f8d1dc6 4725 {
f431efe5
PA
4726 struct breakpoint *b = bs->breakpoint_at;
4727
1a6a67de
TJB
4728 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4729 which has since been deleted. */
4730 if (b == NULL)
4731 return PRINT_UNKNOWN;
4732
348d480f
PA
4733 /* Normal case. Call the breakpoint's print_it method. */
4734 return b->ops->print_it (bs);
4f8d1dc6 4735 }
348d480f 4736 break;
3086aeae 4737
e514a9d6 4738 default:
8e65ff28 4739 internal_error (__FILE__, __LINE__,
e2e0b3e5 4740 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4741 break;
c906108c 4742 }
c906108c
SS
4743}
4744
edcc5120
TT
4745/* A helper function that prints a shared library stopped event. */
4746
4747static void
4748print_solib_event (int is_catchpoint)
4749{
4750 int any_deleted
4751 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4752 int any_added
4753 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4754
4755 if (!is_catchpoint)
4756 {
4757 if (any_added || any_deleted)
4758 ui_out_text (current_uiout,
4759 _("Stopped due to shared library event:\n"));
4760 else
4761 ui_out_text (current_uiout,
4762 _("Stopped due to shared library event (no "
4763 "libraries added or removed)\n"));
4764 }
4765
4766 if (ui_out_is_mi_like_p (current_uiout))
4767 ui_out_field_string (current_uiout, "reason",
4768 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4769
4770 if (any_deleted)
4771 {
4772 struct cleanup *cleanup;
4773 char *name;
4774 int ix;
4775
4776 ui_out_text (current_uiout, _(" Inferior unloaded "));
4777 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4778 "removed");
4779 for (ix = 0;
4780 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4781 ix, name);
4782 ++ix)
4783 {
4784 if (ix > 0)
4785 ui_out_text (current_uiout, " ");
4786 ui_out_field_string (current_uiout, "library", name);
4787 ui_out_text (current_uiout, "\n");
4788 }
4789
4790 do_cleanups (cleanup);
4791 }
4792
4793 if (any_added)
4794 {
4795 struct so_list *iter;
4796 int ix;
4797 struct cleanup *cleanup;
4798
4799 ui_out_text (current_uiout, _(" Inferior loaded "));
4800 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4801 "added");
4802 for (ix = 0;
4803 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4804 ix, iter);
4805 ++ix)
4806 {
4807 if (ix > 0)
4808 ui_out_text (current_uiout, " ");
4809 ui_out_field_string (current_uiout, "library", iter->so_name);
4810 ui_out_text (current_uiout, "\n");
4811 }
4812
4813 do_cleanups (cleanup);
4814 }
4815}
4816
e514a9d6
JM
4817/* Print a message indicating what happened. This is called from
4818 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4819 list - a list of the eventpoints that caused this stop. KIND is
4820 the target_waitkind for the stopping event. This
e514a9d6
JM
4821 routine calls the generic print routine for printing a message
4822 about reasons for stopping. This will print (for example) the
4823 "Breakpoint n," part of the output. The return value of this
4824 routine is one of:
c906108c 4825
4a64f543 4826 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4827 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4828 code to print the location. An example is
c5aa993b
JM
4829 "Breakpoint 1, " which should be followed by
4830 the location.
917317f4 4831 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4832 to also print the location part of the message.
4833 An example is the catch/throw messages, which
4a64f543 4834 don't require a location appended to the end.
917317f4 4835 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4836 further info to be printed. */
c906108c 4837
917317f4 4838enum print_stop_action
36dfb11c 4839bpstat_print (bpstat bs, int kind)
c906108c
SS
4840{
4841 int val;
c5aa993b 4842
c906108c 4843 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4844 (Currently all watchpoints go on the bpstat whether hit or not.
4845 That probably could (should) be changed, provided care is taken
c906108c 4846 with respect to bpstat_explains_signal). */
e514a9d6
JM
4847 for (; bs; bs = bs->next)
4848 {
4849 val = print_bp_stop_message (bs);
4850 if (val == PRINT_SRC_ONLY
4851 || val == PRINT_SRC_AND_LOC
4852 || val == PRINT_NOTHING)
4853 return val;
4854 }
c906108c 4855
36dfb11c
TT
4856 /* If we had hit a shared library event breakpoint,
4857 print_bp_stop_message would print out this message. If we hit an
4858 OS-level shared library event, do the same thing. */
4859 if (kind == TARGET_WAITKIND_LOADED)
4860 {
edcc5120 4861 print_solib_event (0);
36dfb11c
TT
4862 return PRINT_NOTHING;
4863 }
4864
e514a9d6 4865 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4866 with and nothing was printed. */
917317f4 4867 return PRINT_UNKNOWN;
c906108c
SS
4868}
4869
c42bd95a
DE
4870/* Evaluate the expression EXP and return 1 if value is zero.
4871 This returns the inverse of the condition because it is called
4872 from catch_errors which returns 0 if an exception happened, and if an
4873 exception happens we want execution to stop.
4a64f543 4874 The argument is a "struct expression *" that has been cast to a
c42bd95a 4875 "void *" to make it pass through catch_errors. */
c906108c
SS
4876
4877static int
4efb68b1 4878breakpoint_cond_eval (void *exp)
c906108c 4879{
278cd55f 4880 struct value *mark = value_mark ();
c5aa993b 4881 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4882
c906108c
SS
4883 value_free_to_mark (mark);
4884 return i;
4885}
4886
5760d0ab 4887/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4888
4889static bpstat
5760d0ab 4890bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4891{
4892 bpstat bs;
4893
4894 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4895 bs->next = NULL;
4896 **bs_link_pointer = bs;
4897 *bs_link_pointer = &bs->next;
f431efe5
PA
4898 bs->breakpoint_at = bl->owner;
4899 bs->bp_location_at = bl;
4900 incref_bp_location (bl);
c906108c
SS
4901 /* If the condition is false, etc., don't do the commands. */
4902 bs->commands = NULL;
4903 bs->old_val = NULL;
4904 bs->print_it = print_it_normal;
4905 return bs;
4906}
4907\f
d983da9c
DJ
4908/* The target has stopped with waitstatus WS. Check if any hardware
4909 watchpoints have triggered, according to the target. */
4910
4911int
4912watchpoints_triggered (struct target_waitstatus *ws)
4913{
d92524f1 4914 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4915 CORE_ADDR addr;
4916 struct breakpoint *b;
4917
4918 if (!stopped_by_watchpoint)
4919 {
4920 /* We were not stopped by a watchpoint. Mark all watchpoints
4921 as not triggered. */
4922 ALL_BREAKPOINTS (b)
cc60f2e3 4923 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4924 {
4925 struct watchpoint *w = (struct watchpoint *) b;
4926
4927 w->watchpoint_triggered = watch_triggered_no;
4928 }
d983da9c
DJ
4929
4930 return 0;
4931 }
4932
4933 if (!target_stopped_data_address (&current_target, &addr))
4934 {
4935 /* We were stopped by a watchpoint, but we don't know where.
4936 Mark all watchpoints as unknown. */
4937 ALL_BREAKPOINTS (b)
cc60f2e3 4938 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4939 {
4940 struct watchpoint *w = (struct watchpoint *) b;
4941
4942 w->watchpoint_triggered = watch_triggered_unknown;
4943 }
d983da9c 4944
3c4797ba 4945 return 1;
d983da9c
DJ
4946 }
4947
4948 /* The target could report the data address. Mark watchpoints
4949 affected by this data address as triggered, and all others as not
4950 triggered. */
4951
4952 ALL_BREAKPOINTS (b)
cc60f2e3 4953 if (is_hardware_watchpoint (b))
d983da9c 4954 {
3a5c3e22 4955 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4956 struct bp_location *loc;
d983da9c 4957
3a5c3e22 4958 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4959 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4960 {
3a5c3e22 4961 if (is_masked_watchpoint (b))
9c06b0b4 4962 {
3a5c3e22
PA
4963 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4964 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4965
4966 if (newaddr == start)
4967 {
3a5c3e22 4968 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4969 break;
4970 }
4971 }
4972 /* Exact match not required. Within range is sufficient. */
4973 else if (target_watchpoint_addr_within_range (&current_target,
4974 addr, loc->address,
4975 loc->length))
4976 {
3a5c3e22 4977 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4978 break;
4979 }
4980 }
d983da9c
DJ
4981 }
4982
4983 return 1;
4984}
4985
c906108c
SS
4986/* Possible return values for watchpoint_check (this can't be an enum
4987 because of check_errors). */
4988/* The watchpoint has been deleted. */
4989#define WP_DELETED 1
4990/* The value has changed. */
4991#define WP_VALUE_CHANGED 2
4992/* The value has not changed. */
4993#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4994/* Ignore this watchpoint, no matter if the value changed or not. */
4995#define WP_IGNORE 4
c906108c
SS
4996
4997#define BP_TEMPFLAG 1
4998#define BP_HARDWAREFLAG 2
4999
4a64f543
MS
5000/* Evaluate watchpoint condition expression and check if its value
5001 changed.
553e4c11
JB
5002
5003 P should be a pointer to struct bpstat, but is defined as a void *
5004 in order for this function to be usable with catch_errors. */
c906108c
SS
5005
5006static int
4efb68b1 5007watchpoint_check (void *p)
c906108c
SS
5008{
5009 bpstat bs = (bpstat) p;
3a5c3e22 5010 struct watchpoint *b;
c906108c
SS
5011 struct frame_info *fr;
5012 int within_current_scope;
5013
f431efe5 5014 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5015 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5016 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5017
f6bc2008
PA
5018 /* If this is a local watchpoint, we only want to check if the
5019 watchpoint frame is in scope if the current thread is the thread
5020 that was used to create the watchpoint. */
5021 if (!watchpoint_in_thread_scope (b))
60e1c644 5022 return WP_IGNORE;
f6bc2008 5023
c906108c
SS
5024 if (b->exp_valid_block == NULL)
5025 within_current_scope = 1;
5026 else
5027 {
edb3359d
DJ
5028 struct frame_info *frame = get_current_frame ();
5029 struct gdbarch *frame_arch = get_frame_arch (frame);
5030 CORE_ADDR frame_pc = get_frame_pc (frame);
5031
c9cf6e20 5032 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5033 still in the function but the stack frame has already been
5034 invalidated. Since we can't rely on the values of local
5035 variables after the stack has been destroyed, we are treating
5036 the watchpoint in that state as `not changed' without further
5037 checking. Don't mark watchpoints as changed if the current
5038 frame is in an epilogue - even if they are in some other
5039 frame, our view of the stack is likely to be wrong and
5040 frame_find_by_id could error out. */
c9cf6e20 5041 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5042 return WP_IGNORE;
a0f49112 5043
101dcfbe 5044 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5045 within_current_scope = (fr != NULL);
69fbadd5
DJ
5046
5047 /* If we've gotten confused in the unwinder, we might have
5048 returned a frame that can't describe this variable. */
edb3359d
DJ
5049 if (within_current_scope)
5050 {
5051 struct symbol *function;
5052
5053 function = get_frame_function (fr);
5054 if (function == NULL
5055 || !contained_in (b->exp_valid_block,
5056 SYMBOL_BLOCK_VALUE (function)))
5057 within_current_scope = 0;
5058 }
69fbadd5 5059
edb3359d 5060 if (within_current_scope)
c906108c
SS
5061 /* If we end up stopping, the current frame will get selected
5062 in normal_stop. So this call to select_frame won't affect
5063 the user. */
0f7d239c 5064 select_frame (fr);
c906108c 5065 }
c5aa993b 5066
c906108c
SS
5067 if (within_current_scope)
5068 {
4a64f543
MS
5069 /* We use value_{,free_to_}mark because it could be a *long*
5070 time before we return to the command level and call
5071 free_all_values. We can't call free_all_values because we
5072 might be in the middle of evaluating a function call. */
c906108c 5073
0cf6dd15 5074 int pc = 0;
9c06b0b4 5075 struct value *mark;
fa4727a6
DJ
5076 struct value *new_val;
5077
3a5c3e22 5078 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5079 /* Since we don't know the exact trigger address (from
5080 stopped_data_address), just tell the user we've triggered
5081 a mask watchpoint. */
5082 return WP_VALUE_CHANGED;
5083
5084 mark = value_mark ();
3a1115a0 5085 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5086
bb9d5f81
PP
5087 if (b->val_bitsize != 0)
5088 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5089
4a64f543
MS
5090 /* We use value_equal_contents instead of value_equal because
5091 the latter coerces an array to a pointer, thus comparing just
5092 the address of the array instead of its contents. This is
5093 not what we want. */
fa4727a6 5094 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5095 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5096 {
fa4727a6
DJ
5097 if (new_val != NULL)
5098 {
5099 release_value (new_val);
5100 value_free_to_mark (mark);
5101 }
c906108c
SS
5102 bs->old_val = b->val;
5103 b->val = new_val;
fa4727a6 5104 b->val_valid = 1;
c906108c
SS
5105 return WP_VALUE_CHANGED;
5106 }
5107 else
5108 {
60e1c644 5109 /* Nothing changed. */
c906108c 5110 value_free_to_mark (mark);
c906108c
SS
5111 return WP_VALUE_NOT_CHANGED;
5112 }
5113 }
5114 else
5115 {
79a45e25
PA
5116 struct ui_out *uiout = current_uiout;
5117
c906108c 5118 /* This seems like the only logical thing to do because
c5aa993b
JM
5119 if we temporarily ignored the watchpoint, then when
5120 we reenter the block in which it is valid it contains
5121 garbage (in the case of a function, it may have two
5122 garbage values, one before and one after the prologue).
5123 So we can't even detect the first assignment to it and
5124 watch after that (since the garbage may or may not equal
5125 the first value assigned). */
348d480f
PA
5126 /* We print all the stop information in
5127 breakpoint_ops->print_it, but in this case, by the time we
5128 call breakpoint_ops->print_it this bp will be deleted
5129 already. So we have no choice but print the information
5130 here. */
9dc5e2a9 5131 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5132 ui_out_field_string
5133 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5134 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5135 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5136 ui_out_text (uiout,
5137 " deleted because the program has left the block in\n\
8b93c638 5138which its expression is valid.\n");
4ce44c66 5139
cdac0397 5140 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5141 decref_counted_command_line (&b->base.commands);
d0fb5eae 5142 watchpoint_del_at_next_stop (b);
c906108c
SS
5143
5144 return WP_DELETED;
5145 }
5146}
5147
18a18393 5148/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5149 breakpoint location BL. This function does not check if we should
5150 stop, only if BL explains the stop. */
5151
18a18393 5152static int
6c95b8df 5153bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5154 struct address_space *aspace, CORE_ADDR bp_addr,
5155 const struct target_waitstatus *ws)
18a18393
VP
5156{
5157 struct breakpoint *b = bl->owner;
5158
348d480f 5159 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5160 gdb_assert (b != NULL);
5161
09ac7c10 5162 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5163}
5164
3a5c3e22
PA
5165/* Determine if the watched values have actually changed, and we
5166 should stop. If not, set BS->stop to 0. */
5167
18a18393
VP
5168static void
5169bpstat_check_watchpoint (bpstat bs)
5170{
2bdf28a0 5171 const struct bp_location *bl;
3a5c3e22 5172 struct watchpoint *b;
2bdf28a0
JK
5173
5174 /* BS is built for existing struct breakpoint. */
f431efe5 5175 bl = bs->bp_location_at;
2bdf28a0 5176 gdb_assert (bl != NULL);
3a5c3e22 5177 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5178 gdb_assert (b != NULL);
18a18393 5179
18a18393 5180 {
18a18393
VP
5181 int must_check_value = 0;
5182
3a5c3e22 5183 if (b->base.type == bp_watchpoint)
18a18393
VP
5184 /* For a software watchpoint, we must always check the
5185 watched value. */
5186 must_check_value = 1;
5187 else if (b->watchpoint_triggered == watch_triggered_yes)
5188 /* We have a hardware watchpoint (read, write, or access)
5189 and the target earlier reported an address watched by
5190 this watchpoint. */
5191 must_check_value = 1;
5192 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5193 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5194 /* We were stopped by a hardware watchpoint, but the target could
5195 not report the data address. We must check the watchpoint's
5196 value. Access and read watchpoints are out of luck; without
5197 a data address, we can't figure it out. */
5198 must_check_value = 1;
3a5c3e22 5199
18a18393
VP
5200 if (must_check_value)
5201 {
3e43a32a
MS
5202 char *message
5203 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5204 b->base.number);
18a18393
VP
5205 struct cleanup *cleanups = make_cleanup (xfree, message);
5206 int e = catch_errors (watchpoint_check, bs, message,
5207 RETURN_MASK_ALL);
5208 do_cleanups (cleanups);
5209 switch (e)
5210 {
5211 case WP_DELETED:
5212 /* We've already printed what needs to be printed. */
5213 bs->print_it = print_it_done;
5214 /* Stop. */
5215 break;
60e1c644
PA
5216 case WP_IGNORE:
5217 bs->print_it = print_it_noop;
5218 bs->stop = 0;
5219 break;
18a18393 5220 case WP_VALUE_CHANGED:
3a5c3e22 5221 if (b->base.type == bp_read_watchpoint)
18a18393 5222 {
85d721b8
PA
5223 /* There are two cases to consider here:
5224
4a64f543 5225 1. We're watching the triggered memory for reads.
85d721b8
PA
5226 In that case, trust the target, and always report
5227 the watchpoint hit to the user. Even though
5228 reads don't cause value changes, the value may
5229 have changed since the last time it was read, and
5230 since we're not trapping writes, we will not see
5231 those, and as such we should ignore our notion of
5232 old value.
5233
4a64f543 5234 2. We're watching the triggered memory for both
85d721b8
PA
5235 reads and writes. There are two ways this may
5236 happen:
5237
4a64f543 5238 2.1. This is a target that can't break on data
85d721b8
PA
5239 reads only, but can break on accesses (reads or
5240 writes), such as e.g., x86. We detect this case
5241 at the time we try to insert read watchpoints.
5242
4a64f543 5243 2.2. Otherwise, the target supports read
85d721b8
PA
5244 watchpoints, but, the user set an access or write
5245 watchpoint watching the same memory as this read
5246 watchpoint.
5247
5248 If we're watching memory writes as well as reads,
5249 ignore watchpoint hits when we find that the
5250 value hasn't changed, as reads don't cause
5251 changes. This still gives false positives when
5252 the program writes the same value to memory as
5253 what there was already in memory (we will confuse
5254 it for a read), but it's much better than
5255 nothing. */
5256
5257 int other_write_watchpoint = 0;
5258
5259 if (bl->watchpoint_type == hw_read)
5260 {
5261 struct breakpoint *other_b;
5262
5263 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5264 if (other_b->type == bp_hardware_watchpoint
5265 || other_b->type == bp_access_watchpoint)
85d721b8 5266 {
3a5c3e22
PA
5267 struct watchpoint *other_w =
5268 (struct watchpoint *) other_b;
5269
5270 if (other_w->watchpoint_triggered
5271 == watch_triggered_yes)
5272 {
5273 other_write_watchpoint = 1;
5274 break;
5275 }
85d721b8
PA
5276 }
5277 }
5278
5279 if (other_write_watchpoint
5280 || bl->watchpoint_type == hw_access)
5281 {
5282 /* We're watching the same memory for writes,
5283 and the value changed since the last time we
5284 updated it, so this trap must be for a write.
5285 Ignore it. */
5286 bs->print_it = print_it_noop;
5287 bs->stop = 0;
5288 }
18a18393
VP
5289 }
5290 break;
5291 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5292 if (b->base.type == bp_hardware_watchpoint
5293 || b->base.type == bp_watchpoint)
18a18393
VP
5294 {
5295 /* Don't stop: write watchpoints shouldn't fire if
5296 the value hasn't changed. */
5297 bs->print_it = print_it_noop;
5298 bs->stop = 0;
5299 }
5300 /* Stop. */
5301 break;
5302 default:
5303 /* Can't happen. */
5304 case 0:
5305 /* Error from catch_errors. */
3a5c3e22 5306 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5307 watchpoint_del_at_next_stop (b);
18a18393
VP
5308 /* We've already printed what needs to be printed. */
5309 bs->print_it = print_it_done;
5310 break;
5311 }
5312 }
5313 else /* must_check_value == 0 */
5314 {
5315 /* This is a case where some watchpoint(s) triggered, but
5316 not at the address of this watchpoint, or else no
5317 watchpoint triggered after all. So don't print
5318 anything for this watchpoint. */
5319 bs->print_it = print_it_noop;
5320 bs->stop = 0;
5321 }
5322 }
5323}
5324
7d4df6a4
DE
5325/* For breakpoints that are currently marked as telling gdb to stop,
5326 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5327 of breakpoint referred to by BS. If we should not stop for this
5328 breakpoint, set BS->stop to 0. */
f431efe5 5329
18a18393
VP
5330static void
5331bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5332{
2bdf28a0
JK
5333 const struct bp_location *bl;
5334 struct breakpoint *b;
7d4df6a4
DE
5335 int value_is_zero = 0;
5336 struct expression *cond;
5337
5338 gdb_assert (bs->stop);
2bdf28a0
JK
5339
5340 /* BS is built for existing struct breakpoint. */
f431efe5 5341 bl = bs->bp_location_at;
2bdf28a0 5342 gdb_assert (bl != NULL);
f431efe5 5343 b = bs->breakpoint_at;
2bdf28a0 5344 gdb_assert (b != NULL);
18a18393 5345
b775012e
LM
5346 /* Even if the target evaluated the condition on its end and notified GDB, we
5347 need to do so again since GDB does not know if we stopped due to a
5348 breakpoint or a single step breakpoint. */
5349
18a18393 5350 if (frame_id_p (b->frame_id)
edb3359d 5351 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5352 {
7d4df6a4
DE
5353 bs->stop = 0;
5354 return;
5355 }
60e1c644 5356
12ab52e9
PA
5357 /* If this is a thread/task-specific breakpoint, don't waste cpu
5358 evaluating the condition if this isn't the specified
5359 thread/task. */
5360 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5361 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5362
6c1b0f7b
DE
5363 {
5364 bs->stop = 0;
5365 return;
5366 }
5367
6dddc817
DE
5368 /* Evaluate extension language breakpoints that have a "stop" method
5369 implemented. */
5370 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5371
7d4df6a4
DE
5372 if (is_watchpoint (b))
5373 {
5374 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5375
7d4df6a4
DE
5376 cond = w->cond_exp;
5377 }
5378 else
5379 cond = bl->cond;
60e1c644 5380
7d4df6a4
DE
5381 if (cond && b->disposition != disp_del_at_next_stop)
5382 {
5383 int within_current_scope = 1;
5384 struct watchpoint * w;
60e1c644 5385
7d4df6a4
DE
5386 /* We use value_mark and value_free_to_mark because it could
5387 be a long time before we return to the command level and
5388 call free_all_values. We can't call free_all_values
5389 because we might be in the middle of evaluating a
5390 function call. */
5391 struct value *mark = value_mark ();
5392
5393 if (is_watchpoint (b))
5394 w = (struct watchpoint *) b;
5395 else
5396 w = NULL;
5397
5398 /* Need to select the frame, with all that implies so that
5399 the conditions will have the right context. Because we
5400 use the frame, we will not see an inlined function's
5401 variables when we arrive at a breakpoint at the start
5402 of the inlined function; the current frame will be the
5403 call site. */
5404 if (w == NULL || w->cond_exp_valid_block == NULL)
5405 select_frame (get_current_frame ());
5406 else
18a18393 5407 {
7d4df6a4
DE
5408 struct frame_info *frame;
5409
5410 /* For local watchpoint expressions, which particular
5411 instance of a local is being watched matters, so we
5412 keep track of the frame to evaluate the expression
5413 in. To evaluate the condition however, it doesn't
5414 really matter which instantiation of the function
5415 where the condition makes sense triggers the
5416 watchpoint. This allows an expression like "watch
5417 global if q > 10" set in `func', catch writes to
5418 global on all threads that call `func', or catch
5419 writes on all recursive calls of `func' by a single
5420 thread. We simply always evaluate the condition in
5421 the innermost frame that's executing where it makes
5422 sense to evaluate the condition. It seems
5423 intuitive. */
5424 frame = block_innermost_frame (w->cond_exp_valid_block);
5425 if (frame != NULL)
5426 select_frame (frame);
5427 else
5428 within_current_scope = 0;
18a18393 5429 }
7d4df6a4
DE
5430 if (within_current_scope)
5431 value_is_zero
5432 = catch_errors (breakpoint_cond_eval, cond,
5433 "Error in testing breakpoint condition:\n",
5434 RETURN_MASK_ALL);
5435 else
18a18393 5436 {
7d4df6a4
DE
5437 warning (_("Watchpoint condition cannot be tested "
5438 "in the current scope"));
5439 /* If we failed to set the right context for this
5440 watchpoint, unconditionally report it. */
5441 value_is_zero = 0;
18a18393 5442 }
7d4df6a4
DE
5443 /* FIXME-someday, should give breakpoint #. */
5444 value_free_to_mark (mark);
18a18393 5445 }
7d4df6a4
DE
5446
5447 if (cond && value_is_zero)
5448 {
5449 bs->stop = 0;
5450 }
7d4df6a4
DE
5451 else if (b->ignore_count > 0)
5452 {
5453 b->ignore_count--;
5454 bs->stop = 0;
5455 /* Increase the hit count even though we don't stop. */
5456 ++(b->hit_count);
5457 observer_notify_breakpoint_modified (b);
5458 }
18a18393
VP
5459}
5460
1cf4d951
PA
5461/* Returns true if we need to track moribund locations of LOC's type
5462 on the current target. */
5463
5464static int
5465need_moribund_for_location_type (struct bp_location *loc)
5466{
5467 return ((loc->loc_type == bp_loc_software_breakpoint
5468 && !target_supports_stopped_by_sw_breakpoint ())
5469 || (loc->loc_type == bp_loc_hardware_breakpoint
5470 && !target_supports_stopped_by_hw_breakpoint ()));
5471}
5472
18a18393 5473
9709f61c 5474/* Get a bpstat associated with having just stopped at address
d983da9c 5475 BP_ADDR in thread PTID.
c906108c 5476
d983da9c 5477 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5478 don't understand this stop. Result is a chain of bpstat's such
5479 that:
c906108c 5480
c5aa993b 5481 if we don't understand the stop, the result is a null pointer.
c906108c 5482
c5aa993b 5483 if we understand why we stopped, the result is not null.
c906108c 5484
c5aa993b
JM
5485 Each element of the chain refers to a particular breakpoint or
5486 watchpoint at which we have stopped. (We may have stopped for
5487 several reasons concurrently.)
c906108c 5488
c5aa993b
JM
5489 Each element of the chain has valid next, breakpoint_at,
5490 commands, FIXME??? fields. */
c906108c
SS
5491
5492bpstat
6c95b8df 5493bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5494 CORE_ADDR bp_addr, ptid_t ptid,
5495 const struct target_waitstatus *ws)
c906108c 5496{
0d381245 5497 struct breakpoint *b = NULL;
afe38095 5498 struct bp_location *bl;
20874c92 5499 struct bp_location *loc;
5760d0ab
JK
5500 /* First item of allocated bpstat's. */
5501 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5502 /* Pointer to the last thing in the chain currently. */
5760d0ab 5503 bpstat bs;
20874c92 5504 int ix;
429374b8 5505 int need_remove_insert;
f431efe5 5506 int removed_any;
c906108c 5507
f431efe5
PA
5508 /* First, build the bpstat chain with locations that explain a
5509 target stop, while being careful to not set the target running,
5510 as that may invalidate locations (in particular watchpoint
5511 locations are recreated). Resuming will happen here with
5512 breakpoint conditions or watchpoint expressions that include
5513 inferior function calls. */
c5aa993b 5514
429374b8
JK
5515 ALL_BREAKPOINTS (b)
5516 {
1a853c52 5517 if (!breakpoint_enabled (b))
429374b8 5518 continue;
a5606eee 5519
429374b8
JK
5520 for (bl = b->loc; bl != NULL; bl = bl->next)
5521 {
4a64f543
MS
5522 /* For hardware watchpoints, we look only at the first
5523 location. The watchpoint_check function will work on the
5524 entire expression, not the individual locations. For
5525 read watchpoints, the watchpoints_triggered function has
5526 checked all locations already. */
429374b8
JK
5527 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5528 break;
18a18393 5529
f6592439 5530 if (!bl->enabled || bl->shlib_disabled)
429374b8 5531 continue;
c5aa993b 5532
09ac7c10 5533 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5534 continue;
c5aa993b 5535
4a64f543
MS
5536 /* Come here if it's a watchpoint, or if the break address
5537 matches. */
c5aa993b 5538
4a64f543
MS
5539 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5540 explain stop. */
c5aa993b 5541
f431efe5
PA
5542 /* Assume we stop. Should we find a watchpoint that is not
5543 actually triggered, or if the condition of the breakpoint
5544 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5545 bs->stop = 1;
5546 bs->print = 1;
d983da9c 5547
f431efe5
PA
5548 /* If this is a scope breakpoint, mark the associated
5549 watchpoint as triggered so that we will handle the
5550 out-of-scope event. We'll get to the watchpoint next
5551 iteration. */
d0fb5eae 5552 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5553 {
5554 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5555
5556 w->watchpoint_triggered = watch_triggered_yes;
5557 }
f431efe5
PA
5558 }
5559 }
5560
7c16b83e 5561 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5562 if (!target_supports_stopped_by_sw_breakpoint ()
5563 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5564 {
1cf4d951 5565 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5566 {
1cf4d951
PA
5567 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5568 && need_moribund_for_location_type (loc))
5569 {
5570 bs = bpstat_alloc (loc, &bs_link);
5571 /* For hits of moribund locations, we should just proceed. */
5572 bs->stop = 0;
5573 bs->print = 0;
5574 bs->print_it = print_it_noop;
5575 }
f431efe5
PA
5576 }
5577 }
5578
edcc5120
TT
5579 /* A bit of special processing for shlib breakpoints. We need to
5580 process solib loading here, so that the lists of loaded and
5581 unloaded libraries are correct before we handle "catch load" and
5582 "catch unload". */
5583 for (bs = bs_head; bs != NULL; bs = bs->next)
5584 {
5d268276 5585 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5586 {
5587 handle_solib_event ();
5588 break;
5589 }
5590 }
5591
f431efe5
PA
5592 /* Now go through the locations that caused the target to stop, and
5593 check whether we're interested in reporting this stop to higher
5594 layers, or whether we should resume the target transparently. */
5595
5596 removed_any = 0;
5597
5760d0ab 5598 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5599 {
5600 if (!bs->stop)
5601 continue;
5602
f431efe5 5603 b = bs->breakpoint_at;
348d480f
PA
5604 b->ops->check_status (bs);
5605 if (bs->stop)
28010a5d 5606 {
348d480f 5607 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5608
429374b8
JK
5609 if (bs->stop)
5610 {
5611 ++(b->hit_count);
8d3788bd 5612 observer_notify_breakpoint_modified (b);
c906108c 5613
4a64f543 5614 /* We will stop here. */
429374b8
JK
5615 if (b->disposition == disp_disable)
5616 {
816338b5 5617 --(b->enable_count);
1a853c52 5618 if (b->enable_count <= 0)
429374b8 5619 b->enable_state = bp_disabled;
f431efe5 5620 removed_any = 1;
429374b8
JK
5621 }
5622 if (b->silent)
5623 bs->print = 0;
5624 bs->commands = b->commands;
9add0f1b 5625 incref_counted_command_line (bs->commands);
abf85f46
JK
5626 if (command_line_is_silent (bs->commands
5627 ? bs->commands->commands : NULL))
5628 bs->print = 0;
9d6e6e84
HZ
5629
5630 b->ops->after_condition_true (bs);
429374b8
JK
5631 }
5632
348d480f 5633 }
a9b3a50f
PA
5634
5635 /* Print nothing for this entry if we don't stop or don't
5636 print. */
5637 if (!bs->stop || !bs->print)
5638 bs->print_it = print_it_noop;
429374b8 5639 }
876fa593 5640
d983da9c
DJ
5641 /* If we aren't stopping, the value of some hardware watchpoint may
5642 not have changed, but the intermediate memory locations we are
5643 watching may have. Don't bother if we're stopping; this will get
5644 done later. */
d832cb68 5645 need_remove_insert = 0;
5760d0ab
JK
5646 if (! bpstat_causes_stop (bs_head))
5647 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5648 if (!bs->stop
f431efe5
PA
5649 && bs->breakpoint_at
5650 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5651 {
3a5c3e22
PA
5652 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5653
5654 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5655 need_remove_insert = 1;
d983da9c
DJ
5656 }
5657
d832cb68 5658 if (need_remove_insert)
44702360 5659 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5660 else if (removed_any)
44702360 5661 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5662
5760d0ab 5663 return bs_head;
c906108c 5664}
628fe4e4
JK
5665
5666static void
5667handle_jit_event (void)
5668{
5669 struct frame_info *frame;
5670 struct gdbarch *gdbarch;
5671
5672 /* Switch terminal for any messages produced by
5673 breakpoint_re_set. */
5674 target_terminal_ours_for_output ();
5675
5676 frame = get_current_frame ();
5677 gdbarch = get_frame_arch (frame);
5678
5679 jit_event_handler (gdbarch);
5680
5681 target_terminal_inferior ();
5682}
5683
5684/* Prepare WHAT final decision for infrun. */
5685
5686/* Decide what infrun needs to do with this bpstat. */
5687
c906108c 5688struct bpstat_what
0e30163f 5689bpstat_what (bpstat bs_head)
c906108c 5690{
c906108c 5691 struct bpstat_what retval;
628fe4e4 5692 int jit_event = 0;
0e30163f 5693 bpstat bs;
c906108c 5694
628fe4e4 5695 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5696 retval.call_dummy = STOP_NONE;
186c406b 5697 retval.is_longjmp = 0;
628fe4e4 5698
0e30163f 5699 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5700 {
628fe4e4
JK
5701 /* Extract this BS's action. After processing each BS, we check
5702 if its action overrides all we've seem so far. */
5703 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5704 enum bptype bptype;
5705
c906108c 5706 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5707 {
5708 /* I suspect this can happen if it was a momentary
5709 breakpoint which has since been deleted. */
5710 bptype = bp_none;
5711 }
20874c92 5712 else
f431efe5 5713 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5714
5715 switch (bptype)
c906108c
SS
5716 {
5717 case bp_none:
628fe4e4 5718 break;
c906108c
SS
5719 case bp_breakpoint:
5720 case bp_hardware_breakpoint:
7c16b83e 5721 case bp_single_step:
c906108c
SS
5722 case bp_until:
5723 case bp_finish:
a9b3a50f 5724 case bp_shlib_event:
c906108c
SS
5725 if (bs->stop)
5726 {
5727 if (bs->print)
628fe4e4 5728 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5729 else
628fe4e4 5730 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5731 }
5732 else
628fe4e4 5733 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5734 break;
5735 case bp_watchpoint:
5736 case bp_hardware_watchpoint:
5737 case bp_read_watchpoint:
5738 case bp_access_watchpoint:
5739 if (bs->stop)
5740 {
5741 if (bs->print)
628fe4e4 5742 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5743 else
628fe4e4 5744 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5745 }
5746 else
628fe4e4
JK
5747 {
5748 /* There was a watchpoint, but we're not stopping.
5749 This requires no further action. */
5750 }
c906108c
SS
5751 break;
5752 case bp_longjmp:
e2e4d78b 5753 case bp_longjmp_call_dummy:
186c406b 5754 case bp_exception:
628fe4e4 5755 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5756 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5757 break;
5758 case bp_longjmp_resume:
186c406b 5759 case bp_exception_resume:
628fe4e4 5760 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5761 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5762 break;
5763 case bp_step_resume:
5764 if (bs->stop)
628fe4e4
JK
5765 this_action = BPSTAT_WHAT_STEP_RESUME;
5766 else
c906108c 5767 {
628fe4e4
JK
5768 /* It is for the wrong frame. */
5769 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5770 }
c906108c 5771 break;
2c03e5be
PA
5772 case bp_hp_step_resume:
5773 if (bs->stop)
5774 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5775 else
5776 {
5777 /* It is for the wrong frame. */
5778 this_action = BPSTAT_WHAT_SINGLE;
5779 }
5780 break;
c906108c 5781 case bp_watchpoint_scope:
c4093a6a 5782 case bp_thread_event:
1900040c 5783 case bp_overlay_event:
0fd8e87f 5784 case bp_longjmp_master:
aa7d318d 5785 case bp_std_terminate_master:
186c406b 5786 case bp_exception_master:
628fe4e4 5787 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5788 break;
ce78b96d 5789 case bp_catchpoint:
c5aa993b
JM
5790 if (bs->stop)
5791 {
5792 if (bs->print)
628fe4e4 5793 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5794 else
628fe4e4 5795 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5796 }
5797 else
628fe4e4
JK
5798 {
5799 /* There was a catchpoint, but we're not stopping.
5800 This requires no further action. */
5801 }
5802 break;
628fe4e4
JK
5803 case bp_jit_event:
5804 jit_event = 1;
5805 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5806 break;
c906108c 5807 case bp_call_dummy:
53a5351d
JM
5808 /* Make sure the action is stop (silent or noisy),
5809 so infrun.c pops the dummy frame. */
aa7d318d 5810 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5811 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5812 break;
5813 case bp_std_terminate:
5814 /* Make sure the action is stop (silent or noisy),
5815 so infrun.c pops the dummy frame. */
aa7d318d 5816 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5817 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5818 break;
1042e4c0 5819 case bp_tracepoint:
7a697b8d 5820 case bp_fast_tracepoint:
0fb4aa4b 5821 case bp_static_tracepoint:
1042e4c0
SS
5822 /* Tracepoint hits should not be reported back to GDB, and
5823 if one got through somehow, it should have been filtered
5824 out already. */
5825 internal_error (__FILE__, __LINE__,
7a697b8d 5826 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5827 break;
5828 case bp_gnu_ifunc_resolver:
5829 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5830 this_action = BPSTAT_WHAT_SINGLE;
5831 break;
5832 case bp_gnu_ifunc_resolver_return:
5833 /* The breakpoint will be removed, execution will restart from the
5834 PC of the former breakpoint. */
5835 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5836 break;
e7e0cddf
SS
5837
5838 case bp_dprintf:
a11cfd87
HZ
5839 if (bs->stop)
5840 this_action = BPSTAT_WHAT_STOP_SILENT;
5841 else
5842 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5843 break;
5844
628fe4e4
JK
5845 default:
5846 internal_error (__FILE__, __LINE__,
5847 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5848 }
628fe4e4
JK
5849
5850 retval.main_action = max (retval.main_action, this_action);
c906108c 5851 }
628fe4e4 5852
0e30163f
JK
5853 /* These operations may affect the bs->breakpoint_at state so they are
5854 delayed after MAIN_ACTION is decided above. */
5855
628fe4e4
JK
5856 if (jit_event)
5857 {
5858 if (debug_infrun)
5859 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5860
5861 handle_jit_event ();
5862 }
5863
0e30163f
JK
5864 for (bs = bs_head; bs != NULL; bs = bs->next)
5865 {
5866 struct breakpoint *b = bs->breakpoint_at;
5867
5868 if (b == NULL)
5869 continue;
5870 switch (b->type)
5871 {
5872 case bp_gnu_ifunc_resolver:
5873 gnu_ifunc_resolver_stop (b);
5874 break;
5875 case bp_gnu_ifunc_resolver_return:
5876 gnu_ifunc_resolver_return_stop (b);
5877 break;
5878 }
5879 }
5880
c906108c
SS
5881 return retval;
5882}
5883
5884/* Nonzero if we should step constantly (e.g. watchpoints on machines
5885 without hardware support). This isn't related to a specific bpstat,
5886 just to things like whether watchpoints are set. */
5887
c5aa993b 5888int
fba45db2 5889bpstat_should_step (void)
c906108c
SS
5890{
5891 struct breakpoint *b;
cc59ec59 5892
c906108c 5893 ALL_BREAKPOINTS (b)
717a8278 5894 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5895 return 1;
c906108c
SS
5896 return 0;
5897}
5898
67822962
PA
5899int
5900bpstat_causes_stop (bpstat bs)
5901{
5902 for (; bs != NULL; bs = bs->next)
5903 if (bs->stop)
5904 return 1;
5905
5906 return 0;
5907}
5908
c906108c 5909\f
c5aa993b 5910
170b53b2
UW
5911/* Compute a string of spaces suitable to indent the next line
5912 so it starts at the position corresponding to the table column
5913 named COL_NAME in the currently active table of UIOUT. */
5914
5915static char *
5916wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5917{
5918 static char wrap_indent[80];
5919 int i, total_width, width, align;
5920 char *text;
5921
5922 total_width = 0;
5923 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5924 {
5925 if (strcmp (text, col_name) == 0)
5926 {
5927 gdb_assert (total_width < sizeof wrap_indent);
5928 memset (wrap_indent, ' ', total_width);
5929 wrap_indent[total_width] = 0;
5930
5931 return wrap_indent;
5932 }
5933
5934 total_width += width + 1;
5935 }
5936
5937 return NULL;
5938}
5939
b775012e
LM
5940/* Determine if the locations of this breakpoint will have their conditions
5941 evaluated by the target, host or a mix of both. Returns the following:
5942
5943 "host": Host evals condition.
5944 "host or target": Host or Target evals condition.
5945 "target": Target evals condition.
5946*/
5947
5948static const char *
5949bp_condition_evaluator (struct breakpoint *b)
5950{
5951 struct bp_location *bl;
5952 char host_evals = 0;
5953 char target_evals = 0;
5954
5955 if (!b)
5956 return NULL;
5957
5958 if (!is_breakpoint (b))
5959 return NULL;
5960
5961 if (gdb_evaluates_breakpoint_condition_p ()
5962 || !target_supports_evaluation_of_breakpoint_conditions ())
5963 return condition_evaluation_host;
5964
5965 for (bl = b->loc; bl; bl = bl->next)
5966 {
5967 if (bl->cond_bytecode)
5968 target_evals++;
5969 else
5970 host_evals++;
5971 }
5972
5973 if (host_evals && target_evals)
5974 return condition_evaluation_both;
5975 else if (target_evals)
5976 return condition_evaluation_target;
5977 else
5978 return condition_evaluation_host;
5979}
5980
5981/* Determine the breakpoint location's condition evaluator. This is
5982 similar to bp_condition_evaluator, but for locations. */
5983
5984static const char *
5985bp_location_condition_evaluator (struct bp_location *bl)
5986{
5987 if (bl && !is_breakpoint (bl->owner))
5988 return NULL;
5989
5990 if (gdb_evaluates_breakpoint_condition_p ()
5991 || !target_supports_evaluation_of_breakpoint_conditions ())
5992 return condition_evaluation_host;
5993
5994 if (bl && bl->cond_bytecode)
5995 return condition_evaluation_target;
5996 else
5997 return condition_evaluation_host;
5998}
5999
859825b8
JK
6000/* Print the LOC location out of the list of B->LOC locations. */
6001
170b53b2
UW
6002static void
6003print_breakpoint_location (struct breakpoint *b,
6004 struct bp_location *loc)
0d381245 6005{
79a45e25 6006 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6007 struct cleanup *old_chain = save_current_program_space ();
6008
859825b8
JK
6009 if (loc != NULL && loc->shlib_disabled)
6010 loc = NULL;
6011
6c95b8df
PA
6012 if (loc != NULL)
6013 set_current_program_space (loc->pspace);
6014
56435ebe
TT
6015 if (b->display_canonical)
6016 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 6017 else if (loc && loc->symtab)
0d381245
VP
6018 {
6019 struct symbol *sym
6020 = find_pc_sect_function (loc->address, loc->section);
6021 if (sym)
6022 {
6023 ui_out_text (uiout, "in ");
6024 ui_out_field_string (uiout, "func",
6025 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6026 ui_out_text (uiout, " ");
6027 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6028 ui_out_text (uiout, "at ");
0d381245 6029 }
05cba821
JK
6030 ui_out_field_string (uiout, "file",
6031 symtab_to_filename_for_display (loc->symtab));
0d381245 6032 ui_out_text (uiout, ":");
05cba821 6033
0d381245 6034 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6035 ui_out_field_string (uiout, "fullname",
6036 symtab_to_fullname (loc->symtab));
0d381245 6037
f8eba3c6 6038 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6039 }
859825b8 6040 else if (loc)
0d381245 6041 {
f99d8bf4
PA
6042 struct ui_file *stb = mem_fileopen ();
6043 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6044
f99d8bf4 6045 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6046 demangle, "");
0d381245 6047 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6048
6049 do_cleanups (stb_chain);
0d381245 6050 }
859825b8
JK
6051 else
6052 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 6053
b775012e
LM
6054 if (loc && is_breakpoint (b)
6055 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6056 && bp_condition_evaluator (b) == condition_evaluation_both)
6057 {
6058 ui_out_text (uiout, " (");
6059 ui_out_field_string (uiout, "evaluated-by",
6060 bp_location_condition_evaluator (loc));
6061 ui_out_text (uiout, ")");
6062 }
6063
6c95b8df 6064 do_cleanups (old_chain);
0d381245
VP
6065}
6066
269b11a2
PA
6067static const char *
6068bptype_string (enum bptype type)
c906108c 6069{
c4093a6a
JM
6070 struct ep_type_description
6071 {
6072 enum bptype type;
6073 char *description;
6074 };
6075 static struct ep_type_description bptypes[] =
c906108c 6076 {
c5aa993b
JM
6077 {bp_none, "?deleted?"},
6078 {bp_breakpoint, "breakpoint"},
c906108c 6079 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6080 {bp_single_step, "sw single-step"},
c5aa993b
JM
6081 {bp_until, "until"},
6082 {bp_finish, "finish"},
6083 {bp_watchpoint, "watchpoint"},
c906108c 6084 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6085 {bp_read_watchpoint, "read watchpoint"},
6086 {bp_access_watchpoint, "acc watchpoint"},
6087 {bp_longjmp, "longjmp"},
6088 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6089 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6090 {bp_exception, "exception"},
6091 {bp_exception_resume, "exception resume"},
c5aa993b 6092 {bp_step_resume, "step resume"},
2c03e5be 6093 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6094 {bp_watchpoint_scope, "watchpoint scope"},
6095 {bp_call_dummy, "call dummy"},
aa7d318d 6096 {bp_std_terminate, "std::terminate"},
c5aa993b 6097 {bp_shlib_event, "shlib events"},
c4093a6a 6098 {bp_thread_event, "thread events"},
1900040c 6099 {bp_overlay_event, "overlay events"},
0fd8e87f 6100 {bp_longjmp_master, "longjmp master"},
aa7d318d 6101 {bp_std_terminate_master, "std::terminate master"},
186c406b 6102 {bp_exception_master, "exception master"},
ce78b96d 6103 {bp_catchpoint, "catchpoint"},
1042e4c0 6104 {bp_tracepoint, "tracepoint"},
7a697b8d 6105 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6106 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6107 {bp_dprintf, "dprintf"},
4efc6507 6108 {bp_jit_event, "jit events"},
0e30163f
JK
6109 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6110 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6111 };
269b11a2
PA
6112
6113 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6114 || ((int) type != bptypes[(int) type].type))
6115 internal_error (__FILE__, __LINE__,
6116 _("bptypes table does not describe type #%d."),
6117 (int) type);
6118
6119 return bptypes[(int) type].description;
6120}
6121
998580f1
MK
6122/* For MI, output a field named 'thread-groups' with a list as the value.
6123 For CLI, prefix the list with the string 'inf'. */
6124
6125static void
6126output_thread_groups (struct ui_out *uiout,
6127 const char *field_name,
6128 VEC(int) *inf_num,
6129 int mi_only)
6130{
752eb8b4 6131 struct cleanup *back_to;
998580f1
MK
6132 int is_mi = ui_out_is_mi_like_p (uiout);
6133 int inf;
6134 int i;
6135
6136 /* For backward compatibility, don't display inferiors in CLI unless
6137 there are several. Always display them for MI. */
6138 if (!is_mi && mi_only)
6139 return;
6140
752eb8b4
TT
6141 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6142
998580f1
MK
6143 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6144 {
6145 if (is_mi)
6146 {
6147 char mi_group[10];
6148
6149 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6150 ui_out_field_string (uiout, NULL, mi_group);
6151 }
6152 else
6153 {
6154 if (i == 0)
6155 ui_out_text (uiout, " inf ");
6156 else
6157 ui_out_text (uiout, ", ");
6158
6159 ui_out_text (uiout, plongest (inf));
6160 }
6161 }
6162
6163 do_cleanups (back_to);
6164}
6165
269b11a2
PA
6166/* Print B to gdb_stdout. */
6167
6168static void
6169print_one_breakpoint_location (struct breakpoint *b,
6170 struct bp_location *loc,
6171 int loc_number,
6172 struct bp_location **last_loc,
269b11a2
PA
6173 int allflag)
6174{
6175 struct command_line *l;
c2c6d25f 6176 static char bpenables[] = "nynny";
c906108c 6177
79a45e25 6178 struct ui_out *uiout = current_uiout;
0d381245
VP
6179 int header_of_multiple = 0;
6180 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6181 struct value_print_options opts;
6182
6183 get_user_print_options (&opts);
0d381245
VP
6184
6185 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6186 /* See comment in print_one_breakpoint concerning treatment of
6187 breakpoints with single disabled location. */
0d381245
VP
6188 if (loc == NULL
6189 && (b->loc != NULL
6190 && (b->loc->next != NULL || !b->loc->enabled)))
6191 header_of_multiple = 1;
6192 if (loc == NULL)
6193 loc = b->loc;
6194
c4093a6a
JM
6195 annotate_record ();
6196
6197 /* 1 */
6198 annotate_field (0);
0d381245
VP
6199 if (part_of_multiple)
6200 {
6201 char *formatted;
0c6773c1 6202 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6203 ui_out_field_string (uiout, "number", formatted);
6204 xfree (formatted);
6205 }
6206 else
6207 {
6208 ui_out_field_int (uiout, "number", b->number);
6209 }
c4093a6a
JM
6210
6211 /* 2 */
6212 annotate_field (1);
0d381245
VP
6213 if (part_of_multiple)
6214 ui_out_field_skip (uiout, "type");
269b11a2
PA
6215 else
6216 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6217
6218 /* 3 */
6219 annotate_field (2);
0d381245
VP
6220 if (part_of_multiple)
6221 ui_out_field_skip (uiout, "disp");
6222 else
2cec12e5 6223 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6224
c4093a6a
JM
6225
6226 /* 4 */
6227 annotate_field (3);
0d381245 6228 if (part_of_multiple)
54e52265 6229 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6230 else
4a64f543
MS
6231 ui_out_field_fmt (uiout, "enabled", "%c",
6232 bpenables[(int) b->enable_state]);
54e52265 6233 ui_out_spaces (uiout, 2);
0d381245 6234
c4093a6a
JM
6235
6236 /* 5 and 6 */
3086aeae 6237 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6238 {
4a64f543
MS
6239 /* Although the print_one can possibly print all locations,
6240 calling it here is not likely to get any nice result. So,
6241 make sure there's just one location. */
0d381245 6242 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6243 b->ops->print_one (b, last_loc);
0d381245 6244 }
3086aeae
DJ
6245 else
6246 switch (b->type)
6247 {
6248 case bp_none:
6249 internal_error (__FILE__, __LINE__,
e2e0b3e5 6250 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6251 break;
c906108c 6252
3086aeae
DJ
6253 case bp_watchpoint:
6254 case bp_hardware_watchpoint:
6255 case bp_read_watchpoint:
6256 case bp_access_watchpoint:
3a5c3e22
PA
6257 {
6258 struct watchpoint *w = (struct watchpoint *) b;
6259
6260 /* Field 4, the address, is omitted (which makes the columns
6261 not line up too nicely with the headers, but the effect
6262 is relatively readable). */
6263 if (opts.addressprint)
6264 ui_out_field_skip (uiout, "addr");
6265 annotate_field (5);
6266 ui_out_field_string (uiout, "what", w->exp_string);
6267 }
3086aeae
DJ
6268 break;
6269
3086aeae
DJ
6270 case bp_breakpoint:
6271 case bp_hardware_breakpoint:
7c16b83e 6272 case bp_single_step:
3086aeae
DJ
6273 case bp_until:
6274 case bp_finish:
6275 case bp_longjmp:
6276 case bp_longjmp_resume:
e2e4d78b 6277 case bp_longjmp_call_dummy:
186c406b
TT
6278 case bp_exception:
6279 case bp_exception_resume:
3086aeae 6280 case bp_step_resume:
2c03e5be 6281 case bp_hp_step_resume:
3086aeae
DJ
6282 case bp_watchpoint_scope:
6283 case bp_call_dummy:
aa7d318d 6284 case bp_std_terminate:
3086aeae
DJ
6285 case bp_shlib_event:
6286 case bp_thread_event:
6287 case bp_overlay_event:
0fd8e87f 6288 case bp_longjmp_master:
aa7d318d 6289 case bp_std_terminate_master:
186c406b 6290 case bp_exception_master:
1042e4c0 6291 case bp_tracepoint:
7a697b8d 6292 case bp_fast_tracepoint:
0fb4aa4b 6293 case bp_static_tracepoint:
e7e0cddf 6294 case bp_dprintf:
4efc6507 6295 case bp_jit_event:
0e30163f
JK
6296 case bp_gnu_ifunc_resolver:
6297 case bp_gnu_ifunc_resolver_return:
79a45b7d 6298 if (opts.addressprint)
3086aeae
DJ
6299 {
6300 annotate_field (4);
54e52265 6301 if (header_of_multiple)
0d381245 6302 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6303 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6304 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6305 else
5af949e3
UW
6306 ui_out_field_core_addr (uiout, "addr",
6307 loc->gdbarch, loc->address);
3086aeae
DJ
6308 }
6309 annotate_field (5);
0d381245 6310 if (!header_of_multiple)
170b53b2 6311 print_breakpoint_location (b, loc);
0d381245 6312 if (b->loc)
a6d9a66e 6313 *last_loc = b->loc;
3086aeae
DJ
6314 break;
6315 }
c906108c 6316
6c95b8df 6317
998580f1 6318 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6319 {
6320 struct inferior *inf;
998580f1
MK
6321 VEC(int) *inf_num = NULL;
6322 int mi_only = 1;
6c95b8df 6323
998580f1 6324 ALL_INFERIORS (inf)
6c95b8df
PA
6325 {
6326 if (inf->pspace == loc->pspace)
998580f1 6327 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6328 }
998580f1
MK
6329
6330 /* For backward compatibility, don't display inferiors in CLI unless
6331 there are several. Always display for MI. */
6332 if (allflag
6333 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6334 && (number_of_program_spaces () > 1
6335 || number_of_inferiors () > 1)
6336 /* LOC is for existing B, it cannot be in
6337 moribund_locations and thus having NULL OWNER. */
6338 && loc->owner->type != bp_catchpoint))
6339 mi_only = 0;
6340 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6341 VEC_free (int, inf_num);
6c95b8df
PA
6342 }
6343
4a306c9a 6344 if (!part_of_multiple)
c4093a6a 6345 {
4a306c9a
JB
6346 if (b->thread != -1)
6347 {
6348 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6349 "stop only in" line a little further down. */
4a306c9a
JB
6350 ui_out_text (uiout, " thread ");
6351 ui_out_field_int (uiout, "thread", b->thread);
6352 }
6353 else if (b->task != 0)
6354 {
6355 ui_out_text (uiout, " task ");
6356 ui_out_field_int (uiout, "task", b->task);
6357 }
c4093a6a 6358 }
f1310107 6359
8b93c638 6360 ui_out_text (uiout, "\n");
f1310107 6361
348d480f 6362 if (!part_of_multiple)
f1310107
TJB
6363 b->ops->print_one_detail (b, uiout);
6364
0d381245 6365 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6366 {
6367 annotate_field (6);
8b93c638 6368 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6369 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6370 the frame ID. */
5af949e3
UW
6371 ui_out_field_core_addr (uiout, "frame",
6372 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6373 ui_out_text (uiout, "\n");
c4093a6a
JM
6374 }
6375
28010a5d 6376 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6377 {
6378 annotate_field (7);
d77f58be 6379 if (is_tracepoint (b))
1042e4c0
SS
6380 ui_out_text (uiout, "\ttrace only if ");
6381 else
6382 ui_out_text (uiout, "\tstop only if ");
0101ce28 6383 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6384
6385 /* Print whether the target is doing the breakpoint's condition
6386 evaluation. If GDB is doing the evaluation, don't print anything. */
6387 if (is_breakpoint (b)
6388 && breakpoint_condition_evaluation_mode ()
6389 == condition_evaluation_target)
6390 {
6391 ui_out_text (uiout, " (");
6392 ui_out_field_string (uiout, "evaluated-by",
6393 bp_condition_evaluator (b));
6394 ui_out_text (uiout, " evals)");
6395 }
0101ce28
JJ
6396 ui_out_text (uiout, "\n");
6397 }
6398
0d381245 6399 if (!part_of_multiple && b->thread != -1)
c4093a6a 6400 {
4a64f543 6401 /* FIXME should make an annotation for this. */
8b93c638
JM
6402 ui_out_text (uiout, "\tstop only in thread ");
6403 ui_out_field_int (uiout, "thread", b->thread);
6404 ui_out_text (uiout, "\n");
c4093a6a
JM
6405 }
6406
556ec64d
YQ
6407 if (!part_of_multiple)
6408 {
6409 if (b->hit_count)
31f56a27
YQ
6410 {
6411 /* FIXME should make an annotation for this. */
6412 if (is_catchpoint (b))
6413 ui_out_text (uiout, "\tcatchpoint");
6414 else if (is_tracepoint (b))
6415 ui_out_text (uiout, "\ttracepoint");
6416 else
6417 ui_out_text (uiout, "\tbreakpoint");
6418 ui_out_text (uiout, " already hit ");
6419 ui_out_field_int (uiout, "times", b->hit_count);
6420 if (b->hit_count == 1)
6421 ui_out_text (uiout, " time\n");
6422 else
6423 ui_out_text (uiout, " times\n");
6424 }
556ec64d
YQ
6425 else
6426 {
31f56a27
YQ
6427 /* Output the count also if it is zero, but only if this is mi. */
6428 if (ui_out_is_mi_like_p (uiout))
6429 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6430 }
6431 }
8b93c638 6432
0d381245 6433 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6434 {
6435 annotate_field (8);
8b93c638
JM
6436 ui_out_text (uiout, "\tignore next ");
6437 ui_out_field_int (uiout, "ignore", b->ignore_count);
6438 ui_out_text (uiout, " hits\n");
c4093a6a 6439 }
059fb39f 6440
816338b5
SS
6441 /* Note that an enable count of 1 corresponds to "enable once"
6442 behavior, which is reported by the combination of enablement and
6443 disposition, so we don't need to mention it here. */
6444 if (!part_of_multiple && b->enable_count > 1)
6445 {
6446 annotate_field (8);
6447 ui_out_text (uiout, "\tdisable after ");
6448 /* Tweak the wording to clarify that ignore and enable counts
6449 are distinct, and have additive effect. */
6450 if (b->ignore_count)
6451 ui_out_text (uiout, "additional ");
6452 else
6453 ui_out_text (uiout, "next ");
6454 ui_out_field_int (uiout, "enable", b->enable_count);
6455 ui_out_text (uiout, " hits\n");
6456 }
6457
f196051f
SS
6458 if (!part_of_multiple && is_tracepoint (b))
6459 {
6460 struct tracepoint *tp = (struct tracepoint *) b;
6461
6462 if (tp->traceframe_usage)
6463 {
6464 ui_out_text (uiout, "\ttrace buffer usage ");
6465 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6466 ui_out_text (uiout, " bytes\n");
6467 }
6468 }
d3ce09f5 6469
9add0f1b 6470 l = b->commands ? b->commands->commands : NULL;
059fb39f 6471 if (!part_of_multiple && l)
c4093a6a 6472 {
3b31d625
EZ
6473 struct cleanup *script_chain;
6474
c4093a6a 6475 annotate_field (9);
3b31d625 6476 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6477 print_command_lines (uiout, l, 4);
3b31d625 6478 do_cleanups (script_chain);
c4093a6a 6479 }
d24317b4 6480
d9b3f62e 6481 if (is_tracepoint (b))
1042e4c0 6482 {
d9b3f62e
PA
6483 struct tracepoint *t = (struct tracepoint *) b;
6484
6485 if (!part_of_multiple && t->pass_count)
6486 {
6487 annotate_field (10);
6488 ui_out_text (uiout, "\tpass count ");
6489 ui_out_field_int (uiout, "pass", t->pass_count);
6490 ui_out_text (uiout, " \n");
6491 }
f2a8bc8a
YQ
6492
6493 /* Don't display it when tracepoint or tracepoint location is
6494 pending. */
6495 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6496 {
6497 annotate_field (11);
6498
6499 if (ui_out_is_mi_like_p (uiout))
6500 ui_out_field_string (uiout, "installed",
6501 loc->inserted ? "y" : "n");
6502 else
6503 {
6504 if (loc->inserted)
6505 ui_out_text (uiout, "\t");
6506 else
6507 ui_out_text (uiout, "\tnot ");
6508 ui_out_text (uiout, "installed on target\n");
6509 }
6510 }
1042e4c0
SS
6511 }
6512
d24317b4
VP
6513 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6514 {
3a5c3e22
PA
6515 if (is_watchpoint (b))
6516 {
6517 struct watchpoint *w = (struct watchpoint *) b;
6518
6519 ui_out_field_string (uiout, "original-location", w->exp_string);
6520 }
6521 else if (b->addr_string)
d24317b4 6522 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6523 }
c4093a6a 6524}
c5aa993b 6525
0d381245
VP
6526static void
6527print_one_breakpoint (struct breakpoint *b,
4a64f543 6528 struct bp_location **last_loc,
6c95b8df 6529 int allflag)
0d381245 6530{
8d3788bd 6531 struct cleanup *bkpt_chain;
79a45e25 6532 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6533
6534 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6535
12c5a436 6536 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6537 do_cleanups (bkpt_chain);
0d381245
VP
6538
6539 /* If this breakpoint has custom print function,
6540 it's already printed. Otherwise, print individual
6541 locations, if any. */
6542 if (b->ops == NULL || b->ops->print_one == NULL)
6543 {
4a64f543
MS
6544 /* If breakpoint has a single location that is disabled, we
6545 print it as if it had several locations, since otherwise it's
6546 hard to represent "breakpoint enabled, location disabled"
6547 situation.
6548
6549 Note that while hardware watchpoints have several locations
a3be7890 6550 internally, that's not a property exposed to user. */
0d381245 6551 if (b->loc
a5606eee 6552 && !is_hardware_watchpoint (b)
8d3788bd 6553 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6554 {
6555 struct bp_location *loc;
6556 int n = 1;
8d3788bd 6557
0d381245 6558 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6559 {
6560 struct cleanup *inner2 =
6561 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6562 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6563 do_cleanups (inner2);
6564 }
0d381245
VP
6565 }
6566 }
6567}
6568
a6d9a66e
UW
6569static int
6570breakpoint_address_bits (struct breakpoint *b)
6571{
6572 int print_address_bits = 0;
6573 struct bp_location *loc;
6574
6575 for (loc = b->loc; loc; loc = loc->next)
6576 {
c7437ca6
PA
6577 int addr_bit;
6578
6579 /* Software watchpoints that aren't watching memory don't have
6580 an address to print. */
6581 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6582 continue;
6583
6584 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6585 if (addr_bit > print_address_bits)
6586 print_address_bits = addr_bit;
6587 }
6588
6589 return print_address_bits;
6590}
0d381245 6591
c4093a6a
JM
6592struct captured_breakpoint_query_args
6593 {
6594 int bnum;
6595 };
c5aa993b 6596
c4093a6a 6597static int
2b65245e 6598do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6599{
6600 struct captured_breakpoint_query_args *args = data;
52f0bd74 6601 struct breakpoint *b;
a6d9a66e 6602 struct bp_location *dummy_loc = NULL;
cc59ec59 6603
c4093a6a
JM
6604 ALL_BREAKPOINTS (b)
6605 {
6606 if (args->bnum == b->number)
c5aa993b 6607 {
12c5a436 6608 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6609 return GDB_RC_OK;
c5aa993b 6610 }
c4093a6a
JM
6611 }
6612 return GDB_RC_NONE;
6613}
c5aa993b 6614
c4093a6a 6615enum gdb_rc
4a64f543
MS
6616gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6617 char **error_message)
c4093a6a
JM
6618{
6619 struct captured_breakpoint_query_args args;
cc59ec59 6620
c4093a6a
JM
6621 args.bnum = bnum;
6622 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6623 an error. */
b0b13bb4
DJ
6624 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6625 error_message, RETURN_MASK_ALL) < 0)
6626 return GDB_RC_FAIL;
6627 else
6628 return GDB_RC_OK;
c4093a6a 6629}
c5aa993b 6630
09d682a4
TT
6631/* Return true if this breakpoint was set by the user, false if it is
6632 internal or momentary. */
6633
6634int
6635user_breakpoint_p (struct breakpoint *b)
6636{
46c6471b 6637 return b->number > 0;
09d682a4
TT
6638}
6639
7f3b0473 6640/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6641 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6642 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6643 FILTER is non-NULL, call it on each breakpoint and only include the
6644 ones for which it returns non-zero. Return the total number of
6645 breakpoints listed. */
c906108c 6646
d77f58be 6647static int
e5a67952 6648breakpoint_1 (char *args, int allflag,
4a64f543 6649 int (*filter) (const struct breakpoint *))
c4093a6a 6650{
52f0bd74 6651 struct breakpoint *b;
a6d9a66e 6652 struct bp_location *last_loc = NULL;
7f3b0473 6653 int nr_printable_breakpoints;
3b31d625 6654 struct cleanup *bkpttbl_chain;
79a45b7d 6655 struct value_print_options opts;
a6d9a66e 6656 int print_address_bits = 0;
269b11a2 6657 int print_type_col_width = 14;
79a45e25 6658 struct ui_out *uiout = current_uiout;
269b11a2 6659
79a45b7d
TT
6660 get_user_print_options (&opts);
6661
4a64f543
MS
6662 /* Compute the number of rows in the table, as well as the size
6663 required for address fields. */
7f3b0473
AC
6664 nr_printable_breakpoints = 0;
6665 ALL_BREAKPOINTS (b)
e5a67952
MS
6666 {
6667 /* If we have a filter, only list the breakpoints it accepts. */
6668 if (filter && !filter (b))
6669 continue;
6670
6671 /* If we have an "args" string, it is a list of breakpoints to
6672 accept. Skip the others. */
6673 if (args != NULL && *args != '\0')
6674 {
6675 if (allflag && parse_and_eval_long (args) != b->number)
6676 continue;
6677 if (!allflag && !number_is_in_list (args, b->number))
6678 continue;
6679 }
269b11a2 6680
e5a67952
MS
6681 if (allflag || user_breakpoint_p (b))
6682 {
6683 int addr_bit, type_len;
a6d9a66e 6684
e5a67952
MS
6685 addr_bit = breakpoint_address_bits (b);
6686 if (addr_bit > print_address_bits)
6687 print_address_bits = addr_bit;
269b11a2 6688
e5a67952
MS
6689 type_len = strlen (bptype_string (b->type));
6690 if (type_len > print_type_col_width)
6691 print_type_col_width = type_len;
6692
6693 nr_printable_breakpoints++;
6694 }
6695 }
7f3b0473 6696
79a45b7d 6697 if (opts.addressprint)
3b31d625 6698 bkpttbl_chain
3e43a32a
MS
6699 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6700 nr_printable_breakpoints,
3b31d625 6701 "BreakpointTable");
8b93c638 6702 else
3b31d625 6703 bkpttbl_chain
3e43a32a
MS
6704 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6705 nr_printable_breakpoints,
3b31d625 6706 "BreakpointTable");
8b93c638 6707
7f3b0473 6708 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6709 annotate_breakpoints_headers ();
6710 if (nr_printable_breakpoints > 0)
6711 annotate_field (0);
4a64f543 6712 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6713 if (nr_printable_breakpoints > 0)
6714 annotate_field (1);
269b11a2 6715 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6716 "type", "Type"); /* 2 */
d7faa9e7
AC
6717 if (nr_printable_breakpoints > 0)
6718 annotate_field (2);
4a64f543 6719 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6720 if (nr_printable_breakpoints > 0)
6721 annotate_field (3);
54e52265 6722 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6723 if (opts.addressprint)
e5a67952
MS
6724 {
6725 if (nr_printable_breakpoints > 0)
6726 annotate_field (4);
6727 if (print_address_bits <= 32)
6728 ui_out_table_header (uiout, 10, ui_left,
6729 "addr", "Address"); /* 5 */
6730 else
6731 ui_out_table_header (uiout, 18, ui_left,
6732 "addr", "Address"); /* 5 */
6733 }
d7faa9e7
AC
6734 if (nr_printable_breakpoints > 0)
6735 annotate_field (5);
6736 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6737 ui_out_table_body (uiout);
6738 if (nr_printable_breakpoints > 0)
6739 annotate_breakpoints_table ();
7f3b0473 6740
c4093a6a 6741 ALL_BREAKPOINTS (b)
e5a67952
MS
6742 {
6743 QUIT;
6744 /* If we have a filter, only list the breakpoints it accepts. */
6745 if (filter && !filter (b))
6746 continue;
6747
6748 /* If we have an "args" string, it is a list of breakpoints to
6749 accept. Skip the others. */
6750
6751 if (args != NULL && *args != '\0')
6752 {
6753 if (allflag) /* maintenance info breakpoint */
6754 {
6755 if (parse_and_eval_long (args) != b->number)
6756 continue;
6757 }
6758 else /* all others */
6759 {
6760 if (!number_is_in_list (args, b->number))
6761 continue;
6762 }
6763 }
6764 /* We only print out user settable breakpoints unless the
6765 allflag is set. */
6766 if (allflag || user_breakpoint_p (b))
12c5a436 6767 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6768 }
6769
3b31d625 6770 do_cleanups (bkpttbl_chain);
698384cd 6771
7f3b0473 6772 if (nr_printable_breakpoints == 0)
c906108c 6773 {
4a64f543
MS
6774 /* If there's a filter, let the caller decide how to report
6775 empty list. */
d77f58be
SS
6776 if (!filter)
6777 {
e5a67952 6778 if (args == NULL || *args == '\0')
d77f58be
SS
6779 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6780 else
4a64f543 6781 ui_out_message (uiout, 0,
e5a67952
MS
6782 "No breakpoint or watchpoint matching '%s'.\n",
6783 args);
d77f58be 6784 }
c906108c
SS
6785 }
6786 else
c4093a6a 6787 {
a6d9a66e
UW
6788 if (last_loc && !server_command)
6789 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6790 }
c906108c 6791
4a64f543 6792 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6793 there have been breakpoints? */
c906108c 6794 annotate_breakpoints_table_end ();
d77f58be
SS
6795
6796 return nr_printable_breakpoints;
c906108c
SS
6797}
6798
ad443146
SS
6799/* Display the value of default-collect in a way that is generally
6800 compatible with the breakpoint list. */
6801
6802static void
6803default_collect_info (void)
6804{
79a45e25
PA
6805 struct ui_out *uiout = current_uiout;
6806
ad443146
SS
6807 /* If it has no value (which is frequently the case), say nothing; a
6808 message like "No default-collect." gets in user's face when it's
6809 not wanted. */
6810 if (!*default_collect)
6811 return;
6812
6813 /* The following phrase lines up nicely with per-tracepoint collect
6814 actions. */
6815 ui_out_text (uiout, "default collect ");
6816 ui_out_field_string (uiout, "default-collect", default_collect);
6817 ui_out_text (uiout, " \n");
6818}
6819
c906108c 6820static void
e5a67952 6821breakpoints_info (char *args, int from_tty)
c906108c 6822{
e5a67952 6823 breakpoint_1 (args, 0, NULL);
ad443146
SS
6824
6825 default_collect_info ();
d77f58be
SS
6826}
6827
6828static void
e5a67952 6829watchpoints_info (char *args, int from_tty)
d77f58be 6830{
e5a67952 6831 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6832 struct ui_out *uiout = current_uiout;
d77f58be
SS
6833
6834 if (num_printed == 0)
6835 {
e5a67952 6836 if (args == NULL || *args == '\0')
d77f58be
SS
6837 ui_out_message (uiout, 0, "No watchpoints.\n");
6838 else
e5a67952 6839 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6840 }
c906108c
SS
6841}
6842
7a292a7a 6843static void
e5a67952 6844maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6845{
e5a67952 6846 breakpoint_1 (args, 1, NULL);
ad443146
SS
6847
6848 default_collect_info ();
c906108c
SS
6849}
6850
0d381245 6851static int
714835d5 6852breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6853 struct program_space *pspace,
714835d5 6854 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6855{
6856 struct bp_location *bl = b->loc;
cc59ec59 6857
0d381245
VP
6858 for (; bl; bl = bl->next)
6859 {
6c95b8df
PA
6860 if (bl->pspace == pspace
6861 && bl->address == pc
0d381245
VP
6862 && (!overlay_debugging || bl->section == section))
6863 return 1;
6864 }
6865 return 0;
6866}
6867
672f9b60 6868/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6869 concerns with logical breakpoints, so we match program spaces, not
6870 address spaces. */
c906108c
SS
6871
6872static void
6c95b8df
PA
6873describe_other_breakpoints (struct gdbarch *gdbarch,
6874 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6875 struct obj_section *section, int thread)
c906108c 6876{
52f0bd74
AC
6877 int others = 0;
6878 struct breakpoint *b;
c906108c
SS
6879
6880 ALL_BREAKPOINTS (b)
672f9b60
KP
6881 others += (user_breakpoint_p (b)
6882 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6883 if (others > 0)
6884 {
a3f17187
AC
6885 if (others == 1)
6886 printf_filtered (_("Note: breakpoint "));
6887 else /* if (others == ???) */
6888 printf_filtered (_("Note: breakpoints "));
c906108c 6889 ALL_BREAKPOINTS (b)
672f9b60 6890 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6891 {
6892 others--;
6893 printf_filtered ("%d", b->number);
6894 if (b->thread == -1 && thread != -1)
6895 printf_filtered (" (all threads)");
6896 else if (b->thread != -1)
6897 printf_filtered (" (thread %d)", b->thread);
6898 printf_filtered ("%s%s ",
059fb39f 6899 ((b->enable_state == bp_disabled
f8eba3c6 6900 || b->enable_state == bp_call_disabled)
0d381245 6901 ? " (disabled)"
0d381245
VP
6902 : ""),
6903 (others > 1) ? ","
6904 : ((others == 1) ? " and" : ""));
6905 }
a3f17187 6906 printf_filtered (_("also set at pc "));
5af949e3 6907 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6908 printf_filtered (".\n");
6909 }
6910}
6911\f
c906108c 6912
e4f237da
KB
6913/* Return true iff it is meaningful to use the address member of
6914 BPT. For some breakpoint types, the address member is irrelevant
6915 and it makes no sense to attempt to compare it to other addresses
6916 (or use it for any other purpose either).
6917
4a64f543
MS
6918 More specifically, each of the following breakpoint types will
6919 always have a zero valued address and we don't want to mark
6920 breakpoints of any of these types to be a duplicate of an actual
6921 breakpoint at address zero:
e4f237da
KB
6922
6923 bp_watchpoint
2d134ed3
PA
6924 bp_catchpoint
6925
6926*/
e4f237da
KB
6927
6928static int
6929breakpoint_address_is_meaningful (struct breakpoint *bpt)
6930{
6931 enum bptype type = bpt->type;
6932
2d134ed3
PA
6933 return (type != bp_watchpoint && type != bp_catchpoint);
6934}
6935
6936/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6937 true if LOC1 and LOC2 represent the same watchpoint location. */
6938
6939static int
4a64f543
MS
6940watchpoint_locations_match (struct bp_location *loc1,
6941 struct bp_location *loc2)
2d134ed3 6942{
3a5c3e22
PA
6943 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6944 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6945
6946 /* Both of them must exist. */
6947 gdb_assert (w1 != NULL);
6948 gdb_assert (w2 != NULL);
2bdf28a0 6949
4a64f543
MS
6950 /* If the target can evaluate the condition expression in hardware,
6951 then we we need to insert both watchpoints even if they are at
6952 the same place. Otherwise the watchpoint will only trigger when
6953 the condition of whichever watchpoint was inserted evaluates to
6954 true, not giving a chance for GDB to check the condition of the
6955 other watchpoint. */
3a5c3e22 6956 if ((w1->cond_exp
4a64f543
MS
6957 && target_can_accel_watchpoint_condition (loc1->address,
6958 loc1->length,
0cf6dd15 6959 loc1->watchpoint_type,
3a5c3e22
PA
6960 w1->cond_exp))
6961 || (w2->cond_exp
4a64f543
MS
6962 && target_can_accel_watchpoint_condition (loc2->address,
6963 loc2->length,
0cf6dd15 6964 loc2->watchpoint_type,
3a5c3e22 6965 w2->cond_exp)))
0cf6dd15
TJB
6966 return 0;
6967
85d721b8
PA
6968 /* Note that this checks the owner's type, not the location's. In
6969 case the target does not support read watchpoints, but does
6970 support access watchpoints, we'll have bp_read_watchpoint
6971 watchpoints with hw_access locations. Those should be considered
6972 duplicates of hw_read locations. The hw_read locations will
6973 become hw_access locations later. */
2d134ed3
PA
6974 return (loc1->owner->type == loc2->owner->type
6975 && loc1->pspace->aspace == loc2->pspace->aspace
6976 && loc1->address == loc2->address
6977 && loc1->length == loc2->length);
e4f237da
KB
6978}
6979
31e77af2 6980/* See breakpoint.h. */
6c95b8df 6981
31e77af2 6982int
6c95b8df
PA
6983breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6984 struct address_space *aspace2, CORE_ADDR addr2)
6985{
f5656ead 6986 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6987 || aspace1 == aspace2)
6988 && addr1 == addr2);
6989}
6990
f1310107
TJB
6991/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6992 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6993 matches ASPACE2. On targets that have global breakpoints, the address
6994 space doesn't really matter. */
6995
6996static int
6997breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6998 int len1, struct address_space *aspace2,
6999 CORE_ADDR addr2)
7000{
f5656ead 7001 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7002 || aspace1 == aspace2)
7003 && addr2 >= addr1 && addr2 < addr1 + len1);
7004}
7005
7006/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7007 a ranged breakpoint. In most targets, a match happens only if ASPACE
7008 matches the breakpoint's address space. On targets that have global
7009 breakpoints, the address space doesn't really matter. */
7010
7011static int
7012breakpoint_location_address_match (struct bp_location *bl,
7013 struct address_space *aspace,
7014 CORE_ADDR addr)
7015{
7016 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7017 aspace, addr)
7018 || (bl->length
7019 && breakpoint_address_match_range (bl->pspace->aspace,
7020 bl->address, bl->length,
7021 aspace, addr)));
7022}
7023
1e4d1764
YQ
7024/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7025 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7026 true, otherwise returns false. */
7027
7028static int
7029tracepoint_locations_match (struct bp_location *loc1,
7030 struct bp_location *loc2)
7031{
7032 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7033 /* Since tracepoint locations are never duplicated with others', tracepoint
7034 locations at the same address of different tracepoints are regarded as
7035 different locations. */
7036 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7037 else
7038 return 0;
7039}
7040
2d134ed3
PA
7041/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7042 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7043 represent the same location. */
7044
7045static int
4a64f543
MS
7046breakpoint_locations_match (struct bp_location *loc1,
7047 struct bp_location *loc2)
2d134ed3 7048{
2bdf28a0
JK
7049 int hw_point1, hw_point2;
7050
7051 /* Both of them must not be in moribund_locations. */
7052 gdb_assert (loc1->owner != NULL);
7053 gdb_assert (loc2->owner != NULL);
7054
7055 hw_point1 = is_hardware_watchpoint (loc1->owner);
7056 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7057
7058 if (hw_point1 != hw_point2)
7059 return 0;
7060 else if (hw_point1)
7061 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7062 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7063 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7064 else
f1310107
TJB
7065 /* We compare bp_location.length in order to cover ranged breakpoints. */
7066 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7067 loc2->pspace->aspace, loc2->address)
7068 && loc1->length == loc2->length);
2d134ed3
PA
7069}
7070
76897487
KB
7071static void
7072breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7073 int bnum, int have_bnum)
7074{
f63fbe86
MS
7075 /* The longest string possibly returned by hex_string_custom
7076 is 50 chars. These must be at least that big for safety. */
7077 char astr1[64];
7078 char astr2[64];
76897487 7079
bb599908
PH
7080 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7081 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7082 if (have_bnum)
8a3fe4f8 7083 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7084 bnum, astr1, astr2);
7085 else
8a3fe4f8 7086 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7087}
7088
4a64f543
MS
7089/* Adjust a breakpoint's address to account for architectural
7090 constraints on breakpoint placement. Return the adjusted address.
7091 Note: Very few targets require this kind of adjustment. For most
7092 targets, this function is simply the identity function. */
76897487
KB
7093
7094static CORE_ADDR
a6d9a66e
UW
7095adjust_breakpoint_address (struct gdbarch *gdbarch,
7096 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7097{
a6d9a66e 7098 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7099 {
7100 /* Very few targets need any kind of breakpoint adjustment. */
7101 return bpaddr;
7102 }
88f7da05
KB
7103 else if (bptype == bp_watchpoint
7104 || bptype == bp_hardware_watchpoint
7105 || bptype == bp_read_watchpoint
7106 || bptype == bp_access_watchpoint
fe798b75 7107 || bptype == bp_catchpoint)
88f7da05
KB
7108 {
7109 /* Watchpoints and the various bp_catch_* eventpoints should not
7110 have their addresses modified. */
7111 return bpaddr;
7112 }
7c16b83e
PA
7113 else if (bptype == bp_single_step)
7114 {
7115 /* Single-step breakpoints should not have their addresses
7116 modified. If there's any architectural constrain that
7117 applies to this address, then it should have already been
7118 taken into account when the breakpoint was created in the
7119 first place. If we didn't do this, stepping through e.g.,
7120 Thumb-2 IT blocks would break. */
7121 return bpaddr;
7122 }
76897487
KB
7123 else
7124 {
7125 CORE_ADDR adjusted_bpaddr;
7126
7127 /* Some targets have architectural constraints on the placement
7128 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7129 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7130
7131 /* An adjusted breakpoint address can significantly alter
7132 a user's expectations. Print a warning if an adjustment
7133 is required. */
7134 if (adjusted_bpaddr != bpaddr)
7135 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7136
7137 return adjusted_bpaddr;
7138 }
7139}
7140
28010a5d
PA
7141void
7142init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7143 struct breakpoint *owner)
7cc221ef 7144{
7cc221ef
DJ
7145 memset (loc, 0, sizeof (*loc));
7146
348d480f
PA
7147 gdb_assert (ops != NULL);
7148
28010a5d
PA
7149 loc->ops = ops;
7150 loc->owner = owner;
511a6cd4 7151 loc->cond = NULL;
b775012e 7152 loc->cond_bytecode = NULL;
0d381245
VP
7153 loc->shlib_disabled = 0;
7154 loc->enabled = 1;
e049a4b5 7155
28010a5d 7156 switch (owner->type)
e049a4b5
DJ
7157 {
7158 case bp_breakpoint:
7c16b83e 7159 case bp_single_step:
e049a4b5
DJ
7160 case bp_until:
7161 case bp_finish:
7162 case bp_longjmp:
7163 case bp_longjmp_resume:
e2e4d78b 7164 case bp_longjmp_call_dummy:
186c406b
TT
7165 case bp_exception:
7166 case bp_exception_resume:
e049a4b5 7167 case bp_step_resume:
2c03e5be 7168 case bp_hp_step_resume:
e049a4b5
DJ
7169 case bp_watchpoint_scope:
7170 case bp_call_dummy:
aa7d318d 7171 case bp_std_terminate:
e049a4b5
DJ
7172 case bp_shlib_event:
7173 case bp_thread_event:
7174 case bp_overlay_event:
4efc6507 7175 case bp_jit_event:
0fd8e87f 7176 case bp_longjmp_master:
aa7d318d 7177 case bp_std_terminate_master:
186c406b 7178 case bp_exception_master:
0e30163f
JK
7179 case bp_gnu_ifunc_resolver:
7180 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7181 case bp_dprintf:
e049a4b5 7182 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7183 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7184 break;
7185 case bp_hardware_breakpoint:
7186 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7187 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7188 break;
7189 case bp_hardware_watchpoint:
7190 case bp_read_watchpoint:
7191 case bp_access_watchpoint:
7192 loc->loc_type = bp_loc_hardware_watchpoint;
7193 break;
7194 case bp_watchpoint:
ce78b96d 7195 case bp_catchpoint:
15c3d785
PA
7196 case bp_tracepoint:
7197 case bp_fast_tracepoint:
0fb4aa4b 7198 case bp_static_tracepoint:
e049a4b5
DJ
7199 loc->loc_type = bp_loc_other;
7200 break;
7201 default:
e2e0b3e5 7202 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7203 }
7204
f431efe5 7205 loc->refc = 1;
28010a5d
PA
7206}
7207
7208/* Allocate a struct bp_location. */
7209
7210static struct bp_location *
7211allocate_bp_location (struct breakpoint *bpt)
7212{
348d480f
PA
7213 return bpt->ops->allocate_location (bpt);
7214}
7cc221ef 7215
f431efe5
PA
7216static void
7217free_bp_location (struct bp_location *loc)
fe3f5fa8 7218{
348d480f 7219 loc->ops->dtor (loc);
fe3f5fa8
VP
7220 xfree (loc);
7221}
7222
f431efe5
PA
7223/* Increment reference count. */
7224
7225static void
7226incref_bp_location (struct bp_location *bl)
7227{
7228 ++bl->refc;
7229}
7230
7231/* Decrement reference count. If the reference count reaches 0,
7232 destroy the bp_location. Sets *BLP to NULL. */
7233
7234static void
7235decref_bp_location (struct bp_location **blp)
7236{
0807b50c
PA
7237 gdb_assert ((*blp)->refc > 0);
7238
f431efe5
PA
7239 if (--(*blp)->refc == 0)
7240 free_bp_location (*blp);
7241 *blp = NULL;
7242}
7243
346774a9 7244/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7245
346774a9
PA
7246static void
7247add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7248{
346774a9 7249 struct breakpoint *b1;
c906108c 7250
346774a9
PA
7251 /* Add this breakpoint to the end of the chain so that a list of
7252 breakpoints will come out in order of increasing numbers. */
7253
7254 b1 = breakpoint_chain;
7255 if (b1 == 0)
7256 breakpoint_chain = b;
7257 else
7258 {
7259 while (b1->next)
7260 b1 = b1->next;
7261 b1->next = b;
7262 }
7263}
7264
7265/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7266
7267static void
7268init_raw_breakpoint_without_location (struct breakpoint *b,
7269 struct gdbarch *gdbarch,
28010a5d 7270 enum bptype bptype,
c0a91b2b 7271 const struct breakpoint_ops *ops)
346774a9 7272{
c906108c 7273 memset (b, 0, sizeof (*b));
2219d63c 7274
348d480f
PA
7275 gdb_assert (ops != NULL);
7276
28010a5d 7277 b->ops = ops;
4d28f7a8 7278 b->type = bptype;
a6d9a66e 7279 b->gdbarch = gdbarch;
c906108c
SS
7280 b->language = current_language->la_language;
7281 b->input_radix = input_radix;
7282 b->thread = -1;
b5de0fa7 7283 b->enable_state = bp_enabled;
c906108c
SS
7284 b->next = 0;
7285 b->silent = 0;
7286 b->ignore_count = 0;
7287 b->commands = NULL;
818dd999 7288 b->frame_id = null_frame_id;
0d381245 7289 b->condition_not_parsed = 0;
84f4c1fe 7290 b->py_bp_object = NULL;
d0fb5eae 7291 b->related_breakpoint = b;
346774a9
PA
7292}
7293
7294/* Helper to set_raw_breakpoint below. Creates a breakpoint
7295 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7296
7297static struct breakpoint *
7298set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7299 enum bptype bptype,
c0a91b2b 7300 const struct breakpoint_ops *ops)
346774a9
PA
7301{
7302 struct breakpoint *b = XNEW (struct breakpoint);
7303
348d480f 7304 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7305 add_to_breakpoint_chain (b);
0d381245
VP
7306 return b;
7307}
7308
0e30163f
JK
7309/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7310 resolutions should be made as the user specified the location explicitly
7311 enough. */
7312
0d381245 7313static void
0e30163f 7314set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7315{
2bdf28a0
JK
7316 gdb_assert (loc->owner != NULL);
7317
0d381245 7318 if (loc->owner->type == bp_breakpoint
1042e4c0 7319 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7320 || is_tracepoint (loc->owner))
0d381245 7321 {
0e30163f 7322 int is_gnu_ifunc;
2c02bd72 7323 const char *function_name;
6a3a010b 7324 CORE_ADDR func_addr;
0e30163f 7325
2c02bd72 7326 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7327 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7328
7329 if (is_gnu_ifunc && !explicit_loc)
7330 {
7331 struct breakpoint *b = loc->owner;
7332
7333 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7334 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7335 &loc->requested_address))
7336 {
7337 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7338 loc->address = adjust_breakpoint_address (loc->gdbarch,
7339 loc->requested_address,
7340 b->type);
7341 }
7342 else if (b->type == bp_breakpoint && b->loc == loc
7343 && loc->next == NULL && b->related_breakpoint == b)
7344 {
7345 /* Create only the whole new breakpoint of this type but do not
7346 mess more complicated breakpoints with multiple locations. */
7347 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7348 /* Remember the resolver's address for use by the return
7349 breakpoint. */
7350 loc->related_address = func_addr;
0e30163f
JK
7351 }
7352 }
7353
2c02bd72
DE
7354 if (function_name)
7355 loc->function_name = xstrdup (function_name);
0d381245
VP
7356 }
7357}
7358
a6d9a66e 7359/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7360struct gdbarch *
a6d9a66e
UW
7361get_sal_arch (struct symtab_and_line sal)
7362{
7363 if (sal.section)
7364 return get_objfile_arch (sal.section->objfile);
7365 if (sal.symtab)
eb822aa6 7366 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7367
7368 return NULL;
7369}
7370
346774a9
PA
7371/* Low level routine for partially initializing a breakpoint of type
7372 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7373 file name, and line number are provided by SAL.
0d381245
VP
7374
7375 It is expected that the caller will complete the initialization of
7376 the newly created breakpoint struct as well as output any status
c56053d2 7377 information regarding the creation of a new breakpoint. */
0d381245 7378
346774a9
PA
7379static void
7380init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7381 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7382 const struct breakpoint_ops *ops)
0d381245 7383{
28010a5d 7384 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7385
3742cc8b 7386 add_location_to_breakpoint (b, &sal);
0d381245 7387
6c95b8df
PA
7388 if (bptype != bp_catchpoint)
7389 gdb_assert (sal.pspace != NULL);
7390
f8eba3c6
TT
7391 /* Store the program space that was used to set the breakpoint,
7392 except for ordinary breakpoints, which are independent of the
7393 program space. */
7394 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7395 b->pspace = sal.pspace;
346774a9 7396}
c906108c 7397
346774a9
PA
7398/* set_raw_breakpoint is a low level routine for allocating and
7399 partially initializing a breakpoint of type BPTYPE. The newly
7400 created breakpoint's address, section, source file name, and line
7401 number are provided by SAL. The newly created and partially
7402 initialized breakpoint is added to the breakpoint chain and
7403 is also returned as the value of this function.
7404
7405 It is expected that the caller will complete the initialization of
7406 the newly created breakpoint struct as well as output any status
7407 information regarding the creation of a new breakpoint. In
7408 particular, set_raw_breakpoint does NOT set the breakpoint
7409 number! Care should be taken to not allow an error to occur
7410 prior to completing the initialization of the breakpoint. If this
7411 should happen, a bogus breakpoint will be left on the chain. */
7412
7413struct breakpoint *
7414set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7415 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7416 const struct breakpoint_ops *ops)
346774a9
PA
7417{
7418 struct breakpoint *b = XNEW (struct breakpoint);
7419
348d480f 7420 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7421 add_to_breakpoint_chain (b);
c906108c
SS
7422 return b;
7423}
7424
53a5351d 7425/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7426 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7427 initiated the operation. */
c906108c
SS
7428
7429void
186c406b 7430set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7431{
35df4500 7432 struct breakpoint *b, *b_tmp;
186c406b 7433 int thread = tp->num;
0fd8e87f
UW
7434
7435 /* To avoid having to rescan all objfile symbols at every step,
7436 we maintain a list of continually-inserted but always disabled
7437 longjmp "master" breakpoints. Here, we simply create momentary
7438 clones of those and enable them for the requested thread. */
35df4500 7439 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7440 if (b->pspace == current_program_space
186c406b
TT
7441 && (b->type == bp_longjmp_master
7442 || b->type == bp_exception_master))
0fd8e87f 7443 {
06edf0c0
PA
7444 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7445 struct breakpoint *clone;
cc59ec59 7446
e2e4d78b
JK
7447 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7448 after their removal. */
06edf0c0 7449 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7450 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7451 clone->thread = thread;
7452 }
186c406b
TT
7453
7454 tp->initiating_frame = frame;
c906108c
SS
7455}
7456
611c83ae 7457/* Delete all longjmp breakpoints from THREAD. */
c906108c 7458void
611c83ae 7459delete_longjmp_breakpoint (int thread)
c906108c 7460{
35df4500 7461 struct breakpoint *b, *b_tmp;
c906108c 7462
35df4500 7463 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7464 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7465 {
7466 if (b->thread == thread)
7467 delete_breakpoint (b);
7468 }
c906108c
SS
7469}
7470
f59f708a
PA
7471void
7472delete_longjmp_breakpoint_at_next_stop (int thread)
7473{
7474 struct breakpoint *b, *b_tmp;
7475
7476 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7477 if (b->type == bp_longjmp || b->type == bp_exception)
7478 {
7479 if (b->thread == thread)
7480 b->disposition = disp_del_at_next_stop;
7481 }
7482}
7483
e2e4d78b
JK
7484/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7485 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7486 pointer to any of them. Return NULL if this system cannot place longjmp
7487 breakpoints. */
7488
7489struct breakpoint *
7490set_longjmp_breakpoint_for_call_dummy (void)
7491{
7492 struct breakpoint *b, *retval = NULL;
7493
7494 ALL_BREAKPOINTS (b)
7495 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7496 {
7497 struct breakpoint *new_b;
7498
7499 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7500 &momentary_breakpoint_ops,
7501 1);
e2e4d78b
JK
7502 new_b->thread = pid_to_thread_id (inferior_ptid);
7503
7504 /* Link NEW_B into the chain of RETVAL breakpoints. */
7505
7506 gdb_assert (new_b->related_breakpoint == new_b);
7507 if (retval == NULL)
7508 retval = new_b;
7509 new_b->related_breakpoint = retval;
7510 while (retval->related_breakpoint != new_b->related_breakpoint)
7511 retval = retval->related_breakpoint;
7512 retval->related_breakpoint = new_b;
7513 }
7514
7515 return retval;
7516}
7517
7518/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7519 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7520 stack.
7521
7522 You should call this function only at places where it is safe to currently
7523 unwind the whole stack. Failed stack unwind would discard live dummy
7524 frames. */
7525
7526void
b67a2c6f 7527check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7528{
7529 struct breakpoint *b, *b_tmp;
7530
7531 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7532 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7533 {
7534 struct breakpoint *dummy_b = b->related_breakpoint;
7535
7536 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7537 dummy_b = dummy_b->related_breakpoint;
7538 if (dummy_b->type != bp_call_dummy
7539 || frame_find_by_id (dummy_b->frame_id) != NULL)
7540 continue;
7541
b67a2c6f 7542 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7543
7544 while (b->related_breakpoint != b)
7545 {
7546 if (b_tmp == b->related_breakpoint)
7547 b_tmp = b->related_breakpoint->next;
7548 delete_breakpoint (b->related_breakpoint);
7549 }
7550 delete_breakpoint (b);
7551 }
7552}
7553
1900040c
MS
7554void
7555enable_overlay_breakpoints (void)
7556{
52f0bd74 7557 struct breakpoint *b;
1900040c
MS
7558
7559 ALL_BREAKPOINTS (b)
7560 if (b->type == bp_overlay_event)
7561 {
7562 b->enable_state = bp_enabled;
44702360 7563 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7564 overlay_events_enabled = 1;
1900040c
MS
7565 }
7566}
7567
7568void
7569disable_overlay_breakpoints (void)
7570{
52f0bd74 7571 struct breakpoint *b;
1900040c
MS
7572
7573 ALL_BREAKPOINTS (b)
7574 if (b->type == bp_overlay_event)
7575 {
7576 b->enable_state = bp_disabled;
44702360 7577 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7578 overlay_events_enabled = 0;
1900040c
MS
7579 }
7580}
7581
aa7d318d
TT
7582/* Set an active std::terminate breakpoint for each std::terminate
7583 master breakpoint. */
7584void
7585set_std_terminate_breakpoint (void)
7586{
35df4500 7587 struct breakpoint *b, *b_tmp;
aa7d318d 7588
35df4500 7589 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7590 if (b->pspace == current_program_space
7591 && b->type == bp_std_terminate_master)
7592 {
06edf0c0 7593 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7594 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7595 }
7596}
7597
7598/* Delete all the std::terminate breakpoints. */
7599void
7600delete_std_terminate_breakpoint (void)
7601{
35df4500 7602 struct breakpoint *b, *b_tmp;
aa7d318d 7603
35df4500 7604 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7605 if (b->type == bp_std_terminate)
7606 delete_breakpoint (b);
7607}
7608
c4093a6a 7609struct breakpoint *
a6d9a66e 7610create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7611{
7612 struct breakpoint *b;
c4093a6a 7613
06edf0c0
PA
7614 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7615 &internal_breakpoint_ops);
7616
b5de0fa7 7617 b->enable_state = bp_enabled;
c4093a6a 7618 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7619 b->addr_string
7620 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7621
44702360 7622 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7623
c4093a6a
JM
7624 return b;
7625}
7626
7627void
7628remove_thread_event_breakpoints (void)
7629{
35df4500 7630 struct breakpoint *b, *b_tmp;
c4093a6a 7631
35df4500 7632 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7633 if (b->type == bp_thread_event
7634 && b->loc->pspace == current_program_space)
c4093a6a
JM
7635 delete_breakpoint (b);
7636}
7637
0101ce28
JJ
7638struct lang_and_radix
7639 {
7640 enum language lang;
7641 int radix;
7642 };
7643
4efc6507
DE
7644/* Create a breakpoint for JIT code registration and unregistration. */
7645
7646struct breakpoint *
7647create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7648{
7649 struct breakpoint *b;
7650
06edf0c0
PA
7651 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7652 &internal_breakpoint_ops);
44702360 7653 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7654 return b;
7655}
0101ce28 7656
03673fc7
PP
7657/* Remove JIT code registration and unregistration breakpoint(s). */
7658
7659void
7660remove_jit_event_breakpoints (void)
7661{
7662 struct breakpoint *b, *b_tmp;
7663
7664 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7665 if (b->type == bp_jit_event
7666 && b->loc->pspace == current_program_space)
7667 delete_breakpoint (b);
7668}
7669
cae688ec
JJ
7670void
7671remove_solib_event_breakpoints (void)
7672{
35df4500 7673 struct breakpoint *b, *b_tmp;
cae688ec 7674
35df4500 7675 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7676 if (b->type == bp_shlib_event
7677 && b->loc->pspace == current_program_space)
cae688ec
JJ
7678 delete_breakpoint (b);
7679}
7680
f37f681c
PA
7681/* See breakpoint.h. */
7682
7683void
7684remove_solib_event_breakpoints_at_next_stop (void)
7685{
7686 struct breakpoint *b, *b_tmp;
7687
7688 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7689 if (b->type == bp_shlib_event
7690 && b->loc->pspace == current_program_space)
7691 b->disposition = disp_del_at_next_stop;
7692}
7693
04086b45
PA
7694/* Helper for create_solib_event_breakpoint /
7695 create_and_insert_solib_event_breakpoint. Allows specifying which
7696 INSERT_MODE to pass through to update_global_location_list. */
7697
7698static struct breakpoint *
7699create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7700 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7701{
7702 struct breakpoint *b;
7703
06edf0c0
PA
7704 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7705 &internal_breakpoint_ops);
04086b45 7706 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7707 return b;
7708}
7709
04086b45
PA
7710struct breakpoint *
7711create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7712{
7713 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7714}
7715
f37f681c
PA
7716/* See breakpoint.h. */
7717
7718struct breakpoint *
7719create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7720{
7721 struct breakpoint *b;
7722
04086b45
PA
7723 /* Explicitly tell update_global_location_list to insert
7724 locations. */
7725 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7726 if (!b->loc->inserted)
7727 {
7728 delete_breakpoint (b);
7729 return NULL;
7730 }
7731 return b;
7732}
7733
cae688ec
JJ
7734/* Disable any breakpoints that are on code in shared libraries. Only
7735 apply to enabled breakpoints, disabled ones can just stay disabled. */
7736
7737void
cb851954 7738disable_breakpoints_in_shlibs (void)
cae688ec 7739{
876fa593 7740 struct bp_location *loc, **locp_tmp;
cae688ec 7741
876fa593 7742 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7743 {
2bdf28a0 7744 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7745 struct breakpoint *b = loc->owner;
2bdf28a0 7746
4a64f543
MS
7747 /* We apply the check to all breakpoints, including disabled for
7748 those with loc->duplicate set. This is so that when breakpoint
7749 becomes enabled, or the duplicate is removed, gdb will try to
7750 insert all breakpoints. If we don't set shlib_disabled here,
7751 we'll try to insert those breakpoints and fail. */
1042e4c0 7752 if (((b->type == bp_breakpoint)
508ccb1f 7753 || (b->type == bp_jit_event)
1042e4c0 7754 || (b->type == bp_hardware_breakpoint)
d77f58be 7755 || (is_tracepoint (b)))
6c95b8df 7756 && loc->pspace == current_program_space
0d381245 7757 && !loc->shlib_disabled
6c95b8df 7758 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7759 )
0d381245
VP
7760 {
7761 loc->shlib_disabled = 1;
7762 }
cae688ec
JJ
7763 }
7764}
7765
63644780
NB
7766/* Disable any breakpoints and tracepoints that are in SOLIB upon
7767 notification of unloaded_shlib. Only apply to enabled breakpoints,
7768 disabled ones can just stay disabled. */
84acb35a 7769
75149521 7770static void
84acb35a
JJ
7771disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7772{
876fa593 7773 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7774 int disabled_shlib_breaks = 0;
7775
c86cf029
VP
7776 /* SunOS a.out shared libraries are always mapped, so do not
7777 disable breakpoints; they will only be reported as unloaded
7778 through clear_solib when GDB discards its shared library
7779 list. See clear_solib for more information. */
7780 if (exec_bfd != NULL
7781 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7782 return;
7783
876fa593 7784 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7785 {
2bdf28a0 7786 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7787 struct breakpoint *b = loc->owner;
cc59ec59 7788
1e4d1764 7789 if (solib->pspace == loc->pspace
e2dd7057 7790 && !loc->shlib_disabled
1e4d1764
YQ
7791 && (((b->type == bp_breakpoint
7792 || b->type == bp_jit_event
7793 || b->type == bp_hardware_breakpoint)
7794 && (loc->loc_type == bp_loc_hardware_breakpoint
7795 || loc->loc_type == bp_loc_software_breakpoint))
7796 || is_tracepoint (b))
e2dd7057 7797 && solib_contains_address_p (solib, loc->address))
84acb35a 7798 {
e2dd7057
PP
7799 loc->shlib_disabled = 1;
7800 /* At this point, we cannot rely on remove_breakpoint
7801 succeeding so we must mark the breakpoint as not inserted
7802 to prevent future errors occurring in remove_breakpoints. */
7803 loc->inserted = 0;
8d3788bd
VP
7804
7805 /* This may cause duplicate notifications for the same breakpoint. */
7806 observer_notify_breakpoint_modified (b);
7807
e2dd7057
PP
7808 if (!disabled_shlib_breaks)
7809 {
7810 target_terminal_ours_for_output ();
3e43a32a
MS
7811 warning (_("Temporarily disabling breakpoints "
7812 "for unloaded shared library \"%s\""),
e2dd7057 7813 solib->so_name);
84acb35a 7814 }
e2dd7057 7815 disabled_shlib_breaks = 1;
84acb35a
JJ
7816 }
7817 }
84acb35a
JJ
7818}
7819
63644780
NB
7820/* Disable any breakpoints and tracepoints in OBJFILE upon
7821 notification of free_objfile. Only apply to enabled breakpoints,
7822 disabled ones can just stay disabled. */
7823
7824static void
7825disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7826{
7827 struct breakpoint *b;
7828
7829 if (objfile == NULL)
7830 return;
7831
d03de421
PA
7832 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7833 managed by the user with add-symbol-file/remove-symbol-file.
7834 Similarly to how breakpoints in shared libraries are handled in
7835 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7836 shlib_disabled so they end up uninserted on the next global
7837 location list update. Shared libraries not loaded by the user
7838 aren't handled here -- they're already handled in
7839 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7840 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7841 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7842 main objfile). */
7843 if ((objfile->flags & OBJF_SHARED) == 0
7844 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7845 return;
7846
7847 ALL_BREAKPOINTS (b)
7848 {
7849 struct bp_location *loc;
7850 int bp_modified = 0;
7851
7852 if (!is_breakpoint (b) && !is_tracepoint (b))
7853 continue;
7854
7855 for (loc = b->loc; loc != NULL; loc = loc->next)
7856 {
7857 CORE_ADDR loc_addr = loc->address;
7858
7859 if (loc->loc_type != bp_loc_hardware_breakpoint
7860 && loc->loc_type != bp_loc_software_breakpoint)
7861 continue;
7862
7863 if (loc->shlib_disabled != 0)
7864 continue;
7865
7866 if (objfile->pspace != loc->pspace)
7867 continue;
7868
7869 if (loc->loc_type != bp_loc_hardware_breakpoint
7870 && loc->loc_type != bp_loc_software_breakpoint)
7871 continue;
7872
7873 if (is_addr_in_objfile (loc_addr, objfile))
7874 {
7875 loc->shlib_disabled = 1;
08351840
PA
7876 /* At this point, we don't know whether the object was
7877 unmapped from the inferior or not, so leave the
7878 inserted flag alone. We'll handle failure to
7879 uninsert quietly, in case the object was indeed
7880 unmapped. */
63644780
NB
7881
7882 mark_breakpoint_location_modified (loc);
7883
7884 bp_modified = 1;
7885 }
7886 }
7887
7888 if (bp_modified)
7889 observer_notify_breakpoint_modified (b);
7890 }
7891}
7892
ce78b96d
JB
7893/* FORK & VFORK catchpoints. */
7894
e29a4733
PA
7895/* An instance of this type is used to represent a fork or vfork
7896 catchpoint. It includes a "struct breakpoint" as a kind of base
7897 class; users downcast to "struct breakpoint *" when needed. A
7898 breakpoint is really of this type iff its ops pointer points to
7899 CATCH_FORK_BREAKPOINT_OPS. */
7900
7901struct fork_catchpoint
7902{
7903 /* The base class. */
7904 struct breakpoint base;
7905
7906 /* Process id of a child process whose forking triggered this
7907 catchpoint. This field is only valid immediately after this
7908 catchpoint has triggered. */
7909 ptid_t forked_inferior_pid;
7910};
7911
4a64f543
MS
7912/* Implement the "insert" breakpoint_ops method for fork
7913 catchpoints. */
ce78b96d 7914
77b06cd7
TJB
7915static int
7916insert_catch_fork (struct bp_location *bl)
ce78b96d 7917{
dfd4cc63 7918 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7919}
7920
4a64f543
MS
7921/* Implement the "remove" breakpoint_ops method for fork
7922 catchpoints. */
ce78b96d
JB
7923
7924static int
77b06cd7 7925remove_catch_fork (struct bp_location *bl)
ce78b96d 7926{
dfd4cc63 7927 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7928}
7929
7930/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7931 catchpoints. */
7932
7933static int
f1310107 7934breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7935 struct address_space *aspace, CORE_ADDR bp_addr,
7936 const struct target_waitstatus *ws)
ce78b96d 7937{
e29a4733
PA
7938 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7939
f90263c1
TT
7940 if (ws->kind != TARGET_WAITKIND_FORKED)
7941 return 0;
7942
7943 c->forked_inferior_pid = ws->value.related_pid;
7944 return 1;
ce78b96d
JB
7945}
7946
4a64f543
MS
7947/* Implement the "print_it" breakpoint_ops method for fork
7948 catchpoints. */
ce78b96d
JB
7949
7950static enum print_stop_action
348d480f 7951print_it_catch_fork (bpstat bs)
ce78b96d 7952{
36dfb11c 7953 struct ui_out *uiout = current_uiout;
348d480f
PA
7954 struct breakpoint *b = bs->breakpoint_at;
7955 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7956
ce78b96d 7957 annotate_catchpoint (b->number);
36dfb11c
TT
7958 if (b->disposition == disp_del)
7959 ui_out_text (uiout, "\nTemporary catchpoint ");
7960 else
7961 ui_out_text (uiout, "\nCatchpoint ");
7962 if (ui_out_is_mi_like_p (uiout))
7963 {
7964 ui_out_field_string (uiout, "reason",
7965 async_reason_lookup (EXEC_ASYNC_FORK));
7966 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7967 }
7968 ui_out_field_int (uiout, "bkptno", b->number);
7969 ui_out_text (uiout, " (forked process ");
7970 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7971 ui_out_text (uiout, "), ");
ce78b96d
JB
7972 return PRINT_SRC_AND_LOC;
7973}
7974
4a64f543
MS
7975/* Implement the "print_one" breakpoint_ops method for fork
7976 catchpoints. */
ce78b96d
JB
7977
7978static void
a6d9a66e 7979print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7980{
e29a4733 7981 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7982 struct value_print_options opts;
79a45e25 7983 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7984
7985 get_user_print_options (&opts);
7986
4a64f543
MS
7987 /* Field 4, the address, is omitted (which makes the columns not
7988 line up too nicely with the headers, but the effect is relatively
7989 readable). */
79a45b7d 7990 if (opts.addressprint)
ce78b96d
JB
7991 ui_out_field_skip (uiout, "addr");
7992 annotate_field (5);
7993 ui_out_text (uiout, "fork");
e29a4733 7994 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7995 {
7996 ui_out_text (uiout, ", process ");
7997 ui_out_field_int (uiout, "what",
e29a4733 7998 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7999 ui_out_spaces (uiout, 1);
8000 }
8ac3646f
TT
8001
8002 if (ui_out_is_mi_like_p (uiout))
8003 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8004}
8005
8006/* Implement the "print_mention" breakpoint_ops method for fork
8007 catchpoints. */
8008
8009static void
8010print_mention_catch_fork (struct breakpoint *b)
8011{
8012 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8013}
8014
6149aea9
PA
8015/* Implement the "print_recreate" breakpoint_ops method for fork
8016 catchpoints. */
8017
8018static void
8019print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8020{
8021 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8022 print_recreate_thread (b, fp);
6149aea9
PA
8023}
8024
ce78b96d
JB
8025/* The breakpoint_ops structure to be used in fork catchpoints. */
8026
2060206e 8027static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8028
4a64f543
MS
8029/* Implement the "insert" breakpoint_ops method for vfork
8030 catchpoints. */
ce78b96d 8031
77b06cd7
TJB
8032static int
8033insert_catch_vfork (struct bp_location *bl)
ce78b96d 8034{
dfd4cc63 8035 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8036}
8037
4a64f543
MS
8038/* Implement the "remove" breakpoint_ops method for vfork
8039 catchpoints. */
ce78b96d
JB
8040
8041static int
77b06cd7 8042remove_catch_vfork (struct bp_location *bl)
ce78b96d 8043{
dfd4cc63 8044 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8045}
8046
8047/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8048 catchpoints. */
8049
8050static int
f1310107 8051breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8052 struct address_space *aspace, CORE_ADDR bp_addr,
8053 const struct target_waitstatus *ws)
ce78b96d 8054{
e29a4733
PA
8055 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8056
f90263c1
TT
8057 if (ws->kind != TARGET_WAITKIND_VFORKED)
8058 return 0;
8059
8060 c->forked_inferior_pid = ws->value.related_pid;
8061 return 1;
ce78b96d
JB
8062}
8063
4a64f543
MS
8064/* Implement the "print_it" breakpoint_ops method for vfork
8065 catchpoints. */
ce78b96d
JB
8066
8067static enum print_stop_action
348d480f 8068print_it_catch_vfork (bpstat bs)
ce78b96d 8069{
36dfb11c 8070 struct ui_out *uiout = current_uiout;
348d480f 8071 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8072 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8073
ce78b96d 8074 annotate_catchpoint (b->number);
36dfb11c
TT
8075 if (b->disposition == disp_del)
8076 ui_out_text (uiout, "\nTemporary catchpoint ");
8077 else
8078 ui_out_text (uiout, "\nCatchpoint ");
8079 if (ui_out_is_mi_like_p (uiout))
8080 {
8081 ui_out_field_string (uiout, "reason",
8082 async_reason_lookup (EXEC_ASYNC_VFORK));
8083 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8084 }
8085 ui_out_field_int (uiout, "bkptno", b->number);
8086 ui_out_text (uiout, " (vforked process ");
8087 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8088 ui_out_text (uiout, "), ");
ce78b96d
JB
8089 return PRINT_SRC_AND_LOC;
8090}
8091
4a64f543
MS
8092/* Implement the "print_one" breakpoint_ops method for vfork
8093 catchpoints. */
ce78b96d
JB
8094
8095static void
a6d9a66e 8096print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8097{
e29a4733 8098 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8099 struct value_print_options opts;
79a45e25 8100 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8101
8102 get_user_print_options (&opts);
4a64f543
MS
8103 /* Field 4, the address, is omitted (which makes the columns not
8104 line up too nicely with the headers, but the effect is relatively
8105 readable). */
79a45b7d 8106 if (opts.addressprint)
ce78b96d
JB
8107 ui_out_field_skip (uiout, "addr");
8108 annotate_field (5);
8109 ui_out_text (uiout, "vfork");
e29a4733 8110 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8111 {
8112 ui_out_text (uiout, ", process ");
8113 ui_out_field_int (uiout, "what",
e29a4733 8114 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8115 ui_out_spaces (uiout, 1);
8116 }
8ac3646f
TT
8117
8118 if (ui_out_is_mi_like_p (uiout))
8119 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8120}
8121
8122/* Implement the "print_mention" breakpoint_ops method for vfork
8123 catchpoints. */
8124
8125static void
8126print_mention_catch_vfork (struct breakpoint *b)
8127{
8128 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8129}
8130
6149aea9
PA
8131/* Implement the "print_recreate" breakpoint_ops method for vfork
8132 catchpoints. */
8133
8134static void
8135print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8136{
8137 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8138 print_recreate_thread (b, fp);
6149aea9
PA
8139}
8140
ce78b96d
JB
8141/* The breakpoint_ops structure to be used in vfork catchpoints. */
8142
2060206e 8143static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8144
edcc5120
TT
8145/* An instance of this type is used to represent an solib catchpoint.
8146 It includes a "struct breakpoint" as a kind of base class; users
8147 downcast to "struct breakpoint *" when needed. A breakpoint is
8148 really of this type iff its ops pointer points to
8149 CATCH_SOLIB_BREAKPOINT_OPS. */
8150
8151struct solib_catchpoint
8152{
8153 /* The base class. */
8154 struct breakpoint base;
8155
8156 /* True for "catch load", false for "catch unload". */
8157 unsigned char is_load;
8158
8159 /* Regular expression to match, if any. COMPILED is only valid when
8160 REGEX is non-NULL. */
8161 char *regex;
8162 regex_t compiled;
8163};
8164
8165static void
8166dtor_catch_solib (struct breakpoint *b)
8167{
8168 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8169
8170 if (self->regex)
8171 regfree (&self->compiled);
8172 xfree (self->regex);
8173
8174 base_breakpoint_ops.dtor (b);
8175}
8176
8177static int
8178insert_catch_solib (struct bp_location *ignore)
8179{
8180 return 0;
8181}
8182
8183static int
8184remove_catch_solib (struct bp_location *ignore)
8185{
8186 return 0;
8187}
8188
8189static int
8190breakpoint_hit_catch_solib (const struct bp_location *bl,
8191 struct address_space *aspace,
8192 CORE_ADDR bp_addr,
8193 const struct target_waitstatus *ws)
8194{
8195 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8196 struct breakpoint *other;
8197
8198 if (ws->kind == TARGET_WAITKIND_LOADED)
8199 return 1;
8200
8201 ALL_BREAKPOINTS (other)
8202 {
8203 struct bp_location *other_bl;
8204
8205 if (other == bl->owner)
8206 continue;
8207
8208 if (other->type != bp_shlib_event)
8209 continue;
8210
8211 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8212 continue;
8213
8214 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8215 {
8216 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8217 return 1;
8218 }
8219 }
8220
8221 return 0;
8222}
8223
8224static void
8225check_status_catch_solib (struct bpstats *bs)
8226{
8227 struct solib_catchpoint *self
8228 = (struct solib_catchpoint *) bs->breakpoint_at;
8229 int ix;
8230
8231 if (self->is_load)
8232 {
8233 struct so_list *iter;
8234
8235 for (ix = 0;
8236 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8237 ix, iter);
8238 ++ix)
8239 {
8240 if (!self->regex
8241 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8242 return;
8243 }
8244 }
8245 else
8246 {
8247 char *iter;
8248
8249 for (ix = 0;
8250 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8251 ix, iter);
8252 ++ix)
8253 {
8254 if (!self->regex
8255 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8256 return;
8257 }
8258 }
8259
8260 bs->stop = 0;
8261 bs->print_it = print_it_noop;
8262}
8263
8264static enum print_stop_action
8265print_it_catch_solib (bpstat bs)
8266{
8267 struct breakpoint *b = bs->breakpoint_at;
8268 struct ui_out *uiout = current_uiout;
8269
8270 annotate_catchpoint (b->number);
8271 if (b->disposition == disp_del)
8272 ui_out_text (uiout, "\nTemporary catchpoint ");
8273 else
8274 ui_out_text (uiout, "\nCatchpoint ");
8275 ui_out_field_int (uiout, "bkptno", b->number);
8276 ui_out_text (uiout, "\n");
8277 if (ui_out_is_mi_like_p (uiout))
8278 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8279 print_solib_event (1);
8280 return PRINT_SRC_AND_LOC;
8281}
8282
8283static void
8284print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8285{
8286 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8287 struct value_print_options opts;
8288 struct ui_out *uiout = current_uiout;
8289 char *msg;
8290
8291 get_user_print_options (&opts);
8292 /* Field 4, the address, is omitted (which makes the columns not
8293 line up too nicely with the headers, but the effect is relatively
8294 readable). */
8295 if (opts.addressprint)
8296 {
8297 annotate_field (4);
8298 ui_out_field_skip (uiout, "addr");
8299 }
8300
8301 annotate_field (5);
8302 if (self->is_load)
8303 {
8304 if (self->regex)
8305 msg = xstrprintf (_("load of library matching %s"), self->regex);
8306 else
8307 msg = xstrdup (_("load of library"));
8308 }
8309 else
8310 {
8311 if (self->regex)
8312 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8313 else
8314 msg = xstrdup (_("unload of library"));
8315 }
8316 ui_out_field_string (uiout, "what", msg);
8317 xfree (msg);
8ac3646f
TT
8318
8319 if (ui_out_is_mi_like_p (uiout))
8320 ui_out_field_string (uiout, "catch-type",
8321 self->is_load ? "load" : "unload");
edcc5120
TT
8322}
8323
8324static void
8325print_mention_catch_solib (struct breakpoint *b)
8326{
8327 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8328
8329 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8330 self->is_load ? "load" : "unload");
8331}
8332
8333static void
8334print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8335{
8336 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8337
8338 fprintf_unfiltered (fp, "%s %s",
8339 b->disposition == disp_del ? "tcatch" : "catch",
8340 self->is_load ? "load" : "unload");
8341 if (self->regex)
8342 fprintf_unfiltered (fp, " %s", self->regex);
8343 fprintf_unfiltered (fp, "\n");
8344}
8345
8346static struct breakpoint_ops catch_solib_breakpoint_ops;
8347
91985142
MG
8348/* Shared helper function (MI and CLI) for creating and installing
8349 a shared object event catchpoint. If IS_LOAD is non-zero then
8350 the events to be caught are load events, otherwise they are
8351 unload events. If IS_TEMP is non-zero the catchpoint is a
8352 temporary one. If ENABLED is non-zero the catchpoint is
8353 created in an enabled state. */
edcc5120 8354
91985142
MG
8355void
8356add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8357{
8358 struct solib_catchpoint *c;
8359 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8360 struct cleanup *cleanup;
8361
edcc5120
TT
8362 if (!arg)
8363 arg = "";
8364 arg = skip_spaces (arg);
8365
8366 c = XCNEW (struct solib_catchpoint);
8367 cleanup = make_cleanup (xfree, c);
8368
8369 if (*arg != '\0')
8370 {
8371 int errcode;
8372
8373 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8374 if (errcode != 0)
8375 {
8376 char *err = get_regcomp_error (errcode, &c->compiled);
8377
8378 make_cleanup (xfree, err);
8379 error (_("Invalid regexp (%s): %s"), err, arg);
8380 }
8381 c->regex = xstrdup (arg);
8382 }
8383
8384 c->is_load = is_load;
91985142 8385 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8386 &catch_solib_breakpoint_ops);
8387
91985142
MG
8388 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8389
edcc5120
TT
8390 discard_cleanups (cleanup);
8391 install_breakpoint (0, &c->base, 1);
8392}
8393
91985142
MG
8394/* A helper function that does all the work for "catch load" and
8395 "catch unload". */
8396
8397static void
8398catch_load_or_unload (char *arg, int from_tty, int is_load,
8399 struct cmd_list_element *command)
8400{
8401 int tempflag;
8402 const int enabled = 1;
8403
8404 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8405
8406 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8407}
8408
edcc5120
TT
8409static void
8410catch_load_command_1 (char *arg, int from_tty,
8411 struct cmd_list_element *command)
8412{
8413 catch_load_or_unload (arg, from_tty, 1, command);
8414}
8415
8416static void
8417catch_unload_command_1 (char *arg, int from_tty,
8418 struct cmd_list_element *command)
8419{
8420 catch_load_or_unload (arg, from_tty, 0, command);
8421}
8422
346774a9
PA
8423/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8424 is non-zero, then make the breakpoint temporary. If COND_STRING is
8425 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8426 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8427
ab04a2af 8428void
346774a9
PA
8429init_catchpoint (struct breakpoint *b,
8430 struct gdbarch *gdbarch, int tempflag,
8431 char *cond_string,
c0a91b2b 8432 const struct breakpoint_ops *ops)
c906108c 8433{
c5aa993b 8434 struct symtab_and_line sal;
346774a9 8435
fe39c653 8436 init_sal (&sal);
6c95b8df 8437 sal.pspace = current_program_space;
c5aa993b 8438
28010a5d 8439 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8440
1b36a34b 8441 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8442 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8443}
8444
28010a5d 8445void
3ea46bff 8446install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8447{
8448 add_to_breakpoint_chain (b);
3a5c3e22 8449 set_breakpoint_number (internal, b);
558a9d82
YQ
8450 if (is_tracepoint (b))
8451 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8452 if (!internal)
8453 mention (b);
c56053d2 8454 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8455
8456 if (update_gll)
44702360 8457 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8458}
8459
9b70b993 8460static void
a6d9a66e
UW
8461create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8462 int tempflag, char *cond_string,
c0a91b2b 8463 const struct breakpoint_ops *ops)
c906108c 8464{
e29a4733 8465 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8466
e29a4733
PA
8467 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8468
8469 c->forked_inferior_pid = null_ptid;
8470
3ea46bff 8471 install_breakpoint (0, &c->base, 1);
c906108c
SS
8472}
8473
fe798b75
JB
8474/* Exec catchpoints. */
8475
b4d90040
PA
8476/* An instance of this type is used to represent an exec catchpoint.
8477 It includes a "struct breakpoint" as a kind of base class; users
8478 downcast to "struct breakpoint *" when needed. A breakpoint is
8479 really of this type iff its ops pointer points to
8480 CATCH_EXEC_BREAKPOINT_OPS. */
8481
8482struct exec_catchpoint
8483{
8484 /* The base class. */
8485 struct breakpoint base;
8486
8487 /* Filename of a program whose exec triggered this catchpoint.
8488 This field is only valid immediately after this catchpoint has
8489 triggered. */
8490 char *exec_pathname;
8491};
8492
8493/* Implement the "dtor" breakpoint_ops method for exec
8494 catchpoints. */
8495
8496static void
8497dtor_catch_exec (struct breakpoint *b)
8498{
8499 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8500
8501 xfree (c->exec_pathname);
348d480f 8502
2060206e 8503 base_breakpoint_ops.dtor (b);
b4d90040
PA
8504}
8505
77b06cd7
TJB
8506static int
8507insert_catch_exec (struct bp_location *bl)
c906108c 8508{
dfd4cc63 8509 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8510}
c906108c 8511
fe798b75 8512static int
77b06cd7 8513remove_catch_exec (struct bp_location *bl)
fe798b75 8514{
dfd4cc63 8515 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8516}
c906108c 8517
fe798b75 8518static int
f1310107 8519breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8520 struct address_space *aspace, CORE_ADDR bp_addr,
8521 const struct target_waitstatus *ws)
fe798b75 8522{
b4d90040
PA
8523 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8524
f90263c1
TT
8525 if (ws->kind != TARGET_WAITKIND_EXECD)
8526 return 0;
8527
8528 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8529 return 1;
fe798b75 8530}
c906108c 8531
fe798b75 8532static enum print_stop_action
348d480f 8533print_it_catch_exec (bpstat bs)
fe798b75 8534{
36dfb11c 8535 struct ui_out *uiout = current_uiout;
348d480f 8536 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8537 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8538
fe798b75 8539 annotate_catchpoint (b->number);
36dfb11c
TT
8540 if (b->disposition == disp_del)
8541 ui_out_text (uiout, "\nTemporary catchpoint ");
8542 else
8543 ui_out_text (uiout, "\nCatchpoint ");
8544 if (ui_out_is_mi_like_p (uiout))
8545 {
8546 ui_out_field_string (uiout, "reason",
8547 async_reason_lookup (EXEC_ASYNC_EXEC));
8548 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8549 }
8550 ui_out_field_int (uiout, "bkptno", b->number);
8551 ui_out_text (uiout, " (exec'd ");
8552 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8553 ui_out_text (uiout, "), ");
8554
fe798b75 8555 return PRINT_SRC_AND_LOC;
c906108c
SS
8556}
8557
fe798b75 8558static void
a6d9a66e 8559print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8560{
b4d90040 8561 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8562 struct value_print_options opts;
79a45e25 8563 struct ui_out *uiout = current_uiout;
fe798b75
JB
8564
8565 get_user_print_options (&opts);
8566
8567 /* Field 4, the address, is omitted (which makes the columns
8568 not line up too nicely with the headers, but the effect
8569 is relatively readable). */
8570 if (opts.addressprint)
8571 ui_out_field_skip (uiout, "addr");
8572 annotate_field (5);
8573 ui_out_text (uiout, "exec");
b4d90040 8574 if (c->exec_pathname != NULL)
fe798b75
JB
8575 {
8576 ui_out_text (uiout, ", program \"");
b4d90040 8577 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8578 ui_out_text (uiout, "\" ");
8579 }
8ac3646f
TT
8580
8581 if (ui_out_is_mi_like_p (uiout))
8582 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8583}
8584
8585static void
8586print_mention_catch_exec (struct breakpoint *b)
8587{
8588 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8589}
8590
6149aea9
PA
8591/* Implement the "print_recreate" breakpoint_ops method for exec
8592 catchpoints. */
8593
8594static void
8595print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8596{
8597 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8598 print_recreate_thread (b, fp);
6149aea9
PA
8599}
8600
2060206e 8601static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8602
c906108c 8603static int
fba45db2 8604hw_breakpoint_used_count (void)
c906108c 8605{
c906108c 8606 int i = 0;
f1310107
TJB
8607 struct breakpoint *b;
8608 struct bp_location *bl;
c906108c
SS
8609
8610 ALL_BREAKPOINTS (b)
c5aa993b 8611 {
d6b74ac4 8612 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8613 for (bl = b->loc; bl; bl = bl->next)
8614 {
8615 /* Special types of hardware breakpoints may use more than
8616 one register. */
348d480f 8617 i += b->ops->resources_needed (bl);
f1310107 8618 }
c5aa993b 8619 }
c906108c
SS
8620
8621 return i;
8622}
8623
a1398e0c
PA
8624/* Returns the resources B would use if it were a hardware
8625 watchpoint. */
8626
c906108c 8627static int
a1398e0c 8628hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8629{
c906108c 8630 int i = 0;
e09342b5 8631 struct bp_location *bl;
c906108c 8632
a1398e0c
PA
8633 if (!breakpoint_enabled (b))
8634 return 0;
8635
8636 for (bl = b->loc; bl; bl = bl->next)
8637 {
8638 /* Special types of hardware watchpoints may use more than
8639 one register. */
8640 i += b->ops->resources_needed (bl);
8641 }
8642
8643 return i;
8644}
8645
8646/* Returns the sum the used resources of all hardware watchpoints of
8647 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8648 the sum of the used resources of all hardware watchpoints of other
8649 types _not_ TYPE. */
8650
8651static int
8652hw_watchpoint_used_count_others (struct breakpoint *except,
8653 enum bptype type, int *other_type_used)
8654{
8655 int i = 0;
8656 struct breakpoint *b;
8657
c906108c
SS
8658 *other_type_used = 0;
8659 ALL_BREAKPOINTS (b)
e09342b5 8660 {
a1398e0c
PA
8661 if (b == except)
8662 continue;
e09342b5
TJB
8663 if (!breakpoint_enabled (b))
8664 continue;
8665
a1398e0c
PA
8666 if (b->type == type)
8667 i += hw_watchpoint_use_count (b);
8668 else if (is_hardware_watchpoint (b))
8669 *other_type_used = 1;
e09342b5
TJB
8670 }
8671
c906108c
SS
8672 return i;
8673}
8674
c906108c 8675void
fba45db2 8676disable_watchpoints_before_interactive_call_start (void)
c906108c 8677{
c5aa993b 8678 struct breakpoint *b;
c906108c
SS
8679
8680 ALL_BREAKPOINTS (b)
c5aa993b 8681 {
cc60f2e3 8682 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8683 {
b5de0fa7 8684 b->enable_state = bp_call_disabled;
44702360 8685 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8686 }
8687 }
c906108c
SS
8688}
8689
8690void
fba45db2 8691enable_watchpoints_after_interactive_call_stop (void)
c906108c 8692{
c5aa993b 8693 struct breakpoint *b;
c906108c
SS
8694
8695 ALL_BREAKPOINTS (b)
c5aa993b 8696 {
cc60f2e3 8697 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8698 {
b5de0fa7 8699 b->enable_state = bp_enabled;
44702360 8700 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8701 }
8702 }
c906108c
SS
8703}
8704
8bea4e01
UW
8705void
8706disable_breakpoints_before_startup (void)
8707{
6c95b8df 8708 current_program_space->executing_startup = 1;
44702360 8709 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8710}
8711
8712void
8713enable_breakpoints_after_startup (void)
8714{
6c95b8df 8715 current_program_space->executing_startup = 0;
f8eba3c6 8716 breakpoint_re_set ();
8bea4e01
UW
8717}
8718
7c16b83e
PA
8719/* Create a new single-step breakpoint for thread THREAD, with no
8720 locations. */
c906108c 8721
7c16b83e
PA
8722static struct breakpoint *
8723new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8724{
8725 struct breakpoint *b = XNEW (struct breakpoint);
8726
8727 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8728 &momentary_breakpoint_ops);
8729
8730 b->disposition = disp_donttouch;
8731 b->frame_id = null_frame_id;
8732
8733 b->thread = thread;
8734 gdb_assert (b->thread != 0);
8735
8736 add_to_breakpoint_chain (b);
8737
8738 return b;
8739}
8740
8741/* Set a momentary breakpoint of type TYPE at address specified by
8742 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8743 frame. */
c906108c
SS
8744
8745struct breakpoint *
a6d9a66e
UW
8746set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8747 struct frame_id frame_id, enum bptype type)
c906108c 8748{
52f0bd74 8749 struct breakpoint *b;
edb3359d 8750
193facb3
JK
8751 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8752 tail-called one. */
8753 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8754
06edf0c0 8755 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8756 b->enable_state = bp_enabled;
8757 b->disposition = disp_donttouch;
818dd999 8758 b->frame_id = frame_id;
c906108c 8759
4a64f543
MS
8760 /* If we're debugging a multi-threaded program, then we want
8761 momentary breakpoints to be active in only a single thread of
8762 control. */
39f77062
KB
8763 if (in_thread_list (inferior_ptid))
8764 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 8765
44702360 8766 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8767
c906108c
SS
8768 return b;
8769}
611c83ae 8770
06edf0c0 8771/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8772 The new breakpoint will have type TYPE, use OPS as its
8773 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8774
06edf0c0
PA
8775static struct breakpoint *
8776momentary_breakpoint_from_master (struct breakpoint *orig,
8777 enum bptype type,
a1aa2221
LM
8778 const struct breakpoint_ops *ops,
8779 int loc_enabled)
e58b0e63
PA
8780{
8781 struct breakpoint *copy;
8782
06edf0c0 8783 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8784 copy->loc = allocate_bp_location (copy);
0e30163f 8785 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8786
a6d9a66e 8787 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8788 copy->loc->requested_address = orig->loc->requested_address;
8789 copy->loc->address = orig->loc->address;
8790 copy->loc->section = orig->loc->section;
6c95b8df 8791 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8792 copy->loc->probe = orig->loc->probe;
f8eba3c6 8793 copy->loc->line_number = orig->loc->line_number;
2f202fde 8794 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8795 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8796 copy->frame_id = orig->frame_id;
8797 copy->thread = orig->thread;
6c95b8df 8798 copy->pspace = orig->pspace;
e58b0e63
PA
8799
8800 copy->enable_state = bp_enabled;
8801 copy->disposition = disp_donttouch;
8802 copy->number = internal_breakpoint_number--;
8803
44702360 8804 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8805 return copy;
8806}
8807
06edf0c0
PA
8808/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8809 ORIG is NULL. */
8810
8811struct breakpoint *
8812clone_momentary_breakpoint (struct breakpoint *orig)
8813{
8814 /* If there's nothing to clone, then return nothing. */
8815 if (orig == NULL)
8816 return NULL;
8817
a1aa2221 8818 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8819}
8820
611c83ae 8821struct breakpoint *
a6d9a66e
UW
8822set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8823 enum bptype type)
611c83ae
PA
8824{
8825 struct symtab_and_line sal;
8826
8827 sal = find_pc_line (pc, 0);
8828 sal.pc = pc;
8829 sal.section = find_pc_overlay (pc);
8830 sal.explicit_pc = 1;
8831
a6d9a66e 8832 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8833}
c906108c 8834\f
c5aa993b 8835
c906108c
SS
8836/* Tell the user we have just set a breakpoint B. */
8837
8838static void
fba45db2 8839mention (struct breakpoint *b)
c906108c 8840{
348d480f 8841 b->ops->print_mention (b);
79a45e25 8842 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 8843 return;
c906108c
SS
8844 printf_filtered ("\n");
8845}
c906108c 8846\f
c5aa993b 8847
1a853c52
PA
8848static int bp_loc_is_permanent (struct bp_location *loc);
8849
0d381245 8850static struct bp_location *
39d61571 8851add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8852 const struct symtab_and_line *sal)
8853{
8854 struct bp_location *loc, **tmp;
3742cc8b
YQ
8855 CORE_ADDR adjusted_address;
8856 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8857
8858 if (loc_gdbarch == NULL)
8859 loc_gdbarch = b->gdbarch;
8860
8861 /* Adjust the breakpoint's address prior to allocating a location.
8862 Once we call allocate_bp_location(), that mostly uninitialized
8863 location will be placed on the location chain. Adjustment of the
8864 breakpoint may cause target_read_memory() to be called and we do
8865 not want its scan of the location chain to find a breakpoint and
8866 location that's only been partially initialized. */
8867 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8868 sal->pc, b->type);
0d381245 8869
d30113d4 8870 /* Sort the locations by their ADDRESS. */
39d61571 8871 loc = allocate_bp_location (b);
d30113d4
JK
8872 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8873 tmp = &((*tmp)->next))
0d381245 8874 ;
d30113d4 8875 loc->next = *tmp;
0d381245 8876 *tmp = loc;
3742cc8b 8877
0d381245 8878 loc->requested_address = sal->pc;
3742cc8b 8879 loc->address = adjusted_address;
6c95b8df 8880 loc->pspace = sal->pspace;
729662a5
TT
8881 loc->probe.probe = sal->probe;
8882 loc->probe.objfile = sal->objfile;
6c95b8df 8883 gdb_assert (loc->pspace != NULL);
0d381245 8884 loc->section = sal->section;
3742cc8b 8885 loc->gdbarch = loc_gdbarch;
f8eba3c6 8886 loc->line_number = sal->line;
2f202fde 8887 loc->symtab = sal->symtab;
f8eba3c6 8888
0e30163f
JK
8889 set_breakpoint_location_function (loc,
8890 sal->explicit_pc || sal->explicit_line);
1a853c52 8891
6ae88661
LM
8892 /* While by definition, permanent breakpoints are already present in the
8893 code, we don't mark the location as inserted. Normally one would expect
8894 that GDB could rely on that breakpoint instruction to stop the program,
8895 thus removing the need to insert its own breakpoint, except that executing
8896 the breakpoint instruction can kill the target instead of reporting a
8897 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8898 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8899 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8900 breakpoint be inserted normally results in QEMU knowing about the GDB
8901 breakpoint, and thus trap before the breakpoint instruction is executed.
8902 (If GDB later needs to continue execution past the permanent breakpoint,
8903 it manually increments the PC, thus avoiding executing the breakpoint
8904 instruction.) */
1a853c52 8905 if (bp_loc_is_permanent (loc))
6ae88661 8906 loc->permanent = 1;
1a853c52 8907
0d381245
VP
8908 return loc;
8909}
514f746b
AR
8910\f
8911
1cf4d951 8912/* See breakpoint.h. */
514f746b 8913
1cf4d951
PA
8914int
8915program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8916{
8917 int len;
8918 CORE_ADDR addr;
1afeeb75 8919 const gdb_byte *bpoint;
514f746b 8920 gdb_byte *target_mem;
939c61fa
JK
8921 struct cleanup *cleanup;
8922 int retval = 0;
514f746b 8923
1cf4d951
PA
8924 addr = address;
8925 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8926
8927 /* Software breakpoints unsupported? */
8928 if (bpoint == NULL)
8929 return 0;
8930
8931 target_mem = alloca (len);
8932
8933 /* Enable the automatic memory restoration from breakpoints while
8934 we read the memory. Otherwise we could say about our temporary
8935 breakpoints they are permanent. */
8936 cleanup = make_show_memory_breakpoints_cleanup (0);
8937
8938 if (target_read_memory (address, target_mem, len) == 0
8939 && memcmp (target_mem, bpoint, len) == 0)
8940 retval = 1;
8941
8942 do_cleanups (cleanup);
8943
8944 return retval;
8945}
8946
8947/* Return 1 if LOC is pointing to a permanent breakpoint,
8948 return 0 otherwise. */
8949
8950static int
8951bp_loc_is_permanent (struct bp_location *loc)
8952{
8953 struct cleanup *cleanup;
8954 int retval;
8955
514f746b
AR
8956 gdb_assert (loc != NULL);
8957
6c95b8df 8958 cleanup = save_current_space_and_thread ();
6c95b8df 8959 switch_to_program_space_and_thread (loc->pspace);
939c61fa 8960
1cf4d951 8961 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b 8962
939c61fa
JK
8963 do_cleanups (cleanup);
8964
8965 return retval;
514f746b
AR
8966}
8967
e7e0cddf
SS
8968/* Build a command list for the dprintf corresponding to the current
8969 settings of the dprintf style options. */
8970
8971static void
8972update_dprintf_command_list (struct breakpoint *b)
8973{
8974 char *dprintf_args = b->extra_string;
8975 char *printf_line = NULL;
8976
8977 if (!dprintf_args)
8978 return;
8979
8980 dprintf_args = skip_spaces (dprintf_args);
8981
8982 /* Allow a comma, as it may have terminated a location, but don't
8983 insist on it. */
8984 if (*dprintf_args == ',')
8985 ++dprintf_args;
8986 dprintf_args = skip_spaces (dprintf_args);
8987
8988 if (*dprintf_args != '"')
8989 error (_("Bad format string, missing '\"'."));
8990
d3ce09f5 8991 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 8992 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 8993 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
8994 {
8995 if (!dprintf_function)
8996 error (_("No function supplied for dprintf call"));
8997
8998 if (dprintf_channel && strlen (dprintf_channel) > 0)
8999 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9000 dprintf_function,
9001 dprintf_channel,
9002 dprintf_args);
9003 else
9004 printf_line = xstrprintf ("call (void) %s (%s)",
9005 dprintf_function,
9006 dprintf_args);
9007 }
d3ce09f5
SS
9008 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9009 {
9010 if (target_can_run_breakpoint_commands ())
9011 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9012 else
9013 {
9014 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9015 printf_line = xstrprintf ("printf %s", dprintf_args);
9016 }
9017 }
e7e0cddf
SS
9018 else
9019 internal_error (__FILE__, __LINE__,
9020 _("Invalid dprintf style."));
9021
f28045c2 9022 gdb_assert (printf_line != NULL);
9d6e6e84 9023 /* Manufacture a printf sequence. */
f28045c2 9024 {
9d6e6e84
HZ
9025 struct command_line *printf_cmd_line
9026 = xmalloc (sizeof (struct command_line));
e7e0cddf 9027
f28045c2
YQ
9028 printf_cmd_line->control_type = simple_control;
9029 printf_cmd_line->body_count = 0;
9030 printf_cmd_line->body_list = NULL;
9d6e6e84 9031 printf_cmd_line->next = NULL;
f28045c2 9032 printf_cmd_line->line = printf_line;
e7e0cddf 9033
f28045c2
YQ
9034 breakpoint_set_commands (b, printf_cmd_line);
9035 }
e7e0cddf
SS
9036}
9037
9038/* Update all dprintf commands, making their command lists reflect
9039 current style settings. */
9040
9041static void
9042update_dprintf_commands (char *args, int from_tty,
9043 struct cmd_list_element *c)
9044{
9045 struct breakpoint *b;
9046
9047 ALL_BREAKPOINTS (b)
9048 {
9049 if (b->type == bp_dprintf)
9050 update_dprintf_command_list (b);
9051 }
9052}
c3f6f71d 9053
018d34a4
VP
9054/* Create a breakpoint with SAL as location. Use ADDR_STRING
9055 as textual description of the location, and COND_STRING
db107f19 9056 as condition expression. */
018d34a4
VP
9057
9058static void
d9b3f62e
PA
9059init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9060 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9061 char *filter, char *cond_string,
e7e0cddf 9062 char *extra_string,
d9b3f62e
PA
9063 enum bptype type, enum bpdisp disposition,
9064 int thread, int task, int ignore_count,
c0a91b2b 9065 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9066 int enabled, int internal, unsigned flags,
9067 int display_canonical)
018d34a4 9068{
0d381245 9069 int i;
018d34a4
VP
9070
9071 if (type == bp_hardware_breakpoint)
9072 {
fbbd034e
AS
9073 int target_resources_ok;
9074
9075 i = hw_breakpoint_used_count ();
9076 target_resources_ok =
9077 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9078 i + 1, 0);
9079 if (target_resources_ok == 0)
9080 error (_("No hardware breakpoint support in the target."));
9081 else if (target_resources_ok < 0)
9082 error (_("Hardware breakpoints used exceeds limit."));
9083 }
9084
6c95b8df
PA
9085 gdb_assert (sals.nelts > 0);
9086
0d381245
VP
9087 for (i = 0; i < sals.nelts; ++i)
9088 {
9089 struct symtab_and_line sal = sals.sals[i];
9090 struct bp_location *loc;
9091
9092 if (from_tty)
5af949e3
UW
9093 {
9094 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9095 if (!loc_gdbarch)
9096 loc_gdbarch = gdbarch;
9097
9098 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9099 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9100 }
0d381245
VP
9101
9102 if (i == 0)
9103 {
d9b3f62e 9104 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9105 b->thread = thread;
4a306c9a 9106 b->task = task;
855a6e68 9107
0d381245 9108 b->cond_string = cond_string;
e7e0cddf 9109 b->extra_string = extra_string;
0d381245 9110 b->ignore_count = ignore_count;
41447f92 9111 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9112 b->disposition = disposition;
6c95b8df 9113
44f238bb
PA
9114 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9115 b->loc->inserted = 1;
9116
0fb4aa4b
PA
9117 if (type == bp_static_tracepoint)
9118 {
d9b3f62e 9119 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9120 struct static_tracepoint_marker marker;
9121
983af33b 9122 if (strace_marker_p (b))
0fb4aa4b
PA
9123 {
9124 /* We already know the marker exists, otherwise, we
9125 wouldn't see a sal for it. */
9126 char *p = &addr_string[3];
9127 char *endp;
9128 char *marker_str;
0fb4aa4b 9129
e9cafbcc 9130 p = skip_spaces (p);
0fb4aa4b 9131
e9cafbcc 9132 endp = skip_to_space (p);
0fb4aa4b
PA
9133
9134 marker_str = savestring (p, endp - p);
d9b3f62e 9135 t->static_trace_marker_id = marker_str;
0fb4aa4b 9136
3e43a32a
MS
9137 printf_filtered (_("Probed static tracepoint "
9138 "marker \"%s\"\n"),
d9b3f62e 9139 t->static_trace_marker_id);
0fb4aa4b
PA
9140 }
9141 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9142 {
d9b3f62e 9143 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9144 release_static_tracepoint_marker (&marker);
9145
3e43a32a
MS
9146 printf_filtered (_("Probed static tracepoint "
9147 "marker \"%s\"\n"),
d9b3f62e 9148 t->static_trace_marker_id);
0fb4aa4b
PA
9149 }
9150 else
3e43a32a
MS
9151 warning (_("Couldn't determine the static "
9152 "tracepoint marker to probe"));
0fb4aa4b
PA
9153 }
9154
0d381245
VP
9155 loc = b->loc;
9156 }
9157 else
018d34a4 9158 {
39d61571 9159 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9160 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9161 loc->inserted = 1;
0d381245
VP
9162 }
9163
9164 if (b->cond_string)
9165 {
bbc13ae3
KS
9166 const char *arg = b->cond_string;
9167
1bb9788d
TT
9168 loc->cond = parse_exp_1 (&arg, loc->address,
9169 block_for_pc (loc->address), 0);
0d381245 9170 if (*arg)
588ae58c 9171 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9172 }
e7e0cddf
SS
9173
9174 /* Dynamic printf requires and uses additional arguments on the
9175 command line, otherwise it's an error. */
9176 if (type == bp_dprintf)
9177 {
9178 if (b->extra_string)
9179 update_dprintf_command_list (b);
9180 else
9181 error (_("Format string required"));
9182 }
9183 else if (b->extra_string)
588ae58c 9184 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9185 }
018d34a4 9186
56435ebe 9187 b->display_canonical = display_canonical;
018d34a4
VP
9188 if (addr_string)
9189 b->addr_string = addr_string;
9190 else
9191 /* addr_string has to be used or breakpoint_re_set will delete
9192 me. */
5af949e3
UW
9193 b->addr_string
9194 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9195 b->filter = filter;
d9b3f62e 9196}
018d34a4 9197
d9b3f62e
PA
9198static void
9199create_breakpoint_sal (struct gdbarch *gdbarch,
9200 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9201 char *filter, char *cond_string,
e7e0cddf 9202 char *extra_string,
d9b3f62e
PA
9203 enum bptype type, enum bpdisp disposition,
9204 int thread, int task, int ignore_count,
c0a91b2b 9205 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9206 int enabled, int internal, unsigned flags,
9207 int display_canonical)
d9b3f62e
PA
9208{
9209 struct breakpoint *b;
9210 struct cleanup *old_chain;
9211
9212 if (is_tracepoint_type (type))
9213 {
9214 struct tracepoint *t;
9215
9216 t = XCNEW (struct tracepoint);
9217 b = &t->base;
9218 }
9219 else
9220 b = XNEW (struct breakpoint);
9221
9222 old_chain = make_cleanup (xfree, b);
9223
9224 init_breakpoint_sal (b, gdbarch,
9225 sals, addr_string,
e7e0cddf 9226 filter, cond_string, extra_string,
d9b3f62e
PA
9227 type, disposition,
9228 thread, task, ignore_count,
9229 ops, from_tty,
44f238bb
PA
9230 enabled, internal, flags,
9231 display_canonical);
d9b3f62e
PA
9232 discard_cleanups (old_chain);
9233
3ea46bff 9234 install_breakpoint (internal, b, 0);
018d34a4
VP
9235}
9236
9237/* Add SALS.nelts breakpoints to the breakpoint table. For each
9238 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9239 value. COND_STRING, if not NULL, specified the condition to be
9240 used for all breakpoints. Essentially the only case where
9241 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9242 function. In that case, it's still not possible to specify
9243 separate conditions for different overloaded functions, so
9244 we take just a single condition string.
9245
c3f6f71d 9246 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9247 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9248 array contents). If the function fails (error() is called), the
9249 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9250 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9251
9252static void
8cdf0e15 9253create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9254 struct linespec_result *canonical,
e7e0cddf 9255 char *cond_string, char *extra_string,
8cdf0e15
VP
9256 enum bptype type, enum bpdisp disposition,
9257 int thread, int task, int ignore_count,
c0a91b2b 9258 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9259 int enabled, int internal, unsigned flags)
c906108c 9260{
018d34a4 9261 int i;
f8eba3c6 9262 struct linespec_sals *lsal;
cc59ec59 9263
f8eba3c6
TT
9264 if (canonical->pre_expanded)
9265 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9266
9267 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9268 {
f8eba3c6
TT
9269 /* Note that 'addr_string' can be NULL in the case of a plain
9270 'break', without arguments. */
9271 char *addr_string = (canonical->addr_string
9272 ? xstrdup (canonical->addr_string)
9273 : NULL);
9274 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9275 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9276
f8eba3c6
TT
9277 make_cleanup (xfree, filter_string);
9278 create_breakpoint_sal (gdbarch, lsal->sals,
9279 addr_string,
9280 filter_string,
e7e0cddf
SS
9281 cond_string, extra_string,
9282 type, disposition,
84f4c1fe 9283 thread, task, ignore_count, ops,
44f238bb 9284 from_tty, enabled, internal, flags,
56435ebe 9285 canonical->special_display);
f8eba3c6 9286 discard_cleanups (inner);
c3f6f71d 9287 }
c3f6f71d 9288}
c906108c 9289
9998af43 9290/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9291 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9292 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9293 address strings. ADDRESS points to the end of the SAL.
9294
9295 The array and the line spec strings are allocated on the heap, it is
9296 the caller's responsibility to free them. */
c906108c 9297
b9362cc7 9298static void
c3f6f71d 9299parse_breakpoint_sals (char **address,
58438ac1 9300 struct linespec_result *canonical)
c3f6f71d 9301{
c3f6f71d 9302 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9303 breakpoint. */
0578b14e 9304 if ((*address) == NULL || linespec_lexer_lex_keyword (*address))
c906108c 9305 {
1bfeeb0f
JL
9306 /* The last displayed codepoint, if it's valid, is our default breakpoint
9307 address. */
9308 if (last_displayed_sal_is_valid ())
c906108c 9309 {
f8eba3c6 9310 struct linespec_sals lsal;
c3f6f71d 9311 struct symtab_and_line sal;
1c8cdcb1 9312 CORE_ADDR pc;
cc59ec59 9313
4a64f543 9314 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9315 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9316 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9317
9318 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9319 corresponding to the last call to print_frame_info.
9320 Be sure to reinitialize LINE with NOTCURRENT == 0
9321 as the breakpoint line number is inappropriate otherwise.
9322 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9323 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9324 pc = sal.pc;
9325 sal = find_pc_line (pc, 0);
00903456 9326
4a64f543 9327 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9328 where PC is the last displayed codepoint's address. So
9329 make sure to set sal.explicit_pc to prevent GDB from
9330 trying to expand the list of sals to include all other
9331 instances with the same symtab and line. */
1c8cdcb1 9332 sal.pc = pc;
00903456
JK
9333 sal.explicit_pc = 1;
9334
f8eba3c6
TT
9335 lsal.sals.sals[0] = sal;
9336 lsal.sals.nelts = 1;
9337 lsal.canonical = NULL;
9338
9339 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9340 }
9341 else
8a3fe4f8 9342 error (_("No default breakpoint address now."));
c906108c
SS
9343 }
9344 else
9345 {
cc80f267
JK
9346 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9347
c906108c 9348 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9349 current_source_symtab (which is decode_line_1's default).
9350 This should produce the results we want almost all of the
cc80f267
JK
9351 time while leaving default_breakpoint_* alone.
9352
9353 ObjC: However, don't match an Objective-C method name which
9354 may have a '+' or '-' succeeded by a '['. */
9355 if (last_displayed_sal_is_valid ()
9356 && (!cursal.symtab
9357 || ((strchr ("+-", (*address)[0]) != NULL)
9358 && ((*address)[1] != '['))))
f8eba3c6
TT
9359 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9360 get_last_displayed_symtab (),
9361 get_last_displayed_line (),
9362 canonical, NULL, NULL);
c906108c 9363 else
f8eba3c6 9364 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9365 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9366 }
c3f6f71d 9367}
c906108c 9368
c906108c 9369
c3f6f71d 9370/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9371 inserted as a breakpoint. If it can't throw an error. */
c906108c 9372
b9362cc7 9373static void
23e7acfb 9374breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9375{
9376 int i;
cc59ec59 9377
c3f6f71d 9378 for (i = 0; i < sals->nelts; i++)
ee53e872 9379 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9380}
9381
7a697b8d
SS
9382/* Fast tracepoints may have restrictions on valid locations. For
9383 instance, a fast tracepoint using a jump instead of a trap will
9384 likely have to overwrite more bytes than a trap would, and so can
9385 only be placed where the instruction is longer than the jump, or a
9386 multi-instruction sequence does not have a jump into the middle of
9387 it, etc. */
9388
9389static void
9390check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9391 struct symtabs_and_lines *sals)
9392{
9393 int i, rslt;
9394 struct symtab_and_line *sal;
9395 char *msg;
9396 struct cleanup *old_chain;
9397
9398 for (i = 0; i < sals->nelts; i++)
9399 {
f8eba3c6
TT
9400 struct gdbarch *sarch;
9401
7a697b8d
SS
9402 sal = &sals->sals[i];
9403
f8eba3c6
TT
9404 sarch = get_sal_arch (*sal);
9405 /* We fall back to GDBARCH if there is no architecture
9406 associated with SAL. */
9407 if (sarch == NULL)
9408 sarch = gdbarch;
9409 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9410 NULL, &msg);
9411 old_chain = make_cleanup (xfree, msg);
9412
9413 if (!rslt)
9414 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9415 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9416
9417 do_cleanups (old_chain);
9418 }
9419}
9420
af4908ba
KS
9421/* Issue an invalid thread ID error. */
9422
9423static void ATTRIBUTE_NORETURN
9424invalid_thread_id_error (int id)
9425{
9426 error (_("Unknown thread %d."), id);
9427}
9428
018d34a4
VP
9429/* Given TOK, a string specification of condition and thread, as
9430 accepted by the 'break' command, extract the condition
9431 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9432 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9433 If no condition is found, *COND_STRING is set to NULL.
9434 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9435
9436static void
bbc13ae3 9437find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9438 char **cond_string, int *thread, int *task,
9439 char **rest)
018d34a4
VP
9440{
9441 *cond_string = NULL;
9442 *thread = -1;
ed1d1739
KS
9443 *task = 0;
9444 *rest = NULL;
9445
018d34a4
VP
9446 while (tok && *tok)
9447 {
bbc13ae3 9448 const char *end_tok;
018d34a4 9449 int toklen;
bbc13ae3
KS
9450 const char *cond_start = NULL;
9451 const char *cond_end = NULL;
cc59ec59 9452
bbc13ae3 9453 tok = skip_spaces_const (tok);
e7e0cddf
SS
9454
9455 if ((*tok == '"' || *tok == ',') && rest)
9456 {
9457 *rest = savestring (tok, strlen (tok));
9458 return;
9459 }
9460
bbc13ae3 9461 end_tok = skip_to_space_const (tok);
d634f2de 9462
018d34a4 9463 toklen = end_tok - tok;
d634f2de 9464
018d34a4
VP
9465 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9466 {
f7545552
TT
9467 struct expression *expr;
9468
018d34a4 9469 tok = cond_start = end_tok + 1;
1bb9788d 9470 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9471 xfree (expr);
018d34a4 9472 cond_end = tok;
d634f2de 9473 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9474 }
9475 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9476 {
9477 char *tmptok;
d634f2de 9478
018d34a4 9479 tok = end_tok + 1;
bbc13ae3 9480 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9481 if (tok == tmptok)
9482 error (_("Junk after thread keyword."));
9483 if (!valid_thread_id (*thread))
af4908ba 9484 invalid_thread_id_error (*thread);
bbc13ae3 9485 tok = tmptok;
018d34a4 9486 }
4a306c9a
JB
9487 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9488 {
9489 char *tmptok;
9490
9491 tok = end_tok + 1;
bbc13ae3 9492 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9493 if (tok == tmptok)
9494 error (_("Junk after task keyword."));
9495 if (!valid_task_id (*task))
b6199126 9496 error (_("Unknown task %d."), *task);
bbc13ae3 9497 tok = tmptok;
4a306c9a 9498 }
e7e0cddf
SS
9499 else if (rest)
9500 {
9501 *rest = savestring (tok, strlen (tok));
ccab2054 9502 return;
e7e0cddf 9503 }
018d34a4
VP
9504 else
9505 error (_("Junk at end of arguments."));
9506 }
9507}
9508
0fb4aa4b
PA
9509/* Decode a static tracepoint marker spec. */
9510
9511static struct symtabs_and_lines
9512decode_static_tracepoint_spec (char **arg_p)
9513{
9514 VEC(static_tracepoint_marker_p) *markers = NULL;
9515 struct symtabs_and_lines sals;
0fb4aa4b
PA
9516 struct cleanup *old_chain;
9517 char *p = &(*arg_p)[3];
9518 char *endp;
9519 char *marker_str;
9520 int i;
9521
e9cafbcc 9522 p = skip_spaces (p);
0fb4aa4b 9523
e9cafbcc 9524 endp = skip_to_space (p);
0fb4aa4b
PA
9525
9526 marker_str = savestring (p, endp - p);
9527 old_chain = make_cleanup (xfree, marker_str);
9528
9529 markers = target_static_tracepoint_markers_by_strid (marker_str);
9530 if (VEC_empty(static_tracepoint_marker_p, markers))
9531 error (_("No known static tracepoint marker named %s"), marker_str);
9532
9533 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9534 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9535
9536 for (i = 0; i < sals.nelts; i++)
9537 {
9538 struct static_tracepoint_marker *marker;
9539
9540 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9541
9542 init_sal (&sals.sals[i]);
9543
9544 sals.sals[i] = find_pc_line (marker->address, 0);
9545 sals.sals[i].pc = marker->address;
9546
9547 release_static_tracepoint_marker (marker);
9548 }
9549
9550 do_cleanups (old_chain);
9551
9552 *arg_p = endp;
9553 return sals;
9554}
9555
fd9b8c24
PA
9556/* Set a breakpoint. This function is shared between CLI and MI
9557 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9558 modes of operations, selected by the PARSE_ARG parameter. If
9559 non-zero, the function will parse ARG, extracting location,
9560 condition, thread and extra string. Otherwise, ARG is just the
9561 breakpoint's location, with condition, thread, and extra string
9562 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9563 If INTERNAL is non-zero, the breakpoint number will be allocated
9564 from the internal breakpoint count. Returns true if any breakpoint
9565 was created; false otherwise. */
0101ce28 9566
8cdf0e15
VP
9567int
9568create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9569 char *arg, char *cond_string,
9570 int thread, char *extra_string,
f6de8ec2 9571 int parse_arg,
0fb4aa4b 9572 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9573 int ignore_count,
9574 enum auto_boolean pending_break_support,
c0a91b2b 9575 const struct breakpoint_ops *ops,
44f238bb
PA
9576 int from_tty, int enabled, int internal,
9577 unsigned flags)
c3f6f71d 9578{
f8eba3c6 9579 char *copy_arg = NULL;
c3f6f71d 9580 char *addr_start = arg;
7efd8fc2 9581 struct linespec_result canonical;
c3f6f71d 9582 struct cleanup *old_chain;
80c99de1 9583 struct cleanup *bkpt_chain = NULL;
0101ce28 9584 int pending = 0;
4a306c9a 9585 int task = 0;
86b17b60 9586 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9587
348d480f
PA
9588 gdb_assert (ops != NULL);
9589
7efd8fc2 9590 init_linespec_result (&canonical);
c3f6f71d 9591
492d29ea 9592 TRY
b78a6381 9593 {
983af33b
SDJ
9594 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9595 addr_start, &copy_arg);
b78a6381 9596 }
492d29ea 9597 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9598 {
492d29ea
PA
9599 /* If caller is interested in rc value from parse, set
9600 value. */
9601 if (e.error == NOT_FOUND_ERROR)
0101ce28 9602 {
05ff989b
AC
9603 /* If pending breakpoint support is turned off, throw
9604 error. */
fa8d40ab
JJ
9605
9606 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9607 throw_exception (e);
9608
9609 exception_print (gdb_stderr, e);
fa8d40ab 9610
05ff989b
AC
9611 /* If pending breakpoint support is auto query and the user
9612 selects no, then simply return the error code. */
059fb39f 9613 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9614 && !nquery (_("Make %s pending on future shared library load? "),
9615 bptype_string (type_wanted)))
fd9b8c24 9616 return 0;
fa8d40ab 9617
05ff989b
AC
9618 /* At this point, either the user was queried about setting
9619 a pending breakpoint and selected yes, or pending
9620 breakpoint behavior is on and thus a pending breakpoint
9621 is defaulted on behalf of the user. */
f8eba3c6
TT
9622 {
9623 struct linespec_sals lsal;
9624
9625 copy_arg = xstrdup (addr_start);
9626 lsal.canonical = xstrdup (copy_arg);
9627 lsal.sals.nelts = 1;
9628 lsal.sals.sals = XNEW (struct symtab_and_line);
9629 init_sal (&lsal.sals.sals[0]);
9630 pending = 1;
9631 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9632 }
0101ce28 9633 }
492d29ea
PA
9634 else
9635 throw_exception (e);
0101ce28 9636 }
492d29ea
PA
9637 END_CATCH
9638
9639 if (VEC_empty (linespec_sals, canonical.sals))
9640 return 0;
c3f6f71d 9641
4a64f543 9642 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9643 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9644
c3f6f71d
JM
9645 /* ----------------------------- SNIP -----------------------------
9646 Anything added to the cleanup chain beyond this point is assumed
9647 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9648 then the memory is not reclaimed. */
9649 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9650
c3f6f71d
JM
9651 /* Resolve all line numbers to PC's and verify that the addresses
9652 are ok for the target. */
0101ce28 9653 if (!pending)
f8eba3c6
TT
9654 {
9655 int ix;
9656 struct linespec_sals *iter;
9657
9658 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9659 breakpoint_sals_to_pc (&iter->sals);
9660 }
c3f6f71d 9661
7a697b8d 9662 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9663 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9664 {
9665 int ix;
9666 struct linespec_sals *iter;
9667
9668 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9669 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9670 }
7a697b8d 9671
c3f6f71d
JM
9672 /* Verify that condition can be parsed, before setting any
9673 breakpoints. Allocate a separate condition expression for each
4a64f543 9674 breakpoint. */
0101ce28 9675 if (!pending)
c3f6f71d 9676 {
f6de8ec2 9677 if (parse_arg)
72b2ff0e 9678 {
0878d0fa 9679 char *rest;
52d361e1
YQ
9680 struct linespec_sals *lsal;
9681
9682 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9683
0878d0fa
YQ
9684 /* Here we only parse 'arg' to separate condition
9685 from thread number, so parsing in context of first
9686 sal is OK. When setting the breakpoint we'll
9687 re-parse it in context of each sal. */
9688
9689 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9690 &thread, &task, &rest);
9691 if (cond_string)
9692 make_cleanup (xfree, cond_string);
9693 if (rest)
9694 make_cleanup (xfree, rest);
9695 if (rest)
9696 extra_string = rest;
72b2ff0e 9697 }
2f069f6f 9698 else
72b2ff0e 9699 {
0878d0fa
YQ
9700 if (*arg != '\0')
9701 error (_("Garbage '%s' at end of location"), arg);
9702
9703 /* Create a private copy of condition string. */
9704 if (cond_string)
9705 {
9706 cond_string = xstrdup (cond_string);
9707 make_cleanup (xfree, cond_string);
9708 }
9709 /* Create a private copy of any extra string. */
9710 if (extra_string)
9711 {
9712 extra_string = xstrdup (extra_string);
9713 make_cleanup (xfree, extra_string);
9714 }
72b2ff0e 9715 }
0fb4aa4b 9716
52d361e1 9717 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9718 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9719 tempflag ? disp_del : disp_donttouch,
9720 thread, task, ignore_count, ops,
44f238bb 9721 from_tty, enabled, internal, flags);
c906108c 9722 }
0101ce28
JJ
9723 else
9724 {
0101ce28
JJ
9725 struct breakpoint *b;
9726
0101ce28
JJ
9727 make_cleanup (xfree, copy_arg);
9728
bfccc43c
YQ
9729 if (is_tracepoint_type (type_wanted))
9730 {
9731 struct tracepoint *t;
9732
9733 t = XCNEW (struct tracepoint);
9734 b = &t->base;
9735 }
9736 else
9737 b = XNEW (struct breakpoint);
9738
9739 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9740
f8eba3c6 9741 b->addr_string = copy_arg;
f6de8ec2 9742 if (parse_arg)
98aa42ee
AT
9743 {
9744 b->cond_string = NULL;
9745 b->extra_string = NULL;
9746 }
e12c7713
MK
9747 else
9748 {
9749 /* Create a private copy of condition string. */
9750 if (cond_string)
9751 {
9752 cond_string = xstrdup (cond_string);
9753 make_cleanup (xfree, cond_string);
9754 }
98aa42ee
AT
9755 /* Create a private copy of any extra string. */
9756 if (extra_string != NULL)
9757 {
9758 extra_string = xstrdup (extra_string);
9759 make_cleanup (xfree, extra_string);
9760 }
e12c7713 9761 b->cond_string = cond_string;
98aa42ee 9762 b->extra_string = extra_string;
15630549 9763 b->thread = thread;
e12c7713 9764 }
0101ce28 9765 b->ignore_count = ignore_count;
0101ce28 9766 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9767 b->condition_not_parsed = 1;
41447f92 9768 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9769 if ((type_wanted != bp_breakpoint
9770 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9771 b->pspace = current_program_space;
8bea4e01 9772
bfccc43c 9773 install_breakpoint (internal, b, 0);
0101ce28
JJ
9774 }
9775
f8eba3c6 9776 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9777 {
3e43a32a
MS
9778 warning (_("Multiple breakpoints were set.\nUse the "
9779 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9780 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9781 }
9782
80c99de1
PA
9783 /* That's it. Discard the cleanups for data inserted into the
9784 breakpoint. */
9785 discard_cleanups (bkpt_chain);
9786 /* But cleanup everything else. */
c3f6f71d 9787 do_cleanups (old_chain);
217dc9e2 9788
80c99de1 9789 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9790 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9791
9792 return 1;
c3f6f71d 9793}
c906108c 9794
348d480f 9795/* Set a breakpoint.
72b2ff0e
VP
9796 ARG is a string describing breakpoint address,
9797 condition, and thread.
9798 FLAG specifies if a breakpoint is hardware on,
9799 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9800 and BP_TEMPFLAG. */
348d480f 9801
98deb0da 9802static void
72b2ff0e 9803break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9804{
72b2ff0e 9805 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9806 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9807 ? bp_hardware_breakpoint
9808 : bp_breakpoint);
55aa24fb
SDJ
9809 struct breakpoint_ops *ops;
9810 const char *arg_cp = arg;
9811
9812 /* Matching breakpoints on probes. */
9813 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9814 ops = &bkpt_probe_breakpoint_ops;
9815 else
9816 ops = &bkpt_breakpoint_ops;
c3f6f71d 9817
8cdf0e15
VP
9818 create_breakpoint (get_current_arch (),
9819 arg,
e7e0cddf 9820 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 9821 tempflag, type_wanted,
8cdf0e15
VP
9822 0 /* Ignore count */,
9823 pending_break_support,
55aa24fb 9824 ops,
8cdf0e15 9825 from_tty,
84f4c1fe 9826 1 /* enabled */,
44f238bb
PA
9827 0 /* internal */,
9828 0);
c906108c
SS
9829}
9830
c906108c
SS
9831/* Helper function for break_command_1 and disassemble_command. */
9832
9833void
fba45db2 9834resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9835{
9836 CORE_ADDR pc;
9837
9838 if (sal->pc == 0 && sal->symtab != NULL)
9839 {
9840 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9841 error (_("No line %d in file \"%s\"."),
05cba821 9842 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9843 sal->pc = pc;
6a048695 9844
4a64f543
MS
9845 /* If this SAL corresponds to a breakpoint inserted using a line
9846 number, then skip the function prologue if necessary. */
6a048695 9847 if (sal->explicit_line)
059acae7 9848 skip_prologue_sal (sal);
c906108c
SS
9849 }
9850
9851 if (sal->section == 0 && sal->symtab != NULL)
9852 {
346d1dfe 9853 const struct blockvector *bv;
3977b71f 9854 const struct block *b;
c5aa993b 9855 struct symbol *sym;
c906108c 9856
43f3e411
DE
9857 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9858 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9859 if (bv != NULL)
9860 {
7f0df278 9861 sym = block_linkage_function (b);
c906108c
SS
9862 if (sym != NULL)
9863 {
eb822aa6
DE
9864 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9865 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9866 sym);
c906108c
SS
9867 }
9868 else
9869 {
4a64f543
MS
9870 /* It really is worthwhile to have the section, so we'll
9871 just have to look harder. This case can be executed
9872 if we have line numbers but no functions (as can
9873 happen in assembly source). */
c906108c 9874
7cbd4a93 9875 struct bound_minimal_symbol msym;
6c95b8df
PA
9876 struct cleanup *old_chain = save_current_space_and_thread ();
9877
9878 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
9879
9880 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9881 if (msym.minsym)
efd66ac6 9882 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
9883
9884 do_cleanups (old_chain);
c906108c
SS
9885 }
9886 }
9887 }
9888}
9889
9890void
fba45db2 9891break_command (char *arg, int from_tty)
c906108c 9892{
db107f19 9893 break_command_1 (arg, 0, from_tty);
c906108c
SS
9894}
9895
c906108c 9896void
fba45db2 9897tbreak_command (char *arg, int from_tty)
c906108c 9898{
db107f19 9899 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9900}
9901
c906108c 9902static void
fba45db2 9903hbreak_command (char *arg, int from_tty)
c906108c 9904{
db107f19 9905 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9906}
9907
9908static void
fba45db2 9909thbreak_command (char *arg, int from_tty)
c906108c 9910{
db107f19 9911 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9912}
9913
9914static void
fba45db2 9915stop_command (char *arg, int from_tty)
c906108c 9916{
a3f17187 9917 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9918Usage: stop in <function | address>\n\
a3f17187 9919 stop at <line>\n"));
c906108c
SS
9920}
9921
9922static void
fba45db2 9923stopin_command (char *arg, int from_tty)
c906108c
SS
9924{
9925 int badInput = 0;
9926
c5aa993b 9927 if (arg == (char *) NULL)
c906108c
SS
9928 badInput = 1;
9929 else if (*arg != '*')
9930 {
9931 char *argptr = arg;
9932 int hasColon = 0;
9933
4a64f543 9934 /* Look for a ':'. If this is a line number specification, then
53a5351d 9935 say it is bad, otherwise, it should be an address or
4a64f543 9936 function/method name. */
c906108c 9937 while (*argptr && !hasColon)
c5aa993b
JM
9938 {
9939 hasColon = (*argptr == ':');
9940 argptr++;
9941 }
c906108c
SS
9942
9943 if (hasColon)
c5aa993b 9944 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9945 else
c5aa993b 9946 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9947 }
9948
9949 if (badInput)
a3f17187 9950 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9951 else
db107f19 9952 break_command_1 (arg, 0, from_tty);
c906108c
SS
9953}
9954
9955static void
fba45db2 9956stopat_command (char *arg, int from_tty)
c906108c
SS
9957{
9958 int badInput = 0;
9959
c5aa993b 9960 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9961 badInput = 1;
9962 else
9963 {
9964 char *argptr = arg;
9965 int hasColon = 0;
9966
4a64f543
MS
9967 /* Look for a ':'. If there is a '::' then get out, otherwise
9968 it is probably a line number. */
c906108c 9969 while (*argptr && !hasColon)
c5aa993b
JM
9970 {
9971 hasColon = (*argptr == ':');
9972 argptr++;
9973 }
c906108c
SS
9974
9975 if (hasColon)
c5aa993b 9976 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9977 else
c5aa993b 9978 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9979 }
9980
9981 if (badInput)
a3f17187 9982 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 9983 else
db107f19 9984 break_command_1 (arg, 0, from_tty);
c906108c
SS
9985}
9986
e7e0cddf
SS
9987/* The dynamic printf command is mostly like a regular breakpoint, but
9988 with a prewired command list consisting of a single output command,
9989 built from extra arguments supplied on the dprintf command
9990 line. */
9991
da821c7b 9992static void
e7e0cddf
SS
9993dprintf_command (char *arg, int from_tty)
9994{
9995 create_breakpoint (get_current_arch (),
9996 arg,
9997 NULL, 0, NULL, 1 /* parse arg */,
9998 0, bp_dprintf,
9999 0 /* Ignore count */,
10000 pending_break_support,
10001 &dprintf_breakpoint_ops,
10002 from_tty,
10003 1 /* enabled */,
10004 0 /* internal */,
10005 0);
10006}
10007
d3ce09f5
SS
10008static void
10009agent_printf_command (char *arg, int from_tty)
10010{
10011 error (_("May only run agent-printf on the target"));
10012}
10013
f1310107
TJB
10014/* Implement the "breakpoint_hit" breakpoint_ops method for
10015 ranged breakpoints. */
10016
10017static int
10018breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10019 struct address_space *aspace,
09ac7c10
TT
10020 CORE_ADDR bp_addr,
10021 const struct target_waitstatus *ws)
f1310107 10022{
09ac7c10 10023 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10024 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10025 return 0;
10026
f1310107
TJB
10027 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10028 bl->length, aspace, bp_addr);
10029}
10030
10031/* Implement the "resources_needed" breakpoint_ops method for
10032 ranged breakpoints. */
10033
10034static int
10035resources_needed_ranged_breakpoint (const struct bp_location *bl)
10036{
10037 return target_ranged_break_num_registers ();
10038}
10039
10040/* Implement the "print_it" breakpoint_ops method for
10041 ranged breakpoints. */
10042
10043static enum print_stop_action
348d480f 10044print_it_ranged_breakpoint (bpstat bs)
f1310107 10045{
348d480f 10046 struct breakpoint *b = bs->breakpoint_at;
f1310107 10047 struct bp_location *bl = b->loc;
79a45e25 10048 struct ui_out *uiout = current_uiout;
f1310107
TJB
10049
10050 gdb_assert (b->type == bp_hardware_breakpoint);
10051
10052 /* Ranged breakpoints have only one location. */
10053 gdb_assert (bl && bl->next == NULL);
10054
10055 annotate_breakpoint (b->number);
10056 if (b->disposition == disp_del)
10057 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10058 else
10059 ui_out_text (uiout, "\nRanged breakpoint ");
10060 if (ui_out_is_mi_like_p (uiout))
10061 {
10062 ui_out_field_string (uiout, "reason",
10063 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10064 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10065 }
10066 ui_out_field_int (uiout, "bkptno", b->number);
10067 ui_out_text (uiout, ", ");
10068
10069 return PRINT_SRC_AND_LOC;
10070}
10071
10072/* Implement the "print_one" breakpoint_ops method for
10073 ranged breakpoints. */
10074
10075static void
10076print_one_ranged_breakpoint (struct breakpoint *b,
10077 struct bp_location **last_loc)
10078{
10079 struct bp_location *bl = b->loc;
10080 struct value_print_options opts;
79a45e25 10081 struct ui_out *uiout = current_uiout;
f1310107
TJB
10082
10083 /* Ranged breakpoints have only one location. */
10084 gdb_assert (bl && bl->next == NULL);
10085
10086 get_user_print_options (&opts);
10087
10088 if (opts.addressprint)
10089 /* We don't print the address range here, it will be printed later
10090 by print_one_detail_ranged_breakpoint. */
10091 ui_out_field_skip (uiout, "addr");
10092 annotate_field (5);
10093 print_breakpoint_location (b, bl);
10094 *last_loc = bl;
10095}
10096
10097/* Implement the "print_one_detail" breakpoint_ops method for
10098 ranged breakpoints. */
10099
10100static void
10101print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10102 struct ui_out *uiout)
10103{
10104 CORE_ADDR address_start, address_end;
10105 struct bp_location *bl = b->loc;
f99d8bf4
PA
10106 struct ui_file *stb = mem_fileopen ();
10107 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10108
10109 gdb_assert (bl);
10110
10111 address_start = bl->address;
10112 address_end = address_start + bl->length - 1;
10113
10114 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10115 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10116 print_core_address (bl->gdbarch, address_start),
10117 print_core_address (bl->gdbarch, address_end));
10118 ui_out_field_stream (uiout, "addr", stb);
10119 ui_out_text (uiout, "\n");
10120
10121 do_cleanups (cleanup);
10122}
10123
10124/* Implement the "print_mention" breakpoint_ops method for
10125 ranged breakpoints. */
10126
10127static void
10128print_mention_ranged_breakpoint (struct breakpoint *b)
10129{
10130 struct bp_location *bl = b->loc;
79a45e25 10131 struct ui_out *uiout = current_uiout;
f1310107
TJB
10132
10133 gdb_assert (bl);
10134 gdb_assert (b->type == bp_hardware_breakpoint);
10135
10136 if (ui_out_is_mi_like_p (uiout))
10137 return;
10138
10139 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10140 b->number, paddress (bl->gdbarch, bl->address),
10141 paddress (bl->gdbarch, bl->address + bl->length - 1));
10142}
10143
10144/* Implement the "print_recreate" breakpoint_ops method for
10145 ranged breakpoints. */
10146
10147static void
10148print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10149{
10150 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10151 b->addr_string_range_end);
d9b3f62e 10152 print_recreate_thread (b, fp);
f1310107
TJB
10153}
10154
10155/* The breakpoint_ops structure to be used in ranged breakpoints. */
10156
2060206e 10157static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10158
10159/* Find the address where the end of the breakpoint range should be
10160 placed, given the SAL of the end of the range. This is so that if
10161 the user provides a line number, the end of the range is set to the
10162 last instruction of the given line. */
10163
10164static CORE_ADDR
10165find_breakpoint_range_end (struct symtab_and_line sal)
10166{
10167 CORE_ADDR end;
10168
10169 /* If the user provided a PC value, use it. Otherwise,
10170 find the address of the end of the given location. */
10171 if (sal.explicit_pc)
10172 end = sal.pc;
10173 else
10174 {
10175 int ret;
10176 CORE_ADDR start;
10177
10178 ret = find_line_pc_range (sal, &start, &end);
10179 if (!ret)
10180 error (_("Could not find location of the end of the range."));
10181
10182 /* find_line_pc_range returns the start of the next line. */
10183 end--;
10184 }
10185
10186 return end;
10187}
10188
10189/* Implement the "break-range" CLI command. */
10190
10191static void
10192break_range_command (char *arg, int from_tty)
10193{
10194 char *arg_start, *addr_string_start, *addr_string_end;
10195 struct linespec_result canonical_start, canonical_end;
10196 int bp_count, can_use_bp, length;
10197 CORE_ADDR end;
10198 struct breakpoint *b;
10199 struct symtab_and_line sal_start, sal_end;
f1310107 10200 struct cleanup *cleanup_bkpt;
f8eba3c6 10201 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10202
10203 /* We don't support software ranged breakpoints. */
10204 if (target_ranged_break_num_registers () < 0)
10205 error (_("This target does not support hardware ranged breakpoints."));
10206
10207 bp_count = hw_breakpoint_used_count ();
10208 bp_count += target_ranged_break_num_registers ();
10209 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10210 bp_count, 0);
10211 if (can_use_bp < 0)
10212 error (_("Hardware breakpoints used exceeds limit."));
10213
f8eba3c6 10214 arg = skip_spaces (arg);
f1310107
TJB
10215 if (arg == NULL || arg[0] == '\0')
10216 error(_("No address range specified."));
10217
f1310107
TJB
10218 init_linespec_result (&canonical_start);
10219
f8eba3c6
TT
10220 arg_start = arg;
10221 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10222
f8eba3c6 10223 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10224
10225 if (arg[0] != ',')
10226 error (_("Too few arguments."));
f8eba3c6 10227 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10228 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10229
10230 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10231
10232 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10233 || lsal_start->sals.nelts != 1)
f1310107
TJB
10234 error (_("Cannot create a ranged breakpoint with multiple locations."));
10235
f8eba3c6
TT
10236 sal_start = lsal_start->sals.sals[0];
10237 addr_string_start = savestring (arg_start, arg - arg_start);
10238 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10239
10240 arg++; /* Skip the comma. */
f8eba3c6 10241 arg = skip_spaces (arg);
f1310107
TJB
10242
10243 /* Parse the end location. */
10244
f1310107
TJB
10245 init_linespec_result (&canonical_end);
10246 arg_start = arg;
10247
f8eba3c6 10248 /* We call decode_line_full directly here instead of using
f1310107
TJB
10249 parse_breakpoint_sals because we need to specify the start location's
10250 symtab and line as the default symtab and line for the end of the
10251 range. This makes it possible to have ranges like "foo.c:27, +14",
10252 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10253 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10254 sal_start.symtab, sal_start.line,
10255 &canonical_end, NULL, NULL);
10256
10257 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10258
f8eba3c6 10259 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10260 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10261
10262 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10263 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10264 || lsal_end->sals.nelts != 1)
f1310107
TJB
10265 error (_("Cannot create a ranged breakpoint with multiple locations."));
10266
f8eba3c6
TT
10267 sal_end = lsal_end->sals.sals[0];
10268 addr_string_end = savestring (arg_start, arg - arg_start);
10269 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10270
10271 end = find_breakpoint_range_end (sal_end);
10272 if (sal_start.pc > end)
177b42fe 10273 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10274
10275 length = end - sal_start.pc + 1;
10276 if (length < 0)
10277 /* Length overflowed. */
10278 error (_("Address range too large."));
10279 else if (length == 1)
10280 {
10281 /* This range is simple enough to be handled by
10282 the `hbreak' command. */
10283 hbreak_command (addr_string_start, 1);
10284
10285 do_cleanups (cleanup_bkpt);
10286
10287 return;
10288 }
10289
10290 /* Now set up the breakpoint. */
10291 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10292 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10293 set_breakpoint_count (breakpoint_count + 1);
10294 b->number = breakpoint_count;
10295 b->disposition = disp_donttouch;
f8eba3c6
TT
10296 b->addr_string = xstrdup (addr_string_start);
10297 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10298 b->loc->length = length;
10299
f8eba3c6 10300 do_cleanups (cleanup_bkpt);
f1310107
TJB
10301
10302 mention (b);
8d3788bd 10303 observer_notify_breakpoint_created (b);
44702360 10304 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10305}
10306
4a64f543
MS
10307/* Return non-zero if EXP is verified as constant. Returned zero
10308 means EXP is variable. Also the constant detection may fail for
10309 some constant expressions and in such case still falsely return
10310 zero. */
2e6e3d9c 10311
65d79d4b
SDJ
10312static int
10313watchpoint_exp_is_const (const struct expression *exp)
10314{
10315 int i = exp->nelts;
10316
10317 while (i > 0)
10318 {
10319 int oplenp, argsp;
10320
10321 /* We are only interested in the descriptor of each element. */
10322 operator_length (exp, i, &oplenp, &argsp);
10323 i -= oplenp;
10324
10325 switch (exp->elts[i].opcode)
10326 {
10327 case BINOP_ADD:
10328 case BINOP_SUB:
10329 case BINOP_MUL:
10330 case BINOP_DIV:
10331 case BINOP_REM:
10332 case BINOP_MOD:
10333 case BINOP_LSH:
10334 case BINOP_RSH:
10335 case BINOP_LOGICAL_AND:
10336 case BINOP_LOGICAL_OR:
10337 case BINOP_BITWISE_AND:
10338 case BINOP_BITWISE_IOR:
10339 case BINOP_BITWISE_XOR:
10340 case BINOP_EQUAL:
10341 case BINOP_NOTEQUAL:
10342 case BINOP_LESS:
10343 case BINOP_GTR:
10344 case BINOP_LEQ:
10345 case BINOP_GEQ:
10346 case BINOP_REPEAT:
10347 case BINOP_COMMA:
10348 case BINOP_EXP:
10349 case BINOP_MIN:
10350 case BINOP_MAX:
10351 case BINOP_INTDIV:
10352 case BINOP_CONCAT:
65d79d4b
SDJ
10353 case TERNOP_COND:
10354 case TERNOP_SLICE:
65d79d4b
SDJ
10355
10356 case OP_LONG:
10357 case OP_DOUBLE:
10358 case OP_DECFLOAT:
10359 case OP_LAST:
10360 case OP_COMPLEX:
10361 case OP_STRING:
65d79d4b
SDJ
10362 case OP_ARRAY:
10363 case OP_TYPE:
608b4967
TT
10364 case OP_TYPEOF:
10365 case OP_DECLTYPE:
6e72ca20 10366 case OP_TYPEID:
65d79d4b
SDJ
10367 case OP_NAME:
10368 case OP_OBJC_NSSTRING:
10369
10370 case UNOP_NEG:
10371 case UNOP_LOGICAL_NOT:
10372 case UNOP_COMPLEMENT:
10373 case UNOP_ADDR:
10374 case UNOP_HIGH:
aeaa2474 10375 case UNOP_CAST:
9eaf6705
TT
10376
10377 case UNOP_CAST_TYPE:
10378 case UNOP_REINTERPRET_CAST:
10379 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10380 /* Unary, binary and ternary operators: We have to check
10381 their operands. If they are constant, then so is the
10382 result of that operation. For instance, if A and B are
10383 determined to be constants, then so is "A + B".
10384
10385 UNOP_IND is one exception to the rule above, because the
10386 value of *ADDR is not necessarily a constant, even when
10387 ADDR is. */
65d79d4b
SDJ
10388 break;
10389
10390 case OP_VAR_VALUE:
10391 /* Check whether the associated symbol is a constant.
4a64f543 10392
65d79d4b 10393 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10394 possible that a buggy compiler could mark a variable as
10395 constant even when it is not, and TYPE_CONST would return
10396 true in this case, while SYMBOL_CLASS wouldn't.
10397
10398 We also have to check for function symbols because they
10399 are always constant. */
65d79d4b
SDJ
10400 {
10401 struct symbol *s = exp->elts[i + 2].symbol;
10402
10403 if (SYMBOL_CLASS (s) != LOC_BLOCK
10404 && SYMBOL_CLASS (s) != LOC_CONST
10405 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10406 return 0;
10407 break;
10408 }
10409
10410 /* The default action is to return 0 because we are using
10411 the optimistic approach here: If we don't know something,
10412 then it is not a constant. */
10413 default:
10414 return 0;
10415 }
10416 }
10417
10418 return 1;
10419}
10420
3a5c3e22
PA
10421/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10422
10423static void
10424dtor_watchpoint (struct breakpoint *self)
10425{
10426 struct watchpoint *w = (struct watchpoint *) self;
10427
10428 xfree (w->cond_exp);
10429 xfree (w->exp);
10430 xfree (w->exp_string);
10431 xfree (w->exp_string_reparse);
10432 value_free (w->val);
10433
10434 base_breakpoint_ops.dtor (self);
10435}
10436
348d480f
PA
10437/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10438
10439static void
10440re_set_watchpoint (struct breakpoint *b)
10441{
3a5c3e22
PA
10442 struct watchpoint *w = (struct watchpoint *) b;
10443
348d480f
PA
10444 /* Watchpoint can be either on expression using entirely global
10445 variables, or it can be on local variables.
10446
10447 Watchpoints of the first kind are never auto-deleted, and even
10448 persist across program restarts. Since they can use variables
10449 from shared libraries, we need to reparse expression as libraries
10450 are loaded and unloaded.
10451
10452 Watchpoints on local variables can also change meaning as result
10453 of solib event. For example, if a watchpoint uses both a local
10454 and a global variables in expression, it's a local watchpoint,
10455 but unloading of a shared library will make the expression
10456 invalid. This is not a very common use case, but we still
10457 re-evaluate expression, to avoid surprises to the user.
10458
10459 Note that for local watchpoints, we re-evaluate it only if
10460 watchpoints frame id is still valid. If it's not, it means the
10461 watchpoint is out of scope and will be deleted soon. In fact,
10462 I'm not sure we'll ever be called in this case.
10463
10464 If a local watchpoint's frame id is still valid, then
3a5c3e22 10465 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10466
3a5c3e22
PA
10467 Don't do anything about disabled watchpoints, since they will be
10468 reevaluated again when enabled. */
10469 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10470}
10471
77b06cd7
TJB
10472/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10473
10474static int
10475insert_watchpoint (struct bp_location *bl)
10476{
3a5c3e22
PA
10477 struct watchpoint *w = (struct watchpoint *) bl->owner;
10478 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10479
10480 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10481 w->cond_exp);
77b06cd7
TJB
10482}
10483
10484/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10485
10486static int
10487remove_watchpoint (struct bp_location *bl)
10488{
3a5c3e22
PA
10489 struct watchpoint *w = (struct watchpoint *) bl->owner;
10490 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10491
10492 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10493 w->cond_exp);
e09342b5
TJB
10494}
10495
e09342b5 10496static int
348d480f 10497breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10498 struct address_space *aspace, CORE_ADDR bp_addr,
10499 const struct target_waitstatus *ws)
e09342b5 10500{
348d480f 10501 struct breakpoint *b = bl->owner;
3a5c3e22 10502 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10503
348d480f
PA
10504 /* Continuable hardware watchpoints are treated as non-existent if the
10505 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10506 some data address). Otherwise gdb won't stop on a break instruction
10507 in the code (not from a breakpoint) when a hardware watchpoint has
10508 been defined. Also skip watchpoints which we know did not trigger
10509 (did not match the data address). */
10510 if (is_hardware_watchpoint (b)
3a5c3e22 10511 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10512 return 0;
9c06b0b4 10513
348d480f 10514 return 1;
9c06b0b4
TJB
10515}
10516
348d480f
PA
10517static void
10518check_status_watchpoint (bpstat bs)
9c06b0b4 10519{
348d480f 10520 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10521
348d480f 10522 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10523}
10524
10525/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10526 hardware watchpoints. */
9c06b0b4
TJB
10527
10528static int
348d480f 10529resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10530{
3a5c3e22
PA
10531 struct watchpoint *w = (struct watchpoint *) bl->owner;
10532 int length = w->exact? 1 : bl->length;
348d480f
PA
10533
10534 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10535}
10536
10537/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10538 hardware watchpoints. */
9c06b0b4
TJB
10539
10540static int
348d480f 10541works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10542{
efa80663
PA
10543 /* Read and access watchpoints only work with hardware support. */
10544 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10545}
10546
9c06b0b4 10547static enum print_stop_action
348d480f 10548print_it_watchpoint (bpstat bs)
9c06b0b4 10549{
348d480f
PA
10550 struct cleanup *old_chain;
10551 struct breakpoint *b;
f99d8bf4 10552 struct ui_file *stb;
348d480f 10553 enum print_stop_action result;
3a5c3e22 10554 struct watchpoint *w;
79a45e25 10555 struct ui_out *uiout = current_uiout;
348d480f
PA
10556
10557 gdb_assert (bs->bp_location_at != NULL);
10558
348d480f 10559 b = bs->breakpoint_at;
3a5c3e22 10560 w = (struct watchpoint *) b;
348d480f 10561
f99d8bf4
PA
10562 stb = mem_fileopen ();
10563 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10564
10565 switch (b->type)
10566 {
348d480f 10567 case bp_watchpoint:
9c06b0b4
TJB
10568 case bp_hardware_watchpoint:
10569 annotate_watchpoint (b->number);
10570 if (ui_out_is_mi_like_p (uiout))
10571 ui_out_field_string
10572 (uiout, "reason",
10573 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10574 mention (b);
10575 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10576 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10577 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10578 ui_out_field_stream (uiout, "old", stb);
10579 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10580 watchpoint_value_print (w->val, stb);
348d480f
PA
10581 ui_out_field_stream (uiout, "new", stb);
10582 ui_out_text (uiout, "\n");
10583 /* More than one watchpoint may have been triggered. */
10584 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10585 break;
10586
10587 case bp_read_watchpoint:
10588 if (ui_out_is_mi_like_p (uiout))
10589 ui_out_field_string
10590 (uiout, "reason",
10591 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10592 mention (b);
10593 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10594 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10595 watchpoint_value_print (w->val, stb);
348d480f
PA
10596 ui_out_field_stream (uiout, "value", stb);
10597 ui_out_text (uiout, "\n");
10598 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10599 break;
10600
10601 case bp_access_watchpoint:
348d480f
PA
10602 if (bs->old_val != NULL)
10603 {
10604 annotate_watchpoint (b->number);
10605 if (ui_out_is_mi_like_p (uiout))
10606 ui_out_field_string
10607 (uiout, "reason",
10608 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10609 mention (b);
10610 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10611 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10612 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10613 ui_out_field_stream (uiout, "old", stb);
10614 ui_out_text (uiout, "\nNew value = ");
10615 }
10616 else
10617 {
10618 mention (b);
10619 if (ui_out_is_mi_like_p (uiout))
10620 ui_out_field_string
10621 (uiout, "reason",
10622 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10623 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10624 ui_out_text (uiout, "\nValue = ");
10625 }
f99d8bf4 10626 watchpoint_value_print (w->val, stb);
348d480f
PA
10627 ui_out_field_stream (uiout, "new", stb);
10628 ui_out_text (uiout, "\n");
10629 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10630 break;
10631 default:
348d480f 10632 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10633 }
10634
348d480f
PA
10635 do_cleanups (old_chain);
10636 return result;
10637}
10638
10639/* Implement the "print_mention" breakpoint_ops method for hardware
10640 watchpoints. */
10641
10642static void
10643print_mention_watchpoint (struct breakpoint *b)
10644{
10645 struct cleanup *ui_out_chain;
3a5c3e22 10646 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10647 struct ui_out *uiout = current_uiout;
348d480f
PA
10648
10649 switch (b->type)
10650 {
10651 case bp_watchpoint:
10652 ui_out_text (uiout, "Watchpoint ");
10653 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10654 break;
10655 case bp_hardware_watchpoint:
10656 ui_out_text (uiout, "Hardware watchpoint ");
10657 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10658 break;
10659 case bp_read_watchpoint:
10660 ui_out_text (uiout, "Hardware read watchpoint ");
10661 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10662 break;
10663 case bp_access_watchpoint:
10664 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10665 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10666 break;
10667 default:
10668 internal_error (__FILE__, __LINE__,
10669 _("Invalid hardware watchpoint type."));
10670 }
10671
10672 ui_out_field_int (uiout, "number", b->number);
10673 ui_out_text (uiout, ": ");
3a5c3e22 10674 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10675 do_cleanups (ui_out_chain);
10676}
10677
10678/* Implement the "print_recreate" breakpoint_ops method for
10679 watchpoints. */
10680
10681static void
10682print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10683{
3a5c3e22
PA
10684 struct watchpoint *w = (struct watchpoint *) b;
10685
348d480f
PA
10686 switch (b->type)
10687 {
10688 case bp_watchpoint:
10689 case bp_hardware_watchpoint:
10690 fprintf_unfiltered (fp, "watch");
10691 break;
10692 case bp_read_watchpoint:
10693 fprintf_unfiltered (fp, "rwatch");
10694 break;
10695 case bp_access_watchpoint:
10696 fprintf_unfiltered (fp, "awatch");
10697 break;
10698 default:
10699 internal_error (__FILE__, __LINE__,
10700 _("Invalid watchpoint type."));
10701 }
10702
3a5c3e22 10703 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10704 print_recreate_thread (b, fp);
348d480f
PA
10705}
10706
427cd150
TT
10707/* Implement the "explains_signal" breakpoint_ops method for
10708 watchpoints. */
10709
47591c29 10710static int
427cd150
TT
10711explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10712{
10713 /* A software watchpoint cannot cause a signal other than
10714 GDB_SIGNAL_TRAP. */
10715 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10716 return 0;
427cd150 10717
47591c29 10718 return 1;
427cd150
TT
10719}
10720
348d480f
PA
10721/* The breakpoint_ops structure to be used in hardware watchpoints. */
10722
2060206e 10723static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10724
10725/* Implement the "insert" breakpoint_ops method for
10726 masked hardware watchpoints. */
10727
10728static int
10729insert_masked_watchpoint (struct bp_location *bl)
10730{
3a5c3e22
PA
10731 struct watchpoint *w = (struct watchpoint *) bl->owner;
10732
10733 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10734 bl->watchpoint_type);
10735}
10736
10737/* Implement the "remove" breakpoint_ops method for
10738 masked hardware watchpoints. */
10739
10740static int
10741remove_masked_watchpoint (struct bp_location *bl)
10742{
3a5c3e22
PA
10743 struct watchpoint *w = (struct watchpoint *) bl->owner;
10744
10745 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10746 bl->watchpoint_type);
10747}
10748
10749/* Implement the "resources_needed" breakpoint_ops method for
10750 masked hardware watchpoints. */
10751
10752static int
10753resources_needed_masked_watchpoint (const struct bp_location *bl)
10754{
3a5c3e22
PA
10755 struct watchpoint *w = (struct watchpoint *) bl->owner;
10756
10757 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10758}
10759
10760/* Implement the "works_in_software_mode" breakpoint_ops method for
10761 masked hardware watchpoints. */
10762
10763static int
10764works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10765{
10766 return 0;
10767}
10768
10769/* Implement the "print_it" breakpoint_ops method for
10770 masked hardware watchpoints. */
10771
10772static enum print_stop_action
10773print_it_masked_watchpoint (bpstat bs)
10774{
10775 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10776 struct ui_out *uiout = current_uiout;
348d480f
PA
10777
10778 /* Masked watchpoints have only one location. */
10779 gdb_assert (b->loc && b->loc->next == NULL);
10780
10781 switch (b->type)
10782 {
10783 case bp_hardware_watchpoint:
10784 annotate_watchpoint (b->number);
10785 if (ui_out_is_mi_like_p (uiout))
10786 ui_out_field_string
10787 (uiout, "reason",
10788 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10789 break;
10790
10791 case bp_read_watchpoint:
10792 if (ui_out_is_mi_like_p (uiout))
10793 ui_out_field_string
10794 (uiout, "reason",
10795 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10796 break;
10797
10798 case bp_access_watchpoint:
10799 if (ui_out_is_mi_like_p (uiout))
10800 ui_out_field_string
10801 (uiout, "reason",
10802 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10803 break;
10804 default:
10805 internal_error (__FILE__, __LINE__,
10806 _("Invalid hardware watchpoint type."));
10807 }
10808
10809 mention (b);
9c06b0b4
TJB
10810 ui_out_text (uiout, _("\n\
10811Check the underlying instruction at PC for the memory\n\
10812address and value which triggered this watchpoint.\n"));
10813 ui_out_text (uiout, "\n");
10814
10815 /* More than one watchpoint may have been triggered. */
10816 return PRINT_UNKNOWN;
10817}
10818
10819/* Implement the "print_one_detail" breakpoint_ops method for
10820 masked hardware watchpoints. */
10821
10822static void
10823print_one_detail_masked_watchpoint (const struct breakpoint *b,
10824 struct ui_out *uiout)
10825{
3a5c3e22
PA
10826 struct watchpoint *w = (struct watchpoint *) b;
10827
9c06b0b4
TJB
10828 /* Masked watchpoints have only one location. */
10829 gdb_assert (b->loc && b->loc->next == NULL);
10830
10831 ui_out_text (uiout, "\tmask ");
3a5c3e22 10832 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
10833 ui_out_text (uiout, "\n");
10834}
10835
10836/* Implement the "print_mention" breakpoint_ops method for
10837 masked hardware watchpoints. */
10838
10839static void
10840print_mention_masked_watchpoint (struct breakpoint *b)
10841{
3a5c3e22 10842 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10843 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
10844 struct cleanup *ui_out_chain;
10845
10846 switch (b->type)
10847 {
10848 case bp_hardware_watchpoint:
10849 ui_out_text (uiout, "Masked hardware watchpoint ");
10850 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10851 break;
10852 case bp_read_watchpoint:
10853 ui_out_text (uiout, "Masked hardware read watchpoint ");
10854 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10855 break;
10856 case bp_access_watchpoint:
10857 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10858 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10859 break;
10860 default:
10861 internal_error (__FILE__, __LINE__,
10862 _("Invalid hardware watchpoint type."));
10863 }
10864
10865 ui_out_field_int (uiout, "number", b->number);
10866 ui_out_text (uiout, ": ");
3a5c3e22 10867 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
10868 do_cleanups (ui_out_chain);
10869}
10870
10871/* Implement the "print_recreate" breakpoint_ops method for
10872 masked hardware watchpoints. */
10873
10874static void
10875print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10876{
3a5c3e22 10877 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10878 char tmp[40];
10879
10880 switch (b->type)
10881 {
10882 case bp_hardware_watchpoint:
10883 fprintf_unfiltered (fp, "watch");
10884 break;
10885 case bp_read_watchpoint:
10886 fprintf_unfiltered (fp, "rwatch");
10887 break;
10888 case bp_access_watchpoint:
10889 fprintf_unfiltered (fp, "awatch");
10890 break;
10891 default:
10892 internal_error (__FILE__, __LINE__,
10893 _("Invalid hardware watchpoint type."));
10894 }
10895
3a5c3e22
PA
10896 sprintf_vma (tmp, w->hw_wp_mask);
10897 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10898 print_recreate_thread (b, fp);
9c06b0b4
TJB
10899}
10900
10901/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10902
2060206e 10903static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10904
10905/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10906
10907static int
10908is_masked_watchpoint (const struct breakpoint *b)
10909{
10910 return b->ops == &masked_watchpoint_breakpoint_ops;
10911}
10912
53a5351d
JM
10913/* accessflag: hw_write: watch write,
10914 hw_read: watch read,
10915 hw_access: watch access (read or write) */
c906108c 10916static void
bbc13ae3 10917watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10918 int just_location, int internal)
c906108c 10919{
d983da9c 10920 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 10921 struct expression *exp;
270140bd 10922 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10923 struct value *val, *mark, *result;
bb9d5f81 10924 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 10925 struct frame_info *frame;
bbc13ae3
KS
10926 const char *exp_start = NULL;
10927 const char *exp_end = NULL;
10928 const char *tok, *end_tok;
9c06b0b4 10929 int toklen = -1;
bbc13ae3
KS
10930 const char *cond_start = NULL;
10931 const char *cond_end = NULL;
c906108c 10932 enum bptype bp_type;
37e4754d 10933 int thread = -1;
0cf6dd15 10934 int pc = 0;
9c06b0b4
TJB
10935 /* Flag to indicate whether we are going to use masks for
10936 the hardware watchpoint. */
10937 int use_mask = 0;
10938 CORE_ADDR mask = 0;
3a5c3e22 10939 struct watchpoint *w;
bbc13ae3
KS
10940 char *expression;
10941 struct cleanup *back_to;
c906108c 10942
37e4754d
LM
10943 /* Make sure that we actually have parameters to parse. */
10944 if (arg != NULL && arg[0] != '\0')
10945 {
bbc13ae3
KS
10946 const char *value_start;
10947
10948 exp_end = arg + strlen (arg);
37e4754d 10949
9c06b0b4
TJB
10950 /* Look for "parameter value" pairs at the end
10951 of the arguments string. */
bbc13ae3 10952 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
10953 {
10954 /* Skip whitespace at the end of the argument list. */
10955 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10956 tok--;
10957
10958 /* Find the beginning of the last token.
10959 This is the value of the parameter. */
10960 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10961 tok--;
10962 value_start = tok + 1;
10963
10964 /* Skip whitespace. */
10965 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10966 tok--;
10967
10968 end_tok = tok;
10969
10970 /* Find the beginning of the second to last token.
10971 This is the parameter itself. */
10972 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10973 tok--;
10974 tok++;
10975 toklen = end_tok - tok + 1;
10976
61012eef 10977 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4
TJB
10978 {
10979 /* At this point we've found a "thread" token, which means
10980 the user is trying to set a watchpoint that triggers
10981 only in a specific thread. */
10982 char *endp;
37e4754d 10983
9c06b0b4
TJB
10984 if (thread != -1)
10985 error(_("You can specify only one thread."));
37e4754d 10986
9c06b0b4
TJB
10987 /* Extract the thread ID from the next token. */
10988 thread = strtol (value_start, &endp, 0);
37e4754d 10989
9c06b0b4
TJB
10990 /* Check if the user provided a valid numeric value for the
10991 thread ID. */
10992 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10993 error (_("Invalid thread ID specification %s."), value_start);
10994
10995 /* Check if the thread actually exists. */
10996 if (!valid_thread_id (thread))
af4908ba 10997 invalid_thread_id_error (thread);
9c06b0b4 10998 }
61012eef 10999 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11000 {
11001 /* We've found a "mask" token, which means the user wants to
11002 create a hardware watchpoint that is going to have the mask
11003 facility. */
11004 struct value *mask_value, *mark;
37e4754d 11005
9c06b0b4
TJB
11006 if (use_mask)
11007 error(_("You can specify only one mask."));
37e4754d 11008
9c06b0b4 11009 use_mask = just_location = 1;
37e4754d 11010
9c06b0b4
TJB
11011 mark = value_mark ();
11012 mask_value = parse_to_comma_and_eval (&value_start);
11013 mask = value_as_address (mask_value);
11014 value_free_to_mark (mark);
11015 }
11016 else
11017 /* We didn't recognize what we found. We should stop here. */
11018 break;
37e4754d 11019
9c06b0b4
TJB
11020 /* Truncate the string and get rid of the "parameter value" pair before
11021 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11022 exp_end = tok;
9c06b0b4 11023 }
37e4754d 11024 }
bbc13ae3
KS
11025 else
11026 exp_end = arg;
37e4754d 11027
bbc13ae3
KS
11028 /* Parse the rest of the arguments. From here on out, everything
11029 is in terms of a newly allocated string instead of the original
11030 ARG. */
c906108c 11031 innermost_block = NULL;
bbc13ae3
KS
11032 expression = savestring (arg, exp_end - arg);
11033 back_to = make_cleanup (xfree, expression);
11034 exp_start = arg = expression;
1bb9788d 11035 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11036 exp_end = arg;
fa8a61dc
TT
11037 /* Remove trailing whitespace from the expression before saving it.
11038 This makes the eventual display of the expression string a bit
11039 prettier. */
11040 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11041 --exp_end;
11042
65d79d4b
SDJ
11043 /* Checking if the expression is not constant. */
11044 if (watchpoint_exp_is_const (exp))
11045 {
11046 int len;
11047
11048 len = exp_end - exp_start;
11049 while (len > 0 && isspace (exp_start[len - 1]))
11050 len--;
11051 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11052 }
11053
c906108c
SS
11054 exp_valid_block = innermost_block;
11055 mark = value_mark ();
3a1115a0 11056 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11057
bb9d5f81
PP
11058 if (val != NULL && just_location)
11059 {
11060 saved_bitpos = value_bitpos (val);
11061 saved_bitsize = value_bitsize (val);
11062 }
11063
06a64a0b
TT
11064 if (just_location)
11065 {
9c06b0b4
TJB
11066 int ret;
11067
06a64a0b 11068 exp_valid_block = NULL;
a1442452 11069 val = value_addr (result);
06a64a0b
TT
11070 release_value (val);
11071 value_free_to_mark (mark);
9c06b0b4
TJB
11072
11073 if (use_mask)
11074 {
11075 ret = target_masked_watch_num_registers (value_as_address (val),
11076 mask);
11077 if (ret == -1)
11078 error (_("This target does not support masked watchpoints."));
11079 else if (ret == -2)
11080 error (_("Invalid mask or memory region."));
11081 }
06a64a0b
TT
11082 }
11083 else if (val != NULL)
fa4727a6 11084 release_value (val);
c906108c 11085
bbc13ae3
KS
11086 tok = skip_spaces_const (arg);
11087 end_tok = skip_to_space_const (tok);
c906108c
SS
11088
11089 toklen = end_tok - tok;
11090 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11091 {
2d134ed3
PA
11092 struct expression *cond;
11093
60e1c644 11094 innermost_block = NULL;
c906108c 11095 tok = cond_start = end_tok + 1;
1bb9788d 11096 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11097
11098 /* The watchpoint expression may not be local, but the condition
11099 may still be. E.g.: `watch global if local > 0'. */
11100 cond_exp_valid_block = innermost_block;
11101
2d134ed3 11102 xfree (cond);
c906108c
SS
11103 cond_end = tok;
11104 }
11105 if (*tok)
8a3fe4f8 11106 error (_("Junk at end of command."));
c906108c 11107
d983da9c 11108 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11109
11110 /* If the expression is "local", then set up a "watchpoint scope"
11111 breakpoint at the point where we've left the scope of the watchpoint
11112 expression. Create the scope breakpoint before the watchpoint, so
11113 that we will encounter it first in bpstat_stop_status. */
60e1c644 11114 if (exp_valid_block && frame)
d983da9c 11115 {
edb3359d
DJ
11116 if (frame_id_p (frame_unwind_caller_id (frame)))
11117 {
11118 scope_breakpoint
a6d9a66e
UW
11119 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11120 frame_unwind_caller_pc (frame),
06edf0c0
PA
11121 bp_watchpoint_scope,
11122 &momentary_breakpoint_ops);
d983da9c 11123
edb3359d 11124 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11125
edb3359d
DJ
11126 /* Automatically delete the breakpoint when it hits. */
11127 scope_breakpoint->disposition = disp_del;
d983da9c 11128
edb3359d
DJ
11129 /* Only break in the proper frame (help with recursion). */
11130 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11131
edb3359d 11132 /* Set the address at which we will stop. */
a6d9a66e
UW
11133 scope_breakpoint->loc->gdbarch
11134 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11135 scope_breakpoint->loc->requested_address
11136 = frame_unwind_caller_pc (frame);
11137 scope_breakpoint->loc->address
a6d9a66e
UW
11138 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11139 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11140 scope_breakpoint->type);
11141 }
d983da9c
DJ
11142 }
11143
e8369a73
AB
11144 /* Now set up the breakpoint. We create all watchpoints as hardware
11145 watchpoints here even if hardware watchpoints are turned off, a call
11146 to update_watchpoint later in this function will cause the type to
11147 drop back to bp_watchpoint (software watchpoint) if required. */
11148
11149 if (accessflag == hw_read)
11150 bp_type = bp_read_watchpoint;
11151 else if (accessflag == hw_access)
11152 bp_type = bp_access_watchpoint;
11153 else
11154 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11155
11156 w = XCNEW (struct watchpoint);
11157 b = &w->base;
348d480f 11158 if (use_mask)
3a5c3e22
PA
11159 init_raw_breakpoint_without_location (b, NULL, bp_type,
11160 &masked_watchpoint_breakpoint_ops);
348d480f 11161 else
3a5c3e22
PA
11162 init_raw_breakpoint_without_location (b, NULL, bp_type,
11163 &watchpoint_breakpoint_ops);
37e4754d 11164 b->thread = thread;
b5de0fa7 11165 b->disposition = disp_donttouch;
348d480f 11166 b->pspace = current_program_space;
3a5c3e22
PA
11167 w->exp = exp;
11168 w->exp_valid_block = exp_valid_block;
11169 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11170 if (just_location)
11171 {
11172 struct type *t = value_type (val);
11173 CORE_ADDR addr = value_as_address (val);
11174 char *name;
11175
11176 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11177 name = type_to_string (t);
11178
3a5c3e22 11179 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11180 core_addr_to_string (addr));
06a64a0b
TT
11181 xfree (name);
11182
3a5c3e22 11183 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11184 (int) (exp_end - exp_start), exp_start);
11185
06a64a0b
TT
11186 /* The above expression is in C. */
11187 b->language = language_c;
11188 }
11189 else
3a5c3e22 11190 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11191
11192 if (use_mask)
11193 {
3a5c3e22 11194 w->hw_wp_mask = mask;
9c06b0b4
TJB
11195 }
11196 else
11197 {
3a5c3e22 11198 w->val = val;
bb9d5f81
PP
11199 w->val_bitpos = saved_bitpos;
11200 w->val_bitsize = saved_bitsize;
3a5c3e22 11201 w->val_valid = 1;
9c06b0b4 11202 }
77b06cd7 11203
c906108c
SS
11204 if (cond_start)
11205 b->cond_string = savestring (cond_start, cond_end - cond_start);
11206 else
11207 b->cond_string = 0;
c5aa993b 11208
c906108c 11209 if (frame)
f6bc2008 11210 {
3a5c3e22
PA
11211 w->watchpoint_frame = get_frame_id (frame);
11212 w->watchpoint_thread = inferior_ptid;
f6bc2008 11213 }
c906108c 11214 else
f6bc2008 11215 {
3a5c3e22
PA
11216 w->watchpoint_frame = null_frame_id;
11217 w->watchpoint_thread = null_ptid;
f6bc2008 11218 }
c906108c 11219
d983da9c 11220 if (scope_breakpoint != NULL)
c906108c 11221 {
d983da9c
DJ
11222 /* The scope breakpoint is related to the watchpoint. We will
11223 need to act on them together. */
11224 b->related_breakpoint = scope_breakpoint;
11225 scope_breakpoint->related_breakpoint = b;
c906108c 11226 }
d983da9c 11227
06a64a0b
TT
11228 if (!just_location)
11229 value_free_to_mark (mark);
2d134ed3 11230
492d29ea 11231 TRY
a9634178
TJB
11232 {
11233 /* Finally update the new watchpoint. This creates the locations
11234 that should be inserted. */
3a5c3e22 11235 update_watchpoint (w, 1);
a9634178 11236 }
492d29ea 11237 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11238 {
11239 delete_breakpoint (b);
11240 throw_exception (e);
11241 }
492d29ea 11242 END_CATCH
a9634178 11243
3ea46bff 11244 install_breakpoint (internal, b, 1);
bbc13ae3 11245 do_cleanups (back_to);
c906108c
SS
11246}
11247
e09342b5 11248/* Return count of debug registers needed to watch the given expression.
e09342b5 11249 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11250
c906108c 11251static int
a9634178 11252can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11253{
11254 int found_memory_cnt = 0;
2e70b7b9 11255 struct value *head = v;
c906108c
SS
11256
11257 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11258 if (!can_use_hw_watchpoints)
c906108c 11259 return 0;
c5aa993b 11260
5c44784c
JM
11261 /* Make sure that the value of the expression depends only upon
11262 memory contents, and values computed from them within GDB. If we
11263 find any register references or function calls, we can't use a
11264 hardware watchpoint.
11265
11266 The idea here is that evaluating an expression generates a series
11267 of values, one holding the value of every subexpression. (The
11268 expression a*b+c has five subexpressions: a, b, a*b, c, and
11269 a*b+c.) GDB's values hold almost enough information to establish
11270 the criteria given above --- they identify memory lvalues,
11271 register lvalues, computed values, etcetera. So we can evaluate
11272 the expression, and then scan the chain of values that leaves
11273 behind to decide whether we can detect any possible change to the
11274 expression's final value using only hardware watchpoints.
11275
11276 However, I don't think that the values returned by inferior
11277 function calls are special in any way. So this function may not
11278 notice that an expression involving an inferior function call
11279 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11280 for (; v; v = value_next (v))
c906108c 11281 {
5c44784c 11282 if (VALUE_LVAL (v) == lval_memory)
c906108c 11283 {
8464be76
DJ
11284 if (v != head && value_lazy (v))
11285 /* A lazy memory lvalue in the chain is one that GDB never
11286 needed to fetch; we either just used its address (e.g.,
11287 `a' in `a.b') or we never needed it at all (e.g., `a'
11288 in `a,b'). This doesn't apply to HEAD; if that is
11289 lazy then it was not readable, but watch it anyway. */
5c44784c 11290 ;
53a5351d 11291 else
5c44784c
JM
11292 {
11293 /* Ahh, memory we actually used! Check if we can cover
11294 it with hardware watchpoints. */
df407dfe 11295 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11296
11297 /* We only watch structs and arrays if user asked for it
11298 explicitly, never if they just happen to appear in a
11299 middle of some value chain. */
11300 if (v == head
11301 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11302 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11303 {
42ae5230 11304 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11305 int len;
11306 int num_regs;
11307
a9634178 11308 len = (target_exact_watchpoints
e09342b5
TJB
11309 && is_scalar_type_recursive (vtype))?
11310 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11311
e09342b5
TJB
11312 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11313 if (!num_regs)
2e70b7b9
MS
11314 return 0;
11315 else
e09342b5 11316 found_memory_cnt += num_regs;
2e70b7b9 11317 }
5c44784c 11318 }
c5aa993b 11319 }
5086187c
AC
11320 else if (VALUE_LVAL (v) != not_lval
11321 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11322 return 0; /* These are values from the history (e.g., $1). */
5086187c 11323 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11324 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11325 }
11326
11327 /* The expression itself looks suitable for using a hardware
11328 watchpoint, but give the target machine a chance to reject it. */
11329 return found_memory_cnt;
11330}
11331
8b93c638 11332void
84f4c1fe 11333watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11334{
84f4c1fe 11335 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11336}
11337
06a64a0b
TT
11338/* A helper function that looks for the "-location" argument and then
11339 calls watch_command_1. */
11340
11341static void
11342watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11343{
11344 int just_location = 0;
11345
11346 if (arg
11347 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11348 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11349 {
e9cafbcc 11350 arg = skip_spaces (arg);
06a64a0b
TT
11351 just_location = 1;
11352 }
11353
84f4c1fe 11354 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11355}
8926118c 11356
c5aa993b 11357static void
fba45db2 11358watch_command (char *arg, int from_tty)
c906108c 11359{
06a64a0b 11360 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11361}
11362
8b93c638 11363void
84f4c1fe 11364rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11365{
84f4c1fe 11366 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11367}
8926118c 11368
c5aa993b 11369static void
fba45db2 11370rwatch_command (char *arg, int from_tty)
c906108c 11371{
06a64a0b 11372 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11373}
11374
8b93c638 11375void
84f4c1fe 11376awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11377{
84f4c1fe 11378 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11379}
8926118c 11380
c5aa993b 11381static void
fba45db2 11382awatch_command (char *arg, int from_tty)
c906108c 11383{
06a64a0b 11384 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11385}
c906108c 11386\f
c5aa993b 11387
43ff13b4 11388/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11389 because it uses the mechanisms of breakpoints. */
11390
bfec99b2
PA
11391struct until_break_command_continuation_args
11392{
11393 struct breakpoint *breakpoint;
11394 struct breakpoint *breakpoint2;
186c406b 11395 int thread_num;
bfec99b2
PA
11396};
11397
43ff13b4 11398/* This function is called by fetch_inferior_event via the
4a64f543 11399 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11400 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11401 command. */
c2c6d25f 11402static void
fa4cd53f 11403until_break_command_continuation (void *arg, int err)
43ff13b4 11404{
bfec99b2
PA
11405 struct until_break_command_continuation_args *a = arg;
11406
11407 delete_breakpoint (a->breakpoint);
11408 if (a->breakpoint2)
11409 delete_breakpoint (a->breakpoint2);
186c406b 11410 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11411}
11412
c906108c 11413void
ae66c1fc 11414until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11415{
11416 struct symtabs_and_lines sals;
11417 struct symtab_and_line sal;
8556afb4
PA
11418 struct frame_info *frame;
11419 struct gdbarch *frame_gdbarch;
11420 struct frame_id stack_frame_id;
11421 struct frame_id caller_frame_id;
c906108c 11422 struct breakpoint *breakpoint;
f107f563 11423 struct breakpoint *breakpoint2 = NULL;
c906108c 11424 struct cleanup *old_chain;
186c406b
TT
11425 int thread;
11426 struct thread_info *tp;
c906108c 11427
70509625 11428 clear_proceed_status (0);
c906108c
SS
11429
11430 /* Set a breakpoint where the user wants it and at return from
4a64f543 11431 this function. */
c5aa993b 11432
1bfeeb0f 11433 if (last_displayed_sal_is_valid ())
f8eba3c6 11434 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11435 get_last_displayed_symtab (),
f8eba3c6 11436 get_last_displayed_line ());
c906108c 11437 else
f8eba3c6
TT
11438 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11439 (struct symtab *) NULL, 0);
c5aa993b 11440
c906108c 11441 if (sals.nelts != 1)
8a3fe4f8 11442 error (_("Couldn't get information on specified line."));
c5aa993b 11443
c906108c 11444 sal = sals.sals[0];
4a64f543 11445 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11446
c906108c 11447 if (*arg)
8a3fe4f8 11448 error (_("Junk at end of arguments."));
c5aa993b 11449
c906108c 11450 resolve_sal_pc (&sal);
c5aa993b 11451
186c406b
TT
11452 tp = inferior_thread ();
11453 thread = tp->num;
11454
883bc8d1
PA
11455 old_chain = make_cleanup (null_cleanup, NULL);
11456
8556afb4
PA
11457 /* Note linespec handling above invalidates the frame chain.
11458 Installing a breakpoint also invalidates the frame chain (as it
11459 may need to switch threads), so do any frame handling before
11460 that. */
11461
11462 frame = get_selected_frame (NULL);
11463 frame_gdbarch = get_frame_arch (frame);
11464 stack_frame_id = get_stack_frame_id (frame);
11465 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11466
ae66c1fc
EZ
11467 /* Keep within the current frame, or in frames called by the current
11468 one. */
edb3359d 11469
883bc8d1 11470 if (frame_id_p (caller_frame_id))
c906108c 11471 {
883bc8d1
PA
11472 struct symtab_and_line sal2;
11473
11474 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11475 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11476 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11477 sal2,
11478 caller_frame_id,
f107f563
VP
11479 bp_until);
11480 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11481
883bc8d1 11482 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11483 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11484 }
c5aa993b 11485
c70a6932
JK
11486 /* set_momentary_breakpoint could invalidate FRAME. */
11487 frame = NULL;
11488
883bc8d1
PA
11489 if (anywhere)
11490 /* If the user told us to continue until a specified location,
11491 we don't specify a frame at which we need to stop. */
11492 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11493 null_frame_id, bp_until);
11494 else
11495 /* Otherwise, specify the selected frame, because we want to stop
11496 only at the very same frame. */
11497 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11498 stack_frame_id, bp_until);
11499 make_cleanup_delete_breakpoint (breakpoint);
11500
64ce06e4 11501 proceed (-1, GDB_SIGNAL_DEFAULT);
f107f563 11502
4a64f543
MS
11503 /* If we are running asynchronously, and proceed call above has
11504 actually managed to start the target, arrange for breakpoints to
11505 be deleted when the target stops. Otherwise, we're already
11506 stopped and delete breakpoints via cleanup chain. */
f107f563 11507
8ea051c5 11508 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11509 {
bfec99b2
PA
11510 struct until_break_command_continuation_args *args;
11511 args = xmalloc (sizeof (*args));
f107f563 11512
bfec99b2
PA
11513 args->breakpoint = breakpoint;
11514 args->breakpoint2 = breakpoint2;
186c406b 11515 args->thread_num = thread;
f107f563
VP
11516
11517 discard_cleanups (old_chain);
95e54da7
PA
11518 add_continuation (inferior_thread (),
11519 until_break_command_continuation, args,
604ead4a 11520 xfree);
f107f563
VP
11521 }
11522 else
c5aa993b 11523 do_cleanups (old_chain);
c906108c 11524}
ae66c1fc 11525
c906108c
SS
11526/* This function attempts to parse an optional "if <cond>" clause
11527 from the arg string. If one is not found, it returns NULL.
c5aa993b 11528
c906108c
SS
11529 Else, it returns a pointer to the condition string. (It does not
11530 attempt to evaluate the string against a particular block.) And,
11531 it updates arg to point to the first character following the parsed
4a64f543 11532 if clause in the arg string. */
53a5351d 11533
916703c0 11534char *
fba45db2 11535ep_parse_optional_if_clause (char **arg)
c906108c 11536{
c5aa993b
JM
11537 char *cond_string;
11538
11539 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11540 return NULL;
c5aa993b 11541
4a64f543 11542 /* Skip the "if" keyword. */
c906108c 11543 (*arg) += 2;
c5aa993b 11544
c906108c 11545 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11546 condition string. */
e9cafbcc 11547 *arg = skip_spaces (*arg);
c906108c 11548 cond_string = *arg;
c5aa993b 11549
4a64f543
MS
11550 /* Assume that the condition occupies the remainder of the arg
11551 string. */
c906108c 11552 (*arg) += strlen (cond_string);
c5aa993b 11553
c906108c
SS
11554 return cond_string;
11555}
c5aa993b 11556
c906108c
SS
11557/* Commands to deal with catching events, such as signals, exceptions,
11558 process start/exit, etc. */
c5aa993b
JM
11559
11560typedef enum
11561{
44feb3ce
TT
11562 catch_fork_temporary, catch_vfork_temporary,
11563 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11564}
11565catch_fork_kind;
11566
c906108c 11567static void
cc59ec59
MS
11568catch_fork_command_1 (char *arg, int from_tty,
11569 struct cmd_list_element *command)
c906108c 11570{
a6d9a66e 11571 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11572 char *cond_string = NULL;
44feb3ce
TT
11573 catch_fork_kind fork_kind;
11574 int tempflag;
11575
11576 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11577 tempflag = (fork_kind == catch_fork_temporary
11578 || fork_kind == catch_vfork_temporary);
c5aa993b 11579
44feb3ce
TT
11580 if (!arg)
11581 arg = "";
e9cafbcc 11582 arg = skip_spaces (arg);
c5aa993b 11583
c906108c 11584 /* The allowed syntax is:
c5aa993b
JM
11585 catch [v]fork
11586 catch [v]fork if <cond>
11587
4a64f543 11588 First, check if there's an if clause. */
c906108c 11589 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11590
c906108c 11591 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11592 error (_("Junk at end of arguments."));
c5aa993b 11593
c906108c 11594 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11595 and enable reporting of such events. */
c5aa993b
JM
11596 switch (fork_kind)
11597 {
44feb3ce
TT
11598 case catch_fork_temporary:
11599 case catch_fork_permanent:
a6d9a66e 11600 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11601 &catch_fork_breakpoint_ops);
c906108c 11602 break;
44feb3ce
TT
11603 case catch_vfork_temporary:
11604 case catch_vfork_permanent:
a6d9a66e 11605 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11606 &catch_vfork_breakpoint_ops);
c906108c 11607 break;
c5aa993b 11608 default:
8a3fe4f8 11609 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11610 break;
c5aa993b 11611 }
c906108c
SS
11612}
11613
11614static void
cc59ec59
MS
11615catch_exec_command_1 (char *arg, int from_tty,
11616 struct cmd_list_element *command)
c906108c 11617{
b4d90040 11618 struct exec_catchpoint *c;
a6d9a66e 11619 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11620 int tempflag;
c5aa993b 11621 char *cond_string = NULL;
c906108c 11622
44feb3ce
TT
11623 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11624
11625 if (!arg)
11626 arg = "";
e9cafbcc 11627 arg = skip_spaces (arg);
c906108c
SS
11628
11629 /* The allowed syntax is:
c5aa993b
JM
11630 catch exec
11631 catch exec if <cond>
c906108c 11632
4a64f543 11633 First, check if there's an if clause. */
c906108c
SS
11634 cond_string = ep_parse_optional_if_clause (&arg);
11635
11636 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11637 error (_("Junk at end of arguments."));
c906108c 11638
b4d90040
PA
11639 c = XNEW (struct exec_catchpoint);
11640 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11641 &catch_exec_breakpoint_ops);
11642 c->exec_pathname = NULL;
11643
3ea46bff 11644 install_breakpoint (0, &c->base, 1);
c906108c 11645}
c5aa993b 11646
9ac4176b 11647void
28010a5d
PA
11648init_ada_exception_breakpoint (struct breakpoint *b,
11649 struct gdbarch *gdbarch,
11650 struct symtab_and_line sal,
11651 char *addr_string,
c0a91b2b 11652 const struct breakpoint_ops *ops,
28010a5d 11653 int tempflag,
349774ef 11654 int enabled,
28010a5d 11655 int from_tty)
f7f9143b 11656{
f7f9143b
JB
11657 if (from_tty)
11658 {
5af949e3
UW
11659 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11660 if (!loc_gdbarch)
11661 loc_gdbarch = gdbarch;
11662
6c95b8df
PA
11663 describe_other_breakpoints (loc_gdbarch,
11664 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11665 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11666 version for exception catchpoints, because two catchpoints
11667 used for different exception names will use the same address.
11668 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11669 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11670 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11671 the user what type of catchpoint it is. The above is good
11672 enough for now, though. */
11673 }
11674
28010a5d 11675 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11676
349774ef 11677 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11678 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
11679 b->addr_string = addr_string;
11680 b->language = language_ada;
f7f9143b
JB
11681}
11682
c906108c 11683static void
fba45db2 11684catch_command (char *arg, int from_tty)
c906108c 11685{
44feb3ce 11686 error (_("Catch requires an event name."));
c906108c
SS
11687}
11688\f
11689
11690static void
fba45db2 11691tcatch_command (char *arg, int from_tty)
c906108c 11692{
44feb3ce 11693 error (_("Catch requires an event name."));
c906108c
SS
11694}
11695
8a2c437b
TT
11696/* A qsort comparison function that sorts breakpoints in order. */
11697
11698static int
11699compare_breakpoints (const void *a, const void *b)
11700{
11701 const breakpoint_p *ba = a;
11702 uintptr_t ua = (uintptr_t) *ba;
11703 const breakpoint_p *bb = b;
11704 uintptr_t ub = (uintptr_t) *bb;
11705
11706 if ((*ba)->number < (*bb)->number)
11707 return -1;
11708 else if ((*ba)->number > (*bb)->number)
11709 return 1;
11710
11711 /* Now sort by address, in case we see, e..g, two breakpoints with
11712 the number 0. */
11713 if (ua < ub)
11714 return -1;
94b0e70d 11715 return ua > ub ? 1 : 0;
8a2c437b
TT
11716}
11717
80f8a6eb 11718/* Delete breakpoints by address or line. */
c906108c
SS
11719
11720static void
fba45db2 11721clear_command (char *arg, int from_tty)
c906108c 11722{
8a2c437b 11723 struct breakpoint *b, *prev;
d6e956e5
VP
11724 VEC(breakpoint_p) *found = 0;
11725 int ix;
c906108c
SS
11726 int default_match;
11727 struct symtabs_and_lines sals;
11728 struct symtab_and_line sal;
c906108c 11729 int i;
8a2c437b 11730 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11731
11732 if (arg)
11733 {
39cf75f7
DE
11734 sals = decode_line_with_current_source (arg,
11735 (DECODE_LINE_FUNFIRSTLINE
11736 | DECODE_LINE_LIST_MODE));
cf4ded82 11737 make_cleanup (xfree, sals.sals);
c906108c
SS
11738 default_match = 0;
11739 }
11740 else
11741 {
c5aa993b 11742 sals.sals = (struct symtab_and_line *)
c906108c 11743 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 11744 make_cleanup (xfree, sals.sals);
4a64f543 11745 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11746
11747 /* Set sal's line, symtab, pc, and pspace to the values
11748 corresponding to the last call to print_frame_info. If the
11749 codepoint is not valid, this will set all the fields to 0. */
11750 get_last_displayed_sal (&sal);
c906108c 11751 if (sal.symtab == 0)
8a3fe4f8 11752 error (_("No source file specified."));
c906108c
SS
11753
11754 sals.sals[0] = sal;
11755 sals.nelts = 1;
11756
11757 default_match = 1;
11758 }
11759
4a64f543
MS
11760 /* We don't call resolve_sal_pc here. That's not as bad as it
11761 seems, because all existing breakpoints typically have both
11762 file/line and pc set. So, if clear is given file/line, we can
11763 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11764
11765 We only support clearing given the address explicitly
11766 present in breakpoint table. Say, we've set breakpoint
4a64f543 11767 at file:line. There were several PC values for that file:line,
ed0616c6 11768 due to optimization, all in one block.
4a64f543
MS
11769
11770 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11771 PC corresponding to the same file:line, the breakpoint won't
11772 be cleared. We probably can still clear the breakpoint, but
11773 since the other PC value is never presented to user, user
11774 can only find it by guessing, and it does not seem important
11775 to support that. */
11776
4a64f543
MS
11777 /* For each line spec given, delete bps which correspond to it. Do
11778 it in two passes, solely to preserve the current behavior that
11779 from_tty is forced true if we delete more than one
11780 breakpoint. */
c906108c 11781
80f8a6eb 11782 found = NULL;
8a2c437b 11783 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11784 for (i = 0; i < sals.nelts; i++)
11785 {
05cba821
JK
11786 const char *sal_fullname;
11787
c906108c 11788 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11789 If line given (pc == 0), clear all bpts on specified line.
11790 If defaulting, clear all bpts on default line
c906108c 11791 or at default pc.
c5aa993b
JM
11792
11793 defaulting sal.pc != 0 tests to do
11794
11795 0 1 pc
11796 1 1 pc _and_ line
11797 0 0 line
11798 1 0 <can't happen> */
c906108c
SS
11799
11800 sal = sals.sals[i];
05cba821
JK
11801 sal_fullname = (sal.symtab == NULL
11802 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11803
4a64f543 11804 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11805 ALL_BREAKPOINTS (b)
c5aa993b 11806 {
0d381245 11807 int match = 0;
4a64f543 11808 /* Are we going to delete b? */
cc60f2e3 11809 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11810 {
11811 struct bp_location *loc = b->loc;
11812 for (; loc; loc = loc->next)
11813 {
f8eba3c6
TT
11814 /* If the user specified file:line, don't allow a PC
11815 match. This matches historical gdb behavior. */
11816 int pc_match = (!sal.explicit_line
11817 && sal.pc
11818 && (loc->pspace == sal.pspace)
11819 && (loc->address == sal.pc)
11820 && (!section_is_overlay (loc->section)
11821 || loc->section == sal.section));
4aac40c8
TT
11822 int line_match = 0;
11823
11824 if ((default_match || sal.explicit_line)
2f202fde 11825 && loc->symtab != NULL
05cba821 11826 && sal_fullname != NULL
4aac40c8 11827 && sal.pspace == loc->pspace
05cba821
JK
11828 && loc->line_number == sal.line
11829 && filename_cmp (symtab_to_fullname (loc->symtab),
11830 sal_fullname) == 0)
11831 line_match = 1;
4aac40c8 11832
0d381245
VP
11833 if (pc_match || line_match)
11834 {
11835 match = 1;
11836 break;
11837 }
11838 }
11839 }
11840
11841 if (match)
d6e956e5 11842 VEC_safe_push(breakpoint_p, found, b);
c906108c 11843 }
80f8a6eb 11844 }
8a2c437b 11845
80f8a6eb 11846 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11847 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11848 {
11849 if (arg)
8a3fe4f8 11850 error (_("No breakpoint at %s."), arg);
80f8a6eb 11851 else
8a3fe4f8 11852 error (_("No breakpoint at this line."));
80f8a6eb 11853 }
c906108c 11854
8a2c437b
TT
11855 /* Remove duplicates from the vec. */
11856 qsort (VEC_address (breakpoint_p, found),
11857 VEC_length (breakpoint_p, found),
11858 sizeof (breakpoint_p),
11859 compare_breakpoints);
11860 prev = VEC_index (breakpoint_p, found, 0);
11861 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11862 {
11863 if (b == prev)
11864 {
11865 VEC_ordered_remove (breakpoint_p, found, ix);
11866 --ix;
11867 }
11868 }
11869
d6e956e5 11870 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11871 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11872 if (from_tty)
a3f17187 11873 {
d6e956e5 11874 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11875 printf_unfiltered (_("Deleted breakpoint "));
11876 else
11877 printf_unfiltered (_("Deleted breakpoints "));
11878 }
d6e956e5
VP
11879
11880 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11881 {
c5aa993b 11882 if (from_tty)
d6e956e5
VP
11883 printf_unfiltered ("%d ", b->number);
11884 delete_breakpoint (b);
c906108c 11885 }
80f8a6eb
MS
11886 if (from_tty)
11887 putchar_unfiltered ('\n');
8a2c437b
TT
11888
11889 do_cleanups (cleanups);
c906108c
SS
11890}
11891\f
11892/* Delete breakpoint in BS if they are `delete' breakpoints and
11893 all breakpoints that are marked for deletion, whether hit or not.
11894 This is called after any breakpoint is hit, or after errors. */
11895
11896void
fba45db2 11897breakpoint_auto_delete (bpstat bs)
c906108c 11898{
35df4500 11899 struct breakpoint *b, *b_tmp;
c906108c
SS
11900
11901 for (; bs; bs = bs->next)
f431efe5
PA
11902 if (bs->breakpoint_at
11903 && bs->breakpoint_at->disposition == disp_del
c906108c 11904 && bs->stop)
f431efe5 11905 delete_breakpoint (bs->breakpoint_at);
c906108c 11906
35df4500 11907 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11908 {
b5de0fa7 11909 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11910 delete_breakpoint (b);
11911 }
c906108c
SS
11912}
11913
4a64f543
MS
11914/* A comparison function for bp_location AP and BP being interfaced to
11915 qsort. Sort elements primarily by their ADDRESS (no matter what
11916 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 11917 secondarily by ordering first permanent elements and
4a64f543 11918 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11919 qsort being an unstable algorithm. */
876fa593
JK
11920
11921static int
494cfb0f 11922bp_location_compare (const void *ap, const void *bp)
876fa593 11923{
494cfb0f
JK
11924 struct bp_location *a = *(void **) ap;
11925 struct bp_location *b = *(void **) bp;
876fa593
JK
11926
11927 if (a->address != b->address)
11928 return (a->address > b->address) - (a->address < b->address);
11929
dea2aa5f
LM
11930 /* Sort locations at the same address by their pspace number, keeping
11931 locations of the same inferior (in a multi-inferior environment)
11932 grouped. */
11933
11934 if (a->pspace->num != b->pspace->num)
11935 return ((a->pspace->num > b->pspace->num)
11936 - (a->pspace->num < b->pspace->num));
11937
876fa593 11938 /* Sort permanent breakpoints first. */
1a853c52
PA
11939 if (a->permanent != b->permanent)
11940 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 11941
c56a97f9
JK
11942 /* Make the internal GDB representation stable across GDB runs
11943 where A and B memory inside GDB can differ. Breakpoint locations of
11944 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11945
11946 if (a->owner->number != b->owner->number)
c56a97f9
JK
11947 return ((a->owner->number > b->owner->number)
11948 - (a->owner->number < b->owner->number));
876fa593
JK
11949
11950 return (a > b) - (a < b);
11951}
11952
876fa593 11953/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
11954 bp_location_shadow_len_after_address_max according to the current
11955 content of the bp_location array. */
f7545552
TT
11956
11957static void
876fa593 11958bp_location_target_extensions_update (void)
f7545552 11959{
876fa593
JK
11960 struct bp_location *bl, **blp_tmp;
11961
11962 bp_location_placed_address_before_address_max = 0;
11963 bp_location_shadow_len_after_address_max = 0;
11964
11965 ALL_BP_LOCATIONS (bl, blp_tmp)
11966 {
11967 CORE_ADDR start, end, addr;
11968
11969 if (!bp_location_has_shadow (bl))
11970 continue;
11971
11972 start = bl->target_info.placed_address;
11973 end = start + bl->target_info.shadow_len;
11974
11975 gdb_assert (bl->address >= start);
11976 addr = bl->address - start;
11977 if (addr > bp_location_placed_address_before_address_max)
11978 bp_location_placed_address_before_address_max = addr;
11979
11980 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11981
11982 gdb_assert (bl->address < end);
11983 addr = end - bl->address;
11984 if (addr > bp_location_shadow_len_after_address_max)
11985 bp_location_shadow_len_after_address_max = addr;
11986 }
f7545552
TT
11987}
11988
1e4d1764
YQ
11989/* Download tracepoint locations if they haven't been. */
11990
11991static void
11992download_tracepoint_locations (void)
11993{
7ed2c994 11994 struct breakpoint *b;
1e4d1764
YQ
11995 struct cleanup *old_chain;
11996
11997 if (!target_can_download_tracepoint ())
11998 return;
11999
12000 old_chain = save_current_space_and_thread ();
12001
7ed2c994 12002 ALL_TRACEPOINTS (b)
1e4d1764 12003 {
7ed2c994 12004 struct bp_location *bl;
1e4d1764 12005 struct tracepoint *t;
f2a8bc8a 12006 int bp_location_downloaded = 0;
1e4d1764 12007
7ed2c994 12008 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12009 ? !may_insert_fast_tracepoints
12010 : !may_insert_tracepoints))
12011 continue;
12012
7ed2c994
YQ
12013 for (bl = b->loc; bl; bl = bl->next)
12014 {
12015 /* In tracepoint, locations are _never_ duplicated, so
12016 should_be_inserted is equivalent to
12017 unduplicated_should_be_inserted. */
12018 if (!should_be_inserted (bl) || bl->inserted)
12019 continue;
1e4d1764 12020
7ed2c994 12021 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12022
7ed2c994 12023 target_download_tracepoint (bl);
1e4d1764 12024
7ed2c994 12025 bl->inserted = 1;
f2a8bc8a 12026 bp_location_downloaded = 1;
7ed2c994
YQ
12027 }
12028 t = (struct tracepoint *) b;
12029 t->number_on_target = b->number;
f2a8bc8a
YQ
12030 if (bp_location_downloaded)
12031 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12032 }
12033
12034 do_cleanups (old_chain);
12035}
12036
934709f0
PW
12037/* Swap the insertion/duplication state between two locations. */
12038
12039static void
12040swap_insertion (struct bp_location *left, struct bp_location *right)
12041{
12042 const int left_inserted = left->inserted;
12043 const int left_duplicate = left->duplicate;
b775012e 12044 const int left_needs_update = left->needs_update;
934709f0
PW
12045 const struct bp_target_info left_target_info = left->target_info;
12046
1e4d1764
YQ
12047 /* Locations of tracepoints can never be duplicated. */
12048 if (is_tracepoint (left->owner))
12049 gdb_assert (!left->duplicate);
12050 if (is_tracepoint (right->owner))
12051 gdb_assert (!right->duplicate);
12052
934709f0
PW
12053 left->inserted = right->inserted;
12054 left->duplicate = right->duplicate;
b775012e 12055 left->needs_update = right->needs_update;
934709f0
PW
12056 left->target_info = right->target_info;
12057 right->inserted = left_inserted;
12058 right->duplicate = left_duplicate;
b775012e 12059 right->needs_update = left_needs_update;
934709f0
PW
12060 right->target_info = left_target_info;
12061}
12062
b775012e
LM
12063/* Force the re-insertion of the locations at ADDRESS. This is called
12064 once a new/deleted/modified duplicate location is found and we are evaluating
12065 conditions on the target's side. Such conditions need to be updated on
12066 the target. */
12067
12068static void
12069force_breakpoint_reinsertion (struct bp_location *bl)
12070{
12071 struct bp_location **locp = NULL, **loc2p;
12072 struct bp_location *loc;
12073 CORE_ADDR address = 0;
12074 int pspace_num;
12075
12076 address = bl->address;
12077 pspace_num = bl->pspace->num;
12078
12079 /* This is only meaningful if the target is
12080 evaluating conditions and if the user has
12081 opted for condition evaluation on the target's
12082 side. */
12083 if (gdb_evaluates_breakpoint_condition_p ()
12084 || !target_supports_evaluation_of_breakpoint_conditions ())
12085 return;
12086
12087 /* Flag all breakpoint locations with this address and
12088 the same program space as the location
12089 as "its condition has changed". We need to
12090 update the conditions on the target's side. */
12091 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12092 {
12093 loc = *loc2p;
12094
12095 if (!is_breakpoint (loc->owner)
12096 || pspace_num != loc->pspace->num)
12097 continue;
12098
12099 /* Flag the location appropriately. We use a different state to
12100 let everyone know that we already updated the set of locations
12101 with addr bl->address and program space bl->pspace. This is so
12102 we don't have to keep calling these functions just to mark locations
12103 that have already been marked. */
12104 loc->condition_changed = condition_updated;
12105
12106 /* Free the agent expression bytecode as well. We will compute
12107 it later on. */
12108 if (loc->cond_bytecode)
12109 {
12110 free_agent_expr (loc->cond_bytecode);
12111 loc->cond_bytecode = NULL;
12112 }
12113 }
12114}
44702360
PA
12115/* Called whether new breakpoints are created, or existing breakpoints
12116 deleted, to update the global location list and recompute which
12117 locations are duplicate of which.
b775012e 12118
04086b45
PA
12119 The INSERT_MODE flag determines whether locations may not, may, or
12120 shall be inserted now. See 'enum ugll_insert_mode' for more
12121 info. */
b60e7edf 12122
0d381245 12123static void
44702360 12124update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12125{
74960c60 12126 struct breakpoint *b;
876fa593 12127 struct bp_location **locp, *loc;
f7545552 12128 struct cleanup *cleanups;
b775012e
LM
12129 /* Last breakpoint location address that was marked for update. */
12130 CORE_ADDR last_addr = 0;
12131 /* Last breakpoint location program space that was marked for update. */
12132 int last_pspace_num = -1;
f7545552 12133
2d134ed3
PA
12134 /* Used in the duplicates detection below. When iterating over all
12135 bp_locations, points to the first bp_location of a given address.
12136 Breakpoints and watchpoints of different types are never
12137 duplicates of each other. Keep one pointer for each type of
12138 breakpoint/watchpoint, so we only need to loop over all locations
12139 once. */
12140 struct bp_location *bp_loc_first; /* breakpoint */
12141 struct bp_location *wp_loc_first; /* hardware watchpoint */
12142 struct bp_location *awp_loc_first; /* access watchpoint */
12143 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12144
4a64f543
MS
12145 /* Saved former bp_location array which we compare against the newly
12146 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12147 struct bp_location **old_location, **old_locp;
12148 unsigned old_location_count;
12149
12150 old_location = bp_location;
12151 old_location_count = bp_location_count;
12152 bp_location = NULL;
12153 bp_location_count = 0;
12154 cleanups = make_cleanup (xfree, old_location);
0d381245 12155
74960c60 12156 ALL_BREAKPOINTS (b)
876fa593
JK
12157 for (loc = b->loc; loc; loc = loc->next)
12158 bp_location_count++;
12159
12160 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12161 locp = bp_location;
12162 ALL_BREAKPOINTS (b)
12163 for (loc = b->loc; loc; loc = loc->next)
12164 *locp++ = loc;
12165 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12166 bp_location_compare);
876fa593
JK
12167
12168 bp_location_target_extensions_update ();
74960c60 12169
4a64f543
MS
12170 /* Identify bp_location instances that are no longer present in the
12171 new list, and therefore should be freed. Note that it's not
12172 necessary that those locations should be removed from inferior --
12173 if there's another location at the same address (previously
12174 marked as duplicate), we don't need to remove/insert the
12175 location.
876fa593 12176
4a64f543
MS
12177 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12178 and former bp_location array state respectively. */
876fa593
JK
12179
12180 locp = bp_location;
12181 for (old_locp = old_location; old_locp < old_location + old_location_count;
12182 old_locp++)
74960c60 12183 {
876fa593 12184 struct bp_location *old_loc = *old_locp;
c7d46a38 12185 struct bp_location **loc2p;
876fa593 12186
e5dd4106 12187 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12188 not, we have to free it. */
c7d46a38 12189 int found_object = 0;
20874c92
VP
12190 /* Tells if the location should remain inserted in the target. */
12191 int keep_in_target = 0;
12192 int removed = 0;
876fa593 12193
4a64f543
MS
12194 /* Skip LOCP entries which will definitely never be needed.
12195 Stop either at or being the one matching OLD_LOC. */
876fa593 12196 while (locp < bp_location + bp_location_count
c7d46a38 12197 && (*locp)->address < old_loc->address)
876fa593 12198 locp++;
c7d46a38
PA
12199
12200 for (loc2p = locp;
12201 (loc2p < bp_location + bp_location_count
12202 && (*loc2p)->address == old_loc->address);
12203 loc2p++)
12204 {
b775012e
LM
12205 /* Check if this is a new/duplicated location or a duplicated
12206 location that had its condition modified. If so, we want to send
12207 its condition to the target if evaluation of conditions is taking
12208 place there. */
12209 if ((*loc2p)->condition_changed == condition_modified
12210 && (last_addr != old_loc->address
12211 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12212 {
b775012e
LM
12213 force_breakpoint_reinsertion (*loc2p);
12214 last_pspace_num = old_loc->pspace->num;
c7d46a38 12215 }
b775012e
LM
12216
12217 if (*loc2p == old_loc)
12218 found_object = 1;
c7d46a38 12219 }
74960c60 12220
b775012e
LM
12221 /* We have already handled this address, update it so that we don't
12222 have to go through updates again. */
12223 last_addr = old_loc->address;
12224
12225 /* Target-side condition evaluation: Handle deleted locations. */
12226 if (!found_object)
12227 force_breakpoint_reinsertion (old_loc);
12228
4a64f543
MS
12229 /* If this location is no longer present, and inserted, look if
12230 there's maybe a new location at the same address. If so,
12231 mark that one inserted, and don't remove this one. This is
12232 needed so that we don't have a time window where a breakpoint
12233 at certain location is not inserted. */
74960c60 12234
876fa593 12235 if (old_loc->inserted)
0d381245 12236 {
4a64f543
MS
12237 /* If the location is inserted now, we might have to remove
12238 it. */
74960c60 12239
876fa593 12240 if (found_object && should_be_inserted (old_loc))
74960c60 12241 {
4a64f543
MS
12242 /* The location is still present in the location list,
12243 and still should be inserted. Don't do anything. */
20874c92 12244 keep_in_target = 1;
74960c60
VP
12245 }
12246 else
12247 {
b775012e
LM
12248 /* This location still exists, but it won't be kept in the
12249 target since it may have been disabled. We proceed to
12250 remove its target-side condition. */
12251
4a64f543
MS
12252 /* The location is either no longer present, or got
12253 disabled. See if there's another location at the
12254 same address, in which case we don't need to remove
12255 this one from the target. */
876fa593 12256
2bdf28a0 12257 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12258 if (breakpoint_address_is_meaningful (old_loc->owner))
12259 {
876fa593 12260 for (loc2p = locp;
c7d46a38
PA
12261 (loc2p < bp_location + bp_location_count
12262 && (*loc2p)->address == old_loc->address);
876fa593
JK
12263 loc2p++)
12264 {
12265 struct bp_location *loc2 = *loc2p;
12266
2d134ed3 12267 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12268 {
85d721b8
PA
12269 /* Read watchpoint locations are switched to
12270 access watchpoints, if the former are not
12271 supported, but the latter are. */
12272 if (is_hardware_watchpoint (old_loc->owner))
12273 {
12274 gdb_assert (is_hardware_watchpoint (loc2->owner));
12275 loc2->watchpoint_type = old_loc->watchpoint_type;
12276 }
12277
934709f0
PW
12278 /* loc2 is a duplicated location. We need to check
12279 if it should be inserted in case it will be
12280 unduplicated. */
12281 if (loc2 != old_loc
12282 && unduplicated_should_be_inserted (loc2))
c7d46a38 12283 {
934709f0 12284 swap_insertion (old_loc, loc2);
c7d46a38
PA
12285 keep_in_target = 1;
12286 break;
12287 }
876fa593
JK
12288 }
12289 }
12290 }
74960c60
VP
12291 }
12292
20874c92
VP
12293 if (!keep_in_target)
12294 {
876fa593 12295 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12296 {
4a64f543
MS
12297 /* This is just about all we can do. We could keep
12298 this location on the global list, and try to
12299 remove it next time, but there's no particular
12300 reason why we will succeed next time.
20874c92 12301
4a64f543
MS
12302 Note that at this point, old_loc->owner is still
12303 valid, as delete_breakpoint frees the breakpoint
12304 only after calling us. */
3e43a32a
MS
12305 printf_filtered (_("warning: Error removing "
12306 "breakpoint %d\n"),
876fa593 12307 old_loc->owner->number);
20874c92
VP
12308 }
12309 removed = 1;
12310 }
0d381245 12311 }
74960c60
VP
12312
12313 if (!found_object)
1c5cfe86 12314 {
db82e815 12315 if (removed && non_stop
1cf4d951 12316 && need_moribund_for_location_type (old_loc))
20874c92 12317 {
db82e815
PA
12318 /* This location was removed from the target. In
12319 non-stop mode, a race condition is possible where
12320 we've removed a breakpoint, but stop events for that
12321 breakpoint are already queued and will arrive later.
12322 We apply an heuristic to be able to distinguish such
12323 SIGTRAPs from other random SIGTRAPs: we keep this
12324 breakpoint location for a bit, and will retire it
12325 after we see some number of events. The theory here
12326 is that reporting of events should, "on the average",
12327 be fair, so after a while we'll see events from all
12328 threads that have anything of interest, and no longer
12329 need to keep this breakpoint location around. We
12330 don't hold locations forever so to reduce chances of
12331 mistaking a non-breakpoint SIGTRAP for a breakpoint
12332 SIGTRAP.
12333
12334 The heuristic failing can be disastrous on
12335 decr_pc_after_break targets.
12336
12337 On decr_pc_after_break targets, like e.g., x86-linux,
12338 if we fail to recognize a late breakpoint SIGTRAP,
12339 because events_till_retirement has reached 0 too
12340 soon, we'll fail to do the PC adjustment, and report
12341 a random SIGTRAP to the user. When the user resumes
12342 the inferior, it will most likely immediately crash
2dec564e 12343 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12344 corrupted, because of being resumed e.g., in the
12345 middle of a multi-byte instruction, or skipped a
12346 one-byte instruction. This was actually seen happen
12347 on native x86-linux, and should be less rare on
12348 targets that do not support new thread events, like
12349 remote, due to the heuristic depending on
12350 thread_count.
12351
12352 Mistaking a random SIGTRAP for a breakpoint trap
12353 causes similar symptoms (PC adjustment applied when
12354 it shouldn't), but then again, playing with SIGTRAPs
12355 behind the debugger's back is asking for trouble.
12356
12357 Since hardware watchpoint traps are always
12358 distinguishable from other traps, so we don't need to
12359 apply keep hardware watchpoint moribund locations
12360 around. We simply always ignore hardware watchpoint
12361 traps we can no longer explain. */
12362
876fa593
JK
12363 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12364 old_loc->owner = NULL;
20874c92 12365
876fa593 12366 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12367 }
12368 else
f431efe5
PA
12369 {
12370 old_loc->owner = NULL;
12371 decref_bp_location (&old_loc);
12372 }
20874c92 12373 }
74960c60 12374 }
1c5cfe86 12375
348d480f
PA
12376 /* Rescan breakpoints at the same address and section, marking the
12377 first one as "first" and any others as "duplicates". This is so
12378 that the bpt instruction is only inserted once. If we have a
12379 permanent breakpoint at the same place as BPT, make that one the
12380 official one, and the rest as duplicates. Permanent breakpoints
12381 are sorted first for the same address.
12382
12383 Do the same for hardware watchpoints, but also considering the
12384 watchpoint's type (regular/access/read) and length. */
12385
12386 bp_loc_first = NULL;
12387 wp_loc_first = NULL;
12388 awp_loc_first = NULL;
12389 rwp_loc_first = NULL;
12390 ALL_BP_LOCATIONS (loc, locp)
12391 {
12392 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12393 non-NULL. */
348d480f 12394 struct bp_location **loc_first_p;
d3fbdd86 12395 b = loc->owner;
348d480f 12396
6f380991 12397 if (!unduplicated_should_be_inserted (loc)
348d480f 12398 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12399 /* Don't detect duplicate for tracepoint locations because they are
12400 never duplicated. See the comments in field `duplicate' of
12401 `struct bp_location'. */
348d480f 12402 || is_tracepoint (b))
b775012e
LM
12403 {
12404 /* Clear the condition modification flag. */
12405 loc->condition_changed = condition_unchanged;
12406 continue;
12407 }
348d480f 12408
348d480f
PA
12409 if (b->type == bp_hardware_watchpoint)
12410 loc_first_p = &wp_loc_first;
12411 else if (b->type == bp_read_watchpoint)
12412 loc_first_p = &rwp_loc_first;
12413 else if (b->type == bp_access_watchpoint)
12414 loc_first_p = &awp_loc_first;
12415 else
12416 loc_first_p = &bp_loc_first;
12417
12418 if (*loc_first_p == NULL
12419 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12420 || !breakpoint_locations_match (loc, *loc_first_p))
12421 {
12422 *loc_first_p = loc;
12423 loc->duplicate = 0;
b775012e
LM
12424
12425 if (is_breakpoint (loc->owner) && loc->condition_changed)
12426 {
12427 loc->needs_update = 1;
12428 /* Clear the condition modification flag. */
12429 loc->condition_changed = condition_unchanged;
12430 }
348d480f
PA
12431 continue;
12432 }
12433
934709f0
PW
12434
12435 /* This and the above ensure the invariant that the first location
12436 is not duplicated, and is the inserted one.
12437 All following are marked as duplicated, and are not inserted. */
12438 if (loc->inserted)
12439 swap_insertion (loc, *loc_first_p);
348d480f
PA
12440 loc->duplicate = 1;
12441
b775012e
LM
12442 /* Clear the condition modification flag. */
12443 loc->condition_changed = condition_unchanged;
348d480f
PA
12444 }
12445
a25a5a45 12446 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12447 {
04086b45 12448 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12449 insert_breakpoint_locations ();
12450 else
12451 {
44702360
PA
12452 /* Even though the caller told us to not insert new
12453 locations, we may still need to update conditions on the
12454 target's side of breakpoints that were already inserted
12455 if the target is evaluating breakpoint conditions. We
b775012e
LM
12456 only update conditions for locations that are marked
12457 "needs_update". */
12458 update_inserted_breakpoint_locations ();
12459 }
12460 }
348d480f 12461
04086b45 12462 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12463 download_tracepoint_locations ();
12464
348d480f
PA
12465 do_cleanups (cleanups);
12466}
12467
12468void
12469breakpoint_retire_moribund (void)
12470{
12471 struct bp_location *loc;
12472 int ix;
12473
12474 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12475 if (--(loc->events_till_retirement) == 0)
12476 {
12477 decref_bp_location (&loc);
12478 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12479 --ix;
12480 }
12481}
12482
12483static void
44702360 12484update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12485{
348d480f 12486
492d29ea
PA
12487 TRY
12488 {
12489 update_global_location_list (insert_mode);
12490 }
12491 CATCH (e, RETURN_MASK_ERROR)
12492 {
12493 }
12494 END_CATCH
348d480f
PA
12495}
12496
12497/* Clear BKP from a BPS. */
12498
12499static void
12500bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12501{
12502 bpstat bs;
12503
12504 for (bs = bps; bs; bs = bs->next)
12505 if (bs->breakpoint_at == bpt)
12506 {
12507 bs->breakpoint_at = NULL;
12508 bs->old_val = NULL;
12509 /* bs->commands will be freed later. */
12510 }
12511}
12512
12513/* Callback for iterate_over_threads. */
12514static int
12515bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12516{
12517 struct breakpoint *bpt = data;
12518
12519 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12520 return 0;
12521}
12522
12523/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12524 callbacks. */
12525
12526static void
12527say_where (struct breakpoint *b)
12528{
12529 struct value_print_options opts;
12530
12531 get_user_print_options (&opts);
12532
12533 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12534 single string. */
12535 if (b->loc == NULL)
12536 {
12537 printf_filtered (_(" (%s) pending."), b->addr_string);
12538 }
12539 else
12540 {
2f202fde 12541 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12542 {
12543 printf_filtered (" at ");
12544 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12545 gdb_stdout);
12546 }
2f202fde 12547 if (b->loc->symtab != NULL)
f8eba3c6
TT
12548 {
12549 /* If there is a single location, we can print the location
12550 more nicely. */
12551 if (b->loc->next == NULL)
12552 printf_filtered (": file %s, line %d.",
05cba821
JK
12553 symtab_to_filename_for_display (b->loc->symtab),
12554 b->loc->line_number);
f8eba3c6
TT
12555 else
12556 /* This is not ideal, but each location may have a
12557 different file name, and this at least reflects the
12558 real situation somewhat. */
12559 printf_filtered (": %s.", b->addr_string);
12560 }
348d480f
PA
12561
12562 if (b->loc->next)
12563 {
12564 struct bp_location *loc = b->loc;
12565 int n = 0;
12566 for (; loc; loc = loc->next)
12567 ++n;
12568 printf_filtered (" (%d locations)", n);
12569 }
12570 }
12571}
12572
348d480f
PA
12573/* Default bp_location_ops methods. */
12574
12575static void
12576bp_location_dtor (struct bp_location *self)
12577{
12578 xfree (self->cond);
b775012e
LM
12579 if (self->cond_bytecode)
12580 free_agent_expr (self->cond_bytecode);
348d480f 12581 xfree (self->function_name);
8b4f3082
PA
12582
12583 VEC_free (agent_expr_p, self->target_info.conditions);
12584 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12585}
12586
12587static const struct bp_location_ops bp_location_ops =
12588{
12589 bp_location_dtor
12590};
12591
2060206e
PA
12592/* Default breakpoint_ops methods all breakpoint_ops ultimately
12593 inherit from. */
348d480f 12594
2060206e
PA
12595static void
12596base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12597{
12598 decref_counted_command_line (&self->commands);
12599 xfree (self->cond_string);
fb81d016 12600 xfree (self->extra_string);
348d480f 12601 xfree (self->addr_string);
f8eba3c6 12602 xfree (self->filter);
348d480f 12603 xfree (self->addr_string_range_end);
348d480f
PA
12604}
12605
2060206e
PA
12606static struct bp_location *
12607base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12608{
12609 struct bp_location *loc;
12610
12611 loc = XNEW (struct bp_location);
12612 init_bp_location (loc, &bp_location_ops, self);
12613 return loc;
12614}
12615
2060206e
PA
12616static void
12617base_breakpoint_re_set (struct breakpoint *b)
12618{
12619 /* Nothing to re-set. */
12620}
12621
12622#define internal_error_pure_virtual_called() \
12623 gdb_assert_not_reached ("pure virtual function called")
12624
12625static int
12626base_breakpoint_insert_location (struct bp_location *bl)
12627{
12628 internal_error_pure_virtual_called ();
12629}
12630
12631static int
12632base_breakpoint_remove_location (struct bp_location *bl)
12633{
12634 internal_error_pure_virtual_called ();
12635}
12636
12637static int
12638base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12639 struct address_space *aspace,
09ac7c10
TT
12640 CORE_ADDR bp_addr,
12641 const struct target_waitstatus *ws)
2060206e
PA
12642{
12643 internal_error_pure_virtual_called ();
12644}
12645
12646static void
12647base_breakpoint_check_status (bpstat bs)
12648{
12649 /* Always stop. */
12650}
12651
12652/* A "works_in_software_mode" breakpoint_ops method that just internal
12653 errors. */
12654
12655static int
12656base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12657{
12658 internal_error_pure_virtual_called ();
12659}
12660
12661/* A "resources_needed" breakpoint_ops method that just internal
12662 errors. */
12663
12664static int
12665base_breakpoint_resources_needed (const struct bp_location *bl)
12666{
12667 internal_error_pure_virtual_called ();
12668}
12669
12670static enum print_stop_action
12671base_breakpoint_print_it (bpstat bs)
12672{
12673 internal_error_pure_virtual_called ();
12674}
12675
12676static void
12677base_breakpoint_print_one_detail (const struct breakpoint *self,
12678 struct ui_out *uiout)
12679{
12680 /* nothing */
12681}
12682
12683static void
12684base_breakpoint_print_mention (struct breakpoint *b)
12685{
12686 internal_error_pure_virtual_called ();
12687}
12688
12689static void
12690base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12691{
12692 internal_error_pure_virtual_called ();
12693}
12694
983af33b
SDJ
12695static void
12696base_breakpoint_create_sals_from_address (char **arg,
12697 struct linespec_result *canonical,
12698 enum bptype type_wanted,
12699 char *addr_start,
12700 char **copy_arg)
12701{
12702 internal_error_pure_virtual_called ();
12703}
12704
12705static void
12706base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12707 struct linespec_result *c,
983af33b 12708 char *cond_string,
e7e0cddf 12709 char *extra_string,
983af33b
SDJ
12710 enum bptype type_wanted,
12711 enum bpdisp disposition,
12712 int thread,
12713 int task, int ignore_count,
12714 const struct breakpoint_ops *o,
12715 int from_tty, int enabled,
44f238bb 12716 int internal, unsigned flags)
983af33b
SDJ
12717{
12718 internal_error_pure_virtual_called ();
12719}
12720
12721static void
12722base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12723 struct symtabs_and_lines *sals)
12724{
12725 internal_error_pure_virtual_called ();
12726}
12727
ab04a2af
TT
12728/* The default 'explains_signal' method. */
12729
47591c29 12730static int
427cd150 12731base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12732{
47591c29 12733 return 1;
ab04a2af
TT
12734}
12735
9d6e6e84
HZ
12736/* The default "after_condition_true" method. */
12737
12738static void
12739base_breakpoint_after_condition_true (struct bpstats *bs)
12740{
12741 /* Nothing to do. */
12742}
12743
ab04a2af 12744struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
12745{
12746 base_breakpoint_dtor,
12747 base_breakpoint_allocate_location,
12748 base_breakpoint_re_set,
12749 base_breakpoint_insert_location,
12750 base_breakpoint_remove_location,
12751 base_breakpoint_breakpoint_hit,
12752 base_breakpoint_check_status,
12753 base_breakpoint_resources_needed,
12754 base_breakpoint_works_in_software_mode,
12755 base_breakpoint_print_it,
12756 NULL,
12757 base_breakpoint_print_one_detail,
12758 base_breakpoint_print_mention,
983af33b
SDJ
12759 base_breakpoint_print_recreate,
12760 base_breakpoint_create_sals_from_address,
12761 base_breakpoint_create_breakpoints_sal,
12762 base_breakpoint_decode_linespec,
9d6e6e84
HZ
12763 base_breakpoint_explains_signal,
12764 base_breakpoint_after_condition_true,
2060206e
PA
12765};
12766
12767/* Default breakpoint_ops methods. */
12768
12769static void
348d480f
PA
12770bkpt_re_set (struct breakpoint *b)
12771{
06edf0c0
PA
12772 /* FIXME: is this still reachable? */
12773 if (b->addr_string == NULL)
12774 {
12775 /* Anything without a string can't be re-set. */
348d480f 12776 delete_breakpoint (b);
06edf0c0 12777 return;
348d480f 12778 }
06edf0c0
PA
12779
12780 breakpoint_re_set_default (b);
348d480f
PA
12781}
12782
2060206e 12783static int
348d480f
PA
12784bkpt_insert_location (struct bp_location *bl)
12785{
12786 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12787 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12788 else
7c16b83e 12789 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12790}
12791
2060206e 12792static int
348d480f
PA
12793bkpt_remove_location (struct bp_location *bl)
12794{
12795 if (bl->loc_type == bp_loc_hardware_breakpoint)
12796 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12797 else
7c16b83e 12798 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12799}
12800
2060206e 12801static int
348d480f 12802bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12803 struct address_space *aspace, CORE_ADDR bp_addr,
12804 const struct target_waitstatus *ws)
348d480f 12805{
09ac7c10 12806 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12807 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12808 return 0;
12809
348d480f
PA
12810 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12811 aspace, bp_addr))
12812 return 0;
12813
12814 if (overlay_debugging /* unmapped overlay section */
12815 && section_is_overlay (bl->section)
12816 && !section_is_mapped (bl->section))
12817 return 0;
12818
12819 return 1;
12820}
12821
cd1608cc
PA
12822static int
12823dprintf_breakpoint_hit (const struct bp_location *bl,
12824 struct address_space *aspace, CORE_ADDR bp_addr,
12825 const struct target_waitstatus *ws)
12826{
12827 if (dprintf_style == dprintf_style_agent
12828 && target_can_run_breakpoint_commands ())
12829 {
12830 /* An agent-style dprintf never causes a stop. If we see a trap
12831 for this address it must be for a breakpoint that happens to
12832 be set at the same address. */
12833 return 0;
12834 }
12835
12836 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12837}
12838
2060206e 12839static int
348d480f
PA
12840bkpt_resources_needed (const struct bp_location *bl)
12841{
12842 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12843
12844 return 1;
12845}
12846
2060206e 12847static enum print_stop_action
348d480f
PA
12848bkpt_print_it (bpstat bs)
12849{
348d480f
PA
12850 struct breakpoint *b;
12851 const struct bp_location *bl;
001c8c33 12852 int bp_temp;
79a45e25 12853 struct ui_out *uiout = current_uiout;
348d480f
PA
12854
12855 gdb_assert (bs->bp_location_at != NULL);
12856
12857 bl = bs->bp_location_at;
12858 b = bs->breakpoint_at;
12859
001c8c33
PA
12860 bp_temp = b->disposition == disp_del;
12861 if (bl->address != bl->requested_address)
12862 breakpoint_adjustment_warning (bl->requested_address,
12863 bl->address,
12864 b->number, 1);
12865 annotate_breakpoint (b->number);
12866 if (bp_temp)
12867 ui_out_text (uiout, "\nTemporary breakpoint ");
12868 else
12869 ui_out_text (uiout, "\nBreakpoint ");
12870 if (ui_out_is_mi_like_p (uiout))
348d480f 12871 {
001c8c33
PA
12872 ui_out_field_string (uiout, "reason",
12873 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12874 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 12875 }
001c8c33
PA
12876 ui_out_field_int (uiout, "bkptno", b->number);
12877 ui_out_text (uiout, ", ");
06edf0c0 12878
001c8c33 12879 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12880}
12881
2060206e 12882static void
06edf0c0
PA
12883bkpt_print_mention (struct breakpoint *b)
12884{
79a45e25 12885 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
12886 return;
12887
12888 switch (b->type)
12889 {
12890 case bp_breakpoint:
12891 case bp_gnu_ifunc_resolver:
12892 if (b->disposition == disp_del)
12893 printf_filtered (_("Temporary breakpoint"));
12894 else
12895 printf_filtered (_("Breakpoint"));
12896 printf_filtered (_(" %d"), b->number);
12897 if (b->type == bp_gnu_ifunc_resolver)
12898 printf_filtered (_(" at gnu-indirect-function resolver"));
12899 break;
12900 case bp_hardware_breakpoint:
12901 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12902 break;
e7e0cddf
SS
12903 case bp_dprintf:
12904 printf_filtered (_("Dprintf %d"), b->number);
12905 break;
06edf0c0
PA
12906 }
12907
12908 say_where (b);
12909}
12910
2060206e 12911static void
06edf0c0
PA
12912bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12913{
12914 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12915 fprintf_unfiltered (fp, "tbreak");
12916 else if (tp->type == bp_breakpoint)
12917 fprintf_unfiltered (fp, "break");
12918 else if (tp->type == bp_hardware_breakpoint
12919 && tp->disposition == disp_del)
12920 fprintf_unfiltered (fp, "thbreak");
12921 else if (tp->type == bp_hardware_breakpoint)
12922 fprintf_unfiltered (fp, "hbreak");
12923 else
12924 internal_error (__FILE__, __LINE__,
12925 _("unhandled breakpoint type %d"), (int) tp->type);
12926
2060206e 12927 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 12928 print_recreate_thread (tp, fp);
06edf0c0
PA
12929}
12930
983af33b
SDJ
12931static void
12932bkpt_create_sals_from_address (char **arg,
12933 struct linespec_result *canonical,
12934 enum bptype type_wanted,
12935 char *addr_start, char **copy_arg)
12936{
12937 create_sals_from_address_default (arg, canonical, type_wanted,
12938 addr_start, copy_arg);
12939}
12940
12941static void
12942bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12943 struct linespec_result *canonical,
983af33b 12944 char *cond_string,
e7e0cddf 12945 char *extra_string,
983af33b
SDJ
12946 enum bptype type_wanted,
12947 enum bpdisp disposition,
12948 int thread,
12949 int task, int ignore_count,
12950 const struct breakpoint_ops *ops,
12951 int from_tty, int enabled,
44f238bb 12952 int internal, unsigned flags)
983af33b 12953{
023fa29b 12954 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
12955 cond_string, extra_string,
12956 type_wanted,
983af33b
SDJ
12957 disposition, thread, task,
12958 ignore_count, ops, from_tty,
44f238bb 12959 enabled, internal, flags);
983af33b
SDJ
12960}
12961
12962static void
12963bkpt_decode_linespec (struct breakpoint *b, char **s,
12964 struct symtabs_and_lines *sals)
12965{
12966 decode_linespec_default (b, s, sals);
12967}
12968
06edf0c0
PA
12969/* Virtual table for internal breakpoints. */
12970
12971static void
12972internal_bkpt_re_set (struct breakpoint *b)
12973{
12974 switch (b->type)
12975 {
12976 /* Delete overlay event and longjmp master breakpoints; they
12977 will be reset later by breakpoint_re_set. */
12978 case bp_overlay_event:
12979 case bp_longjmp_master:
12980 case bp_std_terminate_master:
12981 case bp_exception_master:
12982 delete_breakpoint (b);
12983 break;
12984
12985 /* This breakpoint is special, it's set up when the inferior
12986 starts and we really don't want to touch it. */
12987 case bp_shlib_event:
12988
12989 /* Like bp_shlib_event, this breakpoint type is special. Once
12990 it is set up, we do not want to touch it. */
12991 case bp_thread_event:
12992 break;
12993 }
12994}
12995
12996static void
12997internal_bkpt_check_status (bpstat bs)
12998{
a9b3a50f
PA
12999 if (bs->breakpoint_at->type == bp_shlib_event)
13000 {
13001 /* If requested, stop when the dynamic linker notifies GDB of
13002 events. This allows the user to get control and place
13003 breakpoints in initializer routines for dynamically loaded
13004 objects (among other things). */
13005 bs->stop = stop_on_solib_events;
13006 bs->print = stop_on_solib_events;
13007 }
13008 else
13009 bs->stop = 0;
06edf0c0
PA
13010}
13011
13012static enum print_stop_action
13013internal_bkpt_print_it (bpstat bs)
13014{
06edf0c0 13015 struct breakpoint *b;
06edf0c0 13016
06edf0c0
PA
13017 b = bs->breakpoint_at;
13018
06edf0c0
PA
13019 switch (b->type)
13020 {
348d480f
PA
13021 case bp_shlib_event:
13022 /* Did we stop because the user set the stop_on_solib_events
13023 variable? (If so, we report this as a generic, "Stopped due
13024 to shlib event" message.) */
edcc5120 13025 print_solib_event (0);
348d480f
PA
13026 break;
13027
13028 case bp_thread_event:
13029 /* Not sure how we will get here.
13030 GDB should not stop for these breakpoints. */
13031 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13032 break;
13033
13034 case bp_overlay_event:
13035 /* By analogy with the thread event, GDB should not stop for these. */
13036 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13037 break;
13038
13039 case bp_longjmp_master:
13040 /* These should never be enabled. */
13041 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13042 break;
13043
13044 case bp_std_terminate_master:
13045 /* These should never be enabled. */
13046 printf_filtered (_("std::terminate Master Breakpoint: "
13047 "gdb should not stop!\n"));
348d480f
PA
13048 break;
13049
13050 case bp_exception_master:
13051 /* These should never be enabled. */
13052 printf_filtered (_("Exception Master Breakpoint: "
13053 "gdb should not stop!\n"));
06edf0c0
PA
13054 break;
13055 }
13056
001c8c33 13057 return PRINT_NOTHING;
06edf0c0
PA
13058}
13059
13060static void
13061internal_bkpt_print_mention (struct breakpoint *b)
13062{
13063 /* Nothing to mention. These breakpoints are internal. */
13064}
13065
06edf0c0
PA
13066/* Virtual table for momentary breakpoints */
13067
13068static void
13069momentary_bkpt_re_set (struct breakpoint *b)
13070{
13071 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13072 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13073 Otherwise these should have been blown away via the cleanup chain
13074 or by breakpoint_init_inferior when we rerun the executable. */
13075}
13076
13077static void
13078momentary_bkpt_check_status (bpstat bs)
13079{
13080 /* Nothing. The point of these breakpoints is causing a stop. */
13081}
13082
13083static enum print_stop_action
13084momentary_bkpt_print_it (bpstat bs)
13085{
79a45e25
PA
13086 struct ui_out *uiout = current_uiout;
13087
001c8c33 13088 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13089 {
001c8c33 13090 struct breakpoint *b = bs->breakpoint_at;
348d480f 13091
001c8c33
PA
13092 switch (b->type)
13093 {
13094 case bp_finish:
13095 ui_out_field_string
13096 (uiout, "reason",
13097 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13098 break;
348d480f 13099
001c8c33
PA
13100 case bp_until:
13101 ui_out_field_string
13102 (uiout, "reason",
13103 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13104 break;
13105 }
348d480f
PA
13106 }
13107
001c8c33 13108 return PRINT_UNKNOWN;
348d480f
PA
13109}
13110
06edf0c0
PA
13111static void
13112momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13113{
06edf0c0 13114 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13115}
13116
e2e4d78b
JK
13117/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13118
13119 It gets cleared already on the removal of the first one of such placed
13120 breakpoints. This is OK as they get all removed altogether. */
13121
13122static void
13123longjmp_bkpt_dtor (struct breakpoint *self)
13124{
13125 struct thread_info *tp = find_thread_id (self->thread);
13126
13127 if (tp)
13128 tp->initiating_frame = null_frame_id;
13129
13130 momentary_breakpoint_ops.dtor (self);
13131}
13132
55aa24fb
SDJ
13133/* Specific methods for probe breakpoints. */
13134
13135static int
13136bkpt_probe_insert_location (struct bp_location *bl)
13137{
13138 int v = bkpt_insert_location (bl);
13139
13140 if (v == 0)
13141 {
13142 /* The insertion was successful, now let's set the probe's semaphore
13143 if needed. */
0ea5cda8
SDJ
13144 if (bl->probe.probe->pops->set_semaphore != NULL)
13145 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13146 bl->probe.objfile,
13147 bl->gdbarch);
55aa24fb
SDJ
13148 }
13149
13150 return v;
13151}
13152
13153static int
13154bkpt_probe_remove_location (struct bp_location *bl)
13155{
13156 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13157 if (bl->probe.probe->pops->clear_semaphore != NULL)
13158 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13159 bl->probe.objfile,
13160 bl->gdbarch);
55aa24fb
SDJ
13161
13162 return bkpt_remove_location (bl);
13163}
13164
13165static void
13166bkpt_probe_create_sals_from_address (char **arg,
13167 struct linespec_result *canonical,
13168 enum bptype type_wanted,
13169 char *addr_start, char **copy_arg)
13170{
13171 struct linespec_sals lsal;
13172
13173 lsal.sals = parse_probes (arg, canonical);
13174
13175 *copy_arg = xstrdup (canonical->addr_string);
13176 lsal.canonical = xstrdup (*copy_arg);
13177
13178 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13179}
13180
13181static void
13182bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13183 struct symtabs_and_lines *sals)
13184{
13185 *sals = parse_probes (s, NULL);
13186 if (!sals->sals)
13187 error (_("probe not found"));
13188}
13189
348d480f 13190/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13191
348d480f
PA
13192static void
13193tracepoint_re_set (struct breakpoint *b)
13194{
13195 breakpoint_re_set_default (b);
13196}
876fa593 13197
348d480f
PA
13198static int
13199tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13200 struct address_space *aspace, CORE_ADDR bp_addr,
13201 const struct target_waitstatus *ws)
348d480f
PA
13202{
13203 /* By definition, the inferior does not report stops at
13204 tracepoints. */
13205 return 0;
74960c60
VP
13206}
13207
13208static void
348d480f
PA
13209tracepoint_print_one_detail (const struct breakpoint *self,
13210 struct ui_out *uiout)
74960c60 13211{
d9b3f62e
PA
13212 struct tracepoint *tp = (struct tracepoint *) self;
13213 if (tp->static_trace_marker_id)
348d480f
PA
13214 {
13215 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13216
348d480f
PA
13217 ui_out_text (uiout, "\tmarker id is ");
13218 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13219 tp->static_trace_marker_id);
348d480f
PA
13220 ui_out_text (uiout, "\n");
13221 }
0d381245
VP
13222}
13223
a474d7c2 13224static void
348d480f 13225tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13226{
79a45e25 13227 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13228 return;
cc59ec59 13229
348d480f
PA
13230 switch (b->type)
13231 {
13232 case bp_tracepoint:
13233 printf_filtered (_("Tracepoint"));
13234 printf_filtered (_(" %d"), b->number);
13235 break;
13236 case bp_fast_tracepoint:
13237 printf_filtered (_("Fast tracepoint"));
13238 printf_filtered (_(" %d"), b->number);
13239 break;
13240 case bp_static_tracepoint:
13241 printf_filtered (_("Static tracepoint"));
13242 printf_filtered (_(" %d"), b->number);
13243 break;
13244 default:
13245 internal_error (__FILE__, __LINE__,
13246 _("unhandled tracepoint type %d"), (int) b->type);
13247 }
13248
13249 say_where (b);
a474d7c2
PA
13250}
13251
348d480f 13252static void
d9b3f62e 13253tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13254{
d9b3f62e
PA
13255 struct tracepoint *tp = (struct tracepoint *) self;
13256
13257 if (self->type == bp_fast_tracepoint)
348d480f 13258 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13259 if (self->type == bp_static_tracepoint)
348d480f 13260 fprintf_unfiltered (fp, "strace");
d9b3f62e 13261 else if (self->type == bp_tracepoint)
348d480f
PA
13262 fprintf_unfiltered (fp, "trace");
13263 else
13264 internal_error (__FILE__, __LINE__,
d9b3f62e 13265 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13266
d9b3f62e
PA
13267 fprintf_unfiltered (fp, " %s", self->addr_string);
13268 print_recreate_thread (self, fp);
13269
13270 if (tp->pass_count)
13271 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13272}
13273
983af33b
SDJ
13274static void
13275tracepoint_create_sals_from_address (char **arg,
13276 struct linespec_result *canonical,
13277 enum bptype type_wanted,
13278 char *addr_start, char **copy_arg)
13279{
13280 create_sals_from_address_default (arg, canonical, type_wanted,
13281 addr_start, copy_arg);
13282}
13283
13284static void
13285tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13286 struct linespec_result *canonical,
983af33b 13287 char *cond_string,
e7e0cddf 13288 char *extra_string,
983af33b
SDJ
13289 enum bptype type_wanted,
13290 enum bpdisp disposition,
13291 int thread,
13292 int task, int ignore_count,
13293 const struct breakpoint_ops *ops,
13294 int from_tty, int enabled,
44f238bb 13295 int internal, unsigned flags)
983af33b 13296{
023fa29b 13297 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13298 cond_string, extra_string,
13299 type_wanted,
983af33b
SDJ
13300 disposition, thread, task,
13301 ignore_count, ops, from_tty,
44f238bb 13302 enabled, internal, flags);
983af33b
SDJ
13303}
13304
13305static void
13306tracepoint_decode_linespec (struct breakpoint *b, char **s,
13307 struct symtabs_and_lines *sals)
13308{
13309 decode_linespec_default (b, s, sals);
13310}
13311
2060206e 13312struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13313
55aa24fb
SDJ
13314/* The breakpoint_ops structure to be use on tracepoints placed in a
13315 static probe. */
13316
13317static void
13318tracepoint_probe_create_sals_from_address (char **arg,
13319 struct linespec_result *canonical,
13320 enum bptype type_wanted,
13321 char *addr_start, char **copy_arg)
13322{
13323 /* We use the same method for breakpoint on probes. */
13324 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13325 addr_start, copy_arg);
13326}
13327
13328static void
13329tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13330 struct symtabs_and_lines *sals)
13331{
13332 /* We use the same method for breakpoint on probes. */
13333 bkpt_probe_decode_linespec (b, s, sals);
13334}
13335
13336static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13337
5c2b4418
HZ
13338/* Dprintf breakpoint_ops methods. */
13339
13340static void
13341dprintf_re_set (struct breakpoint *b)
13342{
13343 breakpoint_re_set_default (b);
13344
13345 /* This breakpoint could have been pending, and be resolved now, and
13346 if so, we should now have the extra string. If we don't, the
13347 dprintf was malformed when created, but we couldn't tell because
13348 we can't extract the extra string until the location is
13349 resolved. */
13350 if (b->loc != NULL && b->extra_string == NULL)
13351 error (_("Format string required"));
13352
13353 /* 1 - connect to target 1, that can run breakpoint commands.
13354 2 - create a dprintf, which resolves fine.
13355 3 - disconnect from target 1
13356 4 - connect to target 2, that can NOT run breakpoint commands.
13357
13358 After steps #3/#4, you'll want the dprintf command list to
13359 be updated, because target 1 and 2 may well return different
13360 answers for target_can_run_breakpoint_commands().
13361 Given absence of finer grained resetting, we get to do
13362 it all the time. */
13363 if (b->extra_string != NULL)
13364 update_dprintf_command_list (b);
13365}
13366
2d9442cc
HZ
13367/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13368
13369static void
13370dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13371{
13372 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13373 tp->extra_string);
13374 print_recreate_thread (tp, fp);
13375}
13376
9d6e6e84
HZ
13377/* Implement the "after_condition_true" breakpoint_ops method for
13378 dprintf.
13379
13380 dprintf's are implemented with regular commands in their command
13381 list, but we run the commands here instead of before presenting the
13382 stop to the user, as dprintf's don't actually cause a stop. This
13383 also makes it so that the commands of multiple dprintfs at the same
13384 address are all handled. */
13385
13386static void
13387dprintf_after_condition_true (struct bpstats *bs)
13388{
13389 struct cleanup *old_chain;
13390 struct bpstats tmp_bs = { NULL };
13391 struct bpstats *tmp_bs_p = &tmp_bs;
13392
13393 /* dprintf's never cause a stop. This wasn't set in the
13394 check_status hook instead because that would make the dprintf's
13395 condition not be evaluated. */
13396 bs->stop = 0;
13397
13398 /* Run the command list here. Take ownership of it instead of
13399 copying. We never want these commands to run later in
13400 bpstat_do_actions, if a breakpoint that causes a stop happens to
13401 be set at same address as this dprintf, or even if running the
13402 commands here throws. */
13403 tmp_bs.commands = bs->commands;
13404 bs->commands = NULL;
13405 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13406
13407 bpstat_do_actions_1 (&tmp_bs_p);
13408
13409 /* 'tmp_bs.commands' will usually be NULL by now, but
13410 bpstat_do_actions_1 may return early without processing the whole
13411 list. */
13412 do_cleanups (old_chain);
13413}
13414
983af33b
SDJ
13415/* The breakpoint_ops structure to be used on static tracepoints with
13416 markers (`-m'). */
13417
13418static void
13419strace_marker_create_sals_from_address (char **arg,
13420 struct linespec_result *canonical,
13421 enum bptype type_wanted,
13422 char *addr_start, char **copy_arg)
13423{
13424 struct linespec_sals lsal;
13425
13426 lsal.sals = decode_static_tracepoint_spec (arg);
13427
13428 *copy_arg = savestring (addr_start, *arg - addr_start);
13429
13430 canonical->addr_string = xstrdup (*copy_arg);
13431 lsal.canonical = xstrdup (*copy_arg);
13432 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13433}
13434
13435static void
13436strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13437 struct linespec_result *canonical,
983af33b 13438 char *cond_string,
e7e0cddf 13439 char *extra_string,
983af33b
SDJ
13440 enum bptype type_wanted,
13441 enum bpdisp disposition,
13442 int thread,
13443 int task, int ignore_count,
13444 const struct breakpoint_ops *ops,
13445 int from_tty, int enabled,
44f238bb 13446 int internal, unsigned flags)
983af33b
SDJ
13447{
13448 int i;
52d361e1
YQ
13449 struct linespec_sals *lsal = VEC_index (linespec_sals,
13450 canonical->sals, 0);
983af33b
SDJ
13451
13452 /* If the user is creating a static tracepoint by marker id
13453 (strace -m MARKER_ID), then store the sals index, so that
13454 breakpoint_re_set can try to match up which of the newly
13455 found markers corresponds to this one, and, don't try to
13456 expand multiple locations for each sal, given than SALS
13457 already should contain all sals for MARKER_ID. */
13458
13459 for (i = 0; i < lsal->sals.nelts; ++i)
13460 {
13461 struct symtabs_and_lines expanded;
13462 struct tracepoint *tp;
13463 struct cleanup *old_chain;
13464 char *addr_string;
13465
13466 expanded.nelts = 1;
13467 expanded.sals = &lsal->sals.sals[i];
13468
13469 addr_string = xstrdup (canonical->addr_string);
13470 old_chain = make_cleanup (xfree, addr_string);
13471
13472 tp = XCNEW (struct tracepoint);
13473 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13474 addr_string, NULL,
e7e0cddf
SS
13475 cond_string, extra_string,
13476 type_wanted, disposition,
983af33b 13477 thread, task, ignore_count, ops,
44f238bb 13478 from_tty, enabled, internal, flags,
983af33b
SDJ
13479 canonical->special_display);
13480 /* Given that its possible to have multiple markers with
13481 the same string id, if the user is creating a static
13482 tracepoint by marker id ("strace -m MARKER_ID"), then
13483 store the sals index, so that breakpoint_re_set can
13484 try to match up which of the newly found markers
13485 corresponds to this one */
13486 tp->static_trace_marker_id_idx = i;
13487
13488 install_breakpoint (internal, &tp->base, 0);
13489
13490 discard_cleanups (old_chain);
13491 }
13492}
13493
13494static void
13495strace_marker_decode_linespec (struct breakpoint *b, char **s,
13496 struct symtabs_and_lines *sals)
13497{
13498 struct tracepoint *tp = (struct tracepoint *) b;
13499
13500 *sals = decode_static_tracepoint_spec (s);
13501 if (sals->nelts > tp->static_trace_marker_id_idx)
13502 {
13503 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13504 sals->nelts = 1;
13505 }
13506 else
13507 error (_("marker %s not found"), tp->static_trace_marker_id);
13508}
13509
13510static struct breakpoint_ops strace_marker_breakpoint_ops;
13511
13512static int
13513strace_marker_p (struct breakpoint *b)
13514{
13515 return b->ops == &strace_marker_breakpoint_ops;
13516}
13517
53a5351d 13518/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13519 structures. */
c906108c
SS
13520
13521void
fba45db2 13522delete_breakpoint (struct breakpoint *bpt)
c906108c 13523{
52f0bd74 13524 struct breakpoint *b;
c906108c 13525
8a3fe4f8 13526 gdb_assert (bpt != NULL);
c906108c 13527
4a64f543
MS
13528 /* Has this bp already been deleted? This can happen because
13529 multiple lists can hold pointers to bp's. bpstat lists are
13530 especial culprits.
13531
13532 One example of this happening is a watchpoint's scope bp. When
13533 the scope bp triggers, we notice that the watchpoint is out of
13534 scope, and delete it. We also delete its scope bp. But the
13535 scope bp is marked "auto-deleting", and is already on a bpstat.
13536 That bpstat is then checked for auto-deleting bp's, which are
13537 deleted.
13538
13539 A real solution to this problem might involve reference counts in
13540 bp's, and/or giving them pointers back to their referencing
13541 bpstat's, and teaching delete_breakpoint to only free a bp's
13542 storage when no more references were extent. A cheaper bandaid
13543 was chosen. */
c906108c
SS
13544 if (bpt->type == bp_none)
13545 return;
13546
4a64f543
MS
13547 /* At least avoid this stale reference until the reference counting
13548 of breakpoints gets resolved. */
d0fb5eae 13549 if (bpt->related_breakpoint != bpt)
e5a0a904 13550 {
d0fb5eae 13551 struct breakpoint *related;
3a5c3e22 13552 struct watchpoint *w;
d0fb5eae
JK
13553
13554 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13555 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13556 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13557 w = (struct watchpoint *) bpt;
13558 else
13559 w = NULL;
13560 if (w != NULL)
13561 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13562
13563 /* Unlink bpt from the bpt->related_breakpoint ring. */
13564 for (related = bpt; related->related_breakpoint != bpt;
13565 related = related->related_breakpoint);
13566 related->related_breakpoint = bpt->related_breakpoint;
13567 bpt->related_breakpoint = bpt;
e5a0a904
JK
13568 }
13569
a9634178
TJB
13570 /* watch_command_1 creates a watchpoint but only sets its number if
13571 update_watchpoint succeeds in creating its bp_locations. If there's
13572 a problem in that process, we'll be asked to delete the half-created
13573 watchpoint. In that case, don't announce the deletion. */
13574 if (bpt->number)
13575 observer_notify_breakpoint_deleted (bpt);
c906108c 13576
c906108c
SS
13577 if (breakpoint_chain == bpt)
13578 breakpoint_chain = bpt->next;
13579
c906108c
SS
13580 ALL_BREAKPOINTS (b)
13581 if (b->next == bpt)
c5aa993b
JM
13582 {
13583 b->next = bpt->next;
13584 break;
13585 }
c906108c 13586
f431efe5
PA
13587 /* Be sure no bpstat's are pointing at the breakpoint after it's
13588 been freed. */
13589 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13590 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13591 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13592 commands are associated with the bpstat; if we remove it here,
13593 then the later call to bpstat_do_actions (&stop_bpstat); in
13594 event-top.c won't do anything, and temporary breakpoints with
13595 commands won't work. */
13596
13597 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13598
4a64f543
MS
13599 /* Now that breakpoint is removed from breakpoint list, update the
13600 global location list. This will remove locations that used to
13601 belong to this breakpoint. Do this before freeing the breakpoint
13602 itself, since remove_breakpoint looks at location's owner. It
13603 might be better design to have location completely
13604 self-contained, but it's not the case now. */
44702360 13605 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13606
348d480f 13607 bpt->ops->dtor (bpt);
4a64f543
MS
13608 /* On the chance that someone will soon try again to delete this
13609 same bp, we mark it as deleted before freeing its storage. */
c906108c 13610 bpt->type = bp_none;
b8c9b27d 13611 xfree (bpt);
c906108c
SS
13612}
13613
4d6140d9
AC
13614static void
13615do_delete_breakpoint_cleanup (void *b)
13616{
13617 delete_breakpoint (b);
13618}
13619
13620struct cleanup *
13621make_cleanup_delete_breakpoint (struct breakpoint *b)
13622{
13623 return make_cleanup (do_delete_breakpoint_cleanup, b);
13624}
13625
51be5b68
PA
13626/* Iterator function to call a user-provided callback function once
13627 for each of B and its related breakpoints. */
13628
13629static void
13630iterate_over_related_breakpoints (struct breakpoint *b,
13631 void (*function) (struct breakpoint *,
13632 void *),
13633 void *data)
13634{
13635 struct breakpoint *related;
13636
13637 related = b;
13638 do
13639 {
13640 struct breakpoint *next;
13641
13642 /* FUNCTION may delete RELATED. */
13643 next = related->related_breakpoint;
13644
13645 if (next == related)
13646 {
13647 /* RELATED is the last ring entry. */
13648 function (related, data);
13649
13650 /* FUNCTION may have deleted it, so we'd never reach back to
13651 B. There's nothing left to do anyway, so just break
13652 out. */
13653 break;
13654 }
13655 else
13656 function (related, data);
13657
13658 related = next;
13659 }
13660 while (related != b);
13661}
95a42b64
TT
13662
13663static void
13664do_delete_breakpoint (struct breakpoint *b, void *ignore)
13665{
13666 delete_breakpoint (b);
13667}
13668
51be5b68
PA
13669/* A callback for map_breakpoint_numbers that calls
13670 delete_breakpoint. */
13671
13672static void
13673do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13674{
13675 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13676}
13677
c906108c 13678void
fba45db2 13679delete_command (char *arg, int from_tty)
c906108c 13680{
35df4500 13681 struct breakpoint *b, *b_tmp;
c906108c 13682
ea9365bb
TT
13683 dont_repeat ();
13684
c906108c
SS
13685 if (arg == 0)
13686 {
13687 int breaks_to_delete = 0;
13688
46c6471b
PA
13689 /* Delete all breakpoints if no argument. Do not delete
13690 internal breakpoints, these have to be deleted with an
13691 explicit breakpoint number argument. */
c5aa993b 13692 ALL_BREAKPOINTS (b)
46c6471b 13693 if (user_breakpoint_p (b))
973d738b
DJ
13694 {
13695 breaks_to_delete = 1;
13696 break;
13697 }
c906108c
SS
13698
13699 /* Ask user only if there are some breakpoints to delete. */
13700 if (!from_tty
e2e0b3e5 13701 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13702 {
35df4500 13703 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13704 if (user_breakpoint_p (b))
c5aa993b 13705 delete_breakpoint (b);
c906108c
SS
13706 }
13707 }
13708 else
51be5b68 13709 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13710}
13711
0d381245
VP
13712static int
13713all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 13714{
0d381245 13715 for (; loc; loc = loc->next)
8645ff69
UW
13716 if (!loc->shlib_disabled
13717 && !loc->pspace->executing_startup)
0d381245
VP
13718 return 0;
13719 return 1;
fe3f5fa8
VP
13720}
13721
776592bf
DE
13722/* Subroutine of update_breakpoint_locations to simplify it.
13723 Return non-zero if multiple fns in list LOC have the same name.
13724 Null names are ignored. */
13725
13726static int
13727ambiguous_names_p (struct bp_location *loc)
13728{
13729 struct bp_location *l;
13730 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13731 (int (*) (const void *,
13732 const void *)) streq,
776592bf
DE
13733 NULL, xcalloc, xfree);
13734
13735 for (l = loc; l != NULL; l = l->next)
13736 {
13737 const char **slot;
13738 const char *name = l->function_name;
13739
13740 /* Allow for some names to be NULL, ignore them. */
13741 if (name == NULL)
13742 continue;
13743
13744 slot = (const char **) htab_find_slot (htab, (const void *) name,
13745 INSERT);
4a64f543
MS
13746 /* NOTE: We can assume slot != NULL here because xcalloc never
13747 returns NULL. */
776592bf
DE
13748 if (*slot != NULL)
13749 {
13750 htab_delete (htab);
13751 return 1;
13752 }
13753 *slot = name;
13754 }
13755
13756 htab_delete (htab);
13757 return 0;
13758}
13759
0fb4aa4b
PA
13760/* When symbols change, it probably means the sources changed as well,
13761 and it might mean the static tracepoint markers are no longer at
13762 the same address or line numbers they used to be at last we
13763 checked. Losing your static tracepoints whenever you rebuild is
13764 undesirable. This function tries to resync/rematch gdb static
13765 tracepoints with the markers on the target, for static tracepoints
13766 that have not been set by marker id. Static tracepoint that have
13767 been set by marker id are reset by marker id in breakpoint_re_set.
13768 The heuristic is:
13769
13770 1) For a tracepoint set at a specific address, look for a marker at
13771 the old PC. If one is found there, assume to be the same marker.
13772 If the name / string id of the marker found is different from the
13773 previous known name, assume that means the user renamed the marker
13774 in the sources, and output a warning.
13775
13776 2) For a tracepoint set at a given line number, look for a marker
13777 at the new address of the old line number. If one is found there,
13778 assume to be the same marker. If the name / string id of the
13779 marker found is different from the previous known name, assume that
13780 means the user renamed the marker in the sources, and output a
13781 warning.
13782
13783 3) If a marker is no longer found at the same address or line, it
13784 may mean the marker no longer exists. But it may also just mean
13785 the code changed a bit. Maybe the user added a few lines of code
13786 that made the marker move up or down (in line number terms). Ask
13787 the target for info about the marker with the string id as we knew
13788 it. If found, update line number and address in the matching
13789 static tracepoint. This will get confused if there's more than one
13790 marker with the same ID (possible in UST, although unadvised
13791 precisely because it confuses tools). */
13792
13793static struct symtab_and_line
13794update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13795{
d9b3f62e 13796 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13797 struct static_tracepoint_marker marker;
13798 CORE_ADDR pc;
0fb4aa4b
PA
13799
13800 pc = sal.pc;
13801 if (sal.line)
13802 find_line_pc (sal.symtab, sal.line, &pc);
13803
13804 if (target_static_tracepoint_marker_at (pc, &marker))
13805 {
d9b3f62e 13806 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13807 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13808 b->number,
d9b3f62e 13809 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13810
d9b3f62e
PA
13811 xfree (tp->static_trace_marker_id);
13812 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13813 release_static_tracepoint_marker (&marker);
13814
13815 return sal;
13816 }
13817
13818 /* Old marker wasn't found on target at lineno. Try looking it up
13819 by string ID. */
13820 if (!sal.explicit_pc
13821 && sal.line != 0
13822 && sal.symtab != NULL
d9b3f62e 13823 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13824 {
13825 VEC(static_tracepoint_marker_p) *markers;
13826
13827 markers
d9b3f62e 13828 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13829
13830 if (!VEC_empty(static_tracepoint_marker_p, markers))
13831 {
80e1d417 13832 struct symtab_and_line sal2;
0fb4aa4b 13833 struct symbol *sym;
80e1d417 13834 struct static_tracepoint_marker *tpmarker;
79a45e25 13835 struct ui_out *uiout = current_uiout;
0fb4aa4b 13836
80e1d417 13837 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13838
d9b3f62e 13839 xfree (tp->static_trace_marker_id);
80e1d417 13840 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13841
13842 warning (_("marker for static tracepoint %d (%s) not "
13843 "found at previous line number"),
d9b3f62e 13844 b->number, tp->static_trace_marker_id);
0fb4aa4b 13845
80e1d417 13846 init_sal (&sal2);
0fb4aa4b 13847
80e1d417 13848 sal2.pc = tpmarker->address;
0fb4aa4b 13849
80e1d417
AS
13850 sal2 = find_pc_line (tpmarker->address, 0);
13851 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
13852 ui_out_text (uiout, "Now in ");
13853 if (sym)
13854 {
13855 ui_out_field_string (uiout, "func",
13856 SYMBOL_PRINT_NAME (sym));
13857 ui_out_text (uiout, " at ");
13858 }
05cba821
JK
13859 ui_out_field_string (uiout, "file",
13860 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
13861 ui_out_text (uiout, ":");
13862
13863 if (ui_out_is_mi_like_p (uiout))
13864 {
0b0865da 13865 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13866
f35a17b5 13867 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
13868 }
13869
80e1d417 13870 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
13871 ui_out_text (uiout, "\n");
13872
80e1d417 13873 b->loc->line_number = sal2.line;
2f202fde 13874 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
13875
13876 xfree (b->addr_string);
13877 b->addr_string = xstrprintf ("%s:%d",
05cba821 13878 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 13879 b->loc->line_number);
0fb4aa4b
PA
13880
13881 /* Might be nice to check if function changed, and warn if
13882 so. */
13883
80e1d417 13884 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13885 }
13886 }
13887 return sal;
13888}
13889
8d3788bd
VP
13890/* Returns 1 iff locations A and B are sufficiently same that
13891 we don't need to report breakpoint as changed. */
13892
13893static int
13894locations_are_equal (struct bp_location *a, struct bp_location *b)
13895{
13896 while (a && b)
13897 {
13898 if (a->address != b->address)
13899 return 0;
13900
13901 if (a->shlib_disabled != b->shlib_disabled)
13902 return 0;
13903
13904 if (a->enabled != b->enabled)
13905 return 0;
13906
13907 a = a->next;
13908 b = b->next;
13909 }
13910
13911 if ((a == NULL) != (b == NULL))
13912 return 0;
13913
13914 return 1;
13915}
13916
f1310107
TJB
13917/* Create new breakpoint locations for B (a hardware or software breakpoint)
13918 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
13919 a ranged breakpoint. */
13920
0e30163f 13921void
0d381245 13922update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
13923 struct symtabs_and_lines sals,
13924 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
13925{
13926 int i;
0d381245
VP
13927 struct bp_location *existing_locations = b->loc;
13928
f8eba3c6
TT
13929 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
13930 {
13931 /* Ranged breakpoints have only one start location and one end
13932 location. */
13933 b->enable_state = bp_disabled;
44702360 13934 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
13935 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13936 "multiple locations found\n"),
13937 b->number);
13938 return;
13939 }
f1310107 13940
4a64f543
MS
13941 /* If there's no new locations, and all existing locations are
13942 pending, don't do anything. This optimizes the common case where
13943 all locations are in the same shared library, that was unloaded.
13944 We'd like to retain the location, so that when the library is
13945 loaded again, we don't loose the enabled/disabled status of the
13946 individual locations. */
0d381245 13947 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
13948 return;
13949
fe3f5fa8
VP
13950 b->loc = NULL;
13951
0d381245 13952 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 13953 {
f8eba3c6
TT
13954 struct bp_location *new_loc;
13955
13956 switch_to_program_space_and_thread (sals.sals[i].pspace);
13957
13958 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 13959
0d381245
VP
13960 /* Reparse conditions, they might contain references to the
13961 old symtab. */
13962 if (b->cond_string != NULL)
13963 {
bbc13ae3 13964 const char *s;
fe3f5fa8 13965
0d381245 13966 s = b->cond_string;
492d29ea 13967 TRY
0d381245 13968 {
1bb9788d
TT
13969 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
13970 block_for_pc (sals.sals[i].pc),
0d381245
VP
13971 0);
13972 }
492d29ea 13973 CATCH (e, RETURN_MASK_ERROR)
0d381245 13974 {
3e43a32a
MS
13975 warning (_("failed to reevaluate condition "
13976 "for breakpoint %d: %s"),
0d381245
VP
13977 b->number, e.message);
13978 new_loc->enabled = 0;
13979 }
492d29ea 13980 END_CATCH
0d381245 13981 }
fe3f5fa8 13982
f1310107
TJB
13983 if (sals_end.nelts)
13984 {
13985 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
13986
13987 new_loc->length = end - sals.sals[0].pc + 1;
13988 }
0d381245 13989 }
fe3f5fa8 13990
4a64f543
MS
13991 /* If possible, carry over 'disable' status from existing
13992 breakpoints. */
0d381245
VP
13993 {
13994 struct bp_location *e = existing_locations;
776592bf
DE
13995 /* If there are multiple breakpoints with the same function name,
13996 e.g. for inline functions, comparing function names won't work.
13997 Instead compare pc addresses; this is just a heuristic as things
13998 may have moved, but in practice it gives the correct answer
13999 often enough until a better solution is found. */
14000 int have_ambiguous_names = ambiguous_names_p (b->loc);
14001
0d381245
VP
14002 for (; e; e = e->next)
14003 {
14004 if (!e->enabled && e->function_name)
14005 {
14006 struct bp_location *l = b->loc;
776592bf
DE
14007 if (have_ambiguous_names)
14008 {
14009 for (; l; l = l->next)
f1310107 14010 if (breakpoint_locations_match (e, l))
776592bf
DE
14011 {
14012 l->enabled = 0;
14013 break;
14014 }
14015 }
14016 else
14017 {
14018 for (; l; l = l->next)
14019 if (l->function_name
14020 && strcmp (e->function_name, l->function_name) == 0)
14021 {
14022 l->enabled = 0;
14023 break;
14024 }
14025 }
0d381245
VP
14026 }
14027 }
14028 }
fe3f5fa8 14029
8d3788bd
VP
14030 if (!locations_are_equal (existing_locations, b->loc))
14031 observer_notify_breakpoint_modified (b);
14032
44702360 14033 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14034}
14035
ef23e705
TJB
14036/* Find the SaL locations corresponding to the given ADDR_STRING.
14037 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14038
14039static struct symtabs_and_lines
14040addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14041{
14042 char *s;
02d20e4a 14043 struct symtabs_and_lines sals = {0};
492d29ea 14044 struct gdb_exception exception = exception_none;
ef23e705 14045
983af33b 14046 gdb_assert (b->ops != NULL);
ef23e705 14047 s = addr_string;
ef23e705 14048
492d29ea 14049 TRY
ef23e705 14050 {
983af33b 14051 b->ops->decode_linespec (b, &s, &sals);
ef23e705 14052 }
492d29ea 14053 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14054 {
14055 int not_found_and_ok = 0;
492d29ea
PA
14056
14057 exception = e;
14058
ef23e705
TJB
14059 /* For pending breakpoints, it's expected that parsing will
14060 fail until the right shared library is loaded. User has
14061 already told to create pending breakpoints and don't need
14062 extra messages. If breakpoint is in bp_shlib_disabled
14063 state, then user already saw the message about that
14064 breakpoint being disabled, and don't want to see more
14065 errors. */
58438ac1 14066 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14067 && (b->condition_not_parsed
14068 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14069 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14070 || b->enable_state == bp_disabled))
14071 not_found_and_ok = 1;
14072
14073 if (!not_found_and_ok)
14074 {
14075 /* We surely don't want to warn about the same breakpoint
14076 10 times. One solution, implemented here, is disable
14077 the breakpoint on error. Another solution would be to
14078 have separate 'warning emitted' flag. Since this
14079 happens only when a binary has changed, I don't know
14080 which approach is better. */
14081 b->enable_state = bp_disabled;
14082 throw_exception (e);
14083 }
14084 }
492d29ea 14085 END_CATCH
ef23e705 14086
492d29ea 14087 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14088 {
f8eba3c6 14089 int i;
ef23e705 14090
f8eba3c6
TT
14091 for (i = 0; i < sals.nelts; ++i)
14092 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14093 if (b->condition_not_parsed && s && s[0])
14094 {
ed1d1739
KS
14095 char *cond_string, *extra_string;
14096 int thread, task;
ef23e705
TJB
14097
14098 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14099 &cond_string, &thread, &task,
14100 &extra_string);
ef23e705
TJB
14101 if (cond_string)
14102 b->cond_string = cond_string;
14103 b->thread = thread;
14104 b->task = task;
e7e0cddf
SS
14105 if (extra_string)
14106 b->extra_string = extra_string;
ef23e705
TJB
14107 b->condition_not_parsed = 0;
14108 }
14109
983af33b 14110 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14111 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14112
58438ac1
TT
14113 *found = 1;
14114 }
14115 else
14116 *found = 0;
ef23e705
TJB
14117
14118 return sals;
14119}
14120
348d480f
PA
14121/* The default re_set method, for typical hardware or software
14122 breakpoints. Reevaluate the breakpoint and recreate its
14123 locations. */
14124
14125static void
28010a5d 14126breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14127{
14128 int found;
f1310107 14129 struct symtabs_and_lines sals, sals_end;
ef23e705 14130 struct symtabs_and_lines expanded = {0};
f1310107 14131 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14132
14133 sals = addr_string_to_sals (b, b->addr_string, &found);
14134 if (found)
14135 {
14136 make_cleanup (xfree, sals.sals);
f8eba3c6 14137 expanded = sals;
ef23e705
TJB
14138 }
14139
f1310107
TJB
14140 if (b->addr_string_range_end)
14141 {
14142 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14143 if (found)
14144 {
14145 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14146 expanded_end = sals_end;
f1310107
TJB
14147 }
14148 }
14149
14150 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14151}
14152
983af33b
SDJ
14153/* Default method for creating SALs from an address string. It basically
14154 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14155
14156static void
14157create_sals_from_address_default (char **arg,
14158 struct linespec_result *canonical,
14159 enum bptype type_wanted,
14160 char *addr_start, char **copy_arg)
14161{
14162 parse_breakpoint_sals (arg, canonical);
14163}
14164
14165/* Call create_breakpoints_sal for the given arguments. This is the default
14166 function for the `create_breakpoints_sal' method of
14167 breakpoint_ops. */
14168
14169static void
14170create_breakpoints_sal_default (struct gdbarch *gdbarch,
14171 struct linespec_result *canonical,
983af33b 14172 char *cond_string,
e7e0cddf 14173 char *extra_string,
983af33b
SDJ
14174 enum bptype type_wanted,
14175 enum bpdisp disposition,
14176 int thread,
14177 int task, int ignore_count,
14178 const struct breakpoint_ops *ops,
14179 int from_tty, int enabled,
44f238bb 14180 int internal, unsigned flags)
983af33b
SDJ
14181{
14182 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14183 extra_string,
983af33b
SDJ
14184 type_wanted, disposition,
14185 thread, task, ignore_count, ops, from_tty,
44f238bb 14186 enabled, internal, flags);
983af33b
SDJ
14187}
14188
14189/* Decode the line represented by S by calling decode_line_full. This is the
14190 default function for the `decode_linespec' method of breakpoint_ops. */
14191
14192static void
14193decode_linespec_default (struct breakpoint *b, char **s,
14194 struct symtabs_and_lines *sals)
14195{
14196 struct linespec_result canonical;
14197
14198 init_linespec_result (&canonical);
14199 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14200 (struct symtab *) NULL, 0,
14201 &canonical, multiple_symbols_all,
14202 b->filter);
14203
14204 /* We should get 0 or 1 resulting SALs. */
14205 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14206
14207 if (VEC_length (linespec_sals, canonical.sals) > 0)
14208 {
14209 struct linespec_sals *lsal;
14210
14211 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14212 *sals = lsal->sals;
14213 /* Arrange it so the destructor does not free the
14214 contents. */
14215 lsal->sals.sals = NULL;
14216 }
14217
14218 destroy_linespec_result (&canonical);
14219}
14220
28010a5d
PA
14221/* Prepare the global context for a re-set of breakpoint B. */
14222
14223static struct cleanup *
14224prepare_re_set_context (struct breakpoint *b)
14225{
14226 struct cleanup *cleanups;
14227
14228 input_radix = b->input_radix;
14229 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14230 if (b->pspace != NULL)
14231 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14232 set_language (b->language);
14233
14234 return cleanups;
ef23e705
TJB
14235}
14236
c906108c
SS
14237/* Reset a breakpoint given it's struct breakpoint * BINT.
14238 The value we return ends up being the return value from catch_errors.
14239 Unused in this case. */
14240
14241static int
4efb68b1 14242breakpoint_re_set_one (void *bint)
c906108c 14243{
4a64f543 14244 /* Get past catch_errs. */
53a5351d 14245 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14246 struct cleanup *cleanups;
c906108c 14247
348d480f
PA
14248 cleanups = prepare_re_set_context (b);
14249 b->ops->re_set (b);
14250 do_cleanups (cleanups);
c906108c
SS
14251 return 0;
14252}
14253
69de3c6a 14254/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14255void
69de3c6a 14256breakpoint_re_set (void)
c906108c 14257{
35df4500 14258 struct breakpoint *b, *b_tmp;
c906108c
SS
14259 enum language save_language;
14260 int save_input_radix;
6c95b8df 14261 struct cleanup *old_chain;
c5aa993b 14262
c906108c
SS
14263 save_language = current_language->la_language;
14264 save_input_radix = input_radix;
6c95b8df
PA
14265 old_chain = save_current_program_space ();
14266
35df4500 14267 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14268 {
4a64f543 14269 /* Format possible error msg. */
fe3f5fa8 14270 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14271 b->number);
14272 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14273 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14274 do_cleanups (cleanups);
c5aa993b 14275 }
c906108c
SS
14276 set_language (save_language);
14277 input_radix = save_input_radix;
e62c965a 14278
0756c555 14279 jit_breakpoint_re_set ();
4efc6507 14280
6c95b8df
PA
14281 do_cleanups (old_chain);
14282
af02033e
PP
14283 create_overlay_event_breakpoint ();
14284 create_longjmp_master_breakpoint ();
14285 create_std_terminate_master_breakpoint ();
186c406b 14286 create_exception_master_breakpoint ();
c906108c
SS
14287}
14288\f
c906108c
SS
14289/* Reset the thread number of this breakpoint:
14290
14291 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14292 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14293void
fba45db2 14294breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14295{
14296 if (b->thread != -1)
14297 {
39f77062
KB
14298 if (in_thread_list (inferior_ptid))
14299 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14300
14301 /* We're being called after following a fork. The new fork is
14302 selected as current, and unless this was a vfork will have a
14303 different program space from the original thread. Reset that
14304 as well. */
14305 b->loc->pspace = current_program_space;
c906108c
SS
14306 }
14307}
14308
03ac34d5
MS
14309/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14310 If from_tty is nonzero, it prints a message to that effect,
14311 which ends with a period (no newline). */
14312
c906108c 14313void
fba45db2 14314set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14315{
52f0bd74 14316 struct breakpoint *b;
c906108c
SS
14317
14318 if (count < 0)
14319 count = 0;
14320
14321 ALL_BREAKPOINTS (b)
14322 if (b->number == bptnum)
c5aa993b 14323 {
d77f58be
SS
14324 if (is_tracepoint (b))
14325 {
14326 if (from_tty && count != 0)
14327 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14328 bptnum);
14329 return;
14330 }
14331
c5aa993b 14332 b->ignore_count = count;
221ea385
KS
14333 if (from_tty)
14334 {
14335 if (count == 0)
3e43a32a
MS
14336 printf_filtered (_("Will stop next time "
14337 "breakpoint %d is reached."),
221ea385
KS
14338 bptnum);
14339 else if (count == 1)
a3f17187 14340 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14341 bptnum);
14342 else
3e43a32a
MS
14343 printf_filtered (_("Will ignore next %d "
14344 "crossings of breakpoint %d."),
221ea385
KS
14345 count, bptnum);
14346 }
8d3788bd 14347 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14348 return;
14349 }
c906108c 14350
8a3fe4f8 14351 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14352}
14353
c906108c
SS
14354/* Command to set ignore-count of breakpoint N to COUNT. */
14355
14356static void
fba45db2 14357ignore_command (char *args, int from_tty)
c906108c
SS
14358{
14359 char *p = args;
52f0bd74 14360 int num;
c906108c
SS
14361
14362 if (p == 0)
e2e0b3e5 14363 error_no_arg (_("a breakpoint number"));
c5aa993b 14364
c906108c 14365 num = get_number (&p);
5c44784c 14366 if (num == 0)
8a3fe4f8 14367 error (_("bad breakpoint number: '%s'"), args);
c906108c 14368 if (*p == 0)
8a3fe4f8 14369 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14370
14371 set_ignore_count (num,
14372 longest_to_int (value_as_long (parse_and_eval (p))),
14373 from_tty);
221ea385
KS
14374 if (from_tty)
14375 printf_filtered ("\n");
c906108c
SS
14376}
14377\f
14378/* Call FUNCTION on each of the breakpoints
14379 whose numbers are given in ARGS. */
14380
14381static void
95a42b64
TT
14382map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14383 void *),
14384 void *data)
c906108c 14385{
52f0bd74
AC
14386 int num;
14387 struct breakpoint *b, *tmp;
11cf8741 14388 int match;
197f0a60 14389 struct get_number_or_range_state state;
c906108c 14390
b9d61307 14391 if (args == 0 || *args == '\0')
e2e0b3e5 14392 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14393
197f0a60
TT
14394 init_number_or_range (&state, args);
14395
14396 while (!state.finished)
c906108c 14397 {
e799154c 14398 const char *p = state.string;
197f0a60 14399
11cf8741 14400 match = 0;
c5aa993b 14401
197f0a60 14402 num = get_number_or_range (&state);
5c44784c 14403 if (num == 0)
c5aa993b 14404 {
8a3fe4f8 14405 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14406 }
14407 else
14408 {
14409 ALL_BREAKPOINTS_SAFE (b, tmp)
14410 if (b->number == num)
14411 {
11cf8741 14412 match = 1;
cdac0397 14413 function (b, data);
11cf8741 14414 break;
5c44784c 14415 }
11cf8741 14416 if (match == 0)
a3f17187 14417 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14418 }
c906108c
SS
14419 }
14420}
14421
0d381245
VP
14422static struct bp_location *
14423find_location_by_number (char *number)
14424{
14425 char *dot = strchr (number, '.');
14426 char *p1;
14427 int bp_num;
14428 int loc_num;
14429 struct breakpoint *b;
14430 struct bp_location *loc;
14431
14432 *dot = '\0';
14433
14434 p1 = number;
197f0a60 14435 bp_num = get_number (&p1);
0d381245
VP
14436 if (bp_num == 0)
14437 error (_("Bad breakpoint number '%s'"), number);
14438
14439 ALL_BREAKPOINTS (b)
14440 if (b->number == bp_num)
14441 {
14442 break;
14443 }
14444
14445 if (!b || b->number != bp_num)
14446 error (_("Bad breakpoint number '%s'"), number);
14447
14448 p1 = dot+1;
197f0a60 14449 loc_num = get_number (&p1);
0d381245
VP
14450 if (loc_num == 0)
14451 error (_("Bad breakpoint location number '%s'"), number);
14452
14453 --loc_num;
14454 loc = b->loc;
14455 for (;loc_num && loc; --loc_num, loc = loc->next)
14456 ;
14457 if (!loc)
14458 error (_("Bad breakpoint location number '%s'"), dot+1);
14459
14460 return loc;
14461}
14462
14463
1900040c
MS
14464/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14465 If from_tty is nonzero, it prints a message to that effect,
14466 which ends with a period (no newline). */
14467
c906108c 14468void
fba45db2 14469disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14470{
14471 /* Never disable a watchpoint scope breakpoint; we want to
14472 hit them when we leave scope so we can delete both the
14473 watchpoint and its scope breakpoint at that time. */
14474 if (bpt->type == bp_watchpoint_scope)
14475 return;
14476
b5de0fa7 14477 bpt->enable_state = bp_disabled;
c906108c 14478
b775012e
LM
14479 /* Mark breakpoint locations modified. */
14480 mark_breakpoint_modified (bpt);
14481
d248b706
KY
14482 if (target_supports_enable_disable_tracepoint ()
14483 && current_trace_status ()->running && is_tracepoint (bpt))
14484 {
14485 struct bp_location *location;
14486
14487 for (location = bpt->loc; location; location = location->next)
14488 target_disable_tracepoint (location);
14489 }
14490
44702360 14491 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14492
8d3788bd 14493 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14494}
14495
51be5b68
PA
14496/* A callback for iterate_over_related_breakpoints. */
14497
14498static void
14499do_disable_breakpoint (struct breakpoint *b, void *ignore)
14500{
14501 disable_breakpoint (b);
14502}
14503
95a42b64
TT
14504/* A callback for map_breakpoint_numbers that calls
14505 disable_breakpoint. */
14506
14507static void
14508do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14509{
51be5b68 14510 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14511}
14512
c906108c 14513static void
fba45db2 14514disable_command (char *args, int from_tty)
c906108c 14515{
c906108c 14516 if (args == 0)
46c6471b
PA
14517 {
14518 struct breakpoint *bpt;
14519
14520 ALL_BREAKPOINTS (bpt)
14521 if (user_breakpoint_p (bpt))
14522 disable_breakpoint (bpt);
14523 }
9eaabc75 14524 else
0d381245 14525 {
9eaabc75
MW
14526 char *num = extract_arg (&args);
14527
14528 while (num)
d248b706 14529 {
9eaabc75 14530 if (strchr (num, '.'))
b775012e 14531 {
9eaabc75
MW
14532 struct bp_location *loc = find_location_by_number (num);
14533
14534 if (loc)
14535 {
14536 if (loc->enabled)
14537 {
14538 loc->enabled = 0;
14539 mark_breakpoint_location_modified (loc);
14540 }
14541 if (target_supports_enable_disable_tracepoint ()
14542 && current_trace_status ()->running && loc->owner
14543 && is_tracepoint (loc->owner))
14544 target_disable_tracepoint (loc);
14545 }
44702360 14546 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14547 }
9eaabc75
MW
14548 else
14549 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14550 num = extract_arg (&args);
d248b706 14551 }
0d381245 14552 }
c906108c
SS
14553}
14554
14555static void
816338b5
SS
14556enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14557 int count)
c906108c 14558{
afe38095 14559 int target_resources_ok;
c906108c
SS
14560
14561 if (bpt->type == bp_hardware_breakpoint)
14562 {
14563 int i;
c5aa993b 14564 i = hw_breakpoint_used_count ();
53a5351d 14565 target_resources_ok =
d92524f1 14566 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14567 i + 1, 0);
c906108c 14568 if (target_resources_ok == 0)
8a3fe4f8 14569 error (_("No hardware breakpoint support in the target."));
c906108c 14570 else if (target_resources_ok < 0)
8a3fe4f8 14571 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14572 }
14573
cc60f2e3 14574 if (is_watchpoint (bpt))
c906108c 14575 {
d07205c2
JK
14576 /* Initialize it just to avoid a GCC false warning. */
14577 enum enable_state orig_enable_state = 0;
dde02812 14578
492d29ea 14579 TRY
c906108c 14580 {
3a5c3e22
PA
14581 struct watchpoint *w = (struct watchpoint *) bpt;
14582
1e718ff1
TJB
14583 orig_enable_state = bpt->enable_state;
14584 bpt->enable_state = bp_enabled;
3a5c3e22 14585 update_watchpoint (w, 1 /* reparse */);
c906108c 14586 }
492d29ea 14587 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14588 {
1e718ff1 14589 bpt->enable_state = orig_enable_state;
dde02812
ES
14590 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14591 bpt->number);
14592 return;
c5aa993b 14593 }
492d29ea 14594 END_CATCH
c906108c 14595 }
0101ce28 14596
b775012e
LM
14597 bpt->enable_state = bp_enabled;
14598
14599 /* Mark breakpoint locations modified. */
14600 mark_breakpoint_modified (bpt);
14601
d248b706
KY
14602 if (target_supports_enable_disable_tracepoint ()
14603 && current_trace_status ()->running && is_tracepoint (bpt))
14604 {
14605 struct bp_location *location;
14606
14607 for (location = bpt->loc; location; location = location->next)
14608 target_enable_tracepoint (location);
14609 }
14610
b4c291bb 14611 bpt->disposition = disposition;
816338b5 14612 bpt->enable_count = count;
44702360 14613 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14614
8d3788bd 14615 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14616}
14617
fe3f5fa8 14618
c906108c 14619void
fba45db2 14620enable_breakpoint (struct breakpoint *bpt)
c906108c 14621{
816338b5 14622 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14623}
14624
14625static void
14626do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14627{
14628 enable_breakpoint (bpt);
c906108c
SS
14629}
14630
95a42b64
TT
14631/* A callback for map_breakpoint_numbers that calls
14632 enable_breakpoint. */
14633
14634static void
14635do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14636{
51be5b68 14637 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14638}
14639
c906108c
SS
14640/* The enable command enables the specified breakpoints (or all defined
14641 breakpoints) so they once again become (or continue to be) effective
1272ad14 14642 in stopping the inferior. */
c906108c 14643
c906108c 14644static void
fba45db2 14645enable_command (char *args, int from_tty)
c906108c 14646{
c906108c 14647 if (args == 0)
46c6471b
PA
14648 {
14649 struct breakpoint *bpt;
14650
14651 ALL_BREAKPOINTS (bpt)
14652 if (user_breakpoint_p (bpt))
14653 enable_breakpoint (bpt);
14654 }
9eaabc75 14655 else
0d381245 14656 {
9eaabc75
MW
14657 char *num = extract_arg (&args);
14658
14659 while (num)
d248b706 14660 {
9eaabc75 14661 if (strchr (num, '.'))
b775012e 14662 {
9eaabc75
MW
14663 struct bp_location *loc = find_location_by_number (num);
14664
14665 if (loc)
14666 {
14667 if (!loc->enabled)
14668 {
14669 loc->enabled = 1;
14670 mark_breakpoint_location_modified (loc);
14671 }
14672 if (target_supports_enable_disable_tracepoint ()
14673 && current_trace_status ()->running && loc->owner
14674 && is_tracepoint (loc->owner))
14675 target_enable_tracepoint (loc);
14676 }
44702360 14677 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14678 }
9eaabc75
MW
14679 else
14680 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14681 num = extract_arg (&args);
d248b706 14682 }
0d381245 14683 }
c906108c
SS
14684}
14685
816338b5
SS
14686/* This struct packages up disposition data for application to multiple
14687 breakpoints. */
14688
14689struct disp_data
14690{
14691 enum bpdisp disp;
14692 int count;
14693};
14694
c906108c 14695static void
51be5b68
PA
14696do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14697{
816338b5 14698 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14699
816338b5 14700 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14701}
14702
14703static void
14704do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14705{
816338b5 14706 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14707
14708 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14709}
14710
c906108c 14711static void
fba45db2 14712enable_once_command (char *args, int from_tty)
c906108c 14713{
51be5b68 14714 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14715}
14716
816338b5
SS
14717static void
14718do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14719{
14720 struct disp_data disp = { disp_disable, *(int *) countptr };
14721
14722 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14723}
14724
14725static void
14726enable_count_command (char *args, int from_tty)
14727{
b9d61307
SM
14728 int count;
14729
14730 if (args == NULL)
14731 error_no_arg (_("hit count"));
14732
14733 count = get_number (&args);
816338b5
SS
14734
14735 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14736}
14737
c906108c 14738static void
51be5b68 14739do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14740{
816338b5 14741 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14742
14743 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14744}
14745
c906108c 14746static void
fba45db2 14747enable_delete_command (char *args, int from_tty)
c906108c 14748{
51be5b68 14749 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14750}
14751\f
fa8d40ab
JJ
14752static void
14753set_breakpoint_cmd (char *args, int from_tty)
14754{
14755}
14756
14757static void
14758show_breakpoint_cmd (char *args, int from_tty)
14759{
14760}
14761
1f3b5d1b
PP
14762/* Invalidate last known value of any hardware watchpoint if
14763 the memory which that value represents has been written to by
14764 GDB itself. */
14765
14766static void
8de0566d
YQ
14767invalidate_bp_value_on_memory_change (struct inferior *inferior,
14768 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14769 const bfd_byte *data)
14770{
14771 struct breakpoint *bp;
14772
14773 ALL_BREAKPOINTS (bp)
14774 if (bp->enable_state == bp_enabled
3a5c3e22 14775 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14776 {
3a5c3e22 14777 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14778
3a5c3e22
PA
14779 if (wp->val_valid && wp->val)
14780 {
14781 struct bp_location *loc;
14782
14783 for (loc = bp->loc; loc != NULL; loc = loc->next)
14784 if (loc->loc_type == bp_loc_hardware_watchpoint
14785 && loc->address + loc->length > addr
14786 && addr + len > loc->address)
14787 {
14788 value_free (wp->val);
14789 wp->val = NULL;
14790 wp->val_valid = 0;
14791 }
14792 }
1f3b5d1b
PP
14793 }
14794}
14795
8181d85f
DJ
14796/* Create and insert a breakpoint for software single step. */
14797
14798void
6c95b8df 14799insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14800 struct address_space *aspace,
14801 CORE_ADDR next_pc)
8181d85f 14802{
7c16b83e
PA
14803 struct thread_info *tp = inferior_thread ();
14804 struct symtab_and_line sal;
14805 CORE_ADDR pc = next_pc;
8181d85f 14806
34b7e8a6
PA
14807 if (tp->control.single_step_breakpoints == NULL)
14808 {
14809 tp->control.single_step_breakpoints
14810 = new_single_step_breakpoint (tp->num, gdbarch);
14811 }
8181d85f 14812
7c16b83e
PA
14813 sal = find_pc_line (pc, 0);
14814 sal.pc = pc;
14815 sal.section = find_pc_overlay (pc);
14816 sal.explicit_pc = 1;
34b7e8a6 14817 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14818
7c16b83e 14819 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14820}
14821
34b7e8a6 14822/* See breakpoint.h. */
f02253f1
HZ
14823
14824int
7c16b83e
PA
14825breakpoint_has_location_inserted_here (struct breakpoint *bp,
14826 struct address_space *aspace,
14827 CORE_ADDR pc)
1aafd4da 14828{
7c16b83e 14829 struct bp_location *loc;
1aafd4da 14830
7c16b83e
PA
14831 for (loc = bp->loc; loc != NULL; loc = loc->next)
14832 if (loc->inserted
14833 && breakpoint_location_address_match (loc, aspace, pc))
14834 return 1;
1aafd4da 14835
7c16b83e 14836 return 0;
ef370185
JB
14837}
14838
14839/* Check whether a software single-step breakpoint is inserted at
14840 PC. */
14841
14842int
14843single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14844 CORE_ADDR pc)
14845{
34b7e8a6
PA
14846 struct breakpoint *bpt;
14847
14848 ALL_BREAKPOINTS (bpt)
14849 {
14850 if (bpt->type == bp_single_step
14851 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14852 return 1;
14853 }
14854 return 0;
1aafd4da
UW
14855}
14856
1042e4c0
SS
14857/* Tracepoint-specific operations. */
14858
14859/* Set tracepoint count to NUM. */
14860static void
14861set_tracepoint_count (int num)
14862{
14863 tracepoint_count = num;
4fa62494 14864 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14865}
14866
70221824 14867static void
1042e4c0
SS
14868trace_command (char *arg, int from_tty)
14869{
55aa24fb
SDJ
14870 struct breakpoint_ops *ops;
14871 const char *arg_cp = arg;
14872
14873 if (arg && probe_linespec_to_ops (&arg_cp))
14874 ops = &tracepoint_probe_breakpoint_ops;
14875 else
14876 ops = &tracepoint_breakpoint_ops;
14877
558a9d82
YQ
14878 create_breakpoint (get_current_arch (),
14879 arg,
14880 NULL, 0, NULL, 1 /* parse arg */,
14881 0 /* tempflag */,
14882 bp_tracepoint /* type_wanted */,
14883 0 /* Ignore count */,
14884 pending_break_support,
14885 ops,
14886 from_tty,
14887 1 /* enabled */,
14888 0 /* internal */, 0);
1042e4c0
SS
14889}
14890
70221824 14891static void
7a697b8d
SS
14892ftrace_command (char *arg, int from_tty)
14893{
558a9d82
YQ
14894 create_breakpoint (get_current_arch (),
14895 arg,
14896 NULL, 0, NULL, 1 /* parse arg */,
14897 0 /* tempflag */,
14898 bp_fast_tracepoint /* type_wanted */,
14899 0 /* Ignore count */,
14900 pending_break_support,
14901 &tracepoint_breakpoint_ops,
14902 from_tty,
14903 1 /* enabled */,
14904 0 /* internal */, 0);
0fb4aa4b
PA
14905}
14906
14907/* strace command implementation. Creates a static tracepoint. */
14908
70221824 14909static void
0fb4aa4b
PA
14910strace_command (char *arg, int from_tty)
14911{
983af33b
SDJ
14912 struct breakpoint_ops *ops;
14913
14914 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14915 or with a normal static tracepoint. */
61012eef 14916 if (arg && startswith (arg, "-m") && isspace (arg[2]))
983af33b
SDJ
14917 ops = &strace_marker_breakpoint_ops;
14918 else
14919 ops = &tracepoint_breakpoint_ops;
14920
558a9d82
YQ
14921 create_breakpoint (get_current_arch (),
14922 arg,
14923 NULL, 0, NULL, 1 /* parse arg */,
14924 0 /* tempflag */,
14925 bp_static_tracepoint /* type_wanted */,
14926 0 /* Ignore count */,
14927 pending_break_support,
14928 ops,
14929 from_tty,
14930 1 /* enabled */,
14931 0 /* internal */, 0);
7a697b8d
SS
14932}
14933
409873ef
SS
14934/* Set up a fake reader function that gets command lines from a linked
14935 list that was acquired during tracepoint uploading. */
14936
14937static struct uploaded_tp *this_utp;
3149d8c1 14938static int next_cmd;
409873ef
SS
14939
14940static char *
14941read_uploaded_action (void)
14942{
14943 char *rslt;
14944
3149d8c1 14945 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 14946
3149d8c1 14947 next_cmd++;
409873ef
SS
14948
14949 return rslt;
14950}
14951
00bf0b85
SS
14952/* Given information about a tracepoint as recorded on a target (which
14953 can be either a live system or a trace file), attempt to create an
14954 equivalent GDB tracepoint. This is not a reliable process, since
14955 the target does not necessarily have all the information used when
14956 the tracepoint was originally defined. */
14957
d9b3f62e 14958struct tracepoint *
00bf0b85 14959create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14960{
409873ef 14961 char *addr_str, small_buf[100];
d9b3f62e 14962 struct tracepoint *tp;
fd9b8c24 14963
409873ef
SS
14964 if (utp->at_string)
14965 addr_str = utp->at_string;
14966 else
14967 {
14968 /* In the absence of a source location, fall back to raw
14969 address. Since there is no way to confirm that the address
14970 means the same thing as when the trace was started, warn the
14971 user. */
3e43a32a
MS
14972 warning (_("Uploaded tracepoint %d has no "
14973 "source location, using raw address"),
409873ef 14974 utp->number);
8c042590 14975 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
14976 addr_str = small_buf;
14977 }
14978
14979 /* There's not much we can do with a sequence of bytecodes. */
14980 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14981 warning (_("Uploaded tracepoint %d condition "
14982 "has no source form, ignoring it"),
409873ef 14983 utp->number);
d5551862 14984
8cdf0e15 14985 if (!create_breakpoint (get_current_arch (),
409873ef 14986 addr_str,
e7e0cddf
SS
14987 utp->cond_string, -1, NULL,
14988 0 /* parse cond/thread */,
8cdf0e15 14989 0 /* tempflag */,
0fb4aa4b 14990 utp->type /* type_wanted */,
8cdf0e15
VP
14991 0 /* Ignore count */,
14992 pending_break_support,
348d480f 14993 &tracepoint_breakpoint_ops,
8cdf0e15 14994 0 /* from_tty */,
84f4c1fe 14995 utp->enabled /* enabled */,
44f238bb
PA
14996 0 /* internal */,
14997 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
14998 return NULL;
14999
409873ef 15000 /* Get the tracepoint we just created. */
fd9b8c24
PA
15001 tp = get_tracepoint (tracepoint_count);
15002 gdb_assert (tp != NULL);
d5551862 15003
00bf0b85
SS
15004 if (utp->pass > 0)
15005 {
8c042590
PM
15006 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15007 tp->base.number);
00bf0b85 15008
409873ef 15009 trace_pass_command (small_buf, 0);
00bf0b85
SS
15010 }
15011
409873ef
SS
15012 /* If we have uploaded versions of the original commands, set up a
15013 special-purpose "reader" function and call the usual command line
15014 reader, then pass the result to the breakpoint command-setting
15015 function. */
3149d8c1 15016 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15017 {
409873ef 15018 struct command_line *cmd_list;
00bf0b85 15019
409873ef 15020 this_utp = utp;
3149d8c1 15021 next_cmd = 0;
d5551862 15022
409873ef
SS
15023 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15024
d9b3f62e 15025 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15026 }
3149d8c1
SS
15027 else if (!VEC_empty (char_ptr, utp->actions)
15028 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15029 warning (_("Uploaded tracepoint %d actions "
15030 "have no source form, ignoring them"),
409873ef 15031 utp->number);
00bf0b85 15032
f196051f
SS
15033 /* Copy any status information that might be available. */
15034 tp->base.hit_count = utp->hit_count;
15035 tp->traceframe_usage = utp->traceframe_usage;
15036
00bf0b85 15037 return tp;
d9b3f62e 15038}
00bf0b85 15039
1042e4c0
SS
15040/* Print information on tracepoint number TPNUM_EXP, or all if
15041 omitted. */
15042
15043static void
e5a67952 15044tracepoints_info (char *args, int from_tty)
1042e4c0 15045{
79a45e25 15046 struct ui_out *uiout = current_uiout;
e5a67952 15047 int num_printed;
1042e4c0 15048
e5a67952 15049 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15050
15051 if (num_printed == 0)
1042e4c0 15052 {
e5a67952 15053 if (args == NULL || *args == '\0')
d77f58be
SS
15054 ui_out_message (uiout, 0, "No tracepoints.\n");
15055 else
e5a67952 15056 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15057 }
ad443146
SS
15058
15059 default_collect_info ();
1042e4c0
SS
15060}
15061
4a64f543 15062/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15063 Not supported by all targets. */
15064static void
15065enable_trace_command (char *args, int from_tty)
15066{
15067 enable_command (args, from_tty);
15068}
15069
4a64f543 15070/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15071 Not supported by all targets. */
15072static void
15073disable_trace_command (char *args, int from_tty)
15074{
15075 disable_command (args, from_tty);
15076}
15077
4a64f543 15078/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15079static void
15080delete_trace_command (char *arg, int from_tty)
15081{
35df4500 15082 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15083
15084 dont_repeat ();
15085
15086 if (arg == 0)
15087 {
15088 int breaks_to_delete = 0;
15089
15090 /* Delete all breakpoints if no argument.
15091 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15092 have to be deleted with an explicit breakpoint number
15093 argument. */
1042e4c0 15094 ALL_TRACEPOINTS (b)
46c6471b 15095 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15096 {
15097 breaks_to_delete = 1;
15098 break;
15099 }
1042e4c0
SS
15100
15101 /* Ask user only if there are some breakpoints to delete. */
15102 if (!from_tty
15103 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15104 {
35df4500 15105 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15106 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15107 delete_breakpoint (b);
1042e4c0
SS
15108 }
15109 }
15110 else
51be5b68 15111 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15112}
15113
197f0a60
TT
15114/* Helper function for trace_pass_command. */
15115
15116static void
d9b3f62e 15117trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15118{
d9b3f62e 15119 tp->pass_count = count;
6f6484cd 15120 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15121 if (from_tty)
15122 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15123 tp->base.number, count);
197f0a60
TT
15124}
15125
1042e4c0
SS
15126/* Set passcount for tracepoint.
15127
15128 First command argument is passcount, second is tracepoint number.
15129 If tracepoint number omitted, apply to most recently defined.
15130 Also accepts special argument "all". */
15131
15132static void
15133trace_pass_command (char *args, int from_tty)
15134{
d9b3f62e 15135 struct tracepoint *t1;
1042e4c0 15136 unsigned int count;
1042e4c0
SS
15137
15138 if (args == 0 || *args == 0)
3e43a32a
MS
15139 error (_("passcount command requires an "
15140 "argument (count + optional TP num)"));
1042e4c0 15141
4a64f543 15142 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15143
529480d0 15144 args = skip_spaces (args);
1042e4c0
SS
15145 if (*args && strncasecmp (args, "all", 3) == 0)
15146 {
d9b3f62e
PA
15147 struct breakpoint *b;
15148
1042e4c0 15149 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15150 if (*args)
15151 error (_("Junk at end of arguments."));
1042e4c0 15152
d9b3f62e 15153 ALL_TRACEPOINTS (b)
197f0a60 15154 {
d9b3f62e 15155 t1 = (struct tracepoint *) b;
197f0a60
TT
15156 trace_pass_set_count (t1, count, from_tty);
15157 }
15158 }
15159 else if (*args == '\0')
1042e4c0 15160 {
5fa1d40e 15161 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15162 if (t1)
197f0a60
TT
15163 trace_pass_set_count (t1, count, from_tty);
15164 }
15165 else
15166 {
15167 struct get_number_or_range_state state;
15168
15169 init_number_or_range (&state, args);
15170 while (!state.finished)
1042e4c0 15171 {
5fa1d40e 15172 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15173 if (t1)
15174 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15175 }
15176 }
1042e4c0
SS
15177}
15178
d9b3f62e 15179struct tracepoint *
1042e4c0
SS
15180get_tracepoint (int num)
15181{
15182 struct breakpoint *t;
15183
15184 ALL_TRACEPOINTS (t)
15185 if (t->number == num)
d9b3f62e 15186 return (struct tracepoint *) t;
1042e4c0
SS
15187
15188 return NULL;
15189}
15190
d5551862
SS
15191/* Find the tracepoint with the given target-side number (which may be
15192 different from the tracepoint number after disconnecting and
15193 reconnecting). */
15194
d9b3f62e 15195struct tracepoint *
d5551862
SS
15196get_tracepoint_by_number_on_target (int num)
15197{
d9b3f62e 15198 struct breakpoint *b;
d5551862 15199
d9b3f62e
PA
15200 ALL_TRACEPOINTS (b)
15201 {
15202 struct tracepoint *t = (struct tracepoint *) b;
15203
15204 if (t->number_on_target == num)
15205 return t;
15206 }
d5551862
SS
15207
15208 return NULL;
15209}
15210
1042e4c0 15211/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15212 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15213 If the argument is missing, the most recent tracepoint
15214 (tracepoint_count) is returned. */
15215
d9b3f62e 15216struct tracepoint *
197f0a60 15217get_tracepoint_by_number (char **arg,
5fa1d40e 15218 struct get_number_or_range_state *state)
1042e4c0 15219{
1042e4c0
SS
15220 struct breakpoint *t;
15221 int tpnum;
15222 char *instring = arg == NULL ? NULL : *arg;
15223
197f0a60
TT
15224 if (state)
15225 {
15226 gdb_assert (!state->finished);
15227 tpnum = get_number_or_range (state);
15228 }
15229 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15230 tpnum = tracepoint_count;
1042e4c0 15231 else
197f0a60 15232 tpnum = get_number (arg);
1042e4c0
SS
15233
15234 if (tpnum <= 0)
15235 {
15236 if (instring && *instring)
15237 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15238 instring);
15239 else
5fa1d40e 15240 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15241 return NULL;
15242 }
15243
15244 ALL_TRACEPOINTS (t)
15245 if (t->number == tpnum)
15246 {
d9b3f62e 15247 return (struct tracepoint *) t;
1042e4c0
SS
15248 }
15249
1042e4c0
SS
15250 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15251 return NULL;
15252}
15253
d9b3f62e
PA
15254void
15255print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15256{
15257 if (b->thread != -1)
15258 fprintf_unfiltered (fp, " thread %d", b->thread);
15259
15260 if (b->task != 0)
15261 fprintf_unfiltered (fp, " task %d", b->task);
15262
15263 fprintf_unfiltered (fp, "\n");
15264}
15265
6149aea9
PA
15266/* Save information on user settable breakpoints (watchpoints, etc) to
15267 a new script file named FILENAME. If FILTER is non-NULL, call it
15268 on each breakpoint and only include the ones for which it returns
15269 non-zero. */
15270
1042e4c0 15271static void
6149aea9
PA
15272save_breakpoints (char *filename, int from_tty,
15273 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15274{
15275 struct breakpoint *tp;
6149aea9 15276 int any = 0;
1042e4c0 15277 struct cleanup *cleanup;
a7bdde9e 15278 struct ui_file *fp;
6149aea9 15279 int extra_trace_bits = 0;
1042e4c0 15280
6149aea9
PA
15281 if (filename == 0 || *filename == 0)
15282 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15283
15284 /* See if we have anything to save. */
6149aea9 15285 ALL_BREAKPOINTS (tp)
1042e4c0 15286 {
6149aea9 15287 /* Skip internal and momentary breakpoints. */
09d682a4 15288 if (!user_breakpoint_p (tp))
6149aea9
PA
15289 continue;
15290
15291 /* If we have a filter, only save the breakpoints it accepts. */
15292 if (filter && !filter (tp))
15293 continue;
15294
15295 any = 1;
15296
15297 if (is_tracepoint (tp))
15298 {
15299 extra_trace_bits = 1;
15300
15301 /* We can stop searching. */
15302 break;
15303 }
1042e4c0 15304 }
6149aea9
PA
15305
15306 if (!any)
1042e4c0 15307 {
6149aea9 15308 warning (_("Nothing to save."));
1042e4c0
SS
15309 return;
15310 }
15311
c718be47
PA
15312 filename = tilde_expand (filename);
15313 cleanup = make_cleanup (xfree, filename);
15314 fp = gdb_fopen (filename, "w");
059fb39f 15315 if (!fp)
6149aea9
PA
15316 error (_("Unable to open file '%s' for saving (%s)"),
15317 filename, safe_strerror (errno));
a7bdde9e 15318 make_cleanup_ui_file_delete (fp);
8bf6485c 15319
6149aea9
PA
15320 if (extra_trace_bits)
15321 save_trace_state_variables (fp);
8bf6485c 15322
6149aea9 15323 ALL_BREAKPOINTS (tp)
1042e4c0 15324 {
6149aea9 15325 /* Skip internal and momentary breakpoints. */
09d682a4 15326 if (!user_breakpoint_p (tp))
6149aea9 15327 continue;
8bf6485c 15328
6149aea9
PA
15329 /* If we have a filter, only save the breakpoints it accepts. */
15330 if (filter && !filter (tp))
15331 continue;
15332
348d480f 15333 tp->ops->print_recreate (tp, fp);
1042e4c0 15334
6149aea9
PA
15335 /* Note, we can't rely on tp->number for anything, as we can't
15336 assume the recreated breakpoint numbers will match. Use $bpnum
15337 instead. */
15338
15339 if (tp->cond_string)
15340 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15341
15342 if (tp->ignore_count)
15343 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15344
2d9442cc 15345 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15346 {
6c63c96a 15347 struct gdb_exception exception;
a7bdde9e 15348
6149aea9 15349 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15350
79a45e25 15351 ui_out_redirect (current_uiout, fp);
492d29ea 15352 TRY
1042e4c0 15353 {
79a45e25 15354 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15355 }
492d29ea
PA
15356 CATCH (ex, RETURN_MASK_ALL)
15357 {
6c63c96a 15358 ui_out_redirect (current_uiout, NULL);
492d29ea
PA
15359 throw_exception (ex);
15360 }
15361 END_CATCH
1042e4c0 15362
6c63c96a 15363 ui_out_redirect (current_uiout, NULL);
a7bdde9e 15364 fprintf_unfiltered (fp, " end\n");
1042e4c0 15365 }
6149aea9
PA
15366
15367 if (tp->enable_state == bp_disabled)
99894e11 15368 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15369
15370 /* If this is a multi-location breakpoint, check if the locations
15371 should be individually disabled. Watchpoint locations are
15372 special, and not user visible. */
15373 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15374 {
15375 struct bp_location *loc;
15376 int n = 1;
15377
15378 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15379 if (!loc->enabled)
15380 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15381 }
1042e4c0 15382 }
8bf6485c 15383
6149aea9 15384 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15385 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15386
1042e4c0 15387 if (from_tty)
6149aea9 15388 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15389 do_cleanups (cleanup);
6149aea9
PA
15390}
15391
15392/* The `save breakpoints' command. */
15393
15394static void
15395save_breakpoints_command (char *args, int from_tty)
15396{
15397 save_breakpoints (args, from_tty, NULL);
15398}
15399
15400/* The `save tracepoints' command. */
15401
15402static void
15403save_tracepoints_command (char *args, int from_tty)
15404{
15405 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15406}
15407
15408/* Create a vector of all tracepoints. */
15409
15410VEC(breakpoint_p) *
eeae04df 15411all_tracepoints (void)
1042e4c0
SS
15412{
15413 VEC(breakpoint_p) *tp_vec = 0;
15414 struct breakpoint *tp;
15415
15416 ALL_TRACEPOINTS (tp)
15417 {
15418 VEC_safe_push (breakpoint_p, tp_vec, tp);
15419 }
15420
15421 return tp_vec;
15422}
15423
c906108c 15424\f
4a64f543
MS
15425/* This help string is used for the break, hbreak, tbreak and thbreak
15426 commands. It is defined as a macro to prevent duplication.
15427 COMMAND should be a string constant containing the name of the
15428 command. */
31e2b00f 15429#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15430command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15431PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15432probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15433guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15434`-probe-dtrace' (for a DTrace probe).\n\
31e2b00f
AS
15435LOCATION may be a line number, function name, or \"*\" and an address.\n\
15436If a line number is specified, break at start of code for that line.\n\
15437If a function is specified, break at start of code for that function.\n\
15438If an address is specified, break at that exact address.\n\
dc10affe
PA
15439With no LOCATION, uses current execution address of the selected\n\
15440stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15441\n\
15442THREADNUM is the number from \"info threads\".\n\
15443CONDITION is a boolean expression.\n\
15444\n\
d41c0fc8
PA
15445Multiple breakpoints at one place are permitted, and useful if their\n\
15446conditions are different.\n\
31e2b00f
AS
15447\n\
15448Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15449
44feb3ce
TT
15450/* List of subcommands for "catch". */
15451static struct cmd_list_element *catch_cmdlist;
15452
15453/* List of subcommands for "tcatch". */
15454static struct cmd_list_element *tcatch_cmdlist;
15455
9ac4176b 15456void
44feb3ce 15457add_catch_command (char *name, char *docstring,
82ae6c8d 15458 cmd_sfunc_ftype *sfunc,
625e8578 15459 completer_ftype *completer,
44feb3ce
TT
15460 void *user_data_catch,
15461 void *user_data_tcatch)
15462{
15463 struct cmd_list_element *command;
15464
15465 command = add_cmd (name, class_breakpoint, NULL, docstring,
15466 &catch_cmdlist);
15467 set_cmd_sfunc (command, sfunc);
15468 set_cmd_context (command, user_data_catch);
a96d9b2e 15469 set_cmd_completer (command, completer);
44feb3ce
TT
15470
15471 command = add_cmd (name, class_breakpoint, NULL, docstring,
15472 &tcatch_cmdlist);
15473 set_cmd_sfunc (command, sfunc);
15474 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15475 set_cmd_completer (command, completer);
44feb3ce
TT
15476}
15477
6149aea9
PA
15478static void
15479save_command (char *arg, int from_tty)
15480{
3e43a32a
MS
15481 printf_unfiltered (_("\"save\" must be followed by "
15482 "the name of a save subcommand.\n"));
635c7e8a 15483 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15484}
15485
84f4c1fe
PM
15486struct breakpoint *
15487iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15488 void *data)
15489{
35df4500 15490 struct breakpoint *b, *b_tmp;
84f4c1fe 15491
35df4500 15492 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15493 {
15494 if ((*callback) (b, data))
15495 return b;
15496 }
15497
15498 return NULL;
15499}
15500
0574c78f
GB
15501/* Zero if any of the breakpoint's locations could be a location where
15502 functions have been inlined, nonzero otherwise. */
15503
15504static int
15505is_non_inline_function (struct breakpoint *b)
15506{
15507 /* The shared library event breakpoint is set on the address of a
15508 non-inline function. */
15509 if (b->type == bp_shlib_event)
15510 return 1;
15511
15512 return 0;
15513}
15514
15515/* Nonzero if the specified PC cannot be a location where functions
15516 have been inlined. */
15517
15518int
09ac7c10
TT
15519pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15520 const struct target_waitstatus *ws)
0574c78f
GB
15521{
15522 struct breakpoint *b;
15523 struct bp_location *bl;
15524
15525 ALL_BREAKPOINTS (b)
15526 {
15527 if (!is_non_inline_function (b))
15528 continue;
15529
15530 for (bl = b->loc; bl != NULL; bl = bl->next)
15531 {
15532 if (!bl->shlib_disabled
09ac7c10 15533 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15534 return 1;
15535 }
15536 }
15537
15538 return 0;
15539}
15540
2f202fde
JK
15541/* Remove any references to OBJFILE which is going to be freed. */
15542
15543void
15544breakpoint_free_objfile (struct objfile *objfile)
15545{
15546 struct bp_location **locp, *loc;
15547
15548 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15549 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15550 loc->symtab = NULL;
15551}
15552
2060206e
PA
15553void
15554initialize_breakpoint_ops (void)
15555{
15556 static int initialized = 0;
15557
15558 struct breakpoint_ops *ops;
15559
15560 if (initialized)
15561 return;
15562 initialized = 1;
15563
15564 /* The breakpoint_ops structure to be inherit by all kinds of
15565 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15566 internal and momentary breakpoints, etc.). */
15567 ops = &bkpt_base_breakpoint_ops;
15568 *ops = base_breakpoint_ops;
15569 ops->re_set = bkpt_re_set;
15570 ops->insert_location = bkpt_insert_location;
15571 ops->remove_location = bkpt_remove_location;
15572 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
15573 ops->create_sals_from_address = bkpt_create_sals_from_address;
15574 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15575 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
15576
15577 /* The breakpoint_ops structure to be used in regular breakpoints. */
15578 ops = &bkpt_breakpoint_ops;
15579 *ops = bkpt_base_breakpoint_ops;
15580 ops->re_set = bkpt_re_set;
15581 ops->resources_needed = bkpt_resources_needed;
15582 ops->print_it = bkpt_print_it;
15583 ops->print_mention = bkpt_print_mention;
15584 ops->print_recreate = bkpt_print_recreate;
15585
15586 /* Ranged breakpoints. */
15587 ops = &ranged_breakpoint_ops;
15588 *ops = bkpt_breakpoint_ops;
15589 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15590 ops->resources_needed = resources_needed_ranged_breakpoint;
15591 ops->print_it = print_it_ranged_breakpoint;
15592 ops->print_one = print_one_ranged_breakpoint;
15593 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15594 ops->print_mention = print_mention_ranged_breakpoint;
15595 ops->print_recreate = print_recreate_ranged_breakpoint;
15596
15597 /* Internal breakpoints. */
15598 ops = &internal_breakpoint_ops;
15599 *ops = bkpt_base_breakpoint_ops;
15600 ops->re_set = internal_bkpt_re_set;
15601 ops->check_status = internal_bkpt_check_status;
15602 ops->print_it = internal_bkpt_print_it;
15603 ops->print_mention = internal_bkpt_print_mention;
15604
15605 /* Momentary breakpoints. */
15606 ops = &momentary_breakpoint_ops;
15607 *ops = bkpt_base_breakpoint_ops;
15608 ops->re_set = momentary_bkpt_re_set;
15609 ops->check_status = momentary_bkpt_check_status;
15610 ops->print_it = momentary_bkpt_print_it;
15611 ops->print_mention = momentary_bkpt_print_mention;
15612
e2e4d78b
JK
15613 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15614 ops = &longjmp_breakpoint_ops;
15615 *ops = momentary_breakpoint_ops;
15616 ops->dtor = longjmp_bkpt_dtor;
15617
55aa24fb
SDJ
15618 /* Probe breakpoints. */
15619 ops = &bkpt_probe_breakpoint_ops;
15620 *ops = bkpt_breakpoint_ops;
15621 ops->insert_location = bkpt_probe_insert_location;
15622 ops->remove_location = bkpt_probe_remove_location;
15623 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15624 ops->decode_linespec = bkpt_probe_decode_linespec;
15625
2060206e
PA
15626 /* Watchpoints. */
15627 ops = &watchpoint_breakpoint_ops;
15628 *ops = base_breakpoint_ops;
3a5c3e22 15629 ops->dtor = dtor_watchpoint;
2060206e
PA
15630 ops->re_set = re_set_watchpoint;
15631 ops->insert_location = insert_watchpoint;
15632 ops->remove_location = remove_watchpoint;
15633 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15634 ops->check_status = check_status_watchpoint;
15635 ops->resources_needed = resources_needed_watchpoint;
15636 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15637 ops->print_it = print_it_watchpoint;
15638 ops->print_mention = print_mention_watchpoint;
15639 ops->print_recreate = print_recreate_watchpoint;
427cd150 15640 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15641
15642 /* Masked watchpoints. */
15643 ops = &masked_watchpoint_breakpoint_ops;
15644 *ops = watchpoint_breakpoint_ops;
15645 ops->insert_location = insert_masked_watchpoint;
15646 ops->remove_location = remove_masked_watchpoint;
15647 ops->resources_needed = resources_needed_masked_watchpoint;
15648 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15649 ops->print_it = print_it_masked_watchpoint;
15650 ops->print_one_detail = print_one_detail_masked_watchpoint;
15651 ops->print_mention = print_mention_masked_watchpoint;
15652 ops->print_recreate = print_recreate_masked_watchpoint;
15653
15654 /* Tracepoints. */
15655 ops = &tracepoint_breakpoint_ops;
15656 *ops = base_breakpoint_ops;
15657 ops->re_set = tracepoint_re_set;
15658 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15659 ops->print_one_detail = tracepoint_print_one_detail;
15660 ops->print_mention = tracepoint_print_mention;
15661 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
15662 ops->create_sals_from_address = tracepoint_create_sals_from_address;
15663 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15664 ops->decode_linespec = tracepoint_decode_linespec;
15665
55aa24fb
SDJ
15666 /* Probe tracepoints. */
15667 ops = &tracepoint_probe_breakpoint_ops;
15668 *ops = tracepoint_breakpoint_ops;
15669 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15670 ops->decode_linespec = tracepoint_probe_decode_linespec;
15671
983af33b
SDJ
15672 /* Static tracepoints with marker (`-m'). */
15673 ops = &strace_marker_breakpoint_ops;
15674 *ops = tracepoint_breakpoint_ops;
15675 ops->create_sals_from_address = strace_marker_create_sals_from_address;
15676 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15677 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
15678
15679 /* Fork catchpoints. */
15680 ops = &catch_fork_breakpoint_ops;
15681 *ops = base_breakpoint_ops;
15682 ops->insert_location = insert_catch_fork;
15683 ops->remove_location = remove_catch_fork;
15684 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15685 ops->print_it = print_it_catch_fork;
15686 ops->print_one = print_one_catch_fork;
15687 ops->print_mention = print_mention_catch_fork;
15688 ops->print_recreate = print_recreate_catch_fork;
15689
15690 /* Vfork catchpoints. */
15691 ops = &catch_vfork_breakpoint_ops;
15692 *ops = base_breakpoint_ops;
15693 ops->insert_location = insert_catch_vfork;
15694 ops->remove_location = remove_catch_vfork;
15695 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15696 ops->print_it = print_it_catch_vfork;
15697 ops->print_one = print_one_catch_vfork;
15698 ops->print_mention = print_mention_catch_vfork;
15699 ops->print_recreate = print_recreate_catch_vfork;
15700
15701 /* Exec catchpoints. */
15702 ops = &catch_exec_breakpoint_ops;
15703 *ops = base_breakpoint_ops;
15704 ops->dtor = dtor_catch_exec;
15705 ops->insert_location = insert_catch_exec;
15706 ops->remove_location = remove_catch_exec;
15707 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15708 ops->print_it = print_it_catch_exec;
15709 ops->print_one = print_one_catch_exec;
15710 ops->print_mention = print_mention_catch_exec;
15711 ops->print_recreate = print_recreate_catch_exec;
15712
edcc5120
TT
15713 /* Solib-related catchpoints. */
15714 ops = &catch_solib_breakpoint_ops;
15715 *ops = base_breakpoint_ops;
15716 ops->dtor = dtor_catch_solib;
15717 ops->insert_location = insert_catch_solib;
15718 ops->remove_location = remove_catch_solib;
15719 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15720 ops->check_status = check_status_catch_solib;
15721 ops->print_it = print_it_catch_solib;
15722 ops->print_one = print_one_catch_solib;
15723 ops->print_mention = print_mention_catch_solib;
15724 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15725
15726 ops = &dprintf_breakpoint_ops;
15727 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15728 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15729 ops->resources_needed = bkpt_resources_needed;
15730 ops->print_it = bkpt_print_it;
15731 ops->print_mention = bkpt_print_mention;
2d9442cc 15732 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15733 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15734 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15735}
15736
8bfd80db
YQ
15737/* Chain containing all defined "enable breakpoint" subcommands. */
15738
15739static struct cmd_list_element *enablebreaklist = NULL;
15740
c906108c 15741void
fba45db2 15742_initialize_breakpoint (void)
c906108c
SS
15743{
15744 struct cmd_list_element *c;
15745
2060206e
PA
15746 initialize_breakpoint_ops ();
15747
84acb35a 15748 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15749 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15750 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15751
55aa24fb
SDJ
15752 breakpoint_objfile_key
15753 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15754
c906108c
SS
15755 breakpoint_chain = 0;
15756 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15757 before a breakpoint is set. */
15758 breakpoint_count = 0;
15759
1042e4c0
SS
15760 tracepoint_count = 0;
15761
1bedd215
AC
15762 add_com ("ignore", class_breakpoint, ignore_command, _("\
15763Set ignore-count of breakpoint number N to COUNT.\n\
15764Usage is `ignore N COUNT'."));
c906108c 15765
1bedd215
AC
15766 add_com ("commands", class_breakpoint, commands_command, _("\
15767Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
15768Give breakpoint number as argument after \"commands\".\n\
15769With no argument, the targeted breakpoint is the last one set.\n\
15770The commands themselves follow starting on the next line.\n\
15771Type a line containing \"end\" to indicate the end of them.\n\
15772Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15773then no output is printed when it is hit, except what the commands print."));
c906108c 15774
d55637df 15775 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15776Specify breakpoint number N to break only if COND is true.\n\
c906108c 15777Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15778expression to be evaluated whenever breakpoint N is reached."));
d55637df 15779 set_cmd_completer (c, condition_completer);
c906108c 15780
1bedd215 15781 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15782Set a temporary breakpoint.\n\
c906108c
SS
15783Like \"break\" except the breakpoint is only temporary,\n\
15784so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15785by using \"enable delete\" on the breakpoint number.\n\
15786\n"
15787BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15788 set_cmd_completer (c, location_completer);
c94fdfd0 15789
1bedd215 15790 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15791Set a hardware assisted breakpoint.\n\
c906108c 15792Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15793some target hardware may not have this support.\n\
15794\n"
15795BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15796 set_cmd_completer (c, location_completer);
c906108c 15797
1bedd215 15798 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15799Set a temporary hardware assisted breakpoint.\n\
c906108c 15800Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15801so it will be deleted when hit.\n\
15802\n"
15803BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15804 set_cmd_completer (c, location_completer);
c906108c 15805
1bedd215
AC
15806 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15807Enable some breakpoints.\n\
c906108c
SS
15808Give breakpoint numbers (separated by spaces) as arguments.\n\
15809With no subcommand, breakpoints are enabled until you command otherwise.\n\
15810This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15811With a subcommand you can enable temporarily."),
c906108c 15812 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15813
15814 add_com_alias ("en", "enable", class_breakpoint, 1);
15815
84951ab5 15816 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15817Enable some breakpoints.\n\
c906108c
SS
15818Give breakpoint numbers (separated by spaces) as arguments.\n\
15819This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15820May be abbreviated to simply \"enable\".\n"),
c5aa993b 15821 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15822
1a966eab
AC
15823 add_cmd ("once", no_class, enable_once_command, _("\
15824Enable breakpoints for one hit. Give breakpoint numbers.\n\
15825If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15826 &enablebreaklist);
15827
1a966eab
AC
15828 add_cmd ("delete", no_class, enable_delete_command, _("\
15829Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15830If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15831 &enablebreaklist);
15832
816338b5
SS
15833 add_cmd ("count", no_class, enable_count_command, _("\
15834Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15835If a breakpoint is hit while enabled in this fashion,\n\
15836the count is decremented; when it reaches zero, the breakpoint is disabled."),
15837 &enablebreaklist);
15838
1a966eab
AC
15839 add_cmd ("delete", no_class, enable_delete_command, _("\
15840Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15841If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15842 &enablelist);
15843
1a966eab
AC
15844 add_cmd ("once", no_class, enable_once_command, _("\
15845Enable breakpoints for one hit. Give breakpoint numbers.\n\
15846If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15847 &enablelist);
15848
15849 add_cmd ("count", no_class, enable_count_command, _("\
15850Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15851If a breakpoint is hit while enabled in this fashion,\n\
15852the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15853 &enablelist);
15854
1bedd215
AC
15855 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15856Disable some breakpoints.\n\
c906108c
SS
15857Arguments are breakpoint numbers with spaces in between.\n\
15858To disable all breakpoints, give no argument.\n\
64b9b334 15859A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15860 &disablelist, "disable ", 1, &cmdlist);
15861 add_com_alias ("dis", "disable", class_breakpoint, 1);
15862 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 15863
1a966eab
AC
15864 add_cmd ("breakpoints", class_alias, disable_command, _("\
15865Disable some breakpoints.\n\
c906108c
SS
15866Arguments are breakpoint numbers with spaces in between.\n\
15867To disable all breakpoints, give no argument.\n\
64b9b334 15868A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15869This command may be abbreviated \"disable\"."),
c906108c
SS
15870 &disablelist);
15871
1bedd215
AC
15872 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15873Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15874Arguments are breakpoint numbers with spaces in between.\n\
15875To delete all breakpoints, give no argument.\n\
15876\n\
15877Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15878The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15879 &deletelist, "delete ", 1, &cmdlist);
15880 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15881 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15882
1a966eab
AC
15883 add_cmd ("breakpoints", class_alias, delete_command, _("\
15884Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15885Arguments are breakpoint numbers with spaces in between.\n\
15886To delete all breakpoints, give no argument.\n\
1a966eab 15887This command may be abbreviated \"delete\"."),
c906108c
SS
15888 &deletelist);
15889
1bedd215
AC
15890 add_com ("clear", class_breakpoint, clear_command, _("\
15891Clear breakpoint at specified line or function.\n\
c906108c
SS
15892Argument may be line number, function name, or \"*\" and an address.\n\
15893If line number is specified, all breakpoints in that line are cleared.\n\
15894If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
15895If an address is specified, breakpoints at that address are cleared.\n\
15896\n\
15897With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
15898is executing in.\n\
15899\n\
1bedd215 15900See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15901 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15902
1bedd215 15903 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
15904Set breakpoint at specified line or function.\n"
15905BREAK_ARGS_HELP ("break")));
5ba2abeb 15906 set_cmd_completer (c, location_completer);
c94fdfd0 15907
c906108c
SS
15908 add_com_alias ("b", "break", class_run, 1);
15909 add_com_alias ("br", "break", class_run, 1);
15910 add_com_alias ("bre", "break", class_run, 1);
15911 add_com_alias ("brea", "break", class_run, 1);
15912
c906108c
SS
15913 if (dbx_commands)
15914 {
1bedd215
AC
15915 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15916Break in function/address or break at a line in the current file."),
c5aa993b
JM
15917 &stoplist, "stop ", 1, &cmdlist);
15918 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15919 _("Break in function or address."), &stoplist);
c5aa993b 15920 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15921 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
15922 add_com ("status", class_info, breakpoints_info, _("\
15923Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15924The \"Type\" column indicates one of:\n\
15925\tbreakpoint - normal breakpoint\n\
15926\twatchpoint - watchpoint\n\
15927The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15928the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15929breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15930address and file/line number respectively.\n\
15931\n\
15932Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15933are set to the address of the last breakpoint listed unless the command\n\
15934is prefixed with \"server \".\n\n\
c906108c 15935Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15936breakpoint set."));
c906108c
SS
15937 }
15938
1bedd215 15939 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 15940Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15941The \"Type\" column indicates one of:\n\
15942\tbreakpoint - normal breakpoint\n\
15943\twatchpoint - watchpoint\n\
15944The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15945the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15946breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15947address and file/line number respectively.\n\
15948\n\
15949Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15950are set to the address of the last breakpoint listed unless the command\n\
15951is prefixed with \"server \".\n\n\
c906108c 15952Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15953breakpoint set."));
c906108c 15954
6b04bdb7
MS
15955 add_info_alias ("b", "breakpoints", 1);
15956
1a966eab
AC
15957 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15958Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15959The \"Type\" column indicates one of:\n\
15960\tbreakpoint - normal breakpoint\n\
15961\twatchpoint - watchpoint\n\
15962\tlongjmp - internal breakpoint used to step through longjmp()\n\
15963\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15964\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15965\tfinish - internal breakpoint used by the \"finish\" command\n\
15966The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15967the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15968breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15969address and file/line number respectively.\n\
15970\n\
15971Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15972are set to the address of the last breakpoint listed unless the command\n\
15973is prefixed with \"server \".\n\n\
c906108c 15974Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15975breakpoint set."),
c906108c
SS
15976 &maintenanceinfolist);
15977
44feb3ce
TT
15978 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15979Set catchpoints to catch events."),
15980 &catch_cmdlist, "catch ",
15981 0/*allow-unknown*/, &cmdlist);
15982
15983 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15984Set temporary catchpoints to catch events."),
15985 &tcatch_cmdlist, "tcatch ",
15986 0/*allow-unknown*/, &cmdlist);
15987
44feb3ce
TT
15988 add_catch_command ("fork", _("Catch calls to fork."),
15989 catch_fork_command_1,
a96d9b2e 15990 NULL,
44feb3ce
TT
15991 (void *) (uintptr_t) catch_fork_permanent,
15992 (void *) (uintptr_t) catch_fork_temporary);
15993 add_catch_command ("vfork", _("Catch calls to vfork."),
15994 catch_fork_command_1,
a96d9b2e 15995 NULL,
44feb3ce
TT
15996 (void *) (uintptr_t) catch_vfork_permanent,
15997 (void *) (uintptr_t) catch_vfork_temporary);
15998 add_catch_command ("exec", _("Catch calls to exec."),
15999 catch_exec_command_1,
a96d9b2e
SDJ
16000 NULL,
16001 CATCH_PERMANENT,
16002 CATCH_TEMPORARY);
edcc5120
TT
16003 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16004Usage: catch load [REGEX]\n\
16005If REGEX is given, only stop for libraries matching the regular expression."),
16006 catch_load_command_1,
16007 NULL,
16008 CATCH_PERMANENT,
16009 CATCH_TEMPORARY);
16010 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16011Usage: catch unload [REGEX]\n\
16012If REGEX is given, only stop for libraries matching the regular expression."),
16013 catch_unload_command_1,
16014 NULL,
16015 CATCH_PERMANENT,
16016 CATCH_TEMPORARY);
c5aa993b 16017
1bedd215
AC
16018 c = add_com ("watch", class_breakpoint, watch_command, _("\
16019Set a watchpoint for an expression.\n\
06a64a0b 16020Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16021A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16022an expression changes.\n\
16023If -l or -location is given, this evaluates EXPRESSION and watches\n\
16024the memory to which it refers."));
65d12d83 16025 set_cmd_completer (c, expression_completer);
c906108c 16026
1bedd215
AC
16027 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16028Set a read watchpoint for an expression.\n\
06a64a0b 16029Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16030A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16031an expression is read.\n\
16032If -l or -location is given, this evaluates EXPRESSION and watches\n\
16033the memory to which it refers."));
65d12d83 16034 set_cmd_completer (c, expression_completer);
c906108c 16035
1bedd215
AC
16036 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16037Set a watchpoint for an expression.\n\
06a64a0b 16038Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16039A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16040an expression is either read or written.\n\
16041If -l or -location is given, this evaluates EXPRESSION and watches\n\
16042the memory to which it refers."));
65d12d83 16043 set_cmd_completer (c, expression_completer);
c906108c 16044
d77f58be 16045 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16046Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16047
920d2a44
AC
16048 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16049 respond to changes - contrary to the description. */
85c07804
AC
16050 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16051 &can_use_hw_watchpoints, _("\
16052Set debugger's willingness to use watchpoint hardware."), _("\
16053Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16054If zero, gdb will not use hardware for new watchpoints, even if\n\
16055such is available. (However, any hardware watchpoints that were\n\
16056created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16057hardware.)"),
16058 NULL,
920d2a44 16059 show_can_use_hw_watchpoints,
85c07804 16060 &setlist, &showlist);
c906108c
SS
16061
16062 can_use_hw_watchpoints = 1;
fa8d40ab 16063
1042e4c0
SS
16064 /* Tracepoint manipulation commands. */
16065
16066 c = add_com ("trace", class_breakpoint, trace_command, _("\
16067Set a tracepoint at specified line or function.\n\
16068\n"
16069BREAK_ARGS_HELP ("trace") "\n\
16070Do \"help tracepoints\" for info on other tracepoint commands."));
16071 set_cmd_completer (c, location_completer);
16072
16073 add_com_alias ("tp", "trace", class_alias, 0);
16074 add_com_alias ("tr", "trace", class_alias, 1);
16075 add_com_alias ("tra", "trace", class_alias, 1);
16076 add_com_alias ("trac", "trace", class_alias, 1);
16077
7a697b8d
SS
16078 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16079Set a fast tracepoint at specified line or function.\n\
16080\n"
16081BREAK_ARGS_HELP ("ftrace") "\n\
16082Do \"help tracepoints\" for info on other tracepoint commands."));
16083 set_cmd_completer (c, location_completer);
16084
0fb4aa4b
PA
16085 c = add_com ("strace", class_breakpoint, strace_command, _("\
16086Set a static tracepoint at specified line, function or marker.\n\
16087\n\
16088strace [LOCATION] [if CONDITION]\n\
16089LOCATION may be a line number, function name, \"*\" and an address,\n\
16090or -m MARKER_ID.\n\
16091If a line number is specified, probe the marker at start of code\n\
16092for that line. If a function is specified, probe the marker at start\n\
16093of code for that function. If an address is specified, probe the marker\n\
16094at that exact address. If a marker id is specified, probe the marker\n\
16095with that name. With no LOCATION, uses current execution address of\n\
16096the selected stack frame.\n\
16097Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16098This collects arbitrary user data passed in the probe point call to the\n\
16099tracing library. You can inspect it when analyzing the trace buffer,\n\
16100by printing the $_sdata variable like any other convenience variable.\n\
16101\n\
16102CONDITION is a boolean expression.\n\
16103\n\
d41c0fc8
PA
16104Multiple tracepoints at one place are permitted, and useful if their\n\
16105conditions are different.\n\
0fb4aa4b
PA
16106\n\
16107Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16108Do \"help tracepoints\" for info on other tracepoint commands."));
16109 set_cmd_completer (c, location_completer);
16110
1042e4c0 16111 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16112Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16113Convenience variable \"$tpnum\" contains the number of the\n\
16114last tracepoint set."));
16115
16116 add_info_alias ("tp", "tracepoints", 1);
16117
16118 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16119Delete specified tracepoints.\n\
16120Arguments are tracepoint numbers, separated by spaces.\n\
16121No argument means delete all tracepoints."),
16122 &deletelist);
7e20dfcd 16123 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16124
16125 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16126Disable specified tracepoints.\n\
16127Arguments are tracepoint numbers, separated by spaces.\n\
16128No argument means disable all tracepoints."),
16129 &disablelist);
16130 deprecate_cmd (c, "disable");
16131
16132 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16133Enable specified tracepoints.\n\
16134Arguments are tracepoint numbers, separated by spaces.\n\
16135No argument means enable all tracepoints."),
16136 &enablelist);
16137 deprecate_cmd (c, "enable");
16138
16139 add_com ("passcount", class_trace, trace_pass_command, _("\
16140Set the passcount for a tracepoint.\n\
16141The trace will end when the tracepoint has been passed 'count' times.\n\
16142Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16143if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16144
6149aea9
PA
16145 add_prefix_cmd ("save", class_breakpoint, save_command,
16146 _("Save breakpoint definitions as a script."),
16147 &save_cmdlist, "save ",
16148 0/*allow-unknown*/, &cmdlist);
16149
16150 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16151Save current breakpoint definitions as a script.\n\
cce7e648 16152This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16153catchpoints, tracepoints). Use the 'source' command in another debug\n\
16154session to restore them."),
16155 &save_cmdlist);
16156 set_cmd_completer (c, filename_completer);
16157
16158 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16159Save current tracepoint definitions as a script.\n\
6149aea9
PA
16160Use the 'source' command in another debug session to restore them."),
16161 &save_cmdlist);
1042e4c0
SS
16162 set_cmd_completer (c, filename_completer);
16163
6149aea9
PA
16164 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16165 deprecate_cmd (c, "save tracepoints");
16166
1bedd215 16167 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16168Breakpoint specific settings\n\
16169Configure various breakpoint-specific variables such as\n\
1bedd215 16170pending breakpoint behavior"),
fa8d40ab
JJ
16171 &breakpoint_set_cmdlist, "set breakpoint ",
16172 0/*allow-unknown*/, &setlist);
1bedd215 16173 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16174Breakpoint specific settings\n\
16175Configure various breakpoint-specific variables such as\n\
1bedd215 16176pending breakpoint behavior"),
fa8d40ab
JJ
16177 &breakpoint_show_cmdlist, "show breakpoint ",
16178 0/*allow-unknown*/, &showlist);
16179
7915a72c
AC
16180 add_setshow_auto_boolean_cmd ("pending", no_class,
16181 &pending_break_support, _("\
16182Set debugger's behavior regarding pending breakpoints."), _("\
16183Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16184If on, an unrecognized breakpoint location will cause gdb to create a\n\
16185pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16186an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16187user-query to see if a pending breakpoint should be created."),
2c5b56ce 16188 NULL,
920d2a44 16189 show_pending_break_support,
6e1d7d6c
AC
16190 &breakpoint_set_cmdlist,
16191 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16192
16193 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16194
16195 add_setshow_boolean_cmd ("auto-hw", no_class,
16196 &automatic_hardware_breakpoints, _("\
16197Set automatic usage of hardware breakpoints."), _("\
16198Show automatic usage of hardware breakpoints."), _("\
16199If set, the debugger will automatically use hardware breakpoints for\n\
16200breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16201a warning will be emitted for such breakpoints."),
16202 NULL,
16203 show_automatic_hardware_breakpoints,
16204 &breakpoint_set_cmdlist,
16205 &breakpoint_show_cmdlist);
74960c60 16206
a25a5a45
PA
16207 add_setshow_boolean_cmd ("always-inserted", class_support,
16208 &always_inserted_mode, _("\
74960c60
VP
16209Set mode for inserting breakpoints."), _("\
16210Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16211When this mode is on, breakpoints are inserted immediately as soon as\n\
16212they're created, kept inserted even when execution stops, and removed\n\
16213only when the user deletes them. When this mode is off (the default),\n\
16214breakpoints are inserted only when execution continues, and removed\n\
16215when execution stops."),
72d0e2c5
YQ
16216 NULL,
16217 &show_always_inserted_mode,
16218 &breakpoint_set_cmdlist,
16219 &breakpoint_show_cmdlist);
f1310107 16220
b775012e
LM
16221 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16222 condition_evaluation_enums,
16223 &condition_evaluation_mode_1, _("\
16224Set mode of breakpoint condition evaluation."), _("\
16225Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16226When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16227evaluated on the host's side by GDB. When it is set to \"target\",\n\
16228breakpoint conditions will be downloaded to the target (if the target\n\
16229supports such feature) and conditions will be evaluated on the target's side.\n\
16230If this is set to \"auto\" (default), this will be automatically set to\n\
16231\"target\" if it supports condition evaluation, otherwise it will\n\
16232be set to \"gdb\""),
16233 &set_condition_evaluation_mode,
16234 &show_condition_evaluation_mode,
16235 &breakpoint_set_cmdlist,
16236 &breakpoint_show_cmdlist);
16237
f1310107
TJB
16238 add_com ("break-range", class_breakpoint, break_range_command, _("\
16239Set a breakpoint for an address range.\n\
16240break-range START-LOCATION, END-LOCATION\n\
16241where START-LOCATION and END-LOCATION can be one of the following:\n\
16242 LINENUM, for that line in the current file,\n\
16243 FILE:LINENUM, for that line in that file,\n\
16244 +OFFSET, for that number of lines after the current line\n\
16245 or the start of the range\n\
16246 FUNCTION, for the first line in that function,\n\
16247 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16248 *ADDRESS, for the instruction at that address.\n\
16249\n\
16250The breakpoint will stop execution of the inferior whenever it executes\n\
16251an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16252range (including START-LOCATION and END-LOCATION)."));
16253
e7e0cddf
SS
16254 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16255Set a dynamic printf at specified line or function.\n\
16256dprintf location,format string,arg1,arg2,...\n\
16257location may be a line number, function name, or \"*\" and an address.\n\
16258If a line number is specified, break at start of code for that line.\n\
0e4777df 16259If a function is specified, break at start of code for that function."));
e7e0cddf
SS
16260 set_cmd_completer (c, location_completer);
16261
16262 add_setshow_enum_cmd ("dprintf-style", class_support,
16263 dprintf_style_enums, &dprintf_style, _("\
16264Set the style of usage for dynamic printf."), _("\
16265Show the style of usage for dynamic printf."), _("\
16266This setting chooses how GDB will do a dynamic printf.\n\
16267If the value is \"gdb\", then the printing is done by GDB to its own\n\
16268console, as with the \"printf\" command.\n\
16269If the value is \"call\", the print is done by calling a function in your\n\
16270program; by default printf(), but you can choose a different function or\n\
16271output stream by setting dprintf-function and dprintf-channel."),
16272 update_dprintf_commands, NULL,
16273 &setlist, &showlist);
16274
16275 dprintf_function = xstrdup ("printf");
16276 add_setshow_string_cmd ("dprintf-function", class_support,
16277 &dprintf_function, _("\
16278Set the function to use for dynamic printf"), _("\
16279Show the function to use for dynamic printf"), NULL,
16280 update_dprintf_commands, NULL,
16281 &setlist, &showlist);
16282
16283 dprintf_channel = xstrdup ("");
16284 add_setshow_string_cmd ("dprintf-channel", class_support,
16285 &dprintf_channel, _("\
16286Set the channel to use for dynamic printf"), _("\
16287Show the channel to use for dynamic printf"), NULL,
16288 update_dprintf_commands, NULL,
16289 &setlist, &showlist);
16290
d3ce09f5
SS
16291 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16292 &disconnected_dprintf, _("\
16293Set whether dprintf continues after GDB disconnects."), _("\
16294Show whether dprintf continues after GDB disconnects."), _("\
16295Use this to let dprintf commands continue to hit and produce output\n\
16296even if GDB disconnects or detaches from the target."),
16297 NULL,
16298 NULL,
16299 &setlist, &showlist);
16300
16301 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16302agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16303(target agent only) This is useful for formatted output in user-defined commands."));
16304
765dc015 16305 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16306
16307 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16308 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16309}
This page took 2.748331 seconds and 4 git commands to generate.