Call target_can_download_tracepoint if there are tracepoints to download
[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 69#include "format.h"
f00aae0f 70#include "location.h"
cfc31633 71#include "thread-fsm.h"
d3ce09f5 72
1042e4c0
SS
73/* readline include files */
74#include "readline/readline.h"
75#include "readline/history.h"
76
77/* readline defines this. */
78#undef savestring
79
034dad6f 80#include "mi/mi-common.h"
6dddc817 81#include "extension.h"
104c1213 82
e7e8980f
YQ
83/* Enums for exception-handling support. */
84enum exception_event_kind
85{
86 EX_EVENT_THROW,
591f19e8 87 EX_EVENT_RETHROW,
e7e8980f
YQ
88 EX_EVENT_CATCH
89};
90
4a64f543 91/* Prototypes for local functions. */
c906108c 92
a14ed312 93static void enable_delete_command (char *, int);
c906108c 94
a14ed312 95static void enable_once_command (char *, int);
c906108c 96
816338b5
SS
97static void enable_count_command (char *, int);
98
a14ed312 99static void disable_command (char *, int);
c906108c 100
a14ed312 101static void enable_command (char *, int);
c906108c 102
95a42b64
TT
103static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
104 void *),
105 void *);
c906108c 106
a14ed312 107static void ignore_command (char *, int);
c906108c 108
4efb68b1 109static int breakpoint_re_set_one (void *);
c906108c 110
348d480f
PA
111static void breakpoint_re_set_default (struct breakpoint *);
112
f00aae0f
KS
113static void
114 create_sals_from_location_default (const struct event_location *location,
115 struct linespec_result *canonical,
116 enum bptype type_wanted);
983af33b
SDJ
117
118static void create_breakpoints_sal_default (struct gdbarch *,
119 struct linespec_result *,
e7e0cddf 120 char *, char *, enum bptype,
983af33b
SDJ
121 enum bpdisp, int, int,
122 int,
123 const struct breakpoint_ops *,
44f238bb 124 int, int, int, unsigned);
983af33b 125
f00aae0f
KS
126static void decode_location_default (struct breakpoint *b,
127 const struct event_location *location,
128 struct symtabs_and_lines *sals);
983af33b 129
a14ed312 130static void clear_command (char *, int);
c906108c 131
a14ed312 132static void catch_command (char *, int);
c906108c 133
a9634178 134static int can_use_hardware_watchpoint (struct value *);
c906108c 135
98deb0da 136static void break_command_1 (char *, int, int);
c906108c 137
a14ed312 138static void mention (struct breakpoint *);
c906108c 139
348d480f
PA
140static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
141 enum bptype,
c0a91b2b 142 const struct breakpoint_ops *);
3742cc8b
YQ
143static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
144 const struct symtab_and_line *);
145
4a64f543
MS
146/* This function is used in gdbtk sources and thus can not be made
147 static. */
63c252f8 148struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 149 struct symtab_and_line,
c0a91b2b
TT
150 enum bptype,
151 const struct breakpoint_ops *);
c906108c 152
06edf0c0
PA
153static struct breakpoint *
154 momentary_breakpoint_from_master (struct breakpoint *orig,
155 enum bptype type,
a1aa2221
LM
156 const struct breakpoint_ops *ops,
157 int loc_enabled);
06edf0c0 158
76897487
KB
159static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
160
a6d9a66e
UW
161static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
162 CORE_ADDR bpaddr,
88f7da05 163 enum bptype bptype);
76897487 164
6c95b8df
PA
165static void describe_other_breakpoints (struct gdbarch *,
166 struct program_space *, CORE_ADDR,
5af949e3 167 struct obj_section *, int);
c906108c 168
85d721b8
PA
169static int watchpoint_locations_match (struct bp_location *loc1,
170 struct bp_location *loc2);
171
f1310107
TJB
172static int breakpoint_location_address_match (struct bp_location *bl,
173 struct address_space *aspace,
174 CORE_ADDR addr);
175
a14ed312 176static void breakpoints_info (char *, int);
c906108c 177
d77f58be
SS
178static void watchpoints_info (char *, int);
179
e5a67952
MS
180static int breakpoint_1 (char *, int,
181 int (*) (const struct breakpoint *));
c906108c 182
4efb68b1 183static int breakpoint_cond_eval (void *);
c906108c 184
4efb68b1 185static void cleanup_executing_breakpoints (void *);
c906108c 186
a14ed312 187static void commands_command (char *, int);
c906108c 188
a14ed312 189static void condition_command (char *, int);
c906108c 190
c5aa993b
JM
191typedef enum
192 {
193 mark_inserted,
194 mark_uninserted
195 }
196insertion_state_t;
c906108c 197
0bde7532 198static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 199static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 200
e514a9d6 201static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 202
4efb68b1 203static int watchpoint_check (void *);
c906108c 204
a14ed312 205static void maintenance_info_breakpoints (char *, int);
c906108c 206
a14ed312 207static int hw_breakpoint_used_count (void);
c906108c 208
a1398e0c
PA
209static int hw_watchpoint_use_count (struct breakpoint *);
210
211static int hw_watchpoint_used_count_others (struct breakpoint *except,
212 enum bptype type,
213 int *other_type_used);
c906108c 214
a14ed312 215static void hbreak_command (char *, int);
c906108c 216
a14ed312 217static void thbreak_command (char *, int);
c906108c 218
816338b5
SS
219static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
220 int count);
c906108c 221
a14ed312 222static void stop_command (char *arg, int from_tty);
7a292a7a 223
a14ed312 224static void stopin_command (char *arg, int from_tty);
7a292a7a 225
a14ed312 226static void stopat_command (char *arg, int from_tty);
7a292a7a 227
a14ed312 228static void tcatch_command (char *arg, int from_tty);
7a292a7a 229
fe3f5fa8 230static void free_bp_location (struct bp_location *loc);
f431efe5
PA
231static void incref_bp_location (struct bp_location *loc);
232static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 233
39d61571 234static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 235
44702360
PA
236/* update_global_location_list's modes of operation wrt to whether to
237 insert locations now. */
238enum ugll_insert_mode
239{
240 /* Don't insert any breakpoint locations into the inferior, only
241 remove already-inserted locations that no longer should be
242 inserted. Functions that delete a breakpoint or breakpoints
243 should specify this mode, so that deleting a breakpoint doesn't
244 have the side effect of inserting the locations of other
245 breakpoints that are marked not-inserted, but should_be_inserted
246 returns true on them.
247
248 This behavior is useful is situations close to tear-down -- e.g.,
249 after an exec, while the target still has execution, but
250 breakpoint shadows of the previous executable image should *NOT*
251 be restored to the new image; or before detaching, where the
252 target still has execution and wants to delete breakpoints from
253 GDB's lists, and all breakpoints had already been removed from
254 the inferior. */
255 UGLL_DONT_INSERT,
256
a25a5a45
PA
257 /* May insert breakpoints iff breakpoints_should_be_inserted_now
258 claims breakpoints should be inserted now. */
04086b45
PA
259 UGLL_MAY_INSERT,
260
a25a5a45
PA
261 /* Insert locations now, irrespective of
262 breakpoints_should_be_inserted_now. E.g., say all threads are
263 stopped right now, and the user did "continue". We need to
264 insert breakpoints _before_ resuming the target, but
265 UGLL_MAY_INSERT wouldn't insert them, because
266 breakpoints_should_be_inserted_now returns false at that point,
267 as no thread is running yet. */
04086b45 268 UGLL_INSERT
44702360
PA
269};
270
271static void update_global_location_list (enum ugll_insert_mode);
a5606eee 272
44702360 273static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 274
d77f58be 275static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
276
277static void insert_breakpoint_locations (void);
a5606eee 278
1042e4c0
SS
279static void tracepoints_info (char *, int);
280
281static void delete_trace_command (char *, int);
282
283static void enable_trace_command (char *, int);
284
285static void disable_trace_command (char *, int);
286
287static void trace_pass_command (char *, int);
288
558a9d82
YQ
289static void set_tracepoint_count (int num);
290
9c06b0b4
TJB
291static int is_masked_watchpoint (const struct breakpoint *b);
292
b775012e
LM
293static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
294
983af33b
SDJ
295/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
296 otherwise. */
297
298static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 299
2060206e
PA
300/* The breakpoint_ops structure to be inherited by all breakpoint_ops
301 that are implemented on top of software or hardware breakpoints
302 (user breakpoints, internal and momentary breakpoints, etc.). */
303static struct breakpoint_ops bkpt_base_breakpoint_ops;
304
305/* Internal breakpoints class type. */
06edf0c0 306static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
307
308/* Momentary breakpoints class type. */
06edf0c0
PA
309static struct breakpoint_ops momentary_breakpoint_ops;
310
e2e4d78b
JK
311/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
312static struct breakpoint_ops longjmp_breakpoint_ops;
313
2060206e
PA
314/* The breakpoint_ops structure to be used in regular user created
315 breakpoints. */
316struct breakpoint_ops bkpt_breakpoint_ops;
317
55aa24fb
SDJ
318/* Breakpoints set on probes. */
319static struct breakpoint_ops bkpt_probe_breakpoint_ops;
320
e7e0cddf 321/* Dynamic printf class type. */
c5867ab6 322struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 323
d3ce09f5
SS
324/* The style in which to perform a dynamic printf. This is a user
325 option because different output options have different tradeoffs;
326 if GDB does the printing, there is better error handling if there
327 is a problem with any of the arguments, but using an inferior
328 function lets you have special-purpose printers and sending of
329 output to the same place as compiled-in print functions. */
330
331static const char dprintf_style_gdb[] = "gdb";
332static const char dprintf_style_call[] = "call";
333static const char dprintf_style_agent[] = "agent";
334static const char *const dprintf_style_enums[] = {
335 dprintf_style_gdb,
336 dprintf_style_call,
337 dprintf_style_agent,
338 NULL
339};
340static const char *dprintf_style = dprintf_style_gdb;
341
342/* The function to use for dynamic printf if the preferred style is to
343 call into the inferior. The value is simply a string that is
344 copied into the command, so it can be anything that GDB can
345 evaluate to a callable address, not necessarily a function name. */
346
347static char *dprintf_function = "";
348
349/* The channel to use for dynamic printf if the preferred style is to
350 call into the inferior; if a nonempty string, it will be passed to
351 the call as the first argument, with the format string as the
352 second. As with the dprintf function, this can be anything that
353 GDB knows how to evaluate, so in addition to common choices like
354 "stderr", this could be an app-specific expression like
355 "mystreams[curlogger]". */
356
357static char *dprintf_channel = "";
358
359/* True if dprintf commands should continue to operate even if GDB
360 has disconnected. */
361static int disconnected_dprintf = 1;
362
5cea2a26
PA
363/* A reference-counted struct command_line. This lets multiple
364 breakpoints share a single command list. */
365struct counted_command_line
366{
367 /* The reference count. */
368 int refc;
369
370 /* The command list. */
371 struct command_line *commands;
372};
373
374struct command_line *
375breakpoint_commands (struct breakpoint *b)
376{
377 return b->commands ? b->commands->commands : NULL;
378}
3daf8fe5 379
f3b1572e
PA
380/* Flag indicating that a command has proceeded the inferior past the
381 current breakpoint. */
382
383static int breakpoint_proceeded;
384
956a9fb9 385const char *
2cec12e5
AR
386bpdisp_text (enum bpdisp disp)
387{
4a64f543
MS
388 /* NOTE: the following values are a part of MI protocol and
389 represent values of 'disp' field returned when inferior stops at
390 a breakpoint. */
bc043ef3 391 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 392
2cec12e5
AR
393 return bpdisps[(int) disp];
394}
c906108c 395
4a64f543 396/* Prototypes for exported functions. */
c906108c 397/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 398 if such is available. */
c906108c
SS
399static int can_use_hw_watchpoints;
400
920d2a44
AC
401static void
402show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
403 struct cmd_list_element *c,
404 const char *value)
405{
3e43a32a
MS
406 fprintf_filtered (file,
407 _("Debugger's willingness to use "
408 "watchpoint hardware is %s.\n"),
920d2a44
AC
409 value);
410}
411
fa8d40ab
JJ
412/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
413 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 414 for unrecognized breakpoint locations.
fa8d40ab
JJ
415 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
416static enum auto_boolean pending_break_support;
920d2a44
AC
417static void
418show_pending_break_support (struct ui_file *file, int from_tty,
419 struct cmd_list_element *c,
420 const char *value)
421{
3e43a32a
MS
422 fprintf_filtered (file,
423 _("Debugger's behavior regarding "
424 "pending breakpoints is %s.\n"),
920d2a44
AC
425 value);
426}
fa8d40ab 427
765dc015 428/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 429 set with "break" but falling in read-only memory.
765dc015
VP
430 If 0, gdb will warn about such breakpoints, but won't automatically
431 use hardware breakpoints. */
432static int automatic_hardware_breakpoints;
433static void
434show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
435 struct cmd_list_element *c,
436 const char *value)
437{
3e43a32a
MS
438 fprintf_filtered (file,
439 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
440 value);
441}
442
a25a5a45
PA
443/* If on, GDB keeps breakpoints inserted even if the inferior is
444 stopped, and immediately inserts any new breakpoints as soon as
445 they're created. If off (default), GDB keeps breakpoints off of
446 the target as long as possible. That is, it delays inserting
447 breakpoints until the next resume, and removes them again when the
448 target fully stops. This is a bit safer in case GDB crashes while
449 processing user input. */
450static int always_inserted_mode = 0;
72d0e2c5 451
33e5cbd6 452static void
74960c60 453show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 454 struct cmd_list_element *c, const char *value)
74960c60 455{
a25a5a45
PA
456 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
457 value);
74960c60
VP
458}
459
b57bacec
PA
460/* See breakpoint.h. */
461
33e5cbd6 462int
a25a5a45 463breakpoints_should_be_inserted_now (void)
33e5cbd6 464{
a25a5a45
PA
465 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
466 {
467 /* If breakpoints are global, they should be inserted even if no
468 thread under gdb's control is running, or even if there are
469 no threads under GDB's control yet. */
470 return 1;
471 }
472 else if (target_has_execution)
473 {
372316f1
PA
474 struct thread_info *tp;
475
a25a5a45
PA
476 if (always_inserted_mode)
477 {
478 /* The user wants breakpoints inserted even if all threads
479 are stopped. */
480 return 1;
481 }
482
b57bacec
PA
483 if (threads_are_executing ())
484 return 1;
372316f1
PA
485
486 /* Don't remove breakpoints yet if, even though all threads are
487 stopped, we still have events to process. */
488 ALL_NON_EXITED_THREADS (tp)
489 if (tp->resumed
490 && tp->suspend.waitstatus_pending_p)
491 return 1;
a25a5a45
PA
492 }
493 return 0;
33e5cbd6 494}
765dc015 495
b775012e
LM
496static const char condition_evaluation_both[] = "host or target";
497
498/* Modes for breakpoint condition evaluation. */
499static const char condition_evaluation_auto[] = "auto";
500static const char condition_evaluation_host[] = "host";
501static const char condition_evaluation_target[] = "target";
502static const char *const condition_evaluation_enums[] = {
503 condition_evaluation_auto,
504 condition_evaluation_host,
505 condition_evaluation_target,
506 NULL
507};
508
509/* Global that holds the current mode for breakpoint condition evaluation. */
510static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
511
512/* Global that we use to display information to the user (gets its value from
513 condition_evaluation_mode_1. */
514static const char *condition_evaluation_mode = condition_evaluation_auto;
515
516/* Translate a condition evaluation mode MODE into either "host"
517 or "target". This is used mostly to translate from "auto" to the
518 real setting that is being used. It returns the translated
519 evaluation mode. */
520
521static const char *
522translate_condition_evaluation_mode (const char *mode)
523{
524 if (mode == condition_evaluation_auto)
525 {
526 if (target_supports_evaluation_of_breakpoint_conditions ())
527 return condition_evaluation_target;
528 else
529 return condition_evaluation_host;
530 }
531 else
532 return mode;
533}
534
535/* Discovers what condition_evaluation_auto translates to. */
536
537static const char *
538breakpoint_condition_evaluation_mode (void)
539{
540 return translate_condition_evaluation_mode (condition_evaluation_mode);
541}
542
543/* Return true if GDB should evaluate breakpoint conditions or false
544 otherwise. */
545
546static int
547gdb_evaluates_breakpoint_condition_p (void)
548{
549 const char *mode = breakpoint_condition_evaluation_mode ();
550
551 return (mode == condition_evaluation_host);
552}
553
a14ed312 554void _initialize_breakpoint (void);
c906108c 555
c906108c
SS
556/* Are we executing breakpoint commands? */
557static int executing_breakpoint_commands;
558
c02f5703
MS
559/* Are overlay event breakpoints enabled? */
560static int overlay_events_enabled;
561
e09342b5
TJB
562/* See description in breakpoint.h. */
563int target_exact_watchpoints = 0;
564
c906108c 565/* Walk the following statement or block through all breakpoints.
e5dd4106 566 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 567 current breakpoint. */
c906108c 568
5c44784c 569#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 570
5c44784c
JM
571#define ALL_BREAKPOINTS_SAFE(B,TMP) \
572 for (B = breakpoint_chain; \
573 B ? (TMP=B->next, 1): 0; \
574 B = TMP)
c906108c 575
4a64f543
MS
576/* Similar iterator for the low-level breakpoints. SAFE variant is
577 not provided so update_global_location_list must not be called
578 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 579
876fa593
JK
580#define ALL_BP_LOCATIONS(B,BP_TMP) \
581 for (BP_TMP = bp_location; \
582 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
583 BP_TMP++)
7cc221ef 584
b775012e
LM
585/* Iterates through locations with address ADDRESS for the currently selected
586 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
587 to where the loop should start from.
588 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
589 appropriate location to start with. */
590
591#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
592 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
593 BP_LOCP_TMP = BP_LOCP_START; \
594 BP_LOCP_START \
595 && (BP_LOCP_TMP < bp_location + bp_location_count \
596 && (*BP_LOCP_TMP)->address == ADDRESS); \
597 BP_LOCP_TMP++)
598
1042e4c0
SS
599/* Iterator for tracepoints only. */
600
601#define ALL_TRACEPOINTS(B) \
602 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 603 if (is_tracepoint (B))
1042e4c0 604
7cc221ef 605/* Chains of all breakpoints defined. */
c906108c
SS
606
607struct breakpoint *breakpoint_chain;
608
876fa593
JK
609/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
610
611static struct bp_location **bp_location;
612
613/* Number of elements of BP_LOCATION. */
614
615static unsigned bp_location_count;
616
4a64f543
MS
617/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
618 ADDRESS for the current elements of BP_LOCATION which get a valid
619 result from bp_location_has_shadow. You can use it for roughly
620 limiting the subrange of BP_LOCATION to scan for shadow bytes for
621 an address you need to read. */
876fa593
JK
622
623static CORE_ADDR bp_location_placed_address_before_address_max;
624
4a64f543
MS
625/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
626 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
627 BP_LOCATION which get a valid result from bp_location_has_shadow.
628 You can use it for roughly limiting the subrange of BP_LOCATION to
629 scan for shadow bytes for an address you need to read. */
876fa593
JK
630
631static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 632
4a64f543
MS
633/* The locations that no longer correspond to any breakpoint, unlinked
634 from bp_location array, but for which a hit may still be reported
635 by a target. */
20874c92
VP
636VEC(bp_location_p) *moribund_locations = NULL;
637
c906108c
SS
638/* Number of last breakpoint made. */
639
95a42b64
TT
640static int breakpoint_count;
641
86b17b60
PA
642/* The value of `breakpoint_count' before the last command that
643 created breakpoints. If the last (break-like) command created more
644 than one breakpoint, then the difference between BREAKPOINT_COUNT
645 and PREV_BREAKPOINT_COUNT is more than one. */
646static int prev_breakpoint_count;
c906108c 647
1042e4c0
SS
648/* Number of last tracepoint made. */
649
95a42b64 650static int tracepoint_count;
1042e4c0 651
6149aea9
PA
652static struct cmd_list_element *breakpoint_set_cmdlist;
653static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 654struct cmd_list_element *save_cmdlist;
6149aea9 655
badd37ce
SDJ
656/* See declaration at breakpoint.h. */
657
658struct breakpoint *
659breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
660 void *user_data)
661{
662 struct breakpoint *b = NULL;
663
664 ALL_BREAKPOINTS (b)
665 {
666 if (func (b, user_data) != 0)
667 break;
668 }
669
670 return b;
671}
672
468d015d
JJ
673/* Return whether a breakpoint is an active enabled breakpoint. */
674static int
675breakpoint_enabled (struct breakpoint *b)
676{
0d381245 677 return (b->enable_state == bp_enabled);
468d015d
JJ
678}
679
c906108c
SS
680/* Set breakpoint count to NUM. */
681
95a42b64 682static void
fba45db2 683set_breakpoint_count (int num)
c906108c 684{
86b17b60 685 prev_breakpoint_count = breakpoint_count;
c906108c 686 breakpoint_count = num;
4fa62494 687 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
688}
689
86b17b60
PA
690/* Used by `start_rbreak_breakpoints' below, to record the current
691 breakpoint count before "rbreak" creates any breakpoint. */
692static int rbreak_start_breakpoint_count;
693
95a42b64
TT
694/* Called at the start an "rbreak" command to record the first
695 breakpoint made. */
86b17b60 696
95a42b64
TT
697void
698start_rbreak_breakpoints (void)
699{
86b17b60 700 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
701}
702
703/* Called at the end of an "rbreak" command to record the last
704 breakpoint made. */
86b17b60 705
95a42b64
TT
706void
707end_rbreak_breakpoints (void)
708{
86b17b60 709 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
710}
711
4a64f543 712/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
713
714void
fba45db2 715clear_breakpoint_hit_counts (void)
c906108c
SS
716{
717 struct breakpoint *b;
718
719 ALL_BREAKPOINTS (b)
720 b->hit_count = 0;
721}
722
9add0f1b
TT
723/* Allocate a new counted_command_line with reference count of 1.
724 The new structure owns COMMANDS. */
725
726static struct counted_command_line *
727alloc_counted_command_line (struct command_line *commands)
728{
8d749320 729 struct counted_command_line *result = XNEW (struct counted_command_line);
cc59ec59 730
9add0f1b
TT
731 result->refc = 1;
732 result->commands = commands;
8d749320 733
9add0f1b
TT
734 return result;
735}
736
737/* Increment reference count. This does nothing if CMD is NULL. */
738
739static void
740incref_counted_command_line (struct counted_command_line *cmd)
741{
742 if (cmd)
743 ++cmd->refc;
744}
745
746/* Decrement reference count. If the reference count reaches 0,
747 destroy the counted_command_line. Sets *CMDP to NULL. This does
748 nothing if *CMDP is NULL. */
749
750static void
751decref_counted_command_line (struct counted_command_line **cmdp)
752{
753 if (*cmdp)
754 {
755 if (--(*cmdp)->refc == 0)
756 {
757 free_command_lines (&(*cmdp)->commands);
758 xfree (*cmdp);
759 }
760 *cmdp = NULL;
761 }
762}
763
764/* A cleanup function that calls decref_counted_command_line. */
765
766static void
767do_cleanup_counted_command_line (void *arg)
768{
769 decref_counted_command_line (arg);
770}
771
772/* Create a cleanup that calls decref_counted_command_line on the
773 argument. */
774
775static struct cleanup *
776make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
777{
778 return make_cleanup (do_cleanup_counted_command_line, cmdp);
779}
780
c906108c 781\f
48cb2d85
VP
782/* Return the breakpoint with the specified number, or NULL
783 if the number does not refer to an existing breakpoint. */
784
785struct breakpoint *
786get_breakpoint (int num)
787{
788 struct breakpoint *b;
789
790 ALL_BREAKPOINTS (b)
791 if (b->number == num)
792 return b;
793
794 return NULL;
795}
5c44784c 796
c906108c 797\f
adc36818 798
b775012e
LM
799/* Mark locations as "conditions have changed" in case the target supports
800 evaluating conditions on its side. */
801
802static void
803mark_breakpoint_modified (struct breakpoint *b)
804{
805 struct bp_location *loc;
806
807 /* This is only meaningful if the target is
808 evaluating conditions and if the user has
809 opted for condition evaluation on the target's
810 side. */
811 if (gdb_evaluates_breakpoint_condition_p ()
812 || !target_supports_evaluation_of_breakpoint_conditions ())
813 return;
814
815 if (!is_breakpoint (b))
816 return;
817
818 for (loc = b->loc; loc; loc = loc->next)
819 loc->condition_changed = condition_modified;
820}
821
822/* Mark location as "conditions have changed" in case the target supports
823 evaluating conditions on its side. */
824
825static void
826mark_breakpoint_location_modified (struct bp_location *loc)
827{
828 /* This is only meaningful if the target is
829 evaluating conditions and if the user has
830 opted for condition evaluation on the target's
831 side. */
832 if (gdb_evaluates_breakpoint_condition_p ()
833 || !target_supports_evaluation_of_breakpoint_conditions ())
834
835 return;
836
837 if (!is_breakpoint (loc->owner))
838 return;
839
840 loc->condition_changed = condition_modified;
841}
842
843/* Sets the condition-evaluation mode using the static global
844 condition_evaluation_mode. */
845
846static void
847set_condition_evaluation_mode (char *args, int from_tty,
848 struct cmd_list_element *c)
849{
b775012e
LM
850 const char *old_mode, *new_mode;
851
852 if ((condition_evaluation_mode_1 == condition_evaluation_target)
853 && !target_supports_evaluation_of_breakpoint_conditions ())
854 {
855 condition_evaluation_mode_1 = condition_evaluation_mode;
856 warning (_("Target does not support breakpoint condition evaluation.\n"
857 "Using host evaluation mode instead."));
858 return;
859 }
860
861 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
862 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
863
abf1152a
JK
864 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
865 settings was "auto". */
866 condition_evaluation_mode = condition_evaluation_mode_1;
867
b775012e
LM
868 /* Only update the mode if the user picked a different one. */
869 if (new_mode != old_mode)
870 {
871 struct bp_location *loc, **loc_tmp;
872 /* If the user switched to a different evaluation mode, we
873 need to synch the changes with the target as follows:
874
875 "host" -> "target": Send all (valid) conditions to the target.
876 "target" -> "host": Remove all the conditions from the target.
877 */
878
b775012e
LM
879 if (new_mode == condition_evaluation_target)
880 {
881 /* Mark everything modified and synch conditions with the
882 target. */
883 ALL_BP_LOCATIONS (loc, loc_tmp)
884 mark_breakpoint_location_modified (loc);
885 }
886 else
887 {
888 /* Manually mark non-duplicate locations to synch conditions
889 with the target. We do this to remove all the conditions the
890 target knows about. */
891 ALL_BP_LOCATIONS (loc, loc_tmp)
892 if (is_breakpoint (loc->owner) && loc->inserted)
893 loc->needs_update = 1;
894 }
895
896 /* Do the update. */
44702360 897 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
898 }
899
900 return;
901}
902
903/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
904 what "auto" is translating to. */
905
906static void
907show_condition_evaluation_mode (struct ui_file *file, int from_tty,
908 struct cmd_list_element *c, const char *value)
909{
910 if (condition_evaluation_mode == condition_evaluation_auto)
911 fprintf_filtered (file,
912 _("Breakpoint condition evaluation "
913 "mode is %s (currently %s).\n"),
914 value,
915 breakpoint_condition_evaluation_mode ());
916 else
917 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
918 value);
919}
920
921/* A comparison function for bp_location AP and BP that is used by
922 bsearch. This comparison function only cares about addresses, unlike
923 the more general bp_location_compare function. */
924
925static int
926bp_location_compare_addrs (const void *ap, const void *bp)
927{
928 struct bp_location *a = *(void **) ap;
929 struct bp_location *b = *(void **) bp;
930
931 if (a->address == b->address)
932 return 0;
933 else
934 return ((a->address > b->address) - (a->address < b->address));
935}
936
937/* Helper function to skip all bp_locations with addresses
938 less than ADDRESS. It returns the first bp_location that
939 is greater than or equal to ADDRESS. If none is found, just
940 return NULL. */
941
942static struct bp_location **
943get_first_locp_gte_addr (CORE_ADDR address)
944{
945 struct bp_location dummy_loc;
946 struct bp_location *dummy_locp = &dummy_loc;
947 struct bp_location **locp_found = NULL;
948
949 /* Initialize the dummy location's address field. */
950 memset (&dummy_loc, 0, sizeof (struct bp_location));
951 dummy_loc.address = address;
952
953 /* Find a close match to the first location at ADDRESS. */
954 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
955 sizeof (struct bp_location **),
956 bp_location_compare_addrs);
957
958 /* Nothing was found, nothing left to do. */
959 if (locp_found == NULL)
960 return NULL;
961
962 /* We may have found a location that is at ADDRESS but is not the first in the
963 location's list. Go backwards (if possible) and locate the first one. */
964 while ((locp_found - 1) >= bp_location
965 && (*(locp_found - 1))->address == address)
966 locp_found--;
967
968 return locp_found;
969}
970
adc36818 971void
7a26bd4d 972set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
973 int from_tty)
974{
3a5c3e22
PA
975 xfree (b->cond_string);
976 b->cond_string = NULL;
adc36818 977
3a5c3e22 978 if (is_watchpoint (b))
adc36818 979 {
3a5c3e22
PA
980 struct watchpoint *w = (struct watchpoint *) b;
981
982 xfree (w->cond_exp);
983 w->cond_exp = NULL;
984 }
985 else
986 {
987 struct bp_location *loc;
988
989 for (loc = b->loc; loc; loc = loc->next)
990 {
991 xfree (loc->cond);
992 loc->cond = NULL;
b775012e
LM
993
994 /* No need to free the condition agent expression
995 bytecode (if we have one). We will handle this
996 when we go through update_global_location_list. */
3a5c3e22 997 }
adc36818 998 }
adc36818
PM
999
1000 if (*exp == 0)
1001 {
1002 if (from_tty)
1003 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1004 }
1005 else
1006 {
bbc13ae3 1007 const char *arg = exp;
cc59ec59 1008
adc36818
PM
1009 /* I don't know if it matters whether this is the string the user
1010 typed in or the decompiled expression. */
1011 b->cond_string = xstrdup (arg);
1012 b->condition_not_parsed = 0;
1013
1014 if (is_watchpoint (b))
1015 {
3a5c3e22
PA
1016 struct watchpoint *w = (struct watchpoint *) b;
1017
adc36818
PM
1018 innermost_block = NULL;
1019 arg = exp;
1bb9788d 1020 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
1021 if (*arg)
1022 error (_("Junk at end of expression"));
3a5c3e22 1023 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1024 }
1025 else
1026 {
3a5c3e22
PA
1027 struct bp_location *loc;
1028
adc36818
PM
1029 for (loc = b->loc; loc; loc = loc->next)
1030 {
1031 arg = exp;
1032 loc->cond =
1bb9788d
TT
1033 parse_exp_1 (&arg, loc->address,
1034 block_for_pc (loc->address), 0);
adc36818
PM
1035 if (*arg)
1036 error (_("Junk at end of expression"));
1037 }
1038 }
1039 }
b775012e
LM
1040 mark_breakpoint_modified (b);
1041
8d3788bd 1042 observer_notify_breakpoint_modified (b);
adc36818
PM
1043}
1044
d55637df
TT
1045/* Completion for the "condition" command. */
1046
1047static VEC (char_ptr) *
6f937416
PA
1048condition_completer (struct cmd_list_element *cmd,
1049 const char *text, const char *word)
d55637df 1050{
6f937416 1051 const char *space;
d55637df 1052
6f937416
PA
1053 text = skip_spaces_const (text);
1054 space = skip_to_space_const (text);
d55637df
TT
1055 if (*space == '\0')
1056 {
1057 int len;
1058 struct breakpoint *b;
1059 VEC (char_ptr) *result = NULL;
1060
1061 if (text[0] == '$')
1062 {
1063 /* We don't support completion of history indices. */
1064 if (isdigit (text[1]))
1065 return NULL;
1066 return complete_internalvar (&text[1]);
1067 }
1068
1069 /* We're completing the breakpoint number. */
1070 len = strlen (text);
1071
1072 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1073 {
1074 char number[50];
1075
1076 xsnprintf (number, sizeof (number), "%d", b->number);
1077
1078 if (strncmp (number, text, len) == 0)
1079 VEC_safe_push (char_ptr, result, xstrdup (number));
1080 }
d55637df
TT
1081
1082 return result;
1083 }
1084
1085 /* We're completing the expression part. */
6f937416 1086 text = skip_spaces_const (space);
d55637df
TT
1087 return expression_completer (cmd, text, word);
1088}
1089
c906108c
SS
1090/* condition N EXP -- set break condition of breakpoint N to EXP. */
1091
1092static void
fba45db2 1093condition_command (char *arg, int from_tty)
c906108c 1094{
52f0bd74 1095 struct breakpoint *b;
c906108c 1096 char *p;
52f0bd74 1097 int bnum;
c906108c
SS
1098
1099 if (arg == 0)
e2e0b3e5 1100 error_no_arg (_("breakpoint number"));
c906108c
SS
1101
1102 p = arg;
1103 bnum = get_number (&p);
5c44784c 1104 if (bnum == 0)
8a3fe4f8 1105 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1106
1107 ALL_BREAKPOINTS (b)
1108 if (b->number == bnum)
2f069f6f 1109 {
6dddc817
DE
1110 /* Check if this breakpoint has a "stop" method implemented in an
1111 extension language. This method and conditions entered into GDB
1112 from the CLI are mutually exclusive. */
1113 const struct extension_language_defn *extlang
1114 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1115
1116 if (extlang != NULL)
1117 {
1118 error (_("Only one stop condition allowed. There is currently"
1119 " a %s stop condition defined for this breakpoint."),
1120 ext_lang_capitalized_name (extlang));
1121 }
2566ad2d 1122 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1123
1124 if (is_breakpoint (b))
44702360 1125 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1126
2f069f6f
JB
1127 return;
1128 }
c906108c 1129
8a3fe4f8 1130 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1131}
1132
a7bdde9e
VP
1133/* Check that COMMAND do not contain commands that are suitable
1134 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1135 Throw if any such commands is found. */
1136
a7bdde9e
VP
1137static void
1138check_no_tracepoint_commands (struct command_line *commands)
1139{
1140 struct command_line *c;
cc59ec59 1141
a7bdde9e
VP
1142 for (c = commands; c; c = c->next)
1143 {
1144 int i;
1145
1146 if (c->control_type == while_stepping_control)
3e43a32a
MS
1147 error (_("The 'while-stepping' command can "
1148 "only be used for tracepoints"));
a7bdde9e
VP
1149
1150 for (i = 0; i < c->body_count; ++i)
1151 check_no_tracepoint_commands ((c->body_list)[i]);
1152
1153 /* Not that command parsing removes leading whitespace and comment
4a64f543 1154 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1155 command directly. */
1156 if (strstr (c->line, "collect ") == c->line)
1157 error (_("The 'collect' command can only be used for tracepoints"));
1158
51661e93
VP
1159 if (strstr (c->line, "teval ") == c->line)
1160 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1161 }
1162}
1163
d77f58be
SS
1164/* Encapsulate tests for different types of tracepoints. */
1165
d9b3f62e
PA
1166static int
1167is_tracepoint_type (enum bptype type)
1168{
1169 return (type == bp_tracepoint
1170 || type == bp_fast_tracepoint
1171 || type == bp_static_tracepoint);
1172}
1173
a7bdde9e 1174int
d77f58be 1175is_tracepoint (const struct breakpoint *b)
a7bdde9e 1176{
d9b3f62e 1177 return is_tracepoint_type (b->type);
a7bdde9e 1178}
d9b3f62e 1179
e5dd4106 1180/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1181 breakpoint. This function will throw an exception if a problem is
1182 found. */
48cb2d85 1183
95a42b64
TT
1184static void
1185validate_commands_for_breakpoint (struct breakpoint *b,
1186 struct command_line *commands)
48cb2d85 1187{
d77f58be 1188 if (is_tracepoint (b))
a7bdde9e 1189 {
c9a6ce02 1190 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1191 struct command_line *c;
1192 struct command_line *while_stepping = 0;
c9a6ce02
PA
1193
1194 /* Reset the while-stepping step count. The previous commands
1195 might have included a while-stepping action, while the new
1196 ones might not. */
1197 t->step_count = 0;
1198
1199 /* We need to verify that each top-level element of commands is
1200 valid for tracepoints, that there's at most one
1201 while-stepping element, and that the while-stepping's body
1202 has valid tracing commands excluding nested while-stepping.
1203 We also need to validate the tracepoint action line in the
1204 context of the tracepoint --- validate_actionline actually
1205 has side effects, like setting the tracepoint's
1206 while-stepping STEP_COUNT, in addition to checking if the
1207 collect/teval actions parse and make sense in the
1208 tracepoint's context. */
a7bdde9e
VP
1209 for (c = commands; c; c = c->next)
1210 {
a7bdde9e
VP
1211 if (c->control_type == while_stepping_control)
1212 {
1213 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1214 error (_("The 'while-stepping' command "
1215 "cannot be used for fast tracepoint"));
0fb4aa4b 1216 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1217 error (_("The 'while-stepping' command "
1218 "cannot be used for static tracepoint"));
a7bdde9e
VP
1219
1220 if (while_stepping)
3e43a32a
MS
1221 error (_("The 'while-stepping' command "
1222 "can be used only once"));
a7bdde9e
VP
1223 else
1224 while_stepping = c;
1225 }
c9a6ce02
PA
1226
1227 validate_actionline (c->line, b);
a7bdde9e
VP
1228 }
1229 if (while_stepping)
1230 {
1231 struct command_line *c2;
1232
1233 gdb_assert (while_stepping->body_count == 1);
1234 c2 = while_stepping->body_list[0];
1235 for (; c2; c2 = c2->next)
1236 {
a7bdde9e
VP
1237 if (c2->control_type == while_stepping_control)
1238 error (_("The 'while-stepping' command cannot be nested"));
1239 }
1240 }
1241 }
1242 else
1243 {
1244 check_no_tracepoint_commands (commands);
1245 }
95a42b64
TT
1246}
1247
0fb4aa4b
PA
1248/* Return a vector of all the static tracepoints set at ADDR. The
1249 caller is responsible for releasing the vector. */
1250
1251VEC(breakpoint_p) *
1252static_tracepoints_here (CORE_ADDR addr)
1253{
1254 struct breakpoint *b;
1255 VEC(breakpoint_p) *found = 0;
1256 struct bp_location *loc;
1257
1258 ALL_BREAKPOINTS (b)
1259 if (b->type == bp_static_tracepoint)
1260 {
1261 for (loc = b->loc; loc; loc = loc->next)
1262 if (loc->address == addr)
1263 VEC_safe_push(breakpoint_p, found, b);
1264 }
1265
1266 return found;
1267}
1268
95a42b64 1269/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1270 validate that only allowed commands are included. */
95a42b64
TT
1271
1272void
4a64f543
MS
1273breakpoint_set_commands (struct breakpoint *b,
1274 struct command_line *commands)
95a42b64
TT
1275{
1276 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1277
9add0f1b
TT
1278 decref_counted_command_line (&b->commands);
1279 b->commands = alloc_counted_command_line (commands);
8d3788bd 1280 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1281}
1282
45a43567
TT
1283/* Set the internal `silent' flag on the breakpoint. Note that this
1284 is not the same as the "silent" that may appear in the breakpoint's
1285 commands. */
1286
1287void
1288breakpoint_set_silent (struct breakpoint *b, int silent)
1289{
1290 int old_silent = b->silent;
1291
1292 b->silent = silent;
1293 if (old_silent != silent)
8d3788bd 1294 observer_notify_breakpoint_modified (b);
45a43567
TT
1295}
1296
1297/* Set the thread for this breakpoint. If THREAD is -1, make the
1298 breakpoint work for any thread. */
1299
1300void
1301breakpoint_set_thread (struct breakpoint *b, int thread)
1302{
1303 int old_thread = b->thread;
1304
1305 b->thread = thread;
1306 if (old_thread != thread)
8d3788bd 1307 observer_notify_breakpoint_modified (b);
45a43567
TT
1308}
1309
1310/* Set the task for this breakpoint. If TASK is 0, make the
1311 breakpoint work for any task. */
1312
1313void
1314breakpoint_set_task (struct breakpoint *b, int task)
1315{
1316 int old_task = b->task;
1317
1318 b->task = task;
1319 if (old_task != task)
8d3788bd 1320 observer_notify_breakpoint_modified (b);
45a43567
TT
1321}
1322
95a42b64
TT
1323void
1324check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1325{
1326 struct breakpoint *b = closure;
cc59ec59 1327
6f937416 1328 validate_actionline (line, b);
a7bdde9e
VP
1329}
1330
95a42b64
TT
1331/* A structure used to pass information through
1332 map_breakpoint_numbers. */
1333
1334struct commands_info
1335{
1336 /* True if the command was typed at a tty. */
1337 int from_tty;
86b17b60
PA
1338
1339 /* The breakpoint range spec. */
1340 char *arg;
1341
95a42b64
TT
1342 /* Non-NULL if the body of the commands are being read from this
1343 already-parsed command. */
1344 struct command_line *control;
86b17b60 1345
95a42b64
TT
1346 /* The command lines read from the user, or NULL if they have not
1347 yet been read. */
1348 struct counted_command_line *cmd;
1349};
1350
1351/* A callback for map_breakpoint_numbers that sets the commands for
1352 commands_command. */
1353
c906108c 1354static void
95a42b64 1355do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1356{
95a42b64 1357 struct commands_info *info = data;
c906108c 1358
95a42b64
TT
1359 if (info->cmd == NULL)
1360 {
1361 struct command_line *l;
5c44784c 1362
95a42b64
TT
1363 if (info->control != NULL)
1364 l = copy_command_lines (info->control->body_list[0]);
1365 else
86b17b60
PA
1366 {
1367 struct cleanup *old_chain;
1368 char *str;
c5aa993b 1369
3e43a32a
MS
1370 str = xstrprintf (_("Type commands for breakpoint(s) "
1371 "%s, one per line."),
86b17b60
PA
1372 info->arg);
1373
1374 old_chain = make_cleanup (xfree, str);
1375
1376 l = read_command_lines (str,
1377 info->from_tty, 1,
d77f58be 1378 (is_tracepoint (b)
86b17b60
PA
1379 ? check_tracepoint_command : 0),
1380 b);
1381
1382 do_cleanups (old_chain);
1383 }
a7bdde9e 1384
95a42b64
TT
1385 info->cmd = alloc_counted_command_line (l);
1386 }
1387
1388 /* If a breakpoint was on the list more than once, we don't need to
1389 do anything. */
1390 if (b->commands != info->cmd)
1391 {
1392 validate_commands_for_breakpoint (b, info->cmd->commands);
1393 incref_counted_command_line (info->cmd);
1394 decref_counted_command_line (&b->commands);
1395 b->commands = info->cmd;
8d3788bd 1396 observer_notify_breakpoint_modified (b);
c5aa993b 1397 }
95a42b64
TT
1398}
1399
1400static void
4a64f543
MS
1401commands_command_1 (char *arg, int from_tty,
1402 struct command_line *control)
95a42b64
TT
1403{
1404 struct cleanup *cleanups;
1405 struct commands_info info;
1406
1407 info.from_tty = from_tty;
1408 info.control = control;
1409 info.cmd = NULL;
1410 /* If we read command lines from the user, then `info' will hold an
1411 extra reference to the commands that we must clean up. */
1412 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1413
1414 if (arg == NULL || !*arg)
1415 {
86b17b60 1416 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1417 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1418 breakpoint_count);
95a42b64
TT
1419 else if (breakpoint_count > 0)
1420 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1421 else
1422 {
1423 /* So that we don't try to free the incoming non-NULL
1424 argument in the cleanup below. Mapping breakpoint
1425 numbers will fail in this case. */
1426 arg = NULL;
1427 }
95a42b64 1428 }
9766ced4
SS
1429 else
1430 /* The command loop has some static state, so we need to preserve
1431 our argument. */
1432 arg = xstrdup (arg);
86b17b60
PA
1433
1434 if (arg != NULL)
1435 make_cleanup (xfree, arg);
1436
1437 info.arg = arg;
95a42b64
TT
1438
1439 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1440
1441 if (info.cmd == NULL)
1442 error (_("No breakpoints specified."));
1443
1444 do_cleanups (cleanups);
1445}
1446
1447static void
1448commands_command (char *arg, int from_tty)
1449{
1450 commands_command_1 (arg, from_tty, NULL);
c906108c 1451}
40c03ae8
EZ
1452
1453/* Like commands_command, but instead of reading the commands from
1454 input stream, takes them from an already parsed command structure.
1455
1456 This is used by cli-script.c to DTRT with breakpoint commands
1457 that are part of if and while bodies. */
1458enum command_control_type
1459commands_from_control_command (char *arg, struct command_line *cmd)
1460{
95a42b64
TT
1461 commands_command_1 (arg, 0, cmd);
1462 return simple_control;
40c03ae8 1463}
876fa593
JK
1464
1465/* Return non-zero if BL->TARGET_INFO contains valid information. */
1466
1467static int
1468bp_location_has_shadow (struct bp_location *bl)
1469{
1470 if (bl->loc_type != bp_loc_software_breakpoint)
1471 return 0;
1472 if (!bl->inserted)
1473 return 0;
1474 if (bl->target_info.shadow_len == 0)
e5dd4106 1475 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1476 return 0;
1477 return 1;
1478}
1479
9d497a19
PA
1480/* Update BUF, which is LEN bytes read from the target address
1481 MEMADDR, by replacing a memory breakpoint with its shadowed
1482 contents.
1483
1484 If READBUF is not NULL, this buffer must not overlap with the of
1485 the breakpoint location's shadow_contents buffer. Otherwise, a
1486 failed assertion internal error will be raised. */
1487
1488static void
1489one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1490 const gdb_byte *writebuf_org,
1491 ULONGEST memaddr, LONGEST len,
1492 struct bp_target_info *target_info,
1493 struct gdbarch *gdbarch)
1494{
1495 /* Now do full processing of the found relevant range of elements. */
1496 CORE_ADDR bp_addr = 0;
1497 int bp_size = 0;
1498 int bptoffset = 0;
1499
1500 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1501 current_program_space->aspace, 0))
1502 {
1503 /* The breakpoint is inserted in a different address space. */
1504 return;
1505 }
1506
1507 /* Addresses and length of the part of the breakpoint that
1508 we need to copy. */
1509 bp_addr = target_info->placed_address;
1510 bp_size = target_info->shadow_len;
1511
1512 if (bp_addr + bp_size <= memaddr)
1513 {
1514 /* The breakpoint is entirely before the chunk of memory we are
1515 reading. */
1516 return;
1517 }
1518
1519 if (bp_addr >= memaddr + len)
1520 {
1521 /* The breakpoint is entirely after the chunk of memory we are
1522 reading. */
1523 return;
1524 }
1525
1526 /* Offset within shadow_contents. */
1527 if (bp_addr < memaddr)
1528 {
1529 /* Only copy the second part of the breakpoint. */
1530 bp_size -= memaddr - bp_addr;
1531 bptoffset = memaddr - bp_addr;
1532 bp_addr = memaddr;
1533 }
1534
1535 if (bp_addr + bp_size > memaddr + len)
1536 {
1537 /* Only copy the first part of the breakpoint. */
1538 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1539 }
1540
1541 if (readbuf != NULL)
1542 {
1543 /* Verify that the readbuf buffer does not overlap with the
1544 shadow_contents buffer. */
1545 gdb_assert (target_info->shadow_contents >= readbuf + len
1546 || readbuf >= (target_info->shadow_contents
1547 + target_info->shadow_len));
1548
1549 /* Update the read buffer with this inserted breakpoint's
1550 shadow. */
1551 memcpy (readbuf + bp_addr - memaddr,
1552 target_info->shadow_contents + bptoffset, bp_size);
1553 }
1554 else
1555 {
1556 const unsigned char *bp;
0d5ed153
MR
1557 CORE_ADDR addr = target_info->reqstd_address;
1558 int placed_size;
9d497a19
PA
1559
1560 /* Update the shadow with what we want to write to memory. */
1561 memcpy (target_info->shadow_contents + bptoffset,
1562 writebuf_org + bp_addr - memaddr, bp_size);
1563
1564 /* Determine appropriate breakpoint contents and size for this
1565 address. */
0d5ed153 1566 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1567
1568 /* Update the final write buffer with this inserted
1569 breakpoint's INSN. */
1570 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1571 }
1572}
1573
8defab1a 1574/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1575 by replacing any memory breakpoints with their shadowed contents.
1576
35c63cd8
JB
1577 If READBUF is not NULL, this buffer must not overlap with any of
1578 the breakpoint location's shadow_contents buffers. Otherwise,
1579 a failed assertion internal error will be raised.
1580
876fa593 1581 The range of shadowed area by each bp_location is:
35df4500
TJB
1582 bl->address - bp_location_placed_address_before_address_max
1583 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1584 The range we were requested to resolve shadows for is:
1585 memaddr ... memaddr + len
1586 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1587 memaddr + len <= (bl->address
1588 - bp_location_placed_address_before_address_max)
876fa593 1589 and:
35df4500 1590 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1591
8defab1a 1592void
f0ba3972
PA
1593breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1594 const gdb_byte *writebuf_org,
1595 ULONGEST memaddr, LONGEST len)
c906108c 1596{
4a64f543
MS
1597 /* Left boundary, right boundary and median element of our binary
1598 search. */
876fa593 1599 unsigned bc_l, bc_r, bc;
9d497a19 1600 size_t i;
876fa593 1601
4a64f543
MS
1602 /* Find BC_L which is a leftmost element which may affect BUF
1603 content. It is safe to report lower value but a failure to
1604 report higher one. */
876fa593
JK
1605
1606 bc_l = 0;
1607 bc_r = bp_location_count;
1608 while (bc_l + 1 < bc_r)
1609 {
35df4500 1610 struct bp_location *bl;
876fa593
JK
1611
1612 bc = (bc_l + bc_r) / 2;
35df4500 1613 bl = bp_location[bc];
876fa593 1614
4a64f543
MS
1615 /* Check first BL->ADDRESS will not overflow due to the added
1616 constant. Then advance the left boundary only if we are sure
1617 the BC element can in no way affect the BUF content (MEMADDR
1618 to MEMADDR + LEN range).
876fa593 1619
4a64f543
MS
1620 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1621 offset so that we cannot miss a breakpoint with its shadow
1622 range tail still reaching MEMADDR. */
c5aa993b 1623
35df4500
TJB
1624 if ((bl->address + bp_location_shadow_len_after_address_max
1625 >= bl->address)
1626 && (bl->address + bp_location_shadow_len_after_address_max
1627 <= memaddr))
876fa593
JK
1628 bc_l = bc;
1629 else
1630 bc_r = bc;
1631 }
1632
128070bb
PA
1633 /* Due to the binary search above, we need to make sure we pick the
1634 first location that's at BC_L's address. E.g., if there are
1635 multiple locations at the same address, BC_L may end up pointing
1636 at a duplicate location, and miss the "master"/"inserted"
1637 location. Say, given locations L1, L2 and L3 at addresses A and
1638 B:
1639
1640 L1@A, L2@A, L3@B, ...
1641
1642 BC_L could end up pointing at location L2, while the "master"
1643 location could be L1. Since the `loc->inserted' flag is only set
1644 on "master" locations, we'd forget to restore the shadow of L1
1645 and L2. */
1646 while (bc_l > 0
1647 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1648 bc_l--;
1649
876fa593
JK
1650 /* Now do full processing of the found relevant range of elements. */
1651
1652 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1653 {
35df4500 1654 struct bp_location *bl = bp_location[bc];
876fa593
JK
1655 CORE_ADDR bp_addr = 0;
1656 int bp_size = 0;
1657 int bptoffset = 0;
1658
35df4500
TJB
1659 /* bp_location array has BL->OWNER always non-NULL. */
1660 if (bl->owner->type == bp_none)
8a3fe4f8 1661 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1662 bl->owner->number);
ffce0d52 1663
e5dd4106 1664 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1665 content. */
1666
35df4500
TJB
1667 if (bl->address >= bp_location_placed_address_before_address_max
1668 && memaddr + len <= (bl->address
1669 - bp_location_placed_address_before_address_max))
876fa593
JK
1670 break;
1671
35df4500 1672 if (!bp_location_has_shadow (bl))
c5aa993b 1673 continue;
6c95b8df 1674
9d497a19
PA
1675 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1676 memaddr, len, &bl->target_info, bl->gdbarch);
1677 }
c906108c 1678}
9d497a19 1679
c906108c 1680\f
c5aa993b 1681
b775012e
LM
1682/* Return true if BPT is either a software breakpoint or a hardware
1683 breakpoint. */
1684
1685int
1686is_breakpoint (const struct breakpoint *bpt)
1687{
1688 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1689 || bpt->type == bp_hardware_breakpoint
1690 || bpt->type == bp_dprintf);
b775012e
LM
1691}
1692
60e1c644
PA
1693/* Return true if BPT is of any hardware watchpoint kind. */
1694
a5606eee 1695static int
d77f58be 1696is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1697{
1698 return (bpt->type == bp_hardware_watchpoint
1699 || bpt->type == bp_read_watchpoint
1700 || bpt->type == bp_access_watchpoint);
1701}
7270d8f2 1702
60e1c644
PA
1703/* Return true if BPT is of any watchpoint kind, hardware or
1704 software. */
1705
3a5c3e22 1706int
d77f58be 1707is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1708{
1709 return (is_hardware_watchpoint (bpt)
1710 || bpt->type == bp_watchpoint);
1711}
1712
3a5c3e22
PA
1713/* Returns true if the current thread and its running state are safe
1714 to evaluate or update watchpoint B. Watchpoints on local
1715 expressions need to be evaluated in the context of the thread that
1716 was current when the watchpoint was created, and, that thread needs
1717 to be stopped to be able to select the correct frame context.
1718 Watchpoints on global expressions can be evaluated on any thread,
1719 and in any state. It is presently left to the target allowing
1720 memory accesses when threads are running. */
f6bc2008
PA
1721
1722static int
3a5c3e22 1723watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1724{
d0d8b0c6
JK
1725 return (b->base.pspace == current_program_space
1726 && (ptid_equal (b->watchpoint_thread, null_ptid)
1727 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1728 && !is_executing (inferior_ptid))));
f6bc2008
PA
1729}
1730
d0fb5eae
JK
1731/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1732 associated bp_watchpoint_scope breakpoint. */
1733
1734static void
3a5c3e22 1735watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1736{
3a5c3e22 1737 struct breakpoint *b = &w->base;
d0fb5eae
JK
1738
1739 if (b->related_breakpoint != b)
1740 {
1741 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1742 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1743 b->related_breakpoint->disposition = disp_del_at_next_stop;
1744 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1745 b->related_breakpoint = b;
1746 }
1747 b->disposition = disp_del_at_next_stop;
1748}
1749
bb9d5f81
PP
1750/* Extract a bitfield value from value VAL using the bit parameters contained in
1751 watchpoint W. */
1752
1753static struct value *
1754extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1755{
1756 struct value *bit_val;
1757
1758 if (val == NULL)
1759 return NULL;
1760
1761 bit_val = allocate_value (value_type (val));
1762
1763 unpack_value_bitfield (bit_val,
1764 w->val_bitpos,
1765 w->val_bitsize,
1766 value_contents_for_printing (val),
1767 value_offset (val),
1768 val);
1769
1770 return bit_val;
1771}
1772
567e1b4e
JB
1773/* Assuming that B is a watchpoint:
1774 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1775 - Evaluate expression and store the result in B->val
567e1b4e
JB
1776 - Evaluate the condition if there is one, and store the result
1777 in b->loc->cond.
a5606eee
VP
1778 - Update the list of values that must be watched in B->loc.
1779
4a64f543
MS
1780 If the watchpoint disposition is disp_del_at_next_stop, then do
1781 nothing. If this is local watchpoint that is out of scope, delete
1782 it.
1783
1784 Even with `set breakpoint always-inserted on' the watchpoints are
1785 removed + inserted on each stop here. Normal breakpoints must
1786 never be removed because they might be missed by a running thread
1787 when debugging in non-stop mode. On the other hand, hardware
1788 watchpoints (is_hardware_watchpoint; processed here) are specific
1789 to each LWP since they are stored in each LWP's hardware debug
1790 registers. Therefore, such LWP must be stopped first in order to
1791 be able to modify its hardware watchpoints.
1792
1793 Hardware watchpoints must be reset exactly once after being
1794 presented to the user. It cannot be done sooner, because it would
1795 reset the data used to present the watchpoint hit to the user. And
1796 it must not be done later because it could display the same single
1797 watchpoint hit during multiple GDB stops. Note that the latter is
1798 relevant only to the hardware watchpoint types bp_read_watchpoint
1799 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1800 not user-visible - its hit is suppressed if the memory content has
1801 not changed.
1802
1803 The following constraints influence the location where we can reset
1804 hardware watchpoints:
1805
1806 * target_stopped_by_watchpoint and target_stopped_data_address are
1807 called several times when GDB stops.
1808
1809 [linux]
1810 * Multiple hardware watchpoints can be hit at the same time,
1811 causing GDB to stop. GDB only presents one hardware watchpoint
1812 hit at a time as the reason for stopping, and all the other hits
1813 are presented later, one after the other, each time the user
1814 requests the execution to be resumed. Execution is not resumed
1815 for the threads still having pending hit event stored in
1816 LWP_INFO->STATUS. While the watchpoint is already removed from
1817 the inferior on the first stop the thread hit event is kept being
1818 reported from its cached value by linux_nat_stopped_data_address
1819 until the real thread resume happens after the watchpoint gets
1820 presented and thus its LWP_INFO->STATUS gets reset.
1821
1822 Therefore the hardware watchpoint hit can get safely reset on the
1823 watchpoint removal from inferior. */
a79d3c27 1824
b40ce68a 1825static void
3a5c3e22 1826update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1827{
a5606eee 1828 int within_current_scope;
a5606eee 1829 struct frame_id saved_frame_id;
66076460 1830 int frame_saved;
a5606eee 1831
f6bc2008
PA
1832 /* If this is a local watchpoint, we only want to check if the
1833 watchpoint frame is in scope if the current thread is the thread
1834 that was used to create the watchpoint. */
1835 if (!watchpoint_in_thread_scope (b))
1836 return;
1837
3a5c3e22 1838 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1839 return;
1840
66076460 1841 frame_saved = 0;
a5606eee
VP
1842
1843 /* Determine if the watchpoint is within scope. */
1844 if (b->exp_valid_block == NULL)
1845 within_current_scope = 1;
1846 else
1847 {
b5db5dfc
UW
1848 struct frame_info *fi = get_current_frame ();
1849 struct gdbarch *frame_arch = get_frame_arch (fi);
1850 CORE_ADDR frame_pc = get_frame_pc (fi);
1851
c9cf6e20
MG
1852 /* If we're at a point where the stack has been destroyed
1853 (e.g. in a function epilogue), unwinding may not work
1854 properly. Do not attempt to recreate locations at this
b5db5dfc 1855 point. See similar comments in watchpoint_check. */
c9cf6e20 1856 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1857 return;
66076460
DJ
1858
1859 /* Save the current frame's ID so we can restore it after
1860 evaluating the watchpoint expression on its own frame. */
1861 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1862 took a frame parameter, so that we didn't have to change the
1863 selected frame. */
1864 frame_saved = 1;
1865 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1866
a5606eee
VP
1867 fi = frame_find_by_id (b->watchpoint_frame);
1868 within_current_scope = (fi != NULL);
1869 if (within_current_scope)
1870 select_frame (fi);
1871 }
1872
b5db5dfc
UW
1873 /* We don't free locations. They are stored in the bp_location array
1874 and update_global_location_list will eventually delete them and
1875 remove breakpoints if needed. */
3a5c3e22 1876 b->base.loc = NULL;
b5db5dfc 1877
a5606eee
VP
1878 if (within_current_scope && reparse)
1879 {
bbc13ae3 1880 const char *s;
d63d0675 1881
a5606eee
VP
1882 if (b->exp)
1883 {
1884 xfree (b->exp);
1885 b->exp = NULL;
1886 }
d63d0675 1887 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1888 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1889 /* If the meaning of expression itself changed, the old value is
1890 no longer relevant. We don't want to report a watchpoint hit
1891 to the user when the old value and the new value may actually
1892 be completely different objects. */
1893 value_free (b->val);
fa4727a6
DJ
1894 b->val = NULL;
1895 b->val_valid = 0;
60e1c644
PA
1896
1897 /* Note that unlike with breakpoints, the watchpoint's condition
1898 expression is stored in the breakpoint object, not in the
1899 locations (re)created below. */
3a5c3e22 1900 if (b->base.cond_string != NULL)
60e1c644
PA
1901 {
1902 if (b->cond_exp != NULL)
1903 {
1904 xfree (b->cond_exp);
1905 b->cond_exp = NULL;
1906 }
1907
3a5c3e22 1908 s = b->base.cond_string;
1bb9788d 1909 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1910 }
a5606eee 1911 }
a5606eee
VP
1912
1913 /* If we failed to parse the expression, for example because
1914 it refers to a global variable in a not-yet-loaded shared library,
1915 don't try to insert watchpoint. We don't automatically delete
1916 such watchpoint, though, since failure to parse expression
1917 is different from out-of-scope watchpoint. */
e8369a73 1918 if (!target_has_execution)
2d134ed3
PA
1919 {
1920 /* Without execution, memory can't change. No use to try and
1921 set watchpoint locations. The watchpoint will be reset when
1922 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1923 if (!can_use_hw_watchpoints)
1924 {
1925 if (b->base.ops->works_in_software_mode (&b->base))
1926 b->base.type = bp_watchpoint;
1927 else
638aa5a1
AB
1928 error (_("Can't set read/access watchpoint when "
1929 "hardware watchpoints are disabled."));
e8369a73 1930 }
2d134ed3
PA
1931 }
1932 else if (within_current_scope && b->exp)
a5606eee 1933 {
0cf6dd15 1934 int pc = 0;
fa4727a6 1935 struct value *val_chain, *v, *result, *next;
2d134ed3 1936 struct program_space *frame_pspace;
a5606eee 1937
3a1115a0 1938 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1939
a5606eee
VP
1940 /* Avoid setting b->val if it's already set. The meaning of
1941 b->val is 'the last value' user saw, and we should update
1942 it only if we reported that last value to user. As it
9c06b0b4
TJB
1943 happens, the code that reports it updates b->val directly.
1944 We don't keep track of the memory value for masked
1945 watchpoints. */
3a5c3e22 1946 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1947 {
bb9d5f81
PP
1948 if (b->val_bitsize != 0)
1949 {
1950 v = extract_bitfield_from_watchpoint_value (b, v);
1951 if (v != NULL)
1952 release_value (v);
1953 }
fa4727a6
DJ
1954 b->val = v;
1955 b->val_valid = 1;
1956 }
a5606eee 1957
2d134ed3
PA
1958 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1959
a5606eee 1960 /* Look at each value on the value chain. */
9fa40276 1961 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1962 {
1963 /* If it's a memory location, and GDB actually needed
1964 its contents to evaluate the expression, then we
fa4727a6
DJ
1965 must watch it. If the first value returned is
1966 still lazy, that means an error occurred reading it;
1967 watch it anyway in case it becomes readable. */
a5606eee 1968 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1969 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1970 {
1971 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1972
a5606eee
VP
1973 /* We only watch structs and arrays if user asked
1974 for it explicitly, never if they just happen to
1975 appear in the middle of some value chain. */
fa4727a6 1976 if (v == result
a5606eee
VP
1977 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1978 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1979 {
1980 CORE_ADDR addr;
f486487f 1981 enum target_hw_bp_type type;
a5606eee 1982 struct bp_location *loc, **tmp;
bb9d5f81
PP
1983 int bitpos = 0, bitsize = 0;
1984
1985 if (value_bitsize (v) != 0)
1986 {
1987 /* Extract the bit parameters out from the bitfield
1988 sub-expression. */
1989 bitpos = value_bitpos (v);
1990 bitsize = value_bitsize (v);
1991 }
1992 else if (v == result && b->val_bitsize != 0)
1993 {
1994 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1995 lvalue whose bit parameters are saved in the fields
1996 VAL_BITPOS and VAL_BITSIZE. */
1997 bitpos = b->val_bitpos;
1998 bitsize = b->val_bitsize;
1999 }
a5606eee 2000
42ae5230 2001 addr = value_address (v);
bb9d5f81
PP
2002 if (bitsize != 0)
2003 {
2004 /* Skip the bytes that don't contain the bitfield. */
2005 addr += bitpos / 8;
2006 }
2007
a5606eee 2008 type = hw_write;
3a5c3e22 2009 if (b->base.type == bp_read_watchpoint)
a5606eee 2010 type = hw_read;
3a5c3e22 2011 else if (b->base.type == bp_access_watchpoint)
a5606eee 2012 type = hw_access;
3a5c3e22
PA
2013
2014 loc = allocate_bp_location (&b->base);
2015 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2016 ;
2017 *tmp = loc;
a6d9a66e 2018 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2019
2020 loc->pspace = frame_pspace;
a5606eee 2021 loc->address = addr;
bb9d5f81
PP
2022
2023 if (bitsize != 0)
2024 {
2025 /* Just cover the bytes that make up the bitfield. */
2026 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2027 }
2028 else
2029 loc->length = TYPE_LENGTH (value_type (v));
2030
a5606eee
VP
2031 loc->watchpoint_type = type;
2032 }
2033 }
9fa40276
TJB
2034 }
2035
2036 /* Change the type of breakpoint between hardware assisted or
2037 an ordinary watchpoint depending on the hardware support
2038 and free hardware slots. REPARSE is set when the inferior
2039 is started. */
a9634178 2040 if (reparse)
9fa40276 2041 {
e09342b5 2042 int reg_cnt;
9fa40276
TJB
2043 enum bp_loc_type loc_type;
2044 struct bp_location *bl;
a5606eee 2045
a9634178 2046 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2047
2048 if (reg_cnt)
9fa40276
TJB
2049 {
2050 int i, target_resources_ok, other_type_used;
a1398e0c 2051 enum bptype type;
9fa40276 2052
a9634178
TJB
2053 /* Use an exact watchpoint when there's only one memory region to be
2054 watched, and only one debug register is needed to watch it. */
2055 b->exact = target_exact_watchpoints && reg_cnt == 1;
2056
9fa40276 2057 /* We need to determine how many resources are already
e09342b5
TJB
2058 used for all other hardware watchpoints plus this one
2059 to see if we still have enough resources to also fit
a1398e0c
PA
2060 this watchpoint in as well. */
2061
2062 /* If this is a software watchpoint, we try to turn it
2063 to a hardware one -- count resources as if B was of
2064 hardware watchpoint type. */
2065 type = b->base.type;
2066 if (type == bp_watchpoint)
2067 type = bp_hardware_watchpoint;
2068
2069 /* This watchpoint may or may not have been placed on
2070 the list yet at this point (it won't be in the list
2071 if we're trying to create it for the first time,
2072 through watch_command), so always account for it
2073 manually. */
2074
2075 /* Count resources used by all watchpoints except B. */
2076 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2077
2078 /* Add in the resources needed for B. */
2079 i += hw_watchpoint_use_count (&b->base);
2080
2081 target_resources_ok
2082 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2083 if (target_resources_ok <= 0)
a9634178 2084 {
3a5c3e22 2085 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2086
2087 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2088 error (_("Target does not support this type of "
2089 "hardware watchpoint."));
9c06b0b4
TJB
2090 else if (target_resources_ok < 0 && !sw_mode)
2091 error (_("There are not enough available hardware "
2092 "resources for this watchpoint."));
a1398e0c
PA
2093
2094 /* Downgrade to software watchpoint. */
2095 b->base.type = bp_watchpoint;
2096 }
2097 else
2098 {
2099 /* If this was a software watchpoint, we've just
2100 found we have enough resources to turn it to a
2101 hardware watchpoint. Otherwise, this is a
2102 nop. */
2103 b->base.type = type;
a9634178 2104 }
9fa40276 2105 }
3a5c3e22 2106 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2107 {
2108 if (!can_use_hw_watchpoints)
2109 error (_("Can't set read/access watchpoint when "
2110 "hardware watchpoints are disabled."));
2111 else
2112 error (_("Expression cannot be implemented with "
2113 "read/access watchpoint."));
2114 }
9fa40276 2115 else
3a5c3e22 2116 b->base.type = bp_watchpoint;
9fa40276 2117
3a5c3e22 2118 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2119 : bp_loc_hardware_watchpoint);
3a5c3e22 2120 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2121 bl->loc_type = loc_type;
2122 }
2123
2124 for (v = val_chain; v; v = next)
2125 {
a5606eee
VP
2126 next = value_next (v);
2127 if (v != b->val)
2128 value_free (v);
2129 }
2130
c7437ca6
PA
2131 /* If a software watchpoint is not watching any memory, then the
2132 above left it without any location set up. But,
2133 bpstat_stop_status requires a location to be able to report
2134 stops, so make sure there's at least a dummy one. */
3a5c3e22 2135 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2136 {
3a5c3e22
PA
2137 struct breakpoint *base = &b->base;
2138 base->loc = allocate_bp_location (base);
2139 base->loc->pspace = frame_pspace;
2140 base->loc->address = -1;
2141 base->loc->length = -1;
2142 base->loc->watchpoint_type = -1;
c7437ca6 2143 }
a5606eee
VP
2144 }
2145 else if (!within_current_scope)
7270d8f2 2146 {
ac74f770
MS
2147 printf_filtered (_("\
2148Watchpoint %d deleted because the program has left the block\n\
2149in which its expression is valid.\n"),
3a5c3e22 2150 b->base.number);
d0fb5eae 2151 watchpoint_del_at_next_stop (b);
7270d8f2 2152 }
a5606eee
VP
2153
2154 /* Restore the selected frame. */
66076460
DJ
2155 if (frame_saved)
2156 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2157}
2158
a5606eee 2159
74960c60 2160/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2161 inserted in the inferior. We don't differentiate the type of BL's owner
2162 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2163 breakpoint_ops is not defined, because in insert_bp_location,
2164 tracepoint's insert_location will not be called. */
74960c60 2165static int
35df4500 2166should_be_inserted (struct bp_location *bl)
74960c60 2167{
35df4500 2168 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2169 return 0;
2170
35df4500 2171 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2172 return 0;
2173
35df4500 2174 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2175 return 0;
2176
f8eba3c6
TT
2177 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2178 return 0;
2179
56710373
PA
2180 /* This is set for example, when we're attached to the parent of a
2181 vfork, and have detached from the child. The child is running
2182 free, and we expect it to do an exec or exit, at which point the
2183 OS makes the parent schedulable again (and the target reports
2184 that the vfork is done). Until the child is done with the shared
2185 memory region, do not insert breakpoints in the parent, otherwise
2186 the child could still trip on the parent's breakpoints. Since
2187 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2188 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2189 return 0;
2190
31e77af2
PA
2191 /* Don't insert a breakpoint if we're trying to step past its
2192 location. */
2193 if ((bl->loc_type == bp_loc_software_breakpoint
2194 || bl->loc_type == bp_loc_hardware_breakpoint)
2195 && stepping_past_instruction_at (bl->pspace->aspace,
2196 bl->address))
e558d7c1
PA
2197 {
2198 if (debug_infrun)
2199 {
2200 fprintf_unfiltered (gdb_stdlog,
2201 "infrun: skipping breakpoint: "
2202 "stepping past insn at: %s\n",
2203 paddress (bl->gdbarch, bl->address));
2204 }
2205 return 0;
2206 }
31e77af2 2207
963f9c80
PA
2208 /* Don't insert watchpoints if we're trying to step past the
2209 instruction that triggered one. */
2210 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2211 && stepping_past_nonsteppable_watchpoint ())
2212 {
2213 if (debug_infrun)
2214 {
2215 fprintf_unfiltered (gdb_stdlog,
2216 "infrun: stepping past non-steppable watchpoint. "
2217 "skipping watchpoint at %s:%d\n",
2218 paddress (bl->gdbarch, bl->address),
2219 bl->length);
2220 }
2221 return 0;
2222 }
2223
74960c60
VP
2224 return 1;
2225}
2226
934709f0
PW
2227/* Same as should_be_inserted but does the check assuming
2228 that the location is not duplicated. */
2229
2230static int
2231unduplicated_should_be_inserted (struct bp_location *bl)
2232{
2233 int result;
2234 const int save_duplicate = bl->duplicate;
2235
2236 bl->duplicate = 0;
2237 result = should_be_inserted (bl);
2238 bl->duplicate = save_duplicate;
2239 return result;
2240}
2241
b775012e
LM
2242/* Parses a conditional described by an expression COND into an
2243 agent expression bytecode suitable for evaluation
2244 by the bytecode interpreter. Return NULL if there was
2245 any error during parsing. */
2246
2247static struct agent_expr *
2248parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2249{
2250 struct agent_expr *aexpr = NULL;
b775012e
LM
2251
2252 if (!cond)
2253 return NULL;
2254
2255 /* We don't want to stop processing, so catch any errors
2256 that may show up. */
492d29ea 2257 TRY
b775012e
LM
2258 {
2259 aexpr = gen_eval_for_expr (scope, cond);
2260 }
2261
492d29ea 2262 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2263 {
2264 /* If we got here, it means the condition could not be parsed to a valid
2265 bytecode expression and thus can't be evaluated on the target's side.
2266 It's no use iterating through the conditions. */
2267 return NULL;
2268 }
492d29ea 2269 END_CATCH
b775012e
LM
2270
2271 /* We have a valid agent expression. */
2272 return aexpr;
2273}
2274
2275/* Based on location BL, create a list of breakpoint conditions to be
2276 passed on to the target. If we have duplicated locations with different
2277 conditions, we will add such conditions to the list. The idea is that the
2278 target will evaluate the list of conditions and will only notify GDB when
2279 one of them is true. */
2280
2281static void
2282build_target_condition_list (struct bp_location *bl)
2283{
2284 struct bp_location **locp = NULL, **loc2p;
2285 int null_condition_or_parse_error = 0;
2286 int modified = bl->needs_update;
2287 struct bp_location *loc;
2288
8b4f3082
PA
2289 /* Release conditions left over from a previous insert. */
2290 VEC_free (agent_expr_p, bl->target_info.conditions);
2291
b775012e
LM
2292 /* This is only meaningful if the target is
2293 evaluating conditions and if the user has
2294 opted for condition evaluation on the target's
2295 side. */
2296 if (gdb_evaluates_breakpoint_condition_p ()
2297 || !target_supports_evaluation_of_breakpoint_conditions ())
2298 return;
2299
2300 /* Do a first pass to check for locations with no assigned
2301 conditions or conditions that fail to parse to a valid agent expression
2302 bytecode. If any of these happen, then it's no use to send conditions
2303 to the target since this location will always trigger and generate a
2304 response back to GDB. */
2305 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2306 {
2307 loc = (*loc2p);
2308 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2309 {
2310 if (modified)
2311 {
2312 struct agent_expr *aexpr;
2313
2314 /* Re-parse the conditions since something changed. In that
2315 case we already freed the condition bytecodes (see
2316 force_breakpoint_reinsertion). We just
2317 need to parse the condition to bytecodes again. */
2318 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2319 loc->cond_bytecode = aexpr;
2320
2321 /* Check if we managed to parse the conditional expression
2322 correctly. If not, we will not send this condition
2323 to the target. */
2324 if (aexpr)
2325 continue;
2326 }
2327
2328 /* If we have a NULL bytecode expression, it means something
2329 went wrong or we have a null condition expression. */
2330 if (!loc->cond_bytecode)
2331 {
2332 null_condition_or_parse_error = 1;
2333 break;
2334 }
2335 }
2336 }
2337
2338 /* If any of these happened, it means we will have to evaluate the conditions
2339 for the location's address on gdb's side. It is no use keeping bytecodes
2340 for all the other duplicate locations, thus we free all of them here.
2341
2342 This is so we have a finer control over which locations' conditions are
2343 being evaluated by GDB or the remote stub. */
2344 if (null_condition_or_parse_error)
2345 {
2346 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2347 {
2348 loc = (*loc2p);
2349 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2350 {
2351 /* Only go as far as the first NULL bytecode is
2352 located. */
2353 if (!loc->cond_bytecode)
2354 return;
2355
2356 free_agent_expr (loc->cond_bytecode);
2357 loc->cond_bytecode = NULL;
2358 }
2359 }
2360 }
2361
2362 /* No NULL conditions or failed bytecode generation. Build a condition list
2363 for this location's address. */
2364 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2365 {
2366 loc = (*loc2p);
2367 if (loc->cond
2368 && is_breakpoint (loc->owner)
2369 && loc->pspace->num == bl->pspace->num
2370 && loc->owner->enable_state == bp_enabled
2371 && loc->enabled)
2372 /* Add the condition to the vector. This will be used later to send the
2373 conditions to the target. */
2374 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2375 loc->cond_bytecode);
2376 }
2377
2378 return;
2379}
2380
d3ce09f5
SS
2381/* Parses a command described by string CMD into an agent expression
2382 bytecode suitable for evaluation by the bytecode interpreter.
2383 Return NULL if there was any error during parsing. */
2384
2385static struct agent_expr *
2386parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2387{
2388 struct cleanup *old_cleanups = 0;
2389 struct expression *expr, **argvec;
2390 struct agent_expr *aexpr = NULL;
bbc13ae3
KS
2391 const char *cmdrest;
2392 const char *format_start, *format_end;
d3ce09f5
SS
2393 struct format_piece *fpieces;
2394 int nargs;
2395 struct gdbarch *gdbarch = get_current_arch ();
2396
2397 if (!cmd)
2398 return NULL;
2399
2400 cmdrest = cmd;
2401
2402 if (*cmdrest == ',')
2403 ++cmdrest;
bbc13ae3 2404 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2405
2406 if (*cmdrest++ != '"')
2407 error (_("No format string following the location"));
2408
2409 format_start = cmdrest;
2410
2411 fpieces = parse_format_string (&cmdrest);
2412
2413 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2414
2415 format_end = cmdrest;
2416
2417 if (*cmdrest++ != '"')
2418 error (_("Bad format string, non-terminated '\"'."));
2419
bbc13ae3 2420 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2421
2422 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2423 error (_("Invalid argument syntax"));
2424
2425 if (*cmdrest == ',')
2426 cmdrest++;
bbc13ae3 2427 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2428
2429 /* For each argument, make an expression. */
2430
2431 argvec = (struct expression **) alloca (strlen (cmd)
2432 * sizeof (struct expression *));
2433
2434 nargs = 0;
2435 while (*cmdrest != '\0')
2436 {
bbc13ae3 2437 const char *cmd1;
d3ce09f5
SS
2438
2439 cmd1 = cmdrest;
2440 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2441 argvec[nargs++] = expr;
2442 cmdrest = cmd1;
2443 if (*cmdrest == ',')
2444 ++cmdrest;
2445 }
2446
2447 /* We don't want to stop processing, so catch any errors
2448 that may show up. */
492d29ea 2449 TRY
d3ce09f5
SS
2450 {
2451 aexpr = gen_printf (scope, gdbarch, 0, 0,
2452 format_start, format_end - format_start,
2453 fpieces, nargs, argvec);
2454 }
492d29ea 2455 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2456 {
2457 /* If we got here, it means the command could not be parsed to a valid
2458 bytecode expression and thus can't be evaluated on the target's side.
2459 It's no use iterating through the other commands. */
492d29ea 2460 aexpr = NULL;
d3ce09f5 2461 }
492d29ea
PA
2462 END_CATCH
2463
2464 do_cleanups (old_cleanups);
d3ce09f5 2465
d3ce09f5
SS
2466 /* We have a valid agent expression, return it. */
2467 return aexpr;
2468}
2469
2470/* Based on location BL, create a list of breakpoint commands to be
2471 passed on to the target. If we have duplicated locations with
2472 different commands, we will add any such to the list. */
2473
2474static void
2475build_target_command_list (struct bp_location *bl)
2476{
2477 struct bp_location **locp = NULL, **loc2p;
2478 int null_command_or_parse_error = 0;
2479 int modified = bl->needs_update;
2480 struct bp_location *loc;
2481
8b4f3082
PA
2482 /* Release commands left over from a previous insert. */
2483 VEC_free (agent_expr_p, bl->target_info.tcommands);
2484
41fac0cf 2485 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2486 return;
2487
41fac0cf
PA
2488 /* For now, limit to agent-style dprintf breakpoints. */
2489 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2490 return;
2491
41fac0cf
PA
2492 /* For now, if we have any duplicate location that isn't a dprintf,
2493 don't install the target-side commands, as that would make the
2494 breakpoint not be reported to the core, and we'd lose
2495 control. */
2496 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2497 {
2498 loc = (*loc2p);
2499 if (is_breakpoint (loc->owner)
2500 && loc->pspace->num == bl->pspace->num
2501 && loc->owner->type != bp_dprintf)
2502 return;
2503 }
2504
d3ce09f5
SS
2505 /* Do a first pass to check for locations with no assigned
2506 conditions or conditions that fail to parse to a valid agent expression
2507 bytecode. If any of these happen, then it's no use to send conditions
2508 to the target since this location will always trigger and generate a
2509 response back to GDB. */
2510 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2511 {
2512 loc = (*loc2p);
2513 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2514 {
2515 if (modified)
2516 {
2517 struct agent_expr *aexpr;
2518
2519 /* Re-parse the commands since something changed. In that
2520 case we already freed the command bytecodes (see
2521 force_breakpoint_reinsertion). We just
2522 need to parse the command to bytecodes again. */
2523 aexpr = parse_cmd_to_aexpr (bl->address,
2524 loc->owner->extra_string);
2525 loc->cmd_bytecode = aexpr;
2526
2527 if (!aexpr)
2528 continue;
2529 }
2530
2531 /* If we have a NULL bytecode expression, it means something
2532 went wrong or we have a null command expression. */
2533 if (!loc->cmd_bytecode)
2534 {
2535 null_command_or_parse_error = 1;
2536 break;
2537 }
2538 }
2539 }
2540
2541 /* If anything failed, then we're not doing target-side commands,
2542 and so clean up. */
2543 if (null_command_or_parse_error)
2544 {
2545 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2546 {
2547 loc = (*loc2p);
2548 if (is_breakpoint (loc->owner)
2549 && loc->pspace->num == bl->pspace->num)
2550 {
2551 /* Only go as far as the first NULL bytecode is
2552 located. */
40fb6c5e 2553 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2554 return;
2555
40fb6c5e
HZ
2556 free_agent_expr (loc->cmd_bytecode);
2557 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2558 }
2559 }
2560 }
2561
2562 /* No NULL commands or failed bytecode generation. Build a command list
2563 for this location's address. */
2564 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2565 {
2566 loc = (*loc2p);
2567 if (loc->owner->extra_string
2568 && is_breakpoint (loc->owner)
2569 && loc->pspace->num == bl->pspace->num
2570 && loc->owner->enable_state == bp_enabled
2571 && loc->enabled)
2572 /* Add the command to the vector. This will be used later
2573 to send the commands to the target. */
2574 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2575 loc->cmd_bytecode);
2576 }
2577
2578 bl->target_info.persist = 0;
2579 /* Maybe flag this location as persistent. */
2580 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2581 bl->target_info.persist = 1;
2582}
2583
35df4500
TJB
2584/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2585 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2586 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2587 Returns 0 for success, 1 if the bp_location type is not supported or
2588 -1 for failure.
879bfdc2 2589
4a64f543
MS
2590 NOTE drow/2003-09-09: This routine could be broken down to an
2591 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2592static int
35df4500 2593insert_bp_location (struct bp_location *bl,
26bb91f3 2594 struct ui_file *tmp_error_stream,
3fbb6ffa 2595 int *disabled_breaks,
dd61ec5c
MW
2596 int *hw_breakpoint_error,
2597 int *hw_bp_error_explained_already)
879bfdc2 2598{
0000e5cc
PA
2599 enum errors bp_err = GDB_NO_ERROR;
2600 const char *bp_err_message = NULL;
879bfdc2 2601
b775012e 2602 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2603 return 0;
2604
35c63cd8
JB
2605 /* Note we don't initialize bl->target_info, as that wipes out
2606 the breakpoint location's shadow_contents if the breakpoint
2607 is still inserted at that location. This in turn breaks
2608 target_read_memory which depends on these buffers when
2609 a memory read is requested at the breakpoint location:
2610 Once the target_info has been wiped, we fail to see that
2611 we have a breakpoint inserted at that address and thus
2612 read the breakpoint instead of returning the data saved in
2613 the breakpoint location's shadow contents. */
0d5ed153 2614 bl->target_info.reqstd_address = bl->address;
35df4500 2615 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2616 bl->target_info.length = bl->length;
8181d85f 2617
b775012e
LM
2618 /* When working with target-side conditions, we must pass all the conditions
2619 for the same breakpoint address down to the target since GDB will not
2620 insert those locations. With a list of breakpoint conditions, the target
2621 can decide when to stop and notify GDB. */
2622
2623 if (is_breakpoint (bl->owner))
2624 {
2625 build_target_condition_list (bl);
d3ce09f5
SS
2626 build_target_command_list (bl);
2627 /* Reset the modification marker. */
b775012e
LM
2628 bl->needs_update = 0;
2629 }
2630
35df4500
TJB
2631 if (bl->loc_type == bp_loc_software_breakpoint
2632 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2633 {
35df4500 2634 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2635 {
2636 /* If the explicitly specified breakpoint type
2637 is not hardware breakpoint, check the memory map to see
2638 if the breakpoint address is in read only memory or not.
4a64f543 2639
765dc015
VP
2640 Two important cases are:
2641 - location type is not hardware breakpoint, memory
2642 is readonly. We change the type of the location to
2643 hardware breakpoint.
4a64f543
MS
2644 - location type is hardware breakpoint, memory is
2645 read-write. This means we've previously made the
2646 location hardware one, but then the memory map changed,
2647 so we undo.
765dc015 2648
4a64f543
MS
2649 When breakpoints are removed, remove_breakpoints will use
2650 location types we've just set here, the only possible
2651 problem is that memory map has changed during running
2652 program, but it's not going to work anyway with current
2653 gdb. */
765dc015 2654 struct mem_region *mr
0d5ed153 2655 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2656
2657 if (mr)
2658 {
2659 if (automatic_hardware_breakpoints)
2660 {
765dc015
VP
2661 enum bp_loc_type new_type;
2662
2663 if (mr->attrib.mode != MEM_RW)
2664 new_type = bp_loc_hardware_breakpoint;
2665 else
2666 new_type = bp_loc_software_breakpoint;
2667
35df4500 2668 if (new_type != bl->loc_type)
765dc015
VP
2669 {
2670 static int said = 0;
cc59ec59 2671
35df4500 2672 bl->loc_type = new_type;
765dc015
VP
2673 if (!said)
2674 {
3e43a32a
MS
2675 fprintf_filtered (gdb_stdout,
2676 _("Note: automatically using "
2677 "hardware breakpoints for "
2678 "read-only addresses.\n"));
765dc015
VP
2679 said = 1;
2680 }
2681 }
2682 }
35df4500 2683 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2684 && mr->attrib.mode != MEM_RW)
2685 {
2686 fprintf_unfiltered (tmp_error_stream,
2687 _("Cannot insert breakpoint %d.\n"
2688 "Cannot set software breakpoint "
2689 "at read-only address %s\n"),
2690 bl->owner->number,
2691 paddress (bl->gdbarch, bl->address));
2692 return 1;
2693 }
765dc015
VP
2694 }
2695 }
2696
879bfdc2
DJ
2697 /* First check to see if we have to handle an overlay. */
2698 if (overlay_debugging == ovly_off
35df4500
TJB
2699 || bl->section == NULL
2700 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2701 {
2702 /* No overlay handling: just set the breakpoint. */
492d29ea 2703 TRY
dd61ec5c 2704 {
0000e5cc
PA
2705 int val;
2706
dd61ec5c 2707 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2708 if (val)
2709 bp_err = GENERIC_ERROR;
dd61ec5c 2710 }
492d29ea 2711 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2712 {
0000e5cc
PA
2713 bp_err = e.error;
2714 bp_err_message = e.message;
dd61ec5c 2715 }
492d29ea 2716 END_CATCH
879bfdc2
DJ
2717 }
2718 else
2719 {
4a64f543 2720 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2721 Shall we set a breakpoint at the LMA? */
2722 if (!overlay_events_enabled)
2723 {
2724 /* Yes -- overlay event support is not active,
2725 so we must try to set a breakpoint at the LMA.
2726 This will not work for a hardware breakpoint. */
35df4500 2727 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2728 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2729 bl->owner->number);
879bfdc2
DJ
2730 else
2731 {
35df4500
TJB
2732 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2733 bl->section);
879bfdc2 2734 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2735 bl->overlay_target_info = bl->target_info;
0d5ed153 2736 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2737
2738 /* No overlay handling: just set the breakpoint. */
492d29ea 2739 TRY
0000e5cc
PA
2740 {
2741 int val;
2742
2743 val = target_insert_breakpoint (bl->gdbarch,
2744 &bl->overlay_target_info);
2745 if (val)
2746 bp_err = GENERIC_ERROR;
2747 }
492d29ea 2748 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2749 {
2750 bp_err = e.error;
2751 bp_err_message = e.message;
2752 }
492d29ea 2753 END_CATCH
0000e5cc
PA
2754
2755 if (bp_err != GDB_NO_ERROR)
99361f52 2756 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2757 "Overlay breakpoint %d "
2758 "failed: in ROM?\n",
35df4500 2759 bl->owner->number);
879bfdc2
DJ
2760 }
2761 }
2762 /* Shall we set a breakpoint at the VMA? */
35df4500 2763 if (section_is_mapped (bl->section))
879bfdc2
DJ
2764 {
2765 /* Yes. This overlay section is mapped into memory. */
492d29ea 2766 TRY
dd61ec5c 2767 {
0000e5cc
PA
2768 int val;
2769
dd61ec5c 2770 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2771 if (val)
2772 bp_err = GENERIC_ERROR;
dd61ec5c 2773 }
492d29ea 2774 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2775 {
0000e5cc
PA
2776 bp_err = e.error;
2777 bp_err_message = e.message;
dd61ec5c 2778 }
492d29ea 2779 END_CATCH
879bfdc2
DJ
2780 }
2781 else
2782 {
2783 /* No. This breakpoint will not be inserted.
2784 No error, but do not mark the bp as 'inserted'. */
2785 return 0;
2786 }
2787 }
2788
0000e5cc 2789 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2790 {
2791 /* Can't set the breakpoint. */
0000e5cc
PA
2792
2793 /* In some cases, we might not be able to insert a
2794 breakpoint in a shared library that has already been
2795 removed, but we have not yet processed the shlib unload
2796 event. Unfortunately, some targets that implement
076855f9
PA
2797 breakpoint insertion themselves can't tell why the
2798 breakpoint insertion failed (e.g., the remote target
2799 doesn't define error codes), so we must treat generic
2800 errors as memory errors. */
0000e5cc 2801 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2802 && bl->loc_type == bp_loc_software_breakpoint
08351840 2803 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2804 || shared_objfile_contains_address_p (bl->pspace,
2805 bl->address)))
879bfdc2 2806 {
4a64f543 2807 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2808 bl->shlib_disabled = 1;
8d3788bd 2809 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2810 if (!*disabled_breaks)
2811 {
2812 fprintf_unfiltered (tmp_error_stream,
2813 "Cannot insert breakpoint %d.\n",
2814 bl->owner->number);
2815 fprintf_unfiltered (tmp_error_stream,
2816 "Temporarily disabling shared "
2817 "library breakpoints:\n");
2818 }
2819 *disabled_breaks = 1;
879bfdc2 2820 fprintf_unfiltered (tmp_error_stream,
35df4500 2821 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2822 return 0;
879bfdc2
DJ
2823 }
2824 else
879bfdc2 2825 {
35df4500 2826 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2827 {
0000e5cc
PA
2828 *hw_breakpoint_error = 1;
2829 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2830 fprintf_unfiltered (tmp_error_stream,
2831 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2832 bl->owner->number, bp_err_message ? ":" : ".\n");
2833 if (bp_err_message != NULL)
2834 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2835 }
2836 else
2837 {
0000e5cc
PA
2838 if (bp_err_message == NULL)
2839 {
2840 char *message
2841 = memory_error_message (TARGET_XFER_E_IO,
2842 bl->gdbarch, bl->address);
2843 struct cleanup *old_chain = make_cleanup (xfree, message);
2844
2845 fprintf_unfiltered (tmp_error_stream,
2846 "Cannot insert breakpoint %d.\n"
2847 "%s\n",
2848 bl->owner->number, message);
2849 do_cleanups (old_chain);
2850 }
2851 else
2852 {
2853 fprintf_unfiltered (tmp_error_stream,
2854 "Cannot insert breakpoint %d: %s\n",
2855 bl->owner->number,
2856 bp_err_message);
2857 }
879bfdc2 2858 }
0000e5cc 2859 return 1;
879bfdc2
DJ
2860
2861 }
2862 }
2863 else
35df4500 2864 bl->inserted = 1;
879bfdc2 2865
0000e5cc 2866 return 0;
879bfdc2
DJ
2867 }
2868
35df4500 2869 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2870 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2871 watchpoints. It's not clear that it's necessary... */
35df4500 2872 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2873 {
0000e5cc
PA
2874 int val;
2875
77b06cd7
TJB
2876 gdb_assert (bl->owner->ops != NULL
2877 && bl->owner->ops->insert_location != NULL);
2878
2879 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2880
2881 /* If trying to set a read-watchpoint, and it turns out it's not
2882 supported, try emulating one with an access watchpoint. */
35df4500 2883 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2884 {
2885 struct bp_location *loc, **loc_temp;
2886
2887 /* But don't try to insert it, if there's already another
2888 hw_access location that would be considered a duplicate
2889 of this one. */
2890 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2891 if (loc != bl
85d721b8 2892 && loc->watchpoint_type == hw_access
35df4500 2893 && watchpoint_locations_match (bl, loc))
85d721b8 2894 {
35df4500
TJB
2895 bl->duplicate = 1;
2896 bl->inserted = 1;
2897 bl->target_info = loc->target_info;
2898 bl->watchpoint_type = hw_access;
85d721b8
PA
2899 val = 0;
2900 break;
2901 }
2902
2903 if (val == 1)
2904 {
77b06cd7
TJB
2905 bl->watchpoint_type = hw_access;
2906 val = bl->owner->ops->insert_location (bl);
2907
2908 if (val)
2909 /* Back to the original value. */
2910 bl->watchpoint_type = hw_read;
85d721b8
PA
2911 }
2912 }
2913
35df4500 2914 bl->inserted = (val == 0);
879bfdc2
DJ
2915 }
2916
35df4500 2917 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2918 {
0000e5cc
PA
2919 int val;
2920
77b06cd7
TJB
2921 gdb_assert (bl->owner->ops != NULL
2922 && bl->owner->ops->insert_location != NULL);
2923
2924 val = bl->owner->ops->insert_location (bl);
2925 if (val)
2926 {
2927 bl->owner->enable_state = bp_disabled;
2928
2929 if (val == 1)
2930 warning (_("\
2931Error inserting catchpoint %d: Your system does not support this type\n\
2932of catchpoint."), bl->owner->number);
2933 else
2934 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2935 }
2936
2937 bl->inserted = (val == 0);
1640b821
DJ
2938
2939 /* We've already printed an error message if there was a problem
2940 inserting this catchpoint, and we've disabled the catchpoint,
2941 so just return success. */
2942 return 0;
879bfdc2
DJ
2943 }
2944
2945 return 0;
2946}
2947
6c95b8df
PA
2948/* This function is called when program space PSPACE is about to be
2949 deleted. It takes care of updating breakpoints to not reference
2950 PSPACE anymore. */
2951
2952void
2953breakpoint_program_space_exit (struct program_space *pspace)
2954{
2955 struct breakpoint *b, *b_temp;
876fa593 2956 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2957
2958 /* Remove any breakpoint that was set through this program space. */
2959 ALL_BREAKPOINTS_SAFE (b, b_temp)
2960 {
2961 if (b->pspace == pspace)
2962 delete_breakpoint (b);
2963 }
2964
2965 /* Breakpoints set through other program spaces could have locations
2966 bound to PSPACE as well. Remove those. */
876fa593 2967 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2968 {
2969 struct bp_location *tmp;
2970
2971 if (loc->pspace == pspace)
2972 {
2bdf28a0 2973 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2974 if (loc->owner->loc == loc)
2975 loc->owner->loc = loc->next;
2976 else
2977 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2978 if (tmp->next == loc)
2979 {
2980 tmp->next = loc->next;
2981 break;
2982 }
2983 }
2984 }
2985
2986 /* Now update the global location list to permanently delete the
2987 removed locations above. */
44702360 2988 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2989}
2990
74960c60
VP
2991/* Make sure all breakpoints are inserted in inferior.
2992 Throws exception on any error.
2993 A breakpoint that is already inserted won't be inserted
2994 again, so calling this function twice is safe. */
2995void
2996insert_breakpoints (void)
2997{
2998 struct breakpoint *bpt;
2999
3000 ALL_BREAKPOINTS (bpt)
3001 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3002 {
3003 struct watchpoint *w = (struct watchpoint *) bpt;
3004
3005 update_watchpoint (w, 0 /* don't reparse. */);
3006 }
74960c60 3007
04086b45
PA
3008 /* Updating watchpoints creates new locations, so update the global
3009 location list. Explicitly tell ugll to insert locations and
3010 ignore breakpoints_always_inserted_mode. */
3011 update_global_location_list (UGLL_INSERT);
74960c60
VP
3012}
3013
20388dd6
YQ
3014/* Invoke CALLBACK for each of bp_location. */
3015
3016void
3017iterate_over_bp_locations (walk_bp_location_callback callback)
3018{
3019 struct bp_location *loc, **loc_tmp;
3020
3021 ALL_BP_LOCATIONS (loc, loc_tmp)
3022 {
3023 callback (loc, NULL);
3024 }
3025}
3026
b775012e
LM
3027/* This is used when we need to synch breakpoint conditions between GDB and the
3028 target. It is the case with deleting and disabling of breakpoints when using
3029 always-inserted mode. */
3030
3031static void
3032update_inserted_breakpoint_locations (void)
3033{
3034 struct bp_location *bl, **blp_tmp;
3035 int error_flag = 0;
3036 int val = 0;
3037 int disabled_breaks = 0;
3038 int hw_breakpoint_error = 0;
dd61ec5c 3039 int hw_bp_details_reported = 0;
b775012e
LM
3040
3041 struct ui_file *tmp_error_stream = mem_fileopen ();
3042 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3043
3044 /* Explicitly mark the warning -- this will only be printed if
3045 there was an error. */
3046 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3047
3048 save_current_space_and_thread ();
3049
3050 ALL_BP_LOCATIONS (bl, blp_tmp)
3051 {
3052 /* We only want to update software breakpoints and hardware
3053 breakpoints. */
3054 if (!is_breakpoint (bl->owner))
3055 continue;
3056
3057 /* We only want to update locations that are already inserted
3058 and need updating. This is to avoid unwanted insertion during
3059 deletion of breakpoints. */
3060 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3061 continue;
3062
3063 switch_to_program_space_and_thread (bl->pspace);
3064
3065 /* For targets that support global breakpoints, there's no need
3066 to select an inferior to insert breakpoint to. In fact, even
3067 if we aren't attached to any process yet, we should still
3068 insert breakpoints. */
f5656ead 3069 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3070 && ptid_equal (inferior_ptid, null_ptid))
3071 continue;
3072
3073 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3074 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3075 if (val)
3076 error_flag = val;
3077 }
3078
3079 if (error_flag)
3080 {
3081 target_terminal_ours_for_output ();
3082 error_stream (tmp_error_stream);
3083 }
3084
3085 do_cleanups (cleanups);
3086}
3087
c30eee59 3088/* Used when starting or continuing the program. */
c906108c 3089
74960c60
VP
3090static void
3091insert_breakpoint_locations (void)
c906108c 3092{
a5606eee 3093 struct breakpoint *bpt;
35df4500 3094 struct bp_location *bl, **blp_tmp;
eacd795a 3095 int error_flag = 0;
c906108c 3096 int val = 0;
3fbb6ffa 3097 int disabled_breaks = 0;
81d0cc19 3098 int hw_breakpoint_error = 0;
dd61ec5c 3099 int hw_bp_error_explained_already = 0;
c906108c 3100
81d0cc19 3101 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3102 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3103
81d0cc19
GS
3104 /* Explicitly mark the warning -- this will only be printed if
3105 there was an error. */
3106 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3107
3108 save_current_space_and_thread ();
3109
35df4500 3110 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3111 {
b775012e 3112 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3113 continue;
3114
4a64f543
MS
3115 /* There is no point inserting thread-specific breakpoints if
3116 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3117 has BL->OWNER always non-NULL. */
35df4500
TJB
3118 if (bl->owner->thread != -1
3119 && !valid_thread_id (bl->owner->thread))
f365de73
AS
3120 continue;
3121
35df4500 3122 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3123
3124 /* For targets that support global breakpoints, there's no need
3125 to select an inferior to insert breakpoint to. In fact, even
3126 if we aren't attached to any process yet, we should still
3127 insert breakpoints. */
f5656ead 3128 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3129 && ptid_equal (inferior_ptid, null_ptid))
3130 continue;
3131
3fbb6ffa 3132 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3133 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3134 if (val)
eacd795a 3135 error_flag = val;
879bfdc2 3136 }
c906108c 3137
4a64f543
MS
3138 /* If we failed to insert all locations of a watchpoint, remove
3139 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3140 ALL_BREAKPOINTS (bpt)
3141 {
3142 int some_failed = 0;
3143 struct bp_location *loc;
3144
3145 if (!is_hardware_watchpoint (bpt))
3146 continue;
3147
d6b74ac4 3148 if (!breakpoint_enabled (bpt))
a5606eee 3149 continue;
74960c60
VP
3150
3151 if (bpt->disposition == disp_del_at_next_stop)
3152 continue;
a5606eee
VP
3153
3154 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3155 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3156 {
3157 some_failed = 1;
3158 break;
3159 }
3160 if (some_failed)
3161 {
3162 for (loc = bpt->loc; loc; loc = loc->next)
3163 if (loc->inserted)
3164 remove_breakpoint (loc, mark_uninserted);
3165
3166 hw_breakpoint_error = 1;
3167 fprintf_unfiltered (tmp_error_stream,
3168 "Could not insert hardware watchpoint %d.\n",
3169 bpt->number);
eacd795a 3170 error_flag = -1;
a5606eee
VP
3171 }
3172 }
3173
eacd795a 3174 if (error_flag)
81d0cc19
GS
3175 {
3176 /* If a hardware breakpoint or watchpoint was inserted, add a
3177 message about possibly exhausted resources. */
dd61ec5c 3178 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3179 {
c6510018
MS
3180 fprintf_unfiltered (tmp_error_stream,
3181 "Could not insert hardware breakpoints:\n\
3182You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3183 }
81d0cc19
GS
3184 target_terminal_ours_for_output ();
3185 error_stream (tmp_error_stream);
3186 }
f7545552
TT
3187
3188 do_cleanups (cleanups);
c906108c
SS
3189}
3190
c30eee59
TJB
3191/* Used when the program stops.
3192 Returns zero if successful, or non-zero if there was a problem
3193 removing a breakpoint location. */
3194
c906108c 3195int
fba45db2 3196remove_breakpoints (void)
c906108c 3197{
35df4500 3198 struct bp_location *bl, **blp_tmp;
3a1bae8e 3199 int val = 0;
c906108c 3200
35df4500 3201 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3202 {
1e4d1764 3203 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3204 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3205 }
3a1bae8e 3206 return val;
c906108c
SS
3207}
3208
49fa26b0
PA
3209/* When a thread exits, remove breakpoints that are related to
3210 that thread. */
3211
3212static void
3213remove_threaded_breakpoints (struct thread_info *tp, int silent)
3214{
3215 struct breakpoint *b, *b_tmp;
3216
3217 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3218 {
96181529 3219 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3220 {
3221 b->disposition = disp_del_at_next_stop;
3222
3223 printf_filtered (_("\
46ecd527 3224Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3225 b->number, tp->num);
3226
3227 /* Hide it from the user. */
3228 b->number = 0;
3229 }
3230 }
3231}
3232
6c95b8df
PA
3233/* Remove breakpoints of process PID. */
3234
3235int
3236remove_breakpoints_pid (int pid)
3237{
35df4500 3238 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3239 int val;
3240 struct inferior *inf = find_inferior_pid (pid);
3241
35df4500 3242 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3243 {
35df4500 3244 if (bl->pspace != inf->pspace)
6c95b8df
PA
3245 continue;
3246
fc126975 3247 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3248 {
35df4500 3249 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3250 if (val != 0)
3251 return val;
3252 }
3253 }
3254 return 0;
3255}
3256
c906108c 3257int
fba45db2 3258reattach_breakpoints (int pid)
c906108c 3259{
6c95b8df 3260 struct cleanup *old_chain;
35df4500 3261 struct bp_location *bl, **blp_tmp;
c906108c 3262 int val;
86b887df 3263 struct ui_file *tmp_error_stream;
dd61ec5c 3264 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3265 struct inferior *inf;
3266 struct thread_info *tp;
3267
3268 tp = any_live_thread_of_process (pid);
3269 if (tp == NULL)
3270 return 1;
3271
3272 inf = find_inferior_pid (pid);
3273 old_chain = save_inferior_ptid ();
3274
3275 inferior_ptid = tp->ptid;
a4954f26 3276
86b887df 3277 tmp_error_stream = mem_fileopen ();
a4954f26 3278 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3279
35df4500 3280 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3281 {
35df4500 3282 if (bl->pspace != inf->pspace)
6c95b8df
PA
3283 continue;
3284
35df4500 3285 if (bl->inserted)
c5aa993b 3286 {
35df4500 3287 bl->inserted = 0;
dd61ec5c 3288 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3289 if (val != 0)
3290 {
ce696e05 3291 do_cleanups (old_chain);
c5aa993b
JM
3292 return val;
3293 }
3294 }
3295 }
ce696e05 3296 do_cleanups (old_chain);
c906108c
SS
3297 return 0;
3298}
3299
e58b0e63
PA
3300static int internal_breakpoint_number = -1;
3301
84f4c1fe
PM
3302/* Set the breakpoint number of B, depending on the value of INTERNAL.
3303 If INTERNAL is non-zero, the breakpoint number will be populated
3304 from internal_breakpoint_number and that variable decremented.
e5dd4106 3305 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3306 breakpoint_count and that value incremented. Internal breakpoints
3307 do not set the internal var bpnum. */
3308static void
3309set_breakpoint_number (int internal, struct breakpoint *b)
3310{
3311 if (internal)
3312 b->number = internal_breakpoint_number--;
3313 else
3314 {
3315 set_breakpoint_count (breakpoint_count + 1);
3316 b->number = breakpoint_count;
3317 }
3318}
3319
e62c965a 3320static struct breakpoint *
a6d9a66e 3321create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3322 CORE_ADDR address, enum bptype type,
c0a91b2b 3323 const struct breakpoint_ops *ops)
e62c965a 3324{
e62c965a
PP
3325 struct symtab_and_line sal;
3326 struct breakpoint *b;
3327
4a64f543 3328 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3329
3330 sal.pc = address;
3331 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3332 sal.pspace = current_program_space;
e62c965a 3333
06edf0c0 3334 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3335 b->number = internal_breakpoint_number--;
3336 b->disposition = disp_donttouch;
3337
3338 return b;
3339}
3340
17450429
PP
3341static const char *const longjmp_names[] =
3342 {
3343 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3344 };
3345#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3346
3347/* Per-objfile data private to breakpoint.c. */
3348struct breakpoint_objfile_data
3349{
3350 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3351 struct bound_minimal_symbol overlay_msym;
17450429
PP
3352
3353 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3354 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3355
28106bc2
SDJ
3356 /* True if we have looked for longjmp probes. */
3357 int longjmp_searched;
3358
3359 /* SystemTap probe points for longjmp (if any). */
3360 VEC (probe_p) *longjmp_probes;
3361
17450429 3362 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3363 struct bound_minimal_symbol terminate_msym;
17450429
PP
3364
3365 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3366 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3367
3368 /* True if we have looked for exception probes. */
3369 int exception_searched;
3370
3371 /* SystemTap probe points for unwinding (if any). */
3372 VEC (probe_p) *exception_probes;
17450429
PP
3373};
3374
3375static const struct objfile_data *breakpoint_objfile_key;
3376
3377/* Minimal symbol not found sentinel. */
3378static struct minimal_symbol msym_not_found;
3379
3380/* Returns TRUE if MSYM point to the "not found" sentinel. */
3381
3382static int
3383msym_not_found_p (const struct minimal_symbol *msym)
3384{
3385 return msym == &msym_not_found;
3386}
3387
3388/* Return per-objfile data needed by breakpoint.c.
3389 Allocate the data if necessary. */
3390
3391static struct breakpoint_objfile_data *
3392get_breakpoint_objfile_data (struct objfile *objfile)
3393{
3394 struct breakpoint_objfile_data *bp_objfile_data;
3395
3396 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3397 if (bp_objfile_data == NULL)
3398 {
8d749320
SM
3399 bp_objfile_data =
3400 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3401
3402 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3403 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3404 }
3405 return bp_objfile_data;
3406}
3407
28106bc2
SDJ
3408static void
3409free_breakpoint_probes (struct objfile *obj, void *data)
3410{
3411 struct breakpoint_objfile_data *bp_objfile_data = data;
3412
3413 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3414 VEC_free (probe_p, bp_objfile_data->exception_probes);
3415}
3416
e62c965a 3417static void
af02033e 3418create_overlay_event_breakpoint (void)
e62c965a 3419{
69de3c6a 3420 struct objfile *objfile;
af02033e 3421 const char *const func_name = "_ovly_debug_event";
e62c965a 3422
69de3c6a
PP
3423 ALL_OBJFILES (objfile)
3424 {
3425 struct breakpoint *b;
17450429
PP
3426 struct breakpoint_objfile_data *bp_objfile_data;
3427 CORE_ADDR addr;
67994074 3428 struct explicit_location explicit_loc;
69de3c6a 3429
17450429
PP
3430 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3431
3b7344d5 3432 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3433 continue;
3434
3b7344d5 3435 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3436 {
3b7344d5 3437 struct bound_minimal_symbol m;
17450429
PP
3438
3439 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3440 if (m.minsym == NULL)
17450429
PP
3441 {
3442 /* Avoid future lookups in this objfile. */
3b7344d5 3443 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3444 continue;
3445 }
3446 bp_objfile_data->overlay_msym = m;
3447 }
e62c965a 3448
77e371c0 3449 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3450 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3451 bp_overlay_event,
3452 &internal_breakpoint_ops);
67994074
KS
3453 initialize_explicit_location (&explicit_loc);
3454 explicit_loc.function_name = ASTRDUP (func_name);
3455 b->location = new_explicit_location (&explicit_loc);
e62c965a 3456
69de3c6a
PP
3457 if (overlay_debugging == ovly_auto)
3458 {
3459 b->enable_state = bp_enabled;
3460 overlay_events_enabled = 1;
3461 }
3462 else
3463 {
3464 b->enable_state = bp_disabled;
3465 overlay_events_enabled = 0;
3466 }
e62c965a 3467 }
44702360 3468 update_global_location_list (UGLL_MAY_INSERT);
e62c965a
PP
3469}
3470
0fd8e87f 3471static void
af02033e 3472create_longjmp_master_breakpoint (void)
0fd8e87f 3473{
6c95b8df 3474 struct program_space *pspace;
6c95b8df
PA
3475 struct cleanup *old_chain;
3476
3477 old_chain = save_current_program_space ();
0fd8e87f 3478
6c95b8df 3479 ALL_PSPACES (pspace)
af02033e
PP
3480 {
3481 struct objfile *objfile;
3482
3483 set_current_program_space (pspace);
3484
3485 ALL_OBJFILES (objfile)
0fd8e87f 3486 {
af02033e
PP
3487 int i;
3488 struct gdbarch *gdbarch;
17450429 3489 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3490
af02033e 3491 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3492
17450429
PP
3493 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3494
28106bc2
SDJ
3495 if (!bp_objfile_data->longjmp_searched)
3496 {
25f9533e
SDJ
3497 VEC (probe_p) *ret;
3498
3499 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3500 if (ret != NULL)
3501 {
3502 /* We are only interested in checking one element. */
3503 struct probe *p = VEC_index (probe_p, ret, 0);
3504
3505 if (!can_evaluate_probe_arguments (p))
3506 {
3507 /* We cannot use the probe interface here, because it does
3508 not know how to evaluate arguments. */
3509 VEC_free (probe_p, ret);
3510 ret = NULL;
3511 }
3512 }
3513 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3514 bp_objfile_data->longjmp_searched = 1;
3515 }
3516
3517 if (bp_objfile_data->longjmp_probes != NULL)
3518 {
3519 int i;
3520 struct probe *probe;
3521 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3522
3523 for (i = 0;
3524 VEC_iterate (probe_p,
3525 bp_objfile_data->longjmp_probes,
3526 i, probe);
3527 ++i)
3528 {
3529 struct breakpoint *b;
3530
729662a5
TT
3531 b = create_internal_breakpoint (gdbarch,
3532 get_probe_address (probe,
3533 objfile),
28106bc2
SDJ
3534 bp_longjmp_master,
3535 &internal_breakpoint_ops);
5b56227b
KS
3536 b->location
3537 = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3538 b->enable_state = bp_disabled;
3539 }
3540
3541 continue;
3542 }
3543
0569175e
TSD
3544 if (!gdbarch_get_longjmp_target_p (gdbarch))
3545 continue;
3546
17450429 3547 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3548 {
3549 struct breakpoint *b;
af02033e 3550 const char *func_name;
17450429 3551 CORE_ADDR addr;
67994074 3552 struct explicit_location explicit_loc;
6c95b8df 3553
3b7344d5 3554 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3555 continue;
0fd8e87f 3556
17450429 3557 func_name = longjmp_names[i];
3b7344d5 3558 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3559 {
3b7344d5 3560 struct bound_minimal_symbol m;
17450429
PP
3561
3562 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3563 if (m.minsym == NULL)
17450429
PP
3564 {
3565 /* Prevent future lookups in this objfile. */
3b7344d5 3566 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3567 continue;
3568 }
3569 bp_objfile_data->longjmp_msym[i] = m;
3570 }
3571
77e371c0 3572 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3573 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3574 &internal_breakpoint_ops);
67994074
KS
3575 initialize_explicit_location (&explicit_loc);
3576 explicit_loc.function_name = ASTRDUP (func_name);
3577 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3578 b->enable_state = bp_disabled;
3579 }
0fd8e87f 3580 }
af02033e 3581 }
44702360 3582 update_global_location_list (UGLL_MAY_INSERT);
6c95b8df
PA
3583
3584 do_cleanups (old_chain);
0fd8e87f
UW
3585}
3586
af02033e 3587/* Create a master std::terminate breakpoint. */
aa7d318d 3588static void
af02033e 3589create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3590{
3591 struct program_space *pspace;
aa7d318d 3592 struct cleanup *old_chain;
af02033e 3593 const char *const func_name = "std::terminate()";
aa7d318d
TT
3594
3595 old_chain = save_current_program_space ();
3596
3597 ALL_PSPACES (pspace)
17450429
PP
3598 {
3599 struct objfile *objfile;
3600 CORE_ADDR addr;
3601
3602 set_current_program_space (pspace);
3603
aa7d318d
TT
3604 ALL_OBJFILES (objfile)
3605 {
3606 struct breakpoint *b;
17450429 3607 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3608 struct explicit_location explicit_loc;
aa7d318d 3609
17450429 3610 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3611
3b7344d5 3612 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3613 continue;
3614
3b7344d5 3615 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3616 {
3b7344d5 3617 struct bound_minimal_symbol m;
17450429
PP
3618
3619 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3620 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3621 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3622 {
3623 /* Prevent future lookups in this objfile. */
3b7344d5 3624 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3625 continue;
3626 }
3627 bp_objfile_data->terminate_msym = m;
3628 }
aa7d318d 3629
77e371c0 3630 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3631 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3632 bp_std_terminate_master,
3633 &internal_breakpoint_ops);
67994074
KS
3634 initialize_explicit_location (&explicit_loc);
3635 explicit_loc.function_name = ASTRDUP (func_name);
3636 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3637 b->enable_state = bp_disabled;
3638 }
17450429
PP
3639 }
3640
44702360 3641 update_global_location_list (UGLL_MAY_INSERT);
aa7d318d
TT
3642
3643 do_cleanups (old_chain);
3644}
3645
186c406b
TT
3646/* Install a master breakpoint on the unwinder's debug hook. */
3647
70221824 3648static void
186c406b
TT
3649create_exception_master_breakpoint (void)
3650{
3651 struct objfile *objfile;
17450429 3652 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3653
3654 ALL_OBJFILES (objfile)
3655 {
17450429
PP
3656 struct breakpoint *b;
3657 struct gdbarch *gdbarch;
3658 struct breakpoint_objfile_data *bp_objfile_data;
3659 CORE_ADDR addr;
67994074 3660 struct explicit_location explicit_loc;
17450429
PP
3661
3662 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3663
28106bc2
SDJ
3664 /* We prefer the SystemTap probe point if it exists. */
3665 if (!bp_objfile_data->exception_searched)
3666 {
25f9533e
SDJ
3667 VEC (probe_p) *ret;
3668
3669 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3670
3671 if (ret != NULL)
3672 {
3673 /* We are only interested in checking one element. */
3674 struct probe *p = VEC_index (probe_p, ret, 0);
3675
3676 if (!can_evaluate_probe_arguments (p))
3677 {
3678 /* We cannot use the probe interface here, because it does
3679 not know how to evaluate arguments. */
3680 VEC_free (probe_p, ret);
3681 ret = NULL;
3682 }
3683 }
3684 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3685 bp_objfile_data->exception_searched = 1;
3686 }
3687
3688 if (bp_objfile_data->exception_probes != NULL)
3689 {
3690 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3691 int i;
3692 struct probe *probe;
3693
3694 for (i = 0;
3695 VEC_iterate (probe_p,
3696 bp_objfile_data->exception_probes,
3697 i, probe);
3698 ++i)
3699 {
3700 struct breakpoint *b;
3701
729662a5
TT
3702 b = create_internal_breakpoint (gdbarch,
3703 get_probe_address (probe,
3704 objfile),
28106bc2
SDJ
3705 bp_exception_master,
3706 &internal_breakpoint_ops);
5b56227b
KS
3707 b->location
3708 = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3709 b->enable_state = bp_disabled;
3710 }
3711
3712 continue;
3713 }
3714
3715 /* Otherwise, try the hook function. */
3716
3b7344d5 3717 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3718 continue;
3719
3720 gdbarch = get_objfile_arch (objfile);
186c406b 3721
3b7344d5 3722 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3723 {
3b7344d5 3724 struct bound_minimal_symbol debug_hook;
186c406b 3725
17450429 3726 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3727 if (debug_hook.minsym == NULL)
17450429 3728 {
3b7344d5 3729 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3730 continue;
3731 }
3732
3733 bp_objfile_data->exception_msym = debug_hook;
186c406b 3734 }
17450429 3735
77e371c0 3736 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3737 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3738 &current_target);
06edf0c0
PA
3739 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3740 &internal_breakpoint_ops);
67994074
KS
3741 initialize_explicit_location (&explicit_loc);
3742 explicit_loc.function_name = ASTRDUP (func_name);
3743 b->location = new_explicit_location (&explicit_loc);
17450429 3744 b->enable_state = bp_disabled;
186c406b
TT
3745 }
3746
44702360 3747 update_global_location_list (UGLL_MAY_INSERT);
186c406b
TT
3748}
3749
c906108c 3750void
fba45db2 3751update_breakpoints_after_exec (void)
c906108c 3752{
35df4500 3753 struct breakpoint *b, *b_tmp;
876fa593 3754 struct bp_location *bploc, **bplocp_tmp;
c906108c 3755
25b22b0a
PA
3756 /* We're about to delete breakpoints from GDB's lists. If the
3757 INSERTED flag is true, GDB will try to lift the breakpoints by
3758 writing the breakpoints' "shadow contents" back into memory. The
3759 "shadow contents" are NOT valid after an exec, so GDB should not
3760 do that. Instead, the target is responsible from marking
3761 breakpoints out as soon as it detects an exec. We don't do that
3762 here instead, because there may be other attempts to delete
3763 breakpoints after detecting an exec and before reaching here. */
876fa593 3764 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3765 if (bploc->pspace == current_program_space)
3766 gdb_assert (!bploc->inserted);
c906108c 3767
35df4500 3768 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3769 {
6c95b8df
PA
3770 if (b->pspace != current_program_space)
3771 continue;
3772
4a64f543 3773 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3774 if (b->type == bp_shlib_event)
3775 {
3776 delete_breakpoint (b);
3777 continue;
3778 }
c906108c 3779
4a64f543 3780 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3781 if (b->type == bp_jit_event)
3782 {
3783 delete_breakpoint (b);
3784 continue;
3785 }
3786
1900040c 3787 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3788 as must overlay event and longjmp master breakpoints. */
3789 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3790 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3791 || b->type == bp_exception_master)
c4093a6a
JM
3792 {
3793 delete_breakpoint (b);
3794 continue;
3795 }
3796
4a64f543 3797 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3798 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3799 {
3800 delete_breakpoint (b);
3801 continue;
3802 }
3803
7c16b83e
PA
3804 /* Just like single-step breakpoints. */
3805 if (b->type == bp_single_step)
3806 {
3807 delete_breakpoint (b);
3808 continue;
3809 }
3810
611c83ae
PA
3811 /* Longjmp and longjmp-resume breakpoints are also meaningless
3812 after an exec. */
186c406b 3813 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3814 || b->type == bp_longjmp_call_dummy
186c406b 3815 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3816 {
3817 delete_breakpoint (b);
3818 continue;
3819 }
3820
ce78b96d
JB
3821 if (b->type == bp_catchpoint)
3822 {
3823 /* For now, none of the bp_catchpoint breakpoints need to
3824 do anything at this point. In the future, if some of
3825 the catchpoints need to something, we will need to add
3826 a new method, and call this method from here. */
3827 continue;
3828 }
3829
c5aa993b
JM
3830 /* bp_finish is a special case. The only way we ought to be able
3831 to see one of these when an exec() has happened, is if the user
3832 caught a vfork, and then said "finish". Ordinarily a finish just
3833 carries them to the call-site of the current callee, by setting
3834 a temporary bp there and resuming. But in this case, the finish
3835 will carry them entirely through the vfork & exec.
3836
3837 We don't want to allow a bp_finish to remain inserted now. But
3838 we can't safely delete it, 'cause finish_command has a handle to
3839 the bp on a bpstat, and will later want to delete it. There's a
3840 chance (and I've seen it happen) that if we delete the bp_finish
3841 here, that its storage will get reused by the time finish_command
3842 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3843 We really must allow finish_command to delete a bp_finish.
3844
e5dd4106 3845 In the absence of a general solution for the "how do we know
53a5351d
JM
3846 it's safe to delete something others may have handles to?"
3847 problem, what we'll do here is just uninsert the bp_finish, and
3848 let finish_command delete it.
3849
3850 (We know the bp_finish is "doomed" in the sense that it's
3851 momentary, and will be deleted as soon as finish_command sees
3852 the inferior stopped. So it doesn't matter that the bp's
3853 address is probably bogus in the new a.out, unlike e.g., the
3854 solib breakpoints.) */
c5aa993b 3855
c5aa993b
JM
3856 if (b->type == bp_finish)
3857 {
3858 continue;
3859 }
3860
3861 /* Without a symbolic address, we have little hope of the
3862 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3863 a.out. */
f00aae0f 3864 if (event_location_empty_p (b->location))
c5aa993b
JM
3865 {
3866 delete_breakpoint (b);
3867 continue;
3868 }
c5aa993b 3869 }
c906108c
SS
3870}
3871
3872int
d80ee84f 3873detach_breakpoints (ptid_t ptid)
c906108c 3874{
35df4500 3875 struct bp_location *bl, **blp_tmp;
3a1bae8e 3876 int val = 0;
ce696e05 3877 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3878 struct inferior *inf = current_inferior ();
c5aa993b 3879
dfd4cc63 3880 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3881 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3882
6c95b8df 3883 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3884 inferior_ptid = ptid;
35df4500 3885 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3886 {
35df4500 3887 if (bl->pspace != inf->pspace)
6c95b8df
PA
3888 continue;
3889
bd9673a4
PW
3890 /* This function must physically remove breakpoints locations
3891 from the specified ptid, without modifying the breakpoint
3892 package's state. Locations of type bp_loc_other are only
3893 maintained at GDB side. So, there is no need to remove
3894 these bp_loc_other locations. Moreover, removing these
3895 would modify the breakpoint package's state. */
3896 if (bl->loc_type == bp_loc_other)
3897 continue;
3898
35df4500
TJB
3899 if (bl->inserted)
3900 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3901 }
d03285ec 3902
ce696e05 3903 do_cleanups (old_chain);
3a1bae8e 3904 return val;
c906108c
SS
3905}
3906
35df4500 3907/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3908 Note that this is used to detach breakpoints from a child fork.
3909 When we get here, the child isn't in the inferior list, and neither
3910 do we have objects to represent its address space --- we should
35df4500 3911 *not* look at bl->pspace->aspace here. */
6c95b8df 3912
c906108c 3913static int
35df4500 3914remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3915{
3916 int val;
c5aa993b 3917
35df4500
TJB
3918 /* BL is never in moribund_locations by our callers. */
3919 gdb_assert (bl->owner != NULL);
2bdf28a0 3920
74960c60
VP
3921 /* The type of none suggests that owner is actually deleted.
3922 This should not ever happen. */
35df4500 3923 gdb_assert (bl->owner->type != bp_none);
0bde7532 3924
35df4500
TJB
3925 if (bl->loc_type == bp_loc_software_breakpoint
3926 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3927 {
c02f5703
MS
3928 /* "Normal" instruction breakpoint: either the standard
3929 trap-instruction bp (bp_breakpoint), or a
3930 bp_hardware_breakpoint. */
3931
3932 /* First check to see if we have to handle an overlay. */
3933 if (overlay_debugging == ovly_off
35df4500
TJB
3934 || bl->section == NULL
3935 || !(section_is_overlay (bl->section)))
c02f5703
MS
3936 {
3937 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3938
3939 /* If we're trying to uninsert a memory breakpoint that we
3940 know is set in a dynamic object that is marked
3941 shlib_disabled, then either the dynamic object was
3942 removed with "remove-symbol-file" or with
3943 "nosharedlibrary". In the former case, we don't know
3944 whether another dynamic object might have loaded over the
3945 breakpoint's address -- the user might well let us know
3946 about it next with add-symbol-file (the whole point of
d03de421 3947 add-symbol-file is letting the user manually maintain a
08351840
PA
3948 list of dynamically loaded objects). If we have the
3949 breakpoint's shadow memory, that is, this is a software
3950 breakpoint managed by GDB, check whether the breakpoint
3951 is still inserted in memory, to avoid overwriting wrong
3952 code with stale saved shadow contents. Note that HW
3953 breakpoints don't have shadow memory, as they're
3954 implemented using a mechanism that is not dependent on
3955 being able to modify the target's memory, and as such
3956 they should always be removed. */
3957 if (bl->shlib_disabled
3958 && bl->target_info.shadow_len != 0
3959 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3960 val = 0;
3961 else
3962 val = bl->owner->ops->remove_location (bl);
c02f5703 3963 }
c906108c
SS
3964 else
3965 {
4a64f543 3966 /* This breakpoint is in an overlay section.
c02f5703
MS
3967 Did we set a breakpoint at the LMA? */
3968 if (!overlay_events_enabled)
3969 {
3970 /* Yes -- overlay event support is not active, so we
3971 should have set a breakpoint at the LMA. Remove it.
3972 */
c02f5703
MS
3973 /* Ignore any failures: if the LMA is in ROM, we will
3974 have already warned when we failed to insert it. */
35df4500
TJB
3975 if (bl->loc_type == bp_loc_hardware_breakpoint)
3976 target_remove_hw_breakpoint (bl->gdbarch,
3977 &bl->overlay_target_info);
c02f5703 3978 else
35df4500
TJB
3979 target_remove_breakpoint (bl->gdbarch,
3980 &bl->overlay_target_info);
c02f5703
MS
3981 }
3982 /* Did we set a breakpoint at the VMA?
3983 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3984 if (bl->inserted)
c906108c 3985 {
c02f5703
MS
3986 /* Yes -- remove it. Previously we did not bother to
3987 remove the breakpoint if the section had been
3988 unmapped, but let's not rely on that being safe. We
3989 don't know what the overlay manager might do. */
aa67235e
UW
3990
3991 /* However, we should remove *software* breakpoints only
3992 if the section is still mapped, or else we overwrite
3993 wrong code with the saved shadow contents. */
348d480f
PA
3994 if (bl->loc_type == bp_loc_hardware_breakpoint
3995 || section_is_mapped (bl->section))
3996 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3997 else
3998 val = 0;
c906108c 3999 }
c02f5703
MS
4000 else
4001 {
4002 /* No -- not inserted, so no need to remove. No error. */
4003 val = 0;
4004 }
c906108c 4005 }
879d1e6b 4006
08351840
PA
4007 /* In some cases, we might not be able to remove a breakpoint in
4008 a shared library that has already been removed, but we have
4009 not yet processed the shlib unload event. Similarly for an
4010 unloaded add-symbol-file object - the user might not yet have
4011 had the chance to remove-symbol-file it. shlib_disabled will
4012 be set if the library/object has already been removed, but
4013 the breakpoint hasn't been uninserted yet, e.g., after
4014 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4015 always-inserted mode. */
076855f9 4016 if (val
08351840
PA
4017 && (bl->loc_type == bp_loc_software_breakpoint
4018 && (bl->shlib_disabled
4019 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4020 || shared_objfile_contains_address_p (bl->pspace,
4021 bl->address))))
879d1e6b
UW
4022 val = 0;
4023
c906108c
SS
4024 if (val)
4025 return val;
35df4500 4026 bl->inserted = (is == mark_inserted);
c906108c 4027 }
35df4500 4028 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4029 {
77b06cd7
TJB
4030 gdb_assert (bl->owner->ops != NULL
4031 && bl->owner->ops->remove_location != NULL);
4032
35df4500 4033 bl->inserted = (is == mark_inserted);
77b06cd7 4034 bl->owner->ops->remove_location (bl);
2e70b7b9 4035
c906108c 4036 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 4037 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4038 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4039 bl->owner->number);
c906108c 4040 }
35df4500
TJB
4041 else if (bl->owner->type == bp_catchpoint
4042 && breakpoint_enabled (bl->owner)
4043 && !bl->duplicate)
ce78b96d 4044 {
77b06cd7
TJB
4045 gdb_assert (bl->owner->ops != NULL
4046 && bl->owner->ops->remove_location != NULL);
ce78b96d 4047
77b06cd7 4048 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4049 if (val)
4050 return val;
77b06cd7 4051
35df4500 4052 bl->inserted = (is == mark_inserted);
ce78b96d 4053 }
c906108c
SS
4054
4055 return 0;
4056}
4057
6c95b8df 4058static int
35df4500 4059remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4060{
4061 int ret;
4062 struct cleanup *old_chain;
4063
35df4500
TJB
4064 /* BL is never in moribund_locations by our callers. */
4065 gdb_assert (bl->owner != NULL);
2bdf28a0 4066
6c95b8df
PA
4067 /* The type of none suggests that owner is actually deleted.
4068 This should not ever happen. */
35df4500 4069 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4070
4071 old_chain = save_current_space_and_thread ();
4072
35df4500 4073 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4074
35df4500 4075 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4076
4077 do_cleanups (old_chain);
4078 return ret;
4079}
4080
c906108c
SS
4081/* Clear the "inserted" flag in all breakpoints. */
4082
25b22b0a 4083void
fba45db2 4084mark_breakpoints_out (void)
c906108c 4085{
35df4500 4086 struct bp_location *bl, **blp_tmp;
c906108c 4087
35df4500 4088 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4089 if (bl->pspace == current_program_space)
35df4500 4090 bl->inserted = 0;
c906108c
SS
4091}
4092
53a5351d
JM
4093/* Clear the "inserted" flag in all breakpoints and delete any
4094 breakpoints which should go away between runs of the program.
c906108c
SS
4095
4096 Plus other such housekeeping that has to be done for breakpoints
4097 between runs.
4098
53a5351d
JM
4099 Note: this function gets called at the end of a run (by
4100 generic_mourn_inferior) and when a run begins (by
4a64f543 4101 init_wait_for_inferior). */
c906108c
SS
4102
4103
4104
4105void
fba45db2 4106breakpoint_init_inferior (enum inf_context context)
c906108c 4107{
35df4500
TJB
4108 struct breakpoint *b, *b_tmp;
4109 struct bp_location *bl, **blp_tmp;
1c5cfe86 4110 int ix;
6c95b8df 4111 struct program_space *pspace = current_program_space;
c906108c 4112
50c71eaf
PA
4113 /* If breakpoint locations are shared across processes, then there's
4114 nothing to do. */
f5656ead 4115 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4116 return;
4117
1a853c52 4118 mark_breakpoints_out ();
075f6582 4119
35df4500 4120 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4121 {
6c95b8df
PA
4122 if (b->loc && b->loc->pspace != pspace)
4123 continue;
4124
c5aa993b
JM
4125 switch (b->type)
4126 {
4127 case bp_call_dummy:
e2e4d78b 4128 case bp_longjmp_call_dummy:
c906108c 4129
c5aa993b 4130 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4131 cause problems when the inferior is rerun, so we better get
4132 rid of it. */
4133
4134 case bp_watchpoint_scope:
4135
4136 /* Also get rid of scope breakpoints. */
4137
4138 case bp_shlib_event:
4139
4140 /* Also remove solib event breakpoints. Their addresses may
4141 have changed since the last time we ran the program.
4142 Actually we may now be debugging against different target;
4143 and so the solib backend that installed this breakpoint may
4144 not be used in by the target. E.g.,
4145
4146 (gdb) file prog-linux
4147 (gdb) run # native linux target
4148 ...
4149 (gdb) kill
4150 (gdb) file prog-win.exe
4151 (gdb) tar rem :9999 # remote Windows gdbserver.
4152 */
c906108c 4153
f59f708a
PA
4154 case bp_step_resume:
4155
4156 /* Also remove step-resume breakpoints. */
4157
7c16b83e
PA
4158 case bp_single_step:
4159
4160 /* Also remove single-step breakpoints. */
4161
c5aa993b
JM
4162 delete_breakpoint (b);
4163 break;
c906108c 4164
c5aa993b
JM
4165 case bp_watchpoint:
4166 case bp_hardware_watchpoint:
4167 case bp_read_watchpoint:
4168 case bp_access_watchpoint:
3a5c3e22
PA
4169 {
4170 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4171
3a5c3e22
PA
4172 /* Likewise for watchpoints on local expressions. */
4173 if (w->exp_valid_block != NULL)
4174 delete_breakpoint (b);
4175 else if (context == inf_starting)
4176 {
4177 /* Reset val field to force reread of starting value in
4178 insert_breakpoints. */
4179 if (w->val)
4180 value_free (w->val);
4181 w->val = NULL;
4182 w->val_valid = 0;
c860120c 4183 }
3a5c3e22 4184 }
c5aa993b
JM
4185 break;
4186 default:
c5aa993b
JM
4187 break;
4188 }
4189 }
1c5cfe86
PA
4190
4191 /* Get rid of the moribund locations. */
35df4500
TJB
4192 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4193 decref_bp_location (&bl);
1c5cfe86 4194 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4195}
4196
6c95b8df
PA
4197/* These functions concern about actual breakpoints inserted in the
4198 target --- to e.g. check if we need to do decr_pc adjustment or if
4199 we need to hop over the bkpt --- so we check for address space
4200 match, not program space. */
4201
c2c6d25f
JM
4202/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4203 exists at PC. It returns ordinary_breakpoint_here if it's an
4204 ordinary breakpoint, or permanent_breakpoint_here if it's a
4205 permanent breakpoint.
4206 - When continuing from a location with an ordinary breakpoint, we
4207 actually single step once before calling insert_breakpoints.
e5dd4106 4208 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4209 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4210 the target, to advance the PC past the breakpoint. */
c906108c 4211
c2c6d25f 4212enum breakpoint_here
6c95b8df 4213breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4214{
35df4500 4215 struct bp_location *bl, **blp_tmp;
c2c6d25f 4216 int any_breakpoint_here = 0;
c906108c 4217
35df4500 4218 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4219 {
35df4500
TJB
4220 if (bl->loc_type != bp_loc_software_breakpoint
4221 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4222 continue;
4223
f1310107 4224 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4225 if ((breakpoint_enabled (bl->owner)
1a853c52 4226 || bl->permanent)
f1310107 4227 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4228 {
4229 if (overlay_debugging
35df4500
TJB
4230 && section_is_overlay (bl->section)
4231 && !section_is_mapped (bl->section))
075f6582 4232 continue; /* unmapped overlay -- can't be a match */
1a853c52 4233 else if (bl->permanent)
075f6582
DJ
4234 return permanent_breakpoint_here;
4235 else
4236 any_breakpoint_here = 1;
4237 }
4238 }
c906108c 4239
f486487f 4240 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4241}
4242
1c5cfe86
PA
4243/* Return true if there's a moribund breakpoint at PC. */
4244
4245int
6c95b8df 4246moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4247{
4248 struct bp_location *loc;
4249 int ix;
4250
4251 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4252 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4253 return 1;
4254
4255 return 0;
4256}
c2c6d25f 4257
f7ce857f
PA
4258/* Returns non-zero iff BL is inserted at PC, in address space
4259 ASPACE. */
4260
4261static int
4262bp_location_inserted_here_p (struct bp_location *bl,
4263 struct address_space *aspace, CORE_ADDR pc)
4264{
4265 if (bl->inserted
4266 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4267 aspace, pc))
4268 {
4269 if (overlay_debugging
4270 && section_is_overlay (bl->section)
4271 && !section_is_mapped (bl->section))
4272 return 0; /* unmapped overlay -- can't be a match */
4273 else
4274 return 1;
4275 }
4276 return 0;
4277}
4278
a1fd2fa5 4279/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4280
4281int
a1fd2fa5 4282breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4283{
f7ce857f
PA
4284 struct bp_location **blp, **blp_tmp = NULL;
4285 struct bp_location *bl;
c906108c 4286
f7ce857f 4287 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4288 {
f7ce857f
PA
4289 struct bp_location *bl = *blp;
4290
35df4500
TJB
4291 if (bl->loc_type != bp_loc_software_breakpoint
4292 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4293 continue;
4294
f7ce857f
PA
4295 if (bp_location_inserted_here_p (bl, aspace, pc))
4296 return 1;
c5aa993b 4297 }
c36b740a
VP
4298 return 0;
4299}
4300
a1fd2fa5
PA
4301/* This function returns non-zero iff there is a software breakpoint
4302 inserted at PC. */
c36b740a
VP
4303
4304int
a1fd2fa5
PA
4305software_breakpoint_inserted_here_p (struct address_space *aspace,
4306 CORE_ADDR pc)
4fa8626c 4307{
f7ce857f
PA
4308 struct bp_location **blp, **blp_tmp = NULL;
4309 struct bp_location *bl;
4fa8626c 4310
f7ce857f 4311 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4312 {
f7ce857f
PA
4313 struct bp_location *bl = *blp;
4314
35df4500 4315 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4316 continue;
4317
f7ce857f
PA
4318 if (bp_location_inserted_here_p (bl, aspace, pc))
4319 return 1;
4fa8626c
DJ
4320 }
4321
4322 return 0;
9c02b525
PA
4323}
4324
4325/* See breakpoint.h. */
4326
4327int
4328hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4329 CORE_ADDR pc)
4330{
4331 struct bp_location **blp, **blp_tmp = NULL;
4332 struct bp_location *bl;
4333
4334 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4335 {
4336 struct bp_location *bl = *blp;
4337
4338 if (bl->loc_type != bp_loc_hardware_breakpoint)
4339 continue;
4340
4341 if (bp_location_inserted_here_p (bl, aspace, pc))
4342 return 1;
4343 }
4344
4345 return 0;
4fa8626c
DJ
4346}
4347
9093389c
PA
4348int
4349hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4350 CORE_ADDR addr, ULONGEST len)
4351{
4352 struct breakpoint *bpt;
4353
4354 ALL_BREAKPOINTS (bpt)
4355 {
4356 struct bp_location *loc;
4357
4358 if (bpt->type != bp_hardware_watchpoint
4359 && bpt->type != bp_access_watchpoint)
4360 continue;
4361
4362 if (!breakpoint_enabled (bpt))
4363 continue;
4364
4365 for (loc = bpt->loc; loc; loc = loc->next)
4366 if (loc->pspace->aspace == aspace && loc->inserted)
4367 {
4368 CORE_ADDR l, h;
4369
4370 /* Check for intersection. */
4371 l = max (loc->address, addr);
4372 h = min (loc->address + loc->length, addr + len);
4373 if (l < h)
4374 return 1;
4375 }
4376 }
4377 return 0;
4378}
c906108c 4379\f
c5aa993b 4380
c906108c
SS
4381/* bpstat stuff. External routines' interfaces are documented
4382 in breakpoint.h. */
4383
4384int
c326b90e 4385is_catchpoint (struct breakpoint *ep)
c906108c 4386{
533be4dd 4387 return (ep->type == bp_catchpoint);
c906108c
SS
4388}
4389
f431efe5
PA
4390/* Frees any storage that is part of a bpstat. Does not walk the
4391 'next' chain. */
4392
4393static void
198757a8
VP
4394bpstat_free (bpstat bs)
4395{
4396 if (bs->old_val != NULL)
4397 value_free (bs->old_val);
9add0f1b 4398 decref_counted_command_line (&bs->commands);
f431efe5 4399 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4400 xfree (bs);
4401}
4402
c906108c
SS
4403/* Clear a bpstat so that it says we are not at any breakpoint.
4404 Also free any storage that is part of a bpstat. */
4405
4406void
fba45db2 4407bpstat_clear (bpstat *bsp)
c906108c
SS
4408{
4409 bpstat p;
4410 bpstat q;
4411
4412 if (bsp == 0)
4413 return;
4414 p = *bsp;
4415 while (p != NULL)
4416 {
4417 q = p->next;
198757a8 4418 bpstat_free (p);
c906108c
SS
4419 p = q;
4420 }
4421 *bsp = NULL;
4422}
4423
4424/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4425 is part of the bpstat is copied as well. */
4426
4427bpstat
fba45db2 4428bpstat_copy (bpstat bs)
c906108c
SS
4429{
4430 bpstat p = NULL;
4431 bpstat tmp;
4432 bpstat retval = NULL;
4433
4434 if (bs == NULL)
4435 return bs;
4436
4437 for (; bs != NULL; bs = bs->next)
4438 {
4439 tmp = (bpstat) xmalloc (sizeof (*tmp));
4440 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4441 incref_counted_command_line (tmp->commands);
f431efe5 4442 incref_bp_location (tmp->bp_location_at);
31cc81e9 4443 if (bs->old_val != NULL)
3c3185ac
JK
4444 {
4445 tmp->old_val = value_copy (bs->old_val);
4446 release_value (tmp->old_val);
4447 }
31cc81e9 4448
c906108c
SS
4449 if (p == NULL)
4450 /* This is the first thing in the chain. */
4451 retval = tmp;
4452 else
4453 p->next = tmp;
4454 p = tmp;
4455 }
4456 p->next = NULL;
4457 return retval;
4458}
4459
4a64f543 4460/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4461
4462bpstat
fba45db2 4463bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4464{
c5aa993b
JM
4465 if (bsp == NULL)
4466 return NULL;
c906108c 4467
c5aa993b
JM
4468 for (; bsp != NULL; bsp = bsp->next)
4469 {
f431efe5 4470 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4471 return bsp;
4472 }
c906108c
SS
4473 return NULL;
4474}
4475
ab04a2af
TT
4476/* See breakpoint.h. */
4477
47591c29 4478int
427cd150 4479bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4480{
ab04a2af
TT
4481 for (; bsp != NULL; bsp = bsp->next)
4482 {
427cd150
TT
4483 if (bsp->breakpoint_at == NULL)
4484 {
4485 /* A moribund location can never explain a signal other than
4486 GDB_SIGNAL_TRAP. */
4487 if (sig == GDB_SIGNAL_TRAP)
47591c29 4488 return 1;
427cd150
TT
4489 }
4490 else
47591c29
PA
4491 {
4492 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4493 sig))
4494 return 1;
4495 }
ab04a2af
TT
4496 }
4497
47591c29 4498 return 0;
ab04a2af
TT
4499}
4500
4a64f543
MS
4501/* Put in *NUM the breakpoint number of the first breakpoint we are
4502 stopped at. *BSP upon return is a bpstat which points to the
4503 remaining breakpoints stopped at (but which is not guaranteed to be
4504 good for anything but further calls to bpstat_num).
4505
8671a17b
PA
4506 Return 0 if passed a bpstat which does not indicate any breakpoints.
4507 Return -1 if stopped at a breakpoint that has been deleted since
4508 we set it.
4509 Return 1 otherwise. */
c906108c
SS
4510
4511int
8671a17b 4512bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4513{
4514 struct breakpoint *b;
4515
4516 if ((*bsp) == NULL)
4517 return 0; /* No more breakpoint values */
8671a17b 4518
4a64f543
MS
4519 /* We assume we'll never have several bpstats that correspond to a
4520 single breakpoint -- otherwise, this function might return the
4521 same number more than once and this will look ugly. */
f431efe5 4522 b = (*bsp)->breakpoint_at;
8671a17b
PA
4523 *bsp = (*bsp)->next;
4524 if (b == NULL)
4525 return -1; /* breakpoint that's been deleted since */
4526
4527 *num = b->number; /* We have its number */
4528 return 1;
c906108c
SS
4529}
4530
e93ca019 4531/* See breakpoint.h. */
c906108c
SS
4532
4533void
e93ca019 4534bpstat_clear_actions (void)
c906108c 4535{
e93ca019
JK
4536 struct thread_info *tp;
4537 bpstat bs;
4538
4539 if (ptid_equal (inferior_ptid, null_ptid))
4540 return;
4541
4542 tp = find_thread_ptid (inferior_ptid);
4543 if (tp == NULL)
4544 return;
4545
4546 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4547 {
9add0f1b 4548 decref_counted_command_line (&bs->commands);
abf85f46 4549
c906108c
SS
4550 if (bs->old_val != NULL)
4551 {
4552 value_free (bs->old_val);
4553 bs->old_val = NULL;
4554 }
4555 }
4556}
4557
f3b1572e
PA
4558/* Called when a command is about to proceed the inferior. */
4559
4560static void
4561breakpoint_about_to_proceed (void)
4562{
4563 if (!ptid_equal (inferior_ptid, null_ptid))
4564 {
4565 struct thread_info *tp = inferior_thread ();
4566
4567 /* Allow inferior function calls in breakpoint commands to not
4568 interrupt the command list. When the call finishes
4569 successfully, the inferior will be standing at the same
4570 breakpoint as if nothing happened. */
16c381f0 4571 if (tp->control.in_infcall)
f3b1572e
PA
4572 return;
4573 }
4574
4575 breakpoint_proceeded = 1;
4576}
4577
4a64f543
MS
4578/* Stub for cleaning up our state if we error-out of a breakpoint
4579 command. */
c906108c 4580static void
4efb68b1 4581cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4582{
4583 executing_breakpoint_commands = 0;
4584}
4585
abf85f46
JK
4586/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4587 or its equivalent. */
4588
4589static int
4590command_line_is_silent (struct command_line *cmd)
4591{
4f45d445 4592 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4593}
4594
4a64f543
MS
4595/* Execute all the commands associated with all the breakpoints at
4596 this location. Any of these commands could cause the process to
4597 proceed beyond this point, etc. We look out for such changes by
4598 checking the global "breakpoint_proceeded" after each command.
c906108c 4599
347bddb7
PA
4600 Returns true if a breakpoint command resumed the inferior. In that
4601 case, it is the caller's responsibility to recall it again with the
4602 bpstat of the current thread. */
4603
4604static int
4605bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4606{
4607 bpstat bs;
4608 struct cleanup *old_chain;
347bddb7 4609 int again = 0;
c906108c
SS
4610
4611 /* Avoid endless recursion if a `source' command is contained
4612 in bs->commands. */
4613 if (executing_breakpoint_commands)
347bddb7 4614 return 0;
c906108c
SS
4615
4616 executing_breakpoint_commands = 1;
4617 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4618
cf6c5ffb
TT
4619 prevent_dont_repeat ();
4620
4a64f543 4621 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4622 bs = *bsp;
4623
4624 breakpoint_proceeded = 0;
4625 for (; bs != NULL; bs = bs->next)
4626 {
9add0f1b 4627 struct counted_command_line *ccmd;
6c50ab1c
JB
4628 struct command_line *cmd;
4629 struct cleanup *this_cmd_tree_chain;
4630
4631 /* Take ownership of the BSP's command tree, if it has one.
4632
4633 The command tree could legitimately contain commands like
4634 'step' and 'next', which call clear_proceed_status, which
4635 frees stop_bpstat's command tree. To make sure this doesn't
4636 free the tree we're executing out from under us, we need to
4637 take ownership of the tree ourselves. Since a given bpstat's
4638 commands are only executed once, we don't need to copy it; we
4639 can clear the pointer in the bpstat, and make sure we free
4640 the tree when we're done. */
9add0f1b
TT
4641 ccmd = bs->commands;
4642 bs->commands = NULL;
abf85f46
JK
4643 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4644 cmd = ccmd ? ccmd->commands : NULL;
4645 if (command_line_is_silent (cmd))
4646 {
4647 /* The action has been already done by bpstat_stop_status. */
4648 cmd = cmd->next;
4649 }
6c50ab1c 4650
c906108c
SS
4651 while (cmd != NULL)
4652 {
4653 execute_control_command (cmd);
4654
4655 if (breakpoint_proceeded)
4656 break;
4657 else
4658 cmd = cmd->next;
4659 }
6c50ab1c
JB
4660
4661 /* We can free this command tree now. */
4662 do_cleanups (this_cmd_tree_chain);
4663
c906108c 4664 if (breakpoint_proceeded)
32c1e744 4665 {
0b333c5e 4666 if (interpreter_async)
347bddb7
PA
4667 /* If we are in async mode, then the target might be still
4668 running, not stopped at any breakpoint, so nothing for
4669 us to do here -- just return to the event loop. */
4670 ;
32c1e744
VP
4671 else
4672 /* In sync mode, when execute_control_command returns
4673 we're already standing on the next breakpoint.
347bddb7
PA
4674 Breakpoint commands for that stop were not run, since
4675 execute_command does not run breakpoint commands --
4676 only command_line_handler does, but that one is not
4677 involved in execution of breakpoint commands. So, we
4678 can now execute breakpoint commands. It should be
4679 noted that making execute_command do bpstat actions is
4680 not an option -- in this case we'll have recursive
4681 invocation of bpstat for each breakpoint with a
4682 command, and can easily blow up GDB stack. Instead, we
4683 return true, which will trigger the caller to recall us
4684 with the new stop_bpstat. */
4685 again = 1;
4686 break;
32c1e744 4687 }
c906108c 4688 }
c2b8ed2c 4689 do_cleanups (old_chain);
347bddb7
PA
4690 return again;
4691}
4692
4693void
4694bpstat_do_actions (void)
4695{
353d1d73
JK
4696 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4697
347bddb7
PA
4698 /* Do any commands attached to breakpoint we are stopped at. */
4699 while (!ptid_equal (inferior_ptid, null_ptid)
4700 && target_has_execution
4701 && !is_exited (inferior_ptid)
4702 && !is_executing (inferior_ptid))
4703 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4704 and only return when it is stopped at the next breakpoint, we
4705 keep doing breakpoint actions until it returns false to
4706 indicate the inferior was not resumed. */
16c381f0 4707 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4708 break;
353d1d73
JK
4709
4710 discard_cleanups (cleanup_if_error);
c906108c
SS
4711}
4712
fa4727a6
DJ
4713/* Print out the (old or new) value associated with a watchpoint. */
4714
4715static void
4716watchpoint_value_print (struct value *val, struct ui_file *stream)
4717{
4718 if (val == NULL)
4719 fprintf_unfiltered (stream, _("<unreadable>"));
4720 else
79a45b7d
TT
4721 {
4722 struct value_print_options opts;
4723 get_user_print_options (&opts);
4724 value_print (val, stream, &opts);
4725 }
fa4727a6
DJ
4726}
4727
e514a9d6 4728/* Generic routine for printing messages indicating why we
4a64f543 4729 stopped. The behavior of this function depends on the value
e514a9d6
JM
4730 'print_it' in the bpstat structure. Under some circumstances we
4731 may decide not to print anything here and delegate the task to
4a64f543 4732 normal_stop(). */
e514a9d6
JM
4733
4734static enum print_stop_action
4735print_bp_stop_message (bpstat bs)
4736{
4737 switch (bs->print_it)
4738 {
4739 case print_it_noop:
4a64f543 4740 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4741 return PRINT_UNKNOWN;
4742 break;
4743
4744 case print_it_done:
4745 /* We still want to print the frame, but we already printed the
4a64f543 4746 relevant messages. */
e514a9d6
JM
4747 return PRINT_SRC_AND_LOC;
4748 break;
4749
4750 case print_it_normal:
4f8d1dc6 4751 {
f431efe5
PA
4752 struct breakpoint *b = bs->breakpoint_at;
4753
1a6a67de
TJB
4754 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4755 which has since been deleted. */
4756 if (b == NULL)
4757 return PRINT_UNKNOWN;
4758
348d480f
PA
4759 /* Normal case. Call the breakpoint's print_it method. */
4760 return b->ops->print_it (bs);
4f8d1dc6 4761 }
348d480f 4762 break;
3086aeae 4763
e514a9d6 4764 default:
8e65ff28 4765 internal_error (__FILE__, __LINE__,
e2e0b3e5 4766 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4767 break;
c906108c 4768 }
c906108c
SS
4769}
4770
edcc5120
TT
4771/* A helper function that prints a shared library stopped event. */
4772
4773static void
4774print_solib_event (int is_catchpoint)
4775{
4776 int any_deleted
4777 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4778 int any_added
4779 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4780
4781 if (!is_catchpoint)
4782 {
4783 if (any_added || any_deleted)
4784 ui_out_text (current_uiout,
4785 _("Stopped due to shared library event:\n"));
4786 else
4787 ui_out_text (current_uiout,
4788 _("Stopped due to shared library event (no "
4789 "libraries added or removed)\n"));
4790 }
4791
4792 if (ui_out_is_mi_like_p (current_uiout))
4793 ui_out_field_string (current_uiout, "reason",
4794 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4795
4796 if (any_deleted)
4797 {
4798 struct cleanup *cleanup;
4799 char *name;
4800 int ix;
4801
4802 ui_out_text (current_uiout, _(" Inferior unloaded "));
4803 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4804 "removed");
4805 for (ix = 0;
4806 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4807 ix, name);
4808 ++ix)
4809 {
4810 if (ix > 0)
4811 ui_out_text (current_uiout, " ");
4812 ui_out_field_string (current_uiout, "library", name);
4813 ui_out_text (current_uiout, "\n");
4814 }
4815
4816 do_cleanups (cleanup);
4817 }
4818
4819 if (any_added)
4820 {
4821 struct so_list *iter;
4822 int ix;
4823 struct cleanup *cleanup;
4824
4825 ui_out_text (current_uiout, _(" Inferior loaded "));
4826 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4827 "added");
4828 for (ix = 0;
4829 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4830 ix, iter);
4831 ++ix)
4832 {
4833 if (ix > 0)
4834 ui_out_text (current_uiout, " ");
4835 ui_out_field_string (current_uiout, "library", iter->so_name);
4836 ui_out_text (current_uiout, "\n");
4837 }
4838
4839 do_cleanups (cleanup);
4840 }
4841}
4842
e514a9d6
JM
4843/* Print a message indicating what happened. This is called from
4844 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4845 list - a list of the eventpoints that caused this stop. KIND is
4846 the target_waitkind for the stopping event. This
e514a9d6
JM
4847 routine calls the generic print routine for printing a message
4848 about reasons for stopping. This will print (for example) the
4849 "Breakpoint n," part of the output. The return value of this
4850 routine is one of:
c906108c 4851
4a64f543 4852 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4853 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4854 code to print the location. An example is
c5aa993b
JM
4855 "Breakpoint 1, " which should be followed by
4856 the location.
917317f4 4857 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4858 to also print the location part of the message.
4859 An example is the catch/throw messages, which
4a64f543 4860 don't require a location appended to the end.
917317f4 4861 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4862 further info to be printed. */
c906108c 4863
917317f4 4864enum print_stop_action
36dfb11c 4865bpstat_print (bpstat bs, int kind)
c906108c 4866{
f486487f 4867 enum print_stop_action val;
c5aa993b 4868
c906108c 4869 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4870 (Currently all watchpoints go on the bpstat whether hit or not.
4871 That probably could (should) be changed, provided care is taken
c906108c 4872 with respect to bpstat_explains_signal). */
e514a9d6
JM
4873 for (; bs; bs = bs->next)
4874 {
4875 val = print_bp_stop_message (bs);
4876 if (val == PRINT_SRC_ONLY
4877 || val == PRINT_SRC_AND_LOC
4878 || val == PRINT_NOTHING)
4879 return val;
4880 }
c906108c 4881
36dfb11c
TT
4882 /* If we had hit a shared library event breakpoint,
4883 print_bp_stop_message would print out this message. If we hit an
4884 OS-level shared library event, do the same thing. */
4885 if (kind == TARGET_WAITKIND_LOADED)
4886 {
edcc5120 4887 print_solib_event (0);
36dfb11c
TT
4888 return PRINT_NOTHING;
4889 }
4890
e514a9d6 4891 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4892 with and nothing was printed. */
917317f4 4893 return PRINT_UNKNOWN;
c906108c
SS
4894}
4895
c42bd95a
DE
4896/* Evaluate the expression EXP and return 1 if value is zero.
4897 This returns the inverse of the condition because it is called
4898 from catch_errors which returns 0 if an exception happened, and if an
4899 exception happens we want execution to stop.
4a64f543 4900 The argument is a "struct expression *" that has been cast to a
c42bd95a 4901 "void *" to make it pass through catch_errors. */
c906108c
SS
4902
4903static int
4efb68b1 4904breakpoint_cond_eval (void *exp)
c906108c 4905{
278cd55f 4906 struct value *mark = value_mark ();
c5aa993b 4907 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4908
c906108c
SS
4909 value_free_to_mark (mark);
4910 return i;
4911}
4912
5760d0ab 4913/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4914
4915static bpstat
5760d0ab 4916bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4917{
4918 bpstat bs;
4919
4920 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4921 bs->next = NULL;
4922 **bs_link_pointer = bs;
4923 *bs_link_pointer = &bs->next;
f431efe5
PA
4924 bs->breakpoint_at = bl->owner;
4925 bs->bp_location_at = bl;
4926 incref_bp_location (bl);
c906108c
SS
4927 /* If the condition is false, etc., don't do the commands. */
4928 bs->commands = NULL;
4929 bs->old_val = NULL;
4930 bs->print_it = print_it_normal;
4931 return bs;
4932}
4933\f
d983da9c
DJ
4934/* The target has stopped with waitstatus WS. Check if any hardware
4935 watchpoints have triggered, according to the target. */
4936
4937int
4938watchpoints_triggered (struct target_waitstatus *ws)
4939{
d92524f1 4940 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4941 CORE_ADDR addr;
4942 struct breakpoint *b;
4943
4944 if (!stopped_by_watchpoint)
4945 {
4946 /* We were not stopped by a watchpoint. Mark all watchpoints
4947 as not triggered. */
4948 ALL_BREAKPOINTS (b)
cc60f2e3 4949 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4950 {
4951 struct watchpoint *w = (struct watchpoint *) b;
4952
4953 w->watchpoint_triggered = watch_triggered_no;
4954 }
d983da9c
DJ
4955
4956 return 0;
4957 }
4958
4959 if (!target_stopped_data_address (&current_target, &addr))
4960 {
4961 /* We were stopped by a watchpoint, but we don't know where.
4962 Mark all watchpoints as unknown. */
4963 ALL_BREAKPOINTS (b)
cc60f2e3 4964 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4965 {
4966 struct watchpoint *w = (struct watchpoint *) b;
4967
4968 w->watchpoint_triggered = watch_triggered_unknown;
4969 }
d983da9c 4970
3c4797ba 4971 return 1;
d983da9c
DJ
4972 }
4973
4974 /* The target could report the data address. Mark watchpoints
4975 affected by this data address as triggered, and all others as not
4976 triggered. */
4977
4978 ALL_BREAKPOINTS (b)
cc60f2e3 4979 if (is_hardware_watchpoint (b))
d983da9c 4980 {
3a5c3e22 4981 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4982 struct bp_location *loc;
d983da9c 4983
3a5c3e22 4984 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4985 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4986 {
3a5c3e22 4987 if (is_masked_watchpoint (b))
9c06b0b4 4988 {
3a5c3e22
PA
4989 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4990 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4991
4992 if (newaddr == start)
4993 {
3a5c3e22 4994 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4995 break;
4996 }
4997 }
4998 /* Exact match not required. Within range is sufficient. */
4999 else if (target_watchpoint_addr_within_range (&current_target,
5000 addr, loc->address,
5001 loc->length))
5002 {
3a5c3e22 5003 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5004 break;
5005 }
5006 }
d983da9c
DJ
5007 }
5008
5009 return 1;
5010}
5011
c906108c
SS
5012/* Possible return values for watchpoint_check (this can't be an enum
5013 because of check_errors). */
5014/* The watchpoint has been deleted. */
5015#define WP_DELETED 1
5016/* The value has changed. */
5017#define WP_VALUE_CHANGED 2
5018/* The value has not changed. */
5019#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5020/* Ignore this watchpoint, no matter if the value changed or not. */
5021#define WP_IGNORE 4
c906108c
SS
5022
5023#define BP_TEMPFLAG 1
5024#define BP_HARDWAREFLAG 2
5025
4a64f543
MS
5026/* Evaluate watchpoint condition expression and check if its value
5027 changed.
553e4c11
JB
5028
5029 P should be a pointer to struct bpstat, but is defined as a void *
5030 in order for this function to be usable with catch_errors. */
c906108c
SS
5031
5032static int
4efb68b1 5033watchpoint_check (void *p)
c906108c
SS
5034{
5035 bpstat bs = (bpstat) p;
3a5c3e22 5036 struct watchpoint *b;
c906108c
SS
5037 struct frame_info *fr;
5038 int within_current_scope;
5039
f431efe5 5040 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5041 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5042 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5043
f6bc2008
PA
5044 /* If this is a local watchpoint, we only want to check if the
5045 watchpoint frame is in scope if the current thread is the thread
5046 that was used to create the watchpoint. */
5047 if (!watchpoint_in_thread_scope (b))
60e1c644 5048 return WP_IGNORE;
f6bc2008 5049
c906108c
SS
5050 if (b->exp_valid_block == NULL)
5051 within_current_scope = 1;
5052 else
5053 {
edb3359d
DJ
5054 struct frame_info *frame = get_current_frame ();
5055 struct gdbarch *frame_arch = get_frame_arch (frame);
5056 CORE_ADDR frame_pc = get_frame_pc (frame);
5057
c9cf6e20 5058 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5059 still in the function but the stack frame has already been
5060 invalidated. Since we can't rely on the values of local
5061 variables after the stack has been destroyed, we are treating
5062 the watchpoint in that state as `not changed' without further
5063 checking. Don't mark watchpoints as changed if the current
5064 frame is in an epilogue - even if they are in some other
5065 frame, our view of the stack is likely to be wrong and
5066 frame_find_by_id could error out. */
c9cf6e20 5067 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5068 return WP_IGNORE;
a0f49112 5069
101dcfbe 5070 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5071 within_current_scope = (fr != NULL);
69fbadd5
DJ
5072
5073 /* If we've gotten confused in the unwinder, we might have
5074 returned a frame that can't describe this variable. */
edb3359d
DJ
5075 if (within_current_scope)
5076 {
5077 struct symbol *function;
5078
5079 function = get_frame_function (fr);
5080 if (function == NULL
5081 || !contained_in (b->exp_valid_block,
5082 SYMBOL_BLOCK_VALUE (function)))
5083 within_current_scope = 0;
5084 }
69fbadd5 5085
edb3359d 5086 if (within_current_scope)
c906108c
SS
5087 /* If we end up stopping, the current frame will get selected
5088 in normal_stop. So this call to select_frame won't affect
5089 the user. */
0f7d239c 5090 select_frame (fr);
c906108c 5091 }
c5aa993b 5092
c906108c
SS
5093 if (within_current_scope)
5094 {
4a64f543
MS
5095 /* We use value_{,free_to_}mark because it could be a *long*
5096 time before we return to the command level and call
5097 free_all_values. We can't call free_all_values because we
5098 might be in the middle of evaluating a function call. */
c906108c 5099
0cf6dd15 5100 int pc = 0;
9c06b0b4 5101 struct value *mark;
fa4727a6
DJ
5102 struct value *new_val;
5103
3a5c3e22 5104 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5105 /* Since we don't know the exact trigger address (from
5106 stopped_data_address), just tell the user we've triggered
5107 a mask watchpoint. */
5108 return WP_VALUE_CHANGED;
5109
5110 mark = value_mark ();
3a1115a0 5111 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5112
bb9d5f81
PP
5113 if (b->val_bitsize != 0)
5114 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5115
4a64f543
MS
5116 /* We use value_equal_contents instead of value_equal because
5117 the latter coerces an array to a pointer, thus comparing just
5118 the address of the array instead of its contents. This is
5119 not what we want. */
fa4727a6 5120 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5121 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5122 {
fa4727a6
DJ
5123 if (new_val != NULL)
5124 {
5125 release_value (new_val);
5126 value_free_to_mark (mark);
5127 }
c906108c
SS
5128 bs->old_val = b->val;
5129 b->val = new_val;
fa4727a6 5130 b->val_valid = 1;
c906108c
SS
5131 return WP_VALUE_CHANGED;
5132 }
5133 else
5134 {
60e1c644 5135 /* Nothing changed. */
c906108c 5136 value_free_to_mark (mark);
c906108c
SS
5137 return WP_VALUE_NOT_CHANGED;
5138 }
5139 }
5140 else
5141 {
79a45e25
PA
5142 struct ui_out *uiout = current_uiout;
5143
c906108c 5144 /* This seems like the only logical thing to do because
c5aa993b
JM
5145 if we temporarily ignored the watchpoint, then when
5146 we reenter the block in which it is valid it contains
5147 garbage (in the case of a function, it may have two
5148 garbage values, one before and one after the prologue).
5149 So we can't even detect the first assignment to it and
5150 watch after that (since the garbage may or may not equal
5151 the first value assigned). */
348d480f
PA
5152 /* We print all the stop information in
5153 breakpoint_ops->print_it, but in this case, by the time we
5154 call breakpoint_ops->print_it this bp will be deleted
5155 already. So we have no choice but print the information
5156 here. */
9dc5e2a9 5157 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5158 ui_out_field_string
5159 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5160 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5161 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5162 ui_out_text (uiout,
5163 " deleted because the program has left the block in\n\
8b93c638 5164which its expression is valid.\n");
4ce44c66 5165
cdac0397 5166 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5167 decref_counted_command_line (&b->base.commands);
d0fb5eae 5168 watchpoint_del_at_next_stop (b);
c906108c
SS
5169
5170 return WP_DELETED;
5171 }
5172}
5173
18a18393 5174/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5175 breakpoint location BL. This function does not check if we should
5176 stop, only if BL explains the stop. */
5177
18a18393 5178static int
6c95b8df 5179bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5180 struct address_space *aspace, CORE_ADDR bp_addr,
5181 const struct target_waitstatus *ws)
18a18393
VP
5182{
5183 struct breakpoint *b = bl->owner;
5184
348d480f 5185 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5186 gdb_assert (b != NULL);
5187
09ac7c10 5188 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5189}
5190
3a5c3e22
PA
5191/* Determine if the watched values have actually changed, and we
5192 should stop. If not, set BS->stop to 0. */
5193
18a18393
VP
5194static void
5195bpstat_check_watchpoint (bpstat bs)
5196{
2bdf28a0 5197 const struct bp_location *bl;
3a5c3e22 5198 struct watchpoint *b;
2bdf28a0
JK
5199
5200 /* BS is built for existing struct breakpoint. */
f431efe5 5201 bl = bs->bp_location_at;
2bdf28a0 5202 gdb_assert (bl != NULL);
3a5c3e22 5203 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5204 gdb_assert (b != NULL);
18a18393 5205
18a18393 5206 {
18a18393
VP
5207 int must_check_value = 0;
5208
3a5c3e22 5209 if (b->base.type == bp_watchpoint)
18a18393
VP
5210 /* For a software watchpoint, we must always check the
5211 watched value. */
5212 must_check_value = 1;
5213 else if (b->watchpoint_triggered == watch_triggered_yes)
5214 /* We have a hardware watchpoint (read, write, or access)
5215 and the target earlier reported an address watched by
5216 this watchpoint. */
5217 must_check_value = 1;
5218 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5219 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5220 /* We were stopped by a hardware watchpoint, but the target could
5221 not report the data address. We must check the watchpoint's
5222 value. Access and read watchpoints are out of luck; without
5223 a data address, we can't figure it out. */
5224 must_check_value = 1;
3a5c3e22 5225
18a18393
VP
5226 if (must_check_value)
5227 {
3e43a32a
MS
5228 char *message
5229 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5230 b->base.number);
18a18393
VP
5231 struct cleanup *cleanups = make_cleanup (xfree, message);
5232 int e = catch_errors (watchpoint_check, bs, message,
5233 RETURN_MASK_ALL);
5234 do_cleanups (cleanups);
5235 switch (e)
5236 {
5237 case WP_DELETED:
5238 /* We've already printed what needs to be printed. */
5239 bs->print_it = print_it_done;
5240 /* Stop. */
5241 break;
60e1c644
PA
5242 case WP_IGNORE:
5243 bs->print_it = print_it_noop;
5244 bs->stop = 0;
5245 break;
18a18393 5246 case WP_VALUE_CHANGED:
3a5c3e22 5247 if (b->base.type == bp_read_watchpoint)
18a18393 5248 {
85d721b8
PA
5249 /* There are two cases to consider here:
5250
4a64f543 5251 1. We're watching the triggered memory for reads.
85d721b8
PA
5252 In that case, trust the target, and always report
5253 the watchpoint hit to the user. Even though
5254 reads don't cause value changes, the value may
5255 have changed since the last time it was read, and
5256 since we're not trapping writes, we will not see
5257 those, and as such we should ignore our notion of
5258 old value.
5259
4a64f543 5260 2. We're watching the triggered memory for both
85d721b8
PA
5261 reads and writes. There are two ways this may
5262 happen:
5263
4a64f543 5264 2.1. This is a target that can't break on data
85d721b8
PA
5265 reads only, but can break on accesses (reads or
5266 writes), such as e.g., x86. We detect this case
5267 at the time we try to insert read watchpoints.
5268
4a64f543 5269 2.2. Otherwise, the target supports read
85d721b8
PA
5270 watchpoints, but, the user set an access or write
5271 watchpoint watching the same memory as this read
5272 watchpoint.
5273
5274 If we're watching memory writes as well as reads,
5275 ignore watchpoint hits when we find that the
5276 value hasn't changed, as reads don't cause
5277 changes. This still gives false positives when
5278 the program writes the same value to memory as
5279 what there was already in memory (we will confuse
5280 it for a read), but it's much better than
5281 nothing. */
5282
5283 int other_write_watchpoint = 0;
5284
5285 if (bl->watchpoint_type == hw_read)
5286 {
5287 struct breakpoint *other_b;
5288
5289 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5290 if (other_b->type == bp_hardware_watchpoint
5291 || other_b->type == bp_access_watchpoint)
85d721b8 5292 {
3a5c3e22
PA
5293 struct watchpoint *other_w =
5294 (struct watchpoint *) other_b;
5295
5296 if (other_w->watchpoint_triggered
5297 == watch_triggered_yes)
5298 {
5299 other_write_watchpoint = 1;
5300 break;
5301 }
85d721b8
PA
5302 }
5303 }
5304
5305 if (other_write_watchpoint
5306 || bl->watchpoint_type == hw_access)
5307 {
5308 /* We're watching the same memory for writes,
5309 and the value changed since the last time we
5310 updated it, so this trap must be for a write.
5311 Ignore it. */
5312 bs->print_it = print_it_noop;
5313 bs->stop = 0;
5314 }
18a18393
VP
5315 }
5316 break;
5317 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5318 if (b->base.type == bp_hardware_watchpoint
5319 || b->base.type == bp_watchpoint)
18a18393
VP
5320 {
5321 /* Don't stop: write watchpoints shouldn't fire if
5322 the value hasn't changed. */
5323 bs->print_it = print_it_noop;
5324 bs->stop = 0;
5325 }
5326 /* Stop. */
5327 break;
5328 default:
5329 /* Can't happen. */
5330 case 0:
5331 /* Error from catch_errors. */
3a5c3e22 5332 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5333 watchpoint_del_at_next_stop (b);
18a18393
VP
5334 /* We've already printed what needs to be printed. */
5335 bs->print_it = print_it_done;
5336 break;
5337 }
5338 }
5339 else /* must_check_value == 0 */
5340 {
5341 /* This is a case where some watchpoint(s) triggered, but
5342 not at the address of this watchpoint, or else no
5343 watchpoint triggered after all. So don't print
5344 anything for this watchpoint. */
5345 bs->print_it = print_it_noop;
5346 bs->stop = 0;
5347 }
5348 }
5349}
5350
7d4df6a4
DE
5351/* For breakpoints that are currently marked as telling gdb to stop,
5352 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5353 of breakpoint referred to by BS. If we should not stop for this
5354 breakpoint, set BS->stop to 0. */
f431efe5 5355
18a18393
VP
5356static void
5357bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5358{
2bdf28a0
JK
5359 const struct bp_location *bl;
5360 struct breakpoint *b;
7d4df6a4
DE
5361 int value_is_zero = 0;
5362 struct expression *cond;
5363
5364 gdb_assert (bs->stop);
2bdf28a0
JK
5365
5366 /* BS is built for existing struct breakpoint. */
f431efe5 5367 bl = bs->bp_location_at;
2bdf28a0 5368 gdb_assert (bl != NULL);
f431efe5 5369 b = bs->breakpoint_at;
2bdf28a0 5370 gdb_assert (b != NULL);
18a18393 5371
b775012e
LM
5372 /* Even if the target evaluated the condition on its end and notified GDB, we
5373 need to do so again since GDB does not know if we stopped due to a
5374 breakpoint or a single step breakpoint. */
5375
18a18393 5376 if (frame_id_p (b->frame_id)
edb3359d 5377 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5378 {
7d4df6a4
DE
5379 bs->stop = 0;
5380 return;
5381 }
60e1c644 5382
12ab52e9
PA
5383 /* If this is a thread/task-specific breakpoint, don't waste cpu
5384 evaluating the condition if this isn't the specified
5385 thread/task. */
5386 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5387 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5388
6c1b0f7b
DE
5389 {
5390 bs->stop = 0;
5391 return;
5392 }
5393
6dddc817
DE
5394 /* Evaluate extension language breakpoints that have a "stop" method
5395 implemented. */
5396 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5397
7d4df6a4
DE
5398 if (is_watchpoint (b))
5399 {
5400 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5401
7d4df6a4
DE
5402 cond = w->cond_exp;
5403 }
5404 else
5405 cond = bl->cond;
60e1c644 5406
7d4df6a4
DE
5407 if (cond && b->disposition != disp_del_at_next_stop)
5408 {
5409 int within_current_scope = 1;
5410 struct watchpoint * w;
60e1c644 5411
7d4df6a4
DE
5412 /* We use value_mark and value_free_to_mark because it could
5413 be a long time before we return to the command level and
5414 call free_all_values. We can't call free_all_values
5415 because we might be in the middle of evaluating a
5416 function call. */
5417 struct value *mark = value_mark ();
5418
5419 if (is_watchpoint (b))
5420 w = (struct watchpoint *) b;
5421 else
5422 w = NULL;
5423
5424 /* Need to select the frame, with all that implies so that
5425 the conditions will have the right context. Because we
5426 use the frame, we will not see an inlined function's
5427 variables when we arrive at a breakpoint at the start
5428 of the inlined function; the current frame will be the
5429 call site. */
5430 if (w == NULL || w->cond_exp_valid_block == NULL)
5431 select_frame (get_current_frame ());
5432 else
18a18393 5433 {
7d4df6a4
DE
5434 struct frame_info *frame;
5435
5436 /* For local watchpoint expressions, which particular
5437 instance of a local is being watched matters, so we
5438 keep track of the frame to evaluate the expression
5439 in. To evaluate the condition however, it doesn't
5440 really matter which instantiation of the function
5441 where the condition makes sense triggers the
5442 watchpoint. This allows an expression like "watch
5443 global if q > 10" set in `func', catch writes to
5444 global on all threads that call `func', or catch
5445 writes on all recursive calls of `func' by a single
5446 thread. We simply always evaluate the condition in
5447 the innermost frame that's executing where it makes
5448 sense to evaluate the condition. It seems
5449 intuitive. */
5450 frame = block_innermost_frame (w->cond_exp_valid_block);
5451 if (frame != NULL)
5452 select_frame (frame);
5453 else
5454 within_current_scope = 0;
18a18393 5455 }
7d4df6a4
DE
5456 if (within_current_scope)
5457 value_is_zero
5458 = catch_errors (breakpoint_cond_eval, cond,
5459 "Error in testing breakpoint condition:\n",
5460 RETURN_MASK_ALL);
5461 else
18a18393 5462 {
7d4df6a4
DE
5463 warning (_("Watchpoint condition cannot be tested "
5464 "in the current scope"));
5465 /* If we failed to set the right context for this
5466 watchpoint, unconditionally report it. */
5467 value_is_zero = 0;
18a18393 5468 }
7d4df6a4
DE
5469 /* FIXME-someday, should give breakpoint #. */
5470 value_free_to_mark (mark);
18a18393 5471 }
7d4df6a4
DE
5472
5473 if (cond && value_is_zero)
5474 {
5475 bs->stop = 0;
5476 }
7d4df6a4
DE
5477 else if (b->ignore_count > 0)
5478 {
5479 b->ignore_count--;
5480 bs->stop = 0;
5481 /* Increase the hit count even though we don't stop. */
5482 ++(b->hit_count);
5483 observer_notify_breakpoint_modified (b);
5484 }
18a18393
VP
5485}
5486
1cf4d951
PA
5487/* Returns true if we need to track moribund locations of LOC's type
5488 on the current target. */
5489
5490static int
5491need_moribund_for_location_type (struct bp_location *loc)
5492{
5493 return ((loc->loc_type == bp_loc_software_breakpoint
5494 && !target_supports_stopped_by_sw_breakpoint ())
5495 || (loc->loc_type == bp_loc_hardware_breakpoint
5496 && !target_supports_stopped_by_hw_breakpoint ()));
5497}
5498
18a18393 5499
9709f61c 5500/* Get a bpstat associated with having just stopped at address
d983da9c 5501 BP_ADDR in thread PTID.
c906108c 5502
d983da9c 5503 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5504 don't understand this stop. Result is a chain of bpstat's such
5505 that:
c906108c 5506
c5aa993b 5507 if we don't understand the stop, the result is a null pointer.
c906108c 5508
c5aa993b 5509 if we understand why we stopped, the result is not null.
c906108c 5510
c5aa993b
JM
5511 Each element of the chain refers to a particular breakpoint or
5512 watchpoint at which we have stopped. (We may have stopped for
5513 several reasons concurrently.)
c906108c 5514
c5aa993b
JM
5515 Each element of the chain has valid next, breakpoint_at,
5516 commands, FIXME??? fields. */
c906108c
SS
5517
5518bpstat
6c95b8df 5519bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5520 CORE_ADDR bp_addr, ptid_t ptid,
5521 const struct target_waitstatus *ws)
c906108c 5522{
0d381245 5523 struct breakpoint *b = NULL;
afe38095 5524 struct bp_location *bl;
20874c92 5525 struct bp_location *loc;
5760d0ab
JK
5526 /* First item of allocated bpstat's. */
5527 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5528 /* Pointer to the last thing in the chain currently. */
5760d0ab 5529 bpstat bs;
20874c92 5530 int ix;
429374b8 5531 int need_remove_insert;
f431efe5 5532 int removed_any;
c906108c 5533
f431efe5
PA
5534 /* First, build the bpstat chain with locations that explain a
5535 target stop, while being careful to not set the target running,
5536 as that may invalidate locations (in particular watchpoint
5537 locations are recreated). Resuming will happen here with
5538 breakpoint conditions or watchpoint expressions that include
5539 inferior function calls. */
c5aa993b 5540
429374b8
JK
5541 ALL_BREAKPOINTS (b)
5542 {
1a853c52 5543 if (!breakpoint_enabled (b))
429374b8 5544 continue;
a5606eee 5545
429374b8
JK
5546 for (bl = b->loc; bl != NULL; bl = bl->next)
5547 {
4a64f543
MS
5548 /* For hardware watchpoints, we look only at the first
5549 location. The watchpoint_check function will work on the
5550 entire expression, not the individual locations. For
5551 read watchpoints, the watchpoints_triggered function has
5552 checked all locations already. */
429374b8
JK
5553 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5554 break;
18a18393 5555
f6592439 5556 if (!bl->enabled || bl->shlib_disabled)
429374b8 5557 continue;
c5aa993b 5558
09ac7c10 5559 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5560 continue;
c5aa993b 5561
4a64f543
MS
5562 /* Come here if it's a watchpoint, or if the break address
5563 matches. */
c5aa993b 5564
4a64f543
MS
5565 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5566 explain stop. */
c5aa993b 5567
f431efe5
PA
5568 /* Assume we stop. Should we find a watchpoint that is not
5569 actually triggered, or if the condition of the breakpoint
5570 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5571 bs->stop = 1;
5572 bs->print = 1;
d983da9c 5573
f431efe5
PA
5574 /* If this is a scope breakpoint, mark the associated
5575 watchpoint as triggered so that we will handle the
5576 out-of-scope event. We'll get to the watchpoint next
5577 iteration. */
d0fb5eae 5578 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5579 {
5580 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5581
5582 w->watchpoint_triggered = watch_triggered_yes;
5583 }
f431efe5
PA
5584 }
5585 }
5586
7c16b83e 5587 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5588 if (!target_supports_stopped_by_sw_breakpoint ()
5589 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5590 {
1cf4d951 5591 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5592 {
1cf4d951
PA
5593 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5594 && need_moribund_for_location_type (loc))
5595 {
5596 bs = bpstat_alloc (loc, &bs_link);
5597 /* For hits of moribund locations, we should just proceed. */
5598 bs->stop = 0;
5599 bs->print = 0;
5600 bs->print_it = print_it_noop;
5601 }
f431efe5
PA
5602 }
5603 }
5604
edcc5120
TT
5605 /* A bit of special processing for shlib breakpoints. We need to
5606 process solib loading here, so that the lists of loaded and
5607 unloaded libraries are correct before we handle "catch load" and
5608 "catch unload". */
5609 for (bs = bs_head; bs != NULL; bs = bs->next)
5610 {
5d268276 5611 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5612 {
5613 handle_solib_event ();
5614 break;
5615 }
5616 }
5617
f431efe5
PA
5618 /* Now go through the locations that caused the target to stop, and
5619 check whether we're interested in reporting this stop to higher
5620 layers, or whether we should resume the target transparently. */
5621
5622 removed_any = 0;
5623
5760d0ab 5624 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5625 {
5626 if (!bs->stop)
5627 continue;
5628
f431efe5 5629 b = bs->breakpoint_at;
348d480f
PA
5630 b->ops->check_status (bs);
5631 if (bs->stop)
28010a5d 5632 {
348d480f 5633 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5634
429374b8
JK
5635 if (bs->stop)
5636 {
5637 ++(b->hit_count);
8d3788bd 5638 observer_notify_breakpoint_modified (b);
c906108c 5639
4a64f543 5640 /* We will stop here. */
429374b8
JK
5641 if (b->disposition == disp_disable)
5642 {
816338b5 5643 --(b->enable_count);
1a853c52 5644 if (b->enable_count <= 0)
429374b8 5645 b->enable_state = bp_disabled;
f431efe5 5646 removed_any = 1;
429374b8
JK
5647 }
5648 if (b->silent)
5649 bs->print = 0;
5650 bs->commands = b->commands;
9add0f1b 5651 incref_counted_command_line (bs->commands);
abf85f46
JK
5652 if (command_line_is_silent (bs->commands
5653 ? bs->commands->commands : NULL))
5654 bs->print = 0;
9d6e6e84
HZ
5655
5656 b->ops->after_condition_true (bs);
429374b8
JK
5657 }
5658
348d480f 5659 }
a9b3a50f
PA
5660
5661 /* Print nothing for this entry if we don't stop or don't
5662 print. */
5663 if (!bs->stop || !bs->print)
5664 bs->print_it = print_it_noop;
429374b8 5665 }
876fa593 5666
d983da9c
DJ
5667 /* If we aren't stopping, the value of some hardware watchpoint may
5668 not have changed, but the intermediate memory locations we are
5669 watching may have. Don't bother if we're stopping; this will get
5670 done later. */
d832cb68 5671 need_remove_insert = 0;
5760d0ab
JK
5672 if (! bpstat_causes_stop (bs_head))
5673 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5674 if (!bs->stop
f431efe5
PA
5675 && bs->breakpoint_at
5676 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5677 {
3a5c3e22
PA
5678 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5679
5680 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5681 need_remove_insert = 1;
d983da9c
DJ
5682 }
5683
d832cb68 5684 if (need_remove_insert)
44702360 5685 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5686 else if (removed_any)
44702360 5687 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5688
5760d0ab 5689 return bs_head;
c906108c 5690}
628fe4e4
JK
5691
5692static void
5693handle_jit_event (void)
5694{
5695 struct frame_info *frame;
5696 struct gdbarch *gdbarch;
5697
243a9253
PA
5698 if (debug_infrun)
5699 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5700
628fe4e4
JK
5701 /* Switch terminal for any messages produced by
5702 breakpoint_re_set. */
5703 target_terminal_ours_for_output ();
5704
5705 frame = get_current_frame ();
5706 gdbarch = get_frame_arch (frame);
5707
5708 jit_event_handler (gdbarch);
5709
5710 target_terminal_inferior ();
5711}
5712
5713/* Prepare WHAT final decision for infrun. */
5714
5715/* Decide what infrun needs to do with this bpstat. */
5716
c906108c 5717struct bpstat_what
0e30163f 5718bpstat_what (bpstat bs_head)
c906108c 5719{
c906108c 5720 struct bpstat_what retval;
628fe4e4 5721 int jit_event = 0;
0e30163f 5722 bpstat bs;
c906108c 5723
628fe4e4 5724 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5725 retval.call_dummy = STOP_NONE;
186c406b 5726 retval.is_longjmp = 0;
628fe4e4 5727
0e30163f 5728 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5729 {
628fe4e4
JK
5730 /* Extract this BS's action. After processing each BS, we check
5731 if its action overrides all we've seem so far. */
5732 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5733 enum bptype bptype;
5734
c906108c 5735 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5736 {
5737 /* I suspect this can happen if it was a momentary
5738 breakpoint which has since been deleted. */
5739 bptype = bp_none;
5740 }
20874c92 5741 else
f431efe5 5742 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5743
5744 switch (bptype)
c906108c
SS
5745 {
5746 case bp_none:
628fe4e4 5747 break;
c906108c
SS
5748 case bp_breakpoint:
5749 case bp_hardware_breakpoint:
7c16b83e 5750 case bp_single_step:
c906108c
SS
5751 case bp_until:
5752 case bp_finish:
a9b3a50f 5753 case bp_shlib_event:
c906108c
SS
5754 if (bs->stop)
5755 {
5756 if (bs->print)
628fe4e4 5757 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5758 else
628fe4e4 5759 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5760 }
5761 else
628fe4e4 5762 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5763 break;
5764 case bp_watchpoint:
5765 case bp_hardware_watchpoint:
5766 case bp_read_watchpoint:
5767 case bp_access_watchpoint:
5768 if (bs->stop)
5769 {
5770 if (bs->print)
628fe4e4 5771 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5772 else
628fe4e4 5773 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5774 }
5775 else
628fe4e4
JK
5776 {
5777 /* There was a watchpoint, but we're not stopping.
5778 This requires no further action. */
5779 }
c906108c
SS
5780 break;
5781 case bp_longjmp:
e2e4d78b 5782 case bp_longjmp_call_dummy:
186c406b 5783 case bp_exception:
0a39bb32
PA
5784 if (bs->stop)
5785 {
5786 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5787 retval.is_longjmp = bptype != bp_exception;
5788 }
5789 else
5790 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5791 break;
5792 case bp_longjmp_resume:
186c406b 5793 case bp_exception_resume:
0a39bb32
PA
5794 if (bs->stop)
5795 {
5796 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5797 retval.is_longjmp = bptype == bp_longjmp_resume;
5798 }
5799 else
5800 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5801 break;
5802 case bp_step_resume:
5803 if (bs->stop)
628fe4e4
JK
5804 this_action = BPSTAT_WHAT_STEP_RESUME;
5805 else
c906108c 5806 {
628fe4e4
JK
5807 /* It is for the wrong frame. */
5808 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5809 }
c906108c 5810 break;
2c03e5be
PA
5811 case bp_hp_step_resume:
5812 if (bs->stop)
5813 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5814 else
5815 {
5816 /* It is for the wrong frame. */
5817 this_action = BPSTAT_WHAT_SINGLE;
5818 }
5819 break;
c906108c 5820 case bp_watchpoint_scope:
c4093a6a 5821 case bp_thread_event:
1900040c 5822 case bp_overlay_event:
0fd8e87f 5823 case bp_longjmp_master:
aa7d318d 5824 case bp_std_terminate_master:
186c406b 5825 case bp_exception_master:
628fe4e4 5826 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5827 break;
ce78b96d 5828 case bp_catchpoint:
c5aa993b
JM
5829 if (bs->stop)
5830 {
5831 if (bs->print)
628fe4e4 5832 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5833 else
628fe4e4 5834 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5835 }
5836 else
628fe4e4
JK
5837 {
5838 /* There was a catchpoint, but we're not stopping.
5839 This requires no further action. */
5840 }
5841 break;
628fe4e4
JK
5842 case bp_jit_event:
5843 jit_event = 1;
5844 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5845 break;
c906108c 5846 case bp_call_dummy:
53a5351d
JM
5847 /* Make sure the action is stop (silent or noisy),
5848 so infrun.c pops the dummy frame. */
aa7d318d 5849 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5850 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5851 break;
5852 case bp_std_terminate:
5853 /* Make sure the action is stop (silent or noisy),
5854 so infrun.c pops the dummy frame. */
aa7d318d 5855 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5856 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5857 break;
1042e4c0 5858 case bp_tracepoint:
7a697b8d 5859 case bp_fast_tracepoint:
0fb4aa4b 5860 case bp_static_tracepoint:
1042e4c0
SS
5861 /* Tracepoint hits should not be reported back to GDB, and
5862 if one got through somehow, it should have been filtered
5863 out already. */
5864 internal_error (__FILE__, __LINE__,
7a697b8d 5865 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5866 break;
5867 case bp_gnu_ifunc_resolver:
5868 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5869 this_action = BPSTAT_WHAT_SINGLE;
5870 break;
5871 case bp_gnu_ifunc_resolver_return:
5872 /* The breakpoint will be removed, execution will restart from the
5873 PC of the former breakpoint. */
5874 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5875 break;
e7e0cddf
SS
5876
5877 case bp_dprintf:
a11cfd87
HZ
5878 if (bs->stop)
5879 this_action = BPSTAT_WHAT_STOP_SILENT;
5880 else
5881 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5882 break;
5883
628fe4e4
JK
5884 default:
5885 internal_error (__FILE__, __LINE__,
5886 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5887 }
628fe4e4
JK
5888
5889 retval.main_action = max (retval.main_action, this_action);
c906108c 5890 }
628fe4e4 5891
243a9253
PA
5892 return retval;
5893}
628fe4e4 5894
243a9253
PA
5895void
5896bpstat_run_callbacks (bpstat bs_head)
5897{
5898 bpstat bs;
628fe4e4 5899
0e30163f
JK
5900 for (bs = bs_head; bs != NULL; bs = bs->next)
5901 {
5902 struct breakpoint *b = bs->breakpoint_at;
5903
5904 if (b == NULL)
5905 continue;
5906 switch (b->type)
5907 {
243a9253
PA
5908 case bp_jit_event:
5909 handle_jit_event ();
5910 break;
0e30163f
JK
5911 case bp_gnu_ifunc_resolver:
5912 gnu_ifunc_resolver_stop (b);
5913 break;
5914 case bp_gnu_ifunc_resolver_return:
5915 gnu_ifunc_resolver_return_stop (b);
5916 break;
5917 }
5918 }
c906108c
SS
5919}
5920
5921/* Nonzero if we should step constantly (e.g. watchpoints on machines
5922 without hardware support). This isn't related to a specific bpstat,
5923 just to things like whether watchpoints are set. */
5924
c5aa993b 5925int
fba45db2 5926bpstat_should_step (void)
c906108c
SS
5927{
5928 struct breakpoint *b;
cc59ec59 5929
c906108c 5930 ALL_BREAKPOINTS (b)
717a8278 5931 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5932 return 1;
c906108c
SS
5933 return 0;
5934}
5935
67822962
PA
5936int
5937bpstat_causes_stop (bpstat bs)
5938{
5939 for (; bs != NULL; bs = bs->next)
5940 if (bs->stop)
5941 return 1;
5942
5943 return 0;
5944}
5945
c906108c 5946\f
c5aa993b 5947
170b53b2
UW
5948/* Compute a string of spaces suitable to indent the next line
5949 so it starts at the position corresponding to the table column
5950 named COL_NAME in the currently active table of UIOUT. */
5951
5952static char *
5953wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5954{
5955 static char wrap_indent[80];
5956 int i, total_width, width, align;
5957 char *text;
5958
5959 total_width = 0;
5960 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5961 {
5962 if (strcmp (text, col_name) == 0)
5963 {
5964 gdb_assert (total_width < sizeof wrap_indent);
5965 memset (wrap_indent, ' ', total_width);
5966 wrap_indent[total_width] = 0;
5967
5968 return wrap_indent;
5969 }
5970
5971 total_width += width + 1;
5972 }
5973
5974 return NULL;
5975}
5976
b775012e
LM
5977/* Determine if the locations of this breakpoint will have their conditions
5978 evaluated by the target, host or a mix of both. Returns the following:
5979
5980 "host": Host evals condition.
5981 "host or target": Host or Target evals condition.
5982 "target": Target evals condition.
5983*/
5984
5985static const char *
5986bp_condition_evaluator (struct breakpoint *b)
5987{
5988 struct bp_location *bl;
5989 char host_evals = 0;
5990 char target_evals = 0;
5991
5992 if (!b)
5993 return NULL;
5994
5995 if (!is_breakpoint (b))
5996 return NULL;
5997
5998 if (gdb_evaluates_breakpoint_condition_p ()
5999 || !target_supports_evaluation_of_breakpoint_conditions ())
6000 return condition_evaluation_host;
6001
6002 for (bl = b->loc; bl; bl = bl->next)
6003 {
6004 if (bl->cond_bytecode)
6005 target_evals++;
6006 else
6007 host_evals++;
6008 }
6009
6010 if (host_evals && target_evals)
6011 return condition_evaluation_both;
6012 else if (target_evals)
6013 return condition_evaluation_target;
6014 else
6015 return condition_evaluation_host;
6016}
6017
6018/* Determine the breakpoint location's condition evaluator. This is
6019 similar to bp_condition_evaluator, but for locations. */
6020
6021static const char *
6022bp_location_condition_evaluator (struct bp_location *bl)
6023{
6024 if (bl && !is_breakpoint (bl->owner))
6025 return NULL;
6026
6027 if (gdb_evaluates_breakpoint_condition_p ()
6028 || !target_supports_evaluation_of_breakpoint_conditions ())
6029 return condition_evaluation_host;
6030
6031 if (bl && bl->cond_bytecode)
6032 return condition_evaluation_target;
6033 else
6034 return condition_evaluation_host;
6035}
6036
859825b8
JK
6037/* Print the LOC location out of the list of B->LOC locations. */
6038
170b53b2
UW
6039static void
6040print_breakpoint_location (struct breakpoint *b,
6041 struct bp_location *loc)
0d381245 6042{
79a45e25 6043 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6044 struct cleanup *old_chain = save_current_program_space ();
6045
859825b8
JK
6046 if (loc != NULL && loc->shlib_disabled)
6047 loc = NULL;
6048
6c95b8df
PA
6049 if (loc != NULL)
6050 set_current_program_space (loc->pspace);
6051
56435ebe 6052 if (b->display_canonical)
f00aae0f
KS
6053 ui_out_field_string (uiout, "what",
6054 event_location_to_string (b->location));
2f202fde 6055 else if (loc && loc->symtab)
0d381245
VP
6056 {
6057 struct symbol *sym
6058 = find_pc_sect_function (loc->address, loc->section);
6059 if (sym)
6060 {
6061 ui_out_text (uiout, "in ");
6062 ui_out_field_string (uiout, "func",
6063 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6064 ui_out_text (uiout, " ");
6065 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6066 ui_out_text (uiout, "at ");
0d381245 6067 }
05cba821
JK
6068 ui_out_field_string (uiout, "file",
6069 symtab_to_filename_for_display (loc->symtab));
0d381245 6070 ui_out_text (uiout, ":");
05cba821 6071
0d381245 6072 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6073 ui_out_field_string (uiout, "fullname",
6074 symtab_to_fullname (loc->symtab));
0d381245 6075
f8eba3c6 6076 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6077 }
859825b8 6078 else if (loc)
0d381245 6079 {
f99d8bf4
PA
6080 struct ui_file *stb = mem_fileopen ();
6081 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6082
f99d8bf4 6083 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6084 demangle, "");
0d381245 6085 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6086
6087 do_cleanups (stb_chain);
0d381245 6088 }
859825b8 6089 else
f00aae0f
KS
6090 {
6091 ui_out_field_string (uiout, "pending",
6092 event_location_to_string (b->location));
6093 /* If extra_string is available, it could be holding a condition
6094 or dprintf arguments. In either case, make sure it is printed,
6095 too, but only for non-MI streams. */
6096 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6097 {
6098 if (b->type == bp_dprintf)
6099 ui_out_text (uiout, ",");
6100 else
6101 ui_out_text (uiout, " ");
6102 ui_out_text (uiout, b->extra_string);
6103 }
6104 }
6c95b8df 6105
b775012e
LM
6106 if (loc && is_breakpoint (b)
6107 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6108 && bp_condition_evaluator (b) == condition_evaluation_both)
6109 {
6110 ui_out_text (uiout, " (");
6111 ui_out_field_string (uiout, "evaluated-by",
6112 bp_location_condition_evaluator (loc));
6113 ui_out_text (uiout, ")");
6114 }
6115
6c95b8df 6116 do_cleanups (old_chain);
0d381245
VP
6117}
6118
269b11a2
PA
6119static const char *
6120bptype_string (enum bptype type)
c906108c 6121{
c4093a6a
JM
6122 struct ep_type_description
6123 {
6124 enum bptype type;
6125 char *description;
6126 };
6127 static struct ep_type_description bptypes[] =
c906108c 6128 {
c5aa993b
JM
6129 {bp_none, "?deleted?"},
6130 {bp_breakpoint, "breakpoint"},
c906108c 6131 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6132 {bp_single_step, "sw single-step"},
c5aa993b
JM
6133 {bp_until, "until"},
6134 {bp_finish, "finish"},
6135 {bp_watchpoint, "watchpoint"},
c906108c 6136 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6137 {bp_read_watchpoint, "read watchpoint"},
6138 {bp_access_watchpoint, "acc watchpoint"},
6139 {bp_longjmp, "longjmp"},
6140 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6141 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6142 {bp_exception, "exception"},
6143 {bp_exception_resume, "exception resume"},
c5aa993b 6144 {bp_step_resume, "step resume"},
2c03e5be 6145 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6146 {bp_watchpoint_scope, "watchpoint scope"},
6147 {bp_call_dummy, "call dummy"},
aa7d318d 6148 {bp_std_terminate, "std::terminate"},
c5aa993b 6149 {bp_shlib_event, "shlib events"},
c4093a6a 6150 {bp_thread_event, "thread events"},
1900040c 6151 {bp_overlay_event, "overlay events"},
0fd8e87f 6152 {bp_longjmp_master, "longjmp master"},
aa7d318d 6153 {bp_std_terminate_master, "std::terminate master"},
186c406b 6154 {bp_exception_master, "exception master"},
ce78b96d 6155 {bp_catchpoint, "catchpoint"},
1042e4c0 6156 {bp_tracepoint, "tracepoint"},
7a697b8d 6157 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6158 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6159 {bp_dprintf, "dprintf"},
4efc6507 6160 {bp_jit_event, "jit events"},
0e30163f
JK
6161 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6162 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6163 };
269b11a2
PA
6164
6165 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6166 || ((int) type != bptypes[(int) type].type))
6167 internal_error (__FILE__, __LINE__,
6168 _("bptypes table does not describe type #%d."),
6169 (int) type);
6170
6171 return bptypes[(int) type].description;
6172}
6173
998580f1
MK
6174/* For MI, output a field named 'thread-groups' with a list as the value.
6175 For CLI, prefix the list with the string 'inf'. */
6176
6177static void
6178output_thread_groups (struct ui_out *uiout,
6179 const char *field_name,
6180 VEC(int) *inf_num,
6181 int mi_only)
6182{
752eb8b4 6183 struct cleanup *back_to;
998580f1
MK
6184 int is_mi = ui_out_is_mi_like_p (uiout);
6185 int inf;
6186 int i;
6187
6188 /* For backward compatibility, don't display inferiors in CLI unless
6189 there are several. Always display them for MI. */
6190 if (!is_mi && mi_only)
6191 return;
6192
752eb8b4
TT
6193 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6194
998580f1
MK
6195 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6196 {
6197 if (is_mi)
6198 {
6199 char mi_group[10];
6200
6201 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6202 ui_out_field_string (uiout, NULL, mi_group);
6203 }
6204 else
6205 {
6206 if (i == 0)
6207 ui_out_text (uiout, " inf ");
6208 else
6209 ui_out_text (uiout, ", ");
6210
6211 ui_out_text (uiout, plongest (inf));
6212 }
6213 }
6214
6215 do_cleanups (back_to);
6216}
6217
269b11a2
PA
6218/* Print B to gdb_stdout. */
6219
6220static void
6221print_one_breakpoint_location (struct breakpoint *b,
6222 struct bp_location *loc,
6223 int loc_number,
6224 struct bp_location **last_loc,
269b11a2
PA
6225 int allflag)
6226{
6227 struct command_line *l;
c2c6d25f 6228 static char bpenables[] = "nynny";
c906108c 6229
79a45e25 6230 struct ui_out *uiout = current_uiout;
0d381245
VP
6231 int header_of_multiple = 0;
6232 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6233 struct value_print_options opts;
6234
6235 get_user_print_options (&opts);
0d381245
VP
6236
6237 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6238 /* See comment in print_one_breakpoint concerning treatment of
6239 breakpoints with single disabled location. */
0d381245
VP
6240 if (loc == NULL
6241 && (b->loc != NULL
6242 && (b->loc->next != NULL || !b->loc->enabled)))
6243 header_of_multiple = 1;
6244 if (loc == NULL)
6245 loc = b->loc;
6246
c4093a6a
JM
6247 annotate_record ();
6248
6249 /* 1 */
6250 annotate_field (0);
0d381245
VP
6251 if (part_of_multiple)
6252 {
6253 char *formatted;
0c6773c1 6254 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6255 ui_out_field_string (uiout, "number", formatted);
6256 xfree (formatted);
6257 }
6258 else
6259 {
6260 ui_out_field_int (uiout, "number", b->number);
6261 }
c4093a6a
JM
6262
6263 /* 2 */
6264 annotate_field (1);
0d381245
VP
6265 if (part_of_multiple)
6266 ui_out_field_skip (uiout, "type");
269b11a2
PA
6267 else
6268 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6269
6270 /* 3 */
6271 annotate_field (2);
0d381245
VP
6272 if (part_of_multiple)
6273 ui_out_field_skip (uiout, "disp");
6274 else
2cec12e5 6275 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6276
c4093a6a
JM
6277
6278 /* 4 */
6279 annotate_field (3);
0d381245 6280 if (part_of_multiple)
54e52265 6281 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6282 else
4a64f543
MS
6283 ui_out_field_fmt (uiout, "enabled", "%c",
6284 bpenables[(int) b->enable_state]);
54e52265 6285 ui_out_spaces (uiout, 2);
0d381245 6286
c4093a6a
JM
6287
6288 /* 5 and 6 */
3086aeae 6289 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6290 {
4a64f543
MS
6291 /* Although the print_one can possibly print all locations,
6292 calling it here is not likely to get any nice result. So,
6293 make sure there's just one location. */
0d381245 6294 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6295 b->ops->print_one (b, last_loc);
0d381245 6296 }
3086aeae
DJ
6297 else
6298 switch (b->type)
6299 {
6300 case bp_none:
6301 internal_error (__FILE__, __LINE__,
e2e0b3e5 6302 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6303 break;
c906108c 6304
3086aeae
DJ
6305 case bp_watchpoint:
6306 case bp_hardware_watchpoint:
6307 case bp_read_watchpoint:
6308 case bp_access_watchpoint:
3a5c3e22
PA
6309 {
6310 struct watchpoint *w = (struct watchpoint *) b;
6311
6312 /* Field 4, the address, is omitted (which makes the columns
6313 not line up too nicely with the headers, but the effect
6314 is relatively readable). */
6315 if (opts.addressprint)
6316 ui_out_field_skip (uiout, "addr");
6317 annotate_field (5);
6318 ui_out_field_string (uiout, "what", w->exp_string);
6319 }
3086aeae
DJ
6320 break;
6321
3086aeae
DJ
6322 case bp_breakpoint:
6323 case bp_hardware_breakpoint:
7c16b83e 6324 case bp_single_step:
3086aeae
DJ
6325 case bp_until:
6326 case bp_finish:
6327 case bp_longjmp:
6328 case bp_longjmp_resume:
e2e4d78b 6329 case bp_longjmp_call_dummy:
186c406b
TT
6330 case bp_exception:
6331 case bp_exception_resume:
3086aeae 6332 case bp_step_resume:
2c03e5be 6333 case bp_hp_step_resume:
3086aeae
DJ
6334 case bp_watchpoint_scope:
6335 case bp_call_dummy:
aa7d318d 6336 case bp_std_terminate:
3086aeae
DJ
6337 case bp_shlib_event:
6338 case bp_thread_event:
6339 case bp_overlay_event:
0fd8e87f 6340 case bp_longjmp_master:
aa7d318d 6341 case bp_std_terminate_master:
186c406b 6342 case bp_exception_master:
1042e4c0 6343 case bp_tracepoint:
7a697b8d 6344 case bp_fast_tracepoint:
0fb4aa4b 6345 case bp_static_tracepoint:
e7e0cddf 6346 case bp_dprintf:
4efc6507 6347 case bp_jit_event:
0e30163f
JK
6348 case bp_gnu_ifunc_resolver:
6349 case bp_gnu_ifunc_resolver_return:
79a45b7d 6350 if (opts.addressprint)
3086aeae
DJ
6351 {
6352 annotate_field (4);
54e52265 6353 if (header_of_multiple)
0d381245 6354 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6355 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6356 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6357 else
5af949e3
UW
6358 ui_out_field_core_addr (uiout, "addr",
6359 loc->gdbarch, loc->address);
3086aeae
DJ
6360 }
6361 annotate_field (5);
0d381245 6362 if (!header_of_multiple)
170b53b2 6363 print_breakpoint_location (b, loc);
0d381245 6364 if (b->loc)
a6d9a66e 6365 *last_loc = b->loc;
3086aeae
DJ
6366 break;
6367 }
c906108c 6368
6c95b8df 6369
998580f1 6370 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6371 {
6372 struct inferior *inf;
998580f1
MK
6373 VEC(int) *inf_num = NULL;
6374 int mi_only = 1;
6c95b8df 6375
998580f1 6376 ALL_INFERIORS (inf)
6c95b8df
PA
6377 {
6378 if (inf->pspace == loc->pspace)
998580f1 6379 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6380 }
998580f1
MK
6381
6382 /* For backward compatibility, don't display inferiors in CLI unless
6383 there are several. Always display for MI. */
6384 if (allflag
6385 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6386 && (number_of_program_spaces () > 1
6387 || number_of_inferiors () > 1)
6388 /* LOC is for existing B, it cannot be in
6389 moribund_locations and thus having NULL OWNER. */
6390 && loc->owner->type != bp_catchpoint))
6391 mi_only = 0;
6392 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6393 VEC_free (int, inf_num);
6c95b8df
PA
6394 }
6395
4a306c9a 6396 if (!part_of_multiple)
c4093a6a 6397 {
4a306c9a
JB
6398 if (b->thread != -1)
6399 {
6400 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6401 "stop only in" line a little further down. */
4a306c9a
JB
6402 ui_out_text (uiout, " thread ");
6403 ui_out_field_int (uiout, "thread", b->thread);
6404 }
6405 else if (b->task != 0)
6406 {
6407 ui_out_text (uiout, " task ");
6408 ui_out_field_int (uiout, "task", b->task);
6409 }
c4093a6a 6410 }
f1310107 6411
8b93c638 6412 ui_out_text (uiout, "\n");
f1310107 6413
348d480f 6414 if (!part_of_multiple)
f1310107
TJB
6415 b->ops->print_one_detail (b, uiout);
6416
0d381245 6417 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6418 {
6419 annotate_field (6);
8b93c638 6420 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6421 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6422 the frame ID. */
5af949e3
UW
6423 ui_out_field_core_addr (uiout, "frame",
6424 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6425 ui_out_text (uiout, "\n");
c4093a6a
JM
6426 }
6427
28010a5d 6428 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6429 {
6430 annotate_field (7);
d77f58be 6431 if (is_tracepoint (b))
1042e4c0
SS
6432 ui_out_text (uiout, "\ttrace only if ");
6433 else
6434 ui_out_text (uiout, "\tstop only if ");
0101ce28 6435 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6436
6437 /* Print whether the target is doing the breakpoint's condition
6438 evaluation. If GDB is doing the evaluation, don't print anything. */
6439 if (is_breakpoint (b)
6440 && breakpoint_condition_evaluation_mode ()
6441 == condition_evaluation_target)
6442 {
6443 ui_out_text (uiout, " (");
6444 ui_out_field_string (uiout, "evaluated-by",
6445 bp_condition_evaluator (b));
6446 ui_out_text (uiout, " evals)");
6447 }
0101ce28
JJ
6448 ui_out_text (uiout, "\n");
6449 }
6450
0d381245 6451 if (!part_of_multiple && b->thread != -1)
c4093a6a 6452 {
4a64f543 6453 /* FIXME should make an annotation for this. */
8b93c638
JM
6454 ui_out_text (uiout, "\tstop only in thread ");
6455 ui_out_field_int (uiout, "thread", b->thread);
6456 ui_out_text (uiout, "\n");
c4093a6a
JM
6457 }
6458
556ec64d
YQ
6459 if (!part_of_multiple)
6460 {
6461 if (b->hit_count)
31f56a27
YQ
6462 {
6463 /* FIXME should make an annotation for this. */
6464 if (is_catchpoint (b))
6465 ui_out_text (uiout, "\tcatchpoint");
6466 else if (is_tracepoint (b))
6467 ui_out_text (uiout, "\ttracepoint");
6468 else
6469 ui_out_text (uiout, "\tbreakpoint");
6470 ui_out_text (uiout, " already hit ");
6471 ui_out_field_int (uiout, "times", b->hit_count);
6472 if (b->hit_count == 1)
6473 ui_out_text (uiout, " time\n");
6474 else
6475 ui_out_text (uiout, " times\n");
6476 }
556ec64d
YQ
6477 else
6478 {
31f56a27
YQ
6479 /* Output the count also if it is zero, but only if this is mi. */
6480 if (ui_out_is_mi_like_p (uiout))
6481 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6482 }
6483 }
8b93c638 6484
0d381245 6485 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6486 {
6487 annotate_field (8);
8b93c638
JM
6488 ui_out_text (uiout, "\tignore next ");
6489 ui_out_field_int (uiout, "ignore", b->ignore_count);
6490 ui_out_text (uiout, " hits\n");
c4093a6a 6491 }
059fb39f 6492
816338b5
SS
6493 /* Note that an enable count of 1 corresponds to "enable once"
6494 behavior, which is reported by the combination of enablement and
6495 disposition, so we don't need to mention it here. */
6496 if (!part_of_multiple && b->enable_count > 1)
6497 {
6498 annotate_field (8);
6499 ui_out_text (uiout, "\tdisable after ");
6500 /* Tweak the wording to clarify that ignore and enable counts
6501 are distinct, and have additive effect. */
6502 if (b->ignore_count)
6503 ui_out_text (uiout, "additional ");
6504 else
6505 ui_out_text (uiout, "next ");
6506 ui_out_field_int (uiout, "enable", b->enable_count);
6507 ui_out_text (uiout, " hits\n");
6508 }
6509
f196051f
SS
6510 if (!part_of_multiple && is_tracepoint (b))
6511 {
6512 struct tracepoint *tp = (struct tracepoint *) b;
6513
6514 if (tp->traceframe_usage)
6515 {
6516 ui_out_text (uiout, "\ttrace buffer usage ");
6517 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6518 ui_out_text (uiout, " bytes\n");
6519 }
6520 }
d3ce09f5 6521
9add0f1b 6522 l = b->commands ? b->commands->commands : NULL;
059fb39f 6523 if (!part_of_multiple && l)
c4093a6a 6524 {
3b31d625
EZ
6525 struct cleanup *script_chain;
6526
c4093a6a 6527 annotate_field (9);
3b31d625 6528 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6529 print_command_lines (uiout, l, 4);
3b31d625 6530 do_cleanups (script_chain);
c4093a6a 6531 }
d24317b4 6532
d9b3f62e 6533 if (is_tracepoint (b))
1042e4c0 6534 {
d9b3f62e
PA
6535 struct tracepoint *t = (struct tracepoint *) b;
6536
6537 if (!part_of_multiple && t->pass_count)
6538 {
6539 annotate_field (10);
6540 ui_out_text (uiout, "\tpass count ");
6541 ui_out_field_int (uiout, "pass", t->pass_count);
6542 ui_out_text (uiout, " \n");
6543 }
f2a8bc8a
YQ
6544
6545 /* Don't display it when tracepoint or tracepoint location is
6546 pending. */
6547 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6548 {
6549 annotate_field (11);
6550
6551 if (ui_out_is_mi_like_p (uiout))
6552 ui_out_field_string (uiout, "installed",
6553 loc->inserted ? "y" : "n");
6554 else
6555 {
6556 if (loc->inserted)
6557 ui_out_text (uiout, "\t");
6558 else
6559 ui_out_text (uiout, "\tnot ");
6560 ui_out_text (uiout, "installed on target\n");
6561 }
6562 }
1042e4c0
SS
6563 }
6564
d24317b4
VP
6565 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6566 {
3a5c3e22
PA
6567 if (is_watchpoint (b))
6568 {
6569 struct watchpoint *w = (struct watchpoint *) b;
6570
6571 ui_out_field_string (uiout, "original-location", w->exp_string);
6572 }
f00aae0f
KS
6573 else if (b->location != NULL
6574 && event_location_to_string (b->location) != NULL)
6575 ui_out_field_string (uiout, "original-location",
6576 event_location_to_string (b->location));
d24317b4 6577 }
c4093a6a 6578}
c5aa993b 6579
0d381245
VP
6580static void
6581print_one_breakpoint (struct breakpoint *b,
4a64f543 6582 struct bp_location **last_loc,
6c95b8df 6583 int allflag)
0d381245 6584{
8d3788bd 6585 struct cleanup *bkpt_chain;
79a45e25 6586 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6587
6588 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6589
12c5a436 6590 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6591 do_cleanups (bkpt_chain);
0d381245
VP
6592
6593 /* If this breakpoint has custom print function,
6594 it's already printed. Otherwise, print individual
6595 locations, if any. */
6596 if (b->ops == NULL || b->ops->print_one == NULL)
6597 {
4a64f543
MS
6598 /* If breakpoint has a single location that is disabled, we
6599 print it as if it had several locations, since otherwise it's
6600 hard to represent "breakpoint enabled, location disabled"
6601 situation.
6602
6603 Note that while hardware watchpoints have several locations
a3be7890 6604 internally, that's not a property exposed to user. */
0d381245 6605 if (b->loc
a5606eee 6606 && !is_hardware_watchpoint (b)
8d3788bd 6607 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6608 {
6609 struct bp_location *loc;
6610 int n = 1;
8d3788bd 6611
0d381245 6612 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6613 {
6614 struct cleanup *inner2 =
6615 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6616 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6617 do_cleanups (inner2);
6618 }
0d381245
VP
6619 }
6620 }
6621}
6622
a6d9a66e
UW
6623static int
6624breakpoint_address_bits (struct breakpoint *b)
6625{
6626 int print_address_bits = 0;
6627 struct bp_location *loc;
6628
6629 for (loc = b->loc; loc; loc = loc->next)
6630 {
c7437ca6
PA
6631 int addr_bit;
6632
6633 /* Software watchpoints that aren't watching memory don't have
6634 an address to print. */
6635 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6636 continue;
6637
6638 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6639 if (addr_bit > print_address_bits)
6640 print_address_bits = addr_bit;
6641 }
6642
6643 return print_address_bits;
6644}
0d381245 6645
c4093a6a
JM
6646struct captured_breakpoint_query_args
6647 {
6648 int bnum;
6649 };
c5aa993b 6650
c4093a6a 6651static int
2b65245e 6652do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6653{
6654 struct captured_breakpoint_query_args *args = data;
52f0bd74 6655 struct breakpoint *b;
a6d9a66e 6656 struct bp_location *dummy_loc = NULL;
cc59ec59 6657
c4093a6a
JM
6658 ALL_BREAKPOINTS (b)
6659 {
6660 if (args->bnum == b->number)
c5aa993b 6661 {
12c5a436 6662 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6663 return GDB_RC_OK;
c5aa993b 6664 }
c4093a6a
JM
6665 }
6666 return GDB_RC_NONE;
6667}
c5aa993b 6668
c4093a6a 6669enum gdb_rc
4a64f543
MS
6670gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6671 char **error_message)
c4093a6a
JM
6672{
6673 struct captured_breakpoint_query_args args;
cc59ec59 6674
c4093a6a
JM
6675 args.bnum = bnum;
6676 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6677 an error. */
b0b13bb4
DJ
6678 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6679 error_message, RETURN_MASK_ALL) < 0)
6680 return GDB_RC_FAIL;
6681 else
6682 return GDB_RC_OK;
c4093a6a 6683}
c5aa993b 6684
09d682a4
TT
6685/* Return true if this breakpoint was set by the user, false if it is
6686 internal or momentary. */
6687
6688int
6689user_breakpoint_p (struct breakpoint *b)
6690{
46c6471b 6691 return b->number > 0;
09d682a4
TT
6692}
6693
7f3b0473 6694/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6695 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6696 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6697 FILTER is non-NULL, call it on each breakpoint and only include the
6698 ones for which it returns non-zero. Return the total number of
6699 breakpoints listed. */
c906108c 6700
d77f58be 6701static int
e5a67952 6702breakpoint_1 (char *args, int allflag,
4a64f543 6703 int (*filter) (const struct breakpoint *))
c4093a6a 6704{
52f0bd74 6705 struct breakpoint *b;
a6d9a66e 6706 struct bp_location *last_loc = NULL;
7f3b0473 6707 int nr_printable_breakpoints;
3b31d625 6708 struct cleanup *bkpttbl_chain;
79a45b7d 6709 struct value_print_options opts;
a6d9a66e 6710 int print_address_bits = 0;
269b11a2 6711 int print_type_col_width = 14;
79a45e25 6712 struct ui_out *uiout = current_uiout;
269b11a2 6713
79a45b7d
TT
6714 get_user_print_options (&opts);
6715
4a64f543
MS
6716 /* Compute the number of rows in the table, as well as the size
6717 required for address fields. */
7f3b0473
AC
6718 nr_printable_breakpoints = 0;
6719 ALL_BREAKPOINTS (b)
e5a67952
MS
6720 {
6721 /* If we have a filter, only list the breakpoints it accepts. */
6722 if (filter && !filter (b))
6723 continue;
6724
6725 /* If we have an "args" string, it is a list of breakpoints to
6726 accept. Skip the others. */
6727 if (args != NULL && *args != '\0')
6728 {
6729 if (allflag && parse_and_eval_long (args) != b->number)
6730 continue;
6731 if (!allflag && !number_is_in_list (args, b->number))
6732 continue;
6733 }
269b11a2 6734
e5a67952
MS
6735 if (allflag || user_breakpoint_p (b))
6736 {
6737 int addr_bit, type_len;
a6d9a66e 6738
e5a67952
MS
6739 addr_bit = breakpoint_address_bits (b);
6740 if (addr_bit > print_address_bits)
6741 print_address_bits = addr_bit;
269b11a2 6742
e5a67952
MS
6743 type_len = strlen (bptype_string (b->type));
6744 if (type_len > print_type_col_width)
6745 print_type_col_width = type_len;
6746
6747 nr_printable_breakpoints++;
6748 }
6749 }
7f3b0473 6750
79a45b7d 6751 if (opts.addressprint)
3b31d625 6752 bkpttbl_chain
3e43a32a
MS
6753 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6754 nr_printable_breakpoints,
3b31d625 6755 "BreakpointTable");
8b93c638 6756 else
3b31d625 6757 bkpttbl_chain
3e43a32a
MS
6758 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6759 nr_printable_breakpoints,
3b31d625 6760 "BreakpointTable");
8b93c638 6761
7f3b0473 6762 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6763 annotate_breakpoints_headers ();
6764 if (nr_printable_breakpoints > 0)
6765 annotate_field (0);
4a64f543 6766 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6767 if (nr_printable_breakpoints > 0)
6768 annotate_field (1);
269b11a2 6769 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6770 "type", "Type"); /* 2 */
d7faa9e7
AC
6771 if (nr_printable_breakpoints > 0)
6772 annotate_field (2);
4a64f543 6773 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6774 if (nr_printable_breakpoints > 0)
6775 annotate_field (3);
54e52265 6776 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6777 if (opts.addressprint)
e5a67952
MS
6778 {
6779 if (nr_printable_breakpoints > 0)
6780 annotate_field (4);
6781 if (print_address_bits <= 32)
6782 ui_out_table_header (uiout, 10, ui_left,
6783 "addr", "Address"); /* 5 */
6784 else
6785 ui_out_table_header (uiout, 18, ui_left,
6786 "addr", "Address"); /* 5 */
6787 }
d7faa9e7
AC
6788 if (nr_printable_breakpoints > 0)
6789 annotate_field (5);
6790 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6791 ui_out_table_body (uiout);
6792 if (nr_printable_breakpoints > 0)
6793 annotate_breakpoints_table ();
7f3b0473 6794
c4093a6a 6795 ALL_BREAKPOINTS (b)
e5a67952
MS
6796 {
6797 QUIT;
6798 /* If we have a filter, only list the breakpoints it accepts. */
6799 if (filter && !filter (b))
6800 continue;
6801
6802 /* If we have an "args" string, it is a list of breakpoints to
6803 accept. Skip the others. */
6804
6805 if (args != NULL && *args != '\0')
6806 {
6807 if (allflag) /* maintenance info breakpoint */
6808 {
6809 if (parse_and_eval_long (args) != b->number)
6810 continue;
6811 }
6812 else /* all others */
6813 {
6814 if (!number_is_in_list (args, b->number))
6815 continue;
6816 }
6817 }
6818 /* We only print out user settable breakpoints unless the
6819 allflag is set. */
6820 if (allflag || user_breakpoint_p (b))
12c5a436 6821 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6822 }
6823
3b31d625 6824 do_cleanups (bkpttbl_chain);
698384cd 6825
7f3b0473 6826 if (nr_printable_breakpoints == 0)
c906108c 6827 {
4a64f543
MS
6828 /* If there's a filter, let the caller decide how to report
6829 empty list. */
d77f58be
SS
6830 if (!filter)
6831 {
e5a67952 6832 if (args == NULL || *args == '\0')
d77f58be
SS
6833 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6834 else
4a64f543 6835 ui_out_message (uiout, 0,
e5a67952
MS
6836 "No breakpoint or watchpoint matching '%s'.\n",
6837 args);
d77f58be 6838 }
c906108c
SS
6839 }
6840 else
c4093a6a 6841 {
a6d9a66e
UW
6842 if (last_loc && !server_command)
6843 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6844 }
c906108c 6845
4a64f543 6846 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6847 there have been breakpoints? */
c906108c 6848 annotate_breakpoints_table_end ();
d77f58be
SS
6849
6850 return nr_printable_breakpoints;
c906108c
SS
6851}
6852
ad443146
SS
6853/* Display the value of default-collect in a way that is generally
6854 compatible with the breakpoint list. */
6855
6856static void
6857default_collect_info (void)
6858{
79a45e25
PA
6859 struct ui_out *uiout = current_uiout;
6860
ad443146
SS
6861 /* If it has no value (which is frequently the case), say nothing; a
6862 message like "No default-collect." gets in user's face when it's
6863 not wanted. */
6864 if (!*default_collect)
6865 return;
6866
6867 /* The following phrase lines up nicely with per-tracepoint collect
6868 actions. */
6869 ui_out_text (uiout, "default collect ");
6870 ui_out_field_string (uiout, "default-collect", default_collect);
6871 ui_out_text (uiout, " \n");
6872}
6873
c906108c 6874static void
e5a67952 6875breakpoints_info (char *args, int from_tty)
c906108c 6876{
e5a67952 6877 breakpoint_1 (args, 0, NULL);
ad443146
SS
6878
6879 default_collect_info ();
d77f58be
SS
6880}
6881
6882static void
e5a67952 6883watchpoints_info (char *args, int from_tty)
d77f58be 6884{
e5a67952 6885 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6886 struct ui_out *uiout = current_uiout;
d77f58be
SS
6887
6888 if (num_printed == 0)
6889 {
e5a67952 6890 if (args == NULL || *args == '\0')
d77f58be
SS
6891 ui_out_message (uiout, 0, "No watchpoints.\n");
6892 else
e5a67952 6893 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6894 }
c906108c
SS
6895}
6896
7a292a7a 6897static void
e5a67952 6898maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6899{
e5a67952 6900 breakpoint_1 (args, 1, NULL);
ad443146
SS
6901
6902 default_collect_info ();
c906108c
SS
6903}
6904
0d381245 6905static int
714835d5 6906breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6907 struct program_space *pspace,
714835d5 6908 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6909{
6910 struct bp_location *bl = b->loc;
cc59ec59 6911
0d381245
VP
6912 for (; bl; bl = bl->next)
6913 {
6c95b8df
PA
6914 if (bl->pspace == pspace
6915 && bl->address == pc
0d381245
VP
6916 && (!overlay_debugging || bl->section == section))
6917 return 1;
6918 }
6919 return 0;
6920}
6921
672f9b60 6922/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6923 concerns with logical breakpoints, so we match program spaces, not
6924 address spaces. */
c906108c
SS
6925
6926static void
6c95b8df
PA
6927describe_other_breakpoints (struct gdbarch *gdbarch,
6928 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6929 struct obj_section *section, int thread)
c906108c 6930{
52f0bd74
AC
6931 int others = 0;
6932 struct breakpoint *b;
c906108c
SS
6933
6934 ALL_BREAKPOINTS (b)
672f9b60
KP
6935 others += (user_breakpoint_p (b)
6936 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6937 if (others > 0)
6938 {
a3f17187
AC
6939 if (others == 1)
6940 printf_filtered (_("Note: breakpoint "));
6941 else /* if (others == ???) */
6942 printf_filtered (_("Note: breakpoints "));
c906108c 6943 ALL_BREAKPOINTS (b)
672f9b60 6944 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6945 {
6946 others--;
6947 printf_filtered ("%d", b->number);
6948 if (b->thread == -1 && thread != -1)
6949 printf_filtered (" (all threads)");
6950 else if (b->thread != -1)
6951 printf_filtered (" (thread %d)", b->thread);
6952 printf_filtered ("%s%s ",
059fb39f 6953 ((b->enable_state == bp_disabled
f8eba3c6 6954 || b->enable_state == bp_call_disabled)
0d381245 6955 ? " (disabled)"
0d381245
VP
6956 : ""),
6957 (others > 1) ? ","
6958 : ((others == 1) ? " and" : ""));
6959 }
a3f17187 6960 printf_filtered (_("also set at pc "));
5af949e3 6961 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6962 printf_filtered (".\n");
6963 }
6964}
6965\f
c906108c 6966
e4f237da 6967/* Return true iff it is meaningful to use the address member of
244558af
LM
6968 BPT locations. For some breakpoint types, the locations' address members
6969 are irrelevant and it makes no sense to attempt to compare them to other
6970 addresses (or use them for any other purpose either).
e4f237da 6971
4a64f543 6972 More specifically, each of the following breakpoint types will
244558af 6973 always have a zero valued location address and we don't want to mark
4a64f543 6974 breakpoints of any of these types to be a duplicate of an actual
244558af 6975 breakpoint location at address zero:
e4f237da
KB
6976
6977 bp_watchpoint
2d134ed3
PA
6978 bp_catchpoint
6979
6980*/
e4f237da
KB
6981
6982static int
6983breakpoint_address_is_meaningful (struct breakpoint *bpt)
6984{
6985 enum bptype type = bpt->type;
6986
2d134ed3
PA
6987 return (type != bp_watchpoint && type != bp_catchpoint);
6988}
6989
6990/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6991 true if LOC1 and LOC2 represent the same watchpoint location. */
6992
6993static int
4a64f543
MS
6994watchpoint_locations_match (struct bp_location *loc1,
6995 struct bp_location *loc2)
2d134ed3 6996{
3a5c3e22
PA
6997 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6998 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6999
7000 /* Both of them must exist. */
7001 gdb_assert (w1 != NULL);
7002 gdb_assert (w2 != NULL);
2bdf28a0 7003
4a64f543
MS
7004 /* If the target can evaluate the condition expression in hardware,
7005 then we we need to insert both watchpoints even if they are at
7006 the same place. Otherwise the watchpoint will only trigger when
7007 the condition of whichever watchpoint was inserted evaluates to
7008 true, not giving a chance for GDB to check the condition of the
7009 other watchpoint. */
3a5c3e22 7010 if ((w1->cond_exp
4a64f543
MS
7011 && target_can_accel_watchpoint_condition (loc1->address,
7012 loc1->length,
0cf6dd15 7013 loc1->watchpoint_type,
3a5c3e22
PA
7014 w1->cond_exp))
7015 || (w2->cond_exp
4a64f543
MS
7016 && target_can_accel_watchpoint_condition (loc2->address,
7017 loc2->length,
0cf6dd15 7018 loc2->watchpoint_type,
3a5c3e22 7019 w2->cond_exp)))
0cf6dd15
TJB
7020 return 0;
7021
85d721b8
PA
7022 /* Note that this checks the owner's type, not the location's. In
7023 case the target does not support read watchpoints, but does
7024 support access watchpoints, we'll have bp_read_watchpoint
7025 watchpoints with hw_access locations. Those should be considered
7026 duplicates of hw_read locations. The hw_read locations will
7027 become hw_access locations later. */
2d134ed3
PA
7028 return (loc1->owner->type == loc2->owner->type
7029 && loc1->pspace->aspace == loc2->pspace->aspace
7030 && loc1->address == loc2->address
7031 && loc1->length == loc2->length);
e4f237da
KB
7032}
7033
31e77af2 7034/* See breakpoint.h. */
6c95b8df 7035
31e77af2 7036int
6c95b8df
PA
7037breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7038 struct address_space *aspace2, CORE_ADDR addr2)
7039{
f5656ead 7040 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7041 || aspace1 == aspace2)
7042 && addr1 == addr2);
7043}
7044
f1310107
TJB
7045/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7046 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7047 matches ASPACE2. On targets that have global breakpoints, the address
7048 space doesn't really matter. */
7049
7050static int
7051breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7052 int len1, struct address_space *aspace2,
7053 CORE_ADDR addr2)
7054{
f5656ead 7055 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7056 || aspace1 == aspace2)
7057 && addr2 >= addr1 && addr2 < addr1 + len1);
7058}
7059
7060/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7061 a ranged breakpoint. In most targets, a match happens only if ASPACE
7062 matches the breakpoint's address space. On targets that have global
7063 breakpoints, the address space doesn't really matter. */
7064
7065static int
7066breakpoint_location_address_match (struct bp_location *bl,
7067 struct address_space *aspace,
7068 CORE_ADDR addr)
7069{
7070 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7071 aspace, addr)
7072 || (bl->length
7073 && breakpoint_address_match_range (bl->pspace->aspace,
7074 bl->address, bl->length,
7075 aspace, addr)));
7076}
7077
1e4d1764
YQ
7078/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7079 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7080 true, otherwise returns false. */
7081
7082static int
7083tracepoint_locations_match (struct bp_location *loc1,
7084 struct bp_location *loc2)
7085{
7086 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7087 /* Since tracepoint locations are never duplicated with others', tracepoint
7088 locations at the same address of different tracepoints are regarded as
7089 different locations. */
7090 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7091 else
7092 return 0;
7093}
7094
2d134ed3
PA
7095/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7096 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7097 represent the same location. */
7098
7099static int
4a64f543
MS
7100breakpoint_locations_match (struct bp_location *loc1,
7101 struct bp_location *loc2)
2d134ed3 7102{
2bdf28a0
JK
7103 int hw_point1, hw_point2;
7104
7105 /* Both of them must not be in moribund_locations. */
7106 gdb_assert (loc1->owner != NULL);
7107 gdb_assert (loc2->owner != NULL);
7108
7109 hw_point1 = is_hardware_watchpoint (loc1->owner);
7110 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7111
7112 if (hw_point1 != hw_point2)
7113 return 0;
7114 else if (hw_point1)
7115 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7116 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7117 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7118 else
f1310107
TJB
7119 /* We compare bp_location.length in order to cover ranged breakpoints. */
7120 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7121 loc2->pspace->aspace, loc2->address)
7122 && loc1->length == loc2->length);
2d134ed3
PA
7123}
7124
76897487
KB
7125static void
7126breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7127 int bnum, int have_bnum)
7128{
f63fbe86
MS
7129 /* The longest string possibly returned by hex_string_custom
7130 is 50 chars. These must be at least that big for safety. */
7131 char astr1[64];
7132 char astr2[64];
76897487 7133
bb599908
PH
7134 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7135 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7136 if (have_bnum)
8a3fe4f8 7137 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7138 bnum, astr1, astr2);
7139 else
8a3fe4f8 7140 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7141}
7142
4a64f543
MS
7143/* Adjust a breakpoint's address to account for architectural
7144 constraints on breakpoint placement. Return the adjusted address.
7145 Note: Very few targets require this kind of adjustment. For most
7146 targets, this function is simply the identity function. */
76897487
KB
7147
7148static CORE_ADDR
a6d9a66e
UW
7149adjust_breakpoint_address (struct gdbarch *gdbarch,
7150 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7151{
a6d9a66e 7152 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7153 {
7154 /* Very few targets need any kind of breakpoint adjustment. */
7155 return bpaddr;
7156 }
88f7da05
KB
7157 else if (bptype == bp_watchpoint
7158 || bptype == bp_hardware_watchpoint
7159 || bptype == bp_read_watchpoint
7160 || bptype == bp_access_watchpoint
fe798b75 7161 || bptype == bp_catchpoint)
88f7da05
KB
7162 {
7163 /* Watchpoints and the various bp_catch_* eventpoints should not
7164 have their addresses modified. */
7165 return bpaddr;
7166 }
7c16b83e
PA
7167 else if (bptype == bp_single_step)
7168 {
7169 /* Single-step breakpoints should not have their addresses
7170 modified. If there's any architectural constrain that
7171 applies to this address, then it should have already been
7172 taken into account when the breakpoint was created in the
7173 first place. If we didn't do this, stepping through e.g.,
7174 Thumb-2 IT blocks would break. */
7175 return bpaddr;
7176 }
76897487
KB
7177 else
7178 {
7179 CORE_ADDR adjusted_bpaddr;
7180
7181 /* Some targets have architectural constraints on the placement
7182 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7183 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7184
7185 /* An adjusted breakpoint address can significantly alter
7186 a user's expectations. Print a warning if an adjustment
7187 is required. */
7188 if (adjusted_bpaddr != bpaddr)
7189 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7190
7191 return adjusted_bpaddr;
7192 }
7193}
7194
28010a5d
PA
7195void
7196init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7197 struct breakpoint *owner)
7cc221ef 7198{
7cc221ef
DJ
7199 memset (loc, 0, sizeof (*loc));
7200
348d480f
PA
7201 gdb_assert (ops != NULL);
7202
28010a5d
PA
7203 loc->ops = ops;
7204 loc->owner = owner;
511a6cd4 7205 loc->cond = NULL;
b775012e 7206 loc->cond_bytecode = NULL;
0d381245
VP
7207 loc->shlib_disabled = 0;
7208 loc->enabled = 1;
e049a4b5 7209
28010a5d 7210 switch (owner->type)
e049a4b5
DJ
7211 {
7212 case bp_breakpoint:
7c16b83e 7213 case bp_single_step:
e049a4b5
DJ
7214 case bp_until:
7215 case bp_finish:
7216 case bp_longjmp:
7217 case bp_longjmp_resume:
e2e4d78b 7218 case bp_longjmp_call_dummy:
186c406b
TT
7219 case bp_exception:
7220 case bp_exception_resume:
e049a4b5 7221 case bp_step_resume:
2c03e5be 7222 case bp_hp_step_resume:
e049a4b5
DJ
7223 case bp_watchpoint_scope:
7224 case bp_call_dummy:
aa7d318d 7225 case bp_std_terminate:
e049a4b5
DJ
7226 case bp_shlib_event:
7227 case bp_thread_event:
7228 case bp_overlay_event:
4efc6507 7229 case bp_jit_event:
0fd8e87f 7230 case bp_longjmp_master:
aa7d318d 7231 case bp_std_terminate_master:
186c406b 7232 case bp_exception_master:
0e30163f
JK
7233 case bp_gnu_ifunc_resolver:
7234 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7235 case bp_dprintf:
e049a4b5 7236 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7237 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7238 break;
7239 case bp_hardware_breakpoint:
7240 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7241 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7242 break;
7243 case bp_hardware_watchpoint:
7244 case bp_read_watchpoint:
7245 case bp_access_watchpoint:
7246 loc->loc_type = bp_loc_hardware_watchpoint;
7247 break;
7248 case bp_watchpoint:
ce78b96d 7249 case bp_catchpoint:
15c3d785
PA
7250 case bp_tracepoint:
7251 case bp_fast_tracepoint:
0fb4aa4b 7252 case bp_static_tracepoint:
e049a4b5
DJ
7253 loc->loc_type = bp_loc_other;
7254 break;
7255 default:
e2e0b3e5 7256 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7257 }
7258
f431efe5 7259 loc->refc = 1;
28010a5d
PA
7260}
7261
7262/* Allocate a struct bp_location. */
7263
7264static struct bp_location *
7265allocate_bp_location (struct breakpoint *bpt)
7266{
348d480f
PA
7267 return bpt->ops->allocate_location (bpt);
7268}
7cc221ef 7269
f431efe5
PA
7270static void
7271free_bp_location (struct bp_location *loc)
fe3f5fa8 7272{
348d480f 7273 loc->ops->dtor (loc);
fe3f5fa8
VP
7274 xfree (loc);
7275}
7276
f431efe5
PA
7277/* Increment reference count. */
7278
7279static void
7280incref_bp_location (struct bp_location *bl)
7281{
7282 ++bl->refc;
7283}
7284
7285/* Decrement reference count. If the reference count reaches 0,
7286 destroy the bp_location. Sets *BLP to NULL. */
7287
7288static void
7289decref_bp_location (struct bp_location **blp)
7290{
0807b50c
PA
7291 gdb_assert ((*blp)->refc > 0);
7292
f431efe5
PA
7293 if (--(*blp)->refc == 0)
7294 free_bp_location (*blp);
7295 *blp = NULL;
7296}
7297
346774a9 7298/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7299
346774a9
PA
7300static void
7301add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7302{
346774a9 7303 struct breakpoint *b1;
c906108c 7304
346774a9
PA
7305 /* Add this breakpoint to the end of the chain so that a list of
7306 breakpoints will come out in order of increasing numbers. */
7307
7308 b1 = breakpoint_chain;
7309 if (b1 == 0)
7310 breakpoint_chain = b;
7311 else
7312 {
7313 while (b1->next)
7314 b1 = b1->next;
7315 b1->next = b;
7316 }
7317}
7318
7319/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7320
7321static void
7322init_raw_breakpoint_without_location (struct breakpoint *b,
7323 struct gdbarch *gdbarch,
28010a5d 7324 enum bptype bptype,
c0a91b2b 7325 const struct breakpoint_ops *ops)
346774a9 7326{
c906108c 7327 memset (b, 0, sizeof (*b));
2219d63c 7328
348d480f
PA
7329 gdb_assert (ops != NULL);
7330
28010a5d 7331 b->ops = ops;
4d28f7a8 7332 b->type = bptype;
a6d9a66e 7333 b->gdbarch = gdbarch;
c906108c
SS
7334 b->language = current_language->la_language;
7335 b->input_radix = input_radix;
7336 b->thread = -1;
b5de0fa7 7337 b->enable_state = bp_enabled;
c906108c
SS
7338 b->next = 0;
7339 b->silent = 0;
7340 b->ignore_count = 0;
7341 b->commands = NULL;
818dd999 7342 b->frame_id = null_frame_id;
0d381245 7343 b->condition_not_parsed = 0;
84f4c1fe 7344 b->py_bp_object = NULL;
d0fb5eae 7345 b->related_breakpoint = b;
f00aae0f 7346 b->location = NULL;
346774a9
PA
7347}
7348
7349/* Helper to set_raw_breakpoint below. Creates a breakpoint
7350 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7351
7352static struct breakpoint *
7353set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7354 enum bptype bptype,
c0a91b2b 7355 const struct breakpoint_ops *ops)
346774a9
PA
7356{
7357 struct breakpoint *b = XNEW (struct breakpoint);
7358
348d480f 7359 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7360 add_to_breakpoint_chain (b);
0d381245
VP
7361 return b;
7362}
7363
0e30163f
JK
7364/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7365 resolutions should be made as the user specified the location explicitly
7366 enough. */
7367
0d381245 7368static void
0e30163f 7369set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7370{
2bdf28a0
JK
7371 gdb_assert (loc->owner != NULL);
7372
0d381245 7373 if (loc->owner->type == bp_breakpoint
1042e4c0 7374 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7375 || is_tracepoint (loc->owner))
0d381245 7376 {
0e30163f 7377 int is_gnu_ifunc;
2c02bd72 7378 const char *function_name;
6a3a010b 7379 CORE_ADDR func_addr;
0e30163f 7380
2c02bd72 7381 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7382 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7383
7384 if (is_gnu_ifunc && !explicit_loc)
7385 {
7386 struct breakpoint *b = loc->owner;
7387
7388 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7389 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7390 &loc->requested_address))
7391 {
7392 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7393 loc->address = adjust_breakpoint_address (loc->gdbarch,
7394 loc->requested_address,
7395 b->type);
7396 }
7397 else if (b->type == bp_breakpoint && b->loc == loc
7398 && loc->next == NULL && b->related_breakpoint == b)
7399 {
7400 /* Create only the whole new breakpoint of this type but do not
7401 mess more complicated breakpoints with multiple locations. */
7402 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7403 /* Remember the resolver's address for use by the return
7404 breakpoint. */
7405 loc->related_address = func_addr;
0e30163f
JK
7406 }
7407 }
7408
2c02bd72
DE
7409 if (function_name)
7410 loc->function_name = xstrdup (function_name);
0d381245
VP
7411 }
7412}
7413
a6d9a66e 7414/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7415struct gdbarch *
a6d9a66e
UW
7416get_sal_arch (struct symtab_and_line sal)
7417{
7418 if (sal.section)
7419 return get_objfile_arch (sal.section->objfile);
7420 if (sal.symtab)
eb822aa6 7421 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7422
7423 return NULL;
7424}
7425
346774a9
PA
7426/* Low level routine for partially initializing a breakpoint of type
7427 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7428 file name, and line number are provided by SAL.
0d381245
VP
7429
7430 It is expected that the caller will complete the initialization of
7431 the newly created breakpoint struct as well as output any status
c56053d2 7432 information regarding the creation of a new breakpoint. */
0d381245 7433
346774a9
PA
7434static void
7435init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7436 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7437 const struct breakpoint_ops *ops)
0d381245 7438{
28010a5d 7439 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7440
3742cc8b 7441 add_location_to_breakpoint (b, &sal);
0d381245 7442
6c95b8df
PA
7443 if (bptype != bp_catchpoint)
7444 gdb_assert (sal.pspace != NULL);
7445
f8eba3c6
TT
7446 /* Store the program space that was used to set the breakpoint,
7447 except for ordinary breakpoints, which are independent of the
7448 program space. */
7449 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7450 b->pspace = sal.pspace;
346774a9 7451}
c906108c 7452
346774a9
PA
7453/* set_raw_breakpoint is a low level routine for allocating and
7454 partially initializing a breakpoint of type BPTYPE. The newly
7455 created breakpoint's address, section, source file name, and line
7456 number are provided by SAL. The newly created and partially
7457 initialized breakpoint is added to the breakpoint chain and
7458 is also returned as the value of this function.
7459
7460 It is expected that the caller will complete the initialization of
7461 the newly created breakpoint struct as well as output any status
7462 information regarding the creation of a new breakpoint. In
7463 particular, set_raw_breakpoint does NOT set the breakpoint
7464 number! Care should be taken to not allow an error to occur
7465 prior to completing the initialization of the breakpoint. If this
7466 should happen, a bogus breakpoint will be left on the chain. */
7467
7468struct breakpoint *
7469set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7470 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7471 const struct breakpoint_ops *ops)
346774a9
PA
7472{
7473 struct breakpoint *b = XNEW (struct breakpoint);
7474
348d480f 7475 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7476 add_to_breakpoint_chain (b);
c906108c
SS
7477 return b;
7478}
7479
53a5351d 7480/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7481 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7482 initiated the operation. */
c906108c
SS
7483
7484void
186c406b 7485set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7486{
35df4500 7487 struct breakpoint *b, *b_tmp;
186c406b 7488 int thread = tp->num;
0fd8e87f
UW
7489
7490 /* To avoid having to rescan all objfile symbols at every step,
7491 we maintain a list of continually-inserted but always disabled
7492 longjmp "master" breakpoints. Here, we simply create momentary
7493 clones of those and enable them for the requested thread. */
35df4500 7494 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7495 if (b->pspace == current_program_space
186c406b
TT
7496 && (b->type == bp_longjmp_master
7497 || b->type == bp_exception_master))
0fd8e87f 7498 {
06edf0c0
PA
7499 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7500 struct breakpoint *clone;
cc59ec59 7501
e2e4d78b
JK
7502 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7503 after their removal. */
06edf0c0 7504 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7505 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7506 clone->thread = thread;
7507 }
186c406b
TT
7508
7509 tp->initiating_frame = frame;
c906108c
SS
7510}
7511
611c83ae 7512/* Delete all longjmp breakpoints from THREAD. */
c906108c 7513void
611c83ae 7514delete_longjmp_breakpoint (int thread)
c906108c 7515{
35df4500 7516 struct breakpoint *b, *b_tmp;
c906108c 7517
35df4500 7518 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7519 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7520 {
7521 if (b->thread == thread)
7522 delete_breakpoint (b);
7523 }
c906108c
SS
7524}
7525
f59f708a
PA
7526void
7527delete_longjmp_breakpoint_at_next_stop (int thread)
7528{
7529 struct breakpoint *b, *b_tmp;
7530
7531 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7532 if (b->type == bp_longjmp || b->type == bp_exception)
7533 {
7534 if (b->thread == thread)
7535 b->disposition = disp_del_at_next_stop;
7536 }
7537}
7538
e2e4d78b
JK
7539/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7540 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7541 pointer to any of them. Return NULL if this system cannot place longjmp
7542 breakpoints. */
7543
7544struct breakpoint *
7545set_longjmp_breakpoint_for_call_dummy (void)
7546{
7547 struct breakpoint *b, *retval = NULL;
7548
7549 ALL_BREAKPOINTS (b)
7550 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7551 {
7552 struct breakpoint *new_b;
7553
7554 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7555 &momentary_breakpoint_ops,
7556 1);
e2e4d78b
JK
7557 new_b->thread = pid_to_thread_id (inferior_ptid);
7558
7559 /* Link NEW_B into the chain of RETVAL breakpoints. */
7560
7561 gdb_assert (new_b->related_breakpoint == new_b);
7562 if (retval == NULL)
7563 retval = new_b;
7564 new_b->related_breakpoint = retval;
7565 while (retval->related_breakpoint != new_b->related_breakpoint)
7566 retval = retval->related_breakpoint;
7567 retval->related_breakpoint = new_b;
7568 }
7569
7570 return retval;
7571}
7572
7573/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7574 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7575 stack.
7576
7577 You should call this function only at places where it is safe to currently
7578 unwind the whole stack. Failed stack unwind would discard live dummy
7579 frames. */
7580
7581void
b67a2c6f 7582check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7583{
7584 struct breakpoint *b, *b_tmp;
7585
7586 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7587 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7588 {
7589 struct breakpoint *dummy_b = b->related_breakpoint;
7590
7591 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7592 dummy_b = dummy_b->related_breakpoint;
7593 if (dummy_b->type != bp_call_dummy
7594 || frame_find_by_id (dummy_b->frame_id) != NULL)
7595 continue;
7596
b67a2c6f 7597 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7598
7599 while (b->related_breakpoint != b)
7600 {
7601 if (b_tmp == b->related_breakpoint)
7602 b_tmp = b->related_breakpoint->next;
7603 delete_breakpoint (b->related_breakpoint);
7604 }
7605 delete_breakpoint (b);
7606 }
7607}
7608
1900040c
MS
7609void
7610enable_overlay_breakpoints (void)
7611{
52f0bd74 7612 struct breakpoint *b;
1900040c
MS
7613
7614 ALL_BREAKPOINTS (b)
7615 if (b->type == bp_overlay_event)
7616 {
7617 b->enable_state = bp_enabled;
44702360 7618 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7619 overlay_events_enabled = 1;
1900040c
MS
7620 }
7621}
7622
7623void
7624disable_overlay_breakpoints (void)
7625{
52f0bd74 7626 struct breakpoint *b;
1900040c
MS
7627
7628 ALL_BREAKPOINTS (b)
7629 if (b->type == bp_overlay_event)
7630 {
7631 b->enable_state = bp_disabled;
44702360 7632 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7633 overlay_events_enabled = 0;
1900040c
MS
7634 }
7635}
7636
aa7d318d
TT
7637/* Set an active std::terminate breakpoint for each std::terminate
7638 master breakpoint. */
7639void
7640set_std_terminate_breakpoint (void)
7641{
35df4500 7642 struct breakpoint *b, *b_tmp;
aa7d318d 7643
35df4500 7644 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7645 if (b->pspace == current_program_space
7646 && b->type == bp_std_terminate_master)
7647 {
06edf0c0 7648 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7649 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7650 }
7651}
7652
7653/* Delete all the std::terminate breakpoints. */
7654void
7655delete_std_terminate_breakpoint (void)
7656{
35df4500 7657 struct breakpoint *b, *b_tmp;
aa7d318d 7658
35df4500 7659 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7660 if (b->type == bp_std_terminate)
7661 delete_breakpoint (b);
7662}
7663
c4093a6a 7664struct breakpoint *
a6d9a66e 7665create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7666{
7667 struct breakpoint *b;
c4093a6a 7668
06edf0c0
PA
7669 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7670 &internal_breakpoint_ops);
7671
b5de0fa7 7672 b->enable_state = bp_enabled;
f00aae0f 7673 /* location has to be used or breakpoint_re_set will delete me. */
a06efdd6 7674 b->location = new_address_location (b->loc->address);
c4093a6a 7675
44702360 7676 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7677
c4093a6a
JM
7678 return b;
7679}
7680
7681void
7682remove_thread_event_breakpoints (void)
7683{
35df4500 7684 struct breakpoint *b, *b_tmp;
c4093a6a 7685
35df4500 7686 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7687 if (b->type == bp_thread_event
7688 && b->loc->pspace == current_program_space)
c4093a6a
JM
7689 delete_breakpoint (b);
7690}
7691
0101ce28
JJ
7692struct lang_and_radix
7693 {
7694 enum language lang;
7695 int radix;
7696 };
7697
4efc6507
DE
7698/* Create a breakpoint for JIT code registration and unregistration. */
7699
7700struct breakpoint *
7701create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7702{
7703 struct breakpoint *b;
7704
06edf0c0
PA
7705 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7706 &internal_breakpoint_ops);
44702360 7707 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7708 return b;
7709}
0101ce28 7710
03673fc7
PP
7711/* Remove JIT code registration and unregistration breakpoint(s). */
7712
7713void
7714remove_jit_event_breakpoints (void)
7715{
7716 struct breakpoint *b, *b_tmp;
7717
7718 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7719 if (b->type == bp_jit_event
7720 && b->loc->pspace == current_program_space)
7721 delete_breakpoint (b);
7722}
7723
cae688ec
JJ
7724void
7725remove_solib_event_breakpoints (void)
7726{
35df4500 7727 struct breakpoint *b, *b_tmp;
cae688ec 7728
35df4500 7729 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7730 if (b->type == bp_shlib_event
7731 && b->loc->pspace == current_program_space)
cae688ec
JJ
7732 delete_breakpoint (b);
7733}
7734
f37f681c
PA
7735/* See breakpoint.h. */
7736
7737void
7738remove_solib_event_breakpoints_at_next_stop (void)
7739{
7740 struct breakpoint *b, *b_tmp;
7741
7742 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7743 if (b->type == bp_shlib_event
7744 && b->loc->pspace == current_program_space)
7745 b->disposition = disp_del_at_next_stop;
7746}
7747
04086b45
PA
7748/* Helper for create_solib_event_breakpoint /
7749 create_and_insert_solib_event_breakpoint. Allows specifying which
7750 INSERT_MODE to pass through to update_global_location_list. */
7751
7752static struct breakpoint *
7753create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7754 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7755{
7756 struct breakpoint *b;
7757
06edf0c0
PA
7758 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7759 &internal_breakpoint_ops);
04086b45 7760 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7761 return b;
7762}
7763
04086b45
PA
7764struct breakpoint *
7765create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7766{
7767 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7768}
7769
f37f681c
PA
7770/* See breakpoint.h. */
7771
7772struct breakpoint *
7773create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7774{
7775 struct breakpoint *b;
7776
04086b45
PA
7777 /* Explicitly tell update_global_location_list to insert
7778 locations. */
7779 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7780 if (!b->loc->inserted)
7781 {
7782 delete_breakpoint (b);
7783 return NULL;
7784 }
7785 return b;
7786}
7787
cae688ec
JJ
7788/* Disable any breakpoints that are on code in shared libraries. Only
7789 apply to enabled breakpoints, disabled ones can just stay disabled. */
7790
7791void
cb851954 7792disable_breakpoints_in_shlibs (void)
cae688ec 7793{
876fa593 7794 struct bp_location *loc, **locp_tmp;
cae688ec 7795
876fa593 7796 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7797 {
2bdf28a0 7798 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7799 struct breakpoint *b = loc->owner;
2bdf28a0 7800
4a64f543
MS
7801 /* We apply the check to all breakpoints, including disabled for
7802 those with loc->duplicate set. This is so that when breakpoint
7803 becomes enabled, or the duplicate is removed, gdb will try to
7804 insert all breakpoints. If we don't set shlib_disabled here,
7805 we'll try to insert those breakpoints and fail. */
1042e4c0 7806 if (((b->type == bp_breakpoint)
508ccb1f 7807 || (b->type == bp_jit_event)
1042e4c0 7808 || (b->type == bp_hardware_breakpoint)
d77f58be 7809 || (is_tracepoint (b)))
6c95b8df 7810 && loc->pspace == current_program_space
0d381245 7811 && !loc->shlib_disabled
6c95b8df 7812 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7813 )
0d381245
VP
7814 {
7815 loc->shlib_disabled = 1;
7816 }
cae688ec
JJ
7817 }
7818}
7819
63644780
NB
7820/* Disable any breakpoints and tracepoints that are in SOLIB upon
7821 notification of unloaded_shlib. Only apply to enabled breakpoints,
7822 disabled ones can just stay disabled. */
84acb35a 7823
75149521 7824static void
84acb35a
JJ
7825disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7826{
876fa593 7827 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7828 int disabled_shlib_breaks = 0;
7829
c86cf029
VP
7830 /* SunOS a.out shared libraries are always mapped, so do not
7831 disable breakpoints; they will only be reported as unloaded
7832 through clear_solib when GDB discards its shared library
7833 list. See clear_solib for more information. */
7834 if (exec_bfd != NULL
7835 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7836 return;
7837
876fa593 7838 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7839 {
2bdf28a0 7840 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7841 struct breakpoint *b = loc->owner;
cc59ec59 7842
1e4d1764 7843 if (solib->pspace == loc->pspace
e2dd7057 7844 && !loc->shlib_disabled
1e4d1764
YQ
7845 && (((b->type == bp_breakpoint
7846 || b->type == bp_jit_event
7847 || b->type == bp_hardware_breakpoint)
7848 && (loc->loc_type == bp_loc_hardware_breakpoint
7849 || loc->loc_type == bp_loc_software_breakpoint))
7850 || is_tracepoint (b))
e2dd7057 7851 && solib_contains_address_p (solib, loc->address))
84acb35a 7852 {
e2dd7057
PP
7853 loc->shlib_disabled = 1;
7854 /* At this point, we cannot rely on remove_breakpoint
7855 succeeding so we must mark the breakpoint as not inserted
7856 to prevent future errors occurring in remove_breakpoints. */
7857 loc->inserted = 0;
8d3788bd
VP
7858
7859 /* This may cause duplicate notifications for the same breakpoint. */
7860 observer_notify_breakpoint_modified (b);
7861
e2dd7057
PP
7862 if (!disabled_shlib_breaks)
7863 {
7864 target_terminal_ours_for_output ();
3e43a32a
MS
7865 warning (_("Temporarily disabling breakpoints "
7866 "for unloaded shared library \"%s\""),
e2dd7057 7867 solib->so_name);
84acb35a 7868 }
e2dd7057 7869 disabled_shlib_breaks = 1;
84acb35a
JJ
7870 }
7871 }
84acb35a
JJ
7872}
7873
63644780
NB
7874/* Disable any breakpoints and tracepoints in OBJFILE upon
7875 notification of free_objfile. Only apply to enabled breakpoints,
7876 disabled ones can just stay disabled. */
7877
7878static void
7879disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7880{
7881 struct breakpoint *b;
7882
7883 if (objfile == NULL)
7884 return;
7885
d03de421
PA
7886 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7887 managed by the user with add-symbol-file/remove-symbol-file.
7888 Similarly to how breakpoints in shared libraries are handled in
7889 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7890 shlib_disabled so they end up uninserted on the next global
7891 location list update. Shared libraries not loaded by the user
7892 aren't handled here -- they're already handled in
7893 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7894 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7895 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7896 main objfile). */
7897 if ((objfile->flags & OBJF_SHARED) == 0
7898 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7899 return;
7900
7901 ALL_BREAKPOINTS (b)
7902 {
7903 struct bp_location *loc;
7904 int bp_modified = 0;
7905
7906 if (!is_breakpoint (b) && !is_tracepoint (b))
7907 continue;
7908
7909 for (loc = b->loc; loc != NULL; loc = loc->next)
7910 {
7911 CORE_ADDR loc_addr = loc->address;
7912
7913 if (loc->loc_type != bp_loc_hardware_breakpoint
7914 && loc->loc_type != bp_loc_software_breakpoint)
7915 continue;
7916
7917 if (loc->shlib_disabled != 0)
7918 continue;
7919
7920 if (objfile->pspace != loc->pspace)
7921 continue;
7922
7923 if (loc->loc_type != bp_loc_hardware_breakpoint
7924 && loc->loc_type != bp_loc_software_breakpoint)
7925 continue;
7926
7927 if (is_addr_in_objfile (loc_addr, objfile))
7928 {
7929 loc->shlib_disabled = 1;
08351840
PA
7930 /* At this point, we don't know whether the object was
7931 unmapped from the inferior or not, so leave the
7932 inserted flag alone. We'll handle failure to
7933 uninsert quietly, in case the object was indeed
7934 unmapped. */
63644780
NB
7935
7936 mark_breakpoint_location_modified (loc);
7937
7938 bp_modified = 1;
7939 }
7940 }
7941
7942 if (bp_modified)
7943 observer_notify_breakpoint_modified (b);
7944 }
7945}
7946
ce78b96d
JB
7947/* FORK & VFORK catchpoints. */
7948
e29a4733
PA
7949/* An instance of this type is used to represent a fork or vfork
7950 catchpoint. It includes a "struct breakpoint" as a kind of base
7951 class; users downcast to "struct breakpoint *" when needed. A
7952 breakpoint is really of this type iff its ops pointer points to
7953 CATCH_FORK_BREAKPOINT_OPS. */
7954
7955struct fork_catchpoint
7956{
7957 /* The base class. */
7958 struct breakpoint base;
7959
7960 /* Process id of a child process whose forking triggered this
7961 catchpoint. This field is only valid immediately after this
7962 catchpoint has triggered. */
7963 ptid_t forked_inferior_pid;
7964};
7965
4a64f543
MS
7966/* Implement the "insert" breakpoint_ops method for fork
7967 catchpoints. */
ce78b96d 7968
77b06cd7
TJB
7969static int
7970insert_catch_fork (struct bp_location *bl)
ce78b96d 7971{
dfd4cc63 7972 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7973}
7974
4a64f543
MS
7975/* Implement the "remove" breakpoint_ops method for fork
7976 catchpoints. */
ce78b96d
JB
7977
7978static int
77b06cd7 7979remove_catch_fork (struct bp_location *bl)
ce78b96d 7980{
dfd4cc63 7981 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7982}
7983
7984/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7985 catchpoints. */
7986
7987static int
f1310107 7988breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7989 struct address_space *aspace, CORE_ADDR bp_addr,
7990 const struct target_waitstatus *ws)
ce78b96d 7991{
e29a4733
PA
7992 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7993
f90263c1
TT
7994 if (ws->kind != TARGET_WAITKIND_FORKED)
7995 return 0;
7996
7997 c->forked_inferior_pid = ws->value.related_pid;
7998 return 1;
ce78b96d
JB
7999}
8000
4a64f543
MS
8001/* Implement the "print_it" breakpoint_ops method for fork
8002 catchpoints. */
ce78b96d
JB
8003
8004static enum print_stop_action
348d480f 8005print_it_catch_fork (bpstat bs)
ce78b96d 8006{
36dfb11c 8007 struct ui_out *uiout = current_uiout;
348d480f
PA
8008 struct breakpoint *b = bs->breakpoint_at;
8009 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8010
ce78b96d 8011 annotate_catchpoint (b->number);
36dfb11c
TT
8012 if (b->disposition == disp_del)
8013 ui_out_text (uiout, "\nTemporary catchpoint ");
8014 else
8015 ui_out_text (uiout, "\nCatchpoint ");
8016 if (ui_out_is_mi_like_p (uiout))
8017 {
8018 ui_out_field_string (uiout, "reason",
8019 async_reason_lookup (EXEC_ASYNC_FORK));
8020 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8021 }
8022 ui_out_field_int (uiout, "bkptno", b->number);
8023 ui_out_text (uiout, " (forked process ");
8024 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8025 ui_out_text (uiout, "), ");
ce78b96d
JB
8026 return PRINT_SRC_AND_LOC;
8027}
8028
4a64f543
MS
8029/* Implement the "print_one" breakpoint_ops method for fork
8030 catchpoints. */
ce78b96d
JB
8031
8032static void
a6d9a66e 8033print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8034{
e29a4733 8035 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8036 struct value_print_options opts;
79a45e25 8037 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8038
8039 get_user_print_options (&opts);
8040
4a64f543
MS
8041 /* Field 4, the address, is omitted (which makes the columns not
8042 line up too nicely with the headers, but the effect is relatively
8043 readable). */
79a45b7d 8044 if (opts.addressprint)
ce78b96d
JB
8045 ui_out_field_skip (uiout, "addr");
8046 annotate_field (5);
8047 ui_out_text (uiout, "fork");
e29a4733 8048 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8049 {
8050 ui_out_text (uiout, ", process ");
8051 ui_out_field_int (uiout, "what",
e29a4733 8052 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8053 ui_out_spaces (uiout, 1);
8054 }
8ac3646f
TT
8055
8056 if (ui_out_is_mi_like_p (uiout))
8057 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8058}
8059
8060/* Implement the "print_mention" breakpoint_ops method for fork
8061 catchpoints. */
8062
8063static void
8064print_mention_catch_fork (struct breakpoint *b)
8065{
8066 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8067}
8068
6149aea9
PA
8069/* Implement the "print_recreate" breakpoint_ops method for fork
8070 catchpoints. */
8071
8072static void
8073print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8074{
8075 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8076 print_recreate_thread (b, fp);
6149aea9
PA
8077}
8078
ce78b96d
JB
8079/* The breakpoint_ops structure to be used in fork catchpoints. */
8080
2060206e 8081static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8082
4a64f543
MS
8083/* Implement the "insert" breakpoint_ops method for vfork
8084 catchpoints. */
ce78b96d 8085
77b06cd7
TJB
8086static int
8087insert_catch_vfork (struct bp_location *bl)
ce78b96d 8088{
dfd4cc63 8089 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8090}
8091
4a64f543
MS
8092/* Implement the "remove" breakpoint_ops method for vfork
8093 catchpoints. */
ce78b96d
JB
8094
8095static int
77b06cd7 8096remove_catch_vfork (struct bp_location *bl)
ce78b96d 8097{
dfd4cc63 8098 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8099}
8100
8101/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8102 catchpoints. */
8103
8104static int
f1310107 8105breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8106 struct address_space *aspace, CORE_ADDR bp_addr,
8107 const struct target_waitstatus *ws)
ce78b96d 8108{
e29a4733
PA
8109 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8110
f90263c1
TT
8111 if (ws->kind != TARGET_WAITKIND_VFORKED)
8112 return 0;
8113
8114 c->forked_inferior_pid = ws->value.related_pid;
8115 return 1;
ce78b96d
JB
8116}
8117
4a64f543
MS
8118/* Implement the "print_it" breakpoint_ops method for vfork
8119 catchpoints. */
ce78b96d
JB
8120
8121static enum print_stop_action
348d480f 8122print_it_catch_vfork (bpstat bs)
ce78b96d 8123{
36dfb11c 8124 struct ui_out *uiout = current_uiout;
348d480f 8125 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8126 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8127
ce78b96d 8128 annotate_catchpoint (b->number);
36dfb11c
TT
8129 if (b->disposition == disp_del)
8130 ui_out_text (uiout, "\nTemporary catchpoint ");
8131 else
8132 ui_out_text (uiout, "\nCatchpoint ");
8133 if (ui_out_is_mi_like_p (uiout))
8134 {
8135 ui_out_field_string (uiout, "reason",
8136 async_reason_lookup (EXEC_ASYNC_VFORK));
8137 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8138 }
8139 ui_out_field_int (uiout, "bkptno", b->number);
8140 ui_out_text (uiout, " (vforked process ");
8141 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8142 ui_out_text (uiout, "), ");
ce78b96d
JB
8143 return PRINT_SRC_AND_LOC;
8144}
8145
4a64f543
MS
8146/* Implement the "print_one" breakpoint_ops method for vfork
8147 catchpoints. */
ce78b96d
JB
8148
8149static void
a6d9a66e 8150print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8151{
e29a4733 8152 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8153 struct value_print_options opts;
79a45e25 8154 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8155
8156 get_user_print_options (&opts);
4a64f543
MS
8157 /* Field 4, the address, is omitted (which makes the columns not
8158 line up too nicely with the headers, but the effect is relatively
8159 readable). */
79a45b7d 8160 if (opts.addressprint)
ce78b96d
JB
8161 ui_out_field_skip (uiout, "addr");
8162 annotate_field (5);
8163 ui_out_text (uiout, "vfork");
e29a4733 8164 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8165 {
8166 ui_out_text (uiout, ", process ");
8167 ui_out_field_int (uiout, "what",
e29a4733 8168 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8169 ui_out_spaces (uiout, 1);
8170 }
8ac3646f
TT
8171
8172 if (ui_out_is_mi_like_p (uiout))
8173 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8174}
8175
8176/* Implement the "print_mention" breakpoint_ops method for vfork
8177 catchpoints. */
8178
8179static void
8180print_mention_catch_vfork (struct breakpoint *b)
8181{
8182 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8183}
8184
6149aea9
PA
8185/* Implement the "print_recreate" breakpoint_ops method for vfork
8186 catchpoints. */
8187
8188static void
8189print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8190{
8191 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8192 print_recreate_thread (b, fp);
6149aea9
PA
8193}
8194
ce78b96d
JB
8195/* The breakpoint_ops structure to be used in vfork catchpoints. */
8196
2060206e 8197static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8198
edcc5120
TT
8199/* An instance of this type is used to represent an solib catchpoint.
8200 It includes a "struct breakpoint" as a kind of base class; users
8201 downcast to "struct breakpoint *" when needed. A breakpoint is
8202 really of this type iff its ops pointer points to
8203 CATCH_SOLIB_BREAKPOINT_OPS. */
8204
8205struct solib_catchpoint
8206{
8207 /* The base class. */
8208 struct breakpoint base;
8209
8210 /* True for "catch load", false for "catch unload". */
8211 unsigned char is_load;
8212
8213 /* Regular expression to match, if any. COMPILED is only valid when
8214 REGEX is non-NULL. */
8215 char *regex;
8216 regex_t compiled;
8217};
8218
8219static void
8220dtor_catch_solib (struct breakpoint *b)
8221{
8222 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8223
8224 if (self->regex)
8225 regfree (&self->compiled);
8226 xfree (self->regex);
8227
8228 base_breakpoint_ops.dtor (b);
8229}
8230
8231static int
8232insert_catch_solib (struct bp_location *ignore)
8233{
8234 return 0;
8235}
8236
8237static int
8238remove_catch_solib (struct bp_location *ignore)
8239{
8240 return 0;
8241}
8242
8243static int
8244breakpoint_hit_catch_solib (const struct bp_location *bl,
8245 struct address_space *aspace,
8246 CORE_ADDR bp_addr,
8247 const struct target_waitstatus *ws)
8248{
8249 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8250 struct breakpoint *other;
8251
8252 if (ws->kind == TARGET_WAITKIND_LOADED)
8253 return 1;
8254
8255 ALL_BREAKPOINTS (other)
8256 {
8257 struct bp_location *other_bl;
8258
8259 if (other == bl->owner)
8260 continue;
8261
8262 if (other->type != bp_shlib_event)
8263 continue;
8264
8265 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8266 continue;
8267
8268 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8269 {
8270 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8271 return 1;
8272 }
8273 }
8274
8275 return 0;
8276}
8277
8278static void
8279check_status_catch_solib (struct bpstats *bs)
8280{
8281 struct solib_catchpoint *self
8282 = (struct solib_catchpoint *) bs->breakpoint_at;
8283 int ix;
8284
8285 if (self->is_load)
8286 {
8287 struct so_list *iter;
8288
8289 for (ix = 0;
8290 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8291 ix, iter);
8292 ++ix)
8293 {
8294 if (!self->regex
8295 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8296 return;
8297 }
8298 }
8299 else
8300 {
8301 char *iter;
8302
8303 for (ix = 0;
8304 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8305 ix, iter);
8306 ++ix)
8307 {
8308 if (!self->regex
8309 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8310 return;
8311 }
8312 }
8313
8314 bs->stop = 0;
8315 bs->print_it = print_it_noop;
8316}
8317
8318static enum print_stop_action
8319print_it_catch_solib (bpstat bs)
8320{
8321 struct breakpoint *b = bs->breakpoint_at;
8322 struct ui_out *uiout = current_uiout;
8323
8324 annotate_catchpoint (b->number);
8325 if (b->disposition == disp_del)
8326 ui_out_text (uiout, "\nTemporary catchpoint ");
8327 else
8328 ui_out_text (uiout, "\nCatchpoint ");
8329 ui_out_field_int (uiout, "bkptno", b->number);
8330 ui_out_text (uiout, "\n");
8331 if (ui_out_is_mi_like_p (uiout))
8332 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8333 print_solib_event (1);
8334 return PRINT_SRC_AND_LOC;
8335}
8336
8337static void
8338print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8339{
8340 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8341 struct value_print_options opts;
8342 struct ui_out *uiout = current_uiout;
8343 char *msg;
8344
8345 get_user_print_options (&opts);
8346 /* Field 4, the address, is omitted (which makes the columns not
8347 line up too nicely with the headers, but the effect is relatively
8348 readable). */
8349 if (opts.addressprint)
8350 {
8351 annotate_field (4);
8352 ui_out_field_skip (uiout, "addr");
8353 }
8354
8355 annotate_field (5);
8356 if (self->is_load)
8357 {
8358 if (self->regex)
8359 msg = xstrprintf (_("load of library matching %s"), self->regex);
8360 else
8361 msg = xstrdup (_("load of library"));
8362 }
8363 else
8364 {
8365 if (self->regex)
8366 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8367 else
8368 msg = xstrdup (_("unload of library"));
8369 }
8370 ui_out_field_string (uiout, "what", msg);
8371 xfree (msg);
8ac3646f
TT
8372
8373 if (ui_out_is_mi_like_p (uiout))
8374 ui_out_field_string (uiout, "catch-type",
8375 self->is_load ? "load" : "unload");
edcc5120
TT
8376}
8377
8378static void
8379print_mention_catch_solib (struct breakpoint *b)
8380{
8381 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8382
8383 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8384 self->is_load ? "load" : "unload");
8385}
8386
8387static void
8388print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8389{
8390 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8391
8392 fprintf_unfiltered (fp, "%s %s",
8393 b->disposition == disp_del ? "tcatch" : "catch",
8394 self->is_load ? "load" : "unload");
8395 if (self->regex)
8396 fprintf_unfiltered (fp, " %s", self->regex);
8397 fprintf_unfiltered (fp, "\n");
8398}
8399
8400static struct breakpoint_ops catch_solib_breakpoint_ops;
8401
91985142
MG
8402/* Shared helper function (MI and CLI) for creating and installing
8403 a shared object event catchpoint. If IS_LOAD is non-zero then
8404 the events to be caught are load events, otherwise they are
8405 unload events. If IS_TEMP is non-zero the catchpoint is a
8406 temporary one. If ENABLED is non-zero the catchpoint is
8407 created in an enabled state. */
edcc5120 8408
91985142
MG
8409void
8410add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8411{
8412 struct solib_catchpoint *c;
8413 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8414 struct cleanup *cleanup;
8415
edcc5120
TT
8416 if (!arg)
8417 arg = "";
8418 arg = skip_spaces (arg);
8419
8420 c = XCNEW (struct solib_catchpoint);
8421 cleanup = make_cleanup (xfree, c);
8422
8423 if (*arg != '\0')
8424 {
8425 int errcode;
8426
8427 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8428 if (errcode != 0)
8429 {
8430 char *err = get_regcomp_error (errcode, &c->compiled);
8431
8432 make_cleanup (xfree, err);
8433 error (_("Invalid regexp (%s): %s"), err, arg);
8434 }
8435 c->regex = xstrdup (arg);
8436 }
8437
8438 c->is_load = is_load;
91985142 8439 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8440 &catch_solib_breakpoint_ops);
8441
91985142
MG
8442 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8443
edcc5120
TT
8444 discard_cleanups (cleanup);
8445 install_breakpoint (0, &c->base, 1);
8446}
8447
91985142
MG
8448/* A helper function that does all the work for "catch load" and
8449 "catch unload". */
8450
8451static void
8452catch_load_or_unload (char *arg, int from_tty, int is_load,
8453 struct cmd_list_element *command)
8454{
8455 int tempflag;
8456 const int enabled = 1;
8457
8458 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8459
8460 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8461}
8462
edcc5120
TT
8463static void
8464catch_load_command_1 (char *arg, int from_tty,
8465 struct cmd_list_element *command)
8466{
8467 catch_load_or_unload (arg, from_tty, 1, command);
8468}
8469
8470static void
8471catch_unload_command_1 (char *arg, int from_tty,
8472 struct cmd_list_element *command)
8473{
8474 catch_load_or_unload (arg, from_tty, 0, command);
8475}
8476
346774a9
PA
8477/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8478 is non-zero, then make the breakpoint temporary. If COND_STRING is
8479 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8480 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8481
ab04a2af 8482void
346774a9
PA
8483init_catchpoint (struct breakpoint *b,
8484 struct gdbarch *gdbarch, int tempflag,
8485 char *cond_string,
c0a91b2b 8486 const struct breakpoint_ops *ops)
c906108c 8487{
c5aa993b 8488 struct symtab_and_line sal;
346774a9 8489
fe39c653 8490 init_sal (&sal);
6c95b8df 8491 sal.pspace = current_program_space;
c5aa993b 8492
28010a5d 8493 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8494
1b36a34b 8495 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8496 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8497}
8498
28010a5d 8499void
3ea46bff 8500install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8501{
8502 add_to_breakpoint_chain (b);
3a5c3e22 8503 set_breakpoint_number (internal, b);
558a9d82
YQ
8504 if (is_tracepoint (b))
8505 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8506 if (!internal)
8507 mention (b);
c56053d2 8508 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8509
8510 if (update_gll)
44702360 8511 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8512}
8513
9b70b993 8514static void
a6d9a66e
UW
8515create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8516 int tempflag, char *cond_string,
c0a91b2b 8517 const struct breakpoint_ops *ops)
c906108c 8518{
e29a4733 8519 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8520
e29a4733
PA
8521 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8522
8523 c->forked_inferior_pid = null_ptid;
8524
3ea46bff 8525 install_breakpoint (0, &c->base, 1);
c906108c
SS
8526}
8527
fe798b75
JB
8528/* Exec catchpoints. */
8529
b4d90040
PA
8530/* An instance of this type is used to represent an exec catchpoint.
8531 It includes a "struct breakpoint" as a kind of base class; users
8532 downcast to "struct breakpoint *" when needed. A breakpoint is
8533 really of this type iff its ops pointer points to
8534 CATCH_EXEC_BREAKPOINT_OPS. */
8535
8536struct exec_catchpoint
8537{
8538 /* The base class. */
8539 struct breakpoint base;
8540
8541 /* Filename of a program whose exec triggered this catchpoint.
8542 This field is only valid immediately after this catchpoint has
8543 triggered. */
8544 char *exec_pathname;
8545};
8546
8547/* Implement the "dtor" breakpoint_ops method for exec
8548 catchpoints. */
8549
8550static void
8551dtor_catch_exec (struct breakpoint *b)
8552{
8553 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8554
8555 xfree (c->exec_pathname);
348d480f 8556
2060206e 8557 base_breakpoint_ops.dtor (b);
b4d90040
PA
8558}
8559
77b06cd7
TJB
8560static int
8561insert_catch_exec (struct bp_location *bl)
c906108c 8562{
dfd4cc63 8563 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8564}
c906108c 8565
fe798b75 8566static int
77b06cd7 8567remove_catch_exec (struct bp_location *bl)
fe798b75 8568{
dfd4cc63 8569 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8570}
c906108c 8571
fe798b75 8572static int
f1310107 8573breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8574 struct address_space *aspace, CORE_ADDR bp_addr,
8575 const struct target_waitstatus *ws)
fe798b75 8576{
b4d90040
PA
8577 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8578
f90263c1
TT
8579 if (ws->kind != TARGET_WAITKIND_EXECD)
8580 return 0;
8581
8582 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8583 return 1;
fe798b75 8584}
c906108c 8585
fe798b75 8586static enum print_stop_action
348d480f 8587print_it_catch_exec (bpstat bs)
fe798b75 8588{
36dfb11c 8589 struct ui_out *uiout = current_uiout;
348d480f 8590 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8591 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8592
fe798b75 8593 annotate_catchpoint (b->number);
36dfb11c
TT
8594 if (b->disposition == disp_del)
8595 ui_out_text (uiout, "\nTemporary catchpoint ");
8596 else
8597 ui_out_text (uiout, "\nCatchpoint ");
8598 if (ui_out_is_mi_like_p (uiout))
8599 {
8600 ui_out_field_string (uiout, "reason",
8601 async_reason_lookup (EXEC_ASYNC_EXEC));
8602 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8603 }
8604 ui_out_field_int (uiout, "bkptno", b->number);
8605 ui_out_text (uiout, " (exec'd ");
8606 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8607 ui_out_text (uiout, "), ");
8608
fe798b75 8609 return PRINT_SRC_AND_LOC;
c906108c
SS
8610}
8611
fe798b75 8612static void
a6d9a66e 8613print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8614{
b4d90040 8615 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8616 struct value_print_options opts;
79a45e25 8617 struct ui_out *uiout = current_uiout;
fe798b75
JB
8618
8619 get_user_print_options (&opts);
8620
8621 /* Field 4, the address, is omitted (which makes the columns
8622 not line up too nicely with the headers, but the effect
8623 is relatively readable). */
8624 if (opts.addressprint)
8625 ui_out_field_skip (uiout, "addr");
8626 annotate_field (5);
8627 ui_out_text (uiout, "exec");
b4d90040 8628 if (c->exec_pathname != NULL)
fe798b75
JB
8629 {
8630 ui_out_text (uiout, ", program \"");
b4d90040 8631 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8632 ui_out_text (uiout, "\" ");
8633 }
8ac3646f
TT
8634
8635 if (ui_out_is_mi_like_p (uiout))
8636 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8637}
8638
8639static void
8640print_mention_catch_exec (struct breakpoint *b)
8641{
8642 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8643}
8644
6149aea9
PA
8645/* Implement the "print_recreate" breakpoint_ops method for exec
8646 catchpoints. */
8647
8648static void
8649print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8650{
8651 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8652 print_recreate_thread (b, fp);
6149aea9
PA
8653}
8654
2060206e 8655static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8656
c906108c 8657static int
fba45db2 8658hw_breakpoint_used_count (void)
c906108c 8659{
c906108c 8660 int i = 0;
f1310107
TJB
8661 struct breakpoint *b;
8662 struct bp_location *bl;
c906108c
SS
8663
8664 ALL_BREAKPOINTS (b)
c5aa993b 8665 {
d6b74ac4 8666 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8667 for (bl = b->loc; bl; bl = bl->next)
8668 {
8669 /* Special types of hardware breakpoints may use more than
8670 one register. */
348d480f 8671 i += b->ops->resources_needed (bl);
f1310107 8672 }
c5aa993b 8673 }
c906108c
SS
8674
8675 return i;
8676}
8677
a1398e0c
PA
8678/* Returns the resources B would use if it were a hardware
8679 watchpoint. */
8680
c906108c 8681static int
a1398e0c 8682hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8683{
c906108c 8684 int i = 0;
e09342b5 8685 struct bp_location *bl;
c906108c 8686
a1398e0c
PA
8687 if (!breakpoint_enabled (b))
8688 return 0;
8689
8690 for (bl = b->loc; bl; bl = bl->next)
8691 {
8692 /* Special types of hardware watchpoints may use more than
8693 one register. */
8694 i += b->ops->resources_needed (bl);
8695 }
8696
8697 return i;
8698}
8699
8700/* Returns the sum the used resources of all hardware watchpoints of
8701 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8702 the sum of the used resources of all hardware watchpoints of other
8703 types _not_ TYPE. */
8704
8705static int
8706hw_watchpoint_used_count_others (struct breakpoint *except,
8707 enum bptype type, int *other_type_used)
8708{
8709 int i = 0;
8710 struct breakpoint *b;
8711
c906108c
SS
8712 *other_type_used = 0;
8713 ALL_BREAKPOINTS (b)
e09342b5 8714 {
a1398e0c
PA
8715 if (b == except)
8716 continue;
e09342b5
TJB
8717 if (!breakpoint_enabled (b))
8718 continue;
8719
a1398e0c
PA
8720 if (b->type == type)
8721 i += hw_watchpoint_use_count (b);
8722 else if (is_hardware_watchpoint (b))
8723 *other_type_used = 1;
e09342b5
TJB
8724 }
8725
c906108c
SS
8726 return i;
8727}
8728
c906108c 8729void
fba45db2 8730disable_watchpoints_before_interactive_call_start (void)
c906108c 8731{
c5aa993b 8732 struct breakpoint *b;
c906108c
SS
8733
8734 ALL_BREAKPOINTS (b)
c5aa993b 8735 {
cc60f2e3 8736 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8737 {
b5de0fa7 8738 b->enable_state = bp_call_disabled;
44702360 8739 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8740 }
8741 }
c906108c
SS
8742}
8743
8744void
fba45db2 8745enable_watchpoints_after_interactive_call_stop (void)
c906108c 8746{
c5aa993b 8747 struct breakpoint *b;
c906108c
SS
8748
8749 ALL_BREAKPOINTS (b)
c5aa993b 8750 {
cc60f2e3 8751 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8752 {
b5de0fa7 8753 b->enable_state = bp_enabled;
44702360 8754 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8755 }
8756 }
c906108c
SS
8757}
8758
8bea4e01
UW
8759void
8760disable_breakpoints_before_startup (void)
8761{
6c95b8df 8762 current_program_space->executing_startup = 1;
44702360 8763 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8764}
8765
8766void
8767enable_breakpoints_after_startup (void)
8768{
6c95b8df 8769 current_program_space->executing_startup = 0;
f8eba3c6 8770 breakpoint_re_set ();
8bea4e01
UW
8771}
8772
7c16b83e
PA
8773/* Create a new single-step breakpoint for thread THREAD, with no
8774 locations. */
c906108c 8775
7c16b83e
PA
8776static struct breakpoint *
8777new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8778{
8779 struct breakpoint *b = XNEW (struct breakpoint);
8780
8781 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8782 &momentary_breakpoint_ops);
8783
8784 b->disposition = disp_donttouch;
8785 b->frame_id = null_frame_id;
8786
8787 b->thread = thread;
8788 gdb_assert (b->thread != 0);
8789
8790 add_to_breakpoint_chain (b);
8791
8792 return b;
8793}
8794
8795/* Set a momentary breakpoint of type TYPE at address specified by
8796 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8797 frame. */
c906108c
SS
8798
8799struct breakpoint *
a6d9a66e
UW
8800set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8801 struct frame_id frame_id, enum bptype type)
c906108c 8802{
52f0bd74 8803 struct breakpoint *b;
edb3359d 8804
193facb3
JK
8805 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8806 tail-called one. */
8807 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8808
06edf0c0 8809 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8810 b->enable_state = bp_enabled;
8811 b->disposition = disp_donttouch;
818dd999 8812 b->frame_id = frame_id;
c906108c 8813
4a64f543
MS
8814 /* If we're debugging a multi-threaded program, then we want
8815 momentary breakpoints to be active in only a single thread of
8816 control. */
39f77062
KB
8817 if (in_thread_list (inferior_ptid))
8818 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 8819
44702360 8820 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8821
c906108c
SS
8822 return b;
8823}
611c83ae 8824
06edf0c0 8825/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8826 The new breakpoint will have type TYPE, use OPS as its
8827 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8828
06edf0c0
PA
8829static struct breakpoint *
8830momentary_breakpoint_from_master (struct breakpoint *orig,
8831 enum bptype type,
a1aa2221
LM
8832 const struct breakpoint_ops *ops,
8833 int loc_enabled)
e58b0e63
PA
8834{
8835 struct breakpoint *copy;
8836
06edf0c0 8837 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8838 copy->loc = allocate_bp_location (copy);
0e30163f 8839 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8840
a6d9a66e 8841 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8842 copy->loc->requested_address = orig->loc->requested_address;
8843 copy->loc->address = orig->loc->address;
8844 copy->loc->section = orig->loc->section;
6c95b8df 8845 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8846 copy->loc->probe = orig->loc->probe;
f8eba3c6 8847 copy->loc->line_number = orig->loc->line_number;
2f202fde 8848 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8849 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8850 copy->frame_id = orig->frame_id;
8851 copy->thread = orig->thread;
6c95b8df 8852 copy->pspace = orig->pspace;
e58b0e63
PA
8853
8854 copy->enable_state = bp_enabled;
8855 copy->disposition = disp_donttouch;
8856 copy->number = internal_breakpoint_number--;
8857
44702360 8858 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8859 return copy;
8860}
8861
06edf0c0
PA
8862/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8863 ORIG is NULL. */
8864
8865struct breakpoint *
8866clone_momentary_breakpoint (struct breakpoint *orig)
8867{
8868 /* If there's nothing to clone, then return nothing. */
8869 if (orig == NULL)
8870 return NULL;
8871
a1aa2221 8872 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8873}
8874
611c83ae 8875struct breakpoint *
a6d9a66e
UW
8876set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8877 enum bptype type)
611c83ae
PA
8878{
8879 struct symtab_and_line sal;
8880
8881 sal = find_pc_line (pc, 0);
8882 sal.pc = pc;
8883 sal.section = find_pc_overlay (pc);
8884 sal.explicit_pc = 1;
8885
a6d9a66e 8886 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8887}
c906108c 8888\f
c5aa993b 8889
c906108c
SS
8890/* Tell the user we have just set a breakpoint B. */
8891
8892static void
fba45db2 8893mention (struct breakpoint *b)
c906108c 8894{
348d480f 8895 b->ops->print_mention (b);
79a45e25 8896 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 8897 return;
c906108c
SS
8898 printf_filtered ("\n");
8899}
c906108c 8900\f
c5aa993b 8901
1a853c52
PA
8902static int bp_loc_is_permanent (struct bp_location *loc);
8903
0d381245 8904static struct bp_location *
39d61571 8905add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8906 const struct symtab_and_line *sal)
8907{
8908 struct bp_location *loc, **tmp;
3742cc8b
YQ
8909 CORE_ADDR adjusted_address;
8910 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8911
8912 if (loc_gdbarch == NULL)
8913 loc_gdbarch = b->gdbarch;
8914
8915 /* Adjust the breakpoint's address prior to allocating a location.
8916 Once we call allocate_bp_location(), that mostly uninitialized
8917 location will be placed on the location chain. Adjustment of the
8918 breakpoint may cause target_read_memory() to be called and we do
8919 not want its scan of the location chain to find a breakpoint and
8920 location that's only been partially initialized. */
8921 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8922 sal->pc, b->type);
0d381245 8923
d30113d4 8924 /* Sort the locations by their ADDRESS. */
39d61571 8925 loc = allocate_bp_location (b);
d30113d4
JK
8926 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8927 tmp = &((*tmp)->next))
0d381245 8928 ;
d30113d4 8929 loc->next = *tmp;
0d381245 8930 *tmp = loc;
3742cc8b 8931
0d381245 8932 loc->requested_address = sal->pc;
3742cc8b 8933 loc->address = adjusted_address;
6c95b8df 8934 loc->pspace = sal->pspace;
729662a5
TT
8935 loc->probe.probe = sal->probe;
8936 loc->probe.objfile = sal->objfile;
6c95b8df 8937 gdb_assert (loc->pspace != NULL);
0d381245 8938 loc->section = sal->section;
3742cc8b 8939 loc->gdbarch = loc_gdbarch;
f8eba3c6 8940 loc->line_number = sal->line;
2f202fde 8941 loc->symtab = sal->symtab;
f8eba3c6 8942
0e30163f
JK
8943 set_breakpoint_location_function (loc,
8944 sal->explicit_pc || sal->explicit_line);
1a853c52 8945
6ae88661
LM
8946 /* While by definition, permanent breakpoints are already present in the
8947 code, we don't mark the location as inserted. Normally one would expect
8948 that GDB could rely on that breakpoint instruction to stop the program,
8949 thus removing the need to insert its own breakpoint, except that executing
8950 the breakpoint instruction can kill the target instead of reporting a
8951 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8952 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8953 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8954 breakpoint be inserted normally results in QEMU knowing about the GDB
8955 breakpoint, and thus trap before the breakpoint instruction is executed.
8956 (If GDB later needs to continue execution past the permanent breakpoint,
8957 it manually increments the PC, thus avoiding executing the breakpoint
8958 instruction.) */
1a853c52 8959 if (bp_loc_is_permanent (loc))
6ae88661 8960 loc->permanent = 1;
1a853c52 8961
0d381245
VP
8962 return loc;
8963}
514f746b
AR
8964\f
8965
1cf4d951 8966/* See breakpoint.h. */
514f746b 8967
1cf4d951
PA
8968int
8969program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8970{
8971 int len;
8972 CORE_ADDR addr;
1afeeb75 8973 const gdb_byte *bpoint;
514f746b 8974 gdb_byte *target_mem;
939c61fa
JK
8975 struct cleanup *cleanup;
8976 int retval = 0;
514f746b 8977
1cf4d951
PA
8978 addr = address;
8979 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8980
8981 /* Software breakpoints unsupported? */
8982 if (bpoint == NULL)
8983 return 0;
8984
8985 target_mem = alloca (len);
8986
8987 /* Enable the automatic memory restoration from breakpoints while
8988 we read the memory. Otherwise we could say about our temporary
8989 breakpoints they are permanent. */
8990 cleanup = make_show_memory_breakpoints_cleanup (0);
8991
8992 if (target_read_memory (address, target_mem, len) == 0
8993 && memcmp (target_mem, bpoint, len) == 0)
8994 retval = 1;
8995
8996 do_cleanups (cleanup);
8997
8998 return retval;
8999}
9000
9001/* Return 1 if LOC is pointing to a permanent breakpoint,
9002 return 0 otherwise. */
9003
9004static int
9005bp_loc_is_permanent (struct bp_location *loc)
9006{
9007 struct cleanup *cleanup;
9008 int retval;
9009
514f746b
AR
9010 gdb_assert (loc != NULL);
9011
244558af
LM
9012 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9013 attempt to read from the addresses the locations of these breakpoint types
9014 point to. program_breakpoint_here_p, below, will attempt to read
9015 memory. */
9016 if (!breakpoint_address_is_meaningful (loc->owner))
9017 return 0;
9018
6c95b8df 9019 cleanup = save_current_space_and_thread ();
6c95b8df 9020 switch_to_program_space_and_thread (loc->pspace);
939c61fa 9021
1cf4d951 9022 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b 9023
939c61fa
JK
9024 do_cleanups (cleanup);
9025
9026 return retval;
514f746b
AR
9027}
9028
e7e0cddf
SS
9029/* Build a command list for the dprintf corresponding to the current
9030 settings of the dprintf style options. */
9031
9032static void
9033update_dprintf_command_list (struct breakpoint *b)
9034{
9035 char *dprintf_args = b->extra_string;
9036 char *printf_line = NULL;
9037
9038 if (!dprintf_args)
9039 return;
9040
9041 dprintf_args = skip_spaces (dprintf_args);
9042
9043 /* Allow a comma, as it may have terminated a location, but don't
9044 insist on it. */
9045 if (*dprintf_args == ',')
9046 ++dprintf_args;
9047 dprintf_args = skip_spaces (dprintf_args);
9048
9049 if (*dprintf_args != '"')
9050 error (_("Bad format string, missing '\"'."));
9051
d3ce09f5 9052 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9053 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9054 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9055 {
9056 if (!dprintf_function)
9057 error (_("No function supplied for dprintf call"));
9058
9059 if (dprintf_channel && strlen (dprintf_channel) > 0)
9060 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9061 dprintf_function,
9062 dprintf_channel,
9063 dprintf_args);
9064 else
9065 printf_line = xstrprintf ("call (void) %s (%s)",
9066 dprintf_function,
9067 dprintf_args);
9068 }
d3ce09f5
SS
9069 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9070 {
9071 if (target_can_run_breakpoint_commands ())
9072 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9073 else
9074 {
9075 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9076 printf_line = xstrprintf ("printf %s", dprintf_args);
9077 }
9078 }
e7e0cddf
SS
9079 else
9080 internal_error (__FILE__, __LINE__,
9081 _("Invalid dprintf style."));
9082
f28045c2 9083 gdb_assert (printf_line != NULL);
9d6e6e84 9084 /* Manufacture a printf sequence. */
f28045c2 9085 {
8d749320 9086 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 9087
f28045c2
YQ
9088 printf_cmd_line->control_type = simple_control;
9089 printf_cmd_line->body_count = 0;
9090 printf_cmd_line->body_list = NULL;
9d6e6e84 9091 printf_cmd_line->next = NULL;
f28045c2 9092 printf_cmd_line->line = printf_line;
e7e0cddf 9093
f28045c2
YQ
9094 breakpoint_set_commands (b, printf_cmd_line);
9095 }
e7e0cddf
SS
9096}
9097
9098/* Update all dprintf commands, making their command lists reflect
9099 current style settings. */
9100
9101static void
9102update_dprintf_commands (char *args, int from_tty,
9103 struct cmd_list_element *c)
9104{
9105 struct breakpoint *b;
9106
9107 ALL_BREAKPOINTS (b)
9108 {
9109 if (b->type == bp_dprintf)
9110 update_dprintf_command_list (b);
9111 }
9112}
c3f6f71d 9113
f00aae0f
KS
9114/* Create a breakpoint with SAL as location. Use LOCATION
9115 as a description of the location, and COND_STRING
db107f19 9116 as condition expression. */
018d34a4
VP
9117
9118static void
d9b3f62e 9119init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
f00aae0f
KS
9120 struct symtabs_and_lines sals,
9121 struct event_location *location,
f8eba3c6 9122 char *filter, char *cond_string,
e7e0cddf 9123 char *extra_string,
d9b3f62e
PA
9124 enum bptype type, enum bpdisp disposition,
9125 int thread, int task, int ignore_count,
c0a91b2b 9126 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9127 int enabled, int internal, unsigned flags,
9128 int display_canonical)
018d34a4 9129{
0d381245 9130 int i;
018d34a4
VP
9131
9132 if (type == bp_hardware_breakpoint)
9133 {
fbbd034e
AS
9134 int target_resources_ok;
9135
9136 i = hw_breakpoint_used_count ();
9137 target_resources_ok =
9138 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9139 i + 1, 0);
9140 if (target_resources_ok == 0)
9141 error (_("No hardware breakpoint support in the target."));
9142 else if (target_resources_ok < 0)
9143 error (_("Hardware breakpoints used exceeds limit."));
9144 }
9145
6c95b8df
PA
9146 gdb_assert (sals.nelts > 0);
9147
0d381245
VP
9148 for (i = 0; i < sals.nelts; ++i)
9149 {
9150 struct symtab_and_line sal = sals.sals[i];
9151 struct bp_location *loc;
9152
9153 if (from_tty)
5af949e3
UW
9154 {
9155 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9156 if (!loc_gdbarch)
9157 loc_gdbarch = gdbarch;
9158
9159 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9160 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9161 }
0d381245
VP
9162
9163 if (i == 0)
9164 {
d9b3f62e 9165 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9166 b->thread = thread;
4a306c9a 9167 b->task = task;
855a6e68 9168
0d381245 9169 b->cond_string = cond_string;
e7e0cddf 9170 b->extra_string = extra_string;
0d381245 9171 b->ignore_count = ignore_count;
41447f92 9172 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9173 b->disposition = disposition;
6c95b8df 9174
44f238bb
PA
9175 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9176 b->loc->inserted = 1;
9177
0fb4aa4b
PA
9178 if (type == bp_static_tracepoint)
9179 {
d9b3f62e 9180 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9181 struct static_tracepoint_marker marker;
9182
983af33b 9183 if (strace_marker_p (b))
0fb4aa4b
PA
9184 {
9185 /* We already know the marker exists, otherwise, we
9186 wouldn't see a sal for it. */
f00aae0f
KS
9187 const char *p = &event_location_to_string (b->location)[3];
9188 const char *endp;
0fb4aa4b 9189 char *marker_str;
0fb4aa4b 9190
f00aae0f 9191 p = skip_spaces_const (p);
0fb4aa4b 9192
f00aae0f 9193 endp = skip_to_space_const (p);
0fb4aa4b
PA
9194
9195 marker_str = savestring (p, endp - p);
d9b3f62e 9196 t->static_trace_marker_id = marker_str;
0fb4aa4b 9197
3e43a32a
MS
9198 printf_filtered (_("Probed static tracepoint "
9199 "marker \"%s\"\n"),
d9b3f62e 9200 t->static_trace_marker_id);
0fb4aa4b
PA
9201 }
9202 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9203 {
d9b3f62e 9204 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9205 release_static_tracepoint_marker (&marker);
9206
3e43a32a
MS
9207 printf_filtered (_("Probed static tracepoint "
9208 "marker \"%s\"\n"),
d9b3f62e 9209 t->static_trace_marker_id);
0fb4aa4b
PA
9210 }
9211 else
3e43a32a
MS
9212 warning (_("Couldn't determine the static "
9213 "tracepoint marker to probe"));
0fb4aa4b
PA
9214 }
9215
0d381245
VP
9216 loc = b->loc;
9217 }
9218 else
018d34a4 9219 {
39d61571 9220 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9221 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9222 loc->inserted = 1;
0d381245
VP
9223 }
9224
9225 if (b->cond_string)
9226 {
bbc13ae3
KS
9227 const char *arg = b->cond_string;
9228
1bb9788d
TT
9229 loc->cond = parse_exp_1 (&arg, loc->address,
9230 block_for_pc (loc->address), 0);
0d381245 9231 if (*arg)
588ae58c 9232 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9233 }
e7e0cddf
SS
9234
9235 /* Dynamic printf requires and uses additional arguments on the
9236 command line, otherwise it's an error. */
9237 if (type == bp_dprintf)
9238 {
9239 if (b->extra_string)
9240 update_dprintf_command_list (b);
9241 else
9242 error (_("Format string required"));
9243 }
9244 else if (b->extra_string)
588ae58c 9245 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9246 }
018d34a4 9247
56435ebe 9248 b->display_canonical = display_canonical;
f00aae0f
KS
9249 if (location != NULL)
9250 b->location = location;
018d34a4 9251 else
a06efdd6 9252 b->location = new_address_location (b->loc->address);
f8eba3c6 9253 b->filter = filter;
d9b3f62e 9254}
018d34a4 9255
d9b3f62e
PA
9256static void
9257create_breakpoint_sal (struct gdbarch *gdbarch,
f00aae0f
KS
9258 struct symtabs_and_lines sals,
9259 struct event_location *location,
f8eba3c6 9260 char *filter, char *cond_string,
e7e0cddf 9261 char *extra_string,
d9b3f62e
PA
9262 enum bptype type, enum bpdisp disposition,
9263 int thread, int task, int ignore_count,
c0a91b2b 9264 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9265 int enabled, int internal, unsigned flags,
9266 int display_canonical)
d9b3f62e
PA
9267{
9268 struct breakpoint *b;
9269 struct cleanup *old_chain;
9270
9271 if (is_tracepoint_type (type))
9272 {
9273 struct tracepoint *t;
9274
9275 t = XCNEW (struct tracepoint);
9276 b = &t->base;
9277 }
9278 else
9279 b = XNEW (struct breakpoint);
9280
9281 old_chain = make_cleanup (xfree, b);
9282
9283 init_breakpoint_sal (b, gdbarch,
f00aae0f 9284 sals, location,
e7e0cddf 9285 filter, cond_string, extra_string,
d9b3f62e
PA
9286 type, disposition,
9287 thread, task, ignore_count,
9288 ops, from_tty,
44f238bb
PA
9289 enabled, internal, flags,
9290 display_canonical);
d9b3f62e
PA
9291 discard_cleanups (old_chain);
9292
3ea46bff 9293 install_breakpoint (internal, b, 0);
018d34a4
VP
9294}
9295
9296/* Add SALS.nelts breakpoints to the breakpoint table. For each
9297 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9298 value. COND_STRING, if not NULL, specified the condition to be
9299 used for all breakpoints. Essentially the only case where
9300 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9301 function. In that case, it's still not possible to specify
9302 separate conditions for different overloaded functions, so
9303 we take just a single condition string.
9304
c3f6f71d 9305 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9306 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9307 array contents). If the function fails (error() is called), the
9308 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9309 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9310
9311static void
8cdf0e15 9312create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9313 struct linespec_result *canonical,
e7e0cddf 9314 char *cond_string, char *extra_string,
8cdf0e15
VP
9315 enum bptype type, enum bpdisp disposition,
9316 int thread, int task, int ignore_count,
c0a91b2b 9317 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9318 int enabled, int internal, unsigned flags)
c906108c 9319{
018d34a4 9320 int i;
f8eba3c6 9321 struct linespec_sals *lsal;
cc59ec59 9322
f8eba3c6
TT
9323 if (canonical->pre_expanded)
9324 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9325
9326 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9327 {
f00aae0f 9328 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9329 'break', without arguments. */
f00aae0f
KS
9330 struct event_location *location
9331 = (canonical->location != NULL
9332 ? copy_event_location (canonical->location) : NULL);
f8eba3c6 9333 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
f00aae0f 9334 struct cleanup *inner = make_cleanup_delete_event_location (location);
0d381245 9335
f8eba3c6
TT
9336 make_cleanup (xfree, filter_string);
9337 create_breakpoint_sal (gdbarch, lsal->sals,
f00aae0f 9338 location,
f8eba3c6 9339 filter_string,
e7e0cddf
SS
9340 cond_string, extra_string,
9341 type, disposition,
84f4c1fe 9342 thread, task, ignore_count, ops,
44f238bb 9343 from_tty, enabled, internal, flags,
56435ebe 9344 canonical->special_display);
f8eba3c6 9345 discard_cleanups (inner);
c3f6f71d 9346 }
c3f6f71d 9347}
c906108c 9348
f00aae0f 9349/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9350 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9351 addresses found. LOCATION points to the end of the SAL (for
9352 linespec locations).
9998af43
TJB
9353
9354 The array and the line spec strings are allocated on the heap, it is
9355 the caller's responsibility to free them. */
c906108c 9356
b9362cc7 9357static void
f00aae0f 9358parse_breakpoint_sals (const struct event_location *location,
58438ac1 9359 struct linespec_result *canonical)
c3f6f71d 9360{
f00aae0f
KS
9361 struct symtab_and_line cursal;
9362
9363 if (event_location_type (location) == LINESPEC_LOCATION)
9364 {
9365 const char *address = get_linespec_location (location);
9366
9367 if (address == NULL)
9368 {
9369 /* The last displayed codepoint, if it's valid, is our default
9370 breakpoint address. */
9371 if (last_displayed_sal_is_valid ())
9372 {
9373 struct linespec_sals lsal;
9374 struct symtab_and_line sal;
9375 CORE_ADDR pc;
9376
9377 init_sal (&sal); /* Initialize to zeroes. */
8d749320 9378 lsal.sals.sals = XNEW (struct symtab_and_line);
f00aae0f
KS
9379
9380 /* Set sal's pspace, pc, symtab, and line to the values
9381 corresponding to the last call to print_frame_info.
9382 Be sure to reinitialize LINE with NOTCURRENT == 0
9383 as the breakpoint line number is inappropriate otherwise.
9384 find_pc_line would adjust PC, re-set it back. */
9385 get_last_displayed_sal (&sal);
9386 pc = sal.pc;
9387 sal = find_pc_line (pc, 0);
9388
9389 /* "break" without arguments is equivalent to "break *PC"
9390 where PC is the last displayed codepoint's address. So
9391 make sure to set sal.explicit_pc to prevent GDB from
9392 trying to expand the list of sals to include all other
9393 instances with the same symtab and line. */
9394 sal.pc = pc;
9395 sal.explicit_pc = 1;
9396
9397 lsal.sals.sals[0] = sal;
9398 lsal.sals.nelts = 1;
9399 lsal.canonical = NULL;
9400
9401 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9402 return;
9403 }
9404 else
9405 error (_("No default breakpoint address now."));
c906108c 9406 }
c906108c 9407 }
f00aae0f
KS
9408
9409 /* Force almost all breakpoints to be in terms of the
9410 current_source_symtab (which is decode_line_1's default).
9411 This should produce the results we want almost all of the
9412 time while leaving default_breakpoint_* alone.
9413
9414 ObjC: However, don't match an Objective-C method name which
9415 may have a '+' or '-' succeeded by a '['. */
9416 cursal = get_current_source_symtab_and_line ();
9417 if (last_displayed_sal_is_valid ())
c906108c 9418 {
f00aae0f 9419 const char *address = NULL;
cc80f267 9420
f00aae0f
KS
9421 if (event_location_type (location) == LINESPEC_LOCATION)
9422 address = get_linespec_location (location);
cc80f267 9423
f00aae0f
KS
9424 if (!cursal.symtab
9425 || (address != NULL
9426 && strchr ("+-", address[0]) != NULL
9427 && address[1] != '['))
9428 {
9429 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
9430 get_last_displayed_symtab (),
9431 get_last_displayed_line (),
9432 canonical, NULL, NULL);
9433 return;
9434 }
c906108c 9435 }
f00aae0f
KS
9436
9437 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
9438 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9439}
c906108c 9440
c906108c 9441
c3f6f71d 9442/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9443 inserted as a breakpoint. If it can't throw an error. */
c906108c 9444
b9362cc7 9445static void
23e7acfb 9446breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9447{
9448 int i;
cc59ec59 9449
c3f6f71d 9450 for (i = 0; i < sals->nelts; i++)
ee53e872 9451 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9452}
9453
7a697b8d
SS
9454/* Fast tracepoints may have restrictions on valid locations. For
9455 instance, a fast tracepoint using a jump instead of a trap will
9456 likely have to overwrite more bytes than a trap would, and so can
9457 only be placed where the instruction is longer than the jump, or a
9458 multi-instruction sequence does not have a jump into the middle of
9459 it, etc. */
9460
9461static void
9462check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9463 struct symtabs_and_lines *sals)
9464{
9465 int i, rslt;
9466 struct symtab_and_line *sal;
9467 char *msg;
9468 struct cleanup *old_chain;
9469
9470 for (i = 0; i < sals->nelts; i++)
9471 {
f8eba3c6
TT
9472 struct gdbarch *sarch;
9473
7a697b8d
SS
9474 sal = &sals->sals[i];
9475
f8eba3c6
TT
9476 sarch = get_sal_arch (*sal);
9477 /* We fall back to GDBARCH if there is no architecture
9478 associated with SAL. */
9479 if (sarch == NULL)
9480 sarch = gdbarch;
6b940e6a 9481 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
7a697b8d
SS
9482 old_chain = make_cleanup (xfree, msg);
9483
9484 if (!rslt)
9485 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9486 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9487
9488 do_cleanups (old_chain);
9489 }
9490}
9491
af4908ba
KS
9492/* Issue an invalid thread ID error. */
9493
9494static void ATTRIBUTE_NORETURN
9495invalid_thread_id_error (int id)
9496{
9497 error (_("Unknown thread %d."), id);
9498}
9499
018d34a4
VP
9500/* Given TOK, a string specification of condition and thread, as
9501 accepted by the 'break' command, extract the condition
9502 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9503 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9504 If no condition is found, *COND_STRING is set to NULL.
9505 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9506
9507static void
bbc13ae3 9508find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9509 char **cond_string, int *thread, int *task,
9510 char **rest)
018d34a4
VP
9511{
9512 *cond_string = NULL;
9513 *thread = -1;
ed1d1739
KS
9514 *task = 0;
9515 *rest = NULL;
9516
018d34a4
VP
9517 while (tok && *tok)
9518 {
bbc13ae3 9519 const char *end_tok;
018d34a4 9520 int toklen;
bbc13ae3
KS
9521 const char *cond_start = NULL;
9522 const char *cond_end = NULL;
cc59ec59 9523
bbc13ae3 9524 tok = skip_spaces_const (tok);
e7e0cddf
SS
9525
9526 if ((*tok == '"' || *tok == ',') && rest)
9527 {
9528 *rest = savestring (tok, strlen (tok));
9529 return;
9530 }
9531
bbc13ae3 9532 end_tok = skip_to_space_const (tok);
d634f2de 9533
018d34a4 9534 toklen = end_tok - tok;
d634f2de 9535
018d34a4
VP
9536 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9537 {
f7545552
TT
9538 struct expression *expr;
9539
018d34a4 9540 tok = cond_start = end_tok + 1;
1bb9788d 9541 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9542 xfree (expr);
018d34a4 9543 cond_end = tok;
d634f2de 9544 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9545 }
9546 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9547 {
9548 char *tmptok;
d634f2de 9549
018d34a4 9550 tok = end_tok + 1;
bbc13ae3 9551 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9552 if (tok == tmptok)
9553 error (_("Junk after thread keyword."));
9554 if (!valid_thread_id (*thread))
af4908ba 9555 invalid_thread_id_error (*thread);
bbc13ae3 9556 tok = tmptok;
018d34a4 9557 }
4a306c9a
JB
9558 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9559 {
9560 char *tmptok;
9561
9562 tok = end_tok + 1;
bbc13ae3 9563 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9564 if (tok == tmptok)
9565 error (_("Junk after task keyword."));
9566 if (!valid_task_id (*task))
b6199126 9567 error (_("Unknown task %d."), *task);
bbc13ae3 9568 tok = tmptok;
4a306c9a 9569 }
e7e0cddf
SS
9570 else if (rest)
9571 {
9572 *rest = savestring (tok, strlen (tok));
ccab2054 9573 return;
e7e0cddf 9574 }
018d34a4
VP
9575 else
9576 error (_("Junk at end of arguments."));
9577 }
9578}
9579
0fb4aa4b
PA
9580/* Decode a static tracepoint marker spec. */
9581
9582static struct symtabs_and_lines
f00aae0f 9583decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9584{
9585 VEC(static_tracepoint_marker_p) *markers = NULL;
9586 struct symtabs_and_lines sals;
0fb4aa4b 9587 struct cleanup *old_chain;
f00aae0f
KS
9588 const char *p = &(*arg_p)[3];
9589 const char *endp;
0fb4aa4b
PA
9590 char *marker_str;
9591 int i;
9592
f00aae0f 9593 p = skip_spaces_const (p);
0fb4aa4b 9594
f00aae0f 9595 endp = skip_to_space_const (p);
0fb4aa4b
PA
9596
9597 marker_str = savestring (p, endp - p);
9598 old_chain = make_cleanup (xfree, marker_str);
9599
9600 markers = target_static_tracepoint_markers_by_strid (marker_str);
9601 if (VEC_empty(static_tracepoint_marker_p, markers))
9602 error (_("No known static tracepoint marker named %s"), marker_str);
9603
9604 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8d749320 9605 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
0fb4aa4b
PA
9606
9607 for (i = 0; i < sals.nelts; i++)
9608 {
9609 struct static_tracepoint_marker *marker;
9610
9611 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9612
9613 init_sal (&sals.sals[i]);
9614
9615 sals.sals[i] = find_pc_line (marker->address, 0);
9616 sals.sals[i].pc = marker->address;
9617
9618 release_static_tracepoint_marker (marker);
9619 }
9620
9621 do_cleanups (old_chain);
9622
9623 *arg_p = endp;
9624 return sals;
9625}
9626
f00aae0f 9627/* See breakpoint.h. */
0101ce28 9628
8cdf0e15
VP
9629int
9630create_breakpoint (struct gdbarch *gdbarch,
f00aae0f 9631 const struct event_location *location, char *cond_string,
e7e0cddf 9632 int thread, char *extra_string,
f00aae0f 9633 int parse_extra,
0fb4aa4b 9634 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9635 int ignore_count,
9636 enum auto_boolean pending_break_support,
c0a91b2b 9637 const struct breakpoint_ops *ops,
44f238bb
PA
9638 int from_tty, int enabled, int internal,
9639 unsigned flags)
c3f6f71d 9640{
7efd8fc2 9641 struct linespec_result canonical;
c3f6f71d 9642 struct cleanup *old_chain;
80c99de1 9643 struct cleanup *bkpt_chain = NULL;
0101ce28 9644 int pending = 0;
4a306c9a 9645 int task = 0;
86b17b60 9646 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9647
348d480f
PA
9648 gdb_assert (ops != NULL);
9649
f00aae0f
KS
9650 /* If extra_string isn't useful, set it to NULL. */
9651 if (extra_string != NULL && *extra_string == '\0')
9652 extra_string = NULL;
9653
7efd8fc2 9654 init_linespec_result (&canonical);
c3f6f71d 9655
492d29ea 9656 TRY
b78a6381 9657 {
f00aae0f 9658 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9659 }
492d29ea 9660 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9661 {
492d29ea
PA
9662 /* If caller is interested in rc value from parse, set
9663 value. */
9664 if (e.error == NOT_FOUND_ERROR)
0101ce28 9665 {
05ff989b
AC
9666 /* If pending breakpoint support is turned off, throw
9667 error. */
fa8d40ab
JJ
9668
9669 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9670 throw_exception (e);
9671
9672 exception_print (gdb_stderr, e);
fa8d40ab 9673
05ff989b
AC
9674 /* If pending breakpoint support is auto query and the user
9675 selects no, then simply return the error code. */
059fb39f 9676 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9677 && !nquery (_("Make %s pending on future shared library load? "),
9678 bptype_string (type_wanted)))
fd9b8c24 9679 return 0;
fa8d40ab 9680
05ff989b
AC
9681 /* At this point, either the user was queried about setting
9682 a pending breakpoint and selected yes, or pending
9683 breakpoint behavior is on and thus a pending breakpoint
9684 is defaulted on behalf of the user. */
f00aae0f 9685 pending = 1;
0101ce28 9686 }
492d29ea
PA
9687 else
9688 throw_exception (e);
0101ce28 9689 }
492d29ea
PA
9690 END_CATCH
9691
f00aae0f 9692 if (!pending && VEC_empty (linespec_sals, canonical.sals))
492d29ea 9693 return 0;
c3f6f71d 9694
4a64f543 9695 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9696 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9697
c3f6f71d
JM
9698 /* ----------------------------- SNIP -----------------------------
9699 Anything added to the cleanup chain beyond this point is assumed
9700 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9701 then the memory is not reclaimed. */
9702 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9703
c3f6f71d
JM
9704 /* Resolve all line numbers to PC's and verify that the addresses
9705 are ok for the target. */
0101ce28 9706 if (!pending)
f8eba3c6
TT
9707 {
9708 int ix;
9709 struct linespec_sals *iter;
9710
9711 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9712 breakpoint_sals_to_pc (&iter->sals);
9713 }
c3f6f71d 9714
7a697b8d 9715 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9716 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9717 {
9718 int ix;
9719 struct linespec_sals *iter;
9720
9721 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9722 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9723 }
7a697b8d 9724
c3f6f71d
JM
9725 /* Verify that condition can be parsed, before setting any
9726 breakpoints. Allocate a separate condition expression for each
4a64f543 9727 breakpoint. */
0101ce28 9728 if (!pending)
c3f6f71d 9729 {
f00aae0f 9730 if (parse_extra)
72b2ff0e 9731 {
0878d0fa 9732 char *rest;
52d361e1
YQ
9733 struct linespec_sals *lsal;
9734
9735 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9736
0878d0fa
YQ
9737 /* Here we only parse 'arg' to separate condition
9738 from thread number, so parsing in context of first
9739 sal is OK. When setting the breakpoint we'll
9740 re-parse it in context of each sal. */
9741
f00aae0f
KS
9742 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9743 &cond_string, &thread, &task, &rest);
0878d0fa
YQ
9744 if (cond_string)
9745 make_cleanup (xfree, cond_string);
9746 if (rest)
9747 make_cleanup (xfree, rest);
9748 if (rest)
9749 extra_string = rest;
f00aae0f
KS
9750 else
9751 extra_string = NULL;
72b2ff0e 9752 }
2f069f6f 9753 else
72b2ff0e 9754 {
f00aae0f
KS
9755 if (type_wanted != bp_dprintf
9756 && extra_string != NULL && *extra_string != '\0')
9757 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9758
9759 /* Create a private copy of condition string. */
9760 if (cond_string)
9761 {
9762 cond_string = xstrdup (cond_string);
9763 make_cleanup (xfree, cond_string);
9764 }
9765 /* Create a private copy of any extra string. */
9766 if (extra_string)
9767 {
9768 extra_string = xstrdup (extra_string);
9769 make_cleanup (xfree, extra_string);
9770 }
72b2ff0e 9771 }
0fb4aa4b 9772
52d361e1 9773 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9774 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9775 tempflag ? disp_del : disp_donttouch,
9776 thread, task, ignore_count, ops,
44f238bb 9777 from_tty, enabled, internal, flags);
c906108c 9778 }
0101ce28
JJ
9779 else
9780 {
0101ce28
JJ
9781 struct breakpoint *b;
9782
bfccc43c
YQ
9783 if (is_tracepoint_type (type_wanted))
9784 {
9785 struct tracepoint *t;
9786
9787 t = XCNEW (struct tracepoint);
9788 b = &t->base;
9789 }
9790 else
9791 b = XNEW (struct breakpoint);
9792
9793 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
f00aae0f 9794 b->location = copy_event_location (location);
bfccc43c 9795
f00aae0f
KS
9796 if (parse_extra)
9797 b->cond_string = NULL;
e12c7713
MK
9798 else
9799 {
9800 /* Create a private copy of condition string. */
9801 if (cond_string)
9802 {
9803 cond_string = xstrdup (cond_string);
9804 make_cleanup (xfree, cond_string);
9805 }
9806 b->cond_string = cond_string;
15630549 9807 b->thread = thread;
e12c7713 9808 }
f00aae0f
KS
9809
9810 /* Create a private copy of any extra string. */
9811 if (extra_string != NULL)
9812 {
9813 extra_string = xstrdup (extra_string);
9814 make_cleanup (xfree, extra_string);
9815 }
9816 b->extra_string = extra_string;
0101ce28 9817 b->ignore_count = ignore_count;
0101ce28 9818 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9819 b->condition_not_parsed = 1;
41447f92 9820 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9821 if ((type_wanted != bp_breakpoint
9822 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9823 b->pspace = current_program_space;
8bea4e01 9824
bfccc43c 9825 install_breakpoint (internal, b, 0);
0101ce28
JJ
9826 }
9827
f8eba3c6 9828 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9829 {
3e43a32a
MS
9830 warning (_("Multiple breakpoints were set.\nUse the "
9831 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9832 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9833 }
9834
80c99de1
PA
9835 /* That's it. Discard the cleanups for data inserted into the
9836 breakpoint. */
9837 discard_cleanups (bkpt_chain);
9838 /* But cleanup everything else. */
c3f6f71d 9839 do_cleanups (old_chain);
217dc9e2 9840
80c99de1 9841 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9842 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9843
9844 return 1;
c3f6f71d 9845}
c906108c 9846
348d480f 9847/* Set a breakpoint.
72b2ff0e
VP
9848 ARG is a string describing breakpoint address,
9849 condition, and thread.
9850 FLAG specifies if a breakpoint is hardware on,
9851 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9852 and BP_TEMPFLAG. */
348d480f 9853
98deb0da 9854static void
72b2ff0e 9855break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9856{
72b2ff0e 9857 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9858 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9859 ? bp_hardware_breakpoint
9860 : bp_breakpoint);
55aa24fb 9861 struct breakpoint_ops *ops;
f00aae0f
KS
9862 struct event_location *location;
9863 struct cleanup *cleanup;
9864
9865 location = string_to_event_location (&arg, current_language);
9866 cleanup = make_cleanup_delete_event_location (location);
55aa24fb
SDJ
9867
9868 /* Matching breakpoints on probes. */
5b56227b
KS
9869 if (location != NULL
9870 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
9871 ops = &bkpt_probe_breakpoint_ops;
9872 else
9873 ops = &bkpt_breakpoint_ops;
c3f6f71d 9874
8cdf0e15 9875 create_breakpoint (get_current_arch (),
f00aae0f
KS
9876 location,
9877 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9878 tempflag, type_wanted,
8cdf0e15
VP
9879 0 /* Ignore count */,
9880 pending_break_support,
55aa24fb 9881 ops,
8cdf0e15 9882 from_tty,
84f4c1fe 9883 1 /* enabled */,
44f238bb
PA
9884 0 /* internal */,
9885 0);
f00aae0f 9886 do_cleanups (cleanup);
c906108c
SS
9887}
9888
c906108c
SS
9889/* Helper function for break_command_1 and disassemble_command. */
9890
9891void
fba45db2 9892resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9893{
9894 CORE_ADDR pc;
9895
9896 if (sal->pc == 0 && sal->symtab != NULL)
9897 {
9898 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9899 error (_("No line %d in file \"%s\"."),
05cba821 9900 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9901 sal->pc = pc;
6a048695 9902
4a64f543
MS
9903 /* If this SAL corresponds to a breakpoint inserted using a line
9904 number, then skip the function prologue if necessary. */
6a048695 9905 if (sal->explicit_line)
059acae7 9906 skip_prologue_sal (sal);
c906108c
SS
9907 }
9908
9909 if (sal->section == 0 && sal->symtab != NULL)
9910 {
346d1dfe 9911 const struct blockvector *bv;
3977b71f 9912 const struct block *b;
c5aa993b 9913 struct symbol *sym;
c906108c 9914
43f3e411
DE
9915 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9916 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9917 if (bv != NULL)
9918 {
7f0df278 9919 sym = block_linkage_function (b);
c906108c
SS
9920 if (sym != NULL)
9921 {
eb822aa6
DE
9922 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9923 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9924 sym);
c906108c
SS
9925 }
9926 else
9927 {
4a64f543
MS
9928 /* It really is worthwhile to have the section, so we'll
9929 just have to look harder. This case can be executed
9930 if we have line numbers but no functions (as can
9931 happen in assembly source). */
c906108c 9932
7cbd4a93 9933 struct bound_minimal_symbol msym;
6c95b8df
PA
9934 struct cleanup *old_chain = save_current_space_and_thread ();
9935
9936 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
9937
9938 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9939 if (msym.minsym)
efd66ac6 9940 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
9941
9942 do_cleanups (old_chain);
c906108c
SS
9943 }
9944 }
9945 }
9946}
9947
9948void
fba45db2 9949break_command (char *arg, int from_tty)
c906108c 9950{
db107f19 9951 break_command_1 (arg, 0, from_tty);
c906108c
SS
9952}
9953
c906108c 9954void
fba45db2 9955tbreak_command (char *arg, int from_tty)
c906108c 9956{
db107f19 9957 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9958}
9959
c906108c 9960static void
fba45db2 9961hbreak_command (char *arg, int from_tty)
c906108c 9962{
db107f19 9963 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9964}
9965
9966static void
fba45db2 9967thbreak_command (char *arg, int from_tty)
c906108c 9968{
db107f19 9969 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9970}
9971
9972static void
fba45db2 9973stop_command (char *arg, int from_tty)
c906108c 9974{
a3f17187 9975 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9976Usage: stop in <function | address>\n\
a3f17187 9977 stop at <line>\n"));
c906108c
SS
9978}
9979
9980static void
fba45db2 9981stopin_command (char *arg, int from_tty)
c906108c
SS
9982{
9983 int badInput = 0;
9984
c5aa993b 9985 if (arg == (char *) NULL)
c906108c
SS
9986 badInput = 1;
9987 else if (*arg != '*')
9988 {
9989 char *argptr = arg;
9990 int hasColon = 0;
9991
4a64f543 9992 /* Look for a ':'. If this is a line number specification, then
53a5351d 9993 say it is bad, otherwise, it should be an address or
4a64f543 9994 function/method name. */
c906108c 9995 while (*argptr && !hasColon)
c5aa993b
JM
9996 {
9997 hasColon = (*argptr == ':');
9998 argptr++;
9999 }
c906108c
SS
10000
10001 if (hasColon)
c5aa993b 10002 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10003 else
c5aa993b 10004 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10005 }
10006
10007 if (badInput)
a3f17187 10008 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10009 else
db107f19 10010 break_command_1 (arg, 0, from_tty);
c906108c
SS
10011}
10012
10013static void
fba45db2 10014stopat_command (char *arg, int from_tty)
c906108c
SS
10015{
10016 int badInput = 0;
10017
c5aa993b 10018 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10019 badInput = 1;
10020 else
10021 {
10022 char *argptr = arg;
10023 int hasColon = 0;
10024
4a64f543
MS
10025 /* Look for a ':'. If there is a '::' then get out, otherwise
10026 it is probably a line number. */
c906108c 10027 while (*argptr && !hasColon)
c5aa993b
JM
10028 {
10029 hasColon = (*argptr == ':');
10030 argptr++;
10031 }
c906108c
SS
10032
10033 if (hasColon)
c5aa993b 10034 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10035 else
c5aa993b 10036 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10037 }
10038
10039 if (badInput)
a3f17187 10040 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10041 else
db107f19 10042 break_command_1 (arg, 0, from_tty);
c906108c
SS
10043}
10044
e7e0cddf
SS
10045/* The dynamic printf command is mostly like a regular breakpoint, but
10046 with a prewired command list consisting of a single output command,
10047 built from extra arguments supplied on the dprintf command
10048 line. */
10049
da821c7b 10050static void
e7e0cddf
SS
10051dprintf_command (char *arg, int from_tty)
10052{
f00aae0f
KS
10053 struct event_location *location;
10054 struct cleanup *cleanup;
10055
10056 location = string_to_event_location (&arg, current_language);
10057 cleanup = make_cleanup_delete_event_location (location);
10058
10059 /* If non-NULL, ARG should have been advanced past the location;
10060 the next character must be ','. */
10061 if (arg != NULL)
10062 {
10063 if (arg[0] != ',' || arg[1] == '\0')
10064 error (_("Format string required"));
10065 else
10066 {
10067 /* Skip the comma. */
10068 ++arg;
10069 }
10070 }
10071
e7e0cddf 10072 create_breakpoint (get_current_arch (),
f00aae0f
KS
10073 location,
10074 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
10075 0, bp_dprintf,
10076 0 /* Ignore count */,
10077 pending_break_support,
10078 &dprintf_breakpoint_ops,
10079 from_tty,
10080 1 /* enabled */,
10081 0 /* internal */,
10082 0);
f00aae0f 10083 do_cleanups (cleanup);
e7e0cddf
SS
10084}
10085
d3ce09f5
SS
10086static void
10087agent_printf_command (char *arg, int from_tty)
10088{
10089 error (_("May only run agent-printf on the target"));
10090}
10091
f1310107
TJB
10092/* Implement the "breakpoint_hit" breakpoint_ops method for
10093 ranged breakpoints. */
10094
10095static int
10096breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10097 struct address_space *aspace,
09ac7c10
TT
10098 CORE_ADDR bp_addr,
10099 const struct target_waitstatus *ws)
f1310107 10100{
09ac7c10 10101 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10102 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10103 return 0;
10104
f1310107
TJB
10105 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10106 bl->length, aspace, bp_addr);
10107}
10108
10109/* Implement the "resources_needed" breakpoint_ops method for
10110 ranged breakpoints. */
10111
10112static int
10113resources_needed_ranged_breakpoint (const struct bp_location *bl)
10114{
10115 return target_ranged_break_num_registers ();
10116}
10117
10118/* Implement the "print_it" breakpoint_ops method for
10119 ranged breakpoints. */
10120
10121static enum print_stop_action
348d480f 10122print_it_ranged_breakpoint (bpstat bs)
f1310107 10123{
348d480f 10124 struct breakpoint *b = bs->breakpoint_at;
f1310107 10125 struct bp_location *bl = b->loc;
79a45e25 10126 struct ui_out *uiout = current_uiout;
f1310107
TJB
10127
10128 gdb_assert (b->type == bp_hardware_breakpoint);
10129
10130 /* Ranged breakpoints have only one location. */
10131 gdb_assert (bl && bl->next == NULL);
10132
10133 annotate_breakpoint (b->number);
10134 if (b->disposition == disp_del)
10135 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10136 else
10137 ui_out_text (uiout, "\nRanged breakpoint ");
10138 if (ui_out_is_mi_like_p (uiout))
10139 {
10140 ui_out_field_string (uiout, "reason",
10141 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10142 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10143 }
10144 ui_out_field_int (uiout, "bkptno", b->number);
10145 ui_out_text (uiout, ", ");
10146
10147 return PRINT_SRC_AND_LOC;
10148}
10149
10150/* Implement the "print_one" breakpoint_ops method for
10151 ranged breakpoints. */
10152
10153static void
10154print_one_ranged_breakpoint (struct breakpoint *b,
10155 struct bp_location **last_loc)
10156{
10157 struct bp_location *bl = b->loc;
10158 struct value_print_options opts;
79a45e25 10159 struct ui_out *uiout = current_uiout;
f1310107
TJB
10160
10161 /* Ranged breakpoints have only one location. */
10162 gdb_assert (bl && bl->next == NULL);
10163
10164 get_user_print_options (&opts);
10165
10166 if (opts.addressprint)
10167 /* We don't print the address range here, it will be printed later
10168 by print_one_detail_ranged_breakpoint. */
10169 ui_out_field_skip (uiout, "addr");
10170 annotate_field (5);
10171 print_breakpoint_location (b, bl);
10172 *last_loc = bl;
10173}
10174
10175/* Implement the "print_one_detail" breakpoint_ops method for
10176 ranged breakpoints. */
10177
10178static void
10179print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10180 struct ui_out *uiout)
10181{
10182 CORE_ADDR address_start, address_end;
10183 struct bp_location *bl = b->loc;
f99d8bf4
PA
10184 struct ui_file *stb = mem_fileopen ();
10185 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10186
10187 gdb_assert (bl);
10188
10189 address_start = bl->address;
10190 address_end = address_start + bl->length - 1;
10191
10192 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10193 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10194 print_core_address (bl->gdbarch, address_start),
10195 print_core_address (bl->gdbarch, address_end));
10196 ui_out_field_stream (uiout, "addr", stb);
10197 ui_out_text (uiout, "\n");
10198
10199 do_cleanups (cleanup);
10200}
10201
10202/* Implement the "print_mention" breakpoint_ops method for
10203 ranged breakpoints. */
10204
10205static void
10206print_mention_ranged_breakpoint (struct breakpoint *b)
10207{
10208 struct bp_location *bl = b->loc;
79a45e25 10209 struct ui_out *uiout = current_uiout;
f1310107
TJB
10210
10211 gdb_assert (bl);
10212 gdb_assert (b->type == bp_hardware_breakpoint);
10213
10214 if (ui_out_is_mi_like_p (uiout))
10215 return;
10216
10217 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10218 b->number, paddress (bl->gdbarch, bl->address),
10219 paddress (bl->gdbarch, bl->address + bl->length - 1));
10220}
10221
10222/* Implement the "print_recreate" breakpoint_ops method for
10223 ranged breakpoints. */
10224
10225static void
10226print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10227{
f00aae0f
KS
10228 fprintf_unfiltered (fp, "break-range %s, %s",
10229 event_location_to_string (b->location),
10230 event_location_to_string (b->location_range_end));
d9b3f62e 10231 print_recreate_thread (b, fp);
f1310107
TJB
10232}
10233
10234/* The breakpoint_ops structure to be used in ranged breakpoints. */
10235
2060206e 10236static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10237
10238/* Find the address where the end of the breakpoint range should be
10239 placed, given the SAL of the end of the range. This is so that if
10240 the user provides a line number, the end of the range is set to the
10241 last instruction of the given line. */
10242
10243static CORE_ADDR
10244find_breakpoint_range_end (struct symtab_and_line sal)
10245{
10246 CORE_ADDR end;
10247
10248 /* If the user provided a PC value, use it. Otherwise,
10249 find the address of the end of the given location. */
10250 if (sal.explicit_pc)
10251 end = sal.pc;
10252 else
10253 {
10254 int ret;
10255 CORE_ADDR start;
10256
10257 ret = find_line_pc_range (sal, &start, &end);
10258 if (!ret)
10259 error (_("Could not find location of the end of the range."));
10260
10261 /* find_line_pc_range returns the start of the next line. */
10262 end--;
10263 }
10264
10265 return end;
10266}
10267
10268/* Implement the "break-range" CLI command. */
10269
10270static void
10271break_range_command (char *arg, int from_tty)
10272{
10273 char *arg_start, *addr_string_start, *addr_string_end;
10274 struct linespec_result canonical_start, canonical_end;
10275 int bp_count, can_use_bp, length;
10276 CORE_ADDR end;
10277 struct breakpoint *b;
10278 struct symtab_and_line sal_start, sal_end;
f1310107 10279 struct cleanup *cleanup_bkpt;
f8eba3c6 10280 struct linespec_sals *lsal_start, *lsal_end;
f00aae0f 10281 struct event_location *start_location, *end_location;
f1310107
TJB
10282
10283 /* We don't support software ranged breakpoints. */
10284 if (target_ranged_break_num_registers () < 0)
10285 error (_("This target does not support hardware ranged breakpoints."));
10286
10287 bp_count = hw_breakpoint_used_count ();
10288 bp_count += target_ranged_break_num_registers ();
10289 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10290 bp_count, 0);
10291 if (can_use_bp < 0)
10292 error (_("Hardware breakpoints used exceeds limit."));
10293
f8eba3c6 10294 arg = skip_spaces (arg);
f1310107
TJB
10295 if (arg == NULL || arg[0] == '\0')
10296 error(_("No address range specified."));
10297
f1310107
TJB
10298 init_linespec_result (&canonical_start);
10299
f8eba3c6 10300 arg_start = arg;
f00aae0f
KS
10301 start_location = string_to_event_location (&arg, current_language);
10302 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10303 parse_breakpoint_sals (start_location, &canonical_start);
10304 make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10305
10306 if (arg[0] != ',')
10307 error (_("Too few arguments."));
f8eba3c6 10308 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10309 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10310
10311 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10312
10313 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10314 || lsal_start->sals.nelts != 1)
f1310107
TJB
10315 error (_("Cannot create a ranged breakpoint with multiple locations."));
10316
f8eba3c6
TT
10317 sal_start = lsal_start->sals.sals[0];
10318 addr_string_start = savestring (arg_start, arg - arg_start);
10319 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10320
10321 arg++; /* Skip the comma. */
f8eba3c6 10322 arg = skip_spaces (arg);
f1310107
TJB
10323
10324 /* Parse the end location. */
10325
f1310107
TJB
10326 init_linespec_result (&canonical_end);
10327 arg_start = arg;
10328
f8eba3c6 10329 /* We call decode_line_full directly here instead of using
f1310107
TJB
10330 parse_breakpoint_sals because we need to specify the start location's
10331 symtab and line as the default symtab and line for the end of the
10332 range. This makes it possible to have ranges like "foo.c:27, +14",
10333 where +14 means 14 lines from the start location. */
f00aae0f
KS
10334 end_location = string_to_event_location (&arg, current_language);
10335 make_cleanup_delete_event_location (end_location);
10336 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE,
f8eba3c6
TT
10337 sal_start.symtab, sal_start.line,
10338 &canonical_end, NULL, NULL);
10339
10340 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10341
f8eba3c6 10342 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10343 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10344
10345 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10346 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10347 || lsal_end->sals.nelts != 1)
f1310107
TJB
10348 error (_("Cannot create a ranged breakpoint with multiple locations."));
10349
f8eba3c6 10350 sal_end = lsal_end->sals.sals[0];
f1310107
TJB
10351
10352 end = find_breakpoint_range_end (sal_end);
10353 if (sal_start.pc > end)
177b42fe 10354 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10355
10356 length = end - sal_start.pc + 1;
10357 if (length < 0)
10358 /* Length overflowed. */
10359 error (_("Address range too large."));
10360 else if (length == 1)
10361 {
10362 /* This range is simple enough to be handled by
10363 the `hbreak' command. */
10364 hbreak_command (addr_string_start, 1);
10365
10366 do_cleanups (cleanup_bkpt);
10367
10368 return;
10369 }
10370
10371 /* Now set up the breakpoint. */
10372 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10373 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10374 set_breakpoint_count (breakpoint_count + 1);
10375 b->number = breakpoint_count;
10376 b->disposition = disp_donttouch;
f00aae0f
KS
10377 b->location = copy_event_location (start_location);
10378 b->location_range_end = copy_event_location (end_location);
f1310107
TJB
10379 b->loc->length = length;
10380
f8eba3c6 10381 do_cleanups (cleanup_bkpt);
f1310107
TJB
10382
10383 mention (b);
8d3788bd 10384 observer_notify_breakpoint_created (b);
44702360 10385 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10386}
10387
4a64f543
MS
10388/* Return non-zero if EXP is verified as constant. Returned zero
10389 means EXP is variable. Also the constant detection may fail for
10390 some constant expressions and in such case still falsely return
10391 zero. */
2e6e3d9c 10392
65d79d4b
SDJ
10393static int
10394watchpoint_exp_is_const (const struct expression *exp)
10395{
10396 int i = exp->nelts;
10397
10398 while (i > 0)
10399 {
10400 int oplenp, argsp;
10401
10402 /* We are only interested in the descriptor of each element. */
10403 operator_length (exp, i, &oplenp, &argsp);
10404 i -= oplenp;
10405
10406 switch (exp->elts[i].opcode)
10407 {
10408 case BINOP_ADD:
10409 case BINOP_SUB:
10410 case BINOP_MUL:
10411 case BINOP_DIV:
10412 case BINOP_REM:
10413 case BINOP_MOD:
10414 case BINOP_LSH:
10415 case BINOP_RSH:
10416 case BINOP_LOGICAL_AND:
10417 case BINOP_LOGICAL_OR:
10418 case BINOP_BITWISE_AND:
10419 case BINOP_BITWISE_IOR:
10420 case BINOP_BITWISE_XOR:
10421 case BINOP_EQUAL:
10422 case BINOP_NOTEQUAL:
10423 case BINOP_LESS:
10424 case BINOP_GTR:
10425 case BINOP_LEQ:
10426 case BINOP_GEQ:
10427 case BINOP_REPEAT:
10428 case BINOP_COMMA:
10429 case BINOP_EXP:
10430 case BINOP_MIN:
10431 case BINOP_MAX:
10432 case BINOP_INTDIV:
10433 case BINOP_CONCAT:
65d79d4b
SDJ
10434 case TERNOP_COND:
10435 case TERNOP_SLICE:
65d79d4b
SDJ
10436
10437 case OP_LONG:
10438 case OP_DOUBLE:
10439 case OP_DECFLOAT:
10440 case OP_LAST:
10441 case OP_COMPLEX:
10442 case OP_STRING:
65d79d4b
SDJ
10443 case OP_ARRAY:
10444 case OP_TYPE:
608b4967
TT
10445 case OP_TYPEOF:
10446 case OP_DECLTYPE:
6e72ca20 10447 case OP_TYPEID:
65d79d4b
SDJ
10448 case OP_NAME:
10449 case OP_OBJC_NSSTRING:
10450
10451 case UNOP_NEG:
10452 case UNOP_LOGICAL_NOT:
10453 case UNOP_COMPLEMENT:
10454 case UNOP_ADDR:
10455 case UNOP_HIGH:
aeaa2474 10456 case UNOP_CAST:
9eaf6705
TT
10457
10458 case UNOP_CAST_TYPE:
10459 case UNOP_REINTERPRET_CAST:
10460 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10461 /* Unary, binary and ternary operators: We have to check
10462 their operands. If they are constant, then so is the
10463 result of that operation. For instance, if A and B are
10464 determined to be constants, then so is "A + B".
10465
10466 UNOP_IND is one exception to the rule above, because the
10467 value of *ADDR is not necessarily a constant, even when
10468 ADDR is. */
65d79d4b
SDJ
10469 break;
10470
10471 case OP_VAR_VALUE:
10472 /* Check whether the associated symbol is a constant.
4a64f543 10473
65d79d4b 10474 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10475 possible that a buggy compiler could mark a variable as
10476 constant even when it is not, and TYPE_CONST would return
10477 true in this case, while SYMBOL_CLASS wouldn't.
10478
10479 We also have to check for function symbols because they
10480 are always constant. */
65d79d4b
SDJ
10481 {
10482 struct symbol *s = exp->elts[i + 2].symbol;
10483
10484 if (SYMBOL_CLASS (s) != LOC_BLOCK
10485 && SYMBOL_CLASS (s) != LOC_CONST
10486 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10487 return 0;
10488 break;
10489 }
10490
10491 /* The default action is to return 0 because we are using
10492 the optimistic approach here: If we don't know something,
10493 then it is not a constant. */
10494 default:
10495 return 0;
10496 }
10497 }
10498
10499 return 1;
10500}
10501
3a5c3e22
PA
10502/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10503
10504static void
10505dtor_watchpoint (struct breakpoint *self)
10506{
10507 struct watchpoint *w = (struct watchpoint *) self;
10508
10509 xfree (w->cond_exp);
10510 xfree (w->exp);
10511 xfree (w->exp_string);
10512 xfree (w->exp_string_reparse);
10513 value_free (w->val);
10514
10515 base_breakpoint_ops.dtor (self);
10516}
10517
348d480f
PA
10518/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10519
10520static void
10521re_set_watchpoint (struct breakpoint *b)
10522{
3a5c3e22
PA
10523 struct watchpoint *w = (struct watchpoint *) b;
10524
348d480f
PA
10525 /* Watchpoint can be either on expression using entirely global
10526 variables, or it can be on local variables.
10527
10528 Watchpoints of the first kind are never auto-deleted, and even
10529 persist across program restarts. Since they can use variables
10530 from shared libraries, we need to reparse expression as libraries
10531 are loaded and unloaded.
10532
10533 Watchpoints on local variables can also change meaning as result
10534 of solib event. For example, if a watchpoint uses both a local
10535 and a global variables in expression, it's a local watchpoint,
10536 but unloading of a shared library will make the expression
10537 invalid. This is not a very common use case, but we still
10538 re-evaluate expression, to avoid surprises to the user.
10539
10540 Note that for local watchpoints, we re-evaluate it only if
10541 watchpoints frame id is still valid. If it's not, it means the
10542 watchpoint is out of scope and will be deleted soon. In fact,
10543 I'm not sure we'll ever be called in this case.
10544
10545 If a local watchpoint's frame id is still valid, then
3a5c3e22 10546 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10547
3a5c3e22
PA
10548 Don't do anything about disabled watchpoints, since they will be
10549 reevaluated again when enabled. */
10550 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10551}
10552
77b06cd7
TJB
10553/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10554
10555static int
10556insert_watchpoint (struct bp_location *bl)
10557{
3a5c3e22
PA
10558 struct watchpoint *w = (struct watchpoint *) bl->owner;
10559 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10560
10561 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10562 w->cond_exp);
77b06cd7
TJB
10563}
10564
10565/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10566
10567static int
10568remove_watchpoint (struct bp_location *bl)
10569{
3a5c3e22
PA
10570 struct watchpoint *w = (struct watchpoint *) bl->owner;
10571 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10572
10573 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10574 w->cond_exp);
e09342b5
TJB
10575}
10576
e09342b5 10577static int
348d480f 10578breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10579 struct address_space *aspace, CORE_ADDR bp_addr,
10580 const struct target_waitstatus *ws)
e09342b5 10581{
348d480f 10582 struct breakpoint *b = bl->owner;
3a5c3e22 10583 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10584
348d480f
PA
10585 /* Continuable hardware watchpoints are treated as non-existent if the
10586 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10587 some data address). Otherwise gdb won't stop on a break instruction
10588 in the code (not from a breakpoint) when a hardware watchpoint has
10589 been defined. Also skip watchpoints which we know did not trigger
10590 (did not match the data address). */
10591 if (is_hardware_watchpoint (b)
3a5c3e22 10592 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10593 return 0;
9c06b0b4 10594
348d480f 10595 return 1;
9c06b0b4
TJB
10596}
10597
348d480f
PA
10598static void
10599check_status_watchpoint (bpstat bs)
9c06b0b4 10600{
348d480f 10601 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10602
348d480f 10603 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10604}
10605
10606/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10607 hardware watchpoints. */
9c06b0b4
TJB
10608
10609static int
348d480f 10610resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10611{
3a5c3e22
PA
10612 struct watchpoint *w = (struct watchpoint *) bl->owner;
10613 int length = w->exact? 1 : bl->length;
348d480f
PA
10614
10615 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10616}
10617
10618/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10619 hardware watchpoints. */
9c06b0b4
TJB
10620
10621static int
348d480f 10622works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10623{
efa80663
PA
10624 /* Read and access watchpoints only work with hardware support. */
10625 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10626}
10627
9c06b0b4 10628static enum print_stop_action
348d480f 10629print_it_watchpoint (bpstat bs)
9c06b0b4 10630{
348d480f
PA
10631 struct cleanup *old_chain;
10632 struct breakpoint *b;
f99d8bf4 10633 struct ui_file *stb;
348d480f 10634 enum print_stop_action result;
3a5c3e22 10635 struct watchpoint *w;
79a45e25 10636 struct ui_out *uiout = current_uiout;
348d480f
PA
10637
10638 gdb_assert (bs->bp_location_at != NULL);
10639
348d480f 10640 b = bs->breakpoint_at;
3a5c3e22 10641 w = (struct watchpoint *) b;
348d480f 10642
f99d8bf4
PA
10643 stb = mem_fileopen ();
10644 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10645
10646 switch (b->type)
10647 {
348d480f 10648 case bp_watchpoint:
9c06b0b4
TJB
10649 case bp_hardware_watchpoint:
10650 annotate_watchpoint (b->number);
10651 if (ui_out_is_mi_like_p (uiout))
10652 ui_out_field_string
10653 (uiout, "reason",
10654 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10655 mention (b);
10656 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10657 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10658 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10659 ui_out_field_stream (uiout, "old", stb);
10660 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10661 watchpoint_value_print (w->val, stb);
348d480f
PA
10662 ui_out_field_stream (uiout, "new", stb);
10663 ui_out_text (uiout, "\n");
10664 /* More than one watchpoint may have been triggered. */
10665 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10666 break;
10667
10668 case bp_read_watchpoint:
10669 if (ui_out_is_mi_like_p (uiout))
10670 ui_out_field_string
10671 (uiout, "reason",
10672 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10673 mention (b);
10674 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10675 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10676 watchpoint_value_print (w->val, stb);
348d480f
PA
10677 ui_out_field_stream (uiout, "value", stb);
10678 ui_out_text (uiout, "\n");
10679 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10680 break;
10681
10682 case bp_access_watchpoint:
348d480f
PA
10683 if (bs->old_val != NULL)
10684 {
10685 annotate_watchpoint (b->number);
10686 if (ui_out_is_mi_like_p (uiout))
10687 ui_out_field_string
10688 (uiout, "reason",
10689 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10690 mention (b);
10691 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10692 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10693 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10694 ui_out_field_stream (uiout, "old", stb);
10695 ui_out_text (uiout, "\nNew value = ");
10696 }
10697 else
10698 {
10699 mention (b);
10700 if (ui_out_is_mi_like_p (uiout))
10701 ui_out_field_string
10702 (uiout, "reason",
10703 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10704 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10705 ui_out_text (uiout, "\nValue = ");
10706 }
f99d8bf4 10707 watchpoint_value_print (w->val, stb);
348d480f
PA
10708 ui_out_field_stream (uiout, "new", stb);
10709 ui_out_text (uiout, "\n");
10710 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10711 break;
10712 default:
348d480f 10713 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10714 }
10715
348d480f
PA
10716 do_cleanups (old_chain);
10717 return result;
10718}
10719
10720/* Implement the "print_mention" breakpoint_ops method for hardware
10721 watchpoints. */
10722
10723static void
10724print_mention_watchpoint (struct breakpoint *b)
10725{
10726 struct cleanup *ui_out_chain;
3a5c3e22 10727 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10728 struct ui_out *uiout = current_uiout;
348d480f
PA
10729
10730 switch (b->type)
10731 {
10732 case bp_watchpoint:
10733 ui_out_text (uiout, "Watchpoint ");
10734 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10735 break;
10736 case bp_hardware_watchpoint:
10737 ui_out_text (uiout, "Hardware watchpoint ");
10738 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10739 break;
10740 case bp_read_watchpoint:
10741 ui_out_text (uiout, "Hardware read watchpoint ");
10742 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10743 break;
10744 case bp_access_watchpoint:
10745 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10746 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10747 break;
10748 default:
10749 internal_error (__FILE__, __LINE__,
10750 _("Invalid hardware watchpoint type."));
10751 }
10752
10753 ui_out_field_int (uiout, "number", b->number);
10754 ui_out_text (uiout, ": ");
3a5c3e22 10755 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10756 do_cleanups (ui_out_chain);
10757}
10758
10759/* Implement the "print_recreate" breakpoint_ops method for
10760 watchpoints. */
10761
10762static void
10763print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10764{
3a5c3e22
PA
10765 struct watchpoint *w = (struct watchpoint *) b;
10766
348d480f
PA
10767 switch (b->type)
10768 {
10769 case bp_watchpoint:
10770 case bp_hardware_watchpoint:
10771 fprintf_unfiltered (fp, "watch");
10772 break;
10773 case bp_read_watchpoint:
10774 fprintf_unfiltered (fp, "rwatch");
10775 break;
10776 case bp_access_watchpoint:
10777 fprintf_unfiltered (fp, "awatch");
10778 break;
10779 default:
10780 internal_error (__FILE__, __LINE__,
10781 _("Invalid watchpoint type."));
10782 }
10783
3a5c3e22 10784 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10785 print_recreate_thread (b, fp);
348d480f
PA
10786}
10787
427cd150
TT
10788/* Implement the "explains_signal" breakpoint_ops method for
10789 watchpoints. */
10790
47591c29 10791static int
427cd150
TT
10792explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10793{
10794 /* A software watchpoint cannot cause a signal other than
10795 GDB_SIGNAL_TRAP. */
10796 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10797 return 0;
427cd150 10798
47591c29 10799 return 1;
427cd150
TT
10800}
10801
348d480f
PA
10802/* The breakpoint_ops structure to be used in hardware watchpoints. */
10803
2060206e 10804static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10805
10806/* Implement the "insert" breakpoint_ops method for
10807 masked hardware watchpoints. */
10808
10809static int
10810insert_masked_watchpoint (struct bp_location *bl)
10811{
3a5c3e22
PA
10812 struct watchpoint *w = (struct watchpoint *) bl->owner;
10813
10814 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10815 bl->watchpoint_type);
10816}
10817
10818/* Implement the "remove" breakpoint_ops method for
10819 masked hardware watchpoints. */
10820
10821static int
10822remove_masked_watchpoint (struct bp_location *bl)
10823{
3a5c3e22
PA
10824 struct watchpoint *w = (struct watchpoint *) bl->owner;
10825
10826 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10827 bl->watchpoint_type);
10828}
10829
10830/* Implement the "resources_needed" breakpoint_ops method for
10831 masked hardware watchpoints. */
10832
10833static int
10834resources_needed_masked_watchpoint (const struct bp_location *bl)
10835{
3a5c3e22
PA
10836 struct watchpoint *w = (struct watchpoint *) bl->owner;
10837
10838 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10839}
10840
10841/* Implement the "works_in_software_mode" breakpoint_ops method for
10842 masked hardware watchpoints. */
10843
10844static int
10845works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10846{
10847 return 0;
10848}
10849
10850/* Implement the "print_it" breakpoint_ops method for
10851 masked hardware watchpoints. */
10852
10853static enum print_stop_action
10854print_it_masked_watchpoint (bpstat bs)
10855{
10856 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10857 struct ui_out *uiout = current_uiout;
348d480f
PA
10858
10859 /* Masked watchpoints have only one location. */
10860 gdb_assert (b->loc && b->loc->next == NULL);
10861
10862 switch (b->type)
10863 {
10864 case bp_hardware_watchpoint:
10865 annotate_watchpoint (b->number);
10866 if (ui_out_is_mi_like_p (uiout))
10867 ui_out_field_string
10868 (uiout, "reason",
10869 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10870 break;
10871
10872 case bp_read_watchpoint:
10873 if (ui_out_is_mi_like_p (uiout))
10874 ui_out_field_string
10875 (uiout, "reason",
10876 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10877 break;
10878
10879 case bp_access_watchpoint:
10880 if (ui_out_is_mi_like_p (uiout))
10881 ui_out_field_string
10882 (uiout, "reason",
10883 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10884 break;
10885 default:
10886 internal_error (__FILE__, __LINE__,
10887 _("Invalid hardware watchpoint type."));
10888 }
10889
10890 mention (b);
9c06b0b4
TJB
10891 ui_out_text (uiout, _("\n\
10892Check the underlying instruction at PC for the memory\n\
10893address and value which triggered this watchpoint.\n"));
10894 ui_out_text (uiout, "\n");
10895
10896 /* More than one watchpoint may have been triggered. */
10897 return PRINT_UNKNOWN;
10898}
10899
10900/* Implement the "print_one_detail" breakpoint_ops method for
10901 masked hardware watchpoints. */
10902
10903static void
10904print_one_detail_masked_watchpoint (const struct breakpoint *b,
10905 struct ui_out *uiout)
10906{
3a5c3e22
PA
10907 struct watchpoint *w = (struct watchpoint *) b;
10908
9c06b0b4
TJB
10909 /* Masked watchpoints have only one location. */
10910 gdb_assert (b->loc && b->loc->next == NULL);
10911
10912 ui_out_text (uiout, "\tmask ");
3a5c3e22 10913 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
10914 ui_out_text (uiout, "\n");
10915}
10916
10917/* Implement the "print_mention" breakpoint_ops method for
10918 masked hardware watchpoints. */
10919
10920static void
10921print_mention_masked_watchpoint (struct breakpoint *b)
10922{
3a5c3e22 10923 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10924 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
10925 struct cleanup *ui_out_chain;
10926
10927 switch (b->type)
10928 {
10929 case bp_hardware_watchpoint:
10930 ui_out_text (uiout, "Masked hardware watchpoint ");
10931 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10932 break;
10933 case bp_read_watchpoint:
10934 ui_out_text (uiout, "Masked hardware read watchpoint ");
10935 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10936 break;
10937 case bp_access_watchpoint:
10938 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10939 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10940 break;
10941 default:
10942 internal_error (__FILE__, __LINE__,
10943 _("Invalid hardware watchpoint type."));
10944 }
10945
10946 ui_out_field_int (uiout, "number", b->number);
10947 ui_out_text (uiout, ": ");
3a5c3e22 10948 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
10949 do_cleanups (ui_out_chain);
10950}
10951
10952/* Implement the "print_recreate" breakpoint_ops method for
10953 masked hardware watchpoints. */
10954
10955static void
10956print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10957{
3a5c3e22 10958 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10959 char tmp[40];
10960
10961 switch (b->type)
10962 {
10963 case bp_hardware_watchpoint:
10964 fprintf_unfiltered (fp, "watch");
10965 break;
10966 case bp_read_watchpoint:
10967 fprintf_unfiltered (fp, "rwatch");
10968 break;
10969 case bp_access_watchpoint:
10970 fprintf_unfiltered (fp, "awatch");
10971 break;
10972 default:
10973 internal_error (__FILE__, __LINE__,
10974 _("Invalid hardware watchpoint type."));
10975 }
10976
3a5c3e22
PA
10977 sprintf_vma (tmp, w->hw_wp_mask);
10978 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10979 print_recreate_thread (b, fp);
9c06b0b4
TJB
10980}
10981
10982/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10983
2060206e 10984static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10985
10986/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10987
10988static int
10989is_masked_watchpoint (const struct breakpoint *b)
10990{
10991 return b->ops == &masked_watchpoint_breakpoint_ops;
10992}
10993
53a5351d
JM
10994/* accessflag: hw_write: watch write,
10995 hw_read: watch read,
10996 hw_access: watch access (read or write) */
c906108c 10997static void
bbc13ae3 10998watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10999 int just_location, int internal)
c906108c 11000{
d983da9c 11001 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11002 struct expression *exp;
270140bd 11003 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11004 struct value *val, *mark, *result;
bb9d5f81 11005 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11006 struct frame_info *frame;
bbc13ae3
KS
11007 const char *exp_start = NULL;
11008 const char *exp_end = NULL;
11009 const char *tok, *end_tok;
9c06b0b4 11010 int toklen = -1;
bbc13ae3
KS
11011 const char *cond_start = NULL;
11012 const char *cond_end = NULL;
c906108c 11013 enum bptype bp_type;
37e4754d 11014 int thread = -1;
0cf6dd15 11015 int pc = 0;
9c06b0b4
TJB
11016 /* Flag to indicate whether we are going to use masks for
11017 the hardware watchpoint. */
11018 int use_mask = 0;
11019 CORE_ADDR mask = 0;
3a5c3e22 11020 struct watchpoint *w;
bbc13ae3
KS
11021 char *expression;
11022 struct cleanup *back_to;
c906108c 11023
37e4754d
LM
11024 /* Make sure that we actually have parameters to parse. */
11025 if (arg != NULL && arg[0] != '\0')
11026 {
bbc13ae3
KS
11027 const char *value_start;
11028
11029 exp_end = arg + strlen (arg);
37e4754d 11030
9c06b0b4
TJB
11031 /* Look for "parameter value" pairs at the end
11032 of the arguments string. */
bbc13ae3 11033 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11034 {
11035 /* Skip whitespace at the end of the argument list. */
11036 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11037 tok--;
11038
11039 /* Find the beginning of the last token.
11040 This is the value of the parameter. */
11041 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11042 tok--;
11043 value_start = tok + 1;
11044
11045 /* Skip whitespace. */
11046 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11047 tok--;
11048
11049 end_tok = tok;
11050
11051 /* Find the beginning of the second to last token.
11052 This is the parameter itself. */
11053 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11054 tok--;
11055 tok++;
11056 toklen = end_tok - tok + 1;
11057
61012eef 11058 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4
TJB
11059 {
11060 /* At this point we've found a "thread" token, which means
11061 the user is trying to set a watchpoint that triggers
11062 only in a specific thread. */
11063 char *endp;
37e4754d 11064
9c06b0b4
TJB
11065 if (thread != -1)
11066 error(_("You can specify only one thread."));
37e4754d 11067
9c06b0b4
TJB
11068 /* Extract the thread ID from the next token. */
11069 thread = strtol (value_start, &endp, 0);
37e4754d 11070
9c06b0b4
TJB
11071 /* Check if the user provided a valid numeric value for the
11072 thread ID. */
11073 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11074 error (_("Invalid thread ID specification %s."), value_start);
11075
11076 /* Check if the thread actually exists. */
11077 if (!valid_thread_id (thread))
af4908ba 11078 invalid_thread_id_error (thread);
9c06b0b4 11079 }
61012eef 11080 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11081 {
11082 /* We've found a "mask" token, which means the user wants to
11083 create a hardware watchpoint that is going to have the mask
11084 facility. */
11085 struct value *mask_value, *mark;
37e4754d 11086
9c06b0b4
TJB
11087 if (use_mask)
11088 error(_("You can specify only one mask."));
37e4754d 11089
9c06b0b4 11090 use_mask = just_location = 1;
37e4754d 11091
9c06b0b4
TJB
11092 mark = value_mark ();
11093 mask_value = parse_to_comma_and_eval (&value_start);
11094 mask = value_as_address (mask_value);
11095 value_free_to_mark (mark);
11096 }
11097 else
11098 /* We didn't recognize what we found. We should stop here. */
11099 break;
37e4754d 11100
9c06b0b4
TJB
11101 /* Truncate the string and get rid of the "parameter value" pair before
11102 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11103 exp_end = tok;
9c06b0b4 11104 }
37e4754d 11105 }
bbc13ae3
KS
11106 else
11107 exp_end = arg;
37e4754d 11108
bbc13ae3
KS
11109 /* Parse the rest of the arguments. From here on out, everything
11110 is in terms of a newly allocated string instead of the original
11111 ARG. */
c906108c 11112 innermost_block = NULL;
bbc13ae3
KS
11113 expression = savestring (arg, exp_end - arg);
11114 back_to = make_cleanup (xfree, expression);
11115 exp_start = arg = expression;
1bb9788d 11116 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11117 exp_end = arg;
fa8a61dc
TT
11118 /* Remove trailing whitespace from the expression before saving it.
11119 This makes the eventual display of the expression string a bit
11120 prettier. */
11121 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11122 --exp_end;
11123
65d79d4b
SDJ
11124 /* Checking if the expression is not constant. */
11125 if (watchpoint_exp_is_const (exp))
11126 {
11127 int len;
11128
11129 len = exp_end - exp_start;
11130 while (len > 0 && isspace (exp_start[len - 1]))
11131 len--;
11132 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11133 }
11134
c906108c
SS
11135 exp_valid_block = innermost_block;
11136 mark = value_mark ();
3a1115a0 11137 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11138
bb9d5f81
PP
11139 if (val != NULL && just_location)
11140 {
11141 saved_bitpos = value_bitpos (val);
11142 saved_bitsize = value_bitsize (val);
11143 }
11144
06a64a0b
TT
11145 if (just_location)
11146 {
9c06b0b4
TJB
11147 int ret;
11148
06a64a0b 11149 exp_valid_block = NULL;
a1442452 11150 val = value_addr (result);
06a64a0b
TT
11151 release_value (val);
11152 value_free_to_mark (mark);
9c06b0b4
TJB
11153
11154 if (use_mask)
11155 {
11156 ret = target_masked_watch_num_registers (value_as_address (val),
11157 mask);
11158 if (ret == -1)
11159 error (_("This target does not support masked watchpoints."));
11160 else if (ret == -2)
11161 error (_("Invalid mask or memory region."));
11162 }
06a64a0b
TT
11163 }
11164 else if (val != NULL)
fa4727a6 11165 release_value (val);
c906108c 11166
bbc13ae3
KS
11167 tok = skip_spaces_const (arg);
11168 end_tok = skip_to_space_const (tok);
c906108c
SS
11169
11170 toklen = end_tok - tok;
11171 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11172 {
2d134ed3
PA
11173 struct expression *cond;
11174
60e1c644 11175 innermost_block = NULL;
c906108c 11176 tok = cond_start = end_tok + 1;
1bb9788d 11177 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11178
11179 /* The watchpoint expression may not be local, but the condition
11180 may still be. E.g.: `watch global if local > 0'. */
11181 cond_exp_valid_block = innermost_block;
11182
2d134ed3 11183 xfree (cond);
c906108c
SS
11184 cond_end = tok;
11185 }
11186 if (*tok)
8a3fe4f8 11187 error (_("Junk at end of command."));
c906108c 11188
d983da9c 11189 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11190
11191 /* If the expression is "local", then set up a "watchpoint scope"
11192 breakpoint at the point where we've left the scope of the watchpoint
11193 expression. Create the scope breakpoint before the watchpoint, so
11194 that we will encounter it first in bpstat_stop_status. */
60e1c644 11195 if (exp_valid_block && frame)
d983da9c 11196 {
edb3359d
DJ
11197 if (frame_id_p (frame_unwind_caller_id (frame)))
11198 {
11199 scope_breakpoint
a6d9a66e
UW
11200 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11201 frame_unwind_caller_pc (frame),
06edf0c0
PA
11202 bp_watchpoint_scope,
11203 &momentary_breakpoint_ops);
d983da9c 11204
edb3359d 11205 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11206
edb3359d
DJ
11207 /* Automatically delete the breakpoint when it hits. */
11208 scope_breakpoint->disposition = disp_del;
d983da9c 11209
edb3359d
DJ
11210 /* Only break in the proper frame (help with recursion). */
11211 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11212
edb3359d 11213 /* Set the address at which we will stop. */
a6d9a66e
UW
11214 scope_breakpoint->loc->gdbarch
11215 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11216 scope_breakpoint->loc->requested_address
11217 = frame_unwind_caller_pc (frame);
11218 scope_breakpoint->loc->address
a6d9a66e
UW
11219 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11220 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11221 scope_breakpoint->type);
11222 }
d983da9c
DJ
11223 }
11224
e8369a73
AB
11225 /* Now set up the breakpoint. We create all watchpoints as hardware
11226 watchpoints here even if hardware watchpoints are turned off, a call
11227 to update_watchpoint later in this function will cause the type to
11228 drop back to bp_watchpoint (software watchpoint) if required. */
11229
11230 if (accessflag == hw_read)
11231 bp_type = bp_read_watchpoint;
11232 else if (accessflag == hw_access)
11233 bp_type = bp_access_watchpoint;
11234 else
11235 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11236
11237 w = XCNEW (struct watchpoint);
11238 b = &w->base;
348d480f 11239 if (use_mask)
3a5c3e22
PA
11240 init_raw_breakpoint_without_location (b, NULL, bp_type,
11241 &masked_watchpoint_breakpoint_ops);
348d480f 11242 else
3a5c3e22
PA
11243 init_raw_breakpoint_without_location (b, NULL, bp_type,
11244 &watchpoint_breakpoint_ops);
37e4754d 11245 b->thread = thread;
b5de0fa7 11246 b->disposition = disp_donttouch;
348d480f 11247 b->pspace = current_program_space;
3a5c3e22
PA
11248 w->exp = exp;
11249 w->exp_valid_block = exp_valid_block;
11250 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11251 if (just_location)
11252 {
11253 struct type *t = value_type (val);
11254 CORE_ADDR addr = value_as_address (val);
11255 char *name;
11256
11257 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11258 name = type_to_string (t);
11259
3a5c3e22 11260 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11261 core_addr_to_string (addr));
06a64a0b
TT
11262 xfree (name);
11263
3a5c3e22 11264 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11265 (int) (exp_end - exp_start), exp_start);
11266
06a64a0b
TT
11267 /* The above expression is in C. */
11268 b->language = language_c;
11269 }
11270 else
3a5c3e22 11271 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11272
11273 if (use_mask)
11274 {
3a5c3e22 11275 w->hw_wp_mask = mask;
9c06b0b4
TJB
11276 }
11277 else
11278 {
3a5c3e22 11279 w->val = val;
bb9d5f81
PP
11280 w->val_bitpos = saved_bitpos;
11281 w->val_bitsize = saved_bitsize;
3a5c3e22 11282 w->val_valid = 1;
9c06b0b4 11283 }
77b06cd7 11284
c906108c
SS
11285 if (cond_start)
11286 b->cond_string = savestring (cond_start, cond_end - cond_start);
11287 else
11288 b->cond_string = 0;
c5aa993b 11289
c906108c 11290 if (frame)
f6bc2008 11291 {
3a5c3e22
PA
11292 w->watchpoint_frame = get_frame_id (frame);
11293 w->watchpoint_thread = inferior_ptid;
f6bc2008 11294 }
c906108c 11295 else
f6bc2008 11296 {
3a5c3e22
PA
11297 w->watchpoint_frame = null_frame_id;
11298 w->watchpoint_thread = null_ptid;
f6bc2008 11299 }
c906108c 11300
d983da9c 11301 if (scope_breakpoint != NULL)
c906108c 11302 {
d983da9c
DJ
11303 /* The scope breakpoint is related to the watchpoint. We will
11304 need to act on them together. */
11305 b->related_breakpoint = scope_breakpoint;
11306 scope_breakpoint->related_breakpoint = b;
c906108c 11307 }
d983da9c 11308
06a64a0b
TT
11309 if (!just_location)
11310 value_free_to_mark (mark);
2d134ed3 11311
492d29ea 11312 TRY
a9634178
TJB
11313 {
11314 /* Finally update the new watchpoint. This creates the locations
11315 that should be inserted. */
3a5c3e22 11316 update_watchpoint (w, 1);
a9634178 11317 }
492d29ea 11318 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11319 {
11320 delete_breakpoint (b);
11321 throw_exception (e);
11322 }
492d29ea 11323 END_CATCH
a9634178 11324
3ea46bff 11325 install_breakpoint (internal, b, 1);
bbc13ae3 11326 do_cleanups (back_to);
c906108c
SS
11327}
11328
e09342b5 11329/* Return count of debug registers needed to watch the given expression.
e09342b5 11330 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11331
c906108c 11332static int
a9634178 11333can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11334{
11335 int found_memory_cnt = 0;
2e70b7b9 11336 struct value *head = v;
c906108c
SS
11337
11338 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11339 if (!can_use_hw_watchpoints)
c906108c 11340 return 0;
c5aa993b 11341
5c44784c
JM
11342 /* Make sure that the value of the expression depends only upon
11343 memory contents, and values computed from them within GDB. If we
11344 find any register references or function calls, we can't use a
11345 hardware watchpoint.
11346
11347 The idea here is that evaluating an expression generates a series
11348 of values, one holding the value of every subexpression. (The
11349 expression a*b+c has five subexpressions: a, b, a*b, c, and
11350 a*b+c.) GDB's values hold almost enough information to establish
11351 the criteria given above --- they identify memory lvalues,
11352 register lvalues, computed values, etcetera. So we can evaluate
11353 the expression, and then scan the chain of values that leaves
11354 behind to decide whether we can detect any possible change to the
11355 expression's final value using only hardware watchpoints.
11356
11357 However, I don't think that the values returned by inferior
11358 function calls are special in any way. So this function may not
11359 notice that an expression involving an inferior function call
11360 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11361 for (; v; v = value_next (v))
c906108c 11362 {
5c44784c 11363 if (VALUE_LVAL (v) == lval_memory)
c906108c 11364 {
8464be76
DJ
11365 if (v != head && value_lazy (v))
11366 /* A lazy memory lvalue in the chain is one that GDB never
11367 needed to fetch; we either just used its address (e.g.,
11368 `a' in `a.b') or we never needed it at all (e.g., `a'
11369 in `a,b'). This doesn't apply to HEAD; if that is
11370 lazy then it was not readable, but watch it anyway. */
5c44784c 11371 ;
53a5351d 11372 else
5c44784c
JM
11373 {
11374 /* Ahh, memory we actually used! Check if we can cover
11375 it with hardware watchpoints. */
df407dfe 11376 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11377
11378 /* We only watch structs and arrays if user asked for it
11379 explicitly, never if they just happen to appear in a
11380 middle of some value chain. */
11381 if (v == head
11382 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11383 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11384 {
42ae5230 11385 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11386 int len;
11387 int num_regs;
11388
a9634178 11389 len = (target_exact_watchpoints
e09342b5
TJB
11390 && is_scalar_type_recursive (vtype))?
11391 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11392
e09342b5
TJB
11393 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11394 if (!num_regs)
2e70b7b9
MS
11395 return 0;
11396 else
e09342b5 11397 found_memory_cnt += num_regs;
2e70b7b9 11398 }
5c44784c 11399 }
c5aa993b 11400 }
5086187c
AC
11401 else if (VALUE_LVAL (v) != not_lval
11402 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11403 return 0; /* These are values from the history (e.g., $1). */
5086187c 11404 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11405 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11406 }
11407
11408 /* The expression itself looks suitable for using a hardware
11409 watchpoint, but give the target machine a chance to reject it. */
11410 return found_memory_cnt;
11411}
11412
8b93c638 11413void
84f4c1fe 11414watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11415{
84f4c1fe 11416 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11417}
11418
06a64a0b
TT
11419/* A helper function that looks for the "-location" argument and then
11420 calls watch_command_1. */
11421
11422static void
11423watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11424{
11425 int just_location = 0;
11426
11427 if (arg
11428 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11429 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11430 {
e9cafbcc 11431 arg = skip_spaces (arg);
06a64a0b
TT
11432 just_location = 1;
11433 }
11434
84f4c1fe 11435 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11436}
8926118c 11437
c5aa993b 11438static void
fba45db2 11439watch_command (char *arg, int from_tty)
c906108c 11440{
06a64a0b 11441 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11442}
11443
8b93c638 11444void
84f4c1fe 11445rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11446{
84f4c1fe 11447 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11448}
8926118c 11449
c5aa993b 11450static void
fba45db2 11451rwatch_command (char *arg, int from_tty)
c906108c 11452{
06a64a0b 11453 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11454}
11455
8b93c638 11456void
84f4c1fe 11457awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11458{
84f4c1fe 11459 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11460}
8926118c 11461
c5aa993b 11462static void
fba45db2 11463awatch_command (char *arg, int from_tty)
c906108c 11464{
06a64a0b 11465 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11466}
c906108c 11467\f
c5aa993b 11468
cfc31633
PA
11469/* Data for the FSM that manages the until(location)/advance commands
11470 in infcmd.c. Here because it uses the mechanisms of
11471 breakpoints. */
c906108c 11472
cfc31633 11473struct until_break_fsm
bfec99b2 11474{
cfc31633
PA
11475 /* The base class. */
11476 struct thread_fsm thread_fsm;
11477
11478 /* The thread that as current when the command was executed. */
11479 int thread;
11480
11481 /* The breakpoint set at the destination location. */
11482 struct breakpoint *location_breakpoint;
11483
11484 /* Breakpoint set at the return address in the caller frame. May be
11485 NULL. */
11486 struct breakpoint *caller_breakpoint;
bfec99b2
PA
11487};
11488
cfc31633
PA
11489static void until_break_fsm_clean_up (struct thread_fsm *self);
11490static int until_break_fsm_should_stop (struct thread_fsm *self);
11491static enum async_reply_reason
11492 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11493
11494/* until_break_fsm's vtable. */
11495
11496static struct thread_fsm_ops until_break_fsm_ops =
11497{
11498 NULL, /* dtor */
11499 until_break_fsm_clean_up,
11500 until_break_fsm_should_stop,
11501 NULL, /* return_value */
11502 until_break_fsm_async_reply_reason,
11503};
11504
11505/* Allocate a new until_break_command_fsm. */
11506
11507static struct until_break_fsm *
11508new_until_break_fsm (int thread,
11509 struct breakpoint *location_breakpoint,
11510 struct breakpoint *caller_breakpoint)
11511{
11512 struct until_break_fsm *sm;
11513
11514 sm = XCNEW (struct until_break_fsm);
11515 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops);
11516
11517 sm->thread = thread;
11518 sm->location_breakpoint = location_breakpoint;
11519 sm->caller_breakpoint = caller_breakpoint;
11520
11521 return sm;
11522}
11523
11524/* Implementation of the 'should_stop' FSM method for the
11525 until(location)/advance commands. */
11526
11527static int
11528until_break_fsm_should_stop (struct thread_fsm *self)
11529{
11530 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11531 struct thread_info *tp = inferior_thread ();
11532
11533 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11534 sm->location_breakpoint) != NULL
11535 || (sm->caller_breakpoint != NULL
11536 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11537 sm->caller_breakpoint) != NULL))
11538 thread_fsm_set_finished (self);
11539
11540 return 1;
11541}
11542
11543/* Implementation of the 'clean_up' FSM method for the
11544 until(location)/advance commands. */
11545
c2c6d25f 11546static void
cfc31633 11547until_break_fsm_clean_up (struct thread_fsm *self)
43ff13b4 11548{
cfc31633 11549 struct until_break_fsm *sm = (struct until_break_fsm *) self;
bfec99b2 11550
cfc31633
PA
11551 /* Clean up our temporary breakpoints. */
11552 if (sm->location_breakpoint != NULL)
11553 {
11554 delete_breakpoint (sm->location_breakpoint);
11555 sm->location_breakpoint = NULL;
11556 }
11557 if (sm->caller_breakpoint != NULL)
11558 {
11559 delete_breakpoint (sm->caller_breakpoint);
11560 sm->caller_breakpoint = NULL;
11561 }
11562 delete_longjmp_breakpoint (sm->thread);
11563}
11564
11565/* Implementation of the 'async_reply_reason' FSM method for the
11566 until(location)/advance commands. */
11567
11568static enum async_reply_reason
11569until_break_fsm_async_reply_reason (struct thread_fsm *self)
11570{
11571 return EXEC_ASYNC_LOCATION_REACHED;
43ff13b4
JM
11572}
11573
c906108c 11574void
ae66c1fc 11575until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11576{
11577 struct symtabs_and_lines sals;
11578 struct symtab_and_line sal;
8556afb4
PA
11579 struct frame_info *frame;
11580 struct gdbarch *frame_gdbarch;
11581 struct frame_id stack_frame_id;
11582 struct frame_id caller_frame_id;
cfc31633
PA
11583 struct breakpoint *location_breakpoint;
11584 struct breakpoint *caller_breakpoint = NULL;
f00aae0f 11585 struct cleanup *old_chain, *cleanup;
186c406b
TT
11586 int thread;
11587 struct thread_info *tp;
f00aae0f 11588 struct event_location *location;
cfc31633 11589 struct until_break_fsm *sm;
c906108c 11590
70509625 11591 clear_proceed_status (0);
c906108c
SS
11592
11593 /* Set a breakpoint where the user wants it and at return from
4a64f543 11594 this function. */
c5aa993b 11595
f00aae0f
KS
11596 location = string_to_event_location (&arg, current_language);
11597 cleanup = make_cleanup_delete_event_location (location);
11598
1bfeeb0f 11599 if (last_displayed_sal_is_valid ())
f00aae0f 11600 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11601 get_last_displayed_symtab (),
f8eba3c6 11602 get_last_displayed_line ());
c906108c 11603 else
f00aae0f 11604 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
f8eba3c6 11605 (struct symtab *) NULL, 0);
c5aa993b 11606
c906108c 11607 if (sals.nelts != 1)
8a3fe4f8 11608 error (_("Couldn't get information on specified line."));
c5aa993b 11609
c906108c 11610 sal = sals.sals[0];
4a64f543 11611 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11612
c906108c 11613 if (*arg)
8a3fe4f8 11614 error (_("Junk at end of arguments."));
c5aa993b 11615
c906108c 11616 resolve_sal_pc (&sal);
c5aa993b 11617
186c406b
TT
11618 tp = inferior_thread ();
11619 thread = tp->num;
11620
883bc8d1
PA
11621 old_chain = make_cleanup (null_cleanup, NULL);
11622
8556afb4
PA
11623 /* Note linespec handling above invalidates the frame chain.
11624 Installing a breakpoint also invalidates the frame chain (as it
11625 may need to switch threads), so do any frame handling before
11626 that. */
11627
11628 frame = get_selected_frame (NULL);
11629 frame_gdbarch = get_frame_arch (frame);
11630 stack_frame_id = get_stack_frame_id (frame);
11631 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11632
ae66c1fc
EZ
11633 /* Keep within the current frame, or in frames called by the current
11634 one. */
edb3359d 11635
883bc8d1 11636 if (frame_id_p (caller_frame_id))
c906108c 11637 {
883bc8d1 11638 struct symtab_and_line sal2;
cfc31633 11639 struct gdbarch *caller_gdbarch;
883bc8d1
PA
11640
11641 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11642 sal2.pc = frame_unwind_caller_pc (frame);
cfc31633
PA
11643 caller_gdbarch = frame_unwind_caller_arch (frame);
11644 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11645 sal2,
11646 caller_frame_id,
11647 bp_until);
11648 make_cleanup_delete_breakpoint (caller_breakpoint);
186c406b 11649
883bc8d1 11650 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11651 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11652 }
c5aa993b 11653
c70a6932
JK
11654 /* set_momentary_breakpoint could invalidate FRAME. */
11655 frame = NULL;
11656
883bc8d1
PA
11657 if (anywhere)
11658 /* If the user told us to continue until a specified location,
11659 we don't specify a frame at which we need to stop. */
cfc31633
PA
11660 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11661 null_frame_id, bp_until);
883bc8d1
PA
11662 else
11663 /* Otherwise, specify the selected frame, because we want to stop
11664 only at the very same frame. */
cfc31633
PA
11665 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11666 stack_frame_id, bp_until);
11667 make_cleanup_delete_breakpoint (location_breakpoint);
883bc8d1 11668
cfc31633
PA
11669 sm = new_until_break_fsm (tp->num, location_breakpoint, caller_breakpoint);
11670 tp->thread_fsm = &sm->thread_fsm;
f107f563 11671
cfc31633 11672 discard_cleanups (old_chain);
f107f563 11673
cfc31633 11674 proceed (-1, GDB_SIGNAL_DEFAULT);
f00aae0f
KS
11675
11676 do_cleanups (cleanup);
c906108c 11677}
ae66c1fc 11678
c906108c
SS
11679/* This function attempts to parse an optional "if <cond>" clause
11680 from the arg string. If one is not found, it returns NULL.
c5aa993b 11681
c906108c
SS
11682 Else, it returns a pointer to the condition string. (It does not
11683 attempt to evaluate the string against a particular block.) And,
11684 it updates arg to point to the first character following the parsed
4a64f543 11685 if clause in the arg string. */
53a5351d 11686
916703c0 11687char *
fba45db2 11688ep_parse_optional_if_clause (char **arg)
c906108c 11689{
c5aa993b
JM
11690 char *cond_string;
11691
11692 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11693 return NULL;
c5aa993b 11694
4a64f543 11695 /* Skip the "if" keyword. */
c906108c 11696 (*arg) += 2;
c5aa993b 11697
c906108c 11698 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11699 condition string. */
e9cafbcc 11700 *arg = skip_spaces (*arg);
c906108c 11701 cond_string = *arg;
c5aa993b 11702
4a64f543
MS
11703 /* Assume that the condition occupies the remainder of the arg
11704 string. */
c906108c 11705 (*arg) += strlen (cond_string);
c5aa993b 11706
c906108c
SS
11707 return cond_string;
11708}
c5aa993b 11709
c906108c
SS
11710/* Commands to deal with catching events, such as signals, exceptions,
11711 process start/exit, etc. */
c5aa993b
JM
11712
11713typedef enum
11714{
44feb3ce
TT
11715 catch_fork_temporary, catch_vfork_temporary,
11716 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11717}
11718catch_fork_kind;
11719
c906108c 11720static void
cc59ec59
MS
11721catch_fork_command_1 (char *arg, int from_tty,
11722 struct cmd_list_element *command)
c906108c 11723{
a6d9a66e 11724 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11725 char *cond_string = NULL;
44feb3ce
TT
11726 catch_fork_kind fork_kind;
11727 int tempflag;
11728
11729 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11730 tempflag = (fork_kind == catch_fork_temporary
11731 || fork_kind == catch_vfork_temporary);
c5aa993b 11732
44feb3ce
TT
11733 if (!arg)
11734 arg = "";
e9cafbcc 11735 arg = skip_spaces (arg);
c5aa993b 11736
c906108c 11737 /* The allowed syntax is:
c5aa993b
JM
11738 catch [v]fork
11739 catch [v]fork if <cond>
11740
4a64f543 11741 First, check if there's an if clause. */
c906108c 11742 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11743
c906108c 11744 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11745 error (_("Junk at end of arguments."));
c5aa993b 11746
c906108c 11747 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11748 and enable reporting of such events. */
c5aa993b
JM
11749 switch (fork_kind)
11750 {
44feb3ce
TT
11751 case catch_fork_temporary:
11752 case catch_fork_permanent:
a6d9a66e 11753 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11754 &catch_fork_breakpoint_ops);
c906108c 11755 break;
44feb3ce
TT
11756 case catch_vfork_temporary:
11757 case catch_vfork_permanent:
a6d9a66e 11758 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11759 &catch_vfork_breakpoint_ops);
c906108c 11760 break;
c5aa993b 11761 default:
8a3fe4f8 11762 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11763 break;
c5aa993b 11764 }
c906108c
SS
11765}
11766
11767static void
cc59ec59
MS
11768catch_exec_command_1 (char *arg, int from_tty,
11769 struct cmd_list_element *command)
c906108c 11770{
b4d90040 11771 struct exec_catchpoint *c;
a6d9a66e 11772 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11773 int tempflag;
c5aa993b 11774 char *cond_string = NULL;
c906108c 11775
44feb3ce
TT
11776 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11777
11778 if (!arg)
11779 arg = "";
e9cafbcc 11780 arg = skip_spaces (arg);
c906108c
SS
11781
11782 /* The allowed syntax is:
c5aa993b
JM
11783 catch exec
11784 catch exec if <cond>
c906108c 11785
4a64f543 11786 First, check if there's an if clause. */
c906108c
SS
11787 cond_string = ep_parse_optional_if_clause (&arg);
11788
11789 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11790 error (_("Junk at end of arguments."));
c906108c 11791
b4d90040
PA
11792 c = XNEW (struct exec_catchpoint);
11793 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11794 &catch_exec_breakpoint_ops);
11795 c->exec_pathname = NULL;
11796
3ea46bff 11797 install_breakpoint (0, &c->base, 1);
c906108c 11798}
c5aa993b 11799
9ac4176b 11800void
28010a5d
PA
11801init_ada_exception_breakpoint (struct breakpoint *b,
11802 struct gdbarch *gdbarch,
11803 struct symtab_and_line sal,
11804 char *addr_string,
c0a91b2b 11805 const struct breakpoint_ops *ops,
28010a5d 11806 int tempflag,
349774ef 11807 int enabled,
28010a5d 11808 int from_tty)
f7f9143b 11809{
f7f9143b
JB
11810 if (from_tty)
11811 {
5af949e3
UW
11812 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11813 if (!loc_gdbarch)
11814 loc_gdbarch = gdbarch;
11815
6c95b8df
PA
11816 describe_other_breakpoints (loc_gdbarch,
11817 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11818 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11819 version for exception catchpoints, because two catchpoints
11820 used for different exception names will use the same address.
11821 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11822 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11823 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11824 the user what type of catchpoint it is. The above is good
11825 enough for now, though. */
11826 }
11827
28010a5d 11828 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11829
349774ef 11830 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11831 b->disposition = tempflag ? disp_del : disp_donttouch;
f00aae0f
KS
11832 b->location = string_to_event_location (&addr_string,
11833 language_def (language_ada));
f7f9143b 11834 b->language = language_ada;
f7f9143b
JB
11835}
11836
c906108c 11837static void
fba45db2 11838catch_command (char *arg, int from_tty)
c906108c 11839{
44feb3ce 11840 error (_("Catch requires an event name."));
c906108c
SS
11841}
11842\f
11843
11844static void
fba45db2 11845tcatch_command (char *arg, int from_tty)
c906108c 11846{
44feb3ce 11847 error (_("Catch requires an event name."));
c906108c
SS
11848}
11849
8a2c437b
TT
11850/* A qsort comparison function that sorts breakpoints in order. */
11851
11852static int
11853compare_breakpoints (const void *a, const void *b)
11854{
11855 const breakpoint_p *ba = a;
11856 uintptr_t ua = (uintptr_t) *ba;
11857 const breakpoint_p *bb = b;
11858 uintptr_t ub = (uintptr_t) *bb;
11859
11860 if ((*ba)->number < (*bb)->number)
11861 return -1;
11862 else if ((*ba)->number > (*bb)->number)
11863 return 1;
11864
11865 /* Now sort by address, in case we see, e..g, two breakpoints with
11866 the number 0. */
11867 if (ua < ub)
11868 return -1;
94b0e70d 11869 return ua > ub ? 1 : 0;
8a2c437b
TT
11870}
11871
80f8a6eb 11872/* Delete breakpoints by address or line. */
c906108c
SS
11873
11874static void
fba45db2 11875clear_command (char *arg, int from_tty)
c906108c 11876{
8a2c437b 11877 struct breakpoint *b, *prev;
d6e956e5
VP
11878 VEC(breakpoint_p) *found = 0;
11879 int ix;
c906108c
SS
11880 int default_match;
11881 struct symtabs_and_lines sals;
11882 struct symtab_and_line sal;
c906108c 11883 int i;
8a2c437b 11884 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11885
11886 if (arg)
11887 {
39cf75f7
DE
11888 sals = decode_line_with_current_source (arg,
11889 (DECODE_LINE_FUNFIRSTLINE
11890 | DECODE_LINE_LIST_MODE));
cf4ded82 11891 make_cleanup (xfree, sals.sals);
c906108c
SS
11892 default_match = 0;
11893 }
11894 else
11895 {
8d749320 11896 sals.sals = XNEW (struct symtab_and_line);
80f8a6eb 11897 make_cleanup (xfree, sals.sals);
4a64f543 11898 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11899
11900 /* Set sal's line, symtab, pc, and pspace to the values
11901 corresponding to the last call to print_frame_info. If the
11902 codepoint is not valid, this will set all the fields to 0. */
11903 get_last_displayed_sal (&sal);
c906108c 11904 if (sal.symtab == 0)
8a3fe4f8 11905 error (_("No source file specified."));
c906108c
SS
11906
11907 sals.sals[0] = sal;
11908 sals.nelts = 1;
11909
11910 default_match = 1;
11911 }
11912
4a64f543
MS
11913 /* We don't call resolve_sal_pc here. That's not as bad as it
11914 seems, because all existing breakpoints typically have both
11915 file/line and pc set. So, if clear is given file/line, we can
11916 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11917
11918 We only support clearing given the address explicitly
11919 present in breakpoint table. Say, we've set breakpoint
4a64f543 11920 at file:line. There were several PC values for that file:line,
ed0616c6 11921 due to optimization, all in one block.
4a64f543
MS
11922
11923 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11924 PC corresponding to the same file:line, the breakpoint won't
11925 be cleared. We probably can still clear the breakpoint, but
11926 since the other PC value is never presented to user, user
11927 can only find it by guessing, and it does not seem important
11928 to support that. */
11929
4a64f543
MS
11930 /* For each line spec given, delete bps which correspond to it. Do
11931 it in two passes, solely to preserve the current behavior that
11932 from_tty is forced true if we delete more than one
11933 breakpoint. */
c906108c 11934
80f8a6eb 11935 found = NULL;
8a2c437b 11936 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11937 for (i = 0; i < sals.nelts; i++)
11938 {
05cba821
JK
11939 const char *sal_fullname;
11940
c906108c 11941 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11942 If line given (pc == 0), clear all bpts on specified line.
11943 If defaulting, clear all bpts on default line
c906108c 11944 or at default pc.
c5aa993b
JM
11945
11946 defaulting sal.pc != 0 tests to do
11947
11948 0 1 pc
11949 1 1 pc _and_ line
11950 0 0 line
11951 1 0 <can't happen> */
c906108c
SS
11952
11953 sal = sals.sals[i];
05cba821
JK
11954 sal_fullname = (sal.symtab == NULL
11955 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11956
4a64f543 11957 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11958 ALL_BREAKPOINTS (b)
c5aa993b 11959 {
0d381245 11960 int match = 0;
4a64f543 11961 /* Are we going to delete b? */
cc60f2e3 11962 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11963 {
11964 struct bp_location *loc = b->loc;
11965 for (; loc; loc = loc->next)
11966 {
f8eba3c6
TT
11967 /* If the user specified file:line, don't allow a PC
11968 match. This matches historical gdb behavior. */
11969 int pc_match = (!sal.explicit_line
11970 && sal.pc
11971 && (loc->pspace == sal.pspace)
11972 && (loc->address == sal.pc)
11973 && (!section_is_overlay (loc->section)
11974 || loc->section == sal.section));
4aac40c8
TT
11975 int line_match = 0;
11976
11977 if ((default_match || sal.explicit_line)
2f202fde 11978 && loc->symtab != NULL
05cba821 11979 && sal_fullname != NULL
4aac40c8 11980 && sal.pspace == loc->pspace
05cba821
JK
11981 && loc->line_number == sal.line
11982 && filename_cmp (symtab_to_fullname (loc->symtab),
11983 sal_fullname) == 0)
11984 line_match = 1;
4aac40c8 11985
0d381245
VP
11986 if (pc_match || line_match)
11987 {
11988 match = 1;
11989 break;
11990 }
11991 }
11992 }
11993
11994 if (match)
d6e956e5 11995 VEC_safe_push(breakpoint_p, found, b);
c906108c 11996 }
80f8a6eb 11997 }
8a2c437b 11998
80f8a6eb 11999 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12000 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12001 {
12002 if (arg)
8a3fe4f8 12003 error (_("No breakpoint at %s."), arg);
80f8a6eb 12004 else
8a3fe4f8 12005 error (_("No breakpoint at this line."));
80f8a6eb 12006 }
c906108c 12007
8a2c437b
TT
12008 /* Remove duplicates from the vec. */
12009 qsort (VEC_address (breakpoint_p, found),
12010 VEC_length (breakpoint_p, found),
12011 sizeof (breakpoint_p),
12012 compare_breakpoints);
12013 prev = VEC_index (breakpoint_p, found, 0);
12014 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12015 {
12016 if (b == prev)
12017 {
12018 VEC_ordered_remove (breakpoint_p, found, ix);
12019 --ix;
12020 }
12021 }
12022
d6e956e5 12023 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12024 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12025 if (from_tty)
a3f17187 12026 {
d6e956e5 12027 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12028 printf_unfiltered (_("Deleted breakpoint "));
12029 else
12030 printf_unfiltered (_("Deleted breakpoints "));
12031 }
d6e956e5
VP
12032
12033 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12034 {
c5aa993b 12035 if (from_tty)
d6e956e5
VP
12036 printf_unfiltered ("%d ", b->number);
12037 delete_breakpoint (b);
c906108c 12038 }
80f8a6eb
MS
12039 if (from_tty)
12040 putchar_unfiltered ('\n');
8a2c437b
TT
12041
12042 do_cleanups (cleanups);
c906108c
SS
12043}
12044\f
12045/* Delete breakpoint in BS if they are `delete' breakpoints and
12046 all breakpoints that are marked for deletion, whether hit or not.
12047 This is called after any breakpoint is hit, or after errors. */
12048
12049void
fba45db2 12050breakpoint_auto_delete (bpstat bs)
c906108c 12051{
35df4500 12052 struct breakpoint *b, *b_tmp;
c906108c
SS
12053
12054 for (; bs; bs = bs->next)
f431efe5
PA
12055 if (bs->breakpoint_at
12056 && bs->breakpoint_at->disposition == disp_del
c906108c 12057 && bs->stop)
f431efe5 12058 delete_breakpoint (bs->breakpoint_at);
c906108c 12059
35df4500 12060 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12061 {
b5de0fa7 12062 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12063 delete_breakpoint (b);
12064 }
c906108c
SS
12065}
12066
4a64f543
MS
12067/* A comparison function for bp_location AP and BP being interfaced to
12068 qsort. Sort elements primarily by their ADDRESS (no matter what
12069 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 12070 secondarily by ordering first permanent elements and
4a64f543 12071 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12072 qsort being an unstable algorithm. */
876fa593
JK
12073
12074static int
494cfb0f 12075bp_location_compare (const void *ap, const void *bp)
876fa593 12076{
494cfb0f
JK
12077 struct bp_location *a = *(void **) ap;
12078 struct bp_location *b = *(void **) bp;
876fa593
JK
12079
12080 if (a->address != b->address)
12081 return (a->address > b->address) - (a->address < b->address);
12082
dea2aa5f
LM
12083 /* Sort locations at the same address by their pspace number, keeping
12084 locations of the same inferior (in a multi-inferior environment)
12085 grouped. */
12086
12087 if (a->pspace->num != b->pspace->num)
12088 return ((a->pspace->num > b->pspace->num)
12089 - (a->pspace->num < b->pspace->num));
12090
876fa593 12091 /* Sort permanent breakpoints first. */
1a853c52
PA
12092 if (a->permanent != b->permanent)
12093 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12094
c56a97f9
JK
12095 /* Make the internal GDB representation stable across GDB runs
12096 where A and B memory inside GDB can differ. Breakpoint locations of
12097 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12098
12099 if (a->owner->number != b->owner->number)
c56a97f9
JK
12100 return ((a->owner->number > b->owner->number)
12101 - (a->owner->number < b->owner->number));
876fa593
JK
12102
12103 return (a > b) - (a < b);
12104}
12105
876fa593 12106/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12107 bp_location_shadow_len_after_address_max according to the current
12108 content of the bp_location array. */
f7545552
TT
12109
12110static void
876fa593 12111bp_location_target_extensions_update (void)
f7545552 12112{
876fa593
JK
12113 struct bp_location *bl, **blp_tmp;
12114
12115 bp_location_placed_address_before_address_max = 0;
12116 bp_location_shadow_len_after_address_max = 0;
12117
12118 ALL_BP_LOCATIONS (bl, blp_tmp)
12119 {
12120 CORE_ADDR start, end, addr;
12121
12122 if (!bp_location_has_shadow (bl))
12123 continue;
12124
12125 start = bl->target_info.placed_address;
12126 end = start + bl->target_info.shadow_len;
12127
12128 gdb_assert (bl->address >= start);
12129 addr = bl->address - start;
12130 if (addr > bp_location_placed_address_before_address_max)
12131 bp_location_placed_address_before_address_max = addr;
12132
12133 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12134
12135 gdb_assert (bl->address < end);
12136 addr = end - bl->address;
12137 if (addr > bp_location_shadow_len_after_address_max)
12138 bp_location_shadow_len_after_address_max = addr;
12139 }
f7545552
TT
12140}
12141
1e4d1764
YQ
12142/* Download tracepoint locations if they haven't been. */
12143
12144static void
12145download_tracepoint_locations (void)
12146{
7ed2c994 12147 struct breakpoint *b;
1e4d1764 12148 struct cleanup *old_chain;
dd2e65cc 12149 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
1e4d1764
YQ
12150
12151 old_chain = save_current_space_and_thread ();
12152
7ed2c994 12153 ALL_TRACEPOINTS (b)
1e4d1764 12154 {
7ed2c994 12155 struct bp_location *bl;
1e4d1764 12156 struct tracepoint *t;
f2a8bc8a 12157 int bp_location_downloaded = 0;
1e4d1764 12158
7ed2c994 12159 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12160 ? !may_insert_fast_tracepoints
12161 : !may_insert_tracepoints))
12162 continue;
12163
dd2e65cc
YQ
12164 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12165 {
12166 if (target_can_download_tracepoint ())
12167 can_download_tracepoint = TRIBOOL_TRUE;
12168 else
12169 can_download_tracepoint = TRIBOOL_FALSE;
12170 }
12171
12172 if (can_download_tracepoint == TRIBOOL_FALSE)
12173 break;
12174
7ed2c994
YQ
12175 for (bl = b->loc; bl; bl = bl->next)
12176 {
12177 /* In tracepoint, locations are _never_ duplicated, so
12178 should_be_inserted is equivalent to
12179 unduplicated_should_be_inserted. */
12180 if (!should_be_inserted (bl) || bl->inserted)
12181 continue;
1e4d1764 12182
7ed2c994 12183 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12184
7ed2c994 12185 target_download_tracepoint (bl);
1e4d1764 12186
7ed2c994 12187 bl->inserted = 1;
f2a8bc8a 12188 bp_location_downloaded = 1;
7ed2c994
YQ
12189 }
12190 t = (struct tracepoint *) b;
12191 t->number_on_target = b->number;
f2a8bc8a
YQ
12192 if (bp_location_downloaded)
12193 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12194 }
12195
12196 do_cleanups (old_chain);
12197}
12198
934709f0
PW
12199/* Swap the insertion/duplication state between two locations. */
12200
12201static void
12202swap_insertion (struct bp_location *left, struct bp_location *right)
12203{
12204 const int left_inserted = left->inserted;
12205 const int left_duplicate = left->duplicate;
b775012e 12206 const int left_needs_update = left->needs_update;
934709f0
PW
12207 const struct bp_target_info left_target_info = left->target_info;
12208
1e4d1764
YQ
12209 /* Locations of tracepoints can never be duplicated. */
12210 if (is_tracepoint (left->owner))
12211 gdb_assert (!left->duplicate);
12212 if (is_tracepoint (right->owner))
12213 gdb_assert (!right->duplicate);
12214
934709f0
PW
12215 left->inserted = right->inserted;
12216 left->duplicate = right->duplicate;
b775012e 12217 left->needs_update = right->needs_update;
934709f0
PW
12218 left->target_info = right->target_info;
12219 right->inserted = left_inserted;
12220 right->duplicate = left_duplicate;
b775012e 12221 right->needs_update = left_needs_update;
934709f0
PW
12222 right->target_info = left_target_info;
12223}
12224
b775012e
LM
12225/* Force the re-insertion of the locations at ADDRESS. This is called
12226 once a new/deleted/modified duplicate location is found and we are evaluating
12227 conditions on the target's side. Such conditions need to be updated on
12228 the target. */
12229
12230static void
12231force_breakpoint_reinsertion (struct bp_location *bl)
12232{
12233 struct bp_location **locp = NULL, **loc2p;
12234 struct bp_location *loc;
12235 CORE_ADDR address = 0;
12236 int pspace_num;
12237
12238 address = bl->address;
12239 pspace_num = bl->pspace->num;
12240
12241 /* This is only meaningful if the target is
12242 evaluating conditions and if the user has
12243 opted for condition evaluation on the target's
12244 side. */
12245 if (gdb_evaluates_breakpoint_condition_p ()
12246 || !target_supports_evaluation_of_breakpoint_conditions ())
12247 return;
12248
12249 /* Flag all breakpoint locations with this address and
12250 the same program space as the location
12251 as "its condition has changed". We need to
12252 update the conditions on the target's side. */
12253 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12254 {
12255 loc = *loc2p;
12256
12257 if (!is_breakpoint (loc->owner)
12258 || pspace_num != loc->pspace->num)
12259 continue;
12260
12261 /* Flag the location appropriately. We use a different state to
12262 let everyone know that we already updated the set of locations
12263 with addr bl->address and program space bl->pspace. This is so
12264 we don't have to keep calling these functions just to mark locations
12265 that have already been marked. */
12266 loc->condition_changed = condition_updated;
12267
12268 /* Free the agent expression bytecode as well. We will compute
12269 it later on. */
12270 if (loc->cond_bytecode)
12271 {
12272 free_agent_expr (loc->cond_bytecode);
12273 loc->cond_bytecode = NULL;
12274 }
12275 }
12276}
44702360
PA
12277/* Called whether new breakpoints are created, or existing breakpoints
12278 deleted, to update the global location list and recompute which
12279 locations are duplicate of which.
b775012e 12280
04086b45
PA
12281 The INSERT_MODE flag determines whether locations may not, may, or
12282 shall be inserted now. See 'enum ugll_insert_mode' for more
12283 info. */
b60e7edf 12284
0d381245 12285static void
44702360 12286update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12287{
74960c60 12288 struct breakpoint *b;
876fa593 12289 struct bp_location **locp, *loc;
f7545552 12290 struct cleanup *cleanups;
b775012e
LM
12291 /* Last breakpoint location address that was marked for update. */
12292 CORE_ADDR last_addr = 0;
12293 /* Last breakpoint location program space that was marked for update. */
12294 int last_pspace_num = -1;
f7545552 12295
2d134ed3
PA
12296 /* Used in the duplicates detection below. When iterating over all
12297 bp_locations, points to the first bp_location of a given address.
12298 Breakpoints and watchpoints of different types are never
12299 duplicates of each other. Keep one pointer for each type of
12300 breakpoint/watchpoint, so we only need to loop over all locations
12301 once. */
12302 struct bp_location *bp_loc_first; /* breakpoint */
12303 struct bp_location *wp_loc_first; /* hardware watchpoint */
12304 struct bp_location *awp_loc_first; /* access watchpoint */
12305 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12306
4a64f543
MS
12307 /* Saved former bp_location array which we compare against the newly
12308 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12309 struct bp_location **old_location, **old_locp;
12310 unsigned old_location_count;
12311
12312 old_location = bp_location;
12313 old_location_count = bp_location_count;
12314 bp_location = NULL;
12315 bp_location_count = 0;
12316 cleanups = make_cleanup (xfree, old_location);
0d381245 12317
74960c60 12318 ALL_BREAKPOINTS (b)
876fa593
JK
12319 for (loc = b->loc; loc; loc = loc->next)
12320 bp_location_count++;
12321
8d749320 12322 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
876fa593
JK
12323 locp = bp_location;
12324 ALL_BREAKPOINTS (b)
12325 for (loc = b->loc; loc; loc = loc->next)
12326 *locp++ = loc;
12327 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12328 bp_location_compare);
876fa593
JK
12329
12330 bp_location_target_extensions_update ();
74960c60 12331
4a64f543
MS
12332 /* Identify bp_location instances that are no longer present in the
12333 new list, and therefore should be freed. Note that it's not
12334 necessary that those locations should be removed from inferior --
12335 if there's another location at the same address (previously
12336 marked as duplicate), we don't need to remove/insert the
12337 location.
876fa593 12338
4a64f543
MS
12339 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12340 and former bp_location array state respectively. */
876fa593
JK
12341
12342 locp = bp_location;
12343 for (old_locp = old_location; old_locp < old_location + old_location_count;
12344 old_locp++)
74960c60 12345 {
876fa593 12346 struct bp_location *old_loc = *old_locp;
c7d46a38 12347 struct bp_location **loc2p;
876fa593 12348
e5dd4106 12349 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12350 not, we have to free it. */
c7d46a38 12351 int found_object = 0;
20874c92
VP
12352 /* Tells if the location should remain inserted in the target. */
12353 int keep_in_target = 0;
12354 int removed = 0;
876fa593 12355
4a64f543
MS
12356 /* Skip LOCP entries which will definitely never be needed.
12357 Stop either at or being the one matching OLD_LOC. */
876fa593 12358 while (locp < bp_location + bp_location_count
c7d46a38 12359 && (*locp)->address < old_loc->address)
876fa593 12360 locp++;
c7d46a38
PA
12361
12362 for (loc2p = locp;
12363 (loc2p < bp_location + bp_location_count
12364 && (*loc2p)->address == old_loc->address);
12365 loc2p++)
12366 {
b775012e
LM
12367 /* Check if this is a new/duplicated location or a duplicated
12368 location that had its condition modified. If so, we want to send
12369 its condition to the target if evaluation of conditions is taking
12370 place there. */
12371 if ((*loc2p)->condition_changed == condition_modified
12372 && (last_addr != old_loc->address
12373 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12374 {
b775012e
LM
12375 force_breakpoint_reinsertion (*loc2p);
12376 last_pspace_num = old_loc->pspace->num;
c7d46a38 12377 }
b775012e
LM
12378
12379 if (*loc2p == old_loc)
12380 found_object = 1;
c7d46a38 12381 }
74960c60 12382
b775012e
LM
12383 /* We have already handled this address, update it so that we don't
12384 have to go through updates again. */
12385 last_addr = old_loc->address;
12386
12387 /* Target-side condition evaluation: Handle deleted locations. */
12388 if (!found_object)
12389 force_breakpoint_reinsertion (old_loc);
12390
4a64f543
MS
12391 /* If this location is no longer present, and inserted, look if
12392 there's maybe a new location at the same address. If so,
12393 mark that one inserted, and don't remove this one. This is
12394 needed so that we don't have a time window where a breakpoint
12395 at certain location is not inserted. */
74960c60 12396
876fa593 12397 if (old_loc->inserted)
0d381245 12398 {
4a64f543
MS
12399 /* If the location is inserted now, we might have to remove
12400 it. */
74960c60 12401
876fa593 12402 if (found_object && should_be_inserted (old_loc))
74960c60 12403 {
4a64f543
MS
12404 /* The location is still present in the location list,
12405 and still should be inserted. Don't do anything. */
20874c92 12406 keep_in_target = 1;
74960c60
VP
12407 }
12408 else
12409 {
b775012e
LM
12410 /* This location still exists, but it won't be kept in the
12411 target since it may have been disabled. We proceed to
12412 remove its target-side condition. */
12413
4a64f543
MS
12414 /* The location is either no longer present, or got
12415 disabled. See if there's another location at the
12416 same address, in which case we don't need to remove
12417 this one from the target. */
876fa593 12418
2bdf28a0 12419 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12420 if (breakpoint_address_is_meaningful (old_loc->owner))
12421 {
876fa593 12422 for (loc2p = locp;
c7d46a38
PA
12423 (loc2p < bp_location + bp_location_count
12424 && (*loc2p)->address == old_loc->address);
876fa593
JK
12425 loc2p++)
12426 {
12427 struct bp_location *loc2 = *loc2p;
12428
2d134ed3 12429 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12430 {
85d721b8
PA
12431 /* Read watchpoint locations are switched to
12432 access watchpoints, if the former are not
12433 supported, but the latter are. */
12434 if (is_hardware_watchpoint (old_loc->owner))
12435 {
12436 gdb_assert (is_hardware_watchpoint (loc2->owner));
12437 loc2->watchpoint_type = old_loc->watchpoint_type;
12438 }
12439
934709f0
PW
12440 /* loc2 is a duplicated location. We need to check
12441 if it should be inserted in case it will be
12442 unduplicated. */
12443 if (loc2 != old_loc
12444 && unduplicated_should_be_inserted (loc2))
c7d46a38 12445 {
934709f0 12446 swap_insertion (old_loc, loc2);
c7d46a38
PA
12447 keep_in_target = 1;
12448 break;
12449 }
876fa593
JK
12450 }
12451 }
12452 }
74960c60
VP
12453 }
12454
20874c92
VP
12455 if (!keep_in_target)
12456 {
876fa593 12457 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12458 {
4a64f543
MS
12459 /* This is just about all we can do. We could keep
12460 this location on the global list, and try to
12461 remove it next time, but there's no particular
12462 reason why we will succeed next time.
20874c92 12463
4a64f543
MS
12464 Note that at this point, old_loc->owner is still
12465 valid, as delete_breakpoint frees the breakpoint
12466 only after calling us. */
3e43a32a
MS
12467 printf_filtered (_("warning: Error removing "
12468 "breakpoint %d\n"),
876fa593 12469 old_loc->owner->number);
20874c92
VP
12470 }
12471 removed = 1;
12472 }
0d381245 12473 }
74960c60
VP
12474
12475 if (!found_object)
1c5cfe86 12476 {
fbea99ea 12477 if (removed && target_is_non_stop_p ()
1cf4d951 12478 && need_moribund_for_location_type (old_loc))
20874c92 12479 {
db82e815
PA
12480 /* This location was removed from the target. In
12481 non-stop mode, a race condition is possible where
12482 we've removed a breakpoint, but stop events for that
12483 breakpoint are already queued and will arrive later.
12484 We apply an heuristic to be able to distinguish such
12485 SIGTRAPs from other random SIGTRAPs: we keep this
12486 breakpoint location for a bit, and will retire it
12487 after we see some number of events. The theory here
12488 is that reporting of events should, "on the average",
12489 be fair, so after a while we'll see events from all
12490 threads that have anything of interest, and no longer
12491 need to keep this breakpoint location around. We
12492 don't hold locations forever so to reduce chances of
12493 mistaking a non-breakpoint SIGTRAP for a breakpoint
12494 SIGTRAP.
12495
12496 The heuristic failing can be disastrous on
12497 decr_pc_after_break targets.
12498
12499 On decr_pc_after_break targets, like e.g., x86-linux,
12500 if we fail to recognize a late breakpoint SIGTRAP,
12501 because events_till_retirement has reached 0 too
12502 soon, we'll fail to do the PC adjustment, and report
12503 a random SIGTRAP to the user. When the user resumes
12504 the inferior, it will most likely immediately crash
2dec564e 12505 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12506 corrupted, because of being resumed e.g., in the
12507 middle of a multi-byte instruction, or skipped a
12508 one-byte instruction. This was actually seen happen
12509 on native x86-linux, and should be less rare on
12510 targets that do not support new thread events, like
12511 remote, due to the heuristic depending on
12512 thread_count.
12513
12514 Mistaking a random SIGTRAP for a breakpoint trap
12515 causes similar symptoms (PC adjustment applied when
12516 it shouldn't), but then again, playing with SIGTRAPs
12517 behind the debugger's back is asking for trouble.
12518
12519 Since hardware watchpoint traps are always
12520 distinguishable from other traps, so we don't need to
12521 apply keep hardware watchpoint moribund locations
12522 around. We simply always ignore hardware watchpoint
12523 traps we can no longer explain. */
12524
876fa593
JK
12525 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12526 old_loc->owner = NULL;
20874c92 12527
876fa593 12528 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12529 }
12530 else
f431efe5
PA
12531 {
12532 old_loc->owner = NULL;
12533 decref_bp_location (&old_loc);
12534 }
20874c92 12535 }
74960c60 12536 }
1c5cfe86 12537
348d480f
PA
12538 /* Rescan breakpoints at the same address and section, marking the
12539 first one as "first" and any others as "duplicates". This is so
12540 that the bpt instruction is only inserted once. If we have a
12541 permanent breakpoint at the same place as BPT, make that one the
12542 official one, and the rest as duplicates. Permanent breakpoints
12543 are sorted first for the same address.
12544
12545 Do the same for hardware watchpoints, but also considering the
12546 watchpoint's type (regular/access/read) and length. */
12547
12548 bp_loc_first = NULL;
12549 wp_loc_first = NULL;
12550 awp_loc_first = NULL;
12551 rwp_loc_first = NULL;
12552 ALL_BP_LOCATIONS (loc, locp)
12553 {
12554 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12555 non-NULL. */
348d480f 12556 struct bp_location **loc_first_p;
d3fbdd86 12557 b = loc->owner;
348d480f 12558
6f380991 12559 if (!unduplicated_should_be_inserted (loc)
348d480f 12560 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12561 /* Don't detect duplicate for tracepoint locations because they are
12562 never duplicated. See the comments in field `duplicate' of
12563 `struct bp_location'. */
348d480f 12564 || is_tracepoint (b))
b775012e
LM
12565 {
12566 /* Clear the condition modification flag. */
12567 loc->condition_changed = condition_unchanged;
12568 continue;
12569 }
348d480f 12570
348d480f
PA
12571 if (b->type == bp_hardware_watchpoint)
12572 loc_first_p = &wp_loc_first;
12573 else if (b->type == bp_read_watchpoint)
12574 loc_first_p = &rwp_loc_first;
12575 else if (b->type == bp_access_watchpoint)
12576 loc_first_p = &awp_loc_first;
12577 else
12578 loc_first_p = &bp_loc_first;
12579
12580 if (*loc_first_p == NULL
12581 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12582 || !breakpoint_locations_match (loc, *loc_first_p))
12583 {
12584 *loc_first_p = loc;
12585 loc->duplicate = 0;
b775012e
LM
12586
12587 if (is_breakpoint (loc->owner) && loc->condition_changed)
12588 {
12589 loc->needs_update = 1;
12590 /* Clear the condition modification flag. */
12591 loc->condition_changed = condition_unchanged;
12592 }
348d480f
PA
12593 continue;
12594 }
12595
934709f0
PW
12596
12597 /* This and the above ensure the invariant that the first location
12598 is not duplicated, and is the inserted one.
12599 All following are marked as duplicated, and are not inserted. */
12600 if (loc->inserted)
12601 swap_insertion (loc, *loc_first_p);
348d480f
PA
12602 loc->duplicate = 1;
12603
b775012e
LM
12604 /* Clear the condition modification flag. */
12605 loc->condition_changed = condition_unchanged;
348d480f
PA
12606 }
12607
a25a5a45 12608 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12609 {
04086b45 12610 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12611 insert_breakpoint_locations ();
12612 else
12613 {
44702360
PA
12614 /* Even though the caller told us to not insert new
12615 locations, we may still need to update conditions on the
12616 target's side of breakpoints that were already inserted
12617 if the target is evaluating breakpoint conditions. We
b775012e
LM
12618 only update conditions for locations that are marked
12619 "needs_update". */
12620 update_inserted_breakpoint_locations ();
12621 }
12622 }
348d480f 12623
04086b45 12624 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12625 download_tracepoint_locations ();
12626
348d480f
PA
12627 do_cleanups (cleanups);
12628}
12629
12630void
12631breakpoint_retire_moribund (void)
12632{
12633 struct bp_location *loc;
12634 int ix;
12635
12636 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12637 if (--(loc->events_till_retirement) == 0)
12638 {
12639 decref_bp_location (&loc);
12640 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12641 --ix;
12642 }
12643}
12644
12645static void
44702360 12646update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12647{
348d480f 12648
492d29ea
PA
12649 TRY
12650 {
12651 update_global_location_list (insert_mode);
12652 }
12653 CATCH (e, RETURN_MASK_ERROR)
12654 {
12655 }
12656 END_CATCH
348d480f
PA
12657}
12658
12659/* Clear BKP from a BPS. */
12660
12661static void
12662bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12663{
12664 bpstat bs;
12665
12666 for (bs = bps; bs; bs = bs->next)
12667 if (bs->breakpoint_at == bpt)
12668 {
12669 bs->breakpoint_at = NULL;
12670 bs->old_val = NULL;
12671 /* bs->commands will be freed later. */
12672 }
12673}
12674
12675/* Callback for iterate_over_threads. */
12676static int
12677bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12678{
12679 struct breakpoint *bpt = data;
12680
12681 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12682 return 0;
12683}
12684
12685/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12686 callbacks. */
12687
12688static void
12689say_where (struct breakpoint *b)
12690{
12691 struct value_print_options opts;
12692
12693 get_user_print_options (&opts);
12694
12695 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12696 single string. */
12697 if (b->loc == NULL)
12698 {
f00aae0f
KS
12699 /* For pending locations, the output differs slightly based
12700 on b->extra_string. If this is non-NULL, it contains either
12701 a condition or dprintf arguments. */
12702 if (b->extra_string == NULL)
12703 {
12704 printf_filtered (_(" (%s) pending."),
12705 event_location_to_string (b->location));
12706 }
12707 else if (b->type == bp_dprintf)
12708 {
12709 printf_filtered (_(" (%s,%s) pending."),
12710 event_location_to_string (b->location),
12711 b->extra_string);
12712 }
12713 else
12714 {
12715 printf_filtered (_(" (%s %s) pending."),
12716 event_location_to_string (b->location),
12717 b->extra_string);
12718 }
348d480f
PA
12719 }
12720 else
12721 {
2f202fde 12722 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12723 {
12724 printf_filtered (" at ");
12725 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12726 gdb_stdout);
12727 }
2f202fde 12728 if (b->loc->symtab != NULL)
f8eba3c6
TT
12729 {
12730 /* If there is a single location, we can print the location
12731 more nicely. */
12732 if (b->loc->next == NULL)
12733 printf_filtered (": file %s, line %d.",
05cba821
JK
12734 symtab_to_filename_for_display (b->loc->symtab),
12735 b->loc->line_number);
f8eba3c6
TT
12736 else
12737 /* This is not ideal, but each location may have a
12738 different file name, and this at least reflects the
12739 real situation somewhat. */
f00aae0f
KS
12740 printf_filtered (": %s.",
12741 event_location_to_string (b->location));
f8eba3c6 12742 }
348d480f
PA
12743
12744 if (b->loc->next)
12745 {
12746 struct bp_location *loc = b->loc;
12747 int n = 0;
12748 for (; loc; loc = loc->next)
12749 ++n;
12750 printf_filtered (" (%d locations)", n);
12751 }
12752 }
12753}
12754
348d480f
PA
12755/* Default bp_location_ops methods. */
12756
12757static void
12758bp_location_dtor (struct bp_location *self)
12759{
12760 xfree (self->cond);
b775012e
LM
12761 if (self->cond_bytecode)
12762 free_agent_expr (self->cond_bytecode);
348d480f 12763 xfree (self->function_name);
8b4f3082
PA
12764
12765 VEC_free (agent_expr_p, self->target_info.conditions);
12766 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12767}
12768
12769static const struct bp_location_ops bp_location_ops =
12770{
12771 bp_location_dtor
12772};
12773
2060206e
PA
12774/* Default breakpoint_ops methods all breakpoint_ops ultimately
12775 inherit from. */
348d480f 12776
2060206e
PA
12777static void
12778base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12779{
12780 decref_counted_command_line (&self->commands);
12781 xfree (self->cond_string);
fb81d016 12782 xfree (self->extra_string);
f8eba3c6 12783 xfree (self->filter);
f00aae0f
KS
12784 delete_event_location (self->location);
12785 delete_event_location (self->location_range_end);
348d480f
PA
12786}
12787
2060206e
PA
12788static struct bp_location *
12789base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12790{
12791 struct bp_location *loc;
12792
12793 loc = XNEW (struct bp_location);
12794 init_bp_location (loc, &bp_location_ops, self);
12795 return loc;
12796}
12797
2060206e
PA
12798static void
12799base_breakpoint_re_set (struct breakpoint *b)
12800{
12801 /* Nothing to re-set. */
12802}
12803
12804#define internal_error_pure_virtual_called() \
12805 gdb_assert_not_reached ("pure virtual function called")
12806
12807static int
12808base_breakpoint_insert_location (struct bp_location *bl)
12809{
12810 internal_error_pure_virtual_called ();
12811}
12812
12813static int
12814base_breakpoint_remove_location (struct bp_location *bl)
12815{
12816 internal_error_pure_virtual_called ();
12817}
12818
12819static int
12820base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12821 struct address_space *aspace,
09ac7c10
TT
12822 CORE_ADDR bp_addr,
12823 const struct target_waitstatus *ws)
2060206e
PA
12824{
12825 internal_error_pure_virtual_called ();
12826}
12827
12828static void
12829base_breakpoint_check_status (bpstat bs)
12830{
12831 /* Always stop. */
12832}
12833
12834/* A "works_in_software_mode" breakpoint_ops method that just internal
12835 errors. */
12836
12837static int
12838base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12839{
12840 internal_error_pure_virtual_called ();
12841}
12842
12843/* A "resources_needed" breakpoint_ops method that just internal
12844 errors. */
12845
12846static int
12847base_breakpoint_resources_needed (const struct bp_location *bl)
12848{
12849 internal_error_pure_virtual_called ();
12850}
12851
12852static enum print_stop_action
12853base_breakpoint_print_it (bpstat bs)
12854{
12855 internal_error_pure_virtual_called ();
12856}
12857
12858static void
12859base_breakpoint_print_one_detail (const struct breakpoint *self,
12860 struct ui_out *uiout)
12861{
12862 /* nothing */
12863}
12864
12865static void
12866base_breakpoint_print_mention (struct breakpoint *b)
12867{
12868 internal_error_pure_virtual_called ();
12869}
12870
12871static void
12872base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12873{
12874 internal_error_pure_virtual_called ();
12875}
12876
983af33b 12877static void
f00aae0f
KS
12878base_breakpoint_create_sals_from_location
12879 (const struct event_location *location,
12880 struct linespec_result *canonical,
12881 enum bptype type_wanted)
983af33b
SDJ
12882{
12883 internal_error_pure_virtual_called ();
12884}
12885
12886static void
12887base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12888 struct linespec_result *c,
983af33b 12889 char *cond_string,
e7e0cddf 12890 char *extra_string,
983af33b
SDJ
12891 enum bptype type_wanted,
12892 enum bpdisp disposition,
12893 int thread,
12894 int task, int ignore_count,
12895 const struct breakpoint_ops *o,
12896 int from_tty, int enabled,
44f238bb 12897 int internal, unsigned flags)
983af33b
SDJ
12898{
12899 internal_error_pure_virtual_called ();
12900}
12901
12902static void
f00aae0f
KS
12903base_breakpoint_decode_location (struct breakpoint *b,
12904 const struct event_location *location,
983af33b
SDJ
12905 struct symtabs_and_lines *sals)
12906{
12907 internal_error_pure_virtual_called ();
12908}
12909
ab04a2af
TT
12910/* The default 'explains_signal' method. */
12911
47591c29 12912static int
427cd150 12913base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12914{
47591c29 12915 return 1;
ab04a2af
TT
12916}
12917
9d6e6e84
HZ
12918/* The default "after_condition_true" method. */
12919
12920static void
12921base_breakpoint_after_condition_true (struct bpstats *bs)
12922{
12923 /* Nothing to do. */
12924}
12925
ab04a2af 12926struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
12927{
12928 base_breakpoint_dtor,
12929 base_breakpoint_allocate_location,
12930 base_breakpoint_re_set,
12931 base_breakpoint_insert_location,
12932 base_breakpoint_remove_location,
12933 base_breakpoint_breakpoint_hit,
12934 base_breakpoint_check_status,
12935 base_breakpoint_resources_needed,
12936 base_breakpoint_works_in_software_mode,
12937 base_breakpoint_print_it,
12938 NULL,
12939 base_breakpoint_print_one_detail,
12940 base_breakpoint_print_mention,
983af33b 12941 base_breakpoint_print_recreate,
5f700d83 12942 base_breakpoint_create_sals_from_location,
983af33b 12943 base_breakpoint_create_breakpoints_sal,
5f700d83 12944 base_breakpoint_decode_location,
9d6e6e84
HZ
12945 base_breakpoint_explains_signal,
12946 base_breakpoint_after_condition_true,
2060206e
PA
12947};
12948
12949/* Default breakpoint_ops methods. */
12950
12951static void
348d480f
PA
12952bkpt_re_set (struct breakpoint *b)
12953{
06edf0c0 12954 /* FIXME: is this still reachable? */
f00aae0f 12955 if (event_location_empty_p (b->location))
06edf0c0 12956 {
f00aae0f 12957 /* Anything without a location can't be re-set. */
348d480f 12958 delete_breakpoint (b);
06edf0c0 12959 return;
348d480f 12960 }
06edf0c0
PA
12961
12962 breakpoint_re_set_default (b);
348d480f
PA
12963}
12964
2060206e 12965static int
348d480f
PA
12966bkpt_insert_location (struct bp_location *bl)
12967{
12968 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12969 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12970 else
7c16b83e 12971 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12972}
12973
2060206e 12974static int
348d480f
PA
12975bkpt_remove_location (struct bp_location *bl)
12976{
12977 if (bl->loc_type == bp_loc_hardware_breakpoint)
12978 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12979 else
7c16b83e 12980 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12981}
12982
2060206e 12983static int
348d480f 12984bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12985 struct address_space *aspace, CORE_ADDR bp_addr,
12986 const struct target_waitstatus *ws)
348d480f 12987{
09ac7c10 12988 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12989 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12990 return 0;
12991
348d480f
PA
12992 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12993 aspace, bp_addr))
12994 return 0;
12995
12996 if (overlay_debugging /* unmapped overlay section */
12997 && section_is_overlay (bl->section)
12998 && !section_is_mapped (bl->section))
12999 return 0;
13000
13001 return 1;
13002}
13003
cd1608cc
PA
13004static int
13005dprintf_breakpoint_hit (const struct bp_location *bl,
13006 struct address_space *aspace, CORE_ADDR bp_addr,
13007 const struct target_waitstatus *ws)
13008{
13009 if (dprintf_style == dprintf_style_agent
13010 && target_can_run_breakpoint_commands ())
13011 {
13012 /* An agent-style dprintf never causes a stop. If we see a trap
13013 for this address it must be for a breakpoint that happens to
13014 be set at the same address. */
13015 return 0;
13016 }
13017
13018 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13019}
13020
2060206e 13021static int
348d480f
PA
13022bkpt_resources_needed (const struct bp_location *bl)
13023{
13024 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13025
13026 return 1;
13027}
13028
2060206e 13029static enum print_stop_action
348d480f
PA
13030bkpt_print_it (bpstat bs)
13031{
348d480f
PA
13032 struct breakpoint *b;
13033 const struct bp_location *bl;
001c8c33 13034 int bp_temp;
79a45e25 13035 struct ui_out *uiout = current_uiout;
348d480f
PA
13036
13037 gdb_assert (bs->bp_location_at != NULL);
13038
13039 bl = bs->bp_location_at;
13040 b = bs->breakpoint_at;
13041
001c8c33
PA
13042 bp_temp = b->disposition == disp_del;
13043 if (bl->address != bl->requested_address)
13044 breakpoint_adjustment_warning (bl->requested_address,
13045 bl->address,
13046 b->number, 1);
13047 annotate_breakpoint (b->number);
13048 if (bp_temp)
13049 ui_out_text (uiout, "\nTemporary breakpoint ");
13050 else
13051 ui_out_text (uiout, "\nBreakpoint ");
13052 if (ui_out_is_mi_like_p (uiout))
348d480f 13053 {
001c8c33
PA
13054 ui_out_field_string (uiout, "reason",
13055 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13056 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13057 }
001c8c33
PA
13058 ui_out_field_int (uiout, "bkptno", b->number);
13059 ui_out_text (uiout, ", ");
06edf0c0 13060
001c8c33 13061 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13062}
13063
2060206e 13064static void
06edf0c0
PA
13065bkpt_print_mention (struct breakpoint *b)
13066{
79a45e25 13067 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13068 return;
13069
13070 switch (b->type)
13071 {
13072 case bp_breakpoint:
13073 case bp_gnu_ifunc_resolver:
13074 if (b->disposition == disp_del)
13075 printf_filtered (_("Temporary breakpoint"));
13076 else
13077 printf_filtered (_("Breakpoint"));
13078 printf_filtered (_(" %d"), b->number);
13079 if (b->type == bp_gnu_ifunc_resolver)
13080 printf_filtered (_(" at gnu-indirect-function resolver"));
13081 break;
13082 case bp_hardware_breakpoint:
13083 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13084 break;
e7e0cddf
SS
13085 case bp_dprintf:
13086 printf_filtered (_("Dprintf %d"), b->number);
13087 break;
06edf0c0
PA
13088 }
13089
13090 say_where (b);
13091}
13092
2060206e 13093static void
06edf0c0
PA
13094bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13095{
13096 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13097 fprintf_unfiltered (fp, "tbreak");
13098 else if (tp->type == bp_breakpoint)
13099 fprintf_unfiltered (fp, "break");
13100 else if (tp->type == bp_hardware_breakpoint
13101 && tp->disposition == disp_del)
13102 fprintf_unfiltered (fp, "thbreak");
13103 else if (tp->type == bp_hardware_breakpoint)
13104 fprintf_unfiltered (fp, "hbreak");
13105 else
13106 internal_error (__FILE__, __LINE__,
13107 _("unhandled breakpoint type %d"), (int) tp->type);
13108
f00aae0f
KS
13109 fprintf_unfiltered (fp, " %s",
13110 event_location_to_string (tp->location));
13111
13112 /* Print out extra_string if this breakpoint is pending. It might
13113 contain, for example, conditions that were set by the user. */
13114 if (tp->loc == NULL && tp->extra_string != NULL)
13115 fprintf_unfiltered (fp, " %s", tp->extra_string);
13116
dd11a36c 13117 print_recreate_thread (tp, fp);
06edf0c0
PA
13118}
13119
983af33b 13120static void
f00aae0f
KS
13121bkpt_create_sals_from_location (const struct event_location *location,
13122 struct linespec_result *canonical,
13123 enum bptype type_wanted)
983af33b 13124{
f00aae0f 13125 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13126}
13127
13128static void
13129bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13130 struct linespec_result *canonical,
983af33b 13131 char *cond_string,
e7e0cddf 13132 char *extra_string,
983af33b
SDJ
13133 enum bptype type_wanted,
13134 enum bpdisp disposition,
13135 int thread,
13136 int task, int ignore_count,
13137 const struct breakpoint_ops *ops,
13138 int from_tty, int enabled,
44f238bb 13139 int internal, unsigned flags)
983af33b 13140{
023fa29b 13141 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13142 cond_string, extra_string,
13143 type_wanted,
983af33b
SDJ
13144 disposition, thread, task,
13145 ignore_count, ops, from_tty,
44f238bb 13146 enabled, internal, flags);
983af33b
SDJ
13147}
13148
13149static void
f00aae0f
KS
13150bkpt_decode_location (struct breakpoint *b,
13151 const struct event_location *location,
983af33b
SDJ
13152 struct symtabs_and_lines *sals)
13153{
f00aae0f 13154 decode_location_default (b, location, sals);
983af33b
SDJ
13155}
13156
06edf0c0
PA
13157/* Virtual table for internal breakpoints. */
13158
13159static void
13160internal_bkpt_re_set (struct breakpoint *b)
13161{
13162 switch (b->type)
13163 {
13164 /* Delete overlay event and longjmp master breakpoints; they
13165 will be reset later by breakpoint_re_set. */
13166 case bp_overlay_event:
13167 case bp_longjmp_master:
13168 case bp_std_terminate_master:
13169 case bp_exception_master:
13170 delete_breakpoint (b);
13171 break;
13172
13173 /* This breakpoint is special, it's set up when the inferior
13174 starts and we really don't want to touch it. */
13175 case bp_shlib_event:
13176
13177 /* Like bp_shlib_event, this breakpoint type is special. Once
13178 it is set up, we do not want to touch it. */
13179 case bp_thread_event:
13180 break;
13181 }
13182}
13183
13184static void
13185internal_bkpt_check_status (bpstat bs)
13186{
a9b3a50f
PA
13187 if (bs->breakpoint_at->type == bp_shlib_event)
13188 {
13189 /* If requested, stop when the dynamic linker notifies GDB of
13190 events. This allows the user to get control and place
13191 breakpoints in initializer routines for dynamically loaded
13192 objects (among other things). */
13193 bs->stop = stop_on_solib_events;
13194 bs->print = stop_on_solib_events;
13195 }
13196 else
13197 bs->stop = 0;
06edf0c0
PA
13198}
13199
13200static enum print_stop_action
13201internal_bkpt_print_it (bpstat bs)
13202{
06edf0c0 13203 struct breakpoint *b;
06edf0c0 13204
06edf0c0
PA
13205 b = bs->breakpoint_at;
13206
06edf0c0
PA
13207 switch (b->type)
13208 {
348d480f
PA
13209 case bp_shlib_event:
13210 /* Did we stop because the user set the stop_on_solib_events
13211 variable? (If so, we report this as a generic, "Stopped due
13212 to shlib event" message.) */
edcc5120 13213 print_solib_event (0);
348d480f
PA
13214 break;
13215
13216 case bp_thread_event:
13217 /* Not sure how we will get here.
13218 GDB should not stop for these breakpoints. */
13219 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13220 break;
13221
13222 case bp_overlay_event:
13223 /* By analogy with the thread event, GDB should not stop for these. */
13224 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13225 break;
13226
13227 case bp_longjmp_master:
13228 /* These should never be enabled. */
13229 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13230 break;
13231
13232 case bp_std_terminate_master:
13233 /* These should never be enabled. */
13234 printf_filtered (_("std::terminate Master Breakpoint: "
13235 "gdb should not stop!\n"));
348d480f
PA
13236 break;
13237
13238 case bp_exception_master:
13239 /* These should never be enabled. */
13240 printf_filtered (_("Exception Master Breakpoint: "
13241 "gdb should not stop!\n"));
06edf0c0
PA
13242 break;
13243 }
13244
001c8c33 13245 return PRINT_NOTHING;
06edf0c0
PA
13246}
13247
13248static void
13249internal_bkpt_print_mention (struct breakpoint *b)
13250{
13251 /* Nothing to mention. These breakpoints are internal. */
13252}
13253
06edf0c0
PA
13254/* Virtual table for momentary breakpoints */
13255
13256static void
13257momentary_bkpt_re_set (struct breakpoint *b)
13258{
13259 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13260 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13261 Otherwise these should have been blown away via the cleanup chain
13262 or by breakpoint_init_inferior when we rerun the executable. */
13263}
13264
13265static void
13266momentary_bkpt_check_status (bpstat bs)
13267{
13268 /* Nothing. The point of these breakpoints is causing a stop. */
13269}
13270
13271static enum print_stop_action
13272momentary_bkpt_print_it (bpstat bs)
13273{
001c8c33 13274 return PRINT_UNKNOWN;
348d480f
PA
13275}
13276
06edf0c0
PA
13277static void
13278momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13279{
06edf0c0 13280 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13281}
13282
e2e4d78b
JK
13283/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13284
13285 It gets cleared already on the removal of the first one of such placed
13286 breakpoints. This is OK as they get all removed altogether. */
13287
13288static void
13289longjmp_bkpt_dtor (struct breakpoint *self)
13290{
13291 struct thread_info *tp = find_thread_id (self->thread);
13292
13293 if (tp)
13294 tp->initiating_frame = null_frame_id;
13295
13296 momentary_breakpoint_ops.dtor (self);
13297}
13298
55aa24fb
SDJ
13299/* Specific methods for probe breakpoints. */
13300
13301static int
13302bkpt_probe_insert_location (struct bp_location *bl)
13303{
13304 int v = bkpt_insert_location (bl);
13305
13306 if (v == 0)
13307 {
13308 /* The insertion was successful, now let's set the probe's semaphore
13309 if needed. */
0ea5cda8
SDJ
13310 if (bl->probe.probe->pops->set_semaphore != NULL)
13311 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13312 bl->probe.objfile,
13313 bl->gdbarch);
55aa24fb
SDJ
13314 }
13315
13316 return v;
13317}
13318
13319static int
13320bkpt_probe_remove_location (struct bp_location *bl)
13321{
13322 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13323 if (bl->probe.probe->pops->clear_semaphore != NULL)
13324 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13325 bl->probe.objfile,
13326 bl->gdbarch);
55aa24fb
SDJ
13327
13328 return bkpt_remove_location (bl);
13329}
13330
13331static void
f00aae0f 13332bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13333 struct linespec_result *canonical,
f00aae0f 13334 enum bptype type_wanted)
55aa24fb
SDJ
13335{
13336 struct linespec_sals lsal;
13337
f00aae0f
KS
13338 lsal.sals = parse_probes (location, canonical);
13339 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
55aa24fb
SDJ
13340 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13341}
13342
13343static void
f00aae0f
KS
13344bkpt_probe_decode_location (struct breakpoint *b,
13345 const struct event_location *location,
55aa24fb
SDJ
13346 struct symtabs_and_lines *sals)
13347{
f00aae0f 13348 *sals = parse_probes (location, NULL);
55aa24fb
SDJ
13349 if (!sals->sals)
13350 error (_("probe not found"));
13351}
13352
348d480f 13353/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13354
348d480f
PA
13355static void
13356tracepoint_re_set (struct breakpoint *b)
13357{
13358 breakpoint_re_set_default (b);
13359}
876fa593 13360
348d480f
PA
13361static int
13362tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13363 struct address_space *aspace, CORE_ADDR bp_addr,
13364 const struct target_waitstatus *ws)
348d480f
PA
13365{
13366 /* By definition, the inferior does not report stops at
13367 tracepoints. */
13368 return 0;
74960c60
VP
13369}
13370
13371static void
348d480f
PA
13372tracepoint_print_one_detail (const struct breakpoint *self,
13373 struct ui_out *uiout)
74960c60 13374{
d9b3f62e
PA
13375 struct tracepoint *tp = (struct tracepoint *) self;
13376 if (tp->static_trace_marker_id)
348d480f
PA
13377 {
13378 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13379
348d480f
PA
13380 ui_out_text (uiout, "\tmarker id is ");
13381 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13382 tp->static_trace_marker_id);
348d480f
PA
13383 ui_out_text (uiout, "\n");
13384 }
0d381245
VP
13385}
13386
a474d7c2 13387static void
348d480f 13388tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13389{
79a45e25 13390 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13391 return;
cc59ec59 13392
348d480f
PA
13393 switch (b->type)
13394 {
13395 case bp_tracepoint:
13396 printf_filtered (_("Tracepoint"));
13397 printf_filtered (_(" %d"), b->number);
13398 break;
13399 case bp_fast_tracepoint:
13400 printf_filtered (_("Fast tracepoint"));
13401 printf_filtered (_(" %d"), b->number);
13402 break;
13403 case bp_static_tracepoint:
13404 printf_filtered (_("Static tracepoint"));
13405 printf_filtered (_(" %d"), b->number);
13406 break;
13407 default:
13408 internal_error (__FILE__, __LINE__,
13409 _("unhandled tracepoint type %d"), (int) b->type);
13410 }
13411
13412 say_where (b);
a474d7c2
PA
13413}
13414
348d480f 13415static void
d9b3f62e 13416tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13417{
d9b3f62e
PA
13418 struct tracepoint *tp = (struct tracepoint *) self;
13419
13420 if (self->type == bp_fast_tracepoint)
348d480f 13421 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13422 if (self->type == bp_static_tracepoint)
348d480f 13423 fprintf_unfiltered (fp, "strace");
d9b3f62e 13424 else if (self->type == bp_tracepoint)
348d480f
PA
13425 fprintf_unfiltered (fp, "trace");
13426 else
13427 internal_error (__FILE__, __LINE__,
d9b3f62e 13428 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13429
f00aae0f
KS
13430 fprintf_unfiltered (fp, " %s",
13431 event_location_to_string (self->location));
d9b3f62e
PA
13432 print_recreate_thread (self, fp);
13433
13434 if (tp->pass_count)
13435 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13436}
13437
983af33b 13438static void
f00aae0f
KS
13439tracepoint_create_sals_from_location (const struct event_location *location,
13440 struct linespec_result *canonical,
13441 enum bptype type_wanted)
983af33b 13442{
f00aae0f 13443 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13444}
13445
13446static void
13447tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13448 struct linespec_result *canonical,
983af33b 13449 char *cond_string,
e7e0cddf 13450 char *extra_string,
983af33b
SDJ
13451 enum bptype type_wanted,
13452 enum bpdisp disposition,
13453 int thread,
13454 int task, int ignore_count,
13455 const struct breakpoint_ops *ops,
13456 int from_tty, int enabled,
44f238bb 13457 int internal, unsigned flags)
983af33b 13458{
023fa29b 13459 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13460 cond_string, extra_string,
13461 type_wanted,
983af33b
SDJ
13462 disposition, thread, task,
13463 ignore_count, ops, from_tty,
44f238bb 13464 enabled, internal, flags);
983af33b
SDJ
13465}
13466
13467static void
f00aae0f
KS
13468tracepoint_decode_location (struct breakpoint *b,
13469 const struct event_location *location,
983af33b
SDJ
13470 struct symtabs_and_lines *sals)
13471{
f00aae0f 13472 decode_location_default (b, location, sals);
983af33b
SDJ
13473}
13474
2060206e 13475struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13476
55aa24fb
SDJ
13477/* The breakpoint_ops structure to be use on tracepoints placed in a
13478 static probe. */
13479
13480static void
f00aae0f
KS
13481tracepoint_probe_create_sals_from_location
13482 (const struct event_location *location,
13483 struct linespec_result *canonical,
13484 enum bptype type_wanted)
55aa24fb
SDJ
13485{
13486 /* We use the same method for breakpoint on probes. */
f00aae0f 13487 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13488}
13489
13490static void
f00aae0f
KS
13491tracepoint_probe_decode_location (struct breakpoint *b,
13492 const struct event_location *location,
55aa24fb
SDJ
13493 struct symtabs_and_lines *sals)
13494{
13495 /* We use the same method for breakpoint on probes. */
f00aae0f 13496 bkpt_probe_decode_location (b, location, sals);
55aa24fb
SDJ
13497}
13498
13499static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13500
5c2b4418
HZ
13501/* Dprintf breakpoint_ops methods. */
13502
13503static void
13504dprintf_re_set (struct breakpoint *b)
13505{
13506 breakpoint_re_set_default (b);
13507
f00aae0f
KS
13508 /* extra_string should never be non-NULL for dprintf. */
13509 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13510
13511 /* 1 - connect to target 1, that can run breakpoint commands.
13512 2 - create a dprintf, which resolves fine.
13513 3 - disconnect from target 1
13514 4 - connect to target 2, that can NOT run breakpoint commands.
13515
13516 After steps #3/#4, you'll want the dprintf command list to
13517 be updated, because target 1 and 2 may well return different
13518 answers for target_can_run_breakpoint_commands().
13519 Given absence of finer grained resetting, we get to do
13520 it all the time. */
13521 if (b->extra_string != NULL)
13522 update_dprintf_command_list (b);
13523}
13524
2d9442cc
HZ
13525/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13526
13527static void
13528dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13529{
f00aae0f
KS
13530 fprintf_unfiltered (fp, "dprintf %s,%s",
13531 event_location_to_string (tp->location),
2d9442cc
HZ
13532 tp->extra_string);
13533 print_recreate_thread (tp, fp);
13534}
13535
9d6e6e84
HZ
13536/* Implement the "after_condition_true" breakpoint_ops method for
13537 dprintf.
13538
13539 dprintf's are implemented with regular commands in their command
13540 list, but we run the commands here instead of before presenting the
13541 stop to the user, as dprintf's don't actually cause a stop. This
13542 also makes it so that the commands of multiple dprintfs at the same
13543 address are all handled. */
13544
13545static void
13546dprintf_after_condition_true (struct bpstats *bs)
13547{
13548 struct cleanup *old_chain;
13549 struct bpstats tmp_bs = { NULL };
13550 struct bpstats *tmp_bs_p = &tmp_bs;
13551
13552 /* dprintf's never cause a stop. This wasn't set in the
13553 check_status hook instead because that would make the dprintf's
13554 condition not be evaluated. */
13555 bs->stop = 0;
13556
13557 /* Run the command list here. Take ownership of it instead of
13558 copying. We never want these commands to run later in
13559 bpstat_do_actions, if a breakpoint that causes a stop happens to
13560 be set at same address as this dprintf, or even if running the
13561 commands here throws. */
13562 tmp_bs.commands = bs->commands;
13563 bs->commands = NULL;
13564 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13565
13566 bpstat_do_actions_1 (&tmp_bs_p);
13567
13568 /* 'tmp_bs.commands' will usually be NULL by now, but
13569 bpstat_do_actions_1 may return early without processing the whole
13570 list. */
13571 do_cleanups (old_chain);
13572}
13573
983af33b
SDJ
13574/* The breakpoint_ops structure to be used on static tracepoints with
13575 markers (`-m'). */
13576
13577static void
f00aae0f 13578strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13579 struct linespec_result *canonical,
f00aae0f 13580 enum bptype type_wanted)
983af33b
SDJ
13581{
13582 struct linespec_sals lsal;
f00aae0f
KS
13583 const char *arg_start, *arg;
13584 char *str;
13585 struct cleanup *cleanup;
983af33b 13586
f00aae0f
KS
13587 arg = arg_start = get_linespec_location (location);
13588 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13589
f00aae0f
KS
13590 str = savestring (arg_start, arg - arg_start);
13591 cleanup = make_cleanup (xfree, str);
13592 canonical->location = new_linespec_location (&str);
13593 do_cleanups (cleanup);
983af33b 13594
f00aae0f 13595 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
983af33b
SDJ
13596 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13597}
13598
13599static void
13600strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13601 struct linespec_result *canonical,
983af33b 13602 char *cond_string,
e7e0cddf 13603 char *extra_string,
983af33b
SDJ
13604 enum bptype type_wanted,
13605 enum bpdisp disposition,
13606 int thread,
13607 int task, int ignore_count,
13608 const struct breakpoint_ops *ops,
13609 int from_tty, int enabled,
44f238bb 13610 int internal, unsigned flags)
983af33b
SDJ
13611{
13612 int i;
52d361e1
YQ
13613 struct linespec_sals *lsal = VEC_index (linespec_sals,
13614 canonical->sals, 0);
983af33b
SDJ
13615
13616 /* If the user is creating a static tracepoint by marker id
13617 (strace -m MARKER_ID), then store the sals index, so that
13618 breakpoint_re_set can try to match up which of the newly
13619 found markers corresponds to this one, and, don't try to
13620 expand multiple locations for each sal, given than SALS
13621 already should contain all sals for MARKER_ID. */
13622
13623 for (i = 0; i < lsal->sals.nelts; ++i)
13624 {
13625 struct symtabs_and_lines expanded;
13626 struct tracepoint *tp;
13627 struct cleanup *old_chain;
f00aae0f 13628 struct event_location *location;
983af33b
SDJ
13629
13630 expanded.nelts = 1;
13631 expanded.sals = &lsal->sals.sals[i];
13632
f00aae0f
KS
13633 location = copy_event_location (canonical->location);
13634 old_chain = make_cleanup_delete_event_location (location);
983af33b
SDJ
13635
13636 tp = XCNEW (struct tracepoint);
13637 init_breakpoint_sal (&tp->base, gdbarch, expanded,
f00aae0f 13638 location, NULL,
e7e0cddf
SS
13639 cond_string, extra_string,
13640 type_wanted, disposition,
983af33b 13641 thread, task, ignore_count, ops,
44f238bb 13642 from_tty, enabled, internal, flags,
983af33b
SDJ
13643 canonical->special_display);
13644 /* Given that its possible to have multiple markers with
13645 the same string id, if the user is creating a static
13646 tracepoint by marker id ("strace -m MARKER_ID"), then
13647 store the sals index, so that breakpoint_re_set can
13648 try to match up which of the newly found markers
13649 corresponds to this one */
13650 tp->static_trace_marker_id_idx = i;
13651
13652 install_breakpoint (internal, &tp->base, 0);
13653
13654 discard_cleanups (old_chain);
13655 }
13656}
13657
13658static void
f00aae0f
KS
13659strace_marker_decode_location (struct breakpoint *b,
13660 const struct event_location *location,
983af33b
SDJ
13661 struct symtabs_and_lines *sals)
13662{
13663 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13664 const char *s = get_linespec_location (location);
983af33b 13665
f00aae0f 13666 *sals = decode_static_tracepoint_spec (&s);
983af33b
SDJ
13667 if (sals->nelts > tp->static_trace_marker_id_idx)
13668 {
13669 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13670 sals->nelts = 1;
13671 }
13672 else
13673 error (_("marker %s not found"), tp->static_trace_marker_id);
13674}
13675
13676static struct breakpoint_ops strace_marker_breakpoint_ops;
13677
13678static int
13679strace_marker_p (struct breakpoint *b)
13680{
13681 return b->ops == &strace_marker_breakpoint_ops;
13682}
13683
53a5351d 13684/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13685 structures. */
c906108c
SS
13686
13687void
fba45db2 13688delete_breakpoint (struct breakpoint *bpt)
c906108c 13689{
52f0bd74 13690 struct breakpoint *b;
c906108c 13691
8a3fe4f8 13692 gdb_assert (bpt != NULL);
c906108c 13693
4a64f543
MS
13694 /* Has this bp already been deleted? This can happen because
13695 multiple lists can hold pointers to bp's. bpstat lists are
13696 especial culprits.
13697
13698 One example of this happening is a watchpoint's scope bp. When
13699 the scope bp triggers, we notice that the watchpoint is out of
13700 scope, and delete it. We also delete its scope bp. But the
13701 scope bp is marked "auto-deleting", and is already on a bpstat.
13702 That bpstat is then checked for auto-deleting bp's, which are
13703 deleted.
13704
13705 A real solution to this problem might involve reference counts in
13706 bp's, and/or giving them pointers back to their referencing
13707 bpstat's, and teaching delete_breakpoint to only free a bp's
13708 storage when no more references were extent. A cheaper bandaid
13709 was chosen. */
c906108c
SS
13710 if (bpt->type == bp_none)
13711 return;
13712
4a64f543
MS
13713 /* At least avoid this stale reference until the reference counting
13714 of breakpoints gets resolved. */
d0fb5eae 13715 if (bpt->related_breakpoint != bpt)
e5a0a904 13716 {
d0fb5eae 13717 struct breakpoint *related;
3a5c3e22 13718 struct watchpoint *w;
d0fb5eae
JK
13719
13720 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13721 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13722 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13723 w = (struct watchpoint *) bpt;
13724 else
13725 w = NULL;
13726 if (w != NULL)
13727 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13728
13729 /* Unlink bpt from the bpt->related_breakpoint ring. */
13730 for (related = bpt; related->related_breakpoint != bpt;
13731 related = related->related_breakpoint);
13732 related->related_breakpoint = bpt->related_breakpoint;
13733 bpt->related_breakpoint = bpt;
e5a0a904
JK
13734 }
13735
a9634178
TJB
13736 /* watch_command_1 creates a watchpoint but only sets its number if
13737 update_watchpoint succeeds in creating its bp_locations. If there's
13738 a problem in that process, we'll be asked to delete the half-created
13739 watchpoint. In that case, don't announce the deletion. */
13740 if (bpt->number)
13741 observer_notify_breakpoint_deleted (bpt);
c906108c 13742
c906108c
SS
13743 if (breakpoint_chain == bpt)
13744 breakpoint_chain = bpt->next;
13745
c906108c
SS
13746 ALL_BREAKPOINTS (b)
13747 if (b->next == bpt)
c5aa993b
JM
13748 {
13749 b->next = bpt->next;
13750 break;
13751 }
c906108c 13752
f431efe5
PA
13753 /* Be sure no bpstat's are pointing at the breakpoint after it's
13754 been freed. */
13755 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13756 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13757 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13758 commands are associated with the bpstat; if we remove it here,
13759 then the later call to bpstat_do_actions (&stop_bpstat); in
13760 event-top.c won't do anything, and temporary breakpoints with
13761 commands won't work. */
13762
13763 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13764
4a64f543
MS
13765 /* Now that breakpoint is removed from breakpoint list, update the
13766 global location list. This will remove locations that used to
13767 belong to this breakpoint. Do this before freeing the breakpoint
13768 itself, since remove_breakpoint looks at location's owner. It
13769 might be better design to have location completely
13770 self-contained, but it's not the case now. */
44702360 13771 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13772
348d480f 13773 bpt->ops->dtor (bpt);
4a64f543
MS
13774 /* On the chance that someone will soon try again to delete this
13775 same bp, we mark it as deleted before freeing its storage. */
c906108c 13776 bpt->type = bp_none;
b8c9b27d 13777 xfree (bpt);
c906108c
SS
13778}
13779
4d6140d9
AC
13780static void
13781do_delete_breakpoint_cleanup (void *b)
13782{
13783 delete_breakpoint (b);
13784}
13785
13786struct cleanup *
13787make_cleanup_delete_breakpoint (struct breakpoint *b)
13788{
13789 return make_cleanup (do_delete_breakpoint_cleanup, b);
13790}
13791
51be5b68
PA
13792/* Iterator function to call a user-provided callback function once
13793 for each of B and its related breakpoints. */
13794
13795static void
13796iterate_over_related_breakpoints (struct breakpoint *b,
13797 void (*function) (struct breakpoint *,
13798 void *),
13799 void *data)
13800{
13801 struct breakpoint *related;
13802
13803 related = b;
13804 do
13805 {
13806 struct breakpoint *next;
13807
13808 /* FUNCTION may delete RELATED. */
13809 next = related->related_breakpoint;
13810
13811 if (next == related)
13812 {
13813 /* RELATED is the last ring entry. */
13814 function (related, data);
13815
13816 /* FUNCTION may have deleted it, so we'd never reach back to
13817 B. There's nothing left to do anyway, so just break
13818 out. */
13819 break;
13820 }
13821 else
13822 function (related, data);
13823
13824 related = next;
13825 }
13826 while (related != b);
13827}
95a42b64
TT
13828
13829static void
13830do_delete_breakpoint (struct breakpoint *b, void *ignore)
13831{
13832 delete_breakpoint (b);
13833}
13834
51be5b68
PA
13835/* A callback for map_breakpoint_numbers that calls
13836 delete_breakpoint. */
13837
13838static void
13839do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13840{
13841 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13842}
13843
c906108c 13844void
fba45db2 13845delete_command (char *arg, int from_tty)
c906108c 13846{
35df4500 13847 struct breakpoint *b, *b_tmp;
c906108c 13848
ea9365bb
TT
13849 dont_repeat ();
13850
c906108c
SS
13851 if (arg == 0)
13852 {
13853 int breaks_to_delete = 0;
13854
46c6471b
PA
13855 /* Delete all breakpoints if no argument. Do not delete
13856 internal breakpoints, these have to be deleted with an
13857 explicit breakpoint number argument. */
c5aa993b 13858 ALL_BREAKPOINTS (b)
46c6471b 13859 if (user_breakpoint_p (b))
973d738b
DJ
13860 {
13861 breaks_to_delete = 1;
13862 break;
13863 }
c906108c
SS
13864
13865 /* Ask user only if there are some breakpoints to delete. */
13866 if (!from_tty
e2e0b3e5 13867 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13868 {
35df4500 13869 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13870 if (user_breakpoint_p (b))
c5aa993b 13871 delete_breakpoint (b);
c906108c
SS
13872 }
13873 }
13874 else
51be5b68 13875 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13876}
13877
0d381245
VP
13878static int
13879all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 13880{
0d381245 13881 for (; loc; loc = loc->next)
8645ff69
UW
13882 if (!loc->shlib_disabled
13883 && !loc->pspace->executing_startup)
0d381245
VP
13884 return 0;
13885 return 1;
fe3f5fa8
VP
13886}
13887
776592bf
DE
13888/* Subroutine of update_breakpoint_locations to simplify it.
13889 Return non-zero if multiple fns in list LOC have the same name.
13890 Null names are ignored. */
13891
13892static int
13893ambiguous_names_p (struct bp_location *loc)
13894{
13895 struct bp_location *l;
13896 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13897 (int (*) (const void *,
13898 const void *)) streq,
776592bf
DE
13899 NULL, xcalloc, xfree);
13900
13901 for (l = loc; l != NULL; l = l->next)
13902 {
13903 const char **slot;
13904 const char *name = l->function_name;
13905
13906 /* Allow for some names to be NULL, ignore them. */
13907 if (name == NULL)
13908 continue;
13909
13910 slot = (const char **) htab_find_slot (htab, (const void *) name,
13911 INSERT);
4a64f543
MS
13912 /* NOTE: We can assume slot != NULL here because xcalloc never
13913 returns NULL. */
776592bf
DE
13914 if (*slot != NULL)
13915 {
13916 htab_delete (htab);
13917 return 1;
13918 }
13919 *slot = name;
13920 }
13921
13922 htab_delete (htab);
13923 return 0;
13924}
13925
0fb4aa4b
PA
13926/* When symbols change, it probably means the sources changed as well,
13927 and it might mean the static tracepoint markers are no longer at
13928 the same address or line numbers they used to be at last we
13929 checked. Losing your static tracepoints whenever you rebuild is
13930 undesirable. This function tries to resync/rematch gdb static
13931 tracepoints with the markers on the target, for static tracepoints
13932 that have not been set by marker id. Static tracepoint that have
13933 been set by marker id are reset by marker id in breakpoint_re_set.
13934 The heuristic is:
13935
13936 1) For a tracepoint set at a specific address, look for a marker at
13937 the old PC. If one is found there, assume to be the same marker.
13938 If the name / string id of the marker found is different from the
13939 previous known name, assume that means the user renamed the marker
13940 in the sources, and output a warning.
13941
13942 2) For a tracepoint set at a given line number, look for a marker
13943 at the new address of the old line number. If one is found there,
13944 assume to be the same marker. If the name / string id of the
13945 marker found is different from the previous known name, assume that
13946 means the user renamed the marker in the sources, and output a
13947 warning.
13948
13949 3) If a marker is no longer found at the same address or line, it
13950 may mean the marker no longer exists. But it may also just mean
13951 the code changed a bit. Maybe the user added a few lines of code
13952 that made the marker move up or down (in line number terms). Ask
13953 the target for info about the marker with the string id as we knew
13954 it. If found, update line number and address in the matching
13955 static tracepoint. This will get confused if there's more than one
13956 marker with the same ID (possible in UST, although unadvised
13957 precisely because it confuses tools). */
13958
13959static struct symtab_and_line
13960update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13961{
d9b3f62e 13962 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13963 struct static_tracepoint_marker marker;
13964 CORE_ADDR pc;
0fb4aa4b
PA
13965
13966 pc = sal.pc;
13967 if (sal.line)
13968 find_line_pc (sal.symtab, sal.line, &pc);
13969
13970 if (target_static_tracepoint_marker_at (pc, &marker))
13971 {
d9b3f62e 13972 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13973 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13974 b->number,
d9b3f62e 13975 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13976
d9b3f62e
PA
13977 xfree (tp->static_trace_marker_id);
13978 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13979 release_static_tracepoint_marker (&marker);
13980
13981 return sal;
13982 }
13983
13984 /* Old marker wasn't found on target at lineno. Try looking it up
13985 by string ID. */
13986 if (!sal.explicit_pc
13987 && sal.line != 0
13988 && sal.symtab != NULL
d9b3f62e 13989 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13990 {
13991 VEC(static_tracepoint_marker_p) *markers;
13992
13993 markers
d9b3f62e 13994 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13995
13996 if (!VEC_empty(static_tracepoint_marker_p, markers))
13997 {
80e1d417 13998 struct symtab_and_line sal2;
0fb4aa4b 13999 struct symbol *sym;
80e1d417 14000 struct static_tracepoint_marker *tpmarker;
79a45e25 14001 struct ui_out *uiout = current_uiout;
67994074 14002 struct explicit_location explicit_loc;
0fb4aa4b 14003
80e1d417 14004 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14005
d9b3f62e 14006 xfree (tp->static_trace_marker_id);
80e1d417 14007 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14008
14009 warning (_("marker for static tracepoint %d (%s) not "
14010 "found at previous line number"),
d9b3f62e 14011 b->number, tp->static_trace_marker_id);
0fb4aa4b 14012
80e1d417 14013 init_sal (&sal2);
0fb4aa4b 14014
80e1d417 14015 sal2.pc = tpmarker->address;
0fb4aa4b 14016
80e1d417
AS
14017 sal2 = find_pc_line (tpmarker->address, 0);
14018 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14019 ui_out_text (uiout, "Now in ");
14020 if (sym)
14021 {
14022 ui_out_field_string (uiout, "func",
14023 SYMBOL_PRINT_NAME (sym));
14024 ui_out_text (uiout, " at ");
14025 }
05cba821
JK
14026 ui_out_field_string (uiout, "file",
14027 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14028 ui_out_text (uiout, ":");
14029
14030 if (ui_out_is_mi_like_p (uiout))
14031 {
0b0865da 14032 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14033
f35a17b5 14034 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14035 }
14036
80e1d417 14037 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14038 ui_out_text (uiout, "\n");
14039
80e1d417 14040 b->loc->line_number = sal2.line;
2f202fde 14041 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 14042
f00aae0f 14043 delete_event_location (b->location);
67994074
KS
14044 initialize_explicit_location (&explicit_loc);
14045 explicit_loc.source_filename
00e52e53 14046 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
14047 explicit_loc.line_offset.offset = b->loc->line_number;
14048 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14049 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
14050
14051 /* Might be nice to check if function changed, and warn if
14052 so. */
14053
80e1d417 14054 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14055 }
14056 }
14057 return sal;
14058}
14059
8d3788bd
VP
14060/* Returns 1 iff locations A and B are sufficiently same that
14061 we don't need to report breakpoint as changed. */
14062
14063static int
14064locations_are_equal (struct bp_location *a, struct bp_location *b)
14065{
14066 while (a && b)
14067 {
14068 if (a->address != b->address)
14069 return 0;
14070
14071 if (a->shlib_disabled != b->shlib_disabled)
14072 return 0;
14073
14074 if (a->enabled != b->enabled)
14075 return 0;
14076
14077 a = a->next;
14078 b = b->next;
14079 }
14080
14081 if ((a == NULL) != (b == NULL))
14082 return 0;
14083
14084 return 1;
14085}
14086
f1310107
TJB
14087/* Create new breakpoint locations for B (a hardware or software breakpoint)
14088 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14089 a ranged breakpoint. */
14090
0e30163f 14091void
0d381245 14092update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14093 struct symtabs_and_lines sals,
14094 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14095{
14096 int i;
0d381245
VP
14097 struct bp_location *existing_locations = b->loc;
14098
f8eba3c6
TT
14099 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14100 {
14101 /* Ranged breakpoints have only one start location and one end
14102 location. */
14103 b->enable_state = bp_disabled;
44702360 14104 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
14105 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14106 "multiple locations found\n"),
14107 b->number);
14108 return;
14109 }
f1310107 14110
4a64f543
MS
14111 /* If there's no new locations, and all existing locations are
14112 pending, don't do anything. This optimizes the common case where
14113 all locations are in the same shared library, that was unloaded.
14114 We'd like to retain the location, so that when the library is
14115 loaded again, we don't loose the enabled/disabled status of the
14116 individual locations. */
0d381245 14117 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14118 return;
14119
fe3f5fa8
VP
14120 b->loc = NULL;
14121
0d381245 14122 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14123 {
f8eba3c6
TT
14124 struct bp_location *new_loc;
14125
14126 switch_to_program_space_and_thread (sals.sals[i].pspace);
14127
14128 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14129
0d381245
VP
14130 /* Reparse conditions, they might contain references to the
14131 old symtab. */
14132 if (b->cond_string != NULL)
14133 {
bbc13ae3 14134 const char *s;
fe3f5fa8 14135
0d381245 14136 s = b->cond_string;
492d29ea 14137 TRY
0d381245 14138 {
1bb9788d
TT
14139 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14140 block_for_pc (sals.sals[i].pc),
0d381245
VP
14141 0);
14142 }
492d29ea 14143 CATCH (e, RETURN_MASK_ERROR)
0d381245 14144 {
3e43a32a
MS
14145 warning (_("failed to reevaluate condition "
14146 "for breakpoint %d: %s"),
0d381245
VP
14147 b->number, e.message);
14148 new_loc->enabled = 0;
14149 }
492d29ea 14150 END_CATCH
0d381245 14151 }
fe3f5fa8 14152
f1310107
TJB
14153 if (sals_end.nelts)
14154 {
14155 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14156
14157 new_loc->length = end - sals.sals[0].pc + 1;
14158 }
0d381245 14159 }
fe3f5fa8 14160
4a64f543
MS
14161 /* If possible, carry over 'disable' status from existing
14162 breakpoints. */
0d381245
VP
14163 {
14164 struct bp_location *e = existing_locations;
776592bf
DE
14165 /* If there are multiple breakpoints with the same function name,
14166 e.g. for inline functions, comparing function names won't work.
14167 Instead compare pc addresses; this is just a heuristic as things
14168 may have moved, but in practice it gives the correct answer
14169 often enough until a better solution is found. */
14170 int have_ambiguous_names = ambiguous_names_p (b->loc);
14171
0d381245
VP
14172 for (; e; e = e->next)
14173 {
14174 if (!e->enabled && e->function_name)
14175 {
14176 struct bp_location *l = b->loc;
776592bf
DE
14177 if (have_ambiguous_names)
14178 {
14179 for (; l; l = l->next)
f1310107 14180 if (breakpoint_locations_match (e, l))
776592bf
DE
14181 {
14182 l->enabled = 0;
14183 break;
14184 }
14185 }
14186 else
14187 {
14188 for (; l; l = l->next)
14189 if (l->function_name
14190 && strcmp (e->function_name, l->function_name) == 0)
14191 {
14192 l->enabled = 0;
14193 break;
14194 }
14195 }
0d381245
VP
14196 }
14197 }
14198 }
fe3f5fa8 14199
8d3788bd
VP
14200 if (!locations_are_equal (existing_locations, b->loc))
14201 observer_notify_breakpoint_modified (b);
14202
44702360 14203 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14204}
14205
f00aae0f 14206/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
14207 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14208
14209static struct symtabs_and_lines
f00aae0f
KS
14210location_to_sals (struct breakpoint *b, struct event_location *location,
14211 int *found)
ef23e705 14212{
02d20e4a 14213 struct symtabs_and_lines sals = {0};
492d29ea 14214 struct gdb_exception exception = exception_none;
ef23e705 14215
983af33b 14216 gdb_assert (b->ops != NULL);
ef23e705 14217
492d29ea 14218 TRY
ef23e705 14219 {
f00aae0f 14220 b->ops->decode_location (b, location, &sals);
ef23e705 14221 }
492d29ea 14222 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14223 {
14224 int not_found_and_ok = 0;
492d29ea
PA
14225
14226 exception = e;
14227
ef23e705
TJB
14228 /* For pending breakpoints, it's expected that parsing will
14229 fail until the right shared library is loaded. User has
14230 already told to create pending breakpoints and don't need
14231 extra messages. If breakpoint is in bp_shlib_disabled
14232 state, then user already saw the message about that
14233 breakpoint being disabled, and don't want to see more
14234 errors. */
58438ac1 14235 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14236 && (b->condition_not_parsed
14237 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14238 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14239 || b->enable_state == bp_disabled))
14240 not_found_and_ok = 1;
14241
14242 if (!not_found_and_ok)
14243 {
14244 /* We surely don't want to warn about the same breakpoint
14245 10 times. One solution, implemented here, is disable
14246 the breakpoint on error. Another solution would be to
14247 have separate 'warning emitted' flag. Since this
14248 happens only when a binary has changed, I don't know
14249 which approach is better. */
14250 b->enable_state = bp_disabled;
14251 throw_exception (e);
14252 }
14253 }
492d29ea 14254 END_CATCH
ef23e705 14255
492d29ea 14256 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14257 {
f8eba3c6 14258 int i;
ef23e705 14259
f8eba3c6
TT
14260 for (i = 0; i < sals.nelts; ++i)
14261 resolve_sal_pc (&sals.sals[i]);
f00aae0f 14262 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14263 {
ed1d1739
KS
14264 char *cond_string, *extra_string;
14265 int thread, task;
ef23e705 14266
f00aae0f 14267 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
e7e0cddf
SS
14268 &cond_string, &thread, &task,
14269 &extra_string);
f00aae0f 14270 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14271 if (cond_string)
14272 b->cond_string = cond_string;
14273 b->thread = thread;
14274 b->task = task;
e7e0cddf 14275 if (extra_string)
f00aae0f
KS
14276 {
14277 xfree (b->extra_string);
14278 b->extra_string = extra_string;
14279 }
ef23e705
TJB
14280 b->condition_not_parsed = 0;
14281 }
14282
983af33b 14283 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14284 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14285
58438ac1
TT
14286 *found = 1;
14287 }
14288 else
14289 *found = 0;
ef23e705
TJB
14290
14291 return sals;
14292}
14293
348d480f
PA
14294/* The default re_set method, for typical hardware or software
14295 breakpoints. Reevaluate the breakpoint and recreate its
14296 locations. */
14297
14298static void
28010a5d 14299breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14300{
14301 int found;
f1310107 14302 struct symtabs_and_lines sals, sals_end;
ef23e705 14303 struct symtabs_and_lines expanded = {0};
f1310107 14304 struct symtabs_and_lines expanded_end = {0};
ef23e705 14305
f00aae0f 14306 sals = location_to_sals (b, b->location, &found);
ef23e705
TJB
14307 if (found)
14308 {
14309 make_cleanup (xfree, sals.sals);
f8eba3c6 14310 expanded = sals;
ef23e705
TJB
14311 }
14312
f00aae0f 14313 if (b->location_range_end != NULL)
f1310107 14314 {
f00aae0f 14315 sals_end = location_to_sals (b, b->location_range_end, &found);
f1310107
TJB
14316 if (found)
14317 {
14318 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14319 expanded_end = sals_end;
f1310107
TJB
14320 }
14321 }
14322
14323 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14324}
14325
983af33b
SDJ
14326/* Default method for creating SALs from an address string. It basically
14327 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14328
14329static void
f00aae0f
KS
14330create_sals_from_location_default (const struct event_location *location,
14331 struct linespec_result *canonical,
14332 enum bptype type_wanted)
983af33b 14333{
f00aae0f 14334 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14335}
14336
14337/* Call create_breakpoints_sal for the given arguments. This is the default
14338 function for the `create_breakpoints_sal' method of
14339 breakpoint_ops. */
14340
14341static void
14342create_breakpoints_sal_default (struct gdbarch *gdbarch,
14343 struct linespec_result *canonical,
983af33b 14344 char *cond_string,
e7e0cddf 14345 char *extra_string,
983af33b
SDJ
14346 enum bptype type_wanted,
14347 enum bpdisp disposition,
14348 int thread,
14349 int task, int ignore_count,
14350 const struct breakpoint_ops *ops,
14351 int from_tty, int enabled,
44f238bb 14352 int internal, unsigned flags)
983af33b
SDJ
14353{
14354 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14355 extra_string,
983af33b
SDJ
14356 type_wanted, disposition,
14357 thread, task, ignore_count, ops, from_tty,
44f238bb 14358 enabled, internal, flags);
983af33b
SDJ
14359}
14360
14361/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14362 default function for the `decode_location' method of breakpoint_ops. */
983af33b
SDJ
14363
14364static void
f00aae0f
KS
14365decode_location_default (struct breakpoint *b,
14366 const struct event_location *location,
983af33b
SDJ
14367 struct symtabs_and_lines *sals)
14368{
14369 struct linespec_result canonical;
14370
14371 init_linespec_result (&canonical);
f00aae0f 14372 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
983af33b
SDJ
14373 (struct symtab *) NULL, 0,
14374 &canonical, multiple_symbols_all,
14375 b->filter);
14376
14377 /* We should get 0 or 1 resulting SALs. */
14378 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14379
14380 if (VEC_length (linespec_sals, canonical.sals) > 0)
14381 {
14382 struct linespec_sals *lsal;
14383
14384 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14385 *sals = lsal->sals;
14386 /* Arrange it so the destructor does not free the
14387 contents. */
14388 lsal->sals.sals = NULL;
14389 }
14390
14391 destroy_linespec_result (&canonical);
14392}
14393
28010a5d
PA
14394/* Prepare the global context for a re-set of breakpoint B. */
14395
14396static struct cleanup *
14397prepare_re_set_context (struct breakpoint *b)
14398{
14399 struct cleanup *cleanups;
14400
14401 input_radix = b->input_radix;
14402 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14403 if (b->pspace != NULL)
14404 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14405 set_language (b->language);
14406
14407 return cleanups;
ef23e705
TJB
14408}
14409
c906108c
SS
14410/* Reset a breakpoint given it's struct breakpoint * BINT.
14411 The value we return ends up being the return value from catch_errors.
14412 Unused in this case. */
14413
14414static int
4efb68b1 14415breakpoint_re_set_one (void *bint)
c906108c 14416{
4a64f543 14417 /* Get past catch_errs. */
53a5351d 14418 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14419 struct cleanup *cleanups;
c906108c 14420
348d480f
PA
14421 cleanups = prepare_re_set_context (b);
14422 b->ops->re_set (b);
14423 do_cleanups (cleanups);
c906108c
SS
14424 return 0;
14425}
14426
69de3c6a 14427/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14428void
69de3c6a 14429breakpoint_re_set (void)
c906108c 14430{
35df4500 14431 struct breakpoint *b, *b_tmp;
c906108c
SS
14432 enum language save_language;
14433 int save_input_radix;
6c95b8df 14434 struct cleanup *old_chain;
c5aa993b 14435
c906108c
SS
14436 save_language = current_language->la_language;
14437 save_input_radix = input_radix;
6c95b8df
PA
14438 old_chain = save_current_program_space ();
14439
35df4500 14440 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14441 {
4a64f543 14442 /* Format possible error msg. */
fe3f5fa8 14443 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14444 b->number);
14445 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14446 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14447 do_cleanups (cleanups);
c5aa993b 14448 }
c906108c
SS
14449 set_language (save_language);
14450 input_radix = save_input_radix;
e62c965a 14451
0756c555 14452 jit_breakpoint_re_set ();
4efc6507 14453
6c95b8df
PA
14454 do_cleanups (old_chain);
14455
af02033e
PP
14456 create_overlay_event_breakpoint ();
14457 create_longjmp_master_breakpoint ();
14458 create_std_terminate_master_breakpoint ();
186c406b 14459 create_exception_master_breakpoint ();
c906108c
SS
14460}
14461\f
c906108c
SS
14462/* Reset the thread number of this breakpoint:
14463
14464 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14465 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14466void
fba45db2 14467breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14468{
14469 if (b->thread != -1)
14470 {
39f77062
KB
14471 if (in_thread_list (inferior_ptid))
14472 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14473
14474 /* We're being called after following a fork. The new fork is
14475 selected as current, and unless this was a vfork will have a
14476 different program space from the original thread. Reset that
14477 as well. */
14478 b->loc->pspace = current_program_space;
c906108c
SS
14479 }
14480}
14481
03ac34d5
MS
14482/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14483 If from_tty is nonzero, it prints a message to that effect,
14484 which ends with a period (no newline). */
14485
c906108c 14486void
fba45db2 14487set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14488{
52f0bd74 14489 struct breakpoint *b;
c906108c
SS
14490
14491 if (count < 0)
14492 count = 0;
14493
14494 ALL_BREAKPOINTS (b)
14495 if (b->number == bptnum)
c5aa993b 14496 {
d77f58be
SS
14497 if (is_tracepoint (b))
14498 {
14499 if (from_tty && count != 0)
14500 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14501 bptnum);
14502 return;
14503 }
14504
c5aa993b 14505 b->ignore_count = count;
221ea385
KS
14506 if (from_tty)
14507 {
14508 if (count == 0)
3e43a32a
MS
14509 printf_filtered (_("Will stop next time "
14510 "breakpoint %d is reached."),
221ea385
KS
14511 bptnum);
14512 else if (count == 1)
a3f17187 14513 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14514 bptnum);
14515 else
3e43a32a
MS
14516 printf_filtered (_("Will ignore next %d "
14517 "crossings of breakpoint %d."),
221ea385
KS
14518 count, bptnum);
14519 }
8d3788bd 14520 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14521 return;
14522 }
c906108c 14523
8a3fe4f8 14524 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14525}
14526
c906108c
SS
14527/* Command to set ignore-count of breakpoint N to COUNT. */
14528
14529static void
fba45db2 14530ignore_command (char *args, int from_tty)
c906108c
SS
14531{
14532 char *p = args;
52f0bd74 14533 int num;
c906108c
SS
14534
14535 if (p == 0)
e2e0b3e5 14536 error_no_arg (_("a breakpoint number"));
c5aa993b 14537
c906108c 14538 num = get_number (&p);
5c44784c 14539 if (num == 0)
8a3fe4f8 14540 error (_("bad breakpoint number: '%s'"), args);
c906108c 14541 if (*p == 0)
8a3fe4f8 14542 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14543
14544 set_ignore_count (num,
14545 longest_to_int (value_as_long (parse_and_eval (p))),
14546 from_tty);
221ea385
KS
14547 if (from_tty)
14548 printf_filtered ("\n");
c906108c
SS
14549}
14550\f
14551/* Call FUNCTION on each of the breakpoints
14552 whose numbers are given in ARGS. */
14553
14554static void
95a42b64
TT
14555map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14556 void *),
14557 void *data)
c906108c 14558{
52f0bd74
AC
14559 int num;
14560 struct breakpoint *b, *tmp;
11cf8741 14561 int match;
197f0a60 14562 struct get_number_or_range_state state;
c906108c 14563
b9d61307 14564 if (args == 0 || *args == '\0')
e2e0b3e5 14565 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14566
197f0a60
TT
14567 init_number_or_range (&state, args);
14568
14569 while (!state.finished)
c906108c 14570 {
e799154c 14571 const char *p = state.string;
197f0a60 14572
11cf8741 14573 match = 0;
c5aa993b 14574
197f0a60 14575 num = get_number_or_range (&state);
5c44784c 14576 if (num == 0)
c5aa993b 14577 {
8a3fe4f8 14578 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14579 }
14580 else
14581 {
14582 ALL_BREAKPOINTS_SAFE (b, tmp)
14583 if (b->number == num)
14584 {
11cf8741 14585 match = 1;
cdac0397 14586 function (b, data);
11cf8741 14587 break;
5c44784c 14588 }
11cf8741 14589 if (match == 0)
a3f17187 14590 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14591 }
c906108c
SS
14592 }
14593}
14594
0d381245
VP
14595static struct bp_location *
14596find_location_by_number (char *number)
14597{
14598 char *dot = strchr (number, '.');
14599 char *p1;
14600 int bp_num;
14601 int loc_num;
14602 struct breakpoint *b;
14603 struct bp_location *loc;
14604
14605 *dot = '\0';
14606
14607 p1 = number;
197f0a60 14608 bp_num = get_number (&p1);
0d381245
VP
14609 if (bp_num == 0)
14610 error (_("Bad breakpoint number '%s'"), number);
14611
14612 ALL_BREAKPOINTS (b)
14613 if (b->number == bp_num)
14614 {
14615 break;
14616 }
14617
14618 if (!b || b->number != bp_num)
14619 error (_("Bad breakpoint number '%s'"), number);
14620
14621 p1 = dot+1;
197f0a60 14622 loc_num = get_number (&p1);
0d381245
VP
14623 if (loc_num == 0)
14624 error (_("Bad breakpoint location number '%s'"), number);
14625
14626 --loc_num;
14627 loc = b->loc;
14628 for (;loc_num && loc; --loc_num, loc = loc->next)
14629 ;
14630 if (!loc)
14631 error (_("Bad breakpoint location number '%s'"), dot+1);
14632
14633 return loc;
14634}
14635
14636
1900040c
MS
14637/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14638 If from_tty is nonzero, it prints a message to that effect,
14639 which ends with a period (no newline). */
14640
c906108c 14641void
fba45db2 14642disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14643{
14644 /* Never disable a watchpoint scope breakpoint; we want to
14645 hit them when we leave scope so we can delete both the
14646 watchpoint and its scope breakpoint at that time. */
14647 if (bpt->type == bp_watchpoint_scope)
14648 return;
14649
b5de0fa7 14650 bpt->enable_state = bp_disabled;
c906108c 14651
b775012e
LM
14652 /* Mark breakpoint locations modified. */
14653 mark_breakpoint_modified (bpt);
14654
d248b706
KY
14655 if (target_supports_enable_disable_tracepoint ()
14656 && current_trace_status ()->running && is_tracepoint (bpt))
14657 {
14658 struct bp_location *location;
14659
14660 for (location = bpt->loc; location; location = location->next)
14661 target_disable_tracepoint (location);
14662 }
14663
44702360 14664 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14665
8d3788bd 14666 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14667}
14668
51be5b68
PA
14669/* A callback for iterate_over_related_breakpoints. */
14670
14671static void
14672do_disable_breakpoint (struct breakpoint *b, void *ignore)
14673{
14674 disable_breakpoint (b);
14675}
14676
95a42b64
TT
14677/* A callback for map_breakpoint_numbers that calls
14678 disable_breakpoint. */
14679
14680static void
14681do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14682{
51be5b68 14683 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14684}
14685
c906108c 14686static void
fba45db2 14687disable_command (char *args, int from_tty)
c906108c 14688{
c906108c 14689 if (args == 0)
46c6471b
PA
14690 {
14691 struct breakpoint *bpt;
14692
14693 ALL_BREAKPOINTS (bpt)
14694 if (user_breakpoint_p (bpt))
14695 disable_breakpoint (bpt);
14696 }
9eaabc75 14697 else
0d381245 14698 {
9eaabc75
MW
14699 char *num = extract_arg (&args);
14700
14701 while (num)
d248b706 14702 {
9eaabc75 14703 if (strchr (num, '.'))
b775012e 14704 {
9eaabc75
MW
14705 struct bp_location *loc = find_location_by_number (num);
14706
14707 if (loc)
14708 {
14709 if (loc->enabled)
14710 {
14711 loc->enabled = 0;
14712 mark_breakpoint_location_modified (loc);
14713 }
14714 if (target_supports_enable_disable_tracepoint ()
14715 && current_trace_status ()->running && loc->owner
14716 && is_tracepoint (loc->owner))
14717 target_disable_tracepoint (loc);
14718 }
44702360 14719 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14720 }
9eaabc75
MW
14721 else
14722 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14723 num = extract_arg (&args);
d248b706 14724 }
0d381245 14725 }
c906108c
SS
14726}
14727
14728static void
816338b5
SS
14729enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14730 int count)
c906108c 14731{
afe38095 14732 int target_resources_ok;
c906108c
SS
14733
14734 if (bpt->type == bp_hardware_breakpoint)
14735 {
14736 int i;
c5aa993b 14737 i = hw_breakpoint_used_count ();
53a5351d 14738 target_resources_ok =
d92524f1 14739 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14740 i + 1, 0);
c906108c 14741 if (target_resources_ok == 0)
8a3fe4f8 14742 error (_("No hardware breakpoint support in the target."));
c906108c 14743 else if (target_resources_ok < 0)
8a3fe4f8 14744 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14745 }
14746
cc60f2e3 14747 if (is_watchpoint (bpt))
c906108c 14748 {
d07205c2 14749 /* Initialize it just to avoid a GCC false warning. */
f486487f 14750 enum enable_state orig_enable_state = bp_disabled;
dde02812 14751
492d29ea 14752 TRY
c906108c 14753 {
3a5c3e22
PA
14754 struct watchpoint *w = (struct watchpoint *) bpt;
14755
1e718ff1
TJB
14756 orig_enable_state = bpt->enable_state;
14757 bpt->enable_state = bp_enabled;
3a5c3e22 14758 update_watchpoint (w, 1 /* reparse */);
c906108c 14759 }
492d29ea 14760 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14761 {
1e718ff1 14762 bpt->enable_state = orig_enable_state;
dde02812
ES
14763 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14764 bpt->number);
14765 return;
c5aa993b 14766 }
492d29ea 14767 END_CATCH
c906108c 14768 }
0101ce28 14769
b775012e
LM
14770 bpt->enable_state = bp_enabled;
14771
14772 /* Mark breakpoint locations modified. */
14773 mark_breakpoint_modified (bpt);
14774
d248b706
KY
14775 if (target_supports_enable_disable_tracepoint ()
14776 && current_trace_status ()->running && is_tracepoint (bpt))
14777 {
14778 struct bp_location *location;
14779
14780 for (location = bpt->loc; location; location = location->next)
14781 target_enable_tracepoint (location);
14782 }
14783
b4c291bb 14784 bpt->disposition = disposition;
816338b5 14785 bpt->enable_count = count;
44702360 14786 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14787
8d3788bd 14788 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14789}
14790
fe3f5fa8 14791
c906108c 14792void
fba45db2 14793enable_breakpoint (struct breakpoint *bpt)
c906108c 14794{
816338b5 14795 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14796}
14797
14798static void
14799do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14800{
14801 enable_breakpoint (bpt);
c906108c
SS
14802}
14803
95a42b64
TT
14804/* A callback for map_breakpoint_numbers that calls
14805 enable_breakpoint. */
14806
14807static void
14808do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14809{
51be5b68 14810 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14811}
14812
c906108c
SS
14813/* The enable command enables the specified breakpoints (or all defined
14814 breakpoints) so they once again become (or continue to be) effective
1272ad14 14815 in stopping the inferior. */
c906108c 14816
c906108c 14817static void
fba45db2 14818enable_command (char *args, int from_tty)
c906108c 14819{
c906108c 14820 if (args == 0)
46c6471b
PA
14821 {
14822 struct breakpoint *bpt;
14823
14824 ALL_BREAKPOINTS (bpt)
14825 if (user_breakpoint_p (bpt))
14826 enable_breakpoint (bpt);
14827 }
9eaabc75 14828 else
0d381245 14829 {
9eaabc75
MW
14830 char *num = extract_arg (&args);
14831
14832 while (num)
d248b706 14833 {
9eaabc75 14834 if (strchr (num, '.'))
b775012e 14835 {
9eaabc75
MW
14836 struct bp_location *loc = find_location_by_number (num);
14837
14838 if (loc)
14839 {
14840 if (!loc->enabled)
14841 {
14842 loc->enabled = 1;
14843 mark_breakpoint_location_modified (loc);
14844 }
14845 if (target_supports_enable_disable_tracepoint ()
14846 && current_trace_status ()->running && loc->owner
14847 && is_tracepoint (loc->owner))
14848 target_enable_tracepoint (loc);
14849 }
44702360 14850 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14851 }
9eaabc75
MW
14852 else
14853 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14854 num = extract_arg (&args);
d248b706 14855 }
0d381245 14856 }
c906108c
SS
14857}
14858
816338b5
SS
14859/* This struct packages up disposition data for application to multiple
14860 breakpoints. */
14861
14862struct disp_data
14863{
14864 enum bpdisp disp;
14865 int count;
14866};
14867
c906108c 14868static void
51be5b68
PA
14869do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14870{
816338b5 14871 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14872
816338b5 14873 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14874}
14875
14876static void
14877do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14878{
816338b5 14879 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14880
14881 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14882}
14883
c906108c 14884static void
fba45db2 14885enable_once_command (char *args, int from_tty)
c906108c 14886{
51be5b68 14887 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14888}
14889
816338b5
SS
14890static void
14891do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14892{
14893 struct disp_data disp = { disp_disable, *(int *) countptr };
14894
14895 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14896}
14897
14898static void
14899enable_count_command (char *args, int from_tty)
14900{
b9d61307
SM
14901 int count;
14902
14903 if (args == NULL)
14904 error_no_arg (_("hit count"));
14905
14906 count = get_number (&args);
816338b5
SS
14907
14908 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14909}
14910
c906108c 14911static void
51be5b68 14912do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14913{
816338b5 14914 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14915
14916 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14917}
14918
c906108c 14919static void
fba45db2 14920enable_delete_command (char *args, int from_tty)
c906108c 14921{
51be5b68 14922 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14923}
14924\f
fa8d40ab
JJ
14925static void
14926set_breakpoint_cmd (char *args, int from_tty)
14927{
14928}
14929
14930static void
14931show_breakpoint_cmd (char *args, int from_tty)
14932{
14933}
14934
1f3b5d1b
PP
14935/* Invalidate last known value of any hardware watchpoint if
14936 the memory which that value represents has been written to by
14937 GDB itself. */
14938
14939static void
8de0566d
YQ
14940invalidate_bp_value_on_memory_change (struct inferior *inferior,
14941 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14942 const bfd_byte *data)
14943{
14944 struct breakpoint *bp;
14945
14946 ALL_BREAKPOINTS (bp)
14947 if (bp->enable_state == bp_enabled
3a5c3e22 14948 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14949 {
3a5c3e22 14950 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14951
3a5c3e22
PA
14952 if (wp->val_valid && wp->val)
14953 {
14954 struct bp_location *loc;
14955
14956 for (loc = bp->loc; loc != NULL; loc = loc->next)
14957 if (loc->loc_type == bp_loc_hardware_watchpoint
14958 && loc->address + loc->length > addr
14959 && addr + len > loc->address)
14960 {
14961 value_free (wp->val);
14962 wp->val = NULL;
14963 wp->val_valid = 0;
14964 }
14965 }
1f3b5d1b
PP
14966 }
14967}
14968
8181d85f
DJ
14969/* Create and insert a breakpoint for software single step. */
14970
14971void
6c95b8df 14972insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14973 struct address_space *aspace,
14974 CORE_ADDR next_pc)
8181d85f 14975{
7c16b83e
PA
14976 struct thread_info *tp = inferior_thread ();
14977 struct symtab_and_line sal;
14978 CORE_ADDR pc = next_pc;
8181d85f 14979
34b7e8a6
PA
14980 if (tp->control.single_step_breakpoints == NULL)
14981 {
14982 tp->control.single_step_breakpoints
14983 = new_single_step_breakpoint (tp->num, gdbarch);
14984 }
8181d85f 14985
7c16b83e
PA
14986 sal = find_pc_line (pc, 0);
14987 sal.pc = pc;
14988 sal.section = find_pc_overlay (pc);
14989 sal.explicit_pc = 1;
34b7e8a6 14990 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14991
7c16b83e 14992 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14993}
14994
34b7e8a6 14995/* See breakpoint.h. */
f02253f1
HZ
14996
14997int
7c16b83e
PA
14998breakpoint_has_location_inserted_here (struct breakpoint *bp,
14999 struct address_space *aspace,
15000 CORE_ADDR pc)
1aafd4da 15001{
7c16b83e 15002 struct bp_location *loc;
1aafd4da 15003
7c16b83e
PA
15004 for (loc = bp->loc; loc != NULL; loc = loc->next)
15005 if (loc->inserted
15006 && breakpoint_location_address_match (loc, aspace, pc))
15007 return 1;
1aafd4da 15008
7c16b83e 15009 return 0;
ef370185
JB
15010}
15011
15012/* Check whether a software single-step breakpoint is inserted at
15013 PC. */
15014
15015int
15016single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15017 CORE_ADDR pc)
15018{
34b7e8a6
PA
15019 struct breakpoint *bpt;
15020
15021 ALL_BREAKPOINTS (bpt)
15022 {
15023 if (bpt->type == bp_single_step
15024 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15025 return 1;
15026 }
15027 return 0;
1aafd4da
UW
15028}
15029
1042e4c0
SS
15030/* Tracepoint-specific operations. */
15031
15032/* Set tracepoint count to NUM. */
15033static void
15034set_tracepoint_count (int num)
15035{
15036 tracepoint_count = num;
4fa62494 15037 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15038}
15039
70221824 15040static void
1042e4c0
SS
15041trace_command (char *arg, int from_tty)
15042{
55aa24fb 15043 struct breakpoint_ops *ops;
f00aae0f
KS
15044 struct event_location *location;
15045 struct cleanup *back_to;
55aa24fb 15046
f00aae0f
KS
15047 location = string_to_event_location (&arg, current_language);
15048 back_to = make_cleanup_delete_event_location (location);
5b56227b
KS
15049 if (location != NULL
15050 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
15051 ops = &tracepoint_probe_breakpoint_ops;
15052 else
15053 ops = &tracepoint_breakpoint_ops;
15054
558a9d82 15055 create_breakpoint (get_current_arch (),
f00aae0f
KS
15056 location,
15057 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15058 0 /* tempflag */,
15059 bp_tracepoint /* type_wanted */,
15060 0 /* Ignore count */,
15061 pending_break_support,
15062 ops,
15063 from_tty,
15064 1 /* enabled */,
15065 0 /* internal */, 0);
f00aae0f 15066 do_cleanups (back_to);
1042e4c0
SS
15067}
15068
70221824 15069static void
7a697b8d
SS
15070ftrace_command (char *arg, int from_tty)
15071{
f00aae0f
KS
15072 struct event_location *location;
15073 struct cleanup *back_to;
15074
15075 location = string_to_event_location (&arg, current_language);
15076 back_to = make_cleanup_delete_event_location (location);
558a9d82 15077 create_breakpoint (get_current_arch (),
f00aae0f
KS
15078 location,
15079 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15080 0 /* tempflag */,
15081 bp_fast_tracepoint /* type_wanted */,
15082 0 /* Ignore count */,
15083 pending_break_support,
15084 &tracepoint_breakpoint_ops,
15085 from_tty,
15086 1 /* enabled */,
15087 0 /* internal */, 0);
f00aae0f 15088 do_cleanups (back_to);
0fb4aa4b
PA
15089}
15090
15091/* strace command implementation. Creates a static tracepoint. */
15092
70221824 15093static void
0fb4aa4b
PA
15094strace_command (char *arg, int from_tty)
15095{
983af33b 15096 struct breakpoint_ops *ops;
f00aae0f
KS
15097 struct event_location *location;
15098 struct cleanup *back_to;
983af33b
SDJ
15099
15100 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15101 or with a normal static tracepoint. */
61012eef 15102 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
15103 {
15104 ops = &strace_marker_breakpoint_ops;
15105 location = new_linespec_location (&arg);
15106 }
983af33b 15107 else
f00aae0f
KS
15108 {
15109 ops = &tracepoint_breakpoint_ops;
15110 location = string_to_event_location (&arg, current_language);
15111 }
983af33b 15112
f00aae0f 15113 back_to = make_cleanup_delete_event_location (location);
558a9d82 15114 create_breakpoint (get_current_arch (),
f00aae0f
KS
15115 location,
15116 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15117 0 /* tempflag */,
15118 bp_static_tracepoint /* type_wanted */,
15119 0 /* Ignore count */,
15120 pending_break_support,
15121 ops,
15122 from_tty,
15123 1 /* enabled */,
15124 0 /* internal */, 0);
f00aae0f 15125 do_cleanups (back_to);
7a697b8d
SS
15126}
15127
409873ef
SS
15128/* Set up a fake reader function that gets command lines from a linked
15129 list that was acquired during tracepoint uploading. */
15130
15131static struct uploaded_tp *this_utp;
3149d8c1 15132static int next_cmd;
409873ef
SS
15133
15134static char *
15135read_uploaded_action (void)
15136{
15137 char *rslt;
15138
3149d8c1 15139 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15140
3149d8c1 15141 next_cmd++;
409873ef
SS
15142
15143 return rslt;
15144}
15145
00bf0b85
SS
15146/* Given information about a tracepoint as recorded on a target (which
15147 can be either a live system or a trace file), attempt to create an
15148 equivalent GDB tracepoint. This is not a reliable process, since
15149 the target does not necessarily have all the information used when
15150 the tracepoint was originally defined. */
15151
d9b3f62e 15152struct tracepoint *
00bf0b85 15153create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15154{
409873ef 15155 char *addr_str, small_buf[100];
d9b3f62e 15156 struct tracepoint *tp;
f00aae0f
KS
15157 struct event_location *location;
15158 struct cleanup *cleanup;
fd9b8c24 15159
409873ef
SS
15160 if (utp->at_string)
15161 addr_str = utp->at_string;
15162 else
15163 {
15164 /* In the absence of a source location, fall back to raw
15165 address. Since there is no way to confirm that the address
15166 means the same thing as when the trace was started, warn the
15167 user. */
3e43a32a
MS
15168 warning (_("Uploaded tracepoint %d has no "
15169 "source location, using raw address"),
409873ef 15170 utp->number);
8c042590 15171 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15172 addr_str = small_buf;
15173 }
15174
15175 /* There's not much we can do with a sequence of bytecodes. */
15176 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15177 warning (_("Uploaded tracepoint %d condition "
15178 "has no source form, ignoring it"),
409873ef 15179 utp->number);
d5551862 15180
f00aae0f
KS
15181 location = string_to_event_location (&addr_str, current_language);
15182 cleanup = make_cleanup_delete_event_location (location);
8cdf0e15 15183 if (!create_breakpoint (get_current_arch (),
f00aae0f
KS
15184 location,
15185 utp->cond_string, -1, addr_str,
e7e0cddf 15186 0 /* parse cond/thread */,
8cdf0e15 15187 0 /* tempflag */,
0fb4aa4b 15188 utp->type /* type_wanted */,
8cdf0e15
VP
15189 0 /* Ignore count */,
15190 pending_break_support,
348d480f 15191 &tracepoint_breakpoint_ops,
8cdf0e15 15192 0 /* from_tty */,
84f4c1fe 15193 utp->enabled /* enabled */,
44f238bb
PA
15194 0 /* internal */,
15195 CREATE_BREAKPOINT_FLAGS_INSERTED))
f00aae0f
KS
15196 {
15197 do_cleanups (cleanup);
15198 return NULL;
15199 }
15200
15201 do_cleanups (cleanup);
fd9b8c24 15202
409873ef 15203 /* Get the tracepoint we just created. */
fd9b8c24
PA
15204 tp = get_tracepoint (tracepoint_count);
15205 gdb_assert (tp != NULL);
d5551862 15206
00bf0b85
SS
15207 if (utp->pass > 0)
15208 {
8c042590
PM
15209 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15210 tp->base.number);
00bf0b85 15211
409873ef 15212 trace_pass_command (small_buf, 0);
00bf0b85
SS
15213 }
15214
409873ef
SS
15215 /* If we have uploaded versions of the original commands, set up a
15216 special-purpose "reader" function and call the usual command line
15217 reader, then pass the result to the breakpoint command-setting
15218 function. */
3149d8c1 15219 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15220 {
409873ef 15221 struct command_line *cmd_list;
00bf0b85 15222
409873ef 15223 this_utp = utp;
3149d8c1 15224 next_cmd = 0;
d5551862 15225
409873ef
SS
15226 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15227
d9b3f62e 15228 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15229 }
3149d8c1
SS
15230 else if (!VEC_empty (char_ptr, utp->actions)
15231 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15232 warning (_("Uploaded tracepoint %d actions "
15233 "have no source form, ignoring them"),
409873ef 15234 utp->number);
00bf0b85 15235
f196051f
SS
15236 /* Copy any status information that might be available. */
15237 tp->base.hit_count = utp->hit_count;
15238 tp->traceframe_usage = utp->traceframe_usage;
15239
00bf0b85 15240 return tp;
d9b3f62e 15241}
00bf0b85 15242
1042e4c0
SS
15243/* Print information on tracepoint number TPNUM_EXP, or all if
15244 omitted. */
15245
15246static void
e5a67952 15247tracepoints_info (char *args, int from_tty)
1042e4c0 15248{
79a45e25 15249 struct ui_out *uiout = current_uiout;
e5a67952 15250 int num_printed;
1042e4c0 15251
e5a67952 15252 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15253
15254 if (num_printed == 0)
1042e4c0 15255 {
e5a67952 15256 if (args == NULL || *args == '\0')
d77f58be
SS
15257 ui_out_message (uiout, 0, "No tracepoints.\n");
15258 else
e5a67952 15259 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15260 }
ad443146
SS
15261
15262 default_collect_info ();
1042e4c0
SS
15263}
15264
4a64f543 15265/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15266 Not supported by all targets. */
15267static void
15268enable_trace_command (char *args, int from_tty)
15269{
15270 enable_command (args, from_tty);
15271}
15272
4a64f543 15273/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15274 Not supported by all targets. */
15275static void
15276disable_trace_command (char *args, int from_tty)
15277{
15278 disable_command (args, from_tty);
15279}
15280
4a64f543 15281/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15282static void
15283delete_trace_command (char *arg, int from_tty)
15284{
35df4500 15285 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15286
15287 dont_repeat ();
15288
15289 if (arg == 0)
15290 {
15291 int breaks_to_delete = 0;
15292
15293 /* Delete all breakpoints if no argument.
15294 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15295 have to be deleted with an explicit breakpoint number
15296 argument. */
1042e4c0 15297 ALL_TRACEPOINTS (b)
46c6471b 15298 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15299 {
15300 breaks_to_delete = 1;
15301 break;
15302 }
1042e4c0
SS
15303
15304 /* Ask user only if there are some breakpoints to delete. */
15305 if (!from_tty
15306 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15307 {
35df4500 15308 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15309 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15310 delete_breakpoint (b);
1042e4c0
SS
15311 }
15312 }
15313 else
51be5b68 15314 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15315}
15316
197f0a60
TT
15317/* Helper function for trace_pass_command. */
15318
15319static void
d9b3f62e 15320trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15321{
d9b3f62e 15322 tp->pass_count = count;
6f6484cd 15323 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15324 if (from_tty)
15325 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15326 tp->base.number, count);
197f0a60
TT
15327}
15328
1042e4c0
SS
15329/* Set passcount for tracepoint.
15330
15331 First command argument is passcount, second is tracepoint number.
15332 If tracepoint number omitted, apply to most recently defined.
15333 Also accepts special argument "all". */
15334
15335static void
15336trace_pass_command (char *args, int from_tty)
15337{
d9b3f62e 15338 struct tracepoint *t1;
1042e4c0 15339 unsigned int count;
1042e4c0
SS
15340
15341 if (args == 0 || *args == 0)
3e43a32a
MS
15342 error (_("passcount command requires an "
15343 "argument (count + optional TP num)"));
1042e4c0 15344
4a64f543 15345 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15346
529480d0 15347 args = skip_spaces (args);
1042e4c0
SS
15348 if (*args && strncasecmp (args, "all", 3) == 0)
15349 {
d9b3f62e
PA
15350 struct breakpoint *b;
15351
1042e4c0 15352 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15353 if (*args)
15354 error (_("Junk at end of arguments."));
1042e4c0 15355
d9b3f62e 15356 ALL_TRACEPOINTS (b)
197f0a60 15357 {
d9b3f62e 15358 t1 = (struct tracepoint *) b;
197f0a60
TT
15359 trace_pass_set_count (t1, count, from_tty);
15360 }
15361 }
15362 else if (*args == '\0')
1042e4c0 15363 {
5fa1d40e 15364 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15365 if (t1)
197f0a60
TT
15366 trace_pass_set_count (t1, count, from_tty);
15367 }
15368 else
15369 {
15370 struct get_number_or_range_state state;
15371
15372 init_number_or_range (&state, args);
15373 while (!state.finished)
1042e4c0 15374 {
5fa1d40e 15375 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15376 if (t1)
15377 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15378 }
15379 }
1042e4c0
SS
15380}
15381
d9b3f62e 15382struct tracepoint *
1042e4c0
SS
15383get_tracepoint (int num)
15384{
15385 struct breakpoint *t;
15386
15387 ALL_TRACEPOINTS (t)
15388 if (t->number == num)
d9b3f62e 15389 return (struct tracepoint *) t;
1042e4c0
SS
15390
15391 return NULL;
15392}
15393
d5551862
SS
15394/* Find the tracepoint with the given target-side number (which may be
15395 different from the tracepoint number after disconnecting and
15396 reconnecting). */
15397
d9b3f62e 15398struct tracepoint *
d5551862
SS
15399get_tracepoint_by_number_on_target (int num)
15400{
d9b3f62e 15401 struct breakpoint *b;
d5551862 15402
d9b3f62e
PA
15403 ALL_TRACEPOINTS (b)
15404 {
15405 struct tracepoint *t = (struct tracepoint *) b;
15406
15407 if (t->number_on_target == num)
15408 return t;
15409 }
d5551862
SS
15410
15411 return NULL;
15412}
15413
1042e4c0 15414/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15415 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15416 If the argument is missing, the most recent tracepoint
15417 (tracepoint_count) is returned. */
15418
d9b3f62e 15419struct tracepoint *
197f0a60 15420get_tracepoint_by_number (char **arg,
5fa1d40e 15421 struct get_number_or_range_state *state)
1042e4c0 15422{
1042e4c0
SS
15423 struct breakpoint *t;
15424 int tpnum;
15425 char *instring = arg == NULL ? NULL : *arg;
15426
197f0a60
TT
15427 if (state)
15428 {
15429 gdb_assert (!state->finished);
15430 tpnum = get_number_or_range (state);
15431 }
15432 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15433 tpnum = tracepoint_count;
1042e4c0 15434 else
197f0a60 15435 tpnum = get_number (arg);
1042e4c0
SS
15436
15437 if (tpnum <= 0)
15438 {
15439 if (instring && *instring)
15440 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15441 instring);
15442 else
5fa1d40e 15443 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15444 return NULL;
15445 }
15446
15447 ALL_TRACEPOINTS (t)
15448 if (t->number == tpnum)
15449 {
d9b3f62e 15450 return (struct tracepoint *) t;
1042e4c0
SS
15451 }
15452
1042e4c0
SS
15453 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15454 return NULL;
15455}
15456
d9b3f62e
PA
15457void
15458print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15459{
15460 if (b->thread != -1)
15461 fprintf_unfiltered (fp, " thread %d", b->thread);
15462
15463 if (b->task != 0)
15464 fprintf_unfiltered (fp, " task %d", b->task);
15465
15466 fprintf_unfiltered (fp, "\n");
15467}
15468
6149aea9
PA
15469/* Save information on user settable breakpoints (watchpoints, etc) to
15470 a new script file named FILENAME. If FILTER is non-NULL, call it
15471 on each breakpoint and only include the ones for which it returns
15472 non-zero. */
15473
1042e4c0 15474static void
6149aea9
PA
15475save_breakpoints (char *filename, int from_tty,
15476 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15477{
15478 struct breakpoint *tp;
6149aea9 15479 int any = 0;
1042e4c0 15480 struct cleanup *cleanup;
a7bdde9e 15481 struct ui_file *fp;
6149aea9 15482 int extra_trace_bits = 0;
1042e4c0 15483
6149aea9
PA
15484 if (filename == 0 || *filename == 0)
15485 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15486
15487 /* See if we have anything to save. */
6149aea9 15488 ALL_BREAKPOINTS (tp)
1042e4c0 15489 {
6149aea9 15490 /* Skip internal and momentary breakpoints. */
09d682a4 15491 if (!user_breakpoint_p (tp))
6149aea9
PA
15492 continue;
15493
15494 /* If we have a filter, only save the breakpoints it accepts. */
15495 if (filter && !filter (tp))
15496 continue;
15497
15498 any = 1;
15499
15500 if (is_tracepoint (tp))
15501 {
15502 extra_trace_bits = 1;
15503
15504 /* We can stop searching. */
15505 break;
15506 }
1042e4c0 15507 }
6149aea9
PA
15508
15509 if (!any)
1042e4c0 15510 {
6149aea9 15511 warning (_("Nothing to save."));
1042e4c0
SS
15512 return;
15513 }
15514
c718be47
PA
15515 filename = tilde_expand (filename);
15516 cleanup = make_cleanup (xfree, filename);
15517 fp = gdb_fopen (filename, "w");
059fb39f 15518 if (!fp)
6149aea9
PA
15519 error (_("Unable to open file '%s' for saving (%s)"),
15520 filename, safe_strerror (errno));
a7bdde9e 15521 make_cleanup_ui_file_delete (fp);
8bf6485c 15522
6149aea9
PA
15523 if (extra_trace_bits)
15524 save_trace_state_variables (fp);
8bf6485c 15525
6149aea9 15526 ALL_BREAKPOINTS (tp)
1042e4c0 15527 {
6149aea9 15528 /* Skip internal and momentary breakpoints. */
09d682a4 15529 if (!user_breakpoint_p (tp))
6149aea9 15530 continue;
8bf6485c 15531
6149aea9
PA
15532 /* If we have a filter, only save the breakpoints it accepts. */
15533 if (filter && !filter (tp))
15534 continue;
15535
348d480f 15536 tp->ops->print_recreate (tp, fp);
1042e4c0 15537
6149aea9
PA
15538 /* Note, we can't rely on tp->number for anything, as we can't
15539 assume the recreated breakpoint numbers will match. Use $bpnum
15540 instead. */
15541
15542 if (tp->cond_string)
15543 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15544
15545 if (tp->ignore_count)
15546 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15547
2d9442cc 15548 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15549 {
6c63c96a 15550 struct gdb_exception exception;
a7bdde9e 15551
6149aea9 15552 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15553
79a45e25 15554 ui_out_redirect (current_uiout, fp);
492d29ea 15555 TRY
1042e4c0 15556 {
79a45e25 15557 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15558 }
492d29ea
PA
15559 CATCH (ex, RETURN_MASK_ALL)
15560 {
6c63c96a 15561 ui_out_redirect (current_uiout, NULL);
492d29ea
PA
15562 throw_exception (ex);
15563 }
15564 END_CATCH
1042e4c0 15565
6c63c96a 15566 ui_out_redirect (current_uiout, NULL);
a7bdde9e 15567 fprintf_unfiltered (fp, " end\n");
1042e4c0 15568 }
6149aea9
PA
15569
15570 if (tp->enable_state == bp_disabled)
99894e11 15571 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15572
15573 /* If this is a multi-location breakpoint, check if the locations
15574 should be individually disabled. Watchpoint locations are
15575 special, and not user visible. */
15576 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15577 {
15578 struct bp_location *loc;
15579 int n = 1;
15580
15581 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15582 if (!loc->enabled)
15583 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15584 }
1042e4c0 15585 }
8bf6485c 15586
6149aea9 15587 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15588 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15589
1042e4c0 15590 if (from_tty)
6149aea9 15591 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15592 do_cleanups (cleanup);
6149aea9
PA
15593}
15594
15595/* The `save breakpoints' command. */
15596
15597static void
15598save_breakpoints_command (char *args, int from_tty)
15599{
15600 save_breakpoints (args, from_tty, NULL);
15601}
15602
15603/* The `save tracepoints' command. */
15604
15605static void
15606save_tracepoints_command (char *args, int from_tty)
15607{
15608 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15609}
15610
15611/* Create a vector of all tracepoints. */
15612
15613VEC(breakpoint_p) *
eeae04df 15614all_tracepoints (void)
1042e4c0
SS
15615{
15616 VEC(breakpoint_p) *tp_vec = 0;
15617 struct breakpoint *tp;
15618
15619 ALL_TRACEPOINTS (tp)
15620 {
15621 VEC_safe_push (breakpoint_p, tp_vec, tp);
15622 }
15623
15624 return tp_vec;
15625}
15626
c906108c 15627\f
629500fa
KS
15628/* This help string is used to consolidate all the help string for specifying
15629 locations used by several commands. */
15630
15631#define LOCATION_HELP_STRING \
15632"Linespecs are colon-separated lists of location parameters, such as\n\
15633source filename, function name, label name, and line number.\n\
15634Example: To specify the start of a label named \"the_top\" in the\n\
15635function \"fact\" in the file \"factorial.c\", use\n\
15636\"factorial.c:fact:the_top\".\n\
15637\n\
15638Address locations begin with \"*\" and specify an exact address in the\n\
15639program. Example: To specify the fourth byte past the start function\n\
15640\"main\", use \"*main + 4\".\n\
15641\n\
15642Explicit locations are similar to linespecs but use an option/argument\n\
15643syntax to specify location parameters.\n\
15644Example: To specify the start of the label named \"the_top\" in the\n\
15645function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15646-function fact -label the_top\".\n"
15647
4a64f543
MS
15648/* This help string is used for the break, hbreak, tbreak and thbreak
15649 commands. It is defined as a macro to prevent duplication.
15650 COMMAND should be a string constant containing the name of the
15651 command. */
629500fa 15652
31e2b00f 15653#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15654command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15655PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15656probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15657guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15658`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15659LOCATION may be a linespec, address, or explicit location as described\n\
15660below.\n\
15661\n\
dc10affe
PA
15662With no LOCATION, uses current execution address of the selected\n\
15663stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15664\n\
15665THREADNUM is the number from \"info threads\".\n\
15666CONDITION is a boolean expression.\n\
629500fa 15667\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15668Multiple breakpoints at one place are permitted, and useful if their\n\
15669conditions are different.\n\
31e2b00f
AS
15670\n\
15671Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15672
44feb3ce
TT
15673/* List of subcommands for "catch". */
15674static struct cmd_list_element *catch_cmdlist;
15675
15676/* List of subcommands for "tcatch". */
15677static struct cmd_list_element *tcatch_cmdlist;
15678
9ac4176b 15679void
44feb3ce 15680add_catch_command (char *name, char *docstring,
82ae6c8d 15681 cmd_sfunc_ftype *sfunc,
625e8578 15682 completer_ftype *completer,
44feb3ce
TT
15683 void *user_data_catch,
15684 void *user_data_tcatch)
15685{
15686 struct cmd_list_element *command;
15687
15688 command = add_cmd (name, class_breakpoint, NULL, docstring,
15689 &catch_cmdlist);
15690 set_cmd_sfunc (command, sfunc);
15691 set_cmd_context (command, user_data_catch);
a96d9b2e 15692 set_cmd_completer (command, completer);
44feb3ce
TT
15693
15694 command = add_cmd (name, class_breakpoint, NULL, docstring,
15695 &tcatch_cmdlist);
15696 set_cmd_sfunc (command, sfunc);
15697 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15698 set_cmd_completer (command, completer);
44feb3ce
TT
15699}
15700
6149aea9
PA
15701static void
15702save_command (char *arg, int from_tty)
15703{
3e43a32a
MS
15704 printf_unfiltered (_("\"save\" must be followed by "
15705 "the name of a save subcommand.\n"));
635c7e8a 15706 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15707}
15708
84f4c1fe
PM
15709struct breakpoint *
15710iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15711 void *data)
15712{
35df4500 15713 struct breakpoint *b, *b_tmp;
84f4c1fe 15714
35df4500 15715 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15716 {
15717 if ((*callback) (b, data))
15718 return b;
15719 }
15720
15721 return NULL;
15722}
15723
0574c78f
GB
15724/* Zero if any of the breakpoint's locations could be a location where
15725 functions have been inlined, nonzero otherwise. */
15726
15727static int
15728is_non_inline_function (struct breakpoint *b)
15729{
15730 /* The shared library event breakpoint is set on the address of a
15731 non-inline function. */
15732 if (b->type == bp_shlib_event)
15733 return 1;
15734
15735 return 0;
15736}
15737
15738/* Nonzero if the specified PC cannot be a location where functions
15739 have been inlined. */
15740
15741int
09ac7c10
TT
15742pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15743 const struct target_waitstatus *ws)
0574c78f
GB
15744{
15745 struct breakpoint *b;
15746 struct bp_location *bl;
15747
15748 ALL_BREAKPOINTS (b)
15749 {
15750 if (!is_non_inline_function (b))
15751 continue;
15752
15753 for (bl = b->loc; bl != NULL; bl = bl->next)
15754 {
15755 if (!bl->shlib_disabled
09ac7c10 15756 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15757 return 1;
15758 }
15759 }
15760
15761 return 0;
15762}
15763
2f202fde
JK
15764/* Remove any references to OBJFILE which is going to be freed. */
15765
15766void
15767breakpoint_free_objfile (struct objfile *objfile)
15768{
15769 struct bp_location **locp, *loc;
15770
15771 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15772 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15773 loc->symtab = NULL;
15774}
15775
2060206e
PA
15776void
15777initialize_breakpoint_ops (void)
15778{
15779 static int initialized = 0;
15780
15781 struct breakpoint_ops *ops;
15782
15783 if (initialized)
15784 return;
15785 initialized = 1;
15786
15787 /* The breakpoint_ops structure to be inherit by all kinds of
15788 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15789 internal and momentary breakpoints, etc.). */
15790 ops = &bkpt_base_breakpoint_ops;
15791 *ops = base_breakpoint_ops;
15792 ops->re_set = bkpt_re_set;
15793 ops->insert_location = bkpt_insert_location;
15794 ops->remove_location = bkpt_remove_location;
15795 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15796 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15797 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15798 ops->decode_location = bkpt_decode_location;
2060206e
PA
15799
15800 /* The breakpoint_ops structure to be used in regular breakpoints. */
15801 ops = &bkpt_breakpoint_ops;
15802 *ops = bkpt_base_breakpoint_ops;
15803 ops->re_set = bkpt_re_set;
15804 ops->resources_needed = bkpt_resources_needed;
15805 ops->print_it = bkpt_print_it;
15806 ops->print_mention = bkpt_print_mention;
15807 ops->print_recreate = bkpt_print_recreate;
15808
15809 /* Ranged breakpoints. */
15810 ops = &ranged_breakpoint_ops;
15811 *ops = bkpt_breakpoint_ops;
15812 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15813 ops->resources_needed = resources_needed_ranged_breakpoint;
15814 ops->print_it = print_it_ranged_breakpoint;
15815 ops->print_one = print_one_ranged_breakpoint;
15816 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15817 ops->print_mention = print_mention_ranged_breakpoint;
15818 ops->print_recreate = print_recreate_ranged_breakpoint;
15819
15820 /* Internal breakpoints. */
15821 ops = &internal_breakpoint_ops;
15822 *ops = bkpt_base_breakpoint_ops;
15823 ops->re_set = internal_bkpt_re_set;
15824 ops->check_status = internal_bkpt_check_status;
15825 ops->print_it = internal_bkpt_print_it;
15826 ops->print_mention = internal_bkpt_print_mention;
15827
15828 /* Momentary breakpoints. */
15829 ops = &momentary_breakpoint_ops;
15830 *ops = bkpt_base_breakpoint_ops;
15831 ops->re_set = momentary_bkpt_re_set;
15832 ops->check_status = momentary_bkpt_check_status;
15833 ops->print_it = momentary_bkpt_print_it;
15834 ops->print_mention = momentary_bkpt_print_mention;
15835
e2e4d78b
JK
15836 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15837 ops = &longjmp_breakpoint_ops;
15838 *ops = momentary_breakpoint_ops;
15839 ops->dtor = longjmp_bkpt_dtor;
15840
55aa24fb
SDJ
15841 /* Probe breakpoints. */
15842 ops = &bkpt_probe_breakpoint_ops;
15843 *ops = bkpt_breakpoint_ops;
15844 ops->insert_location = bkpt_probe_insert_location;
15845 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15846 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15847 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15848
2060206e
PA
15849 /* Watchpoints. */
15850 ops = &watchpoint_breakpoint_ops;
15851 *ops = base_breakpoint_ops;
3a5c3e22 15852 ops->dtor = dtor_watchpoint;
2060206e
PA
15853 ops->re_set = re_set_watchpoint;
15854 ops->insert_location = insert_watchpoint;
15855 ops->remove_location = remove_watchpoint;
15856 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15857 ops->check_status = check_status_watchpoint;
15858 ops->resources_needed = resources_needed_watchpoint;
15859 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15860 ops->print_it = print_it_watchpoint;
15861 ops->print_mention = print_mention_watchpoint;
15862 ops->print_recreate = print_recreate_watchpoint;
427cd150 15863 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15864
15865 /* Masked watchpoints. */
15866 ops = &masked_watchpoint_breakpoint_ops;
15867 *ops = watchpoint_breakpoint_ops;
15868 ops->insert_location = insert_masked_watchpoint;
15869 ops->remove_location = remove_masked_watchpoint;
15870 ops->resources_needed = resources_needed_masked_watchpoint;
15871 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15872 ops->print_it = print_it_masked_watchpoint;
15873 ops->print_one_detail = print_one_detail_masked_watchpoint;
15874 ops->print_mention = print_mention_masked_watchpoint;
15875 ops->print_recreate = print_recreate_masked_watchpoint;
15876
15877 /* Tracepoints. */
15878 ops = &tracepoint_breakpoint_ops;
15879 *ops = base_breakpoint_ops;
15880 ops->re_set = tracepoint_re_set;
15881 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15882 ops->print_one_detail = tracepoint_print_one_detail;
15883 ops->print_mention = tracepoint_print_mention;
15884 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15885 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15886 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15887 ops->decode_location = tracepoint_decode_location;
983af33b 15888
55aa24fb
SDJ
15889 /* Probe tracepoints. */
15890 ops = &tracepoint_probe_breakpoint_ops;
15891 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15892 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15893 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15894
983af33b
SDJ
15895 /* Static tracepoints with marker (`-m'). */
15896 ops = &strace_marker_breakpoint_ops;
15897 *ops = tracepoint_breakpoint_ops;
5f700d83 15898 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15899 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15900 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15901
15902 /* Fork catchpoints. */
15903 ops = &catch_fork_breakpoint_ops;
15904 *ops = base_breakpoint_ops;
15905 ops->insert_location = insert_catch_fork;
15906 ops->remove_location = remove_catch_fork;
15907 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15908 ops->print_it = print_it_catch_fork;
15909 ops->print_one = print_one_catch_fork;
15910 ops->print_mention = print_mention_catch_fork;
15911 ops->print_recreate = print_recreate_catch_fork;
15912
15913 /* Vfork catchpoints. */
15914 ops = &catch_vfork_breakpoint_ops;
15915 *ops = base_breakpoint_ops;
15916 ops->insert_location = insert_catch_vfork;
15917 ops->remove_location = remove_catch_vfork;
15918 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15919 ops->print_it = print_it_catch_vfork;
15920 ops->print_one = print_one_catch_vfork;
15921 ops->print_mention = print_mention_catch_vfork;
15922 ops->print_recreate = print_recreate_catch_vfork;
15923
15924 /* Exec catchpoints. */
15925 ops = &catch_exec_breakpoint_ops;
15926 *ops = base_breakpoint_ops;
15927 ops->dtor = dtor_catch_exec;
15928 ops->insert_location = insert_catch_exec;
15929 ops->remove_location = remove_catch_exec;
15930 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15931 ops->print_it = print_it_catch_exec;
15932 ops->print_one = print_one_catch_exec;
15933 ops->print_mention = print_mention_catch_exec;
15934 ops->print_recreate = print_recreate_catch_exec;
15935
edcc5120
TT
15936 /* Solib-related catchpoints. */
15937 ops = &catch_solib_breakpoint_ops;
15938 *ops = base_breakpoint_ops;
15939 ops->dtor = dtor_catch_solib;
15940 ops->insert_location = insert_catch_solib;
15941 ops->remove_location = remove_catch_solib;
15942 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15943 ops->check_status = check_status_catch_solib;
15944 ops->print_it = print_it_catch_solib;
15945 ops->print_one = print_one_catch_solib;
15946 ops->print_mention = print_mention_catch_solib;
15947 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15948
15949 ops = &dprintf_breakpoint_ops;
15950 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15951 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15952 ops->resources_needed = bkpt_resources_needed;
15953 ops->print_it = bkpt_print_it;
15954 ops->print_mention = bkpt_print_mention;
2d9442cc 15955 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15956 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15957 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15958}
15959
8bfd80db
YQ
15960/* Chain containing all defined "enable breakpoint" subcommands. */
15961
15962static struct cmd_list_element *enablebreaklist = NULL;
15963
c906108c 15964void
fba45db2 15965_initialize_breakpoint (void)
c906108c
SS
15966{
15967 struct cmd_list_element *c;
15968
2060206e
PA
15969 initialize_breakpoint_ops ();
15970
84acb35a 15971 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15972 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15973 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15974
55aa24fb
SDJ
15975 breakpoint_objfile_key
15976 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15977
c906108c
SS
15978 breakpoint_chain = 0;
15979 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15980 before a breakpoint is set. */
15981 breakpoint_count = 0;
15982
1042e4c0
SS
15983 tracepoint_count = 0;
15984
1bedd215
AC
15985 add_com ("ignore", class_breakpoint, ignore_command, _("\
15986Set ignore-count of breakpoint number N to COUNT.\n\
15987Usage is `ignore N COUNT'."));
c906108c 15988
1bedd215
AC
15989 add_com ("commands", class_breakpoint, commands_command, _("\
15990Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
15991Give breakpoint number as argument after \"commands\".\n\
15992With no argument, the targeted breakpoint is the last one set.\n\
15993The commands themselves follow starting on the next line.\n\
15994Type a line containing \"end\" to indicate the end of them.\n\
15995Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15996then no output is printed when it is hit, except what the commands print."));
c906108c 15997
d55637df 15998 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15999Specify breakpoint number N to break only if COND is true.\n\
c906108c 16000Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16001expression to be evaluated whenever breakpoint N is reached."));
d55637df 16002 set_cmd_completer (c, condition_completer);
c906108c 16003
1bedd215 16004 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16005Set a temporary breakpoint.\n\
c906108c
SS
16006Like \"break\" except the breakpoint is only temporary,\n\
16007so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16008by using \"enable delete\" on the breakpoint number.\n\
16009\n"
16010BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16011 set_cmd_completer (c, location_completer);
c94fdfd0 16012
1bedd215 16013 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16014Set a hardware assisted breakpoint.\n\
c906108c 16015Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16016some target hardware may not have this support.\n\
16017\n"
16018BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16019 set_cmd_completer (c, location_completer);
c906108c 16020
1bedd215 16021 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16022Set a temporary hardware assisted breakpoint.\n\
c906108c 16023Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16024so it will be deleted when hit.\n\
16025\n"
16026BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16027 set_cmd_completer (c, location_completer);
c906108c 16028
1bedd215
AC
16029 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16030Enable some breakpoints.\n\
c906108c
SS
16031Give breakpoint numbers (separated by spaces) as arguments.\n\
16032With no subcommand, breakpoints are enabled until you command otherwise.\n\
16033This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16034With a subcommand you can enable temporarily."),
c906108c 16035 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
16036
16037 add_com_alias ("en", "enable", class_breakpoint, 1);
16038
84951ab5 16039 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16040Enable some breakpoints.\n\
c906108c
SS
16041Give breakpoint numbers (separated by spaces) as arguments.\n\
16042This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16043May be abbreviated to simply \"enable\".\n"),
c5aa993b 16044 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16045
1a966eab
AC
16046 add_cmd ("once", no_class, enable_once_command, _("\
16047Enable breakpoints for one hit. Give breakpoint numbers.\n\
16048If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16049 &enablebreaklist);
16050
1a966eab
AC
16051 add_cmd ("delete", no_class, enable_delete_command, _("\
16052Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16053If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16054 &enablebreaklist);
16055
816338b5
SS
16056 add_cmd ("count", no_class, enable_count_command, _("\
16057Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16058If a breakpoint is hit while enabled in this fashion,\n\
16059the count is decremented; when it reaches zero, the breakpoint is disabled."),
16060 &enablebreaklist);
16061
1a966eab
AC
16062 add_cmd ("delete", no_class, enable_delete_command, _("\
16063Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16064If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16065 &enablelist);
16066
1a966eab
AC
16067 add_cmd ("once", no_class, enable_once_command, _("\
16068Enable breakpoints for one hit. Give breakpoint numbers.\n\
16069If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16070 &enablelist);
16071
16072 add_cmd ("count", no_class, enable_count_command, _("\
16073Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16074If a breakpoint is hit while enabled in this fashion,\n\
16075the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16076 &enablelist);
16077
1bedd215
AC
16078 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16079Disable some breakpoints.\n\
c906108c
SS
16080Arguments are breakpoint numbers with spaces in between.\n\
16081To disable all breakpoints, give no argument.\n\
64b9b334 16082A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16083 &disablelist, "disable ", 1, &cmdlist);
16084 add_com_alias ("dis", "disable", class_breakpoint, 1);
16085 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 16086
1a966eab
AC
16087 add_cmd ("breakpoints", class_alias, disable_command, _("\
16088Disable some breakpoints.\n\
c906108c
SS
16089Arguments are breakpoint numbers with spaces in between.\n\
16090To disable all breakpoints, give no argument.\n\
64b9b334 16091A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16092This command may be abbreviated \"disable\"."),
c906108c
SS
16093 &disablelist);
16094
1bedd215
AC
16095 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16096Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16097Arguments are breakpoint numbers with spaces in between.\n\
16098To delete all breakpoints, give no argument.\n\
16099\n\
16100Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16101The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16102 &deletelist, "delete ", 1, &cmdlist);
16103 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16104 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16105
1a966eab
AC
16106 add_cmd ("breakpoints", class_alias, delete_command, _("\
16107Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16108Arguments are breakpoint numbers with spaces in between.\n\
16109To delete all breakpoints, give no argument.\n\
1a966eab 16110This command may be abbreviated \"delete\"."),
c906108c
SS
16111 &deletelist);
16112
1bedd215 16113 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
16114Clear breakpoint at specified location.\n\
16115Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
16116\n\
16117With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
16118is executing in.\n"
16119"\n" LOCATION_HELP_STRING "\n\
1bedd215 16120See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16121 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16122
1bedd215 16123 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 16124Set breakpoint at specified location.\n"
31e2b00f 16125BREAK_ARGS_HELP ("break")));
5ba2abeb 16126 set_cmd_completer (c, location_completer);
c94fdfd0 16127
c906108c
SS
16128 add_com_alias ("b", "break", class_run, 1);
16129 add_com_alias ("br", "break", class_run, 1);
16130 add_com_alias ("bre", "break", class_run, 1);
16131 add_com_alias ("brea", "break", class_run, 1);
16132
c906108c
SS
16133 if (dbx_commands)
16134 {
1bedd215
AC
16135 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16136Break in function/address or break at a line in the current file."),
c5aa993b
JM
16137 &stoplist, "stop ", 1, &cmdlist);
16138 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16139 _("Break in function or address."), &stoplist);
c5aa993b 16140 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16141 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16142 add_com ("status", class_info, breakpoints_info, _("\
16143Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16144The \"Type\" column indicates one of:\n\
16145\tbreakpoint - normal breakpoint\n\
16146\twatchpoint - watchpoint\n\
16147The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16148the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16149breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16150address and file/line number respectively.\n\
16151\n\
16152Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16153are set to the address of the last breakpoint listed unless the command\n\
16154is prefixed with \"server \".\n\n\
c906108c 16155Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16156breakpoint set."));
c906108c
SS
16157 }
16158
1bedd215 16159 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16160Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16161The \"Type\" column indicates one of:\n\
16162\tbreakpoint - normal breakpoint\n\
16163\twatchpoint - watchpoint\n\
16164The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16165the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16166breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16167address and file/line number respectively.\n\
16168\n\
16169Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16170are set to the address of the last breakpoint listed unless the command\n\
16171is prefixed with \"server \".\n\n\
c906108c 16172Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16173breakpoint set."));
c906108c 16174
6b04bdb7
MS
16175 add_info_alias ("b", "breakpoints", 1);
16176
1a966eab
AC
16177 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16178Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16179The \"Type\" column indicates one of:\n\
16180\tbreakpoint - normal breakpoint\n\
16181\twatchpoint - watchpoint\n\
16182\tlongjmp - internal breakpoint used to step through longjmp()\n\
16183\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16184\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16185\tfinish - internal breakpoint used by the \"finish\" command\n\
16186The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16187the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16188breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16189address and file/line number respectively.\n\
16190\n\
16191Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16192are set to the address of the last breakpoint listed unless the command\n\
16193is prefixed with \"server \".\n\n\
c906108c 16194Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16195breakpoint set."),
c906108c
SS
16196 &maintenanceinfolist);
16197
44feb3ce
TT
16198 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16199Set catchpoints to catch events."),
16200 &catch_cmdlist, "catch ",
16201 0/*allow-unknown*/, &cmdlist);
16202
16203 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16204Set temporary catchpoints to catch events."),
16205 &tcatch_cmdlist, "tcatch ",
16206 0/*allow-unknown*/, &cmdlist);
16207
44feb3ce
TT
16208 add_catch_command ("fork", _("Catch calls to fork."),
16209 catch_fork_command_1,
a96d9b2e 16210 NULL,
44feb3ce
TT
16211 (void *) (uintptr_t) catch_fork_permanent,
16212 (void *) (uintptr_t) catch_fork_temporary);
16213 add_catch_command ("vfork", _("Catch calls to vfork."),
16214 catch_fork_command_1,
a96d9b2e 16215 NULL,
44feb3ce
TT
16216 (void *) (uintptr_t) catch_vfork_permanent,
16217 (void *) (uintptr_t) catch_vfork_temporary);
16218 add_catch_command ("exec", _("Catch calls to exec."),
16219 catch_exec_command_1,
a96d9b2e
SDJ
16220 NULL,
16221 CATCH_PERMANENT,
16222 CATCH_TEMPORARY);
edcc5120
TT
16223 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16224Usage: catch load [REGEX]\n\
16225If REGEX is given, only stop for libraries matching the regular expression."),
16226 catch_load_command_1,
16227 NULL,
16228 CATCH_PERMANENT,
16229 CATCH_TEMPORARY);
16230 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16231Usage: catch unload [REGEX]\n\
16232If REGEX is given, only stop for libraries matching the regular expression."),
16233 catch_unload_command_1,
16234 NULL,
16235 CATCH_PERMANENT,
16236 CATCH_TEMPORARY);
c5aa993b 16237
1bedd215
AC
16238 c = add_com ("watch", class_breakpoint, watch_command, _("\
16239Set a watchpoint for an expression.\n\
06a64a0b 16240Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16241A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16242an expression changes.\n\
16243If -l or -location is given, this evaluates EXPRESSION and watches\n\
16244the memory to which it refers."));
65d12d83 16245 set_cmd_completer (c, expression_completer);
c906108c 16246
1bedd215
AC
16247 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16248Set a read watchpoint for an expression.\n\
06a64a0b 16249Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16250A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16251an expression is read.\n\
16252If -l or -location is given, this evaluates EXPRESSION and watches\n\
16253the memory to which it refers."));
65d12d83 16254 set_cmd_completer (c, expression_completer);
c906108c 16255
1bedd215
AC
16256 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16257Set a watchpoint for an expression.\n\
06a64a0b 16258Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16259A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16260an expression is either read or written.\n\
16261If -l or -location is given, this evaluates EXPRESSION and watches\n\
16262the memory to which it refers."));
65d12d83 16263 set_cmd_completer (c, expression_completer);
c906108c 16264
d77f58be 16265 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16266Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16267
920d2a44
AC
16268 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16269 respond to changes - contrary to the description. */
85c07804
AC
16270 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16271 &can_use_hw_watchpoints, _("\
16272Set debugger's willingness to use watchpoint hardware."), _("\
16273Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16274If zero, gdb will not use hardware for new watchpoints, even if\n\
16275such is available. (However, any hardware watchpoints that were\n\
16276created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16277hardware.)"),
16278 NULL,
920d2a44 16279 show_can_use_hw_watchpoints,
85c07804 16280 &setlist, &showlist);
c906108c
SS
16281
16282 can_use_hw_watchpoints = 1;
fa8d40ab 16283
1042e4c0
SS
16284 /* Tracepoint manipulation commands. */
16285
16286 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16287Set a tracepoint at specified location.\n\
1042e4c0
SS
16288\n"
16289BREAK_ARGS_HELP ("trace") "\n\
16290Do \"help tracepoints\" for info on other tracepoint commands."));
16291 set_cmd_completer (c, location_completer);
16292
16293 add_com_alias ("tp", "trace", class_alias, 0);
16294 add_com_alias ("tr", "trace", class_alias, 1);
16295 add_com_alias ("tra", "trace", class_alias, 1);
16296 add_com_alias ("trac", "trace", class_alias, 1);
16297
7a697b8d 16298 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16299Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16300\n"
16301BREAK_ARGS_HELP ("ftrace") "\n\
16302Do \"help tracepoints\" for info on other tracepoint commands."));
16303 set_cmd_completer (c, location_completer);
16304
0fb4aa4b 16305 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16306Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16307\n\
16308strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16309LOCATION may be a linespec, explicit, or address location (described below) \n\
16310or -m MARKER_ID.\n\n\
16311If a marker id is specified, probe the marker with that name. With\n\
16312no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16313Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16314This collects arbitrary user data passed in the probe point call to the\n\
16315tracing library. You can inspect it when analyzing the trace buffer,\n\
16316by printing the $_sdata variable like any other convenience variable.\n\
16317\n\
16318CONDITION is a boolean expression.\n\
629500fa 16319\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16320Multiple tracepoints at one place are permitted, and useful if their\n\
16321conditions are different.\n\
0fb4aa4b
PA
16322\n\
16323Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16324Do \"help tracepoints\" for info on other tracepoint commands."));
16325 set_cmd_completer (c, location_completer);
16326
1042e4c0 16327 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16328Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16329Convenience variable \"$tpnum\" contains the number of the\n\
16330last tracepoint set."));
16331
16332 add_info_alias ("tp", "tracepoints", 1);
16333
16334 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16335Delete specified tracepoints.\n\
16336Arguments are tracepoint numbers, separated by spaces.\n\
16337No argument means delete all tracepoints."),
16338 &deletelist);
7e20dfcd 16339 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16340
16341 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16342Disable specified tracepoints.\n\
16343Arguments are tracepoint numbers, separated by spaces.\n\
16344No argument means disable all tracepoints."),
16345 &disablelist);
16346 deprecate_cmd (c, "disable");
16347
16348 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16349Enable specified tracepoints.\n\
16350Arguments are tracepoint numbers, separated by spaces.\n\
16351No argument means enable all tracepoints."),
16352 &enablelist);
16353 deprecate_cmd (c, "enable");
16354
16355 add_com ("passcount", class_trace, trace_pass_command, _("\
16356Set the passcount for a tracepoint.\n\
16357The trace will end when the tracepoint has been passed 'count' times.\n\
16358Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16359if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16360
6149aea9
PA
16361 add_prefix_cmd ("save", class_breakpoint, save_command,
16362 _("Save breakpoint definitions as a script."),
16363 &save_cmdlist, "save ",
16364 0/*allow-unknown*/, &cmdlist);
16365
16366 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16367Save current breakpoint definitions as a script.\n\
cce7e648 16368This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16369catchpoints, tracepoints). Use the 'source' command in another debug\n\
16370session to restore them."),
16371 &save_cmdlist);
16372 set_cmd_completer (c, filename_completer);
16373
16374 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16375Save current tracepoint definitions as a script.\n\
6149aea9
PA
16376Use the 'source' command in another debug session to restore them."),
16377 &save_cmdlist);
1042e4c0
SS
16378 set_cmd_completer (c, filename_completer);
16379
6149aea9
PA
16380 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16381 deprecate_cmd (c, "save tracepoints");
16382
1bedd215 16383 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16384Breakpoint specific settings\n\
16385Configure various breakpoint-specific variables such as\n\
1bedd215 16386pending breakpoint behavior"),
fa8d40ab
JJ
16387 &breakpoint_set_cmdlist, "set breakpoint ",
16388 0/*allow-unknown*/, &setlist);
1bedd215 16389 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16390Breakpoint specific settings\n\
16391Configure various breakpoint-specific variables such as\n\
1bedd215 16392pending breakpoint behavior"),
fa8d40ab
JJ
16393 &breakpoint_show_cmdlist, "show breakpoint ",
16394 0/*allow-unknown*/, &showlist);
16395
7915a72c
AC
16396 add_setshow_auto_boolean_cmd ("pending", no_class,
16397 &pending_break_support, _("\
16398Set debugger's behavior regarding pending breakpoints."), _("\
16399Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16400If on, an unrecognized breakpoint location will cause gdb to create a\n\
16401pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16402an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16403user-query to see if a pending breakpoint should be created."),
2c5b56ce 16404 NULL,
920d2a44 16405 show_pending_break_support,
6e1d7d6c
AC
16406 &breakpoint_set_cmdlist,
16407 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16408
16409 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16410
16411 add_setshow_boolean_cmd ("auto-hw", no_class,
16412 &automatic_hardware_breakpoints, _("\
16413Set automatic usage of hardware breakpoints."), _("\
16414Show automatic usage of hardware breakpoints."), _("\
16415If set, the debugger will automatically use hardware breakpoints for\n\
16416breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16417a warning will be emitted for such breakpoints."),
16418 NULL,
16419 show_automatic_hardware_breakpoints,
16420 &breakpoint_set_cmdlist,
16421 &breakpoint_show_cmdlist);
74960c60 16422
a25a5a45
PA
16423 add_setshow_boolean_cmd ("always-inserted", class_support,
16424 &always_inserted_mode, _("\
74960c60
VP
16425Set mode for inserting breakpoints."), _("\
16426Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16427When this mode is on, breakpoints are inserted immediately as soon as\n\
16428they're created, kept inserted even when execution stops, and removed\n\
16429only when the user deletes them. When this mode is off (the default),\n\
16430breakpoints are inserted only when execution continues, and removed\n\
16431when execution stops."),
72d0e2c5
YQ
16432 NULL,
16433 &show_always_inserted_mode,
16434 &breakpoint_set_cmdlist,
16435 &breakpoint_show_cmdlist);
f1310107 16436
b775012e
LM
16437 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16438 condition_evaluation_enums,
16439 &condition_evaluation_mode_1, _("\
16440Set mode of breakpoint condition evaluation."), _("\
16441Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16442When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16443evaluated on the host's side by GDB. When it is set to \"target\",\n\
16444breakpoint conditions will be downloaded to the target (if the target\n\
16445supports such feature) and conditions will be evaluated on the target's side.\n\
16446If this is set to \"auto\" (default), this will be automatically set to\n\
16447\"target\" if it supports condition evaluation, otherwise it will\n\
16448be set to \"gdb\""),
16449 &set_condition_evaluation_mode,
16450 &show_condition_evaluation_mode,
16451 &breakpoint_set_cmdlist,
16452 &breakpoint_show_cmdlist);
16453
f1310107
TJB
16454 add_com ("break-range", class_breakpoint, break_range_command, _("\
16455Set a breakpoint for an address range.\n\
16456break-range START-LOCATION, END-LOCATION\n\
16457where START-LOCATION and END-LOCATION can be one of the following:\n\
16458 LINENUM, for that line in the current file,\n\
16459 FILE:LINENUM, for that line in that file,\n\
16460 +OFFSET, for that number of lines after the current line\n\
16461 or the start of the range\n\
16462 FUNCTION, for the first line in that function,\n\
16463 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16464 *ADDRESS, for the instruction at that address.\n\
16465\n\
16466The breakpoint will stop execution of the inferior whenever it executes\n\
16467an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16468range (including START-LOCATION and END-LOCATION)."));
16469
e7e0cddf 16470 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16471Set a dynamic printf at specified location.\n\
e7e0cddf 16472dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16473location may be a linespec, explicit, or address location.\n"
16474"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16475 set_cmd_completer (c, location_completer);
16476
16477 add_setshow_enum_cmd ("dprintf-style", class_support,
16478 dprintf_style_enums, &dprintf_style, _("\
16479Set the style of usage for dynamic printf."), _("\
16480Show the style of usage for dynamic printf."), _("\
16481This setting chooses how GDB will do a dynamic printf.\n\
16482If the value is \"gdb\", then the printing is done by GDB to its own\n\
16483console, as with the \"printf\" command.\n\
16484If the value is \"call\", the print is done by calling a function in your\n\
16485program; by default printf(), but you can choose a different function or\n\
16486output stream by setting dprintf-function and dprintf-channel."),
16487 update_dprintf_commands, NULL,
16488 &setlist, &showlist);
16489
16490 dprintf_function = xstrdup ("printf");
16491 add_setshow_string_cmd ("dprintf-function", class_support,
16492 &dprintf_function, _("\
16493Set the function to use for dynamic printf"), _("\
16494Show the function to use for dynamic printf"), NULL,
16495 update_dprintf_commands, NULL,
16496 &setlist, &showlist);
16497
16498 dprintf_channel = xstrdup ("");
16499 add_setshow_string_cmd ("dprintf-channel", class_support,
16500 &dprintf_channel, _("\
16501Set the channel to use for dynamic printf"), _("\
16502Show the channel to use for dynamic printf"), NULL,
16503 update_dprintf_commands, NULL,
16504 &setlist, &showlist);
16505
d3ce09f5
SS
16506 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16507 &disconnected_dprintf, _("\
16508Set whether dprintf continues after GDB disconnects."), _("\
16509Show whether dprintf continues after GDB disconnects."), _("\
16510Use this to let dprintf commands continue to hit and produce output\n\
16511even if GDB disconnects or detaches from the target."),
16512 NULL,
16513 NULL,
16514 &setlist, &showlist);
16515
16516 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16517agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16518(target agent only) This is useful for formatted output in user-defined commands."));
16519
765dc015 16520 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16521
16522 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16523 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16524}
This page took 3.589364 seconds and 4 git commands to generate.