]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/breakpoint.c
Convert infcalls to thread_fsm mechanism
[thirdparty/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"
d3ce09f5 71
1042e4c0
SS
72/* readline include files */
73#include "readline/readline.h"
74#include "readline/history.h"
75
76/* readline defines this. */
77#undef savestring
78
034dad6f 79#include "mi/mi-common.h"
6dddc817 80#include "extension.h"
104c1213 81
e7e8980f
YQ
82/* Enums for exception-handling support. */
83enum exception_event_kind
84{
85 EX_EVENT_THROW,
591f19e8 86 EX_EVENT_RETHROW,
e7e8980f
YQ
87 EX_EVENT_CATCH
88};
89
4a64f543 90/* Prototypes for local functions. */
c906108c 91
a14ed312 92static void enable_delete_command (char *, int);
c906108c 93
a14ed312 94static void enable_once_command (char *, int);
c906108c 95
816338b5
SS
96static void enable_count_command (char *, int);
97
a14ed312 98static void disable_command (char *, int);
c906108c 99
a14ed312 100static void enable_command (char *, int);
c906108c 101
95a42b64
TT
102static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
103 void *),
104 void *);
c906108c 105
a14ed312 106static void ignore_command (char *, int);
c906108c 107
4efb68b1 108static int breakpoint_re_set_one (void *);
c906108c 109
348d480f
PA
110static void breakpoint_re_set_default (struct breakpoint *);
111
f00aae0f
KS
112static void
113 create_sals_from_location_default (const struct event_location *location,
114 struct linespec_result *canonical,
115 enum bptype type_wanted);
983af33b
SDJ
116
117static void create_breakpoints_sal_default (struct gdbarch *,
118 struct linespec_result *,
e7e0cddf 119 char *, char *, enum bptype,
983af33b
SDJ
120 enum bpdisp, int, int,
121 int,
122 const struct breakpoint_ops *,
44f238bb 123 int, int, int, unsigned);
983af33b 124
f00aae0f
KS
125static void decode_location_default (struct breakpoint *b,
126 const struct event_location *location,
127 struct symtabs_and_lines *sals);
983af33b 128
a14ed312 129static void clear_command (char *, int);
c906108c 130
a14ed312 131static void catch_command (char *, int);
c906108c 132
a9634178 133static int can_use_hardware_watchpoint (struct value *);
c906108c 134
98deb0da 135static void break_command_1 (char *, int, int);
c906108c 136
a14ed312 137static void mention (struct breakpoint *);
c906108c 138
348d480f
PA
139static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
140 enum bptype,
c0a91b2b 141 const struct breakpoint_ops *);
3742cc8b
YQ
142static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
143 const struct symtab_and_line *);
144
4a64f543
MS
145/* This function is used in gdbtk sources and thus can not be made
146 static. */
63c252f8 147struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 148 struct symtab_and_line,
c0a91b2b
TT
149 enum bptype,
150 const struct breakpoint_ops *);
c906108c 151
06edf0c0
PA
152static struct breakpoint *
153 momentary_breakpoint_from_master (struct breakpoint *orig,
154 enum bptype type,
a1aa2221
LM
155 const struct breakpoint_ops *ops,
156 int loc_enabled);
06edf0c0 157
76897487
KB
158static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
159
a6d9a66e
UW
160static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
161 CORE_ADDR bpaddr,
88f7da05 162 enum bptype bptype);
76897487 163
6c95b8df
PA
164static void describe_other_breakpoints (struct gdbarch *,
165 struct program_space *, CORE_ADDR,
5af949e3 166 struct obj_section *, int);
c906108c 167
85d721b8
PA
168static int watchpoint_locations_match (struct bp_location *loc1,
169 struct bp_location *loc2);
170
f1310107
TJB
171static int breakpoint_location_address_match (struct bp_location *bl,
172 struct address_space *aspace,
173 CORE_ADDR addr);
174
a14ed312 175static void breakpoints_info (char *, int);
c906108c 176
d77f58be
SS
177static void watchpoints_info (char *, int);
178
e5a67952
MS
179static int breakpoint_1 (char *, int,
180 int (*) (const struct breakpoint *));
c906108c 181
4efb68b1 182static int breakpoint_cond_eval (void *);
c906108c 183
4efb68b1 184static void cleanup_executing_breakpoints (void *);
c906108c 185
a14ed312 186static void commands_command (char *, int);
c906108c 187
a14ed312 188static void condition_command (char *, int);
c906108c 189
c5aa993b
JM
190typedef enum
191 {
192 mark_inserted,
193 mark_uninserted
194 }
195insertion_state_t;
c906108c 196
0bde7532 197static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 198static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 199
e514a9d6 200static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 201
4efb68b1 202static int watchpoint_check (void *);
c906108c 203
a14ed312 204static void maintenance_info_breakpoints (char *, int);
c906108c 205
a14ed312 206static int hw_breakpoint_used_count (void);
c906108c 207
a1398e0c
PA
208static int hw_watchpoint_use_count (struct breakpoint *);
209
210static int hw_watchpoint_used_count_others (struct breakpoint *except,
211 enum bptype type,
212 int *other_type_used);
c906108c 213
a14ed312 214static void hbreak_command (char *, int);
c906108c 215
a14ed312 216static void thbreak_command (char *, int);
c906108c 217
816338b5
SS
218static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
219 int count);
c906108c 220
a14ed312 221static void stop_command (char *arg, int from_tty);
7a292a7a 222
a14ed312 223static void stopin_command (char *arg, int from_tty);
7a292a7a 224
a14ed312 225static void stopat_command (char *arg, int from_tty);
7a292a7a 226
a14ed312 227static void tcatch_command (char *arg, int from_tty);
7a292a7a 228
fe3f5fa8 229static void free_bp_location (struct bp_location *loc);
f431efe5
PA
230static void incref_bp_location (struct bp_location *loc);
231static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 232
39d61571 233static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 234
44702360
PA
235/* update_global_location_list's modes of operation wrt to whether to
236 insert locations now. */
237enum ugll_insert_mode
238{
239 /* Don't insert any breakpoint locations into the inferior, only
240 remove already-inserted locations that no longer should be
241 inserted. Functions that delete a breakpoint or breakpoints
242 should specify this mode, so that deleting a breakpoint doesn't
243 have the side effect of inserting the locations of other
244 breakpoints that are marked not-inserted, but should_be_inserted
245 returns true on them.
246
247 This behavior is useful is situations close to tear-down -- e.g.,
248 after an exec, while the target still has execution, but
249 breakpoint shadows of the previous executable image should *NOT*
250 be restored to the new image; or before detaching, where the
251 target still has execution and wants to delete breakpoints from
252 GDB's lists, and all breakpoints had already been removed from
253 the inferior. */
254 UGLL_DONT_INSERT,
255
a25a5a45
PA
256 /* May insert breakpoints iff breakpoints_should_be_inserted_now
257 claims breakpoints should be inserted now. */
04086b45
PA
258 UGLL_MAY_INSERT,
259
a25a5a45
PA
260 /* Insert locations now, irrespective of
261 breakpoints_should_be_inserted_now. E.g., say all threads are
262 stopped right now, and the user did "continue". We need to
263 insert breakpoints _before_ resuming the target, but
264 UGLL_MAY_INSERT wouldn't insert them, because
265 breakpoints_should_be_inserted_now returns false at that point,
266 as no thread is running yet. */
04086b45 267 UGLL_INSERT
44702360
PA
268};
269
270static void update_global_location_list (enum ugll_insert_mode);
a5606eee 271
44702360 272static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 273
d77f58be 274static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
275
276static void insert_breakpoint_locations (void);
a5606eee 277
1042e4c0
SS
278static void tracepoints_info (char *, int);
279
280static void delete_trace_command (char *, int);
281
282static void enable_trace_command (char *, int);
283
284static void disable_trace_command (char *, int);
285
286static void trace_pass_command (char *, int);
287
558a9d82
YQ
288static void set_tracepoint_count (int num);
289
9c06b0b4
TJB
290static int is_masked_watchpoint (const struct breakpoint *b);
291
b775012e
LM
292static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
293
983af33b
SDJ
294/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
295 otherwise. */
296
297static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 298
2060206e
PA
299/* The breakpoint_ops structure to be inherited by all breakpoint_ops
300 that are implemented on top of software or hardware breakpoints
301 (user breakpoints, internal and momentary breakpoints, etc.). */
302static struct breakpoint_ops bkpt_base_breakpoint_ops;
303
304/* Internal breakpoints class type. */
06edf0c0 305static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
306
307/* Momentary breakpoints class type. */
06edf0c0
PA
308static struct breakpoint_ops momentary_breakpoint_ops;
309
e2e4d78b
JK
310/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
311static struct breakpoint_ops longjmp_breakpoint_ops;
312
2060206e
PA
313/* The breakpoint_ops structure to be used in regular user created
314 breakpoints. */
315struct breakpoint_ops bkpt_breakpoint_ops;
316
55aa24fb
SDJ
317/* Breakpoints set on probes. */
318static struct breakpoint_ops bkpt_probe_breakpoint_ops;
319
e7e0cddf 320/* Dynamic printf class type. */
c5867ab6 321struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 322
d3ce09f5
SS
323/* The style in which to perform a dynamic printf. This is a user
324 option because different output options have different tradeoffs;
325 if GDB does the printing, there is better error handling if there
326 is a problem with any of the arguments, but using an inferior
327 function lets you have special-purpose printers and sending of
328 output to the same place as compiled-in print functions. */
329
330static const char dprintf_style_gdb[] = "gdb";
331static const char dprintf_style_call[] = "call";
332static const char dprintf_style_agent[] = "agent";
333static const char *const dprintf_style_enums[] = {
334 dprintf_style_gdb,
335 dprintf_style_call,
336 dprintf_style_agent,
337 NULL
338};
339static const char *dprintf_style = dprintf_style_gdb;
340
341/* The function to use for dynamic printf if the preferred style is to
342 call into the inferior. The value is simply a string that is
343 copied into the command, so it can be anything that GDB can
344 evaluate to a callable address, not necessarily a function name. */
345
346static char *dprintf_function = "";
347
348/* The channel to use for dynamic printf if the preferred style is to
349 call into the inferior; if a nonempty string, it will be passed to
350 the call as the first argument, with the format string as the
351 second. As with the dprintf function, this can be anything that
352 GDB knows how to evaluate, so in addition to common choices like
353 "stderr", this could be an app-specific expression like
354 "mystreams[curlogger]". */
355
356static char *dprintf_channel = "";
357
358/* True if dprintf commands should continue to operate even if GDB
359 has disconnected. */
360static int disconnected_dprintf = 1;
361
5cea2a26
PA
362/* A reference-counted struct command_line. This lets multiple
363 breakpoints share a single command list. */
364struct counted_command_line
365{
366 /* The reference count. */
367 int refc;
368
369 /* The command list. */
370 struct command_line *commands;
371};
372
373struct command_line *
374breakpoint_commands (struct breakpoint *b)
375{
376 return b->commands ? b->commands->commands : NULL;
377}
3daf8fe5 378
f3b1572e
PA
379/* Flag indicating that a command has proceeded the inferior past the
380 current breakpoint. */
381
382static int breakpoint_proceeded;
383
956a9fb9 384const char *
2cec12e5
AR
385bpdisp_text (enum bpdisp disp)
386{
4a64f543
MS
387 /* NOTE: the following values are a part of MI protocol and
388 represent values of 'disp' field returned when inferior stops at
389 a breakpoint. */
bc043ef3 390 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 391
2cec12e5
AR
392 return bpdisps[(int) disp];
393}
c906108c 394
4a64f543 395/* Prototypes for exported functions. */
c906108c 396/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 397 if such is available. */
c906108c
SS
398static int can_use_hw_watchpoints;
399
920d2a44
AC
400static void
401show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
402 struct cmd_list_element *c,
403 const char *value)
404{
3e43a32a
MS
405 fprintf_filtered (file,
406 _("Debugger's willingness to use "
407 "watchpoint hardware is %s.\n"),
920d2a44
AC
408 value);
409}
410
fa8d40ab
JJ
411/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
412 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 413 for unrecognized breakpoint locations.
fa8d40ab
JJ
414 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
415static enum auto_boolean pending_break_support;
920d2a44
AC
416static void
417show_pending_break_support (struct ui_file *file, int from_tty,
418 struct cmd_list_element *c,
419 const char *value)
420{
3e43a32a
MS
421 fprintf_filtered (file,
422 _("Debugger's behavior regarding "
423 "pending breakpoints is %s.\n"),
920d2a44
AC
424 value);
425}
fa8d40ab 426
765dc015 427/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 428 set with "break" but falling in read-only memory.
765dc015
VP
429 If 0, gdb will warn about such breakpoints, but won't automatically
430 use hardware breakpoints. */
431static int automatic_hardware_breakpoints;
432static void
433show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
434 struct cmd_list_element *c,
435 const char *value)
436{
3e43a32a
MS
437 fprintf_filtered (file,
438 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
439 value);
440}
441
a25a5a45
PA
442/* If on, GDB keeps breakpoints inserted even if the inferior is
443 stopped, and immediately inserts any new breakpoints as soon as
444 they're created. If off (default), GDB keeps breakpoints off of
445 the target as long as possible. That is, it delays inserting
446 breakpoints until the next resume, and removes them again when the
447 target fully stops. This is a bit safer in case GDB crashes while
448 processing user input. */
449static int always_inserted_mode = 0;
72d0e2c5 450
33e5cbd6 451static void
74960c60 452show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 453 struct cmd_list_element *c, const char *value)
74960c60 454{
a25a5a45
PA
455 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
456 value);
74960c60
VP
457}
458
b57bacec
PA
459/* See breakpoint.h. */
460
33e5cbd6 461int
a25a5a45 462breakpoints_should_be_inserted_now (void)
33e5cbd6 463{
a25a5a45
PA
464 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
465 {
466 /* If breakpoints are global, they should be inserted even if no
467 thread under gdb's control is running, or even if there are
468 no threads under GDB's control yet. */
469 return 1;
470 }
471 else if (target_has_execution)
472 {
372316f1
PA
473 struct thread_info *tp;
474
a25a5a45
PA
475 if (always_inserted_mode)
476 {
477 /* The user wants breakpoints inserted even if all threads
478 are stopped. */
479 return 1;
480 }
481
b57bacec
PA
482 if (threads_are_executing ())
483 return 1;
372316f1
PA
484
485 /* Don't remove breakpoints yet if, even though all threads are
486 stopped, we still have events to process. */
487 ALL_NON_EXITED_THREADS (tp)
488 if (tp->resumed
489 && tp->suspend.waitstatus_pending_p)
490 return 1;
a25a5a45
PA
491 }
492 return 0;
33e5cbd6 493}
765dc015 494
b775012e
LM
495static const char condition_evaluation_both[] = "host or target";
496
497/* Modes for breakpoint condition evaluation. */
498static const char condition_evaluation_auto[] = "auto";
499static const char condition_evaluation_host[] = "host";
500static const char condition_evaluation_target[] = "target";
501static const char *const condition_evaluation_enums[] = {
502 condition_evaluation_auto,
503 condition_evaluation_host,
504 condition_evaluation_target,
505 NULL
506};
507
508/* Global that holds the current mode for breakpoint condition evaluation. */
509static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
510
511/* Global that we use to display information to the user (gets its value from
512 condition_evaluation_mode_1. */
513static const char *condition_evaluation_mode = condition_evaluation_auto;
514
515/* Translate a condition evaluation mode MODE into either "host"
516 or "target". This is used mostly to translate from "auto" to the
517 real setting that is being used. It returns the translated
518 evaluation mode. */
519
520static const char *
521translate_condition_evaluation_mode (const char *mode)
522{
523 if (mode == condition_evaluation_auto)
524 {
525 if (target_supports_evaluation_of_breakpoint_conditions ())
526 return condition_evaluation_target;
527 else
528 return condition_evaluation_host;
529 }
530 else
531 return mode;
532}
533
534/* Discovers what condition_evaluation_auto translates to. */
535
536static const char *
537breakpoint_condition_evaluation_mode (void)
538{
539 return translate_condition_evaluation_mode (condition_evaluation_mode);
540}
541
542/* Return true if GDB should evaluate breakpoint conditions or false
543 otherwise. */
544
545static int
546gdb_evaluates_breakpoint_condition_p (void)
547{
548 const char *mode = breakpoint_condition_evaluation_mode ();
549
550 return (mode == condition_evaluation_host);
551}
552
a14ed312 553void _initialize_breakpoint (void);
c906108c 554
c906108c
SS
555/* Are we executing breakpoint commands? */
556static int executing_breakpoint_commands;
557
c02f5703
MS
558/* Are overlay event breakpoints enabled? */
559static int overlay_events_enabled;
560
e09342b5
TJB
561/* See description in breakpoint.h. */
562int target_exact_watchpoints = 0;
563
c906108c 564/* Walk the following statement or block through all breakpoints.
e5dd4106 565 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 566 current breakpoint. */
c906108c 567
5c44784c 568#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 569
5c44784c
JM
570#define ALL_BREAKPOINTS_SAFE(B,TMP) \
571 for (B = breakpoint_chain; \
572 B ? (TMP=B->next, 1): 0; \
573 B = TMP)
c906108c 574
4a64f543
MS
575/* Similar iterator for the low-level breakpoints. SAFE variant is
576 not provided so update_global_location_list must not be called
577 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 578
876fa593
JK
579#define ALL_BP_LOCATIONS(B,BP_TMP) \
580 for (BP_TMP = bp_location; \
581 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
582 BP_TMP++)
7cc221ef 583
b775012e
LM
584/* Iterates through locations with address ADDRESS for the currently selected
585 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
586 to where the loop should start from.
587 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
588 appropriate location to start with. */
589
590#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
591 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
592 BP_LOCP_TMP = BP_LOCP_START; \
593 BP_LOCP_START \
594 && (BP_LOCP_TMP < bp_location + bp_location_count \
595 && (*BP_LOCP_TMP)->address == ADDRESS); \
596 BP_LOCP_TMP++)
597
1042e4c0
SS
598/* Iterator for tracepoints only. */
599
600#define ALL_TRACEPOINTS(B) \
601 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 602 if (is_tracepoint (B))
1042e4c0 603
7cc221ef 604/* Chains of all breakpoints defined. */
c906108c
SS
605
606struct breakpoint *breakpoint_chain;
607
876fa593
JK
608/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
609
610static struct bp_location **bp_location;
611
612/* Number of elements of BP_LOCATION. */
613
614static unsigned bp_location_count;
615
4a64f543
MS
616/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
617 ADDRESS for the current elements of BP_LOCATION which get a valid
618 result from bp_location_has_shadow. You can use it for roughly
619 limiting the subrange of BP_LOCATION to scan for shadow bytes for
620 an address you need to read. */
876fa593
JK
621
622static CORE_ADDR bp_location_placed_address_before_address_max;
623
4a64f543
MS
624/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
625 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
626 BP_LOCATION which get a valid result from bp_location_has_shadow.
627 You can use it for roughly limiting the subrange of BP_LOCATION to
628 scan for shadow bytes for an address you need to read. */
876fa593
JK
629
630static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 631
4a64f543
MS
632/* The locations that no longer correspond to any breakpoint, unlinked
633 from bp_location array, but for which a hit may still be reported
634 by a target. */
20874c92
VP
635VEC(bp_location_p) *moribund_locations = NULL;
636
c906108c
SS
637/* Number of last breakpoint made. */
638
95a42b64
TT
639static int breakpoint_count;
640
86b17b60
PA
641/* The value of `breakpoint_count' before the last command that
642 created breakpoints. If the last (break-like) command created more
643 than one breakpoint, then the difference between BREAKPOINT_COUNT
644 and PREV_BREAKPOINT_COUNT is more than one. */
645static int prev_breakpoint_count;
c906108c 646
1042e4c0
SS
647/* Number of last tracepoint made. */
648
95a42b64 649static int tracepoint_count;
1042e4c0 650
6149aea9
PA
651static struct cmd_list_element *breakpoint_set_cmdlist;
652static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 653struct cmd_list_element *save_cmdlist;
6149aea9 654
badd37ce
SDJ
655/* See declaration at breakpoint.h. */
656
657struct breakpoint *
658breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
659 void *user_data)
660{
661 struct breakpoint *b = NULL;
662
663 ALL_BREAKPOINTS (b)
664 {
665 if (func (b, user_data) != 0)
666 break;
667 }
668
669 return b;
670}
671
468d015d
JJ
672/* Return whether a breakpoint is an active enabled breakpoint. */
673static int
674breakpoint_enabled (struct breakpoint *b)
675{
0d381245 676 return (b->enable_state == bp_enabled);
468d015d
JJ
677}
678
c906108c
SS
679/* Set breakpoint count to NUM. */
680
95a42b64 681static void
fba45db2 682set_breakpoint_count (int num)
c906108c 683{
86b17b60 684 prev_breakpoint_count = breakpoint_count;
c906108c 685 breakpoint_count = num;
4fa62494 686 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
687}
688
86b17b60
PA
689/* Used by `start_rbreak_breakpoints' below, to record the current
690 breakpoint count before "rbreak" creates any breakpoint. */
691static int rbreak_start_breakpoint_count;
692
95a42b64
TT
693/* Called at the start an "rbreak" command to record the first
694 breakpoint made. */
86b17b60 695
95a42b64
TT
696void
697start_rbreak_breakpoints (void)
698{
86b17b60 699 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
700}
701
702/* Called at the end of an "rbreak" command to record the last
703 breakpoint made. */
86b17b60 704
95a42b64
TT
705void
706end_rbreak_breakpoints (void)
707{
86b17b60 708 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
709}
710
4a64f543 711/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
712
713void
fba45db2 714clear_breakpoint_hit_counts (void)
c906108c
SS
715{
716 struct breakpoint *b;
717
718 ALL_BREAKPOINTS (b)
719 b->hit_count = 0;
720}
721
9add0f1b
TT
722/* Allocate a new counted_command_line with reference count of 1.
723 The new structure owns COMMANDS. */
724
725static struct counted_command_line *
726alloc_counted_command_line (struct command_line *commands)
727{
8d749320 728 struct counted_command_line *result = XNEW (struct counted_command_line);
cc59ec59 729
9add0f1b
TT
730 result->refc = 1;
731 result->commands = commands;
8d749320 732
9add0f1b
TT
733 return result;
734}
735
736/* Increment reference count. This does nothing if CMD is NULL. */
737
738static void
739incref_counted_command_line (struct counted_command_line *cmd)
740{
741 if (cmd)
742 ++cmd->refc;
743}
744
745/* Decrement reference count. If the reference count reaches 0,
746 destroy the counted_command_line. Sets *CMDP to NULL. This does
747 nothing if *CMDP is NULL. */
748
749static void
750decref_counted_command_line (struct counted_command_line **cmdp)
751{
752 if (*cmdp)
753 {
754 if (--(*cmdp)->refc == 0)
755 {
756 free_command_lines (&(*cmdp)->commands);
757 xfree (*cmdp);
758 }
759 *cmdp = NULL;
760 }
761}
762
763/* A cleanup function that calls decref_counted_command_line. */
764
765static void
766do_cleanup_counted_command_line (void *arg)
767{
768 decref_counted_command_line (arg);
769}
770
771/* Create a cleanup that calls decref_counted_command_line on the
772 argument. */
773
774static struct cleanup *
775make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
776{
777 return make_cleanup (do_cleanup_counted_command_line, cmdp);
778}
779
c906108c 780\f
48cb2d85
VP
781/* Return the breakpoint with the specified number, or NULL
782 if the number does not refer to an existing breakpoint. */
783
784struct breakpoint *
785get_breakpoint (int num)
786{
787 struct breakpoint *b;
788
789 ALL_BREAKPOINTS (b)
790 if (b->number == num)
791 return b;
792
793 return NULL;
794}
5c44784c 795
c906108c 796\f
adc36818 797
b775012e
LM
798/* Mark locations as "conditions have changed" in case the target supports
799 evaluating conditions on its side. */
800
801static void
802mark_breakpoint_modified (struct breakpoint *b)
803{
804 struct bp_location *loc;
805
806 /* This is only meaningful if the target is
807 evaluating conditions and if the user has
808 opted for condition evaluation on the target's
809 side. */
810 if (gdb_evaluates_breakpoint_condition_p ()
811 || !target_supports_evaluation_of_breakpoint_conditions ())
812 return;
813
814 if (!is_breakpoint (b))
815 return;
816
817 for (loc = b->loc; loc; loc = loc->next)
818 loc->condition_changed = condition_modified;
819}
820
821/* Mark location as "conditions have changed" in case the target supports
822 evaluating conditions on its side. */
823
824static void
825mark_breakpoint_location_modified (struct bp_location *loc)
826{
827 /* This is only meaningful if the target is
828 evaluating conditions and if the user has
829 opted for condition evaluation on the target's
830 side. */
831 if (gdb_evaluates_breakpoint_condition_p ()
832 || !target_supports_evaluation_of_breakpoint_conditions ())
833
834 return;
835
836 if (!is_breakpoint (loc->owner))
837 return;
838
839 loc->condition_changed = condition_modified;
840}
841
842/* Sets the condition-evaluation mode using the static global
843 condition_evaluation_mode. */
844
845static void
846set_condition_evaluation_mode (char *args, int from_tty,
847 struct cmd_list_element *c)
848{
b775012e
LM
849 const char *old_mode, *new_mode;
850
851 if ((condition_evaluation_mode_1 == condition_evaluation_target)
852 && !target_supports_evaluation_of_breakpoint_conditions ())
853 {
854 condition_evaluation_mode_1 = condition_evaluation_mode;
855 warning (_("Target does not support breakpoint condition evaluation.\n"
856 "Using host evaluation mode instead."));
857 return;
858 }
859
860 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
861 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
862
abf1152a
JK
863 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
864 settings was "auto". */
865 condition_evaluation_mode = condition_evaluation_mode_1;
866
b775012e
LM
867 /* Only update the mode if the user picked a different one. */
868 if (new_mode != old_mode)
869 {
870 struct bp_location *loc, **loc_tmp;
871 /* If the user switched to a different evaluation mode, we
872 need to synch the changes with the target as follows:
873
874 "host" -> "target": Send all (valid) conditions to the target.
875 "target" -> "host": Remove all the conditions from the target.
876 */
877
b775012e
LM
878 if (new_mode == condition_evaluation_target)
879 {
880 /* Mark everything modified and synch conditions with the
881 target. */
882 ALL_BP_LOCATIONS (loc, loc_tmp)
883 mark_breakpoint_location_modified (loc);
884 }
885 else
886 {
887 /* Manually mark non-duplicate locations to synch conditions
888 with the target. We do this to remove all the conditions the
889 target knows about. */
890 ALL_BP_LOCATIONS (loc, loc_tmp)
891 if (is_breakpoint (loc->owner) && loc->inserted)
892 loc->needs_update = 1;
893 }
894
895 /* Do the update. */
44702360 896 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
897 }
898
899 return;
900}
901
902/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
903 what "auto" is translating to. */
904
905static void
906show_condition_evaluation_mode (struct ui_file *file, int from_tty,
907 struct cmd_list_element *c, const char *value)
908{
909 if (condition_evaluation_mode == condition_evaluation_auto)
910 fprintf_filtered (file,
911 _("Breakpoint condition evaluation "
912 "mode is %s (currently %s).\n"),
913 value,
914 breakpoint_condition_evaluation_mode ());
915 else
916 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
917 value);
918}
919
920/* A comparison function for bp_location AP and BP that is used by
921 bsearch. This comparison function only cares about addresses, unlike
922 the more general bp_location_compare function. */
923
924static int
925bp_location_compare_addrs (const void *ap, const void *bp)
926{
927 struct bp_location *a = *(void **) ap;
928 struct bp_location *b = *(void **) bp;
929
930 if (a->address == b->address)
931 return 0;
932 else
933 return ((a->address > b->address) - (a->address < b->address));
934}
935
936/* Helper function to skip all bp_locations with addresses
937 less than ADDRESS. It returns the first bp_location that
938 is greater than or equal to ADDRESS. If none is found, just
939 return NULL. */
940
941static struct bp_location **
942get_first_locp_gte_addr (CORE_ADDR address)
943{
944 struct bp_location dummy_loc;
945 struct bp_location *dummy_locp = &dummy_loc;
946 struct bp_location **locp_found = NULL;
947
948 /* Initialize the dummy location's address field. */
949 memset (&dummy_loc, 0, sizeof (struct bp_location));
950 dummy_loc.address = address;
951
952 /* Find a close match to the first location at ADDRESS. */
953 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
954 sizeof (struct bp_location **),
955 bp_location_compare_addrs);
956
957 /* Nothing was found, nothing left to do. */
958 if (locp_found == NULL)
959 return NULL;
960
961 /* We may have found a location that is at ADDRESS but is not the first in the
962 location's list. Go backwards (if possible) and locate the first one. */
963 while ((locp_found - 1) >= bp_location
964 && (*(locp_found - 1))->address == address)
965 locp_found--;
966
967 return locp_found;
968}
969
adc36818 970void
7a26bd4d 971set_breakpoint_condition (struct breakpoint *b, const char *exp,
adc36818
PM
972 int from_tty)
973{
3a5c3e22
PA
974 xfree (b->cond_string);
975 b->cond_string = NULL;
adc36818 976
3a5c3e22 977 if (is_watchpoint (b))
adc36818 978 {
3a5c3e22
PA
979 struct watchpoint *w = (struct watchpoint *) b;
980
981 xfree (w->cond_exp);
982 w->cond_exp = NULL;
983 }
984 else
985 {
986 struct bp_location *loc;
987
988 for (loc = b->loc; loc; loc = loc->next)
989 {
990 xfree (loc->cond);
991 loc->cond = NULL;
b775012e
LM
992
993 /* No need to free the condition agent expression
994 bytecode (if we have one). We will handle this
995 when we go through update_global_location_list. */
3a5c3e22 996 }
adc36818 997 }
adc36818
PM
998
999 if (*exp == 0)
1000 {
1001 if (from_tty)
1002 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1003 }
1004 else
1005 {
bbc13ae3 1006 const char *arg = exp;
cc59ec59 1007
adc36818
PM
1008 /* I don't know if it matters whether this is the string the user
1009 typed in or the decompiled expression. */
1010 b->cond_string = xstrdup (arg);
1011 b->condition_not_parsed = 0;
1012
1013 if (is_watchpoint (b))
1014 {
3a5c3e22
PA
1015 struct watchpoint *w = (struct watchpoint *) b;
1016
adc36818
PM
1017 innermost_block = NULL;
1018 arg = exp;
1bb9788d 1019 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
1020 if (*arg)
1021 error (_("Junk at end of expression"));
3a5c3e22 1022 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1023 }
1024 else
1025 {
3a5c3e22
PA
1026 struct bp_location *loc;
1027
adc36818
PM
1028 for (loc = b->loc; loc; loc = loc->next)
1029 {
1030 arg = exp;
1031 loc->cond =
1bb9788d
TT
1032 parse_exp_1 (&arg, loc->address,
1033 block_for_pc (loc->address), 0);
adc36818
PM
1034 if (*arg)
1035 error (_("Junk at end of expression"));
1036 }
1037 }
1038 }
b775012e
LM
1039 mark_breakpoint_modified (b);
1040
8d3788bd 1041 observer_notify_breakpoint_modified (b);
adc36818
PM
1042}
1043
d55637df
TT
1044/* Completion for the "condition" command. */
1045
1046static VEC (char_ptr) *
6f937416
PA
1047condition_completer (struct cmd_list_element *cmd,
1048 const char *text, const char *word)
d55637df 1049{
6f937416 1050 const char *space;
d55637df 1051
6f937416
PA
1052 text = skip_spaces_const (text);
1053 space = skip_to_space_const (text);
d55637df
TT
1054 if (*space == '\0')
1055 {
1056 int len;
1057 struct breakpoint *b;
1058 VEC (char_ptr) *result = NULL;
1059
1060 if (text[0] == '$')
1061 {
1062 /* We don't support completion of history indices. */
1063 if (isdigit (text[1]))
1064 return NULL;
1065 return complete_internalvar (&text[1]);
1066 }
1067
1068 /* We're completing the breakpoint number. */
1069 len = strlen (text);
1070
1071 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1072 {
1073 char number[50];
1074
1075 xsnprintf (number, sizeof (number), "%d", b->number);
1076
1077 if (strncmp (number, text, len) == 0)
1078 VEC_safe_push (char_ptr, result, xstrdup (number));
1079 }
d55637df
TT
1080
1081 return result;
1082 }
1083
1084 /* We're completing the expression part. */
6f937416 1085 text = skip_spaces_const (space);
d55637df
TT
1086 return expression_completer (cmd, text, word);
1087}
1088
c906108c
SS
1089/* condition N EXP -- set break condition of breakpoint N to EXP. */
1090
1091static void
fba45db2 1092condition_command (char *arg, int from_tty)
c906108c 1093{
52f0bd74 1094 struct breakpoint *b;
c906108c 1095 char *p;
52f0bd74 1096 int bnum;
c906108c
SS
1097
1098 if (arg == 0)
e2e0b3e5 1099 error_no_arg (_("breakpoint number"));
c906108c
SS
1100
1101 p = arg;
1102 bnum = get_number (&p);
5c44784c 1103 if (bnum == 0)
8a3fe4f8 1104 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1105
1106 ALL_BREAKPOINTS (b)
1107 if (b->number == bnum)
2f069f6f 1108 {
6dddc817
DE
1109 /* Check if this breakpoint has a "stop" method implemented in an
1110 extension language. This method and conditions entered into GDB
1111 from the CLI are mutually exclusive. */
1112 const struct extension_language_defn *extlang
1113 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1114
1115 if (extlang != NULL)
1116 {
1117 error (_("Only one stop condition allowed. There is currently"
1118 " a %s stop condition defined for this breakpoint."),
1119 ext_lang_capitalized_name (extlang));
1120 }
2566ad2d 1121 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1122
1123 if (is_breakpoint (b))
44702360 1124 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1125
2f069f6f
JB
1126 return;
1127 }
c906108c 1128
8a3fe4f8 1129 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1130}
1131
a7bdde9e
VP
1132/* Check that COMMAND do not contain commands that are suitable
1133 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1134 Throw if any such commands is found. */
1135
a7bdde9e
VP
1136static void
1137check_no_tracepoint_commands (struct command_line *commands)
1138{
1139 struct command_line *c;
cc59ec59 1140
a7bdde9e
VP
1141 for (c = commands; c; c = c->next)
1142 {
1143 int i;
1144
1145 if (c->control_type == while_stepping_control)
3e43a32a
MS
1146 error (_("The 'while-stepping' command can "
1147 "only be used for tracepoints"));
a7bdde9e
VP
1148
1149 for (i = 0; i < c->body_count; ++i)
1150 check_no_tracepoint_commands ((c->body_list)[i]);
1151
1152 /* Not that command parsing removes leading whitespace and comment
4a64f543 1153 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1154 command directly. */
1155 if (strstr (c->line, "collect ") == c->line)
1156 error (_("The 'collect' command can only be used for tracepoints"));
1157
51661e93
VP
1158 if (strstr (c->line, "teval ") == c->line)
1159 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1160 }
1161}
1162
d77f58be
SS
1163/* Encapsulate tests for different types of tracepoints. */
1164
d9b3f62e
PA
1165static int
1166is_tracepoint_type (enum bptype type)
1167{
1168 return (type == bp_tracepoint
1169 || type == bp_fast_tracepoint
1170 || type == bp_static_tracepoint);
1171}
1172
a7bdde9e 1173int
d77f58be 1174is_tracepoint (const struct breakpoint *b)
a7bdde9e 1175{
d9b3f62e 1176 return is_tracepoint_type (b->type);
a7bdde9e 1177}
d9b3f62e 1178
e5dd4106 1179/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1180 breakpoint. This function will throw an exception if a problem is
1181 found. */
48cb2d85 1182
95a42b64
TT
1183static void
1184validate_commands_for_breakpoint (struct breakpoint *b,
1185 struct command_line *commands)
48cb2d85 1186{
d77f58be 1187 if (is_tracepoint (b))
a7bdde9e 1188 {
c9a6ce02 1189 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1190 struct command_line *c;
1191 struct command_line *while_stepping = 0;
c9a6ce02
PA
1192
1193 /* Reset the while-stepping step count. The previous commands
1194 might have included a while-stepping action, while the new
1195 ones might not. */
1196 t->step_count = 0;
1197
1198 /* We need to verify that each top-level element of commands is
1199 valid for tracepoints, that there's at most one
1200 while-stepping element, and that the while-stepping's body
1201 has valid tracing commands excluding nested while-stepping.
1202 We also need to validate the tracepoint action line in the
1203 context of the tracepoint --- validate_actionline actually
1204 has side effects, like setting the tracepoint's
1205 while-stepping STEP_COUNT, in addition to checking if the
1206 collect/teval actions parse and make sense in the
1207 tracepoint's context. */
a7bdde9e
VP
1208 for (c = commands; c; c = c->next)
1209 {
a7bdde9e
VP
1210 if (c->control_type == while_stepping_control)
1211 {
1212 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1213 error (_("The 'while-stepping' command "
1214 "cannot be used for fast tracepoint"));
0fb4aa4b 1215 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1216 error (_("The 'while-stepping' command "
1217 "cannot be used for static tracepoint"));
a7bdde9e
VP
1218
1219 if (while_stepping)
3e43a32a
MS
1220 error (_("The 'while-stepping' command "
1221 "can be used only once"));
a7bdde9e
VP
1222 else
1223 while_stepping = c;
1224 }
c9a6ce02
PA
1225
1226 validate_actionline (c->line, b);
a7bdde9e
VP
1227 }
1228 if (while_stepping)
1229 {
1230 struct command_line *c2;
1231
1232 gdb_assert (while_stepping->body_count == 1);
1233 c2 = while_stepping->body_list[0];
1234 for (; c2; c2 = c2->next)
1235 {
a7bdde9e
VP
1236 if (c2->control_type == while_stepping_control)
1237 error (_("The 'while-stepping' command cannot be nested"));
1238 }
1239 }
1240 }
1241 else
1242 {
1243 check_no_tracepoint_commands (commands);
1244 }
95a42b64
TT
1245}
1246
0fb4aa4b
PA
1247/* Return a vector of all the static tracepoints set at ADDR. The
1248 caller is responsible for releasing the vector. */
1249
1250VEC(breakpoint_p) *
1251static_tracepoints_here (CORE_ADDR addr)
1252{
1253 struct breakpoint *b;
1254 VEC(breakpoint_p) *found = 0;
1255 struct bp_location *loc;
1256
1257 ALL_BREAKPOINTS (b)
1258 if (b->type == bp_static_tracepoint)
1259 {
1260 for (loc = b->loc; loc; loc = loc->next)
1261 if (loc->address == addr)
1262 VEC_safe_push(breakpoint_p, found, b);
1263 }
1264
1265 return found;
1266}
1267
95a42b64 1268/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1269 validate that only allowed commands are included. */
95a42b64
TT
1270
1271void
4a64f543
MS
1272breakpoint_set_commands (struct breakpoint *b,
1273 struct command_line *commands)
95a42b64
TT
1274{
1275 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1276
9add0f1b
TT
1277 decref_counted_command_line (&b->commands);
1278 b->commands = alloc_counted_command_line (commands);
8d3788bd 1279 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1280}
1281
45a43567
TT
1282/* Set the internal `silent' flag on the breakpoint. Note that this
1283 is not the same as the "silent" that may appear in the breakpoint's
1284 commands. */
1285
1286void
1287breakpoint_set_silent (struct breakpoint *b, int silent)
1288{
1289 int old_silent = b->silent;
1290
1291 b->silent = silent;
1292 if (old_silent != silent)
8d3788bd 1293 observer_notify_breakpoint_modified (b);
45a43567
TT
1294}
1295
1296/* Set the thread for this breakpoint. If THREAD is -1, make the
1297 breakpoint work for any thread. */
1298
1299void
1300breakpoint_set_thread (struct breakpoint *b, int thread)
1301{
1302 int old_thread = b->thread;
1303
1304 b->thread = thread;
1305 if (old_thread != thread)
8d3788bd 1306 observer_notify_breakpoint_modified (b);
45a43567
TT
1307}
1308
1309/* Set the task for this breakpoint. If TASK is 0, make the
1310 breakpoint work for any task. */
1311
1312void
1313breakpoint_set_task (struct breakpoint *b, int task)
1314{
1315 int old_task = b->task;
1316
1317 b->task = task;
1318 if (old_task != task)
8d3788bd 1319 observer_notify_breakpoint_modified (b);
45a43567
TT
1320}
1321
95a42b64
TT
1322void
1323check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1324{
1325 struct breakpoint *b = closure;
cc59ec59 1326
6f937416 1327 validate_actionline (line, b);
a7bdde9e
VP
1328}
1329
95a42b64
TT
1330/* A structure used to pass information through
1331 map_breakpoint_numbers. */
1332
1333struct commands_info
1334{
1335 /* True if the command was typed at a tty. */
1336 int from_tty;
86b17b60
PA
1337
1338 /* The breakpoint range spec. */
1339 char *arg;
1340
95a42b64
TT
1341 /* Non-NULL if the body of the commands are being read from this
1342 already-parsed command. */
1343 struct command_line *control;
86b17b60 1344
95a42b64
TT
1345 /* The command lines read from the user, or NULL if they have not
1346 yet been read. */
1347 struct counted_command_line *cmd;
1348};
1349
1350/* A callback for map_breakpoint_numbers that sets the commands for
1351 commands_command. */
1352
c906108c 1353static void
95a42b64 1354do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1355{
95a42b64 1356 struct commands_info *info = data;
c906108c 1357
95a42b64
TT
1358 if (info->cmd == NULL)
1359 {
1360 struct command_line *l;
5c44784c 1361
95a42b64
TT
1362 if (info->control != NULL)
1363 l = copy_command_lines (info->control->body_list[0]);
1364 else
86b17b60
PA
1365 {
1366 struct cleanup *old_chain;
1367 char *str;
c5aa993b 1368
3e43a32a
MS
1369 str = xstrprintf (_("Type commands for breakpoint(s) "
1370 "%s, one per line."),
86b17b60
PA
1371 info->arg);
1372
1373 old_chain = make_cleanup (xfree, str);
1374
1375 l = read_command_lines (str,
1376 info->from_tty, 1,
d77f58be 1377 (is_tracepoint (b)
86b17b60
PA
1378 ? check_tracepoint_command : 0),
1379 b);
1380
1381 do_cleanups (old_chain);
1382 }
a7bdde9e 1383
95a42b64
TT
1384 info->cmd = alloc_counted_command_line (l);
1385 }
1386
1387 /* If a breakpoint was on the list more than once, we don't need to
1388 do anything. */
1389 if (b->commands != info->cmd)
1390 {
1391 validate_commands_for_breakpoint (b, info->cmd->commands);
1392 incref_counted_command_line (info->cmd);
1393 decref_counted_command_line (&b->commands);
1394 b->commands = info->cmd;
8d3788bd 1395 observer_notify_breakpoint_modified (b);
c5aa993b 1396 }
95a42b64
TT
1397}
1398
1399static void
4a64f543
MS
1400commands_command_1 (char *arg, int from_tty,
1401 struct command_line *control)
95a42b64
TT
1402{
1403 struct cleanup *cleanups;
1404 struct commands_info info;
1405
1406 info.from_tty = from_tty;
1407 info.control = control;
1408 info.cmd = NULL;
1409 /* If we read command lines from the user, then `info' will hold an
1410 extra reference to the commands that we must clean up. */
1411 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1412
1413 if (arg == NULL || !*arg)
1414 {
86b17b60 1415 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1416 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1417 breakpoint_count);
95a42b64
TT
1418 else if (breakpoint_count > 0)
1419 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1420 else
1421 {
1422 /* So that we don't try to free the incoming non-NULL
1423 argument in the cleanup below. Mapping breakpoint
1424 numbers will fail in this case. */
1425 arg = NULL;
1426 }
95a42b64 1427 }
9766ced4
SS
1428 else
1429 /* The command loop has some static state, so we need to preserve
1430 our argument. */
1431 arg = xstrdup (arg);
86b17b60
PA
1432
1433 if (arg != NULL)
1434 make_cleanup (xfree, arg);
1435
1436 info.arg = arg;
95a42b64
TT
1437
1438 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1439
1440 if (info.cmd == NULL)
1441 error (_("No breakpoints specified."));
1442
1443 do_cleanups (cleanups);
1444}
1445
1446static void
1447commands_command (char *arg, int from_tty)
1448{
1449 commands_command_1 (arg, from_tty, NULL);
c906108c 1450}
40c03ae8
EZ
1451
1452/* Like commands_command, but instead of reading the commands from
1453 input stream, takes them from an already parsed command structure.
1454
1455 This is used by cli-script.c to DTRT with breakpoint commands
1456 that are part of if and while bodies. */
1457enum command_control_type
1458commands_from_control_command (char *arg, struct command_line *cmd)
1459{
95a42b64
TT
1460 commands_command_1 (arg, 0, cmd);
1461 return simple_control;
40c03ae8 1462}
876fa593
JK
1463
1464/* Return non-zero if BL->TARGET_INFO contains valid information. */
1465
1466static int
1467bp_location_has_shadow (struct bp_location *bl)
1468{
1469 if (bl->loc_type != bp_loc_software_breakpoint)
1470 return 0;
1471 if (!bl->inserted)
1472 return 0;
1473 if (bl->target_info.shadow_len == 0)
e5dd4106 1474 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1475 return 0;
1476 return 1;
1477}
1478
9d497a19
PA
1479/* Update BUF, which is LEN bytes read from the target address
1480 MEMADDR, by replacing a memory breakpoint with its shadowed
1481 contents.
1482
1483 If READBUF is not NULL, this buffer must not overlap with the of
1484 the breakpoint location's shadow_contents buffer. Otherwise, a
1485 failed assertion internal error will be raised. */
1486
1487static void
1488one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1489 const gdb_byte *writebuf_org,
1490 ULONGEST memaddr, LONGEST len,
1491 struct bp_target_info *target_info,
1492 struct gdbarch *gdbarch)
1493{
1494 /* Now do full processing of the found relevant range of elements. */
1495 CORE_ADDR bp_addr = 0;
1496 int bp_size = 0;
1497 int bptoffset = 0;
1498
1499 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1500 current_program_space->aspace, 0))
1501 {
1502 /* The breakpoint is inserted in a different address space. */
1503 return;
1504 }
1505
1506 /* Addresses and length of the part of the breakpoint that
1507 we need to copy. */
1508 bp_addr = target_info->placed_address;
1509 bp_size = target_info->shadow_len;
1510
1511 if (bp_addr + bp_size <= memaddr)
1512 {
1513 /* The breakpoint is entirely before the chunk of memory we are
1514 reading. */
1515 return;
1516 }
1517
1518 if (bp_addr >= memaddr + len)
1519 {
1520 /* The breakpoint is entirely after the chunk of memory we are
1521 reading. */
1522 return;
1523 }
1524
1525 /* Offset within shadow_contents. */
1526 if (bp_addr < memaddr)
1527 {
1528 /* Only copy the second part of the breakpoint. */
1529 bp_size -= memaddr - bp_addr;
1530 bptoffset = memaddr - bp_addr;
1531 bp_addr = memaddr;
1532 }
1533
1534 if (bp_addr + bp_size > memaddr + len)
1535 {
1536 /* Only copy the first part of the breakpoint. */
1537 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1538 }
1539
1540 if (readbuf != NULL)
1541 {
1542 /* Verify that the readbuf buffer does not overlap with the
1543 shadow_contents buffer. */
1544 gdb_assert (target_info->shadow_contents >= readbuf + len
1545 || readbuf >= (target_info->shadow_contents
1546 + target_info->shadow_len));
1547
1548 /* Update the read buffer with this inserted breakpoint's
1549 shadow. */
1550 memcpy (readbuf + bp_addr - memaddr,
1551 target_info->shadow_contents + bptoffset, bp_size);
1552 }
1553 else
1554 {
1555 const unsigned char *bp;
0d5ed153
MR
1556 CORE_ADDR addr = target_info->reqstd_address;
1557 int placed_size;
9d497a19
PA
1558
1559 /* Update the shadow with what we want to write to memory. */
1560 memcpy (target_info->shadow_contents + bptoffset,
1561 writebuf_org + bp_addr - memaddr, bp_size);
1562
1563 /* Determine appropriate breakpoint contents and size for this
1564 address. */
0d5ed153 1565 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1566
1567 /* Update the final write buffer with this inserted
1568 breakpoint's INSN. */
1569 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1570 }
1571}
1572
8defab1a 1573/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1574 by replacing any memory breakpoints with their shadowed contents.
1575
35c63cd8
JB
1576 If READBUF is not NULL, this buffer must not overlap with any of
1577 the breakpoint location's shadow_contents buffers. Otherwise,
1578 a failed assertion internal error will be raised.
1579
876fa593 1580 The range of shadowed area by each bp_location is:
35df4500
TJB
1581 bl->address - bp_location_placed_address_before_address_max
1582 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1583 The range we were requested to resolve shadows for is:
1584 memaddr ... memaddr + len
1585 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1586 memaddr + len <= (bl->address
1587 - bp_location_placed_address_before_address_max)
876fa593 1588 and:
35df4500 1589 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1590
8defab1a 1591void
f0ba3972
PA
1592breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1593 const gdb_byte *writebuf_org,
1594 ULONGEST memaddr, LONGEST len)
c906108c 1595{
4a64f543
MS
1596 /* Left boundary, right boundary and median element of our binary
1597 search. */
876fa593 1598 unsigned bc_l, bc_r, bc;
9d497a19 1599 size_t i;
876fa593 1600
4a64f543
MS
1601 /* Find BC_L which is a leftmost element which may affect BUF
1602 content. It is safe to report lower value but a failure to
1603 report higher one. */
876fa593
JK
1604
1605 bc_l = 0;
1606 bc_r = bp_location_count;
1607 while (bc_l + 1 < bc_r)
1608 {
35df4500 1609 struct bp_location *bl;
876fa593
JK
1610
1611 bc = (bc_l + bc_r) / 2;
35df4500 1612 bl = bp_location[bc];
876fa593 1613
4a64f543
MS
1614 /* Check first BL->ADDRESS will not overflow due to the added
1615 constant. Then advance the left boundary only if we are sure
1616 the BC element can in no way affect the BUF content (MEMADDR
1617 to MEMADDR + LEN range).
876fa593 1618
4a64f543
MS
1619 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1620 offset so that we cannot miss a breakpoint with its shadow
1621 range tail still reaching MEMADDR. */
c5aa993b 1622
35df4500
TJB
1623 if ((bl->address + bp_location_shadow_len_after_address_max
1624 >= bl->address)
1625 && (bl->address + bp_location_shadow_len_after_address_max
1626 <= memaddr))
876fa593
JK
1627 bc_l = bc;
1628 else
1629 bc_r = bc;
1630 }
1631
128070bb
PA
1632 /* Due to the binary search above, we need to make sure we pick the
1633 first location that's at BC_L's address. E.g., if there are
1634 multiple locations at the same address, BC_L may end up pointing
1635 at a duplicate location, and miss the "master"/"inserted"
1636 location. Say, given locations L1, L2 and L3 at addresses A and
1637 B:
1638
1639 L1@A, L2@A, L3@B, ...
1640
1641 BC_L could end up pointing at location L2, while the "master"
1642 location could be L1. Since the `loc->inserted' flag is only set
1643 on "master" locations, we'd forget to restore the shadow of L1
1644 and L2. */
1645 while (bc_l > 0
1646 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1647 bc_l--;
1648
876fa593
JK
1649 /* Now do full processing of the found relevant range of elements. */
1650
1651 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1652 {
35df4500 1653 struct bp_location *bl = bp_location[bc];
876fa593
JK
1654 CORE_ADDR bp_addr = 0;
1655 int bp_size = 0;
1656 int bptoffset = 0;
1657
35df4500
TJB
1658 /* bp_location array has BL->OWNER always non-NULL. */
1659 if (bl->owner->type == bp_none)
8a3fe4f8 1660 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1661 bl->owner->number);
ffce0d52 1662
e5dd4106 1663 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1664 content. */
1665
35df4500
TJB
1666 if (bl->address >= bp_location_placed_address_before_address_max
1667 && memaddr + len <= (bl->address
1668 - bp_location_placed_address_before_address_max))
876fa593
JK
1669 break;
1670
35df4500 1671 if (!bp_location_has_shadow (bl))
c5aa993b 1672 continue;
6c95b8df 1673
9d497a19
PA
1674 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1675 memaddr, len, &bl->target_info, bl->gdbarch);
1676 }
c906108c 1677}
9d497a19 1678
c906108c 1679\f
c5aa993b 1680
b775012e
LM
1681/* Return true if BPT is either a software breakpoint or a hardware
1682 breakpoint. */
1683
1684int
1685is_breakpoint (const struct breakpoint *bpt)
1686{
1687 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1688 || bpt->type == bp_hardware_breakpoint
1689 || bpt->type == bp_dprintf);
b775012e
LM
1690}
1691
60e1c644
PA
1692/* Return true if BPT is of any hardware watchpoint kind. */
1693
a5606eee 1694static int
d77f58be 1695is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1696{
1697 return (bpt->type == bp_hardware_watchpoint
1698 || bpt->type == bp_read_watchpoint
1699 || bpt->type == bp_access_watchpoint);
1700}
7270d8f2 1701
60e1c644
PA
1702/* Return true if BPT is of any watchpoint kind, hardware or
1703 software. */
1704
3a5c3e22 1705int
d77f58be 1706is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1707{
1708 return (is_hardware_watchpoint (bpt)
1709 || bpt->type == bp_watchpoint);
1710}
1711
3a5c3e22
PA
1712/* Returns true if the current thread and its running state are safe
1713 to evaluate or update watchpoint B. Watchpoints on local
1714 expressions need to be evaluated in the context of the thread that
1715 was current when the watchpoint was created, and, that thread needs
1716 to be stopped to be able to select the correct frame context.
1717 Watchpoints on global expressions can be evaluated on any thread,
1718 and in any state. It is presently left to the target allowing
1719 memory accesses when threads are running. */
f6bc2008
PA
1720
1721static int
3a5c3e22 1722watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1723{
d0d8b0c6
JK
1724 return (b->base.pspace == current_program_space
1725 && (ptid_equal (b->watchpoint_thread, null_ptid)
1726 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1727 && !is_executing (inferior_ptid))));
f6bc2008
PA
1728}
1729
d0fb5eae
JK
1730/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1731 associated bp_watchpoint_scope breakpoint. */
1732
1733static void
3a5c3e22 1734watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1735{
3a5c3e22 1736 struct breakpoint *b = &w->base;
d0fb5eae
JK
1737
1738 if (b->related_breakpoint != b)
1739 {
1740 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1741 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1742 b->related_breakpoint->disposition = disp_del_at_next_stop;
1743 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1744 b->related_breakpoint = b;
1745 }
1746 b->disposition = disp_del_at_next_stop;
1747}
1748
bb9d5f81
PP
1749/* Extract a bitfield value from value VAL using the bit parameters contained in
1750 watchpoint W. */
1751
1752static struct value *
1753extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1754{
1755 struct value *bit_val;
1756
1757 if (val == NULL)
1758 return NULL;
1759
1760 bit_val = allocate_value (value_type (val));
1761
1762 unpack_value_bitfield (bit_val,
1763 w->val_bitpos,
1764 w->val_bitsize,
1765 value_contents_for_printing (val),
1766 value_offset (val),
1767 val);
1768
1769 return bit_val;
1770}
1771
567e1b4e
JB
1772/* Assuming that B is a watchpoint:
1773 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1774 - Evaluate expression and store the result in B->val
567e1b4e
JB
1775 - Evaluate the condition if there is one, and store the result
1776 in b->loc->cond.
a5606eee
VP
1777 - Update the list of values that must be watched in B->loc.
1778
4a64f543
MS
1779 If the watchpoint disposition is disp_del_at_next_stop, then do
1780 nothing. If this is local watchpoint that is out of scope, delete
1781 it.
1782
1783 Even with `set breakpoint always-inserted on' the watchpoints are
1784 removed + inserted on each stop here. Normal breakpoints must
1785 never be removed because they might be missed by a running thread
1786 when debugging in non-stop mode. On the other hand, hardware
1787 watchpoints (is_hardware_watchpoint; processed here) are specific
1788 to each LWP since they are stored in each LWP's hardware debug
1789 registers. Therefore, such LWP must be stopped first in order to
1790 be able to modify its hardware watchpoints.
1791
1792 Hardware watchpoints must be reset exactly once after being
1793 presented to the user. It cannot be done sooner, because it would
1794 reset the data used to present the watchpoint hit to the user. And
1795 it must not be done later because it could display the same single
1796 watchpoint hit during multiple GDB stops. Note that the latter is
1797 relevant only to the hardware watchpoint types bp_read_watchpoint
1798 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1799 not user-visible - its hit is suppressed if the memory content has
1800 not changed.
1801
1802 The following constraints influence the location where we can reset
1803 hardware watchpoints:
1804
1805 * target_stopped_by_watchpoint and target_stopped_data_address are
1806 called several times when GDB stops.
1807
1808 [linux]
1809 * Multiple hardware watchpoints can be hit at the same time,
1810 causing GDB to stop. GDB only presents one hardware watchpoint
1811 hit at a time as the reason for stopping, and all the other hits
1812 are presented later, one after the other, each time the user
1813 requests the execution to be resumed. Execution is not resumed
1814 for the threads still having pending hit event stored in
1815 LWP_INFO->STATUS. While the watchpoint is already removed from
1816 the inferior on the first stop the thread hit event is kept being
1817 reported from its cached value by linux_nat_stopped_data_address
1818 until the real thread resume happens after the watchpoint gets
1819 presented and thus its LWP_INFO->STATUS gets reset.
1820
1821 Therefore the hardware watchpoint hit can get safely reset on the
1822 watchpoint removal from inferior. */
a79d3c27 1823
b40ce68a 1824static void
3a5c3e22 1825update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1826{
a5606eee 1827 int within_current_scope;
a5606eee 1828 struct frame_id saved_frame_id;
66076460 1829 int frame_saved;
a5606eee 1830
f6bc2008
PA
1831 /* If this is a local watchpoint, we only want to check if the
1832 watchpoint frame is in scope if the current thread is the thread
1833 that was used to create the watchpoint. */
1834 if (!watchpoint_in_thread_scope (b))
1835 return;
1836
3a5c3e22 1837 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1838 return;
1839
66076460 1840 frame_saved = 0;
a5606eee
VP
1841
1842 /* Determine if the watchpoint is within scope. */
1843 if (b->exp_valid_block == NULL)
1844 within_current_scope = 1;
1845 else
1846 {
b5db5dfc
UW
1847 struct frame_info *fi = get_current_frame ();
1848 struct gdbarch *frame_arch = get_frame_arch (fi);
1849 CORE_ADDR frame_pc = get_frame_pc (fi);
1850
c9cf6e20
MG
1851 /* If we're at a point where the stack has been destroyed
1852 (e.g. in a function epilogue), unwinding may not work
1853 properly. Do not attempt to recreate locations at this
b5db5dfc 1854 point. See similar comments in watchpoint_check. */
c9cf6e20 1855 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
b5db5dfc 1856 return;
66076460
DJ
1857
1858 /* Save the current frame's ID so we can restore it after
1859 evaluating the watchpoint expression on its own frame. */
1860 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1861 took a frame parameter, so that we didn't have to change the
1862 selected frame. */
1863 frame_saved = 1;
1864 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1865
a5606eee
VP
1866 fi = frame_find_by_id (b->watchpoint_frame);
1867 within_current_scope = (fi != NULL);
1868 if (within_current_scope)
1869 select_frame (fi);
1870 }
1871
b5db5dfc
UW
1872 /* We don't free locations. They are stored in the bp_location array
1873 and update_global_location_list will eventually delete them and
1874 remove breakpoints if needed. */
3a5c3e22 1875 b->base.loc = NULL;
b5db5dfc 1876
a5606eee
VP
1877 if (within_current_scope && reparse)
1878 {
bbc13ae3 1879 const char *s;
d63d0675 1880
a5606eee
VP
1881 if (b->exp)
1882 {
1883 xfree (b->exp);
1884 b->exp = NULL;
1885 }
d63d0675 1886 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1887 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1888 /* If the meaning of expression itself changed, the old value is
1889 no longer relevant. We don't want to report a watchpoint hit
1890 to the user when the old value and the new value may actually
1891 be completely different objects. */
1892 value_free (b->val);
fa4727a6
DJ
1893 b->val = NULL;
1894 b->val_valid = 0;
60e1c644
PA
1895
1896 /* Note that unlike with breakpoints, the watchpoint's condition
1897 expression is stored in the breakpoint object, not in the
1898 locations (re)created below. */
3a5c3e22 1899 if (b->base.cond_string != NULL)
60e1c644
PA
1900 {
1901 if (b->cond_exp != NULL)
1902 {
1903 xfree (b->cond_exp);
1904 b->cond_exp = NULL;
1905 }
1906
3a5c3e22 1907 s = b->base.cond_string;
1bb9788d 1908 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1909 }
a5606eee 1910 }
a5606eee
VP
1911
1912 /* If we failed to parse the expression, for example because
1913 it refers to a global variable in a not-yet-loaded shared library,
1914 don't try to insert watchpoint. We don't automatically delete
1915 such watchpoint, though, since failure to parse expression
1916 is different from out-of-scope watchpoint. */
e8369a73 1917 if (!target_has_execution)
2d134ed3
PA
1918 {
1919 /* Without execution, memory can't change. No use to try and
1920 set watchpoint locations. The watchpoint will be reset when
1921 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1922 if (!can_use_hw_watchpoints)
1923 {
1924 if (b->base.ops->works_in_software_mode (&b->base))
1925 b->base.type = bp_watchpoint;
1926 else
638aa5a1
AB
1927 error (_("Can't set read/access watchpoint when "
1928 "hardware watchpoints are disabled."));
e8369a73 1929 }
2d134ed3
PA
1930 }
1931 else if (within_current_scope && b->exp)
a5606eee 1932 {
0cf6dd15 1933 int pc = 0;
fa4727a6 1934 struct value *val_chain, *v, *result, *next;
2d134ed3 1935 struct program_space *frame_pspace;
a5606eee 1936
3a1115a0 1937 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1938
a5606eee
VP
1939 /* Avoid setting b->val if it's already set. The meaning of
1940 b->val is 'the last value' user saw, and we should update
1941 it only if we reported that last value to user. As it
9c06b0b4
TJB
1942 happens, the code that reports it updates b->val directly.
1943 We don't keep track of the memory value for masked
1944 watchpoints. */
3a5c3e22 1945 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1946 {
bb9d5f81
PP
1947 if (b->val_bitsize != 0)
1948 {
1949 v = extract_bitfield_from_watchpoint_value (b, v);
1950 if (v != NULL)
1951 release_value (v);
1952 }
fa4727a6
DJ
1953 b->val = v;
1954 b->val_valid = 1;
1955 }
a5606eee 1956
2d134ed3
PA
1957 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1958
a5606eee 1959 /* Look at each value on the value chain. */
9fa40276 1960 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1961 {
1962 /* If it's a memory location, and GDB actually needed
1963 its contents to evaluate the expression, then we
fa4727a6
DJ
1964 must watch it. If the first value returned is
1965 still lazy, that means an error occurred reading it;
1966 watch it anyway in case it becomes readable. */
a5606eee 1967 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1968 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1969 {
1970 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1971
a5606eee
VP
1972 /* We only watch structs and arrays if user asked
1973 for it explicitly, never if they just happen to
1974 appear in the middle of some value chain. */
fa4727a6 1975 if (v == result
a5606eee
VP
1976 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1977 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1978 {
1979 CORE_ADDR addr;
f486487f 1980 enum target_hw_bp_type type;
a5606eee 1981 struct bp_location *loc, **tmp;
bb9d5f81
PP
1982 int bitpos = 0, bitsize = 0;
1983
1984 if (value_bitsize (v) != 0)
1985 {
1986 /* Extract the bit parameters out from the bitfield
1987 sub-expression. */
1988 bitpos = value_bitpos (v);
1989 bitsize = value_bitsize (v);
1990 }
1991 else if (v == result && b->val_bitsize != 0)
1992 {
1993 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1994 lvalue whose bit parameters are saved in the fields
1995 VAL_BITPOS and VAL_BITSIZE. */
1996 bitpos = b->val_bitpos;
1997 bitsize = b->val_bitsize;
1998 }
a5606eee 1999
42ae5230 2000 addr = value_address (v);
bb9d5f81
PP
2001 if (bitsize != 0)
2002 {
2003 /* Skip the bytes that don't contain the bitfield. */
2004 addr += bitpos / 8;
2005 }
2006
a5606eee 2007 type = hw_write;
3a5c3e22 2008 if (b->base.type == bp_read_watchpoint)
a5606eee 2009 type = hw_read;
3a5c3e22 2010 else if (b->base.type == bp_access_watchpoint)
a5606eee 2011 type = hw_access;
3a5c3e22
PA
2012
2013 loc = allocate_bp_location (&b->base);
2014 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2015 ;
2016 *tmp = loc;
a6d9a66e 2017 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2018
2019 loc->pspace = frame_pspace;
a5606eee 2020 loc->address = addr;
bb9d5f81
PP
2021
2022 if (bitsize != 0)
2023 {
2024 /* Just cover the bytes that make up the bitfield. */
2025 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2026 }
2027 else
2028 loc->length = TYPE_LENGTH (value_type (v));
2029
a5606eee
VP
2030 loc->watchpoint_type = type;
2031 }
2032 }
9fa40276
TJB
2033 }
2034
2035 /* Change the type of breakpoint between hardware assisted or
2036 an ordinary watchpoint depending on the hardware support
2037 and free hardware slots. REPARSE is set when the inferior
2038 is started. */
a9634178 2039 if (reparse)
9fa40276 2040 {
e09342b5 2041 int reg_cnt;
9fa40276
TJB
2042 enum bp_loc_type loc_type;
2043 struct bp_location *bl;
a5606eee 2044
a9634178 2045 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2046
2047 if (reg_cnt)
9fa40276
TJB
2048 {
2049 int i, target_resources_ok, other_type_used;
a1398e0c 2050 enum bptype type;
9fa40276 2051
a9634178
TJB
2052 /* Use an exact watchpoint when there's only one memory region to be
2053 watched, and only one debug register is needed to watch it. */
2054 b->exact = target_exact_watchpoints && reg_cnt == 1;
2055
9fa40276 2056 /* We need to determine how many resources are already
e09342b5
TJB
2057 used for all other hardware watchpoints plus this one
2058 to see if we still have enough resources to also fit
a1398e0c
PA
2059 this watchpoint in as well. */
2060
2061 /* If this is a software watchpoint, we try to turn it
2062 to a hardware one -- count resources as if B was of
2063 hardware watchpoint type. */
2064 type = b->base.type;
2065 if (type == bp_watchpoint)
2066 type = bp_hardware_watchpoint;
2067
2068 /* This watchpoint may or may not have been placed on
2069 the list yet at this point (it won't be in the list
2070 if we're trying to create it for the first time,
2071 through watch_command), so always account for it
2072 manually. */
2073
2074 /* Count resources used by all watchpoints except B. */
2075 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2076
2077 /* Add in the resources needed for B. */
2078 i += hw_watchpoint_use_count (&b->base);
2079
2080 target_resources_ok
2081 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2082 if (target_resources_ok <= 0)
a9634178 2083 {
3a5c3e22 2084 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2085
2086 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2087 error (_("Target does not support this type of "
2088 "hardware watchpoint."));
9c06b0b4
TJB
2089 else if (target_resources_ok < 0 && !sw_mode)
2090 error (_("There are not enough available hardware "
2091 "resources for this watchpoint."));
a1398e0c
PA
2092
2093 /* Downgrade to software watchpoint. */
2094 b->base.type = bp_watchpoint;
2095 }
2096 else
2097 {
2098 /* If this was a software watchpoint, we've just
2099 found we have enough resources to turn it to a
2100 hardware watchpoint. Otherwise, this is a
2101 nop. */
2102 b->base.type = type;
a9634178 2103 }
9fa40276 2104 }
3a5c3e22 2105 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2106 {
2107 if (!can_use_hw_watchpoints)
2108 error (_("Can't set read/access watchpoint when "
2109 "hardware watchpoints are disabled."));
2110 else
2111 error (_("Expression cannot be implemented with "
2112 "read/access watchpoint."));
2113 }
9fa40276 2114 else
3a5c3e22 2115 b->base.type = bp_watchpoint;
9fa40276 2116
3a5c3e22 2117 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2118 : bp_loc_hardware_watchpoint);
3a5c3e22 2119 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2120 bl->loc_type = loc_type;
2121 }
2122
2123 for (v = val_chain; v; v = next)
2124 {
a5606eee
VP
2125 next = value_next (v);
2126 if (v != b->val)
2127 value_free (v);
2128 }
2129
c7437ca6
PA
2130 /* If a software watchpoint is not watching any memory, then the
2131 above left it without any location set up. But,
2132 bpstat_stop_status requires a location to be able to report
2133 stops, so make sure there's at least a dummy one. */
3a5c3e22 2134 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2135 {
3a5c3e22
PA
2136 struct breakpoint *base = &b->base;
2137 base->loc = allocate_bp_location (base);
2138 base->loc->pspace = frame_pspace;
2139 base->loc->address = -1;
2140 base->loc->length = -1;
2141 base->loc->watchpoint_type = -1;
c7437ca6 2142 }
a5606eee
VP
2143 }
2144 else if (!within_current_scope)
7270d8f2 2145 {
ac74f770
MS
2146 printf_filtered (_("\
2147Watchpoint %d deleted because the program has left the block\n\
2148in which its expression is valid.\n"),
3a5c3e22 2149 b->base.number);
d0fb5eae 2150 watchpoint_del_at_next_stop (b);
7270d8f2 2151 }
a5606eee
VP
2152
2153 /* Restore the selected frame. */
66076460
DJ
2154 if (frame_saved)
2155 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2156}
2157
a5606eee 2158
74960c60 2159/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2160 inserted in the inferior. We don't differentiate the type of BL's owner
2161 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2162 breakpoint_ops is not defined, because in insert_bp_location,
2163 tracepoint's insert_location will not be called. */
74960c60 2164static int
35df4500 2165should_be_inserted (struct bp_location *bl)
74960c60 2166{
35df4500 2167 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2168 return 0;
2169
35df4500 2170 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2171 return 0;
2172
35df4500 2173 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2174 return 0;
2175
f8eba3c6
TT
2176 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2177 return 0;
2178
56710373
PA
2179 /* This is set for example, when we're attached to the parent of a
2180 vfork, and have detached from the child. The child is running
2181 free, and we expect it to do an exec or exit, at which point the
2182 OS makes the parent schedulable again (and the target reports
2183 that the vfork is done). Until the child is done with the shared
2184 memory region, do not insert breakpoints in the parent, otherwise
2185 the child could still trip on the parent's breakpoints. Since
2186 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2187 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2188 return 0;
2189
31e77af2
PA
2190 /* Don't insert a breakpoint if we're trying to step past its
2191 location. */
2192 if ((bl->loc_type == bp_loc_software_breakpoint
2193 || bl->loc_type == bp_loc_hardware_breakpoint)
2194 && stepping_past_instruction_at (bl->pspace->aspace,
2195 bl->address))
e558d7c1
PA
2196 {
2197 if (debug_infrun)
2198 {
2199 fprintf_unfiltered (gdb_stdlog,
2200 "infrun: skipping breakpoint: "
2201 "stepping past insn at: %s\n",
2202 paddress (bl->gdbarch, bl->address));
2203 }
2204 return 0;
2205 }
31e77af2 2206
963f9c80
PA
2207 /* Don't insert watchpoints if we're trying to step past the
2208 instruction that triggered one. */
2209 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2210 && stepping_past_nonsteppable_watchpoint ())
2211 {
2212 if (debug_infrun)
2213 {
2214 fprintf_unfiltered (gdb_stdlog,
2215 "infrun: stepping past non-steppable watchpoint. "
2216 "skipping watchpoint at %s:%d\n",
2217 paddress (bl->gdbarch, bl->address),
2218 bl->length);
2219 }
2220 return 0;
2221 }
2222
74960c60
VP
2223 return 1;
2224}
2225
934709f0
PW
2226/* Same as should_be_inserted but does the check assuming
2227 that the location is not duplicated. */
2228
2229static int
2230unduplicated_should_be_inserted (struct bp_location *bl)
2231{
2232 int result;
2233 const int save_duplicate = bl->duplicate;
2234
2235 bl->duplicate = 0;
2236 result = should_be_inserted (bl);
2237 bl->duplicate = save_duplicate;
2238 return result;
2239}
2240
b775012e
LM
2241/* Parses a conditional described by an expression COND into an
2242 agent expression bytecode suitable for evaluation
2243 by the bytecode interpreter. Return NULL if there was
2244 any error during parsing. */
2245
2246static struct agent_expr *
2247parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2248{
2249 struct agent_expr *aexpr = NULL;
b775012e
LM
2250
2251 if (!cond)
2252 return NULL;
2253
2254 /* We don't want to stop processing, so catch any errors
2255 that may show up. */
492d29ea 2256 TRY
b775012e
LM
2257 {
2258 aexpr = gen_eval_for_expr (scope, cond);
2259 }
2260
492d29ea 2261 CATCH (ex, RETURN_MASK_ERROR)
b775012e
LM
2262 {
2263 /* If we got here, it means the condition could not be parsed to a valid
2264 bytecode expression and thus can't be evaluated on the target's side.
2265 It's no use iterating through the conditions. */
2266 return NULL;
2267 }
492d29ea 2268 END_CATCH
b775012e
LM
2269
2270 /* We have a valid agent expression. */
2271 return aexpr;
2272}
2273
2274/* Based on location BL, create a list of breakpoint conditions to be
2275 passed on to the target. If we have duplicated locations with different
2276 conditions, we will add such conditions to the list. The idea is that the
2277 target will evaluate the list of conditions and will only notify GDB when
2278 one of them is true. */
2279
2280static void
2281build_target_condition_list (struct bp_location *bl)
2282{
2283 struct bp_location **locp = NULL, **loc2p;
2284 int null_condition_or_parse_error = 0;
2285 int modified = bl->needs_update;
2286 struct bp_location *loc;
2287
8b4f3082
PA
2288 /* Release conditions left over from a previous insert. */
2289 VEC_free (agent_expr_p, bl->target_info.conditions);
2290
b775012e
LM
2291 /* This is only meaningful if the target is
2292 evaluating conditions and if the user has
2293 opted for condition evaluation on the target's
2294 side. */
2295 if (gdb_evaluates_breakpoint_condition_p ()
2296 || !target_supports_evaluation_of_breakpoint_conditions ())
2297 return;
2298
2299 /* Do a first pass to check for locations with no assigned
2300 conditions or conditions that fail to parse to a valid agent expression
2301 bytecode. If any of these happen, then it's no use to send conditions
2302 to the target since this location will always trigger and generate a
2303 response back to GDB. */
2304 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2305 {
2306 loc = (*loc2p);
2307 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2308 {
2309 if (modified)
2310 {
2311 struct agent_expr *aexpr;
2312
2313 /* Re-parse the conditions since something changed. In that
2314 case we already freed the condition bytecodes (see
2315 force_breakpoint_reinsertion). We just
2316 need to parse the condition to bytecodes again. */
2317 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2318 loc->cond_bytecode = aexpr;
2319
2320 /* Check if we managed to parse the conditional expression
2321 correctly. If not, we will not send this condition
2322 to the target. */
2323 if (aexpr)
2324 continue;
2325 }
2326
2327 /* If we have a NULL bytecode expression, it means something
2328 went wrong or we have a null condition expression. */
2329 if (!loc->cond_bytecode)
2330 {
2331 null_condition_or_parse_error = 1;
2332 break;
2333 }
2334 }
2335 }
2336
2337 /* If any of these happened, it means we will have to evaluate the conditions
2338 for the location's address on gdb's side. It is no use keeping bytecodes
2339 for all the other duplicate locations, thus we free all of them here.
2340
2341 This is so we have a finer control over which locations' conditions are
2342 being evaluated by GDB or the remote stub. */
2343 if (null_condition_or_parse_error)
2344 {
2345 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2346 {
2347 loc = (*loc2p);
2348 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2349 {
2350 /* Only go as far as the first NULL bytecode is
2351 located. */
2352 if (!loc->cond_bytecode)
2353 return;
2354
2355 free_agent_expr (loc->cond_bytecode);
2356 loc->cond_bytecode = NULL;
2357 }
2358 }
2359 }
2360
2361 /* No NULL conditions or failed bytecode generation. Build a condition list
2362 for this location's address. */
2363 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2364 {
2365 loc = (*loc2p);
2366 if (loc->cond
2367 && is_breakpoint (loc->owner)
2368 && loc->pspace->num == bl->pspace->num
2369 && loc->owner->enable_state == bp_enabled
2370 && loc->enabled)
2371 /* Add the condition to the vector. This will be used later to send the
2372 conditions to the target. */
2373 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2374 loc->cond_bytecode);
2375 }
2376
2377 return;
2378}
2379
d3ce09f5
SS
2380/* Parses a command described by string CMD into an agent expression
2381 bytecode suitable for evaluation by the bytecode interpreter.
2382 Return NULL if there was any error during parsing. */
2383
2384static struct agent_expr *
2385parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2386{
2387 struct cleanup *old_cleanups = 0;
2388 struct expression *expr, **argvec;
2389 struct agent_expr *aexpr = NULL;
bbc13ae3
KS
2390 const char *cmdrest;
2391 const char *format_start, *format_end;
d3ce09f5
SS
2392 struct format_piece *fpieces;
2393 int nargs;
2394 struct gdbarch *gdbarch = get_current_arch ();
2395
2396 if (!cmd)
2397 return NULL;
2398
2399 cmdrest = cmd;
2400
2401 if (*cmdrest == ',')
2402 ++cmdrest;
bbc13ae3 2403 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2404
2405 if (*cmdrest++ != '"')
2406 error (_("No format string following the location"));
2407
2408 format_start = cmdrest;
2409
2410 fpieces = parse_format_string (&cmdrest);
2411
2412 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2413
2414 format_end = cmdrest;
2415
2416 if (*cmdrest++ != '"')
2417 error (_("Bad format string, non-terminated '\"'."));
2418
bbc13ae3 2419 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2420
2421 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2422 error (_("Invalid argument syntax"));
2423
2424 if (*cmdrest == ',')
2425 cmdrest++;
bbc13ae3 2426 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2427
2428 /* For each argument, make an expression. */
2429
2430 argvec = (struct expression **) alloca (strlen (cmd)
2431 * sizeof (struct expression *));
2432
2433 nargs = 0;
2434 while (*cmdrest != '\0')
2435 {
bbc13ae3 2436 const char *cmd1;
d3ce09f5
SS
2437
2438 cmd1 = cmdrest;
2439 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2440 argvec[nargs++] = expr;
2441 cmdrest = cmd1;
2442 if (*cmdrest == ',')
2443 ++cmdrest;
2444 }
2445
2446 /* We don't want to stop processing, so catch any errors
2447 that may show up. */
492d29ea 2448 TRY
d3ce09f5
SS
2449 {
2450 aexpr = gen_printf (scope, gdbarch, 0, 0,
2451 format_start, format_end - format_start,
2452 fpieces, nargs, argvec);
2453 }
492d29ea 2454 CATCH (ex, RETURN_MASK_ERROR)
d3ce09f5
SS
2455 {
2456 /* If we got here, it means the command could not be parsed to a valid
2457 bytecode expression and thus can't be evaluated on the target's side.
2458 It's no use iterating through the other commands. */
492d29ea 2459 aexpr = NULL;
d3ce09f5 2460 }
492d29ea
PA
2461 END_CATCH
2462
2463 do_cleanups (old_cleanups);
d3ce09f5 2464
d3ce09f5
SS
2465 /* We have a valid agent expression, return it. */
2466 return aexpr;
2467}
2468
2469/* Based on location BL, create a list of breakpoint commands to be
2470 passed on to the target. If we have duplicated locations with
2471 different commands, we will add any such to the list. */
2472
2473static void
2474build_target_command_list (struct bp_location *bl)
2475{
2476 struct bp_location **locp = NULL, **loc2p;
2477 int null_command_or_parse_error = 0;
2478 int modified = bl->needs_update;
2479 struct bp_location *loc;
2480
8b4f3082
PA
2481 /* Release commands left over from a previous insert. */
2482 VEC_free (agent_expr_p, bl->target_info.tcommands);
2483
41fac0cf 2484 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2485 return;
2486
41fac0cf
PA
2487 /* For now, limit to agent-style dprintf breakpoints. */
2488 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2489 return;
2490
41fac0cf
PA
2491 /* For now, if we have any duplicate location that isn't a dprintf,
2492 don't install the target-side commands, as that would make the
2493 breakpoint not be reported to the core, and we'd lose
2494 control. */
2495 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2496 {
2497 loc = (*loc2p);
2498 if (is_breakpoint (loc->owner)
2499 && loc->pspace->num == bl->pspace->num
2500 && loc->owner->type != bp_dprintf)
2501 return;
2502 }
2503
d3ce09f5
SS
2504 /* Do a first pass to check for locations with no assigned
2505 conditions or conditions that fail to parse to a valid agent expression
2506 bytecode. If any of these happen, then it's no use to send conditions
2507 to the target since this location will always trigger and generate a
2508 response back to GDB. */
2509 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2510 {
2511 loc = (*loc2p);
2512 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2513 {
2514 if (modified)
2515 {
2516 struct agent_expr *aexpr;
2517
2518 /* Re-parse the commands since something changed. In that
2519 case we already freed the command bytecodes (see
2520 force_breakpoint_reinsertion). We just
2521 need to parse the command to bytecodes again. */
2522 aexpr = parse_cmd_to_aexpr (bl->address,
2523 loc->owner->extra_string);
2524 loc->cmd_bytecode = aexpr;
2525
2526 if (!aexpr)
2527 continue;
2528 }
2529
2530 /* If we have a NULL bytecode expression, it means something
2531 went wrong or we have a null command expression. */
2532 if (!loc->cmd_bytecode)
2533 {
2534 null_command_or_parse_error = 1;
2535 break;
2536 }
2537 }
2538 }
2539
2540 /* If anything failed, then we're not doing target-side commands,
2541 and so clean up. */
2542 if (null_command_or_parse_error)
2543 {
2544 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2545 {
2546 loc = (*loc2p);
2547 if (is_breakpoint (loc->owner)
2548 && loc->pspace->num == bl->pspace->num)
2549 {
2550 /* Only go as far as the first NULL bytecode is
2551 located. */
40fb6c5e 2552 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2553 return;
2554
40fb6c5e
HZ
2555 free_agent_expr (loc->cmd_bytecode);
2556 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2557 }
2558 }
2559 }
2560
2561 /* No NULL commands or failed bytecode generation. Build a command list
2562 for this location's address. */
2563 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2564 {
2565 loc = (*loc2p);
2566 if (loc->owner->extra_string
2567 && is_breakpoint (loc->owner)
2568 && loc->pspace->num == bl->pspace->num
2569 && loc->owner->enable_state == bp_enabled
2570 && loc->enabled)
2571 /* Add the command to the vector. This will be used later
2572 to send the commands to the target. */
2573 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2574 loc->cmd_bytecode);
2575 }
2576
2577 bl->target_info.persist = 0;
2578 /* Maybe flag this location as persistent. */
2579 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2580 bl->target_info.persist = 1;
2581}
2582
35df4500
TJB
2583/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2584 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2585 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2586 Returns 0 for success, 1 if the bp_location type is not supported or
2587 -1 for failure.
879bfdc2 2588
4a64f543
MS
2589 NOTE drow/2003-09-09: This routine could be broken down to an
2590 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2591static int
35df4500 2592insert_bp_location (struct bp_location *bl,
26bb91f3 2593 struct ui_file *tmp_error_stream,
3fbb6ffa 2594 int *disabled_breaks,
dd61ec5c
MW
2595 int *hw_breakpoint_error,
2596 int *hw_bp_error_explained_already)
879bfdc2 2597{
0000e5cc
PA
2598 enum errors bp_err = GDB_NO_ERROR;
2599 const char *bp_err_message = NULL;
879bfdc2 2600
b775012e 2601 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2602 return 0;
2603
35c63cd8
JB
2604 /* Note we don't initialize bl->target_info, as that wipes out
2605 the breakpoint location's shadow_contents if the breakpoint
2606 is still inserted at that location. This in turn breaks
2607 target_read_memory which depends on these buffers when
2608 a memory read is requested at the breakpoint location:
2609 Once the target_info has been wiped, we fail to see that
2610 we have a breakpoint inserted at that address and thus
2611 read the breakpoint instead of returning the data saved in
2612 the breakpoint location's shadow contents. */
0d5ed153 2613 bl->target_info.reqstd_address = bl->address;
35df4500 2614 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2615 bl->target_info.length = bl->length;
8181d85f 2616
b775012e
LM
2617 /* When working with target-side conditions, we must pass all the conditions
2618 for the same breakpoint address down to the target since GDB will not
2619 insert those locations. With a list of breakpoint conditions, the target
2620 can decide when to stop and notify GDB. */
2621
2622 if (is_breakpoint (bl->owner))
2623 {
2624 build_target_condition_list (bl);
d3ce09f5
SS
2625 build_target_command_list (bl);
2626 /* Reset the modification marker. */
b775012e
LM
2627 bl->needs_update = 0;
2628 }
2629
35df4500
TJB
2630 if (bl->loc_type == bp_loc_software_breakpoint
2631 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2632 {
35df4500 2633 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2634 {
2635 /* If the explicitly specified breakpoint type
2636 is not hardware breakpoint, check the memory map to see
2637 if the breakpoint address is in read only memory or not.
4a64f543 2638
765dc015
VP
2639 Two important cases are:
2640 - location type is not hardware breakpoint, memory
2641 is readonly. We change the type of the location to
2642 hardware breakpoint.
4a64f543
MS
2643 - location type is hardware breakpoint, memory is
2644 read-write. This means we've previously made the
2645 location hardware one, but then the memory map changed,
2646 so we undo.
765dc015 2647
4a64f543
MS
2648 When breakpoints are removed, remove_breakpoints will use
2649 location types we've just set here, the only possible
2650 problem is that memory map has changed during running
2651 program, but it's not going to work anyway with current
2652 gdb. */
765dc015 2653 struct mem_region *mr
0d5ed153 2654 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2655
2656 if (mr)
2657 {
2658 if (automatic_hardware_breakpoints)
2659 {
765dc015
VP
2660 enum bp_loc_type new_type;
2661
2662 if (mr->attrib.mode != MEM_RW)
2663 new_type = bp_loc_hardware_breakpoint;
2664 else
2665 new_type = bp_loc_software_breakpoint;
2666
35df4500 2667 if (new_type != bl->loc_type)
765dc015
VP
2668 {
2669 static int said = 0;
cc59ec59 2670
35df4500 2671 bl->loc_type = new_type;
765dc015
VP
2672 if (!said)
2673 {
3e43a32a
MS
2674 fprintf_filtered (gdb_stdout,
2675 _("Note: automatically using "
2676 "hardware breakpoints for "
2677 "read-only addresses.\n"));
765dc015
VP
2678 said = 1;
2679 }
2680 }
2681 }
35df4500 2682 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2683 && mr->attrib.mode != MEM_RW)
2684 {
2685 fprintf_unfiltered (tmp_error_stream,
2686 _("Cannot insert breakpoint %d.\n"
2687 "Cannot set software breakpoint "
2688 "at read-only address %s\n"),
2689 bl->owner->number,
2690 paddress (bl->gdbarch, bl->address));
2691 return 1;
2692 }
765dc015
VP
2693 }
2694 }
2695
879bfdc2
DJ
2696 /* First check to see if we have to handle an overlay. */
2697 if (overlay_debugging == ovly_off
35df4500
TJB
2698 || bl->section == NULL
2699 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2700 {
2701 /* No overlay handling: just set the breakpoint. */
492d29ea 2702 TRY
dd61ec5c 2703 {
0000e5cc
PA
2704 int val;
2705
dd61ec5c 2706 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2707 if (val)
2708 bp_err = GENERIC_ERROR;
dd61ec5c 2709 }
492d29ea 2710 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2711 {
0000e5cc
PA
2712 bp_err = e.error;
2713 bp_err_message = e.message;
dd61ec5c 2714 }
492d29ea 2715 END_CATCH
879bfdc2
DJ
2716 }
2717 else
2718 {
4a64f543 2719 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2720 Shall we set a breakpoint at the LMA? */
2721 if (!overlay_events_enabled)
2722 {
2723 /* Yes -- overlay event support is not active,
2724 so we must try to set a breakpoint at the LMA.
2725 This will not work for a hardware breakpoint. */
35df4500 2726 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2727 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2728 bl->owner->number);
879bfdc2
DJ
2729 else
2730 {
35df4500
TJB
2731 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2732 bl->section);
879bfdc2 2733 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2734 bl->overlay_target_info = bl->target_info;
0d5ed153 2735 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2736
2737 /* No overlay handling: just set the breakpoint. */
492d29ea 2738 TRY
0000e5cc
PA
2739 {
2740 int val;
2741
2742 val = target_insert_breakpoint (bl->gdbarch,
2743 &bl->overlay_target_info);
2744 if (val)
2745 bp_err = GENERIC_ERROR;
2746 }
492d29ea 2747 CATCH (e, RETURN_MASK_ALL)
0000e5cc
PA
2748 {
2749 bp_err = e.error;
2750 bp_err_message = e.message;
2751 }
492d29ea 2752 END_CATCH
0000e5cc
PA
2753
2754 if (bp_err != GDB_NO_ERROR)
99361f52 2755 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2756 "Overlay breakpoint %d "
2757 "failed: in ROM?\n",
35df4500 2758 bl->owner->number);
879bfdc2
DJ
2759 }
2760 }
2761 /* Shall we set a breakpoint at the VMA? */
35df4500 2762 if (section_is_mapped (bl->section))
879bfdc2
DJ
2763 {
2764 /* Yes. This overlay section is mapped into memory. */
492d29ea 2765 TRY
dd61ec5c 2766 {
0000e5cc
PA
2767 int val;
2768
dd61ec5c 2769 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2770 if (val)
2771 bp_err = GENERIC_ERROR;
dd61ec5c 2772 }
492d29ea 2773 CATCH (e, RETURN_MASK_ALL)
dd61ec5c 2774 {
0000e5cc
PA
2775 bp_err = e.error;
2776 bp_err_message = e.message;
dd61ec5c 2777 }
492d29ea 2778 END_CATCH
879bfdc2
DJ
2779 }
2780 else
2781 {
2782 /* No. This breakpoint will not be inserted.
2783 No error, but do not mark the bp as 'inserted'. */
2784 return 0;
2785 }
2786 }
2787
0000e5cc 2788 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2789 {
2790 /* Can't set the breakpoint. */
0000e5cc
PA
2791
2792 /* In some cases, we might not be able to insert a
2793 breakpoint in a shared library that has already been
2794 removed, but we have not yet processed the shlib unload
2795 event. Unfortunately, some targets that implement
076855f9
PA
2796 breakpoint insertion themselves can't tell why the
2797 breakpoint insertion failed (e.g., the remote target
2798 doesn't define error codes), so we must treat generic
2799 errors as memory errors. */
0000e5cc 2800 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2801 && bl->loc_type == bp_loc_software_breakpoint
08351840 2802 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2803 || shared_objfile_contains_address_p (bl->pspace,
2804 bl->address)))
879bfdc2 2805 {
4a64f543 2806 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2807 bl->shlib_disabled = 1;
8d3788bd 2808 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2809 if (!*disabled_breaks)
2810 {
2811 fprintf_unfiltered (tmp_error_stream,
2812 "Cannot insert breakpoint %d.\n",
2813 bl->owner->number);
2814 fprintf_unfiltered (tmp_error_stream,
2815 "Temporarily disabling shared "
2816 "library breakpoints:\n");
2817 }
2818 *disabled_breaks = 1;
879bfdc2 2819 fprintf_unfiltered (tmp_error_stream,
35df4500 2820 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2821 return 0;
879bfdc2
DJ
2822 }
2823 else
879bfdc2 2824 {
35df4500 2825 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2826 {
0000e5cc
PA
2827 *hw_breakpoint_error = 1;
2828 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2829 fprintf_unfiltered (tmp_error_stream,
2830 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2831 bl->owner->number, bp_err_message ? ":" : ".\n");
2832 if (bp_err_message != NULL)
2833 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2834 }
2835 else
2836 {
0000e5cc
PA
2837 if (bp_err_message == NULL)
2838 {
2839 char *message
2840 = memory_error_message (TARGET_XFER_E_IO,
2841 bl->gdbarch, bl->address);
2842 struct cleanup *old_chain = make_cleanup (xfree, message);
2843
2844 fprintf_unfiltered (tmp_error_stream,
2845 "Cannot insert breakpoint %d.\n"
2846 "%s\n",
2847 bl->owner->number, message);
2848 do_cleanups (old_chain);
2849 }
2850 else
2851 {
2852 fprintf_unfiltered (tmp_error_stream,
2853 "Cannot insert breakpoint %d: %s\n",
2854 bl->owner->number,
2855 bp_err_message);
2856 }
879bfdc2 2857 }
0000e5cc 2858 return 1;
879bfdc2
DJ
2859
2860 }
2861 }
2862 else
35df4500 2863 bl->inserted = 1;
879bfdc2 2864
0000e5cc 2865 return 0;
879bfdc2
DJ
2866 }
2867
35df4500 2868 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2869 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2870 watchpoints. It's not clear that it's necessary... */
35df4500 2871 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2872 {
0000e5cc
PA
2873 int val;
2874
77b06cd7
TJB
2875 gdb_assert (bl->owner->ops != NULL
2876 && bl->owner->ops->insert_location != NULL);
2877
2878 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2879
2880 /* If trying to set a read-watchpoint, and it turns out it's not
2881 supported, try emulating one with an access watchpoint. */
35df4500 2882 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2883 {
2884 struct bp_location *loc, **loc_temp;
2885
2886 /* But don't try to insert it, if there's already another
2887 hw_access location that would be considered a duplicate
2888 of this one. */
2889 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2890 if (loc != bl
85d721b8 2891 && loc->watchpoint_type == hw_access
35df4500 2892 && watchpoint_locations_match (bl, loc))
85d721b8 2893 {
35df4500
TJB
2894 bl->duplicate = 1;
2895 bl->inserted = 1;
2896 bl->target_info = loc->target_info;
2897 bl->watchpoint_type = hw_access;
85d721b8
PA
2898 val = 0;
2899 break;
2900 }
2901
2902 if (val == 1)
2903 {
77b06cd7
TJB
2904 bl->watchpoint_type = hw_access;
2905 val = bl->owner->ops->insert_location (bl);
2906
2907 if (val)
2908 /* Back to the original value. */
2909 bl->watchpoint_type = hw_read;
85d721b8
PA
2910 }
2911 }
2912
35df4500 2913 bl->inserted = (val == 0);
879bfdc2
DJ
2914 }
2915
35df4500 2916 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2917 {
0000e5cc
PA
2918 int val;
2919
77b06cd7
TJB
2920 gdb_assert (bl->owner->ops != NULL
2921 && bl->owner->ops->insert_location != NULL);
2922
2923 val = bl->owner->ops->insert_location (bl);
2924 if (val)
2925 {
2926 bl->owner->enable_state = bp_disabled;
2927
2928 if (val == 1)
2929 warning (_("\
2930Error inserting catchpoint %d: Your system does not support this type\n\
2931of catchpoint."), bl->owner->number);
2932 else
2933 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2934 }
2935
2936 bl->inserted = (val == 0);
1640b821
DJ
2937
2938 /* We've already printed an error message if there was a problem
2939 inserting this catchpoint, and we've disabled the catchpoint,
2940 so just return success. */
2941 return 0;
879bfdc2
DJ
2942 }
2943
2944 return 0;
2945}
2946
6c95b8df
PA
2947/* This function is called when program space PSPACE is about to be
2948 deleted. It takes care of updating breakpoints to not reference
2949 PSPACE anymore. */
2950
2951void
2952breakpoint_program_space_exit (struct program_space *pspace)
2953{
2954 struct breakpoint *b, *b_temp;
876fa593 2955 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2956
2957 /* Remove any breakpoint that was set through this program space. */
2958 ALL_BREAKPOINTS_SAFE (b, b_temp)
2959 {
2960 if (b->pspace == pspace)
2961 delete_breakpoint (b);
2962 }
2963
2964 /* Breakpoints set through other program spaces could have locations
2965 bound to PSPACE as well. Remove those. */
876fa593 2966 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2967 {
2968 struct bp_location *tmp;
2969
2970 if (loc->pspace == pspace)
2971 {
2bdf28a0 2972 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2973 if (loc->owner->loc == loc)
2974 loc->owner->loc = loc->next;
2975 else
2976 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2977 if (tmp->next == loc)
2978 {
2979 tmp->next = loc->next;
2980 break;
2981 }
2982 }
2983 }
2984
2985 /* Now update the global location list to permanently delete the
2986 removed locations above. */
44702360 2987 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2988}
2989
74960c60
VP
2990/* Make sure all breakpoints are inserted in inferior.
2991 Throws exception on any error.
2992 A breakpoint that is already inserted won't be inserted
2993 again, so calling this function twice is safe. */
2994void
2995insert_breakpoints (void)
2996{
2997 struct breakpoint *bpt;
2998
2999 ALL_BREAKPOINTS (bpt)
3000 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
3001 {
3002 struct watchpoint *w = (struct watchpoint *) bpt;
3003
3004 update_watchpoint (w, 0 /* don't reparse. */);
3005 }
74960c60 3006
04086b45
PA
3007 /* Updating watchpoints creates new locations, so update the global
3008 location list. Explicitly tell ugll to insert locations and
3009 ignore breakpoints_always_inserted_mode. */
3010 update_global_location_list (UGLL_INSERT);
74960c60
VP
3011}
3012
20388dd6
YQ
3013/* Invoke CALLBACK for each of bp_location. */
3014
3015void
3016iterate_over_bp_locations (walk_bp_location_callback callback)
3017{
3018 struct bp_location *loc, **loc_tmp;
3019
3020 ALL_BP_LOCATIONS (loc, loc_tmp)
3021 {
3022 callback (loc, NULL);
3023 }
3024}
3025
b775012e
LM
3026/* This is used when we need to synch breakpoint conditions between GDB and the
3027 target. It is the case with deleting and disabling of breakpoints when using
3028 always-inserted mode. */
3029
3030static void
3031update_inserted_breakpoint_locations (void)
3032{
3033 struct bp_location *bl, **blp_tmp;
3034 int error_flag = 0;
3035 int val = 0;
3036 int disabled_breaks = 0;
3037 int hw_breakpoint_error = 0;
dd61ec5c 3038 int hw_bp_details_reported = 0;
b775012e
LM
3039
3040 struct ui_file *tmp_error_stream = mem_fileopen ();
3041 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3042
3043 /* Explicitly mark the warning -- this will only be printed if
3044 there was an error. */
3045 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3046
3047 save_current_space_and_thread ();
3048
3049 ALL_BP_LOCATIONS (bl, blp_tmp)
3050 {
3051 /* We only want to update software breakpoints and hardware
3052 breakpoints. */
3053 if (!is_breakpoint (bl->owner))
3054 continue;
3055
3056 /* We only want to update locations that are already inserted
3057 and need updating. This is to avoid unwanted insertion during
3058 deletion of breakpoints. */
3059 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3060 continue;
3061
3062 switch_to_program_space_and_thread (bl->pspace);
3063
3064 /* For targets that support global breakpoints, there's no need
3065 to select an inferior to insert breakpoint to. In fact, even
3066 if we aren't attached to any process yet, we should still
3067 insert breakpoints. */
f5656ead 3068 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3069 && ptid_equal (inferior_ptid, null_ptid))
3070 continue;
3071
3072 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3073 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3074 if (val)
3075 error_flag = val;
3076 }
3077
3078 if (error_flag)
3079 {
3080 target_terminal_ours_for_output ();
3081 error_stream (tmp_error_stream);
3082 }
3083
3084 do_cleanups (cleanups);
3085}
3086
c30eee59 3087/* Used when starting or continuing the program. */
c906108c 3088
74960c60
VP
3089static void
3090insert_breakpoint_locations (void)
c906108c 3091{
a5606eee 3092 struct breakpoint *bpt;
35df4500 3093 struct bp_location *bl, **blp_tmp;
eacd795a 3094 int error_flag = 0;
c906108c 3095 int val = 0;
3fbb6ffa 3096 int disabled_breaks = 0;
81d0cc19 3097 int hw_breakpoint_error = 0;
dd61ec5c 3098 int hw_bp_error_explained_already = 0;
c906108c 3099
81d0cc19 3100 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3101 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3102
81d0cc19
GS
3103 /* Explicitly mark the warning -- this will only be printed if
3104 there was an error. */
3105 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3106
3107 save_current_space_and_thread ();
3108
35df4500 3109 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3110 {
b775012e 3111 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3112 continue;
3113
4a64f543
MS
3114 /* There is no point inserting thread-specific breakpoints if
3115 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3116 has BL->OWNER always non-NULL. */
35df4500
TJB
3117 if (bl->owner->thread != -1
3118 && !valid_thread_id (bl->owner->thread))
f365de73
AS
3119 continue;
3120
35df4500 3121 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3122
3123 /* For targets that support global breakpoints, there's no need
3124 to select an inferior to insert breakpoint to. In fact, even
3125 if we aren't attached to any process yet, we should still
3126 insert breakpoints. */
f5656ead 3127 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3128 && ptid_equal (inferior_ptid, null_ptid))
3129 continue;
3130
3fbb6ffa 3131 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3132 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3133 if (val)
eacd795a 3134 error_flag = val;
879bfdc2 3135 }
c906108c 3136
4a64f543
MS
3137 /* If we failed to insert all locations of a watchpoint, remove
3138 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3139 ALL_BREAKPOINTS (bpt)
3140 {
3141 int some_failed = 0;
3142 struct bp_location *loc;
3143
3144 if (!is_hardware_watchpoint (bpt))
3145 continue;
3146
d6b74ac4 3147 if (!breakpoint_enabled (bpt))
a5606eee 3148 continue;
74960c60
VP
3149
3150 if (bpt->disposition == disp_del_at_next_stop)
3151 continue;
a5606eee
VP
3152
3153 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3154 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3155 {
3156 some_failed = 1;
3157 break;
3158 }
3159 if (some_failed)
3160 {
3161 for (loc = bpt->loc; loc; loc = loc->next)
3162 if (loc->inserted)
3163 remove_breakpoint (loc, mark_uninserted);
3164
3165 hw_breakpoint_error = 1;
3166 fprintf_unfiltered (tmp_error_stream,
3167 "Could not insert hardware watchpoint %d.\n",
3168 bpt->number);
eacd795a 3169 error_flag = -1;
a5606eee
VP
3170 }
3171 }
3172
eacd795a 3173 if (error_flag)
81d0cc19
GS
3174 {
3175 /* If a hardware breakpoint or watchpoint was inserted, add a
3176 message about possibly exhausted resources. */
dd61ec5c 3177 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3178 {
c6510018
MS
3179 fprintf_unfiltered (tmp_error_stream,
3180 "Could not insert hardware breakpoints:\n\
3181You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3182 }
81d0cc19
GS
3183 target_terminal_ours_for_output ();
3184 error_stream (tmp_error_stream);
3185 }
f7545552
TT
3186
3187 do_cleanups (cleanups);
c906108c
SS
3188}
3189
c30eee59
TJB
3190/* Used when the program stops.
3191 Returns zero if successful, or non-zero if there was a problem
3192 removing a breakpoint location. */
3193
c906108c 3194int
fba45db2 3195remove_breakpoints (void)
c906108c 3196{
35df4500 3197 struct bp_location *bl, **blp_tmp;
3a1bae8e 3198 int val = 0;
c906108c 3199
35df4500 3200 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3201 {
1e4d1764 3202 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3203 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3204 }
3a1bae8e 3205 return val;
c906108c
SS
3206}
3207
49fa26b0
PA
3208/* When a thread exits, remove breakpoints that are related to
3209 that thread. */
3210
3211static void
3212remove_threaded_breakpoints (struct thread_info *tp, int silent)
3213{
3214 struct breakpoint *b, *b_tmp;
3215
3216 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3217 {
96181529 3218 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3219 {
3220 b->disposition = disp_del_at_next_stop;
3221
3222 printf_filtered (_("\
46ecd527 3223Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3224 b->number, tp->num);
3225
3226 /* Hide it from the user. */
3227 b->number = 0;
3228 }
3229 }
3230}
3231
6c95b8df
PA
3232/* Remove breakpoints of process PID. */
3233
3234int
3235remove_breakpoints_pid (int pid)
3236{
35df4500 3237 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3238 int val;
3239 struct inferior *inf = find_inferior_pid (pid);
3240
35df4500 3241 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3242 {
35df4500 3243 if (bl->pspace != inf->pspace)
6c95b8df
PA
3244 continue;
3245
fc126975 3246 if (bl->inserted && !bl->target_info.persist)
6c95b8df 3247 {
35df4500 3248 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3249 if (val != 0)
3250 return val;
3251 }
3252 }
3253 return 0;
3254}
3255
c906108c 3256int
fba45db2 3257reattach_breakpoints (int pid)
c906108c 3258{
6c95b8df 3259 struct cleanup *old_chain;
35df4500 3260 struct bp_location *bl, **blp_tmp;
c906108c 3261 int val;
86b887df 3262 struct ui_file *tmp_error_stream;
dd61ec5c 3263 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3264 struct inferior *inf;
3265 struct thread_info *tp;
3266
3267 tp = any_live_thread_of_process (pid);
3268 if (tp == NULL)
3269 return 1;
3270
3271 inf = find_inferior_pid (pid);
3272 old_chain = save_inferior_ptid ();
3273
3274 inferior_ptid = tp->ptid;
a4954f26 3275
86b887df 3276 tmp_error_stream = mem_fileopen ();
a4954f26 3277 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3278
35df4500 3279 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3280 {
35df4500 3281 if (bl->pspace != inf->pspace)
6c95b8df
PA
3282 continue;
3283
35df4500 3284 if (bl->inserted)
c5aa993b 3285 {
35df4500 3286 bl->inserted = 0;
dd61ec5c 3287 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3288 if (val != 0)
3289 {
ce696e05 3290 do_cleanups (old_chain);
c5aa993b
JM
3291 return val;
3292 }
3293 }
3294 }
ce696e05 3295 do_cleanups (old_chain);
c906108c
SS
3296 return 0;
3297}
3298
e58b0e63
PA
3299static int internal_breakpoint_number = -1;
3300
84f4c1fe
PM
3301/* Set the breakpoint number of B, depending on the value of INTERNAL.
3302 If INTERNAL is non-zero, the breakpoint number will be populated
3303 from internal_breakpoint_number and that variable decremented.
e5dd4106 3304 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3305 breakpoint_count and that value incremented. Internal breakpoints
3306 do not set the internal var bpnum. */
3307static void
3308set_breakpoint_number (int internal, struct breakpoint *b)
3309{
3310 if (internal)
3311 b->number = internal_breakpoint_number--;
3312 else
3313 {
3314 set_breakpoint_count (breakpoint_count + 1);
3315 b->number = breakpoint_count;
3316 }
3317}
3318
e62c965a 3319static struct breakpoint *
a6d9a66e 3320create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3321 CORE_ADDR address, enum bptype type,
c0a91b2b 3322 const struct breakpoint_ops *ops)
e62c965a 3323{
e62c965a
PP
3324 struct symtab_and_line sal;
3325 struct breakpoint *b;
3326
4a64f543 3327 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3328
3329 sal.pc = address;
3330 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3331 sal.pspace = current_program_space;
e62c965a 3332
06edf0c0 3333 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3334 b->number = internal_breakpoint_number--;
3335 b->disposition = disp_donttouch;
3336
3337 return b;
3338}
3339
17450429
PP
3340static const char *const longjmp_names[] =
3341 {
3342 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3343 };
3344#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3345
3346/* Per-objfile data private to breakpoint.c. */
3347struct breakpoint_objfile_data
3348{
3349 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3350 struct bound_minimal_symbol overlay_msym;
17450429
PP
3351
3352 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3353 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3354
28106bc2
SDJ
3355 /* True if we have looked for longjmp probes. */
3356 int longjmp_searched;
3357
3358 /* SystemTap probe points for longjmp (if any). */
3359 VEC (probe_p) *longjmp_probes;
3360
17450429 3361 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3362 struct bound_minimal_symbol terminate_msym;
17450429
PP
3363
3364 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3365 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3366
3367 /* True if we have looked for exception probes. */
3368 int exception_searched;
3369
3370 /* SystemTap probe points for unwinding (if any). */
3371 VEC (probe_p) *exception_probes;
17450429
PP
3372};
3373
3374static const struct objfile_data *breakpoint_objfile_key;
3375
3376/* Minimal symbol not found sentinel. */
3377static struct minimal_symbol msym_not_found;
3378
3379/* Returns TRUE if MSYM point to the "not found" sentinel. */
3380
3381static int
3382msym_not_found_p (const struct minimal_symbol *msym)
3383{
3384 return msym == &msym_not_found;
3385}
3386
3387/* Return per-objfile data needed by breakpoint.c.
3388 Allocate the data if necessary. */
3389
3390static struct breakpoint_objfile_data *
3391get_breakpoint_objfile_data (struct objfile *objfile)
3392{
3393 struct breakpoint_objfile_data *bp_objfile_data;
3394
3395 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3396 if (bp_objfile_data == NULL)
3397 {
8d749320
SM
3398 bp_objfile_data =
3399 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
17450429
PP
3400
3401 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3402 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3403 }
3404 return bp_objfile_data;
3405}
3406
28106bc2
SDJ
3407static void
3408free_breakpoint_probes (struct objfile *obj, void *data)
3409{
3410 struct breakpoint_objfile_data *bp_objfile_data = data;
3411
3412 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3413 VEC_free (probe_p, bp_objfile_data->exception_probes);
3414}
3415
e62c965a 3416static void
af02033e 3417create_overlay_event_breakpoint (void)
e62c965a 3418{
69de3c6a 3419 struct objfile *objfile;
af02033e 3420 const char *const func_name = "_ovly_debug_event";
e62c965a 3421
69de3c6a
PP
3422 ALL_OBJFILES (objfile)
3423 {
3424 struct breakpoint *b;
17450429
PP
3425 struct breakpoint_objfile_data *bp_objfile_data;
3426 CORE_ADDR addr;
67994074 3427 struct explicit_location explicit_loc;
69de3c6a 3428
17450429
PP
3429 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3430
3b7344d5 3431 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3432 continue;
3433
3b7344d5 3434 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3435 {
3b7344d5 3436 struct bound_minimal_symbol m;
17450429
PP
3437
3438 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3439 if (m.minsym == NULL)
17450429
PP
3440 {
3441 /* Avoid future lookups in this objfile. */
3b7344d5 3442 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3443 continue;
3444 }
3445 bp_objfile_data->overlay_msym = m;
3446 }
e62c965a 3447
77e371c0 3448 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3449 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3450 bp_overlay_event,
3451 &internal_breakpoint_ops);
67994074
KS
3452 initialize_explicit_location (&explicit_loc);
3453 explicit_loc.function_name = ASTRDUP (func_name);
3454 b->location = new_explicit_location (&explicit_loc);
e62c965a 3455
69de3c6a
PP
3456 if (overlay_debugging == ovly_auto)
3457 {
3458 b->enable_state = bp_enabled;
3459 overlay_events_enabled = 1;
3460 }
3461 else
3462 {
3463 b->enable_state = bp_disabled;
3464 overlay_events_enabled = 0;
3465 }
e62c965a 3466 }
44702360 3467 update_global_location_list (UGLL_MAY_INSERT);
e62c965a
PP
3468}
3469
0fd8e87f 3470static void
af02033e 3471create_longjmp_master_breakpoint (void)
0fd8e87f 3472{
6c95b8df 3473 struct program_space *pspace;
6c95b8df
PA
3474 struct cleanup *old_chain;
3475
3476 old_chain = save_current_program_space ();
0fd8e87f 3477
6c95b8df 3478 ALL_PSPACES (pspace)
af02033e
PP
3479 {
3480 struct objfile *objfile;
3481
3482 set_current_program_space (pspace);
3483
3484 ALL_OBJFILES (objfile)
0fd8e87f 3485 {
af02033e
PP
3486 int i;
3487 struct gdbarch *gdbarch;
17450429 3488 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3489
af02033e 3490 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3491
17450429
PP
3492 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3493
28106bc2
SDJ
3494 if (!bp_objfile_data->longjmp_searched)
3495 {
25f9533e
SDJ
3496 VEC (probe_p) *ret;
3497
3498 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3499 if (ret != NULL)
3500 {
3501 /* We are only interested in checking one element. */
3502 struct probe *p = VEC_index (probe_p, ret, 0);
3503
3504 if (!can_evaluate_probe_arguments (p))
3505 {
3506 /* We cannot use the probe interface here, because it does
3507 not know how to evaluate arguments. */
3508 VEC_free (probe_p, ret);
3509 ret = NULL;
3510 }
3511 }
3512 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3513 bp_objfile_data->longjmp_searched = 1;
3514 }
3515
3516 if (bp_objfile_data->longjmp_probes != NULL)
3517 {
3518 int i;
3519 struct probe *probe;
3520 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3521
3522 for (i = 0;
3523 VEC_iterate (probe_p,
3524 bp_objfile_data->longjmp_probes,
3525 i, probe);
3526 ++i)
3527 {
3528 struct breakpoint *b;
3529
729662a5
TT
3530 b = create_internal_breakpoint (gdbarch,
3531 get_probe_address (probe,
3532 objfile),
28106bc2
SDJ
3533 bp_longjmp_master,
3534 &internal_breakpoint_ops);
5b56227b
KS
3535 b->location
3536 = new_probe_location ("-probe-stap libc:longjmp");
28106bc2
SDJ
3537 b->enable_state = bp_disabled;
3538 }
3539
3540 continue;
3541 }
3542
0569175e
TSD
3543 if (!gdbarch_get_longjmp_target_p (gdbarch))
3544 continue;
3545
17450429 3546 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3547 {
3548 struct breakpoint *b;
af02033e 3549 const char *func_name;
17450429 3550 CORE_ADDR addr;
67994074 3551 struct explicit_location explicit_loc;
6c95b8df 3552
3b7344d5 3553 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3554 continue;
0fd8e87f 3555
17450429 3556 func_name = longjmp_names[i];
3b7344d5 3557 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3558 {
3b7344d5 3559 struct bound_minimal_symbol m;
17450429
PP
3560
3561 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3562 if (m.minsym == NULL)
17450429
PP
3563 {
3564 /* Prevent future lookups in this objfile. */
3b7344d5 3565 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3566 continue;
3567 }
3568 bp_objfile_data->longjmp_msym[i] = m;
3569 }
3570
77e371c0 3571 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3572 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3573 &internal_breakpoint_ops);
67994074
KS
3574 initialize_explicit_location (&explicit_loc);
3575 explicit_loc.function_name = ASTRDUP (func_name);
3576 b->location = new_explicit_location (&explicit_loc);
af02033e
PP
3577 b->enable_state = bp_disabled;
3578 }
0fd8e87f 3579 }
af02033e 3580 }
44702360 3581 update_global_location_list (UGLL_MAY_INSERT);
6c95b8df
PA
3582
3583 do_cleanups (old_chain);
0fd8e87f
UW
3584}
3585
af02033e 3586/* Create a master std::terminate breakpoint. */
aa7d318d 3587static void
af02033e 3588create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3589{
3590 struct program_space *pspace;
aa7d318d 3591 struct cleanup *old_chain;
af02033e 3592 const char *const func_name = "std::terminate()";
aa7d318d
TT
3593
3594 old_chain = save_current_program_space ();
3595
3596 ALL_PSPACES (pspace)
17450429
PP
3597 {
3598 struct objfile *objfile;
3599 CORE_ADDR addr;
3600
3601 set_current_program_space (pspace);
3602
aa7d318d
TT
3603 ALL_OBJFILES (objfile)
3604 {
3605 struct breakpoint *b;
17450429 3606 struct breakpoint_objfile_data *bp_objfile_data;
67994074 3607 struct explicit_location explicit_loc;
aa7d318d 3608
17450429 3609 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3610
3b7344d5 3611 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3612 continue;
3613
3b7344d5 3614 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3615 {
3b7344d5 3616 struct bound_minimal_symbol m;
17450429
PP
3617
3618 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3619 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3620 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3621 {
3622 /* Prevent future lookups in this objfile. */
3b7344d5 3623 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3624 continue;
3625 }
3626 bp_objfile_data->terminate_msym = m;
3627 }
aa7d318d 3628
77e371c0 3629 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3630 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3631 bp_std_terminate_master,
3632 &internal_breakpoint_ops);
67994074
KS
3633 initialize_explicit_location (&explicit_loc);
3634 explicit_loc.function_name = ASTRDUP (func_name);
3635 b->location = new_explicit_location (&explicit_loc);
aa7d318d
TT
3636 b->enable_state = bp_disabled;
3637 }
17450429
PP
3638 }
3639
44702360 3640 update_global_location_list (UGLL_MAY_INSERT);
aa7d318d
TT
3641
3642 do_cleanups (old_chain);
3643}
3644
186c406b
TT
3645/* Install a master breakpoint on the unwinder's debug hook. */
3646
70221824 3647static void
186c406b
TT
3648create_exception_master_breakpoint (void)
3649{
3650 struct objfile *objfile;
17450429 3651 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3652
3653 ALL_OBJFILES (objfile)
3654 {
17450429
PP
3655 struct breakpoint *b;
3656 struct gdbarch *gdbarch;
3657 struct breakpoint_objfile_data *bp_objfile_data;
3658 CORE_ADDR addr;
67994074 3659 struct explicit_location explicit_loc;
17450429
PP
3660
3661 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3662
28106bc2
SDJ
3663 /* We prefer the SystemTap probe point if it exists. */
3664 if (!bp_objfile_data->exception_searched)
3665 {
25f9533e
SDJ
3666 VEC (probe_p) *ret;
3667
3668 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3669
3670 if (ret != NULL)
3671 {
3672 /* We are only interested in checking one element. */
3673 struct probe *p = VEC_index (probe_p, ret, 0);
3674
3675 if (!can_evaluate_probe_arguments (p))
3676 {
3677 /* We cannot use the probe interface here, because it does
3678 not know how to evaluate arguments. */
3679 VEC_free (probe_p, ret);
3680 ret = NULL;
3681 }
3682 }
3683 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3684 bp_objfile_data->exception_searched = 1;
3685 }
3686
3687 if (bp_objfile_data->exception_probes != NULL)
3688 {
3689 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3690 int i;
3691 struct probe *probe;
3692
3693 for (i = 0;
3694 VEC_iterate (probe_p,
3695 bp_objfile_data->exception_probes,
3696 i, probe);
3697 ++i)
3698 {
3699 struct breakpoint *b;
3700
729662a5
TT
3701 b = create_internal_breakpoint (gdbarch,
3702 get_probe_address (probe,
3703 objfile),
28106bc2
SDJ
3704 bp_exception_master,
3705 &internal_breakpoint_ops);
5b56227b
KS
3706 b->location
3707 = new_probe_location ("-probe-stap libgcc:unwind");
28106bc2
SDJ
3708 b->enable_state = bp_disabled;
3709 }
3710
3711 continue;
3712 }
3713
3714 /* Otherwise, try the hook function. */
3715
3b7344d5 3716 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3717 continue;
3718
3719 gdbarch = get_objfile_arch (objfile);
186c406b 3720
3b7344d5 3721 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3722 {
3b7344d5 3723 struct bound_minimal_symbol debug_hook;
186c406b 3724
17450429 3725 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3726 if (debug_hook.minsym == NULL)
17450429 3727 {
3b7344d5 3728 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3729 continue;
3730 }
3731
3732 bp_objfile_data->exception_msym = debug_hook;
186c406b 3733 }
17450429 3734
77e371c0 3735 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3736 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3737 &current_target);
06edf0c0
PA
3738 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3739 &internal_breakpoint_ops);
67994074
KS
3740 initialize_explicit_location (&explicit_loc);
3741 explicit_loc.function_name = ASTRDUP (func_name);
3742 b->location = new_explicit_location (&explicit_loc);
17450429 3743 b->enable_state = bp_disabled;
186c406b
TT
3744 }
3745
44702360 3746 update_global_location_list (UGLL_MAY_INSERT);
186c406b
TT
3747}
3748
c906108c 3749void
fba45db2 3750update_breakpoints_after_exec (void)
c906108c 3751{
35df4500 3752 struct breakpoint *b, *b_tmp;
876fa593 3753 struct bp_location *bploc, **bplocp_tmp;
c906108c 3754
25b22b0a
PA
3755 /* We're about to delete breakpoints from GDB's lists. If the
3756 INSERTED flag is true, GDB will try to lift the breakpoints by
3757 writing the breakpoints' "shadow contents" back into memory. The
3758 "shadow contents" are NOT valid after an exec, so GDB should not
3759 do that. Instead, the target is responsible from marking
3760 breakpoints out as soon as it detects an exec. We don't do that
3761 here instead, because there may be other attempts to delete
3762 breakpoints after detecting an exec and before reaching here. */
876fa593 3763 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3764 if (bploc->pspace == current_program_space)
3765 gdb_assert (!bploc->inserted);
c906108c 3766
35df4500 3767 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3768 {
6c95b8df
PA
3769 if (b->pspace != current_program_space)
3770 continue;
3771
4a64f543 3772 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3773 if (b->type == bp_shlib_event)
3774 {
3775 delete_breakpoint (b);
3776 continue;
3777 }
c906108c 3778
4a64f543 3779 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3780 if (b->type == bp_jit_event)
3781 {
3782 delete_breakpoint (b);
3783 continue;
3784 }
3785
1900040c 3786 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3787 as must overlay event and longjmp master breakpoints. */
3788 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3789 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3790 || b->type == bp_exception_master)
c4093a6a
JM
3791 {
3792 delete_breakpoint (b);
3793 continue;
3794 }
3795
4a64f543 3796 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3797 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3798 {
3799 delete_breakpoint (b);
3800 continue;
3801 }
3802
7c16b83e
PA
3803 /* Just like single-step breakpoints. */
3804 if (b->type == bp_single_step)
3805 {
3806 delete_breakpoint (b);
3807 continue;
3808 }
3809
611c83ae
PA
3810 /* Longjmp and longjmp-resume breakpoints are also meaningless
3811 after an exec. */
186c406b 3812 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3813 || b->type == bp_longjmp_call_dummy
186c406b 3814 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3815 {
3816 delete_breakpoint (b);
3817 continue;
3818 }
3819
ce78b96d
JB
3820 if (b->type == bp_catchpoint)
3821 {
3822 /* For now, none of the bp_catchpoint breakpoints need to
3823 do anything at this point. In the future, if some of
3824 the catchpoints need to something, we will need to add
3825 a new method, and call this method from here. */
3826 continue;
3827 }
3828
c5aa993b
JM
3829 /* bp_finish is a special case. The only way we ought to be able
3830 to see one of these when an exec() has happened, is if the user
3831 caught a vfork, and then said "finish". Ordinarily a finish just
3832 carries them to the call-site of the current callee, by setting
3833 a temporary bp there and resuming. But in this case, the finish
3834 will carry them entirely through the vfork & exec.
3835
3836 We don't want to allow a bp_finish to remain inserted now. But
3837 we can't safely delete it, 'cause finish_command has a handle to
3838 the bp on a bpstat, and will later want to delete it. There's a
3839 chance (and I've seen it happen) that if we delete the bp_finish
3840 here, that its storage will get reused by the time finish_command
3841 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3842 We really must allow finish_command to delete a bp_finish.
3843
e5dd4106 3844 In the absence of a general solution for the "how do we know
53a5351d
JM
3845 it's safe to delete something others may have handles to?"
3846 problem, what we'll do here is just uninsert the bp_finish, and
3847 let finish_command delete it.
3848
3849 (We know the bp_finish is "doomed" in the sense that it's
3850 momentary, and will be deleted as soon as finish_command sees
3851 the inferior stopped. So it doesn't matter that the bp's
3852 address is probably bogus in the new a.out, unlike e.g., the
3853 solib breakpoints.) */
c5aa993b 3854
c5aa993b
JM
3855 if (b->type == bp_finish)
3856 {
3857 continue;
3858 }
3859
3860 /* Without a symbolic address, we have little hope of the
3861 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3862 a.out. */
f00aae0f 3863 if (event_location_empty_p (b->location))
c5aa993b
JM
3864 {
3865 delete_breakpoint (b);
3866 continue;
3867 }
c5aa993b 3868 }
c906108c
SS
3869}
3870
3871int
d80ee84f 3872detach_breakpoints (ptid_t ptid)
c906108c 3873{
35df4500 3874 struct bp_location *bl, **blp_tmp;
3a1bae8e 3875 int val = 0;
ce696e05 3876 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3877 struct inferior *inf = current_inferior ();
c5aa993b 3878
dfd4cc63 3879 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3880 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3881
6c95b8df 3882 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3883 inferior_ptid = ptid;
35df4500 3884 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3885 {
35df4500 3886 if (bl->pspace != inf->pspace)
6c95b8df
PA
3887 continue;
3888
bd9673a4
PW
3889 /* This function must physically remove breakpoints locations
3890 from the specified ptid, without modifying the breakpoint
3891 package's state. Locations of type bp_loc_other are only
3892 maintained at GDB side. So, there is no need to remove
3893 these bp_loc_other locations. Moreover, removing these
3894 would modify the breakpoint package's state. */
3895 if (bl->loc_type == bp_loc_other)
3896 continue;
3897
35df4500
TJB
3898 if (bl->inserted)
3899 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3900 }
d03285ec 3901
ce696e05 3902 do_cleanups (old_chain);
3a1bae8e 3903 return val;
c906108c
SS
3904}
3905
35df4500 3906/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3907 Note that this is used to detach breakpoints from a child fork.
3908 When we get here, the child isn't in the inferior list, and neither
3909 do we have objects to represent its address space --- we should
35df4500 3910 *not* look at bl->pspace->aspace here. */
6c95b8df 3911
c906108c 3912static int
35df4500 3913remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3914{
3915 int val;
c5aa993b 3916
35df4500
TJB
3917 /* BL is never in moribund_locations by our callers. */
3918 gdb_assert (bl->owner != NULL);
2bdf28a0 3919
74960c60
VP
3920 /* The type of none suggests that owner is actually deleted.
3921 This should not ever happen. */
35df4500 3922 gdb_assert (bl->owner->type != bp_none);
0bde7532 3923
35df4500
TJB
3924 if (bl->loc_type == bp_loc_software_breakpoint
3925 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3926 {
c02f5703
MS
3927 /* "Normal" instruction breakpoint: either the standard
3928 trap-instruction bp (bp_breakpoint), or a
3929 bp_hardware_breakpoint. */
3930
3931 /* First check to see if we have to handle an overlay. */
3932 if (overlay_debugging == ovly_off
35df4500
TJB
3933 || bl->section == NULL
3934 || !(section_is_overlay (bl->section)))
c02f5703
MS
3935 {
3936 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3937
3938 /* If we're trying to uninsert a memory breakpoint that we
3939 know is set in a dynamic object that is marked
3940 shlib_disabled, then either the dynamic object was
3941 removed with "remove-symbol-file" or with
3942 "nosharedlibrary". In the former case, we don't know
3943 whether another dynamic object might have loaded over the
3944 breakpoint's address -- the user might well let us know
3945 about it next with add-symbol-file (the whole point of
d03de421 3946 add-symbol-file is letting the user manually maintain a
08351840
PA
3947 list of dynamically loaded objects). If we have the
3948 breakpoint's shadow memory, that is, this is a software
3949 breakpoint managed by GDB, check whether the breakpoint
3950 is still inserted in memory, to avoid overwriting wrong
3951 code with stale saved shadow contents. Note that HW
3952 breakpoints don't have shadow memory, as they're
3953 implemented using a mechanism that is not dependent on
3954 being able to modify the target's memory, and as such
3955 they should always be removed. */
3956 if (bl->shlib_disabled
3957 && bl->target_info.shadow_len != 0
3958 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3959 val = 0;
3960 else
3961 val = bl->owner->ops->remove_location (bl);
c02f5703 3962 }
c906108c
SS
3963 else
3964 {
4a64f543 3965 /* This breakpoint is in an overlay section.
c02f5703
MS
3966 Did we set a breakpoint at the LMA? */
3967 if (!overlay_events_enabled)
3968 {
3969 /* Yes -- overlay event support is not active, so we
3970 should have set a breakpoint at the LMA. Remove it.
3971 */
c02f5703
MS
3972 /* Ignore any failures: if the LMA is in ROM, we will
3973 have already warned when we failed to insert it. */
35df4500
TJB
3974 if (bl->loc_type == bp_loc_hardware_breakpoint)
3975 target_remove_hw_breakpoint (bl->gdbarch,
3976 &bl->overlay_target_info);
c02f5703 3977 else
35df4500
TJB
3978 target_remove_breakpoint (bl->gdbarch,
3979 &bl->overlay_target_info);
c02f5703
MS
3980 }
3981 /* Did we set a breakpoint at the VMA?
3982 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3983 if (bl->inserted)
c906108c 3984 {
c02f5703
MS
3985 /* Yes -- remove it. Previously we did not bother to
3986 remove the breakpoint if the section had been
3987 unmapped, but let's not rely on that being safe. We
3988 don't know what the overlay manager might do. */
aa67235e
UW
3989
3990 /* However, we should remove *software* breakpoints only
3991 if the section is still mapped, or else we overwrite
3992 wrong code with the saved shadow contents. */
348d480f
PA
3993 if (bl->loc_type == bp_loc_hardware_breakpoint
3994 || section_is_mapped (bl->section))
3995 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3996 else
3997 val = 0;
c906108c 3998 }
c02f5703
MS
3999 else
4000 {
4001 /* No -- not inserted, so no need to remove. No error. */
4002 val = 0;
4003 }
c906108c 4004 }
879d1e6b 4005
08351840
PA
4006 /* In some cases, we might not be able to remove a breakpoint in
4007 a shared library that has already been removed, but we have
4008 not yet processed the shlib unload event. Similarly for an
4009 unloaded add-symbol-file object - the user might not yet have
4010 had the chance to remove-symbol-file it. shlib_disabled will
4011 be set if the library/object has already been removed, but
4012 the breakpoint hasn't been uninserted yet, e.g., after
4013 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4014 always-inserted mode. */
076855f9 4015 if (val
08351840
PA
4016 && (bl->loc_type == bp_loc_software_breakpoint
4017 && (bl->shlib_disabled
4018 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
4019 || shared_objfile_contains_address_p (bl->pspace,
4020 bl->address))))
879d1e6b
UW
4021 val = 0;
4022
c906108c
SS
4023 if (val)
4024 return val;
35df4500 4025 bl->inserted = (is == mark_inserted);
c906108c 4026 }
35df4500 4027 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4028 {
77b06cd7
TJB
4029 gdb_assert (bl->owner->ops != NULL
4030 && bl->owner->ops->remove_location != NULL);
4031
35df4500 4032 bl->inserted = (is == mark_inserted);
77b06cd7 4033 bl->owner->ops->remove_location (bl);
2e70b7b9 4034
c906108c 4035 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 4036 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4037 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4038 bl->owner->number);
c906108c 4039 }
35df4500
TJB
4040 else if (bl->owner->type == bp_catchpoint
4041 && breakpoint_enabled (bl->owner)
4042 && !bl->duplicate)
ce78b96d 4043 {
77b06cd7
TJB
4044 gdb_assert (bl->owner->ops != NULL
4045 && bl->owner->ops->remove_location != NULL);
ce78b96d 4046
77b06cd7 4047 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4048 if (val)
4049 return val;
77b06cd7 4050
35df4500 4051 bl->inserted = (is == mark_inserted);
ce78b96d 4052 }
c906108c
SS
4053
4054 return 0;
4055}
4056
6c95b8df 4057static int
35df4500 4058remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4059{
4060 int ret;
4061 struct cleanup *old_chain;
4062
35df4500
TJB
4063 /* BL is never in moribund_locations by our callers. */
4064 gdb_assert (bl->owner != NULL);
2bdf28a0 4065
6c95b8df
PA
4066 /* The type of none suggests that owner is actually deleted.
4067 This should not ever happen. */
35df4500 4068 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4069
4070 old_chain = save_current_space_and_thread ();
4071
35df4500 4072 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4073
35df4500 4074 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4075
4076 do_cleanups (old_chain);
4077 return ret;
4078}
4079
c906108c
SS
4080/* Clear the "inserted" flag in all breakpoints. */
4081
25b22b0a 4082void
fba45db2 4083mark_breakpoints_out (void)
c906108c 4084{
35df4500 4085 struct bp_location *bl, **blp_tmp;
c906108c 4086
35df4500 4087 ALL_BP_LOCATIONS (bl, blp_tmp)
66c4b3e8 4088 if (bl->pspace == current_program_space)
35df4500 4089 bl->inserted = 0;
c906108c
SS
4090}
4091
53a5351d
JM
4092/* Clear the "inserted" flag in all breakpoints and delete any
4093 breakpoints which should go away between runs of the program.
c906108c
SS
4094
4095 Plus other such housekeeping that has to be done for breakpoints
4096 between runs.
4097
53a5351d
JM
4098 Note: this function gets called at the end of a run (by
4099 generic_mourn_inferior) and when a run begins (by
4a64f543 4100 init_wait_for_inferior). */
c906108c
SS
4101
4102
4103
4104void
fba45db2 4105breakpoint_init_inferior (enum inf_context context)
c906108c 4106{
35df4500
TJB
4107 struct breakpoint *b, *b_tmp;
4108 struct bp_location *bl, **blp_tmp;
1c5cfe86 4109 int ix;
6c95b8df 4110 struct program_space *pspace = current_program_space;
c906108c 4111
50c71eaf
PA
4112 /* If breakpoint locations are shared across processes, then there's
4113 nothing to do. */
f5656ead 4114 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4115 return;
4116
1a853c52 4117 mark_breakpoints_out ();
075f6582 4118
35df4500 4119 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4120 {
6c95b8df
PA
4121 if (b->loc && b->loc->pspace != pspace)
4122 continue;
4123
c5aa993b
JM
4124 switch (b->type)
4125 {
4126 case bp_call_dummy:
e2e4d78b 4127 case bp_longjmp_call_dummy:
c906108c 4128
c5aa993b 4129 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4130 cause problems when the inferior is rerun, so we better get
4131 rid of it. */
4132
4133 case bp_watchpoint_scope:
4134
4135 /* Also get rid of scope breakpoints. */
4136
4137 case bp_shlib_event:
4138
4139 /* Also remove solib event breakpoints. Their addresses may
4140 have changed since the last time we ran the program.
4141 Actually we may now be debugging against different target;
4142 and so the solib backend that installed this breakpoint may
4143 not be used in by the target. E.g.,
4144
4145 (gdb) file prog-linux
4146 (gdb) run # native linux target
4147 ...
4148 (gdb) kill
4149 (gdb) file prog-win.exe
4150 (gdb) tar rem :9999 # remote Windows gdbserver.
4151 */
c906108c 4152
f59f708a
PA
4153 case bp_step_resume:
4154
4155 /* Also remove step-resume breakpoints. */
4156
7c16b83e
PA
4157 case bp_single_step:
4158
4159 /* Also remove single-step breakpoints. */
4160
c5aa993b
JM
4161 delete_breakpoint (b);
4162 break;
c906108c 4163
c5aa993b
JM
4164 case bp_watchpoint:
4165 case bp_hardware_watchpoint:
4166 case bp_read_watchpoint:
4167 case bp_access_watchpoint:
3a5c3e22
PA
4168 {
4169 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4170
3a5c3e22
PA
4171 /* Likewise for watchpoints on local expressions. */
4172 if (w->exp_valid_block != NULL)
4173 delete_breakpoint (b);
4174 else if (context == inf_starting)
4175 {
4176 /* Reset val field to force reread of starting value in
4177 insert_breakpoints. */
4178 if (w->val)
4179 value_free (w->val);
4180 w->val = NULL;
4181 w->val_valid = 0;
c860120c 4182 }
3a5c3e22 4183 }
c5aa993b
JM
4184 break;
4185 default:
c5aa993b
JM
4186 break;
4187 }
4188 }
1c5cfe86
PA
4189
4190 /* Get rid of the moribund locations. */
35df4500
TJB
4191 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4192 decref_bp_location (&bl);
1c5cfe86 4193 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4194}
4195
6c95b8df
PA
4196/* These functions concern about actual breakpoints inserted in the
4197 target --- to e.g. check if we need to do decr_pc adjustment or if
4198 we need to hop over the bkpt --- so we check for address space
4199 match, not program space. */
4200
c2c6d25f
JM
4201/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4202 exists at PC. It returns ordinary_breakpoint_here if it's an
4203 ordinary breakpoint, or permanent_breakpoint_here if it's a
4204 permanent breakpoint.
4205 - When continuing from a location with an ordinary breakpoint, we
4206 actually single step once before calling insert_breakpoints.
e5dd4106 4207 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4208 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4209 the target, to advance the PC past the breakpoint. */
c906108c 4210
c2c6d25f 4211enum breakpoint_here
6c95b8df 4212breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4213{
35df4500 4214 struct bp_location *bl, **blp_tmp;
c2c6d25f 4215 int any_breakpoint_here = 0;
c906108c 4216
35df4500 4217 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4218 {
35df4500
TJB
4219 if (bl->loc_type != bp_loc_software_breakpoint
4220 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4221 continue;
4222
f1310107 4223 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500 4224 if ((breakpoint_enabled (bl->owner)
1a853c52 4225 || bl->permanent)
f1310107 4226 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4227 {
4228 if (overlay_debugging
35df4500
TJB
4229 && section_is_overlay (bl->section)
4230 && !section_is_mapped (bl->section))
075f6582 4231 continue; /* unmapped overlay -- can't be a match */
1a853c52 4232 else if (bl->permanent)
075f6582
DJ
4233 return permanent_breakpoint_here;
4234 else
4235 any_breakpoint_here = 1;
4236 }
4237 }
c906108c 4238
f486487f 4239 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
c906108c
SS
4240}
4241
1c5cfe86
PA
4242/* Return true if there's a moribund breakpoint at PC. */
4243
4244int
6c95b8df 4245moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4246{
4247 struct bp_location *loc;
4248 int ix;
4249
4250 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4251 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4252 return 1;
4253
4254 return 0;
4255}
c2c6d25f 4256
f7ce857f
PA
4257/* Returns non-zero iff BL is inserted at PC, in address space
4258 ASPACE. */
4259
4260static int
4261bp_location_inserted_here_p (struct bp_location *bl,
4262 struct address_space *aspace, CORE_ADDR pc)
4263{
4264 if (bl->inserted
4265 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4266 aspace, pc))
4267 {
4268 if (overlay_debugging
4269 && section_is_overlay (bl->section)
4270 && !section_is_mapped (bl->section))
4271 return 0; /* unmapped overlay -- can't be a match */
4272 else
4273 return 1;
4274 }
4275 return 0;
4276}
4277
a1fd2fa5 4278/* Returns non-zero iff there's a breakpoint inserted at PC. */
c906108c
SS
4279
4280int
a1fd2fa5 4281breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4282{
f7ce857f
PA
4283 struct bp_location **blp, **blp_tmp = NULL;
4284 struct bp_location *bl;
c906108c 4285
f7ce857f 4286 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
c5aa993b 4287 {
f7ce857f
PA
4288 struct bp_location *bl = *blp;
4289
35df4500
TJB
4290 if (bl->loc_type != bp_loc_software_breakpoint
4291 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4292 continue;
4293
f7ce857f
PA
4294 if (bp_location_inserted_here_p (bl, aspace, pc))
4295 return 1;
c5aa993b 4296 }
c36b740a
VP
4297 return 0;
4298}
4299
a1fd2fa5
PA
4300/* This function returns non-zero iff there is a software breakpoint
4301 inserted at PC. */
c36b740a
VP
4302
4303int
a1fd2fa5
PA
4304software_breakpoint_inserted_here_p (struct address_space *aspace,
4305 CORE_ADDR pc)
4fa8626c 4306{
f7ce857f
PA
4307 struct bp_location **blp, **blp_tmp = NULL;
4308 struct bp_location *bl;
4fa8626c 4309
f7ce857f 4310 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4fa8626c 4311 {
f7ce857f
PA
4312 struct bp_location *bl = *blp;
4313
35df4500 4314 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4315 continue;
4316
f7ce857f
PA
4317 if (bp_location_inserted_here_p (bl, aspace, pc))
4318 return 1;
4fa8626c
DJ
4319 }
4320
4321 return 0;
9c02b525
PA
4322}
4323
4324/* See breakpoint.h. */
4325
4326int
4327hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4328 CORE_ADDR pc)
4329{
4330 struct bp_location **blp, **blp_tmp = NULL;
4331 struct bp_location *bl;
4332
4333 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4334 {
4335 struct bp_location *bl = *blp;
4336
4337 if (bl->loc_type != bp_loc_hardware_breakpoint)
4338 continue;
4339
4340 if (bp_location_inserted_here_p (bl, aspace, pc))
4341 return 1;
4342 }
4343
4344 return 0;
4fa8626c
DJ
4345}
4346
9093389c
PA
4347int
4348hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4349 CORE_ADDR addr, ULONGEST len)
4350{
4351 struct breakpoint *bpt;
4352
4353 ALL_BREAKPOINTS (bpt)
4354 {
4355 struct bp_location *loc;
4356
4357 if (bpt->type != bp_hardware_watchpoint
4358 && bpt->type != bp_access_watchpoint)
4359 continue;
4360
4361 if (!breakpoint_enabled (bpt))
4362 continue;
4363
4364 for (loc = bpt->loc; loc; loc = loc->next)
4365 if (loc->pspace->aspace == aspace && loc->inserted)
4366 {
4367 CORE_ADDR l, h;
4368
4369 /* Check for intersection. */
4370 l = max (loc->address, addr);
4371 h = min (loc->address + loc->length, addr + len);
4372 if (l < h)
4373 return 1;
4374 }
4375 }
4376 return 0;
4377}
c906108c 4378\f
c5aa993b 4379
c906108c
SS
4380/* bpstat stuff. External routines' interfaces are documented
4381 in breakpoint.h. */
4382
4383int
c326b90e 4384is_catchpoint (struct breakpoint *ep)
c906108c 4385{
533be4dd 4386 return (ep->type == bp_catchpoint);
c906108c
SS
4387}
4388
f431efe5
PA
4389/* Frees any storage that is part of a bpstat. Does not walk the
4390 'next' chain. */
4391
4392static void
198757a8
VP
4393bpstat_free (bpstat bs)
4394{
4395 if (bs->old_val != NULL)
4396 value_free (bs->old_val);
9add0f1b 4397 decref_counted_command_line (&bs->commands);
f431efe5 4398 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4399 xfree (bs);
4400}
4401
c906108c
SS
4402/* Clear a bpstat so that it says we are not at any breakpoint.
4403 Also free any storage that is part of a bpstat. */
4404
4405void
fba45db2 4406bpstat_clear (bpstat *bsp)
c906108c
SS
4407{
4408 bpstat p;
4409 bpstat q;
4410
4411 if (bsp == 0)
4412 return;
4413 p = *bsp;
4414 while (p != NULL)
4415 {
4416 q = p->next;
198757a8 4417 bpstat_free (p);
c906108c
SS
4418 p = q;
4419 }
4420 *bsp = NULL;
4421}
4422
4423/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4424 is part of the bpstat is copied as well. */
4425
4426bpstat
fba45db2 4427bpstat_copy (bpstat bs)
c906108c
SS
4428{
4429 bpstat p = NULL;
4430 bpstat tmp;
4431 bpstat retval = NULL;
4432
4433 if (bs == NULL)
4434 return bs;
4435
4436 for (; bs != NULL; bs = bs->next)
4437 {
4438 tmp = (bpstat) xmalloc (sizeof (*tmp));
4439 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4440 incref_counted_command_line (tmp->commands);
f431efe5 4441 incref_bp_location (tmp->bp_location_at);
31cc81e9 4442 if (bs->old_val != NULL)
3c3185ac
JK
4443 {
4444 tmp->old_val = value_copy (bs->old_val);
4445 release_value (tmp->old_val);
4446 }
31cc81e9 4447
c906108c
SS
4448 if (p == NULL)
4449 /* This is the first thing in the chain. */
4450 retval = tmp;
4451 else
4452 p->next = tmp;
4453 p = tmp;
4454 }
4455 p->next = NULL;
4456 return retval;
4457}
4458
4a64f543 4459/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4460
4461bpstat
fba45db2 4462bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4463{
c5aa993b
JM
4464 if (bsp == NULL)
4465 return NULL;
c906108c 4466
c5aa993b
JM
4467 for (; bsp != NULL; bsp = bsp->next)
4468 {
f431efe5 4469 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4470 return bsp;
4471 }
c906108c
SS
4472 return NULL;
4473}
4474
ab04a2af
TT
4475/* See breakpoint.h. */
4476
47591c29 4477int
427cd150 4478bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4479{
ab04a2af
TT
4480 for (; bsp != NULL; bsp = bsp->next)
4481 {
427cd150
TT
4482 if (bsp->breakpoint_at == NULL)
4483 {
4484 /* A moribund location can never explain a signal other than
4485 GDB_SIGNAL_TRAP. */
4486 if (sig == GDB_SIGNAL_TRAP)
47591c29 4487 return 1;
427cd150
TT
4488 }
4489 else
47591c29
PA
4490 {
4491 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4492 sig))
4493 return 1;
4494 }
ab04a2af
TT
4495 }
4496
47591c29 4497 return 0;
ab04a2af
TT
4498}
4499
4a64f543
MS
4500/* Put in *NUM the breakpoint number of the first breakpoint we are
4501 stopped at. *BSP upon return is a bpstat which points to the
4502 remaining breakpoints stopped at (but which is not guaranteed to be
4503 good for anything but further calls to bpstat_num).
4504
8671a17b
PA
4505 Return 0 if passed a bpstat which does not indicate any breakpoints.
4506 Return -1 if stopped at a breakpoint that has been deleted since
4507 we set it.
4508 Return 1 otherwise. */
c906108c
SS
4509
4510int
8671a17b 4511bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4512{
4513 struct breakpoint *b;
4514
4515 if ((*bsp) == NULL)
4516 return 0; /* No more breakpoint values */
8671a17b 4517
4a64f543
MS
4518 /* We assume we'll never have several bpstats that correspond to a
4519 single breakpoint -- otherwise, this function might return the
4520 same number more than once and this will look ugly. */
f431efe5 4521 b = (*bsp)->breakpoint_at;
8671a17b
PA
4522 *bsp = (*bsp)->next;
4523 if (b == NULL)
4524 return -1; /* breakpoint that's been deleted since */
4525
4526 *num = b->number; /* We have its number */
4527 return 1;
c906108c
SS
4528}
4529
e93ca019 4530/* See breakpoint.h. */
c906108c
SS
4531
4532void
e93ca019 4533bpstat_clear_actions (void)
c906108c 4534{
e93ca019
JK
4535 struct thread_info *tp;
4536 bpstat bs;
4537
4538 if (ptid_equal (inferior_ptid, null_ptid))
4539 return;
4540
4541 tp = find_thread_ptid (inferior_ptid);
4542 if (tp == NULL)
4543 return;
4544
4545 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4546 {
9add0f1b 4547 decref_counted_command_line (&bs->commands);
abf85f46 4548
c906108c
SS
4549 if (bs->old_val != NULL)
4550 {
4551 value_free (bs->old_val);
4552 bs->old_val = NULL;
4553 }
4554 }
4555}
4556
f3b1572e
PA
4557/* Called when a command is about to proceed the inferior. */
4558
4559static void
4560breakpoint_about_to_proceed (void)
4561{
4562 if (!ptid_equal (inferior_ptid, null_ptid))
4563 {
4564 struct thread_info *tp = inferior_thread ();
4565
4566 /* Allow inferior function calls in breakpoint commands to not
4567 interrupt the command list. When the call finishes
4568 successfully, the inferior will be standing at the same
4569 breakpoint as if nothing happened. */
16c381f0 4570 if (tp->control.in_infcall)
f3b1572e
PA
4571 return;
4572 }
4573
4574 breakpoint_proceeded = 1;
4575}
4576
4a64f543
MS
4577/* Stub for cleaning up our state if we error-out of a breakpoint
4578 command. */
c906108c 4579static void
4efb68b1 4580cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4581{
4582 executing_breakpoint_commands = 0;
4583}
4584
abf85f46
JK
4585/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4586 or its equivalent. */
4587
4588static int
4589command_line_is_silent (struct command_line *cmd)
4590{
4f45d445 4591 return cmd && (strcmp ("silent", cmd->line) == 0);
abf85f46
JK
4592}
4593
4a64f543
MS
4594/* Execute all the commands associated with all the breakpoints at
4595 this location. Any of these commands could cause the process to
4596 proceed beyond this point, etc. We look out for such changes by
4597 checking the global "breakpoint_proceeded" after each command.
c906108c 4598
347bddb7
PA
4599 Returns true if a breakpoint command resumed the inferior. In that
4600 case, it is the caller's responsibility to recall it again with the
4601 bpstat of the current thread. */
4602
4603static int
4604bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4605{
4606 bpstat bs;
4607 struct cleanup *old_chain;
347bddb7 4608 int again = 0;
c906108c
SS
4609
4610 /* Avoid endless recursion if a `source' command is contained
4611 in bs->commands. */
4612 if (executing_breakpoint_commands)
347bddb7 4613 return 0;
c906108c
SS
4614
4615 executing_breakpoint_commands = 1;
4616 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4617
cf6c5ffb
TT
4618 prevent_dont_repeat ();
4619
4a64f543 4620 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4621 bs = *bsp;
4622
4623 breakpoint_proceeded = 0;
4624 for (; bs != NULL; bs = bs->next)
4625 {
9add0f1b 4626 struct counted_command_line *ccmd;
6c50ab1c
JB
4627 struct command_line *cmd;
4628 struct cleanup *this_cmd_tree_chain;
4629
4630 /* Take ownership of the BSP's command tree, if it has one.
4631
4632 The command tree could legitimately contain commands like
4633 'step' and 'next', which call clear_proceed_status, which
4634 frees stop_bpstat's command tree. To make sure this doesn't
4635 free the tree we're executing out from under us, we need to
4636 take ownership of the tree ourselves. Since a given bpstat's
4637 commands are only executed once, we don't need to copy it; we
4638 can clear the pointer in the bpstat, and make sure we free
4639 the tree when we're done. */
9add0f1b
TT
4640 ccmd = bs->commands;
4641 bs->commands = NULL;
abf85f46
JK
4642 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4643 cmd = ccmd ? ccmd->commands : NULL;
4644 if (command_line_is_silent (cmd))
4645 {
4646 /* The action has been already done by bpstat_stop_status. */
4647 cmd = cmd->next;
4648 }
6c50ab1c 4649
c906108c
SS
4650 while (cmd != NULL)
4651 {
4652 execute_control_command (cmd);
4653
4654 if (breakpoint_proceeded)
4655 break;
4656 else
4657 cmd = cmd->next;
4658 }
6c50ab1c
JB
4659
4660 /* We can free this command tree now. */
4661 do_cleanups (this_cmd_tree_chain);
4662
c906108c 4663 if (breakpoint_proceeded)
32c1e744 4664 {
0b333c5e 4665 if (interpreter_async)
347bddb7
PA
4666 /* If we are in async mode, then the target might be still
4667 running, not stopped at any breakpoint, so nothing for
4668 us to do here -- just return to the event loop. */
4669 ;
32c1e744
VP
4670 else
4671 /* In sync mode, when execute_control_command returns
4672 we're already standing on the next breakpoint.
347bddb7
PA
4673 Breakpoint commands for that stop were not run, since
4674 execute_command does not run breakpoint commands --
4675 only command_line_handler does, but that one is not
4676 involved in execution of breakpoint commands. So, we
4677 can now execute breakpoint commands. It should be
4678 noted that making execute_command do bpstat actions is
4679 not an option -- in this case we'll have recursive
4680 invocation of bpstat for each breakpoint with a
4681 command, and can easily blow up GDB stack. Instead, we
4682 return true, which will trigger the caller to recall us
4683 with the new stop_bpstat. */
4684 again = 1;
4685 break;
32c1e744 4686 }
c906108c 4687 }
c2b8ed2c 4688 do_cleanups (old_chain);
347bddb7
PA
4689 return again;
4690}
4691
4692void
4693bpstat_do_actions (void)
4694{
353d1d73
JK
4695 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4696
347bddb7
PA
4697 /* Do any commands attached to breakpoint we are stopped at. */
4698 while (!ptid_equal (inferior_ptid, null_ptid)
4699 && target_has_execution
4700 && !is_exited (inferior_ptid)
4701 && !is_executing (inferior_ptid))
4702 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4703 and only return when it is stopped at the next breakpoint, we
4704 keep doing breakpoint actions until it returns false to
4705 indicate the inferior was not resumed. */
16c381f0 4706 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4707 break;
353d1d73
JK
4708
4709 discard_cleanups (cleanup_if_error);
c906108c
SS
4710}
4711
fa4727a6
DJ
4712/* Print out the (old or new) value associated with a watchpoint. */
4713
4714static void
4715watchpoint_value_print (struct value *val, struct ui_file *stream)
4716{
4717 if (val == NULL)
4718 fprintf_unfiltered (stream, _("<unreadable>"));
4719 else
79a45b7d
TT
4720 {
4721 struct value_print_options opts;
4722 get_user_print_options (&opts);
4723 value_print (val, stream, &opts);
4724 }
fa4727a6
DJ
4725}
4726
e514a9d6 4727/* Generic routine for printing messages indicating why we
4a64f543 4728 stopped. The behavior of this function depends on the value
e514a9d6
JM
4729 'print_it' in the bpstat structure. Under some circumstances we
4730 may decide not to print anything here and delegate the task to
4a64f543 4731 normal_stop(). */
e514a9d6
JM
4732
4733static enum print_stop_action
4734print_bp_stop_message (bpstat bs)
4735{
4736 switch (bs->print_it)
4737 {
4738 case print_it_noop:
4a64f543 4739 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4740 return PRINT_UNKNOWN;
4741 break;
4742
4743 case print_it_done:
4744 /* We still want to print the frame, but we already printed the
4a64f543 4745 relevant messages. */
e514a9d6
JM
4746 return PRINT_SRC_AND_LOC;
4747 break;
4748
4749 case print_it_normal:
4f8d1dc6 4750 {
f431efe5
PA
4751 struct breakpoint *b = bs->breakpoint_at;
4752
1a6a67de
TJB
4753 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4754 which has since been deleted. */
4755 if (b == NULL)
4756 return PRINT_UNKNOWN;
4757
348d480f
PA
4758 /* Normal case. Call the breakpoint's print_it method. */
4759 return b->ops->print_it (bs);
4f8d1dc6 4760 }
348d480f 4761 break;
3086aeae 4762
e514a9d6 4763 default:
8e65ff28 4764 internal_error (__FILE__, __LINE__,
e2e0b3e5 4765 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4766 break;
c906108c 4767 }
c906108c
SS
4768}
4769
edcc5120
TT
4770/* A helper function that prints a shared library stopped event. */
4771
4772static void
4773print_solib_event (int is_catchpoint)
4774{
4775 int any_deleted
4776 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4777 int any_added
4778 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4779
4780 if (!is_catchpoint)
4781 {
4782 if (any_added || any_deleted)
4783 ui_out_text (current_uiout,
4784 _("Stopped due to shared library event:\n"));
4785 else
4786 ui_out_text (current_uiout,
4787 _("Stopped due to shared library event (no "
4788 "libraries added or removed)\n"));
4789 }
4790
4791 if (ui_out_is_mi_like_p (current_uiout))
4792 ui_out_field_string (current_uiout, "reason",
4793 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4794
4795 if (any_deleted)
4796 {
4797 struct cleanup *cleanup;
4798 char *name;
4799 int ix;
4800
4801 ui_out_text (current_uiout, _(" Inferior unloaded "));
4802 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4803 "removed");
4804 for (ix = 0;
4805 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4806 ix, name);
4807 ++ix)
4808 {
4809 if (ix > 0)
4810 ui_out_text (current_uiout, " ");
4811 ui_out_field_string (current_uiout, "library", name);
4812 ui_out_text (current_uiout, "\n");
4813 }
4814
4815 do_cleanups (cleanup);
4816 }
4817
4818 if (any_added)
4819 {
4820 struct so_list *iter;
4821 int ix;
4822 struct cleanup *cleanup;
4823
4824 ui_out_text (current_uiout, _(" Inferior loaded "));
4825 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4826 "added");
4827 for (ix = 0;
4828 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4829 ix, iter);
4830 ++ix)
4831 {
4832 if (ix > 0)
4833 ui_out_text (current_uiout, " ");
4834 ui_out_field_string (current_uiout, "library", iter->so_name);
4835 ui_out_text (current_uiout, "\n");
4836 }
4837
4838 do_cleanups (cleanup);
4839 }
4840}
4841
e514a9d6
JM
4842/* Print a message indicating what happened. This is called from
4843 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4844 list - a list of the eventpoints that caused this stop. KIND is
4845 the target_waitkind for the stopping event. This
e514a9d6
JM
4846 routine calls the generic print routine for printing a message
4847 about reasons for stopping. This will print (for example) the
4848 "Breakpoint n," part of the output. The return value of this
4849 routine is one of:
c906108c 4850
4a64f543 4851 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4852 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4853 code to print the location. An example is
c5aa993b
JM
4854 "Breakpoint 1, " which should be followed by
4855 the location.
917317f4 4856 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4857 to also print the location part of the message.
4858 An example is the catch/throw messages, which
4a64f543 4859 don't require a location appended to the end.
917317f4 4860 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4861 further info to be printed. */
c906108c 4862
917317f4 4863enum print_stop_action
36dfb11c 4864bpstat_print (bpstat bs, int kind)
c906108c 4865{
f486487f 4866 enum print_stop_action val;
c5aa993b 4867
c906108c 4868 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4869 (Currently all watchpoints go on the bpstat whether hit or not.
4870 That probably could (should) be changed, provided care is taken
c906108c 4871 with respect to bpstat_explains_signal). */
e514a9d6
JM
4872 for (; bs; bs = bs->next)
4873 {
4874 val = print_bp_stop_message (bs);
4875 if (val == PRINT_SRC_ONLY
4876 || val == PRINT_SRC_AND_LOC
4877 || val == PRINT_NOTHING)
4878 return val;
4879 }
c906108c 4880
36dfb11c
TT
4881 /* If we had hit a shared library event breakpoint,
4882 print_bp_stop_message would print out this message. If we hit an
4883 OS-level shared library event, do the same thing. */
4884 if (kind == TARGET_WAITKIND_LOADED)
4885 {
edcc5120 4886 print_solib_event (0);
36dfb11c
TT
4887 return PRINT_NOTHING;
4888 }
4889
e514a9d6 4890 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4891 with and nothing was printed. */
917317f4 4892 return PRINT_UNKNOWN;
c906108c
SS
4893}
4894
c42bd95a
DE
4895/* Evaluate the expression EXP and return 1 if value is zero.
4896 This returns the inverse of the condition because it is called
4897 from catch_errors which returns 0 if an exception happened, and if an
4898 exception happens we want execution to stop.
4a64f543 4899 The argument is a "struct expression *" that has been cast to a
c42bd95a 4900 "void *" to make it pass through catch_errors. */
c906108c
SS
4901
4902static int
4efb68b1 4903breakpoint_cond_eval (void *exp)
c906108c 4904{
278cd55f 4905 struct value *mark = value_mark ();
c5aa993b 4906 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4907
c906108c
SS
4908 value_free_to_mark (mark);
4909 return i;
4910}
4911
5760d0ab 4912/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4913
4914static bpstat
5760d0ab 4915bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4916{
4917 bpstat bs;
4918
4919 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4920 bs->next = NULL;
4921 **bs_link_pointer = bs;
4922 *bs_link_pointer = &bs->next;
f431efe5
PA
4923 bs->breakpoint_at = bl->owner;
4924 bs->bp_location_at = bl;
4925 incref_bp_location (bl);
c906108c
SS
4926 /* If the condition is false, etc., don't do the commands. */
4927 bs->commands = NULL;
4928 bs->old_val = NULL;
4929 bs->print_it = print_it_normal;
4930 return bs;
4931}
4932\f
d983da9c
DJ
4933/* The target has stopped with waitstatus WS. Check if any hardware
4934 watchpoints have triggered, according to the target. */
4935
4936int
4937watchpoints_triggered (struct target_waitstatus *ws)
4938{
d92524f1 4939 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4940 CORE_ADDR addr;
4941 struct breakpoint *b;
4942
4943 if (!stopped_by_watchpoint)
4944 {
4945 /* We were not stopped by a watchpoint. Mark all watchpoints
4946 as not triggered. */
4947 ALL_BREAKPOINTS (b)
cc60f2e3 4948 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4949 {
4950 struct watchpoint *w = (struct watchpoint *) b;
4951
4952 w->watchpoint_triggered = watch_triggered_no;
4953 }
d983da9c
DJ
4954
4955 return 0;
4956 }
4957
4958 if (!target_stopped_data_address (&current_target, &addr))
4959 {
4960 /* We were stopped by a watchpoint, but we don't know where.
4961 Mark all watchpoints as unknown. */
4962 ALL_BREAKPOINTS (b)
cc60f2e3 4963 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4964 {
4965 struct watchpoint *w = (struct watchpoint *) b;
4966
4967 w->watchpoint_triggered = watch_triggered_unknown;
4968 }
d983da9c 4969
3c4797ba 4970 return 1;
d983da9c
DJ
4971 }
4972
4973 /* The target could report the data address. Mark watchpoints
4974 affected by this data address as triggered, and all others as not
4975 triggered. */
4976
4977 ALL_BREAKPOINTS (b)
cc60f2e3 4978 if (is_hardware_watchpoint (b))
d983da9c 4979 {
3a5c3e22 4980 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4981 struct bp_location *loc;
d983da9c 4982
3a5c3e22 4983 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4984 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4985 {
3a5c3e22 4986 if (is_masked_watchpoint (b))
9c06b0b4 4987 {
3a5c3e22
PA
4988 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4989 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4990
4991 if (newaddr == start)
4992 {
3a5c3e22 4993 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4994 break;
4995 }
4996 }
4997 /* Exact match not required. Within range is sufficient. */
4998 else if (target_watchpoint_addr_within_range (&current_target,
4999 addr, loc->address,
5000 loc->length))
5001 {
3a5c3e22 5002 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5003 break;
5004 }
5005 }
d983da9c
DJ
5006 }
5007
5008 return 1;
5009}
5010
c906108c
SS
5011/* Possible return values for watchpoint_check (this can't be an enum
5012 because of check_errors). */
5013/* The watchpoint has been deleted. */
5014#define WP_DELETED 1
5015/* The value has changed. */
5016#define WP_VALUE_CHANGED 2
5017/* The value has not changed. */
5018#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5019/* Ignore this watchpoint, no matter if the value changed or not. */
5020#define WP_IGNORE 4
c906108c
SS
5021
5022#define BP_TEMPFLAG 1
5023#define BP_HARDWAREFLAG 2
5024
4a64f543
MS
5025/* Evaluate watchpoint condition expression and check if its value
5026 changed.
553e4c11
JB
5027
5028 P should be a pointer to struct bpstat, but is defined as a void *
5029 in order for this function to be usable with catch_errors. */
c906108c
SS
5030
5031static int
4efb68b1 5032watchpoint_check (void *p)
c906108c
SS
5033{
5034 bpstat bs = (bpstat) p;
3a5c3e22 5035 struct watchpoint *b;
c906108c
SS
5036 struct frame_info *fr;
5037 int within_current_scope;
5038
f431efe5 5039 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5040 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5041 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5042
f6bc2008
PA
5043 /* If this is a local watchpoint, we only want to check if the
5044 watchpoint frame is in scope if the current thread is the thread
5045 that was used to create the watchpoint. */
5046 if (!watchpoint_in_thread_scope (b))
60e1c644 5047 return WP_IGNORE;
f6bc2008 5048
c906108c
SS
5049 if (b->exp_valid_block == NULL)
5050 within_current_scope = 1;
5051 else
5052 {
edb3359d
DJ
5053 struct frame_info *frame = get_current_frame ();
5054 struct gdbarch *frame_arch = get_frame_arch (frame);
5055 CORE_ADDR frame_pc = get_frame_pc (frame);
5056
c9cf6e20 5057 /* stack_frame_destroyed_p() returns a non-zero value if we're
4a64f543
MS
5058 still in the function but the stack frame has already been
5059 invalidated. Since we can't rely on the values of local
5060 variables after the stack has been destroyed, we are treating
5061 the watchpoint in that state as `not changed' without further
5062 checking. Don't mark watchpoints as changed if the current
5063 frame is in an epilogue - even if they are in some other
5064 frame, our view of the stack is likely to be wrong and
5065 frame_find_by_id could error out. */
c9cf6e20 5066 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
60e1c644 5067 return WP_IGNORE;
a0f49112 5068
101dcfbe 5069 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5070 within_current_scope = (fr != NULL);
69fbadd5
DJ
5071
5072 /* If we've gotten confused in the unwinder, we might have
5073 returned a frame that can't describe this variable. */
edb3359d
DJ
5074 if (within_current_scope)
5075 {
5076 struct symbol *function;
5077
5078 function = get_frame_function (fr);
5079 if (function == NULL
5080 || !contained_in (b->exp_valid_block,
5081 SYMBOL_BLOCK_VALUE (function)))
5082 within_current_scope = 0;
5083 }
69fbadd5 5084
edb3359d 5085 if (within_current_scope)
c906108c
SS
5086 /* If we end up stopping, the current frame will get selected
5087 in normal_stop. So this call to select_frame won't affect
5088 the user. */
0f7d239c 5089 select_frame (fr);
c906108c 5090 }
c5aa993b 5091
c906108c
SS
5092 if (within_current_scope)
5093 {
4a64f543
MS
5094 /* We use value_{,free_to_}mark because it could be a *long*
5095 time before we return to the command level and call
5096 free_all_values. We can't call free_all_values because we
5097 might be in the middle of evaluating a function call. */
c906108c 5098
0cf6dd15 5099 int pc = 0;
9c06b0b4 5100 struct value *mark;
fa4727a6
DJ
5101 struct value *new_val;
5102
3a5c3e22 5103 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5104 /* Since we don't know the exact trigger address (from
5105 stopped_data_address), just tell the user we've triggered
5106 a mask watchpoint. */
5107 return WP_VALUE_CHANGED;
5108
5109 mark = value_mark ();
3a1115a0 5110 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5111
bb9d5f81
PP
5112 if (b->val_bitsize != 0)
5113 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5114
4a64f543
MS
5115 /* We use value_equal_contents instead of value_equal because
5116 the latter coerces an array to a pointer, thus comparing just
5117 the address of the array instead of its contents. This is
5118 not what we want. */
fa4727a6 5119 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5120 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5121 {
fa4727a6
DJ
5122 if (new_val != NULL)
5123 {
5124 release_value (new_val);
5125 value_free_to_mark (mark);
5126 }
c906108c
SS
5127 bs->old_val = b->val;
5128 b->val = new_val;
fa4727a6 5129 b->val_valid = 1;
c906108c
SS
5130 return WP_VALUE_CHANGED;
5131 }
5132 else
5133 {
60e1c644 5134 /* Nothing changed. */
c906108c 5135 value_free_to_mark (mark);
c906108c
SS
5136 return WP_VALUE_NOT_CHANGED;
5137 }
5138 }
5139 else
5140 {
79a45e25
PA
5141 struct ui_out *uiout = current_uiout;
5142
c906108c 5143 /* This seems like the only logical thing to do because
c5aa993b
JM
5144 if we temporarily ignored the watchpoint, then when
5145 we reenter the block in which it is valid it contains
5146 garbage (in the case of a function, it may have two
5147 garbage values, one before and one after the prologue).
5148 So we can't even detect the first assignment to it and
5149 watch after that (since the garbage may or may not equal
5150 the first value assigned). */
348d480f
PA
5151 /* We print all the stop information in
5152 breakpoint_ops->print_it, but in this case, by the time we
5153 call breakpoint_ops->print_it this bp will be deleted
5154 already. So we have no choice but print the information
5155 here. */
9dc5e2a9 5156 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5157 ui_out_field_string
5158 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5159 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5160 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5161 ui_out_text (uiout,
5162 " deleted because the program has left the block in\n\
8b93c638 5163which its expression is valid.\n");
4ce44c66 5164
cdac0397 5165 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5166 decref_counted_command_line (&b->base.commands);
d0fb5eae 5167 watchpoint_del_at_next_stop (b);
c906108c
SS
5168
5169 return WP_DELETED;
5170 }
5171}
5172
18a18393 5173/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5174 breakpoint location BL. This function does not check if we should
5175 stop, only if BL explains the stop. */
5176
18a18393 5177static int
6c95b8df 5178bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5179 struct address_space *aspace, CORE_ADDR bp_addr,
5180 const struct target_waitstatus *ws)
18a18393
VP
5181{
5182 struct breakpoint *b = bl->owner;
5183
348d480f 5184 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5185 gdb_assert (b != NULL);
5186
09ac7c10 5187 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5188}
5189
3a5c3e22
PA
5190/* Determine if the watched values have actually changed, and we
5191 should stop. If not, set BS->stop to 0. */
5192
18a18393
VP
5193static void
5194bpstat_check_watchpoint (bpstat bs)
5195{
2bdf28a0 5196 const struct bp_location *bl;
3a5c3e22 5197 struct watchpoint *b;
2bdf28a0
JK
5198
5199 /* BS is built for existing struct breakpoint. */
f431efe5 5200 bl = bs->bp_location_at;
2bdf28a0 5201 gdb_assert (bl != NULL);
3a5c3e22 5202 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5203 gdb_assert (b != NULL);
18a18393 5204
18a18393 5205 {
18a18393
VP
5206 int must_check_value = 0;
5207
3a5c3e22 5208 if (b->base.type == bp_watchpoint)
18a18393
VP
5209 /* For a software watchpoint, we must always check the
5210 watched value. */
5211 must_check_value = 1;
5212 else if (b->watchpoint_triggered == watch_triggered_yes)
5213 /* We have a hardware watchpoint (read, write, or access)
5214 and the target earlier reported an address watched by
5215 this watchpoint. */
5216 must_check_value = 1;
5217 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5218 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5219 /* We were stopped by a hardware watchpoint, but the target could
5220 not report the data address. We must check the watchpoint's
5221 value. Access and read watchpoints are out of luck; without
5222 a data address, we can't figure it out. */
5223 must_check_value = 1;
3a5c3e22 5224
18a18393
VP
5225 if (must_check_value)
5226 {
3e43a32a
MS
5227 char *message
5228 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5229 b->base.number);
18a18393
VP
5230 struct cleanup *cleanups = make_cleanup (xfree, message);
5231 int e = catch_errors (watchpoint_check, bs, message,
5232 RETURN_MASK_ALL);
5233 do_cleanups (cleanups);
5234 switch (e)
5235 {
5236 case WP_DELETED:
5237 /* We've already printed what needs to be printed. */
5238 bs->print_it = print_it_done;
5239 /* Stop. */
5240 break;
60e1c644
PA
5241 case WP_IGNORE:
5242 bs->print_it = print_it_noop;
5243 bs->stop = 0;
5244 break;
18a18393 5245 case WP_VALUE_CHANGED:
3a5c3e22 5246 if (b->base.type == bp_read_watchpoint)
18a18393 5247 {
85d721b8
PA
5248 /* There are two cases to consider here:
5249
4a64f543 5250 1. We're watching the triggered memory for reads.
85d721b8
PA
5251 In that case, trust the target, and always report
5252 the watchpoint hit to the user. Even though
5253 reads don't cause value changes, the value may
5254 have changed since the last time it was read, and
5255 since we're not trapping writes, we will not see
5256 those, and as such we should ignore our notion of
5257 old value.
5258
4a64f543 5259 2. We're watching the triggered memory for both
85d721b8
PA
5260 reads and writes. There are two ways this may
5261 happen:
5262
4a64f543 5263 2.1. This is a target that can't break on data
85d721b8
PA
5264 reads only, but can break on accesses (reads or
5265 writes), such as e.g., x86. We detect this case
5266 at the time we try to insert read watchpoints.
5267
4a64f543 5268 2.2. Otherwise, the target supports read
85d721b8
PA
5269 watchpoints, but, the user set an access or write
5270 watchpoint watching the same memory as this read
5271 watchpoint.
5272
5273 If we're watching memory writes as well as reads,
5274 ignore watchpoint hits when we find that the
5275 value hasn't changed, as reads don't cause
5276 changes. This still gives false positives when
5277 the program writes the same value to memory as
5278 what there was already in memory (we will confuse
5279 it for a read), but it's much better than
5280 nothing. */
5281
5282 int other_write_watchpoint = 0;
5283
5284 if (bl->watchpoint_type == hw_read)
5285 {
5286 struct breakpoint *other_b;
5287
5288 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5289 if (other_b->type == bp_hardware_watchpoint
5290 || other_b->type == bp_access_watchpoint)
85d721b8 5291 {
3a5c3e22
PA
5292 struct watchpoint *other_w =
5293 (struct watchpoint *) other_b;
5294
5295 if (other_w->watchpoint_triggered
5296 == watch_triggered_yes)
5297 {
5298 other_write_watchpoint = 1;
5299 break;
5300 }
85d721b8
PA
5301 }
5302 }
5303
5304 if (other_write_watchpoint
5305 || bl->watchpoint_type == hw_access)
5306 {
5307 /* We're watching the same memory for writes,
5308 and the value changed since the last time we
5309 updated it, so this trap must be for a write.
5310 Ignore it. */
5311 bs->print_it = print_it_noop;
5312 bs->stop = 0;
5313 }
18a18393
VP
5314 }
5315 break;
5316 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5317 if (b->base.type == bp_hardware_watchpoint
5318 || b->base.type == bp_watchpoint)
18a18393
VP
5319 {
5320 /* Don't stop: write watchpoints shouldn't fire if
5321 the value hasn't changed. */
5322 bs->print_it = print_it_noop;
5323 bs->stop = 0;
5324 }
5325 /* Stop. */
5326 break;
5327 default:
5328 /* Can't happen. */
5329 case 0:
5330 /* Error from catch_errors. */
3a5c3e22 5331 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5332 watchpoint_del_at_next_stop (b);
18a18393
VP
5333 /* We've already printed what needs to be printed. */
5334 bs->print_it = print_it_done;
5335 break;
5336 }
5337 }
5338 else /* must_check_value == 0 */
5339 {
5340 /* This is a case where some watchpoint(s) triggered, but
5341 not at the address of this watchpoint, or else no
5342 watchpoint triggered after all. So don't print
5343 anything for this watchpoint. */
5344 bs->print_it = print_it_noop;
5345 bs->stop = 0;
5346 }
5347 }
5348}
5349
7d4df6a4
DE
5350/* For breakpoints that are currently marked as telling gdb to stop,
5351 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5352 of breakpoint referred to by BS. If we should not stop for this
5353 breakpoint, set BS->stop to 0. */
f431efe5 5354
18a18393
VP
5355static void
5356bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5357{
2bdf28a0
JK
5358 const struct bp_location *bl;
5359 struct breakpoint *b;
7d4df6a4
DE
5360 int value_is_zero = 0;
5361 struct expression *cond;
5362
5363 gdb_assert (bs->stop);
2bdf28a0
JK
5364
5365 /* BS is built for existing struct breakpoint. */
f431efe5 5366 bl = bs->bp_location_at;
2bdf28a0 5367 gdb_assert (bl != NULL);
f431efe5 5368 b = bs->breakpoint_at;
2bdf28a0 5369 gdb_assert (b != NULL);
18a18393 5370
b775012e
LM
5371 /* Even if the target evaluated the condition on its end and notified GDB, we
5372 need to do so again since GDB does not know if we stopped due to a
5373 breakpoint or a single step breakpoint. */
5374
18a18393 5375 if (frame_id_p (b->frame_id)
edb3359d 5376 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5377 {
7d4df6a4
DE
5378 bs->stop = 0;
5379 return;
5380 }
60e1c644 5381
12ab52e9
PA
5382 /* If this is a thread/task-specific breakpoint, don't waste cpu
5383 evaluating the condition if this isn't the specified
5384 thread/task. */
5385 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5386 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5387
6c1b0f7b
DE
5388 {
5389 bs->stop = 0;
5390 return;
5391 }
5392
6dddc817
DE
5393 /* Evaluate extension language breakpoints that have a "stop" method
5394 implemented. */
5395 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5396
7d4df6a4
DE
5397 if (is_watchpoint (b))
5398 {
5399 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5400
7d4df6a4
DE
5401 cond = w->cond_exp;
5402 }
5403 else
5404 cond = bl->cond;
60e1c644 5405
7d4df6a4
DE
5406 if (cond && b->disposition != disp_del_at_next_stop)
5407 {
5408 int within_current_scope = 1;
5409 struct watchpoint * w;
60e1c644 5410
7d4df6a4
DE
5411 /* We use value_mark and value_free_to_mark because it could
5412 be a long time before we return to the command level and
5413 call free_all_values. We can't call free_all_values
5414 because we might be in the middle of evaluating a
5415 function call. */
5416 struct value *mark = value_mark ();
5417
5418 if (is_watchpoint (b))
5419 w = (struct watchpoint *) b;
5420 else
5421 w = NULL;
5422
5423 /* Need to select the frame, with all that implies so that
5424 the conditions will have the right context. Because we
5425 use the frame, we will not see an inlined function's
5426 variables when we arrive at a breakpoint at the start
5427 of the inlined function; the current frame will be the
5428 call site. */
5429 if (w == NULL || w->cond_exp_valid_block == NULL)
5430 select_frame (get_current_frame ());
5431 else
18a18393 5432 {
7d4df6a4
DE
5433 struct frame_info *frame;
5434
5435 /* For local watchpoint expressions, which particular
5436 instance of a local is being watched matters, so we
5437 keep track of the frame to evaluate the expression
5438 in. To evaluate the condition however, it doesn't
5439 really matter which instantiation of the function
5440 where the condition makes sense triggers the
5441 watchpoint. This allows an expression like "watch
5442 global if q > 10" set in `func', catch writes to
5443 global on all threads that call `func', or catch
5444 writes on all recursive calls of `func' by a single
5445 thread. We simply always evaluate the condition in
5446 the innermost frame that's executing where it makes
5447 sense to evaluate the condition. It seems
5448 intuitive. */
5449 frame = block_innermost_frame (w->cond_exp_valid_block);
5450 if (frame != NULL)
5451 select_frame (frame);
5452 else
5453 within_current_scope = 0;
18a18393 5454 }
7d4df6a4
DE
5455 if (within_current_scope)
5456 value_is_zero
5457 = catch_errors (breakpoint_cond_eval, cond,
5458 "Error in testing breakpoint condition:\n",
5459 RETURN_MASK_ALL);
5460 else
18a18393 5461 {
7d4df6a4
DE
5462 warning (_("Watchpoint condition cannot be tested "
5463 "in the current scope"));
5464 /* If we failed to set the right context for this
5465 watchpoint, unconditionally report it. */
5466 value_is_zero = 0;
18a18393 5467 }
7d4df6a4
DE
5468 /* FIXME-someday, should give breakpoint #. */
5469 value_free_to_mark (mark);
18a18393 5470 }
7d4df6a4
DE
5471
5472 if (cond && value_is_zero)
5473 {
5474 bs->stop = 0;
5475 }
7d4df6a4
DE
5476 else if (b->ignore_count > 0)
5477 {
5478 b->ignore_count--;
5479 bs->stop = 0;
5480 /* Increase the hit count even though we don't stop. */
5481 ++(b->hit_count);
5482 observer_notify_breakpoint_modified (b);
5483 }
18a18393
VP
5484}
5485
1cf4d951
PA
5486/* Returns true if we need to track moribund locations of LOC's type
5487 on the current target. */
5488
5489static int
5490need_moribund_for_location_type (struct bp_location *loc)
5491{
5492 return ((loc->loc_type == bp_loc_software_breakpoint
5493 && !target_supports_stopped_by_sw_breakpoint ())
5494 || (loc->loc_type == bp_loc_hardware_breakpoint
5495 && !target_supports_stopped_by_hw_breakpoint ()));
5496}
5497
18a18393 5498
9709f61c 5499/* Get a bpstat associated with having just stopped at address
d983da9c 5500 BP_ADDR in thread PTID.
c906108c 5501
d983da9c 5502 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5503 don't understand this stop. Result is a chain of bpstat's such
5504 that:
c906108c 5505
c5aa993b 5506 if we don't understand the stop, the result is a null pointer.
c906108c 5507
c5aa993b 5508 if we understand why we stopped, the result is not null.
c906108c 5509
c5aa993b
JM
5510 Each element of the chain refers to a particular breakpoint or
5511 watchpoint at which we have stopped. (We may have stopped for
5512 several reasons concurrently.)
c906108c 5513
c5aa993b
JM
5514 Each element of the chain has valid next, breakpoint_at,
5515 commands, FIXME??? fields. */
c906108c
SS
5516
5517bpstat
6c95b8df 5518bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5519 CORE_ADDR bp_addr, ptid_t ptid,
5520 const struct target_waitstatus *ws)
c906108c 5521{
0d381245 5522 struct breakpoint *b = NULL;
afe38095 5523 struct bp_location *bl;
20874c92 5524 struct bp_location *loc;
5760d0ab
JK
5525 /* First item of allocated bpstat's. */
5526 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5527 /* Pointer to the last thing in the chain currently. */
5760d0ab 5528 bpstat bs;
20874c92 5529 int ix;
429374b8 5530 int need_remove_insert;
f431efe5 5531 int removed_any;
c906108c 5532
f431efe5
PA
5533 /* First, build the bpstat chain with locations that explain a
5534 target stop, while being careful to not set the target running,
5535 as that may invalidate locations (in particular watchpoint
5536 locations are recreated). Resuming will happen here with
5537 breakpoint conditions or watchpoint expressions that include
5538 inferior function calls. */
c5aa993b 5539
429374b8
JK
5540 ALL_BREAKPOINTS (b)
5541 {
1a853c52 5542 if (!breakpoint_enabled (b))
429374b8 5543 continue;
a5606eee 5544
429374b8
JK
5545 for (bl = b->loc; bl != NULL; bl = bl->next)
5546 {
4a64f543
MS
5547 /* For hardware watchpoints, we look only at the first
5548 location. The watchpoint_check function will work on the
5549 entire expression, not the individual locations. For
5550 read watchpoints, the watchpoints_triggered function has
5551 checked all locations already. */
429374b8
JK
5552 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5553 break;
18a18393 5554
f6592439 5555 if (!bl->enabled || bl->shlib_disabled)
429374b8 5556 continue;
c5aa993b 5557
09ac7c10 5558 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5559 continue;
c5aa993b 5560
4a64f543
MS
5561 /* Come here if it's a watchpoint, or if the break address
5562 matches. */
c5aa993b 5563
4a64f543
MS
5564 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5565 explain stop. */
c5aa993b 5566
f431efe5
PA
5567 /* Assume we stop. Should we find a watchpoint that is not
5568 actually triggered, or if the condition of the breakpoint
5569 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5570 bs->stop = 1;
5571 bs->print = 1;
d983da9c 5572
f431efe5
PA
5573 /* If this is a scope breakpoint, mark the associated
5574 watchpoint as triggered so that we will handle the
5575 out-of-scope event. We'll get to the watchpoint next
5576 iteration. */
d0fb5eae 5577 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5578 {
5579 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5580
5581 w->watchpoint_triggered = watch_triggered_yes;
5582 }
f431efe5
PA
5583 }
5584 }
5585
7c16b83e 5586 /* Check if a moribund breakpoint explains the stop. */
1cf4d951
PA
5587 if (!target_supports_stopped_by_sw_breakpoint ()
5588 || !target_supports_stopped_by_hw_breakpoint ())
f431efe5 5589 {
1cf4d951 5590 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f431efe5 5591 {
1cf4d951
PA
5592 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5593 && need_moribund_for_location_type (loc))
5594 {
5595 bs = bpstat_alloc (loc, &bs_link);
5596 /* For hits of moribund locations, we should just proceed. */
5597 bs->stop = 0;
5598 bs->print = 0;
5599 bs->print_it = print_it_noop;
5600 }
f431efe5
PA
5601 }
5602 }
5603
edcc5120
TT
5604 /* A bit of special processing for shlib breakpoints. We need to
5605 process solib loading here, so that the lists of loaded and
5606 unloaded libraries are correct before we handle "catch load" and
5607 "catch unload". */
5608 for (bs = bs_head; bs != NULL; bs = bs->next)
5609 {
5d268276 5610 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5611 {
5612 handle_solib_event ();
5613 break;
5614 }
5615 }
5616
f431efe5
PA
5617 /* Now go through the locations that caused the target to stop, and
5618 check whether we're interested in reporting this stop to higher
5619 layers, or whether we should resume the target transparently. */
5620
5621 removed_any = 0;
5622
5760d0ab 5623 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5624 {
5625 if (!bs->stop)
5626 continue;
5627
f431efe5 5628 b = bs->breakpoint_at;
348d480f
PA
5629 b->ops->check_status (bs);
5630 if (bs->stop)
28010a5d 5631 {
348d480f 5632 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5633
429374b8
JK
5634 if (bs->stop)
5635 {
5636 ++(b->hit_count);
8d3788bd 5637 observer_notify_breakpoint_modified (b);
c906108c 5638
4a64f543 5639 /* We will stop here. */
429374b8
JK
5640 if (b->disposition == disp_disable)
5641 {
816338b5 5642 --(b->enable_count);
1a853c52 5643 if (b->enable_count <= 0)
429374b8 5644 b->enable_state = bp_disabled;
f431efe5 5645 removed_any = 1;
429374b8
JK
5646 }
5647 if (b->silent)
5648 bs->print = 0;
5649 bs->commands = b->commands;
9add0f1b 5650 incref_counted_command_line (bs->commands);
abf85f46
JK
5651 if (command_line_is_silent (bs->commands
5652 ? bs->commands->commands : NULL))
5653 bs->print = 0;
9d6e6e84
HZ
5654
5655 b->ops->after_condition_true (bs);
429374b8
JK
5656 }
5657
348d480f 5658 }
a9b3a50f
PA
5659
5660 /* Print nothing for this entry if we don't stop or don't
5661 print. */
5662 if (!bs->stop || !bs->print)
5663 bs->print_it = print_it_noop;
429374b8 5664 }
876fa593 5665
d983da9c
DJ
5666 /* If we aren't stopping, the value of some hardware watchpoint may
5667 not have changed, but the intermediate memory locations we are
5668 watching may have. Don't bother if we're stopping; this will get
5669 done later. */
d832cb68 5670 need_remove_insert = 0;
5760d0ab
JK
5671 if (! bpstat_causes_stop (bs_head))
5672 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5673 if (!bs->stop
f431efe5
PA
5674 && bs->breakpoint_at
5675 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5676 {
3a5c3e22
PA
5677 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5678
5679 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5680 need_remove_insert = 1;
d983da9c
DJ
5681 }
5682
d832cb68 5683 if (need_remove_insert)
44702360 5684 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5685 else if (removed_any)
44702360 5686 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5687
5760d0ab 5688 return bs_head;
c906108c 5689}
628fe4e4
JK
5690
5691static void
5692handle_jit_event (void)
5693{
5694 struct frame_info *frame;
5695 struct gdbarch *gdbarch;
5696
243a9253
PA
5697 if (debug_infrun)
5698 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5699
628fe4e4
JK
5700 /* Switch terminal for any messages produced by
5701 breakpoint_re_set. */
5702 target_terminal_ours_for_output ();
5703
5704 frame = get_current_frame ();
5705 gdbarch = get_frame_arch (frame);
5706
5707 jit_event_handler (gdbarch);
5708
5709 target_terminal_inferior ();
5710}
5711
5712/* Prepare WHAT final decision for infrun. */
5713
5714/* Decide what infrun needs to do with this bpstat. */
5715
c906108c 5716struct bpstat_what
0e30163f 5717bpstat_what (bpstat bs_head)
c906108c 5718{
c906108c 5719 struct bpstat_what retval;
628fe4e4 5720 int jit_event = 0;
0e30163f 5721 bpstat bs;
c906108c 5722
628fe4e4 5723 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5724 retval.call_dummy = STOP_NONE;
186c406b 5725 retval.is_longjmp = 0;
628fe4e4 5726
0e30163f 5727 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5728 {
628fe4e4
JK
5729 /* Extract this BS's action. After processing each BS, we check
5730 if its action overrides all we've seem so far. */
5731 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5732 enum bptype bptype;
5733
c906108c 5734 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5735 {
5736 /* I suspect this can happen if it was a momentary
5737 breakpoint which has since been deleted. */
5738 bptype = bp_none;
5739 }
20874c92 5740 else
f431efe5 5741 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5742
5743 switch (bptype)
c906108c
SS
5744 {
5745 case bp_none:
628fe4e4 5746 break;
c906108c
SS
5747 case bp_breakpoint:
5748 case bp_hardware_breakpoint:
7c16b83e 5749 case bp_single_step:
c906108c
SS
5750 case bp_until:
5751 case bp_finish:
a9b3a50f 5752 case bp_shlib_event:
c906108c
SS
5753 if (bs->stop)
5754 {
5755 if (bs->print)
628fe4e4 5756 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5757 else
628fe4e4 5758 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5759 }
5760 else
628fe4e4 5761 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5762 break;
5763 case bp_watchpoint:
5764 case bp_hardware_watchpoint:
5765 case bp_read_watchpoint:
5766 case bp_access_watchpoint:
5767 if (bs->stop)
5768 {
5769 if (bs->print)
628fe4e4 5770 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5771 else
628fe4e4 5772 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5773 }
5774 else
628fe4e4
JK
5775 {
5776 /* There was a watchpoint, but we're not stopping.
5777 This requires no further action. */
5778 }
c906108c
SS
5779 break;
5780 case bp_longjmp:
e2e4d78b 5781 case bp_longjmp_call_dummy:
186c406b 5782 case bp_exception:
0a39bb32
PA
5783 if (bs->stop)
5784 {
5785 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5786 retval.is_longjmp = bptype != bp_exception;
5787 }
5788 else
5789 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5790 break;
5791 case bp_longjmp_resume:
186c406b 5792 case bp_exception_resume:
0a39bb32
PA
5793 if (bs->stop)
5794 {
5795 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5796 retval.is_longjmp = bptype == bp_longjmp_resume;
5797 }
5798 else
5799 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5800 break;
5801 case bp_step_resume:
5802 if (bs->stop)
628fe4e4
JK
5803 this_action = BPSTAT_WHAT_STEP_RESUME;
5804 else
c906108c 5805 {
628fe4e4
JK
5806 /* It is for the wrong frame. */
5807 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5808 }
c906108c 5809 break;
2c03e5be
PA
5810 case bp_hp_step_resume:
5811 if (bs->stop)
5812 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5813 else
5814 {
5815 /* It is for the wrong frame. */
5816 this_action = BPSTAT_WHAT_SINGLE;
5817 }
5818 break;
c906108c 5819 case bp_watchpoint_scope:
c4093a6a 5820 case bp_thread_event:
1900040c 5821 case bp_overlay_event:
0fd8e87f 5822 case bp_longjmp_master:
aa7d318d 5823 case bp_std_terminate_master:
186c406b 5824 case bp_exception_master:
628fe4e4 5825 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5826 break;
ce78b96d 5827 case bp_catchpoint:
c5aa993b
JM
5828 if (bs->stop)
5829 {
5830 if (bs->print)
628fe4e4 5831 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5832 else
628fe4e4 5833 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5834 }
5835 else
628fe4e4
JK
5836 {
5837 /* There was a catchpoint, but we're not stopping.
5838 This requires no further action. */
5839 }
5840 break;
628fe4e4
JK
5841 case bp_jit_event:
5842 jit_event = 1;
5843 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5844 break;
c906108c 5845 case bp_call_dummy:
53a5351d
JM
5846 /* Make sure the action is stop (silent or noisy),
5847 so infrun.c pops the dummy frame. */
aa7d318d 5848 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5849 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5850 break;
5851 case bp_std_terminate:
5852 /* Make sure the action is stop (silent or noisy),
5853 so infrun.c pops the dummy frame. */
aa7d318d 5854 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5855 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5856 break;
1042e4c0 5857 case bp_tracepoint:
7a697b8d 5858 case bp_fast_tracepoint:
0fb4aa4b 5859 case bp_static_tracepoint:
1042e4c0
SS
5860 /* Tracepoint hits should not be reported back to GDB, and
5861 if one got through somehow, it should have been filtered
5862 out already. */
5863 internal_error (__FILE__, __LINE__,
7a697b8d 5864 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5865 break;
5866 case bp_gnu_ifunc_resolver:
5867 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5868 this_action = BPSTAT_WHAT_SINGLE;
5869 break;
5870 case bp_gnu_ifunc_resolver_return:
5871 /* The breakpoint will be removed, execution will restart from the
5872 PC of the former breakpoint. */
5873 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5874 break;
e7e0cddf
SS
5875
5876 case bp_dprintf:
a11cfd87
HZ
5877 if (bs->stop)
5878 this_action = BPSTAT_WHAT_STOP_SILENT;
5879 else
5880 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5881 break;
5882
628fe4e4
JK
5883 default:
5884 internal_error (__FILE__, __LINE__,
5885 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5886 }
628fe4e4
JK
5887
5888 retval.main_action = max (retval.main_action, this_action);
c906108c 5889 }
628fe4e4 5890
243a9253
PA
5891 return retval;
5892}
628fe4e4 5893
243a9253
PA
5894void
5895bpstat_run_callbacks (bpstat bs_head)
5896{
5897 bpstat bs;
628fe4e4 5898
0e30163f
JK
5899 for (bs = bs_head; bs != NULL; bs = bs->next)
5900 {
5901 struct breakpoint *b = bs->breakpoint_at;
5902
5903 if (b == NULL)
5904 continue;
5905 switch (b->type)
5906 {
243a9253
PA
5907 case bp_jit_event:
5908 handle_jit_event ();
5909 break;
0e30163f
JK
5910 case bp_gnu_ifunc_resolver:
5911 gnu_ifunc_resolver_stop (b);
5912 break;
5913 case bp_gnu_ifunc_resolver_return:
5914 gnu_ifunc_resolver_return_stop (b);
5915 break;
5916 }
5917 }
c906108c
SS
5918}
5919
5920/* Nonzero if we should step constantly (e.g. watchpoints on machines
5921 without hardware support). This isn't related to a specific bpstat,
5922 just to things like whether watchpoints are set. */
5923
c5aa993b 5924int
fba45db2 5925bpstat_should_step (void)
c906108c
SS
5926{
5927 struct breakpoint *b;
cc59ec59 5928
c906108c 5929 ALL_BREAKPOINTS (b)
717a8278 5930 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5931 return 1;
c906108c
SS
5932 return 0;
5933}
5934
67822962
PA
5935int
5936bpstat_causes_stop (bpstat bs)
5937{
5938 for (; bs != NULL; bs = bs->next)
5939 if (bs->stop)
5940 return 1;
5941
5942 return 0;
5943}
5944
c906108c 5945\f
c5aa993b 5946
170b53b2
UW
5947/* Compute a string of spaces suitable to indent the next line
5948 so it starts at the position corresponding to the table column
5949 named COL_NAME in the currently active table of UIOUT. */
5950
5951static char *
5952wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5953{
5954 static char wrap_indent[80];
5955 int i, total_width, width, align;
5956 char *text;
5957
5958 total_width = 0;
5959 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5960 {
5961 if (strcmp (text, col_name) == 0)
5962 {
5963 gdb_assert (total_width < sizeof wrap_indent);
5964 memset (wrap_indent, ' ', total_width);
5965 wrap_indent[total_width] = 0;
5966
5967 return wrap_indent;
5968 }
5969
5970 total_width += width + 1;
5971 }
5972
5973 return NULL;
5974}
5975
b775012e
LM
5976/* Determine if the locations of this breakpoint will have their conditions
5977 evaluated by the target, host or a mix of both. Returns the following:
5978
5979 "host": Host evals condition.
5980 "host or target": Host or Target evals condition.
5981 "target": Target evals condition.
5982*/
5983
5984static const char *
5985bp_condition_evaluator (struct breakpoint *b)
5986{
5987 struct bp_location *bl;
5988 char host_evals = 0;
5989 char target_evals = 0;
5990
5991 if (!b)
5992 return NULL;
5993
5994 if (!is_breakpoint (b))
5995 return NULL;
5996
5997 if (gdb_evaluates_breakpoint_condition_p ()
5998 || !target_supports_evaluation_of_breakpoint_conditions ())
5999 return condition_evaluation_host;
6000
6001 for (bl = b->loc; bl; bl = bl->next)
6002 {
6003 if (bl->cond_bytecode)
6004 target_evals++;
6005 else
6006 host_evals++;
6007 }
6008
6009 if (host_evals && target_evals)
6010 return condition_evaluation_both;
6011 else if (target_evals)
6012 return condition_evaluation_target;
6013 else
6014 return condition_evaluation_host;
6015}
6016
6017/* Determine the breakpoint location's condition evaluator. This is
6018 similar to bp_condition_evaluator, but for locations. */
6019
6020static const char *
6021bp_location_condition_evaluator (struct bp_location *bl)
6022{
6023 if (bl && !is_breakpoint (bl->owner))
6024 return NULL;
6025
6026 if (gdb_evaluates_breakpoint_condition_p ()
6027 || !target_supports_evaluation_of_breakpoint_conditions ())
6028 return condition_evaluation_host;
6029
6030 if (bl && bl->cond_bytecode)
6031 return condition_evaluation_target;
6032 else
6033 return condition_evaluation_host;
6034}
6035
859825b8
JK
6036/* Print the LOC location out of the list of B->LOC locations. */
6037
170b53b2
UW
6038static void
6039print_breakpoint_location (struct breakpoint *b,
6040 struct bp_location *loc)
0d381245 6041{
79a45e25 6042 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6043 struct cleanup *old_chain = save_current_program_space ();
6044
859825b8
JK
6045 if (loc != NULL && loc->shlib_disabled)
6046 loc = NULL;
6047
6c95b8df
PA
6048 if (loc != NULL)
6049 set_current_program_space (loc->pspace);
6050
56435ebe 6051 if (b->display_canonical)
f00aae0f
KS
6052 ui_out_field_string (uiout, "what",
6053 event_location_to_string (b->location));
2f202fde 6054 else if (loc && loc->symtab)
0d381245
VP
6055 {
6056 struct symbol *sym
6057 = find_pc_sect_function (loc->address, loc->section);
6058 if (sym)
6059 {
6060 ui_out_text (uiout, "in ");
6061 ui_out_field_string (uiout, "func",
6062 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6063 ui_out_text (uiout, " ");
6064 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6065 ui_out_text (uiout, "at ");
0d381245 6066 }
05cba821
JK
6067 ui_out_field_string (uiout, "file",
6068 symtab_to_filename_for_display (loc->symtab));
0d381245 6069 ui_out_text (uiout, ":");
05cba821 6070
0d381245 6071 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6072 ui_out_field_string (uiout, "fullname",
6073 symtab_to_fullname (loc->symtab));
0d381245 6074
f8eba3c6 6075 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6076 }
859825b8 6077 else if (loc)
0d381245 6078 {
f99d8bf4
PA
6079 struct ui_file *stb = mem_fileopen ();
6080 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6081
f99d8bf4 6082 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6083 demangle, "");
0d381245 6084 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6085
6086 do_cleanups (stb_chain);
0d381245 6087 }
859825b8 6088 else
f00aae0f
KS
6089 {
6090 ui_out_field_string (uiout, "pending",
6091 event_location_to_string (b->location));
6092 /* If extra_string is available, it could be holding a condition
6093 or dprintf arguments. In either case, make sure it is printed,
6094 too, but only for non-MI streams. */
6095 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6096 {
6097 if (b->type == bp_dprintf)
6098 ui_out_text (uiout, ",");
6099 else
6100 ui_out_text (uiout, " ");
6101 ui_out_text (uiout, b->extra_string);
6102 }
6103 }
6c95b8df 6104
b775012e
LM
6105 if (loc && is_breakpoint (b)
6106 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6107 && bp_condition_evaluator (b) == condition_evaluation_both)
6108 {
6109 ui_out_text (uiout, " (");
6110 ui_out_field_string (uiout, "evaluated-by",
6111 bp_location_condition_evaluator (loc));
6112 ui_out_text (uiout, ")");
6113 }
6114
6c95b8df 6115 do_cleanups (old_chain);
0d381245
VP
6116}
6117
269b11a2
PA
6118static const char *
6119bptype_string (enum bptype type)
c906108c 6120{
c4093a6a
JM
6121 struct ep_type_description
6122 {
6123 enum bptype type;
6124 char *description;
6125 };
6126 static struct ep_type_description bptypes[] =
c906108c 6127 {
c5aa993b
JM
6128 {bp_none, "?deleted?"},
6129 {bp_breakpoint, "breakpoint"},
c906108c 6130 {bp_hardware_breakpoint, "hw breakpoint"},
7c16b83e 6131 {bp_single_step, "sw single-step"},
c5aa993b
JM
6132 {bp_until, "until"},
6133 {bp_finish, "finish"},
6134 {bp_watchpoint, "watchpoint"},
c906108c 6135 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6136 {bp_read_watchpoint, "read watchpoint"},
6137 {bp_access_watchpoint, "acc watchpoint"},
6138 {bp_longjmp, "longjmp"},
6139 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6140 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6141 {bp_exception, "exception"},
6142 {bp_exception_resume, "exception resume"},
c5aa993b 6143 {bp_step_resume, "step resume"},
2c03e5be 6144 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6145 {bp_watchpoint_scope, "watchpoint scope"},
6146 {bp_call_dummy, "call dummy"},
aa7d318d 6147 {bp_std_terminate, "std::terminate"},
c5aa993b 6148 {bp_shlib_event, "shlib events"},
c4093a6a 6149 {bp_thread_event, "thread events"},
1900040c 6150 {bp_overlay_event, "overlay events"},
0fd8e87f 6151 {bp_longjmp_master, "longjmp master"},
aa7d318d 6152 {bp_std_terminate_master, "std::terminate master"},
186c406b 6153 {bp_exception_master, "exception master"},
ce78b96d 6154 {bp_catchpoint, "catchpoint"},
1042e4c0 6155 {bp_tracepoint, "tracepoint"},
7a697b8d 6156 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6157 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6158 {bp_dprintf, "dprintf"},
4efc6507 6159 {bp_jit_event, "jit events"},
0e30163f
JK
6160 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6161 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6162 };
269b11a2
PA
6163
6164 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6165 || ((int) type != bptypes[(int) type].type))
6166 internal_error (__FILE__, __LINE__,
6167 _("bptypes table does not describe type #%d."),
6168 (int) type);
6169
6170 return bptypes[(int) type].description;
6171}
6172
998580f1
MK
6173/* For MI, output a field named 'thread-groups' with a list as the value.
6174 For CLI, prefix the list with the string 'inf'. */
6175
6176static void
6177output_thread_groups (struct ui_out *uiout,
6178 const char *field_name,
6179 VEC(int) *inf_num,
6180 int mi_only)
6181{
752eb8b4 6182 struct cleanup *back_to;
998580f1
MK
6183 int is_mi = ui_out_is_mi_like_p (uiout);
6184 int inf;
6185 int i;
6186
6187 /* For backward compatibility, don't display inferiors in CLI unless
6188 there are several. Always display them for MI. */
6189 if (!is_mi && mi_only)
6190 return;
6191
752eb8b4
TT
6192 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6193
998580f1
MK
6194 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6195 {
6196 if (is_mi)
6197 {
6198 char mi_group[10];
6199
6200 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6201 ui_out_field_string (uiout, NULL, mi_group);
6202 }
6203 else
6204 {
6205 if (i == 0)
6206 ui_out_text (uiout, " inf ");
6207 else
6208 ui_out_text (uiout, ", ");
6209
6210 ui_out_text (uiout, plongest (inf));
6211 }
6212 }
6213
6214 do_cleanups (back_to);
6215}
6216
269b11a2
PA
6217/* Print B to gdb_stdout. */
6218
6219static void
6220print_one_breakpoint_location (struct breakpoint *b,
6221 struct bp_location *loc,
6222 int loc_number,
6223 struct bp_location **last_loc,
269b11a2
PA
6224 int allflag)
6225{
6226 struct command_line *l;
c2c6d25f 6227 static char bpenables[] = "nynny";
c906108c 6228
79a45e25 6229 struct ui_out *uiout = current_uiout;
0d381245
VP
6230 int header_of_multiple = 0;
6231 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6232 struct value_print_options opts;
6233
6234 get_user_print_options (&opts);
0d381245
VP
6235
6236 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6237 /* See comment in print_one_breakpoint concerning treatment of
6238 breakpoints with single disabled location. */
0d381245
VP
6239 if (loc == NULL
6240 && (b->loc != NULL
6241 && (b->loc->next != NULL || !b->loc->enabled)))
6242 header_of_multiple = 1;
6243 if (loc == NULL)
6244 loc = b->loc;
6245
c4093a6a
JM
6246 annotate_record ();
6247
6248 /* 1 */
6249 annotate_field (0);
0d381245
VP
6250 if (part_of_multiple)
6251 {
6252 char *formatted;
0c6773c1 6253 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6254 ui_out_field_string (uiout, "number", formatted);
6255 xfree (formatted);
6256 }
6257 else
6258 {
6259 ui_out_field_int (uiout, "number", b->number);
6260 }
c4093a6a
JM
6261
6262 /* 2 */
6263 annotate_field (1);
0d381245
VP
6264 if (part_of_multiple)
6265 ui_out_field_skip (uiout, "type");
269b11a2
PA
6266 else
6267 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6268
6269 /* 3 */
6270 annotate_field (2);
0d381245
VP
6271 if (part_of_multiple)
6272 ui_out_field_skip (uiout, "disp");
6273 else
2cec12e5 6274 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6275
c4093a6a
JM
6276
6277 /* 4 */
6278 annotate_field (3);
0d381245 6279 if (part_of_multiple)
54e52265 6280 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6281 else
4a64f543
MS
6282 ui_out_field_fmt (uiout, "enabled", "%c",
6283 bpenables[(int) b->enable_state]);
54e52265 6284 ui_out_spaces (uiout, 2);
0d381245 6285
c4093a6a
JM
6286
6287 /* 5 and 6 */
3086aeae 6288 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6289 {
4a64f543
MS
6290 /* Although the print_one can possibly print all locations,
6291 calling it here is not likely to get any nice result. So,
6292 make sure there's just one location. */
0d381245 6293 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6294 b->ops->print_one (b, last_loc);
0d381245 6295 }
3086aeae
DJ
6296 else
6297 switch (b->type)
6298 {
6299 case bp_none:
6300 internal_error (__FILE__, __LINE__,
e2e0b3e5 6301 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6302 break;
c906108c 6303
3086aeae
DJ
6304 case bp_watchpoint:
6305 case bp_hardware_watchpoint:
6306 case bp_read_watchpoint:
6307 case bp_access_watchpoint:
3a5c3e22
PA
6308 {
6309 struct watchpoint *w = (struct watchpoint *) b;
6310
6311 /* Field 4, the address, is omitted (which makes the columns
6312 not line up too nicely with the headers, but the effect
6313 is relatively readable). */
6314 if (opts.addressprint)
6315 ui_out_field_skip (uiout, "addr");
6316 annotate_field (5);
6317 ui_out_field_string (uiout, "what", w->exp_string);
6318 }
3086aeae
DJ
6319 break;
6320
3086aeae
DJ
6321 case bp_breakpoint:
6322 case bp_hardware_breakpoint:
7c16b83e 6323 case bp_single_step:
3086aeae
DJ
6324 case bp_until:
6325 case bp_finish:
6326 case bp_longjmp:
6327 case bp_longjmp_resume:
e2e4d78b 6328 case bp_longjmp_call_dummy:
186c406b
TT
6329 case bp_exception:
6330 case bp_exception_resume:
3086aeae 6331 case bp_step_resume:
2c03e5be 6332 case bp_hp_step_resume:
3086aeae
DJ
6333 case bp_watchpoint_scope:
6334 case bp_call_dummy:
aa7d318d 6335 case bp_std_terminate:
3086aeae
DJ
6336 case bp_shlib_event:
6337 case bp_thread_event:
6338 case bp_overlay_event:
0fd8e87f 6339 case bp_longjmp_master:
aa7d318d 6340 case bp_std_terminate_master:
186c406b 6341 case bp_exception_master:
1042e4c0 6342 case bp_tracepoint:
7a697b8d 6343 case bp_fast_tracepoint:
0fb4aa4b 6344 case bp_static_tracepoint:
e7e0cddf 6345 case bp_dprintf:
4efc6507 6346 case bp_jit_event:
0e30163f
JK
6347 case bp_gnu_ifunc_resolver:
6348 case bp_gnu_ifunc_resolver_return:
79a45b7d 6349 if (opts.addressprint)
3086aeae
DJ
6350 {
6351 annotate_field (4);
54e52265 6352 if (header_of_multiple)
0d381245 6353 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6354 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6355 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6356 else
5af949e3
UW
6357 ui_out_field_core_addr (uiout, "addr",
6358 loc->gdbarch, loc->address);
3086aeae
DJ
6359 }
6360 annotate_field (5);
0d381245 6361 if (!header_of_multiple)
170b53b2 6362 print_breakpoint_location (b, loc);
0d381245 6363 if (b->loc)
a6d9a66e 6364 *last_loc = b->loc;
3086aeae
DJ
6365 break;
6366 }
c906108c 6367
6c95b8df 6368
998580f1 6369 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6370 {
6371 struct inferior *inf;
998580f1
MK
6372 VEC(int) *inf_num = NULL;
6373 int mi_only = 1;
6c95b8df 6374
998580f1 6375 ALL_INFERIORS (inf)
6c95b8df
PA
6376 {
6377 if (inf->pspace == loc->pspace)
998580f1 6378 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6379 }
998580f1
MK
6380
6381 /* For backward compatibility, don't display inferiors in CLI unless
6382 there are several. Always display for MI. */
6383 if (allflag
6384 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6385 && (number_of_program_spaces () > 1
6386 || number_of_inferiors () > 1)
6387 /* LOC is for existing B, it cannot be in
6388 moribund_locations and thus having NULL OWNER. */
6389 && loc->owner->type != bp_catchpoint))
6390 mi_only = 0;
6391 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6392 VEC_free (int, inf_num);
6c95b8df
PA
6393 }
6394
4a306c9a 6395 if (!part_of_multiple)
c4093a6a 6396 {
4a306c9a
JB
6397 if (b->thread != -1)
6398 {
6399 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6400 "stop only in" line a little further down. */
4a306c9a
JB
6401 ui_out_text (uiout, " thread ");
6402 ui_out_field_int (uiout, "thread", b->thread);
6403 }
6404 else if (b->task != 0)
6405 {
6406 ui_out_text (uiout, " task ");
6407 ui_out_field_int (uiout, "task", b->task);
6408 }
c4093a6a 6409 }
f1310107 6410
8b93c638 6411 ui_out_text (uiout, "\n");
f1310107 6412
348d480f 6413 if (!part_of_multiple)
f1310107
TJB
6414 b->ops->print_one_detail (b, uiout);
6415
0d381245 6416 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6417 {
6418 annotate_field (6);
8b93c638 6419 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6420 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6421 the frame ID. */
5af949e3
UW
6422 ui_out_field_core_addr (uiout, "frame",
6423 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6424 ui_out_text (uiout, "\n");
c4093a6a
JM
6425 }
6426
28010a5d 6427 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6428 {
6429 annotate_field (7);
d77f58be 6430 if (is_tracepoint (b))
1042e4c0
SS
6431 ui_out_text (uiout, "\ttrace only if ");
6432 else
6433 ui_out_text (uiout, "\tstop only if ");
0101ce28 6434 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6435
6436 /* Print whether the target is doing the breakpoint's condition
6437 evaluation. If GDB is doing the evaluation, don't print anything. */
6438 if (is_breakpoint (b)
6439 && breakpoint_condition_evaluation_mode ()
6440 == condition_evaluation_target)
6441 {
6442 ui_out_text (uiout, " (");
6443 ui_out_field_string (uiout, "evaluated-by",
6444 bp_condition_evaluator (b));
6445 ui_out_text (uiout, " evals)");
6446 }
0101ce28
JJ
6447 ui_out_text (uiout, "\n");
6448 }
6449
0d381245 6450 if (!part_of_multiple && b->thread != -1)
c4093a6a 6451 {
4a64f543 6452 /* FIXME should make an annotation for this. */
8b93c638
JM
6453 ui_out_text (uiout, "\tstop only in thread ");
6454 ui_out_field_int (uiout, "thread", b->thread);
6455 ui_out_text (uiout, "\n");
c4093a6a
JM
6456 }
6457
556ec64d
YQ
6458 if (!part_of_multiple)
6459 {
6460 if (b->hit_count)
31f56a27
YQ
6461 {
6462 /* FIXME should make an annotation for this. */
6463 if (is_catchpoint (b))
6464 ui_out_text (uiout, "\tcatchpoint");
6465 else if (is_tracepoint (b))
6466 ui_out_text (uiout, "\ttracepoint");
6467 else
6468 ui_out_text (uiout, "\tbreakpoint");
6469 ui_out_text (uiout, " already hit ");
6470 ui_out_field_int (uiout, "times", b->hit_count);
6471 if (b->hit_count == 1)
6472 ui_out_text (uiout, " time\n");
6473 else
6474 ui_out_text (uiout, " times\n");
6475 }
556ec64d
YQ
6476 else
6477 {
31f56a27
YQ
6478 /* Output the count also if it is zero, but only if this is mi. */
6479 if (ui_out_is_mi_like_p (uiout))
6480 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6481 }
6482 }
8b93c638 6483
0d381245 6484 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6485 {
6486 annotate_field (8);
8b93c638
JM
6487 ui_out_text (uiout, "\tignore next ");
6488 ui_out_field_int (uiout, "ignore", b->ignore_count);
6489 ui_out_text (uiout, " hits\n");
c4093a6a 6490 }
059fb39f 6491
816338b5
SS
6492 /* Note that an enable count of 1 corresponds to "enable once"
6493 behavior, which is reported by the combination of enablement and
6494 disposition, so we don't need to mention it here. */
6495 if (!part_of_multiple && b->enable_count > 1)
6496 {
6497 annotate_field (8);
6498 ui_out_text (uiout, "\tdisable after ");
6499 /* Tweak the wording to clarify that ignore and enable counts
6500 are distinct, and have additive effect. */
6501 if (b->ignore_count)
6502 ui_out_text (uiout, "additional ");
6503 else
6504 ui_out_text (uiout, "next ");
6505 ui_out_field_int (uiout, "enable", b->enable_count);
6506 ui_out_text (uiout, " hits\n");
6507 }
6508
f196051f
SS
6509 if (!part_of_multiple && is_tracepoint (b))
6510 {
6511 struct tracepoint *tp = (struct tracepoint *) b;
6512
6513 if (tp->traceframe_usage)
6514 {
6515 ui_out_text (uiout, "\ttrace buffer usage ");
6516 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6517 ui_out_text (uiout, " bytes\n");
6518 }
6519 }
d3ce09f5 6520
9add0f1b 6521 l = b->commands ? b->commands->commands : NULL;
059fb39f 6522 if (!part_of_multiple && l)
c4093a6a 6523 {
3b31d625
EZ
6524 struct cleanup *script_chain;
6525
c4093a6a 6526 annotate_field (9);
3b31d625 6527 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6528 print_command_lines (uiout, l, 4);
3b31d625 6529 do_cleanups (script_chain);
c4093a6a 6530 }
d24317b4 6531
d9b3f62e 6532 if (is_tracepoint (b))
1042e4c0 6533 {
d9b3f62e
PA
6534 struct tracepoint *t = (struct tracepoint *) b;
6535
6536 if (!part_of_multiple && t->pass_count)
6537 {
6538 annotate_field (10);
6539 ui_out_text (uiout, "\tpass count ");
6540 ui_out_field_int (uiout, "pass", t->pass_count);
6541 ui_out_text (uiout, " \n");
6542 }
f2a8bc8a
YQ
6543
6544 /* Don't display it when tracepoint or tracepoint location is
6545 pending. */
6546 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6547 {
6548 annotate_field (11);
6549
6550 if (ui_out_is_mi_like_p (uiout))
6551 ui_out_field_string (uiout, "installed",
6552 loc->inserted ? "y" : "n");
6553 else
6554 {
6555 if (loc->inserted)
6556 ui_out_text (uiout, "\t");
6557 else
6558 ui_out_text (uiout, "\tnot ");
6559 ui_out_text (uiout, "installed on target\n");
6560 }
6561 }
1042e4c0
SS
6562 }
6563
d24317b4
VP
6564 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6565 {
3a5c3e22
PA
6566 if (is_watchpoint (b))
6567 {
6568 struct watchpoint *w = (struct watchpoint *) b;
6569
6570 ui_out_field_string (uiout, "original-location", w->exp_string);
6571 }
f00aae0f
KS
6572 else if (b->location != NULL
6573 && event_location_to_string (b->location) != NULL)
6574 ui_out_field_string (uiout, "original-location",
6575 event_location_to_string (b->location));
d24317b4 6576 }
c4093a6a 6577}
c5aa993b 6578
0d381245
VP
6579static void
6580print_one_breakpoint (struct breakpoint *b,
4a64f543 6581 struct bp_location **last_loc,
6c95b8df 6582 int allflag)
0d381245 6583{
8d3788bd 6584 struct cleanup *bkpt_chain;
79a45e25 6585 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6586
6587 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6588
12c5a436 6589 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6590 do_cleanups (bkpt_chain);
0d381245
VP
6591
6592 /* If this breakpoint has custom print function,
6593 it's already printed. Otherwise, print individual
6594 locations, if any. */
6595 if (b->ops == NULL || b->ops->print_one == NULL)
6596 {
4a64f543
MS
6597 /* If breakpoint has a single location that is disabled, we
6598 print it as if it had several locations, since otherwise it's
6599 hard to represent "breakpoint enabled, location disabled"
6600 situation.
6601
6602 Note that while hardware watchpoints have several locations
a3be7890 6603 internally, that's not a property exposed to user. */
0d381245 6604 if (b->loc
a5606eee 6605 && !is_hardware_watchpoint (b)
8d3788bd 6606 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6607 {
6608 struct bp_location *loc;
6609 int n = 1;
8d3788bd 6610
0d381245 6611 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6612 {
6613 struct cleanup *inner2 =
6614 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6615 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6616 do_cleanups (inner2);
6617 }
0d381245
VP
6618 }
6619 }
6620}
6621
a6d9a66e
UW
6622static int
6623breakpoint_address_bits (struct breakpoint *b)
6624{
6625 int print_address_bits = 0;
6626 struct bp_location *loc;
6627
6628 for (loc = b->loc; loc; loc = loc->next)
6629 {
c7437ca6
PA
6630 int addr_bit;
6631
6632 /* Software watchpoints that aren't watching memory don't have
6633 an address to print. */
6634 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6635 continue;
6636
6637 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6638 if (addr_bit > print_address_bits)
6639 print_address_bits = addr_bit;
6640 }
6641
6642 return print_address_bits;
6643}
0d381245 6644
c4093a6a
JM
6645struct captured_breakpoint_query_args
6646 {
6647 int bnum;
6648 };
c5aa993b 6649
c4093a6a 6650static int
2b65245e 6651do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6652{
6653 struct captured_breakpoint_query_args *args = data;
52f0bd74 6654 struct breakpoint *b;
a6d9a66e 6655 struct bp_location *dummy_loc = NULL;
cc59ec59 6656
c4093a6a
JM
6657 ALL_BREAKPOINTS (b)
6658 {
6659 if (args->bnum == b->number)
c5aa993b 6660 {
12c5a436 6661 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6662 return GDB_RC_OK;
c5aa993b 6663 }
c4093a6a
JM
6664 }
6665 return GDB_RC_NONE;
6666}
c5aa993b 6667
c4093a6a 6668enum gdb_rc
4a64f543
MS
6669gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6670 char **error_message)
c4093a6a
JM
6671{
6672 struct captured_breakpoint_query_args args;
cc59ec59 6673
c4093a6a
JM
6674 args.bnum = bnum;
6675 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6676 an error. */
b0b13bb4
DJ
6677 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6678 error_message, RETURN_MASK_ALL) < 0)
6679 return GDB_RC_FAIL;
6680 else
6681 return GDB_RC_OK;
c4093a6a 6682}
c5aa993b 6683
09d682a4
TT
6684/* Return true if this breakpoint was set by the user, false if it is
6685 internal or momentary. */
6686
6687int
6688user_breakpoint_p (struct breakpoint *b)
6689{
46c6471b 6690 return b->number > 0;
09d682a4
TT
6691}
6692
7f3b0473 6693/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6694 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6695 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6696 FILTER is non-NULL, call it on each breakpoint and only include the
6697 ones for which it returns non-zero. Return the total number of
6698 breakpoints listed. */
c906108c 6699
d77f58be 6700static int
e5a67952 6701breakpoint_1 (char *args, int allflag,
4a64f543 6702 int (*filter) (const struct breakpoint *))
c4093a6a 6703{
52f0bd74 6704 struct breakpoint *b;
a6d9a66e 6705 struct bp_location *last_loc = NULL;
7f3b0473 6706 int nr_printable_breakpoints;
3b31d625 6707 struct cleanup *bkpttbl_chain;
79a45b7d 6708 struct value_print_options opts;
a6d9a66e 6709 int print_address_bits = 0;
269b11a2 6710 int print_type_col_width = 14;
79a45e25 6711 struct ui_out *uiout = current_uiout;
269b11a2 6712
79a45b7d
TT
6713 get_user_print_options (&opts);
6714
4a64f543
MS
6715 /* Compute the number of rows in the table, as well as the size
6716 required for address fields. */
7f3b0473
AC
6717 nr_printable_breakpoints = 0;
6718 ALL_BREAKPOINTS (b)
e5a67952
MS
6719 {
6720 /* If we have a filter, only list the breakpoints it accepts. */
6721 if (filter && !filter (b))
6722 continue;
6723
6724 /* If we have an "args" string, it is a list of breakpoints to
6725 accept. Skip the others. */
6726 if (args != NULL && *args != '\0')
6727 {
6728 if (allflag && parse_and_eval_long (args) != b->number)
6729 continue;
6730 if (!allflag && !number_is_in_list (args, b->number))
6731 continue;
6732 }
269b11a2 6733
e5a67952
MS
6734 if (allflag || user_breakpoint_p (b))
6735 {
6736 int addr_bit, type_len;
a6d9a66e 6737
e5a67952
MS
6738 addr_bit = breakpoint_address_bits (b);
6739 if (addr_bit > print_address_bits)
6740 print_address_bits = addr_bit;
269b11a2 6741
e5a67952
MS
6742 type_len = strlen (bptype_string (b->type));
6743 if (type_len > print_type_col_width)
6744 print_type_col_width = type_len;
6745
6746 nr_printable_breakpoints++;
6747 }
6748 }
7f3b0473 6749
79a45b7d 6750 if (opts.addressprint)
3b31d625 6751 bkpttbl_chain
3e43a32a
MS
6752 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6753 nr_printable_breakpoints,
3b31d625 6754 "BreakpointTable");
8b93c638 6755 else
3b31d625 6756 bkpttbl_chain
3e43a32a
MS
6757 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6758 nr_printable_breakpoints,
3b31d625 6759 "BreakpointTable");
8b93c638 6760
7f3b0473 6761 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6762 annotate_breakpoints_headers ();
6763 if (nr_printable_breakpoints > 0)
6764 annotate_field (0);
4a64f543 6765 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6766 if (nr_printable_breakpoints > 0)
6767 annotate_field (1);
269b11a2 6768 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6769 "type", "Type"); /* 2 */
d7faa9e7
AC
6770 if (nr_printable_breakpoints > 0)
6771 annotate_field (2);
4a64f543 6772 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6773 if (nr_printable_breakpoints > 0)
6774 annotate_field (3);
54e52265 6775 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6776 if (opts.addressprint)
e5a67952
MS
6777 {
6778 if (nr_printable_breakpoints > 0)
6779 annotate_field (4);
6780 if (print_address_bits <= 32)
6781 ui_out_table_header (uiout, 10, ui_left,
6782 "addr", "Address"); /* 5 */
6783 else
6784 ui_out_table_header (uiout, 18, ui_left,
6785 "addr", "Address"); /* 5 */
6786 }
d7faa9e7
AC
6787 if (nr_printable_breakpoints > 0)
6788 annotate_field (5);
6789 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6790 ui_out_table_body (uiout);
6791 if (nr_printable_breakpoints > 0)
6792 annotate_breakpoints_table ();
7f3b0473 6793
c4093a6a 6794 ALL_BREAKPOINTS (b)
e5a67952
MS
6795 {
6796 QUIT;
6797 /* If we have a filter, only list the breakpoints it accepts. */
6798 if (filter && !filter (b))
6799 continue;
6800
6801 /* If we have an "args" string, it is a list of breakpoints to
6802 accept. Skip the others. */
6803
6804 if (args != NULL && *args != '\0')
6805 {
6806 if (allflag) /* maintenance info breakpoint */
6807 {
6808 if (parse_and_eval_long (args) != b->number)
6809 continue;
6810 }
6811 else /* all others */
6812 {
6813 if (!number_is_in_list (args, b->number))
6814 continue;
6815 }
6816 }
6817 /* We only print out user settable breakpoints unless the
6818 allflag is set. */
6819 if (allflag || user_breakpoint_p (b))
12c5a436 6820 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6821 }
6822
3b31d625 6823 do_cleanups (bkpttbl_chain);
698384cd 6824
7f3b0473 6825 if (nr_printable_breakpoints == 0)
c906108c 6826 {
4a64f543
MS
6827 /* If there's a filter, let the caller decide how to report
6828 empty list. */
d77f58be
SS
6829 if (!filter)
6830 {
e5a67952 6831 if (args == NULL || *args == '\0')
d77f58be
SS
6832 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6833 else
4a64f543 6834 ui_out_message (uiout, 0,
e5a67952
MS
6835 "No breakpoint or watchpoint matching '%s'.\n",
6836 args);
d77f58be 6837 }
c906108c
SS
6838 }
6839 else
c4093a6a 6840 {
a6d9a66e
UW
6841 if (last_loc && !server_command)
6842 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6843 }
c906108c 6844
4a64f543 6845 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6846 there have been breakpoints? */
c906108c 6847 annotate_breakpoints_table_end ();
d77f58be
SS
6848
6849 return nr_printable_breakpoints;
c906108c
SS
6850}
6851
ad443146
SS
6852/* Display the value of default-collect in a way that is generally
6853 compatible with the breakpoint list. */
6854
6855static void
6856default_collect_info (void)
6857{
79a45e25
PA
6858 struct ui_out *uiout = current_uiout;
6859
ad443146
SS
6860 /* If it has no value (which is frequently the case), say nothing; a
6861 message like "No default-collect." gets in user's face when it's
6862 not wanted. */
6863 if (!*default_collect)
6864 return;
6865
6866 /* The following phrase lines up nicely with per-tracepoint collect
6867 actions. */
6868 ui_out_text (uiout, "default collect ");
6869 ui_out_field_string (uiout, "default-collect", default_collect);
6870 ui_out_text (uiout, " \n");
6871}
6872
c906108c 6873static void
e5a67952 6874breakpoints_info (char *args, int from_tty)
c906108c 6875{
e5a67952 6876 breakpoint_1 (args, 0, NULL);
ad443146
SS
6877
6878 default_collect_info ();
d77f58be
SS
6879}
6880
6881static void
e5a67952 6882watchpoints_info (char *args, int from_tty)
d77f58be 6883{
e5a67952 6884 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6885 struct ui_out *uiout = current_uiout;
d77f58be
SS
6886
6887 if (num_printed == 0)
6888 {
e5a67952 6889 if (args == NULL || *args == '\0')
d77f58be
SS
6890 ui_out_message (uiout, 0, "No watchpoints.\n");
6891 else
e5a67952 6892 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6893 }
c906108c
SS
6894}
6895
7a292a7a 6896static void
e5a67952 6897maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6898{
e5a67952 6899 breakpoint_1 (args, 1, NULL);
ad443146
SS
6900
6901 default_collect_info ();
c906108c
SS
6902}
6903
0d381245 6904static int
714835d5 6905breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6906 struct program_space *pspace,
714835d5 6907 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6908{
6909 struct bp_location *bl = b->loc;
cc59ec59 6910
0d381245
VP
6911 for (; bl; bl = bl->next)
6912 {
6c95b8df
PA
6913 if (bl->pspace == pspace
6914 && bl->address == pc
0d381245
VP
6915 && (!overlay_debugging || bl->section == section))
6916 return 1;
6917 }
6918 return 0;
6919}
6920
672f9b60 6921/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6922 concerns with logical breakpoints, so we match program spaces, not
6923 address spaces. */
c906108c
SS
6924
6925static void
6c95b8df
PA
6926describe_other_breakpoints (struct gdbarch *gdbarch,
6927 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6928 struct obj_section *section, int thread)
c906108c 6929{
52f0bd74
AC
6930 int others = 0;
6931 struct breakpoint *b;
c906108c
SS
6932
6933 ALL_BREAKPOINTS (b)
672f9b60
KP
6934 others += (user_breakpoint_p (b)
6935 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6936 if (others > 0)
6937 {
a3f17187
AC
6938 if (others == 1)
6939 printf_filtered (_("Note: breakpoint "));
6940 else /* if (others == ???) */
6941 printf_filtered (_("Note: breakpoints "));
c906108c 6942 ALL_BREAKPOINTS (b)
672f9b60 6943 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6944 {
6945 others--;
6946 printf_filtered ("%d", b->number);
6947 if (b->thread == -1 && thread != -1)
6948 printf_filtered (" (all threads)");
6949 else if (b->thread != -1)
6950 printf_filtered (" (thread %d)", b->thread);
6951 printf_filtered ("%s%s ",
059fb39f 6952 ((b->enable_state == bp_disabled
f8eba3c6 6953 || b->enable_state == bp_call_disabled)
0d381245 6954 ? " (disabled)"
0d381245
VP
6955 : ""),
6956 (others > 1) ? ","
6957 : ((others == 1) ? " and" : ""));
6958 }
a3f17187 6959 printf_filtered (_("also set at pc "));
5af949e3 6960 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6961 printf_filtered (".\n");
6962 }
6963}
6964\f
c906108c 6965
e4f237da 6966/* Return true iff it is meaningful to use the address member of
244558af
LM
6967 BPT locations. For some breakpoint types, the locations' address members
6968 are irrelevant and it makes no sense to attempt to compare them to other
6969 addresses (or use them for any other purpose either).
e4f237da 6970
4a64f543 6971 More specifically, each of the following breakpoint types will
244558af 6972 always have a zero valued location address and we don't want to mark
4a64f543 6973 breakpoints of any of these types to be a duplicate of an actual
244558af 6974 breakpoint location at address zero:
e4f237da
KB
6975
6976 bp_watchpoint
2d134ed3
PA
6977 bp_catchpoint
6978
6979*/
e4f237da
KB
6980
6981static int
6982breakpoint_address_is_meaningful (struct breakpoint *bpt)
6983{
6984 enum bptype type = bpt->type;
6985
2d134ed3
PA
6986 return (type != bp_watchpoint && type != bp_catchpoint);
6987}
6988
6989/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6990 true if LOC1 and LOC2 represent the same watchpoint location. */
6991
6992static int
4a64f543
MS
6993watchpoint_locations_match (struct bp_location *loc1,
6994 struct bp_location *loc2)
2d134ed3 6995{
3a5c3e22
PA
6996 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6997 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6998
6999 /* Both of them must exist. */
7000 gdb_assert (w1 != NULL);
7001 gdb_assert (w2 != NULL);
2bdf28a0 7002
4a64f543
MS
7003 /* If the target can evaluate the condition expression in hardware,
7004 then we we need to insert both watchpoints even if they are at
7005 the same place. Otherwise the watchpoint will only trigger when
7006 the condition of whichever watchpoint was inserted evaluates to
7007 true, not giving a chance for GDB to check the condition of the
7008 other watchpoint. */
3a5c3e22 7009 if ((w1->cond_exp
4a64f543
MS
7010 && target_can_accel_watchpoint_condition (loc1->address,
7011 loc1->length,
0cf6dd15 7012 loc1->watchpoint_type,
3a5c3e22
PA
7013 w1->cond_exp))
7014 || (w2->cond_exp
4a64f543
MS
7015 && target_can_accel_watchpoint_condition (loc2->address,
7016 loc2->length,
0cf6dd15 7017 loc2->watchpoint_type,
3a5c3e22 7018 w2->cond_exp)))
0cf6dd15
TJB
7019 return 0;
7020
85d721b8
PA
7021 /* Note that this checks the owner's type, not the location's. In
7022 case the target does not support read watchpoints, but does
7023 support access watchpoints, we'll have bp_read_watchpoint
7024 watchpoints with hw_access locations. Those should be considered
7025 duplicates of hw_read locations. The hw_read locations will
7026 become hw_access locations later. */
2d134ed3
PA
7027 return (loc1->owner->type == loc2->owner->type
7028 && loc1->pspace->aspace == loc2->pspace->aspace
7029 && loc1->address == loc2->address
7030 && loc1->length == loc2->length);
e4f237da
KB
7031}
7032
31e77af2 7033/* See breakpoint.h. */
6c95b8df 7034
31e77af2 7035int
6c95b8df
PA
7036breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7037 struct address_space *aspace2, CORE_ADDR addr2)
7038{
f5656ead 7039 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
7040 || aspace1 == aspace2)
7041 && addr1 == addr2);
7042}
7043
f1310107
TJB
7044/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7045 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7046 matches ASPACE2. On targets that have global breakpoints, the address
7047 space doesn't really matter. */
7048
7049static int
7050breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7051 int len1, struct address_space *aspace2,
7052 CORE_ADDR addr2)
7053{
f5656ead 7054 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
7055 || aspace1 == aspace2)
7056 && addr2 >= addr1 && addr2 < addr1 + len1);
7057}
7058
7059/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7060 a ranged breakpoint. In most targets, a match happens only if ASPACE
7061 matches the breakpoint's address space. On targets that have global
7062 breakpoints, the address space doesn't really matter. */
7063
7064static int
7065breakpoint_location_address_match (struct bp_location *bl,
7066 struct address_space *aspace,
7067 CORE_ADDR addr)
7068{
7069 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7070 aspace, addr)
7071 || (bl->length
7072 && breakpoint_address_match_range (bl->pspace->aspace,
7073 bl->address, bl->length,
7074 aspace, addr)));
7075}
7076
1e4d1764
YQ
7077/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7078 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7079 true, otherwise returns false. */
7080
7081static int
7082tracepoint_locations_match (struct bp_location *loc1,
7083 struct bp_location *loc2)
7084{
7085 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7086 /* Since tracepoint locations are never duplicated with others', tracepoint
7087 locations at the same address of different tracepoints are regarded as
7088 different locations. */
7089 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7090 else
7091 return 0;
7092}
7093
2d134ed3
PA
7094/* Assuming LOC1 and LOC2's types' have meaningful target addresses
7095 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7096 represent the same location. */
7097
7098static int
4a64f543
MS
7099breakpoint_locations_match (struct bp_location *loc1,
7100 struct bp_location *loc2)
2d134ed3 7101{
2bdf28a0
JK
7102 int hw_point1, hw_point2;
7103
7104 /* Both of them must not be in moribund_locations. */
7105 gdb_assert (loc1->owner != NULL);
7106 gdb_assert (loc2->owner != NULL);
7107
7108 hw_point1 = is_hardware_watchpoint (loc1->owner);
7109 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
7110
7111 if (hw_point1 != hw_point2)
7112 return 0;
7113 else if (hw_point1)
7114 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
7115 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7116 return tracepoint_locations_match (loc1, loc2);
2d134ed3 7117 else
f1310107
TJB
7118 /* We compare bp_location.length in order to cover ranged breakpoints. */
7119 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7120 loc2->pspace->aspace, loc2->address)
7121 && loc1->length == loc2->length);
2d134ed3
PA
7122}
7123
76897487
KB
7124static void
7125breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7126 int bnum, int have_bnum)
7127{
f63fbe86
MS
7128 /* The longest string possibly returned by hex_string_custom
7129 is 50 chars. These must be at least that big for safety. */
7130 char astr1[64];
7131 char astr2[64];
76897487 7132
bb599908
PH
7133 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7134 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 7135 if (have_bnum)
8a3fe4f8 7136 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
7137 bnum, astr1, astr2);
7138 else
8a3fe4f8 7139 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
7140}
7141
4a64f543
MS
7142/* Adjust a breakpoint's address to account for architectural
7143 constraints on breakpoint placement. Return the adjusted address.
7144 Note: Very few targets require this kind of adjustment. For most
7145 targets, this function is simply the identity function. */
76897487
KB
7146
7147static CORE_ADDR
a6d9a66e
UW
7148adjust_breakpoint_address (struct gdbarch *gdbarch,
7149 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7150{
a6d9a66e 7151 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7152 {
7153 /* Very few targets need any kind of breakpoint adjustment. */
7154 return bpaddr;
7155 }
88f7da05
KB
7156 else if (bptype == bp_watchpoint
7157 || bptype == bp_hardware_watchpoint
7158 || bptype == bp_read_watchpoint
7159 || bptype == bp_access_watchpoint
fe798b75 7160 || bptype == bp_catchpoint)
88f7da05
KB
7161 {
7162 /* Watchpoints and the various bp_catch_* eventpoints should not
7163 have their addresses modified. */
7164 return bpaddr;
7165 }
7c16b83e
PA
7166 else if (bptype == bp_single_step)
7167 {
7168 /* Single-step breakpoints should not have their addresses
7169 modified. If there's any architectural constrain that
7170 applies to this address, then it should have already been
7171 taken into account when the breakpoint was created in the
7172 first place. If we didn't do this, stepping through e.g.,
7173 Thumb-2 IT blocks would break. */
7174 return bpaddr;
7175 }
76897487
KB
7176 else
7177 {
7178 CORE_ADDR adjusted_bpaddr;
7179
7180 /* Some targets have architectural constraints on the placement
7181 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7182 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7183
7184 /* An adjusted breakpoint address can significantly alter
7185 a user's expectations. Print a warning if an adjustment
7186 is required. */
7187 if (adjusted_bpaddr != bpaddr)
7188 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7189
7190 return adjusted_bpaddr;
7191 }
7192}
7193
28010a5d
PA
7194void
7195init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7196 struct breakpoint *owner)
7cc221ef 7197{
7cc221ef
DJ
7198 memset (loc, 0, sizeof (*loc));
7199
348d480f
PA
7200 gdb_assert (ops != NULL);
7201
28010a5d
PA
7202 loc->ops = ops;
7203 loc->owner = owner;
511a6cd4 7204 loc->cond = NULL;
b775012e 7205 loc->cond_bytecode = NULL;
0d381245
VP
7206 loc->shlib_disabled = 0;
7207 loc->enabled = 1;
e049a4b5 7208
28010a5d 7209 switch (owner->type)
e049a4b5
DJ
7210 {
7211 case bp_breakpoint:
7c16b83e 7212 case bp_single_step:
e049a4b5
DJ
7213 case bp_until:
7214 case bp_finish:
7215 case bp_longjmp:
7216 case bp_longjmp_resume:
e2e4d78b 7217 case bp_longjmp_call_dummy:
186c406b
TT
7218 case bp_exception:
7219 case bp_exception_resume:
e049a4b5 7220 case bp_step_resume:
2c03e5be 7221 case bp_hp_step_resume:
e049a4b5
DJ
7222 case bp_watchpoint_scope:
7223 case bp_call_dummy:
aa7d318d 7224 case bp_std_terminate:
e049a4b5
DJ
7225 case bp_shlib_event:
7226 case bp_thread_event:
7227 case bp_overlay_event:
4efc6507 7228 case bp_jit_event:
0fd8e87f 7229 case bp_longjmp_master:
aa7d318d 7230 case bp_std_terminate_master:
186c406b 7231 case bp_exception_master:
0e30163f
JK
7232 case bp_gnu_ifunc_resolver:
7233 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7234 case bp_dprintf:
e049a4b5 7235 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7236 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7237 break;
7238 case bp_hardware_breakpoint:
7239 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7240 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7241 break;
7242 case bp_hardware_watchpoint:
7243 case bp_read_watchpoint:
7244 case bp_access_watchpoint:
7245 loc->loc_type = bp_loc_hardware_watchpoint;
7246 break;
7247 case bp_watchpoint:
ce78b96d 7248 case bp_catchpoint:
15c3d785
PA
7249 case bp_tracepoint:
7250 case bp_fast_tracepoint:
0fb4aa4b 7251 case bp_static_tracepoint:
e049a4b5
DJ
7252 loc->loc_type = bp_loc_other;
7253 break;
7254 default:
e2e0b3e5 7255 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7256 }
7257
f431efe5 7258 loc->refc = 1;
28010a5d
PA
7259}
7260
7261/* Allocate a struct bp_location. */
7262
7263static struct bp_location *
7264allocate_bp_location (struct breakpoint *bpt)
7265{
348d480f
PA
7266 return bpt->ops->allocate_location (bpt);
7267}
7cc221ef 7268
f431efe5
PA
7269static void
7270free_bp_location (struct bp_location *loc)
fe3f5fa8 7271{
348d480f 7272 loc->ops->dtor (loc);
fe3f5fa8
VP
7273 xfree (loc);
7274}
7275
f431efe5
PA
7276/* Increment reference count. */
7277
7278static void
7279incref_bp_location (struct bp_location *bl)
7280{
7281 ++bl->refc;
7282}
7283
7284/* Decrement reference count. If the reference count reaches 0,
7285 destroy the bp_location. Sets *BLP to NULL. */
7286
7287static void
7288decref_bp_location (struct bp_location **blp)
7289{
0807b50c
PA
7290 gdb_assert ((*blp)->refc > 0);
7291
f431efe5
PA
7292 if (--(*blp)->refc == 0)
7293 free_bp_location (*blp);
7294 *blp = NULL;
7295}
7296
346774a9 7297/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7298
346774a9
PA
7299static void
7300add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7301{
346774a9 7302 struct breakpoint *b1;
c906108c 7303
346774a9
PA
7304 /* Add this breakpoint to the end of the chain so that a list of
7305 breakpoints will come out in order of increasing numbers. */
7306
7307 b1 = breakpoint_chain;
7308 if (b1 == 0)
7309 breakpoint_chain = b;
7310 else
7311 {
7312 while (b1->next)
7313 b1 = b1->next;
7314 b1->next = b;
7315 }
7316}
7317
7318/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7319
7320static void
7321init_raw_breakpoint_without_location (struct breakpoint *b,
7322 struct gdbarch *gdbarch,
28010a5d 7323 enum bptype bptype,
c0a91b2b 7324 const struct breakpoint_ops *ops)
346774a9 7325{
c906108c 7326 memset (b, 0, sizeof (*b));
2219d63c 7327
348d480f
PA
7328 gdb_assert (ops != NULL);
7329
28010a5d 7330 b->ops = ops;
4d28f7a8 7331 b->type = bptype;
a6d9a66e 7332 b->gdbarch = gdbarch;
c906108c
SS
7333 b->language = current_language->la_language;
7334 b->input_radix = input_radix;
7335 b->thread = -1;
b5de0fa7 7336 b->enable_state = bp_enabled;
c906108c
SS
7337 b->next = 0;
7338 b->silent = 0;
7339 b->ignore_count = 0;
7340 b->commands = NULL;
818dd999 7341 b->frame_id = null_frame_id;
0d381245 7342 b->condition_not_parsed = 0;
84f4c1fe 7343 b->py_bp_object = NULL;
d0fb5eae 7344 b->related_breakpoint = b;
f00aae0f 7345 b->location = NULL;
346774a9
PA
7346}
7347
7348/* Helper to set_raw_breakpoint below. Creates a breakpoint
7349 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7350
7351static struct breakpoint *
7352set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7353 enum bptype bptype,
c0a91b2b 7354 const struct breakpoint_ops *ops)
346774a9
PA
7355{
7356 struct breakpoint *b = XNEW (struct breakpoint);
7357
348d480f 7358 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7359 add_to_breakpoint_chain (b);
0d381245
VP
7360 return b;
7361}
7362
0e30163f
JK
7363/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7364 resolutions should be made as the user specified the location explicitly
7365 enough. */
7366
0d381245 7367static void
0e30163f 7368set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7369{
2bdf28a0
JK
7370 gdb_assert (loc->owner != NULL);
7371
0d381245 7372 if (loc->owner->type == bp_breakpoint
1042e4c0 7373 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7374 || is_tracepoint (loc->owner))
0d381245 7375 {
0e30163f 7376 int is_gnu_ifunc;
2c02bd72 7377 const char *function_name;
6a3a010b 7378 CORE_ADDR func_addr;
0e30163f 7379
2c02bd72 7380 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7381 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7382
7383 if (is_gnu_ifunc && !explicit_loc)
7384 {
7385 struct breakpoint *b = loc->owner;
7386
7387 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7388 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7389 &loc->requested_address))
7390 {
7391 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7392 loc->address = adjust_breakpoint_address (loc->gdbarch,
7393 loc->requested_address,
7394 b->type);
7395 }
7396 else if (b->type == bp_breakpoint && b->loc == loc
7397 && loc->next == NULL && b->related_breakpoint == b)
7398 {
7399 /* Create only the whole new breakpoint of this type but do not
7400 mess more complicated breakpoints with multiple locations. */
7401 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7402 /* Remember the resolver's address for use by the return
7403 breakpoint. */
7404 loc->related_address = func_addr;
0e30163f
JK
7405 }
7406 }
7407
2c02bd72
DE
7408 if (function_name)
7409 loc->function_name = xstrdup (function_name);
0d381245
VP
7410 }
7411}
7412
a6d9a66e 7413/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7414struct gdbarch *
a6d9a66e
UW
7415get_sal_arch (struct symtab_and_line sal)
7416{
7417 if (sal.section)
7418 return get_objfile_arch (sal.section->objfile);
7419 if (sal.symtab)
eb822aa6 7420 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
a6d9a66e
UW
7421
7422 return NULL;
7423}
7424
346774a9
PA
7425/* Low level routine for partially initializing a breakpoint of type
7426 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7427 file name, and line number are provided by SAL.
0d381245
VP
7428
7429 It is expected that the caller will complete the initialization of
7430 the newly created breakpoint struct as well as output any status
c56053d2 7431 information regarding the creation of a new breakpoint. */
0d381245 7432
346774a9
PA
7433static void
7434init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7435 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7436 const struct breakpoint_ops *ops)
0d381245 7437{
28010a5d 7438 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7439
3742cc8b 7440 add_location_to_breakpoint (b, &sal);
0d381245 7441
6c95b8df
PA
7442 if (bptype != bp_catchpoint)
7443 gdb_assert (sal.pspace != NULL);
7444
f8eba3c6
TT
7445 /* Store the program space that was used to set the breakpoint,
7446 except for ordinary breakpoints, which are independent of the
7447 program space. */
7448 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7449 b->pspace = sal.pspace;
346774a9 7450}
c906108c 7451
346774a9
PA
7452/* set_raw_breakpoint is a low level routine for allocating and
7453 partially initializing a breakpoint of type BPTYPE. The newly
7454 created breakpoint's address, section, source file name, and line
7455 number are provided by SAL. The newly created and partially
7456 initialized breakpoint is added to the breakpoint chain and
7457 is also returned as the value of this function.
7458
7459 It is expected that the caller will complete the initialization of
7460 the newly created breakpoint struct as well as output any status
7461 information regarding the creation of a new breakpoint. In
7462 particular, set_raw_breakpoint does NOT set the breakpoint
7463 number! Care should be taken to not allow an error to occur
7464 prior to completing the initialization of the breakpoint. If this
7465 should happen, a bogus breakpoint will be left on the chain. */
7466
7467struct breakpoint *
7468set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7469 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7470 const struct breakpoint_ops *ops)
346774a9
PA
7471{
7472 struct breakpoint *b = XNEW (struct breakpoint);
7473
348d480f 7474 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7475 add_to_breakpoint_chain (b);
c906108c
SS
7476 return b;
7477}
7478
53a5351d 7479/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7480 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7481 initiated the operation. */
c906108c
SS
7482
7483void
186c406b 7484set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7485{
35df4500 7486 struct breakpoint *b, *b_tmp;
186c406b 7487 int thread = tp->num;
0fd8e87f
UW
7488
7489 /* To avoid having to rescan all objfile symbols at every step,
7490 we maintain a list of continually-inserted but always disabled
7491 longjmp "master" breakpoints. Here, we simply create momentary
7492 clones of those and enable them for the requested thread. */
35df4500 7493 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7494 if (b->pspace == current_program_space
186c406b
TT
7495 && (b->type == bp_longjmp_master
7496 || b->type == bp_exception_master))
0fd8e87f 7497 {
06edf0c0
PA
7498 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7499 struct breakpoint *clone;
cc59ec59 7500
e2e4d78b
JK
7501 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7502 after their removal. */
06edf0c0 7503 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7504 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7505 clone->thread = thread;
7506 }
186c406b
TT
7507
7508 tp->initiating_frame = frame;
c906108c
SS
7509}
7510
611c83ae 7511/* Delete all longjmp breakpoints from THREAD. */
c906108c 7512void
611c83ae 7513delete_longjmp_breakpoint (int thread)
c906108c 7514{
35df4500 7515 struct breakpoint *b, *b_tmp;
c906108c 7516
35df4500 7517 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7518 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7519 {
7520 if (b->thread == thread)
7521 delete_breakpoint (b);
7522 }
c906108c
SS
7523}
7524
f59f708a
PA
7525void
7526delete_longjmp_breakpoint_at_next_stop (int thread)
7527{
7528 struct breakpoint *b, *b_tmp;
7529
7530 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7531 if (b->type == bp_longjmp || b->type == bp_exception)
7532 {
7533 if (b->thread == thread)
7534 b->disposition = disp_del_at_next_stop;
7535 }
7536}
7537
e2e4d78b
JK
7538/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7539 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7540 pointer to any of them. Return NULL if this system cannot place longjmp
7541 breakpoints. */
7542
7543struct breakpoint *
7544set_longjmp_breakpoint_for_call_dummy (void)
7545{
7546 struct breakpoint *b, *retval = NULL;
7547
7548 ALL_BREAKPOINTS (b)
7549 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7550 {
7551 struct breakpoint *new_b;
7552
7553 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7554 &momentary_breakpoint_ops,
7555 1);
e2e4d78b
JK
7556 new_b->thread = pid_to_thread_id (inferior_ptid);
7557
7558 /* Link NEW_B into the chain of RETVAL breakpoints. */
7559
7560 gdb_assert (new_b->related_breakpoint == new_b);
7561 if (retval == NULL)
7562 retval = new_b;
7563 new_b->related_breakpoint = retval;
7564 while (retval->related_breakpoint != new_b->related_breakpoint)
7565 retval = retval->related_breakpoint;
7566 retval->related_breakpoint = new_b;
7567 }
7568
7569 return retval;
7570}
7571
7572/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7573 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7574 stack.
7575
7576 You should call this function only at places where it is safe to currently
7577 unwind the whole stack. Failed stack unwind would discard live dummy
7578 frames. */
7579
7580void
b67a2c6f 7581check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7582{
7583 struct breakpoint *b, *b_tmp;
7584
7585 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7586 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7587 {
7588 struct breakpoint *dummy_b = b->related_breakpoint;
7589
7590 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7591 dummy_b = dummy_b->related_breakpoint;
7592 if (dummy_b->type != bp_call_dummy
7593 || frame_find_by_id (dummy_b->frame_id) != NULL)
7594 continue;
7595
b67a2c6f 7596 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7597
7598 while (b->related_breakpoint != b)
7599 {
7600 if (b_tmp == b->related_breakpoint)
7601 b_tmp = b->related_breakpoint->next;
7602 delete_breakpoint (b->related_breakpoint);
7603 }
7604 delete_breakpoint (b);
7605 }
7606}
7607
1900040c
MS
7608void
7609enable_overlay_breakpoints (void)
7610{
52f0bd74 7611 struct breakpoint *b;
1900040c
MS
7612
7613 ALL_BREAKPOINTS (b)
7614 if (b->type == bp_overlay_event)
7615 {
7616 b->enable_state = bp_enabled;
44702360 7617 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7618 overlay_events_enabled = 1;
1900040c
MS
7619 }
7620}
7621
7622void
7623disable_overlay_breakpoints (void)
7624{
52f0bd74 7625 struct breakpoint *b;
1900040c
MS
7626
7627 ALL_BREAKPOINTS (b)
7628 if (b->type == bp_overlay_event)
7629 {
7630 b->enable_state = bp_disabled;
44702360 7631 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7632 overlay_events_enabled = 0;
1900040c
MS
7633 }
7634}
7635
aa7d318d
TT
7636/* Set an active std::terminate breakpoint for each std::terminate
7637 master breakpoint. */
7638void
7639set_std_terminate_breakpoint (void)
7640{
35df4500 7641 struct breakpoint *b, *b_tmp;
aa7d318d 7642
35df4500 7643 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7644 if (b->pspace == current_program_space
7645 && b->type == bp_std_terminate_master)
7646 {
06edf0c0 7647 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7648 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7649 }
7650}
7651
7652/* Delete all the std::terminate breakpoints. */
7653void
7654delete_std_terminate_breakpoint (void)
7655{
35df4500 7656 struct breakpoint *b, *b_tmp;
aa7d318d 7657
35df4500 7658 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7659 if (b->type == bp_std_terminate)
7660 delete_breakpoint (b);
7661}
7662
c4093a6a 7663struct breakpoint *
a6d9a66e 7664create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7665{
7666 struct breakpoint *b;
c4093a6a 7667
06edf0c0
PA
7668 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7669 &internal_breakpoint_ops);
7670
b5de0fa7 7671 b->enable_state = bp_enabled;
f00aae0f 7672 /* location has to be used or breakpoint_re_set will delete me. */
a06efdd6 7673 b->location = new_address_location (b->loc->address);
c4093a6a 7674
44702360 7675 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7676
c4093a6a
JM
7677 return b;
7678}
7679
7680void
7681remove_thread_event_breakpoints (void)
7682{
35df4500 7683 struct breakpoint *b, *b_tmp;
c4093a6a 7684
35df4500 7685 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7686 if (b->type == bp_thread_event
7687 && b->loc->pspace == current_program_space)
c4093a6a
JM
7688 delete_breakpoint (b);
7689}
7690
0101ce28
JJ
7691struct lang_and_radix
7692 {
7693 enum language lang;
7694 int radix;
7695 };
7696
4efc6507
DE
7697/* Create a breakpoint for JIT code registration and unregistration. */
7698
7699struct breakpoint *
7700create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7701{
7702 struct breakpoint *b;
7703
06edf0c0
PA
7704 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7705 &internal_breakpoint_ops);
44702360 7706 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7707 return b;
7708}
0101ce28 7709
03673fc7
PP
7710/* Remove JIT code registration and unregistration breakpoint(s). */
7711
7712void
7713remove_jit_event_breakpoints (void)
7714{
7715 struct breakpoint *b, *b_tmp;
7716
7717 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7718 if (b->type == bp_jit_event
7719 && b->loc->pspace == current_program_space)
7720 delete_breakpoint (b);
7721}
7722
cae688ec
JJ
7723void
7724remove_solib_event_breakpoints (void)
7725{
35df4500 7726 struct breakpoint *b, *b_tmp;
cae688ec 7727
35df4500 7728 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7729 if (b->type == bp_shlib_event
7730 && b->loc->pspace == current_program_space)
cae688ec
JJ
7731 delete_breakpoint (b);
7732}
7733
f37f681c
PA
7734/* See breakpoint.h. */
7735
7736void
7737remove_solib_event_breakpoints_at_next_stop (void)
7738{
7739 struct breakpoint *b, *b_tmp;
7740
7741 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7742 if (b->type == bp_shlib_event
7743 && b->loc->pspace == current_program_space)
7744 b->disposition = disp_del_at_next_stop;
7745}
7746
04086b45
PA
7747/* Helper for create_solib_event_breakpoint /
7748 create_and_insert_solib_event_breakpoint. Allows specifying which
7749 INSERT_MODE to pass through to update_global_location_list. */
7750
7751static struct breakpoint *
7752create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7753 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7754{
7755 struct breakpoint *b;
7756
06edf0c0
PA
7757 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7758 &internal_breakpoint_ops);
04086b45 7759 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7760 return b;
7761}
7762
04086b45
PA
7763struct breakpoint *
7764create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7765{
7766 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7767}
7768
f37f681c
PA
7769/* See breakpoint.h. */
7770
7771struct breakpoint *
7772create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7773{
7774 struct breakpoint *b;
7775
04086b45
PA
7776 /* Explicitly tell update_global_location_list to insert
7777 locations. */
7778 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7779 if (!b->loc->inserted)
7780 {
7781 delete_breakpoint (b);
7782 return NULL;
7783 }
7784 return b;
7785}
7786
cae688ec
JJ
7787/* Disable any breakpoints that are on code in shared libraries. Only
7788 apply to enabled breakpoints, disabled ones can just stay disabled. */
7789
7790void
cb851954 7791disable_breakpoints_in_shlibs (void)
cae688ec 7792{
876fa593 7793 struct bp_location *loc, **locp_tmp;
cae688ec 7794
876fa593 7795 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7796 {
2bdf28a0 7797 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7798 struct breakpoint *b = loc->owner;
2bdf28a0 7799
4a64f543
MS
7800 /* We apply the check to all breakpoints, including disabled for
7801 those with loc->duplicate set. This is so that when breakpoint
7802 becomes enabled, or the duplicate is removed, gdb will try to
7803 insert all breakpoints. If we don't set shlib_disabled here,
7804 we'll try to insert those breakpoints and fail. */
1042e4c0 7805 if (((b->type == bp_breakpoint)
508ccb1f 7806 || (b->type == bp_jit_event)
1042e4c0 7807 || (b->type == bp_hardware_breakpoint)
d77f58be 7808 || (is_tracepoint (b)))
6c95b8df 7809 && loc->pspace == current_program_space
0d381245 7810 && !loc->shlib_disabled
6c95b8df 7811 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7812 )
0d381245
VP
7813 {
7814 loc->shlib_disabled = 1;
7815 }
cae688ec
JJ
7816 }
7817}
7818
63644780
NB
7819/* Disable any breakpoints and tracepoints that are in SOLIB upon
7820 notification of unloaded_shlib. Only apply to enabled breakpoints,
7821 disabled ones can just stay disabled. */
84acb35a 7822
75149521 7823static void
84acb35a
JJ
7824disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7825{
876fa593 7826 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7827 int disabled_shlib_breaks = 0;
7828
c86cf029
VP
7829 /* SunOS a.out shared libraries are always mapped, so do not
7830 disable breakpoints; they will only be reported as unloaded
7831 through clear_solib when GDB discards its shared library
7832 list. See clear_solib for more information. */
7833 if (exec_bfd != NULL
7834 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7835 return;
7836
876fa593 7837 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7838 {
2bdf28a0 7839 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7840 struct breakpoint *b = loc->owner;
cc59ec59 7841
1e4d1764 7842 if (solib->pspace == loc->pspace
e2dd7057 7843 && !loc->shlib_disabled
1e4d1764
YQ
7844 && (((b->type == bp_breakpoint
7845 || b->type == bp_jit_event
7846 || b->type == bp_hardware_breakpoint)
7847 && (loc->loc_type == bp_loc_hardware_breakpoint
7848 || loc->loc_type == bp_loc_software_breakpoint))
7849 || is_tracepoint (b))
e2dd7057 7850 && solib_contains_address_p (solib, loc->address))
84acb35a 7851 {
e2dd7057
PP
7852 loc->shlib_disabled = 1;
7853 /* At this point, we cannot rely on remove_breakpoint
7854 succeeding so we must mark the breakpoint as not inserted
7855 to prevent future errors occurring in remove_breakpoints. */
7856 loc->inserted = 0;
8d3788bd
VP
7857
7858 /* This may cause duplicate notifications for the same breakpoint. */
7859 observer_notify_breakpoint_modified (b);
7860
e2dd7057
PP
7861 if (!disabled_shlib_breaks)
7862 {
7863 target_terminal_ours_for_output ();
3e43a32a
MS
7864 warning (_("Temporarily disabling breakpoints "
7865 "for unloaded shared library \"%s\""),
e2dd7057 7866 solib->so_name);
84acb35a 7867 }
e2dd7057 7868 disabled_shlib_breaks = 1;
84acb35a
JJ
7869 }
7870 }
84acb35a
JJ
7871}
7872
63644780
NB
7873/* Disable any breakpoints and tracepoints in OBJFILE upon
7874 notification of free_objfile. Only apply to enabled breakpoints,
7875 disabled ones can just stay disabled. */
7876
7877static void
7878disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7879{
7880 struct breakpoint *b;
7881
7882 if (objfile == NULL)
7883 return;
7884
d03de421
PA
7885 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7886 managed by the user with add-symbol-file/remove-symbol-file.
7887 Similarly to how breakpoints in shared libraries are handled in
7888 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7889 shlib_disabled so they end up uninserted on the next global
7890 location list update. Shared libraries not loaded by the user
7891 aren't handled here -- they're already handled in
7892 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7893 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7894 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7895 main objfile). */
7896 if ((objfile->flags & OBJF_SHARED) == 0
7897 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7898 return;
7899
7900 ALL_BREAKPOINTS (b)
7901 {
7902 struct bp_location *loc;
7903 int bp_modified = 0;
7904
7905 if (!is_breakpoint (b) && !is_tracepoint (b))
7906 continue;
7907
7908 for (loc = b->loc; loc != NULL; loc = loc->next)
7909 {
7910 CORE_ADDR loc_addr = loc->address;
7911
7912 if (loc->loc_type != bp_loc_hardware_breakpoint
7913 && loc->loc_type != bp_loc_software_breakpoint)
7914 continue;
7915
7916 if (loc->shlib_disabled != 0)
7917 continue;
7918
7919 if (objfile->pspace != loc->pspace)
7920 continue;
7921
7922 if (loc->loc_type != bp_loc_hardware_breakpoint
7923 && loc->loc_type != bp_loc_software_breakpoint)
7924 continue;
7925
7926 if (is_addr_in_objfile (loc_addr, objfile))
7927 {
7928 loc->shlib_disabled = 1;
08351840
PA
7929 /* At this point, we don't know whether the object was
7930 unmapped from the inferior or not, so leave the
7931 inserted flag alone. We'll handle failure to
7932 uninsert quietly, in case the object was indeed
7933 unmapped. */
63644780
NB
7934
7935 mark_breakpoint_location_modified (loc);
7936
7937 bp_modified = 1;
7938 }
7939 }
7940
7941 if (bp_modified)
7942 observer_notify_breakpoint_modified (b);
7943 }
7944}
7945
ce78b96d
JB
7946/* FORK & VFORK catchpoints. */
7947
e29a4733
PA
7948/* An instance of this type is used to represent a fork or vfork
7949 catchpoint. It includes a "struct breakpoint" as a kind of base
7950 class; users downcast to "struct breakpoint *" when needed. A
7951 breakpoint is really of this type iff its ops pointer points to
7952 CATCH_FORK_BREAKPOINT_OPS. */
7953
7954struct fork_catchpoint
7955{
7956 /* The base class. */
7957 struct breakpoint base;
7958
7959 /* Process id of a child process whose forking triggered this
7960 catchpoint. This field is only valid immediately after this
7961 catchpoint has triggered. */
7962 ptid_t forked_inferior_pid;
7963};
7964
4a64f543
MS
7965/* Implement the "insert" breakpoint_ops method for fork
7966 catchpoints. */
ce78b96d 7967
77b06cd7
TJB
7968static int
7969insert_catch_fork (struct bp_location *bl)
ce78b96d 7970{
dfd4cc63 7971 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7972}
7973
4a64f543
MS
7974/* Implement the "remove" breakpoint_ops method for fork
7975 catchpoints. */
ce78b96d
JB
7976
7977static int
77b06cd7 7978remove_catch_fork (struct bp_location *bl)
ce78b96d 7979{
dfd4cc63 7980 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7981}
7982
7983/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7984 catchpoints. */
7985
7986static int
f1310107 7987breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7988 struct address_space *aspace, CORE_ADDR bp_addr,
7989 const struct target_waitstatus *ws)
ce78b96d 7990{
e29a4733
PA
7991 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7992
f90263c1
TT
7993 if (ws->kind != TARGET_WAITKIND_FORKED)
7994 return 0;
7995
7996 c->forked_inferior_pid = ws->value.related_pid;
7997 return 1;
ce78b96d
JB
7998}
7999
4a64f543
MS
8000/* Implement the "print_it" breakpoint_ops method for fork
8001 catchpoints. */
ce78b96d
JB
8002
8003static enum print_stop_action
348d480f 8004print_it_catch_fork (bpstat bs)
ce78b96d 8005{
36dfb11c 8006 struct ui_out *uiout = current_uiout;
348d480f
PA
8007 struct breakpoint *b = bs->breakpoint_at;
8008 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8009
ce78b96d 8010 annotate_catchpoint (b->number);
36dfb11c
TT
8011 if (b->disposition == disp_del)
8012 ui_out_text (uiout, "\nTemporary catchpoint ");
8013 else
8014 ui_out_text (uiout, "\nCatchpoint ");
8015 if (ui_out_is_mi_like_p (uiout))
8016 {
8017 ui_out_field_string (uiout, "reason",
8018 async_reason_lookup (EXEC_ASYNC_FORK));
8019 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8020 }
8021 ui_out_field_int (uiout, "bkptno", b->number);
8022 ui_out_text (uiout, " (forked process ");
8023 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8024 ui_out_text (uiout, "), ");
ce78b96d
JB
8025 return PRINT_SRC_AND_LOC;
8026}
8027
4a64f543
MS
8028/* Implement the "print_one" breakpoint_ops method for fork
8029 catchpoints. */
ce78b96d
JB
8030
8031static void
a6d9a66e 8032print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8033{
e29a4733 8034 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8035 struct value_print_options opts;
79a45e25 8036 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8037
8038 get_user_print_options (&opts);
8039
4a64f543
MS
8040 /* Field 4, the address, is omitted (which makes the columns not
8041 line up too nicely with the headers, but the effect is relatively
8042 readable). */
79a45b7d 8043 if (opts.addressprint)
ce78b96d
JB
8044 ui_out_field_skip (uiout, "addr");
8045 annotate_field (5);
8046 ui_out_text (uiout, "fork");
e29a4733 8047 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8048 {
8049 ui_out_text (uiout, ", process ");
8050 ui_out_field_int (uiout, "what",
e29a4733 8051 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8052 ui_out_spaces (uiout, 1);
8053 }
8ac3646f
TT
8054
8055 if (ui_out_is_mi_like_p (uiout))
8056 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8057}
8058
8059/* Implement the "print_mention" breakpoint_ops method for fork
8060 catchpoints. */
8061
8062static void
8063print_mention_catch_fork (struct breakpoint *b)
8064{
8065 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8066}
8067
6149aea9
PA
8068/* Implement the "print_recreate" breakpoint_ops method for fork
8069 catchpoints. */
8070
8071static void
8072print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8073{
8074 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8075 print_recreate_thread (b, fp);
6149aea9
PA
8076}
8077
ce78b96d
JB
8078/* The breakpoint_ops structure to be used in fork catchpoints. */
8079
2060206e 8080static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8081
4a64f543
MS
8082/* Implement the "insert" breakpoint_ops method for vfork
8083 catchpoints. */
ce78b96d 8084
77b06cd7
TJB
8085static int
8086insert_catch_vfork (struct bp_location *bl)
ce78b96d 8087{
dfd4cc63 8088 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8089}
8090
4a64f543
MS
8091/* Implement the "remove" breakpoint_ops method for vfork
8092 catchpoints. */
ce78b96d
JB
8093
8094static int
77b06cd7 8095remove_catch_vfork (struct bp_location *bl)
ce78b96d 8096{
dfd4cc63 8097 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8098}
8099
8100/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8101 catchpoints. */
8102
8103static int
f1310107 8104breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8105 struct address_space *aspace, CORE_ADDR bp_addr,
8106 const struct target_waitstatus *ws)
ce78b96d 8107{
e29a4733
PA
8108 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8109
f90263c1
TT
8110 if (ws->kind != TARGET_WAITKIND_VFORKED)
8111 return 0;
8112
8113 c->forked_inferior_pid = ws->value.related_pid;
8114 return 1;
ce78b96d
JB
8115}
8116
4a64f543
MS
8117/* Implement the "print_it" breakpoint_ops method for vfork
8118 catchpoints. */
ce78b96d
JB
8119
8120static enum print_stop_action
348d480f 8121print_it_catch_vfork (bpstat bs)
ce78b96d 8122{
36dfb11c 8123 struct ui_out *uiout = current_uiout;
348d480f 8124 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8125 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8126
ce78b96d 8127 annotate_catchpoint (b->number);
36dfb11c
TT
8128 if (b->disposition == disp_del)
8129 ui_out_text (uiout, "\nTemporary catchpoint ");
8130 else
8131 ui_out_text (uiout, "\nCatchpoint ");
8132 if (ui_out_is_mi_like_p (uiout))
8133 {
8134 ui_out_field_string (uiout, "reason",
8135 async_reason_lookup (EXEC_ASYNC_VFORK));
8136 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8137 }
8138 ui_out_field_int (uiout, "bkptno", b->number);
8139 ui_out_text (uiout, " (vforked process ");
8140 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8141 ui_out_text (uiout, "), ");
ce78b96d
JB
8142 return PRINT_SRC_AND_LOC;
8143}
8144
4a64f543
MS
8145/* Implement the "print_one" breakpoint_ops method for vfork
8146 catchpoints. */
ce78b96d
JB
8147
8148static void
a6d9a66e 8149print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8150{
e29a4733 8151 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8152 struct value_print_options opts;
79a45e25 8153 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8154
8155 get_user_print_options (&opts);
4a64f543
MS
8156 /* Field 4, the address, is omitted (which makes the columns not
8157 line up too nicely with the headers, but the effect is relatively
8158 readable). */
79a45b7d 8159 if (opts.addressprint)
ce78b96d
JB
8160 ui_out_field_skip (uiout, "addr");
8161 annotate_field (5);
8162 ui_out_text (uiout, "vfork");
e29a4733 8163 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8164 {
8165 ui_out_text (uiout, ", process ");
8166 ui_out_field_int (uiout, "what",
e29a4733 8167 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8168 ui_out_spaces (uiout, 1);
8169 }
8ac3646f
TT
8170
8171 if (ui_out_is_mi_like_p (uiout))
8172 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8173}
8174
8175/* Implement the "print_mention" breakpoint_ops method for vfork
8176 catchpoints. */
8177
8178static void
8179print_mention_catch_vfork (struct breakpoint *b)
8180{
8181 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8182}
8183
6149aea9
PA
8184/* Implement the "print_recreate" breakpoint_ops method for vfork
8185 catchpoints. */
8186
8187static void
8188print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8189{
8190 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8191 print_recreate_thread (b, fp);
6149aea9
PA
8192}
8193
ce78b96d
JB
8194/* The breakpoint_ops structure to be used in vfork catchpoints. */
8195
2060206e 8196static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8197
edcc5120
TT
8198/* An instance of this type is used to represent an solib catchpoint.
8199 It includes a "struct breakpoint" as a kind of base class; users
8200 downcast to "struct breakpoint *" when needed. A breakpoint is
8201 really of this type iff its ops pointer points to
8202 CATCH_SOLIB_BREAKPOINT_OPS. */
8203
8204struct solib_catchpoint
8205{
8206 /* The base class. */
8207 struct breakpoint base;
8208
8209 /* True for "catch load", false for "catch unload". */
8210 unsigned char is_load;
8211
8212 /* Regular expression to match, if any. COMPILED is only valid when
8213 REGEX is non-NULL. */
8214 char *regex;
8215 regex_t compiled;
8216};
8217
8218static void
8219dtor_catch_solib (struct breakpoint *b)
8220{
8221 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8222
8223 if (self->regex)
8224 regfree (&self->compiled);
8225 xfree (self->regex);
8226
8227 base_breakpoint_ops.dtor (b);
8228}
8229
8230static int
8231insert_catch_solib (struct bp_location *ignore)
8232{
8233 return 0;
8234}
8235
8236static int
8237remove_catch_solib (struct bp_location *ignore)
8238{
8239 return 0;
8240}
8241
8242static int
8243breakpoint_hit_catch_solib (const struct bp_location *bl,
8244 struct address_space *aspace,
8245 CORE_ADDR bp_addr,
8246 const struct target_waitstatus *ws)
8247{
8248 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8249 struct breakpoint *other;
8250
8251 if (ws->kind == TARGET_WAITKIND_LOADED)
8252 return 1;
8253
8254 ALL_BREAKPOINTS (other)
8255 {
8256 struct bp_location *other_bl;
8257
8258 if (other == bl->owner)
8259 continue;
8260
8261 if (other->type != bp_shlib_event)
8262 continue;
8263
8264 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8265 continue;
8266
8267 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8268 {
8269 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8270 return 1;
8271 }
8272 }
8273
8274 return 0;
8275}
8276
8277static void
8278check_status_catch_solib (struct bpstats *bs)
8279{
8280 struct solib_catchpoint *self
8281 = (struct solib_catchpoint *) bs->breakpoint_at;
8282 int ix;
8283
8284 if (self->is_load)
8285 {
8286 struct so_list *iter;
8287
8288 for (ix = 0;
8289 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8290 ix, iter);
8291 ++ix)
8292 {
8293 if (!self->regex
8294 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8295 return;
8296 }
8297 }
8298 else
8299 {
8300 char *iter;
8301
8302 for (ix = 0;
8303 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8304 ix, iter);
8305 ++ix)
8306 {
8307 if (!self->regex
8308 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8309 return;
8310 }
8311 }
8312
8313 bs->stop = 0;
8314 bs->print_it = print_it_noop;
8315}
8316
8317static enum print_stop_action
8318print_it_catch_solib (bpstat bs)
8319{
8320 struct breakpoint *b = bs->breakpoint_at;
8321 struct ui_out *uiout = current_uiout;
8322
8323 annotate_catchpoint (b->number);
8324 if (b->disposition == disp_del)
8325 ui_out_text (uiout, "\nTemporary catchpoint ");
8326 else
8327 ui_out_text (uiout, "\nCatchpoint ");
8328 ui_out_field_int (uiout, "bkptno", b->number);
8329 ui_out_text (uiout, "\n");
8330 if (ui_out_is_mi_like_p (uiout))
8331 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8332 print_solib_event (1);
8333 return PRINT_SRC_AND_LOC;
8334}
8335
8336static void
8337print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8338{
8339 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8340 struct value_print_options opts;
8341 struct ui_out *uiout = current_uiout;
8342 char *msg;
8343
8344 get_user_print_options (&opts);
8345 /* Field 4, the address, is omitted (which makes the columns not
8346 line up too nicely with the headers, but the effect is relatively
8347 readable). */
8348 if (opts.addressprint)
8349 {
8350 annotate_field (4);
8351 ui_out_field_skip (uiout, "addr");
8352 }
8353
8354 annotate_field (5);
8355 if (self->is_load)
8356 {
8357 if (self->regex)
8358 msg = xstrprintf (_("load of library matching %s"), self->regex);
8359 else
8360 msg = xstrdup (_("load of library"));
8361 }
8362 else
8363 {
8364 if (self->regex)
8365 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8366 else
8367 msg = xstrdup (_("unload of library"));
8368 }
8369 ui_out_field_string (uiout, "what", msg);
8370 xfree (msg);
8ac3646f
TT
8371
8372 if (ui_out_is_mi_like_p (uiout))
8373 ui_out_field_string (uiout, "catch-type",
8374 self->is_load ? "load" : "unload");
edcc5120
TT
8375}
8376
8377static void
8378print_mention_catch_solib (struct breakpoint *b)
8379{
8380 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8381
8382 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8383 self->is_load ? "load" : "unload");
8384}
8385
8386static void
8387print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8388{
8389 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8390
8391 fprintf_unfiltered (fp, "%s %s",
8392 b->disposition == disp_del ? "tcatch" : "catch",
8393 self->is_load ? "load" : "unload");
8394 if (self->regex)
8395 fprintf_unfiltered (fp, " %s", self->regex);
8396 fprintf_unfiltered (fp, "\n");
8397}
8398
8399static struct breakpoint_ops catch_solib_breakpoint_ops;
8400
91985142
MG
8401/* Shared helper function (MI and CLI) for creating and installing
8402 a shared object event catchpoint. If IS_LOAD is non-zero then
8403 the events to be caught are load events, otherwise they are
8404 unload events. If IS_TEMP is non-zero the catchpoint is a
8405 temporary one. If ENABLED is non-zero the catchpoint is
8406 created in an enabled state. */
edcc5120 8407
91985142
MG
8408void
8409add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8410{
8411 struct solib_catchpoint *c;
8412 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8413 struct cleanup *cleanup;
8414
edcc5120
TT
8415 if (!arg)
8416 arg = "";
8417 arg = skip_spaces (arg);
8418
8419 c = XCNEW (struct solib_catchpoint);
8420 cleanup = make_cleanup (xfree, c);
8421
8422 if (*arg != '\0')
8423 {
8424 int errcode;
8425
8426 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8427 if (errcode != 0)
8428 {
8429 char *err = get_regcomp_error (errcode, &c->compiled);
8430
8431 make_cleanup (xfree, err);
8432 error (_("Invalid regexp (%s): %s"), err, arg);
8433 }
8434 c->regex = xstrdup (arg);
8435 }
8436
8437 c->is_load = is_load;
91985142 8438 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8439 &catch_solib_breakpoint_ops);
8440
91985142
MG
8441 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8442
edcc5120
TT
8443 discard_cleanups (cleanup);
8444 install_breakpoint (0, &c->base, 1);
8445}
8446
91985142
MG
8447/* A helper function that does all the work for "catch load" and
8448 "catch unload". */
8449
8450static void
8451catch_load_or_unload (char *arg, int from_tty, int is_load,
8452 struct cmd_list_element *command)
8453{
8454 int tempflag;
8455 const int enabled = 1;
8456
8457 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8458
8459 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8460}
8461
edcc5120
TT
8462static void
8463catch_load_command_1 (char *arg, int from_tty,
8464 struct cmd_list_element *command)
8465{
8466 catch_load_or_unload (arg, from_tty, 1, command);
8467}
8468
8469static void
8470catch_unload_command_1 (char *arg, int from_tty,
8471 struct cmd_list_element *command)
8472{
8473 catch_load_or_unload (arg, from_tty, 0, command);
8474}
8475
346774a9
PA
8476/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8477 is non-zero, then make the breakpoint temporary. If COND_STRING is
8478 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8479 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8480
ab04a2af 8481void
346774a9
PA
8482init_catchpoint (struct breakpoint *b,
8483 struct gdbarch *gdbarch, int tempflag,
8484 char *cond_string,
c0a91b2b 8485 const struct breakpoint_ops *ops)
c906108c 8486{
c5aa993b 8487 struct symtab_and_line sal;
346774a9 8488
fe39c653 8489 init_sal (&sal);
6c95b8df 8490 sal.pspace = current_program_space;
c5aa993b 8491
28010a5d 8492 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8493
1b36a34b 8494 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8495 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8496}
8497
28010a5d 8498void
3ea46bff 8499install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8500{
8501 add_to_breakpoint_chain (b);
3a5c3e22 8502 set_breakpoint_number (internal, b);
558a9d82
YQ
8503 if (is_tracepoint (b))
8504 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8505 if (!internal)
8506 mention (b);
c56053d2 8507 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8508
8509 if (update_gll)
44702360 8510 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8511}
8512
9b70b993 8513static void
a6d9a66e
UW
8514create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8515 int tempflag, char *cond_string,
c0a91b2b 8516 const struct breakpoint_ops *ops)
c906108c 8517{
e29a4733 8518 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8519
e29a4733
PA
8520 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8521
8522 c->forked_inferior_pid = null_ptid;
8523
3ea46bff 8524 install_breakpoint (0, &c->base, 1);
c906108c
SS
8525}
8526
fe798b75
JB
8527/* Exec catchpoints. */
8528
b4d90040
PA
8529/* An instance of this type is used to represent an exec catchpoint.
8530 It includes a "struct breakpoint" as a kind of base class; users
8531 downcast to "struct breakpoint *" when needed. A breakpoint is
8532 really of this type iff its ops pointer points to
8533 CATCH_EXEC_BREAKPOINT_OPS. */
8534
8535struct exec_catchpoint
8536{
8537 /* The base class. */
8538 struct breakpoint base;
8539
8540 /* Filename of a program whose exec triggered this catchpoint.
8541 This field is only valid immediately after this catchpoint has
8542 triggered. */
8543 char *exec_pathname;
8544};
8545
8546/* Implement the "dtor" breakpoint_ops method for exec
8547 catchpoints. */
8548
8549static void
8550dtor_catch_exec (struct breakpoint *b)
8551{
8552 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8553
8554 xfree (c->exec_pathname);
348d480f 8555
2060206e 8556 base_breakpoint_ops.dtor (b);
b4d90040
PA
8557}
8558
77b06cd7
TJB
8559static int
8560insert_catch_exec (struct bp_location *bl)
c906108c 8561{
dfd4cc63 8562 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8563}
c906108c 8564
fe798b75 8565static int
77b06cd7 8566remove_catch_exec (struct bp_location *bl)
fe798b75 8567{
dfd4cc63 8568 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8569}
c906108c 8570
fe798b75 8571static int
f1310107 8572breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8573 struct address_space *aspace, CORE_ADDR bp_addr,
8574 const struct target_waitstatus *ws)
fe798b75 8575{
b4d90040
PA
8576 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8577
f90263c1
TT
8578 if (ws->kind != TARGET_WAITKIND_EXECD)
8579 return 0;
8580
8581 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8582 return 1;
fe798b75 8583}
c906108c 8584
fe798b75 8585static enum print_stop_action
348d480f 8586print_it_catch_exec (bpstat bs)
fe798b75 8587{
36dfb11c 8588 struct ui_out *uiout = current_uiout;
348d480f 8589 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8590 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8591
fe798b75 8592 annotate_catchpoint (b->number);
36dfb11c
TT
8593 if (b->disposition == disp_del)
8594 ui_out_text (uiout, "\nTemporary catchpoint ");
8595 else
8596 ui_out_text (uiout, "\nCatchpoint ");
8597 if (ui_out_is_mi_like_p (uiout))
8598 {
8599 ui_out_field_string (uiout, "reason",
8600 async_reason_lookup (EXEC_ASYNC_EXEC));
8601 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8602 }
8603 ui_out_field_int (uiout, "bkptno", b->number);
8604 ui_out_text (uiout, " (exec'd ");
8605 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8606 ui_out_text (uiout, "), ");
8607
fe798b75 8608 return PRINT_SRC_AND_LOC;
c906108c
SS
8609}
8610
fe798b75 8611static void
a6d9a66e 8612print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8613{
b4d90040 8614 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8615 struct value_print_options opts;
79a45e25 8616 struct ui_out *uiout = current_uiout;
fe798b75
JB
8617
8618 get_user_print_options (&opts);
8619
8620 /* Field 4, the address, is omitted (which makes the columns
8621 not line up too nicely with the headers, but the effect
8622 is relatively readable). */
8623 if (opts.addressprint)
8624 ui_out_field_skip (uiout, "addr");
8625 annotate_field (5);
8626 ui_out_text (uiout, "exec");
b4d90040 8627 if (c->exec_pathname != NULL)
fe798b75
JB
8628 {
8629 ui_out_text (uiout, ", program \"");
b4d90040 8630 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8631 ui_out_text (uiout, "\" ");
8632 }
8ac3646f
TT
8633
8634 if (ui_out_is_mi_like_p (uiout))
8635 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8636}
8637
8638static void
8639print_mention_catch_exec (struct breakpoint *b)
8640{
8641 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8642}
8643
6149aea9
PA
8644/* Implement the "print_recreate" breakpoint_ops method for exec
8645 catchpoints. */
8646
8647static void
8648print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8649{
8650 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8651 print_recreate_thread (b, fp);
6149aea9
PA
8652}
8653
2060206e 8654static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8655
c906108c 8656static int
fba45db2 8657hw_breakpoint_used_count (void)
c906108c 8658{
c906108c 8659 int i = 0;
f1310107
TJB
8660 struct breakpoint *b;
8661 struct bp_location *bl;
c906108c
SS
8662
8663 ALL_BREAKPOINTS (b)
c5aa993b 8664 {
d6b74ac4 8665 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8666 for (bl = b->loc; bl; bl = bl->next)
8667 {
8668 /* Special types of hardware breakpoints may use more than
8669 one register. */
348d480f 8670 i += b->ops->resources_needed (bl);
f1310107 8671 }
c5aa993b 8672 }
c906108c
SS
8673
8674 return i;
8675}
8676
a1398e0c
PA
8677/* Returns the resources B would use if it were a hardware
8678 watchpoint. */
8679
c906108c 8680static int
a1398e0c 8681hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8682{
c906108c 8683 int i = 0;
e09342b5 8684 struct bp_location *bl;
c906108c 8685
a1398e0c
PA
8686 if (!breakpoint_enabled (b))
8687 return 0;
8688
8689 for (bl = b->loc; bl; bl = bl->next)
8690 {
8691 /* Special types of hardware watchpoints may use more than
8692 one register. */
8693 i += b->ops->resources_needed (bl);
8694 }
8695
8696 return i;
8697}
8698
8699/* Returns the sum the used resources of all hardware watchpoints of
8700 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8701 the sum of the used resources of all hardware watchpoints of other
8702 types _not_ TYPE. */
8703
8704static int
8705hw_watchpoint_used_count_others (struct breakpoint *except,
8706 enum bptype type, int *other_type_used)
8707{
8708 int i = 0;
8709 struct breakpoint *b;
8710
c906108c
SS
8711 *other_type_used = 0;
8712 ALL_BREAKPOINTS (b)
e09342b5 8713 {
a1398e0c
PA
8714 if (b == except)
8715 continue;
e09342b5
TJB
8716 if (!breakpoint_enabled (b))
8717 continue;
8718
a1398e0c
PA
8719 if (b->type == type)
8720 i += hw_watchpoint_use_count (b);
8721 else if (is_hardware_watchpoint (b))
8722 *other_type_used = 1;
e09342b5
TJB
8723 }
8724
c906108c
SS
8725 return i;
8726}
8727
c906108c 8728void
fba45db2 8729disable_watchpoints_before_interactive_call_start (void)
c906108c 8730{
c5aa993b 8731 struct breakpoint *b;
c906108c
SS
8732
8733 ALL_BREAKPOINTS (b)
c5aa993b 8734 {
cc60f2e3 8735 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8736 {
b5de0fa7 8737 b->enable_state = bp_call_disabled;
44702360 8738 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
8739 }
8740 }
c906108c
SS
8741}
8742
8743void
fba45db2 8744enable_watchpoints_after_interactive_call_stop (void)
c906108c 8745{
c5aa993b 8746 struct breakpoint *b;
c906108c
SS
8747
8748 ALL_BREAKPOINTS (b)
c5aa993b 8749 {
cc60f2e3 8750 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8751 {
b5de0fa7 8752 b->enable_state = bp_enabled;
44702360 8753 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
8754 }
8755 }
c906108c
SS
8756}
8757
8bea4e01
UW
8758void
8759disable_breakpoints_before_startup (void)
8760{
6c95b8df 8761 current_program_space->executing_startup = 1;
44702360 8762 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
8763}
8764
8765void
8766enable_breakpoints_after_startup (void)
8767{
6c95b8df 8768 current_program_space->executing_startup = 0;
f8eba3c6 8769 breakpoint_re_set ();
8bea4e01
UW
8770}
8771
7c16b83e
PA
8772/* Create a new single-step breakpoint for thread THREAD, with no
8773 locations. */
c906108c 8774
7c16b83e
PA
8775static struct breakpoint *
8776new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8777{
8778 struct breakpoint *b = XNEW (struct breakpoint);
8779
8780 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8781 &momentary_breakpoint_ops);
8782
8783 b->disposition = disp_donttouch;
8784 b->frame_id = null_frame_id;
8785
8786 b->thread = thread;
8787 gdb_assert (b->thread != 0);
8788
8789 add_to_breakpoint_chain (b);
8790
8791 return b;
8792}
8793
8794/* Set a momentary breakpoint of type TYPE at address specified by
8795 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8796 frame. */
c906108c
SS
8797
8798struct breakpoint *
a6d9a66e
UW
8799set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8800 struct frame_id frame_id, enum bptype type)
c906108c 8801{
52f0bd74 8802 struct breakpoint *b;
edb3359d 8803
193facb3
JK
8804 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8805 tail-called one. */
8806 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 8807
06edf0c0 8808 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8809 b->enable_state = bp_enabled;
8810 b->disposition = disp_donttouch;
818dd999 8811 b->frame_id = frame_id;
c906108c 8812
4a64f543
MS
8813 /* If we're debugging a multi-threaded program, then we want
8814 momentary breakpoints to be active in only a single thread of
8815 control. */
39f77062
KB
8816 if (in_thread_list (inferior_ptid))
8817 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 8818
44702360 8819 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 8820
c906108c
SS
8821 return b;
8822}
611c83ae 8823
06edf0c0 8824/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
8825 The new breakpoint will have type TYPE, use OPS as its
8826 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 8827
06edf0c0
PA
8828static struct breakpoint *
8829momentary_breakpoint_from_master (struct breakpoint *orig,
8830 enum bptype type,
a1aa2221
LM
8831 const struct breakpoint_ops *ops,
8832 int loc_enabled)
e58b0e63
PA
8833{
8834 struct breakpoint *copy;
8835
06edf0c0 8836 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8837 copy->loc = allocate_bp_location (copy);
0e30163f 8838 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8839
a6d9a66e 8840 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8841 copy->loc->requested_address = orig->loc->requested_address;
8842 copy->loc->address = orig->loc->address;
8843 copy->loc->section = orig->loc->section;
6c95b8df 8844 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8845 copy->loc->probe = orig->loc->probe;
f8eba3c6 8846 copy->loc->line_number = orig->loc->line_number;
2f202fde 8847 copy->loc->symtab = orig->loc->symtab;
a1aa2221 8848 copy->loc->enabled = loc_enabled;
e58b0e63
PA
8849 copy->frame_id = orig->frame_id;
8850 copy->thread = orig->thread;
6c95b8df 8851 copy->pspace = orig->pspace;
e58b0e63
PA
8852
8853 copy->enable_state = bp_enabled;
8854 copy->disposition = disp_donttouch;
8855 copy->number = internal_breakpoint_number--;
8856
44702360 8857 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
8858 return copy;
8859}
8860
06edf0c0
PA
8861/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8862 ORIG is NULL. */
8863
8864struct breakpoint *
8865clone_momentary_breakpoint (struct breakpoint *orig)
8866{
8867 /* If there's nothing to clone, then return nothing. */
8868 if (orig == NULL)
8869 return NULL;
8870
a1aa2221 8871 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
8872}
8873
611c83ae 8874struct breakpoint *
a6d9a66e
UW
8875set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8876 enum bptype type)
611c83ae
PA
8877{
8878 struct symtab_and_line sal;
8879
8880 sal = find_pc_line (pc, 0);
8881 sal.pc = pc;
8882 sal.section = find_pc_overlay (pc);
8883 sal.explicit_pc = 1;
8884
a6d9a66e 8885 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8886}
c906108c 8887\f
c5aa993b 8888
c906108c
SS
8889/* Tell the user we have just set a breakpoint B. */
8890
8891static void
fba45db2 8892mention (struct breakpoint *b)
c906108c 8893{
348d480f 8894 b->ops->print_mention (b);
79a45e25 8895 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 8896 return;
c906108c
SS
8897 printf_filtered ("\n");
8898}
c906108c 8899\f
c5aa993b 8900
1a853c52
PA
8901static int bp_loc_is_permanent (struct bp_location *loc);
8902
0d381245 8903static struct bp_location *
39d61571 8904add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8905 const struct symtab_and_line *sal)
8906{
8907 struct bp_location *loc, **tmp;
3742cc8b
YQ
8908 CORE_ADDR adjusted_address;
8909 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8910
8911 if (loc_gdbarch == NULL)
8912 loc_gdbarch = b->gdbarch;
8913
8914 /* Adjust the breakpoint's address prior to allocating a location.
8915 Once we call allocate_bp_location(), that mostly uninitialized
8916 location will be placed on the location chain. Adjustment of the
8917 breakpoint may cause target_read_memory() to be called and we do
8918 not want its scan of the location chain to find a breakpoint and
8919 location that's only been partially initialized. */
8920 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8921 sal->pc, b->type);
0d381245 8922
d30113d4 8923 /* Sort the locations by their ADDRESS. */
39d61571 8924 loc = allocate_bp_location (b);
d30113d4
JK
8925 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8926 tmp = &((*tmp)->next))
0d381245 8927 ;
d30113d4 8928 loc->next = *tmp;
0d381245 8929 *tmp = loc;
3742cc8b 8930
0d381245 8931 loc->requested_address = sal->pc;
3742cc8b 8932 loc->address = adjusted_address;
6c95b8df 8933 loc->pspace = sal->pspace;
729662a5
TT
8934 loc->probe.probe = sal->probe;
8935 loc->probe.objfile = sal->objfile;
6c95b8df 8936 gdb_assert (loc->pspace != NULL);
0d381245 8937 loc->section = sal->section;
3742cc8b 8938 loc->gdbarch = loc_gdbarch;
f8eba3c6 8939 loc->line_number = sal->line;
2f202fde 8940 loc->symtab = sal->symtab;
f8eba3c6 8941
0e30163f
JK
8942 set_breakpoint_location_function (loc,
8943 sal->explicit_pc || sal->explicit_line);
1a853c52 8944
6ae88661
LM
8945 /* While by definition, permanent breakpoints are already present in the
8946 code, we don't mark the location as inserted. Normally one would expect
8947 that GDB could rely on that breakpoint instruction to stop the program,
8948 thus removing the need to insert its own breakpoint, except that executing
8949 the breakpoint instruction can kill the target instead of reporting a
8950 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8951 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8952 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8953 breakpoint be inserted normally results in QEMU knowing about the GDB
8954 breakpoint, and thus trap before the breakpoint instruction is executed.
8955 (If GDB later needs to continue execution past the permanent breakpoint,
8956 it manually increments the PC, thus avoiding executing the breakpoint
8957 instruction.) */
1a853c52 8958 if (bp_loc_is_permanent (loc))
6ae88661 8959 loc->permanent = 1;
1a853c52 8960
0d381245
VP
8961 return loc;
8962}
514f746b
AR
8963\f
8964
1cf4d951 8965/* See breakpoint.h. */
514f746b 8966
1cf4d951
PA
8967int
8968program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
514f746b
AR
8969{
8970 int len;
8971 CORE_ADDR addr;
1afeeb75 8972 const gdb_byte *bpoint;
514f746b 8973 gdb_byte *target_mem;
939c61fa
JK
8974 struct cleanup *cleanup;
8975 int retval = 0;
514f746b 8976
1cf4d951
PA
8977 addr = address;
8978 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8979
8980 /* Software breakpoints unsupported? */
8981 if (bpoint == NULL)
8982 return 0;
8983
8984 target_mem = alloca (len);
8985
8986 /* Enable the automatic memory restoration from breakpoints while
8987 we read the memory. Otherwise we could say about our temporary
8988 breakpoints they are permanent. */
8989 cleanup = make_show_memory_breakpoints_cleanup (0);
8990
8991 if (target_read_memory (address, target_mem, len) == 0
8992 && memcmp (target_mem, bpoint, len) == 0)
8993 retval = 1;
8994
8995 do_cleanups (cleanup);
8996
8997 return retval;
8998}
8999
9000/* Return 1 if LOC is pointing to a permanent breakpoint,
9001 return 0 otherwise. */
9002
9003static int
9004bp_loc_is_permanent (struct bp_location *loc)
9005{
9006 struct cleanup *cleanup;
9007 int retval;
9008
514f746b
AR
9009 gdb_assert (loc != NULL);
9010
244558af
LM
9011 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9012 attempt to read from the addresses the locations of these breakpoint types
9013 point to. program_breakpoint_here_p, below, will attempt to read
9014 memory. */
9015 if (!breakpoint_address_is_meaningful (loc->owner))
9016 return 0;
9017
6c95b8df 9018 cleanup = save_current_space_and_thread ();
6c95b8df 9019 switch_to_program_space_and_thread (loc->pspace);
939c61fa 9020
1cf4d951 9021 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
514f746b 9022
939c61fa
JK
9023 do_cleanups (cleanup);
9024
9025 return retval;
514f746b
AR
9026}
9027
e7e0cddf
SS
9028/* Build a command list for the dprintf corresponding to the current
9029 settings of the dprintf style options. */
9030
9031static void
9032update_dprintf_command_list (struct breakpoint *b)
9033{
9034 char *dprintf_args = b->extra_string;
9035 char *printf_line = NULL;
9036
9037 if (!dprintf_args)
9038 return;
9039
9040 dprintf_args = skip_spaces (dprintf_args);
9041
9042 /* Allow a comma, as it may have terminated a location, but don't
9043 insist on it. */
9044 if (*dprintf_args == ',')
9045 ++dprintf_args;
9046 dprintf_args = skip_spaces (dprintf_args);
9047
9048 if (*dprintf_args != '"')
9049 error (_("Bad format string, missing '\"'."));
9050
d3ce09f5 9051 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9052 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9053 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9054 {
9055 if (!dprintf_function)
9056 error (_("No function supplied for dprintf call"));
9057
9058 if (dprintf_channel && strlen (dprintf_channel) > 0)
9059 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9060 dprintf_function,
9061 dprintf_channel,
9062 dprintf_args);
9063 else
9064 printf_line = xstrprintf ("call (void) %s (%s)",
9065 dprintf_function,
9066 dprintf_args);
9067 }
d3ce09f5
SS
9068 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9069 {
9070 if (target_can_run_breakpoint_commands ())
9071 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9072 else
9073 {
9074 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9075 printf_line = xstrprintf ("printf %s", dprintf_args);
9076 }
9077 }
e7e0cddf
SS
9078 else
9079 internal_error (__FILE__, __LINE__,
9080 _("Invalid dprintf style."));
9081
f28045c2 9082 gdb_assert (printf_line != NULL);
9d6e6e84 9083 /* Manufacture a printf sequence. */
f28045c2 9084 {
8d749320 9085 struct command_line *printf_cmd_line = XNEW (struct command_line);
e7e0cddf 9086
f28045c2
YQ
9087 printf_cmd_line->control_type = simple_control;
9088 printf_cmd_line->body_count = 0;
9089 printf_cmd_line->body_list = NULL;
9d6e6e84 9090 printf_cmd_line->next = NULL;
f28045c2 9091 printf_cmd_line->line = printf_line;
e7e0cddf 9092
f28045c2
YQ
9093 breakpoint_set_commands (b, printf_cmd_line);
9094 }
e7e0cddf
SS
9095}
9096
9097/* Update all dprintf commands, making their command lists reflect
9098 current style settings. */
9099
9100static void
9101update_dprintf_commands (char *args, int from_tty,
9102 struct cmd_list_element *c)
9103{
9104 struct breakpoint *b;
9105
9106 ALL_BREAKPOINTS (b)
9107 {
9108 if (b->type == bp_dprintf)
9109 update_dprintf_command_list (b);
9110 }
9111}
c3f6f71d 9112
f00aae0f
KS
9113/* Create a breakpoint with SAL as location. Use LOCATION
9114 as a description of the location, and COND_STRING
db107f19 9115 as condition expression. */
018d34a4
VP
9116
9117static void
d9b3f62e 9118init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
f00aae0f
KS
9119 struct symtabs_and_lines sals,
9120 struct event_location *location,
f8eba3c6 9121 char *filter, char *cond_string,
e7e0cddf 9122 char *extra_string,
d9b3f62e
PA
9123 enum bptype type, enum bpdisp disposition,
9124 int thread, int task, int ignore_count,
c0a91b2b 9125 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9126 int enabled, int internal, unsigned flags,
9127 int display_canonical)
018d34a4 9128{
0d381245 9129 int i;
018d34a4
VP
9130
9131 if (type == bp_hardware_breakpoint)
9132 {
fbbd034e
AS
9133 int target_resources_ok;
9134
9135 i = hw_breakpoint_used_count ();
9136 target_resources_ok =
9137 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9138 i + 1, 0);
9139 if (target_resources_ok == 0)
9140 error (_("No hardware breakpoint support in the target."));
9141 else if (target_resources_ok < 0)
9142 error (_("Hardware breakpoints used exceeds limit."));
9143 }
9144
6c95b8df
PA
9145 gdb_assert (sals.nelts > 0);
9146
0d381245
VP
9147 for (i = 0; i < sals.nelts; ++i)
9148 {
9149 struct symtab_and_line sal = sals.sals[i];
9150 struct bp_location *loc;
9151
9152 if (from_tty)
5af949e3
UW
9153 {
9154 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9155 if (!loc_gdbarch)
9156 loc_gdbarch = gdbarch;
9157
9158 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9159 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9160 }
0d381245
VP
9161
9162 if (i == 0)
9163 {
d9b3f62e 9164 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9165 b->thread = thread;
4a306c9a 9166 b->task = task;
855a6e68 9167
0d381245 9168 b->cond_string = cond_string;
e7e0cddf 9169 b->extra_string = extra_string;
0d381245 9170 b->ignore_count = ignore_count;
41447f92 9171 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9172 b->disposition = disposition;
6c95b8df 9173
44f238bb
PA
9174 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9175 b->loc->inserted = 1;
9176
0fb4aa4b
PA
9177 if (type == bp_static_tracepoint)
9178 {
d9b3f62e 9179 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9180 struct static_tracepoint_marker marker;
9181
983af33b 9182 if (strace_marker_p (b))
0fb4aa4b
PA
9183 {
9184 /* We already know the marker exists, otherwise, we
9185 wouldn't see a sal for it. */
f00aae0f
KS
9186 const char *p = &event_location_to_string (b->location)[3];
9187 const char *endp;
0fb4aa4b 9188 char *marker_str;
0fb4aa4b 9189
f00aae0f 9190 p = skip_spaces_const (p);
0fb4aa4b 9191
f00aae0f 9192 endp = skip_to_space_const (p);
0fb4aa4b
PA
9193
9194 marker_str = savestring (p, endp - p);
d9b3f62e 9195 t->static_trace_marker_id = marker_str;
0fb4aa4b 9196
3e43a32a
MS
9197 printf_filtered (_("Probed static tracepoint "
9198 "marker \"%s\"\n"),
d9b3f62e 9199 t->static_trace_marker_id);
0fb4aa4b
PA
9200 }
9201 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9202 {
d9b3f62e 9203 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9204 release_static_tracepoint_marker (&marker);
9205
3e43a32a
MS
9206 printf_filtered (_("Probed static tracepoint "
9207 "marker \"%s\"\n"),
d9b3f62e 9208 t->static_trace_marker_id);
0fb4aa4b
PA
9209 }
9210 else
3e43a32a
MS
9211 warning (_("Couldn't determine the static "
9212 "tracepoint marker to probe"));
0fb4aa4b
PA
9213 }
9214
0d381245
VP
9215 loc = b->loc;
9216 }
9217 else
018d34a4 9218 {
39d61571 9219 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9220 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9221 loc->inserted = 1;
0d381245
VP
9222 }
9223
9224 if (b->cond_string)
9225 {
bbc13ae3
KS
9226 const char *arg = b->cond_string;
9227
1bb9788d
TT
9228 loc->cond = parse_exp_1 (&arg, loc->address,
9229 block_for_pc (loc->address), 0);
0d381245 9230 if (*arg)
588ae58c 9231 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9232 }
e7e0cddf
SS
9233
9234 /* Dynamic printf requires and uses additional arguments on the
9235 command line, otherwise it's an error. */
9236 if (type == bp_dprintf)
9237 {
9238 if (b->extra_string)
9239 update_dprintf_command_list (b);
9240 else
9241 error (_("Format string required"));
9242 }
9243 else if (b->extra_string)
588ae58c 9244 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9245 }
018d34a4 9246
56435ebe 9247 b->display_canonical = display_canonical;
f00aae0f
KS
9248 if (location != NULL)
9249 b->location = location;
018d34a4 9250 else
a06efdd6 9251 b->location = new_address_location (b->loc->address);
f8eba3c6 9252 b->filter = filter;
d9b3f62e 9253}
018d34a4 9254
d9b3f62e
PA
9255static void
9256create_breakpoint_sal (struct gdbarch *gdbarch,
f00aae0f
KS
9257 struct symtabs_and_lines sals,
9258 struct event_location *location,
f8eba3c6 9259 char *filter, char *cond_string,
e7e0cddf 9260 char *extra_string,
d9b3f62e
PA
9261 enum bptype type, enum bpdisp disposition,
9262 int thread, int task, int ignore_count,
c0a91b2b 9263 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9264 int enabled, int internal, unsigned flags,
9265 int display_canonical)
d9b3f62e
PA
9266{
9267 struct breakpoint *b;
9268 struct cleanup *old_chain;
9269
9270 if (is_tracepoint_type (type))
9271 {
9272 struct tracepoint *t;
9273
9274 t = XCNEW (struct tracepoint);
9275 b = &t->base;
9276 }
9277 else
9278 b = XNEW (struct breakpoint);
9279
9280 old_chain = make_cleanup (xfree, b);
9281
9282 init_breakpoint_sal (b, gdbarch,
f00aae0f 9283 sals, location,
e7e0cddf 9284 filter, cond_string, extra_string,
d9b3f62e
PA
9285 type, disposition,
9286 thread, task, ignore_count,
9287 ops, from_tty,
44f238bb
PA
9288 enabled, internal, flags,
9289 display_canonical);
d9b3f62e
PA
9290 discard_cleanups (old_chain);
9291
3ea46bff 9292 install_breakpoint (internal, b, 0);
018d34a4
VP
9293}
9294
9295/* Add SALS.nelts breakpoints to the breakpoint table. For each
9296 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9297 value. COND_STRING, if not NULL, specified the condition to be
9298 used for all breakpoints. Essentially the only case where
9299 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9300 function. In that case, it's still not possible to specify
9301 separate conditions for different overloaded functions, so
9302 we take just a single condition string.
9303
c3f6f71d 9304 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9305 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9306 array contents). If the function fails (error() is called), the
9307 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9308 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9309
9310static void
8cdf0e15 9311create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9312 struct linespec_result *canonical,
e7e0cddf 9313 char *cond_string, char *extra_string,
8cdf0e15
VP
9314 enum bptype type, enum bpdisp disposition,
9315 int thread, int task, int ignore_count,
c0a91b2b 9316 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9317 int enabled, int internal, unsigned flags)
c906108c 9318{
018d34a4 9319 int i;
f8eba3c6 9320 struct linespec_sals *lsal;
cc59ec59 9321
f8eba3c6
TT
9322 if (canonical->pre_expanded)
9323 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9324
9325 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9326 {
f00aae0f 9327 /* Note that 'location' can be NULL in the case of a plain
f8eba3c6 9328 'break', without arguments. */
f00aae0f
KS
9329 struct event_location *location
9330 = (canonical->location != NULL
9331 ? copy_event_location (canonical->location) : NULL);
f8eba3c6 9332 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
f00aae0f 9333 struct cleanup *inner = make_cleanup_delete_event_location (location);
0d381245 9334
f8eba3c6
TT
9335 make_cleanup (xfree, filter_string);
9336 create_breakpoint_sal (gdbarch, lsal->sals,
f00aae0f 9337 location,
f8eba3c6 9338 filter_string,
e7e0cddf
SS
9339 cond_string, extra_string,
9340 type, disposition,
84f4c1fe 9341 thread, task, ignore_count, ops,
44f238bb 9342 from_tty, enabled, internal, flags,
56435ebe 9343 canonical->special_display);
f8eba3c6 9344 discard_cleanups (inner);
c3f6f71d 9345 }
c3f6f71d 9346}
c906108c 9347
f00aae0f 9348/* Parse LOCATION which is assumed to be a SAL specification possibly
c3f6f71d 9349 followed by conditionals. On return, SALS contains an array of SAL
f00aae0f
KS
9350 addresses found. LOCATION points to the end of the SAL (for
9351 linespec locations).
9998af43
TJB
9352
9353 The array and the line spec strings are allocated on the heap, it is
9354 the caller's responsibility to free them. */
c906108c 9355
b9362cc7 9356static void
f00aae0f 9357parse_breakpoint_sals (const struct event_location *location,
58438ac1 9358 struct linespec_result *canonical)
c3f6f71d 9359{
f00aae0f
KS
9360 struct symtab_and_line cursal;
9361
9362 if (event_location_type (location) == LINESPEC_LOCATION)
9363 {
9364 const char *address = get_linespec_location (location);
9365
9366 if (address == NULL)
9367 {
9368 /* The last displayed codepoint, if it's valid, is our default
9369 breakpoint address. */
9370 if (last_displayed_sal_is_valid ())
9371 {
9372 struct linespec_sals lsal;
9373 struct symtab_and_line sal;
9374 CORE_ADDR pc;
9375
9376 init_sal (&sal); /* Initialize to zeroes. */
8d749320 9377 lsal.sals.sals = XNEW (struct symtab_and_line);
f00aae0f
KS
9378
9379 /* Set sal's pspace, pc, symtab, and line to the values
9380 corresponding to the last call to print_frame_info.
9381 Be sure to reinitialize LINE with NOTCURRENT == 0
9382 as the breakpoint line number is inappropriate otherwise.
9383 find_pc_line would adjust PC, re-set it back. */
9384 get_last_displayed_sal (&sal);
9385 pc = sal.pc;
9386 sal = find_pc_line (pc, 0);
9387
9388 /* "break" without arguments is equivalent to "break *PC"
9389 where PC is the last displayed codepoint's address. So
9390 make sure to set sal.explicit_pc to prevent GDB from
9391 trying to expand the list of sals to include all other
9392 instances with the same symtab and line. */
9393 sal.pc = pc;
9394 sal.explicit_pc = 1;
9395
9396 lsal.sals.sals[0] = sal;
9397 lsal.sals.nelts = 1;
9398 lsal.canonical = NULL;
9399
9400 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9401 return;
9402 }
9403 else
9404 error (_("No default breakpoint address now."));
c906108c 9405 }
c906108c 9406 }
f00aae0f
KS
9407
9408 /* Force almost all breakpoints to be in terms of the
9409 current_source_symtab (which is decode_line_1's default).
9410 This should produce the results we want almost all of the
9411 time while leaving default_breakpoint_* alone.
9412
9413 ObjC: However, don't match an Objective-C method name which
9414 may have a '+' or '-' succeeded by a '['. */
9415 cursal = get_current_source_symtab_and_line ();
9416 if (last_displayed_sal_is_valid ())
c906108c 9417 {
f00aae0f 9418 const char *address = NULL;
cc80f267 9419
f00aae0f
KS
9420 if (event_location_type (location) == LINESPEC_LOCATION)
9421 address = get_linespec_location (location);
cc80f267 9422
f00aae0f
KS
9423 if (!cursal.symtab
9424 || (address != NULL
9425 && strchr ("+-", address[0]) != NULL
9426 && address[1] != '['))
9427 {
9428 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
9429 get_last_displayed_symtab (),
9430 get_last_displayed_line (),
9431 canonical, NULL, NULL);
9432 return;
9433 }
c906108c 9434 }
f00aae0f
KS
9435
9436 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
9437 cursal.symtab, cursal.line, canonical, NULL, NULL);
c3f6f71d 9438}
c906108c 9439
c906108c 9440
c3f6f71d 9441/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9442 inserted as a breakpoint. If it can't throw an error. */
c906108c 9443
b9362cc7 9444static void
23e7acfb 9445breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9446{
9447 int i;
cc59ec59 9448
c3f6f71d 9449 for (i = 0; i < sals->nelts; i++)
ee53e872 9450 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9451}
9452
7a697b8d
SS
9453/* Fast tracepoints may have restrictions on valid locations. For
9454 instance, a fast tracepoint using a jump instead of a trap will
9455 likely have to overwrite more bytes than a trap would, and so can
9456 only be placed where the instruction is longer than the jump, or a
9457 multi-instruction sequence does not have a jump into the middle of
9458 it, etc. */
9459
9460static void
9461check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9462 struct symtabs_and_lines *sals)
9463{
9464 int i, rslt;
9465 struct symtab_and_line *sal;
9466 char *msg;
9467 struct cleanup *old_chain;
9468
9469 for (i = 0; i < sals->nelts; i++)
9470 {
f8eba3c6
TT
9471 struct gdbarch *sarch;
9472
7a697b8d
SS
9473 sal = &sals->sals[i];
9474
f8eba3c6
TT
9475 sarch = get_sal_arch (*sal);
9476 /* We fall back to GDBARCH if there is no architecture
9477 associated with SAL. */
9478 if (sarch == NULL)
9479 sarch = gdbarch;
6b940e6a 9480 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
7a697b8d
SS
9481 old_chain = make_cleanup (xfree, msg);
9482
9483 if (!rslt)
9484 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9485 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9486
9487 do_cleanups (old_chain);
9488 }
9489}
9490
af4908ba
KS
9491/* Issue an invalid thread ID error. */
9492
9493static void ATTRIBUTE_NORETURN
9494invalid_thread_id_error (int id)
9495{
9496 error (_("Unknown thread %d."), id);
9497}
9498
018d34a4
VP
9499/* Given TOK, a string specification of condition and thread, as
9500 accepted by the 'break' command, extract the condition
9501 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9502 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9503 If no condition is found, *COND_STRING is set to NULL.
9504 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9505
9506static void
bbc13ae3 9507find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9508 char **cond_string, int *thread, int *task,
9509 char **rest)
018d34a4
VP
9510{
9511 *cond_string = NULL;
9512 *thread = -1;
ed1d1739
KS
9513 *task = 0;
9514 *rest = NULL;
9515
018d34a4
VP
9516 while (tok && *tok)
9517 {
bbc13ae3 9518 const char *end_tok;
018d34a4 9519 int toklen;
bbc13ae3
KS
9520 const char *cond_start = NULL;
9521 const char *cond_end = NULL;
cc59ec59 9522
bbc13ae3 9523 tok = skip_spaces_const (tok);
e7e0cddf
SS
9524
9525 if ((*tok == '"' || *tok == ',') && rest)
9526 {
9527 *rest = savestring (tok, strlen (tok));
9528 return;
9529 }
9530
bbc13ae3 9531 end_tok = skip_to_space_const (tok);
d634f2de 9532
018d34a4 9533 toklen = end_tok - tok;
d634f2de 9534
018d34a4
VP
9535 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9536 {
f7545552
TT
9537 struct expression *expr;
9538
018d34a4 9539 tok = cond_start = end_tok + 1;
1bb9788d 9540 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9541 xfree (expr);
018d34a4 9542 cond_end = tok;
d634f2de 9543 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9544 }
9545 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9546 {
9547 char *tmptok;
d634f2de 9548
018d34a4 9549 tok = end_tok + 1;
bbc13ae3 9550 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9551 if (tok == tmptok)
9552 error (_("Junk after thread keyword."));
9553 if (!valid_thread_id (*thread))
af4908ba 9554 invalid_thread_id_error (*thread);
bbc13ae3 9555 tok = tmptok;
018d34a4 9556 }
4a306c9a
JB
9557 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9558 {
9559 char *tmptok;
9560
9561 tok = end_tok + 1;
bbc13ae3 9562 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9563 if (tok == tmptok)
9564 error (_("Junk after task keyword."));
9565 if (!valid_task_id (*task))
b6199126 9566 error (_("Unknown task %d."), *task);
bbc13ae3 9567 tok = tmptok;
4a306c9a 9568 }
e7e0cddf
SS
9569 else if (rest)
9570 {
9571 *rest = savestring (tok, strlen (tok));
ccab2054 9572 return;
e7e0cddf 9573 }
018d34a4
VP
9574 else
9575 error (_("Junk at end of arguments."));
9576 }
9577}
9578
0fb4aa4b
PA
9579/* Decode a static tracepoint marker spec. */
9580
9581static struct symtabs_and_lines
f00aae0f 9582decode_static_tracepoint_spec (const char **arg_p)
0fb4aa4b
PA
9583{
9584 VEC(static_tracepoint_marker_p) *markers = NULL;
9585 struct symtabs_and_lines sals;
0fb4aa4b 9586 struct cleanup *old_chain;
f00aae0f
KS
9587 const char *p = &(*arg_p)[3];
9588 const char *endp;
0fb4aa4b
PA
9589 char *marker_str;
9590 int i;
9591
f00aae0f 9592 p = skip_spaces_const (p);
0fb4aa4b 9593
f00aae0f 9594 endp = skip_to_space_const (p);
0fb4aa4b
PA
9595
9596 marker_str = savestring (p, endp - p);
9597 old_chain = make_cleanup (xfree, marker_str);
9598
9599 markers = target_static_tracepoint_markers_by_strid (marker_str);
9600 if (VEC_empty(static_tracepoint_marker_p, markers))
9601 error (_("No known static tracepoint marker named %s"), marker_str);
9602
9603 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
8d749320 9604 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
0fb4aa4b
PA
9605
9606 for (i = 0; i < sals.nelts; i++)
9607 {
9608 struct static_tracepoint_marker *marker;
9609
9610 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9611
9612 init_sal (&sals.sals[i]);
9613
9614 sals.sals[i] = find_pc_line (marker->address, 0);
9615 sals.sals[i].pc = marker->address;
9616
9617 release_static_tracepoint_marker (marker);
9618 }
9619
9620 do_cleanups (old_chain);
9621
9622 *arg_p = endp;
9623 return sals;
9624}
9625
f00aae0f 9626/* See breakpoint.h. */
0101ce28 9627
8cdf0e15
VP
9628int
9629create_breakpoint (struct gdbarch *gdbarch,
f00aae0f 9630 const struct event_location *location, char *cond_string,
e7e0cddf 9631 int thread, char *extra_string,
f00aae0f 9632 int parse_extra,
0fb4aa4b 9633 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9634 int ignore_count,
9635 enum auto_boolean pending_break_support,
c0a91b2b 9636 const struct breakpoint_ops *ops,
44f238bb
PA
9637 int from_tty, int enabled, int internal,
9638 unsigned flags)
c3f6f71d 9639{
7efd8fc2 9640 struct linespec_result canonical;
c3f6f71d 9641 struct cleanup *old_chain;
80c99de1 9642 struct cleanup *bkpt_chain = NULL;
0101ce28 9643 int pending = 0;
4a306c9a 9644 int task = 0;
86b17b60 9645 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9646
348d480f
PA
9647 gdb_assert (ops != NULL);
9648
f00aae0f
KS
9649 /* If extra_string isn't useful, set it to NULL. */
9650 if (extra_string != NULL && *extra_string == '\0')
9651 extra_string = NULL;
9652
7efd8fc2 9653 init_linespec_result (&canonical);
c3f6f71d 9654
492d29ea 9655 TRY
b78a6381 9656 {
f00aae0f 9657 ops->create_sals_from_location (location, &canonical, type_wanted);
b78a6381 9658 }
492d29ea 9659 CATCH (e, RETURN_MASK_ERROR)
0101ce28 9660 {
492d29ea
PA
9661 /* If caller is interested in rc value from parse, set
9662 value. */
9663 if (e.error == NOT_FOUND_ERROR)
0101ce28 9664 {
05ff989b
AC
9665 /* If pending breakpoint support is turned off, throw
9666 error. */
fa8d40ab
JJ
9667
9668 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9669 throw_exception (e);
9670
9671 exception_print (gdb_stderr, e);
fa8d40ab 9672
05ff989b
AC
9673 /* If pending breakpoint support is auto query and the user
9674 selects no, then simply return the error code. */
059fb39f 9675 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9676 && !nquery (_("Make %s pending on future shared library load? "),
9677 bptype_string (type_wanted)))
fd9b8c24 9678 return 0;
fa8d40ab 9679
05ff989b
AC
9680 /* At this point, either the user was queried about setting
9681 a pending breakpoint and selected yes, or pending
9682 breakpoint behavior is on and thus a pending breakpoint
9683 is defaulted on behalf of the user. */
f00aae0f 9684 pending = 1;
0101ce28 9685 }
492d29ea
PA
9686 else
9687 throw_exception (e);
0101ce28 9688 }
492d29ea
PA
9689 END_CATCH
9690
f00aae0f 9691 if (!pending && VEC_empty (linespec_sals, canonical.sals))
492d29ea 9692 return 0;
c3f6f71d 9693
4a64f543 9694 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9695 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9696
c3f6f71d
JM
9697 /* ----------------------------- SNIP -----------------------------
9698 Anything added to the cleanup chain beyond this point is assumed
9699 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9700 then the memory is not reclaimed. */
9701 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9702
c3f6f71d
JM
9703 /* Resolve all line numbers to PC's and verify that the addresses
9704 are ok for the target. */
0101ce28 9705 if (!pending)
f8eba3c6
TT
9706 {
9707 int ix;
9708 struct linespec_sals *iter;
9709
9710 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9711 breakpoint_sals_to_pc (&iter->sals);
9712 }
c3f6f71d 9713
7a697b8d 9714 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9715 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9716 {
9717 int ix;
9718 struct linespec_sals *iter;
9719
9720 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9721 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9722 }
7a697b8d 9723
c3f6f71d
JM
9724 /* Verify that condition can be parsed, before setting any
9725 breakpoints. Allocate a separate condition expression for each
4a64f543 9726 breakpoint. */
0101ce28 9727 if (!pending)
c3f6f71d 9728 {
f00aae0f 9729 if (parse_extra)
72b2ff0e 9730 {
0878d0fa 9731 char *rest;
52d361e1
YQ
9732 struct linespec_sals *lsal;
9733
9734 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9735
0878d0fa
YQ
9736 /* Here we only parse 'arg' to separate condition
9737 from thread number, so parsing in context of first
9738 sal is OK. When setting the breakpoint we'll
9739 re-parse it in context of each sal. */
9740
f00aae0f
KS
9741 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9742 &cond_string, &thread, &task, &rest);
0878d0fa
YQ
9743 if (cond_string)
9744 make_cleanup (xfree, cond_string);
9745 if (rest)
9746 make_cleanup (xfree, rest);
9747 if (rest)
9748 extra_string = rest;
f00aae0f
KS
9749 else
9750 extra_string = NULL;
72b2ff0e 9751 }
2f069f6f 9752 else
72b2ff0e 9753 {
f00aae0f
KS
9754 if (type_wanted != bp_dprintf
9755 && extra_string != NULL && *extra_string != '\0')
9756 error (_("Garbage '%s' at end of location"), extra_string);
0878d0fa
YQ
9757
9758 /* Create a private copy of condition string. */
9759 if (cond_string)
9760 {
9761 cond_string = xstrdup (cond_string);
9762 make_cleanup (xfree, cond_string);
9763 }
9764 /* Create a private copy of any extra string. */
9765 if (extra_string)
9766 {
9767 extra_string = xstrdup (extra_string);
9768 make_cleanup (xfree, extra_string);
9769 }
72b2ff0e 9770 }
0fb4aa4b 9771
52d361e1 9772 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9773 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9774 tempflag ? disp_del : disp_donttouch,
9775 thread, task, ignore_count, ops,
44f238bb 9776 from_tty, enabled, internal, flags);
c906108c 9777 }
0101ce28
JJ
9778 else
9779 {
0101ce28
JJ
9780 struct breakpoint *b;
9781
bfccc43c
YQ
9782 if (is_tracepoint_type (type_wanted))
9783 {
9784 struct tracepoint *t;
9785
9786 t = XCNEW (struct tracepoint);
9787 b = &t->base;
9788 }
9789 else
9790 b = XNEW (struct breakpoint);
9791
9792 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
f00aae0f 9793 b->location = copy_event_location (location);
bfccc43c 9794
f00aae0f
KS
9795 if (parse_extra)
9796 b->cond_string = NULL;
e12c7713
MK
9797 else
9798 {
9799 /* Create a private copy of condition string. */
9800 if (cond_string)
9801 {
9802 cond_string = xstrdup (cond_string);
9803 make_cleanup (xfree, cond_string);
9804 }
9805 b->cond_string = cond_string;
15630549 9806 b->thread = thread;
e12c7713 9807 }
f00aae0f
KS
9808
9809 /* Create a private copy of any extra string. */
9810 if (extra_string != NULL)
9811 {
9812 extra_string = xstrdup (extra_string);
9813 make_cleanup (xfree, extra_string);
9814 }
9815 b->extra_string = extra_string;
0101ce28 9816 b->ignore_count = ignore_count;
0101ce28 9817 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9818 b->condition_not_parsed = 1;
41447f92 9819 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9820 if ((type_wanted != bp_breakpoint
9821 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9822 b->pspace = current_program_space;
8bea4e01 9823
bfccc43c 9824 install_breakpoint (internal, b, 0);
0101ce28
JJ
9825 }
9826
f8eba3c6 9827 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9828 {
3e43a32a
MS
9829 warning (_("Multiple breakpoints were set.\nUse the "
9830 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9831 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9832 }
9833
80c99de1
PA
9834 /* That's it. Discard the cleanups for data inserted into the
9835 breakpoint. */
9836 discard_cleanups (bkpt_chain);
9837 /* But cleanup everything else. */
c3f6f71d 9838 do_cleanups (old_chain);
217dc9e2 9839
80c99de1 9840 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 9841 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
9842
9843 return 1;
c3f6f71d 9844}
c906108c 9845
348d480f 9846/* Set a breakpoint.
72b2ff0e
VP
9847 ARG is a string describing breakpoint address,
9848 condition, and thread.
9849 FLAG specifies if a breakpoint is hardware on,
9850 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9851 and BP_TEMPFLAG. */
348d480f 9852
98deb0da 9853static void
72b2ff0e 9854break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9855{
72b2ff0e 9856 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9857 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9858 ? bp_hardware_breakpoint
9859 : bp_breakpoint);
55aa24fb 9860 struct breakpoint_ops *ops;
f00aae0f
KS
9861 struct event_location *location;
9862 struct cleanup *cleanup;
9863
9864 location = string_to_event_location (&arg, current_language);
9865 cleanup = make_cleanup_delete_event_location (location);
55aa24fb
SDJ
9866
9867 /* Matching breakpoints on probes. */
5b56227b
KS
9868 if (location != NULL
9869 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
9870 ops = &bkpt_probe_breakpoint_ops;
9871 else
9872 ops = &bkpt_breakpoint_ops;
c3f6f71d 9873
8cdf0e15 9874 create_breakpoint (get_current_arch (),
f00aae0f
KS
9875 location,
9876 NULL, 0, arg, 1 /* parse arg */,
0fb4aa4b 9877 tempflag, type_wanted,
8cdf0e15
VP
9878 0 /* Ignore count */,
9879 pending_break_support,
55aa24fb 9880 ops,
8cdf0e15 9881 from_tty,
84f4c1fe 9882 1 /* enabled */,
44f238bb
PA
9883 0 /* internal */,
9884 0);
f00aae0f 9885 do_cleanups (cleanup);
c906108c
SS
9886}
9887
c906108c
SS
9888/* Helper function for break_command_1 and disassemble_command. */
9889
9890void
fba45db2 9891resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9892{
9893 CORE_ADDR pc;
9894
9895 if (sal->pc == 0 && sal->symtab != NULL)
9896 {
9897 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9898 error (_("No line %d in file \"%s\"."),
05cba821 9899 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 9900 sal->pc = pc;
6a048695 9901
4a64f543
MS
9902 /* If this SAL corresponds to a breakpoint inserted using a line
9903 number, then skip the function prologue if necessary. */
6a048695 9904 if (sal->explicit_line)
059acae7 9905 skip_prologue_sal (sal);
c906108c
SS
9906 }
9907
9908 if (sal->section == 0 && sal->symtab != NULL)
9909 {
346d1dfe 9910 const struct blockvector *bv;
3977b71f 9911 const struct block *b;
c5aa993b 9912 struct symbol *sym;
c906108c 9913
43f3e411
DE
9914 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9915 SYMTAB_COMPUNIT (sal->symtab));
c906108c
SS
9916 if (bv != NULL)
9917 {
7f0df278 9918 sym = block_linkage_function (b);
c906108c
SS
9919 if (sym != NULL)
9920 {
eb822aa6
DE
9921 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9922 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9923 sym);
c906108c
SS
9924 }
9925 else
9926 {
4a64f543
MS
9927 /* It really is worthwhile to have the section, so we'll
9928 just have to look harder. This case can be executed
9929 if we have line numbers but no functions (as can
9930 happen in assembly source). */
c906108c 9931
7cbd4a93 9932 struct bound_minimal_symbol msym;
6c95b8df
PA
9933 struct cleanup *old_chain = save_current_space_and_thread ();
9934
9935 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
9936
9937 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 9938 if (msym.minsym)
efd66ac6 9939 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
9940
9941 do_cleanups (old_chain);
c906108c
SS
9942 }
9943 }
9944 }
9945}
9946
9947void
fba45db2 9948break_command (char *arg, int from_tty)
c906108c 9949{
db107f19 9950 break_command_1 (arg, 0, from_tty);
c906108c
SS
9951}
9952
c906108c 9953void
fba45db2 9954tbreak_command (char *arg, int from_tty)
c906108c 9955{
db107f19 9956 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9957}
9958
c906108c 9959static void
fba45db2 9960hbreak_command (char *arg, int from_tty)
c906108c 9961{
db107f19 9962 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9963}
9964
9965static void
fba45db2 9966thbreak_command (char *arg, int from_tty)
c906108c 9967{
db107f19 9968 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9969}
9970
9971static void
fba45db2 9972stop_command (char *arg, int from_tty)
c906108c 9973{
a3f17187 9974 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9975Usage: stop in <function | address>\n\
a3f17187 9976 stop at <line>\n"));
c906108c
SS
9977}
9978
9979static void
fba45db2 9980stopin_command (char *arg, int from_tty)
c906108c
SS
9981{
9982 int badInput = 0;
9983
c5aa993b 9984 if (arg == (char *) NULL)
c906108c
SS
9985 badInput = 1;
9986 else if (*arg != '*')
9987 {
9988 char *argptr = arg;
9989 int hasColon = 0;
9990
4a64f543 9991 /* Look for a ':'. If this is a line number specification, then
53a5351d 9992 say it is bad, otherwise, it should be an address or
4a64f543 9993 function/method name. */
c906108c 9994 while (*argptr && !hasColon)
c5aa993b
JM
9995 {
9996 hasColon = (*argptr == ':');
9997 argptr++;
9998 }
c906108c
SS
9999
10000 if (hasColon)
c5aa993b 10001 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10002 else
c5aa993b 10003 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10004 }
10005
10006 if (badInput)
a3f17187 10007 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10008 else
db107f19 10009 break_command_1 (arg, 0, from_tty);
c906108c
SS
10010}
10011
10012static void
fba45db2 10013stopat_command (char *arg, int from_tty)
c906108c
SS
10014{
10015 int badInput = 0;
10016
c5aa993b 10017 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10018 badInput = 1;
10019 else
10020 {
10021 char *argptr = arg;
10022 int hasColon = 0;
10023
4a64f543
MS
10024 /* Look for a ':'. If there is a '::' then get out, otherwise
10025 it is probably a line number. */
c906108c 10026 while (*argptr && !hasColon)
c5aa993b
JM
10027 {
10028 hasColon = (*argptr == ':');
10029 argptr++;
10030 }
c906108c
SS
10031
10032 if (hasColon)
c5aa993b 10033 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10034 else
c5aa993b 10035 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10036 }
10037
10038 if (badInput)
a3f17187 10039 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10040 else
db107f19 10041 break_command_1 (arg, 0, from_tty);
c906108c
SS
10042}
10043
e7e0cddf
SS
10044/* The dynamic printf command is mostly like a regular breakpoint, but
10045 with a prewired command list consisting of a single output command,
10046 built from extra arguments supplied on the dprintf command
10047 line. */
10048
da821c7b 10049static void
e7e0cddf
SS
10050dprintf_command (char *arg, int from_tty)
10051{
f00aae0f
KS
10052 struct event_location *location;
10053 struct cleanup *cleanup;
10054
10055 location = string_to_event_location (&arg, current_language);
10056 cleanup = make_cleanup_delete_event_location (location);
10057
10058 /* If non-NULL, ARG should have been advanced past the location;
10059 the next character must be ','. */
10060 if (arg != NULL)
10061 {
10062 if (arg[0] != ',' || arg[1] == '\0')
10063 error (_("Format string required"));
10064 else
10065 {
10066 /* Skip the comma. */
10067 ++arg;
10068 }
10069 }
10070
e7e0cddf 10071 create_breakpoint (get_current_arch (),
f00aae0f
KS
10072 location,
10073 NULL, 0, arg, 1 /* parse arg */,
e7e0cddf
SS
10074 0, bp_dprintf,
10075 0 /* Ignore count */,
10076 pending_break_support,
10077 &dprintf_breakpoint_ops,
10078 from_tty,
10079 1 /* enabled */,
10080 0 /* internal */,
10081 0);
f00aae0f 10082 do_cleanups (cleanup);
e7e0cddf
SS
10083}
10084
d3ce09f5
SS
10085static void
10086agent_printf_command (char *arg, int from_tty)
10087{
10088 error (_("May only run agent-printf on the target"));
10089}
10090
f1310107
TJB
10091/* Implement the "breakpoint_hit" breakpoint_ops method for
10092 ranged breakpoints. */
10093
10094static int
10095breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10096 struct address_space *aspace,
09ac7c10
TT
10097 CORE_ADDR bp_addr,
10098 const struct target_waitstatus *ws)
f1310107 10099{
09ac7c10 10100 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10101 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10102 return 0;
10103
f1310107
TJB
10104 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10105 bl->length, aspace, bp_addr);
10106}
10107
10108/* Implement the "resources_needed" breakpoint_ops method for
10109 ranged breakpoints. */
10110
10111static int
10112resources_needed_ranged_breakpoint (const struct bp_location *bl)
10113{
10114 return target_ranged_break_num_registers ();
10115}
10116
10117/* Implement the "print_it" breakpoint_ops method for
10118 ranged breakpoints. */
10119
10120static enum print_stop_action
348d480f 10121print_it_ranged_breakpoint (bpstat bs)
f1310107 10122{
348d480f 10123 struct breakpoint *b = bs->breakpoint_at;
f1310107 10124 struct bp_location *bl = b->loc;
79a45e25 10125 struct ui_out *uiout = current_uiout;
f1310107
TJB
10126
10127 gdb_assert (b->type == bp_hardware_breakpoint);
10128
10129 /* Ranged breakpoints have only one location. */
10130 gdb_assert (bl && bl->next == NULL);
10131
10132 annotate_breakpoint (b->number);
10133 if (b->disposition == disp_del)
10134 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10135 else
10136 ui_out_text (uiout, "\nRanged breakpoint ");
10137 if (ui_out_is_mi_like_p (uiout))
10138 {
10139 ui_out_field_string (uiout, "reason",
10140 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10141 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10142 }
10143 ui_out_field_int (uiout, "bkptno", b->number);
10144 ui_out_text (uiout, ", ");
10145
10146 return PRINT_SRC_AND_LOC;
10147}
10148
10149/* Implement the "print_one" breakpoint_ops method for
10150 ranged breakpoints. */
10151
10152static void
10153print_one_ranged_breakpoint (struct breakpoint *b,
10154 struct bp_location **last_loc)
10155{
10156 struct bp_location *bl = b->loc;
10157 struct value_print_options opts;
79a45e25 10158 struct ui_out *uiout = current_uiout;
f1310107
TJB
10159
10160 /* Ranged breakpoints have only one location. */
10161 gdb_assert (bl && bl->next == NULL);
10162
10163 get_user_print_options (&opts);
10164
10165 if (opts.addressprint)
10166 /* We don't print the address range here, it will be printed later
10167 by print_one_detail_ranged_breakpoint. */
10168 ui_out_field_skip (uiout, "addr");
10169 annotate_field (5);
10170 print_breakpoint_location (b, bl);
10171 *last_loc = bl;
10172}
10173
10174/* Implement the "print_one_detail" breakpoint_ops method for
10175 ranged breakpoints. */
10176
10177static void
10178print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10179 struct ui_out *uiout)
10180{
10181 CORE_ADDR address_start, address_end;
10182 struct bp_location *bl = b->loc;
f99d8bf4
PA
10183 struct ui_file *stb = mem_fileopen ();
10184 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10185
10186 gdb_assert (bl);
10187
10188 address_start = bl->address;
10189 address_end = address_start + bl->length - 1;
10190
10191 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10192 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10193 print_core_address (bl->gdbarch, address_start),
10194 print_core_address (bl->gdbarch, address_end));
10195 ui_out_field_stream (uiout, "addr", stb);
10196 ui_out_text (uiout, "\n");
10197
10198 do_cleanups (cleanup);
10199}
10200
10201/* Implement the "print_mention" breakpoint_ops method for
10202 ranged breakpoints. */
10203
10204static void
10205print_mention_ranged_breakpoint (struct breakpoint *b)
10206{
10207 struct bp_location *bl = b->loc;
79a45e25 10208 struct ui_out *uiout = current_uiout;
f1310107
TJB
10209
10210 gdb_assert (bl);
10211 gdb_assert (b->type == bp_hardware_breakpoint);
10212
10213 if (ui_out_is_mi_like_p (uiout))
10214 return;
10215
10216 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10217 b->number, paddress (bl->gdbarch, bl->address),
10218 paddress (bl->gdbarch, bl->address + bl->length - 1));
10219}
10220
10221/* Implement the "print_recreate" breakpoint_ops method for
10222 ranged breakpoints. */
10223
10224static void
10225print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10226{
f00aae0f
KS
10227 fprintf_unfiltered (fp, "break-range %s, %s",
10228 event_location_to_string (b->location),
10229 event_location_to_string (b->location_range_end));
d9b3f62e 10230 print_recreate_thread (b, fp);
f1310107
TJB
10231}
10232
10233/* The breakpoint_ops structure to be used in ranged breakpoints. */
10234
2060206e 10235static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10236
10237/* Find the address where the end of the breakpoint range should be
10238 placed, given the SAL of the end of the range. This is so that if
10239 the user provides a line number, the end of the range is set to the
10240 last instruction of the given line. */
10241
10242static CORE_ADDR
10243find_breakpoint_range_end (struct symtab_and_line sal)
10244{
10245 CORE_ADDR end;
10246
10247 /* If the user provided a PC value, use it. Otherwise,
10248 find the address of the end of the given location. */
10249 if (sal.explicit_pc)
10250 end = sal.pc;
10251 else
10252 {
10253 int ret;
10254 CORE_ADDR start;
10255
10256 ret = find_line_pc_range (sal, &start, &end);
10257 if (!ret)
10258 error (_("Could not find location of the end of the range."));
10259
10260 /* find_line_pc_range returns the start of the next line. */
10261 end--;
10262 }
10263
10264 return end;
10265}
10266
10267/* Implement the "break-range" CLI command. */
10268
10269static void
10270break_range_command (char *arg, int from_tty)
10271{
10272 char *arg_start, *addr_string_start, *addr_string_end;
10273 struct linespec_result canonical_start, canonical_end;
10274 int bp_count, can_use_bp, length;
10275 CORE_ADDR end;
10276 struct breakpoint *b;
10277 struct symtab_and_line sal_start, sal_end;
f1310107 10278 struct cleanup *cleanup_bkpt;
f8eba3c6 10279 struct linespec_sals *lsal_start, *lsal_end;
f00aae0f 10280 struct event_location *start_location, *end_location;
f1310107
TJB
10281
10282 /* We don't support software ranged breakpoints. */
10283 if (target_ranged_break_num_registers () < 0)
10284 error (_("This target does not support hardware ranged breakpoints."));
10285
10286 bp_count = hw_breakpoint_used_count ();
10287 bp_count += target_ranged_break_num_registers ();
10288 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10289 bp_count, 0);
10290 if (can_use_bp < 0)
10291 error (_("Hardware breakpoints used exceeds limit."));
10292
f8eba3c6 10293 arg = skip_spaces (arg);
f1310107
TJB
10294 if (arg == NULL || arg[0] == '\0')
10295 error(_("No address range specified."));
10296
f1310107
TJB
10297 init_linespec_result (&canonical_start);
10298
f8eba3c6 10299 arg_start = arg;
f00aae0f
KS
10300 start_location = string_to_event_location (&arg, current_language);
10301 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10302 parse_breakpoint_sals (start_location, &canonical_start);
10303 make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10304
10305 if (arg[0] != ',')
10306 error (_("Too few arguments."));
f8eba3c6 10307 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10308 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10309
10310 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10311
10312 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10313 || lsal_start->sals.nelts != 1)
f1310107
TJB
10314 error (_("Cannot create a ranged breakpoint with multiple locations."));
10315
f8eba3c6
TT
10316 sal_start = lsal_start->sals.sals[0];
10317 addr_string_start = savestring (arg_start, arg - arg_start);
10318 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10319
10320 arg++; /* Skip the comma. */
f8eba3c6 10321 arg = skip_spaces (arg);
f1310107
TJB
10322
10323 /* Parse the end location. */
10324
f1310107
TJB
10325 init_linespec_result (&canonical_end);
10326 arg_start = arg;
10327
f8eba3c6 10328 /* We call decode_line_full directly here instead of using
f1310107
TJB
10329 parse_breakpoint_sals because we need to specify the start location's
10330 symtab and line as the default symtab and line for the end of the
10331 range. This makes it possible to have ranges like "foo.c:27, +14",
10332 where +14 means 14 lines from the start location. */
f00aae0f
KS
10333 end_location = string_to_event_location (&arg, current_language);
10334 make_cleanup_delete_event_location (end_location);
10335 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE,
f8eba3c6
TT
10336 sal_start.symtab, sal_start.line,
10337 &canonical_end, NULL, NULL);
10338
10339 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10340
f8eba3c6 10341 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10342 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10343
10344 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10345 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10346 || lsal_end->sals.nelts != 1)
f1310107
TJB
10347 error (_("Cannot create a ranged breakpoint with multiple locations."));
10348
f8eba3c6 10349 sal_end = lsal_end->sals.sals[0];
f1310107
TJB
10350
10351 end = find_breakpoint_range_end (sal_end);
10352 if (sal_start.pc > end)
177b42fe 10353 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10354
10355 length = end - sal_start.pc + 1;
10356 if (length < 0)
10357 /* Length overflowed. */
10358 error (_("Address range too large."));
10359 else if (length == 1)
10360 {
10361 /* This range is simple enough to be handled by
10362 the `hbreak' command. */
10363 hbreak_command (addr_string_start, 1);
10364
10365 do_cleanups (cleanup_bkpt);
10366
10367 return;
10368 }
10369
10370 /* Now set up the breakpoint. */
10371 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10372 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10373 set_breakpoint_count (breakpoint_count + 1);
10374 b->number = breakpoint_count;
10375 b->disposition = disp_donttouch;
f00aae0f
KS
10376 b->location = copy_event_location (start_location);
10377 b->location_range_end = copy_event_location (end_location);
f1310107
TJB
10378 b->loc->length = length;
10379
f8eba3c6 10380 do_cleanups (cleanup_bkpt);
f1310107
TJB
10381
10382 mention (b);
8d3788bd 10383 observer_notify_breakpoint_created (b);
44702360 10384 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10385}
10386
4a64f543
MS
10387/* Return non-zero if EXP is verified as constant. Returned zero
10388 means EXP is variable. Also the constant detection may fail for
10389 some constant expressions and in such case still falsely return
10390 zero. */
2e6e3d9c 10391
65d79d4b
SDJ
10392static int
10393watchpoint_exp_is_const (const struct expression *exp)
10394{
10395 int i = exp->nelts;
10396
10397 while (i > 0)
10398 {
10399 int oplenp, argsp;
10400
10401 /* We are only interested in the descriptor of each element. */
10402 operator_length (exp, i, &oplenp, &argsp);
10403 i -= oplenp;
10404
10405 switch (exp->elts[i].opcode)
10406 {
10407 case BINOP_ADD:
10408 case BINOP_SUB:
10409 case BINOP_MUL:
10410 case BINOP_DIV:
10411 case BINOP_REM:
10412 case BINOP_MOD:
10413 case BINOP_LSH:
10414 case BINOP_RSH:
10415 case BINOP_LOGICAL_AND:
10416 case BINOP_LOGICAL_OR:
10417 case BINOP_BITWISE_AND:
10418 case BINOP_BITWISE_IOR:
10419 case BINOP_BITWISE_XOR:
10420 case BINOP_EQUAL:
10421 case BINOP_NOTEQUAL:
10422 case BINOP_LESS:
10423 case BINOP_GTR:
10424 case BINOP_LEQ:
10425 case BINOP_GEQ:
10426 case BINOP_REPEAT:
10427 case BINOP_COMMA:
10428 case BINOP_EXP:
10429 case BINOP_MIN:
10430 case BINOP_MAX:
10431 case BINOP_INTDIV:
10432 case BINOP_CONCAT:
65d79d4b
SDJ
10433 case TERNOP_COND:
10434 case TERNOP_SLICE:
65d79d4b
SDJ
10435
10436 case OP_LONG:
10437 case OP_DOUBLE:
10438 case OP_DECFLOAT:
10439 case OP_LAST:
10440 case OP_COMPLEX:
10441 case OP_STRING:
65d79d4b
SDJ
10442 case OP_ARRAY:
10443 case OP_TYPE:
608b4967
TT
10444 case OP_TYPEOF:
10445 case OP_DECLTYPE:
6e72ca20 10446 case OP_TYPEID:
65d79d4b
SDJ
10447 case OP_NAME:
10448 case OP_OBJC_NSSTRING:
10449
10450 case UNOP_NEG:
10451 case UNOP_LOGICAL_NOT:
10452 case UNOP_COMPLEMENT:
10453 case UNOP_ADDR:
10454 case UNOP_HIGH:
aeaa2474 10455 case UNOP_CAST:
9eaf6705
TT
10456
10457 case UNOP_CAST_TYPE:
10458 case UNOP_REINTERPRET_CAST:
10459 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10460 /* Unary, binary and ternary operators: We have to check
10461 their operands. If they are constant, then so is the
10462 result of that operation. For instance, if A and B are
10463 determined to be constants, then so is "A + B".
10464
10465 UNOP_IND is one exception to the rule above, because the
10466 value of *ADDR is not necessarily a constant, even when
10467 ADDR is. */
65d79d4b
SDJ
10468 break;
10469
10470 case OP_VAR_VALUE:
10471 /* Check whether the associated symbol is a constant.
4a64f543 10472
65d79d4b 10473 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10474 possible that a buggy compiler could mark a variable as
10475 constant even when it is not, and TYPE_CONST would return
10476 true in this case, while SYMBOL_CLASS wouldn't.
10477
10478 We also have to check for function symbols because they
10479 are always constant. */
65d79d4b
SDJ
10480 {
10481 struct symbol *s = exp->elts[i + 2].symbol;
10482
10483 if (SYMBOL_CLASS (s) != LOC_BLOCK
10484 && SYMBOL_CLASS (s) != LOC_CONST
10485 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10486 return 0;
10487 break;
10488 }
10489
10490 /* The default action is to return 0 because we are using
10491 the optimistic approach here: If we don't know something,
10492 then it is not a constant. */
10493 default:
10494 return 0;
10495 }
10496 }
10497
10498 return 1;
10499}
10500
3a5c3e22
PA
10501/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10502
10503static void
10504dtor_watchpoint (struct breakpoint *self)
10505{
10506 struct watchpoint *w = (struct watchpoint *) self;
10507
10508 xfree (w->cond_exp);
10509 xfree (w->exp);
10510 xfree (w->exp_string);
10511 xfree (w->exp_string_reparse);
10512 value_free (w->val);
10513
10514 base_breakpoint_ops.dtor (self);
10515}
10516
348d480f
PA
10517/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10518
10519static void
10520re_set_watchpoint (struct breakpoint *b)
10521{
3a5c3e22
PA
10522 struct watchpoint *w = (struct watchpoint *) b;
10523
348d480f
PA
10524 /* Watchpoint can be either on expression using entirely global
10525 variables, or it can be on local variables.
10526
10527 Watchpoints of the first kind are never auto-deleted, and even
10528 persist across program restarts. Since they can use variables
10529 from shared libraries, we need to reparse expression as libraries
10530 are loaded and unloaded.
10531
10532 Watchpoints on local variables can also change meaning as result
10533 of solib event. For example, if a watchpoint uses both a local
10534 and a global variables in expression, it's a local watchpoint,
10535 but unloading of a shared library will make the expression
10536 invalid. This is not a very common use case, but we still
10537 re-evaluate expression, to avoid surprises to the user.
10538
10539 Note that for local watchpoints, we re-evaluate it only if
10540 watchpoints frame id is still valid. If it's not, it means the
10541 watchpoint is out of scope and will be deleted soon. In fact,
10542 I'm not sure we'll ever be called in this case.
10543
10544 If a local watchpoint's frame id is still valid, then
3a5c3e22 10545 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10546
3a5c3e22
PA
10547 Don't do anything about disabled watchpoints, since they will be
10548 reevaluated again when enabled. */
10549 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10550}
10551
77b06cd7
TJB
10552/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10553
10554static int
10555insert_watchpoint (struct bp_location *bl)
10556{
3a5c3e22
PA
10557 struct watchpoint *w = (struct watchpoint *) bl->owner;
10558 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10559
10560 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10561 w->cond_exp);
77b06cd7
TJB
10562}
10563
10564/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10565
10566static int
10567remove_watchpoint (struct bp_location *bl)
10568{
3a5c3e22
PA
10569 struct watchpoint *w = (struct watchpoint *) bl->owner;
10570 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10571
10572 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10573 w->cond_exp);
e09342b5
TJB
10574}
10575
e09342b5 10576static int
348d480f 10577breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10578 struct address_space *aspace, CORE_ADDR bp_addr,
10579 const struct target_waitstatus *ws)
e09342b5 10580{
348d480f 10581 struct breakpoint *b = bl->owner;
3a5c3e22 10582 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10583
348d480f
PA
10584 /* Continuable hardware watchpoints are treated as non-existent if the
10585 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10586 some data address). Otherwise gdb won't stop on a break instruction
10587 in the code (not from a breakpoint) when a hardware watchpoint has
10588 been defined. Also skip watchpoints which we know did not trigger
10589 (did not match the data address). */
10590 if (is_hardware_watchpoint (b)
3a5c3e22 10591 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10592 return 0;
9c06b0b4 10593
348d480f 10594 return 1;
9c06b0b4
TJB
10595}
10596
348d480f
PA
10597static void
10598check_status_watchpoint (bpstat bs)
9c06b0b4 10599{
348d480f 10600 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10601
348d480f 10602 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10603}
10604
10605/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10606 hardware watchpoints. */
9c06b0b4
TJB
10607
10608static int
348d480f 10609resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10610{
3a5c3e22
PA
10611 struct watchpoint *w = (struct watchpoint *) bl->owner;
10612 int length = w->exact? 1 : bl->length;
348d480f
PA
10613
10614 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10615}
10616
10617/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10618 hardware watchpoints. */
9c06b0b4
TJB
10619
10620static int
348d480f 10621works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10622{
efa80663
PA
10623 /* Read and access watchpoints only work with hardware support. */
10624 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10625}
10626
9c06b0b4 10627static enum print_stop_action
348d480f 10628print_it_watchpoint (bpstat bs)
9c06b0b4 10629{
348d480f
PA
10630 struct cleanup *old_chain;
10631 struct breakpoint *b;
f99d8bf4 10632 struct ui_file *stb;
348d480f 10633 enum print_stop_action result;
3a5c3e22 10634 struct watchpoint *w;
79a45e25 10635 struct ui_out *uiout = current_uiout;
348d480f
PA
10636
10637 gdb_assert (bs->bp_location_at != NULL);
10638
348d480f 10639 b = bs->breakpoint_at;
3a5c3e22 10640 w = (struct watchpoint *) b;
348d480f 10641
f99d8bf4
PA
10642 stb = mem_fileopen ();
10643 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10644
10645 switch (b->type)
10646 {
348d480f 10647 case bp_watchpoint:
9c06b0b4
TJB
10648 case bp_hardware_watchpoint:
10649 annotate_watchpoint (b->number);
10650 if (ui_out_is_mi_like_p (uiout))
10651 ui_out_field_string
10652 (uiout, "reason",
10653 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10654 mention (b);
10655 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10656 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10657 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10658 ui_out_field_stream (uiout, "old", stb);
10659 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10660 watchpoint_value_print (w->val, stb);
348d480f
PA
10661 ui_out_field_stream (uiout, "new", stb);
10662 ui_out_text (uiout, "\n");
10663 /* More than one watchpoint may have been triggered. */
10664 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10665 break;
10666
10667 case bp_read_watchpoint:
10668 if (ui_out_is_mi_like_p (uiout))
10669 ui_out_field_string
10670 (uiout, "reason",
10671 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10672 mention (b);
10673 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10674 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10675 watchpoint_value_print (w->val, stb);
348d480f
PA
10676 ui_out_field_stream (uiout, "value", stb);
10677 ui_out_text (uiout, "\n");
10678 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10679 break;
10680
10681 case bp_access_watchpoint:
348d480f
PA
10682 if (bs->old_val != NULL)
10683 {
10684 annotate_watchpoint (b->number);
10685 if (ui_out_is_mi_like_p (uiout))
10686 ui_out_field_string
10687 (uiout, "reason",
10688 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10689 mention (b);
10690 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10691 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10692 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10693 ui_out_field_stream (uiout, "old", stb);
10694 ui_out_text (uiout, "\nNew value = ");
10695 }
10696 else
10697 {
10698 mention (b);
10699 if (ui_out_is_mi_like_p (uiout))
10700 ui_out_field_string
10701 (uiout, "reason",
10702 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10703 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10704 ui_out_text (uiout, "\nValue = ");
10705 }
f99d8bf4 10706 watchpoint_value_print (w->val, stb);
348d480f
PA
10707 ui_out_field_stream (uiout, "new", stb);
10708 ui_out_text (uiout, "\n");
10709 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10710 break;
10711 default:
348d480f 10712 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10713 }
10714
348d480f
PA
10715 do_cleanups (old_chain);
10716 return result;
10717}
10718
10719/* Implement the "print_mention" breakpoint_ops method for hardware
10720 watchpoints. */
10721
10722static void
10723print_mention_watchpoint (struct breakpoint *b)
10724{
10725 struct cleanup *ui_out_chain;
3a5c3e22 10726 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10727 struct ui_out *uiout = current_uiout;
348d480f
PA
10728
10729 switch (b->type)
10730 {
10731 case bp_watchpoint:
10732 ui_out_text (uiout, "Watchpoint ");
10733 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10734 break;
10735 case bp_hardware_watchpoint:
10736 ui_out_text (uiout, "Hardware watchpoint ");
10737 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10738 break;
10739 case bp_read_watchpoint:
10740 ui_out_text (uiout, "Hardware read watchpoint ");
10741 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10742 break;
10743 case bp_access_watchpoint:
10744 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10745 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10746 break;
10747 default:
10748 internal_error (__FILE__, __LINE__,
10749 _("Invalid hardware watchpoint type."));
10750 }
10751
10752 ui_out_field_int (uiout, "number", b->number);
10753 ui_out_text (uiout, ": ");
3a5c3e22 10754 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10755 do_cleanups (ui_out_chain);
10756}
10757
10758/* Implement the "print_recreate" breakpoint_ops method for
10759 watchpoints. */
10760
10761static void
10762print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10763{
3a5c3e22
PA
10764 struct watchpoint *w = (struct watchpoint *) b;
10765
348d480f
PA
10766 switch (b->type)
10767 {
10768 case bp_watchpoint:
10769 case bp_hardware_watchpoint:
10770 fprintf_unfiltered (fp, "watch");
10771 break;
10772 case bp_read_watchpoint:
10773 fprintf_unfiltered (fp, "rwatch");
10774 break;
10775 case bp_access_watchpoint:
10776 fprintf_unfiltered (fp, "awatch");
10777 break;
10778 default:
10779 internal_error (__FILE__, __LINE__,
10780 _("Invalid watchpoint type."));
10781 }
10782
3a5c3e22 10783 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10784 print_recreate_thread (b, fp);
348d480f
PA
10785}
10786
427cd150
TT
10787/* Implement the "explains_signal" breakpoint_ops method for
10788 watchpoints. */
10789
47591c29 10790static int
427cd150
TT
10791explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10792{
10793 /* A software watchpoint cannot cause a signal other than
10794 GDB_SIGNAL_TRAP. */
10795 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10796 return 0;
427cd150 10797
47591c29 10798 return 1;
427cd150
TT
10799}
10800
348d480f
PA
10801/* The breakpoint_ops structure to be used in hardware watchpoints. */
10802
2060206e 10803static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10804
10805/* Implement the "insert" breakpoint_ops method for
10806 masked hardware watchpoints. */
10807
10808static int
10809insert_masked_watchpoint (struct bp_location *bl)
10810{
3a5c3e22
PA
10811 struct watchpoint *w = (struct watchpoint *) bl->owner;
10812
10813 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10814 bl->watchpoint_type);
10815}
10816
10817/* Implement the "remove" breakpoint_ops method for
10818 masked hardware watchpoints. */
10819
10820static int
10821remove_masked_watchpoint (struct bp_location *bl)
10822{
3a5c3e22
PA
10823 struct watchpoint *w = (struct watchpoint *) bl->owner;
10824
10825 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10826 bl->watchpoint_type);
10827}
10828
10829/* Implement the "resources_needed" breakpoint_ops method for
10830 masked hardware watchpoints. */
10831
10832static int
10833resources_needed_masked_watchpoint (const struct bp_location *bl)
10834{
3a5c3e22
PA
10835 struct watchpoint *w = (struct watchpoint *) bl->owner;
10836
10837 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10838}
10839
10840/* Implement the "works_in_software_mode" breakpoint_ops method for
10841 masked hardware watchpoints. */
10842
10843static int
10844works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10845{
10846 return 0;
10847}
10848
10849/* Implement the "print_it" breakpoint_ops method for
10850 masked hardware watchpoints. */
10851
10852static enum print_stop_action
10853print_it_masked_watchpoint (bpstat bs)
10854{
10855 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10856 struct ui_out *uiout = current_uiout;
348d480f
PA
10857
10858 /* Masked watchpoints have only one location. */
10859 gdb_assert (b->loc && b->loc->next == NULL);
10860
10861 switch (b->type)
10862 {
10863 case bp_hardware_watchpoint:
10864 annotate_watchpoint (b->number);
10865 if (ui_out_is_mi_like_p (uiout))
10866 ui_out_field_string
10867 (uiout, "reason",
10868 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10869 break;
10870
10871 case bp_read_watchpoint:
10872 if (ui_out_is_mi_like_p (uiout))
10873 ui_out_field_string
10874 (uiout, "reason",
10875 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10876 break;
10877
10878 case bp_access_watchpoint:
10879 if (ui_out_is_mi_like_p (uiout))
10880 ui_out_field_string
10881 (uiout, "reason",
10882 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10883 break;
10884 default:
10885 internal_error (__FILE__, __LINE__,
10886 _("Invalid hardware watchpoint type."));
10887 }
10888
10889 mention (b);
9c06b0b4
TJB
10890 ui_out_text (uiout, _("\n\
10891Check the underlying instruction at PC for the memory\n\
10892address and value which triggered this watchpoint.\n"));
10893 ui_out_text (uiout, "\n");
10894
10895 /* More than one watchpoint may have been triggered. */
10896 return PRINT_UNKNOWN;
10897}
10898
10899/* Implement the "print_one_detail" breakpoint_ops method for
10900 masked hardware watchpoints. */
10901
10902static void
10903print_one_detail_masked_watchpoint (const struct breakpoint *b,
10904 struct ui_out *uiout)
10905{
3a5c3e22
PA
10906 struct watchpoint *w = (struct watchpoint *) b;
10907
9c06b0b4
TJB
10908 /* Masked watchpoints have only one location. */
10909 gdb_assert (b->loc && b->loc->next == NULL);
10910
10911 ui_out_text (uiout, "\tmask ");
3a5c3e22 10912 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
10913 ui_out_text (uiout, "\n");
10914}
10915
10916/* Implement the "print_mention" breakpoint_ops method for
10917 masked hardware watchpoints. */
10918
10919static void
10920print_mention_masked_watchpoint (struct breakpoint *b)
10921{
3a5c3e22 10922 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10923 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
10924 struct cleanup *ui_out_chain;
10925
10926 switch (b->type)
10927 {
10928 case bp_hardware_watchpoint:
10929 ui_out_text (uiout, "Masked hardware watchpoint ");
10930 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10931 break;
10932 case bp_read_watchpoint:
10933 ui_out_text (uiout, "Masked hardware read watchpoint ");
10934 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10935 break;
10936 case bp_access_watchpoint:
10937 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10938 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10939 break;
10940 default:
10941 internal_error (__FILE__, __LINE__,
10942 _("Invalid hardware watchpoint type."));
10943 }
10944
10945 ui_out_field_int (uiout, "number", b->number);
10946 ui_out_text (uiout, ": ");
3a5c3e22 10947 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
10948 do_cleanups (ui_out_chain);
10949}
10950
10951/* Implement the "print_recreate" breakpoint_ops method for
10952 masked hardware watchpoints. */
10953
10954static void
10955print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10956{
3a5c3e22 10957 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10958 char tmp[40];
10959
10960 switch (b->type)
10961 {
10962 case bp_hardware_watchpoint:
10963 fprintf_unfiltered (fp, "watch");
10964 break;
10965 case bp_read_watchpoint:
10966 fprintf_unfiltered (fp, "rwatch");
10967 break;
10968 case bp_access_watchpoint:
10969 fprintf_unfiltered (fp, "awatch");
10970 break;
10971 default:
10972 internal_error (__FILE__, __LINE__,
10973 _("Invalid hardware watchpoint type."));
10974 }
10975
3a5c3e22
PA
10976 sprintf_vma (tmp, w->hw_wp_mask);
10977 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10978 print_recreate_thread (b, fp);
9c06b0b4
TJB
10979}
10980
10981/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10982
2060206e 10983static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10984
10985/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10986
10987static int
10988is_masked_watchpoint (const struct breakpoint *b)
10989{
10990 return b->ops == &masked_watchpoint_breakpoint_ops;
10991}
10992
53a5351d
JM
10993/* accessflag: hw_write: watch write,
10994 hw_read: watch read,
10995 hw_access: watch access (read or write) */
c906108c 10996static void
bbc13ae3 10997watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 10998 int just_location, int internal)
c906108c 10999{
d983da9c 11000 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11001 struct expression *exp;
270140bd 11002 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11003 struct value *val, *mark, *result;
bb9d5f81 11004 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11005 struct frame_info *frame;
bbc13ae3
KS
11006 const char *exp_start = NULL;
11007 const char *exp_end = NULL;
11008 const char *tok, *end_tok;
9c06b0b4 11009 int toklen = -1;
bbc13ae3
KS
11010 const char *cond_start = NULL;
11011 const char *cond_end = NULL;
c906108c 11012 enum bptype bp_type;
37e4754d 11013 int thread = -1;
0cf6dd15 11014 int pc = 0;
9c06b0b4
TJB
11015 /* Flag to indicate whether we are going to use masks for
11016 the hardware watchpoint. */
11017 int use_mask = 0;
11018 CORE_ADDR mask = 0;
3a5c3e22 11019 struct watchpoint *w;
bbc13ae3
KS
11020 char *expression;
11021 struct cleanup *back_to;
c906108c 11022
37e4754d
LM
11023 /* Make sure that we actually have parameters to parse. */
11024 if (arg != NULL && arg[0] != '\0')
11025 {
bbc13ae3
KS
11026 const char *value_start;
11027
11028 exp_end = arg + strlen (arg);
37e4754d 11029
9c06b0b4
TJB
11030 /* Look for "parameter value" pairs at the end
11031 of the arguments string. */
bbc13ae3 11032 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11033 {
11034 /* Skip whitespace at the end of the argument list. */
11035 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11036 tok--;
11037
11038 /* Find the beginning of the last token.
11039 This is the value of the parameter. */
11040 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11041 tok--;
11042 value_start = tok + 1;
11043
11044 /* Skip whitespace. */
11045 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11046 tok--;
11047
11048 end_tok = tok;
11049
11050 /* Find the beginning of the second to last token.
11051 This is the parameter itself. */
11052 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11053 tok--;
11054 tok++;
11055 toklen = end_tok - tok + 1;
11056
61012eef 11057 if (toklen == 6 && startswith (tok, "thread"))
9c06b0b4
TJB
11058 {
11059 /* At this point we've found a "thread" token, which means
11060 the user is trying to set a watchpoint that triggers
11061 only in a specific thread. */
11062 char *endp;
37e4754d 11063
9c06b0b4
TJB
11064 if (thread != -1)
11065 error(_("You can specify only one thread."));
37e4754d 11066
9c06b0b4
TJB
11067 /* Extract the thread ID from the next token. */
11068 thread = strtol (value_start, &endp, 0);
37e4754d 11069
9c06b0b4
TJB
11070 /* Check if the user provided a valid numeric value for the
11071 thread ID. */
11072 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11073 error (_("Invalid thread ID specification %s."), value_start);
11074
11075 /* Check if the thread actually exists. */
11076 if (!valid_thread_id (thread))
af4908ba 11077 invalid_thread_id_error (thread);
9c06b0b4 11078 }
61012eef 11079 else if (toklen == 4 && startswith (tok, "mask"))
9c06b0b4
TJB
11080 {
11081 /* We've found a "mask" token, which means the user wants to
11082 create a hardware watchpoint that is going to have the mask
11083 facility. */
11084 struct value *mask_value, *mark;
37e4754d 11085
9c06b0b4
TJB
11086 if (use_mask)
11087 error(_("You can specify only one mask."));
37e4754d 11088
9c06b0b4 11089 use_mask = just_location = 1;
37e4754d 11090
9c06b0b4
TJB
11091 mark = value_mark ();
11092 mask_value = parse_to_comma_and_eval (&value_start);
11093 mask = value_as_address (mask_value);
11094 value_free_to_mark (mark);
11095 }
11096 else
11097 /* We didn't recognize what we found. We should stop here. */
11098 break;
37e4754d 11099
9c06b0b4
TJB
11100 /* Truncate the string and get rid of the "parameter value" pair before
11101 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11102 exp_end = tok;
9c06b0b4 11103 }
37e4754d 11104 }
bbc13ae3
KS
11105 else
11106 exp_end = arg;
37e4754d 11107
bbc13ae3
KS
11108 /* Parse the rest of the arguments. From here on out, everything
11109 is in terms of a newly allocated string instead of the original
11110 ARG. */
c906108c 11111 innermost_block = NULL;
bbc13ae3
KS
11112 expression = savestring (arg, exp_end - arg);
11113 back_to = make_cleanup (xfree, expression);
11114 exp_start = arg = expression;
1bb9788d 11115 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11116 exp_end = arg;
fa8a61dc
TT
11117 /* Remove trailing whitespace from the expression before saving it.
11118 This makes the eventual display of the expression string a bit
11119 prettier. */
11120 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11121 --exp_end;
11122
65d79d4b
SDJ
11123 /* Checking if the expression is not constant. */
11124 if (watchpoint_exp_is_const (exp))
11125 {
11126 int len;
11127
11128 len = exp_end - exp_start;
11129 while (len > 0 && isspace (exp_start[len - 1]))
11130 len--;
11131 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11132 }
11133
c906108c
SS
11134 exp_valid_block = innermost_block;
11135 mark = value_mark ();
3a1115a0 11136 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11137
bb9d5f81
PP
11138 if (val != NULL && just_location)
11139 {
11140 saved_bitpos = value_bitpos (val);
11141 saved_bitsize = value_bitsize (val);
11142 }
11143
06a64a0b
TT
11144 if (just_location)
11145 {
9c06b0b4
TJB
11146 int ret;
11147
06a64a0b 11148 exp_valid_block = NULL;
a1442452 11149 val = value_addr (result);
06a64a0b
TT
11150 release_value (val);
11151 value_free_to_mark (mark);
9c06b0b4
TJB
11152
11153 if (use_mask)
11154 {
11155 ret = target_masked_watch_num_registers (value_as_address (val),
11156 mask);
11157 if (ret == -1)
11158 error (_("This target does not support masked watchpoints."));
11159 else if (ret == -2)
11160 error (_("Invalid mask or memory region."));
11161 }
06a64a0b
TT
11162 }
11163 else if (val != NULL)
fa4727a6 11164 release_value (val);
c906108c 11165
bbc13ae3
KS
11166 tok = skip_spaces_const (arg);
11167 end_tok = skip_to_space_const (tok);
c906108c
SS
11168
11169 toklen = end_tok - tok;
11170 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11171 {
2d134ed3
PA
11172 struct expression *cond;
11173
60e1c644 11174 innermost_block = NULL;
c906108c 11175 tok = cond_start = end_tok + 1;
1bb9788d 11176 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11177
11178 /* The watchpoint expression may not be local, but the condition
11179 may still be. E.g.: `watch global if local > 0'. */
11180 cond_exp_valid_block = innermost_block;
11181
2d134ed3 11182 xfree (cond);
c906108c
SS
11183 cond_end = tok;
11184 }
11185 if (*tok)
8a3fe4f8 11186 error (_("Junk at end of command."));
c906108c 11187
d983da9c 11188 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11189
11190 /* If the expression is "local", then set up a "watchpoint scope"
11191 breakpoint at the point where we've left the scope of the watchpoint
11192 expression. Create the scope breakpoint before the watchpoint, so
11193 that we will encounter it first in bpstat_stop_status. */
60e1c644 11194 if (exp_valid_block && frame)
d983da9c 11195 {
edb3359d
DJ
11196 if (frame_id_p (frame_unwind_caller_id (frame)))
11197 {
11198 scope_breakpoint
a6d9a66e
UW
11199 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11200 frame_unwind_caller_pc (frame),
06edf0c0
PA
11201 bp_watchpoint_scope,
11202 &momentary_breakpoint_ops);
d983da9c 11203
edb3359d 11204 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11205
edb3359d
DJ
11206 /* Automatically delete the breakpoint when it hits. */
11207 scope_breakpoint->disposition = disp_del;
d983da9c 11208
edb3359d
DJ
11209 /* Only break in the proper frame (help with recursion). */
11210 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11211
edb3359d 11212 /* Set the address at which we will stop. */
a6d9a66e
UW
11213 scope_breakpoint->loc->gdbarch
11214 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11215 scope_breakpoint->loc->requested_address
11216 = frame_unwind_caller_pc (frame);
11217 scope_breakpoint->loc->address
a6d9a66e
UW
11218 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11219 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11220 scope_breakpoint->type);
11221 }
d983da9c
DJ
11222 }
11223
e8369a73
AB
11224 /* Now set up the breakpoint. We create all watchpoints as hardware
11225 watchpoints here even if hardware watchpoints are turned off, a call
11226 to update_watchpoint later in this function will cause the type to
11227 drop back to bp_watchpoint (software watchpoint) if required. */
11228
11229 if (accessflag == hw_read)
11230 bp_type = bp_read_watchpoint;
11231 else if (accessflag == hw_access)
11232 bp_type = bp_access_watchpoint;
11233 else
11234 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11235
11236 w = XCNEW (struct watchpoint);
11237 b = &w->base;
348d480f 11238 if (use_mask)
3a5c3e22
PA
11239 init_raw_breakpoint_without_location (b, NULL, bp_type,
11240 &masked_watchpoint_breakpoint_ops);
348d480f 11241 else
3a5c3e22
PA
11242 init_raw_breakpoint_without_location (b, NULL, bp_type,
11243 &watchpoint_breakpoint_ops);
37e4754d 11244 b->thread = thread;
b5de0fa7 11245 b->disposition = disp_donttouch;
348d480f 11246 b->pspace = current_program_space;
3a5c3e22
PA
11247 w->exp = exp;
11248 w->exp_valid_block = exp_valid_block;
11249 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11250 if (just_location)
11251 {
11252 struct type *t = value_type (val);
11253 CORE_ADDR addr = value_as_address (val);
11254 char *name;
11255
11256 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11257 name = type_to_string (t);
11258
3a5c3e22 11259 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11260 core_addr_to_string (addr));
06a64a0b
TT
11261 xfree (name);
11262
3a5c3e22 11263 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11264 (int) (exp_end - exp_start), exp_start);
11265
06a64a0b
TT
11266 /* The above expression is in C. */
11267 b->language = language_c;
11268 }
11269 else
3a5c3e22 11270 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11271
11272 if (use_mask)
11273 {
3a5c3e22 11274 w->hw_wp_mask = mask;
9c06b0b4
TJB
11275 }
11276 else
11277 {
3a5c3e22 11278 w->val = val;
bb9d5f81
PP
11279 w->val_bitpos = saved_bitpos;
11280 w->val_bitsize = saved_bitsize;
3a5c3e22 11281 w->val_valid = 1;
9c06b0b4 11282 }
77b06cd7 11283
c906108c
SS
11284 if (cond_start)
11285 b->cond_string = savestring (cond_start, cond_end - cond_start);
11286 else
11287 b->cond_string = 0;
c5aa993b 11288
c906108c 11289 if (frame)
f6bc2008 11290 {
3a5c3e22
PA
11291 w->watchpoint_frame = get_frame_id (frame);
11292 w->watchpoint_thread = inferior_ptid;
f6bc2008 11293 }
c906108c 11294 else
f6bc2008 11295 {
3a5c3e22
PA
11296 w->watchpoint_frame = null_frame_id;
11297 w->watchpoint_thread = null_ptid;
f6bc2008 11298 }
c906108c 11299
d983da9c 11300 if (scope_breakpoint != NULL)
c906108c 11301 {
d983da9c
DJ
11302 /* The scope breakpoint is related to the watchpoint. We will
11303 need to act on them together. */
11304 b->related_breakpoint = scope_breakpoint;
11305 scope_breakpoint->related_breakpoint = b;
c906108c 11306 }
d983da9c 11307
06a64a0b
TT
11308 if (!just_location)
11309 value_free_to_mark (mark);
2d134ed3 11310
492d29ea 11311 TRY
a9634178
TJB
11312 {
11313 /* Finally update the new watchpoint. This creates the locations
11314 that should be inserted. */
3a5c3e22 11315 update_watchpoint (w, 1);
a9634178 11316 }
492d29ea 11317 CATCH (e, RETURN_MASK_ALL)
a9634178
TJB
11318 {
11319 delete_breakpoint (b);
11320 throw_exception (e);
11321 }
492d29ea 11322 END_CATCH
a9634178 11323
3ea46bff 11324 install_breakpoint (internal, b, 1);
bbc13ae3 11325 do_cleanups (back_to);
c906108c
SS
11326}
11327
e09342b5 11328/* Return count of debug registers needed to watch the given expression.
e09342b5 11329 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11330
c906108c 11331static int
a9634178 11332can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11333{
11334 int found_memory_cnt = 0;
2e70b7b9 11335 struct value *head = v;
c906108c
SS
11336
11337 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11338 if (!can_use_hw_watchpoints)
c906108c 11339 return 0;
c5aa993b 11340
5c44784c
JM
11341 /* Make sure that the value of the expression depends only upon
11342 memory contents, and values computed from them within GDB. If we
11343 find any register references or function calls, we can't use a
11344 hardware watchpoint.
11345
11346 The idea here is that evaluating an expression generates a series
11347 of values, one holding the value of every subexpression. (The
11348 expression a*b+c has five subexpressions: a, b, a*b, c, and
11349 a*b+c.) GDB's values hold almost enough information to establish
11350 the criteria given above --- they identify memory lvalues,
11351 register lvalues, computed values, etcetera. So we can evaluate
11352 the expression, and then scan the chain of values that leaves
11353 behind to decide whether we can detect any possible change to the
11354 expression's final value using only hardware watchpoints.
11355
11356 However, I don't think that the values returned by inferior
11357 function calls are special in any way. So this function may not
11358 notice that an expression involving an inferior function call
11359 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11360 for (; v; v = value_next (v))
c906108c 11361 {
5c44784c 11362 if (VALUE_LVAL (v) == lval_memory)
c906108c 11363 {
8464be76
DJ
11364 if (v != head && value_lazy (v))
11365 /* A lazy memory lvalue in the chain is one that GDB never
11366 needed to fetch; we either just used its address (e.g.,
11367 `a' in `a.b') or we never needed it at all (e.g., `a'
11368 in `a,b'). This doesn't apply to HEAD; if that is
11369 lazy then it was not readable, but watch it anyway. */
5c44784c 11370 ;
53a5351d 11371 else
5c44784c
JM
11372 {
11373 /* Ahh, memory we actually used! Check if we can cover
11374 it with hardware watchpoints. */
df407dfe 11375 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11376
11377 /* We only watch structs and arrays if user asked for it
11378 explicitly, never if they just happen to appear in a
11379 middle of some value chain. */
11380 if (v == head
11381 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11382 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11383 {
42ae5230 11384 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11385 int len;
11386 int num_regs;
11387
a9634178 11388 len = (target_exact_watchpoints
e09342b5
TJB
11389 && is_scalar_type_recursive (vtype))?
11390 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11391
e09342b5
TJB
11392 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11393 if (!num_regs)
2e70b7b9
MS
11394 return 0;
11395 else
e09342b5 11396 found_memory_cnt += num_regs;
2e70b7b9 11397 }
5c44784c 11398 }
c5aa993b 11399 }
5086187c
AC
11400 else if (VALUE_LVAL (v) != not_lval
11401 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11402 return 0; /* These are values from the history (e.g., $1). */
5086187c 11403 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11404 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11405 }
11406
11407 /* The expression itself looks suitable for using a hardware
11408 watchpoint, but give the target machine a chance to reject it. */
11409 return found_memory_cnt;
11410}
11411
8b93c638 11412void
84f4c1fe 11413watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11414{
84f4c1fe 11415 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11416}
11417
06a64a0b
TT
11418/* A helper function that looks for the "-location" argument and then
11419 calls watch_command_1. */
11420
11421static void
11422watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11423{
11424 int just_location = 0;
11425
11426 if (arg
11427 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11428 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11429 {
e9cafbcc 11430 arg = skip_spaces (arg);
06a64a0b
TT
11431 just_location = 1;
11432 }
11433
84f4c1fe 11434 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11435}
8926118c 11436
c5aa993b 11437static void
fba45db2 11438watch_command (char *arg, int from_tty)
c906108c 11439{
06a64a0b 11440 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11441}
11442
8b93c638 11443void
84f4c1fe 11444rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11445{
84f4c1fe 11446 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11447}
8926118c 11448
c5aa993b 11449static void
fba45db2 11450rwatch_command (char *arg, int from_tty)
c906108c 11451{
06a64a0b 11452 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11453}
11454
8b93c638 11455void
84f4c1fe 11456awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11457{
84f4c1fe 11458 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11459}
8926118c 11460
c5aa993b 11461static void
fba45db2 11462awatch_command (char *arg, int from_tty)
c906108c 11463{
06a64a0b 11464 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11465}
c906108c 11466\f
c5aa993b 11467
43ff13b4 11468/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11469 because it uses the mechanisms of breakpoints. */
11470
bfec99b2
PA
11471struct until_break_command_continuation_args
11472{
11473 struct breakpoint *breakpoint;
11474 struct breakpoint *breakpoint2;
186c406b 11475 int thread_num;
bfec99b2
PA
11476};
11477
43ff13b4 11478/* This function is called by fetch_inferior_event via the
4a64f543 11479 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11480 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11481 command. */
c2c6d25f 11482static void
fa4cd53f 11483until_break_command_continuation (void *arg, int err)
43ff13b4 11484{
bfec99b2
PA
11485 struct until_break_command_continuation_args *a = arg;
11486
11487 delete_breakpoint (a->breakpoint);
11488 if (a->breakpoint2)
11489 delete_breakpoint (a->breakpoint2);
186c406b 11490 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11491}
11492
c906108c 11493void
ae66c1fc 11494until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11495{
11496 struct symtabs_and_lines sals;
11497 struct symtab_and_line sal;
8556afb4
PA
11498 struct frame_info *frame;
11499 struct gdbarch *frame_gdbarch;
11500 struct frame_id stack_frame_id;
11501 struct frame_id caller_frame_id;
c906108c 11502 struct breakpoint *breakpoint;
f107f563 11503 struct breakpoint *breakpoint2 = NULL;
f00aae0f 11504 struct cleanup *old_chain, *cleanup;
186c406b
TT
11505 int thread;
11506 struct thread_info *tp;
f00aae0f 11507 struct event_location *location;
c906108c 11508
70509625 11509 clear_proceed_status (0);
c906108c
SS
11510
11511 /* Set a breakpoint where the user wants it and at return from
4a64f543 11512 this function. */
c5aa993b 11513
f00aae0f
KS
11514 location = string_to_event_location (&arg, current_language);
11515 cleanup = make_cleanup_delete_event_location (location);
11516
1bfeeb0f 11517 if (last_displayed_sal_is_valid ())
f00aae0f 11518 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11519 get_last_displayed_symtab (),
f8eba3c6 11520 get_last_displayed_line ());
c906108c 11521 else
f00aae0f 11522 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
f8eba3c6 11523 (struct symtab *) NULL, 0);
c5aa993b 11524
c906108c 11525 if (sals.nelts != 1)
8a3fe4f8 11526 error (_("Couldn't get information on specified line."));
c5aa993b 11527
c906108c 11528 sal = sals.sals[0];
4a64f543 11529 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11530
c906108c 11531 if (*arg)
8a3fe4f8 11532 error (_("Junk at end of arguments."));
c5aa993b 11533
c906108c 11534 resolve_sal_pc (&sal);
c5aa993b 11535
186c406b
TT
11536 tp = inferior_thread ();
11537 thread = tp->num;
11538
883bc8d1
PA
11539 old_chain = make_cleanup (null_cleanup, NULL);
11540
8556afb4
PA
11541 /* Note linespec handling above invalidates the frame chain.
11542 Installing a breakpoint also invalidates the frame chain (as it
11543 may need to switch threads), so do any frame handling before
11544 that. */
11545
11546 frame = get_selected_frame (NULL);
11547 frame_gdbarch = get_frame_arch (frame);
11548 stack_frame_id = get_stack_frame_id (frame);
11549 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11550
ae66c1fc
EZ
11551 /* Keep within the current frame, or in frames called by the current
11552 one. */
edb3359d 11553
883bc8d1 11554 if (frame_id_p (caller_frame_id))
c906108c 11555 {
883bc8d1
PA
11556 struct symtab_and_line sal2;
11557
11558 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11559 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11560 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11561 sal2,
11562 caller_frame_id,
f107f563
VP
11563 bp_until);
11564 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11565
883bc8d1 11566 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11567 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11568 }
c5aa993b 11569
c70a6932
JK
11570 /* set_momentary_breakpoint could invalidate FRAME. */
11571 frame = NULL;
11572
883bc8d1
PA
11573 if (anywhere)
11574 /* If the user told us to continue until a specified location,
11575 we don't specify a frame at which we need to stop. */
11576 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11577 null_frame_id, bp_until);
11578 else
11579 /* Otherwise, specify the selected frame, because we want to stop
11580 only at the very same frame. */
11581 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11582 stack_frame_id, bp_until);
11583 make_cleanup_delete_breakpoint (breakpoint);
11584
64ce06e4 11585 proceed (-1, GDB_SIGNAL_DEFAULT);
f107f563 11586
4a64f543
MS
11587 /* If we are running asynchronously, and proceed call above has
11588 actually managed to start the target, arrange for breakpoints to
11589 be deleted when the target stops. Otherwise, we're already
11590 stopped and delete breakpoints via cleanup chain. */
f107f563 11591
0b333c5e 11592 if (is_running (inferior_ptid))
f107f563 11593 {
8d749320
SM
11594 struct until_break_command_continuation_args *args =
11595 XNEW (struct until_break_command_continuation_args);
f107f563 11596
bfec99b2
PA
11597 args->breakpoint = breakpoint;
11598 args->breakpoint2 = breakpoint2;
186c406b 11599 args->thread_num = thread;
f107f563
VP
11600
11601 discard_cleanups (old_chain);
95e54da7
PA
11602 add_continuation (inferior_thread (),
11603 until_break_command_continuation, args,
604ead4a 11604 xfree);
f107f563
VP
11605 }
11606 else
c5aa993b 11607 do_cleanups (old_chain);
f00aae0f
KS
11608
11609 do_cleanups (cleanup);
c906108c 11610}
ae66c1fc 11611
c906108c
SS
11612/* This function attempts to parse an optional "if <cond>" clause
11613 from the arg string. If one is not found, it returns NULL.
c5aa993b 11614
c906108c
SS
11615 Else, it returns a pointer to the condition string. (It does not
11616 attempt to evaluate the string against a particular block.) And,
11617 it updates arg to point to the first character following the parsed
4a64f543 11618 if clause in the arg string. */
53a5351d 11619
916703c0 11620char *
fba45db2 11621ep_parse_optional_if_clause (char **arg)
c906108c 11622{
c5aa993b
JM
11623 char *cond_string;
11624
11625 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11626 return NULL;
c5aa993b 11627
4a64f543 11628 /* Skip the "if" keyword. */
c906108c 11629 (*arg) += 2;
c5aa993b 11630
c906108c 11631 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11632 condition string. */
e9cafbcc 11633 *arg = skip_spaces (*arg);
c906108c 11634 cond_string = *arg;
c5aa993b 11635
4a64f543
MS
11636 /* Assume that the condition occupies the remainder of the arg
11637 string. */
c906108c 11638 (*arg) += strlen (cond_string);
c5aa993b 11639
c906108c
SS
11640 return cond_string;
11641}
c5aa993b 11642
c906108c
SS
11643/* Commands to deal with catching events, such as signals, exceptions,
11644 process start/exit, etc. */
c5aa993b
JM
11645
11646typedef enum
11647{
44feb3ce
TT
11648 catch_fork_temporary, catch_vfork_temporary,
11649 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11650}
11651catch_fork_kind;
11652
c906108c 11653static void
cc59ec59
MS
11654catch_fork_command_1 (char *arg, int from_tty,
11655 struct cmd_list_element *command)
c906108c 11656{
a6d9a66e 11657 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11658 char *cond_string = NULL;
44feb3ce
TT
11659 catch_fork_kind fork_kind;
11660 int tempflag;
11661
11662 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11663 tempflag = (fork_kind == catch_fork_temporary
11664 || fork_kind == catch_vfork_temporary);
c5aa993b 11665
44feb3ce
TT
11666 if (!arg)
11667 arg = "";
e9cafbcc 11668 arg = skip_spaces (arg);
c5aa993b 11669
c906108c 11670 /* The allowed syntax is:
c5aa993b
JM
11671 catch [v]fork
11672 catch [v]fork if <cond>
11673
4a64f543 11674 First, check if there's an if clause. */
c906108c 11675 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11676
c906108c 11677 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11678 error (_("Junk at end of arguments."));
c5aa993b 11679
c906108c 11680 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11681 and enable reporting of such events. */
c5aa993b
JM
11682 switch (fork_kind)
11683 {
44feb3ce
TT
11684 case catch_fork_temporary:
11685 case catch_fork_permanent:
a6d9a66e 11686 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11687 &catch_fork_breakpoint_ops);
c906108c 11688 break;
44feb3ce
TT
11689 case catch_vfork_temporary:
11690 case catch_vfork_permanent:
a6d9a66e 11691 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11692 &catch_vfork_breakpoint_ops);
c906108c 11693 break;
c5aa993b 11694 default:
8a3fe4f8 11695 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11696 break;
c5aa993b 11697 }
c906108c
SS
11698}
11699
11700static void
cc59ec59
MS
11701catch_exec_command_1 (char *arg, int from_tty,
11702 struct cmd_list_element *command)
c906108c 11703{
b4d90040 11704 struct exec_catchpoint *c;
a6d9a66e 11705 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11706 int tempflag;
c5aa993b 11707 char *cond_string = NULL;
c906108c 11708
44feb3ce
TT
11709 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11710
11711 if (!arg)
11712 arg = "";
e9cafbcc 11713 arg = skip_spaces (arg);
c906108c
SS
11714
11715 /* The allowed syntax is:
c5aa993b
JM
11716 catch exec
11717 catch exec if <cond>
c906108c 11718
4a64f543 11719 First, check if there's an if clause. */
c906108c
SS
11720 cond_string = ep_parse_optional_if_clause (&arg);
11721
11722 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11723 error (_("Junk at end of arguments."));
c906108c 11724
b4d90040
PA
11725 c = XNEW (struct exec_catchpoint);
11726 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11727 &catch_exec_breakpoint_ops);
11728 c->exec_pathname = NULL;
11729
3ea46bff 11730 install_breakpoint (0, &c->base, 1);
c906108c 11731}
c5aa993b 11732
9ac4176b 11733void
28010a5d
PA
11734init_ada_exception_breakpoint (struct breakpoint *b,
11735 struct gdbarch *gdbarch,
11736 struct symtab_and_line sal,
11737 char *addr_string,
c0a91b2b 11738 const struct breakpoint_ops *ops,
28010a5d 11739 int tempflag,
349774ef 11740 int enabled,
28010a5d 11741 int from_tty)
f7f9143b 11742{
f7f9143b
JB
11743 if (from_tty)
11744 {
5af949e3
UW
11745 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11746 if (!loc_gdbarch)
11747 loc_gdbarch = gdbarch;
11748
6c95b8df
PA
11749 describe_other_breakpoints (loc_gdbarch,
11750 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11751 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11752 version for exception catchpoints, because two catchpoints
11753 used for different exception names will use the same address.
11754 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11755 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11756 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11757 the user what type of catchpoint it is. The above is good
11758 enough for now, though. */
11759 }
11760
28010a5d 11761 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11762
349774ef 11763 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11764 b->disposition = tempflag ? disp_del : disp_donttouch;
f00aae0f
KS
11765 b->location = string_to_event_location (&addr_string,
11766 language_def (language_ada));
f7f9143b 11767 b->language = language_ada;
f7f9143b
JB
11768}
11769
c906108c 11770static void
fba45db2 11771catch_command (char *arg, int from_tty)
c906108c 11772{
44feb3ce 11773 error (_("Catch requires an event name."));
c906108c
SS
11774}
11775\f
11776
11777static void
fba45db2 11778tcatch_command (char *arg, int from_tty)
c906108c 11779{
44feb3ce 11780 error (_("Catch requires an event name."));
c906108c
SS
11781}
11782
8a2c437b
TT
11783/* A qsort comparison function that sorts breakpoints in order. */
11784
11785static int
11786compare_breakpoints (const void *a, const void *b)
11787{
11788 const breakpoint_p *ba = a;
11789 uintptr_t ua = (uintptr_t) *ba;
11790 const breakpoint_p *bb = b;
11791 uintptr_t ub = (uintptr_t) *bb;
11792
11793 if ((*ba)->number < (*bb)->number)
11794 return -1;
11795 else if ((*ba)->number > (*bb)->number)
11796 return 1;
11797
11798 /* Now sort by address, in case we see, e..g, two breakpoints with
11799 the number 0. */
11800 if (ua < ub)
11801 return -1;
94b0e70d 11802 return ua > ub ? 1 : 0;
8a2c437b
TT
11803}
11804
80f8a6eb 11805/* Delete breakpoints by address or line. */
c906108c
SS
11806
11807static void
fba45db2 11808clear_command (char *arg, int from_tty)
c906108c 11809{
8a2c437b 11810 struct breakpoint *b, *prev;
d6e956e5
VP
11811 VEC(breakpoint_p) *found = 0;
11812 int ix;
c906108c
SS
11813 int default_match;
11814 struct symtabs_and_lines sals;
11815 struct symtab_and_line sal;
c906108c 11816 int i;
8a2c437b 11817 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11818
11819 if (arg)
11820 {
39cf75f7
DE
11821 sals = decode_line_with_current_source (arg,
11822 (DECODE_LINE_FUNFIRSTLINE
11823 | DECODE_LINE_LIST_MODE));
cf4ded82 11824 make_cleanup (xfree, sals.sals);
c906108c
SS
11825 default_match = 0;
11826 }
11827 else
11828 {
8d749320 11829 sals.sals = XNEW (struct symtab_and_line);
80f8a6eb 11830 make_cleanup (xfree, sals.sals);
4a64f543 11831 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11832
11833 /* Set sal's line, symtab, pc, and pspace to the values
11834 corresponding to the last call to print_frame_info. If the
11835 codepoint is not valid, this will set all the fields to 0. */
11836 get_last_displayed_sal (&sal);
c906108c 11837 if (sal.symtab == 0)
8a3fe4f8 11838 error (_("No source file specified."));
c906108c
SS
11839
11840 sals.sals[0] = sal;
11841 sals.nelts = 1;
11842
11843 default_match = 1;
11844 }
11845
4a64f543
MS
11846 /* We don't call resolve_sal_pc here. That's not as bad as it
11847 seems, because all existing breakpoints typically have both
11848 file/line and pc set. So, if clear is given file/line, we can
11849 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11850
11851 We only support clearing given the address explicitly
11852 present in breakpoint table. Say, we've set breakpoint
4a64f543 11853 at file:line. There were several PC values for that file:line,
ed0616c6 11854 due to optimization, all in one block.
4a64f543
MS
11855
11856 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11857 PC corresponding to the same file:line, the breakpoint won't
11858 be cleared. We probably can still clear the breakpoint, but
11859 since the other PC value is never presented to user, user
11860 can only find it by guessing, and it does not seem important
11861 to support that. */
11862
4a64f543
MS
11863 /* For each line spec given, delete bps which correspond to it. Do
11864 it in two passes, solely to preserve the current behavior that
11865 from_tty is forced true if we delete more than one
11866 breakpoint. */
c906108c 11867
80f8a6eb 11868 found = NULL;
8a2c437b 11869 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11870 for (i = 0; i < sals.nelts; i++)
11871 {
05cba821
JK
11872 const char *sal_fullname;
11873
c906108c 11874 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11875 If line given (pc == 0), clear all bpts on specified line.
11876 If defaulting, clear all bpts on default line
c906108c 11877 or at default pc.
c5aa993b
JM
11878
11879 defaulting sal.pc != 0 tests to do
11880
11881 0 1 pc
11882 1 1 pc _and_ line
11883 0 0 line
11884 1 0 <can't happen> */
c906108c
SS
11885
11886 sal = sals.sals[i];
05cba821
JK
11887 sal_fullname = (sal.symtab == NULL
11888 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 11889
4a64f543 11890 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11891 ALL_BREAKPOINTS (b)
c5aa993b 11892 {
0d381245 11893 int match = 0;
4a64f543 11894 /* Are we going to delete b? */
cc60f2e3 11895 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11896 {
11897 struct bp_location *loc = b->loc;
11898 for (; loc; loc = loc->next)
11899 {
f8eba3c6
TT
11900 /* If the user specified file:line, don't allow a PC
11901 match. This matches historical gdb behavior. */
11902 int pc_match = (!sal.explicit_line
11903 && sal.pc
11904 && (loc->pspace == sal.pspace)
11905 && (loc->address == sal.pc)
11906 && (!section_is_overlay (loc->section)
11907 || loc->section == sal.section));
4aac40c8
TT
11908 int line_match = 0;
11909
11910 if ((default_match || sal.explicit_line)
2f202fde 11911 && loc->symtab != NULL
05cba821 11912 && sal_fullname != NULL
4aac40c8 11913 && sal.pspace == loc->pspace
05cba821
JK
11914 && loc->line_number == sal.line
11915 && filename_cmp (symtab_to_fullname (loc->symtab),
11916 sal_fullname) == 0)
11917 line_match = 1;
4aac40c8 11918
0d381245
VP
11919 if (pc_match || line_match)
11920 {
11921 match = 1;
11922 break;
11923 }
11924 }
11925 }
11926
11927 if (match)
d6e956e5 11928 VEC_safe_push(breakpoint_p, found, b);
c906108c 11929 }
80f8a6eb 11930 }
8a2c437b 11931
80f8a6eb 11932 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11933 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11934 {
11935 if (arg)
8a3fe4f8 11936 error (_("No breakpoint at %s."), arg);
80f8a6eb 11937 else
8a3fe4f8 11938 error (_("No breakpoint at this line."));
80f8a6eb 11939 }
c906108c 11940
8a2c437b
TT
11941 /* Remove duplicates from the vec. */
11942 qsort (VEC_address (breakpoint_p, found),
11943 VEC_length (breakpoint_p, found),
11944 sizeof (breakpoint_p),
11945 compare_breakpoints);
11946 prev = VEC_index (breakpoint_p, found, 0);
11947 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11948 {
11949 if (b == prev)
11950 {
11951 VEC_ordered_remove (breakpoint_p, found, ix);
11952 --ix;
11953 }
11954 }
11955
d6e956e5 11956 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11957 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11958 if (from_tty)
a3f17187 11959 {
d6e956e5 11960 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11961 printf_unfiltered (_("Deleted breakpoint "));
11962 else
11963 printf_unfiltered (_("Deleted breakpoints "));
11964 }
d6e956e5
VP
11965
11966 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11967 {
c5aa993b 11968 if (from_tty)
d6e956e5
VP
11969 printf_unfiltered ("%d ", b->number);
11970 delete_breakpoint (b);
c906108c 11971 }
80f8a6eb
MS
11972 if (from_tty)
11973 putchar_unfiltered ('\n');
8a2c437b
TT
11974
11975 do_cleanups (cleanups);
c906108c
SS
11976}
11977\f
11978/* Delete breakpoint in BS if they are `delete' breakpoints and
11979 all breakpoints that are marked for deletion, whether hit or not.
11980 This is called after any breakpoint is hit, or after errors. */
11981
11982void
fba45db2 11983breakpoint_auto_delete (bpstat bs)
c906108c 11984{
35df4500 11985 struct breakpoint *b, *b_tmp;
c906108c
SS
11986
11987 for (; bs; bs = bs->next)
f431efe5
PA
11988 if (bs->breakpoint_at
11989 && bs->breakpoint_at->disposition == disp_del
c906108c 11990 && bs->stop)
f431efe5 11991 delete_breakpoint (bs->breakpoint_at);
c906108c 11992
35df4500 11993 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11994 {
b5de0fa7 11995 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11996 delete_breakpoint (b);
11997 }
c906108c
SS
11998}
11999
4a64f543
MS
12000/* A comparison function for bp_location AP and BP being interfaced to
12001 qsort. Sort elements primarily by their ADDRESS (no matter what
12002 does breakpoint_address_is_meaningful say for its OWNER),
1a853c52 12003 secondarily by ordering first permanent elements and
4a64f543 12004 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12005 qsort being an unstable algorithm. */
876fa593
JK
12006
12007static int
494cfb0f 12008bp_location_compare (const void *ap, const void *bp)
876fa593 12009{
494cfb0f
JK
12010 struct bp_location *a = *(void **) ap;
12011 struct bp_location *b = *(void **) bp;
876fa593
JK
12012
12013 if (a->address != b->address)
12014 return (a->address > b->address) - (a->address < b->address);
12015
dea2aa5f
LM
12016 /* Sort locations at the same address by their pspace number, keeping
12017 locations of the same inferior (in a multi-inferior environment)
12018 grouped. */
12019
12020 if (a->pspace->num != b->pspace->num)
12021 return ((a->pspace->num > b->pspace->num)
12022 - (a->pspace->num < b->pspace->num));
12023
876fa593 12024 /* Sort permanent breakpoints first. */
1a853c52
PA
12025 if (a->permanent != b->permanent)
12026 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
876fa593 12027
c56a97f9
JK
12028 /* Make the internal GDB representation stable across GDB runs
12029 where A and B memory inside GDB can differ. Breakpoint locations of
12030 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12031
12032 if (a->owner->number != b->owner->number)
c56a97f9
JK
12033 return ((a->owner->number > b->owner->number)
12034 - (a->owner->number < b->owner->number));
876fa593
JK
12035
12036 return (a > b) - (a < b);
12037}
12038
876fa593 12039/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12040 bp_location_shadow_len_after_address_max according to the current
12041 content of the bp_location array. */
f7545552
TT
12042
12043static void
876fa593 12044bp_location_target_extensions_update (void)
f7545552 12045{
876fa593
JK
12046 struct bp_location *bl, **blp_tmp;
12047
12048 bp_location_placed_address_before_address_max = 0;
12049 bp_location_shadow_len_after_address_max = 0;
12050
12051 ALL_BP_LOCATIONS (bl, blp_tmp)
12052 {
12053 CORE_ADDR start, end, addr;
12054
12055 if (!bp_location_has_shadow (bl))
12056 continue;
12057
12058 start = bl->target_info.placed_address;
12059 end = start + bl->target_info.shadow_len;
12060
12061 gdb_assert (bl->address >= start);
12062 addr = bl->address - start;
12063 if (addr > bp_location_placed_address_before_address_max)
12064 bp_location_placed_address_before_address_max = addr;
12065
12066 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12067
12068 gdb_assert (bl->address < end);
12069 addr = end - bl->address;
12070 if (addr > bp_location_shadow_len_after_address_max)
12071 bp_location_shadow_len_after_address_max = addr;
12072 }
f7545552
TT
12073}
12074
1e4d1764
YQ
12075/* Download tracepoint locations if they haven't been. */
12076
12077static void
12078download_tracepoint_locations (void)
12079{
7ed2c994 12080 struct breakpoint *b;
1e4d1764
YQ
12081 struct cleanup *old_chain;
12082
12083 if (!target_can_download_tracepoint ())
12084 return;
12085
12086 old_chain = save_current_space_and_thread ();
12087
7ed2c994 12088 ALL_TRACEPOINTS (b)
1e4d1764 12089 {
7ed2c994 12090 struct bp_location *bl;
1e4d1764 12091 struct tracepoint *t;
f2a8bc8a 12092 int bp_location_downloaded = 0;
1e4d1764 12093
7ed2c994 12094 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12095 ? !may_insert_fast_tracepoints
12096 : !may_insert_tracepoints))
12097 continue;
12098
7ed2c994
YQ
12099 for (bl = b->loc; bl; bl = bl->next)
12100 {
12101 /* In tracepoint, locations are _never_ duplicated, so
12102 should_be_inserted is equivalent to
12103 unduplicated_should_be_inserted. */
12104 if (!should_be_inserted (bl) || bl->inserted)
12105 continue;
1e4d1764 12106
7ed2c994 12107 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12108
7ed2c994 12109 target_download_tracepoint (bl);
1e4d1764 12110
7ed2c994 12111 bl->inserted = 1;
f2a8bc8a 12112 bp_location_downloaded = 1;
7ed2c994
YQ
12113 }
12114 t = (struct tracepoint *) b;
12115 t->number_on_target = b->number;
f2a8bc8a
YQ
12116 if (bp_location_downloaded)
12117 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12118 }
12119
12120 do_cleanups (old_chain);
12121}
12122
934709f0
PW
12123/* Swap the insertion/duplication state between two locations. */
12124
12125static void
12126swap_insertion (struct bp_location *left, struct bp_location *right)
12127{
12128 const int left_inserted = left->inserted;
12129 const int left_duplicate = left->duplicate;
b775012e 12130 const int left_needs_update = left->needs_update;
934709f0
PW
12131 const struct bp_target_info left_target_info = left->target_info;
12132
1e4d1764
YQ
12133 /* Locations of tracepoints can never be duplicated. */
12134 if (is_tracepoint (left->owner))
12135 gdb_assert (!left->duplicate);
12136 if (is_tracepoint (right->owner))
12137 gdb_assert (!right->duplicate);
12138
934709f0
PW
12139 left->inserted = right->inserted;
12140 left->duplicate = right->duplicate;
b775012e 12141 left->needs_update = right->needs_update;
934709f0
PW
12142 left->target_info = right->target_info;
12143 right->inserted = left_inserted;
12144 right->duplicate = left_duplicate;
b775012e 12145 right->needs_update = left_needs_update;
934709f0
PW
12146 right->target_info = left_target_info;
12147}
12148
b775012e
LM
12149/* Force the re-insertion of the locations at ADDRESS. This is called
12150 once a new/deleted/modified duplicate location is found and we are evaluating
12151 conditions on the target's side. Such conditions need to be updated on
12152 the target. */
12153
12154static void
12155force_breakpoint_reinsertion (struct bp_location *bl)
12156{
12157 struct bp_location **locp = NULL, **loc2p;
12158 struct bp_location *loc;
12159 CORE_ADDR address = 0;
12160 int pspace_num;
12161
12162 address = bl->address;
12163 pspace_num = bl->pspace->num;
12164
12165 /* This is only meaningful if the target is
12166 evaluating conditions and if the user has
12167 opted for condition evaluation on the target's
12168 side. */
12169 if (gdb_evaluates_breakpoint_condition_p ()
12170 || !target_supports_evaluation_of_breakpoint_conditions ())
12171 return;
12172
12173 /* Flag all breakpoint locations with this address and
12174 the same program space as the location
12175 as "its condition has changed". We need to
12176 update the conditions on the target's side. */
12177 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12178 {
12179 loc = *loc2p;
12180
12181 if (!is_breakpoint (loc->owner)
12182 || pspace_num != loc->pspace->num)
12183 continue;
12184
12185 /* Flag the location appropriately. We use a different state to
12186 let everyone know that we already updated the set of locations
12187 with addr bl->address and program space bl->pspace. This is so
12188 we don't have to keep calling these functions just to mark locations
12189 that have already been marked. */
12190 loc->condition_changed = condition_updated;
12191
12192 /* Free the agent expression bytecode as well. We will compute
12193 it later on. */
12194 if (loc->cond_bytecode)
12195 {
12196 free_agent_expr (loc->cond_bytecode);
12197 loc->cond_bytecode = NULL;
12198 }
12199 }
12200}
44702360
PA
12201/* Called whether new breakpoints are created, or existing breakpoints
12202 deleted, to update the global location list and recompute which
12203 locations are duplicate of which.
b775012e 12204
04086b45
PA
12205 The INSERT_MODE flag determines whether locations may not, may, or
12206 shall be inserted now. See 'enum ugll_insert_mode' for more
12207 info. */
b60e7edf 12208
0d381245 12209static void
44702360 12210update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12211{
74960c60 12212 struct breakpoint *b;
876fa593 12213 struct bp_location **locp, *loc;
f7545552 12214 struct cleanup *cleanups;
b775012e
LM
12215 /* Last breakpoint location address that was marked for update. */
12216 CORE_ADDR last_addr = 0;
12217 /* Last breakpoint location program space that was marked for update. */
12218 int last_pspace_num = -1;
f7545552 12219
2d134ed3
PA
12220 /* Used in the duplicates detection below. When iterating over all
12221 bp_locations, points to the first bp_location of a given address.
12222 Breakpoints and watchpoints of different types are never
12223 duplicates of each other. Keep one pointer for each type of
12224 breakpoint/watchpoint, so we only need to loop over all locations
12225 once. */
12226 struct bp_location *bp_loc_first; /* breakpoint */
12227 struct bp_location *wp_loc_first; /* hardware watchpoint */
12228 struct bp_location *awp_loc_first; /* access watchpoint */
12229 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12230
4a64f543
MS
12231 /* Saved former bp_location array which we compare against the newly
12232 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12233 struct bp_location **old_location, **old_locp;
12234 unsigned old_location_count;
12235
12236 old_location = bp_location;
12237 old_location_count = bp_location_count;
12238 bp_location = NULL;
12239 bp_location_count = 0;
12240 cleanups = make_cleanup (xfree, old_location);
0d381245 12241
74960c60 12242 ALL_BREAKPOINTS (b)
876fa593
JK
12243 for (loc = b->loc; loc; loc = loc->next)
12244 bp_location_count++;
12245
8d749320 12246 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
876fa593
JK
12247 locp = bp_location;
12248 ALL_BREAKPOINTS (b)
12249 for (loc = b->loc; loc; loc = loc->next)
12250 *locp++ = loc;
12251 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12252 bp_location_compare);
876fa593
JK
12253
12254 bp_location_target_extensions_update ();
74960c60 12255
4a64f543
MS
12256 /* Identify bp_location instances that are no longer present in the
12257 new list, and therefore should be freed. Note that it's not
12258 necessary that those locations should be removed from inferior --
12259 if there's another location at the same address (previously
12260 marked as duplicate), we don't need to remove/insert the
12261 location.
876fa593 12262
4a64f543
MS
12263 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12264 and former bp_location array state respectively. */
876fa593
JK
12265
12266 locp = bp_location;
12267 for (old_locp = old_location; old_locp < old_location + old_location_count;
12268 old_locp++)
74960c60 12269 {
876fa593 12270 struct bp_location *old_loc = *old_locp;
c7d46a38 12271 struct bp_location **loc2p;
876fa593 12272
e5dd4106 12273 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12274 not, we have to free it. */
c7d46a38 12275 int found_object = 0;
20874c92
VP
12276 /* Tells if the location should remain inserted in the target. */
12277 int keep_in_target = 0;
12278 int removed = 0;
876fa593 12279
4a64f543
MS
12280 /* Skip LOCP entries which will definitely never be needed.
12281 Stop either at or being the one matching OLD_LOC. */
876fa593 12282 while (locp < bp_location + bp_location_count
c7d46a38 12283 && (*locp)->address < old_loc->address)
876fa593 12284 locp++;
c7d46a38
PA
12285
12286 for (loc2p = locp;
12287 (loc2p < bp_location + bp_location_count
12288 && (*loc2p)->address == old_loc->address);
12289 loc2p++)
12290 {
b775012e
LM
12291 /* Check if this is a new/duplicated location or a duplicated
12292 location that had its condition modified. If so, we want to send
12293 its condition to the target if evaluation of conditions is taking
12294 place there. */
12295 if ((*loc2p)->condition_changed == condition_modified
12296 && (last_addr != old_loc->address
12297 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12298 {
b775012e
LM
12299 force_breakpoint_reinsertion (*loc2p);
12300 last_pspace_num = old_loc->pspace->num;
c7d46a38 12301 }
b775012e
LM
12302
12303 if (*loc2p == old_loc)
12304 found_object = 1;
c7d46a38 12305 }
74960c60 12306
b775012e
LM
12307 /* We have already handled this address, update it so that we don't
12308 have to go through updates again. */
12309 last_addr = old_loc->address;
12310
12311 /* Target-side condition evaluation: Handle deleted locations. */
12312 if (!found_object)
12313 force_breakpoint_reinsertion (old_loc);
12314
4a64f543
MS
12315 /* If this location is no longer present, and inserted, look if
12316 there's maybe a new location at the same address. If so,
12317 mark that one inserted, and don't remove this one. This is
12318 needed so that we don't have a time window where a breakpoint
12319 at certain location is not inserted. */
74960c60 12320
876fa593 12321 if (old_loc->inserted)
0d381245 12322 {
4a64f543
MS
12323 /* If the location is inserted now, we might have to remove
12324 it. */
74960c60 12325
876fa593 12326 if (found_object && should_be_inserted (old_loc))
74960c60 12327 {
4a64f543
MS
12328 /* The location is still present in the location list,
12329 and still should be inserted. Don't do anything. */
20874c92 12330 keep_in_target = 1;
74960c60
VP
12331 }
12332 else
12333 {
b775012e
LM
12334 /* This location still exists, but it won't be kept in the
12335 target since it may have been disabled. We proceed to
12336 remove its target-side condition. */
12337
4a64f543
MS
12338 /* The location is either no longer present, or got
12339 disabled. See if there's another location at the
12340 same address, in which case we don't need to remove
12341 this one from the target. */
876fa593 12342
2bdf28a0 12343 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12344 if (breakpoint_address_is_meaningful (old_loc->owner))
12345 {
876fa593 12346 for (loc2p = locp;
c7d46a38
PA
12347 (loc2p < bp_location + bp_location_count
12348 && (*loc2p)->address == old_loc->address);
876fa593
JK
12349 loc2p++)
12350 {
12351 struct bp_location *loc2 = *loc2p;
12352
2d134ed3 12353 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12354 {
85d721b8
PA
12355 /* Read watchpoint locations are switched to
12356 access watchpoints, if the former are not
12357 supported, but the latter are. */
12358 if (is_hardware_watchpoint (old_loc->owner))
12359 {
12360 gdb_assert (is_hardware_watchpoint (loc2->owner));
12361 loc2->watchpoint_type = old_loc->watchpoint_type;
12362 }
12363
934709f0
PW
12364 /* loc2 is a duplicated location. We need to check
12365 if it should be inserted in case it will be
12366 unduplicated. */
12367 if (loc2 != old_loc
12368 && unduplicated_should_be_inserted (loc2))
c7d46a38 12369 {
934709f0 12370 swap_insertion (old_loc, loc2);
c7d46a38
PA
12371 keep_in_target = 1;
12372 break;
12373 }
876fa593
JK
12374 }
12375 }
12376 }
74960c60
VP
12377 }
12378
20874c92
VP
12379 if (!keep_in_target)
12380 {
876fa593 12381 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12382 {
4a64f543
MS
12383 /* This is just about all we can do. We could keep
12384 this location on the global list, and try to
12385 remove it next time, but there's no particular
12386 reason why we will succeed next time.
20874c92 12387
4a64f543
MS
12388 Note that at this point, old_loc->owner is still
12389 valid, as delete_breakpoint frees the breakpoint
12390 only after calling us. */
3e43a32a
MS
12391 printf_filtered (_("warning: Error removing "
12392 "breakpoint %d\n"),
876fa593 12393 old_loc->owner->number);
20874c92
VP
12394 }
12395 removed = 1;
12396 }
0d381245 12397 }
74960c60
VP
12398
12399 if (!found_object)
1c5cfe86 12400 {
fbea99ea 12401 if (removed && target_is_non_stop_p ()
1cf4d951 12402 && need_moribund_for_location_type (old_loc))
20874c92 12403 {
db82e815
PA
12404 /* This location was removed from the target. In
12405 non-stop mode, a race condition is possible where
12406 we've removed a breakpoint, but stop events for that
12407 breakpoint are already queued and will arrive later.
12408 We apply an heuristic to be able to distinguish such
12409 SIGTRAPs from other random SIGTRAPs: we keep this
12410 breakpoint location for a bit, and will retire it
12411 after we see some number of events. The theory here
12412 is that reporting of events should, "on the average",
12413 be fair, so after a while we'll see events from all
12414 threads that have anything of interest, and no longer
12415 need to keep this breakpoint location around. We
12416 don't hold locations forever so to reduce chances of
12417 mistaking a non-breakpoint SIGTRAP for a breakpoint
12418 SIGTRAP.
12419
12420 The heuristic failing can be disastrous on
12421 decr_pc_after_break targets.
12422
12423 On decr_pc_after_break targets, like e.g., x86-linux,
12424 if we fail to recognize a late breakpoint SIGTRAP,
12425 because events_till_retirement has reached 0 too
12426 soon, we'll fail to do the PC adjustment, and report
12427 a random SIGTRAP to the user. When the user resumes
12428 the inferior, it will most likely immediately crash
2dec564e 12429 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12430 corrupted, because of being resumed e.g., in the
12431 middle of a multi-byte instruction, or skipped a
12432 one-byte instruction. This was actually seen happen
12433 on native x86-linux, and should be less rare on
12434 targets that do not support new thread events, like
12435 remote, due to the heuristic depending on
12436 thread_count.
12437
12438 Mistaking a random SIGTRAP for a breakpoint trap
12439 causes similar symptoms (PC adjustment applied when
12440 it shouldn't), but then again, playing with SIGTRAPs
12441 behind the debugger's back is asking for trouble.
12442
12443 Since hardware watchpoint traps are always
12444 distinguishable from other traps, so we don't need to
12445 apply keep hardware watchpoint moribund locations
12446 around. We simply always ignore hardware watchpoint
12447 traps we can no longer explain. */
12448
876fa593
JK
12449 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12450 old_loc->owner = NULL;
20874c92 12451
876fa593 12452 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12453 }
12454 else
f431efe5
PA
12455 {
12456 old_loc->owner = NULL;
12457 decref_bp_location (&old_loc);
12458 }
20874c92 12459 }
74960c60 12460 }
1c5cfe86 12461
348d480f
PA
12462 /* Rescan breakpoints at the same address and section, marking the
12463 first one as "first" and any others as "duplicates". This is so
12464 that the bpt instruction is only inserted once. If we have a
12465 permanent breakpoint at the same place as BPT, make that one the
12466 official one, and the rest as duplicates. Permanent breakpoints
12467 are sorted first for the same address.
12468
12469 Do the same for hardware watchpoints, but also considering the
12470 watchpoint's type (regular/access/read) and length. */
12471
12472 bp_loc_first = NULL;
12473 wp_loc_first = NULL;
12474 awp_loc_first = NULL;
12475 rwp_loc_first = NULL;
12476 ALL_BP_LOCATIONS (loc, locp)
12477 {
12478 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12479 non-NULL. */
348d480f 12480 struct bp_location **loc_first_p;
d3fbdd86 12481 b = loc->owner;
348d480f 12482
6f380991 12483 if (!unduplicated_should_be_inserted (loc)
348d480f 12484 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12485 /* Don't detect duplicate for tracepoint locations because they are
12486 never duplicated. See the comments in field `duplicate' of
12487 `struct bp_location'. */
348d480f 12488 || is_tracepoint (b))
b775012e
LM
12489 {
12490 /* Clear the condition modification flag. */
12491 loc->condition_changed = condition_unchanged;
12492 continue;
12493 }
348d480f 12494
348d480f
PA
12495 if (b->type == bp_hardware_watchpoint)
12496 loc_first_p = &wp_loc_first;
12497 else if (b->type == bp_read_watchpoint)
12498 loc_first_p = &rwp_loc_first;
12499 else if (b->type == bp_access_watchpoint)
12500 loc_first_p = &awp_loc_first;
12501 else
12502 loc_first_p = &bp_loc_first;
12503
12504 if (*loc_first_p == NULL
12505 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12506 || !breakpoint_locations_match (loc, *loc_first_p))
12507 {
12508 *loc_first_p = loc;
12509 loc->duplicate = 0;
b775012e
LM
12510
12511 if (is_breakpoint (loc->owner) && loc->condition_changed)
12512 {
12513 loc->needs_update = 1;
12514 /* Clear the condition modification flag. */
12515 loc->condition_changed = condition_unchanged;
12516 }
348d480f
PA
12517 continue;
12518 }
12519
934709f0
PW
12520
12521 /* This and the above ensure the invariant that the first location
12522 is not duplicated, and is the inserted one.
12523 All following are marked as duplicated, and are not inserted. */
12524 if (loc->inserted)
12525 swap_insertion (loc, *loc_first_p);
348d480f
PA
12526 loc->duplicate = 1;
12527
b775012e
LM
12528 /* Clear the condition modification flag. */
12529 loc->condition_changed = condition_unchanged;
348d480f
PA
12530 }
12531
a25a5a45 12532 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12533 {
04086b45 12534 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12535 insert_breakpoint_locations ();
12536 else
12537 {
44702360
PA
12538 /* Even though the caller told us to not insert new
12539 locations, we may still need to update conditions on the
12540 target's side of breakpoints that were already inserted
12541 if the target is evaluating breakpoint conditions. We
b775012e
LM
12542 only update conditions for locations that are marked
12543 "needs_update". */
12544 update_inserted_breakpoint_locations ();
12545 }
12546 }
348d480f 12547
04086b45 12548 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12549 download_tracepoint_locations ();
12550
348d480f
PA
12551 do_cleanups (cleanups);
12552}
12553
12554void
12555breakpoint_retire_moribund (void)
12556{
12557 struct bp_location *loc;
12558 int ix;
12559
12560 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12561 if (--(loc->events_till_retirement) == 0)
12562 {
12563 decref_bp_location (&loc);
12564 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12565 --ix;
12566 }
12567}
12568
12569static void
44702360 12570update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 12571{
348d480f 12572
492d29ea
PA
12573 TRY
12574 {
12575 update_global_location_list (insert_mode);
12576 }
12577 CATCH (e, RETURN_MASK_ERROR)
12578 {
12579 }
12580 END_CATCH
348d480f
PA
12581}
12582
12583/* Clear BKP from a BPS. */
12584
12585static void
12586bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12587{
12588 bpstat bs;
12589
12590 for (bs = bps; bs; bs = bs->next)
12591 if (bs->breakpoint_at == bpt)
12592 {
12593 bs->breakpoint_at = NULL;
12594 bs->old_val = NULL;
12595 /* bs->commands will be freed later. */
12596 }
12597}
12598
12599/* Callback for iterate_over_threads. */
12600static int
12601bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12602{
12603 struct breakpoint *bpt = data;
12604
12605 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12606 return 0;
12607}
12608
12609/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12610 callbacks. */
12611
12612static void
12613say_where (struct breakpoint *b)
12614{
12615 struct value_print_options opts;
12616
12617 get_user_print_options (&opts);
12618
12619 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12620 single string. */
12621 if (b->loc == NULL)
12622 {
f00aae0f
KS
12623 /* For pending locations, the output differs slightly based
12624 on b->extra_string. If this is non-NULL, it contains either
12625 a condition or dprintf arguments. */
12626 if (b->extra_string == NULL)
12627 {
12628 printf_filtered (_(" (%s) pending."),
12629 event_location_to_string (b->location));
12630 }
12631 else if (b->type == bp_dprintf)
12632 {
12633 printf_filtered (_(" (%s,%s) pending."),
12634 event_location_to_string (b->location),
12635 b->extra_string);
12636 }
12637 else
12638 {
12639 printf_filtered (_(" (%s %s) pending."),
12640 event_location_to_string (b->location),
12641 b->extra_string);
12642 }
348d480f
PA
12643 }
12644 else
12645 {
2f202fde 12646 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12647 {
12648 printf_filtered (" at ");
12649 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12650 gdb_stdout);
12651 }
2f202fde 12652 if (b->loc->symtab != NULL)
f8eba3c6
TT
12653 {
12654 /* If there is a single location, we can print the location
12655 more nicely. */
12656 if (b->loc->next == NULL)
12657 printf_filtered (": file %s, line %d.",
05cba821
JK
12658 symtab_to_filename_for_display (b->loc->symtab),
12659 b->loc->line_number);
f8eba3c6
TT
12660 else
12661 /* This is not ideal, but each location may have a
12662 different file name, and this at least reflects the
12663 real situation somewhat. */
f00aae0f
KS
12664 printf_filtered (": %s.",
12665 event_location_to_string (b->location));
f8eba3c6 12666 }
348d480f
PA
12667
12668 if (b->loc->next)
12669 {
12670 struct bp_location *loc = b->loc;
12671 int n = 0;
12672 for (; loc; loc = loc->next)
12673 ++n;
12674 printf_filtered (" (%d locations)", n);
12675 }
12676 }
12677}
12678
348d480f
PA
12679/* Default bp_location_ops methods. */
12680
12681static void
12682bp_location_dtor (struct bp_location *self)
12683{
12684 xfree (self->cond);
b775012e
LM
12685 if (self->cond_bytecode)
12686 free_agent_expr (self->cond_bytecode);
348d480f 12687 xfree (self->function_name);
8b4f3082
PA
12688
12689 VEC_free (agent_expr_p, self->target_info.conditions);
12690 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12691}
12692
12693static const struct bp_location_ops bp_location_ops =
12694{
12695 bp_location_dtor
12696};
12697
2060206e
PA
12698/* Default breakpoint_ops methods all breakpoint_ops ultimately
12699 inherit from. */
348d480f 12700
2060206e
PA
12701static void
12702base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12703{
12704 decref_counted_command_line (&self->commands);
12705 xfree (self->cond_string);
fb81d016 12706 xfree (self->extra_string);
f8eba3c6 12707 xfree (self->filter);
f00aae0f
KS
12708 delete_event_location (self->location);
12709 delete_event_location (self->location_range_end);
348d480f
PA
12710}
12711
2060206e
PA
12712static struct bp_location *
12713base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12714{
12715 struct bp_location *loc;
12716
12717 loc = XNEW (struct bp_location);
12718 init_bp_location (loc, &bp_location_ops, self);
12719 return loc;
12720}
12721
2060206e
PA
12722static void
12723base_breakpoint_re_set (struct breakpoint *b)
12724{
12725 /* Nothing to re-set. */
12726}
12727
12728#define internal_error_pure_virtual_called() \
12729 gdb_assert_not_reached ("pure virtual function called")
12730
12731static int
12732base_breakpoint_insert_location (struct bp_location *bl)
12733{
12734 internal_error_pure_virtual_called ();
12735}
12736
12737static int
12738base_breakpoint_remove_location (struct bp_location *bl)
12739{
12740 internal_error_pure_virtual_called ();
12741}
12742
12743static int
12744base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12745 struct address_space *aspace,
09ac7c10
TT
12746 CORE_ADDR bp_addr,
12747 const struct target_waitstatus *ws)
2060206e
PA
12748{
12749 internal_error_pure_virtual_called ();
12750}
12751
12752static void
12753base_breakpoint_check_status (bpstat bs)
12754{
12755 /* Always stop. */
12756}
12757
12758/* A "works_in_software_mode" breakpoint_ops method that just internal
12759 errors. */
12760
12761static int
12762base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12763{
12764 internal_error_pure_virtual_called ();
12765}
12766
12767/* A "resources_needed" breakpoint_ops method that just internal
12768 errors. */
12769
12770static int
12771base_breakpoint_resources_needed (const struct bp_location *bl)
12772{
12773 internal_error_pure_virtual_called ();
12774}
12775
12776static enum print_stop_action
12777base_breakpoint_print_it (bpstat bs)
12778{
12779 internal_error_pure_virtual_called ();
12780}
12781
12782static void
12783base_breakpoint_print_one_detail (const struct breakpoint *self,
12784 struct ui_out *uiout)
12785{
12786 /* nothing */
12787}
12788
12789static void
12790base_breakpoint_print_mention (struct breakpoint *b)
12791{
12792 internal_error_pure_virtual_called ();
12793}
12794
12795static void
12796base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12797{
12798 internal_error_pure_virtual_called ();
12799}
12800
983af33b 12801static void
f00aae0f
KS
12802base_breakpoint_create_sals_from_location
12803 (const struct event_location *location,
12804 struct linespec_result *canonical,
12805 enum bptype type_wanted)
983af33b
SDJ
12806{
12807 internal_error_pure_virtual_called ();
12808}
12809
12810static void
12811base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12812 struct linespec_result *c,
983af33b 12813 char *cond_string,
e7e0cddf 12814 char *extra_string,
983af33b
SDJ
12815 enum bptype type_wanted,
12816 enum bpdisp disposition,
12817 int thread,
12818 int task, int ignore_count,
12819 const struct breakpoint_ops *o,
12820 int from_tty, int enabled,
44f238bb 12821 int internal, unsigned flags)
983af33b
SDJ
12822{
12823 internal_error_pure_virtual_called ();
12824}
12825
12826static void
f00aae0f
KS
12827base_breakpoint_decode_location (struct breakpoint *b,
12828 const struct event_location *location,
983af33b
SDJ
12829 struct symtabs_and_lines *sals)
12830{
12831 internal_error_pure_virtual_called ();
12832}
12833
ab04a2af
TT
12834/* The default 'explains_signal' method. */
12835
47591c29 12836static int
427cd150 12837base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 12838{
47591c29 12839 return 1;
ab04a2af
TT
12840}
12841
9d6e6e84
HZ
12842/* The default "after_condition_true" method. */
12843
12844static void
12845base_breakpoint_after_condition_true (struct bpstats *bs)
12846{
12847 /* Nothing to do. */
12848}
12849
ab04a2af 12850struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
12851{
12852 base_breakpoint_dtor,
12853 base_breakpoint_allocate_location,
12854 base_breakpoint_re_set,
12855 base_breakpoint_insert_location,
12856 base_breakpoint_remove_location,
12857 base_breakpoint_breakpoint_hit,
12858 base_breakpoint_check_status,
12859 base_breakpoint_resources_needed,
12860 base_breakpoint_works_in_software_mode,
12861 base_breakpoint_print_it,
12862 NULL,
12863 base_breakpoint_print_one_detail,
12864 base_breakpoint_print_mention,
983af33b 12865 base_breakpoint_print_recreate,
5f700d83 12866 base_breakpoint_create_sals_from_location,
983af33b 12867 base_breakpoint_create_breakpoints_sal,
5f700d83 12868 base_breakpoint_decode_location,
9d6e6e84
HZ
12869 base_breakpoint_explains_signal,
12870 base_breakpoint_after_condition_true,
2060206e
PA
12871};
12872
12873/* Default breakpoint_ops methods. */
12874
12875static void
348d480f
PA
12876bkpt_re_set (struct breakpoint *b)
12877{
06edf0c0 12878 /* FIXME: is this still reachable? */
f00aae0f 12879 if (event_location_empty_p (b->location))
06edf0c0 12880 {
f00aae0f 12881 /* Anything without a location can't be re-set. */
348d480f 12882 delete_breakpoint (b);
06edf0c0 12883 return;
348d480f 12884 }
06edf0c0
PA
12885
12886 breakpoint_re_set_default (b);
348d480f
PA
12887}
12888
2060206e 12889static int
348d480f
PA
12890bkpt_insert_location (struct bp_location *bl)
12891{
12892 if (bl->loc_type == bp_loc_hardware_breakpoint)
7c16b83e 12893 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
348d480f 12894 else
7c16b83e 12895 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12896}
12897
2060206e 12898static int
348d480f
PA
12899bkpt_remove_location (struct bp_location *bl)
12900{
12901 if (bl->loc_type == bp_loc_hardware_breakpoint)
12902 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12903 else
7c16b83e 12904 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
348d480f
PA
12905}
12906
2060206e 12907static int
348d480f 12908bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12909 struct address_space *aspace, CORE_ADDR bp_addr,
12910 const struct target_waitstatus *ws)
348d480f 12911{
09ac7c10 12912 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12913 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12914 return 0;
12915
348d480f
PA
12916 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12917 aspace, bp_addr))
12918 return 0;
12919
12920 if (overlay_debugging /* unmapped overlay section */
12921 && section_is_overlay (bl->section)
12922 && !section_is_mapped (bl->section))
12923 return 0;
12924
12925 return 1;
12926}
12927
cd1608cc
PA
12928static int
12929dprintf_breakpoint_hit (const struct bp_location *bl,
12930 struct address_space *aspace, CORE_ADDR bp_addr,
12931 const struct target_waitstatus *ws)
12932{
12933 if (dprintf_style == dprintf_style_agent
12934 && target_can_run_breakpoint_commands ())
12935 {
12936 /* An agent-style dprintf never causes a stop. If we see a trap
12937 for this address it must be for a breakpoint that happens to
12938 be set at the same address. */
12939 return 0;
12940 }
12941
12942 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12943}
12944
2060206e 12945static int
348d480f
PA
12946bkpt_resources_needed (const struct bp_location *bl)
12947{
12948 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12949
12950 return 1;
12951}
12952
2060206e 12953static enum print_stop_action
348d480f
PA
12954bkpt_print_it (bpstat bs)
12955{
348d480f
PA
12956 struct breakpoint *b;
12957 const struct bp_location *bl;
001c8c33 12958 int bp_temp;
79a45e25 12959 struct ui_out *uiout = current_uiout;
348d480f
PA
12960
12961 gdb_assert (bs->bp_location_at != NULL);
12962
12963 bl = bs->bp_location_at;
12964 b = bs->breakpoint_at;
12965
001c8c33
PA
12966 bp_temp = b->disposition == disp_del;
12967 if (bl->address != bl->requested_address)
12968 breakpoint_adjustment_warning (bl->requested_address,
12969 bl->address,
12970 b->number, 1);
12971 annotate_breakpoint (b->number);
12972 if (bp_temp)
12973 ui_out_text (uiout, "\nTemporary breakpoint ");
12974 else
12975 ui_out_text (uiout, "\nBreakpoint ");
12976 if (ui_out_is_mi_like_p (uiout))
348d480f 12977 {
001c8c33
PA
12978 ui_out_field_string (uiout, "reason",
12979 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12980 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 12981 }
001c8c33
PA
12982 ui_out_field_int (uiout, "bkptno", b->number);
12983 ui_out_text (uiout, ", ");
06edf0c0 12984
001c8c33 12985 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12986}
12987
2060206e 12988static void
06edf0c0
PA
12989bkpt_print_mention (struct breakpoint *b)
12990{
79a45e25 12991 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
12992 return;
12993
12994 switch (b->type)
12995 {
12996 case bp_breakpoint:
12997 case bp_gnu_ifunc_resolver:
12998 if (b->disposition == disp_del)
12999 printf_filtered (_("Temporary breakpoint"));
13000 else
13001 printf_filtered (_("Breakpoint"));
13002 printf_filtered (_(" %d"), b->number);
13003 if (b->type == bp_gnu_ifunc_resolver)
13004 printf_filtered (_(" at gnu-indirect-function resolver"));
13005 break;
13006 case bp_hardware_breakpoint:
13007 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13008 break;
e7e0cddf
SS
13009 case bp_dprintf:
13010 printf_filtered (_("Dprintf %d"), b->number);
13011 break;
06edf0c0
PA
13012 }
13013
13014 say_where (b);
13015}
13016
2060206e 13017static void
06edf0c0
PA
13018bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13019{
13020 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13021 fprintf_unfiltered (fp, "tbreak");
13022 else if (tp->type == bp_breakpoint)
13023 fprintf_unfiltered (fp, "break");
13024 else if (tp->type == bp_hardware_breakpoint
13025 && tp->disposition == disp_del)
13026 fprintf_unfiltered (fp, "thbreak");
13027 else if (tp->type == bp_hardware_breakpoint)
13028 fprintf_unfiltered (fp, "hbreak");
13029 else
13030 internal_error (__FILE__, __LINE__,
13031 _("unhandled breakpoint type %d"), (int) tp->type);
13032
f00aae0f
KS
13033 fprintf_unfiltered (fp, " %s",
13034 event_location_to_string (tp->location));
13035
13036 /* Print out extra_string if this breakpoint is pending. It might
13037 contain, for example, conditions that were set by the user. */
13038 if (tp->loc == NULL && tp->extra_string != NULL)
13039 fprintf_unfiltered (fp, " %s", tp->extra_string);
13040
dd11a36c 13041 print_recreate_thread (tp, fp);
06edf0c0
PA
13042}
13043
983af33b 13044static void
f00aae0f
KS
13045bkpt_create_sals_from_location (const struct event_location *location,
13046 struct linespec_result *canonical,
13047 enum bptype type_wanted)
983af33b 13048{
f00aae0f 13049 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13050}
13051
13052static void
13053bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13054 struct linespec_result *canonical,
983af33b 13055 char *cond_string,
e7e0cddf 13056 char *extra_string,
983af33b
SDJ
13057 enum bptype type_wanted,
13058 enum bpdisp disposition,
13059 int thread,
13060 int task, int ignore_count,
13061 const struct breakpoint_ops *ops,
13062 int from_tty, int enabled,
44f238bb 13063 int internal, unsigned flags)
983af33b 13064{
023fa29b 13065 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13066 cond_string, extra_string,
13067 type_wanted,
983af33b
SDJ
13068 disposition, thread, task,
13069 ignore_count, ops, from_tty,
44f238bb 13070 enabled, internal, flags);
983af33b
SDJ
13071}
13072
13073static void
f00aae0f
KS
13074bkpt_decode_location (struct breakpoint *b,
13075 const struct event_location *location,
983af33b
SDJ
13076 struct symtabs_and_lines *sals)
13077{
f00aae0f 13078 decode_location_default (b, location, sals);
983af33b
SDJ
13079}
13080
06edf0c0
PA
13081/* Virtual table for internal breakpoints. */
13082
13083static void
13084internal_bkpt_re_set (struct breakpoint *b)
13085{
13086 switch (b->type)
13087 {
13088 /* Delete overlay event and longjmp master breakpoints; they
13089 will be reset later by breakpoint_re_set. */
13090 case bp_overlay_event:
13091 case bp_longjmp_master:
13092 case bp_std_terminate_master:
13093 case bp_exception_master:
13094 delete_breakpoint (b);
13095 break;
13096
13097 /* This breakpoint is special, it's set up when the inferior
13098 starts and we really don't want to touch it. */
13099 case bp_shlib_event:
13100
13101 /* Like bp_shlib_event, this breakpoint type is special. Once
13102 it is set up, we do not want to touch it. */
13103 case bp_thread_event:
13104 break;
13105 }
13106}
13107
13108static void
13109internal_bkpt_check_status (bpstat bs)
13110{
a9b3a50f
PA
13111 if (bs->breakpoint_at->type == bp_shlib_event)
13112 {
13113 /* If requested, stop when the dynamic linker notifies GDB of
13114 events. This allows the user to get control and place
13115 breakpoints in initializer routines for dynamically loaded
13116 objects (among other things). */
13117 bs->stop = stop_on_solib_events;
13118 bs->print = stop_on_solib_events;
13119 }
13120 else
13121 bs->stop = 0;
06edf0c0
PA
13122}
13123
13124static enum print_stop_action
13125internal_bkpt_print_it (bpstat bs)
13126{
06edf0c0 13127 struct breakpoint *b;
06edf0c0 13128
06edf0c0
PA
13129 b = bs->breakpoint_at;
13130
06edf0c0
PA
13131 switch (b->type)
13132 {
348d480f
PA
13133 case bp_shlib_event:
13134 /* Did we stop because the user set the stop_on_solib_events
13135 variable? (If so, we report this as a generic, "Stopped due
13136 to shlib event" message.) */
edcc5120 13137 print_solib_event (0);
348d480f
PA
13138 break;
13139
13140 case bp_thread_event:
13141 /* Not sure how we will get here.
13142 GDB should not stop for these breakpoints. */
13143 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13144 break;
13145
13146 case bp_overlay_event:
13147 /* By analogy with the thread event, GDB should not stop for these. */
13148 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13149 break;
13150
13151 case bp_longjmp_master:
13152 /* These should never be enabled. */
13153 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13154 break;
13155
13156 case bp_std_terminate_master:
13157 /* These should never be enabled. */
13158 printf_filtered (_("std::terminate Master Breakpoint: "
13159 "gdb should not stop!\n"));
348d480f
PA
13160 break;
13161
13162 case bp_exception_master:
13163 /* These should never be enabled. */
13164 printf_filtered (_("Exception Master Breakpoint: "
13165 "gdb should not stop!\n"));
06edf0c0
PA
13166 break;
13167 }
13168
001c8c33 13169 return PRINT_NOTHING;
06edf0c0
PA
13170}
13171
13172static void
13173internal_bkpt_print_mention (struct breakpoint *b)
13174{
13175 /* Nothing to mention. These breakpoints are internal. */
13176}
13177
06edf0c0
PA
13178/* Virtual table for momentary breakpoints */
13179
13180static void
13181momentary_bkpt_re_set (struct breakpoint *b)
13182{
13183 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13184 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13185 Otherwise these should have been blown away via the cleanup chain
13186 or by breakpoint_init_inferior when we rerun the executable. */
13187}
13188
13189static void
13190momentary_bkpt_check_status (bpstat bs)
13191{
13192 /* Nothing. The point of these breakpoints is causing a stop. */
13193}
13194
13195static enum print_stop_action
13196momentary_bkpt_print_it (bpstat bs)
13197{
79a45e25
PA
13198 struct ui_out *uiout = current_uiout;
13199
001c8c33 13200 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13201 {
001c8c33 13202 struct breakpoint *b = bs->breakpoint_at;
348d480f 13203
001c8c33
PA
13204 switch (b->type)
13205 {
001c8c33
PA
13206 case bp_until:
13207 ui_out_field_string
13208 (uiout, "reason",
13209 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13210 break;
13211 }
348d480f
PA
13212 }
13213
001c8c33 13214 return PRINT_UNKNOWN;
348d480f
PA
13215}
13216
06edf0c0
PA
13217static void
13218momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13219{
06edf0c0 13220 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13221}
13222
e2e4d78b
JK
13223/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13224
13225 It gets cleared already on the removal of the first one of such placed
13226 breakpoints. This is OK as they get all removed altogether. */
13227
13228static void
13229longjmp_bkpt_dtor (struct breakpoint *self)
13230{
13231 struct thread_info *tp = find_thread_id (self->thread);
13232
13233 if (tp)
13234 tp->initiating_frame = null_frame_id;
13235
13236 momentary_breakpoint_ops.dtor (self);
13237}
13238
55aa24fb
SDJ
13239/* Specific methods for probe breakpoints. */
13240
13241static int
13242bkpt_probe_insert_location (struct bp_location *bl)
13243{
13244 int v = bkpt_insert_location (bl);
13245
13246 if (v == 0)
13247 {
13248 /* The insertion was successful, now let's set the probe's semaphore
13249 if needed. */
0ea5cda8
SDJ
13250 if (bl->probe.probe->pops->set_semaphore != NULL)
13251 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13252 bl->probe.objfile,
13253 bl->gdbarch);
55aa24fb
SDJ
13254 }
13255
13256 return v;
13257}
13258
13259static int
13260bkpt_probe_remove_location (struct bp_location *bl)
13261{
13262 /* Let's clear the semaphore before removing the location. */
0ea5cda8
SDJ
13263 if (bl->probe.probe->pops->clear_semaphore != NULL)
13264 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13265 bl->probe.objfile,
13266 bl->gdbarch);
55aa24fb
SDJ
13267
13268 return bkpt_remove_location (bl);
13269}
13270
13271static void
f00aae0f 13272bkpt_probe_create_sals_from_location (const struct event_location *location,
5f700d83 13273 struct linespec_result *canonical,
f00aae0f 13274 enum bptype type_wanted)
55aa24fb
SDJ
13275{
13276 struct linespec_sals lsal;
13277
f00aae0f
KS
13278 lsal.sals = parse_probes (location, canonical);
13279 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
55aa24fb
SDJ
13280 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13281}
13282
13283static void
f00aae0f
KS
13284bkpt_probe_decode_location (struct breakpoint *b,
13285 const struct event_location *location,
55aa24fb
SDJ
13286 struct symtabs_and_lines *sals)
13287{
f00aae0f 13288 *sals = parse_probes (location, NULL);
55aa24fb
SDJ
13289 if (!sals->sals)
13290 error (_("probe not found"));
13291}
13292
348d480f 13293/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13294
348d480f
PA
13295static void
13296tracepoint_re_set (struct breakpoint *b)
13297{
13298 breakpoint_re_set_default (b);
13299}
876fa593 13300
348d480f
PA
13301static int
13302tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13303 struct address_space *aspace, CORE_ADDR bp_addr,
13304 const struct target_waitstatus *ws)
348d480f
PA
13305{
13306 /* By definition, the inferior does not report stops at
13307 tracepoints. */
13308 return 0;
74960c60
VP
13309}
13310
13311static void
348d480f
PA
13312tracepoint_print_one_detail (const struct breakpoint *self,
13313 struct ui_out *uiout)
74960c60 13314{
d9b3f62e
PA
13315 struct tracepoint *tp = (struct tracepoint *) self;
13316 if (tp->static_trace_marker_id)
348d480f
PA
13317 {
13318 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13319
348d480f
PA
13320 ui_out_text (uiout, "\tmarker id is ");
13321 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13322 tp->static_trace_marker_id);
348d480f
PA
13323 ui_out_text (uiout, "\n");
13324 }
0d381245
VP
13325}
13326
a474d7c2 13327static void
348d480f 13328tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13329{
79a45e25 13330 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13331 return;
cc59ec59 13332
348d480f
PA
13333 switch (b->type)
13334 {
13335 case bp_tracepoint:
13336 printf_filtered (_("Tracepoint"));
13337 printf_filtered (_(" %d"), b->number);
13338 break;
13339 case bp_fast_tracepoint:
13340 printf_filtered (_("Fast tracepoint"));
13341 printf_filtered (_(" %d"), b->number);
13342 break;
13343 case bp_static_tracepoint:
13344 printf_filtered (_("Static tracepoint"));
13345 printf_filtered (_(" %d"), b->number);
13346 break;
13347 default:
13348 internal_error (__FILE__, __LINE__,
13349 _("unhandled tracepoint type %d"), (int) b->type);
13350 }
13351
13352 say_where (b);
a474d7c2
PA
13353}
13354
348d480f 13355static void
d9b3f62e 13356tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13357{
d9b3f62e
PA
13358 struct tracepoint *tp = (struct tracepoint *) self;
13359
13360 if (self->type == bp_fast_tracepoint)
348d480f 13361 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13362 if (self->type == bp_static_tracepoint)
348d480f 13363 fprintf_unfiltered (fp, "strace");
d9b3f62e 13364 else if (self->type == bp_tracepoint)
348d480f
PA
13365 fprintf_unfiltered (fp, "trace");
13366 else
13367 internal_error (__FILE__, __LINE__,
d9b3f62e 13368 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13369
f00aae0f
KS
13370 fprintf_unfiltered (fp, " %s",
13371 event_location_to_string (self->location));
d9b3f62e
PA
13372 print_recreate_thread (self, fp);
13373
13374 if (tp->pass_count)
13375 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13376}
13377
983af33b 13378static void
f00aae0f
KS
13379tracepoint_create_sals_from_location (const struct event_location *location,
13380 struct linespec_result *canonical,
13381 enum bptype type_wanted)
983af33b 13382{
f00aae0f 13383 create_sals_from_location_default (location, canonical, type_wanted);
983af33b
SDJ
13384}
13385
13386static void
13387tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13388 struct linespec_result *canonical,
983af33b 13389 char *cond_string,
e7e0cddf 13390 char *extra_string,
983af33b
SDJ
13391 enum bptype type_wanted,
13392 enum bpdisp disposition,
13393 int thread,
13394 int task, int ignore_count,
13395 const struct breakpoint_ops *ops,
13396 int from_tty, int enabled,
44f238bb 13397 int internal, unsigned flags)
983af33b 13398{
023fa29b 13399 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13400 cond_string, extra_string,
13401 type_wanted,
983af33b
SDJ
13402 disposition, thread, task,
13403 ignore_count, ops, from_tty,
44f238bb 13404 enabled, internal, flags);
983af33b
SDJ
13405}
13406
13407static void
f00aae0f
KS
13408tracepoint_decode_location (struct breakpoint *b,
13409 const struct event_location *location,
983af33b
SDJ
13410 struct symtabs_and_lines *sals)
13411{
f00aae0f 13412 decode_location_default (b, location, sals);
983af33b
SDJ
13413}
13414
2060206e 13415struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13416
55aa24fb
SDJ
13417/* The breakpoint_ops structure to be use on tracepoints placed in a
13418 static probe. */
13419
13420static void
f00aae0f
KS
13421tracepoint_probe_create_sals_from_location
13422 (const struct event_location *location,
13423 struct linespec_result *canonical,
13424 enum bptype type_wanted)
55aa24fb
SDJ
13425{
13426 /* We use the same method for breakpoint on probes. */
f00aae0f 13427 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
55aa24fb
SDJ
13428}
13429
13430static void
f00aae0f
KS
13431tracepoint_probe_decode_location (struct breakpoint *b,
13432 const struct event_location *location,
55aa24fb
SDJ
13433 struct symtabs_and_lines *sals)
13434{
13435 /* We use the same method for breakpoint on probes. */
f00aae0f 13436 bkpt_probe_decode_location (b, location, sals);
55aa24fb
SDJ
13437}
13438
13439static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13440
5c2b4418
HZ
13441/* Dprintf breakpoint_ops methods. */
13442
13443static void
13444dprintf_re_set (struct breakpoint *b)
13445{
13446 breakpoint_re_set_default (b);
13447
f00aae0f
KS
13448 /* extra_string should never be non-NULL for dprintf. */
13449 gdb_assert (b->extra_string != NULL);
5c2b4418
HZ
13450
13451 /* 1 - connect to target 1, that can run breakpoint commands.
13452 2 - create a dprintf, which resolves fine.
13453 3 - disconnect from target 1
13454 4 - connect to target 2, that can NOT run breakpoint commands.
13455
13456 After steps #3/#4, you'll want the dprintf command list to
13457 be updated, because target 1 and 2 may well return different
13458 answers for target_can_run_breakpoint_commands().
13459 Given absence of finer grained resetting, we get to do
13460 it all the time. */
13461 if (b->extra_string != NULL)
13462 update_dprintf_command_list (b);
13463}
13464
2d9442cc
HZ
13465/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13466
13467static void
13468dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13469{
f00aae0f
KS
13470 fprintf_unfiltered (fp, "dprintf %s,%s",
13471 event_location_to_string (tp->location),
2d9442cc
HZ
13472 tp->extra_string);
13473 print_recreate_thread (tp, fp);
13474}
13475
9d6e6e84
HZ
13476/* Implement the "after_condition_true" breakpoint_ops method for
13477 dprintf.
13478
13479 dprintf's are implemented with regular commands in their command
13480 list, but we run the commands here instead of before presenting the
13481 stop to the user, as dprintf's don't actually cause a stop. This
13482 also makes it so that the commands of multiple dprintfs at the same
13483 address are all handled. */
13484
13485static void
13486dprintf_after_condition_true (struct bpstats *bs)
13487{
13488 struct cleanup *old_chain;
13489 struct bpstats tmp_bs = { NULL };
13490 struct bpstats *tmp_bs_p = &tmp_bs;
13491
13492 /* dprintf's never cause a stop. This wasn't set in the
13493 check_status hook instead because that would make the dprintf's
13494 condition not be evaluated. */
13495 bs->stop = 0;
13496
13497 /* Run the command list here. Take ownership of it instead of
13498 copying. We never want these commands to run later in
13499 bpstat_do_actions, if a breakpoint that causes a stop happens to
13500 be set at same address as this dprintf, or even if running the
13501 commands here throws. */
13502 tmp_bs.commands = bs->commands;
13503 bs->commands = NULL;
13504 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13505
13506 bpstat_do_actions_1 (&tmp_bs_p);
13507
13508 /* 'tmp_bs.commands' will usually be NULL by now, but
13509 bpstat_do_actions_1 may return early without processing the whole
13510 list. */
13511 do_cleanups (old_chain);
13512}
13513
983af33b
SDJ
13514/* The breakpoint_ops structure to be used on static tracepoints with
13515 markers (`-m'). */
13516
13517static void
f00aae0f 13518strace_marker_create_sals_from_location (const struct event_location *location,
5f700d83 13519 struct linespec_result *canonical,
f00aae0f 13520 enum bptype type_wanted)
983af33b
SDJ
13521{
13522 struct linespec_sals lsal;
f00aae0f
KS
13523 const char *arg_start, *arg;
13524 char *str;
13525 struct cleanup *cleanup;
983af33b 13526
f00aae0f
KS
13527 arg = arg_start = get_linespec_location (location);
13528 lsal.sals = decode_static_tracepoint_spec (&arg);
983af33b 13529
f00aae0f
KS
13530 str = savestring (arg_start, arg - arg_start);
13531 cleanup = make_cleanup (xfree, str);
13532 canonical->location = new_linespec_location (&str);
13533 do_cleanups (cleanup);
983af33b 13534
f00aae0f 13535 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
983af33b
SDJ
13536 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13537}
13538
13539static void
13540strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13541 struct linespec_result *canonical,
983af33b 13542 char *cond_string,
e7e0cddf 13543 char *extra_string,
983af33b
SDJ
13544 enum bptype type_wanted,
13545 enum bpdisp disposition,
13546 int thread,
13547 int task, int ignore_count,
13548 const struct breakpoint_ops *ops,
13549 int from_tty, int enabled,
44f238bb 13550 int internal, unsigned flags)
983af33b
SDJ
13551{
13552 int i;
52d361e1
YQ
13553 struct linespec_sals *lsal = VEC_index (linespec_sals,
13554 canonical->sals, 0);
983af33b
SDJ
13555
13556 /* If the user is creating a static tracepoint by marker id
13557 (strace -m MARKER_ID), then store the sals index, so that
13558 breakpoint_re_set can try to match up which of the newly
13559 found markers corresponds to this one, and, don't try to
13560 expand multiple locations for each sal, given than SALS
13561 already should contain all sals for MARKER_ID. */
13562
13563 for (i = 0; i < lsal->sals.nelts; ++i)
13564 {
13565 struct symtabs_and_lines expanded;
13566 struct tracepoint *tp;
13567 struct cleanup *old_chain;
f00aae0f 13568 struct event_location *location;
983af33b
SDJ
13569
13570 expanded.nelts = 1;
13571 expanded.sals = &lsal->sals.sals[i];
13572
f00aae0f
KS
13573 location = copy_event_location (canonical->location);
13574 old_chain = make_cleanup_delete_event_location (location);
983af33b
SDJ
13575
13576 tp = XCNEW (struct tracepoint);
13577 init_breakpoint_sal (&tp->base, gdbarch, expanded,
f00aae0f 13578 location, NULL,
e7e0cddf
SS
13579 cond_string, extra_string,
13580 type_wanted, disposition,
983af33b 13581 thread, task, ignore_count, ops,
44f238bb 13582 from_tty, enabled, internal, flags,
983af33b
SDJ
13583 canonical->special_display);
13584 /* Given that its possible to have multiple markers with
13585 the same string id, if the user is creating a static
13586 tracepoint by marker id ("strace -m MARKER_ID"), then
13587 store the sals index, so that breakpoint_re_set can
13588 try to match up which of the newly found markers
13589 corresponds to this one */
13590 tp->static_trace_marker_id_idx = i;
13591
13592 install_breakpoint (internal, &tp->base, 0);
13593
13594 discard_cleanups (old_chain);
13595 }
13596}
13597
13598static void
f00aae0f
KS
13599strace_marker_decode_location (struct breakpoint *b,
13600 const struct event_location *location,
983af33b
SDJ
13601 struct symtabs_and_lines *sals)
13602{
13603 struct tracepoint *tp = (struct tracepoint *) b;
f00aae0f 13604 const char *s = get_linespec_location (location);
983af33b 13605
f00aae0f 13606 *sals = decode_static_tracepoint_spec (&s);
983af33b
SDJ
13607 if (sals->nelts > tp->static_trace_marker_id_idx)
13608 {
13609 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13610 sals->nelts = 1;
13611 }
13612 else
13613 error (_("marker %s not found"), tp->static_trace_marker_id);
13614}
13615
13616static struct breakpoint_ops strace_marker_breakpoint_ops;
13617
13618static int
13619strace_marker_p (struct breakpoint *b)
13620{
13621 return b->ops == &strace_marker_breakpoint_ops;
13622}
13623
53a5351d 13624/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13625 structures. */
c906108c
SS
13626
13627void
fba45db2 13628delete_breakpoint (struct breakpoint *bpt)
c906108c 13629{
52f0bd74 13630 struct breakpoint *b;
c906108c 13631
8a3fe4f8 13632 gdb_assert (bpt != NULL);
c906108c 13633
4a64f543
MS
13634 /* Has this bp already been deleted? This can happen because
13635 multiple lists can hold pointers to bp's. bpstat lists are
13636 especial culprits.
13637
13638 One example of this happening is a watchpoint's scope bp. When
13639 the scope bp triggers, we notice that the watchpoint is out of
13640 scope, and delete it. We also delete its scope bp. But the
13641 scope bp is marked "auto-deleting", and is already on a bpstat.
13642 That bpstat is then checked for auto-deleting bp's, which are
13643 deleted.
13644
13645 A real solution to this problem might involve reference counts in
13646 bp's, and/or giving them pointers back to their referencing
13647 bpstat's, and teaching delete_breakpoint to only free a bp's
13648 storage when no more references were extent. A cheaper bandaid
13649 was chosen. */
c906108c
SS
13650 if (bpt->type == bp_none)
13651 return;
13652
4a64f543
MS
13653 /* At least avoid this stale reference until the reference counting
13654 of breakpoints gets resolved. */
d0fb5eae 13655 if (bpt->related_breakpoint != bpt)
e5a0a904 13656 {
d0fb5eae 13657 struct breakpoint *related;
3a5c3e22 13658 struct watchpoint *w;
d0fb5eae
JK
13659
13660 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13661 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13662 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13663 w = (struct watchpoint *) bpt;
13664 else
13665 w = NULL;
13666 if (w != NULL)
13667 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13668
13669 /* Unlink bpt from the bpt->related_breakpoint ring. */
13670 for (related = bpt; related->related_breakpoint != bpt;
13671 related = related->related_breakpoint);
13672 related->related_breakpoint = bpt->related_breakpoint;
13673 bpt->related_breakpoint = bpt;
e5a0a904
JK
13674 }
13675
a9634178
TJB
13676 /* watch_command_1 creates a watchpoint but only sets its number if
13677 update_watchpoint succeeds in creating its bp_locations. If there's
13678 a problem in that process, we'll be asked to delete the half-created
13679 watchpoint. In that case, don't announce the deletion. */
13680 if (bpt->number)
13681 observer_notify_breakpoint_deleted (bpt);
c906108c 13682
c906108c
SS
13683 if (breakpoint_chain == bpt)
13684 breakpoint_chain = bpt->next;
13685
c906108c
SS
13686 ALL_BREAKPOINTS (b)
13687 if (b->next == bpt)
c5aa993b
JM
13688 {
13689 b->next = bpt->next;
13690 break;
13691 }
c906108c 13692
f431efe5
PA
13693 /* Be sure no bpstat's are pointing at the breakpoint after it's
13694 been freed. */
13695 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13696 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13697 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13698 commands are associated with the bpstat; if we remove it here,
13699 then the later call to bpstat_do_actions (&stop_bpstat); in
13700 event-top.c won't do anything, and temporary breakpoints with
13701 commands won't work. */
13702
13703 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13704
4a64f543
MS
13705 /* Now that breakpoint is removed from breakpoint list, update the
13706 global location list. This will remove locations that used to
13707 belong to this breakpoint. Do this before freeing the breakpoint
13708 itself, since remove_breakpoint looks at location's owner. It
13709 might be better design to have location completely
13710 self-contained, but it's not the case now. */
44702360 13711 update_global_location_list (UGLL_DONT_INSERT);
74960c60 13712
348d480f 13713 bpt->ops->dtor (bpt);
4a64f543
MS
13714 /* On the chance that someone will soon try again to delete this
13715 same bp, we mark it as deleted before freeing its storage. */
c906108c 13716 bpt->type = bp_none;
b8c9b27d 13717 xfree (bpt);
c906108c
SS
13718}
13719
4d6140d9
AC
13720static void
13721do_delete_breakpoint_cleanup (void *b)
13722{
13723 delete_breakpoint (b);
13724}
13725
13726struct cleanup *
13727make_cleanup_delete_breakpoint (struct breakpoint *b)
13728{
13729 return make_cleanup (do_delete_breakpoint_cleanup, b);
13730}
13731
51be5b68
PA
13732/* Iterator function to call a user-provided callback function once
13733 for each of B and its related breakpoints. */
13734
13735static void
13736iterate_over_related_breakpoints (struct breakpoint *b,
13737 void (*function) (struct breakpoint *,
13738 void *),
13739 void *data)
13740{
13741 struct breakpoint *related;
13742
13743 related = b;
13744 do
13745 {
13746 struct breakpoint *next;
13747
13748 /* FUNCTION may delete RELATED. */
13749 next = related->related_breakpoint;
13750
13751 if (next == related)
13752 {
13753 /* RELATED is the last ring entry. */
13754 function (related, data);
13755
13756 /* FUNCTION may have deleted it, so we'd never reach back to
13757 B. There's nothing left to do anyway, so just break
13758 out. */
13759 break;
13760 }
13761 else
13762 function (related, data);
13763
13764 related = next;
13765 }
13766 while (related != b);
13767}
95a42b64
TT
13768
13769static void
13770do_delete_breakpoint (struct breakpoint *b, void *ignore)
13771{
13772 delete_breakpoint (b);
13773}
13774
51be5b68
PA
13775/* A callback for map_breakpoint_numbers that calls
13776 delete_breakpoint. */
13777
13778static void
13779do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13780{
13781 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13782}
13783
c906108c 13784void
fba45db2 13785delete_command (char *arg, int from_tty)
c906108c 13786{
35df4500 13787 struct breakpoint *b, *b_tmp;
c906108c 13788
ea9365bb
TT
13789 dont_repeat ();
13790
c906108c
SS
13791 if (arg == 0)
13792 {
13793 int breaks_to_delete = 0;
13794
46c6471b
PA
13795 /* Delete all breakpoints if no argument. Do not delete
13796 internal breakpoints, these have to be deleted with an
13797 explicit breakpoint number argument. */
c5aa993b 13798 ALL_BREAKPOINTS (b)
46c6471b 13799 if (user_breakpoint_p (b))
973d738b
DJ
13800 {
13801 breaks_to_delete = 1;
13802 break;
13803 }
c906108c
SS
13804
13805 /* Ask user only if there are some breakpoints to delete. */
13806 if (!from_tty
e2e0b3e5 13807 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13808 {
35df4500 13809 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13810 if (user_breakpoint_p (b))
c5aa993b 13811 delete_breakpoint (b);
c906108c
SS
13812 }
13813 }
13814 else
51be5b68 13815 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13816}
13817
0d381245
VP
13818static int
13819all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 13820{
0d381245 13821 for (; loc; loc = loc->next)
8645ff69
UW
13822 if (!loc->shlib_disabled
13823 && !loc->pspace->executing_startup)
0d381245
VP
13824 return 0;
13825 return 1;
fe3f5fa8
VP
13826}
13827
776592bf
DE
13828/* Subroutine of update_breakpoint_locations to simplify it.
13829 Return non-zero if multiple fns in list LOC have the same name.
13830 Null names are ignored. */
13831
13832static int
13833ambiguous_names_p (struct bp_location *loc)
13834{
13835 struct bp_location *l;
13836 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13837 (int (*) (const void *,
13838 const void *)) streq,
776592bf
DE
13839 NULL, xcalloc, xfree);
13840
13841 for (l = loc; l != NULL; l = l->next)
13842 {
13843 const char **slot;
13844 const char *name = l->function_name;
13845
13846 /* Allow for some names to be NULL, ignore them. */
13847 if (name == NULL)
13848 continue;
13849
13850 slot = (const char **) htab_find_slot (htab, (const void *) name,
13851 INSERT);
4a64f543
MS
13852 /* NOTE: We can assume slot != NULL here because xcalloc never
13853 returns NULL. */
776592bf
DE
13854 if (*slot != NULL)
13855 {
13856 htab_delete (htab);
13857 return 1;
13858 }
13859 *slot = name;
13860 }
13861
13862 htab_delete (htab);
13863 return 0;
13864}
13865
0fb4aa4b
PA
13866/* When symbols change, it probably means the sources changed as well,
13867 and it might mean the static tracepoint markers are no longer at
13868 the same address or line numbers they used to be at last we
13869 checked. Losing your static tracepoints whenever you rebuild is
13870 undesirable. This function tries to resync/rematch gdb static
13871 tracepoints with the markers on the target, for static tracepoints
13872 that have not been set by marker id. Static tracepoint that have
13873 been set by marker id are reset by marker id in breakpoint_re_set.
13874 The heuristic is:
13875
13876 1) For a tracepoint set at a specific address, look for a marker at
13877 the old PC. If one is found there, assume to be the same marker.
13878 If the name / string id of the marker found is different from the
13879 previous known name, assume that means the user renamed the marker
13880 in the sources, and output a warning.
13881
13882 2) For a tracepoint set at a given line number, look for a marker
13883 at the new address of the old line number. If one is found there,
13884 assume to be the same marker. If the name / string id of the
13885 marker found is different from the previous known name, assume that
13886 means the user renamed the marker in the sources, and output a
13887 warning.
13888
13889 3) If a marker is no longer found at the same address or line, it
13890 may mean the marker no longer exists. But it may also just mean
13891 the code changed a bit. Maybe the user added a few lines of code
13892 that made the marker move up or down (in line number terms). Ask
13893 the target for info about the marker with the string id as we knew
13894 it. If found, update line number and address in the matching
13895 static tracepoint. This will get confused if there's more than one
13896 marker with the same ID (possible in UST, although unadvised
13897 precisely because it confuses tools). */
13898
13899static struct symtab_and_line
13900update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13901{
d9b3f62e 13902 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13903 struct static_tracepoint_marker marker;
13904 CORE_ADDR pc;
0fb4aa4b
PA
13905
13906 pc = sal.pc;
13907 if (sal.line)
13908 find_line_pc (sal.symtab, sal.line, &pc);
13909
13910 if (target_static_tracepoint_marker_at (pc, &marker))
13911 {
d9b3f62e 13912 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13913 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13914 b->number,
d9b3f62e 13915 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13916
d9b3f62e
PA
13917 xfree (tp->static_trace_marker_id);
13918 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13919 release_static_tracepoint_marker (&marker);
13920
13921 return sal;
13922 }
13923
13924 /* Old marker wasn't found on target at lineno. Try looking it up
13925 by string ID. */
13926 if (!sal.explicit_pc
13927 && sal.line != 0
13928 && sal.symtab != NULL
d9b3f62e 13929 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13930 {
13931 VEC(static_tracepoint_marker_p) *markers;
13932
13933 markers
d9b3f62e 13934 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13935
13936 if (!VEC_empty(static_tracepoint_marker_p, markers))
13937 {
80e1d417 13938 struct symtab_and_line sal2;
0fb4aa4b 13939 struct symbol *sym;
80e1d417 13940 struct static_tracepoint_marker *tpmarker;
79a45e25 13941 struct ui_out *uiout = current_uiout;
67994074 13942 struct explicit_location explicit_loc;
0fb4aa4b 13943
80e1d417 13944 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13945
d9b3f62e 13946 xfree (tp->static_trace_marker_id);
80e1d417 13947 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13948
13949 warning (_("marker for static tracepoint %d (%s) not "
13950 "found at previous line number"),
d9b3f62e 13951 b->number, tp->static_trace_marker_id);
0fb4aa4b 13952
80e1d417 13953 init_sal (&sal2);
0fb4aa4b 13954
80e1d417 13955 sal2.pc = tpmarker->address;
0fb4aa4b 13956
80e1d417
AS
13957 sal2 = find_pc_line (tpmarker->address, 0);
13958 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
13959 ui_out_text (uiout, "Now in ");
13960 if (sym)
13961 {
13962 ui_out_field_string (uiout, "func",
13963 SYMBOL_PRINT_NAME (sym));
13964 ui_out_text (uiout, " at ");
13965 }
05cba821
JK
13966 ui_out_field_string (uiout, "file",
13967 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
13968 ui_out_text (uiout, ":");
13969
13970 if (ui_out_is_mi_like_p (uiout))
13971 {
0b0865da 13972 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 13973
f35a17b5 13974 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
13975 }
13976
80e1d417 13977 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
13978 ui_out_text (uiout, "\n");
13979
80e1d417 13980 b->loc->line_number = sal2.line;
2f202fde 13981 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b 13982
f00aae0f 13983 delete_event_location (b->location);
67994074
KS
13984 initialize_explicit_location (&explicit_loc);
13985 explicit_loc.source_filename
00e52e53 13986 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
67994074
KS
13987 explicit_loc.line_offset.offset = b->loc->line_number;
13988 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
13989 b->location = new_explicit_location (&explicit_loc);
0fb4aa4b
PA
13990
13991 /* Might be nice to check if function changed, and warn if
13992 so. */
13993
80e1d417 13994 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13995 }
13996 }
13997 return sal;
13998}
13999
8d3788bd
VP
14000/* Returns 1 iff locations A and B are sufficiently same that
14001 we don't need to report breakpoint as changed. */
14002
14003static int
14004locations_are_equal (struct bp_location *a, struct bp_location *b)
14005{
14006 while (a && b)
14007 {
14008 if (a->address != b->address)
14009 return 0;
14010
14011 if (a->shlib_disabled != b->shlib_disabled)
14012 return 0;
14013
14014 if (a->enabled != b->enabled)
14015 return 0;
14016
14017 a = a->next;
14018 b = b->next;
14019 }
14020
14021 if ((a == NULL) != (b == NULL))
14022 return 0;
14023
14024 return 1;
14025}
14026
f1310107
TJB
14027/* Create new breakpoint locations for B (a hardware or software breakpoint)
14028 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14029 a ranged breakpoint. */
14030
0e30163f 14031void
0d381245 14032update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14033 struct symtabs_and_lines sals,
14034 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14035{
14036 int i;
0d381245
VP
14037 struct bp_location *existing_locations = b->loc;
14038
f8eba3c6
TT
14039 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14040 {
14041 /* Ranged breakpoints have only one start location and one end
14042 location. */
14043 b->enable_state = bp_disabled;
44702360 14044 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
14045 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14046 "multiple locations found\n"),
14047 b->number);
14048 return;
14049 }
f1310107 14050
4a64f543
MS
14051 /* If there's no new locations, and all existing locations are
14052 pending, don't do anything. This optimizes the common case where
14053 all locations are in the same shared library, that was unloaded.
14054 We'd like to retain the location, so that when the library is
14055 loaded again, we don't loose the enabled/disabled status of the
14056 individual locations. */
0d381245 14057 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14058 return;
14059
fe3f5fa8
VP
14060 b->loc = NULL;
14061
0d381245 14062 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14063 {
f8eba3c6
TT
14064 struct bp_location *new_loc;
14065
14066 switch_to_program_space_and_thread (sals.sals[i].pspace);
14067
14068 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14069
0d381245
VP
14070 /* Reparse conditions, they might contain references to the
14071 old symtab. */
14072 if (b->cond_string != NULL)
14073 {
bbc13ae3 14074 const char *s;
fe3f5fa8 14075
0d381245 14076 s = b->cond_string;
492d29ea 14077 TRY
0d381245 14078 {
1bb9788d
TT
14079 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14080 block_for_pc (sals.sals[i].pc),
0d381245
VP
14081 0);
14082 }
492d29ea 14083 CATCH (e, RETURN_MASK_ERROR)
0d381245 14084 {
3e43a32a
MS
14085 warning (_("failed to reevaluate condition "
14086 "for breakpoint %d: %s"),
0d381245
VP
14087 b->number, e.message);
14088 new_loc->enabled = 0;
14089 }
492d29ea 14090 END_CATCH
0d381245 14091 }
fe3f5fa8 14092
f1310107
TJB
14093 if (sals_end.nelts)
14094 {
14095 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14096
14097 new_loc->length = end - sals.sals[0].pc + 1;
14098 }
0d381245 14099 }
fe3f5fa8 14100
4a64f543
MS
14101 /* If possible, carry over 'disable' status from existing
14102 breakpoints. */
0d381245
VP
14103 {
14104 struct bp_location *e = existing_locations;
776592bf
DE
14105 /* If there are multiple breakpoints with the same function name,
14106 e.g. for inline functions, comparing function names won't work.
14107 Instead compare pc addresses; this is just a heuristic as things
14108 may have moved, but in practice it gives the correct answer
14109 often enough until a better solution is found. */
14110 int have_ambiguous_names = ambiguous_names_p (b->loc);
14111
0d381245
VP
14112 for (; e; e = e->next)
14113 {
14114 if (!e->enabled && e->function_name)
14115 {
14116 struct bp_location *l = b->loc;
776592bf
DE
14117 if (have_ambiguous_names)
14118 {
14119 for (; l; l = l->next)
f1310107 14120 if (breakpoint_locations_match (e, l))
776592bf
DE
14121 {
14122 l->enabled = 0;
14123 break;
14124 }
14125 }
14126 else
14127 {
14128 for (; l; l = l->next)
14129 if (l->function_name
14130 && strcmp (e->function_name, l->function_name) == 0)
14131 {
14132 l->enabled = 0;
14133 break;
14134 }
14135 }
0d381245
VP
14136 }
14137 }
14138 }
fe3f5fa8 14139
8d3788bd
VP
14140 if (!locations_are_equal (existing_locations, b->loc))
14141 observer_notify_breakpoint_modified (b);
14142
44702360 14143 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14144}
14145
f00aae0f 14146/* Find the SaL locations corresponding to the given LOCATION.
ef23e705
TJB
14147 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14148
14149static struct symtabs_and_lines
f00aae0f
KS
14150location_to_sals (struct breakpoint *b, struct event_location *location,
14151 int *found)
ef23e705 14152{
02d20e4a 14153 struct symtabs_and_lines sals = {0};
492d29ea 14154 struct gdb_exception exception = exception_none;
ef23e705 14155
983af33b 14156 gdb_assert (b->ops != NULL);
ef23e705 14157
492d29ea 14158 TRY
ef23e705 14159 {
f00aae0f 14160 b->ops->decode_location (b, location, &sals);
ef23e705 14161 }
492d29ea 14162 CATCH (e, RETURN_MASK_ERROR)
ef23e705
TJB
14163 {
14164 int not_found_and_ok = 0;
492d29ea
PA
14165
14166 exception = e;
14167
ef23e705
TJB
14168 /* For pending breakpoints, it's expected that parsing will
14169 fail until the right shared library is loaded. User has
14170 already told to create pending breakpoints and don't need
14171 extra messages. If breakpoint is in bp_shlib_disabled
14172 state, then user already saw the message about that
14173 breakpoint being disabled, and don't want to see more
14174 errors. */
58438ac1 14175 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14176 && (b->condition_not_parsed
14177 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14178 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14179 || b->enable_state == bp_disabled))
14180 not_found_and_ok = 1;
14181
14182 if (!not_found_and_ok)
14183 {
14184 /* We surely don't want to warn about the same breakpoint
14185 10 times. One solution, implemented here, is disable
14186 the breakpoint on error. Another solution would be to
14187 have separate 'warning emitted' flag. Since this
14188 happens only when a binary has changed, I don't know
14189 which approach is better. */
14190 b->enable_state = bp_disabled;
14191 throw_exception (e);
14192 }
14193 }
492d29ea 14194 END_CATCH
ef23e705 14195
492d29ea 14196 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
ef23e705 14197 {
f8eba3c6 14198 int i;
ef23e705 14199
f8eba3c6
TT
14200 for (i = 0; i < sals.nelts; ++i)
14201 resolve_sal_pc (&sals.sals[i]);
f00aae0f 14202 if (b->condition_not_parsed && b->extra_string != NULL)
ef23e705 14203 {
ed1d1739
KS
14204 char *cond_string, *extra_string;
14205 int thread, task;
ef23e705 14206
f00aae0f 14207 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
e7e0cddf
SS
14208 &cond_string, &thread, &task,
14209 &extra_string);
f00aae0f 14210 gdb_assert (b->cond_string == NULL);
ef23e705
TJB
14211 if (cond_string)
14212 b->cond_string = cond_string;
14213 b->thread = thread;
14214 b->task = task;
e7e0cddf 14215 if (extra_string)
f00aae0f
KS
14216 {
14217 xfree (b->extra_string);
14218 b->extra_string = extra_string;
14219 }
ef23e705
TJB
14220 b->condition_not_parsed = 0;
14221 }
14222
983af33b 14223 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14224 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14225
58438ac1
TT
14226 *found = 1;
14227 }
14228 else
14229 *found = 0;
ef23e705
TJB
14230
14231 return sals;
14232}
14233
348d480f
PA
14234/* The default re_set method, for typical hardware or software
14235 breakpoints. Reevaluate the breakpoint and recreate its
14236 locations. */
14237
14238static void
28010a5d 14239breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14240{
14241 int found;
f1310107 14242 struct symtabs_and_lines sals, sals_end;
ef23e705 14243 struct symtabs_and_lines expanded = {0};
f1310107 14244 struct symtabs_and_lines expanded_end = {0};
ef23e705 14245
f00aae0f 14246 sals = location_to_sals (b, b->location, &found);
ef23e705
TJB
14247 if (found)
14248 {
14249 make_cleanup (xfree, sals.sals);
f8eba3c6 14250 expanded = sals;
ef23e705
TJB
14251 }
14252
f00aae0f 14253 if (b->location_range_end != NULL)
f1310107 14254 {
f00aae0f 14255 sals_end = location_to_sals (b, b->location_range_end, &found);
f1310107
TJB
14256 if (found)
14257 {
14258 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14259 expanded_end = sals_end;
f1310107
TJB
14260 }
14261 }
14262
14263 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14264}
14265
983af33b
SDJ
14266/* Default method for creating SALs from an address string. It basically
14267 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14268
14269static void
f00aae0f
KS
14270create_sals_from_location_default (const struct event_location *location,
14271 struct linespec_result *canonical,
14272 enum bptype type_wanted)
983af33b 14273{
f00aae0f 14274 parse_breakpoint_sals (location, canonical);
983af33b
SDJ
14275}
14276
14277/* Call create_breakpoints_sal for the given arguments. This is the default
14278 function for the `create_breakpoints_sal' method of
14279 breakpoint_ops. */
14280
14281static void
14282create_breakpoints_sal_default (struct gdbarch *gdbarch,
14283 struct linespec_result *canonical,
983af33b 14284 char *cond_string,
e7e0cddf 14285 char *extra_string,
983af33b
SDJ
14286 enum bptype type_wanted,
14287 enum bpdisp disposition,
14288 int thread,
14289 int task, int ignore_count,
14290 const struct breakpoint_ops *ops,
14291 int from_tty, int enabled,
44f238bb 14292 int internal, unsigned flags)
983af33b
SDJ
14293{
14294 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14295 extra_string,
983af33b
SDJ
14296 type_wanted, disposition,
14297 thread, task, ignore_count, ops, from_tty,
44f238bb 14298 enabled, internal, flags);
983af33b
SDJ
14299}
14300
14301/* Decode the line represented by S by calling decode_line_full. This is the
5f700d83 14302 default function for the `decode_location' method of breakpoint_ops. */
983af33b
SDJ
14303
14304static void
f00aae0f
KS
14305decode_location_default (struct breakpoint *b,
14306 const struct event_location *location,
983af33b
SDJ
14307 struct symtabs_and_lines *sals)
14308{
14309 struct linespec_result canonical;
14310
14311 init_linespec_result (&canonical);
f00aae0f 14312 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
983af33b
SDJ
14313 (struct symtab *) NULL, 0,
14314 &canonical, multiple_symbols_all,
14315 b->filter);
14316
14317 /* We should get 0 or 1 resulting SALs. */
14318 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14319
14320 if (VEC_length (linespec_sals, canonical.sals) > 0)
14321 {
14322 struct linespec_sals *lsal;
14323
14324 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14325 *sals = lsal->sals;
14326 /* Arrange it so the destructor does not free the
14327 contents. */
14328 lsal->sals.sals = NULL;
14329 }
14330
14331 destroy_linespec_result (&canonical);
14332}
14333
28010a5d
PA
14334/* Prepare the global context for a re-set of breakpoint B. */
14335
14336static struct cleanup *
14337prepare_re_set_context (struct breakpoint *b)
14338{
14339 struct cleanup *cleanups;
14340
14341 input_radix = b->input_radix;
14342 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14343 if (b->pspace != NULL)
14344 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14345 set_language (b->language);
14346
14347 return cleanups;
ef23e705
TJB
14348}
14349
c906108c
SS
14350/* Reset a breakpoint given it's struct breakpoint * BINT.
14351 The value we return ends up being the return value from catch_errors.
14352 Unused in this case. */
14353
14354static int
4efb68b1 14355breakpoint_re_set_one (void *bint)
c906108c 14356{
4a64f543 14357 /* Get past catch_errs. */
53a5351d 14358 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14359 struct cleanup *cleanups;
c906108c 14360
348d480f
PA
14361 cleanups = prepare_re_set_context (b);
14362 b->ops->re_set (b);
14363 do_cleanups (cleanups);
c906108c
SS
14364 return 0;
14365}
14366
69de3c6a 14367/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14368void
69de3c6a 14369breakpoint_re_set (void)
c906108c 14370{
35df4500 14371 struct breakpoint *b, *b_tmp;
c906108c
SS
14372 enum language save_language;
14373 int save_input_radix;
6c95b8df 14374 struct cleanup *old_chain;
c5aa993b 14375
c906108c
SS
14376 save_language = current_language->la_language;
14377 save_input_radix = input_radix;
6c95b8df
PA
14378 old_chain = save_current_program_space ();
14379
35df4500 14380 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14381 {
4a64f543 14382 /* Format possible error msg. */
fe3f5fa8 14383 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14384 b->number);
14385 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14386 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14387 do_cleanups (cleanups);
c5aa993b 14388 }
c906108c
SS
14389 set_language (save_language);
14390 input_radix = save_input_radix;
e62c965a 14391
0756c555 14392 jit_breakpoint_re_set ();
4efc6507 14393
6c95b8df
PA
14394 do_cleanups (old_chain);
14395
af02033e
PP
14396 create_overlay_event_breakpoint ();
14397 create_longjmp_master_breakpoint ();
14398 create_std_terminate_master_breakpoint ();
186c406b 14399 create_exception_master_breakpoint ();
c906108c
SS
14400}
14401\f
c906108c
SS
14402/* Reset the thread number of this breakpoint:
14403
14404 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14405 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14406void
fba45db2 14407breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14408{
14409 if (b->thread != -1)
14410 {
39f77062
KB
14411 if (in_thread_list (inferior_ptid))
14412 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14413
14414 /* We're being called after following a fork. The new fork is
14415 selected as current, and unless this was a vfork will have a
14416 different program space from the original thread. Reset that
14417 as well. */
14418 b->loc->pspace = current_program_space;
c906108c
SS
14419 }
14420}
14421
03ac34d5
MS
14422/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14423 If from_tty is nonzero, it prints a message to that effect,
14424 which ends with a period (no newline). */
14425
c906108c 14426void
fba45db2 14427set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14428{
52f0bd74 14429 struct breakpoint *b;
c906108c
SS
14430
14431 if (count < 0)
14432 count = 0;
14433
14434 ALL_BREAKPOINTS (b)
14435 if (b->number == bptnum)
c5aa993b 14436 {
d77f58be
SS
14437 if (is_tracepoint (b))
14438 {
14439 if (from_tty && count != 0)
14440 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14441 bptnum);
14442 return;
14443 }
14444
c5aa993b 14445 b->ignore_count = count;
221ea385
KS
14446 if (from_tty)
14447 {
14448 if (count == 0)
3e43a32a
MS
14449 printf_filtered (_("Will stop next time "
14450 "breakpoint %d is reached."),
221ea385
KS
14451 bptnum);
14452 else if (count == 1)
a3f17187 14453 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14454 bptnum);
14455 else
3e43a32a
MS
14456 printf_filtered (_("Will ignore next %d "
14457 "crossings of breakpoint %d."),
221ea385
KS
14458 count, bptnum);
14459 }
8d3788bd 14460 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14461 return;
14462 }
c906108c 14463
8a3fe4f8 14464 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14465}
14466
c906108c
SS
14467/* Command to set ignore-count of breakpoint N to COUNT. */
14468
14469static void
fba45db2 14470ignore_command (char *args, int from_tty)
c906108c
SS
14471{
14472 char *p = args;
52f0bd74 14473 int num;
c906108c
SS
14474
14475 if (p == 0)
e2e0b3e5 14476 error_no_arg (_("a breakpoint number"));
c5aa993b 14477
c906108c 14478 num = get_number (&p);
5c44784c 14479 if (num == 0)
8a3fe4f8 14480 error (_("bad breakpoint number: '%s'"), args);
c906108c 14481 if (*p == 0)
8a3fe4f8 14482 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14483
14484 set_ignore_count (num,
14485 longest_to_int (value_as_long (parse_and_eval (p))),
14486 from_tty);
221ea385
KS
14487 if (from_tty)
14488 printf_filtered ("\n");
c906108c
SS
14489}
14490\f
14491/* Call FUNCTION on each of the breakpoints
14492 whose numbers are given in ARGS. */
14493
14494static void
95a42b64
TT
14495map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14496 void *),
14497 void *data)
c906108c 14498{
52f0bd74
AC
14499 int num;
14500 struct breakpoint *b, *tmp;
11cf8741 14501 int match;
197f0a60 14502 struct get_number_or_range_state state;
c906108c 14503
b9d61307 14504 if (args == 0 || *args == '\0')
e2e0b3e5 14505 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14506
197f0a60
TT
14507 init_number_or_range (&state, args);
14508
14509 while (!state.finished)
c906108c 14510 {
e799154c 14511 const char *p = state.string;
197f0a60 14512
11cf8741 14513 match = 0;
c5aa993b 14514
197f0a60 14515 num = get_number_or_range (&state);
5c44784c 14516 if (num == 0)
c5aa993b 14517 {
8a3fe4f8 14518 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14519 }
14520 else
14521 {
14522 ALL_BREAKPOINTS_SAFE (b, tmp)
14523 if (b->number == num)
14524 {
11cf8741 14525 match = 1;
cdac0397 14526 function (b, data);
11cf8741 14527 break;
5c44784c 14528 }
11cf8741 14529 if (match == 0)
a3f17187 14530 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14531 }
c906108c
SS
14532 }
14533}
14534
0d381245
VP
14535static struct bp_location *
14536find_location_by_number (char *number)
14537{
14538 char *dot = strchr (number, '.');
14539 char *p1;
14540 int bp_num;
14541 int loc_num;
14542 struct breakpoint *b;
14543 struct bp_location *loc;
14544
14545 *dot = '\0';
14546
14547 p1 = number;
197f0a60 14548 bp_num = get_number (&p1);
0d381245
VP
14549 if (bp_num == 0)
14550 error (_("Bad breakpoint number '%s'"), number);
14551
14552 ALL_BREAKPOINTS (b)
14553 if (b->number == bp_num)
14554 {
14555 break;
14556 }
14557
14558 if (!b || b->number != bp_num)
14559 error (_("Bad breakpoint number '%s'"), number);
14560
14561 p1 = dot+1;
197f0a60 14562 loc_num = get_number (&p1);
0d381245
VP
14563 if (loc_num == 0)
14564 error (_("Bad breakpoint location number '%s'"), number);
14565
14566 --loc_num;
14567 loc = b->loc;
14568 for (;loc_num && loc; --loc_num, loc = loc->next)
14569 ;
14570 if (!loc)
14571 error (_("Bad breakpoint location number '%s'"), dot+1);
14572
14573 return loc;
14574}
14575
14576
1900040c
MS
14577/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14578 If from_tty is nonzero, it prints a message to that effect,
14579 which ends with a period (no newline). */
14580
c906108c 14581void
fba45db2 14582disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14583{
14584 /* Never disable a watchpoint scope breakpoint; we want to
14585 hit them when we leave scope so we can delete both the
14586 watchpoint and its scope breakpoint at that time. */
14587 if (bpt->type == bp_watchpoint_scope)
14588 return;
14589
b5de0fa7 14590 bpt->enable_state = bp_disabled;
c906108c 14591
b775012e
LM
14592 /* Mark breakpoint locations modified. */
14593 mark_breakpoint_modified (bpt);
14594
d248b706
KY
14595 if (target_supports_enable_disable_tracepoint ()
14596 && current_trace_status ()->running && is_tracepoint (bpt))
14597 {
14598 struct bp_location *location;
14599
14600 for (location = bpt->loc; location; location = location->next)
14601 target_disable_tracepoint (location);
14602 }
14603
44702360 14604 update_global_location_list (UGLL_DONT_INSERT);
c906108c 14605
8d3788bd 14606 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14607}
14608
51be5b68
PA
14609/* A callback for iterate_over_related_breakpoints. */
14610
14611static void
14612do_disable_breakpoint (struct breakpoint *b, void *ignore)
14613{
14614 disable_breakpoint (b);
14615}
14616
95a42b64
TT
14617/* A callback for map_breakpoint_numbers that calls
14618 disable_breakpoint. */
14619
14620static void
14621do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14622{
51be5b68 14623 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14624}
14625
c906108c 14626static void
fba45db2 14627disable_command (char *args, int from_tty)
c906108c 14628{
c906108c 14629 if (args == 0)
46c6471b
PA
14630 {
14631 struct breakpoint *bpt;
14632
14633 ALL_BREAKPOINTS (bpt)
14634 if (user_breakpoint_p (bpt))
14635 disable_breakpoint (bpt);
14636 }
9eaabc75 14637 else
0d381245 14638 {
9eaabc75
MW
14639 char *num = extract_arg (&args);
14640
14641 while (num)
d248b706 14642 {
9eaabc75 14643 if (strchr (num, '.'))
b775012e 14644 {
9eaabc75
MW
14645 struct bp_location *loc = find_location_by_number (num);
14646
14647 if (loc)
14648 {
14649 if (loc->enabled)
14650 {
14651 loc->enabled = 0;
14652 mark_breakpoint_location_modified (loc);
14653 }
14654 if (target_supports_enable_disable_tracepoint ()
14655 && current_trace_status ()->running && loc->owner
14656 && is_tracepoint (loc->owner))
14657 target_disable_tracepoint (loc);
14658 }
44702360 14659 update_global_location_list (UGLL_DONT_INSERT);
b775012e 14660 }
9eaabc75
MW
14661 else
14662 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14663 num = extract_arg (&args);
d248b706 14664 }
0d381245 14665 }
c906108c
SS
14666}
14667
14668static void
816338b5
SS
14669enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14670 int count)
c906108c 14671{
afe38095 14672 int target_resources_ok;
c906108c
SS
14673
14674 if (bpt->type == bp_hardware_breakpoint)
14675 {
14676 int i;
c5aa993b 14677 i = hw_breakpoint_used_count ();
53a5351d 14678 target_resources_ok =
d92524f1 14679 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14680 i + 1, 0);
c906108c 14681 if (target_resources_ok == 0)
8a3fe4f8 14682 error (_("No hardware breakpoint support in the target."));
c906108c 14683 else if (target_resources_ok < 0)
8a3fe4f8 14684 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14685 }
14686
cc60f2e3 14687 if (is_watchpoint (bpt))
c906108c 14688 {
d07205c2 14689 /* Initialize it just to avoid a GCC false warning. */
f486487f 14690 enum enable_state orig_enable_state = bp_disabled;
dde02812 14691
492d29ea 14692 TRY
c906108c 14693 {
3a5c3e22
PA
14694 struct watchpoint *w = (struct watchpoint *) bpt;
14695
1e718ff1
TJB
14696 orig_enable_state = bpt->enable_state;
14697 bpt->enable_state = bp_enabled;
3a5c3e22 14698 update_watchpoint (w, 1 /* reparse */);
c906108c 14699 }
492d29ea 14700 CATCH (e, RETURN_MASK_ALL)
c5aa993b 14701 {
1e718ff1 14702 bpt->enable_state = orig_enable_state;
dde02812
ES
14703 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14704 bpt->number);
14705 return;
c5aa993b 14706 }
492d29ea 14707 END_CATCH
c906108c 14708 }
0101ce28 14709
b775012e
LM
14710 bpt->enable_state = bp_enabled;
14711
14712 /* Mark breakpoint locations modified. */
14713 mark_breakpoint_modified (bpt);
14714
d248b706
KY
14715 if (target_supports_enable_disable_tracepoint ()
14716 && current_trace_status ()->running && is_tracepoint (bpt))
14717 {
14718 struct bp_location *location;
14719
14720 for (location = bpt->loc; location; location = location->next)
14721 target_enable_tracepoint (location);
14722 }
14723
b4c291bb 14724 bpt->disposition = disposition;
816338b5 14725 bpt->enable_count = count;
44702360 14726 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 14727
8d3788bd 14728 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14729}
14730
fe3f5fa8 14731
c906108c 14732void
fba45db2 14733enable_breakpoint (struct breakpoint *bpt)
c906108c 14734{
816338b5 14735 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14736}
14737
14738static void
14739do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14740{
14741 enable_breakpoint (bpt);
c906108c
SS
14742}
14743
95a42b64
TT
14744/* A callback for map_breakpoint_numbers that calls
14745 enable_breakpoint. */
14746
14747static void
14748do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14749{
51be5b68 14750 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14751}
14752
c906108c
SS
14753/* The enable command enables the specified breakpoints (or all defined
14754 breakpoints) so they once again become (or continue to be) effective
1272ad14 14755 in stopping the inferior. */
c906108c 14756
c906108c 14757static void
fba45db2 14758enable_command (char *args, int from_tty)
c906108c 14759{
c906108c 14760 if (args == 0)
46c6471b
PA
14761 {
14762 struct breakpoint *bpt;
14763
14764 ALL_BREAKPOINTS (bpt)
14765 if (user_breakpoint_p (bpt))
14766 enable_breakpoint (bpt);
14767 }
9eaabc75 14768 else
0d381245 14769 {
9eaabc75
MW
14770 char *num = extract_arg (&args);
14771
14772 while (num)
d248b706 14773 {
9eaabc75 14774 if (strchr (num, '.'))
b775012e 14775 {
9eaabc75
MW
14776 struct bp_location *loc = find_location_by_number (num);
14777
14778 if (loc)
14779 {
14780 if (!loc->enabled)
14781 {
14782 loc->enabled = 1;
14783 mark_breakpoint_location_modified (loc);
14784 }
14785 if (target_supports_enable_disable_tracepoint ()
14786 && current_trace_status ()->running && loc->owner
14787 && is_tracepoint (loc->owner))
14788 target_enable_tracepoint (loc);
14789 }
44702360 14790 update_global_location_list (UGLL_MAY_INSERT);
b775012e 14791 }
9eaabc75
MW
14792 else
14793 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14794 num = extract_arg (&args);
d248b706 14795 }
0d381245 14796 }
c906108c
SS
14797}
14798
816338b5
SS
14799/* This struct packages up disposition data for application to multiple
14800 breakpoints. */
14801
14802struct disp_data
14803{
14804 enum bpdisp disp;
14805 int count;
14806};
14807
c906108c 14808static void
51be5b68
PA
14809do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14810{
816338b5 14811 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14812
816338b5 14813 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14814}
14815
14816static void
14817do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14818{
816338b5 14819 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14820
14821 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14822}
14823
c906108c 14824static void
fba45db2 14825enable_once_command (char *args, int from_tty)
c906108c 14826{
51be5b68 14827 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14828}
14829
816338b5
SS
14830static void
14831do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14832{
14833 struct disp_data disp = { disp_disable, *(int *) countptr };
14834
14835 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14836}
14837
14838static void
14839enable_count_command (char *args, int from_tty)
14840{
b9d61307
SM
14841 int count;
14842
14843 if (args == NULL)
14844 error_no_arg (_("hit count"));
14845
14846 count = get_number (&args);
816338b5
SS
14847
14848 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14849}
14850
c906108c 14851static void
51be5b68 14852do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14853{
816338b5 14854 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14855
14856 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14857}
14858
c906108c 14859static void
fba45db2 14860enable_delete_command (char *args, int from_tty)
c906108c 14861{
51be5b68 14862 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14863}
14864\f
fa8d40ab
JJ
14865static void
14866set_breakpoint_cmd (char *args, int from_tty)
14867{
14868}
14869
14870static void
14871show_breakpoint_cmd (char *args, int from_tty)
14872{
14873}
14874
1f3b5d1b
PP
14875/* Invalidate last known value of any hardware watchpoint if
14876 the memory which that value represents has been written to by
14877 GDB itself. */
14878
14879static void
8de0566d
YQ
14880invalidate_bp_value_on_memory_change (struct inferior *inferior,
14881 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
14882 const bfd_byte *data)
14883{
14884 struct breakpoint *bp;
14885
14886 ALL_BREAKPOINTS (bp)
14887 if (bp->enable_state == bp_enabled
3a5c3e22 14888 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14889 {
3a5c3e22 14890 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14891
3a5c3e22
PA
14892 if (wp->val_valid && wp->val)
14893 {
14894 struct bp_location *loc;
14895
14896 for (loc = bp->loc; loc != NULL; loc = loc->next)
14897 if (loc->loc_type == bp_loc_hardware_watchpoint
14898 && loc->address + loc->length > addr
14899 && addr + len > loc->address)
14900 {
14901 value_free (wp->val);
14902 wp->val = NULL;
14903 wp->val_valid = 0;
14904 }
14905 }
1f3b5d1b
PP
14906 }
14907}
14908
8181d85f
DJ
14909/* Create and insert a breakpoint for software single step. */
14910
14911void
6c95b8df 14912insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14913 struct address_space *aspace,
14914 CORE_ADDR next_pc)
8181d85f 14915{
7c16b83e
PA
14916 struct thread_info *tp = inferior_thread ();
14917 struct symtab_and_line sal;
14918 CORE_ADDR pc = next_pc;
8181d85f 14919
34b7e8a6
PA
14920 if (tp->control.single_step_breakpoints == NULL)
14921 {
14922 tp->control.single_step_breakpoints
14923 = new_single_step_breakpoint (tp->num, gdbarch);
14924 }
8181d85f 14925
7c16b83e
PA
14926 sal = find_pc_line (pc, 0);
14927 sal.pc = pc;
14928 sal.section = find_pc_overlay (pc);
14929 sal.explicit_pc = 1;
34b7e8a6 14930 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
8181d85f 14931
7c16b83e 14932 update_global_location_list (UGLL_INSERT);
8181d85f
DJ
14933}
14934
34b7e8a6 14935/* See breakpoint.h. */
f02253f1
HZ
14936
14937int
7c16b83e
PA
14938breakpoint_has_location_inserted_here (struct breakpoint *bp,
14939 struct address_space *aspace,
14940 CORE_ADDR pc)
1aafd4da 14941{
7c16b83e 14942 struct bp_location *loc;
1aafd4da 14943
7c16b83e
PA
14944 for (loc = bp->loc; loc != NULL; loc = loc->next)
14945 if (loc->inserted
14946 && breakpoint_location_address_match (loc, aspace, pc))
14947 return 1;
1aafd4da 14948
7c16b83e 14949 return 0;
ef370185
JB
14950}
14951
14952/* Check whether a software single-step breakpoint is inserted at
14953 PC. */
14954
14955int
14956single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14957 CORE_ADDR pc)
14958{
34b7e8a6
PA
14959 struct breakpoint *bpt;
14960
14961 ALL_BREAKPOINTS (bpt)
14962 {
14963 if (bpt->type == bp_single_step
14964 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14965 return 1;
14966 }
14967 return 0;
1aafd4da
UW
14968}
14969
1042e4c0
SS
14970/* Tracepoint-specific operations. */
14971
14972/* Set tracepoint count to NUM. */
14973static void
14974set_tracepoint_count (int num)
14975{
14976 tracepoint_count = num;
4fa62494 14977 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14978}
14979
70221824 14980static void
1042e4c0
SS
14981trace_command (char *arg, int from_tty)
14982{
55aa24fb 14983 struct breakpoint_ops *ops;
f00aae0f
KS
14984 struct event_location *location;
14985 struct cleanup *back_to;
55aa24fb 14986
f00aae0f
KS
14987 location = string_to_event_location (&arg, current_language);
14988 back_to = make_cleanup_delete_event_location (location);
5b56227b
KS
14989 if (location != NULL
14990 && event_location_type (location) == PROBE_LOCATION)
55aa24fb
SDJ
14991 ops = &tracepoint_probe_breakpoint_ops;
14992 else
14993 ops = &tracepoint_breakpoint_ops;
14994
558a9d82 14995 create_breakpoint (get_current_arch (),
f00aae0f
KS
14996 location,
14997 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
14998 0 /* tempflag */,
14999 bp_tracepoint /* type_wanted */,
15000 0 /* Ignore count */,
15001 pending_break_support,
15002 ops,
15003 from_tty,
15004 1 /* enabled */,
15005 0 /* internal */, 0);
f00aae0f 15006 do_cleanups (back_to);
1042e4c0
SS
15007}
15008
70221824 15009static void
7a697b8d
SS
15010ftrace_command (char *arg, int from_tty)
15011{
f00aae0f
KS
15012 struct event_location *location;
15013 struct cleanup *back_to;
15014
15015 location = string_to_event_location (&arg, current_language);
15016 back_to = make_cleanup_delete_event_location (location);
558a9d82 15017 create_breakpoint (get_current_arch (),
f00aae0f
KS
15018 location,
15019 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15020 0 /* tempflag */,
15021 bp_fast_tracepoint /* type_wanted */,
15022 0 /* Ignore count */,
15023 pending_break_support,
15024 &tracepoint_breakpoint_ops,
15025 from_tty,
15026 1 /* enabled */,
15027 0 /* internal */, 0);
f00aae0f 15028 do_cleanups (back_to);
0fb4aa4b
PA
15029}
15030
15031/* strace command implementation. Creates a static tracepoint. */
15032
70221824 15033static void
0fb4aa4b
PA
15034strace_command (char *arg, int from_tty)
15035{
983af33b 15036 struct breakpoint_ops *ops;
f00aae0f
KS
15037 struct event_location *location;
15038 struct cleanup *back_to;
983af33b
SDJ
15039
15040 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15041 or with a normal static tracepoint. */
61012eef 15042 if (arg && startswith (arg, "-m") && isspace (arg[2]))
f00aae0f
KS
15043 {
15044 ops = &strace_marker_breakpoint_ops;
15045 location = new_linespec_location (&arg);
15046 }
983af33b 15047 else
f00aae0f
KS
15048 {
15049 ops = &tracepoint_breakpoint_ops;
15050 location = string_to_event_location (&arg, current_language);
15051 }
983af33b 15052
f00aae0f 15053 back_to = make_cleanup_delete_event_location (location);
558a9d82 15054 create_breakpoint (get_current_arch (),
f00aae0f
KS
15055 location,
15056 NULL, 0, arg, 1 /* parse arg */,
558a9d82
YQ
15057 0 /* tempflag */,
15058 bp_static_tracepoint /* type_wanted */,
15059 0 /* Ignore count */,
15060 pending_break_support,
15061 ops,
15062 from_tty,
15063 1 /* enabled */,
15064 0 /* internal */, 0);
f00aae0f 15065 do_cleanups (back_to);
7a697b8d
SS
15066}
15067
409873ef
SS
15068/* Set up a fake reader function that gets command lines from a linked
15069 list that was acquired during tracepoint uploading. */
15070
15071static struct uploaded_tp *this_utp;
3149d8c1 15072static int next_cmd;
409873ef
SS
15073
15074static char *
15075read_uploaded_action (void)
15076{
15077 char *rslt;
15078
3149d8c1 15079 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15080
3149d8c1 15081 next_cmd++;
409873ef
SS
15082
15083 return rslt;
15084}
15085
00bf0b85
SS
15086/* Given information about a tracepoint as recorded on a target (which
15087 can be either a live system or a trace file), attempt to create an
15088 equivalent GDB tracepoint. This is not a reliable process, since
15089 the target does not necessarily have all the information used when
15090 the tracepoint was originally defined. */
15091
d9b3f62e 15092struct tracepoint *
00bf0b85 15093create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15094{
409873ef 15095 char *addr_str, small_buf[100];
d9b3f62e 15096 struct tracepoint *tp;
f00aae0f
KS
15097 struct event_location *location;
15098 struct cleanup *cleanup;
fd9b8c24 15099
409873ef
SS
15100 if (utp->at_string)
15101 addr_str = utp->at_string;
15102 else
15103 {
15104 /* In the absence of a source location, fall back to raw
15105 address. Since there is no way to confirm that the address
15106 means the same thing as when the trace was started, warn the
15107 user. */
3e43a32a
MS
15108 warning (_("Uploaded tracepoint %d has no "
15109 "source location, using raw address"),
409873ef 15110 utp->number);
8c042590 15111 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15112 addr_str = small_buf;
15113 }
15114
15115 /* There's not much we can do with a sequence of bytecodes. */
15116 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15117 warning (_("Uploaded tracepoint %d condition "
15118 "has no source form, ignoring it"),
409873ef 15119 utp->number);
d5551862 15120
f00aae0f
KS
15121 location = string_to_event_location (&addr_str, current_language);
15122 cleanup = make_cleanup_delete_event_location (location);
8cdf0e15 15123 if (!create_breakpoint (get_current_arch (),
f00aae0f
KS
15124 location,
15125 utp->cond_string, -1, addr_str,
e7e0cddf 15126 0 /* parse cond/thread */,
8cdf0e15 15127 0 /* tempflag */,
0fb4aa4b 15128 utp->type /* type_wanted */,
8cdf0e15
VP
15129 0 /* Ignore count */,
15130 pending_break_support,
348d480f 15131 &tracepoint_breakpoint_ops,
8cdf0e15 15132 0 /* from_tty */,
84f4c1fe 15133 utp->enabled /* enabled */,
44f238bb
PA
15134 0 /* internal */,
15135 CREATE_BREAKPOINT_FLAGS_INSERTED))
f00aae0f
KS
15136 {
15137 do_cleanups (cleanup);
15138 return NULL;
15139 }
15140
15141 do_cleanups (cleanup);
fd9b8c24 15142
409873ef 15143 /* Get the tracepoint we just created. */
fd9b8c24
PA
15144 tp = get_tracepoint (tracepoint_count);
15145 gdb_assert (tp != NULL);
d5551862 15146
00bf0b85
SS
15147 if (utp->pass > 0)
15148 {
8c042590
PM
15149 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15150 tp->base.number);
00bf0b85 15151
409873ef 15152 trace_pass_command (small_buf, 0);
00bf0b85
SS
15153 }
15154
409873ef
SS
15155 /* If we have uploaded versions of the original commands, set up a
15156 special-purpose "reader" function and call the usual command line
15157 reader, then pass the result to the breakpoint command-setting
15158 function. */
3149d8c1 15159 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15160 {
409873ef 15161 struct command_line *cmd_list;
00bf0b85 15162
409873ef 15163 this_utp = utp;
3149d8c1 15164 next_cmd = 0;
d5551862 15165
409873ef
SS
15166 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15167
d9b3f62e 15168 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15169 }
3149d8c1
SS
15170 else if (!VEC_empty (char_ptr, utp->actions)
15171 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15172 warning (_("Uploaded tracepoint %d actions "
15173 "have no source form, ignoring them"),
409873ef 15174 utp->number);
00bf0b85 15175
f196051f
SS
15176 /* Copy any status information that might be available. */
15177 tp->base.hit_count = utp->hit_count;
15178 tp->traceframe_usage = utp->traceframe_usage;
15179
00bf0b85 15180 return tp;
d9b3f62e 15181}
00bf0b85 15182
1042e4c0
SS
15183/* Print information on tracepoint number TPNUM_EXP, or all if
15184 omitted. */
15185
15186static void
e5a67952 15187tracepoints_info (char *args, int from_tty)
1042e4c0 15188{
79a45e25 15189 struct ui_out *uiout = current_uiout;
e5a67952 15190 int num_printed;
1042e4c0 15191
e5a67952 15192 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15193
15194 if (num_printed == 0)
1042e4c0 15195 {
e5a67952 15196 if (args == NULL || *args == '\0')
d77f58be
SS
15197 ui_out_message (uiout, 0, "No tracepoints.\n");
15198 else
e5a67952 15199 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15200 }
ad443146
SS
15201
15202 default_collect_info ();
1042e4c0
SS
15203}
15204
4a64f543 15205/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15206 Not supported by all targets. */
15207static void
15208enable_trace_command (char *args, int from_tty)
15209{
15210 enable_command (args, from_tty);
15211}
15212
4a64f543 15213/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15214 Not supported by all targets. */
15215static void
15216disable_trace_command (char *args, int from_tty)
15217{
15218 disable_command (args, from_tty);
15219}
15220
4a64f543 15221/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15222static void
15223delete_trace_command (char *arg, int from_tty)
15224{
35df4500 15225 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15226
15227 dont_repeat ();
15228
15229 if (arg == 0)
15230 {
15231 int breaks_to_delete = 0;
15232
15233 /* Delete all breakpoints if no argument.
15234 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15235 have to be deleted with an explicit breakpoint number
15236 argument. */
1042e4c0 15237 ALL_TRACEPOINTS (b)
46c6471b 15238 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15239 {
15240 breaks_to_delete = 1;
15241 break;
15242 }
1042e4c0
SS
15243
15244 /* Ask user only if there are some breakpoints to delete. */
15245 if (!from_tty
15246 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15247 {
35df4500 15248 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15249 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15250 delete_breakpoint (b);
1042e4c0
SS
15251 }
15252 }
15253 else
51be5b68 15254 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15255}
15256
197f0a60
TT
15257/* Helper function for trace_pass_command. */
15258
15259static void
d9b3f62e 15260trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15261{
d9b3f62e 15262 tp->pass_count = count;
6f6484cd 15263 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15264 if (from_tty)
15265 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15266 tp->base.number, count);
197f0a60
TT
15267}
15268
1042e4c0
SS
15269/* Set passcount for tracepoint.
15270
15271 First command argument is passcount, second is tracepoint number.
15272 If tracepoint number omitted, apply to most recently defined.
15273 Also accepts special argument "all". */
15274
15275static void
15276trace_pass_command (char *args, int from_tty)
15277{
d9b3f62e 15278 struct tracepoint *t1;
1042e4c0 15279 unsigned int count;
1042e4c0
SS
15280
15281 if (args == 0 || *args == 0)
3e43a32a
MS
15282 error (_("passcount command requires an "
15283 "argument (count + optional TP num)"));
1042e4c0 15284
4a64f543 15285 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15286
529480d0 15287 args = skip_spaces (args);
1042e4c0
SS
15288 if (*args && strncasecmp (args, "all", 3) == 0)
15289 {
d9b3f62e
PA
15290 struct breakpoint *b;
15291
1042e4c0 15292 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15293 if (*args)
15294 error (_("Junk at end of arguments."));
1042e4c0 15295
d9b3f62e 15296 ALL_TRACEPOINTS (b)
197f0a60 15297 {
d9b3f62e 15298 t1 = (struct tracepoint *) b;
197f0a60
TT
15299 trace_pass_set_count (t1, count, from_tty);
15300 }
15301 }
15302 else if (*args == '\0')
1042e4c0 15303 {
5fa1d40e 15304 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15305 if (t1)
197f0a60
TT
15306 trace_pass_set_count (t1, count, from_tty);
15307 }
15308 else
15309 {
15310 struct get_number_or_range_state state;
15311
15312 init_number_or_range (&state, args);
15313 while (!state.finished)
1042e4c0 15314 {
5fa1d40e 15315 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15316 if (t1)
15317 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15318 }
15319 }
1042e4c0
SS
15320}
15321
d9b3f62e 15322struct tracepoint *
1042e4c0
SS
15323get_tracepoint (int num)
15324{
15325 struct breakpoint *t;
15326
15327 ALL_TRACEPOINTS (t)
15328 if (t->number == num)
d9b3f62e 15329 return (struct tracepoint *) t;
1042e4c0
SS
15330
15331 return NULL;
15332}
15333
d5551862
SS
15334/* Find the tracepoint with the given target-side number (which may be
15335 different from the tracepoint number after disconnecting and
15336 reconnecting). */
15337
d9b3f62e 15338struct tracepoint *
d5551862
SS
15339get_tracepoint_by_number_on_target (int num)
15340{
d9b3f62e 15341 struct breakpoint *b;
d5551862 15342
d9b3f62e
PA
15343 ALL_TRACEPOINTS (b)
15344 {
15345 struct tracepoint *t = (struct tracepoint *) b;
15346
15347 if (t->number_on_target == num)
15348 return t;
15349 }
d5551862
SS
15350
15351 return NULL;
15352}
15353
1042e4c0 15354/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15355 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15356 If the argument is missing, the most recent tracepoint
15357 (tracepoint_count) is returned. */
15358
d9b3f62e 15359struct tracepoint *
197f0a60 15360get_tracepoint_by_number (char **arg,
5fa1d40e 15361 struct get_number_or_range_state *state)
1042e4c0 15362{
1042e4c0
SS
15363 struct breakpoint *t;
15364 int tpnum;
15365 char *instring = arg == NULL ? NULL : *arg;
15366
197f0a60
TT
15367 if (state)
15368 {
15369 gdb_assert (!state->finished);
15370 tpnum = get_number_or_range (state);
15371 }
15372 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15373 tpnum = tracepoint_count;
1042e4c0 15374 else
197f0a60 15375 tpnum = get_number (arg);
1042e4c0
SS
15376
15377 if (tpnum <= 0)
15378 {
15379 if (instring && *instring)
15380 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15381 instring);
15382 else
5fa1d40e 15383 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15384 return NULL;
15385 }
15386
15387 ALL_TRACEPOINTS (t)
15388 if (t->number == tpnum)
15389 {
d9b3f62e 15390 return (struct tracepoint *) t;
1042e4c0
SS
15391 }
15392
1042e4c0
SS
15393 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15394 return NULL;
15395}
15396
d9b3f62e
PA
15397void
15398print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15399{
15400 if (b->thread != -1)
15401 fprintf_unfiltered (fp, " thread %d", b->thread);
15402
15403 if (b->task != 0)
15404 fprintf_unfiltered (fp, " task %d", b->task);
15405
15406 fprintf_unfiltered (fp, "\n");
15407}
15408
6149aea9
PA
15409/* Save information on user settable breakpoints (watchpoints, etc) to
15410 a new script file named FILENAME. If FILTER is non-NULL, call it
15411 on each breakpoint and only include the ones for which it returns
15412 non-zero. */
15413
1042e4c0 15414static void
6149aea9
PA
15415save_breakpoints (char *filename, int from_tty,
15416 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15417{
15418 struct breakpoint *tp;
6149aea9 15419 int any = 0;
1042e4c0 15420 struct cleanup *cleanup;
a7bdde9e 15421 struct ui_file *fp;
6149aea9 15422 int extra_trace_bits = 0;
1042e4c0 15423
6149aea9
PA
15424 if (filename == 0 || *filename == 0)
15425 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15426
15427 /* See if we have anything to save. */
6149aea9 15428 ALL_BREAKPOINTS (tp)
1042e4c0 15429 {
6149aea9 15430 /* Skip internal and momentary breakpoints. */
09d682a4 15431 if (!user_breakpoint_p (tp))
6149aea9
PA
15432 continue;
15433
15434 /* If we have a filter, only save the breakpoints it accepts. */
15435 if (filter && !filter (tp))
15436 continue;
15437
15438 any = 1;
15439
15440 if (is_tracepoint (tp))
15441 {
15442 extra_trace_bits = 1;
15443
15444 /* We can stop searching. */
15445 break;
15446 }
1042e4c0 15447 }
6149aea9
PA
15448
15449 if (!any)
1042e4c0 15450 {
6149aea9 15451 warning (_("Nothing to save."));
1042e4c0
SS
15452 return;
15453 }
15454
c718be47
PA
15455 filename = tilde_expand (filename);
15456 cleanup = make_cleanup (xfree, filename);
15457 fp = gdb_fopen (filename, "w");
059fb39f 15458 if (!fp)
6149aea9
PA
15459 error (_("Unable to open file '%s' for saving (%s)"),
15460 filename, safe_strerror (errno));
a7bdde9e 15461 make_cleanup_ui_file_delete (fp);
8bf6485c 15462
6149aea9
PA
15463 if (extra_trace_bits)
15464 save_trace_state_variables (fp);
8bf6485c 15465
6149aea9 15466 ALL_BREAKPOINTS (tp)
1042e4c0 15467 {
6149aea9 15468 /* Skip internal and momentary breakpoints. */
09d682a4 15469 if (!user_breakpoint_p (tp))
6149aea9 15470 continue;
8bf6485c 15471
6149aea9
PA
15472 /* If we have a filter, only save the breakpoints it accepts. */
15473 if (filter && !filter (tp))
15474 continue;
15475
348d480f 15476 tp->ops->print_recreate (tp, fp);
1042e4c0 15477
6149aea9
PA
15478 /* Note, we can't rely on tp->number for anything, as we can't
15479 assume the recreated breakpoint numbers will match. Use $bpnum
15480 instead. */
15481
15482 if (tp->cond_string)
15483 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15484
15485 if (tp->ignore_count)
15486 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15487
2d9442cc 15488 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15489 {
6c63c96a 15490 struct gdb_exception exception;
a7bdde9e 15491
6149aea9 15492 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15493
79a45e25 15494 ui_out_redirect (current_uiout, fp);
492d29ea 15495 TRY
1042e4c0 15496 {
79a45e25 15497 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15498 }
492d29ea
PA
15499 CATCH (ex, RETURN_MASK_ALL)
15500 {
6c63c96a 15501 ui_out_redirect (current_uiout, NULL);
492d29ea
PA
15502 throw_exception (ex);
15503 }
15504 END_CATCH
1042e4c0 15505
6c63c96a 15506 ui_out_redirect (current_uiout, NULL);
a7bdde9e 15507 fprintf_unfiltered (fp, " end\n");
1042e4c0 15508 }
6149aea9
PA
15509
15510 if (tp->enable_state == bp_disabled)
99894e11 15511 fprintf_unfiltered (fp, "disable $bpnum\n");
6149aea9
PA
15512
15513 /* If this is a multi-location breakpoint, check if the locations
15514 should be individually disabled. Watchpoint locations are
15515 special, and not user visible. */
15516 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15517 {
15518 struct bp_location *loc;
15519 int n = 1;
15520
15521 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15522 if (!loc->enabled)
15523 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15524 }
1042e4c0 15525 }
8bf6485c 15526
6149aea9 15527 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15528 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15529
1042e4c0 15530 if (from_tty)
6149aea9 15531 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15532 do_cleanups (cleanup);
6149aea9
PA
15533}
15534
15535/* The `save breakpoints' command. */
15536
15537static void
15538save_breakpoints_command (char *args, int from_tty)
15539{
15540 save_breakpoints (args, from_tty, NULL);
15541}
15542
15543/* The `save tracepoints' command. */
15544
15545static void
15546save_tracepoints_command (char *args, int from_tty)
15547{
15548 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15549}
15550
15551/* Create a vector of all tracepoints. */
15552
15553VEC(breakpoint_p) *
eeae04df 15554all_tracepoints (void)
1042e4c0
SS
15555{
15556 VEC(breakpoint_p) *tp_vec = 0;
15557 struct breakpoint *tp;
15558
15559 ALL_TRACEPOINTS (tp)
15560 {
15561 VEC_safe_push (breakpoint_p, tp_vec, tp);
15562 }
15563
15564 return tp_vec;
15565}
15566
c906108c 15567\f
629500fa
KS
15568/* This help string is used to consolidate all the help string for specifying
15569 locations used by several commands. */
15570
15571#define LOCATION_HELP_STRING \
15572"Linespecs are colon-separated lists of location parameters, such as\n\
15573source filename, function name, label name, and line number.\n\
15574Example: To specify the start of a label named \"the_top\" in the\n\
15575function \"fact\" in the file \"factorial.c\", use\n\
15576\"factorial.c:fact:the_top\".\n\
15577\n\
15578Address locations begin with \"*\" and specify an exact address in the\n\
15579program. Example: To specify the fourth byte past the start function\n\
15580\"main\", use \"*main + 4\".\n\
15581\n\
15582Explicit locations are similar to linespecs but use an option/argument\n\
15583syntax to specify location parameters.\n\
15584Example: To specify the start of the label named \"the_top\" in the\n\
15585function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15586-function fact -label the_top\".\n"
15587
4a64f543
MS
15588/* This help string is used for the break, hbreak, tbreak and thbreak
15589 commands. It is defined as a macro to prevent duplication.
15590 COMMAND should be a string constant containing the name of the
15591 command. */
629500fa 15592
31e2b00f 15593#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15594command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15595PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15596probe point. Accepted values are `-probe' (for a generic, automatically\n\
d4777acb
JM
15597guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15598`-probe-dtrace' (for a DTrace probe).\n\
629500fa
KS
15599LOCATION may be a linespec, address, or explicit location as described\n\
15600below.\n\
15601\n\
dc10affe
PA
15602With no LOCATION, uses current execution address of the selected\n\
15603stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15604\n\
15605THREADNUM is the number from \"info threads\".\n\
15606CONDITION is a boolean expression.\n\
629500fa 15607\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
15608Multiple breakpoints at one place are permitted, and useful if their\n\
15609conditions are different.\n\
31e2b00f
AS
15610\n\
15611Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15612
44feb3ce
TT
15613/* List of subcommands for "catch". */
15614static struct cmd_list_element *catch_cmdlist;
15615
15616/* List of subcommands for "tcatch". */
15617static struct cmd_list_element *tcatch_cmdlist;
15618
9ac4176b 15619void
44feb3ce 15620add_catch_command (char *name, char *docstring,
82ae6c8d 15621 cmd_sfunc_ftype *sfunc,
625e8578 15622 completer_ftype *completer,
44feb3ce
TT
15623 void *user_data_catch,
15624 void *user_data_tcatch)
15625{
15626 struct cmd_list_element *command;
15627
15628 command = add_cmd (name, class_breakpoint, NULL, docstring,
15629 &catch_cmdlist);
15630 set_cmd_sfunc (command, sfunc);
15631 set_cmd_context (command, user_data_catch);
a96d9b2e 15632 set_cmd_completer (command, completer);
44feb3ce
TT
15633
15634 command = add_cmd (name, class_breakpoint, NULL, docstring,
15635 &tcatch_cmdlist);
15636 set_cmd_sfunc (command, sfunc);
15637 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15638 set_cmd_completer (command, completer);
44feb3ce
TT
15639}
15640
6149aea9
PA
15641static void
15642save_command (char *arg, int from_tty)
15643{
3e43a32a
MS
15644 printf_unfiltered (_("\"save\" must be followed by "
15645 "the name of a save subcommand.\n"));
635c7e8a 15646 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
15647}
15648
84f4c1fe
PM
15649struct breakpoint *
15650iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15651 void *data)
15652{
35df4500 15653 struct breakpoint *b, *b_tmp;
84f4c1fe 15654
35df4500 15655 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15656 {
15657 if ((*callback) (b, data))
15658 return b;
15659 }
15660
15661 return NULL;
15662}
15663
0574c78f
GB
15664/* Zero if any of the breakpoint's locations could be a location where
15665 functions have been inlined, nonzero otherwise. */
15666
15667static int
15668is_non_inline_function (struct breakpoint *b)
15669{
15670 /* The shared library event breakpoint is set on the address of a
15671 non-inline function. */
15672 if (b->type == bp_shlib_event)
15673 return 1;
15674
15675 return 0;
15676}
15677
15678/* Nonzero if the specified PC cannot be a location where functions
15679 have been inlined. */
15680
15681int
09ac7c10
TT
15682pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15683 const struct target_waitstatus *ws)
0574c78f
GB
15684{
15685 struct breakpoint *b;
15686 struct bp_location *bl;
15687
15688 ALL_BREAKPOINTS (b)
15689 {
15690 if (!is_non_inline_function (b))
15691 continue;
15692
15693 for (bl = b->loc; bl != NULL; bl = bl->next)
15694 {
15695 if (!bl->shlib_disabled
09ac7c10 15696 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15697 return 1;
15698 }
15699 }
15700
15701 return 0;
15702}
15703
2f202fde
JK
15704/* Remove any references to OBJFILE which is going to be freed. */
15705
15706void
15707breakpoint_free_objfile (struct objfile *objfile)
15708{
15709 struct bp_location **locp, *loc;
15710
15711 ALL_BP_LOCATIONS (loc, locp)
eb822aa6 15712 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
2f202fde
JK
15713 loc->symtab = NULL;
15714}
15715
2060206e
PA
15716void
15717initialize_breakpoint_ops (void)
15718{
15719 static int initialized = 0;
15720
15721 struct breakpoint_ops *ops;
15722
15723 if (initialized)
15724 return;
15725 initialized = 1;
15726
15727 /* The breakpoint_ops structure to be inherit by all kinds of
15728 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15729 internal and momentary breakpoints, etc.). */
15730 ops = &bkpt_base_breakpoint_ops;
15731 *ops = base_breakpoint_ops;
15732 ops->re_set = bkpt_re_set;
15733 ops->insert_location = bkpt_insert_location;
15734 ops->remove_location = bkpt_remove_location;
15735 ops->breakpoint_hit = bkpt_breakpoint_hit;
5f700d83 15736 ops->create_sals_from_location = bkpt_create_sals_from_location;
983af33b 15737 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
5f700d83 15738 ops->decode_location = bkpt_decode_location;
2060206e
PA
15739
15740 /* The breakpoint_ops structure to be used in regular breakpoints. */
15741 ops = &bkpt_breakpoint_ops;
15742 *ops = bkpt_base_breakpoint_ops;
15743 ops->re_set = bkpt_re_set;
15744 ops->resources_needed = bkpt_resources_needed;
15745 ops->print_it = bkpt_print_it;
15746 ops->print_mention = bkpt_print_mention;
15747 ops->print_recreate = bkpt_print_recreate;
15748
15749 /* Ranged breakpoints. */
15750 ops = &ranged_breakpoint_ops;
15751 *ops = bkpt_breakpoint_ops;
15752 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15753 ops->resources_needed = resources_needed_ranged_breakpoint;
15754 ops->print_it = print_it_ranged_breakpoint;
15755 ops->print_one = print_one_ranged_breakpoint;
15756 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15757 ops->print_mention = print_mention_ranged_breakpoint;
15758 ops->print_recreate = print_recreate_ranged_breakpoint;
15759
15760 /* Internal breakpoints. */
15761 ops = &internal_breakpoint_ops;
15762 *ops = bkpt_base_breakpoint_ops;
15763 ops->re_set = internal_bkpt_re_set;
15764 ops->check_status = internal_bkpt_check_status;
15765 ops->print_it = internal_bkpt_print_it;
15766 ops->print_mention = internal_bkpt_print_mention;
15767
15768 /* Momentary breakpoints. */
15769 ops = &momentary_breakpoint_ops;
15770 *ops = bkpt_base_breakpoint_ops;
15771 ops->re_set = momentary_bkpt_re_set;
15772 ops->check_status = momentary_bkpt_check_status;
15773 ops->print_it = momentary_bkpt_print_it;
15774 ops->print_mention = momentary_bkpt_print_mention;
15775
e2e4d78b
JK
15776 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15777 ops = &longjmp_breakpoint_ops;
15778 *ops = momentary_breakpoint_ops;
15779 ops->dtor = longjmp_bkpt_dtor;
15780
55aa24fb
SDJ
15781 /* Probe breakpoints. */
15782 ops = &bkpt_probe_breakpoint_ops;
15783 *ops = bkpt_breakpoint_ops;
15784 ops->insert_location = bkpt_probe_insert_location;
15785 ops->remove_location = bkpt_probe_remove_location;
5f700d83
KS
15786 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15787 ops->decode_location = bkpt_probe_decode_location;
55aa24fb 15788
2060206e
PA
15789 /* Watchpoints. */
15790 ops = &watchpoint_breakpoint_ops;
15791 *ops = base_breakpoint_ops;
3a5c3e22 15792 ops->dtor = dtor_watchpoint;
2060206e
PA
15793 ops->re_set = re_set_watchpoint;
15794 ops->insert_location = insert_watchpoint;
15795 ops->remove_location = remove_watchpoint;
15796 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15797 ops->check_status = check_status_watchpoint;
15798 ops->resources_needed = resources_needed_watchpoint;
15799 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15800 ops->print_it = print_it_watchpoint;
15801 ops->print_mention = print_mention_watchpoint;
15802 ops->print_recreate = print_recreate_watchpoint;
427cd150 15803 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
15804
15805 /* Masked watchpoints. */
15806 ops = &masked_watchpoint_breakpoint_ops;
15807 *ops = watchpoint_breakpoint_ops;
15808 ops->insert_location = insert_masked_watchpoint;
15809 ops->remove_location = remove_masked_watchpoint;
15810 ops->resources_needed = resources_needed_masked_watchpoint;
15811 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15812 ops->print_it = print_it_masked_watchpoint;
15813 ops->print_one_detail = print_one_detail_masked_watchpoint;
15814 ops->print_mention = print_mention_masked_watchpoint;
15815 ops->print_recreate = print_recreate_masked_watchpoint;
15816
15817 /* Tracepoints. */
15818 ops = &tracepoint_breakpoint_ops;
15819 *ops = base_breakpoint_ops;
15820 ops->re_set = tracepoint_re_set;
15821 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15822 ops->print_one_detail = tracepoint_print_one_detail;
15823 ops->print_mention = tracepoint_print_mention;
15824 ops->print_recreate = tracepoint_print_recreate;
5f700d83 15825 ops->create_sals_from_location = tracepoint_create_sals_from_location;
983af33b 15826 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
5f700d83 15827 ops->decode_location = tracepoint_decode_location;
983af33b 15828
55aa24fb
SDJ
15829 /* Probe tracepoints. */
15830 ops = &tracepoint_probe_breakpoint_ops;
15831 *ops = tracepoint_breakpoint_ops;
5f700d83
KS
15832 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15833 ops->decode_location = tracepoint_probe_decode_location;
55aa24fb 15834
983af33b
SDJ
15835 /* Static tracepoints with marker (`-m'). */
15836 ops = &strace_marker_breakpoint_ops;
15837 *ops = tracepoint_breakpoint_ops;
5f700d83 15838 ops->create_sals_from_location = strace_marker_create_sals_from_location;
983af33b 15839 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
5f700d83 15840 ops->decode_location = strace_marker_decode_location;
2060206e
PA
15841
15842 /* Fork catchpoints. */
15843 ops = &catch_fork_breakpoint_ops;
15844 *ops = base_breakpoint_ops;
15845 ops->insert_location = insert_catch_fork;
15846 ops->remove_location = remove_catch_fork;
15847 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15848 ops->print_it = print_it_catch_fork;
15849 ops->print_one = print_one_catch_fork;
15850 ops->print_mention = print_mention_catch_fork;
15851 ops->print_recreate = print_recreate_catch_fork;
15852
15853 /* Vfork catchpoints. */
15854 ops = &catch_vfork_breakpoint_ops;
15855 *ops = base_breakpoint_ops;
15856 ops->insert_location = insert_catch_vfork;
15857 ops->remove_location = remove_catch_vfork;
15858 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15859 ops->print_it = print_it_catch_vfork;
15860 ops->print_one = print_one_catch_vfork;
15861 ops->print_mention = print_mention_catch_vfork;
15862 ops->print_recreate = print_recreate_catch_vfork;
15863
15864 /* Exec catchpoints. */
15865 ops = &catch_exec_breakpoint_ops;
15866 *ops = base_breakpoint_ops;
15867 ops->dtor = dtor_catch_exec;
15868 ops->insert_location = insert_catch_exec;
15869 ops->remove_location = remove_catch_exec;
15870 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15871 ops->print_it = print_it_catch_exec;
15872 ops->print_one = print_one_catch_exec;
15873 ops->print_mention = print_mention_catch_exec;
15874 ops->print_recreate = print_recreate_catch_exec;
15875
edcc5120
TT
15876 /* Solib-related catchpoints. */
15877 ops = &catch_solib_breakpoint_ops;
15878 *ops = base_breakpoint_ops;
15879 ops->dtor = dtor_catch_solib;
15880 ops->insert_location = insert_catch_solib;
15881 ops->remove_location = remove_catch_solib;
15882 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15883 ops->check_status = check_status_catch_solib;
15884 ops->print_it = print_it_catch_solib;
15885 ops->print_one = print_one_catch_solib;
15886 ops->print_mention = print_mention_catch_solib;
15887 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15888
15889 ops = &dprintf_breakpoint_ops;
15890 *ops = bkpt_base_breakpoint_ops;
5c2b4418 15891 ops->re_set = dprintf_re_set;
e7e0cddf
SS
15892 ops->resources_needed = bkpt_resources_needed;
15893 ops->print_it = bkpt_print_it;
15894 ops->print_mention = bkpt_print_mention;
2d9442cc 15895 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 15896 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 15897 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
15898}
15899
8bfd80db
YQ
15900/* Chain containing all defined "enable breakpoint" subcommands. */
15901
15902static struct cmd_list_element *enablebreaklist = NULL;
15903
c906108c 15904void
fba45db2 15905_initialize_breakpoint (void)
c906108c
SS
15906{
15907 struct cmd_list_element *c;
15908
2060206e
PA
15909 initialize_breakpoint_ops ();
15910
84acb35a 15911 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 15912 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
1f3b5d1b 15913 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15914
55aa24fb
SDJ
15915 breakpoint_objfile_key
15916 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15917
c906108c
SS
15918 breakpoint_chain = 0;
15919 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15920 before a breakpoint is set. */
15921 breakpoint_count = 0;
15922
1042e4c0
SS
15923 tracepoint_count = 0;
15924
1bedd215
AC
15925 add_com ("ignore", class_breakpoint, ignore_command, _("\
15926Set ignore-count of breakpoint number N to COUNT.\n\
15927Usage is `ignore N COUNT'."));
c906108c 15928
1bedd215
AC
15929 add_com ("commands", class_breakpoint, commands_command, _("\
15930Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
15931Give breakpoint number as argument after \"commands\".\n\
15932With no argument, the targeted breakpoint is the last one set.\n\
15933The commands themselves follow starting on the next line.\n\
15934Type a line containing \"end\" to indicate the end of them.\n\
15935Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15936then no output is printed when it is hit, except what the commands print."));
c906108c 15937
d55637df 15938 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15939Specify breakpoint number N to break only if COND is true.\n\
c906108c 15940Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15941expression to be evaluated whenever breakpoint N is reached."));
d55637df 15942 set_cmd_completer (c, condition_completer);
c906108c 15943
1bedd215 15944 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15945Set a temporary breakpoint.\n\
c906108c
SS
15946Like \"break\" except the breakpoint is only temporary,\n\
15947so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15948by using \"enable delete\" on the breakpoint number.\n\
15949\n"
15950BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15951 set_cmd_completer (c, location_completer);
c94fdfd0 15952
1bedd215 15953 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15954Set a hardware assisted breakpoint.\n\
c906108c 15955Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15956some target hardware may not have this support.\n\
15957\n"
15958BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15959 set_cmd_completer (c, location_completer);
c906108c 15960
1bedd215 15961 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15962Set a temporary hardware assisted breakpoint.\n\
c906108c 15963Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15964so it will be deleted when hit.\n\
15965\n"
15966BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15967 set_cmd_completer (c, location_completer);
c906108c 15968
1bedd215
AC
15969 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15970Enable some breakpoints.\n\
c906108c
SS
15971Give breakpoint numbers (separated by spaces) as arguments.\n\
15972With no subcommand, breakpoints are enabled until you command otherwise.\n\
15973This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15974With a subcommand you can enable temporarily."),
c906108c 15975 &enablelist, "enable ", 1, &cmdlist);
c906108c
SS
15976
15977 add_com_alias ("en", "enable", class_breakpoint, 1);
15978
84951ab5 15979 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15980Enable some breakpoints.\n\
c906108c
SS
15981Give breakpoint numbers (separated by spaces) as arguments.\n\
15982This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15983May be abbreviated to simply \"enable\".\n"),
c5aa993b 15984 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15985
1a966eab
AC
15986 add_cmd ("once", no_class, enable_once_command, _("\
15987Enable breakpoints for one hit. Give breakpoint numbers.\n\
15988If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15989 &enablebreaklist);
15990
1a966eab
AC
15991 add_cmd ("delete", no_class, enable_delete_command, _("\
15992Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15993If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15994 &enablebreaklist);
15995
816338b5
SS
15996 add_cmd ("count", no_class, enable_count_command, _("\
15997Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15998If a breakpoint is hit while enabled in this fashion,\n\
15999the count is decremented; when it reaches zero, the breakpoint is disabled."),
16000 &enablebreaklist);
16001
1a966eab
AC
16002 add_cmd ("delete", no_class, enable_delete_command, _("\
16003Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16004If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16005 &enablelist);
16006
1a966eab
AC
16007 add_cmd ("once", no_class, enable_once_command, _("\
16008Enable breakpoints for one hit. Give breakpoint numbers.\n\
16009If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16010 &enablelist);
16011
16012 add_cmd ("count", no_class, enable_count_command, _("\
16013Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16014If a breakpoint is hit while enabled in this fashion,\n\
16015the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16016 &enablelist);
16017
1bedd215
AC
16018 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16019Disable some breakpoints.\n\
c906108c
SS
16020Arguments are breakpoint numbers with spaces in between.\n\
16021To disable all breakpoints, give no argument.\n\
64b9b334 16022A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16023 &disablelist, "disable ", 1, &cmdlist);
16024 add_com_alias ("dis", "disable", class_breakpoint, 1);
16025 add_com_alias ("disa", "disable", class_breakpoint, 1);
c906108c 16026
1a966eab
AC
16027 add_cmd ("breakpoints", class_alias, disable_command, _("\
16028Disable some breakpoints.\n\
c906108c
SS
16029Arguments are breakpoint numbers with spaces in between.\n\
16030To disable all breakpoints, give no argument.\n\
64b9b334 16031A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16032This command may be abbreviated \"disable\"."),
c906108c
SS
16033 &disablelist);
16034
1bedd215
AC
16035 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16036Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16037Arguments are breakpoint numbers with spaces in between.\n\
16038To delete all breakpoints, give no argument.\n\
16039\n\
16040Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16041The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16042 &deletelist, "delete ", 1, &cmdlist);
16043 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16044 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16045
1a966eab
AC
16046 add_cmd ("breakpoints", class_alias, delete_command, _("\
16047Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16048Arguments are breakpoint numbers with spaces in between.\n\
16049To delete all breakpoints, give no argument.\n\
1a966eab 16050This command may be abbreviated \"delete\"."),
c906108c
SS
16051 &deletelist);
16052
1bedd215 16053 add_com ("clear", class_breakpoint, clear_command, _("\
629500fa
KS
16054Clear breakpoint at specified location.\n\
16055Argument may be a linespec, explicit, or address location as described below.\n\
1bedd215
AC
16056\n\
16057With no argument, clears all breakpoints in the line that the selected frame\n\
629500fa
KS
16058is executing in.\n"
16059"\n" LOCATION_HELP_STRING "\n\
1bedd215 16060See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16061 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16062
1bedd215 16063 c = add_com ("break", class_breakpoint, break_command, _("\
629500fa 16064Set breakpoint at specified location.\n"
31e2b00f 16065BREAK_ARGS_HELP ("break")));
5ba2abeb 16066 set_cmd_completer (c, location_completer);
c94fdfd0 16067
c906108c
SS
16068 add_com_alias ("b", "break", class_run, 1);
16069 add_com_alias ("br", "break", class_run, 1);
16070 add_com_alias ("bre", "break", class_run, 1);
16071 add_com_alias ("brea", "break", class_run, 1);
16072
c906108c
SS
16073 if (dbx_commands)
16074 {
1bedd215
AC
16075 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16076Break in function/address or break at a line in the current file."),
c5aa993b
JM
16077 &stoplist, "stop ", 1, &cmdlist);
16078 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16079 _("Break in function or address."), &stoplist);
c5aa993b 16080 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16081 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16082 add_com ("status", class_info, breakpoints_info, _("\
16083Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16084The \"Type\" column indicates one of:\n\
16085\tbreakpoint - normal breakpoint\n\
16086\twatchpoint - watchpoint\n\
16087The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16088the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16089breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16090address and file/line number respectively.\n\
16091\n\
16092Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16093are set to the address of the last breakpoint listed unless the command\n\
16094is prefixed with \"server \".\n\n\
c906108c 16095Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16096breakpoint set."));
c906108c
SS
16097 }
16098
1bedd215 16099 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16100Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16101The \"Type\" column indicates one of:\n\
16102\tbreakpoint - normal breakpoint\n\
16103\twatchpoint - watchpoint\n\
16104The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16105the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16106breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16107address and file/line number respectively.\n\
16108\n\
16109Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16110are set to the address of the last breakpoint listed unless the command\n\
16111is prefixed with \"server \".\n\n\
c906108c 16112Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16113breakpoint set."));
c906108c 16114
6b04bdb7
MS
16115 add_info_alias ("b", "breakpoints", 1);
16116
1a966eab
AC
16117 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16118Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16119The \"Type\" column indicates one of:\n\
16120\tbreakpoint - normal breakpoint\n\
16121\twatchpoint - watchpoint\n\
16122\tlongjmp - internal breakpoint used to step through longjmp()\n\
16123\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16124\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16125\tfinish - internal breakpoint used by the \"finish\" command\n\
16126The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16127the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16128breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16129address and file/line number respectively.\n\
16130\n\
16131Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16132are set to the address of the last breakpoint listed unless the command\n\
16133is prefixed with \"server \".\n\n\
c906108c 16134Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16135breakpoint set."),
c906108c
SS
16136 &maintenanceinfolist);
16137
44feb3ce
TT
16138 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16139Set catchpoints to catch events."),
16140 &catch_cmdlist, "catch ",
16141 0/*allow-unknown*/, &cmdlist);
16142
16143 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16144Set temporary catchpoints to catch events."),
16145 &tcatch_cmdlist, "tcatch ",
16146 0/*allow-unknown*/, &cmdlist);
16147
44feb3ce
TT
16148 add_catch_command ("fork", _("Catch calls to fork."),
16149 catch_fork_command_1,
a96d9b2e 16150 NULL,
44feb3ce
TT
16151 (void *) (uintptr_t) catch_fork_permanent,
16152 (void *) (uintptr_t) catch_fork_temporary);
16153 add_catch_command ("vfork", _("Catch calls to vfork."),
16154 catch_fork_command_1,
a96d9b2e 16155 NULL,
44feb3ce
TT
16156 (void *) (uintptr_t) catch_vfork_permanent,
16157 (void *) (uintptr_t) catch_vfork_temporary);
16158 add_catch_command ("exec", _("Catch calls to exec."),
16159 catch_exec_command_1,
a96d9b2e
SDJ
16160 NULL,
16161 CATCH_PERMANENT,
16162 CATCH_TEMPORARY);
edcc5120
TT
16163 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16164Usage: catch load [REGEX]\n\
16165If REGEX is given, only stop for libraries matching the regular expression."),
16166 catch_load_command_1,
16167 NULL,
16168 CATCH_PERMANENT,
16169 CATCH_TEMPORARY);
16170 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16171Usage: catch unload [REGEX]\n\
16172If REGEX is given, only stop for libraries matching the regular expression."),
16173 catch_unload_command_1,
16174 NULL,
16175 CATCH_PERMANENT,
16176 CATCH_TEMPORARY);
c5aa993b 16177
1bedd215
AC
16178 c = add_com ("watch", class_breakpoint, watch_command, _("\
16179Set a watchpoint for an expression.\n\
06a64a0b 16180Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16181A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16182an expression changes.\n\
16183If -l or -location is given, this evaluates EXPRESSION and watches\n\
16184the memory to which it refers."));
65d12d83 16185 set_cmd_completer (c, expression_completer);
c906108c 16186
1bedd215
AC
16187 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16188Set a read watchpoint for an expression.\n\
06a64a0b 16189Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16190A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16191an expression is read.\n\
16192If -l or -location is given, this evaluates EXPRESSION and watches\n\
16193the memory to which it refers."));
65d12d83 16194 set_cmd_completer (c, expression_completer);
c906108c 16195
1bedd215
AC
16196 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16197Set a watchpoint for an expression.\n\
06a64a0b 16198Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16199A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16200an expression is either read or written.\n\
16201If -l or -location is given, this evaluates EXPRESSION and watches\n\
16202the memory to which it refers."));
65d12d83 16203 set_cmd_completer (c, expression_completer);
c906108c 16204
d77f58be 16205 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16206Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16207
920d2a44
AC
16208 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16209 respond to changes - contrary to the description. */
85c07804
AC
16210 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16211 &can_use_hw_watchpoints, _("\
16212Set debugger's willingness to use watchpoint hardware."), _("\
16213Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16214If zero, gdb will not use hardware for new watchpoints, even if\n\
16215such is available. (However, any hardware watchpoints that were\n\
16216created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16217hardware.)"),
16218 NULL,
920d2a44 16219 show_can_use_hw_watchpoints,
85c07804 16220 &setlist, &showlist);
c906108c
SS
16221
16222 can_use_hw_watchpoints = 1;
fa8d40ab 16223
1042e4c0
SS
16224 /* Tracepoint manipulation commands. */
16225
16226 c = add_com ("trace", class_breakpoint, trace_command, _("\
629500fa 16227Set a tracepoint at specified location.\n\
1042e4c0
SS
16228\n"
16229BREAK_ARGS_HELP ("trace") "\n\
16230Do \"help tracepoints\" for info on other tracepoint commands."));
16231 set_cmd_completer (c, location_completer);
16232
16233 add_com_alias ("tp", "trace", class_alias, 0);
16234 add_com_alias ("tr", "trace", class_alias, 1);
16235 add_com_alias ("tra", "trace", class_alias, 1);
16236 add_com_alias ("trac", "trace", class_alias, 1);
16237
7a697b8d 16238 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
629500fa 16239Set a fast tracepoint at specified location.\n\
7a697b8d
SS
16240\n"
16241BREAK_ARGS_HELP ("ftrace") "\n\
16242Do \"help tracepoints\" for info on other tracepoint commands."));
16243 set_cmd_completer (c, location_completer);
16244
0fb4aa4b 16245 c = add_com ("strace", class_breakpoint, strace_command, _("\
629500fa 16246Set a static tracepoint at location or marker.\n\
0fb4aa4b
PA
16247\n\
16248strace [LOCATION] [if CONDITION]\n\
629500fa
KS
16249LOCATION may be a linespec, explicit, or address location (described below) \n\
16250or -m MARKER_ID.\n\n\
16251If a marker id is specified, probe the marker with that name. With\n\
16252no LOCATION, uses current execution address of the selected stack frame.\n\
0fb4aa4b
PA
16253Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16254This collects arbitrary user data passed in the probe point call to the\n\
16255tracing library. You can inspect it when analyzing the trace buffer,\n\
16256by printing the $_sdata variable like any other convenience variable.\n\
16257\n\
16258CONDITION is a boolean expression.\n\
629500fa 16259\n" LOCATION_HELP_STRING "\n\
d41c0fc8
PA
16260Multiple tracepoints at one place are permitted, and useful if their\n\
16261conditions are different.\n\
0fb4aa4b
PA
16262\n\
16263Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16264Do \"help tracepoints\" for info on other tracepoint commands."));
16265 set_cmd_completer (c, location_completer);
16266
1042e4c0 16267 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16268Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16269Convenience variable \"$tpnum\" contains the number of the\n\
16270last tracepoint set."));
16271
16272 add_info_alias ("tp", "tracepoints", 1);
16273
16274 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16275Delete specified tracepoints.\n\
16276Arguments are tracepoint numbers, separated by spaces.\n\
16277No argument means delete all tracepoints."),
16278 &deletelist);
7e20dfcd 16279 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16280
16281 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16282Disable specified tracepoints.\n\
16283Arguments are tracepoint numbers, separated by spaces.\n\
16284No argument means disable all tracepoints."),
16285 &disablelist);
16286 deprecate_cmd (c, "disable");
16287
16288 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16289Enable specified tracepoints.\n\
16290Arguments are tracepoint numbers, separated by spaces.\n\
16291No argument means enable all tracepoints."),
16292 &enablelist);
16293 deprecate_cmd (c, "enable");
16294
16295 add_com ("passcount", class_trace, trace_pass_command, _("\
16296Set the passcount for a tracepoint.\n\
16297The trace will end when the tracepoint has been passed 'count' times.\n\
16298Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16299if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16300
6149aea9
PA
16301 add_prefix_cmd ("save", class_breakpoint, save_command,
16302 _("Save breakpoint definitions as a script."),
16303 &save_cmdlist, "save ",
16304 0/*allow-unknown*/, &cmdlist);
16305
16306 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16307Save current breakpoint definitions as a script.\n\
cce7e648 16308This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16309catchpoints, tracepoints). Use the 'source' command in another debug\n\
16310session to restore them."),
16311 &save_cmdlist);
16312 set_cmd_completer (c, filename_completer);
16313
16314 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16315Save current tracepoint definitions as a script.\n\
6149aea9
PA
16316Use the 'source' command in another debug session to restore them."),
16317 &save_cmdlist);
1042e4c0
SS
16318 set_cmd_completer (c, filename_completer);
16319
6149aea9
PA
16320 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16321 deprecate_cmd (c, "save tracepoints");
16322
1bedd215 16323 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16324Breakpoint specific settings\n\
16325Configure various breakpoint-specific variables such as\n\
1bedd215 16326pending breakpoint behavior"),
fa8d40ab
JJ
16327 &breakpoint_set_cmdlist, "set breakpoint ",
16328 0/*allow-unknown*/, &setlist);
1bedd215 16329 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16330Breakpoint specific settings\n\
16331Configure various breakpoint-specific variables such as\n\
1bedd215 16332pending breakpoint behavior"),
fa8d40ab
JJ
16333 &breakpoint_show_cmdlist, "show breakpoint ",
16334 0/*allow-unknown*/, &showlist);
16335
7915a72c
AC
16336 add_setshow_auto_boolean_cmd ("pending", no_class,
16337 &pending_break_support, _("\
16338Set debugger's behavior regarding pending breakpoints."), _("\
16339Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16340If on, an unrecognized breakpoint location will cause gdb to create a\n\
16341pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16342an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16343user-query to see if a pending breakpoint should be created."),
2c5b56ce 16344 NULL,
920d2a44 16345 show_pending_break_support,
6e1d7d6c
AC
16346 &breakpoint_set_cmdlist,
16347 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16348
16349 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16350
16351 add_setshow_boolean_cmd ("auto-hw", no_class,
16352 &automatic_hardware_breakpoints, _("\
16353Set automatic usage of hardware breakpoints."), _("\
16354Show automatic usage of hardware breakpoints."), _("\
16355If set, the debugger will automatically use hardware breakpoints for\n\
16356breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16357a warning will be emitted for such breakpoints."),
16358 NULL,
16359 show_automatic_hardware_breakpoints,
16360 &breakpoint_set_cmdlist,
16361 &breakpoint_show_cmdlist);
74960c60 16362
a25a5a45
PA
16363 add_setshow_boolean_cmd ("always-inserted", class_support,
16364 &always_inserted_mode, _("\
74960c60
VP
16365Set mode for inserting breakpoints."), _("\
16366Show mode for inserting breakpoints."), _("\
a25a5a45
PA
16367When this mode is on, breakpoints are inserted immediately as soon as\n\
16368they're created, kept inserted even when execution stops, and removed\n\
16369only when the user deletes them. When this mode is off (the default),\n\
16370breakpoints are inserted only when execution continues, and removed\n\
16371when execution stops."),
72d0e2c5
YQ
16372 NULL,
16373 &show_always_inserted_mode,
16374 &breakpoint_set_cmdlist,
16375 &breakpoint_show_cmdlist);
f1310107 16376
b775012e
LM
16377 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16378 condition_evaluation_enums,
16379 &condition_evaluation_mode_1, _("\
16380Set mode of breakpoint condition evaluation."), _("\
16381Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16382When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16383evaluated on the host's side by GDB. When it is set to \"target\",\n\
16384breakpoint conditions will be downloaded to the target (if the target\n\
16385supports such feature) and conditions will be evaluated on the target's side.\n\
16386If this is set to \"auto\" (default), this will be automatically set to\n\
16387\"target\" if it supports condition evaluation, otherwise it will\n\
16388be set to \"gdb\""),
16389 &set_condition_evaluation_mode,
16390 &show_condition_evaluation_mode,
16391 &breakpoint_set_cmdlist,
16392 &breakpoint_show_cmdlist);
16393
f1310107
TJB
16394 add_com ("break-range", class_breakpoint, break_range_command, _("\
16395Set a breakpoint for an address range.\n\
16396break-range START-LOCATION, END-LOCATION\n\
16397where START-LOCATION and END-LOCATION can be one of the following:\n\
16398 LINENUM, for that line in the current file,\n\
16399 FILE:LINENUM, for that line in that file,\n\
16400 +OFFSET, for that number of lines after the current line\n\
16401 or the start of the range\n\
16402 FUNCTION, for the first line in that function,\n\
16403 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16404 *ADDRESS, for the instruction at that address.\n\
16405\n\
16406The breakpoint will stop execution of the inferior whenever it executes\n\
16407an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16408range (including START-LOCATION and END-LOCATION)."));
16409
e7e0cddf 16410 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
629500fa 16411Set a dynamic printf at specified location.\n\
e7e0cddf 16412dprintf location,format string,arg1,arg2,...\n\
629500fa
KS
16413location may be a linespec, explicit, or address location.\n"
16414"\n" LOCATION_HELP_STRING));
e7e0cddf
SS
16415 set_cmd_completer (c, location_completer);
16416
16417 add_setshow_enum_cmd ("dprintf-style", class_support,
16418 dprintf_style_enums, &dprintf_style, _("\
16419Set the style of usage for dynamic printf."), _("\
16420Show the style of usage for dynamic printf."), _("\
16421This setting chooses how GDB will do a dynamic printf.\n\
16422If the value is \"gdb\", then the printing is done by GDB to its own\n\
16423console, as with the \"printf\" command.\n\
16424If the value is \"call\", the print is done by calling a function in your\n\
16425program; by default printf(), but you can choose a different function or\n\
16426output stream by setting dprintf-function and dprintf-channel."),
16427 update_dprintf_commands, NULL,
16428 &setlist, &showlist);
16429
16430 dprintf_function = xstrdup ("printf");
16431 add_setshow_string_cmd ("dprintf-function", class_support,
16432 &dprintf_function, _("\
16433Set the function to use for dynamic printf"), _("\
16434Show the function to use for dynamic printf"), NULL,
16435 update_dprintf_commands, NULL,
16436 &setlist, &showlist);
16437
16438 dprintf_channel = xstrdup ("");
16439 add_setshow_string_cmd ("dprintf-channel", class_support,
16440 &dprintf_channel, _("\
16441Set the channel to use for dynamic printf"), _("\
16442Show the channel to use for dynamic printf"), NULL,
16443 update_dprintf_commands, NULL,
16444 &setlist, &showlist);
16445
d3ce09f5
SS
16446 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16447 &disconnected_dprintf, _("\
16448Set whether dprintf continues after GDB disconnects."), _("\
16449Show whether dprintf continues after GDB disconnects."), _("\
16450Use this to let dprintf commands continue to hit and produce output\n\
16451even if GDB disconnects or detaches from the target."),
16452 NULL,
16453 NULL,
16454 &setlist, &showlist);
16455
16456 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16457agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16458(target agent only) This is useful for formatted output in user-defined commands."));
16459
765dc015 16460 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16461
16462 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16463 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16464}