daily update
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
ecd75fc8 3 Copyright (C) 1986-2014 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"
60250e8b 54#include "exceptions.h"
765dc015 55#include "memattr.h"
f7f9143b 56#include "ada-lang.h"
d1aa2f50 57#include "top.h"
79a45b7d 58#include "valprint.h"
4efc6507 59#include "jit.h"
a96d9b2e 60#include "xml-syscall.h"
65d79d4b 61#include "parser-defs.h"
55aa24fb
SDJ
62#include "gdb_regex.h"
63#include "probe.h"
e9cafbcc 64#include "cli/cli-utils.h"
be34f849 65#include "continuations.h"
1bfeeb0f
JL
66#include "stack.h"
67#include "skip.h"
b775012e 68#include "ax-gdb.h"
e2e4d78b 69#include "dummy-frame.h"
c906108c 70
d3ce09f5
SS
71#include "format.h"
72
1042e4c0
SS
73/* readline include files */
74#include "readline/readline.h"
75#include "readline/history.h"
76
77/* readline defines this. */
78#undef savestring
79
034dad6f 80#include "mi/mi-common.h"
6dddc817 81#include "extension.h"
104c1213 82
e7e8980f
YQ
83/* Enums for exception-handling support. */
84enum exception_event_kind
85{
86 EX_EVENT_THROW,
591f19e8 87 EX_EVENT_RETHROW,
e7e8980f
YQ
88 EX_EVENT_CATCH
89};
90
4a64f543 91/* Prototypes for local functions. */
c906108c 92
a14ed312 93static void enable_delete_command (char *, int);
c906108c 94
a14ed312 95static void enable_once_command (char *, int);
c906108c 96
816338b5
SS
97static void enable_count_command (char *, int);
98
a14ed312 99static void disable_command (char *, int);
c906108c 100
a14ed312 101static void enable_command (char *, int);
c906108c 102
95a42b64
TT
103static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
104 void *),
105 void *);
c906108c 106
a14ed312 107static void ignore_command (char *, int);
c906108c 108
4efb68b1 109static int breakpoint_re_set_one (void *);
c906108c 110
348d480f
PA
111static void breakpoint_re_set_default (struct breakpoint *);
112
983af33b
SDJ
113static void create_sals_from_address_default (char **,
114 struct linespec_result *,
115 enum bptype, char *,
116 char **);
117
118static void create_breakpoints_sal_default (struct gdbarch *,
119 struct linespec_result *,
e7e0cddf 120 char *, char *, enum bptype,
983af33b
SDJ
121 enum bpdisp, int, int,
122 int,
123 const struct breakpoint_ops *,
44f238bb 124 int, int, int, unsigned);
983af33b
SDJ
125
126static void decode_linespec_default (struct breakpoint *, char **,
127 struct symtabs_and_lines *);
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
d03285ec
UW
229static void detach_single_step_breakpoints (void);
230
ef370185
JB
231static int find_single_step_breakpoint (struct address_space *aspace,
232 CORE_ADDR pc);
233
fe3f5fa8 234static void free_bp_location (struct bp_location *loc);
f431efe5
PA
235static void incref_bp_location (struct bp_location *loc);
236static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 237
39d61571 238static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 239
44702360
PA
240/* update_global_location_list's modes of operation wrt to whether to
241 insert locations now. */
242enum ugll_insert_mode
243{
244 /* Don't insert any breakpoint locations into the inferior, only
245 remove already-inserted locations that no longer should be
246 inserted. Functions that delete a breakpoint or breakpoints
247 should specify this mode, so that deleting a breakpoint doesn't
248 have the side effect of inserting the locations of other
249 breakpoints that are marked not-inserted, but should_be_inserted
250 returns true on them.
251
252 This behavior is useful is situations close to tear-down -- e.g.,
253 after an exec, while the target still has execution, but
254 breakpoint shadows of the previous executable image should *NOT*
255 be restored to the new image; or before detaching, where the
256 target still has execution and wants to delete breakpoints from
257 GDB's lists, and all breakpoints had already been removed from
258 the inferior. */
259 UGLL_DONT_INSERT,
260
a25a5a45
PA
261 /* May insert breakpoints iff breakpoints_should_be_inserted_now
262 claims breakpoints should be inserted now. */
04086b45
PA
263 UGLL_MAY_INSERT,
264
a25a5a45
PA
265 /* Insert locations now, irrespective of
266 breakpoints_should_be_inserted_now. E.g., say all threads are
267 stopped right now, and the user did "continue". We need to
268 insert breakpoints _before_ resuming the target, but
269 UGLL_MAY_INSERT wouldn't insert them, because
270 breakpoints_should_be_inserted_now returns false at that point,
271 as no thread is running yet. */
04086b45 272 UGLL_INSERT
44702360
PA
273};
274
275static void update_global_location_list (enum ugll_insert_mode);
a5606eee 276
44702360 277static void update_global_location_list_nothrow (enum ugll_insert_mode);
74960c60 278
d77f58be 279static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
280
281static void insert_breakpoint_locations (void);
a5606eee 282
a96d9b2e
SDJ
283static int syscall_catchpoint_p (struct breakpoint *b);
284
1042e4c0
SS
285static void tracepoints_info (char *, int);
286
287static void delete_trace_command (char *, int);
288
289static void enable_trace_command (char *, int);
290
291static void disable_trace_command (char *, int);
292
293static void trace_pass_command (char *, int);
294
558a9d82
YQ
295static void set_tracepoint_count (int num);
296
9c06b0b4
TJB
297static int is_masked_watchpoint (const struct breakpoint *b);
298
b775012e
LM
299static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
300
983af33b
SDJ
301/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
302 otherwise. */
303
304static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 305
2060206e
PA
306/* The abstract base class all breakpoint_ops structures inherit
307 from. */
ab04a2af 308struct breakpoint_ops base_breakpoint_ops;
2060206e
PA
309
310/* The breakpoint_ops structure to be inherited by all breakpoint_ops
311 that are implemented on top of software or hardware breakpoints
312 (user breakpoints, internal and momentary breakpoints, etc.). */
313static struct breakpoint_ops bkpt_base_breakpoint_ops;
314
315/* Internal breakpoints class type. */
06edf0c0 316static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
317
318/* Momentary breakpoints class type. */
06edf0c0
PA
319static struct breakpoint_ops momentary_breakpoint_ops;
320
e2e4d78b
JK
321/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
322static struct breakpoint_ops longjmp_breakpoint_ops;
323
2060206e
PA
324/* The breakpoint_ops structure to be used in regular user created
325 breakpoints. */
326struct breakpoint_ops bkpt_breakpoint_ops;
327
55aa24fb
SDJ
328/* Breakpoints set on probes. */
329static struct breakpoint_ops bkpt_probe_breakpoint_ops;
330
e7e0cddf 331/* Dynamic printf class type. */
c5867ab6 332struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 333
9d497a19
PA
334/* One (or perhaps two) breakpoints used for software single
335 stepping. */
336
337static void *single_step_breakpoints[2];
338static struct gdbarch *single_step_gdbarch[2];
339
d3ce09f5
SS
340/* The style in which to perform a dynamic printf. This is a user
341 option because different output options have different tradeoffs;
342 if GDB does the printing, there is better error handling if there
343 is a problem with any of the arguments, but using an inferior
344 function lets you have special-purpose printers and sending of
345 output to the same place as compiled-in print functions. */
346
347static const char dprintf_style_gdb[] = "gdb";
348static const char dprintf_style_call[] = "call";
349static const char dprintf_style_agent[] = "agent";
350static const char *const dprintf_style_enums[] = {
351 dprintf_style_gdb,
352 dprintf_style_call,
353 dprintf_style_agent,
354 NULL
355};
356static const char *dprintf_style = dprintf_style_gdb;
357
358/* The function to use for dynamic printf if the preferred style is to
359 call into the inferior. The value is simply a string that is
360 copied into the command, so it can be anything that GDB can
361 evaluate to a callable address, not necessarily a function name. */
362
363static char *dprintf_function = "";
364
365/* The channel to use for dynamic printf if the preferred style is to
366 call into the inferior; if a nonempty string, it will be passed to
367 the call as the first argument, with the format string as the
368 second. As with the dprintf function, this can be anything that
369 GDB knows how to evaluate, so in addition to common choices like
370 "stderr", this could be an app-specific expression like
371 "mystreams[curlogger]". */
372
373static char *dprintf_channel = "";
374
375/* True if dprintf commands should continue to operate even if GDB
376 has disconnected. */
377static int disconnected_dprintf = 1;
378
5cea2a26
PA
379/* A reference-counted struct command_line. This lets multiple
380 breakpoints share a single command list. */
381struct counted_command_line
382{
383 /* The reference count. */
384 int refc;
385
386 /* The command list. */
387 struct command_line *commands;
388};
389
390struct command_line *
391breakpoint_commands (struct breakpoint *b)
392{
393 return b->commands ? b->commands->commands : NULL;
394}
3daf8fe5 395
f3b1572e
PA
396/* Flag indicating that a command has proceeded the inferior past the
397 current breakpoint. */
398
399static int breakpoint_proceeded;
400
956a9fb9 401const char *
2cec12e5
AR
402bpdisp_text (enum bpdisp disp)
403{
4a64f543
MS
404 /* NOTE: the following values are a part of MI protocol and
405 represent values of 'disp' field returned when inferior stops at
406 a breakpoint. */
bc043ef3 407 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 408
2cec12e5
AR
409 return bpdisps[(int) disp];
410}
c906108c 411
4a64f543 412/* Prototypes for exported functions. */
c906108c 413/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 414 if such is available. */
c906108c
SS
415static int can_use_hw_watchpoints;
416
920d2a44
AC
417static void
418show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
419 struct cmd_list_element *c,
420 const char *value)
421{
3e43a32a
MS
422 fprintf_filtered (file,
423 _("Debugger's willingness to use "
424 "watchpoint hardware is %s.\n"),
920d2a44
AC
425 value);
426}
427
fa8d40ab
JJ
428/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
429 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 430 for unrecognized breakpoint locations.
fa8d40ab
JJ
431 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
432static enum auto_boolean pending_break_support;
920d2a44
AC
433static void
434show_pending_break_support (struct ui_file *file, int from_tty,
435 struct cmd_list_element *c,
436 const char *value)
437{
3e43a32a
MS
438 fprintf_filtered (file,
439 _("Debugger's behavior regarding "
440 "pending breakpoints is %s.\n"),
920d2a44
AC
441 value);
442}
fa8d40ab 443
765dc015 444/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 445 set with "break" but falling in read-only memory.
765dc015
VP
446 If 0, gdb will warn about such breakpoints, but won't automatically
447 use hardware breakpoints. */
448static int automatic_hardware_breakpoints;
449static void
450show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
451 struct cmd_list_element *c,
452 const char *value)
453{
3e43a32a
MS
454 fprintf_filtered (file,
455 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
456 value);
457}
458
a25a5a45
PA
459/* If on, GDB keeps breakpoints inserted even if the inferior is
460 stopped, and immediately inserts any new breakpoints as soon as
461 they're created. If off (default), GDB keeps breakpoints off of
462 the target as long as possible. That is, it delays inserting
463 breakpoints until the next resume, and removes them again when the
464 target fully stops. This is a bit safer in case GDB crashes while
465 processing user input. */
466static int always_inserted_mode = 0;
72d0e2c5 467
33e5cbd6 468static void
74960c60 469show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 470 struct cmd_list_element *c, const char *value)
74960c60 471{
a25a5a45
PA
472 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
473 value);
74960c60
VP
474}
475
b57bacec
PA
476/* See breakpoint.h. */
477
33e5cbd6 478int
a25a5a45 479breakpoints_should_be_inserted_now (void)
33e5cbd6 480{
a25a5a45
PA
481 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
482 {
483 /* If breakpoints are global, they should be inserted even if no
484 thread under gdb's control is running, or even if there are
485 no threads under GDB's control yet. */
486 return 1;
487 }
488 else if (target_has_execution)
489 {
a25a5a45
PA
490 if (always_inserted_mode)
491 {
492 /* The user wants breakpoints inserted even if all threads
493 are stopped. */
494 return 1;
495 }
496
b57bacec
PA
497 if (threads_are_executing ())
498 return 1;
a25a5a45
PA
499 }
500 return 0;
33e5cbd6 501}
765dc015 502
b775012e
LM
503static const char condition_evaluation_both[] = "host or target";
504
505/* Modes for breakpoint condition evaluation. */
506static const char condition_evaluation_auto[] = "auto";
507static const char condition_evaluation_host[] = "host";
508static const char condition_evaluation_target[] = "target";
509static const char *const condition_evaluation_enums[] = {
510 condition_evaluation_auto,
511 condition_evaluation_host,
512 condition_evaluation_target,
513 NULL
514};
515
516/* Global that holds the current mode for breakpoint condition evaluation. */
517static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
518
519/* Global that we use to display information to the user (gets its value from
520 condition_evaluation_mode_1. */
521static const char *condition_evaluation_mode = condition_evaluation_auto;
522
523/* Translate a condition evaluation mode MODE into either "host"
524 or "target". This is used mostly to translate from "auto" to the
525 real setting that is being used. It returns the translated
526 evaluation mode. */
527
528static const char *
529translate_condition_evaluation_mode (const char *mode)
530{
531 if (mode == condition_evaluation_auto)
532 {
533 if (target_supports_evaluation_of_breakpoint_conditions ())
534 return condition_evaluation_target;
535 else
536 return condition_evaluation_host;
537 }
538 else
539 return mode;
540}
541
542/* Discovers what condition_evaluation_auto translates to. */
543
544static const char *
545breakpoint_condition_evaluation_mode (void)
546{
547 return translate_condition_evaluation_mode (condition_evaluation_mode);
548}
549
550/* Return true if GDB should evaluate breakpoint conditions or false
551 otherwise. */
552
553static int
554gdb_evaluates_breakpoint_condition_p (void)
555{
556 const char *mode = breakpoint_condition_evaluation_mode ();
557
558 return (mode == condition_evaluation_host);
559}
560
a14ed312 561void _initialize_breakpoint (void);
c906108c 562
c906108c
SS
563/* Are we executing breakpoint commands? */
564static int executing_breakpoint_commands;
565
c02f5703
MS
566/* Are overlay event breakpoints enabled? */
567static int overlay_events_enabled;
568
e09342b5
TJB
569/* See description in breakpoint.h. */
570int target_exact_watchpoints = 0;
571
c906108c 572/* Walk the following statement or block through all breakpoints.
e5dd4106 573 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 574 current breakpoint. */
c906108c 575
5c44784c 576#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 577
5c44784c
JM
578#define ALL_BREAKPOINTS_SAFE(B,TMP) \
579 for (B = breakpoint_chain; \
580 B ? (TMP=B->next, 1): 0; \
581 B = TMP)
c906108c 582
4a64f543
MS
583/* Similar iterator for the low-level breakpoints. SAFE variant is
584 not provided so update_global_location_list must not be called
585 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 586
876fa593
JK
587#define ALL_BP_LOCATIONS(B,BP_TMP) \
588 for (BP_TMP = bp_location; \
589 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
590 BP_TMP++)
7cc221ef 591
b775012e
LM
592/* Iterates through locations with address ADDRESS for the currently selected
593 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
594 to where the loop should start from.
595 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
596 appropriate location to start with. */
597
598#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
599 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
600 BP_LOCP_TMP = BP_LOCP_START; \
601 BP_LOCP_START \
602 && (BP_LOCP_TMP < bp_location + bp_location_count \
603 && (*BP_LOCP_TMP)->address == ADDRESS); \
604 BP_LOCP_TMP++)
605
1042e4c0
SS
606/* Iterator for tracepoints only. */
607
608#define ALL_TRACEPOINTS(B) \
609 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 610 if (is_tracepoint (B))
1042e4c0 611
7cc221ef 612/* Chains of all breakpoints defined. */
c906108c
SS
613
614struct breakpoint *breakpoint_chain;
615
876fa593
JK
616/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
617
618static struct bp_location **bp_location;
619
620/* Number of elements of BP_LOCATION. */
621
622static unsigned bp_location_count;
623
4a64f543
MS
624/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
625 ADDRESS for the current elements of BP_LOCATION which get a valid
626 result from bp_location_has_shadow. You can use it for roughly
627 limiting the subrange of BP_LOCATION to scan for shadow bytes for
628 an address you need to read. */
876fa593
JK
629
630static CORE_ADDR bp_location_placed_address_before_address_max;
631
4a64f543
MS
632/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
633 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
634 BP_LOCATION which get a valid result from bp_location_has_shadow.
635 You can use it for roughly limiting the subrange of BP_LOCATION to
636 scan for shadow bytes for an address you need to read. */
876fa593
JK
637
638static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 639
4a64f543
MS
640/* The locations that no longer correspond to any breakpoint, unlinked
641 from bp_location array, but for which a hit may still be reported
642 by a target. */
20874c92
VP
643VEC(bp_location_p) *moribund_locations = NULL;
644
c906108c
SS
645/* Number of last breakpoint made. */
646
95a42b64
TT
647static int breakpoint_count;
648
86b17b60
PA
649/* The value of `breakpoint_count' before the last command that
650 created breakpoints. If the last (break-like) command created more
651 than one breakpoint, then the difference between BREAKPOINT_COUNT
652 and PREV_BREAKPOINT_COUNT is more than one. */
653static int prev_breakpoint_count;
c906108c 654
1042e4c0
SS
655/* Number of last tracepoint made. */
656
95a42b64 657static int tracepoint_count;
1042e4c0 658
6149aea9
PA
659static struct cmd_list_element *breakpoint_set_cmdlist;
660static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 661struct cmd_list_element *save_cmdlist;
6149aea9 662
468d015d
JJ
663/* Return whether a breakpoint is an active enabled breakpoint. */
664static int
665breakpoint_enabled (struct breakpoint *b)
666{
0d381245 667 return (b->enable_state == bp_enabled);
468d015d
JJ
668}
669
c906108c
SS
670/* Set breakpoint count to NUM. */
671
95a42b64 672static void
fba45db2 673set_breakpoint_count (int num)
c906108c 674{
86b17b60 675 prev_breakpoint_count = breakpoint_count;
c906108c 676 breakpoint_count = num;
4fa62494 677 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
678}
679
86b17b60
PA
680/* Used by `start_rbreak_breakpoints' below, to record the current
681 breakpoint count before "rbreak" creates any breakpoint. */
682static int rbreak_start_breakpoint_count;
683
95a42b64
TT
684/* Called at the start an "rbreak" command to record the first
685 breakpoint made. */
86b17b60 686
95a42b64
TT
687void
688start_rbreak_breakpoints (void)
689{
86b17b60 690 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
691}
692
693/* Called at the end of an "rbreak" command to record the last
694 breakpoint made. */
86b17b60 695
95a42b64
TT
696void
697end_rbreak_breakpoints (void)
698{
86b17b60 699 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
700}
701
4a64f543 702/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
703
704void
fba45db2 705clear_breakpoint_hit_counts (void)
c906108c
SS
706{
707 struct breakpoint *b;
708
709 ALL_BREAKPOINTS (b)
710 b->hit_count = 0;
711}
712
9add0f1b
TT
713/* Allocate a new counted_command_line with reference count of 1.
714 The new structure owns COMMANDS. */
715
716static struct counted_command_line *
717alloc_counted_command_line (struct command_line *commands)
718{
719 struct counted_command_line *result
720 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 721
9add0f1b
TT
722 result->refc = 1;
723 result->commands = commands;
724 return result;
725}
726
727/* Increment reference count. This does nothing if CMD is NULL. */
728
729static void
730incref_counted_command_line (struct counted_command_line *cmd)
731{
732 if (cmd)
733 ++cmd->refc;
734}
735
736/* Decrement reference count. If the reference count reaches 0,
737 destroy the counted_command_line. Sets *CMDP to NULL. This does
738 nothing if *CMDP is NULL. */
739
740static void
741decref_counted_command_line (struct counted_command_line **cmdp)
742{
743 if (*cmdp)
744 {
745 if (--(*cmdp)->refc == 0)
746 {
747 free_command_lines (&(*cmdp)->commands);
748 xfree (*cmdp);
749 }
750 *cmdp = NULL;
751 }
752}
753
754/* A cleanup function that calls decref_counted_command_line. */
755
756static void
757do_cleanup_counted_command_line (void *arg)
758{
759 decref_counted_command_line (arg);
760}
761
762/* Create a cleanup that calls decref_counted_command_line on the
763 argument. */
764
765static struct cleanup *
766make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
767{
768 return make_cleanup (do_cleanup_counted_command_line, cmdp);
769}
770
c906108c 771\f
48cb2d85
VP
772/* Return the breakpoint with the specified number, or NULL
773 if the number does not refer to an existing breakpoint. */
774
775struct breakpoint *
776get_breakpoint (int num)
777{
778 struct breakpoint *b;
779
780 ALL_BREAKPOINTS (b)
781 if (b->number == num)
782 return b;
783
784 return NULL;
785}
5c44784c 786
c906108c 787\f
adc36818 788
b775012e
LM
789/* Mark locations as "conditions have changed" in case the target supports
790 evaluating conditions on its side. */
791
792static void
793mark_breakpoint_modified (struct breakpoint *b)
794{
795 struct bp_location *loc;
796
797 /* This is only meaningful if the target is
798 evaluating conditions and if the user has
799 opted for condition evaluation on the target's
800 side. */
801 if (gdb_evaluates_breakpoint_condition_p ()
802 || !target_supports_evaluation_of_breakpoint_conditions ())
803 return;
804
805 if (!is_breakpoint (b))
806 return;
807
808 for (loc = b->loc; loc; loc = loc->next)
809 loc->condition_changed = condition_modified;
810}
811
812/* Mark location as "conditions have changed" in case the target supports
813 evaluating conditions on its side. */
814
815static void
816mark_breakpoint_location_modified (struct bp_location *loc)
817{
818 /* This is only meaningful if the target is
819 evaluating conditions and if the user has
820 opted for condition evaluation on the target's
821 side. */
822 if (gdb_evaluates_breakpoint_condition_p ()
823 || !target_supports_evaluation_of_breakpoint_conditions ())
824
825 return;
826
827 if (!is_breakpoint (loc->owner))
828 return;
829
830 loc->condition_changed = condition_modified;
831}
832
833/* Sets the condition-evaluation mode using the static global
834 condition_evaluation_mode. */
835
836static void
837set_condition_evaluation_mode (char *args, int from_tty,
838 struct cmd_list_element *c)
839{
b775012e
LM
840 const char *old_mode, *new_mode;
841
842 if ((condition_evaluation_mode_1 == condition_evaluation_target)
843 && !target_supports_evaluation_of_breakpoint_conditions ())
844 {
845 condition_evaluation_mode_1 = condition_evaluation_mode;
846 warning (_("Target does not support breakpoint condition evaluation.\n"
847 "Using host evaluation mode instead."));
848 return;
849 }
850
851 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
852 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
853
abf1152a
JK
854 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
855 settings was "auto". */
856 condition_evaluation_mode = condition_evaluation_mode_1;
857
b775012e
LM
858 /* Only update the mode if the user picked a different one. */
859 if (new_mode != old_mode)
860 {
861 struct bp_location *loc, **loc_tmp;
862 /* If the user switched to a different evaluation mode, we
863 need to synch the changes with the target as follows:
864
865 "host" -> "target": Send all (valid) conditions to the target.
866 "target" -> "host": Remove all the conditions from the target.
867 */
868
b775012e
LM
869 if (new_mode == condition_evaluation_target)
870 {
871 /* Mark everything modified and synch conditions with the
872 target. */
873 ALL_BP_LOCATIONS (loc, loc_tmp)
874 mark_breakpoint_location_modified (loc);
875 }
876 else
877 {
878 /* Manually mark non-duplicate locations to synch conditions
879 with the target. We do this to remove all the conditions the
880 target knows about. */
881 ALL_BP_LOCATIONS (loc, loc_tmp)
882 if (is_breakpoint (loc->owner) && loc->inserted)
883 loc->needs_update = 1;
884 }
885
886 /* Do the update. */
44702360 887 update_global_location_list (UGLL_MAY_INSERT);
b775012e
LM
888 }
889
890 return;
891}
892
893/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
894 what "auto" is translating to. */
895
896static void
897show_condition_evaluation_mode (struct ui_file *file, int from_tty,
898 struct cmd_list_element *c, const char *value)
899{
900 if (condition_evaluation_mode == condition_evaluation_auto)
901 fprintf_filtered (file,
902 _("Breakpoint condition evaluation "
903 "mode is %s (currently %s).\n"),
904 value,
905 breakpoint_condition_evaluation_mode ());
906 else
907 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
908 value);
909}
910
911/* A comparison function for bp_location AP and BP that is used by
912 bsearch. This comparison function only cares about addresses, unlike
913 the more general bp_location_compare function. */
914
915static int
916bp_location_compare_addrs (const void *ap, const void *bp)
917{
918 struct bp_location *a = *(void **) ap;
919 struct bp_location *b = *(void **) bp;
920
921 if (a->address == b->address)
922 return 0;
923 else
924 return ((a->address > b->address) - (a->address < b->address));
925}
926
927/* Helper function to skip all bp_locations with addresses
928 less than ADDRESS. It returns the first bp_location that
929 is greater than or equal to ADDRESS. If none is found, just
930 return NULL. */
931
932static struct bp_location **
933get_first_locp_gte_addr (CORE_ADDR address)
934{
935 struct bp_location dummy_loc;
936 struct bp_location *dummy_locp = &dummy_loc;
937 struct bp_location **locp_found = NULL;
938
939 /* Initialize the dummy location's address field. */
940 memset (&dummy_loc, 0, sizeof (struct bp_location));
941 dummy_loc.address = address;
942
943 /* Find a close match to the first location at ADDRESS. */
944 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
945 sizeof (struct bp_location **),
946 bp_location_compare_addrs);
947
948 /* Nothing was found, nothing left to do. */
949 if (locp_found == NULL)
950 return NULL;
951
952 /* We may have found a location that is at ADDRESS but is not the first in the
953 location's list. Go backwards (if possible) and locate the first one. */
954 while ((locp_found - 1) >= bp_location
955 && (*(locp_found - 1))->address == address)
956 locp_found--;
957
958 return locp_found;
959}
960
adc36818
PM
961void
962set_breakpoint_condition (struct breakpoint *b, char *exp,
963 int from_tty)
964{
3a5c3e22
PA
965 xfree (b->cond_string);
966 b->cond_string = NULL;
adc36818 967
3a5c3e22 968 if (is_watchpoint (b))
adc36818 969 {
3a5c3e22
PA
970 struct watchpoint *w = (struct watchpoint *) b;
971
972 xfree (w->cond_exp);
973 w->cond_exp = NULL;
974 }
975 else
976 {
977 struct bp_location *loc;
978
979 for (loc = b->loc; loc; loc = loc->next)
980 {
981 xfree (loc->cond);
982 loc->cond = NULL;
b775012e
LM
983
984 /* No need to free the condition agent expression
985 bytecode (if we have one). We will handle this
986 when we go through update_global_location_list. */
3a5c3e22 987 }
adc36818 988 }
adc36818
PM
989
990 if (*exp == 0)
991 {
992 if (from_tty)
993 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
994 }
995 else
996 {
bbc13ae3 997 const char *arg = exp;
cc59ec59 998
adc36818
PM
999 /* I don't know if it matters whether this is the string the user
1000 typed in or the decompiled expression. */
1001 b->cond_string = xstrdup (arg);
1002 b->condition_not_parsed = 0;
1003
1004 if (is_watchpoint (b))
1005 {
3a5c3e22
PA
1006 struct watchpoint *w = (struct watchpoint *) b;
1007
adc36818
PM
1008 innermost_block = NULL;
1009 arg = exp;
1bb9788d 1010 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
1011 if (*arg)
1012 error (_("Junk at end of expression"));
3a5c3e22 1013 w->cond_exp_valid_block = innermost_block;
adc36818
PM
1014 }
1015 else
1016 {
3a5c3e22
PA
1017 struct bp_location *loc;
1018
adc36818
PM
1019 for (loc = b->loc; loc; loc = loc->next)
1020 {
1021 arg = exp;
1022 loc->cond =
1bb9788d
TT
1023 parse_exp_1 (&arg, loc->address,
1024 block_for_pc (loc->address), 0);
adc36818
PM
1025 if (*arg)
1026 error (_("Junk at end of expression"));
1027 }
1028 }
1029 }
b775012e
LM
1030 mark_breakpoint_modified (b);
1031
8d3788bd 1032 observer_notify_breakpoint_modified (b);
adc36818
PM
1033}
1034
d55637df
TT
1035/* Completion for the "condition" command. */
1036
1037static VEC (char_ptr) *
6f937416
PA
1038condition_completer (struct cmd_list_element *cmd,
1039 const char *text, const char *word)
d55637df 1040{
6f937416 1041 const char *space;
d55637df 1042
6f937416
PA
1043 text = skip_spaces_const (text);
1044 space = skip_to_space_const (text);
d55637df
TT
1045 if (*space == '\0')
1046 {
1047 int len;
1048 struct breakpoint *b;
1049 VEC (char_ptr) *result = NULL;
1050
1051 if (text[0] == '$')
1052 {
1053 /* We don't support completion of history indices. */
1054 if (isdigit (text[1]))
1055 return NULL;
1056 return complete_internalvar (&text[1]);
1057 }
1058
1059 /* We're completing the breakpoint number. */
1060 len = strlen (text);
1061
1062 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1063 {
1064 char number[50];
1065
1066 xsnprintf (number, sizeof (number), "%d", b->number);
1067
1068 if (strncmp (number, text, len) == 0)
1069 VEC_safe_push (char_ptr, result, xstrdup (number));
1070 }
d55637df
TT
1071
1072 return result;
1073 }
1074
1075 /* We're completing the expression part. */
6f937416 1076 text = skip_spaces_const (space);
d55637df
TT
1077 return expression_completer (cmd, text, word);
1078}
1079
c906108c
SS
1080/* condition N EXP -- set break condition of breakpoint N to EXP. */
1081
1082static void
fba45db2 1083condition_command (char *arg, int from_tty)
c906108c 1084{
52f0bd74 1085 struct breakpoint *b;
c906108c 1086 char *p;
52f0bd74 1087 int bnum;
c906108c
SS
1088
1089 if (arg == 0)
e2e0b3e5 1090 error_no_arg (_("breakpoint number"));
c906108c
SS
1091
1092 p = arg;
1093 bnum = get_number (&p);
5c44784c 1094 if (bnum == 0)
8a3fe4f8 1095 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1096
1097 ALL_BREAKPOINTS (b)
1098 if (b->number == bnum)
2f069f6f 1099 {
6dddc817
DE
1100 /* Check if this breakpoint has a "stop" method implemented in an
1101 extension language. This method and conditions entered into GDB
1102 from the CLI are mutually exclusive. */
1103 const struct extension_language_defn *extlang
1104 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1105
1106 if (extlang != NULL)
1107 {
1108 error (_("Only one stop condition allowed. There is currently"
1109 " a %s stop condition defined for this breakpoint."),
1110 ext_lang_capitalized_name (extlang));
1111 }
2566ad2d 1112 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1113
1114 if (is_breakpoint (b))
44702360 1115 update_global_location_list (UGLL_MAY_INSERT);
b775012e 1116
2f069f6f
JB
1117 return;
1118 }
c906108c 1119
8a3fe4f8 1120 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1121}
1122
a7bdde9e
VP
1123/* Check that COMMAND do not contain commands that are suitable
1124 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1125 Throw if any such commands is found. */
1126
a7bdde9e
VP
1127static void
1128check_no_tracepoint_commands (struct command_line *commands)
1129{
1130 struct command_line *c;
cc59ec59 1131
a7bdde9e
VP
1132 for (c = commands; c; c = c->next)
1133 {
1134 int i;
1135
1136 if (c->control_type == while_stepping_control)
3e43a32a
MS
1137 error (_("The 'while-stepping' command can "
1138 "only be used for tracepoints"));
a7bdde9e
VP
1139
1140 for (i = 0; i < c->body_count; ++i)
1141 check_no_tracepoint_commands ((c->body_list)[i]);
1142
1143 /* Not that command parsing removes leading whitespace and comment
4a64f543 1144 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1145 command directly. */
1146 if (strstr (c->line, "collect ") == c->line)
1147 error (_("The 'collect' command can only be used for tracepoints"));
1148
51661e93
VP
1149 if (strstr (c->line, "teval ") == c->line)
1150 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1151 }
1152}
1153
d77f58be
SS
1154/* Encapsulate tests for different types of tracepoints. */
1155
d9b3f62e
PA
1156static int
1157is_tracepoint_type (enum bptype type)
1158{
1159 return (type == bp_tracepoint
1160 || type == bp_fast_tracepoint
1161 || type == bp_static_tracepoint);
1162}
1163
a7bdde9e 1164int
d77f58be 1165is_tracepoint (const struct breakpoint *b)
a7bdde9e 1166{
d9b3f62e 1167 return is_tracepoint_type (b->type);
a7bdde9e 1168}
d9b3f62e 1169
e5dd4106 1170/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1171 breakpoint. This function will throw an exception if a problem is
1172 found. */
48cb2d85 1173
95a42b64
TT
1174static void
1175validate_commands_for_breakpoint (struct breakpoint *b,
1176 struct command_line *commands)
48cb2d85 1177{
d77f58be 1178 if (is_tracepoint (b))
a7bdde9e 1179 {
c9a6ce02 1180 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1181 struct command_line *c;
1182 struct command_line *while_stepping = 0;
c9a6ce02
PA
1183
1184 /* Reset the while-stepping step count. The previous commands
1185 might have included a while-stepping action, while the new
1186 ones might not. */
1187 t->step_count = 0;
1188
1189 /* We need to verify that each top-level element of commands is
1190 valid for tracepoints, that there's at most one
1191 while-stepping element, and that the while-stepping's body
1192 has valid tracing commands excluding nested while-stepping.
1193 We also need to validate the tracepoint action line in the
1194 context of the tracepoint --- validate_actionline actually
1195 has side effects, like setting the tracepoint's
1196 while-stepping STEP_COUNT, in addition to checking if the
1197 collect/teval actions parse and make sense in the
1198 tracepoint's context. */
a7bdde9e
VP
1199 for (c = commands; c; c = c->next)
1200 {
a7bdde9e
VP
1201 if (c->control_type == while_stepping_control)
1202 {
1203 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1204 error (_("The 'while-stepping' command "
1205 "cannot be used for fast tracepoint"));
0fb4aa4b 1206 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1207 error (_("The 'while-stepping' command "
1208 "cannot be used for static tracepoint"));
a7bdde9e
VP
1209
1210 if (while_stepping)
3e43a32a
MS
1211 error (_("The 'while-stepping' command "
1212 "can be used only once"));
a7bdde9e
VP
1213 else
1214 while_stepping = c;
1215 }
c9a6ce02
PA
1216
1217 validate_actionline (c->line, b);
a7bdde9e
VP
1218 }
1219 if (while_stepping)
1220 {
1221 struct command_line *c2;
1222
1223 gdb_assert (while_stepping->body_count == 1);
1224 c2 = while_stepping->body_list[0];
1225 for (; c2; c2 = c2->next)
1226 {
a7bdde9e
VP
1227 if (c2->control_type == while_stepping_control)
1228 error (_("The 'while-stepping' command cannot be nested"));
1229 }
1230 }
1231 }
1232 else
1233 {
1234 check_no_tracepoint_commands (commands);
1235 }
95a42b64
TT
1236}
1237
0fb4aa4b
PA
1238/* Return a vector of all the static tracepoints set at ADDR. The
1239 caller is responsible for releasing the vector. */
1240
1241VEC(breakpoint_p) *
1242static_tracepoints_here (CORE_ADDR addr)
1243{
1244 struct breakpoint *b;
1245 VEC(breakpoint_p) *found = 0;
1246 struct bp_location *loc;
1247
1248 ALL_BREAKPOINTS (b)
1249 if (b->type == bp_static_tracepoint)
1250 {
1251 for (loc = b->loc; loc; loc = loc->next)
1252 if (loc->address == addr)
1253 VEC_safe_push(breakpoint_p, found, b);
1254 }
1255
1256 return found;
1257}
1258
95a42b64 1259/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1260 validate that only allowed commands are included. */
95a42b64
TT
1261
1262void
4a64f543
MS
1263breakpoint_set_commands (struct breakpoint *b,
1264 struct command_line *commands)
95a42b64
TT
1265{
1266 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1267
9add0f1b
TT
1268 decref_counted_command_line (&b->commands);
1269 b->commands = alloc_counted_command_line (commands);
8d3788bd 1270 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1271}
1272
45a43567
TT
1273/* Set the internal `silent' flag on the breakpoint. Note that this
1274 is not the same as the "silent" that may appear in the breakpoint's
1275 commands. */
1276
1277void
1278breakpoint_set_silent (struct breakpoint *b, int silent)
1279{
1280 int old_silent = b->silent;
1281
1282 b->silent = silent;
1283 if (old_silent != silent)
8d3788bd 1284 observer_notify_breakpoint_modified (b);
45a43567
TT
1285}
1286
1287/* Set the thread for this breakpoint. If THREAD is -1, make the
1288 breakpoint work for any thread. */
1289
1290void
1291breakpoint_set_thread (struct breakpoint *b, int thread)
1292{
1293 int old_thread = b->thread;
1294
1295 b->thread = thread;
1296 if (old_thread != thread)
8d3788bd 1297 observer_notify_breakpoint_modified (b);
45a43567
TT
1298}
1299
1300/* Set the task for this breakpoint. If TASK is 0, make the
1301 breakpoint work for any task. */
1302
1303void
1304breakpoint_set_task (struct breakpoint *b, int task)
1305{
1306 int old_task = b->task;
1307
1308 b->task = task;
1309 if (old_task != task)
8d3788bd 1310 observer_notify_breakpoint_modified (b);
45a43567
TT
1311}
1312
95a42b64
TT
1313void
1314check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1315{
1316 struct breakpoint *b = closure;
cc59ec59 1317
6f937416 1318 validate_actionline (line, b);
a7bdde9e
VP
1319}
1320
95a42b64
TT
1321/* A structure used to pass information through
1322 map_breakpoint_numbers. */
1323
1324struct commands_info
1325{
1326 /* True if the command was typed at a tty. */
1327 int from_tty;
86b17b60
PA
1328
1329 /* The breakpoint range spec. */
1330 char *arg;
1331
95a42b64
TT
1332 /* Non-NULL if the body of the commands are being read from this
1333 already-parsed command. */
1334 struct command_line *control;
86b17b60 1335
95a42b64
TT
1336 /* The command lines read from the user, or NULL if they have not
1337 yet been read. */
1338 struct counted_command_line *cmd;
1339};
1340
1341/* A callback for map_breakpoint_numbers that sets the commands for
1342 commands_command. */
1343
c906108c 1344static void
95a42b64 1345do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1346{
95a42b64 1347 struct commands_info *info = data;
c906108c 1348
95a42b64
TT
1349 if (info->cmd == NULL)
1350 {
1351 struct command_line *l;
5c44784c 1352
95a42b64
TT
1353 if (info->control != NULL)
1354 l = copy_command_lines (info->control->body_list[0]);
1355 else
86b17b60
PA
1356 {
1357 struct cleanup *old_chain;
1358 char *str;
c5aa993b 1359
3e43a32a
MS
1360 str = xstrprintf (_("Type commands for breakpoint(s) "
1361 "%s, one per line."),
86b17b60
PA
1362 info->arg);
1363
1364 old_chain = make_cleanup (xfree, str);
1365
1366 l = read_command_lines (str,
1367 info->from_tty, 1,
d77f58be 1368 (is_tracepoint (b)
86b17b60
PA
1369 ? check_tracepoint_command : 0),
1370 b);
1371
1372 do_cleanups (old_chain);
1373 }
a7bdde9e 1374
95a42b64
TT
1375 info->cmd = alloc_counted_command_line (l);
1376 }
1377
1378 /* If a breakpoint was on the list more than once, we don't need to
1379 do anything. */
1380 if (b->commands != info->cmd)
1381 {
1382 validate_commands_for_breakpoint (b, info->cmd->commands);
1383 incref_counted_command_line (info->cmd);
1384 decref_counted_command_line (&b->commands);
1385 b->commands = info->cmd;
8d3788bd 1386 observer_notify_breakpoint_modified (b);
c5aa993b 1387 }
95a42b64
TT
1388}
1389
1390static void
4a64f543
MS
1391commands_command_1 (char *arg, int from_tty,
1392 struct command_line *control)
95a42b64
TT
1393{
1394 struct cleanup *cleanups;
1395 struct commands_info info;
1396
1397 info.from_tty = from_tty;
1398 info.control = control;
1399 info.cmd = NULL;
1400 /* If we read command lines from the user, then `info' will hold an
1401 extra reference to the commands that we must clean up. */
1402 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1403
1404 if (arg == NULL || !*arg)
1405 {
86b17b60 1406 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1407 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1408 breakpoint_count);
95a42b64
TT
1409 else if (breakpoint_count > 0)
1410 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1411 else
1412 {
1413 /* So that we don't try to free the incoming non-NULL
1414 argument in the cleanup below. Mapping breakpoint
1415 numbers will fail in this case. */
1416 arg = NULL;
1417 }
95a42b64 1418 }
9766ced4
SS
1419 else
1420 /* The command loop has some static state, so we need to preserve
1421 our argument. */
1422 arg = xstrdup (arg);
86b17b60
PA
1423
1424 if (arg != NULL)
1425 make_cleanup (xfree, arg);
1426
1427 info.arg = arg;
95a42b64
TT
1428
1429 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1430
1431 if (info.cmd == NULL)
1432 error (_("No breakpoints specified."));
1433
1434 do_cleanups (cleanups);
1435}
1436
1437static void
1438commands_command (char *arg, int from_tty)
1439{
1440 commands_command_1 (arg, from_tty, NULL);
c906108c 1441}
40c03ae8
EZ
1442
1443/* Like commands_command, but instead of reading the commands from
1444 input stream, takes them from an already parsed command structure.
1445
1446 This is used by cli-script.c to DTRT with breakpoint commands
1447 that are part of if and while bodies. */
1448enum command_control_type
1449commands_from_control_command (char *arg, struct command_line *cmd)
1450{
95a42b64
TT
1451 commands_command_1 (arg, 0, cmd);
1452 return simple_control;
40c03ae8 1453}
876fa593
JK
1454
1455/* Return non-zero if BL->TARGET_INFO contains valid information. */
1456
1457static int
1458bp_location_has_shadow (struct bp_location *bl)
1459{
1460 if (bl->loc_type != bp_loc_software_breakpoint)
1461 return 0;
1462 if (!bl->inserted)
1463 return 0;
1464 if (bl->target_info.shadow_len == 0)
e5dd4106 1465 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1466 return 0;
1467 return 1;
1468}
1469
9d497a19
PA
1470/* Update BUF, which is LEN bytes read from the target address
1471 MEMADDR, by replacing a memory breakpoint with its shadowed
1472 contents.
1473
1474 If READBUF is not NULL, this buffer must not overlap with the of
1475 the breakpoint location's shadow_contents buffer. Otherwise, a
1476 failed assertion internal error will be raised. */
1477
1478static void
1479one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1480 const gdb_byte *writebuf_org,
1481 ULONGEST memaddr, LONGEST len,
1482 struct bp_target_info *target_info,
1483 struct gdbarch *gdbarch)
1484{
1485 /* Now do full processing of the found relevant range of elements. */
1486 CORE_ADDR bp_addr = 0;
1487 int bp_size = 0;
1488 int bptoffset = 0;
1489
1490 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1491 current_program_space->aspace, 0))
1492 {
1493 /* The breakpoint is inserted in a different address space. */
1494 return;
1495 }
1496
1497 /* Addresses and length of the part of the breakpoint that
1498 we need to copy. */
1499 bp_addr = target_info->placed_address;
1500 bp_size = target_info->shadow_len;
1501
1502 if (bp_addr + bp_size <= memaddr)
1503 {
1504 /* The breakpoint is entirely before the chunk of memory we are
1505 reading. */
1506 return;
1507 }
1508
1509 if (bp_addr >= memaddr + len)
1510 {
1511 /* The breakpoint is entirely after the chunk of memory we are
1512 reading. */
1513 return;
1514 }
1515
1516 /* Offset within shadow_contents. */
1517 if (bp_addr < memaddr)
1518 {
1519 /* Only copy the second part of the breakpoint. */
1520 bp_size -= memaddr - bp_addr;
1521 bptoffset = memaddr - bp_addr;
1522 bp_addr = memaddr;
1523 }
1524
1525 if (bp_addr + bp_size > memaddr + len)
1526 {
1527 /* Only copy the first part of the breakpoint. */
1528 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1529 }
1530
1531 if (readbuf != NULL)
1532 {
1533 /* Verify that the readbuf buffer does not overlap with the
1534 shadow_contents buffer. */
1535 gdb_assert (target_info->shadow_contents >= readbuf + len
1536 || readbuf >= (target_info->shadow_contents
1537 + target_info->shadow_len));
1538
1539 /* Update the read buffer with this inserted breakpoint's
1540 shadow. */
1541 memcpy (readbuf + bp_addr - memaddr,
1542 target_info->shadow_contents + bptoffset, bp_size);
1543 }
1544 else
1545 {
1546 const unsigned char *bp;
0d5ed153
MR
1547 CORE_ADDR addr = target_info->reqstd_address;
1548 int placed_size;
9d497a19
PA
1549
1550 /* Update the shadow with what we want to write to memory. */
1551 memcpy (target_info->shadow_contents + bptoffset,
1552 writebuf_org + bp_addr - memaddr, bp_size);
1553
1554 /* Determine appropriate breakpoint contents and size for this
1555 address. */
0d5ed153 1556 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
9d497a19
PA
1557
1558 /* Update the final write buffer with this inserted
1559 breakpoint's INSN. */
1560 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1561 }
1562}
1563
8defab1a 1564/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1565 by replacing any memory breakpoints with their shadowed contents.
1566
35c63cd8
JB
1567 If READBUF is not NULL, this buffer must not overlap with any of
1568 the breakpoint location's shadow_contents buffers. Otherwise,
1569 a failed assertion internal error will be raised.
1570
876fa593 1571 The range of shadowed area by each bp_location is:
35df4500
TJB
1572 bl->address - bp_location_placed_address_before_address_max
1573 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1574 The range we were requested to resolve shadows for is:
1575 memaddr ... memaddr + len
1576 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1577 memaddr + len <= (bl->address
1578 - bp_location_placed_address_before_address_max)
876fa593 1579 and:
35df4500 1580 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1581
8defab1a 1582void
f0ba3972
PA
1583breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1584 const gdb_byte *writebuf_org,
1585 ULONGEST memaddr, LONGEST len)
c906108c 1586{
4a64f543
MS
1587 /* Left boundary, right boundary and median element of our binary
1588 search. */
876fa593 1589 unsigned bc_l, bc_r, bc;
9d497a19 1590 size_t i;
876fa593 1591
4a64f543
MS
1592 /* Find BC_L which is a leftmost element which may affect BUF
1593 content. It is safe to report lower value but a failure to
1594 report higher one. */
876fa593
JK
1595
1596 bc_l = 0;
1597 bc_r = bp_location_count;
1598 while (bc_l + 1 < bc_r)
1599 {
35df4500 1600 struct bp_location *bl;
876fa593
JK
1601
1602 bc = (bc_l + bc_r) / 2;
35df4500 1603 bl = bp_location[bc];
876fa593 1604
4a64f543
MS
1605 /* Check first BL->ADDRESS will not overflow due to the added
1606 constant. Then advance the left boundary only if we are sure
1607 the BC element can in no way affect the BUF content (MEMADDR
1608 to MEMADDR + LEN range).
876fa593 1609
4a64f543
MS
1610 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1611 offset so that we cannot miss a breakpoint with its shadow
1612 range tail still reaching MEMADDR. */
c5aa993b 1613
35df4500
TJB
1614 if ((bl->address + bp_location_shadow_len_after_address_max
1615 >= bl->address)
1616 && (bl->address + bp_location_shadow_len_after_address_max
1617 <= memaddr))
876fa593
JK
1618 bc_l = bc;
1619 else
1620 bc_r = bc;
1621 }
1622
128070bb
PA
1623 /* Due to the binary search above, we need to make sure we pick the
1624 first location that's at BC_L's address. E.g., if there are
1625 multiple locations at the same address, BC_L may end up pointing
1626 at a duplicate location, and miss the "master"/"inserted"
1627 location. Say, given locations L1, L2 and L3 at addresses A and
1628 B:
1629
1630 L1@A, L2@A, L3@B, ...
1631
1632 BC_L could end up pointing at location L2, while the "master"
1633 location could be L1. Since the `loc->inserted' flag is only set
1634 on "master" locations, we'd forget to restore the shadow of L1
1635 and L2. */
1636 while (bc_l > 0
1637 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1638 bc_l--;
1639
876fa593
JK
1640 /* Now do full processing of the found relevant range of elements. */
1641
1642 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1643 {
35df4500 1644 struct bp_location *bl = bp_location[bc];
876fa593
JK
1645 CORE_ADDR bp_addr = 0;
1646 int bp_size = 0;
1647 int bptoffset = 0;
1648
35df4500
TJB
1649 /* bp_location array has BL->OWNER always non-NULL. */
1650 if (bl->owner->type == bp_none)
8a3fe4f8 1651 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1652 bl->owner->number);
ffce0d52 1653
e5dd4106 1654 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1655 content. */
1656
35df4500
TJB
1657 if (bl->address >= bp_location_placed_address_before_address_max
1658 && memaddr + len <= (bl->address
1659 - bp_location_placed_address_before_address_max))
876fa593
JK
1660 break;
1661
35df4500 1662 if (!bp_location_has_shadow (bl))
c5aa993b 1663 continue;
6c95b8df 1664
9d497a19
PA
1665 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1666 memaddr, len, &bl->target_info, bl->gdbarch);
1667 }
c5aa993b 1668
9d497a19
PA
1669 /* Now process single-step breakpoints. These are not found in the
1670 bp_location array. */
1671 for (i = 0; i < 2; i++)
1672 {
1673 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
c5aa993b 1674
9d497a19
PA
1675 if (bp_tgt != NULL)
1676 {
1677 struct gdbarch *gdbarch = single_step_gdbarch[i];
c5aa993b 1678
9d497a19
PA
1679 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1680 memaddr, len, bp_tgt, gdbarch);
1681 }
1682 }
c906108c 1683}
9d497a19 1684
c906108c 1685\f
c5aa993b 1686
b775012e
LM
1687/* Return true if BPT is either a software breakpoint or a hardware
1688 breakpoint. */
1689
1690int
1691is_breakpoint (const struct breakpoint *bpt)
1692{
1693 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1694 || bpt->type == bp_hardware_breakpoint
1695 || bpt->type == bp_dprintf);
b775012e
LM
1696}
1697
60e1c644
PA
1698/* Return true if BPT is of any hardware watchpoint kind. */
1699
a5606eee 1700static int
d77f58be 1701is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1702{
1703 return (bpt->type == bp_hardware_watchpoint
1704 || bpt->type == bp_read_watchpoint
1705 || bpt->type == bp_access_watchpoint);
1706}
7270d8f2 1707
60e1c644
PA
1708/* Return true if BPT is of any watchpoint kind, hardware or
1709 software. */
1710
3a5c3e22 1711int
d77f58be 1712is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1713{
1714 return (is_hardware_watchpoint (bpt)
1715 || bpt->type == bp_watchpoint);
1716}
1717
3a5c3e22
PA
1718/* Returns true if the current thread and its running state are safe
1719 to evaluate or update watchpoint B. Watchpoints on local
1720 expressions need to be evaluated in the context of the thread that
1721 was current when the watchpoint was created, and, that thread needs
1722 to be stopped to be able to select the correct frame context.
1723 Watchpoints on global expressions can be evaluated on any thread,
1724 and in any state. It is presently left to the target allowing
1725 memory accesses when threads are running. */
f6bc2008
PA
1726
1727static int
3a5c3e22 1728watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1729{
d0d8b0c6
JK
1730 return (b->base.pspace == current_program_space
1731 && (ptid_equal (b->watchpoint_thread, null_ptid)
1732 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1733 && !is_executing (inferior_ptid))));
f6bc2008
PA
1734}
1735
d0fb5eae
JK
1736/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1737 associated bp_watchpoint_scope breakpoint. */
1738
1739static void
3a5c3e22 1740watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1741{
3a5c3e22 1742 struct breakpoint *b = &w->base;
d0fb5eae
JK
1743
1744 if (b->related_breakpoint != b)
1745 {
1746 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1747 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1748 b->related_breakpoint->disposition = disp_del_at_next_stop;
1749 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1750 b->related_breakpoint = b;
1751 }
1752 b->disposition = disp_del_at_next_stop;
1753}
1754
bb9d5f81
PP
1755/* Extract a bitfield value from value VAL using the bit parameters contained in
1756 watchpoint W. */
1757
1758static struct value *
1759extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1760{
1761 struct value *bit_val;
1762
1763 if (val == NULL)
1764 return NULL;
1765
1766 bit_val = allocate_value (value_type (val));
1767
1768 unpack_value_bitfield (bit_val,
1769 w->val_bitpos,
1770 w->val_bitsize,
1771 value_contents_for_printing (val),
1772 value_offset (val),
1773 val);
1774
1775 return bit_val;
1776}
1777
567e1b4e
JB
1778/* Assuming that B is a watchpoint:
1779 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1780 - Evaluate expression and store the result in B->val
567e1b4e
JB
1781 - Evaluate the condition if there is one, and store the result
1782 in b->loc->cond.
a5606eee
VP
1783 - Update the list of values that must be watched in B->loc.
1784
4a64f543
MS
1785 If the watchpoint disposition is disp_del_at_next_stop, then do
1786 nothing. If this is local watchpoint that is out of scope, delete
1787 it.
1788
1789 Even with `set breakpoint always-inserted on' the watchpoints are
1790 removed + inserted on each stop here. Normal breakpoints must
1791 never be removed because they might be missed by a running thread
1792 when debugging in non-stop mode. On the other hand, hardware
1793 watchpoints (is_hardware_watchpoint; processed here) are specific
1794 to each LWP since they are stored in each LWP's hardware debug
1795 registers. Therefore, such LWP must be stopped first in order to
1796 be able to modify its hardware watchpoints.
1797
1798 Hardware watchpoints must be reset exactly once after being
1799 presented to the user. It cannot be done sooner, because it would
1800 reset the data used to present the watchpoint hit to the user. And
1801 it must not be done later because it could display the same single
1802 watchpoint hit during multiple GDB stops. Note that the latter is
1803 relevant only to the hardware watchpoint types bp_read_watchpoint
1804 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1805 not user-visible - its hit is suppressed if the memory content has
1806 not changed.
1807
1808 The following constraints influence the location where we can reset
1809 hardware watchpoints:
1810
1811 * target_stopped_by_watchpoint and target_stopped_data_address are
1812 called several times when GDB stops.
1813
1814 [linux]
1815 * Multiple hardware watchpoints can be hit at the same time,
1816 causing GDB to stop. GDB only presents one hardware watchpoint
1817 hit at a time as the reason for stopping, and all the other hits
1818 are presented later, one after the other, each time the user
1819 requests the execution to be resumed. Execution is not resumed
1820 for the threads still having pending hit event stored in
1821 LWP_INFO->STATUS. While the watchpoint is already removed from
1822 the inferior on the first stop the thread hit event is kept being
1823 reported from its cached value by linux_nat_stopped_data_address
1824 until the real thread resume happens after the watchpoint gets
1825 presented and thus its LWP_INFO->STATUS gets reset.
1826
1827 Therefore the hardware watchpoint hit can get safely reset on the
1828 watchpoint removal from inferior. */
a79d3c27 1829
b40ce68a 1830static void
3a5c3e22 1831update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1832{
a5606eee 1833 int within_current_scope;
a5606eee 1834 struct frame_id saved_frame_id;
66076460 1835 int frame_saved;
a5606eee 1836
f6bc2008
PA
1837 /* If this is a local watchpoint, we only want to check if the
1838 watchpoint frame is in scope if the current thread is the thread
1839 that was used to create the watchpoint. */
1840 if (!watchpoint_in_thread_scope (b))
1841 return;
1842
3a5c3e22 1843 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1844 return;
1845
66076460 1846 frame_saved = 0;
a5606eee
VP
1847
1848 /* Determine if the watchpoint is within scope. */
1849 if (b->exp_valid_block == NULL)
1850 within_current_scope = 1;
1851 else
1852 {
b5db5dfc
UW
1853 struct frame_info *fi = get_current_frame ();
1854 struct gdbarch *frame_arch = get_frame_arch (fi);
1855 CORE_ADDR frame_pc = get_frame_pc (fi);
1856
1857 /* If we're in a function epilogue, unwinding may not work
1858 properly, so do not attempt to recreate locations at this
1859 point. See similar comments in watchpoint_check. */
1860 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1861 return;
66076460
DJ
1862
1863 /* Save the current frame's ID so we can restore it after
1864 evaluating the watchpoint expression on its own frame. */
1865 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1866 took a frame parameter, so that we didn't have to change the
1867 selected frame. */
1868 frame_saved = 1;
1869 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1870
a5606eee
VP
1871 fi = frame_find_by_id (b->watchpoint_frame);
1872 within_current_scope = (fi != NULL);
1873 if (within_current_scope)
1874 select_frame (fi);
1875 }
1876
b5db5dfc
UW
1877 /* We don't free locations. They are stored in the bp_location array
1878 and update_global_location_list will eventually delete them and
1879 remove breakpoints if needed. */
3a5c3e22 1880 b->base.loc = NULL;
b5db5dfc 1881
a5606eee
VP
1882 if (within_current_scope && reparse)
1883 {
bbc13ae3 1884 const char *s;
d63d0675 1885
a5606eee
VP
1886 if (b->exp)
1887 {
1888 xfree (b->exp);
1889 b->exp = NULL;
1890 }
d63d0675 1891 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1892 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1893 /* If the meaning of expression itself changed, the old value is
1894 no longer relevant. We don't want to report a watchpoint hit
1895 to the user when the old value and the new value may actually
1896 be completely different objects. */
1897 value_free (b->val);
fa4727a6
DJ
1898 b->val = NULL;
1899 b->val_valid = 0;
60e1c644
PA
1900
1901 /* Note that unlike with breakpoints, the watchpoint's condition
1902 expression is stored in the breakpoint object, not in the
1903 locations (re)created below. */
3a5c3e22 1904 if (b->base.cond_string != NULL)
60e1c644
PA
1905 {
1906 if (b->cond_exp != NULL)
1907 {
1908 xfree (b->cond_exp);
1909 b->cond_exp = NULL;
1910 }
1911
3a5c3e22 1912 s = b->base.cond_string;
1bb9788d 1913 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1914 }
a5606eee 1915 }
a5606eee
VP
1916
1917 /* If we failed to parse the expression, for example because
1918 it refers to a global variable in a not-yet-loaded shared library,
1919 don't try to insert watchpoint. We don't automatically delete
1920 such watchpoint, though, since failure to parse expression
1921 is different from out-of-scope watchpoint. */
e8369a73 1922 if (!target_has_execution)
2d134ed3
PA
1923 {
1924 /* Without execution, memory can't change. No use to try and
1925 set watchpoint locations. The watchpoint will be reset when
1926 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1927 if (!can_use_hw_watchpoints)
1928 {
1929 if (b->base.ops->works_in_software_mode (&b->base))
1930 b->base.type = bp_watchpoint;
1931 else
638aa5a1
AB
1932 error (_("Can't set read/access watchpoint when "
1933 "hardware watchpoints are disabled."));
e8369a73 1934 }
2d134ed3
PA
1935 }
1936 else if (within_current_scope && b->exp)
a5606eee 1937 {
0cf6dd15 1938 int pc = 0;
fa4727a6 1939 struct value *val_chain, *v, *result, *next;
2d134ed3 1940 struct program_space *frame_pspace;
a5606eee 1941
3a1115a0 1942 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1943
a5606eee
VP
1944 /* Avoid setting b->val if it's already set. The meaning of
1945 b->val is 'the last value' user saw, and we should update
1946 it only if we reported that last value to user. As it
9c06b0b4
TJB
1947 happens, the code that reports it updates b->val directly.
1948 We don't keep track of the memory value for masked
1949 watchpoints. */
3a5c3e22 1950 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6 1951 {
bb9d5f81
PP
1952 if (b->val_bitsize != 0)
1953 {
1954 v = extract_bitfield_from_watchpoint_value (b, v);
1955 if (v != NULL)
1956 release_value (v);
1957 }
fa4727a6
DJ
1958 b->val = v;
1959 b->val_valid = 1;
1960 }
a5606eee 1961
2d134ed3
PA
1962 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1963
a5606eee 1964 /* Look at each value on the value chain. */
9fa40276 1965 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1966 {
1967 /* If it's a memory location, and GDB actually needed
1968 its contents to evaluate the expression, then we
fa4727a6
DJ
1969 must watch it. If the first value returned is
1970 still lazy, that means an error occurred reading it;
1971 watch it anyway in case it becomes readable. */
a5606eee 1972 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1973 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1974 {
1975 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1976
a5606eee
VP
1977 /* We only watch structs and arrays if user asked
1978 for it explicitly, never if they just happen to
1979 appear in the middle of some value chain. */
fa4727a6 1980 if (v == result
a5606eee
VP
1981 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1982 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1983 {
1984 CORE_ADDR addr;
744a8059 1985 int type;
a5606eee 1986 struct bp_location *loc, **tmp;
bb9d5f81
PP
1987 int bitpos = 0, bitsize = 0;
1988
1989 if (value_bitsize (v) != 0)
1990 {
1991 /* Extract the bit parameters out from the bitfield
1992 sub-expression. */
1993 bitpos = value_bitpos (v);
1994 bitsize = value_bitsize (v);
1995 }
1996 else if (v == result && b->val_bitsize != 0)
1997 {
1998 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1999 lvalue whose bit parameters are saved in the fields
2000 VAL_BITPOS and VAL_BITSIZE. */
2001 bitpos = b->val_bitpos;
2002 bitsize = b->val_bitsize;
2003 }
a5606eee 2004
42ae5230 2005 addr = value_address (v);
bb9d5f81
PP
2006 if (bitsize != 0)
2007 {
2008 /* Skip the bytes that don't contain the bitfield. */
2009 addr += bitpos / 8;
2010 }
2011
a5606eee 2012 type = hw_write;
3a5c3e22 2013 if (b->base.type == bp_read_watchpoint)
a5606eee 2014 type = hw_read;
3a5c3e22 2015 else if (b->base.type == bp_access_watchpoint)
a5606eee 2016 type = hw_access;
3a5c3e22
PA
2017
2018 loc = allocate_bp_location (&b->base);
2019 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
2020 ;
2021 *tmp = loc;
a6d9a66e 2022 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
2023
2024 loc->pspace = frame_pspace;
a5606eee 2025 loc->address = addr;
bb9d5f81
PP
2026
2027 if (bitsize != 0)
2028 {
2029 /* Just cover the bytes that make up the bitfield. */
2030 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2031 }
2032 else
2033 loc->length = TYPE_LENGTH (value_type (v));
2034
a5606eee
VP
2035 loc->watchpoint_type = type;
2036 }
2037 }
9fa40276
TJB
2038 }
2039
2040 /* Change the type of breakpoint between hardware assisted or
2041 an ordinary watchpoint depending on the hardware support
2042 and free hardware slots. REPARSE is set when the inferior
2043 is started. */
a9634178 2044 if (reparse)
9fa40276 2045 {
e09342b5 2046 int reg_cnt;
9fa40276
TJB
2047 enum bp_loc_type loc_type;
2048 struct bp_location *bl;
a5606eee 2049
a9634178 2050 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
2051
2052 if (reg_cnt)
9fa40276
TJB
2053 {
2054 int i, target_resources_ok, other_type_used;
a1398e0c 2055 enum bptype type;
9fa40276 2056
a9634178
TJB
2057 /* Use an exact watchpoint when there's only one memory region to be
2058 watched, and only one debug register is needed to watch it. */
2059 b->exact = target_exact_watchpoints && reg_cnt == 1;
2060
9fa40276 2061 /* We need to determine how many resources are already
e09342b5
TJB
2062 used for all other hardware watchpoints plus this one
2063 to see if we still have enough resources to also fit
a1398e0c
PA
2064 this watchpoint in as well. */
2065
2066 /* If this is a software watchpoint, we try to turn it
2067 to a hardware one -- count resources as if B was of
2068 hardware watchpoint type. */
2069 type = b->base.type;
2070 if (type == bp_watchpoint)
2071 type = bp_hardware_watchpoint;
2072
2073 /* This watchpoint may or may not have been placed on
2074 the list yet at this point (it won't be in the list
2075 if we're trying to create it for the first time,
2076 through watch_command), so always account for it
2077 manually. */
2078
2079 /* Count resources used by all watchpoints except B. */
2080 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2081
2082 /* Add in the resources needed for B. */
2083 i += hw_watchpoint_use_count (&b->base);
2084
2085 target_resources_ok
2086 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 2087 if (target_resources_ok <= 0)
a9634178 2088 {
3a5c3e22 2089 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
2090
2091 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
2092 error (_("Target does not support this type of "
2093 "hardware watchpoint."));
9c06b0b4
TJB
2094 else if (target_resources_ok < 0 && !sw_mode)
2095 error (_("There are not enough available hardware "
2096 "resources for this watchpoint."));
a1398e0c
PA
2097
2098 /* Downgrade to software watchpoint. */
2099 b->base.type = bp_watchpoint;
2100 }
2101 else
2102 {
2103 /* If this was a software watchpoint, we've just
2104 found we have enough resources to turn it to a
2105 hardware watchpoint. Otherwise, this is a
2106 nop. */
2107 b->base.type = type;
a9634178 2108 }
9fa40276 2109 }
3a5c3e22 2110 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
2111 {
2112 if (!can_use_hw_watchpoints)
2113 error (_("Can't set read/access watchpoint when "
2114 "hardware watchpoints are disabled."));
2115 else
2116 error (_("Expression cannot be implemented with "
2117 "read/access watchpoint."));
2118 }
9fa40276 2119 else
3a5c3e22 2120 b->base.type = bp_watchpoint;
9fa40276 2121
3a5c3e22 2122 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2123 : bp_loc_hardware_watchpoint);
3a5c3e22 2124 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2125 bl->loc_type = loc_type;
2126 }
2127
2128 for (v = val_chain; v; v = next)
2129 {
a5606eee
VP
2130 next = value_next (v);
2131 if (v != b->val)
2132 value_free (v);
2133 }
2134
c7437ca6
PA
2135 /* If a software watchpoint is not watching any memory, then the
2136 above left it without any location set up. But,
2137 bpstat_stop_status requires a location to be able to report
2138 stops, so make sure there's at least a dummy one. */
3a5c3e22 2139 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2140 {
3a5c3e22
PA
2141 struct breakpoint *base = &b->base;
2142 base->loc = allocate_bp_location (base);
2143 base->loc->pspace = frame_pspace;
2144 base->loc->address = -1;
2145 base->loc->length = -1;
2146 base->loc->watchpoint_type = -1;
c7437ca6 2147 }
a5606eee
VP
2148 }
2149 else if (!within_current_scope)
7270d8f2 2150 {
ac74f770
MS
2151 printf_filtered (_("\
2152Watchpoint %d deleted because the program has left the block\n\
2153in which its expression is valid.\n"),
3a5c3e22 2154 b->base.number);
d0fb5eae 2155 watchpoint_del_at_next_stop (b);
7270d8f2 2156 }
a5606eee
VP
2157
2158 /* Restore the selected frame. */
66076460
DJ
2159 if (frame_saved)
2160 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2161}
2162
a5606eee 2163
74960c60 2164/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2165 inserted in the inferior. We don't differentiate the type of BL's owner
2166 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2167 breakpoint_ops is not defined, because in insert_bp_location,
2168 tracepoint's insert_location will not be called. */
74960c60 2169static int
35df4500 2170should_be_inserted (struct bp_location *bl)
74960c60 2171{
35df4500 2172 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2173 return 0;
2174
35df4500 2175 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2176 return 0;
2177
35df4500 2178 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2179 return 0;
2180
f8eba3c6
TT
2181 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2182 return 0;
2183
56710373
PA
2184 /* This is set for example, when we're attached to the parent of a
2185 vfork, and have detached from the child. The child is running
2186 free, and we expect it to do an exec or exit, at which point the
2187 OS makes the parent schedulable again (and the target reports
2188 that the vfork is done). Until the child is done with the shared
2189 memory region, do not insert breakpoints in the parent, otherwise
2190 the child could still trip on the parent's breakpoints. Since
2191 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2192 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2193 return 0;
2194
31e77af2
PA
2195 /* Don't insert a breakpoint if we're trying to step past its
2196 location. */
2197 if ((bl->loc_type == bp_loc_software_breakpoint
2198 || bl->loc_type == bp_loc_hardware_breakpoint)
2199 && stepping_past_instruction_at (bl->pspace->aspace,
2200 bl->address))
e558d7c1
PA
2201 {
2202 if (debug_infrun)
2203 {
2204 fprintf_unfiltered (gdb_stdlog,
2205 "infrun: skipping breakpoint: "
2206 "stepping past insn at: %s\n",
2207 paddress (bl->gdbarch, bl->address));
2208 }
2209 return 0;
2210 }
31e77af2 2211
74960c60
VP
2212 return 1;
2213}
2214
934709f0
PW
2215/* Same as should_be_inserted but does the check assuming
2216 that the location is not duplicated. */
2217
2218static int
2219unduplicated_should_be_inserted (struct bp_location *bl)
2220{
2221 int result;
2222 const int save_duplicate = bl->duplicate;
2223
2224 bl->duplicate = 0;
2225 result = should_be_inserted (bl);
2226 bl->duplicate = save_duplicate;
2227 return result;
2228}
2229
b775012e
LM
2230/* Parses a conditional described by an expression COND into an
2231 agent expression bytecode suitable for evaluation
2232 by the bytecode interpreter. Return NULL if there was
2233 any error during parsing. */
2234
2235static struct agent_expr *
2236parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2237{
2238 struct agent_expr *aexpr = NULL;
b775012e
LM
2239 volatile struct gdb_exception ex;
2240
2241 if (!cond)
2242 return NULL;
2243
2244 /* We don't want to stop processing, so catch any errors
2245 that may show up. */
2246 TRY_CATCH (ex, RETURN_MASK_ERROR)
2247 {
2248 aexpr = gen_eval_for_expr (scope, cond);
2249 }
2250
2251 if (ex.reason < 0)
2252 {
2253 /* If we got here, it means the condition could not be parsed to a valid
2254 bytecode expression and thus can't be evaluated on the target's side.
2255 It's no use iterating through the conditions. */
2256 return NULL;
2257 }
2258
2259 /* We have a valid agent expression. */
2260 return aexpr;
2261}
2262
2263/* Based on location BL, create a list of breakpoint conditions to be
2264 passed on to the target. If we have duplicated locations with different
2265 conditions, we will add such conditions to the list. The idea is that the
2266 target will evaluate the list of conditions and will only notify GDB when
2267 one of them is true. */
2268
2269static void
2270build_target_condition_list (struct bp_location *bl)
2271{
2272 struct bp_location **locp = NULL, **loc2p;
2273 int null_condition_or_parse_error = 0;
2274 int modified = bl->needs_update;
2275 struct bp_location *loc;
2276
8b4f3082
PA
2277 /* Release conditions left over from a previous insert. */
2278 VEC_free (agent_expr_p, bl->target_info.conditions);
2279
b775012e
LM
2280 /* This is only meaningful if the target is
2281 evaluating conditions and if the user has
2282 opted for condition evaluation on the target's
2283 side. */
2284 if (gdb_evaluates_breakpoint_condition_p ()
2285 || !target_supports_evaluation_of_breakpoint_conditions ())
2286 return;
2287
2288 /* Do a first pass to check for locations with no assigned
2289 conditions or conditions that fail to parse to a valid agent expression
2290 bytecode. If any of these happen, then it's no use to send conditions
2291 to the target since this location will always trigger and generate a
2292 response back to GDB. */
2293 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2294 {
2295 loc = (*loc2p);
2296 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2297 {
2298 if (modified)
2299 {
2300 struct agent_expr *aexpr;
2301
2302 /* Re-parse the conditions since something changed. In that
2303 case we already freed the condition bytecodes (see
2304 force_breakpoint_reinsertion). We just
2305 need to parse the condition to bytecodes again. */
2306 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2307 loc->cond_bytecode = aexpr;
2308
2309 /* Check if we managed to parse the conditional expression
2310 correctly. If not, we will not send this condition
2311 to the target. */
2312 if (aexpr)
2313 continue;
2314 }
2315
2316 /* If we have a NULL bytecode expression, it means something
2317 went wrong or we have a null condition expression. */
2318 if (!loc->cond_bytecode)
2319 {
2320 null_condition_or_parse_error = 1;
2321 break;
2322 }
2323 }
2324 }
2325
2326 /* If any of these happened, it means we will have to evaluate the conditions
2327 for the location's address on gdb's side. It is no use keeping bytecodes
2328 for all the other duplicate locations, thus we free all of them here.
2329
2330 This is so we have a finer control over which locations' conditions are
2331 being evaluated by GDB or the remote stub. */
2332 if (null_condition_or_parse_error)
2333 {
2334 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2335 {
2336 loc = (*loc2p);
2337 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2338 {
2339 /* Only go as far as the first NULL bytecode is
2340 located. */
2341 if (!loc->cond_bytecode)
2342 return;
2343
2344 free_agent_expr (loc->cond_bytecode);
2345 loc->cond_bytecode = NULL;
2346 }
2347 }
2348 }
2349
2350 /* No NULL conditions or failed bytecode generation. Build a condition list
2351 for this location's address. */
2352 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2353 {
2354 loc = (*loc2p);
2355 if (loc->cond
2356 && is_breakpoint (loc->owner)
2357 && loc->pspace->num == bl->pspace->num
2358 && loc->owner->enable_state == bp_enabled
2359 && loc->enabled)
2360 /* Add the condition to the vector. This will be used later to send the
2361 conditions to the target. */
2362 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2363 loc->cond_bytecode);
2364 }
2365
2366 return;
2367}
2368
d3ce09f5
SS
2369/* Parses a command described by string CMD into an agent expression
2370 bytecode suitable for evaluation by the bytecode interpreter.
2371 Return NULL if there was any error during parsing. */
2372
2373static struct agent_expr *
2374parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2375{
2376 struct cleanup *old_cleanups = 0;
2377 struct expression *expr, **argvec;
2378 struct agent_expr *aexpr = NULL;
d3ce09f5 2379 volatile struct gdb_exception ex;
bbc13ae3
KS
2380 const char *cmdrest;
2381 const char *format_start, *format_end;
d3ce09f5
SS
2382 struct format_piece *fpieces;
2383 int nargs;
2384 struct gdbarch *gdbarch = get_current_arch ();
2385
2386 if (!cmd)
2387 return NULL;
2388
2389 cmdrest = cmd;
2390
2391 if (*cmdrest == ',')
2392 ++cmdrest;
bbc13ae3 2393 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2394
2395 if (*cmdrest++ != '"')
2396 error (_("No format string following the location"));
2397
2398 format_start = cmdrest;
2399
2400 fpieces = parse_format_string (&cmdrest);
2401
2402 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2403
2404 format_end = cmdrest;
2405
2406 if (*cmdrest++ != '"')
2407 error (_("Bad format string, non-terminated '\"'."));
2408
bbc13ae3 2409 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2410
2411 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2412 error (_("Invalid argument syntax"));
2413
2414 if (*cmdrest == ',')
2415 cmdrest++;
bbc13ae3 2416 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2417
2418 /* For each argument, make an expression. */
2419
2420 argvec = (struct expression **) alloca (strlen (cmd)
2421 * sizeof (struct expression *));
2422
2423 nargs = 0;
2424 while (*cmdrest != '\0')
2425 {
bbc13ae3 2426 const char *cmd1;
d3ce09f5
SS
2427
2428 cmd1 = cmdrest;
2429 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2430 argvec[nargs++] = expr;
2431 cmdrest = cmd1;
2432 if (*cmdrest == ',')
2433 ++cmdrest;
2434 }
2435
2436 /* We don't want to stop processing, so catch any errors
2437 that may show up. */
2438 TRY_CATCH (ex, RETURN_MASK_ERROR)
2439 {
2440 aexpr = gen_printf (scope, gdbarch, 0, 0,
2441 format_start, format_end - format_start,
2442 fpieces, nargs, argvec);
2443 }
2444
752eb8b4
TT
2445 do_cleanups (old_cleanups);
2446
d3ce09f5
SS
2447 if (ex.reason < 0)
2448 {
2449 /* If we got here, it means the command could not be parsed to a valid
2450 bytecode expression and thus can't be evaluated on the target's side.
2451 It's no use iterating through the other commands. */
2452 return NULL;
2453 }
2454
d3ce09f5
SS
2455 /* We have a valid agent expression, return it. */
2456 return aexpr;
2457}
2458
2459/* Based on location BL, create a list of breakpoint commands to be
2460 passed on to the target. If we have duplicated locations with
2461 different commands, we will add any such to the list. */
2462
2463static void
2464build_target_command_list (struct bp_location *bl)
2465{
2466 struct bp_location **locp = NULL, **loc2p;
2467 int null_command_or_parse_error = 0;
2468 int modified = bl->needs_update;
2469 struct bp_location *loc;
2470
8b4f3082
PA
2471 /* Release commands left over from a previous insert. */
2472 VEC_free (agent_expr_p, bl->target_info.tcommands);
2473
41fac0cf 2474 if (!target_can_run_breakpoint_commands ())
d3ce09f5
SS
2475 return;
2476
41fac0cf
PA
2477 /* For now, limit to agent-style dprintf breakpoints. */
2478 if (dprintf_style != dprintf_style_agent)
d3ce09f5
SS
2479 return;
2480
41fac0cf
PA
2481 /* For now, if we have any duplicate location that isn't a dprintf,
2482 don't install the target-side commands, as that would make the
2483 breakpoint not be reported to the core, and we'd lose
2484 control. */
2485 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2486 {
2487 loc = (*loc2p);
2488 if (is_breakpoint (loc->owner)
2489 && loc->pspace->num == bl->pspace->num
2490 && loc->owner->type != bp_dprintf)
2491 return;
2492 }
2493
d3ce09f5
SS
2494 /* Do a first pass to check for locations with no assigned
2495 conditions or conditions that fail to parse to a valid agent expression
2496 bytecode. If any of these happen, then it's no use to send conditions
2497 to the target since this location will always trigger and generate a
2498 response back to GDB. */
2499 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2500 {
2501 loc = (*loc2p);
2502 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2503 {
2504 if (modified)
2505 {
2506 struct agent_expr *aexpr;
2507
2508 /* Re-parse the commands since something changed. In that
2509 case we already freed the command bytecodes (see
2510 force_breakpoint_reinsertion). We just
2511 need to parse the command to bytecodes again. */
2512 aexpr = parse_cmd_to_aexpr (bl->address,
2513 loc->owner->extra_string);
2514 loc->cmd_bytecode = aexpr;
2515
2516 if (!aexpr)
2517 continue;
2518 }
2519
2520 /* If we have a NULL bytecode expression, it means something
2521 went wrong or we have a null command expression. */
2522 if (!loc->cmd_bytecode)
2523 {
2524 null_command_or_parse_error = 1;
2525 break;
2526 }
2527 }
2528 }
2529
2530 /* If anything failed, then we're not doing target-side commands,
2531 and so clean up. */
2532 if (null_command_or_parse_error)
2533 {
2534 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2535 {
2536 loc = (*loc2p);
2537 if (is_breakpoint (loc->owner)
2538 && loc->pspace->num == bl->pspace->num)
2539 {
2540 /* Only go as far as the first NULL bytecode is
2541 located. */
40fb6c5e 2542 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2543 return;
2544
40fb6c5e
HZ
2545 free_agent_expr (loc->cmd_bytecode);
2546 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2547 }
2548 }
2549 }
2550
2551 /* No NULL commands or failed bytecode generation. Build a command list
2552 for this location's address. */
2553 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2554 {
2555 loc = (*loc2p);
2556 if (loc->owner->extra_string
2557 && is_breakpoint (loc->owner)
2558 && loc->pspace->num == bl->pspace->num
2559 && loc->owner->enable_state == bp_enabled
2560 && loc->enabled)
2561 /* Add the command to the vector. This will be used later
2562 to send the commands to the target. */
2563 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2564 loc->cmd_bytecode);
2565 }
2566
2567 bl->target_info.persist = 0;
2568 /* Maybe flag this location as persistent. */
2569 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2570 bl->target_info.persist = 1;
2571}
2572
35df4500
TJB
2573/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2574 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2575 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2576 Returns 0 for success, 1 if the bp_location type is not supported or
2577 -1 for failure.
879bfdc2 2578
4a64f543
MS
2579 NOTE drow/2003-09-09: This routine could be broken down to an
2580 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2581static int
35df4500 2582insert_bp_location (struct bp_location *bl,
26bb91f3 2583 struct ui_file *tmp_error_stream,
3fbb6ffa 2584 int *disabled_breaks,
dd61ec5c
MW
2585 int *hw_breakpoint_error,
2586 int *hw_bp_error_explained_already)
879bfdc2 2587{
0000e5cc
PA
2588 enum errors bp_err = GDB_NO_ERROR;
2589 const char *bp_err_message = NULL;
c90a6fb7 2590 volatile struct gdb_exception e;
879bfdc2 2591
b775012e 2592 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2593 return 0;
2594
35c63cd8
JB
2595 /* Note we don't initialize bl->target_info, as that wipes out
2596 the breakpoint location's shadow_contents if the breakpoint
2597 is still inserted at that location. This in turn breaks
2598 target_read_memory which depends on these buffers when
2599 a memory read is requested at the breakpoint location:
2600 Once the target_info has been wiped, we fail to see that
2601 we have a breakpoint inserted at that address and thus
2602 read the breakpoint instead of returning the data saved in
2603 the breakpoint location's shadow contents. */
0d5ed153 2604 bl->target_info.reqstd_address = bl->address;
35df4500 2605 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2606 bl->target_info.length = bl->length;
8181d85f 2607
b775012e
LM
2608 /* When working with target-side conditions, we must pass all the conditions
2609 for the same breakpoint address down to the target since GDB will not
2610 insert those locations. With a list of breakpoint conditions, the target
2611 can decide when to stop and notify GDB. */
2612
2613 if (is_breakpoint (bl->owner))
2614 {
2615 build_target_condition_list (bl);
d3ce09f5
SS
2616 build_target_command_list (bl);
2617 /* Reset the modification marker. */
b775012e
LM
2618 bl->needs_update = 0;
2619 }
2620
35df4500
TJB
2621 if (bl->loc_type == bp_loc_software_breakpoint
2622 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2623 {
35df4500 2624 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2625 {
2626 /* If the explicitly specified breakpoint type
2627 is not hardware breakpoint, check the memory map to see
2628 if the breakpoint address is in read only memory or not.
4a64f543 2629
765dc015
VP
2630 Two important cases are:
2631 - location type is not hardware breakpoint, memory
2632 is readonly. We change the type of the location to
2633 hardware breakpoint.
4a64f543
MS
2634 - location type is hardware breakpoint, memory is
2635 read-write. This means we've previously made the
2636 location hardware one, but then the memory map changed,
2637 so we undo.
765dc015 2638
4a64f543
MS
2639 When breakpoints are removed, remove_breakpoints will use
2640 location types we've just set here, the only possible
2641 problem is that memory map has changed during running
2642 program, but it's not going to work anyway with current
2643 gdb. */
765dc015 2644 struct mem_region *mr
0d5ed153 2645 = lookup_mem_region (bl->target_info.reqstd_address);
765dc015
VP
2646
2647 if (mr)
2648 {
2649 if (automatic_hardware_breakpoints)
2650 {
765dc015
VP
2651 enum bp_loc_type new_type;
2652
2653 if (mr->attrib.mode != MEM_RW)
2654 new_type = bp_loc_hardware_breakpoint;
2655 else
2656 new_type = bp_loc_software_breakpoint;
2657
35df4500 2658 if (new_type != bl->loc_type)
765dc015
VP
2659 {
2660 static int said = 0;
cc59ec59 2661
35df4500 2662 bl->loc_type = new_type;
765dc015
VP
2663 if (!said)
2664 {
3e43a32a
MS
2665 fprintf_filtered (gdb_stdout,
2666 _("Note: automatically using "
2667 "hardware breakpoints for "
2668 "read-only addresses.\n"));
765dc015
VP
2669 said = 1;
2670 }
2671 }
2672 }
35df4500 2673 else if (bl->loc_type == bp_loc_software_breakpoint
0fec99e8
PA
2674 && mr->attrib.mode != MEM_RW)
2675 {
2676 fprintf_unfiltered (tmp_error_stream,
2677 _("Cannot insert breakpoint %d.\n"
2678 "Cannot set software breakpoint "
2679 "at read-only address %s\n"),
2680 bl->owner->number,
2681 paddress (bl->gdbarch, bl->address));
2682 return 1;
2683 }
765dc015
VP
2684 }
2685 }
2686
879bfdc2
DJ
2687 /* First check to see if we have to handle an overlay. */
2688 if (overlay_debugging == ovly_off
35df4500
TJB
2689 || bl->section == NULL
2690 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2691 {
2692 /* No overlay handling: just set the breakpoint. */
dd61ec5c
MW
2693 TRY_CATCH (e, RETURN_MASK_ALL)
2694 {
0000e5cc
PA
2695 int val;
2696
dd61ec5c 2697 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2698 if (val)
2699 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2700 }
2701 if (e.reason < 0)
2702 {
0000e5cc
PA
2703 bp_err = e.error;
2704 bp_err_message = e.message;
dd61ec5c 2705 }
879bfdc2
DJ
2706 }
2707 else
2708 {
4a64f543 2709 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2710 Shall we set a breakpoint at the LMA? */
2711 if (!overlay_events_enabled)
2712 {
2713 /* Yes -- overlay event support is not active,
2714 so we must try to set a breakpoint at the LMA.
2715 This will not work for a hardware breakpoint. */
35df4500 2716 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2717 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2718 bl->owner->number);
879bfdc2
DJ
2719 else
2720 {
35df4500
TJB
2721 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2722 bl->section);
879bfdc2 2723 /* Set a software (trap) breakpoint at the LMA. */
35df4500 2724 bl->overlay_target_info = bl->target_info;
0d5ed153 2725 bl->overlay_target_info.reqstd_address = addr;
0000e5cc
PA
2726
2727 /* No overlay handling: just set the breakpoint. */
2728 TRY_CATCH (e, RETURN_MASK_ALL)
2729 {
2730 int val;
2731
2732 val = target_insert_breakpoint (bl->gdbarch,
2733 &bl->overlay_target_info);
2734 if (val)
2735 bp_err = GENERIC_ERROR;
2736 }
2737 if (e.reason < 0)
2738 {
2739 bp_err = e.error;
2740 bp_err_message = e.message;
2741 }
2742
2743 if (bp_err != GDB_NO_ERROR)
99361f52 2744 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2745 "Overlay breakpoint %d "
2746 "failed: in ROM?\n",
35df4500 2747 bl->owner->number);
879bfdc2
DJ
2748 }
2749 }
2750 /* Shall we set a breakpoint at the VMA? */
35df4500 2751 if (section_is_mapped (bl->section))
879bfdc2
DJ
2752 {
2753 /* Yes. This overlay section is mapped into memory. */
dd61ec5c
MW
2754 TRY_CATCH (e, RETURN_MASK_ALL)
2755 {
0000e5cc
PA
2756 int val;
2757
dd61ec5c 2758 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2759 if (val)
2760 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2761 }
2762 if (e.reason < 0)
2763 {
0000e5cc
PA
2764 bp_err = e.error;
2765 bp_err_message = e.message;
dd61ec5c 2766 }
879bfdc2
DJ
2767 }
2768 else
2769 {
2770 /* No. This breakpoint will not be inserted.
2771 No error, but do not mark the bp as 'inserted'. */
2772 return 0;
2773 }
2774 }
2775
0000e5cc 2776 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2777 {
2778 /* Can't set the breakpoint. */
0000e5cc
PA
2779
2780 /* In some cases, we might not be able to insert a
2781 breakpoint in a shared library that has already been
2782 removed, but we have not yet processed the shlib unload
2783 event. Unfortunately, some targets that implement
076855f9
PA
2784 breakpoint insertion themselves can't tell why the
2785 breakpoint insertion failed (e.g., the remote target
2786 doesn't define error codes), so we must treat generic
2787 errors as memory errors. */
0000e5cc 2788 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2789 && bl->loc_type == bp_loc_software_breakpoint
08351840 2790 && (solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
2791 || shared_objfile_contains_address_p (bl->pspace,
2792 bl->address)))
879bfdc2 2793 {
4a64f543 2794 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2795 bl->shlib_disabled = 1;
8d3788bd 2796 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2797 if (!*disabled_breaks)
2798 {
2799 fprintf_unfiltered (tmp_error_stream,
2800 "Cannot insert breakpoint %d.\n",
2801 bl->owner->number);
2802 fprintf_unfiltered (tmp_error_stream,
2803 "Temporarily disabling shared "
2804 "library breakpoints:\n");
2805 }
2806 *disabled_breaks = 1;
879bfdc2 2807 fprintf_unfiltered (tmp_error_stream,
35df4500 2808 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2809 return 0;
879bfdc2
DJ
2810 }
2811 else
879bfdc2 2812 {
35df4500 2813 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2814 {
0000e5cc
PA
2815 *hw_breakpoint_error = 1;
2816 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2817 fprintf_unfiltered (tmp_error_stream,
2818 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2819 bl->owner->number, bp_err_message ? ":" : ".\n");
2820 if (bp_err_message != NULL)
2821 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2822 }
2823 else
2824 {
0000e5cc
PA
2825 if (bp_err_message == NULL)
2826 {
2827 char *message
2828 = memory_error_message (TARGET_XFER_E_IO,
2829 bl->gdbarch, bl->address);
2830 struct cleanup *old_chain = make_cleanup (xfree, message);
2831
2832 fprintf_unfiltered (tmp_error_stream,
2833 "Cannot insert breakpoint %d.\n"
2834 "%s\n",
2835 bl->owner->number, message);
2836 do_cleanups (old_chain);
2837 }
2838 else
2839 {
2840 fprintf_unfiltered (tmp_error_stream,
2841 "Cannot insert breakpoint %d: %s\n",
2842 bl->owner->number,
2843 bp_err_message);
2844 }
879bfdc2 2845 }
0000e5cc 2846 return 1;
879bfdc2
DJ
2847
2848 }
2849 }
2850 else
35df4500 2851 bl->inserted = 1;
879bfdc2 2852
0000e5cc 2853 return 0;
879bfdc2
DJ
2854 }
2855
35df4500 2856 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2857 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2858 watchpoints. It's not clear that it's necessary... */
35df4500 2859 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2860 {
0000e5cc
PA
2861 int val;
2862
77b06cd7
TJB
2863 gdb_assert (bl->owner->ops != NULL
2864 && bl->owner->ops->insert_location != NULL);
2865
2866 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2867
2868 /* If trying to set a read-watchpoint, and it turns out it's not
2869 supported, try emulating one with an access watchpoint. */
35df4500 2870 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2871 {
2872 struct bp_location *loc, **loc_temp;
2873
2874 /* But don't try to insert it, if there's already another
2875 hw_access location that would be considered a duplicate
2876 of this one. */
2877 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2878 if (loc != bl
85d721b8 2879 && loc->watchpoint_type == hw_access
35df4500 2880 && watchpoint_locations_match (bl, loc))
85d721b8 2881 {
35df4500
TJB
2882 bl->duplicate = 1;
2883 bl->inserted = 1;
2884 bl->target_info = loc->target_info;
2885 bl->watchpoint_type = hw_access;
85d721b8
PA
2886 val = 0;
2887 break;
2888 }
2889
2890 if (val == 1)
2891 {
77b06cd7
TJB
2892 bl->watchpoint_type = hw_access;
2893 val = bl->owner->ops->insert_location (bl);
2894
2895 if (val)
2896 /* Back to the original value. */
2897 bl->watchpoint_type = hw_read;
85d721b8
PA
2898 }
2899 }
2900
35df4500 2901 bl->inserted = (val == 0);
879bfdc2
DJ
2902 }
2903
35df4500 2904 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2905 {
0000e5cc
PA
2906 int val;
2907
77b06cd7
TJB
2908 gdb_assert (bl->owner->ops != NULL
2909 && bl->owner->ops->insert_location != NULL);
2910
2911 val = bl->owner->ops->insert_location (bl);
2912 if (val)
2913 {
2914 bl->owner->enable_state = bp_disabled;
2915
2916 if (val == 1)
2917 warning (_("\
2918Error inserting catchpoint %d: Your system does not support this type\n\
2919of catchpoint."), bl->owner->number);
2920 else
2921 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2922 }
2923
2924 bl->inserted = (val == 0);
1640b821
DJ
2925
2926 /* We've already printed an error message if there was a problem
2927 inserting this catchpoint, and we've disabled the catchpoint,
2928 so just return success. */
2929 return 0;
879bfdc2
DJ
2930 }
2931
2932 return 0;
2933}
2934
6c95b8df
PA
2935/* This function is called when program space PSPACE is about to be
2936 deleted. It takes care of updating breakpoints to not reference
2937 PSPACE anymore. */
2938
2939void
2940breakpoint_program_space_exit (struct program_space *pspace)
2941{
2942 struct breakpoint *b, *b_temp;
876fa593 2943 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2944
2945 /* Remove any breakpoint that was set through this program space. */
2946 ALL_BREAKPOINTS_SAFE (b, b_temp)
2947 {
2948 if (b->pspace == pspace)
2949 delete_breakpoint (b);
2950 }
2951
2952 /* Breakpoints set through other program spaces could have locations
2953 bound to PSPACE as well. Remove those. */
876fa593 2954 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2955 {
2956 struct bp_location *tmp;
2957
2958 if (loc->pspace == pspace)
2959 {
2bdf28a0 2960 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2961 if (loc->owner->loc == loc)
2962 loc->owner->loc = loc->next;
2963 else
2964 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2965 if (tmp->next == loc)
2966 {
2967 tmp->next = loc->next;
2968 break;
2969 }
2970 }
2971 }
2972
2973 /* Now update the global location list to permanently delete the
2974 removed locations above. */
44702360 2975 update_global_location_list (UGLL_DONT_INSERT);
6c95b8df
PA
2976}
2977
74960c60
VP
2978/* Make sure all breakpoints are inserted in inferior.
2979 Throws exception on any error.
2980 A breakpoint that is already inserted won't be inserted
2981 again, so calling this function twice is safe. */
2982void
2983insert_breakpoints (void)
2984{
2985 struct breakpoint *bpt;
2986
2987 ALL_BREAKPOINTS (bpt)
2988 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2989 {
2990 struct watchpoint *w = (struct watchpoint *) bpt;
2991
2992 update_watchpoint (w, 0 /* don't reparse. */);
2993 }
74960c60 2994
04086b45
PA
2995 /* Updating watchpoints creates new locations, so update the global
2996 location list. Explicitly tell ugll to insert locations and
2997 ignore breakpoints_always_inserted_mode. */
2998 update_global_location_list (UGLL_INSERT);
74960c60
VP
2999}
3000
20388dd6
YQ
3001/* Invoke CALLBACK for each of bp_location. */
3002
3003void
3004iterate_over_bp_locations (walk_bp_location_callback callback)
3005{
3006 struct bp_location *loc, **loc_tmp;
3007
3008 ALL_BP_LOCATIONS (loc, loc_tmp)
3009 {
3010 callback (loc, NULL);
3011 }
3012}
3013
b775012e
LM
3014/* This is used when we need to synch breakpoint conditions between GDB and the
3015 target. It is the case with deleting and disabling of breakpoints when using
3016 always-inserted mode. */
3017
3018static void
3019update_inserted_breakpoint_locations (void)
3020{
3021 struct bp_location *bl, **blp_tmp;
3022 int error_flag = 0;
3023 int val = 0;
3024 int disabled_breaks = 0;
3025 int hw_breakpoint_error = 0;
dd61ec5c 3026 int hw_bp_details_reported = 0;
b775012e
LM
3027
3028 struct ui_file *tmp_error_stream = mem_fileopen ();
3029 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3030
3031 /* Explicitly mark the warning -- this will only be printed if
3032 there was an error. */
3033 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3034
3035 save_current_space_and_thread ();
3036
3037 ALL_BP_LOCATIONS (bl, blp_tmp)
3038 {
3039 /* We only want to update software breakpoints and hardware
3040 breakpoints. */
3041 if (!is_breakpoint (bl->owner))
3042 continue;
3043
3044 /* We only want to update locations that are already inserted
3045 and need updating. This is to avoid unwanted insertion during
3046 deletion of breakpoints. */
3047 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3048 continue;
3049
3050 switch_to_program_space_and_thread (bl->pspace);
3051
3052 /* For targets that support global breakpoints, there's no need
3053 to select an inferior to insert breakpoint to. In fact, even
3054 if we aren't attached to any process yet, we should still
3055 insert breakpoints. */
f5656ead 3056 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
3057 && ptid_equal (inferior_ptid, null_ptid))
3058 continue;
3059
3060 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3061 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
3062 if (val)
3063 error_flag = val;
3064 }
3065
3066 if (error_flag)
3067 {
3068 target_terminal_ours_for_output ();
3069 error_stream (tmp_error_stream);
3070 }
3071
3072 do_cleanups (cleanups);
3073}
3074
c30eee59 3075/* Used when starting or continuing the program. */
c906108c 3076
74960c60
VP
3077static void
3078insert_breakpoint_locations (void)
c906108c 3079{
a5606eee 3080 struct breakpoint *bpt;
35df4500 3081 struct bp_location *bl, **blp_tmp;
eacd795a 3082 int error_flag = 0;
c906108c 3083 int val = 0;
3fbb6ffa 3084 int disabled_breaks = 0;
81d0cc19 3085 int hw_breakpoint_error = 0;
dd61ec5c 3086 int hw_bp_error_explained_already = 0;
c906108c 3087
81d0cc19 3088 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 3089 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 3090
81d0cc19
GS
3091 /* Explicitly mark the warning -- this will only be printed if
3092 there was an error. */
3093 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
3094
3095 save_current_space_and_thread ();
3096
35df4500 3097 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 3098 {
b775012e 3099 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
3100 continue;
3101
4a64f543
MS
3102 /* There is no point inserting thread-specific breakpoints if
3103 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3104 has BL->OWNER always non-NULL. */
35df4500
TJB
3105 if (bl->owner->thread != -1
3106 && !valid_thread_id (bl->owner->thread))
f365de73
AS
3107 continue;
3108
35df4500 3109 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
3110
3111 /* For targets that support global breakpoints, there's no need
3112 to select an inferior to insert breakpoint to. In fact, even
3113 if we aren't attached to any process yet, we should still
3114 insert breakpoints. */
f5656ead 3115 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
3116 && ptid_equal (inferior_ptid, null_ptid))
3117 continue;
3118
3fbb6ffa 3119 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 3120 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 3121 if (val)
eacd795a 3122 error_flag = val;
879bfdc2 3123 }
c906108c 3124
4a64f543
MS
3125 /* If we failed to insert all locations of a watchpoint, remove
3126 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
3127 ALL_BREAKPOINTS (bpt)
3128 {
3129 int some_failed = 0;
3130 struct bp_location *loc;
3131
3132 if (!is_hardware_watchpoint (bpt))
3133 continue;
3134
d6b74ac4 3135 if (!breakpoint_enabled (bpt))
a5606eee 3136 continue;
74960c60
VP
3137
3138 if (bpt->disposition == disp_del_at_next_stop)
3139 continue;
a5606eee
VP
3140
3141 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3142 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3143 {
3144 some_failed = 1;
3145 break;
3146 }
3147 if (some_failed)
3148 {
3149 for (loc = bpt->loc; loc; loc = loc->next)
3150 if (loc->inserted)
3151 remove_breakpoint (loc, mark_uninserted);
3152
3153 hw_breakpoint_error = 1;
3154 fprintf_unfiltered (tmp_error_stream,
3155 "Could not insert hardware watchpoint %d.\n",
3156 bpt->number);
eacd795a 3157 error_flag = -1;
a5606eee
VP
3158 }
3159 }
3160
eacd795a 3161 if (error_flag)
81d0cc19
GS
3162 {
3163 /* If a hardware breakpoint or watchpoint was inserted, add a
3164 message about possibly exhausted resources. */
dd61ec5c 3165 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3166 {
c6510018
MS
3167 fprintf_unfiltered (tmp_error_stream,
3168 "Could not insert hardware breakpoints:\n\
3169You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3170 }
81d0cc19
GS
3171 target_terminal_ours_for_output ();
3172 error_stream (tmp_error_stream);
3173 }
f7545552
TT
3174
3175 do_cleanups (cleanups);
c906108c
SS
3176}
3177
c30eee59
TJB
3178/* Used when the program stops.
3179 Returns zero if successful, or non-zero if there was a problem
3180 removing a breakpoint location. */
3181
c906108c 3182int
fba45db2 3183remove_breakpoints (void)
c906108c 3184{
35df4500 3185 struct bp_location *bl, **blp_tmp;
3a1bae8e 3186 int val = 0;
c906108c 3187
35df4500 3188 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3189 {
1e4d1764 3190 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3191 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3192 }
3a1bae8e 3193 return val;
c906108c
SS
3194}
3195
49fa26b0
PA
3196/* When a thread exits, remove breakpoints that are related to
3197 that thread. */
3198
3199static void
3200remove_threaded_breakpoints (struct thread_info *tp, int silent)
3201{
3202 struct breakpoint *b, *b_tmp;
3203
3204 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3205 {
96181529 3206 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3207 {
3208 b->disposition = disp_del_at_next_stop;
3209
3210 printf_filtered (_("\
46ecd527 3211Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3212 b->number, tp->num);
3213
3214 /* Hide it from the user. */
3215 b->number = 0;
3216 }
3217 }
3218}
3219
6c95b8df
PA
3220/* Remove breakpoints of process PID. */
3221
3222int
3223remove_breakpoints_pid (int pid)
3224{
35df4500 3225 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3226 int val;
3227 struct inferior *inf = find_inferior_pid (pid);
3228
35df4500 3229 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3230 {
35df4500 3231 if (bl->pspace != inf->pspace)
6c95b8df
PA
3232 continue;
3233
d3ce09f5
SS
3234 if (bl->owner->type == bp_dprintf)
3235 continue;
3236
35df4500 3237 if (bl->inserted)
6c95b8df 3238 {
35df4500 3239 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3240 if (val != 0)
3241 return val;
3242 }
3243 }
3244 return 0;
3245}
3246
c906108c 3247int
fba45db2 3248reattach_breakpoints (int pid)
c906108c 3249{
6c95b8df 3250 struct cleanup *old_chain;
35df4500 3251 struct bp_location *bl, **blp_tmp;
c906108c 3252 int val;
86b887df 3253 struct ui_file *tmp_error_stream;
dd61ec5c 3254 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3255 struct inferior *inf;
3256 struct thread_info *tp;
3257
3258 tp = any_live_thread_of_process (pid);
3259 if (tp == NULL)
3260 return 1;
3261
3262 inf = find_inferior_pid (pid);
3263 old_chain = save_inferior_ptid ();
3264
3265 inferior_ptid = tp->ptid;
a4954f26 3266
86b887df 3267 tmp_error_stream = mem_fileopen ();
a4954f26 3268 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3269
35df4500 3270 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3271 {
35df4500 3272 if (bl->pspace != inf->pspace)
6c95b8df
PA
3273 continue;
3274
35df4500 3275 if (bl->inserted)
c5aa993b 3276 {
35df4500 3277 bl->inserted = 0;
dd61ec5c 3278 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3279 if (val != 0)
3280 {
ce696e05 3281 do_cleanups (old_chain);
c5aa993b
JM
3282 return val;
3283 }
3284 }
3285 }
ce696e05 3286 do_cleanups (old_chain);
c906108c
SS
3287 return 0;
3288}
3289
e58b0e63
PA
3290static int internal_breakpoint_number = -1;
3291
84f4c1fe
PM
3292/* Set the breakpoint number of B, depending on the value of INTERNAL.
3293 If INTERNAL is non-zero, the breakpoint number will be populated
3294 from internal_breakpoint_number and that variable decremented.
e5dd4106 3295 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3296 breakpoint_count and that value incremented. Internal breakpoints
3297 do not set the internal var bpnum. */
3298static void
3299set_breakpoint_number (int internal, struct breakpoint *b)
3300{
3301 if (internal)
3302 b->number = internal_breakpoint_number--;
3303 else
3304 {
3305 set_breakpoint_count (breakpoint_count + 1);
3306 b->number = breakpoint_count;
3307 }
3308}
3309
e62c965a 3310static struct breakpoint *
a6d9a66e 3311create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3312 CORE_ADDR address, enum bptype type,
c0a91b2b 3313 const struct breakpoint_ops *ops)
e62c965a 3314{
e62c965a
PP
3315 struct symtab_and_line sal;
3316 struct breakpoint *b;
3317
4a64f543 3318 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3319
3320 sal.pc = address;
3321 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3322 sal.pspace = current_program_space;
e62c965a 3323
06edf0c0 3324 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3325 b->number = internal_breakpoint_number--;
3326 b->disposition = disp_donttouch;
3327
3328 return b;
3329}
3330
17450429
PP
3331static const char *const longjmp_names[] =
3332 {
3333 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3334 };
3335#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3336
3337/* Per-objfile data private to breakpoint.c. */
3338struct breakpoint_objfile_data
3339{
3340 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3341 struct bound_minimal_symbol overlay_msym;
17450429
PP
3342
3343 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3344 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3345
28106bc2
SDJ
3346 /* True if we have looked for longjmp probes. */
3347 int longjmp_searched;
3348
3349 /* SystemTap probe points for longjmp (if any). */
3350 VEC (probe_p) *longjmp_probes;
3351
17450429 3352 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3353 struct bound_minimal_symbol terminate_msym;
17450429
PP
3354
3355 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3356 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3357
3358 /* True if we have looked for exception probes. */
3359 int exception_searched;
3360
3361 /* SystemTap probe points for unwinding (if any). */
3362 VEC (probe_p) *exception_probes;
17450429
PP
3363};
3364
3365static const struct objfile_data *breakpoint_objfile_key;
3366
3367/* Minimal symbol not found sentinel. */
3368static struct minimal_symbol msym_not_found;
3369
3370/* Returns TRUE if MSYM point to the "not found" sentinel. */
3371
3372static int
3373msym_not_found_p (const struct minimal_symbol *msym)
3374{
3375 return msym == &msym_not_found;
3376}
3377
3378/* Return per-objfile data needed by breakpoint.c.
3379 Allocate the data if necessary. */
3380
3381static struct breakpoint_objfile_data *
3382get_breakpoint_objfile_data (struct objfile *objfile)
3383{
3384 struct breakpoint_objfile_data *bp_objfile_data;
3385
3386 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3387 if (bp_objfile_data == NULL)
3388 {
3389 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3390 sizeof (*bp_objfile_data));
3391
3392 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3393 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3394 }
3395 return bp_objfile_data;
3396}
3397
28106bc2
SDJ
3398static void
3399free_breakpoint_probes (struct objfile *obj, void *data)
3400{
3401 struct breakpoint_objfile_data *bp_objfile_data = data;
3402
3403 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3404 VEC_free (probe_p, bp_objfile_data->exception_probes);
3405}
3406
e62c965a 3407static void
af02033e 3408create_overlay_event_breakpoint (void)
e62c965a 3409{
69de3c6a 3410 struct objfile *objfile;
af02033e 3411 const char *const func_name = "_ovly_debug_event";
e62c965a 3412
69de3c6a
PP
3413 ALL_OBJFILES (objfile)
3414 {
3415 struct breakpoint *b;
17450429
PP
3416 struct breakpoint_objfile_data *bp_objfile_data;
3417 CORE_ADDR addr;
69de3c6a 3418
17450429
PP
3419 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3420
3b7344d5 3421 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3422 continue;
3423
3b7344d5 3424 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3425 {
3b7344d5 3426 struct bound_minimal_symbol m;
17450429
PP
3427
3428 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3429 if (m.minsym == NULL)
17450429
PP
3430 {
3431 /* Avoid future lookups in this objfile. */
3b7344d5 3432 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3433 continue;
3434 }
3435 bp_objfile_data->overlay_msym = m;
3436 }
e62c965a 3437
77e371c0 3438 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3439 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3440 bp_overlay_event,
3441 &internal_breakpoint_ops);
69de3c6a 3442 b->addr_string = xstrdup (func_name);
e62c965a 3443
69de3c6a
PP
3444 if (overlay_debugging == ovly_auto)
3445 {
3446 b->enable_state = bp_enabled;
3447 overlay_events_enabled = 1;
3448 }
3449 else
3450 {
3451 b->enable_state = bp_disabled;
3452 overlay_events_enabled = 0;
3453 }
e62c965a 3454 }
44702360 3455 update_global_location_list (UGLL_MAY_INSERT);
e62c965a
PP
3456}
3457
0fd8e87f 3458static void
af02033e 3459create_longjmp_master_breakpoint (void)
0fd8e87f 3460{
6c95b8df 3461 struct program_space *pspace;
6c95b8df
PA
3462 struct cleanup *old_chain;
3463
3464 old_chain = save_current_program_space ();
0fd8e87f 3465
6c95b8df 3466 ALL_PSPACES (pspace)
af02033e
PP
3467 {
3468 struct objfile *objfile;
3469
3470 set_current_program_space (pspace);
3471
3472 ALL_OBJFILES (objfile)
0fd8e87f 3473 {
af02033e
PP
3474 int i;
3475 struct gdbarch *gdbarch;
17450429 3476 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3477
af02033e 3478 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3479
17450429
PP
3480 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3481
28106bc2
SDJ
3482 if (!bp_objfile_data->longjmp_searched)
3483 {
25f9533e
SDJ
3484 VEC (probe_p) *ret;
3485
3486 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3487 if (ret != NULL)
3488 {
3489 /* We are only interested in checking one element. */
3490 struct probe *p = VEC_index (probe_p, ret, 0);
3491
3492 if (!can_evaluate_probe_arguments (p))
3493 {
3494 /* We cannot use the probe interface here, because it does
3495 not know how to evaluate arguments. */
3496 VEC_free (probe_p, ret);
3497 ret = NULL;
3498 }
3499 }
3500 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3501 bp_objfile_data->longjmp_searched = 1;
3502 }
3503
3504 if (bp_objfile_data->longjmp_probes != NULL)
3505 {
3506 int i;
3507 struct probe *probe;
3508 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3509
3510 for (i = 0;
3511 VEC_iterate (probe_p,
3512 bp_objfile_data->longjmp_probes,
3513 i, probe);
3514 ++i)
3515 {
3516 struct breakpoint *b;
3517
729662a5
TT
3518 b = create_internal_breakpoint (gdbarch,
3519 get_probe_address (probe,
3520 objfile),
28106bc2
SDJ
3521 bp_longjmp_master,
3522 &internal_breakpoint_ops);
3523 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3524 b->enable_state = bp_disabled;
3525 }
3526
3527 continue;
3528 }
3529
0569175e
TSD
3530 if (!gdbarch_get_longjmp_target_p (gdbarch))
3531 continue;
3532
17450429 3533 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3534 {
3535 struct breakpoint *b;
af02033e 3536 const char *func_name;
17450429 3537 CORE_ADDR addr;
6c95b8df 3538
3b7344d5 3539 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3540 continue;
0fd8e87f 3541
17450429 3542 func_name = longjmp_names[i];
3b7344d5 3543 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3544 {
3b7344d5 3545 struct bound_minimal_symbol m;
17450429
PP
3546
3547 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3548 if (m.minsym == NULL)
17450429
PP
3549 {
3550 /* Prevent future lookups in this objfile. */
3b7344d5 3551 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3552 continue;
3553 }
3554 bp_objfile_data->longjmp_msym[i] = m;
3555 }
3556
77e371c0 3557 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3558 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3559 &internal_breakpoint_ops);
af02033e
PP
3560 b->addr_string = xstrdup (func_name);
3561 b->enable_state = bp_disabled;
3562 }
0fd8e87f 3563 }
af02033e 3564 }
44702360 3565 update_global_location_list (UGLL_MAY_INSERT);
6c95b8df
PA
3566
3567 do_cleanups (old_chain);
0fd8e87f
UW
3568}
3569
af02033e 3570/* Create a master std::terminate breakpoint. */
aa7d318d 3571static void
af02033e 3572create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3573{
3574 struct program_space *pspace;
aa7d318d 3575 struct cleanup *old_chain;
af02033e 3576 const char *const func_name = "std::terminate()";
aa7d318d
TT
3577
3578 old_chain = save_current_program_space ();
3579
3580 ALL_PSPACES (pspace)
17450429
PP
3581 {
3582 struct objfile *objfile;
3583 CORE_ADDR addr;
3584
3585 set_current_program_space (pspace);
3586
aa7d318d
TT
3587 ALL_OBJFILES (objfile)
3588 {
3589 struct breakpoint *b;
17450429 3590 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3591
17450429 3592 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3593
3b7344d5 3594 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3595 continue;
3596
3b7344d5 3597 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3598 {
3b7344d5 3599 struct bound_minimal_symbol m;
17450429
PP
3600
3601 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3602 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3603 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3604 {
3605 /* Prevent future lookups in this objfile. */
3b7344d5 3606 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3607 continue;
3608 }
3609 bp_objfile_data->terminate_msym = m;
3610 }
aa7d318d 3611
77e371c0 3612 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3613 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3614 bp_std_terminate_master,
3615 &internal_breakpoint_ops);
aa7d318d
TT
3616 b->addr_string = xstrdup (func_name);
3617 b->enable_state = bp_disabled;
3618 }
17450429
PP
3619 }
3620
44702360 3621 update_global_location_list (UGLL_MAY_INSERT);
aa7d318d
TT
3622
3623 do_cleanups (old_chain);
3624}
3625
186c406b
TT
3626/* Install a master breakpoint on the unwinder's debug hook. */
3627
70221824 3628static void
186c406b
TT
3629create_exception_master_breakpoint (void)
3630{
3631 struct objfile *objfile;
17450429 3632 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3633
3634 ALL_OBJFILES (objfile)
3635 {
17450429
PP
3636 struct breakpoint *b;
3637 struct gdbarch *gdbarch;
3638 struct breakpoint_objfile_data *bp_objfile_data;
3639 CORE_ADDR addr;
3640
3641 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3642
28106bc2
SDJ
3643 /* We prefer the SystemTap probe point if it exists. */
3644 if (!bp_objfile_data->exception_searched)
3645 {
25f9533e
SDJ
3646 VEC (probe_p) *ret;
3647
3648 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3649
3650 if (ret != NULL)
3651 {
3652 /* We are only interested in checking one element. */
3653 struct probe *p = VEC_index (probe_p, ret, 0);
3654
3655 if (!can_evaluate_probe_arguments (p))
3656 {
3657 /* We cannot use the probe interface here, because it does
3658 not know how to evaluate arguments. */
3659 VEC_free (probe_p, ret);
3660 ret = NULL;
3661 }
3662 }
3663 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3664 bp_objfile_data->exception_searched = 1;
3665 }
3666
3667 if (bp_objfile_data->exception_probes != NULL)
3668 {
3669 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3670 int i;
3671 struct probe *probe;
3672
3673 for (i = 0;
3674 VEC_iterate (probe_p,
3675 bp_objfile_data->exception_probes,
3676 i, probe);
3677 ++i)
3678 {
3679 struct breakpoint *b;
3680
729662a5
TT
3681 b = create_internal_breakpoint (gdbarch,
3682 get_probe_address (probe,
3683 objfile),
28106bc2
SDJ
3684 bp_exception_master,
3685 &internal_breakpoint_ops);
3686 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3687 b->enable_state = bp_disabled;
3688 }
3689
3690 continue;
3691 }
3692
3693 /* Otherwise, try the hook function. */
3694
3b7344d5 3695 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3696 continue;
3697
3698 gdbarch = get_objfile_arch (objfile);
186c406b 3699
3b7344d5 3700 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3701 {
3b7344d5 3702 struct bound_minimal_symbol debug_hook;
186c406b 3703
17450429 3704 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3705 if (debug_hook.minsym == NULL)
17450429 3706 {
3b7344d5 3707 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3708 continue;
3709 }
3710
3711 bp_objfile_data->exception_msym = debug_hook;
186c406b 3712 }
17450429 3713
77e371c0 3714 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3715 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3716 &current_target);
06edf0c0
PA
3717 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3718 &internal_breakpoint_ops);
17450429
PP
3719 b->addr_string = xstrdup (func_name);
3720 b->enable_state = bp_disabled;
186c406b
TT
3721 }
3722
44702360 3723 update_global_location_list (UGLL_MAY_INSERT);
186c406b
TT
3724}
3725
c906108c 3726void
fba45db2 3727update_breakpoints_after_exec (void)
c906108c 3728{
35df4500 3729 struct breakpoint *b, *b_tmp;
876fa593 3730 struct bp_location *bploc, **bplocp_tmp;
c906108c 3731
25b22b0a
PA
3732 /* We're about to delete breakpoints from GDB's lists. If the
3733 INSERTED flag is true, GDB will try to lift the breakpoints by
3734 writing the breakpoints' "shadow contents" back into memory. The
3735 "shadow contents" are NOT valid after an exec, so GDB should not
3736 do that. Instead, the target is responsible from marking
3737 breakpoints out as soon as it detects an exec. We don't do that
3738 here instead, because there may be other attempts to delete
3739 breakpoints after detecting an exec and before reaching here. */
876fa593 3740 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3741 if (bploc->pspace == current_program_space)
3742 gdb_assert (!bploc->inserted);
c906108c 3743
35df4500 3744 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3745 {
6c95b8df
PA
3746 if (b->pspace != current_program_space)
3747 continue;
3748
4a64f543 3749 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3750 if (b->type == bp_shlib_event)
3751 {
3752 delete_breakpoint (b);
3753 continue;
3754 }
c906108c 3755
4a64f543 3756 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3757 if (b->type == bp_jit_event)
3758 {
3759 delete_breakpoint (b);
3760 continue;
3761 }
3762
1900040c 3763 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3764 as must overlay event and longjmp master breakpoints. */
3765 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3766 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3767 || b->type == bp_exception_master)
c4093a6a
JM
3768 {
3769 delete_breakpoint (b);
3770 continue;
3771 }
3772
4a64f543 3773 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3774 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3775 {
3776 delete_breakpoint (b);
3777 continue;
3778 }
3779
611c83ae
PA
3780 /* Longjmp and longjmp-resume breakpoints are also meaningless
3781 after an exec. */
186c406b 3782 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3783 || b->type == bp_longjmp_call_dummy
186c406b 3784 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3785 {
3786 delete_breakpoint (b);
3787 continue;
3788 }
3789
ce78b96d
JB
3790 if (b->type == bp_catchpoint)
3791 {
3792 /* For now, none of the bp_catchpoint breakpoints need to
3793 do anything at this point. In the future, if some of
3794 the catchpoints need to something, we will need to add
3795 a new method, and call this method from here. */
3796 continue;
3797 }
3798
c5aa993b
JM
3799 /* bp_finish is a special case. The only way we ought to be able
3800 to see one of these when an exec() has happened, is if the user
3801 caught a vfork, and then said "finish". Ordinarily a finish just
3802 carries them to the call-site of the current callee, by setting
3803 a temporary bp there and resuming. But in this case, the finish
3804 will carry them entirely through the vfork & exec.
3805
3806 We don't want to allow a bp_finish to remain inserted now. But
3807 we can't safely delete it, 'cause finish_command has a handle to
3808 the bp on a bpstat, and will later want to delete it. There's a
3809 chance (and I've seen it happen) that if we delete the bp_finish
3810 here, that its storage will get reused by the time finish_command
3811 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3812 We really must allow finish_command to delete a bp_finish.
3813
e5dd4106 3814 In the absence of a general solution for the "how do we know
53a5351d
JM
3815 it's safe to delete something others may have handles to?"
3816 problem, what we'll do here is just uninsert the bp_finish, and
3817 let finish_command delete it.
3818
3819 (We know the bp_finish is "doomed" in the sense that it's
3820 momentary, and will be deleted as soon as finish_command sees
3821 the inferior stopped. So it doesn't matter that the bp's
3822 address is probably bogus in the new a.out, unlike e.g., the
3823 solib breakpoints.) */
c5aa993b 3824
c5aa993b
JM
3825 if (b->type == bp_finish)
3826 {
3827 continue;
3828 }
3829
3830 /* Without a symbolic address, we have little hope of the
3831 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3832 a.out. */
c5aa993b
JM
3833 if (b->addr_string == NULL)
3834 {
3835 delete_breakpoint (b);
3836 continue;
3837 }
c5aa993b 3838 }
c906108c
SS
3839}
3840
3841int
d80ee84f 3842detach_breakpoints (ptid_t ptid)
c906108c 3843{
35df4500 3844 struct bp_location *bl, **blp_tmp;
3a1bae8e 3845 int val = 0;
ce696e05 3846 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3847 struct inferior *inf = current_inferior ();
c5aa993b 3848
dfd4cc63 3849 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3850 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3851
6c95b8df 3852 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3853 inferior_ptid = ptid;
35df4500 3854 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3855 {
35df4500 3856 if (bl->pspace != inf->pspace)
6c95b8df
PA
3857 continue;
3858
bd9673a4
PW
3859 /* This function must physically remove breakpoints locations
3860 from the specified ptid, without modifying the breakpoint
3861 package's state. Locations of type bp_loc_other are only
3862 maintained at GDB side. So, there is no need to remove
3863 these bp_loc_other locations. Moreover, removing these
3864 would modify the breakpoint package's state. */
3865 if (bl->loc_type == bp_loc_other)
3866 continue;
3867
35df4500
TJB
3868 if (bl->inserted)
3869 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3870 }
d03285ec
UW
3871
3872 /* Detach single-step breakpoints as well. */
3873 detach_single_step_breakpoints ();
3874
ce696e05 3875 do_cleanups (old_chain);
3a1bae8e 3876 return val;
c906108c
SS
3877}
3878
35df4500 3879/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3880 Note that this is used to detach breakpoints from a child fork.
3881 When we get here, the child isn't in the inferior list, and neither
3882 do we have objects to represent its address space --- we should
35df4500 3883 *not* look at bl->pspace->aspace here. */
6c95b8df 3884
c906108c 3885static int
35df4500 3886remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3887{
3888 int val;
c5aa993b 3889
35df4500
TJB
3890 /* BL is never in moribund_locations by our callers. */
3891 gdb_assert (bl->owner != NULL);
2bdf28a0 3892
35df4500 3893 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3894 /* Permanent breakpoints cannot be inserted or removed. */
3895 return 0;
3896
74960c60
VP
3897 /* The type of none suggests that owner is actually deleted.
3898 This should not ever happen. */
35df4500 3899 gdb_assert (bl->owner->type != bp_none);
0bde7532 3900
35df4500
TJB
3901 if (bl->loc_type == bp_loc_software_breakpoint
3902 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3903 {
c02f5703
MS
3904 /* "Normal" instruction breakpoint: either the standard
3905 trap-instruction bp (bp_breakpoint), or a
3906 bp_hardware_breakpoint. */
3907
3908 /* First check to see if we have to handle an overlay. */
3909 if (overlay_debugging == ovly_off
35df4500
TJB
3910 || bl->section == NULL
3911 || !(section_is_overlay (bl->section)))
c02f5703
MS
3912 {
3913 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3914
3915 /* If we're trying to uninsert a memory breakpoint that we
3916 know is set in a dynamic object that is marked
3917 shlib_disabled, then either the dynamic object was
3918 removed with "remove-symbol-file" or with
3919 "nosharedlibrary". In the former case, we don't know
3920 whether another dynamic object might have loaded over the
3921 breakpoint's address -- the user might well let us know
3922 about it next with add-symbol-file (the whole point of
d03de421 3923 add-symbol-file is letting the user manually maintain a
08351840
PA
3924 list of dynamically loaded objects). If we have the
3925 breakpoint's shadow memory, that is, this is a software
3926 breakpoint managed by GDB, check whether the breakpoint
3927 is still inserted in memory, to avoid overwriting wrong
3928 code with stale saved shadow contents. Note that HW
3929 breakpoints don't have shadow memory, as they're
3930 implemented using a mechanism that is not dependent on
3931 being able to modify the target's memory, and as such
3932 they should always be removed. */
3933 if (bl->shlib_disabled
3934 && bl->target_info.shadow_len != 0
3935 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3936 val = 0;
3937 else
3938 val = bl->owner->ops->remove_location (bl);
c02f5703 3939 }
c906108c
SS
3940 else
3941 {
4a64f543 3942 /* This breakpoint is in an overlay section.
c02f5703
MS
3943 Did we set a breakpoint at the LMA? */
3944 if (!overlay_events_enabled)
3945 {
3946 /* Yes -- overlay event support is not active, so we
3947 should have set a breakpoint at the LMA. Remove it.
3948 */
c02f5703
MS
3949 /* Ignore any failures: if the LMA is in ROM, we will
3950 have already warned when we failed to insert it. */
35df4500
TJB
3951 if (bl->loc_type == bp_loc_hardware_breakpoint)
3952 target_remove_hw_breakpoint (bl->gdbarch,
3953 &bl->overlay_target_info);
c02f5703 3954 else
35df4500
TJB
3955 target_remove_breakpoint (bl->gdbarch,
3956 &bl->overlay_target_info);
c02f5703
MS
3957 }
3958 /* Did we set a breakpoint at the VMA?
3959 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3960 if (bl->inserted)
c906108c 3961 {
c02f5703
MS
3962 /* Yes -- remove it. Previously we did not bother to
3963 remove the breakpoint if the section had been
3964 unmapped, but let's not rely on that being safe. We
3965 don't know what the overlay manager might do. */
aa67235e
UW
3966
3967 /* However, we should remove *software* breakpoints only
3968 if the section is still mapped, or else we overwrite
3969 wrong code with the saved shadow contents. */
348d480f
PA
3970 if (bl->loc_type == bp_loc_hardware_breakpoint
3971 || section_is_mapped (bl->section))
3972 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3973 else
3974 val = 0;
c906108c 3975 }
c02f5703
MS
3976 else
3977 {
3978 /* No -- not inserted, so no need to remove. No error. */
3979 val = 0;
3980 }
c906108c 3981 }
879d1e6b 3982
08351840
PA
3983 /* In some cases, we might not be able to remove a breakpoint in
3984 a shared library that has already been removed, but we have
3985 not yet processed the shlib unload event. Similarly for an
3986 unloaded add-symbol-file object - the user might not yet have
3987 had the chance to remove-symbol-file it. shlib_disabled will
3988 be set if the library/object has already been removed, but
3989 the breakpoint hasn't been uninserted yet, e.g., after
3990 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3991 always-inserted mode. */
076855f9 3992 if (val
08351840
PA
3993 && (bl->loc_type == bp_loc_software_breakpoint
3994 && (bl->shlib_disabled
3995 || solib_name_from_address (bl->pspace, bl->address)
d03de421
PA
3996 || shared_objfile_contains_address_p (bl->pspace,
3997 bl->address))))
879d1e6b
UW
3998 val = 0;
3999
c906108c
SS
4000 if (val)
4001 return val;
35df4500 4002 bl->inserted = (is == mark_inserted);
c906108c 4003 }
35df4500 4004 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 4005 {
77b06cd7
TJB
4006 gdb_assert (bl->owner->ops != NULL
4007 && bl->owner->ops->remove_location != NULL);
4008
35df4500 4009 bl->inserted = (is == mark_inserted);
77b06cd7 4010 bl->owner->ops->remove_location (bl);
2e70b7b9 4011
c906108c 4012 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 4013 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 4014 warning (_("Could not remove hardware watchpoint %d."),
35df4500 4015 bl->owner->number);
c906108c 4016 }
35df4500
TJB
4017 else if (bl->owner->type == bp_catchpoint
4018 && breakpoint_enabled (bl->owner)
4019 && !bl->duplicate)
ce78b96d 4020 {
77b06cd7
TJB
4021 gdb_assert (bl->owner->ops != NULL
4022 && bl->owner->ops->remove_location != NULL);
ce78b96d 4023
77b06cd7 4024 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
4025 if (val)
4026 return val;
77b06cd7 4027
35df4500 4028 bl->inserted = (is == mark_inserted);
ce78b96d 4029 }
c906108c
SS
4030
4031 return 0;
4032}
4033
6c95b8df 4034static int
35df4500 4035remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
4036{
4037 int ret;
4038 struct cleanup *old_chain;
4039
35df4500
TJB
4040 /* BL is never in moribund_locations by our callers. */
4041 gdb_assert (bl->owner != NULL);
2bdf28a0 4042
35df4500 4043 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
4044 /* Permanent breakpoints cannot be inserted or removed. */
4045 return 0;
4046
4047 /* The type of none suggests that owner is actually deleted.
4048 This should not ever happen. */
35df4500 4049 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
4050
4051 old_chain = save_current_space_and_thread ();
4052
35df4500 4053 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 4054
35df4500 4055 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
4056
4057 do_cleanups (old_chain);
4058 return ret;
4059}
4060
c906108c
SS
4061/* Clear the "inserted" flag in all breakpoints. */
4062
25b22b0a 4063void
fba45db2 4064mark_breakpoints_out (void)
c906108c 4065{
35df4500 4066 struct bp_location *bl, **blp_tmp;
c906108c 4067
35df4500
TJB
4068 ALL_BP_LOCATIONS (bl, blp_tmp)
4069 if (bl->pspace == current_program_space)
4070 bl->inserted = 0;
c906108c
SS
4071}
4072
53a5351d
JM
4073/* Clear the "inserted" flag in all breakpoints and delete any
4074 breakpoints which should go away between runs of the program.
c906108c
SS
4075
4076 Plus other such housekeeping that has to be done for breakpoints
4077 between runs.
4078
53a5351d
JM
4079 Note: this function gets called at the end of a run (by
4080 generic_mourn_inferior) and when a run begins (by
4a64f543 4081 init_wait_for_inferior). */
c906108c
SS
4082
4083
4084
4085void
fba45db2 4086breakpoint_init_inferior (enum inf_context context)
c906108c 4087{
35df4500
TJB
4088 struct breakpoint *b, *b_tmp;
4089 struct bp_location *bl, **blp_tmp;
1c5cfe86 4090 int ix;
6c95b8df 4091 struct program_space *pspace = current_program_space;
c906108c 4092
50c71eaf
PA
4093 /* If breakpoint locations are shared across processes, then there's
4094 nothing to do. */
f5656ead 4095 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
4096 return;
4097
35df4500 4098 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 4099 {
35df4500
TJB
4100 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4101 if (bl->pspace == pspace
4102 && bl->owner->enable_state != bp_permanent)
4103 bl->inserted = 0;
6c95b8df 4104 }
075f6582 4105
35df4500 4106 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 4107 {
6c95b8df
PA
4108 if (b->loc && b->loc->pspace != pspace)
4109 continue;
4110
c5aa993b
JM
4111 switch (b->type)
4112 {
4113 case bp_call_dummy:
e2e4d78b 4114 case bp_longjmp_call_dummy:
c906108c 4115
c5aa993b 4116 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
4117 cause problems when the inferior is rerun, so we better get
4118 rid of it. */
4119
4120 case bp_watchpoint_scope:
4121
4122 /* Also get rid of scope breakpoints. */
4123
4124 case bp_shlib_event:
4125
4126 /* Also remove solib event breakpoints. Their addresses may
4127 have changed since the last time we ran the program.
4128 Actually we may now be debugging against different target;
4129 and so the solib backend that installed this breakpoint may
4130 not be used in by the target. E.g.,
4131
4132 (gdb) file prog-linux
4133 (gdb) run # native linux target
4134 ...
4135 (gdb) kill
4136 (gdb) file prog-win.exe
4137 (gdb) tar rem :9999 # remote Windows gdbserver.
4138 */
c906108c 4139
f59f708a
PA
4140 case bp_step_resume:
4141
4142 /* Also remove step-resume breakpoints. */
4143
c5aa993b
JM
4144 delete_breakpoint (b);
4145 break;
c906108c 4146
c5aa993b
JM
4147 case bp_watchpoint:
4148 case bp_hardware_watchpoint:
4149 case bp_read_watchpoint:
4150 case bp_access_watchpoint:
3a5c3e22
PA
4151 {
4152 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4153
3a5c3e22
PA
4154 /* Likewise for watchpoints on local expressions. */
4155 if (w->exp_valid_block != NULL)
4156 delete_breakpoint (b);
4157 else if (context == inf_starting)
4158 {
4159 /* Reset val field to force reread of starting value in
4160 insert_breakpoints. */
4161 if (w->val)
4162 value_free (w->val);
4163 w->val = NULL;
4164 w->val_valid = 0;
c860120c 4165 }
3a5c3e22 4166 }
c5aa993b
JM
4167 break;
4168 default:
c5aa993b
JM
4169 break;
4170 }
4171 }
1c5cfe86
PA
4172
4173 /* Get rid of the moribund locations. */
35df4500
TJB
4174 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4175 decref_bp_location (&bl);
1c5cfe86 4176 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4177}
4178
6c95b8df
PA
4179/* These functions concern about actual breakpoints inserted in the
4180 target --- to e.g. check if we need to do decr_pc adjustment or if
4181 we need to hop over the bkpt --- so we check for address space
4182 match, not program space. */
4183
c2c6d25f
JM
4184/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4185 exists at PC. It returns ordinary_breakpoint_here if it's an
4186 ordinary breakpoint, or permanent_breakpoint_here if it's a
4187 permanent breakpoint.
4188 - When continuing from a location with an ordinary breakpoint, we
4189 actually single step once before calling insert_breakpoints.
e5dd4106 4190 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4191 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4192 the target, to advance the PC past the breakpoint. */
c906108c 4193
c2c6d25f 4194enum breakpoint_here
6c95b8df 4195breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4196{
35df4500 4197 struct bp_location *bl, **blp_tmp;
c2c6d25f 4198 int any_breakpoint_here = 0;
c906108c 4199
35df4500 4200 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4201 {
35df4500
TJB
4202 if (bl->loc_type != bp_loc_software_breakpoint
4203 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4204 continue;
4205
f1310107 4206 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
4207 if ((breakpoint_enabled (bl->owner)
4208 || bl->owner->enable_state == bp_permanent)
f1310107 4209 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4210 {
4211 if (overlay_debugging
35df4500
TJB
4212 && section_is_overlay (bl->section)
4213 && !section_is_mapped (bl->section))
075f6582 4214 continue; /* unmapped overlay -- can't be a match */
35df4500 4215 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
4216 return permanent_breakpoint_here;
4217 else
4218 any_breakpoint_here = 1;
4219 }
4220 }
c906108c 4221
c2c6d25f 4222 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
4223}
4224
1c5cfe86
PA
4225/* Return true if there's a moribund breakpoint at PC. */
4226
4227int
6c95b8df 4228moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4229{
4230 struct bp_location *loc;
4231 int ix;
4232
4233 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4234 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4235 return 1;
4236
4237 return 0;
4238}
c2c6d25f 4239
c36b740a 4240/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
4241 inserted using regular breakpoint_chain / bp_location array
4242 mechanism. This does not check for single-step breakpoints, which
4243 are inserted and removed using direct target manipulation. */
c906108c
SS
4244
4245int
4a64f543
MS
4246regular_breakpoint_inserted_here_p (struct address_space *aspace,
4247 CORE_ADDR pc)
c906108c 4248{
35df4500 4249 struct bp_location *bl, **blp_tmp;
c906108c 4250
35df4500 4251 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4252 {
35df4500
TJB
4253 if (bl->loc_type != bp_loc_software_breakpoint
4254 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4255 continue;
4256
35df4500 4257 if (bl->inserted
f1310107 4258 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4259 {
4260 if (overlay_debugging
35df4500
TJB
4261 && section_is_overlay (bl->section)
4262 && !section_is_mapped (bl->section))
075f6582
DJ
4263 continue; /* unmapped overlay -- can't be a match */
4264 else
4265 return 1;
4266 }
c5aa993b 4267 }
c36b740a
VP
4268 return 0;
4269}
4270
4271/* Returns non-zero iff there's either regular breakpoint
4272 or a single step breakpoint inserted at PC. */
4273
4274int
6c95b8df 4275breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 4276{
6c95b8df 4277 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 4278 return 1;
c906108c 4279
6c95b8df 4280 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4281 return 1;
4282
c906108c
SS
4283 return 0;
4284}
4285
ef370185
JB
4286/* Ignoring deprecated raw breakpoints, return non-zero iff there is a
4287 software breakpoint inserted at PC. */
4fa8626c 4288
ef370185
JB
4289static struct bp_location *
4290find_non_raw_software_breakpoint_inserted_here (struct address_space *aspace,
4291 CORE_ADDR pc)
4fa8626c 4292{
35df4500 4293 struct bp_location *bl, **blp_tmp;
4fa8626c 4294
35df4500 4295 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 4296 {
35df4500 4297 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4298 continue;
4299
35df4500
TJB
4300 if (bl->inserted
4301 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 4302 aspace, pc))
4fa8626c
DJ
4303 {
4304 if (overlay_debugging
35df4500
TJB
4305 && section_is_overlay (bl->section)
4306 && !section_is_mapped (bl->section))
4fa8626c
DJ
4307 continue; /* unmapped overlay -- can't be a match */
4308 else
ef370185 4309 return bl;
4fa8626c
DJ
4310 }
4311 }
4312
ef370185
JB
4313 return NULL;
4314}
4315
4316/* This function returns non-zero iff there is a software breakpoint
4317 inserted at PC. */
4318
4319int
4320software_breakpoint_inserted_here_p (struct address_space *aspace,
4321 CORE_ADDR pc)
4322{
4323 if (find_non_raw_software_breakpoint_inserted_here (aspace, pc) != NULL)
4324 return 1;
4325
1aafd4da 4326 /* Also check for software single-step breakpoints. */
6c95b8df 4327 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4328 return 1;
4329
4fa8626c
DJ
4330 return 0;
4331}
4332
9093389c
PA
4333int
4334hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4335 CORE_ADDR addr, ULONGEST len)
4336{
4337 struct breakpoint *bpt;
4338
4339 ALL_BREAKPOINTS (bpt)
4340 {
4341 struct bp_location *loc;
4342
4343 if (bpt->type != bp_hardware_watchpoint
4344 && bpt->type != bp_access_watchpoint)
4345 continue;
4346
4347 if (!breakpoint_enabled (bpt))
4348 continue;
4349
4350 for (loc = bpt->loc; loc; loc = loc->next)
4351 if (loc->pspace->aspace == aspace && loc->inserted)
4352 {
4353 CORE_ADDR l, h;
4354
4355 /* Check for intersection. */
4356 l = max (loc->address, addr);
4357 h = min (loc->address + loc->length, addr + len);
4358 if (l < h)
4359 return 1;
4360 }
4361 }
4362 return 0;
4363}
4364
075f6582
DJ
4365/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4366 PC is valid for process/thread PTID. */
c906108c
SS
4367
4368int
6c95b8df
PA
4369breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4370 ptid_t ptid)
c906108c 4371{
35df4500 4372 struct bp_location *bl, **blp_tmp;
4a306c9a 4373 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 4374 int thread = -1;
4a306c9a 4375 int task = 0;
a6f1cd96 4376
35df4500 4377 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4378 {
35df4500
TJB
4379 if (bl->loc_type != bp_loc_software_breakpoint
4380 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4381 continue;
4382
35df4500
TJB
4383 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
4384 if (!breakpoint_enabled (bl->owner)
4385 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
4386 continue;
4387
f1310107 4388 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
4389 continue;
4390
35df4500 4391 if (bl->owner->thread != -1)
075f6582 4392 {
a6f1cd96
JB
4393 /* This is a thread-specific breakpoint. Check that ptid
4394 matches that thread. If thread hasn't been computed yet,
4395 it is now time to do so. */
4396 if (thread == -1)
4397 thread = pid_to_thread_id (ptid);
35df4500 4398 if (bl->owner->thread != thread)
a6f1cd96 4399 continue;
075f6582 4400 }
a6f1cd96 4401
35df4500 4402 if (bl->owner->task != 0)
4a306c9a
JB
4403 {
4404 /* This is a task-specific breakpoint. Check that ptid
4405 matches that task. If task hasn't been computed yet,
4406 it is now time to do so. */
4407 if (task == 0)
4408 task = ada_get_task_number (ptid);
35df4500 4409 if (bl->owner->task != task)
4a306c9a
JB
4410 continue;
4411 }
4412
a6f1cd96 4413 if (overlay_debugging
35df4500
TJB
4414 && section_is_overlay (bl->section)
4415 && !section_is_mapped (bl->section))
a6f1cd96
JB
4416 continue; /* unmapped overlay -- can't be a match */
4417
4418 return 1;
c5aa993b 4419 }
c906108c
SS
4420
4421 return 0;
4422}
c906108c 4423\f
c5aa993b 4424
c906108c
SS
4425/* bpstat stuff. External routines' interfaces are documented
4426 in breakpoint.h. */
4427
4428int
c326b90e 4429is_catchpoint (struct breakpoint *ep)
c906108c 4430{
533be4dd 4431 return (ep->type == bp_catchpoint);
c906108c
SS
4432}
4433
f431efe5
PA
4434/* Frees any storage that is part of a bpstat. Does not walk the
4435 'next' chain. */
4436
4437static void
198757a8
VP
4438bpstat_free (bpstat bs)
4439{
4440 if (bs->old_val != NULL)
4441 value_free (bs->old_val);
9add0f1b 4442 decref_counted_command_line (&bs->commands);
f431efe5 4443 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4444 xfree (bs);
4445}
4446
c906108c
SS
4447/* Clear a bpstat so that it says we are not at any breakpoint.
4448 Also free any storage that is part of a bpstat. */
4449
4450void
fba45db2 4451bpstat_clear (bpstat *bsp)
c906108c
SS
4452{
4453 bpstat p;
4454 bpstat q;
4455
4456 if (bsp == 0)
4457 return;
4458 p = *bsp;
4459 while (p != NULL)
4460 {
4461 q = p->next;
198757a8 4462 bpstat_free (p);
c906108c
SS
4463 p = q;
4464 }
4465 *bsp = NULL;
4466}
4467
4468/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4469 is part of the bpstat is copied as well. */
4470
4471bpstat
fba45db2 4472bpstat_copy (bpstat bs)
c906108c
SS
4473{
4474 bpstat p = NULL;
4475 bpstat tmp;
4476 bpstat retval = NULL;
4477
4478 if (bs == NULL)
4479 return bs;
4480
4481 for (; bs != NULL; bs = bs->next)
4482 {
4483 tmp = (bpstat) xmalloc (sizeof (*tmp));
4484 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4485 incref_counted_command_line (tmp->commands);
f431efe5 4486 incref_bp_location (tmp->bp_location_at);
31cc81e9 4487 if (bs->old_val != NULL)
3c3185ac
JK
4488 {
4489 tmp->old_val = value_copy (bs->old_val);
4490 release_value (tmp->old_val);
4491 }
31cc81e9 4492
c906108c
SS
4493 if (p == NULL)
4494 /* This is the first thing in the chain. */
4495 retval = tmp;
4496 else
4497 p->next = tmp;
4498 p = tmp;
4499 }
4500 p->next = NULL;
4501 return retval;
4502}
4503
4a64f543 4504/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4505
4506bpstat
fba45db2 4507bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4508{
c5aa993b
JM
4509 if (bsp == NULL)
4510 return NULL;
c906108c 4511
c5aa993b
JM
4512 for (; bsp != NULL; bsp = bsp->next)
4513 {
f431efe5 4514 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4515 return bsp;
4516 }
c906108c
SS
4517 return NULL;
4518}
4519
ab04a2af
TT
4520/* See breakpoint.h. */
4521
47591c29 4522int
427cd150 4523bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4524{
ab04a2af
TT
4525 for (; bsp != NULL; bsp = bsp->next)
4526 {
427cd150
TT
4527 if (bsp->breakpoint_at == NULL)
4528 {
4529 /* A moribund location can never explain a signal other than
4530 GDB_SIGNAL_TRAP. */
4531 if (sig == GDB_SIGNAL_TRAP)
47591c29 4532 return 1;
427cd150
TT
4533 }
4534 else
47591c29
PA
4535 {
4536 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4537 sig))
4538 return 1;
4539 }
ab04a2af
TT
4540 }
4541
47591c29 4542 return 0;
ab04a2af
TT
4543}
4544
4a64f543
MS
4545/* Put in *NUM the breakpoint number of the first breakpoint we are
4546 stopped at. *BSP upon return is a bpstat which points to the
4547 remaining breakpoints stopped at (but which is not guaranteed to be
4548 good for anything but further calls to bpstat_num).
4549
8671a17b
PA
4550 Return 0 if passed a bpstat which does not indicate any breakpoints.
4551 Return -1 if stopped at a breakpoint that has been deleted since
4552 we set it.
4553 Return 1 otherwise. */
c906108c
SS
4554
4555int
8671a17b 4556bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4557{
4558 struct breakpoint *b;
4559
4560 if ((*bsp) == NULL)
4561 return 0; /* No more breakpoint values */
8671a17b 4562
4a64f543
MS
4563 /* We assume we'll never have several bpstats that correspond to a
4564 single breakpoint -- otherwise, this function might return the
4565 same number more than once and this will look ugly. */
f431efe5 4566 b = (*bsp)->breakpoint_at;
8671a17b
PA
4567 *bsp = (*bsp)->next;
4568 if (b == NULL)
4569 return -1; /* breakpoint that's been deleted since */
4570
4571 *num = b->number; /* We have its number */
4572 return 1;
c906108c
SS
4573}
4574
e93ca019 4575/* See breakpoint.h. */
c906108c
SS
4576
4577void
e93ca019 4578bpstat_clear_actions (void)
c906108c 4579{
e93ca019
JK
4580 struct thread_info *tp;
4581 bpstat bs;
4582
4583 if (ptid_equal (inferior_ptid, null_ptid))
4584 return;
4585
4586 tp = find_thread_ptid (inferior_ptid);
4587 if (tp == NULL)
4588 return;
4589
4590 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4591 {
9add0f1b 4592 decref_counted_command_line (&bs->commands);
abf85f46 4593
c906108c
SS
4594 if (bs->old_val != NULL)
4595 {
4596 value_free (bs->old_val);
4597 bs->old_val = NULL;
4598 }
4599 }
4600}
4601
f3b1572e
PA
4602/* Called when a command is about to proceed the inferior. */
4603
4604static void
4605breakpoint_about_to_proceed (void)
4606{
4607 if (!ptid_equal (inferior_ptid, null_ptid))
4608 {
4609 struct thread_info *tp = inferior_thread ();
4610
4611 /* Allow inferior function calls in breakpoint commands to not
4612 interrupt the command list. When the call finishes
4613 successfully, the inferior will be standing at the same
4614 breakpoint as if nothing happened. */
16c381f0 4615 if (tp->control.in_infcall)
f3b1572e
PA
4616 return;
4617 }
4618
4619 breakpoint_proceeded = 1;
4620}
4621
4a64f543
MS
4622/* Stub for cleaning up our state if we error-out of a breakpoint
4623 command. */
c906108c 4624static void
4efb68b1 4625cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4626{
4627 executing_breakpoint_commands = 0;
4628}
4629
abf85f46
JK
4630/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4631 or its equivalent. */
4632
4633static int
4634command_line_is_silent (struct command_line *cmd)
4635{
4636 return cmd && (strcmp ("silent", cmd->line) == 0
4637 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4638}
4639
4a64f543
MS
4640/* Execute all the commands associated with all the breakpoints at
4641 this location. Any of these commands could cause the process to
4642 proceed beyond this point, etc. We look out for such changes by
4643 checking the global "breakpoint_proceeded" after each command.
c906108c 4644
347bddb7
PA
4645 Returns true if a breakpoint command resumed the inferior. In that
4646 case, it is the caller's responsibility to recall it again with the
4647 bpstat of the current thread. */
4648
4649static int
4650bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4651{
4652 bpstat bs;
4653 struct cleanup *old_chain;
347bddb7 4654 int again = 0;
c906108c
SS
4655
4656 /* Avoid endless recursion if a `source' command is contained
4657 in bs->commands. */
4658 if (executing_breakpoint_commands)
347bddb7 4659 return 0;
c906108c
SS
4660
4661 executing_breakpoint_commands = 1;
4662 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4663
cf6c5ffb
TT
4664 prevent_dont_repeat ();
4665
4a64f543 4666 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4667 bs = *bsp;
4668
4669 breakpoint_proceeded = 0;
4670 for (; bs != NULL; bs = bs->next)
4671 {
9add0f1b 4672 struct counted_command_line *ccmd;
6c50ab1c
JB
4673 struct command_line *cmd;
4674 struct cleanup *this_cmd_tree_chain;
4675
4676 /* Take ownership of the BSP's command tree, if it has one.
4677
4678 The command tree could legitimately contain commands like
4679 'step' and 'next', which call clear_proceed_status, which
4680 frees stop_bpstat's command tree. To make sure this doesn't
4681 free the tree we're executing out from under us, we need to
4682 take ownership of the tree ourselves. Since a given bpstat's
4683 commands are only executed once, we don't need to copy it; we
4684 can clear the pointer in the bpstat, and make sure we free
4685 the tree when we're done. */
9add0f1b
TT
4686 ccmd = bs->commands;
4687 bs->commands = NULL;
abf85f46
JK
4688 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4689 cmd = ccmd ? ccmd->commands : NULL;
4690 if (command_line_is_silent (cmd))
4691 {
4692 /* The action has been already done by bpstat_stop_status. */
4693 cmd = cmd->next;
4694 }
6c50ab1c 4695
c906108c
SS
4696 while (cmd != NULL)
4697 {
4698 execute_control_command (cmd);
4699
4700 if (breakpoint_proceeded)
4701 break;
4702 else
4703 cmd = cmd->next;
4704 }
6c50ab1c
JB
4705
4706 /* We can free this command tree now. */
4707 do_cleanups (this_cmd_tree_chain);
4708
c906108c 4709 if (breakpoint_proceeded)
32c1e744
VP
4710 {
4711 if (target_can_async_p ())
347bddb7
PA
4712 /* If we are in async mode, then the target might be still
4713 running, not stopped at any breakpoint, so nothing for
4714 us to do here -- just return to the event loop. */
4715 ;
32c1e744
VP
4716 else
4717 /* In sync mode, when execute_control_command returns
4718 we're already standing on the next breakpoint.
347bddb7
PA
4719 Breakpoint commands for that stop were not run, since
4720 execute_command does not run breakpoint commands --
4721 only command_line_handler does, but that one is not
4722 involved in execution of breakpoint commands. So, we
4723 can now execute breakpoint commands. It should be
4724 noted that making execute_command do bpstat actions is
4725 not an option -- in this case we'll have recursive
4726 invocation of bpstat for each breakpoint with a
4727 command, and can easily blow up GDB stack. Instead, we
4728 return true, which will trigger the caller to recall us
4729 with the new stop_bpstat. */
4730 again = 1;
4731 break;
32c1e744 4732 }
c906108c 4733 }
c2b8ed2c 4734 do_cleanups (old_chain);
347bddb7
PA
4735 return again;
4736}
4737
4738void
4739bpstat_do_actions (void)
4740{
353d1d73
JK
4741 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4742
347bddb7
PA
4743 /* Do any commands attached to breakpoint we are stopped at. */
4744 while (!ptid_equal (inferior_ptid, null_ptid)
4745 && target_has_execution
4746 && !is_exited (inferior_ptid)
4747 && !is_executing (inferior_ptid))
4748 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4749 and only return when it is stopped at the next breakpoint, we
4750 keep doing breakpoint actions until it returns false to
4751 indicate the inferior was not resumed. */
16c381f0 4752 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4753 break;
353d1d73
JK
4754
4755 discard_cleanups (cleanup_if_error);
c906108c
SS
4756}
4757
fa4727a6
DJ
4758/* Print out the (old or new) value associated with a watchpoint. */
4759
4760static void
4761watchpoint_value_print (struct value *val, struct ui_file *stream)
4762{
4763 if (val == NULL)
4764 fprintf_unfiltered (stream, _("<unreadable>"));
4765 else
79a45b7d
TT
4766 {
4767 struct value_print_options opts;
4768 get_user_print_options (&opts);
4769 value_print (val, stream, &opts);
4770 }
fa4727a6
DJ
4771}
4772
e514a9d6 4773/* Generic routine for printing messages indicating why we
4a64f543 4774 stopped. The behavior of this function depends on the value
e514a9d6
JM
4775 'print_it' in the bpstat structure. Under some circumstances we
4776 may decide not to print anything here and delegate the task to
4a64f543 4777 normal_stop(). */
e514a9d6
JM
4778
4779static enum print_stop_action
4780print_bp_stop_message (bpstat bs)
4781{
4782 switch (bs->print_it)
4783 {
4784 case print_it_noop:
4a64f543 4785 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4786 return PRINT_UNKNOWN;
4787 break;
4788
4789 case print_it_done:
4790 /* We still want to print the frame, but we already printed the
4a64f543 4791 relevant messages. */
e514a9d6
JM
4792 return PRINT_SRC_AND_LOC;
4793 break;
4794
4795 case print_it_normal:
4f8d1dc6 4796 {
f431efe5
PA
4797 struct breakpoint *b = bs->breakpoint_at;
4798
1a6a67de
TJB
4799 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4800 which has since been deleted. */
4801 if (b == NULL)
4802 return PRINT_UNKNOWN;
4803
348d480f
PA
4804 /* Normal case. Call the breakpoint's print_it method. */
4805 return b->ops->print_it (bs);
4f8d1dc6 4806 }
348d480f 4807 break;
3086aeae 4808
e514a9d6 4809 default:
8e65ff28 4810 internal_error (__FILE__, __LINE__,
e2e0b3e5 4811 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4812 break;
c906108c 4813 }
c906108c
SS
4814}
4815
edcc5120
TT
4816/* A helper function that prints a shared library stopped event. */
4817
4818static void
4819print_solib_event (int is_catchpoint)
4820{
4821 int any_deleted
4822 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4823 int any_added
4824 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4825
4826 if (!is_catchpoint)
4827 {
4828 if (any_added || any_deleted)
4829 ui_out_text (current_uiout,
4830 _("Stopped due to shared library event:\n"));
4831 else
4832 ui_out_text (current_uiout,
4833 _("Stopped due to shared library event (no "
4834 "libraries added or removed)\n"));
4835 }
4836
4837 if (ui_out_is_mi_like_p (current_uiout))
4838 ui_out_field_string (current_uiout, "reason",
4839 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4840
4841 if (any_deleted)
4842 {
4843 struct cleanup *cleanup;
4844 char *name;
4845 int ix;
4846
4847 ui_out_text (current_uiout, _(" Inferior unloaded "));
4848 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4849 "removed");
4850 for (ix = 0;
4851 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4852 ix, name);
4853 ++ix)
4854 {
4855 if (ix > 0)
4856 ui_out_text (current_uiout, " ");
4857 ui_out_field_string (current_uiout, "library", name);
4858 ui_out_text (current_uiout, "\n");
4859 }
4860
4861 do_cleanups (cleanup);
4862 }
4863
4864 if (any_added)
4865 {
4866 struct so_list *iter;
4867 int ix;
4868 struct cleanup *cleanup;
4869
4870 ui_out_text (current_uiout, _(" Inferior loaded "));
4871 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4872 "added");
4873 for (ix = 0;
4874 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4875 ix, iter);
4876 ++ix)
4877 {
4878 if (ix > 0)
4879 ui_out_text (current_uiout, " ");
4880 ui_out_field_string (current_uiout, "library", iter->so_name);
4881 ui_out_text (current_uiout, "\n");
4882 }
4883
4884 do_cleanups (cleanup);
4885 }
4886}
4887
e514a9d6
JM
4888/* Print a message indicating what happened. This is called from
4889 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4890 list - a list of the eventpoints that caused this stop. KIND is
4891 the target_waitkind for the stopping event. This
e514a9d6
JM
4892 routine calls the generic print routine for printing a message
4893 about reasons for stopping. This will print (for example) the
4894 "Breakpoint n," part of the output. The return value of this
4895 routine is one of:
c906108c 4896
4a64f543 4897 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4898 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4899 code to print the location. An example is
c5aa993b
JM
4900 "Breakpoint 1, " which should be followed by
4901 the location.
917317f4 4902 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4903 to also print the location part of the message.
4904 An example is the catch/throw messages, which
4a64f543 4905 don't require a location appended to the end.
917317f4 4906 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4907 further info to be printed. */
c906108c 4908
917317f4 4909enum print_stop_action
36dfb11c 4910bpstat_print (bpstat bs, int kind)
c906108c
SS
4911{
4912 int val;
c5aa993b 4913
c906108c 4914 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4915 (Currently all watchpoints go on the bpstat whether hit or not.
4916 That probably could (should) be changed, provided care is taken
c906108c 4917 with respect to bpstat_explains_signal). */
e514a9d6
JM
4918 for (; bs; bs = bs->next)
4919 {
4920 val = print_bp_stop_message (bs);
4921 if (val == PRINT_SRC_ONLY
4922 || val == PRINT_SRC_AND_LOC
4923 || val == PRINT_NOTHING)
4924 return val;
4925 }
c906108c 4926
36dfb11c
TT
4927 /* If we had hit a shared library event breakpoint,
4928 print_bp_stop_message would print out this message. If we hit an
4929 OS-level shared library event, do the same thing. */
4930 if (kind == TARGET_WAITKIND_LOADED)
4931 {
edcc5120 4932 print_solib_event (0);
36dfb11c
TT
4933 return PRINT_NOTHING;
4934 }
4935
e514a9d6 4936 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4937 with and nothing was printed. */
917317f4 4938 return PRINT_UNKNOWN;
c906108c
SS
4939}
4940
c42bd95a
DE
4941/* Evaluate the expression EXP and return 1 if value is zero.
4942 This returns the inverse of the condition because it is called
4943 from catch_errors which returns 0 if an exception happened, and if an
4944 exception happens we want execution to stop.
4a64f543 4945 The argument is a "struct expression *" that has been cast to a
c42bd95a 4946 "void *" to make it pass through catch_errors. */
c906108c
SS
4947
4948static int
4efb68b1 4949breakpoint_cond_eval (void *exp)
c906108c 4950{
278cd55f 4951 struct value *mark = value_mark ();
c5aa993b 4952 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4953
c906108c
SS
4954 value_free_to_mark (mark);
4955 return i;
4956}
4957
5760d0ab 4958/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4959
4960static bpstat
5760d0ab 4961bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4962{
4963 bpstat bs;
4964
4965 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4966 bs->next = NULL;
4967 **bs_link_pointer = bs;
4968 *bs_link_pointer = &bs->next;
f431efe5
PA
4969 bs->breakpoint_at = bl->owner;
4970 bs->bp_location_at = bl;
4971 incref_bp_location (bl);
c906108c
SS
4972 /* If the condition is false, etc., don't do the commands. */
4973 bs->commands = NULL;
4974 bs->old_val = NULL;
4975 bs->print_it = print_it_normal;
4976 return bs;
4977}
4978\f
d983da9c
DJ
4979/* The target has stopped with waitstatus WS. Check if any hardware
4980 watchpoints have triggered, according to the target. */
4981
4982int
4983watchpoints_triggered (struct target_waitstatus *ws)
4984{
d92524f1 4985 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4986 CORE_ADDR addr;
4987 struct breakpoint *b;
4988
4989 if (!stopped_by_watchpoint)
4990 {
4991 /* We were not stopped by a watchpoint. Mark all watchpoints
4992 as not triggered. */
4993 ALL_BREAKPOINTS (b)
cc60f2e3 4994 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4995 {
4996 struct watchpoint *w = (struct watchpoint *) b;
4997
4998 w->watchpoint_triggered = watch_triggered_no;
4999 }
d983da9c
DJ
5000
5001 return 0;
5002 }
5003
5004 if (!target_stopped_data_address (&current_target, &addr))
5005 {
5006 /* We were stopped by a watchpoint, but we don't know where.
5007 Mark all watchpoints as unknown. */
5008 ALL_BREAKPOINTS (b)
cc60f2e3 5009 if (is_hardware_watchpoint (b))
3a5c3e22
PA
5010 {
5011 struct watchpoint *w = (struct watchpoint *) b;
5012
5013 w->watchpoint_triggered = watch_triggered_unknown;
5014 }
d983da9c 5015
3c4797ba 5016 return 1;
d983da9c
DJ
5017 }
5018
5019 /* The target could report the data address. Mark watchpoints
5020 affected by this data address as triggered, and all others as not
5021 triggered. */
5022
5023 ALL_BREAKPOINTS (b)
cc60f2e3 5024 if (is_hardware_watchpoint (b))
d983da9c 5025 {
3a5c3e22 5026 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 5027 struct bp_location *loc;
d983da9c 5028
3a5c3e22 5029 w->watchpoint_triggered = watch_triggered_no;
a5606eee 5030 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 5031 {
3a5c3e22 5032 if (is_masked_watchpoint (b))
9c06b0b4 5033 {
3a5c3e22
PA
5034 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5035 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
5036
5037 if (newaddr == start)
5038 {
3a5c3e22 5039 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5040 break;
5041 }
5042 }
5043 /* Exact match not required. Within range is sufficient. */
5044 else if (target_watchpoint_addr_within_range (&current_target,
5045 addr, loc->address,
5046 loc->length))
5047 {
3a5c3e22 5048 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
5049 break;
5050 }
5051 }
d983da9c
DJ
5052 }
5053
5054 return 1;
5055}
5056
c906108c
SS
5057/* Possible return values for watchpoint_check (this can't be an enum
5058 because of check_errors). */
5059/* The watchpoint has been deleted. */
5060#define WP_DELETED 1
5061/* The value has changed. */
5062#define WP_VALUE_CHANGED 2
5063/* The value has not changed. */
5064#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
5065/* Ignore this watchpoint, no matter if the value changed or not. */
5066#define WP_IGNORE 4
c906108c
SS
5067
5068#define BP_TEMPFLAG 1
5069#define BP_HARDWAREFLAG 2
5070
4a64f543
MS
5071/* Evaluate watchpoint condition expression and check if its value
5072 changed.
553e4c11
JB
5073
5074 P should be a pointer to struct bpstat, but is defined as a void *
5075 in order for this function to be usable with catch_errors. */
c906108c
SS
5076
5077static int
4efb68b1 5078watchpoint_check (void *p)
c906108c
SS
5079{
5080 bpstat bs = (bpstat) p;
3a5c3e22 5081 struct watchpoint *b;
c906108c
SS
5082 struct frame_info *fr;
5083 int within_current_scope;
5084
f431efe5 5085 /* BS is built from an existing struct breakpoint. */
2bdf28a0 5086 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 5087 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 5088
f6bc2008
PA
5089 /* If this is a local watchpoint, we only want to check if the
5090 watchpoint frame is in scope if the current thread is the thread
5091 that was used to create the watchpoint. */
5092 if (!watchpoint_in_thread_scope (b))
60e1c644 5093 return WP_IGNORE;
f6bc2008 5094
c906108c
SS
5095 if (b->exp_valid_block == NULL)
5096 within_current_scope = 1;
5097 else
5098 {
edb3359d
DJ
5099 struct frame_info *frame = get_current_frame ();
5100 struct gdbarch *frame_arch = get_frame_arch (frame);
5101 CORE_ADDR frame_pc = get_frame_pc (frame);
5102
4a64f543
MS
5103 /* in_function_epilogue_p() returns a non-zero value if we're
5104 still in the function but the stack frame has already been
5105 invalidated. Since we can't rely on the values of local
5106 variables after the stack has been destroyed, we are treating
5107 the watchpoint in that state as `not changed' without further
5108 checking. Don't mark watchpoints as changed if the current
5109 frame is in an epilogue - even if they are in some other
5110 frame, our view of the stack is likely to be wrong and
5111 frame_find_by_id could error out. */
a0f49112 5112 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 5113 return WP_IGNORE;
a0f49112 5114
101dcfbe 5115 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 5116 within_current_scope = (fr != NULL);
69fbadd5
DJ
5117
5118 /* If we've gotten confused in the unwinder, we might have
5119 returned a frame that can't describe this variable. */
edb3359d
DJ
5120 if (within_current_scope)
5121 {
5122 struct symbol *function;
5123
5124 function = get_frame_function (fr);
5125 if (function == NULL
5126 || !contained_in (b->exp_valid_block,
5127 SYMBOL_BLOCK_VALUE (function)))
5128 within_current_scope = 0;
5129 }
69fbadd5 5130
edb3359d 5131 if (within_current_scope)
c906108c
SS
5132 /* If we end up stopping, the current frame will get selected
5133 in normal_stop. So this call to select_frame won't affect
5134 the user. */
0f7d239c 5135 select_frame (fr);
c906108c 5136 }
c5aa993b 5137
c906108c
SS
5138 if (within_current_scope)
5139 {
4a64f543
MS
5140 /* We use value_{,free_to_}mark because it could be a *long*
5141 time before we return to the command level and call
5142 free_all_values. We can't call free_all_values because we
5143 might be in the middle of evaluating a function call. */
c906108c 5144
0cf6dd15 5145 int pc = 0;
9c06b0b4 5146 struct value *mark;
fa4727a6
DJ
5147 struct value *new_val;
5148
3a5c3e22 5149 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5150 /* Since we don't know the exact trigger address (from
5151 stopped_data_address), just tell the user we've triggered
5152 a mask watchpoint. */
5153 return WP_VALUE_CHANGED;
5154
5155 mark = value_mark ();
3a1115a0 5156 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5157
bb9d5f81
PP
5158 if (b->val_bitsize != 0)
5159 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5160
4a64f543
MS
5161 /* We use value_equal_contents instead of value_equal because
5162 the latter coerces an array to a pointer, thus comparing just
5163 the address of the array instead of its contents. This is
5164 not what we want. */
fa4727a6 5165 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5166 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5167 {
fa4727a6
DJ
5168 if (new_val != NULL)
5169 {
5170 release_value (new_val);
5171 value_free_to_mark (mark);
5172 }
c906108c
SS
5173 bs->old_val = b->val;
5174 b->val = new_val;
fa4727a6 5175 b->val_valid = 1;
c906108c
SS
5176 return WP_VALUE_CHANGED;
5177 }
5178 else
5179 {
60e1c644 5180 /* Nothing changed. */
c906108c 5181 value_free_to_mark (mark);
c906108c
SS
5182 return WP_VALUE_NOT_CHANGED;
5183 }
5184 }
5185 else
5186 {
79a45e25
PA
5187 struct ui_out *uiout = current_uiout;
5188
c906108c 5189 /* This seems like the only logical thing to do because
c5aa993b
JM
5190 if we temporarily ignored the watchpoint, then when
5191 we reenter the block in which it is valid it contains
5192 garbage (in the case of a function, it may have two
5193 garbage values, one before and one after the prologue).
5194 So we can't even detect the first assignment to it and
5195 watch after that (since the garbage may or may not equal
5196 the first value assigned). */
348d480f
PA
5197 /* We print all the stop information in
5198 breakpoint_ops->print_it, but in this case, by the time we
5199 call breakpoint_ops->print_it this bp will be deleted
5200 already. So we have no choice but print the information
5201 here. */
9dc5e2a9 5202 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5203 ui_out_field_string
5204 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5205 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5206 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5207 ui_out_text (uiout,
5208 " deleted because the program has left the block in\n\
8b93c638 5209which its expression is valid.\n");
4ce44c66 5210
cdac0397 5211 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5212 decref_counted_command_line (&b->base.commands);
d0fb5eae 5213 watchpoint_del_at_next_stop (b);
c906108c
SS
5214
5215 return WP_DELETED;
5216 }
5217}
5218
18a18393 5219/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5220 breakpoint location BL. This function does not check if we should
5221 stop, only if BL explains the stop. */
5222
18a18393 5223static int
6c95b8df 5224bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5225 struct address_space *aspace, CORE_ADDR bp_addr,
5226 const struct target_waitstatus *ws)
18a18393
VP
5227{
5228 struct breakpoint *b = bl->owner;
5229
348d480f 5230 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5231 gdb_assert (b != NULL);
5232
09ac7c10 5233 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5234}
5235
3a5c3e22
PA
5236/* Determine if the watched values have actually changed, and we
5237 should stop. If not, set BS->stop to 0. */
5238
18a18393
VP
5239static void
5240bpstat_check_watchpoint (bpstat bs)
5241{
2bdf28a0 5242 const struct bp_location *bl;
3a5c3e22 5243 struct watchpoint *b;
2bdf28a0
JK
5244
5245 /* BS is built for existing struct breakpoint. */
f431efe5 5246 bl = bs->bp_location_at;
2bdf28a0 5247 gdb_assert (bl != NULL);
3a5c3e22 5248 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5249 gdb_assert (b != NULL);
18a18393 5250
18a18393 5251 {
18a18393
VP
5252 int must_check_value = 0;
5253
3a5c3e22 5254 if (b->base.type == bp_watchpoint)
18a18393
VP
5255 /* For a software watchpoint, we must always check the
5256 watched value. */
5257 must_check_value = 1;
5258 else if (b->watchpoint_triggered == watch_triggered_yes)
5259 /* We have a hardware watchpoint (read, write, or access)
5260 and the target earlier reported an address watched by
5261 this watchpoint. */
5262 must_check_value = 1;
5263 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5264 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5265 /* We were stopped by a hardware watchpoint, but the target could
5266 not report the data address. We must check the watchpoint's
5267 value. Access and read watchpoints are out of luck; without
5268 a data address, we can't figure it out. */
5269 must_check_value = 1;
3a5c3e22 5270
18a18393
VP
5271 if (must_check_value)
5272 {
3e43a32a
MS
5273 char *message
5274 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5275 b->base.number);
18a18393
VP
5276 struct cleanup *cleanups = make_cleanup (xfree, message);
5277 int e = catch_errors (watchpoint_check, bs, message,
5278 RETURN_MASK_ALL);
5279 do_cleanups (cleanups);
5280 switch (e)
5281 {
5282 case WP_DELETED:
5283 /* We've already printed what needs to be printed. */
5284 bs->print_it = print_it_done;
5285 /* Stop. */
5286 break;
60e1c644
PA
5287 case WP_IGNORE:
5288 bs->print_it = print_it_noop;
5289 bs->stop = 0;
5290 break;
18a18393 5291 case WP_VALUE_CHANGED:
3a5c3e22 5292 if (b->base.type == bp_read_watchpoint)
18a18393 5293 {
85d721b8
PA
5294 /* There are two cases to consider here:
5295
4a64f543 5296 1. We're watching the triggered memory for reads.
85d721b8
PA
5297 In that case, trust the target, and always report
5298 the watchpoint hit to the user. Even though
5299 reads don't cause value changes, the value may
5300 have changed since the last time it was read, and
5301 since we're not trapping writes, we will not see
5302 those, and as such we should ignore our notion of
5303 old value.
5304
4a64f543 5305 2. We're watching the triggered memory for both
85d721b8
PA
5306 reads and writes. There are two ways this may
5307 happen:
5308
4a64f543 5309 2.1. This is a target that can't break on data
85d721b8
PA
5310 reads only, but can break on accesses (reads or
5311 writes), such as e.g., x86. We detect this case
5312 at the time we try to insert read watchpoints.
5313
4a64f543 5314 2.2. Otherwise, the target supports read
85d721b8
PA
5315 watchpoints, but, the user set an access or write
5316 watchpoint watching the same memory as this read
5317 watchpoint.
5318
5319 If we're watching memory writes as well as reads,
5320 ignore watchpoint hits when we find that the
5321 value hasn't changed, as reads don't cause
5322 changes. This still gives false positives when
5323 the program writes the same value to memory as
5324 what there was already in memory (we will confuse
5325 it for a read), but it's much better than
5326 nothing. */
5327
5328 int other_write_watchpoint = 0;
5329
5330 if (bl->watchpoint_type == hw_read)
5331 {
5332 struct breakpoint *other_b;
5333
5334 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5335 if (other_b->type == bp_hardware_watchpoint
5336 || other_b->type == bp_access_watchpoint)
85d721b8 5337 {
3a5c3e22
PA
5338 struct watchpoint *other_w =
5339 (struct watchpoint *) other_b;
5340
5341 if (other_w->watchpoint_triggered
5342 == watch_triggered_yes)
5343 {
5344 other_write_watchpoint = 1;
5345 break;
5346 }
85d721b8
PA
5347 }
5348 }
5349
5350 if (other_write_watchpoint
5351 || bl->watchpoint_type == hw_access)
5352 {
5353 /* We're watching the same memory for writes,
5354 and the value changed since the last time we
5355 updated it, so this trap must be for a write.
5356 Ignore it. */
5357 bs->print_it = print_it_noop;
5358 bs->stop = 0;
5359 }
18a18393
VP
5360 }
5361 break;
5362 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5363 if (b->base.type == bp_hardware_watchpoint
5364 || b->base.type == bp_watchpoint)
18a18393
VP
5365 {
5366 /* Don't stop: write watchpoints shouldn't fire if
5367 the value hasn't changed. */
5368 bs->print_it = print_it_noop;
5369 bs->stop = 0;
5370 }
5371 /* Stop. */
5372 break;
5373 default:
5374 /* Can't happen. */
5375 case 0:
5376 /* Error from catch_errors. */
3a5c3e22 5377 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5378 watchpoint_del_at_next_stop (b);
18a18393
VP
5379 /* We've already printed what needs to be printed. */
5380 bs->print_it = print_it_done;
5381 break;
5382 }
5383 }
5384 else /* must_check_value == 0 */
5385 {
5386 /* This is a case where some watchpoint(s) triggered, but
5387 not at the address of this watchpoint, or else no
5388 watchpoint triggered after all. So don't print
5389 anything for this watchpoint. */
5390 bs->print_it = print_it_noop;
5391 bs->stop = 0;
5392 }
5393 }
5394}
5395
7d4df6a4
DE
5396/* For breakpoints that are currently marked as telling gdb to stop,
5397 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5398 of breakpoint referred to by BS. If we should not stop for this
5399 breakpoint, set BS->stop to 0. */
f431efe5 5400
18a18393
VP
5401static void
5402bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5403{
2bdf28a0
JK
5404 const struct bp_location *bl;
5405 struct breakpoint *b;
7d4df6a4
DE
5406 int value_is_zero = 0;
5407 struct expression *cond;
5408
5409 gdb_assert (bs->stop);
2bdf28a0
JK
5410
5411 /* BS is built for existing struct breakpoint. */
f431efe5 5412 bl = bs->bp_location_at;
2bdf28a0 5413 gdb_assert (bl != NULL);
f431efe5 5414 b = bs->breakpoint_at;
2bdf28a0 5415 gdb_assert (b != NULL);
18a18393 5416
b775012e
LM
5417 /* Even if the target evaluated the condition on its end and notified GDB, we
5418 need to do so again since GDB does not know if we stopped due to a
5419 breakpoint or a single step breakpoint. */
5420
18a18393 5421 if (frame_id_p (b->frame_id)
edb3359d 5422 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5423 {
7d4df6a4
DE
5424 bs->stop = 0;
5425 return;
5426 }
60e1c644 5427
12ab52e9
PA
5428 /* If this is a thread/task-specific breakpoint, don't waste cpu
5429 evaluating the condition if this isn't the specified
5430 thread/task. */
5431 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5432 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5433
6c1b0f7b
DE
5434 {
5435 bs->stop = 0;
5436 return;
5437 }
5438
6dddc817
DE
5439 /* Evaluate extension language breakpoints that have a "stop" method
5440 implemented. */
5441 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5442
7d4df6a4
DE
5443 if (is_watchpoint (b))
5444 {
5445 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5446
7d4df6a4
DE
5447 cond = w->cond_exp;
5448 }
5449 else
5450 cond = bl->cond;
60e1c644 5451
7d4df6a4
DE
5452 if (cond && b->disposition != disp_del_at_next_stop)
5453 {
5454 int within_current_scope = 1;
5455 struct watchpoint * w;
60e1c644 5456
7d4df6a4
DE
5457 /* We use value_mark and value_free_to_mark because it could
5458 be a long time before we return to the command level and
5459 call free_all_values. We can't call free_all_values
5460 because we might be in the middle of evaluating a
5461 function call. */
5462 struct value *mark = value_mark ();
5463
5464 if (is_watchpoint (b))
5465 w = (struct watchpoint *) b;
5466 else
5467 w = NULL;
5468
5469 /* Need to select the frame, with all that implies so that
5470 the conditions will have the right context. Because we
5471 use the frame, we will not see an inlined function's
5472 variables when we arrive at a breakpoint at the start
5473 of the inlined function; the current frame will be the
5474 call site. */
5475 if (w == NULL || w->cond_exp_valid_block == NULL)
5476 select_frame (get_current_frame ());
5477 else
18a18393 5478 {
7d4df6a4
DE
5479 struct frame_info *frame;
5480
5481 /* For local watchpoint expressions, which particular
5482 instance of a local is being watched matters, so we
5483 keep track of the frame to evaluate the expression
5484 in. To evaluate the condition however, it doesn't
5485 really matter which instantiation of the function
5486 where the condition makes sense triggers the
5487 watchpoint. This allows an expression like "watch
5488 global if q > 10" set in `func', catch writes to
5489 global on all threads that call `func', or catch
5490 writes on all recursive calls of `func' by a single
5491 thread. We simply always evaluate the condition in
5492 the innermost frame that's executing where it makes
5493 sense to evaluate the condition. It seems
5494 intuitive. */
5495 frame = block_innermost_frame (w->cond_exp_valid_block);
5496 if (frame != NULL)
5497 select_frame (frame);
5498 else
5499 within_current_scope = 0;
18a18393 5500 }
7d4df6a4
DE
5501 if (within_current_scope)
5502 value_is_zero
5503 = catch_errors (breakpoint_cond_eval, cond,
5504 "Error in testing breakpoint condition:\n",
5505 RETURN_MASK_ALL);
5506 else
18a18393 5507 {
7d4df6a4
DE
5508 warning (_("Watchpoint condition cannot be tested "
5509 "in the current scope"));
5510 /* If we failed to set the right context for this
5511 watchpoint, unconditionally report it. */
5512 value_is_zero = 0;
18a18393 5513 }
7d4df6a4
DE
5514 /* FIXME-someday, should give breakpoint #. */
5515 value_free_to_mark (mark);
18a18393 5516 }
7d4df6a4
DE
5517
5518 if (cond && value_is_zero)
5519 {
5520 bs->stop = 0;
5521 }
7d4df6a4
DE
5522 else if (b->ignore_count > 0)
5523 {
5524 b->ignore_count--;
5525 bs->stop = 0;
5526 /* Increase the hit count even though we don't stop. */
5527 ++(b->hit_count);
5528 observer_notify_breakpoint_modified (b);
5529 }
18a18393
VP
5530}
5531
5532
9709f61c 5533/* Get a bpstat associated with having just stopped at address
d983da9c 5534 BP_ADDR in thread PTID.
c906108c 5535
d983da9c 5536 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5537 don't understand this stop. Result is a chain of bpstat's such
5538 that:
c906108c 5539
c5aa993b 5540 if we don't understand the stop, the result is a null pointer.
c906108c 5541
c5aa993b 5542 if we understand why we stopped, the result is not null.
c906108c 5543
c5aa993b
JM
5544 Each element of the chain refers to a particular breakpoint or
5545 watchpoint at which we have stopped. (We may have stopped for
5546 several reasons concurrently.)
c906108c 5547
c5aa993b
JM
5548 Each element of the chain has valid next, breakpoint_at,
5549 commands, FIXME??? fields. */
c906108c
SS
5550
5551bpstat
6c95b8df 5552bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5553 CORE_ADDR bp_addr, ptid_t ptid,
5554 const struct target_waitstatus *ws)
c906108c 5555{
0d381245 5556 struct breakpoint *b = NULL;
afe38095 5557 struct bp_location *bl;
20874c92 5558 struct bp_location *loc;
5760d0ab
JK
5559 /* First item of allocated bpstat's. */
5560 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5561 /* Pointer to the last thing in the chain currently. */
5760d0ab 5562 bpstat bs;
20874c92 5563 int ix;
429374b8 5564 int need_remove_insert;
f431efe5 5565 int removed_any;
c906108c 5566
f431efe5
PA
5567 /* First, build the bpstat chain with locations that explain a
5568 target stop, while being careful to not set the target running,
5569 as that may invalidate locations (in particular watchpoint
5570 locations are recreated). Resuming will happen here with
5571 breakpoint conditions or watchpoint expressions that include
5572 inferior function calls. */
c5aa993b 5573
429374b8
JK
5574 ALL_BREAKPOINTS (b)
5575 {
5576 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5577 continue;
a5606eee 5578
429374b8
JK
5579 for (bl = b->loc; bl != NULL; bl = bl->next)
5580 {
4a64f543
MS
5581 /* For hardware watchpoints, we look only at the first
5582 location. The watchpoint_check function will work on the
5583 entire expression, not the individual locations. For
5584 read watchpoints, the watchpoints_triggered function has
5585 checked all locations already. */
429374b8
JK
5586 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5587 break;
18a18393 5588
f6592439 5589 if (!bl->enabled || bl->shlib_disabled)
429374b8 5590 continue;
c5aa993b 5591
09ac7c10 5592 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5593 continue;
c5aa993b 5594
4a64f543
MS
5595 /* Come here if it's a watchpoint, or if the break address
5596 matches. */
c5aa993b 5597
4a64f543
MS
5598 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5599 explain stop. */
c5aa993b 5600
f431efe5
PA
5601 /* Assume we stop. Should we find a watchpoint that is not
5602 actually triggered, or if the condition of the breakpoint
5603 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5604 bs->stop = 1;
5605 bs->print = 1;
d983da9c 5606
f431efe5
PA
5607 /* If this is a scope breakpoint, mark the associated
5608 watchpoint as triggered so that we will handle the
5609 out-of-scope event. We'll get to the watchpoint next
5610 iteration. */
d0fb5eae 5611 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5612 {
5613 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5614
5615 w->watchpoint_triggered = watch_triggered_yes;
5616 }
f431efe5
PA
5617 }
5618 }
5619
5620 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5621 {
f1310107 5622 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 5623 {
5760d0ab 5624 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
5625 /* For hits of moribund locations, we should just proceed. */
5626 bs->stop = 0;
5627 bs->print = 0;
5628 bs->print_it = print_it_noop;
5629 }
5630 }
5631
edcc5120
TT
5632 /* A bit of special processing for shlib breakpoints. We need to
5633 process solib loading here, so that the lists of loaded and
5634 unloaded libraries are correct before we handle "catch load" and
5635 "catch unload". */
5636 for (bs = bs_head; bs != NULL; bs = bs->next)
5637 {
5d268276 5638 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5639 {
5640 handle_solib_event ();
5641 break;
5642 }
5643 }
5644
f431efe5
PA
5645 /* Now go through the locations that caused the target to stop, and
5646 check whether we're interested in reporting this stop to higher
5647 layers, or whether we should resume the target transparently. */
5648
5649 removed_any = 0;
5650
5760d0ab 5651 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5652 {
5653 if (!bs->stop)
5654 continue;
5655
f431efe5 5656 b = bs->breakpoint_at;
348d480f
PA
5657 b->ops->check_status (bs);
5658 if (bs->stop)
28010a5d 5659 {
348d480f 5660 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5661
429374b8
JK
5662 if (bs->stop)
5663 {
5664 ++(b->hit_count);
8d3788bd 5665 observer_notify_breakpoint_modified (b);
c906108c 5666
4a64f543 5667 /* We will stop here. */
429374b8
JK
5668 if (b->disposition == disp_disable)
5669 {
816338b5
SS
5670 --(b->enable_count);
5671 if (b->enable_count <= 0
5672 && b->enable_state != bp_permanent)
429374b8 5673 b->enable_state = bp_disabled;
f431efe5 5674 removed_any = 1;
429374b8
JK
5675 }
5676 if (b->silent)
5677 bs->print = 0;
5678 bs->commands = b->commands;
9add0f1b 5679 incref_counted_command_line (bs->commands);
abf85f46
JK
5680 if (command_line_is_silent (bs->commands
5681 ? bs->commands->commands : NULL))
5682 bs->print = 0;
9d6e6e84
HZ
5683
5684 b->ops->after_condition_true (bs);
429374b8
JK
5685 }
5686
348d480f 5687 }
a9b3a50f
PA
5688
5689 /* Print nothing for this entry if we don't stop or don't
5690 print. */
5691 if (!bs->stop || !bs->print)
5692 bs->print_it = print_it_noop;
429374b8 5693 }
876fa593 5694
d983da9c
DJ
5695 /* If we aren't stopping, the value of some hardware watchpoint may
5696 not have changed, but the intermediate memory locations we are
5697 watching may have. Don't bother if we're stopping; this will get
5698 done later. */
d832cb68 5699 need_remove_insert = 0;
5760d0ab
JK
5700 if (! bpstat_causes_stop (bs_head))
5701 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5702 if (!bs->stop
f431efe5
PA
5703 && bs->breakpoint_at
5704 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5705 {
3a5c3e22
PA
5706 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5707
5708 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5709 need_remove_insert = 1;
d983da9c
DJ
5710 }
5711
d832cb68 5712 if (need_remove_insert)
44702360 5713 update_global_location_list (UGLL_MAY_INSERT);
f431efe5 5714 else if (removed_any)
44702360 5715 update_global_location_list (UGLL_DONT_INSERT);
d832cb68 5716
5760d0ab 5717 return bs_head;
c906108c 5718}
628fe4e4
JK
5719
5720static void
5721handle_jit_event (void)
5722{
5723 struct frame_info *frame;
5724 struct gdbarch *gdbarch;
5725
5726 /* Switch terminal for any messages produced by
5727 breakpoint_re_set. */
5728 target_terminal_ours_for_output ();
5729
5730 frame = get_current_frame ();
5731 gdbarch = get_frame_arch (frame);
5732
5733 jit_event_handler (gdbarch);
5734
5735 target_terminal_inferior ();
5736}
5737
5738/* Prepare WHAT final decision for infrun. */
5739
5740/* Decide what infrun needs to do with this bpstat. */
5741
c906108c 5742struct bpstat_what
0e30163f 5743bpstat_what (bpstat bs_head)
c906108c 5744{
c906108c 5745 struct bpstat_what retval;
628fe4e4 5746 int jit_event = 0;
0e30163f 5747 bpstat bs;
c906108c 5748
628fe4e4 5749 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5750 retval.call_dummy = STOP_NONE;
186c406b 5751 retval.is_longjmp = 0;
628fe4e4 5752
0e30163f 5753 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5754 {
628fe4e4
JK
5755 /* Extract this BS's action. After processing each BS, we check
5756 if its action overrides all we've seem so far. */
5757 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5758 enum bptype bptype;
5759
c906108c 5760 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5761 {
5762 /* I suspect this can happen if it was a momentary
5763 breakpoint which has since been deleted. */
5764 bptype = bp_none;
5765 }
20874c92 5766 else
f431efe5 5767 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5768
5769 switch (bptype)
c906108c
SS
5770 {
5771 case bp_none:
628fe4e4 5772 break;
c906108c
SS
5773 case bp_breakpoint:
5774 case bp_hardware_breakpoint:
5775 case bp_until:
5776 case bp_finish:
a9b3a50f 5777 case bp_shlib_event:
c906108c
SS
5778 if (bs->stop)
5779 {
5780 if (bs->print)
628fe4e4 5781 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5782 else
628fe4e4 5783 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5784 }
5785 else
628fe4e4 5786 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5787 break;
5788 case bp_watchpoint:
5789 case bp_hardware_watchpoint:
5790 case bp_read_watchpoint:
5791 case bp_access_watchpoint:
5792 if (bs->stop)
5793 {
5794 if (bs->print)
628fe4e4 5795 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5796 else
628fe4e4 5797 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5798 }
5799 else
628fe4e4
JK
5800 {
5801 /* There was a watchpoint, but we're not stopping.
5802 This requires no further action. */
5803 }
c906108c
SS
5804 break;
5805 case bp_longjmp:
e2e4d78b 5806 case bp_longjmp_call_dummy:
186c406b 5807 case bp_exception:
628fe4e4 5808 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5809 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5810 break;
5811 case bp_longjmp_resume:
186c406b 5812 case bp_exception_resume:
628fe4e4 5813 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5814 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5815 break;
5816 case bp_step_resume:
5817 if (bs->stop)
628fe4e4
JK
5818 this_action = BPSTAT_WHAT_STEP_RESUME;
5819 else
c906108c 5820 {
628fe4e4
JK
5821 /* It is for the wrong frame. */
5822 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5823 }
c906108c 5824 break;
2c03e5be
PA
5825 case bp_hp_step_resume:
5826 if (bs->stop)
5827 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5828 else
5829 {
5830 /* It is for the wrong frame. */
5831 this_action = BPSTAT_WHAT_SINGLE;
5832 }
5833 break;
c906108c 5834 case bp_watchpoint_scope:
c4093a6a 5835 case bp_thread_event:
1900040c 5836 case bp_overlay_event:
0fd8e87f 5837 case bp_longjmp_master:
aa7d318d 5838 case bp_std_terminate_master:
186c406b 5839 case bp_exception_master:
628fe4e4 5840 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5841 break;
ce78b96d 5842 case bp_catchpoint:
c5aa993b
JM
5843 if (bs->stop)
5844 {
5845 if (bs->print)
628fe4e4 5846 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5847 else
628fe4e4 5848 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5849 }
5850 else
628fe4e4
JK
5851 {
5852 /* There was a catchpoint, but we're not stopping.
5853 This requires no further action. */
5854 }
5855 break;
628fe4e4
JK
5856 case bp_jit_event:
5857 jit_event = 1;
5858 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5859 break;
c906108c 5860 case bp_call_dummy:
53a5351d
JM
5861 /* Make sure the action is stop (silent or noisy),
5862 so infrun.c pops the dummy frame. */
aa7d318d 5863 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5864 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5865 break;
5866 case bp_std_terminate:
5867 /* Make sure the action is stop (silent or noisy),
5868 so infrun.c pops the dummy frame. */
aa7d318d 5869 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5870 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5871 break;
1042e4c0 5872 case bp_tracepoint:
7a697b8d 5873 case bp_fast_tracepoint:
0fb4aa4b 5874 case bp_static_tracepoint:
1042e4c0
SS
5875 /* Tracepoint hits should not be reported back to GDB, and
5876 if one got through somehow, it should have been filtered
5877 out already. */
5878 internal_error (__FILE__, __LINE__,
7a697b8d 5879 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5880 break;
5881 case bp_gnu_ifunc_resolver:
5882 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5883 this_action = BPSTAT_WHAT_SINGLE;
5884 break;
5885 case bp_gnu_ifunc_resolver_return:
5886 /* The breakpoint will be removed, execution will restart from the
5887 PC of the former breakpoint. */
5888 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5889 break;
e7e0cddf
SS
5890
5891 case bp_dprintf:
a11cfd87
HZ
5892 if (bs->stop)
5893 this_action = BPSTAT_WHAT_STOP_SILENT;
5894 else
5895 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5896 break;
5897
628fe4e4
JK
5898 default:
5899 internal_error (__FILE__, __LINE__,
5900 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5901 }
628fe4e4
JK
5902
5903 retval.main_action = max (retval.main_action, this_action);
c906108c 5904 }
628fe4e4 5905
0e30163f
JK
5906 /* These operations may affect the bs->breakpoint_at state so they are
5907 delayed after MAIN_ACTION is decided above. */
5908
628fe4e4
JK
5909 if (jit_event)
5910 {
5911 if (debug_infrun)
5912 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5913
5914 handle_jit_event ();
5915 }
5916
0e30163f
JK
5917 for (bs = bs_head; bs != NULL; bs = bs->next)
5918 {
5919 struct breakpoint *b = bs->breakpoint_at;
5920
5921 if (b == NULL)
5922 continue;
5923 switch (b->type)
5924 {
5925 case bp_gnu_ifunc_resolver:
5926 gnu_ifunc_resolver_stop (b);
5927 break;
5928 case bp_gnu_ifunc_resolver_return:
5929 gnu_ifunc_resolver_return_stop (b);
5930 break;
5931 }
5932 }
5933
c906108c
SS
5934 return retval;
5935}
5936
5937/* Nonzero if we should step constantly (e.g. watchpoints on machines
5938 without hardware support). This isn't related to a specific bpstat,
5939 just to things like whether watchpoints are set. */
5940
c5aa993b 5941int
fba45db2 5942bpstat_should_step (void)
c906108c
SS
5943{
5944 struct breakpoint *b;
cc59ec59 5945
c906108c 5946 ALL_BREAKPOINTS (b)
717a8278 5947 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5948 return 1;
c906108c
SS
5949 return 0;
5950}
5951
67822962
PA
5952int
5953bpstat_causes_stop (bpstat bs)
5954{
5955 for (; bs != NULL; bs = bs->next)
5956 if (bs->stop)
5957 return 1;
5958
5959 return 0;
5960}
5961
c906108c 5962\f
c5aa993b 5963
170b53b2
UW
5964/* Compute a string of spaces suitable to indent the next line
5965 so it starts at the position corresponding to the table column
5966 named COL_NAME in the currently active table of UIOUT. */
5967
5968static char *
5969wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5970{
5971 static char wrap_indent[80];
5972 int i, total_width, width, align;
5973 char *text;
5974
5975 total_width = 0;
5976 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5977 {
5978 if (strcmp (text, col_name) == 0)
5979 {
5980 gdb_assert (total_width < sizeof wrap_indent);
5981 memset (wrap_indent, ' ', total_width);
5982 wrap_indent[total_width] = 0;
5983
5984 return wrap_indent;
5985 }
5986
5987 total_width += width + 1;
5988 }
5989
5990 return NULL;
5991}
5992
b775012e
LM
5993/* Determine if the locations of this breakpoint will have their conditions
5994 evaluated by the target, host or a mix of both. Returns the following:
5995
5996 "host": Host evals condition.
5997 "host or target": Host or Target evals condition.
5998 "target": Target evals condition.
5999*/
6000
6001static const char *
6002bp_condition_evaluator (struct breakpoint *b)
6003{
6004 struct bp_location *bl;
6005 char host_evals = 0;
6006 char target_evals = 0;
6007
6008 if (!b)
6009 return NULL;
6010
6011 if (!is_breakpoint (b))
6012 return NULL;
6013
6014 if (gdb_evaluates_breakpoint_condition_p ()
6015 || !target_supports_evaluation_of_breakpoint_conditions ())
6016 return condition_evaluation_host;
6017
6018 for (bl = b->loc; bl; bl = bl->next)
6019 {
6020 if (bl->cond_bytecode)
6021 target_evals++;
6022 else
6023 host_evals++;
6024 }
6025
6026 if (host_evals && target_evals)
6027 return condition_evaluation_both;
6028 else if (target_evals)
6029 return condition_evaluation_target;
6030 else
6031 return condition_evaluation_host;
6032}
6033
6034/* Determine the breakpoint location's condition evaluator. This is
6035 similar to bp_condition_evaluator, but for locations. */
6036
6037static const char *
6038bp_location_condition_evaluator (struct bp_location *bl)
6039{
6040 if (bl && !is_breakpoint (bl->owner))
6041 return NULL;
6042
6043 if (gdb_evaluates_breakpoint_condition_p ()
6044 || !target_supports_evaluation_of_breakpoint_conditions ())
6045 return condition_evaluation_host;
6046
6047 if (bl && bl->cond_bytecode)
6048 return condition_evaluation_target;
6049 else
6050 return condition_evaluation_host;
6051}
6052
859825b8
JK
6053/* Print the LOC location out of the list of B->LOC locations. */
6054
170b53b2
UW
6055static void
6056print_breakpoint_location (struct breakpoint *b,
6057 struct bp_location *loc)
0d381245 6058{
79a45e25 6059 struct ui_out *uiout = current_uiout;
6c95b8df
PA
6060 struct cleanup *old_chain = save_current_program_space ();
6061
859825b8
JK
6062 if (loc != NULL && loc->shlib_disabled)
6063 loc = NULL;
6064
6c95b8df
PA
6065 if (loc != NULL)
6066 set_current_program_space (loc->pspace);
6067
56435ebe
TT
6068 if (b->display_canonical)
6069 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 6070 else if (loc && loc->symtab)
0d381245
VP
6071 {
6072 struct symbol *sym
6073 = find_pc_sect_function (loc->address, loc->section);
6074 if (sym)
6075 {
6076 ui_out_text (uiout, "in ");
6077 ui_out_field_string (uiout, "func",
6078 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
6079 ui_out_text (uiout, " ");
6080 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6081 ui_out_text (uiout, "at ");
0d381245 6082 }
05cba821
JK
6083 ui_out_field_string (uiout, "file",
6084 symtab_to_filename_for_display (loc->symtab));
0d381245 6085 ui_out_text (uiout, ":");
05cba821 6086
0d381245 6087 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
6088 ui_out_field_string (uiout, "fullname",
6089 symtab_to_fullname (loc->symtab));
0d381245 6090
f8eba3c6 6091 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 6092 }
859825b8 6093 else if (loc)
0d381245 6094 {
f99d8bf4
PA
6095 struct ui_file *stb = mem_fileopen ();
6096 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 6097
f99d8bf4 6098 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 6099 demangle, "");
0d381245 6100 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
6101
6102 do_cleanups (stb_chain);
0d381245 6103 }
859825b8
JK
6104 else
6105 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 6106
b775012e
LM
6107 if (loc && is_breakpoint (b)
6108 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6109 && bp_condition_evaluator (b) == condition_evaluation_both)
6110 {
6111 ui_out_text (uiout, " (");
6112 ui_out_field_string (uiout, "evaluated-by",
6113 bp_location_condition_evaluator (loc));
6114 ui_out_text (uiout, ")");
6115 }
6116
6c95b8df 6117 do_cleanups (old_chain);
0d381245
VP
6118}
6119
269b11a2
PA
6120static const char *
6121bptype_string (enum bptype type)
c906108c 6122{
c4093a6a
JM
6123 struct ep_type_description
6124 {
6125 enum bptype type;
6126 char *description;
6127 };
6128 static struct ep_type_description bptypes[] =
c906108c 6129 {
c5aa993b
JM
6130 {bp_none, "?deleted?"},
6131 {bp_breakpoint, "breakpoint"},
c906108c 6132 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
6133 {bp_until, "until"},
6134 {bp_finish, "finish"},
6135 {bp_watchpoint, "watchpoint"},
c906108c 6136 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
6137 {bp_read_watchpoint, "read watchpoint"},
6138 {bp_access_watchpoint, "acc watchpoint"},
6139 {bp_longjmp, "longjmp"},
6140 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 6141 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
6142 {bp_exception, "exception"},
6143 {bp_exception_resume, "exception resume"},
c5aa993b 6144 {bp_step_resume, "step resume"},
2c03e5be 6145 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
6146 {bp_watchpoint_scope, "watchpoint scope"},
6147 {bp_call_dummy, "call dummy"},
aa7d318d 6148 {bp_std_terminate, "std::terminate"},
c5aa993b 6149 {bp_shlib_event, "shlib events"},
c4093a6a 6150 {bp_thread_event, "thread events"},
1900040c 6151 {bp_overlay_event, "overlay events"},
0fd8e87f 6152 {bp_longjmp_master, "longjmp master"},
aa7d318d 6153 {bp_std_terminate_master, "std::terminate master"},
186c406b 6154 {bp_exception_master, "exception master"},
ce78b96d 6155 {bp_catchpoint, "catchpoint"},
1042e4c0 6156 {bp_tracepoint, "tracepoint"},
7a697b8d 6157 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6158 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6159 {bp_dprintf, "dprintf"},
4efc6507 6160 {bp_jit_event, "jit events"},
0e30163f
JK
6161 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6162 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6163 };
269b11a2
PA
6164
6165 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6166 || ((int) type != bptypes[(int) type].type))
6167 internal_error (__FILE__, __LINE__,
6168 _("bptypes table does not describe type #%d."),
6169 (int) type);
6170
6171 return bptypes[(int) type].description;
6172}
6173
998580f1
MK
6174/* For MI, output a field named 'thread-groups' with a list as the value.
6175 For CLI, prefix the list with the string 'inf'. */
6176
6177static void
6178output_thread_groups (struct ui_out *uiout,
6179 const char *field_name,
6180 VEC(int) *inf_num,
6181 int mi_only)
6182{
752eb8b4 6183 struct cleanup *back_to;
998580f1
MK
6184 int is_mi = ui_out_is_mi_like_p (uiout);
6185 int inf;
6186 int i;
6187
6188 /* For backward compatibility, don't display inferiors in CLI unless
6189 there are several. Always display them for MI. */
6190 if (!is_mi && mi_only)
6191 return;
6192
752eb8b4
TT
6193 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6194
998580f1
MK
6195 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6196 {
6197 if (is_mi)
6198 {
6199 char mi_group[10];
6200
6201 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6202 ui_out_field_string (uiout, NULL, mi_group);
6203 }
6204 else
6205 {
6206 if (i == 0)
6207 ui_out_text (uiout, " inf ");
6208 else
6209 ui_out_text (uiout, ", ");
6210
6211 ui_out_text (uiout, plongest (inf));
6212 }
6213 }
6214
6215 do_cleanups (back_to);
6216}
6217
269b11a2
PA
6218/* Print B to gdb_stdout. */
6219
6220static void
6221print_one_breakpoint_location (struct breakpoint *b,
6222 struct bp_location *loc,
6223 int loc_number,
6224 struct bp_location **last_loc,
269b11a2
PA
6225 int allflag)
6226{
6227 struct command_line *l;
c2c6d25f 6228 static char bpenables[] = "nynny";
c906108c 6229
79a45e25 6230 struct ui_out *uiout = current_uiout;
0d381245
VP
6231 int header_of_multiple = 0;
6232 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6233 struct value_print_options opts;
6234
6235 get_user_print_options (&opts);
0d381245
VP
6236
6237 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6238 /* See comment in print_one_breakpoint concerning treatment of
6239 breakpoints with single disabled location. */
0d381245
VP
6240 if (loc == NULL
6241 && (b->loc != NULL
6242 && (b->loc->next != NULL || !b->loc->enabled)))
6243 header_of_multiple = 1;
6244 if (loc == NULL)
6245 loc = b->loc;
6246
c4093a6a
JM
6247 annotate_record ();
6248
6249 /* 1 */
6250 annotate_field (0);
0d381245
VP
6251 if (part_of_multiple)
6252 {
6253 char *formatted;
0c6773c1 6254 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6255 ui_out_field_string (uiout, "number", formatted);
6256 xfree (formatted);
6257 }
6258 else
6259 {
6260 ui_out_field_int (uiout, "number", b->number);
6261 }
c4093a6a
JM
6262
6263 /* 2 */
6264 annotate_field (1);
0d381245
VP
6265 if (part_of_multiple)
6266 ui_out_field_skip (uiout, "type");
269b11a2
PA
6267 else
6268 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6269
6270 /* 3 */
6271 annotate_field (2);
0d381245
VP
6272 if (part_of_multiple)
6273 ui_out_field_skip (uiout, "disp");
6274 else
2cec12e5 6275 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6276
c4093a6a
JM
6277
6278 /* 4 */
6279 annotate_field (3);
0d381245 6280 if (part_of_multiple)
54e52265 6281 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6282 else
4a64f543
MS
6283 ui_out_field_fmt (uiout, "enabled", "%c",
6284 bpenables[(int) b->enable_state]);
54e52265 6285 ui_out_spaces (uiout, 2);
0d381245 6286
c4093a6a
JM
6287
6288 /* 5 and 6 */
3086aeae 6289 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6290 {
4a64f543
MS
6291 /* Although the print_one can possibly print all locations,
6292 calling it here is not likely to get any nice result. So,
6293 make sure there's just one location. */
0d381245 6294 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6295 b->ops->print_one (b, last_loc);
0d381245 6296 }
3086aeae
DJ
6297 else
6298 switch (b->type)
6299 {
6300 case bp_none:
6301 internal_error (__FILE__, __LINE__,
e2e0b3e5 6302 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6303 break;
c906108c 6304
3086aeae
DJ
6305 case bp_watchpoint:
6306 case bp_hardware_watchpoint:
6307 case bp_read_watchpoint:
6308 case bp_access_watchpoint:
3a5c3e22
PA
6309 {
6310 struct watchpoint *w = (struct watchpoint *) b;
6311
6312 /* Field 4, the address, is omitted (which makes the columns
6313 not line up too nicely with the headers, but the effect
6314 is relatively readable). */
6315 if (opts.addressprint)
6316 ui_out_field_skip (uiout, "addr");
6317 annotate_field (5);
6318 ui_out_field_string (uiout, "what", w->exp_string);
6319 }
3086aeae
DJ
6320 break;
6321
3086aeae
DJ
6322 case bp_breakpoint:
6323 case bp_hardware_breakpoint:
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 }
6572 else if (b->addr_string)
d24317b4 6573 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6574 }
c4093a6a 6575}
c5aa993b 6576
0d381245
VP
6577static void
6578print_one_breakpoint (struct breakpoint *b,
4a64f543 6579 struct bp_location **last_loc,
6c95b8df 6580 int allflag)
0d381245 6581{
8d3788bd 6582 struct cleanup *bkpt_chain;
79a45e25 6583 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6584
6585 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6586
12c5a436 6587 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6588 do_cleanups (bkpt_chain);
0d381245
VP
6589
6590 /* If this breakpoint has custom print function,
6591 it's already printed. Otherwise, print individual
6592 locations, if any. */
6593 if (b->ops == NULL || b->ops->print_one == NULL)
6594 {
4a64f543
MS
6595 /* If breakpoint has a single location that is disabled, we
6596 print it as if it had several locations, since otherwise it's
6597 hard to represent "breakpoint enabled, location disabled"
6598 situation.
6599
6600 Note that while hardware watchpoints have several locations
a3be7890 6601 internally, that's not a property exposed to user. */
0d381245 6602 if (b->loc
a5606eee 6603 && !is_hardware_watchpoint (b)
8d3788bd 6604 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6605 {
6606 struct bp_location *loc;
6607 int n = 1;
8d3788bd 6608
0d381245 6609 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6610 {
6611 struct cleanup *inner2 =
6612 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6613 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6614 do_cleanups (inner2);
6615 }
0d381245
VP
6616 }
6617 }
6618}
6619
a6d9a66e
UW
6620static int
6621breakpoint_address_bits (struct breakpoint *b)
6622{
6623 int print_address_bits = 0;
6624 struct bp_location *loc;
6625
6626 for (loc = b->loc; loc; loc = loc->next)
6627 {
c7437ca6
PA
6628 int addr_bit;
6629
6630 /* Software watchpoints that aren't watching memory don't have
6631 an address to print. */
6632 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6633 continue;
6634
6635 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6636 if (addr_bit > print_address_bits)
6637 print_address_bits = addr_bit;
6638 }
6639
6640 return print_address_bits;
6641}
0d381245 6642
c4093a6a
JM
6643struct captured_breakpoint_query_args
6644 {
6645 int bnum;
6646 };
c5aa993b 6647
c4093a6a 6648static int
2b65245e 6649do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6650{
6651 struct captured_breakpoint_query_args *args = data;
52f0bd74 6652 struct breakpoint *b;
a6d9a66e 6653 struct bp_location *dummy_loc = NULL;
cc59ec59 6654
c4093a6a
JM
6655 ALL_BREAKPOINTS (b)
6656 {
6657 if (args->bnum == b->number)
c5aa993b 6658 {
12c5a436 6659 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6660 return GDB_RC_OK;
c5aa993b 6661 }
c4093a6a
JM
6662 }
6663 return GDB_RC_NONE;
6664}
c5aa993b 6665
c4093a6a 6666enum gdb_rc
4a64f543
MS
6667gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6668 char **error_message)
c4093a6a
JM
6669{
6670 struct captured_breakpoint_query_args args;
cc59ec59 6671
c4093a6a
JM
6672 args.bnum = bnum;
6673 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6674 an error. */
b0b13bb4
DJ
6675 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6676 error_message, RETURN_MASK_ALL) < 0)
6677 return GDB_RC_FAIL;
6678 else
6679 return GDB_RC_OK;
c4093a6a 6680}
c5aa993b 6681
09d682a4
TT
6682/* Return true if this breakpoint was set by the user, false if it is
6683 internal or momentary. */
6684
6685int
6686user_breakpoint_p (struct breakpoint *b)
6687{
46c6471b 6688 return b->number > 0;
09d682a4
TT
6689}
6690
7f3b0473 6691/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6692 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6693 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6694 FILTER is non-NULL, call it on each breakpoint and only include the
6695 ones for which it returns non-zero. Return the total number of
6696 breakpoints listed. */
c906108c 6697
d77f58be 6698static int
e5a67952 6699breakpoint_1 (char *args, int allflag,
4a64f543 6700 int (*filter) (const struct breakpoint *))
c4093a6a 6701{
52f0bd74 6702 struct breakpoint *b;
a6d9a66e 6703 struct bp_location *last_loc = NULL;
7f3b0473 6704 int nr_printable_breakpoints;
3b31d625 6705 struct cleanup *bkpttbl_chain;
79a45b7d 6706 struct value_print_options opts;
a6d9a66e 6707 int print_address_bits = 0;
269b11a2 6708 int print_type_col_width = 14;
79a45e25 6709 struct ui_out *uiout = current_uiout;
269b11a2 6710
79a45b7d
TT
6711 get_user_print_options (&opts);
6712
4a64f543
MS
6713 /* Compute the number of rows in the table, as well as the size
6714 required for address fields. */
7f3b0473
AC
6715 nr_printable_breakpoints = 0;
6716 ALL_BREAKPOINTS (b)
e5a67952
MS
6717 {
6718 /* If we have a filter, only list the breakpoints it accepts. */
6719 if (filter && !filter (b))
6720 continue;
6721
6722 /* If we have an "args" string, it is a list of breakpoints to
6723 accept. Skip the others. */
6724 if (args != NULL && *args != '\0')
6725 {
6726 if (allflag && parse_and_eval_long (args) != b->number)
6727 continue;
6728 if (!allflag && !number_is_in_list (args, b->number))
6729 continue;
6730 }
269b11a2 6731
e5a67952
MS
6732 if (allflag || user_breakpoint_p (b))
6733 {
6734 int addr_bit, type_len;
a6d9a66e 6735
e5a67952
MS
6736 addr_bit = breakpoint_address_bits (b);
6737 if (addr_bit > print_address_bits)
6738 print_address_bits = addr_bit;
269b11a2 6739
e5a67952
MS
6740 type_len = strlen (bptype_string (b->type));
6741 if (type_len > print_type_col_width)
6742 print_type_col_width = type_len;
6743
6744 nr_printable_breakpoints++;
6745 }
6746 }
7f3b0473 6747
79a45b7d 6748 if (opts.addressprint)
3b31d625 6749 bkpttbl_chain
3e43a32a
MS
6750 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6751 nr_printable_breakpoints,
3b31d625 6752 "BreakpointTable");
8b93c638 6753 else
3b31d625 6754 bkpttbl_chain
3e43a32a
MS
6755 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6756 nr_printable_breakpoints,
3b31d625 6757 "BreakpointTable");
8b93c638 6758
7f3b0473 6759 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6760 annotate_breakpoints_headers ();
6761 if (nr_printable_breakpoints > 0)
6762 annotate_field (0);
4a64f543 6763 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6764 if (nr_printable_breakpoints > 0)
6765 annotate_field (1);
269b11a2 6766 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6767 "type", "Type"); /* 2 */
d7faa9e7
AC
6768 if (nr_printable_breakpoints > 0)
6769 annotate_field (2);
4a64f543 6770 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6771 if (nr_printable_breakpoints > 0)
6772 annotate_field (3);
54e52265 6773 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6774 if (opts.addressprint)
e5a67952
MS
6775 {
6776 if (nr_printable_breakpoints > 0)
6777 annotate_field (4);
6778 if (print_address_bits <= 32)
6779 ui_out_table_header (uiout, 10, ui_left,
6780 "addr", "Address"); /* 5 */
6781 else
6782 ui_out_table_header (uiout, 18, ui_left,
6783 "addr", "Address"); /* 5 */
6784 }
d7faa9e7
AC
6785 if (nr_printable_breakpoints > 0)
6786 annotate_field (5);
6787 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6788 ui_out_table_body (uiout);
6789 if (nr_printable_breakpoints > 0)
6790 annotate_breakpoints_table ();
7f3b0473 6791
c4093a6a 6792 ALL_BREAKPOINTS (b)
e5a67952
MS
6793 {
6794 QUIT;
6795 /* If we have a filter, only list the breakpoints it accepts. */
6796 if (filter && !filter (b))
6797 continue;
6798
6799 /* If we have an "args" string, it is a list of breakpoints to
6800 accept. Skip the others. */
6801
6802 if (args != NULL && *args != '\0')
6803 {
6804 if (allflag) /* maintenance info breakpoint */
6805 {
6806 if (parse_and_eval_long (args) != b->number)
6807 continue;
6808 }
6809 else /* all others */
6810 {
6811 if (!number_is_in_list (args, b->number))
6812 continue;
6813 }
6814 }
6815 /* We only print out user settable breakpoints unless the
6816 allflag is set. */
6817 if (allflag || user_breakpoint_p (b))
12c5a436 6818 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6819 }
6820
3b31d625 6821 do_cleanups (bkpttbl_chain);
698384cd 6822
7f3b0473 6823 if (nr_printable_breakpoints == 0)
c906108c 6824 {
4a64f543
MS
6825 /* If there's a filter, let the caller decide how to report
6826 empty list. */
d77f58be
SS
6827 if (!filter)
6828 {
e5a67952 6829 if (args == NULL || *args == '\0')
d77f58be
SS
6830 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6831 else
4a64f543 6832 ui_out_message (uiout, 0,
e5a67952
MS
6833 "No breakpoint or watchpoint matching '%s'.\n",
6834 args);
d77f58be 6835 }
c906108c
SS
6836 }
6837 else
c4093a6a 6838 {
a6d9a66e
UW
6839 if (last_loc && !server_command)
6840 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6841 }
c906108c 6842
4a64f543 6843 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6844 there have been breakpoints? */
c906108c 6845 annotate_breakpoints_table_end ();
d77f58be
SS
6846
6847 return nr_printable_breakpoints;
c906108c
SS
6848}
6849
ad443146
SS
6850/* Display the value of default-collect in a way that is generally
6851 compatible with the breakpoint list. */
6852
6853static void
6854default_collect_info (void)
6855{
79a45e25
PA
6856 struct ui_out *uiout = current_uiout;
6857
ad443146
SS
6858 /* If it has no value (which is frequently the case), say nothing; a
6859 message like "No default-collect." gets in user's face when it's
6860 not wanted. */
6861 if (!*default_collect)
6862 return;
6863
6864 /* The following phrase lines up nicely with per-tracepoint collect
6865 actions. */
6866 ui_out_text (uiout, "default collect ");
6867 ui_out_field_string (uiout, "default-collect", default_collect);
6868 ui_out_text (uiout, " \n");
6869}
6870
c906108c 6871static void
e5a67952 6872breakpoints_info (char *args, int from_tty)
c906108c 6873{
e5a67952 6874 breakpoint_1 (args, 0, NULL);
ad443146
SS
6875
6876 default_collect_info ();
d77f58be
SS
6877}
6878
6879static void
e5a67952 6880watchpoints_info (char *args, int from_tty)
d77f58be 6881{
e5a67952 6882 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6883 struct ui_out *uiout = current_uiout;
d77f58be
SS
6884
6885 if (num_printed == 0)
6886 {
e5a67952 6887 if (args == NULL || *args == '\0')
d77f58be
SS
6888 ui_out_message (uiout, 0, "No watchpoints.\n");
6889 else
e5a67952 6890 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6891 }
c906108c
SS
6892}
6893
7a292a7a 6894static void
e5a67952 6895maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6896{
e5a67952 6897 breakpoint_1 (args, 1, NULL);
ad443146
SS
6898
6899 default_collect_info ();
c906108c
SS
6900}
6901
0d381245 6902static int
714835d5 6903breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6904 struct program_space *pspace,
714835d5 6905 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6906{
6907 struct bp_location *bl = b->loc;
cc59ec59 6908
0d381245
VP
6909 for (; bl; bl = bl->next)
6910 {
6c95b8df
PA
6911 if (bl->pspace == pspace
6912 && bl->address == pc
0d381245
VP
6913 && (!overlay_debugging || bl->section == section))
6914 return 1;
6915 }
6916 return 0;
6917}
6918
672f9b60 6919/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6920 concerns with logical breakpoints, so we match program spaces, not
6921 address spaces. */
c906108c
SS
6922
6923static void
6c95b8df
PA
6924describe_other_breakpoints (struct gdbarch *gdbarch,
6925 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6926 struct obj_section *section, int thread)
c906108c 6927{
52f0bd74
AC
6928 int others = 0;
6929 struct breakpoint *b;
c906108c
SS
6930
6931 ALL_BREAKPOINTS (b)
672f9b60
KP
6932 others += (user_breakpoint_p (b)
6933 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6934 if (others > 0)
6935 {
a3f17187
AC
6936 if (others == 1)
6937 printf_filtered (_("Note: breakpoint "));
6938 else /* if (others == ???) */
6939 printf_filtered (_("Note: breakpoints "));
c906108c 6940 ALL_BREAKPOINTS (b)
672f9b60 6941 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6942 {
6943 others--;
6944 printf_filtered ("%d", b->number);
6945 if (b->thread == -1 && thread != -1)
6946 printf_filtered (" (all threads)");
6947 else if (b->thread != -1)
6948 printf_filtered (" (thread %d)", b->thread);
6949 printf_filtered ("%s%s ",
059fb39f 6950 ((b->enable_state == bp_disabled
f8eba3c6 6951 || b->enable_state == bp_call_disabled)
0d381245
VP
6952 ? " (disabled)"
6953 : b->enable_state == bp_permanent
6954 ? " (permanent)"
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
KB
6966/* Return true iff it is meaningful to use the address member of
6967 BPT. For some breakpoint types, the address member is irrelevant
6968 and it makes no sense to attempt to compare it to other addresses
6969 (or use it for any other purpose either).
6970
4a64f543
MS
6971 More specifically, each of the following breakpoint types will
6972 always have a zero valued address and we don't want to mark
6973 breakpoints of any of these types to be a duplicate of an actual
6974 breakpoint 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 }
76897487
KB
7166 else
7167 {
7168 CORE_ADDR adjusted_bpaddr;
7169
7170 /* Some targets have architectural constraints on the placement
7171 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7172 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7173
7174 /* An adjusted breakpoint address can significantly alter
7175 a user's expectations. Print a warning if an adjustment
7176 is required. */
7177 if (adjusted_bpaddr != bpaddr)
7178 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7179
7180 return adjusted_bpaddr;
7181 }
7182}
7183
28010a5d
PA
7184void
7185init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7186 struct breakpoint *owner)
7cc221ef 7187{
7cc221ef
DJ
7188 memset (loc, 0, sizeof (*loc));
7189
348d480f
PA
7190 gdb_assert (ops != NULL);
7191
28010a5d
PA
7192 loc->ops = ops;
7193 loc->owner = owner;
511a6cd4 7194 loc->cond = NULL;
b775012e 7195 loc->cond_bytecode = NULL;
0d381245
VP
7196 loc->shlib_disabled = 0;
7197 loc->enabled = 1;
e049a4b5 7198
28010a5d 7199 switch (owner->type)
e049a4b5
DJ
7200 {
7201 case bp_breakpoint:
7202 case bp_until:
7203 case bp_finish:
7204 case bp_longjmp:
7205 case bp_longjmp_resume:
e2e4d78b 7206 case bp_longjmp_call_dummy:
186c406b
TT
7207 case bp_exception:
7208 case bp_exception_resume:
e049a4b5 7209 case bp_step_resume:
2c03e5be 7210 case bp_hp_step_resume:
e049a4b5
DJ
7211 case bp_watchpoint_scope:
7212 case bp_call_dummy:
aa7d318d 7213 case bp_std_terminate:
e049a4b5
DJ
7214 case bp_shlib_event:
7215 case bp_thread_event:
7216 case bp_overlay_event:
4efc6507 7217 case bp_jit_event:
0fd8e87f 7218 case bp_longjmp_master:
aa7d318d 7219 case bp_std_terminate_master:
186c406b 7220 case bp_exception_master:
0e30163f
JK
7221 case bp_gnu_ifunc_resolver:
7222 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7223 case bp_dprintf:
e049a4b5 7224 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7225 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7226 break;
7227 case bp_hardware_breakpoint:
7228 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7229 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7230 break;
7231 case bp_hardware_watchpoint:
7232 case bp_read_watchpoint:
7233 case bp_access_watchpoint:
7234 loc->loc_type = bp_loc_hardware_watchpoint;
7235 break;
7236 case bp_watchpoint:
ce78b96d 7237 case bp_catchpoint:
15c3d785
PA
7238 case bp_tracepoint:
7239 case bp_fast_tracepoint:
0fb4aa4b 7240 case bp_static_tracepoint:
e049a4b5
DJ
7241 loc->loc_type = bp_loc_other;
7242 break;
7243 default:
e2e0b3e5 7244 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7245 }
7246
f431efe5 7247 loc->refc = 1;
28010a5d
PA
7248}
7249
7250/* Allocate a struct bp_location. */
7251
7252static struct bp_location *
7253allocate_bp_location (struct breakpoint *bpt)
7254{
348d480f
PA
7255 return bpt->ops->allocate_location (bpt);
7256}
7cc221ef 7257
f431efe5
PA
7258static void
7259free_bp_location (struct bp_location *loc)
fe3f5fa8 7260{
348d480f 7261 loc->ops->dtor (loc);
fe3f5fa8
VP
7262 xfree (loc);
7263}
7264
f431efe5
PA
7265/* Increment reference count. */
7266
7267static void
7268incref_bp_location (struct bp_location *bl)
7269{
7270 ++bl->refc;
7271}
7272
7273/* Decrement reference count. If the reference count reaches 0,
7274 destroy the bp_location. Sets *BLP to NULL. */
7275
7276static void
7277decref_bp_location (struct bp_location **blp)
7278{
0807b50c
PA
7279 gdb_assert ((*blp)->refc > 0);
7280
f431efe5
PA
7281 if (--(*blp)->refc == 0)
7282 free_bp_location (*blp);
7283 *blp = NULL;
7284}
7285
346774a9 7286/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7287
346774a9
PA
7288static void
7289add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7290{
346774a9 7291 struct breakpoint *b1;
c906108c 7292
346774a9
PA
7293 /* Add this breakpoint to the end of the chain so that a list of
7294 breakpoints will come out in order of increasing numbers. */
7295
7296 b1 = breakpoint_chain;
7297 if (b1 == 0)
7298 breakpoint_chain = b;
7299 else
7300 {
7301 while (b1->next)
7302 b1 = b1->next;
7303 b1->next = b;
7304 }
7305}
7306
7307/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7308
7309static void
7310init_raw_breakpoint_without_location (struct breakpoint *b,
7311 struct gdbarch *gdbarch,
28010a5d 7312 enum bptype bptype,
c0a91b2b 7313 const struct breakpoint_ops *ops)
346774a9 7314{
c906108c 7315 memset (b, 0, sizeof (*b));
2219d63c 7316
348d480f
PA
7317 gdb_assert (ops != NULL);
7318
28010a5d 7319 b->ops = ops;
4d28f7a8 7320 b->type = bptype;
a6d9a66e 7321 b->gdbarch = gdbarch;
c906108c
SS
7322 b->language = current_language->la_language;
7323 b->input_radix = input_radix;
7324 b->thread = -1;
b5de0fa7 7325 b->enable_state = bp_enabled;
c906108c
SS
7326 b->next = 0;
7327 b->silent = 0;
7328 b->ignore_count = 0;
7329 b->commands = NULL;
818dd999 7330 b->frame_id = null_frame_id;
0d381245 7331 b->condition_not_parsed = 0;
84f4c1fe 7332 b->py_bp_object = NULL;
d0fb5eae 7333 b->related_breakpoint = b;
346774a9
PA
7334}
7335
7336/* Helper to set_raw_breakpoint below. Creates a breakpoint
7337 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7338
7339static struct breakpoint *
7340set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7341 enum bptype bptype,
c0a91b2b 7342 const struct breakpoint_ops *ops)
346774a9
PA
7343{
7344 struct breakpoint *b = XNEW (struct breakpoint);
7345
348d480f 7346 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7347 add_to_breakpoint_chain (b);
0d381245
VP
7348 return b;
7349}
7350
0e30163f
JK
7351/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7352 resolutions should be made as the user specified the location explicitly
7353 enough. */
7354
0d381245 7355static void
0e30163f 7356set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7357{
2bdf28a0
JK
7358 gdb_assert (loc->owner != NULL);
7359
0d381245 7360 if (loc->owner->type == bp_breakpoint
1042e4c0 7361 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7362 || is_tracepoint (loc->owner))
0d381245 7363 {
0e30163f 7364 int is_gnu_ifunc;
2c02bd72 7365 const char *function_name;
6a3a010b 7366 CORE_ADDR func_addr;
0e30163f 7367
2c02bd72 7368 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7369 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7370
7371 if (is_gnu_ifunc && !explicit_loc)
7372 {
7373 struct breakpoint *b = loc->owner;
7374
7375 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7376 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7377 &loc->requested_address))
7378 {
7379 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7380 loc->address = adjust_breakpoint_address (loc->gdbarch,
7381 loc->requested_address,
7382 b->type);
7383 }
7384 else if (b->type == bp_breakpoint && b->loc == loc
7385 && loc->next == NULL && b->related_breakpoint == b)
7386 {
7387 /* Create only the whole new breakpoint of this type but do not
7388 mess more complicated breakpoints with multiple locations. */
7389 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7390 /* Remember the resolver's address for use by the return
7391 breakpoint. */
7392 loc->related_address = func_addr;
0e30163f
JK
7393 }
7394 }
7395
2c02bd72
DE
7396 if (function_name)
7397 loc->function_name = xstrdup (function_name);
0d381245
VP
7398 }
7399}
7400
a6d9a66e 7401/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7402struct gdbarch *
a6d9a66e
UW
7403get_sal_arch (struct symtab_and_line sal)
7404{
7405 if (sal.section)
7406 return get_objfile_arch (sal.section->objfile);
7407 if (sal.symtab)
7408 return get_objfile_arch (sal.symtab->objfile);
7409
7410 return NULL;
7411}
7412
346774a9
PA
7413/* Low level routine for partially initializing a breakpoint of type
7414 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7415 file name, and line number are provided by SAL.
0d381245
VP
7416
7417 It is expected that the caller will complete the initialization of
7418 the newly created breakpoint struct as well as output any status
c56053d2 7419 information regarding the creation of a new breakpoint. */
0d381245 7420
346774a9
PA
7421static void
7422init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7423 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7424 const struct breakpoint_ops *ops)
0d381245 7425{
28010a5d 7426 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7427
3742cc8b 7428 add_location_to_breakpoint (b, &sal);
0d381245 7429
6c95b8df
PA
7430 if (bptype != bp_catchpoint)
7431 gdb_assert (sal.pspace != NULL);
7432
f8eba3c6
TT
7433 /* Store the program space that was used to set the breakpoint,
7434 except for ordinary breakpoints, which are independent of the
7435 program space. */
7436 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7437 b->pspace = sal.pspace;
346774a9 7438}
c906108c 7439
346774a9
PA
7440/* set_raw_breakpoint is a low level routine for allocating and
7441 partially initializing a breakpoint of type BPTYPE. The newly
7442 created breakpoint's address, section, source file name, and line
7443 number are provided by SAL. The newly created and partially
7444 initialized breakpoint is added to the breakpoint chain and
7445 is also returned as the value of this function.
7446
7447 It is expected that the caller will complete the initialization of
7448 the newly created breakpoint struct as well as output any status
7449 information regarding the creation of a new breakpoint. In
7450 particular, set_raw_breakpoint does NOT set the breakpoint
7451 number! Care should be taken to not allow an error to occur
7452 prior to completing the initialization of the breakpoint. If this
7453 should happen, a bogus breakpoint will be left on the chain. */
7454
7455struct breakpoint *
7456set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7457 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7458 const struct breakpoint_ops *ops)
346774a9
PA
7459{
7460 struct breakpoint *b = XNEW (struct breakpoint);
7461
348d480f 7462 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7463 add_to_breakpoint_chain (b);
c906108c
SS
7464 return b;
7465}
7466
c2c6d25f
JM
7467
7468/* Note that the breakpoint object B describes a permanent breakpoint
7469 instruction, hard-wired into the inferior's code. */
7470void
7471make_breakpoint_permanent (struct breakpoint *b)
7472{
0d381245 7473 struct bp_location *bl;
cc59ec59 7474
b5de0fa7 7475 b->enable_state = bp_permanent;
c2c6d25f 7476
4a64f543
MS
7477 /* By definition, permanent breakpoints are already present in the
7478 code. Mark all locations as inserted. For now,
7479 make_breakpoint_permanent is called in just one place, so it's
7480 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7481 multiple locations or not, but it's easy to implement. */
0d381245
VP
7482 for (bl = b->loc; bl; bl = bl->next)
7483 bl->inserted = 1;
c2c6d25f
JM
7484}
7485
53a5351d 7486/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7487 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7488 initiated the operation. */
c906108c
SS
7489
7490void
186c406b 7491set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7492{
35df4500 7493 struct breakpoint *b, *b_tmp;
186c406b 7494 int thread = tp->num;
0fd8e87f
UW
7495
7496 /* To avoid having to rescan all objfile symbols at every step,
7497 we maintain a list of continually-inserted but always disabled
7498 longjmp "master" breakpoints. Here, we simply create momentary
7499 clones of those and enable them for the requested thread. */
35df4500 7500 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7501 if (b->pspace == current_program_space
186c406b
TT
7502 && (b->type == bp_longjmp_master
7503 || b->type == bp_exception_master))
0fd8e87f 7504 {
06edf0c0
PA
7505 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7506 struct breakpoint *clone;
cc59ec59 7507
e2e4d78b
JK
7508 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7509 after their removal. */
06edf0c0 7510 clone = momentary_breakpoint_from_master (b, type,
a1aa2221 7511 &longjmp_breakpoint_ops, 1);
0fd8e87f
UW
7512 clone->thread = thread;
7513 }
186c406b
TT
7514
7515 tp->initiating_frame = frame;
c906108c
SS
7516}
7517
611c83ae 7518/* Delete all longjmp breakpoints from THREAD. */
c906108c 7519void
611c83ae 7520delete_longjmp_breakpoint (int thread)
c906108c 7521{
35df4500 7522 struct breakpoint *b, *b_tmp;
c906108c 7523
35df4500 7524 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7525 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7526 {
7527 if (b->thread == thread)
7528 delete_breakpoint (b);
7529 }
c906108c
SS
7530}
7531
f59f708a
PA
7532void
7533delete_longjmp_breakpoint_at_next_stop (int thread)
7534{
7535 struct breakpoint *b, *b_tmp;
7536
7537 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7538 if (b->type == bp_longjmp || b->type == bp_exception)
7539 {
7540 if (b->thread == thread)
7541 b->disposition = disp_del_at_next_stop;
7542 }
7543}
7544
e2e4d78b
JK
7545/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7546 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7547 pointer to any of them. Return NULL if this system cannot place longjmp
7548 breakpoints. */
7549
7550struct breakpoint *
7551set_longjmp_breakpoint_for_call_dummy (void)
7552{
7553 struct breakpoint *b, *retval = NULL;
7554
7555 ALL_BREAKPOINTS (b)
7556 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7557 {
7558 struct breakpoint *new_b;
7559
7560 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
a1aa2221
LM
7561 &momentary_breakpoint_ops,
7562 1);
e2e4d78b
JK
7563 new_b->thread = pid_to_thread_id (inferior_ptid);
7564
7565 /* Link NEW_B into the chain of RETVAL breakpoints. */
7566
7567 gdb_assert (new_b->related_breakpoint == new_b);
7568 if (retval == NULL)
7569 retval = new_b;
7570 new_b->related_breakpoint = retval;
7571 while (retval->related_breakpoint != new_b->related_breakpoint)
7572 retval = retval->related_breakpoint;
7573 retval->related_breakpoint = new_b;
7574 }
7575
7576 return retval;
7577}
7578
7579/* Verify all existing dummy frames and their associated breakpoints for
b67a2c6f 7580 TP. Remove those which can no longer be found in the current frame
e2e4d78b
JK
7581 stack.
7582
7583 You should call this function only at places where it is safe to currently
7584 unwind the whole stack. Failed stack unwind would discard live dummy
7585 frames. */
7586
7587void
b67a2c6f 7588check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
e2e4d78b
JK
7589{
7590 struct breakpoint *b, *b_tmp;
7591
7592 ALL_BREAKPOINTS_SAFE (b, b_tmp)
b67a2c6f 7593 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
e2e4d78b
JK
7594 {
7595 struct breakpoint *dummy_b = b->related_breakpoint;
7596
7597 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7598 dummy_b = dummy_b->related_breakpoint;
7599 if (dummy_b->type != bp_call_dummy
7600 || frame_find_by_id (dummy_b->frame_id) != NULL)
7601 continue;
7602
b67a2c6f 7603 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
e2e4d78b
JK
7604
7605 while (b->related_breakpoint != b)
7606 {
7607 if (b_tmp == b->related_breakpoint)
7608 b_tmp = b->related_breakpoint->next;
7609 delete_breakpoint (b->related_breakpoint);
7610 }
7611 delete_breakpoint (b);
7612 }
7613}
7614
1900040c
MS
7615void
7616enable_overlay_breakpoints (void)
7617{
52f0bd74 7618 struct breakpoint *b;
1900040c
MS
7619
7620 ALL_BREAKPOINTS (b)
7621 if (b->type == bp_overlay_event)
7622 {
7623 b->enable_state = bp_enabled;
44702360 7624 update_global_location_list (UGLL_MAY_INSERT);
c02f5703 7625 overlay_events_enabled = 1;
1900040c
MS
7626 }
7627}
7628
7629void
7630disable_overlay_breakpoints (void)
7631{
52f0bd74 7632 struct breakpoint *b;
1900040c
MS
7633
7634 ALL_BREAKPOINTS (b)
7635 if (b->type == bp_overlay_event)
7636 {
7637 b->enable_state = bp_disabled;
44702360 7638 update_global_location_list (UGLL_DONT_INSERT);
c02f5703 7639 overlay_events_enabled = 0;
1900040c
MS
7640 }
7641}
7642
aa7d318d
TT
7643/* Set an active std::terminate breakpoint for each std::terminate
7644 master breakpoint. */
7645void
7646set_std_terminate_breakpoint (void)
7647{
35df4500 7648 struct breakpoint *b, *b_tmp;
aa7d318d 7649
35df4500 7650 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7651 if (b->pspace == current_program_space
7652 && b->type == bp_std_terminate_master)
7653 {
06edf0c0 7654 momentary_breakpoint_from_master (b, bp_std_terminate,
a1aa2221 7655 &momentary_breakpoint_ops, 1);
aa7d318d
TT
7656 }
7657}
7658
7659/* Delete all the std::terminate breakpoints. */
7660void
7661delete_std_terminate_breakpoint (void)
7662{
35df4500 7663 struct breakpoint *b, *b_tmp;
aa7d318d 7664
35df4500 7665 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7666 if (b->type == bp_std_terminate)
7667 delete_breakpoint (b);
7668}
7669
c4093a6a 7670struct breakpoint *
a6d9a66e 7671create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7672{
7673 struct breakpoint *b;
c4093a6a 7674
06edf0c0
PA
7675 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7676 &internal_breakpoint_ops);
7677
b5de0fa7 7678 b->enable_state = bp_enabled;
c4093a6a 7679 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7680 b->addr_string
7681 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7682
44702360 7683 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 7684
c4093a6a
JM
7685 return b;
7686}
7687
7688void
7689remove_thread_event_breakpoints (void)
7690{
35df4500 7691 struct breakpoint *b, *b_tmp;
c4093a6a 7692
35df4500 7693 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7694 if (b->type == bp_thread_event
7695 && b->loc->pspace == current_program_space)
c4093a6a
JM
7696 delete_breakpoint (b);
7697}
7698
0101ce28
JJ
7699struct lang_and_radix
7700 {
7701 enum language lang;
7702 int radix;
7703 };
7704
4efc6507
DE
7705/* Create a breakpoint for JIT code registration and unregistration. */
7706
7707struct breakpoint *
7708create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7709{
7710 struct breakpoint *b;
7711
06edf0c0
PA
7712 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7713 &internal_breakpoint_ops);
44702360 7714 update_global_location_list_nothrow (UGLL_MAY_INSERT);
4efc6507
DE
7715 return b;
7716}
0101ce28 7717
03673fc7
PP
7718/* Remove JIT code registration and unregistration breakpoint(s). */
7719
7720void
7721remove_jit_event_breakpoints (void)
7722{
7723 struct breakpoint *b, *b_tmp;
7724
7725 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7726 if (b->type == bp_jit_event
7727 && b->loc->pspace == current_program_space)
7728 delete_breakpoint (b);
7729}
7730
cae688ec
JJ
7731void
7732remove_solib_event_breakpoints (void)
7733{
35df4500 7734 struct breakpoint *b, *b_tmp;
cae688ec 7735
35df4500 7736 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7737 if (b->type == bp_shlib_event
7738 && b->loc->pspace == current_program_space)
cae688ec
JJ
7739 delete_breakpoint (b);
7740}
7741
f37f681c
PA
7742/* See breakpoint.h. */
7743
7744void
7745remove_solib_event_breakpoints_at_next_stop (void)
7746{
7747 struct breakpoint *b, *b_tmp;
7748
7749 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7750 if (b->type == bp_shlib_event
7751 && b->loc->pspace == current_program_space)
7752 b->disposition = disp_del_at_next_stop;
7753}
7754
04086b45
PA
7755/* Helper for create_solib_event_breakpoint /
7756 create_and_insert_solib_event_breakpoint. Allows specifying which
7757 INSERT_MODE to pass through to update_global_location_list. */
7758
7759static struct breakpoint *
7760create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7761 enum ugll_insert_mode insert_mode)
cae688ec
JJ
7762{
7763 struct breakpoint *b;
7764
06edf0c0
PA
7765 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7766 &internal_breakpoint_ops);
04086b45 7767 update_global_location_list_nothrow (insert_mode);
cae688ec
JJ
7768 return b;
7769}
7770
04086b45
PA
7771struct breakpoint *
7772create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7773{
7774 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7775}
7776
f37f681c
PA
7777/* See breakpoint.h. */
7778
7779struct breakpoint *
7780create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7781{
7782 struct breakpoint *b;
7783
04086b45
PA
7784 /* Explicitly tell update_global_location_list to insert
7785 locations. */
7786 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
f37f681c
PA
7787 if (!b->loc->inserted)
7788 {
7789 delete_breakpoint (b);
7790 return NULL;
7791 }
7792 return b;
7793}
7794
cae688ec
JJ
7795/* Disable any breakpoints that are on code in shared libraries. Only
7796 apply to enabled breakpoints, disabled ones can just stay disabled. */
7797
7798void
cb851954 7799disable_breakpoints_in_shlibs (void)
cae688ec 7800{
876fa593 7801 struct bp_location *loc, **locp_tmp;
cae688ec 7802
876fa593 7803 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7804 {
2bdf28a0 7805 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7806 struct breakpoint *b = loc->owner;
2bdf28a0 7807
4a64f543
MS
7808 /* We apply the check to all breakpoints, including disabled for
7809 those with loc->duplicate set. This is so that when breakpoint
7810 becomes enabled, or the duplicate is removed, gdb will try to
7811 insert all breakpoints. If we don't set shlib_disabled here,
7812 we'll try to insert those breakpoints and fail. */
1042e4c0 7813 if (((b->type == bp_breakpoint)
508ccb1f 7814 || (b->type == bp_jit_event)
1042e4c0 7815 || (b->type == bp_hardware_breakpoint)
d77f58be 7816 || (is_tracepoint (b)))
6c95b8df 7817 && loc->pspace == current_program_space
0d381245 7818 && !loc->shlib_disabled
6c95b8df 7819 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7820 )
0d381245
VP
7821 {
7822 loc->shlib_disabled = 1;
7823 }
cae688ec
JJ
7824 }
7825}
7826
63644780
NB
7827/* Disable any breakpoints and tracepoints that are in SOLIB upon
7828 notification of unloaded_shlib. Only apply to enabled breakpoints,
7829 disabled ones can just stay disabled. */
84acb35a 7830
75149521 7831static void
84acb35a
JJ
7832disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7833{
876fa593 7834 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7835 int disabled_shlib_breaks = 0;
7836
c86cf029
VP
7837 /* SunOS a.out shared libraries are always mapped, so do not
7838 disable breakpoints; they will only be reported as unloaded
7839 through clear_solib when GDB discards its shared library
7840 list. See clear_solib for more information. */
7841 if (exec_bfd != NULL
7842 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7843 return;
7844
876fa593 7845 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7846 {
2bdf28a0 7847 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7848 struct breakpoint *b = loc->owner;
cc59ec59 7849
1e4d1764 7850 if (solib->pspace == loc->pspace
e2dd7057 7851 && !loc->shlib_disabled
1e4d1764
YQ
7852 && (((b->type == bp_breakpoint
7853 || b->type == bp_jit_event
7854 || b->type == bp_hardware_breakpoint)
7855 && (loc->loc_type == bp_loc_hardware_breakpoint
7856 || loc->loc_type == bp_loc_software_breakpoint))
7857 || is_tracepoint (b))
e2dd7057 7858 && solib_contains_address_p (solib, loc->address))
84acb35a 7859 {
e2dd7057
PP
7860 loc->shlib_disabled = 1;
7861 /* At this point, we cannot rely on remove_breakpoint
7862 succeeding so we must mark the breakpoint as not inserted
7863 to prevent future errors occurring in remove_breakpoints. */
7864 loc->inserted = 0;
8d3788bd
VP
7865
7866 /* This may cause duplicate notifications for the same breakpoint. */
7867 observer_notify_breakpoint_modified (b);
7868
e2dd7057
PP
7869 if (!disabled_shlib_breaks)
7870 {
7871 target_terminal_ours_for_output ();
3e43a32a
MS
7872 warning (_("Temporarily disabling breakpoints "
7873 "for unloaded shared library \"%s\""),
e2dd7057 7874 solib->so_name);
84acb35a 7875 }
e2dd7057 7876 disabled_shlib_breaks = 1;
84acb35a
JJ
7877 }
7878 }
84acb35a
JJ
7879}
7880
63644780
NB
7881/* Disable any breakpoints and tracepoints in OBJFILE upon
7882 notification of free_objfile. Only apply to enabled breakpoints,
7883 disabled ones can just stay disabled. */
7884
7885static void
7886disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7887{
7888 struct breakpoint *b;
7889
7890 if (objfile == NULL)
7891 return;
7892
d03de421
PA
7893 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7894 managed by the user with add-symbol-file/remove-symbol-file.
7895 Similarly to how breakpoints in shared libraries are handled in
7896 response to "nosharedlibrary", mark breakpoints in such modules
08351840
PA
7897 shlib_disabled so they end up uninserted on the next global
7898 location list update. Shared libraries not loaded by the user
7899 aren't handled here -- they're already handled in
7900 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7901 solib_unloaded observer. We skip objfiles that are not
d03de421
PA
7902 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7903 main objfile). */
7904 if ((objfile->flags & OBJF_SHARED) == 0
7905 || (objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7906 return;
7907
7908 ALL_BREAKPOINTS (b)
7909 {
7910 struct bp_location *loc;
7911 int bp_modified = 0;
7912
7913 if (!is_breakpoint (b) && !is_tracepoint (b))
7914 continue;
7915
7916 for (loc = b->loc; loc != NULL; loc = loc->next)
7917 {
7918 CORE_ADDR loc_addr = loc->address;
7919
7920 if (loc->loc_type != bp_loc_hardware_breakpoint
7921 && loc->loc_type != bp_loc_software_breakpoint)
7922 continue;
7923
7924 if (loc->shlib_disabled != 0)
7925 continue;
7926
7927 if (objfile->pspace != loc->pspace)
7928 continue;
7929
7930 if (loc->loc_type != bp_loc_hardware_breakpoint
7931 && loc->loc_type != bp_loc_software_breakpoint)
7932 continue;
7933
7934 if (is_addr_in_objfile (loc_addr, objfile))
7935 {
7936 loc->shlib_disabled = 1;
08351840
PA
7937 /* At this point, we don't know whether the object was
7938 unmapped from the inferior or not, so leave the
7939 inserted flag alone. We'll handle failure to
7940 uninsert quietly, in case the object was indeed
7941 unmapped. */
63644780
NB
7942
7943 mark_breakpoint_location_modified (loc);
7944
7945 bp_modified = 1;
7946 }
7947 }
7948
7949 if (bp_modified)
7950 observer_notify_breakpoint_modified (b);
7951 }
7952}
7953
ce78b96d
JB
7954/* FORK & VFORK catchpoints. */
7955
e29a4733
PA
7956/* An instance of this type is used to represent a fork or vfork
7957 catchpoint. It includes a "struct breakpoint" as a kind of base
7958 class; users downcast to "struct breakpoint *" when needed. A
7959 breakpoint is really of this type iff its ops pointer points to
7960 CATCH_FORK_BREAKPOINT_OPS. */
7961
7962struct fork_catchpoint
7963{
7964 /* The base class. */
7965 struct breakpoint base;
7966
7967 /* Process id of a child process whose forking triggered this
7968 catchpoint. This field is only valid immediately after this
7969 catchpoint has triggered. */
7970 ptid_t forked_inferior_pid;
7971};
7972
4a64f543
MS
7973/* Implement the "insert" breakpoint_ops method for fork
7974 catchpoints. */
ce78b96d 7975
77b06cd7
TJB
7976static int
7977insert_catch_fork (struct bp_location *bl)
ce78b96d 7978{
dfd4cc63 7979 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7980}
7981
4a64f543
MS
7982/* Implement the "remove" breakpoint_ops method for fork
7983 catchpoints. */
ce78b96d
JB
7984
7985static int
77b06cd7 7986remove_catch_fork (struct bp_location *bl)
ce78b96d 7987{
dfd4cc63 7988 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7989}
7990
7991/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7992 catchpoints. */
7993
7994static int
f1310107 7995breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7996 struct address_space *aspace, CORE_ADDR bp_addr,
7997 const struct target_waitstatus *ws)
ce78b96d 7998{
e29a4733
PA
7999 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8000
f90263c1
TT
8001 if (ws->kind != TARGET_WAITKIND_FORKED)
8002 return 0;
8003
8004 c->forked_inferior_pid = ws->value.related_pid;
8005 return 1;
ce78b96d
JB
8006}
8007
4a64f543
MS
8008/* Implement the "print_it" breakpoint_ops method for fork
8009 catchpoints. */
ce78b96d
JB
8010
8011static enum print_stop_action
348d480f 8012print_it_catch_fork (bpstat bs)
ce78b96d 8013{
36dfb11c 8014 struct ui_out *uiout = current_uiout;
348d480f
PA
8015 struct breakpoint *b = bs->breakpoint_at;
8016 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 8017
ce78b96d 8018 annotate_catchpoint (b->number);
36dfb11c
TT
8019 if (b->disposition == disp_del)
8020 ui_out_text (uiout, "\nTemporary catchpoint ");
8021 else
8022 ui_out_text (uiout, "\nCatchpoint ");
8023 if (ui_out_is_mi_like_p (uiout))
8024 {
8025 ui_out_field_string (uiout, "reason",
8026 async_reason_lookup (EXEC_ASYNC_FORK));
8027 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8028 }
8029 ui_out_field_int (uiout, "bkptno", b->number);
8030 ui_out_text (uiout, " (forked process ");
8031 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8032 ui_out_text (uiout, "), ");
ce78b96d
JB
8033 return PRINT_SRC_AND_LOC;
8034}
8035
4a64f543
MS
8036/* Implement the "print_one" breakpoint_ops method for fork
8037 catchpoints. */
ce78b96d
JB
8038
8039static void
a6d9a66e 8040print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8041{
e29a4733 8042 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8043 struct value_print_options opts;
79a45e25 8044 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8045
8046 get_user_print_options (&opts);
8047
4a64f543
MS
8048 /* Field 4, the address, is omitted (which makes the columns not
8049 line up too nicely with the headers, but the effect is relatively
8050 readable). */
79a45b7d 8051 if (opts.addressprint)
ce78b96d
JB
8052 ui_out_field_skip (uiout, "addr");
8053 annotate_field (5);
8054 ui_out_text (uiout, "fork");
e29a4733 8055 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8056 {
8057 ui_out_text (uiout, ", process ");
8058 ui_out_field_int (uiout, "what",
e29a4733 8059 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8060 ui_out_spaces (uiout, 1);
8061 }
8ac3646f
TT
8062
8063 if (ui_out_is_mi_like_p (uiout))
8064 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
8065}
8066
8067/* Implement the "print_mention" breakpoint_ops method for fork
8068 catchpoints. */
8069
8070static void
8071print_mention_catch_fork (struct breakpoint *b)
8072{
8073 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8074}
8075
6149aea9
PA
8076/* Implement the "print_recreate" breakpoint_ops method for fork
8077 catchpoints. */
8078
8079static void
8080print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8081{
8082 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 8083 print_recreate_thread (b, fp);
6149aea9
PA
8084}
8085
ce78b96d
JB
8086/* The breakpoint_ops structure to be used in fork catchpoints. */
8087
2060206e 8088static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 8089
4a64f543
MS
8090/* Implement the "insert" breakpoint_ops method for vfork
8091 catchpoints. */
ce78b96d 8092
77b06cd7
TJB
8093static int
8094insert_catch_vfork (struct bp_location *bl)
ce78b96d 8095{
dfd4cc63 8096 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8097}
8098
4a64f543
MS
8099/* Implement the "remove" breakpoint_ops method for vfork
8100 catchpoints. */
ce78b96d
JB
8101
8102static int
77b06cd7 8103remove_catch_vfork (struct bp_location *bl)
ce78b96d 8104{
dfd4cc63 8105 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
8106}
8107
8108/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8109 catchpoints. */
8110
8111static int
f1310107 8112breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
8113 struct address_space *aspace, CORE_ADDR bp_addr,
8114 const struct target_waitstatus *ws)
ce78b96d 8115{
e29a4733
PA
8116 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8117
f90263c1
TT
8118 if (ws->kind != TARGET_WAITKIND_VFORKED)
8119 return 0;
8120
8121 c->forked_inferior_pid = ws->value.related_pid;
8122 return 1;
ce78b96d
JB
8123}
8124
4a64f543
MS
8125/* Implement the "print_it" breakpoint_ops method for vfork
8126 catchpoints. */
ce78b96d
JB
8127
8128static enum print_stop_action
348d480f 8129print_it_catch_vfork (bpstat bs)
ce78b96d 8130{
36dfb11c 8131 struct ui_out *uiout = current_uiout;
348d480f 8132 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
8133 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8134
ce78b96d 8135 annotate_catchpoint (b->number);
36dfb11c
TT
8136 if (b->disposition == disp_del)
8137 ui_out_text (uiout, "\nTemporary catchpoint ");
8138 else
8139 ui_out_text (uiout, "\nCatchpoint ");
8140 if (ui_out_is_mi_like_p (uiout))
8141 {
8142 ui_out_field_string (uiout, "reason",
8143 async_reason_lookup (EXEC_ASYNC_VFORK));
8144 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8145 }
8146 ui_out_field_int (uiout, "bkptno", b->number);
8147 ui_out_text (uiout, " (vforked process ");
8148 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8149 ui_out_text (uiout, "), ");
ce78b96d
JB
8150 return PRINT_SRC_AND_LOC;
8151}
8152
4a64f543
MS
8153/* Implement the "print_one" breakpoint_ops method for vfork
8154 catchpoints. */
ce78b96d
JB
8155
8156static void
a6d9a66e 8157print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 8158{
e29a4733 8159 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 8160 struct value_print_options opts;
79a45e25 8161 struct ui_out *uiout = current_uiout;
79a45b7d
TT
8162
8163 get_user_print_options (&opts);
4a64f543
MS
8164 /* Field 4, the address, is omitted (which makes the columns not
8165 line up too nicely with the headers, but the effect is relatively
8166 readable). */
79a45b7d 8167 if (opts.addressprint)
ce78b96d
JB
8168 ui_out_field_skip (uiout, "addr");
8169 annotate_field (5);
8170 ui_out_text (uiout, "vfork");
e29a4733 8171 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
8172 {
8173 ui_out_text (uiout, ", process ");
8174 ui_out_field_int (uiout, "what",
e29a4733 8175 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
8176 ui_out_spaces (uiout, 1);
8177 }
8ac3646f
TT
8178
8179 if (ui_out_is_mi_like_p (uiout))
8180 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
8181}
8182
8183/* Implement the "print_mention" breakpoint_ops method for vfork
8184 catchpoints. */
8185
8186static void
8187print_mention_catch_vfork (struct breakpoint *b)
8188{
8189 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8190}
8191
6149aea9
PA
8192/* Implement the "print_recreate" breakpoint_ops method for vfork
8193 catchpoints. */
8194
8195static void
8196print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8197{
8198 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8199 print_recreate_thread (b, fp);
6149aea9
PA
8200}
8201
ce78b96d
JB
8202/* The breakpoint_ops structure to be used in vfork catchpoints. */
8203
2060206e 8204static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8205
edcc5120
TT
8206/* An instance of this type is used to represent an solib catchpoint.
8207 It includes a "struct breakpoint" as a kind of base class; users
8208 downcast to "struct breakpoint *" when needed. A breakpoint is
8209 really of this type iff its ops pointer points to
8210 CATCH_SOLIB_BREAKPOINT_OPS. */
8211
8212struct solib_catchpoint
8213{
8214 /* The base class. */
8215 struct breakpoint base;
8216
8217 /* True for "catch load", false for "catch unload". */
8218 unsigned char is_load;
8219
8220 /* Regular expression to match, if any. COMPILED is only valid when
8221 REGEX is non-NULL. */
8222 char *regex;
8223 regex_t compiled;
8224};
8225
8226static void
8227dtor_catch_solib (struct breakpoint *b)
8228{
8229 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8230
8231 if (self->regex)
8232 regfree (&self->compiled);
8233 xfree (self->regex);
8234
8235 base_breakpoint_ops.dtor (b);
8236}
8237
8238static int
8239insert_catch_solib (struct bp_location *ignore)
8240{
8241 return 0;
8242}
8243
8244static int
8245remove_catch_solib (struct bp_location *ignore)
8246{
8247 return 0;
8248}
8249
8250static int
8251breakpoint_hit_catch_solib (const struct bp_location *bl,
8252 struct address_space *aspace,
8253 CORE_ADDR bp_addr,
8254 const struct target_waitstatus *ws)
8255{
8256 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8257 struct breakpoint *other;
8258
8259 if (ws->kind == TARGET_WAITKIND_LOADED)
8260 return 1;
8261
8262 ALL_BREAKPOINTS (other)
8263 {
8264 struct bp_location *other_bl;
8265
8266 if (other == bl->owner)
8267 continue;
8268
8269 if (other->type != bp_shlib_event)
8270 continue;
8271
8272 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8273 continue;
8274
8275 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8276 {
8277 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8278 return 1;
8279 }
8280 }
8281
8282 return 0;
8283}
8284
8285static void
8286check_status_catch_solib (struct bpstats *bs)
8287{
8288 struct solib_catchpoint *self
8289 = (struct solib_catchpoint *) bs->breakpoint_at;
8290 int ix;
8291
8292 if (self->is_load)
8293 {
8294 struct so_list *iter;
8295
8296 for (ix = 0;
8297 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8298 ix, iter);
8299 ++ix)
8300 {
8301 if (!self->regex
8302 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8303 return;
8304 }
8305 }
8306 else
8307 {
8308 char *iter;
8309
8310 for (ix = 0;
8311 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8312 ix, iter);
8313 ++ix)
8314 {
8315 if (!self->regex
8316 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8317 return;
8318 }
8319 }
8320
8321 bs->stop = 0;
8322 bs->print_it = print_it_noop;
8323}
8324
8325static enum print_stop_action
8326print_it_catch_solib (bpstat bs)
8327{
8328 struct breakpoint *b = bs->breakpoint_at;
8329 struct ui_out *uiout = current_uiout;
8330
8331 annotate_catchpoint (b->number);
8332 if (b->disposition == disp_del)
8333 ui_out_text (uiout, "\nTemporary catchpoint ");
8334 else
8335 ui_out_text (uiout, "\nCatchpoint ");
8336 ui_out_field_int (uiout, "bkptno", b->number);
8337 ui_out_text (uiout, "\n");
8338 if (ui_out_is_mi_like_p (uiout))
8339 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8340 print_solib_event (1);
8341 return PRINT_SRC_AND_LOC;
8342}
8343
8344static void
8345print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8346{
8347 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8348 struct value_print_options opts;
8349 struct ui_out *uiout = current_uiout;
8350 char *msg;
8351
8352 get_user_print_options (&opts);
8353 /* Field 4, the address, is omitted (which makes the columns not
8354 line up too nicely with the headers, but the effect is relatively
8355 readable). */
8356 if (opts.addressprint)
8357 {
8358 annotate_field (4);
8359 ui_out_field_skip (uiout, "addr");
8360 }
8361
8362 annotate_field (5);
8363 if (self->is_load)
8364 {
8365 if (self->regex)
8366 msg = xstrprintf (_("load of library matching %s"), self->regex);
8367 else
8368 msg = xstrdup (_("load of library"));
8369 }
8370 else
8371 {
8372 if (self->regex)
8373 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8374 else
8375 msg = xstrdup (_("unload of library"));
8376 }
8377 ui_out_field_string (uiout, "what", msg);
8378 xfree (msg);
8ac3646f
TT
8379
8380 if (ui_out_is_mi_like_p (uiout))
8381 ui_out_field_string (uiout, "catch-type",
8382 self->is_load ? "load" : "unload");
edcc5120
TT
8383}
8384
8385static void
8386print_mention_catch_solib (struct breakpoint *b)
8387{
8388 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8389
8390 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8391 self->is_load ? "load" : "unload");
8392}
8393
8394static void
8395print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8396{
8397 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8398
8399 fprintf_unfiltered (fp, "%s %s",
8400 b->disposition == disp_del ? "tcatch" : "catch",
8401 self->is_load ? "load" : "unload");
8402 if (self->regex)
8403 fprintf_unfiltered (fp, " %s", self->regex);
8404 fprintf_unfiltered (fp, "\n");
8405}
8406
8407static struct breakpoint_ops catch_solib_breakpoint_ops;
8408
91985142
MG
8409/* Shared helper function (MI and CLI) for creating and installing
8410 a shared object event catchpoint. If IS_LOAD is non-zero then
8411 the events to be caught are load events, otherwise they are
8412 unload events. If IS_TEMP is non-zero the catchpoint is a
8413 temporary one. If ENABLED is non-zero the catchpoint is
8414 created in an enabled state. */
edcc5120 8415
91985142
MG
8416void
8417add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8418{
8419 struct solib_catchpoint *c;
8420 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8421 struct cleanup *cleanup;
8422
edcc5120
TT
8423 if (!arg)
8424 arg = "";
8425 arg = skip_spaces (arg);
8426
8427 c = XCNEW (struct solib_catchpoint);
8428 cleanup = make_cleanup (xfree, c);
8429
8430 if (*arg != '\0')
8431 {
8432 int errcode;
8433
8434 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8435 if (errcode != 0)
8436 {
8437 char *err = get_regcomp_error (errcode, &c->compiled);
8438
8439 make_cleanup (xfree, err);
8440 error (_("Invalid regexp (%s): %s"), err, arg);
8441 }
8442 c->regex = xstrdup (arg);
8443 }
8444
8445 c->is_load = is_load;
91985142 8446 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8447 &catch_solib_breakpoint_ops);
8448
91985142
MG
8449 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8450
edcc5120
TT
8451 discard_cleanups (cleanup);
8452 install_breakpoint (0, &c->base, 1);
8453}
8454
91985142
MG
8455/* A helper function that does all the work for "catch load" and
8456 "catch unload". */
8457
8458static void
8459catch_load_or_unload (char *arg, int from_tty, int is_load,
8460 struct cmd_list_element *command)
8461{
8462 int tempflag;
8463 const int enabled = 1;
8464
8465 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8466
8467 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8468}
8469
edcc5120
TT
8470static void
8471catch_load_command_1 (char *arg, int from_tty,
8472 struct cmd_list_element *command)
8473{
8474 catch_load_or_unload (arg, from_tty, 1, command);
8475}
8476
8477static void
8478catch_unload_command_1 (char *arg, int from_tty,
8479 struct cmd_list_element *command)
8480{
8481 catch_load_or_unload (arg, from_tty, 0, command);
8482}
8483
be5c67c1
PA
8484/* An instance of this type is used to represent a syscall catchpoint.
8485 It includes a "struct breakpoint" as a kind of base class; users
8486 downcast to "struct breakpoint *" when needed. A breakpoint is
8487 really of this type iff its ops pointer points to
8488 CATCH_SYSCALL_BREAKPOINT_OPS. */
8489
8490struct syscall_catchpoint
8491{
8492 /* The base class. */
8493 struct breakpoint base;
8494
8495 /* Syscall numbers used for the 'catch syscall' feature. If no
8496 syscall has been specified for filtering, its value is NULL.
8497 Otherwise, it holds a list of all syscalls to be caught. The
8498 list elements are allocated with xmalloc. */
8499 VEC(int) *syscalls_to_be_caught;
8500};
8501
8502/* Implement the "dtor" breakpoint_ops method for syscall
8503 catchpoints. */
8504
8505static void
8506dtor_catch_syscall (struct breakpoint *b)
8507{
8508 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8509
8510 VEC_free (int, c->syscalls_to_be_caught);
348d480f 8511
2060206e 8512 base_breakpoint_ops.dtor (b);
be5c67c1
PA
8513}
8514
fa3064dd
YQ
8515static const struct inferior_data *catch_syscall_inferior_data = NULL;
8516
8517struct catch_syscall_inferior_data
8518{
8519 /* We keep a count of the number of times the user has requested a
8520 particular syscall to be tracked, and pass this information to the
8521 target. This lets capable targets implement filtering directly. */
8522
8523 /* Number of times that "any" syscall is requested. */
8524 int any_syscall_count;
8525
8526 /* Count of each system call. */
8527 VEC(int) *syscalls_counts;
8528
8529 /* This counts all syscall catch requests, so we can readily determine
8530 if any catching is necessary. */
8531 int total_syscalls_count;
8532};
8533
8534static struct catch_syscall_inferior_data*
8535get_catch_syscall_inferior_data (struct inferior *inf)
8536{
8537 struct catch_syscall_inferior_data *inf_data;
8538
8539 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8540 if (inf_data == NULL)
8541 {
41bf6aca 8542 inf_data = XCNEW (struct catch_syscall_inferior_data);
fa3064dd
YQ
8543 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8544 }
8545
8546 return inf_data;
8547}
8548
8549static void
8550catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8551{
8552 xfree (arg);
8553}
8554
8555
a96d9b2e
SDJ
8556/* Implement the "insert" breakpoint_ops method for syscall
8557 catchpoints. */
8558
77b06cd7
TJB
8559static int
8560insert_catch_syscall (struct bp_location *bl)
a96d9b2e 8561{
be5c67c1 8562 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8563 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8564 struct catch_syscall_inferior_data *inf_data
8565 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8566
fa3064dd 8567 ++inf_data->total_syscalls_count;
be5c67c1 8568 if (!c->syscalls_to_be_caught)
fa3064dd 8569 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
8570 else
8571 {
8572 int i, iter;
cc59ec59 8573
a96d9b2e 8574 for (i = 0;
be5c67c1 8575 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8576 i++)
8577 {
8578 int elem;
cc59ec59 8579
fa3064dd 8580 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 8581 {
fa3064dd 8582 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
8583 uintptr_t vec_addr_offset
8584 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 8585 uintptr_t vec_addr;
fa3064dd
YQ
8586 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8587 vec_addr = ((uintptr_t) VEC_address (int,
8588 inf_data->syscalls_counts)
8589 + vec_addr_offset);
a96d9b2e
SDJ
8590 memset ((void *) vec_addr, 0,
8591 (iter + 1 - old_size) * sizeof (int));
8592 }
fa3064dd
YQ
8593 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8594 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
8595 }
8596 }
8597
dfd4cc63 8598 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8599 inf_data->total_syscalls_count != 0,
8600 inf_data->any_syscall_count,
8601 VEC_length (int,
8602 inf_data->syscalls_counts),
8603 VEC_address (int,
8604 inf_data->syscalls_counts));
a96d9b2e
SDJ
8605}
8606
8607/* Implement the "remove" breakpoint_ops method for syscall
8608 catchpoints. */
8609
8610static int
77b06cd7 8611remove_catch_syscall (struct bp_location *bl)
a96d9b2e 8612{
be5c67c1 8613 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8614 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8615 struct catch_syscall_inferior_data *inf_data
8616 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8617
fa3064dd 8618 --inf_data->total_syscalls_count;
be5c67c1 8619 if (!c->syscalls_to_be_caught)
fa3064dd 8620 --inf_data->any_syscall_count;
a96d9b2e
SDJ
8621 else
8622 {
8623 int i, iter;
cc59ec59 8624
a96d9b2e 8625 for (i = 0;
be5c67c1 8626 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8627 i++)
8628 {
8629 int elem;
fa3064dd 8630 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
8631 /* Shouldn't happen. */
8632 continue;
fa3064dd
YQ
8633 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8634 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
8635 }
8636 }
8637
dfd4cc63 8638 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8639 inf_data->total_syscalls_count != 0,
8640 inf_data->any_syscall_count,
8641 VEC_length (int,
8642 inf_data->syscalls_counts),
3e43a32a 8643 VEC_address (int,
fa3064dd 8644 inf_data->syscalls_counts));
a96d9b2e
SDJ
8645}
8646
8647/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8648 catchpoints. */
8649
8650static int
f1310107 8651breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
8652 struct address_space *aspace, CORE_ADDR bp_addr,
8653 const struct target_waitstatus *ws)
a96d9b2e 8654{
4a64f543
MS
8655 /* We must check if we are catching specific syscalls in this
8656 breakpoint. If we are, then we must guarantee that the called
8657 syscall is the same syscall we are catching. */
a96d9b2e 8658 int syscall_number = 0;
be5c67c1
PA
8659 const struct syscall_catchpoint *c
8660 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 8661
f90263c1
TT
8662 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8663 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
8664 return 0;
8665
f90263c1
TT
8666 syscall_number = ws->value.syscall_number;
8667
a96d9b2e 8668 /* Now, checking if the syscall is the same. */
be5c67c1 8669 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8670 {
8671 int i, iter;
cc59ec59 8672
a96d9b2e 8673 for (i = 0;
be5c67c1 8674 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8675 i++)
8676 if (syscall_number == iter)
4924df79
GKB
8677 return 1;
8678
8679 return 0;
a96d9b2e
SDJ
8680 }
8681
8682 return 1;
8683}
8684
8685/* Implement the "print_it" breakpoint_ops method for syscall
8686 catchpoints. */
8687
8688static enum print_stop_action
348d480f 8689print_it_catch_syscall (bpstat bs)
a96d9b2e 8690{
36dfb11c 8691 struct ui_out *uiout = current_uiout;
348d480f 8692 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
8693 /* These are needed because we want to know in which state a
8694 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8695 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8696 must print "called syscall" or "returned from syscall". */
8697 ptid_t ptid;
8698 struct target_waitstatus last;
8699 struct syscall s;
a96d9b2e
SDJ
8700
8701 get_last_target_status (&ptid, &last);
8702
8703 get_syscall_by_number (last.value.syscall_number, &s);
8704
8705 annotate_catchpoint (b->number);
8706
36dfb11c
TT
8707 if (b->disposition == disp_del)
8708 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 8709 else
36dfb11c
TT
8710 ui_out_text (uiout, "\nCatchpoint ");
8711 if (ui_out_is_mi_like_p (uiout))
8712 {
8713 ui_out_field_string (uiout, "reason",
8714 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8715 ? EXEC_ASYNC_SYSCALL_ENTRY
8716 : EXEC_ASYNC_SYSCALL_RETURN));
8717 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8718 }
8719 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
8720
8721 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
8722 ui_out_text (uiout, " (call to syscall ");
8723 else
8724 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 8725
36dfb11c
TT
8726 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8727 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8728 if (s.name != NULL)
8729 ui_out_field_string (uiout, "syscall-name", s.name);
8730
8731 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
8732
8733 return PRINT_SRC_AND_LOC;
8734}
8735
8736/* Implement the "print_one" breakpoint_ops method for syscall
8737 catchpoints. */
8738
8739static void
8740print_one_catch_syscall (struct breakpoint *b,
f1310107 8741 struct bp_location **last_loc)
a96d9b2e 8742{
be5c67c1 8743 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 8744 struct value_print_options opts;
79a45e25 8745 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
8746
8747 get_user_print_options (&opts);
4a64f543
MS
8748 /* Field 4, the address, is omitted (which makes the columns not
8749 line up too nicely with the headers, but the effect is relatively
8750 readable). */
a96d9b2e
SDJ
8751 if (opts.addressprint)
8752 ui_out_field_skip (uiout, "addr");
8753 annotate_field (5);
8754
be5c67c1
PA
8755 if (c->syscalls_to_be_caught
8756 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8757 ui_out_text (uiout, "syscalls \"");
8758 else
8759 ui_out_text (uiout, "syscall \"");
8760
be5c67c1 8761 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8762 {
8763 int i, iter;
8764 char *text = xstrprintf ("%s", "");
cc59ec59 8765
a96d9b2e 8766 for (i = 0;
be5c67c1 8767 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8768 i++)
8769 {
8770 char *x = text;
8771 struct syscall s;
8772 get_syscall_by_number (iter, &s);
8773
8774 if (s.name != NULL)
8775 text = xstrprintf ("%s%s, ", text, s.name);
8776 else
8777 text = xstrprintf ("%s%d, ", text, iter);
8778
8779 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 8780 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
8781 on every call. */
8782 xfree (x);
8783 }
8784 /* Remove the last comma. */
8785 text[strlen (text) - 2] = '\0';
8786 ui_out_field_string (uiout, "what", text);
8787 }
8788 else
8789 ui_out_field_string (uiout, "what", "<any syscall>");
8790 ui_out_text (uiout, "\" ");
8ac3646f
TT
8791
8792 if (ui_out_is_mi_like_p (uiout))
8793 ui_out_field_string (uiout, "catch-type", "syscall");
a96d9b2e
SDJ
8794}
8795
8796/* Implement the "print_mention" breakpoint_ops method for syscall
8797 catchpoints. */
8798
8799static void
8800print_mention_catch_syscall (struct breakpoint *b)
8801{
be5c67c1
PA
8802 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8803
8804 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8805 {
8806 int i, iter;
8807
be5c67c1 8808 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8809 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8810 else
8811 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8812
8813 for (i = 0;
be5c67c1 8814 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8815 i++)
8816 {
8817 struct syscall s;
8818 get_syscall_by_number (iter, &s);
8819
8820 if (s.name)
8821 printf_filtered (" '%s' [%d]", s.name, s.number);
8822 else
8823 printf_filtered (" %d", s.number);
8824 }
8825 printf_filtered (")");
8826 }
8827 else
8828 printf_filtered (_("Catchpoint %d (any syscall)"),
8829 b->number);
8830}
8831
6149aea9
PA
8832/* Implement the "print_recreate" breakpoint_ops method for syscall
8833 catchpoints. */
8834
8835static void
8836print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8837{
be5c67c1
PA
8838 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8839
6149aea9
PA
8840 fprintf_unfiltered (fp, "catch syscall");
8841
be5c67c1 8842 if (c->syscalls_to_be_caught)
6149aea9
PA
8843 {
8844 int i, iter;
8845
8846 for (i = 0;
be5c67c1 8847 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8848 i++)
8849 {
8850 struct syscall s;
8851
8852 get_syscall_by_number (iter, &s);
8853 if (s.name)
8854 fprintf_unfiltered (fp, " %s", s.name);
8855 else
8856 fprintf_unfiltered (fp, " %d", s.number);
8857 }
8858 }
d9b3f62e 8859 print_recreate_thread (b, fp);
6149aea9
PA
8860}
8861
a96d9b2e
SDJ
8862/* The breakpoint_ops structure to be used in syscall catchpoints. */
8863
2060206e 8864static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8865
8866/* Returns non-zero if 'b' is a syscall catchpoint. */
8867
8868static int
8869syscall_catchpoint_p (struct breakpoint *b)
8870{
8871 return (b->ops == &catch_syscall_breakpoint_ops);
8872}
8873
346774a9
PA
8874/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8875 is non-zero, then make the breakpoint temporary. If COND_STRING is
8876 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8877 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8878
ab04a2af 8879void
346774a9
PA
8880init_catchpoint (struct breakpoint *b,
8881 struct gdbarch *gdbarch, int tempflag,
8882 char *cond_string,
c0a91b2b 8883 const struct breakpoint_ops *ops)
c906108c 8884{
c5aa993b 8885 struct symtab_and_line sal;
346774a9 8886
fe39c653 8887 init_sal (&sal);
6c95b8df 8888 sal.pspace = current_program_space;
c5aa993b 8889
28010a5d 8890 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8891
1b36a34b 8892 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8893 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8894}
8895
28010a5d 8896void
3ea46bff 8897install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8898{
8899 add_to_breakpoint_chain (b);
3a5c3e22 8900 set_breakpoint_number (internal, b);
558a9d82
YQ
8901 if (is_tracepoint (b))
8902 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8903 if (!internal)
8904 mention (b);
c56053d2 8905 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8906
8907 if (update_gll)
44702360 8908 update_global_location_list (UGLL_MAY_INSERT);
c56053d2
PA
8909}
8910
9b70b993 8911static void
a6d9a66e
UW
8912create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8913 int tempflag, char *cond_string,
c0a91b2b 8914 const struct breakpoint_ops *ops)
c906108c 8915{
e29a4733 8916 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8917
e29a4733
PA
8918 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8919
8920 c->forked_inferior_pid = null_ptid;
8921
3ea46bff 8922 install_breakpoint (0, &c->base, 1);
c906108c
SS
8923}
8924
fe798b75
JB
8925/* Exec catchpoints. */
8926
b4d90040
PA
8927/* An instance of this type is used to represent an exec catchpoint.
8928 It includes a "struct breakpoint" as a kind of base class; users
8929 downcast to "struct breakpoint *" when needed. A breakpoint is
8930 really of this type iff its ops pointer points to
8931 CATCH_EXEC_BREAKPOINT_OPS. */
8932
8933struct exec_catchpoint
8934{
8935 /* The base class. */
8936 struct breakpoint base;
8937
8938 /* Filename of a program whose exec triggered this catchpoint.
8939 This field is only valid immediately after this catchpoint has
8940 triggered. */
8941 char *exec_pathname;
8942};
8943
8944/* Implement the "dtor" breakpoint_ops method for exec
8945 catchpoints. */
8946
8947static void
8948dtor_catch_exec (struct breakpoint *b)
8949{
8950 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8951
8952 xfree (c->exec_pathname);
348d480f 8953
2060206e 8954 base_breakpoint_ops.dtor (b);
b4d90040
PA
8955}
8956
77b06cd7
TJB
8957static int
8958insert_catch_exec (struct bp_location *bl)
c906108c 8959{
dfd4cc63 8960 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8961}
c906108c 8962
fe798b75 8963static int
77b06cd7 8964remove_catch_exec (struct bp_location *bl)
fe798b75 8965{
dfd4cc63 8966 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8967}
c906108c 8968
fe798b75 8969static int
f1310107 8970breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8971 struct address_space *aspace, CORE_ADDR bp_addr,
8972 const struct target_waitstatus *ws)
fe798b75 8973{
b4d90040
PA
8974 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8975
f90263c1
TT
8976 if (ws->kind != TARGET_WAITKIND_EXECD)
8977 return 0;
8978
8979 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8980 return 1;
fe798b75 8981}
c906108c 8982
fe798b75 8983static enum print_stop_action
348d480f 8984print_it_catch_exec (bpstat bs)
fe798b75 8985{
36dfb11c 8986 struct ui_out *uiout = current_uiout;
348d480f 8987 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8988 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8989
fe798b75 8990 annotate_catchpoint (b->number);
36dfb11c
TT
8991 if (b->disposition == disp_del)
8992 ui_out_text (uiout, "\nTemporary catchpoint ");
8993 else
8994 ui_out_text (uiout, "\nCatchpoint ");
8995 if (ui_out_is_mi_like_p (uiout))
8996 {
8997 ui_out_field_string (uiout, "reason",
8998 async_reason_lookup (EXEC_ASYNC_EXEC));
8999 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9000 }
9001 ui_out_field_int (uiout, "bkptno", b->number);
9002 ui_out_text (uiout, " (exec'd ");
9003 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
9004 ui_out_text (uiout, "), ");
9005
fe798b75 9006 return PRINT_SRC_AND_LOC;
c906108c
SS
9007}
9008
fe798b75 9009static void
a6d9a66e 9010print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 9011{
b4d90040 9012 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 9013 struct value_print_options opts;
79a45e25 9014 struct ui_out *uiout = current_uiout;
fe798b75
JB
9015
9016 get_user_print_options (&opts);
9017
9018 /* Field 4, the address, is omitted (which makes the columns
9019 not line up too nicely with the headers, but the effect
9020 is relatively readable). */
9021 if (opts.addressprint)
9022 ui_out_field_skip (uiout, "addr");
9023 annotate_field (5);
9024 ui_out_text (uiout, "exec");
b4d90040 9025 if (c->exec_pathname != NULL)
fe798b75
JB
9026 {
9027 ui_out_text (uiout, ", program \"");
b4d90040 9028 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
9029 ui_out_text (uiout, "\" ");
9030 }
8ac3646f
TT
9031
9032 if (ui_out_is_mi_like_p (uiout))
9033 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
9034}
9035
9036static void
9037print_mention_catch_exec (struct breakpoint *b)
9038{
9039 printf_filtered (_("Catchpoint %d (exec)"), b->number);
9040}
9041
6149aea9
PA
9042/* Implement the "print_recreate" breakpoint_ops method for exec
9043 catchpoints. */
9044
9045static void
9046print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
9047{
9048 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 9049 print_recreate_thread (b, fp);
6149aea9
PA
9050}
9051
2060206e 9052static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 9053
a96d9b2e
SDJ
9054static void
9055create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 9056 const struct breakpoint_ops *ops)
a96d9b2e 9057{
be5c67c1 9058 struct syscall_catchpoint *c;
a96d9b2e 9059 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 9060
be5c67c1
PA
9061 c = XNEW (struct syscall_catchpoint);
9062 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
9063 c->syscalls_to_be_caught = filter;
a96d9b2e 9064
3ea46bff 9065 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
9066}
9067
c906108c 9068static int
fba45db2 9069hw_breakpoint_used_count (void)
c906108c 9070{
c906108c 9071 int i = 0;
f1310107
TJB
9072 struct breakpoint *b;
9073 struct bp_location *bl;
c906108c
SS
9074
9075 ALL_BREAKPOINTS (b)
c5aa993b 9076 {
d6b74ac4 9077 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
9078 for (bl = b->loc; bl; bl = bl->next)
9079 {
9080 /* Special types of hardware breakpoints may use more than
9081 one register. */
348d480f 9082 i += b->ops->resources_needed (bl);
f1310107 9083 }
c5aa993b 9084 }
c906108c
SS
9085
9086 return i;
9087}
9088
a1398e0c
PA
9089/* Returns the resources B would use if it were a hardware
9090 watchpoint. */
9091
c906108c 9092static int
a1398e0c 9093hw_watchpoint_use_count (struct breakpoint *b)
c906108c 9094{
c906108c 9095 int i = 0;
e09342b5 9096 struct bp_location *bl;
c906108c 9097
a1398e0c
PA
9098 if (!breakpoint_enabled (b))
9099 return 0;
9100
9101 for (bl = b->loc; bl; bl = bl->next)
9102 {
9103 /* Special types of hardware watchpoints may use more than
9104 one register. */
9105 i += b->ops->resources_needed (bl);
9106 }
9107
9108 return i;
9109}
9110
9111/* Returns the sum the used resources of all hardware watchpoints of
9112 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
9113 the sum of the used resources of all hardware watchpoints of other
9114 types _not_ TYPE. */
9115
9116static int
9117hw_watchpoint_used_count_others (struct breakpoint *except,
9118 enum bptype type, int *other_type_used)
9119{
9120 int i = 0;
9121 struct breakpoint *b;
9122
c906108c
SS
9123 *other_type_used = 0;
9124 ALL_BREAKPOINTS (b)
e09342b5 9125 {
a1398e0c
PA
9126 if (b == except)
9127 continue;
e09342b5
TJB
9128 if (!breakpoint_enabled (b))
9129 continue;
9130
a1398e0c
PA
9131 if (b->type == type)
9132 i += hw_watchpoint_use_count (b);
9133 else if (is_hardware_watchpoint (b))
9134 *other_type_used = 1;
e09342b5
TJB
9135 }
9136
c906108c
SS
9137 return i;
9138}
9139
c906108c 9140void
fba45db2 9141disable_watchpoints_before_interactive_call_start (void)
c906108c 9142{
c5aa993b 9143 struct breakpoint *b;
c906108c
SS
9144
9145 ALL_BREAKPOINTS (b)
c5aa993b 9146 {
cc60f2e3 9147 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 9148 {
b5de0fa7 9149 b->enable_state = bp_call_disabled;
44702360 9150 update_global_location_list (UGLL_DONT_INSERT);
c5aa993b
JM
9151 }
9152 }
c906108c
SS
9153}
9154
9155void
fba45db2 9156enable_watchpoints_after_interactive_call_stop (void)
c906108c 9157{
c5aa993b 9158 struct breakpoint *b;
c906108c
SS
9159
9160 ALL_BREAKPOINTS (b)
c5aa993b 9161 {
cc60f2e3 9162 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 9163 {
b5de0fa7 9164 b->enable_state = bp_enabled;
44702360 9165 update_global_location_list (UGLL_MAY_INSERT);
c5aa993b
JM
9166 }
9167 }
c906108c
SS
9168}
9169
8bea4e01
UW
9170void
9171disable_breakpoints_before_startup (void)
9172{
6c95b8df 9173 current_program_space->executing_startup = 1;
44702360 9174 update_global_location_list (UGLL_DONT_INSERT);
8bea4e01
UW
9175}
9176
9177void
9178enable_breakpoints_after_startup (void)
9179{
6c95b8df 9180 current_program_space->executing_startup = 0;
f8eba3c6 9181 breakpoint_re_set ();
8bea4e01
UW
9182}
9183
c906108c
SS
9184
9185/* Set a breakpoint that will evaporate an end of command
9186 at address specified by SAL.
9187 Restrict it to frame FRAME if FRAME is nonzero. */
9188
9189struct breakpoint *
a6d9a66e
UW
9190set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9191 struct frame_id frame_id, enum bptype type)
c906108c 9192{
52f0bd74 9193 struct breakpoint *b;
edb3359d 9194
193facb3
JK
9195 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9196 tail-called one. */
9197 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 9198
06edf0c0 9199 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
9200 b->enable_state = bp_enabled;
9201 b->disposition = disp_donttouch;
818dd999 9202 b->frame_id = frame_id;
c906108c 9203
4a64f543
MS
9204 /* If we're debugging a multi-threaded program, then we want
9205 momentary breakpoints to be active in only a single thread of
9206 control. */
39f77062
KB
9207 if (in_thread_list (inferior_ptid))
9208 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 9209
44702360 9210 update_global_location_list_nothrow (UGLL_MAY_INSERT);
74960c60 9211
c906108c
SS
9212 return b;
9213}
611c83ae 9214
06edf0c0 9215/* Make a momentary breakpoint based on the master breakpoint ORIG.
a1aa2221
LM
9216 The new breakpoint will have type TYPE, use OPS as its
9217 breakpoint_ops, and will set enabled to LOC_ENABLED. */
e58b0e63 9218
06edf0c0
PA
9219static struct breakpoint *
9220momentary_breakpoint_from_master (struct breakpoint *orig,
9221 enum bptype type,
a1aa2221
LM
9222 const struct breakpoint_ops *ops,
9223 int loc_enabled)
e58b0e63
PA
9224{
9225 struct breakpoint *copy;
9226
06edf0c0 9227 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 9228 copy->loc = allocate_bp_location (copy);
0e30163f 9229 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 9230
a6d9a66e 9231 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
9232 copy->loc->requested_address = orig->loc->requested_address;
9233 copy->loc->address = orig->loc->address;
9234 copy->loc->section = orig->loc->section;
6c95b8df 9235 copy->loc->pspace = orig->loc->pspace;
55aa24fb 9236 copy->loc->probe = orig->loc->probe;
f8eba3c6 9237 copy->loc->line_number = orig->loc->line_number;
2f202fde 9238 copy->loc->symtab = orig->loc->symtab;
a1aa2221 9239 copy->loc->enabled = loc_enabled;
e58b0e63
PA
9240 copy->frame_id = orig->frame_id;
9241 copy->thread = orig->thread;
6c95b8df 9242 copy->pspace = orig->pspace;
e58b0e63
PA
9243
9244 copy->enable_state = bp_enabled;
9245 copy->disposition = disp_donttouch;
9246 copy->number = internal_breakpoint_number--;
9247
44702360 9248 update_global_location_list_nothrow (UGLL_DONT_INSERT);
e58b0e63
PA
9249 return copy;
9250}
9251
06edf0c0
PA
9252/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9253 ORIG is NULL. */
9254
9255struct breakpoint *
9256clone_momentary_breakpoint (struct breakpoint *orig)
9257{
9258 /* If there's nothing to clone, then return nothing. */
9259 if (orig == NULL)
9260 return NULL;
9261
a1aa2221 9262 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
06edf0c0
PA
9263}
9264
611c83ae 9265struct breakpoint *
a6d9a66e
UW
9266set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9267 enum bptype type)
611c83ae
PA
9268{
9269 struct symtab_and_line sal;
9270
9271 sal = find_pc_line (pc, 0);
9272 sal.pc = pc;
9273 sal.section = find_pc_overlay (pc);
9274 sal.explicit_pc = 1;
9275
a6d9a66e 9276 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9277}
c906108c 9278\f
c5aa993b 9279
c906108c
SS
9280/* Tell the user we have just set a breakpoint B. */
9281
9282static void
fba45db2 9283mention (struct breakpoint *b)
c906108c 9284{
348d480f 9285 b->ops->print_mention (b);
79a45e25 9286 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9287 return;
c906108c
SS
9288 printf_filtered ("\n");
9289}
c906108c 9290\f
c5aa993b 9291
0d381245 9292static struct bp_location *
39d61571 9293add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9294 const struct symtab_and_line *sal)
9295{
9296 struct bp_location *loc, **tmp;
3742cc8b
YQ
9297 CORE_ADDR adjusted_address;
9298 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9299
9300 if (loc_gdbarch == NULL)
9301 loc_gdbarch = b->gdbarch;
9302
9303 /* Adjust the breakpoint's address prior to allocating a location.
9304 Once we call allocate_bp_location(), that mostly uninitialized
9305 location will be placed on the location chain. Adjustment of the
9306 breakpoint may cause target_read_memory() to be called and we do
9307 not want its scan of the location chain to find a breakpoint and
9308 location that's only been partially initialized. */
9309 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9310 sal->pc, b->type);
0d381245 9311
d30113d4 9312 /* Sort the locations by their ADDRESS. */
39d61571 9313 loc = allocate_bp_location (b);
d30113d4
JK
9314 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9315 tmp = &((*tmp)->next))
0d381245 9316 ;
d30113d4 9317 loc->next = *tmp;
0d381245 9318 *tmp = loc;
3742cc8b 9319
0d381245 9320 loc->requested_address = sal->pc;
3742cc8b 9321 loc->address = adjusted_address;
6c95b8df 9322 loc->pspace = sal->pspace;
729662a5
TT
9323 loc->probe.probe = sal->probe;
9324 loc->probe.objfile = sal->objfile;
6c95b8df 9325 gdb_assert (loc->pspace != NULL);
0d381245 9326 loc->section = sal->section;
3742cc8b 9327 loc->gdbarch = loc_gdbarch;
f8eba3c6 9328 loc->line_number = sal->line;
2f202fde 9329 loc->symtab = sal->symtab;
f8eba3c6 9330
0e30163f
JK
9331 set_breakpoint_location_function (loc,
9332 sal->explicit_pc || sal->explicit_line);
0d381245
VP
9333 return loc;
9334}
514f746b
AR
9335\f
9336
9337/* Return 1 if LOC is pointing to a permanent breakpoint,
9338 return 0 otherwise. */
9339
9340static int
9341bp_loc_is_permanent (struct bp_location *loc)
9342{
9343 int len;
9344 CORE_ADDR addr;
1afeeb75 9345 const gdb_byte *bpoint;
514f746b 9346 gdb_byte *target_mem;
939c61fa
JK
9347 struct cleanup *cleanup;
9348 int retval = 0;
514f746b
AR
9349
9350 gdb_assert (loc != NULL);
9351
9352 addr = loc->address;
1afeeb75 9353 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 9354
939c61fa 9355 /* Software breakpoints unsupported? */
1afeeb75 9356 if (bpoint == NULL)
939c61fa
JK
9357 return 0;
9358
514f746b
AR
9359 target_mem = alloca (len);
9360
939c61fa
JK
9361 /* Enable the automatic memory restoration from breakpoints while
9362 we read the memory. Otherwise we could say about our temporary
9363 breakpoints they are permanent. */
6c95b8df
PA
9364 cleanup = save_current_space_and_thread ();
9365
9366 switch_to_program_space_and_thread (loc->pspace);
9367 make_show_memory_breakpoints_cleanup (0);
939c61fa 9368
514f746b 9369 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 9370 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 9371 retval = 1;
514f746b 9372
939c61fa
JK
9373 do_cleanups (cleanup);
9374
9375 return retval;
514f746b
AR
9376}
9377
e7e0cddf
SS
9378/* Build a command list for the dprintf corresponding to the current
9379 settings of the dprintf style options. */
9380
9381static void
9382update_dprintf_command_list (struct breakpoint *b)
9383{
9384 char *dprintf_args = b->extra_string;
9385 char *printf_line = NULL;
9386
9387 if (!dprintf_args)
9388 return;
9389
9390 dprintf_args = skip_spaces (dprintf_args);
9391
9392 /* Allow a comma, as it may have terminated a location, but don't
9393 insist on it. */
9394 if (*dprintf_args == ',')
9395 ++dprintf_args;
9396 dprintf_args = skip_spaces (dprintf_args);
9397
9398 if (*dprintf_args != '"')
9399 error (_("Bad format string, missing '\"'."));
9400
d3ce09f5 9401 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9402 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9403 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9404 {
9405 if (!dprintf_function)
9406 error (_("No function supplied for dprintf call"));
9407
9408 if (dprintf_channel && strlen (dprintf_channel) > 0)
9409 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9410 dprintf_function,
9411 dprintf_channel,
9412 dprintf_args);
9413 else
9414 printf_line = xstrprintf ("call (void) %s (%s)",
9415 dprintf_function,
9416 dprintf_args);
9417 }
d3ce09f5
SS
9418 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9419 {
9420 if (target_can_run_breakpoint_commands ())
9421 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9422 else
9423 {
9424 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9425 printf_line = xstrprintf ("printf %s", dprintf_args);
9426 }
9427 }
e7e0cddf
SS
9428 else
9429 internal_error (__FILE__, __LINE__,
9430 _("Invalid dprintf style."));
9431
f28045c2 9432 gdb_assert (printf_line != NULL);
9d6e6e84 9433 /* Manufacture a printf sequence. */
f28045c2 9434 {
9d6e6e84
HZ
9435 struct command_line *printf_cmd_line
9436 = xmalloc (sizeof (struct command_line));
e7e0cddf 9437
f28045c2
YQ
9438 printf_cmd_line = xmalloc (sizeof (struct command_line));
9439 printf_cmd_line->control_type = simple_control;
9440 printf_cmd_line->body_count = 0;
9441 printf_cmd_line->body_list = NULL;
9d6e6e84 9442 printf_cmd_line->next = NULL;
f28045c2 9443 printf_cmd_line->line = printf_line;
e7e0cddf 9444
f28045c2
YQ
9445 breakpoint_set_commands (b, printf_cmd_line);
9446 }
e7e0cddf
SS
9447}
9448
9449/* Update all dprintf commands, making their command lists reflect
9450 current style settings. */
9451
9452static void
9453update_dprintf_commands (char *args, int from_tty,
9454 struct cmd_list_element *c)
9455{
9456 struct breakpoint *b;
9457
9458 ALL_BREAKPOINTS (b)
9459 {
9460 if (b->type == bp_dprintf)
9461 update_dprintf_command_list (b);
9462 }
9463}
c3f6f71d 9464
018d34a4
VP
9465/* Create a breakpoint with SAL as location. Use ADDR_STRING
9466 as textual description of the location, and COND_STRING
db107f19 9467 as condition expression. */
018d34a4
VP
9468
9469static void
d9b3f62e
PA
9470init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9471 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9472 char *filter, char *cond_string,
e7e0cddf 9473 char *extra_string,
d9b3f62e
PA
9474 enum bptype type, enum bpdisp disposition,
9475 int thread, int task, int ignore_count,
c0a91b2b 9476 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9477 int enabled, int internal, unsigned flags,
9478 int display_canonical)
018d34a4 9479{
0d381245 9480 int i;
018d34a4
VP
9481
9482 if (type == bp_hardware_breakpoint)
9483 {
fbbd034e
AS
9484 int target_resources_ok;
9485
9486 i = hw_breakpoint_used_count ();
9487 target_resources_ok =
9488 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9489 i + 1, 0);
9490 if (target_resources_ok == 0)
9491 error (_("No hardware breakpoint support in the target."));
9492 else if (target_resources_ok < 0)
9493 error (_("Hardware breakpoints used exceeds limit."));
9494 }
9495
6c95b8df
PA
9496 gdb_assert (sals.nelts > 0);
9497
0d381245
VP
9498 for (i = 0; i < sals.nelts; ++i)
9499 {
9500 struct symtab_and_line sal = sals.sals[i];
9501 struct bp_location *loc;
9502
9503 if (from_tty)
5af949e3
UW
9504 {
9505 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9506 if (!loc_gdbarch)
9507 loc_gdbarch = gdbarch;
9508
9509 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9510 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9511 }
0d381245
VP
9512
9513 if (i == 0)
9514 {
d9b3f62e 9515 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9516 b->thread = thread;
4a306c9a 9517 b->task = task;
855a6e68 9518
0d381245 9519 b->cond_string = cond_string;
e7e0cddf 9520 b->extra_string = extra_string;
0d381245 9521 b->ignore_count = ignore_count;
41447f92 9522 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9523 b->disposition = disposition;
6c95b8df 9524
44f238bb
PA
9525 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9526 b->loc->inserted = 1;
9527
0fb4aa4b
PA
9528 if (type == bp_static_tracepoint)
9529 {
d9b3f62e 9530 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9531 struct static_tracepoint_marker marker;
9532
983af33b 9533 if (strace_marker_p (b))
0fb4aa4b
PA
9534 {
9535 /* We already know the marker exists, otherwise, we
9536 wouldn't see a sal for it. */
9537 char *p = &addr_string[3];
9538 char *endp;
9539 char *marker_str;
0fb4aa4b 9540
e9cafbcc 9541 p = skip_spaces (p);
0fb4aa4b 9542
e9cafbcc 9543 endp = skip_to_space (p);
0fb4aa4b
PA
9544
9545 marker_str = savestring (p, endp - p);
d9b3f62e 9546 t->static_trace_marker_id = marker_str;
0fb4aa4b 9547
3e43a32a
MS
9548 printf_filtered (_("Probed static tracepoint "
9549 "marker \"%s\"\n"),
d9b3f62e 9550 t->static_trace_marker_id);
0fb4aa4b
PA
9551 }
9552 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9553 {
d9b3f62e 9554 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9555 release_static_tracepoint_marker (&marker);
9556
3e43a32a
MS
9557 printf_filtered (_("Probed static tracepoint "
9558 "marker \"%s\"\n"),
d9b3f62e 9559 t->static_trace_marker_id);
0fb4aa4b
PA
9560 }
9561 else
3e43a32a
MS
9562 warning (_("Couldn't determine the static "
9563 "tracepoint marker to probe"));
0fb4aa4b
PA
9564 }
9565
0d381245
VP
9566 loc = b->loc;
9567 }
9568 else
018d34a4 9569 {
39d61571 9570 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9571 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9572 loc->inserted = 1;
0d381245
VP
9573 }
9574
514f746b
AR
9575 if (bp_loc_is_permanent (loc))
9576 make_breakpoint_permanent (b);
9577
0d381245
VP
9578 if (b->cond_string)
9579 {
bbc13ae3
KS
9580 const char *arg = b->cond_string;
9581
1bb9788d
TT
9582 loc->cond = parse_exp_1 (&arg, loc->address,
9583 block_for_pc (loc->address), 0);
0d381245 9584 if (*arg)
588ae58c 9585 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9586 }
e7e0cddf
SS
9587
9588 /* Dynamic printf requires and uses additional arguments on the
9589 command line, otherwise it's an error. */
9590 if (type == bp_dprintf)
9591 {
9592 if (b->extra_string)
9593 update_dprintf_command_list (b);
9594 else
9595 error (_("Format string required"));
9596 }
9597 else if (b->extra_string)
588ae58c 9598 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9599 }
018d34a4 9600
56435ebe 9601 b->display_canonical = display_canonical;
018d34a4
VP
9602 if (addr_string)
9603 b->addr_string = addr_string;
9604 else
9605 /* addr_string has to be used or breakpoint_re_set will delete
9606 me. */
5af949e3
UW
9607 b->addr_string
9608 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9609 b->filter = filter;
d9b3f62e 9610}
018d34a4 9611
d9b3f62e
PA
9612static void
9613create_breakpoint_sal (struct gdbarch *gdbarch,
9614 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9615 char *filter, char *cond_string,
e7e0cddf 9616 char *extra_string,
d9b3f62e
PA
9617 enum bptype type, enum bpdisp disposition,
9618 int thread, int task, int ignore_count,
c0a91b2b 9619 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9620 int enabled, int internal, unsigned flags,
9621 int display_canonical)
d9b3f62e
PA
9622{
9623 struct breakpoint *b;
9624 struct cleanup *old_chain;
9625
9626 if (is_tracepoint_type (type))
9627 {
9628 struct tracepoint *t;
9629
9630 t = XCNEW (struct tracepoint);
9631 b = &t->base;
9632 }
9633 else
9634 b = XNEW (struct breakpoint);
9635
9636 old_chain = make_cleanup (xfree, b);
9637
9638 init_breakpoint_sal (b, gdbarch,
9639 sals, addr_string,
e7e0cddf 9640 filter, cond_string, extra_string,
d9b3f62e
PA
9641 type, disposition,
9642 thread, task, ignore_count,
9643 ops, from_tty,
44f238bb
PA
9644 enabled, internal, flags,
9645 display_canonical);
d9b3f62e
PA
9646 discard_cleanups (old_chain);
9647
3ea46bff 9648 install_breakpoint (internal, b, 0);
018d34a4
VP
9649}
9650
9651/* Add SALS.nelts breakpoints to the breakpoint table. For each
9652 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9653 value. COND_STRING, if not NULL, specified the condition to be
9654 used for all breakpoints. Essentially the only case where
9655 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9656 function. In that case, it's still not possible to specify
9657 separate conditions for different overloaded functions, so
9658 we take just a single condition string.
9659
c3f6f71d 9660 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9661 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9662 array contents). If the function fails (error() is called), the
9663 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9664 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9665
9666static void
8cdf0e15 9667create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9668 struct linespec_result *canonical,
e7e0cddf 9669 char *cond_string, char *extra_string,
8cdf0e15
VP
9670 enum bptype type, enum bpdisp disposition,
9671 int thread, int task, int ignore_count,
c0a91b2b 9672 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9673 int enabled, int internal, unsigned flags)
c906108c 9674{
018d34a4 9675 int i;
f8eba3c6 9676 struct linespec_sals *lsal;
cc59ec59 9677
f8eba3c6
TT
9678 if (canonical->pre_expanded)
9679 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9680
9681 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9682 {
f8eba3c6
TT
9683 /* Note that 'addr_string' can be NULL in the case of a plain
9684 'break', without arguments. */
9685 char *addr_string = (canonical->addr_string
9686 ? xstrdup (canonical->addr_string)
9687 : NULL);
9688 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9689 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9690
f8eba3c6
TT
9691 make_cleanup (xfree, filter_string);
9692 create_breakpoint_sal (gdbarch, lsal->sals,
9693 addr_string,
9694 filter_string,
e7e0cddf
SS
9695 cond_string, extra_string,
9696 type, disposition,
84f4c1fe 9697 thread, task, ignore_count, ops,
44f238bb 9698 from_tty, enabled, internal, flags,
56435ebe 9699 canonical->special_display);
f8eba3c6 9700 discard_cleanups (inner);
c3f6f71d 9701 }
c3f6f71d 9702}
c906108c 9703
9998af43 9704/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9705 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9706 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9707 address strings. ADDRESS points to the end of the SAL.
9708
9709 The array and the line spec strings are allocated on the heap, it is
9710 the caller's responsibility to free them. */
c906108c 9711
b9362cc7 9712static void
c3f6f71d 9713parse_breakpoint_sals (char **address,
58438ac1 9714 struct linespec_result *canonical)
c3f6f71d 9715{
c3f6f71d 9716 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9717 breakpoint. */
c3f6f71d
JM
9718 if ((*address) == NULL
9719 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 9720 {
1bfeeb0f
JL
9721 /* The last displayed codepoint, if it's valid, is our default breakpoint
9722 address. */
9723 if (last_displayed_sal_is_valid ())
c906108c 9724 {
f8eba3c6 9725 struct linespec_sals lsal;
c3f6f71d 9726 struct symtab_and_line sal;
1c8cdcb1 9727 CORE_ADDR pc;
cc59ec59 9728
4a64f543 9729 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9730 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9731 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9732
9733 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9734 corresponding to the last call to print_frame_info.
9735 Be sure to reinitialize LINE with NOTCURRENT == 0
9736 as the breakpoint line number is inappropriate otherwise.
9737 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9738 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9739 pc = sal.pc;
9740 sal = find_pc_line (pc, 0);
00903456 9741
4a64f543 9742 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9743 where PC is the last displayed codepoint's address. So
9744 make sure to set sal.explicit_pc to prevent GDB from
9745 trying to expand the list of sals to include all other
9746 instances with the same symtab and line. */
1c8cdcb1 9747 sal.pc = pc;
00903456
JK
9748 sal.explicit_pc = 1;
9749
f8eba3c6
TT
9750 lsal.sals.sals[0] = sal;
9751 lsal.sals.nelts = 1;
9752 lsal.canonical = NULL;
9753
9754 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9755 }
9756 else
8a3fe4f8 9757 error (_("No default breakpoint address now."));
c906108c
SS
9758 }
9759 else
9760 {
cc80f267
JK
9761 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9762
c906108c 9763 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9764 current_source_symtab (which is decode_line_1's default).
9765 This should produce the results we want almost all of the
cc80f267
JK
9766 time while leaving default_breakpoint_* alone.
9767
9768 ObjC: However, don't match an Objective-C method name which
9769 may have a '+' or '-' succeeded by a '['. */
9770 if (last_displayed_sal_is_valid ()
9771 && (!cursal.symtab
9772 || ((strchr ("+-", (*address)[0]) != NULL)
9773 && ((*address)[1] != '['))))
f8eba3c6
TT
9774 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9775 get_last_displayed_symtab (),
9776 get_last_displayed_line (),
9777 canonical, NULL, NULL);
c906108c 9778 else
f8eba3c6 9779 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9780 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9781 }
c3f6f71d 9782}
c906108c 9783
c906108c 9784
c3f6f71d 9785/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9786 inserted as a breakpoint. If it can't throw an error. */
c906108c 9787
b9362cc7 9788static void
23e7acfb 9789breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9790{
9791 int i;
cc59ec59 9792
c3f6f71d 9793 for (i = 0; i < sals->nelts; i++)
ee53e872 9794 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9795}
9796
7a697b8d
SS
9797/* Fast tracepoints may have restrictions on valid locations. For
9798 instance, a fast tracepoint using a jump instead of a trap will
9799 likely have to overwrite more bytes than a trap would, and so can
9800 only be placed where the instruction is longer than the jump, or a
9801 multi-instruction sequence does not have a jump into the middle of
9802 it, etc. */
9803
9804static void
9805check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9806 struct symtabs_and_lines *sals)
9807{
9808 int i, rslt;
9809 struct symtab_and_line *sal;
9810 char *msg;
9811 struct cleanup *old_chain;
9812
9813 for (i = 0; i < sals->nelts; i++)
9814 {
f8eba3c6
TT
9815 struct gdbarch *sarch;
9816
7a697b8d
SS
9817 sal = &sals->sals[i];
9818
f8eba3c6
TT
9819 sarch = get_sal_arch (*sal);
9820 /* We fall back to GDBARCH if there is no architecture
9821 associated with SAL. */
9822 if (sarch == NULL)
9823 sarch = gdbarch;
9824 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9825 NULL, &msg);
9826 old_chain = make_cleanup (xfree, msg);
9827
9828 if (!rslt)
9829 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9830 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9831
9832 do_cleanups (old_chain);
9833 }
9834}
9835
af4908ba
KS
9836/* Issue an invalid thread ID error. */
9837
9838static void ATTRIBUTE_NORETURN
9839invalid_thread_id_error (int id)
9840{
9841 error (_("Unknown thread %d."), id);
9842}
9843
018d34a4
VP
9844/* Given TOK, a string specification of condition and thread, as
9845 accepted by the 'break' command, extract the condition
9846 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9847 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9848 If no condition is found, *COND_STRING is set to NULL.
9849 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9850
9851static void
bbc13ae3 9852find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9853 char **cond_string, int *thread, int *task,
9854 char **rest)
018d34a4
VP
9855{
9856 *cond_string = NULL;
9857 *thread = -1;
ed1d1739
KS
9858 *task = 0;
9859 *rest = NULL;
9860
018d34a4
VP
9861 while (tok && *tok)
9862 {
bbc13ae3 9863 const char *end_tok;
018d34a4 9864 int toklen;
bbc13ae3
KS
9865 const char *cond_start = NULL;
9866 const char *cond_end = NULL;
cc59ec59 9867
bbc13ae3 9868 tok = skip_spaces_const (tok);
e7e0cddf
SS
9869
9870 if ((*tok == '"' || *tok == ',') && rest)
9871 {
9872 *rest = savestring (tok, strlen (tok));
9873 return;
9874 }
9875
bbc13ae3 9876 end_tok = skip_to_space_const (tok);
d634f2de 9877
018d34a4 9878 toklen = end_tok - tok;
d634f2de 9879
018d34a4
VP
9880 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9881 {
f7545552
TT
9882 struct expression *expr;
9883
018d34a4 9884 tok = cond_start = end_tok + 1;
1bb9788d 9885 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9886 xfree (expr);
018d34a4 9887 cond_end = tok;
d634f2de 9888 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9889 }
9890 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9891 {
9892 char *tmptok;
d634f2de 9893
018d34a4 9894 tok = end_tok + 1;
bbc13ae3 9895 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9896 if (tok == tmptok)
9897 error (_("Junk after thread keyword."));
9898 if (!valid_thread_id (*thread))
af4908ba 9899 invalid_thread_id_error (*thread);
bbc13ae3 9900 tok = tmptok;
018d34a4 9901 }
4a306c9a
JB
9902 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9903 {
9904 char *tmptok;
9905
9906 tok = end_tok + 1;
bbc13ae3 9907 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9908 if (tok == tmptok)
9909 error (_("Junk after task keyword."));
9910 if (!valid_task_id (*task))
b6199126 9911 error (_("Unknown task %d."), *task);
bbc13ae3 9912 tok = tmptok;
4a306c9a 9913 }
e7e0cddf
SS
9914 else if (rest)
9915 {
9916 *rest = savestring (tok, strlen (tok));
ccab2054 9917 return;
e7e0cddf 9918 }
018d34a4
VP
9919 else
9920 error (_("Junk at end of arguments."));
9921 }
9922}
9923
0fb4aa4b
PA
9924/* Decode a static tracepoint marker spec. */
9925
9926static struct symtabs_and_lines
9927decode_static_tracepoint_spec (char **arg_p)
9928{
9929 VEC(static_tracepoint_marker_p) *markers = NULL;
9930 struct symtabs_and_lines sals;
0fb4aa4b
PA
9931 struct cleanup *old_chain;
9932 char *p = &(*arg_p)[3];
9933 char *endp;
9934 char *marker_str;
9935 int i;
9936
e9cafbcc 9937 p = skip_spaces (p);
0fb4aa4b 9938
e9cafbcc 9939 endp = skip_to_space (p);
0fb4aa4b
PA
9940
9941 marker_str = savestring (p, endp - p);
9942 old_chain = make_cleanup (xfree, marker_str);
9943
9944 markers = target_static_tracepoint_markers_by_strid (marker_str);
9945 if (VEC_empty(static_tracepoint_marker_p, markers))
9946 error (_("No known static tracepoint marker named %s"), marker_str);
9947
9948 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9949 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9950
9951 for (i = 0; i < sals.nelts; i++)
9952 {
9953 struct static_tracepoint_marker *marker;
9954
9955 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9956
9957 init_sal (&sals.sals[i]);
9958
9959 sals.sals[i] = find_pc_line (marker->address, 0);
9960 sals.sals[i].pc = marker->address;
9961
9962 release_static_tracepoint_marker (marker);
9963 }
9964
9965 do_cleanups (old_chain);
9966
9967 *arg_p = endp;
9968 return sals;
9969}
9970
fd9b8c24
PA
9971/* Set a breakpoint. This function is shared between CLI and MI
9972 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9973 modes of operations, selected by the PARSE_ARG parameter. If
9974 non-zero, the function will parse ARG, extracting location,
9975 condition, thread and extra string. Otherwise, ARG is just the
9976 breakpoint's location, with condition, thread, and extra string
9977 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9978 If INTERNAL is non-zero, the breakpoint number will be allocated
9979 from the internal breakpoint count. Returns true if any breakpoint
9980 was created; false otherwise. */
0101ce28 9981
8cdf0e15
VP
9982int
9983create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9984 char *arg, char *cond_string,
9985 int thread, char *extra_string,
f6de8ec2 9986 int parse_arg,
0fb4aa4b 9987 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9988 int ignore_count,
9989 enum auto_boolean pending_break_support,
c0a91b2b 9990 const struct breakpoint_ops *ops,
44f238bb
PA
9991 int from_tty, int enabled, int internal,
9992 unsigned flags)
c3f6f71d 9993{
b78a6381 9994 volatile struct gdb_exception e;
f8eba3c6 9995 char *copy_arg = NULL;
c3f6f71d 9996 char *addr_start = arg;
7efd8fc2 9997 struct linespec_result canonical;
c3f6f71d 9998 struct cleanup *old_chain;
80c99de1 9999 struct cleanup *bkpt_chain = NULL;
0101ce28 10000 int pending = 0;
4a306c9a 10001 int task = 0;
86b17b60 10002 int prev_bkpt_count = breakpoint_count;
c3f6f71d 10003
348d480f
PA
10004 gdb_assert (ops != NULL);
10005
7efd8fc2 10006 init_linespec_result (&canonical);
c3f6f71d 10007
b78a6381
TT
10008 TRY_CATCH (e, RETURN_MASK_ALL)
10009 {
983af33b
SDJ
10010 ops->create_sals_from_address (&arg, &canonical, type_wanted,
10011 addr_start, &copy_arg);
b78a6381 10012 }
0101ce28
JJ
10013
10014 /* If caller is interested in rc value from parse, set value. */
05ff989b 10015 switch (e.reason)
0101ce28 10016 {
983af33b
SDJ
10017 case GDB_NO_ERROR:
10018 if (VEC_empty (linespec_sals, canonical.sals))
10019 return 0;
10020 break;
05ff989b
AC
10021 case RETURN_ERROR:
10022 switch (e.error)
0101ce28 10023 {
05ff989b 10024 case NOT_FOUND_ERROR:
0101ce28 10025
05ff989b
AC
10026 /* If pending breakpoint support is turned off, throw
10027 error. */
fa8d40ab
JJ
10028
10029 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
10030 throw_exception (e);
10031
10032 exception_print (gdb_stderr, e);
fa8d40ab 10033
05ff989b
AC
10034 /* If pending breakpoint support is auto query and the user
10035 selects no, then simply return the error code. */
059fb39f 10036 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
10037 && !nquery (_("Make %s pending on future shared library load? "),
10038 bptype_string (type_wanted)))
fd9b8c24 10039 return 0;
fa8d40ab 10040
05ff989b
AC
10041 /* At this point, either the user was queried about setting
10042 a pending breakpoint and selected yes, or pending
10043 breakpoint behavior is on and thus a pending breakpoint
10044 is defaulted on behalf of the user. */
f8eba3c6
TT
10045 {
10046 struct linespec_sals lsal;
10047
10048 copy_arg = xstrdup (addr_start);
10049 lsal.canonical = xstrdup (copy_arg);
10050 lsal.sals.nelts = 1;
10051 lsal.sals.sals = XNEW (struct symtab_and_line);
10052 init_sal (&lsal.sals.sals[0]);
10053 pending = 1;
10054 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
10055 }
05ff989b
AC
10056 break;
10057 default:
98deb0da 10058 throw_exception (e);
0101ce28 10059 }
2abae994 10060 break;
05ff989b 10061 default:
983af33b 10062 throw_exception (e);
0101ce28 10063 }
c3f6f71d 10064
4a64f543 10065 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 10066 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 10067
c3f6f71d
JM
10068 /* ----------------------------- SNIP -----------------------------
10069 Anything added to the cleanup chain beyond this point is assumed
10070 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
10071 then the memory is not reclaimed. */
10072 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 10073
c3f6f71d
JM
10074 /* Resolve all line numbers to PC's and verify that the addresses
10075 are ok for the target. */
0101ce28 10076 if (!pending)
f8eba3c6
TT
10077 {
10078 int ix;
10079 struct linespec_sals *iter;
10080
10081 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10082 breakpoint_sals_to_pc (&iter->sals);
10083 }
c3f6f71d 10084
7a697b8d 10085 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 10086 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
10087 {
10088 int ix;
10089 struct linespec_sals *iter;
10090
10091 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10092 check_fast_tracepoint_sals (gdbarch, &iter->sals);
10093 }
7a697b8d 10094
c3f6f71d
JM
10095 /* Verify that condition can be parsed, before setting any
10096 breakpoints. Allocate a separate condition expression for each
4a64f543 10097 breakpoint. */
0101ce28 10098 if (!pending)
c3f6f71d 10099 {
f6de8ec2 10100 if (parse_arg)
72b2ff0e 10101 {
0878d0fa 10102 char *rest;
52d361e1
YQ
10103 struct linespec_sals *lsal;
10104
10105 lsal = VEC_index (linespec_sals, canonical.sals, 0);
10106
0878d0fa
YQ
10107 /* Here we only parse 'arg' to separate condition
10108 from thread number, so parsing in context of first
10109 sal is OK. When setting the breakpoint we'll
10110 re-parse it in context of each sal. */
10111
10112 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
10113 &thread, &task, &rest);
10114 if (cond_string)
10115 make_cleanup (xfree, cond_string);
10116 if (rest)
10117 make_cleanup (xfree, rest);
10118 if (rest)
10119 extra_string = rest;
72b2ff0e 10120 }
2f069f6f 10121 else
72b2ff0e 10122 {
0878d0fa
YQ
10123 if (*arg != '\0')
10124 error (_("Garbage '%s' at end of location"), arg);
10125
10126 /* Create a private copy of condition string. */
10127 if (cond_string)
10128 {
10129 cond_string = xstrdup (cond_string);
10130 make_cleanup (xfree, cond_string);
10131 }
10132 /* Create a private copy of any extra string. */
10133 if (extra_string)
10134 {
10135 extra_string = xstrdup (extra_string);
10136 make_cleanup (xfree, extra_string);
10137 }
72b2ff0e 10138 }
0fb4aa4b 10139
52d361e1 10140 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 10141 cond_string, extra_string, type_wanted,
d9b3f62e
PA
10142 tempflag ? disp_del : disp_donttouch,
10143 thread, task, ignore_count, ops,
44f238bb 10144 from_tty, enabled, internal, flags);
c906108c 10145 }
0101ce28
JJ
10146 else
10147 {
0101ce28
JJ
10148 struct breakpoint *b;
10149
0101ce28
JJ
10150 make_cleanup (xfree, copy_arg);
10151
bfccc43c
YQ
10152 if (is_tracepoint_type (type_wanted))
10153 {
10154 struct tracepoint *t;
10155
10156 t = XCNEW (struct tracepoint);
10157 b = &t->base;
10158 }
10159 else
10160 b = XNEW (struct breakpoint);
10161
10162 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
10163
f8eba3c6 10164 b->addr_string = copy_arg;
f6de8ec2 10165 if (parse_arg)
e12c7713
MK
10166 b->cond_string = NULL;
10167 else
10168 {
10169 /* Create a private copy of condition string. */
10170 if (cond_string)
10171 {
10172 cond_string = xstrdup (cond_string);
10173 make_cleanup (xfree, cond_string);
10174 }
10175 b->cond_string = cond_string;
10176 }
e7e0cddf 10177 b->extra_string = NULL;
0101ce28 10178 b->ignore_count = ignore_count;
0101ce28 10179 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 10180 b->condition_not_parsed = 1;
41447f92 10181 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
10182 if ((type_wanted != bp_breakpoint
10183 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 10184 b->pspace = current_program_space;
8bea4e01 10185
bfccc43c 10186 install_breakpoint (internal, b, 0);
0101ce28
JJ
10187 }
10188
f8eba3c6 10189 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 10190 {
3e43a32a
MS
10191 warning (_("Multiple breakpoints were set.\nUse the "
10192 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 10193 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
10194 }
10195
80c99de1
PA
10196 /* That's it. Discard the cleanups for data inserted into the
10197 breakpoint. */
10198 discard_cleanups (bkpt_chain);
10199 /* But cleanup everything else. */
c3f6f71d 10200 do_cleanups (old_chain);
217dc9e2 10201
80c99de1 10202 /* error call may happen here - have BKPT_CHAIN already discarded. */
44702360 10203 update_global_location_list (UGLL_MAY_INSERT);
fd9b8c24
PA
10204
10205 return 1;
c3f6f71d 10206}
c906108c 10207
348d480f 10208/* Set a breakpoint.
72b2ff0e
VP
10209 ARG is a string describing breakpoint address,
10210 condition, and thread.
10211 FLAG specifies if a breakpoint is hardware on,
10212 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10213 and BP_TEMPFLAG. */
348d480f 10214
98deb0da 10215static void
72b2ff0e 10216break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 10217{
72b2ff0e 10218 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
10219 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10220 ? bp_hardware_breakpoint
10221 : bp_breakpoint);
55aa24fb
SDJ
10222 struct breakpoint_ops *ops;
10223 const char *arg_cp = arg;
10224
10225 /* Matching breakpoints on probes. */
10226 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10227 ops = &bkpt_probe_breakpoint_ops;
10228 else
10229 ops = &bkpt_breakpoint_ops;
c3f6f71d 10230
8cdf0e15
VP
10231 create_breakpoint (get_current_arch (),
10232 arg,
e7e0cddf 10233 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 10234 tempflag, type_wanted,
8cdf0e15
VP
10235 0 /* Ignore count */,
10236 pending_break_support,
55aa24fb 10237 ops,
8cdf0e15 10238 from_tty,
84f4c1fe 10239 1 /* enabled */,
44f238bb
PA
10240 0 /* internal */,
10241 0);
c906108c
SS
10242}
10243
c906108c
SS
10244/* Helper function for break_command_1 and disassemble_command. */
10245
10246void
fba45db2 10247resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10248{
10249 CORE_ADDR pc;
10250
10251 if (sal->pc == 0 && sal->symtab != NULL)
10252 {
10253 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10254 error (_("No line %d in file \"%s\"."),
05cba821 10255 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10256 sal->pc = pc;
6a048695 10257
4a64f543
MS
10258 /* If this SAL corresponds to a breakpoint inserted using a line
10259 number, then skip the function prologue if necessary. */
6a048695 10260 if (sal->explicit_line)
059acae7 10261 skip_prologue_sal (sal);
c906108c
SS
10262 }
10263
10264 if (sal->section == 0 && sal->symtab != NULL)
10265 {
346d1dfe 10266 const struct blockvector *bv;
3977b71f 10267 const struct block *b;
c5aa993b 10268 struct symbol *sym;
c906108c 10269
801e3a5b 10270 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
10271 if (bv != NULL)
10272 {
7f0df278 10273 sym = block_linkage_function (b);
c906108c
SS
10274 if (sym != NULL)
10275 {
10276 fixup_symbol_section (sym, sal->symtab->objfile);
e27d198c 10277 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
c906108c
SS
10278 }
10279 else
10280 {
4a64f543
MS
10281 /* It really is worthwhile to have the section, so we'll
10282 just have to look harder. This case can be executed
10283 if we have line numbers but no functions (as can
10284 happen in assembly source). */
c906108c 10285
7cbd4a93 10286 struct bound_minimal_symbol msym;
6c95b8df
PA
10287 struct cleanup *old_chain = save_current_space_and_thread ();
10288
10289 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10290
10291 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10292 if (msym.minsym)
efd66ac6 10293 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10294
10295 do_cleanups (old_chain);
c906108c
SS
10296 }
10297 }
10298 }
10299}
10300
10301void
fba45db2 10302break_command (char *arg, int from_tty)
c906108c 10303{
db107f19 10304 break_command_1 (arg, 0, from_tty);
c906108c
SS
10305}
10306
c906108c 10307void
fba45db2 10308tbreak_command (char *arg, int from_tty)
c906108c 10309{
db107f19 10310 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10311}
10312
c906108c 10313static void
fba45db2 10314hbreak_command (char *arg, int from_tty)
c906108c 10315{
db107f19 10316 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10317}
10318
10319static void
fba45db2 10320thbreak_command (char *arg, int from_tty)
c906108c 10321{
db107f19 10322 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10323}
10324
10325static void
fba45db2 10326stop_command (char *arg, int from_tty)
c906108c 10327{
a3f17187 10328 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10329Usage: stop in <function | address>\n\
a3f17187 10330 stop at <line>\n"));
c906108c
SS
10331}
10332
10333static void
fba45db2 10334stopin_command (char *arg, int from_tty)
c906108c
SS
10335{
10336 int badInput = 0;
10337
c5aa993b 10338 if (arg == (char *) NULL)
c906108c
SS
10339 badInput = 1;
10340 else if (*arg != '*')
10341 {
10342 char *argptr = arg;
10343 int hasColon = 0;
10344
4a64f543 10345 /* Look for a ':'. If this is a line number specification, then
53a5351d 10346 say it is bad, otherwise, it should be an address or
4a64f543 10347 function/method name. */
c906108c 10348 while (*argptr && !hasColon)
c5aa993b
JM
10349 {
10350 hasColon = (*argptr == ':');
10351 argptr++;
10352 }
c906108c
SS
10353
10354 if (hasColon)
c5aa993b 10355 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10356 else
c5aa993b 10357 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10358 }
10359
10360 if (badInput)
a3f17187 10361 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10362 else
db107f19 10363 break_command_1 (arg, 0, from_tty);
c906108c
SS
10364}
10365
10366static void
fba45db2 10367stopat_command (char *arg, int from_tty)
c906108c
SS
10368{
10369 int badInput = 0;
10370
c5aa993b 10371 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10372 badInput = 1;
10373 else
10374 {
10375 char *argptr = arg;
10376 int hasColon = 0;
10377
4a64f543
MS
10378 /* Look for a ':'. If there is a '::' then get out, otherwise
10379 it is probably a line number. */
c906108c 10380 while (*argptr && !hasColon)
c5aa993b
JM
10381 {
10382 hasColon = (*argptr == ':');
10383 argptr++;
10384 }
c906108c
SS
10385
10386 if (hasColon)
c5aa993b 10387 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10388 else
c5aa993b 10389 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10390 }
10391
10392 if (badInput)
a3f17187 10393 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10394 else
db107f19 10395 break_command_1 (arg, 0, from_tty);
c906108c
SS
10396}
10397
e7e0cddf
SS
10398/* The dynamic printf command is mostly like a regular breakpoint, but
10399 with a prewired command list consisting of a single output command,
10400 built from extra arguments supplied on the dprintf command
10401 line. */
10402
da821c7b 10403static void
e7e0cddf
SS
10404dprintf_command (char *arg, int from_tty)
10405{
10406 create_breakpoint (get_current_arch (),
10407 arg,
10408 NULL, 0, NULL, 1 /* parse arg */,
10409 0, bp_dprintf,
10410 0 /* Ignore count */,
10411 pending_break_support,
10412 &dprintf_breakpoint_ops,
10413 from_tty,
10414 1 /* enabled */,
10415 0 /* internal */,
10416 0);
10417}
10418
d3ce09f5
SS
10419static void
10420agent_printf_command (char *arg, int from_tty)
10421{
10422 error (_("May only run agent-printf on the target"));
10423}
10424
f1310107
TJB
10425/* Implement the "breakpoint_hit" breakpoint_ops method for
10426 ranged breakpoints. */
10427
10428static int
10429breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10430 struct address_space *aspace,
09ac7c10
TT
10431 CORE_ADDR bp_addr,
10432 const struct target_waitstatus *ws)
f1310107 10433{
09ac7c10 10434 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10435 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10436 return 0;
10437
f1310107
TJB
10438 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10439 bl->length, aspace, bp_addr);
10440}
10441
10442/* Implement the "resources_needed" breakpoint_ops method for
10443 ranged breakpoints. */
10444
10445static int
10446resources_needed_ranged_breakpoint (const struct bp_location *bl)
10447{
10448 return target_ranged_break_num_registers ();
10449}
10450
10451/* Implement the "print_it" breakpoint_ops method for
10452 ranged breakpoints. */
10453
10454static enum print_stop_action
348d480f 10455print_it_ranged_breakpoint (bpstat bs)
f1310107 10456{
348d480f 10457 struct breakpoint *b = bs->breakpoint_at;
f1310107 10458 struct bp_location *bl = b->loc;
79a45e25 10459 struct ui_out *uiout = current_uiout;
f1310107
TJB
10460
10461 gdb_assert (b->type == bp_hardware_breakpoint);
10462
10463 /* Ranged breakpoints have only one location. */
10464 gdb_assert (bl && bl->next == NULL);
10465
10466 annotate_breakpoint (b->number);
10467 if (b->disposition == disp_del)
10468 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10469 else
10470 ui_out_text (uiout, "\nRanged breakpoint ");
10471 if (ui_out_is_mi_like_p (uiout))
10472 {
10473 ui_out_field_string (uiout, "reason",
10474 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10475 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10476 }
10477 ui_out_field_int (uiout, "bkptno", b->number);
10478 ui_out_text (uiout, ", ");
10479
10480 return PRINT_SRC_AND_LOC;
10481}
10482
10483/* Implement the "print_one" breakpoint_ops method for
10484 ranged breakpoints. */
10485
10486static void
10487print_one_ranged_breakpoint (struct breakpoint *b,
10488 struct bp_location **last_loc)
10489{
10490 struct bp_location *bl = b->loc;
10491 struct value_print_options opts;
79a45e25 10492 struct ui_out *uiout = current_uiout;
f1310107
TJB
10493
10494 /* Ranged breakpoints have only one location. */
10495 gdb_assert (bl && bl->next == NULL);
10496
10497 get_user_print_options (&opts);
10498
10499 if (opts.addressprint)
10500 /* We don't print the address range here, it will be printed later
10501 by print_one_detail_ranged_breakpoint. */
10502 ui_out_field_skip (uiout, "addr");
10503 annotate_field (5);
10504 print_breakpoint_location (b, bl);
10505 *last_loc = bl;
10506}
10507
10508/* Implement the "print_one_detail" breakpoint_ops method for
10509 ranged breakpoints. */
10510
10511static void
10512print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10513 struct ui_out *uiout)
10514{
10515 CORE_ADDR address_start, address_end;
10516 struct bp_location *bl = b->loc;
f99d8bf4
PA
10517 struct ui_file *stb = mem_fileopen ();
10518 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10519
10520 gdb_assert (bl);
10521
10522 address_start = bl->address;
10523 address_end = address_start + bl->length - 1;
10524
10525 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10526 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10527 print_core_address (bl->gdbarch, address_start),
10528 print_core_address (bl->gdbarch, address_end));
10529 ui_out_field_stream (uiout, "addr", stb);
10530 ui_out_text (uiout, "\n");
10531
10532 do_cleanups (cleanup);
10533}
10534
10535/* Implement the "print_mention" breakpoint_ops method for
10536 ranged breakpoints. */
10537
10538static void
10539print_mention_ranged_breakpoint (struct breakpoint *b)
10540{
10541 struct bp_location *bl = b->loc;
79a45e25 10542 struct ui_out *uiout = current_uiout;
f1310107
TJB
10543
10544 gdb_assert (bl);
10545 gdb_assert (b->type == bp_hardware_breakpoint);
10546
10547 if (ui_out_is_mi_like_p (uiout))
10548 return;
10549
10550 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10551 b->number, paddress (bl->gdbarch, bl->address),
10552 paddress (bl->gdbarch, bl->address + bl->length - 1));
10553}
10554
10555/* Implement the "print_recreate" breakpoint_ops method for
10556 ranged breakpoints. */
10557
10558static void
10559print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10560{
10561 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10562 b->addr_string_range_end);
d9b3f62e 10563 print_recreate_thread (b, fp);
f1310107
TJB
10564}
10565
10566/* The breakpoint_ops structure to be used in ranged breakpoints. */
10567
2060206e 10568static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10569
10570/* Find the address where the end of the breakpoint range should be
10571 placed, given the SAL of the end of the range. This is so that if
10572 the user provides a line number, the end of the range is set to the
10573 last instruction of the given line. */
10574
10575static CORE_ADDR
10576find_breakpoint_range_end (struct symtab_and_line sal)
10577{
10578 CORE_ADDR end;
10579
10580 /* If the user provided a PC value, use it. Otherwise,
10581 find the address of the end of the given location. */
10582 if (sal.explicit_pc)
10583 end = sal.pc;
10584 else
10585 {
10586 int ret;
10587 CORE_ADDR start;
10588
10589 ret = find_line_pc_range (sal, &start, &end);
10590 if (!ret)
10591 error (_("Could not find location of the end of the range."));
10592
10593 /* find_line_pc_range returns the start of the next line. */
10594 end--;
10595 }
10596
10597 return end;
10598}
10599
10600/* Implement the "break-range" CLI command. */
10601
10602static void
10603break_range_command (char *arg, int from_tty)
10604{
10605 char *arg_start, *addr_string_start, *addr_string_end;
10606 struct linespec_result canonical_start, canonical_end;
10607 int bp_count, can_use_bp, length;
10608 CORE_ADDR end;
10609 struct breakpoint *b;
10610 struct symtab_and_line sal_start, sal_end;
f1310107 10611 struct cleanup *cleanup_bkpt;
f8eba3c6 10612 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10613
10614 /* We don't support software ranged breakpoints. */
10615 if (target_ranged_break_num_registers () < 0)
10616 error (_("This target does not support hardware ranged breakpoints."));
10617
10618 bp_count = hw_breakpoint_used_count ();
10619 bp_count += target_ranged_break_num_registers ();
10620 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10621 bp_count, 0);
10622 if (can_use_bp < 0)
10623 error (_("Hardware breakpoints used exceeds limit."));
10624
f8eba3c6 10625 arg = skip_spaces (arg);
f1310107
TJB
10626 if (arg == NULL || arg[0] == '\0')
10627 error(_("No address range specified."));
10628
f1310107
TJB
10629 init_linespec_result (&canonical_start);
10630
f8eba3c6
TT
10631 arg_start = arg;
10632 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10633
f8eba3c6 10634 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10635
10636 if (arg[0] != ',')
10637 error (_("Too few arguments."));
f8eba3c6 10638 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10639 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10640
10641 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10642
10643 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10644 || lsal_start->sals.nelts != 1)
f1310107
TJB
10645 error (_("Cannot create a ranged breakpoint with multiple locations."));
10646
f8eba3c6
TT
10647 sal_start = lsal_start->sals.sals[0];
10648 addr_string_start = savestring (arg_start, arg - arg_start);
10649 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10650
10651 arg++; /* Skip the comma. */
f8eba3c6 10652 arg = skip_spaces (arg);
f1310107
TJB
10653
10654 /* Parse the end location. */
10655
f1310107
TJB
10656 init_linespec_result (&canonical_end);
10657 arg_start = arg;
10658
f8eba3c6 10659 /* We call decode_line_full directly here instead of using
f1310107
TJB
10660 parse_breakpoint_sals because we need to specify the start location's
10661 symtab and line as the default symtab and line for the end of the
10662 range. This makes it possible to have ranges like "foo.c:27, +14",
10663 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10664 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10665 sal_start.symtab, sal_start.line,
10666 &canonical_end, NULL, NULL);
10667
10668 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10669
f8eba3c6 10670 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10671 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10672
10673 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10674 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10675 || lsal_end->sals.nelts != 1)
f1310107
TJB
10676 error (_("Cannot create a ranged breakpoint with multiple locations."));
10677
f8eba3c6
TT
10678 sal_end = lsal_end->sals.sals[0];
10679 addr_string_end = savestring (arg_start, arg - arg_start);
10680 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10681
10682 end = find_breakpoint_range_end (sal_end);
10683 if (sal_start.pc > end)
177b42fe 10684 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10685
10686 length = end - sal_start.pc + 1;
10687 if (length < 0)
10688 /* Length overflowed. */
10689 error (_("Address range too large."));
10690 else if (length == 1)
10691 {
10692 /* This range is simple enough to be handled by
10693 the `hbreak' command. */
10694 hbreak_command (addr_string_start, 1);
10695
10696 do_cleanups (cleanup_bkpt);
10697
10698 return;
10699 }
10700
10701 /* Now set up the breakpoint. */
10702 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10703 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10704 set_breakpoint_count (breakpoint_count + 1);
10705 b->number = breakpoint_count;
10706 b->disposition = disp_donttouch;
f8eba3c6
TT
10707 b->addr_string = xstrdup (addr_string_start);
10708 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10709 b->loc->length = length;
10710
f8eba3c6 10711 do_cleanups (cleanup_bkpt);
f1310107
TJB
10712
10713 mention (b);
8d3788bd 10714 observer_notify_breakpoint_created (b);
44702360 10715 update_global_location_list (UGLL_MAY_INSERT);
f1310107
TJB
10716}
10717
4a64f543
MS
10718/* Return non-zero if EXP is verified as constant. Returned zero
10719 means EXP is variable. Also the constant detection may fail for
10720 some constant expressions and in such case still falsely return
10721 zero. */
2e6e3d9c 10722
65d79d4b
SDJ
10723static int
10724watchpoint_exp_is_const (const struct expression *exp)
10725{
10726 int i = exp->nelts;
10727
10728 while (i > 0)
10729 {
10730 int oplenp, argsp;
10731
10732 /* We are only interested in the descriptor of each element. */
10733 operator_length (exp, i, &oplenp, &argsp);
10734 i -= oplenp;
10735
10736 switch (exp->elts[i].opcode)
10737 {
10738 case BINOP_ADD:
10739 case BINOP_SUB:
10740 case BINOP_MUL:
10741 case BINOP_DIV:
10742 case BINOP_REM:
10743 case BINOP_MOD:
10744 case BINOP_LSH:
10745 case BINOP_RSH:
10746 case BINOP_LOGICAL_AND:
10747 case BINOP_LOGICAL_OR:
10748 case BINOP_BITWISE_AND:
10749 case BINOP_BITWISE_IOR:
10750 case BINOP_BITWISE_XOR:
10751 case BINOP_EQUAL:
10752 case BINOP_NOTEQUAL:
10753 case BINOP_LESS:
10754 case BINOP_GTR:
10755 case BINOP_LEQ:
10756 case BINOP_GEQ:
10757 case BINOP_REPEAT:
10758 case BINOP_COMMA:
10759 case BINOP_EXP:
10760 case BINOP_MIN:
10761 case BINOP_MAX:
10762 case BINOP_INTDIV:
10763 case BINOP_CONCAT:
65d79d4b
SDJ
10764 case TERNOP_COND:
10765 case TERNOP_SLICE:
65d79d4b
SDJ
10766
10767 case OP_LONG:
10768 case OP_DOUBLE:
10769 case OP_DECFLOAT:
10770 case OP_LAST:
10771 case OP_COMPLEX:
10772 case OP_STRING:
65d79d4b
SDJ
10773 case OP_ARRAY:
10774 case OP_TYPE:
608b4967
TT
10775 case OP_TYPEOF:
10776 case OP_DECLTYPE:
6e72ca20 10777 case OP_TYPEID:
65d79d4b
SDJ
10778 case OP_NAME:
10779 case OP_OBJC_NSSTRING:
10780
10781 case UNOP_NEG:
10782 case UNOP_LOGICAL_NOT:
10783 case UNOP_COMPLEMENT:
10784 case UNOP_ADDR:
10785 case UNOP_HIGH:
aeaa2474 10786 case UNOP_CAST:
9eaf6705
TT
10787
10788 case UNOP_CAST_TYPE:
10789 case UNOP_REINTERPRET_CAST:
10790 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10791 /* Unary, binary and ternary operators: We have to check
10792 their operands. If they are constant, then so is the
10793 result of that operation. For instance, if A and B are
10794 determined to be constants, then so is "A + B".
10795
10796 UNOP_IND is one exception to the rule above, because the
10797 value of *ADDR is not necessarily a constant, even when
10798 ADDR is. */
65d79d4b
SDJ
10799 break;
10800
10801 case OP_VAR_VALUE:
10802 /* Check whether the associated symbol is a constant.
4a64f543 10803
65d79d4b 10804 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10805 possible that a buggy compiler could mark a variable as
10806 constant even when it is not, and TYPE_CONST would return
10807 true in this case, while SYMBOL_CLASS wouldn't.
10808
10809 We also have to check for function symbols because they
10810 are always constant. */
65d79d4b
SDJ
10811 {
10812 struct symbol *s = exp->elts[i + 2].symbol;
10813
10814 if (SYMBOL_CLASS (s) != LOC_BLOCK
10815 && SYMBOL_CLASS (s) != LOC_CONST
10816 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10817 return 0;
10818 break;
10819 }
10820
10821 /* The default action is to return 0 because we are using
10822 the optimistic approach here: If we don't know something,
10823 then it is not a constant. */
10824 default:
10825 return 0;
10826 }
10827 }
10828
10829 return 1;
10830}
10831
3a5c3e22
PA
10832/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10833
10834static void
10835dtor_watchpoint (struct breakpoint *self)
10836{
10837 struct watchpoint *w = (struct watchpoint *) self;
10838
10839 xfree (w->cond_exp);
10840 xfree (w->exp);
10841 xfree (w->exp_string);
10842 xfree (w->exp_string_reparse);
10843 value_free (w->val);
10844
10845 base_breakpoint_ops.dtor (self);
10846}
10847
348d480f
PA
10848/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10849
10850static void
10851re_set_watchpoint (struct breakpoint *b)
10852{
3a5c3e22
PA
10853 struct watchpoint *w = (struct watchpoint *) b;
10854
348d480f
PA
10855 /* Watchpoint can be either on expression using entirely global
10856 variables, or it can be on local variables.
10857
10858 Watchpoints of the first kind are never auto-deleted, and even
10859 persist across program restarts. Since they can use variables
10860 from shared libraries, we need to reparse expression as libraries
10861 are loaded and unloaded.
10862
10863 Watchpoints on local variables can also change meaning as result
10864 of solib event. For example, if a watchpoint uses both a local
10865 and a global variables in expression, it's a local watchpoint,
10866 but unloading of a shared library will make the expression
10867 invalid. This is not a very common use case, but we still
10868 re-evaluate expression, to avoid surprises to the user.
10869
10870 Note that for local watchpoints, we re-evaluate it only if
10871 watchpoints frame id is still valid. If it's not, it means the
10872 watchpoint is out of scope and will be deleted soon. In fact,
10873 I'm not sure we'll ever be called in this case.
10874
10875 If a local watchpoint's frame id is still valid, then
3a5c3e22 10876 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10877
3a5c3e22
PA
10878 Don't do anything about disabled watchpoints, since they will be
10879 reevaluated again when enabled. */
10880 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10881}
10882
77b06cd7
TJB
10883/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10884
10885static int
10886insert_watchpoint (struct bp_location *bl)
10887{
3a5c3e22
PA
10888 struct watchpoint *w = (struct watchpoint *) bl->owner;
10889 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10890
10891 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10892 w->cond_exp);
77b06cd7
TJB
10893}
10894
10895/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10896
10897static int
10898remove_watchpoint (struct bp_location *bl)
10899{
3a5c3e22
PA
10900 struct watchpoint *w = (struct watchpoint *) bl->owner;
10901 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10902
10903 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10904 w->cond_exp);
e09342b5
TJB
10905}
10906
e09342b5 10907static int
348d480f 10908breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10909 struct address_space *aspace, CORE_ADDR bp_addr,
10910 const struct target_waitstatus *ws)
e09342b5 10911{
348d480f 10912 struct breakpoint *b = bl->owner;
3a5c3e22 10913 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10914
348d480f
PA
10915 /* Continuable hardware watchpoints are treated as non-existent if the
10916 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10917 some data address). Otherwise gdb won't stop on a break instruction
10918 in the code (not from a breakpoint) when a hardware watchpoint has
10919 been defined. Also skip watchpoints which we know did not trigger
10920 (did not match the data address). */
10921 if (is_hardware_watchpoint (b)
3a5c3e22 10922 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10923 return 0;
9c06b0b4 10924
348d480f 10925 return 1;
9c06b0b4
TJB
10926}
10927
348d480f
PA
10928static void
10929check_status_watchpoint (bpstat bs)
9c06b0b4 10930{
348d480f 10931 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10932
348d480f 10933 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10934}
10935
10936/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10937 hardware watchpoints. */
9c06b0b4
TJB
10938
10939static int
348d480f 10940resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10941{
3a5c3e22
PA
10942 struct watchpoint *w = (struct watchpoint *) bl->owner;
10943 int length = w->exact? 1 : bl->length;
348d480f
PA
10944
10945 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10946}
10947
10948/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10949 hardware watchpoints. */
9c06b0b4
TJB
10950
10951static int
348d480f 10952works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10953{
efa80663
PA
10954 /* Read and access watchpoints only work with hardware support. */
10955 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10956}
10957
9c06b0b4 10958static enum print_stop_action
348d480f 10959print_it_watchpoint (bpstat bs)
9c06b0b4 10960{
348d480f
PA
10961 struct cleanup *old_chain;
10962 struct breakpoint *b;
f99d8bf4 10963 struct ui_file *stb;
348d480f 10964 enum print_stop_action result;
3a5c3e22 10965 struct watchpoint *w;
79a45e25 10966 struct ui_out *uiout = current_uiout;
348d480f
PA
10967
10968 gdb_assert (bs->bp_location_at != NULL);
10969
348d480f 10970 b = bs->breakpoint_at;
3a5c3e22 10971 w = (struct watchpoint *) b;
348d480f 10972
f99d8bf4
PA
10973 stb = mem_fileopen ();
10974 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10975
10976 switch (b->type)
10977 {
348d480f 10978 case bp_watchpoint:
9c06b0b4
TJB
10979 case bp_hardware_watchpoint:
10980 annotate_watchpoint (b->number);
10981 if (ui_out_is_mi_like_p (uiout))
10982 ui_out_field_string
10983 (uiout, "reason",
10984 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10985 mention (b);
10986 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10987 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10988 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10989 ui_out_field_stream (uiout, "old", stb);
10990 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10991 watchpoint_value_print (w->val, stb);
348d480f
PA
10992 ui_out_field_stream (uiout, "new", stb);
10993 ui_out_text (uiout, "\n");
10994 /* More than one watchpoint may have been triggered. */
10995 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10996 break;
10997
10998 case bp_read_watchpoint:
10999 if (ui_out_is_mi_like_p (uiout))
11000 ui_out_field_string
11001 (uiout, "reason",
11002 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
11003 mention (b);
11004 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11005 ui_out_text (uiout, "\nValue = ");
f99d8bf4 11006 watchpoint_value_print (w->val, stb);
348d480f
PA
11007 ui_out_field_stream (uiout, "value", stb);
11008 ui_out_text (uiout, "\n");
11009 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11010 break;
11011
11012 case bp_access_watchpoint:
348d480f
PA
11013 if (bs->old_val != NULL)
11014 {
11015 annotate_watchpoint (b->number);
11016 if (ui_out_is_mi_like_p (uiout))
11017 ui_out_field_string
11018 (uiout, "reason",
11019 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11020 mention (b);
11021 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11022 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 11023 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
11024 ui_out_field_stream (uiout, "old", stb);
11025 ui_out_text (uiout, "\nNew value = ");
11026 }
11027 else
11028 {
11029 mention (b);
11030 if (ui_out_is_mi_like_p (uiout))
11031 ui_out_field_string
11032 (uiout, "reason",
11033 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11034 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11035 ui_out_text (uiout, "\nValue = ");
11036 }
f99d8bf4 11037 watchpoint_value_print (w->val, stb);
348d480f
PA
11038 ui_out_field_stream (uiout, "new", stb);
11039 ui_out_text (uiout, "\n");
11040 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11041 break;
11042 default:
348d480f 11043 result = PRINT_UNKNOWN;
9c06b0b4
TJB
11044 }
11045
348d480f
PA
11046 do_cleanups (old_chain);
11047 return result;
11048}
11049
11050/* Implement the "print_mention" breakpoint_ops method for hardware
11051 watchpoints. */
11052
11053static void
11054print_mention_watchpoint (struct breakpoint *b)
11055{
11056 struct cleanup *ui_out_chain;
3a5c3e22 11057 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11058 struct ui_out *uiout = current_uiout;
348d480f
PA
11059
11060 switch (b->type)
11061 {
11062 case bp_watchpoint:
11063 ui_out_text (uiout, "Watchpoint ");
11064 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11065 break;
11066 case bp_hardware_watchpoint:
11067 ui_out_text (uiout, "Hardware watchpoint ");
11068 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11069 break;
11070 case bp_read_watchpoint:
11071 ui_out_text (uiout, "Hardware read watchpoint ");
11072 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11073 break;
11074 case bp_access_watchpoint:
11075 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
11076 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11077 break;
11078 default:
11079 internal_error (__FILE__, __LINE__,
11080 _("Invalid hardware watchpoint type."));
11081 }
11082
11083 ui_out_field_int (uiout, "number", b->number);
11084 ui_out_text (uiout, ": ");
3a5c3e22 11085 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
11086 do_cleanups (ui_out_chain);
11087}
11088
11089/* Implement the "print_recreate" breakpoint_ops method for
11090 watchpoints. */
11091
11092static void
11093print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
11094{
3a5c3e22
PA
11095 struct watchpoint *w = (struct watchpoint *) b;
11096
348d480f
PA
11097 switch (b->type)
11098 {
11099 case bp_watchpoint:
11100 case bp_hardware_watchpoint:
11101 fprintf_unfiltered (fp, "watch");
11102 break;
11103 case bp_read_watchpoint:
11104 fprintf_unfiltered (fp, "rwatch");
11105 break;
11106 case bp_access_watchpoint:
11107 fprintf_unfiltered (fp, "awatch");
11108 break;
11109 default:
11110 internal_error (__FILE__, __LINE__,
11111 _("Invalid watchpoint type."));
11112 }
11113
3a5c3e22 11114 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 11115 print_recreate_thread (b, fp);
348d480f
PA
11116}
11117
427cd150
TT
11118/* Implement the "explains_signal" breakpoint_ops method for
11119 watchpoints. */
11120
47591c29 11121static int
427cd150
TT
11122explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
11123{
11124 /* A software watchpoint cannot cause a signal other than
11125 GDB_SIGNAL_TRAP. */
11126 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 11127 return 0;
427cd150 11128
47591c29 11129 return 1;
427cd150
TT
11130}
11131
348d480f
PA
11132/* The breakpoint_ops structure to be used in hardware watchpoints. */
11133
2060206e 11134static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
11135
11136/* Implement the "insert" breakpoint_ops method for
11137 masked hardware watchpoints. */
11138
11139static int
11140insert_masked_watchpoint (struct bp_location *bl)
11141{
3a5c3e22
PA
11142 struct watchpoint *w = (struct watchpoint *) bl->owner;
11143
11144 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11145 bl->watchpoint_type);
11146}
11147
11148/* Implement the "remove" breakpoint_ops method for
11149 masked hardware watchpoints. */
11150
11151static int
11152remove_masked_watchpoint (struct bp_location *bl)
11153{
3a5c3e22
PA
11154 struct watchpoint *w = (struct watchpoint *) bl->owner;
11155
11156 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
11157 bl->watchpoint_type);
11158}
11159
11160/* Implement the "resources_needed" breakpoint_ops method for
11161 masked hardware watchpoints. */
11162
11163static int
11164resources_needed_masked_watchpoint (const struct bp_location *bl)
11165{
3a5c3e22
PA
11166 struct watchpoint *w = (struct watchpoint *) bl->owner;
11167
11168 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
11169}
11170
11171/* Implement the "works_in_software_mode" breakpoint_ops method for
11172 masked hardware watchpoints. */
11173
11174static int
11175works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
11176{
11177 return 0;
11178}
11179
11180/* Implement the "print_it" breakpoint_ops method for
11181 masked hardware watchpoints. */
11182
11183static enum print_stop_action
11184print_it_masked_watchpoint (bpstat bs)
11185{
11186 struct breakpoint *b = bs->breakpoint_at;
79a45e25 11187 struct ui_out *uiout = current_uiout;
348d480f
PA
11188
11189 /* Masked watchpoints have only one location. */
11190 gdb_assert (b->loc && b->loc->next == NULL);
11191
11192 switch (b->type)
11193 {
11194 case bp_hardware_watchpoint:
11195 annotate_watchpoint (b->number);
11196 if (ui_out_is_mi_like_p (uiout))
11197 ui_out_field_string
11198 (uiout, "reason",
11199 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11200 break;
11201
11202 case bp_read_watchpoint:
11203 if (ui_out_is_mi_like_p (uiout))
11204 ui_out_field_string
11205 (uiout, "reason",
11206 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11207 break;
11208
11209 case bp_access_watchpoint:
11210 if (ui_out_is_mi_like_p (uiout))
11211 ui_out_field_string
11212 (uiout, "reason",
11213 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11214 break;
11215 default:
11216 internal_error (__FILE__, __LINE__,
11217 _("Invalid hardware watchpoint type."));
11218 }
11219
11220 mention (b);
9c06b0b4
TJB
11221 ui_out_text (uiout, _("\n\
11222Check the underlying instruction at PC for the memory\n\
11223address and value which triggered this watchpoint.\n"));
11224 ui_out_text (uiout, "\n");
11225
11226 /* More than one watchpoint may have been triggered. */
11227 return PRINT_UNKNOWN;
11228}
11229
11230/* Implement the "print_one_detail" breakpoint_ops method for
11231 masked hardware watchpoints. */
11232
11233static void
11234print_one_detail_masked_watchpoint (const struct breakpoint *b,
11235 struct ui_out *uiout)
11236{
3a5c3e22
PA
11237 struct watchpoint *w = (struct watchpoint *) b;
11238
9c06b0b4
TJB
11239 /* Masked watchpoints have only one location. */
11240 gdb_assert (b->loc && b->loc->next == NULL);
11241
11242 ui_out_text (uiout, "\tmask ");
3a5c3e22 11243 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11244 ui_out_text (uiout, "\n");
11245}
11246
11247/* Implement the "print_mention" breakpoint_ops method for
11248 masked hardware watchpoints. */
11249
11250static void
11251print_mention_masked_watchpoint (struct breakpoint *b)
11252{
3a5c3e22 11253 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11254 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11255 struct cleanup *ui_out_chain;
11256
11257 switch (b->type)
11258 {
11259 case bp_hardware_watchpoint:
11260 ui_out_text (uiout, "Masked hardware watchpoint ");
11261 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11262 break;
11263 case bp_read_watchpoint:
11264 ui_out_text (uiout, "Masked hardware read watchpoint ");
11265 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11266 break;
11267 case bp_access_watchpoint:
11268 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11269 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11270 break;
11271 default:
11272 internal_error (__FILE__, __LINE__,
11273 _("Invalid hardware watchpoint type."));
11274 }
11275
11276 ui_out_field_int (uiout, "number", b->number);
11277 ui_out_text (uiout, ": ");
3a5c3e22 11278 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11279 do_cleanups (ui_out_chain);
11280}
11281
11282/* Implement the "print_recreate" breakpoint_ops method for
11283 masked hardware watchpoints. */
11284
11285static void
11286print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11287{
3a5c3e22 11288 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11289 char tmp[40];
11290
11291 switch (b->type)
11292 {
11293 case bp_hardware_watchpoint:
11294 fprintf_unfiltered (fp, "watch");
11295 break;
11296 case bp_read_watchpoint:
11297 fprintf_unfiltered (fp, "rwatch");
11298 break;
11299 case bp_access_watchpoint:
11300 fprintf_unfiltered (fp, "awatch");
11301 break;
11302 default:
11303 internal_error (__FILE__, __LINE__,
11304 _("Invalid hardware watchpoint type."));
11305 }
11306
3a5c3e22
PA
11307 sprintf_vma (tmp, w->hw_wp_mask);
11308 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11309 print_recreate_thread (b, fp);
9c06b0b4
TJB
11310}
11311
11312/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11313
2060206e 11314static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11315
11316/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11317
11318static int
11319is_masked_watchpoint (const struct breakpoint *b)
11320{
11321 return b->ops == &masked_watchpoint_breakpoint_ops;
11322}
11323
53a5351d
JM
11324/* accessflag: hw_write: watch write,
11325 hw_read: watch read,
11326 hw_access: watch access (read or write) */
c906108c 11327static void
bbc13ae3 11328watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11329 int just_location, int internal)
c906108c 11330{
a9634178 11331 volatile struct gdb_exception e;
d983da9c 11332 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11333 struct expression *exp;
270140bd 11334 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11335 struct value *val, *mark, *result;
bb9d5f81 11336 int saved_bitpos = 0, saved_bitsize = 0;
c906108c 11337 struct frame_info *frame;
bbc13ae3
KS
11338 const char *exp_start = NULL;
11339 const char *exp_end = NULL;
11340 const char *tok, *end_tok;
9c06b0b4 11341 int toklen = -1;
bbc13ae3
KS
11342 const char *cond_start = NULL;
11343 const char *cond_end = NULL;
c906108c 11344 enum bptype bp_type;
37e4754d 11345 int thread = -1;
0cf6dd15 11346 int pc = 0;
9c06b0b4
TJB
11347 /* Flag to indicate whether we are going to use masks for
11348 the hardware watchpoint. */
11349 int use_mask = 0;
11350 CORE_ADDR mask = 0;
3a5c3e22 11351 struct watchpoint *w;
bbc13ae3
KS
11352 char *expression;
11353 struct cleanup *back_to;
c906108c 11354
37e4754d
LM
11355 /* Make sure that we actually have parameters to parse. */
11356 if (arg != NULL && arg[0] != '\0')
11357 {
bbc13ae3
KS
11358 const char *value_start;
11359
11360 exp_end = arg + strlen (arg);
37e4754d 11361
9c06b0b4
TJB
11362 /* Look for "parameter value" pairs at the end
11363 of the arguments string. */
bbc13ae3 11364 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11365 {
11366 /* Skip whitespace at the end of the argument list. */
11367 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11368 tok--;
11369
11370 /* Find the beginning of the last token.
11371 This is the value of the parameter. */
11372 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11373 tok--;
11374 value_start = tok + 1;
11375
11376 /* Skip whitespace. */
11377 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11378 tok--;
11379
11380 end_tok = tok;
11381
11382 /* Find the beginning of the second to last token.
11383 This is the parameter itself. */
11384 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11385 tok--;
11386 tok++;
11387 toklen = end_tok - tok + 1;
11388
11389 if (toklen == 6 && !strncmp (tok, "thread", 6))
11390 {
11391 /* At this point we've found a "thread" token, which means
11392 the user is trying to set a watchpoint that triggers
11393 only in a specific thread. */
11394 char *endp;
37e4754d 11395
9c06b0b4
TJB
11396 if (thread != -1)
11397 error(_("You can specify only one thread."));
37e4754d 11398
9c06b0b4
TJB
11399 /* Extract the thread ID from the next token. */
11400 thread = strtol (value_start, &endp, 0);
37e4754d 11401
9c06b0b4
TJB
11402 /* Check if the user provided a valid numeric value for the
11403 thread ID. */
11404 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11405 error (_("Invalid thread ID specification %s."), value_start);
11406
11407 /* Check if the thread actually exists. */
11408 if (!valid_thread_id (thread))
af4908ba 11409 invalid_thread_id_error (thread);
9c06b0b4
TJB
11410 }
11411 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11412 {
11413 /* We've found a "mask" token, which means the user wants to
11414 create a hardware watchpoint that is going to have the mask
11415 facility. */
11416 struct value *mask_value, *mark;
37e4754d 11417
9c06b0b4
TJB
11418 if (use_mask)
11419 error(_("You can specify only one mask."));
37e4754d 11420
9c06b0b4 11421 use_mask = just_location = 1;
37e4754d 11422
9c06b0b4
TJB
11423 mark = value_mark ();
11424 mask_value = parse_to_comma_and_eval (&value_start);
11425 mask = value_as_address (mask_value);
11426 value_free_to_mark (mark);
11427 }
11428 else
11429 /* We didn't recognize what we found. We should stop here. */
11430 break;
37e4754d 11431
9c06b0b4
TJB
11432 /* Truncate the string and get rid of the "parameter value" pair before
11433 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11434 exp_end = tok;
9c06b0b4 11435 }
37e4754d 11436 }
bbc13ae3
KS
11437 else
11438 exp_end = arg;
37e4754d 11439
bbc13ae3
KS
11440 /* Parse the rest of the arguments. From here on out, everything
11441 is in terms of a newly allocated string instead of the original
11442 ARG. */
c906108c 11443 innermost_block = NULL;
bbc13ae3
KS
11444 expression = savestring (arg, exp_end - arg);
11445 back_to = make_cleanup (xfree, expression);
11446 exp_start = arg = expression;
1bb9788d 11447 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11448 exp_end = arg;
fa8a61dc
TT
11449 /* Remove trailing whitespace from the expression before saving it.
11450 This makes the eventual display of the expression string a bit
11451 prettier. */
11452 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11453 --exp_end;
11454
65d79d4b
SDJ
11455 /* Checking if the expression is not constant. */
11456 if (watchpoint_exp_is_const (exp))
11457 {
11458 int len;
11459
11460 len = exp_end - exp_start;
11461 while (len > 0 && isspace (exp_start[len - 1]))
11462 len--;
11463 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11464 }
11465
c906108c
SS
11466 exp_valid_block = innermost_block;
11467 mark = value_mark ();
3a1115a0 11468 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b 11469
bb9d5f81
PP
11470 if (val != NULL && just_location)
11471 {
11472 saved_bitpos = value_bitpos (val);
11473 saved_bitsize = value_bitsize (val);
11474 }
11475
06a64a0b
TT
11476 if (just_location)
11477 {
9c06b0b4
TJB
11478 int ret;
11479
06a64a0b 11480 exp_valid_block = NULL;
a1442452 11481 val = value_addr (result);
06a64a0b
TT
11482 release_value (val);
11483 value_free_to_mark (mark);
9c06b0b4
TJB
11484
11485 if (use_mask)
11486 {
11487 ret = target_masked_watch_num_registers (value_as_address (val),
11488 mask);
11489 if (ret == -1)
11490 error (_("This target does not support masked watchpoints."));
11491 else if (ret == -2)
11492 error (_("Invalid mask or memory region."));
11493 }
06a64a0b
TT
11494 }
11495 else if (val != NULL)
fa4727a6 11496 release_value (val);
c906108c 11497
bbc13ae3
KS
11498 tok = skip_spaces_const (arg);
11499 end_tok = skip_to_space_const (tok);
c906108c
SS
11500
11501 toklen = end_tok - tok;
11502 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11503 {
2d134ed3
PA
11504 struct expression *cond;
11505
60e1c644 11506 innermost_block = NULL;
c906108c 11507 tok = cond_start = end_tok + 1;
1bb9788d 11508 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11509
11510 /* The watchpoint expression may not be local, but the condition
11511 may still be. E.g.: `watch global if local > 0'. */
11512 cond_exp_valid_block = innermost_block;
11513
2d134ed3 11514 xfree (cond);
c906108c
SS
11515 cond_end = tok;
11516 }
11517 if (*tok)
8a3fe4f8 11518 error (_("Junk at end of command."));
c906108c 11519
d983da9c 11520 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11521
11522 /* If the expression is "local", then set up a "watchpoint scope"
11523 breakpoint at the point where we've left the scope of the watchpoint
11524 expression. Create the scope breakpoint before the watchpoint, so
11525 that we will encounter it first in bpstat_stop_status. */
60e1c644 11526 if (exp_valid_block && frame)
d983da9c 11527 {
edb3359d
DJ
11528 if (frame_id_p (frame_unwind_caller_id (frame)))
11529 {
11530 scope_breakpoint
a6d9a66e
UW
11531 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11532 frame_unwind_caller_pc (frame),
06edf0c0
PA
11533 bp_watchpoint_scope,
11534 &momentary_breakpoint_ops);
d983da9c 11535
edb3359d 11536 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11537
edb3359d
DJ
11538 /* Automatically delete the breakpoint when it hits. */
11539 scope_breakpoint->disposition = disp_del;
d983da9c 11540
edb3359d
DJ
11541 /* Only break in the proper frame (help with recursion). */
11542 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11543
edb3359d 11544 /* Set the address at which we will stop. */
a6d9a66e
UW
11545 scope_breakpoint->loc->gdbarch
11546 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11547 scope_breakpoint->loc->requested_address
11548 = frame_unwind_caller_pc (frame);
11549 scope_breakpoint->loc->address
a6d9a66e
UW
11550 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11551 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11552 scope_breakpoint->type);
11553 }
d983da9c
DJ
11554 }
11555
e8369a73
AB
11556 /* Now set up the breakpoint. We create all watchpoints as hardware
11557 watchpoints here even if hardware watchpoints are turned off, a call
11558 to update_watchpoint later in this function will cause the type to
11559 drop back to bp_watchpoint (software watchpoint) if required. */
11560
11561 if (accessflag == hw_read)
11562 bp_type = bp_read_watchpoint;
11563 else if (accessflag == hw_access)
11564 bp_type = bp_access_watchpoint;
11565 else
11566 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11567
11568 w = XCNEW (struct watchpoint);
11569 b = &w->base;
348d480f 11570 if (use_mask)
3a5c3e22
PA
11571 init_raw_breakpoint_without_location (b, NULL, bp_type,
11572 &masked_watchpoint_breakpoint_ops);
348d480f 11573 else
3a5c3e22
PA
11574 init_raw_breakpoint_without_location (b, NULL, bp_type,
11575 &watchpoint_breakpoint_ops);
37e4754d 11576 b->thread = thread;
b5de0fa7 11577 b->disposition = disp_donttouch;
348d480f 11578 b->pspace = current_program_space;
3a5c3e22
PA
11579 w->exp = exp;
11580 w->exp_valid_block = exp_valid_block;
11581 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11582 if (just_location)
11583 {
11584 struct type *t = value_type (val);
11585 CORE_ADDR addr = value_as_address (val);
11586 char *name;
11587
11588 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11589 name = type_to_string (t);
11590
3a5c3e22 11591 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11592 core_addr_to_string (addr));
06a64a0b
TT
11593 xfree (name);
11594
3a5c3e22 11595 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11596 (int) (exp_end - exp_start), exp_start);
11597
06a64a0b
TT
11598 /* The above expression is in C. */
11599 b->language = language_c;
11600 }
11601 else
3a5c3e22 11602 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11603
11604 if (use_mask)
11605 {
3a5c3e22 11606 w->hw_wp_mask = mask;
9c06b0b4
TJB
11607 }
11608 else
11609 {
3a5c3e22 11610 w->val = val;
bb9d5f81
PP
11611 w->val_bitpos = saved_bitpos;
11612 w->val_bitsize = saved_bitsize;
3a5c3e22 11613 w->val_valid = 1;
9c06b0b4 11614 }
77b06cd7 11615
c906108c
SS
11616 if (cond_start)
11617 b->cond_string = savestring (cond_start, cond_end - cond_start);
11618 else
11619 b->cond_string = 0;
c5aa993b 11620
c906108c 11621 if (frame)
f6bc2008 11622 {
3a5c3e22
PA
11623 w->watchpoint_frame = get_frame_id (frame);
11624 w->watchpoint_thread = inferior_ptid;
f6bc2008 11625 }
c906108c 11626 else
f6bc2008 11627 {
3a5c3e22
PA
11628 w->watchpoint_frame = null_frame_id;
11629 w->watchpoint_thread = null_ptid;
f6bc2008 11630 }
c906108c 11631
d983da9c 11632 if (scope_breakpoint != NULL)
c906108c 11633 {
d983da9c
DJ
11634 /* The scope breakpoint is related to the watchpoint. We will
11635 need to act on them together. */
11636 b->related_breakpoint = scope_breakpoint;
11637 scope_breakpoint->related_breakpoint = b;
c906108c 11638 }
d983da9c 11639
06a64a0b
TT
11640 if (!just_location)
11641 value_free_to_mark (mark);
2d134ed3 11642
a9634178
TJB
11643 TRY_CATCH (e, RETURN_MASK_ALL)
11644 {
11645 /* Finally update the new watchpoint. This creates the locations
11646 that should be inserted. */
3a5c3e22 11647 update_watchpoint (w, 1);
a9634178
TJB
11648 }
11649 if (e.reason < 0)
11650 {
11651 delete_breakpoint (b);
11652 throw_exception (e);
11653 }
11654
3ea46bff 11655 install_breakpoint (internal, b, 1);
bbc13ae3 11656 do_cleanups (back_to);
c906108c
SS
11657}
11658
e09342b5 11659/* Return count of debug registers needed to watch the given expression.
e09342b5 11660 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11661
c906108c 11662static int
a9634178 11663can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11664{
11665 int found_memory_cnt = 0;
2e70b7b9 11666 struct value *head = v;
c906108c
SS
11667
11668 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11669 if (!can_use_hw_watchpoints)
c906108c 11670 return 0;
c5aa993b 11671
5c44784c
JM
11672 /* Make sure that the value of the expression depends only upon
11673 memory contents, and values computed from them within GDB. If we
11674 find any register references or function calls, we can't use a
11675 hardware watchpoint.
11676
11677 The idea here is that evaluating an expression generates a series
11678 of values, one holding the value of every subexpression. (The
11679 expression a*b+c has five subexpressions: a, b, a*b, c, and
11680 a*b+c.) GDB's values hold almost enough information to establish
11681 the criteria given above --- they identify memory lvalues,
11682 register lvalues, computed values, etcetera. So we can evaluate
11683 the expression, and then scan the chain of values that leaves
11684 behind to decide whether we can detect any possible change to the
11685 expression's final value using only hardware watchpoints.
11686
11687 However, I don't think that the values returned by inferior
11688 function calls are special in any way. So this function may not
11689 notice that an expression involving an inferior function call
11690 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11691 for (; v; v = value_next (v))
c906108c 11692 {
5c44784c 11693 if (VALUE_LVAL (v) == lval_memory)
c906108c 11694 {
8464be76
DJ
11695 if (v != head && value_lazy (v))
11696 /* A lazy memory lvalue in the chain is one that GDB never
11697 needed to fetch; we either just used its address (e.g.,
11698 `a' in `a.b') or we never needed it at all (e.g., `a'
11699 in `a,b'). This doesn't apply to HEAD; if that is
11700 lazy then it was not readable, but watch it anyway. */
5c44784c 11701 ;
53a5351d 11702 else
5c44784c
JM
11703 {
11704 /* Ahh, memory we actually used! Check if we can cover
11705 it with hardware watchpoints. */
df407dfe 11706 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11707
11708 /* We only watch structs and arrays if user asked for it
11709 explicitly, never if they just happen to appear in a
11710 middle of some value chain. */
11711 if (v == head
11712 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11713 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11714 {
42ae5230 11715 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11716 int len;
11717 int num_regs;
11718
a9634178 11719 len = (target_exact_watchpoints
e09342b5
TJB
11720 && is_scalar_type_recursive (vtype))?
11721 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11722
e09342b5
TJB
11723 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11724 if (!num_regs)
2e70b7b9
MS
11725 return 0;
11726 else
e09342b5 11727 found_memory_cnt += num_regs;
2e70b7b9 11728 }
5c44784c 11729 }
c5aa993b 11730 }
5086187c
AC
11731 else if (VALUE_LVAL (v) != not_lval
11732 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11733 return 0; /* These are values from the history (e.g., $1). */
5086187c 11734 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11735 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11736 }
11737
11738 /* The expression itself looks suitable for using a hardware
11739 watchpoint, but give the target machine a chance to reject it. */
11740 return found_memory_cnt;
11741}
11742
8b93c638 11743void
84f4c1fe 11744watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11745{
84f4c1fe 11746 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11747}
11748
06a64a0b
TT
11749/* A helper function that looks for the "-location" argument and then
11750 calls watch_command_1. */
11751
11752static void
11753watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11754{
11755 int just_location = 0;
11756
11757 if (arg
11758 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11759 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11760 {
e9cafbcc 11761 arg = skip_spaces (arg);
06a64a0b
TT
11762 just_location = 1;
11763 }
11764
84f4c1fe 11765 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11766}
8926118c 11767
c5aa993b 11768static void
fba45db2 11769watch_command (char *arg, int from_tty)
c906108c 11770{
06a64a0b 11771 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11772}
11773
8b93c638 11774void
84f4c1fe 11775rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11776{
84f4c1fe 11777 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11778}
8926118c 11779
c5aa993b 11780static void
fba45db2 11781rwatch_command (char *arg, int from_tty)
c906108c 11782{
06a64a0b 11783 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11784}
11785
8b93c638 11786void
84f4c1fe 11787awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11788{
84f4c1fe 11789 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11790}
8926118c 11791
c5aa993b 11792static void
fba45db2 11793awatch_command (char *arg, int from_tty)
c906108c 11794{
06a64a0b 11795 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11796}
c906108c 11797\f
c5aa993b 11798
43ff13b4 11799/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11800 because it uses the mechanisms of breakpoints. */
11801
bfec99b2
PA
11802struct until_break_command_continuation_args
11803{
11804 struct breakpoint *breakpoint;
11805 struct breakpoint *breakpoint2;
186c406b 11806 int thread_num;
bfec99b2
PA
11807};
11808
43ff13b4 11809/* This function is called by fetch_inferior_event via the
4a64f543 11810 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11811 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11812 command. */
c2c6d25f 11813static void
fa4cd53f 11814until_break_command_continuation (void *arg, int err)
43ff13b4 11815{
bfec99b2
PA
11816 struct until_break_command_continuation_args *a = arg;
11817
11818 delete_breakpoint (a->breakpoint);
11819 if (a->breakpoint2)
11820 delete_breakpoint (a->breakpoint2);
186c406b 11821 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11822}
11823
c906108c 11824void
ae66c1fc 11825until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11826{
11827 struct symtabs_and_lines sals;
11828 struct symtab_and_line sal;
8556afb4
PA
11829 struct frame_info *frame;
11830 struct gdbarch *frame_gdbarch;
11831 struct frame_id stack_frame_id;
11832 struct frame_id caller_frame_id;
c906108c 11833 struct breakpoint *breakpoint;
f107f563 11834 struct breakpoint *breakpoint2 = NULL;
c906108c 11835 struct cleanup *old_chain;
186c406b
TT
11836 int thread;
11837 struct thread_info *tp;
c906108c 11838
70509625 11839 clear_proceed_status (0);
c906108c
SS
11840
11841 /* Set a breakpoint where the user wants it and at return from
4a64f543 11842 this function. */
c5aa993b 11843
1bfeeb0f 11844 if (last_displayed_sal_is_valid ())
f8eba3c6 11845 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11846 get_last_displayed_symtab (),
f8eba3c6 11847 get_last_displayed_line ());
c906108c 11848 else
f8eba3c6
TT
11849 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11850 (struct symtab *) NULL, 0);
c5aa993b 11851
c906108c 11852 if (sals.nelts != 1)
8a3fe4f8 11853 error (_("Couldn't get information on specified line."));
c5aa993b 11854
c906108c 11855 sal = sals.sals[0];
4a64f543 11856 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11857
c906108c 11858 if (*arg)
8a3fe4f8 11859 error (_("Junk at end of arguments."));
c5aa993b 11860
c906108c 11861 resolve_sal_pc (&sal);
c5aa993b 11862
186c406b
TT
11863 tp = inferior_thread ();
11864 thread = tp->num;
11865
883bc8d1
PA
11866 old_chain = make_cleanup (null_cleanup, NULL);
11867
8556afb4
PA
11868 /* Note linespec handling above invalidates the frame chain.
11869 Installing a breakpoint also invalidates the frame chain (as it
11870 may need to switch threads), so do any frame handling before
11871 that. */
11872
11873 frame = get_selected_frame (NULL);
11874 frame_gdbarch = get_frame_arch (frame);
11875 stack_frame_id = get_stack_frame_id (frame);
11876 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11877
ae66c1fc
EZ
11878 /* Keep within the current frame, or in frames called by the current
11879 one. */
edb3359d 11880
883bc8d1 11881 if (frame_id_p (caller_frame_id))
c906108c 11882 {
883bc8d1
PA
11883 struct symtab_and_line sal2;
11884
11885 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11886 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11887 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11888 sal2,
11889 caller_frame_id,
f107f563
VP
11890 bp_until);
11891 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11892
883bc8d1 11893 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11894 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11895 }
c5aa993b 11896
c70a6932
JK
11897 /* set_momentary_breakpoint could invalidate FRAME. */
11898 frame = NULL;
11899
883bc8d1
PA
11900 if (anywhere)
11901 /* If the user told us to continue until a specified location,
11902 we don't specify a frame at which we need to stop. */
11903 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11904 null_frame_id, bp_until);
11905 else
11906 /* Otherwise, specify the selected frame, because we want to stop
11907 only at the very same frame. */
11908 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11909 stack_frame_id, bp_until);
11910 make_cleanup_delete_breakpoint (breakpoint);
11911
a493e3e2 11912 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11913
4a64f543
MS
11914 /* If we are running asynchronously, and proceed call above has
11915 actually managed to start the target, arrange for breakpoints to
11916 be deleted when the target stops. Otherwise, we're already
11917 stopped and delete breakpoints via cleanup chain. */
f107f563 11918
8ea051c5 11919 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11920 {
bfec99b2
PA
11921 struct until_break_command_continuation_args *args;
11922 args = xmalloc (sizeof (*args));
f107f563 11923
bfec99b2
PA
11924 args->breakpoint = breakpoint;
11925 args->breakpoint2 = breakpoint2;
186c406b 11926 args->thread_num = thread;
f107f563
VP
11927
11928 discard_cleanups (old_chain);
95e54da7
PA
11929 add_continuation (inferior_thread (),
11930 until_break_command_continuation, args,
604ead4a 11931 xfree);
f107f563
VP
11932 }
11933 else
c5aa993b 11934 do_cleanups (old_chain);
c906108c 11935}
ae66c1fc 11936
c906108c
SS
11937/* This function attempts to parse an optional "if <cond>" clause
11938 from the arg string. If one is not found, it returns NULL.
c5aa993b 11939
c906108c
SS
11940 Else, it returns a pointer to the condition string. (It does not
11941 attempt to evaluate the string against a particular block.) And,
11942 it updates arg to point to the first character following the parsed
4a64f543 11943 if clause in the arg string. */
53a5351d 11944
916703c0 11945char *
fba45db2 11946ep_parse_optional_if_clause (char **arg)
c906108c 11947{
c5aa993b
JM
11948 char *cond_string;
11949
11950 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11951 return NULL;
c5aa993b 11952
4a64f543 11953 /* Skip the "if" keyword. */
c906108c 11954 (*arg) += 2;
c5aa993b 11955
c906108c 11956 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11957 condition string. */
e9cafbcc 11958 *arg = skip_spaces (*arg);
c906108c 11959 cond_string = *arg;
c5aa993b 11960
4a64f543
MS
11961 /* Assume that the condition occupies the remainder of the arg
11962 string. */
c906108c 11963 (*arg) += strlen (cond_string);
c5aa993b 11964
c906108c
SS
11965 return cond_string;
11966}
c5aa993b 11967
c906108c
SS
11968/* Commands to deal with catching events, such as signals, exceptions,
11969 process start/exit, etc. */
c5aa993b
JM
11970
11971typedef enum
11972{
44feb3ce
TT
11973 catch_fork_temporary, catch_vfork_temporary,
11974 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11975}
11976catch_fork_kind;
11977
c906108c 11978static void
cc59ec59
MS
11979catch_fork_command_1 (char *arg, int from_tty,
11980 struct cmd_list_element *command)
c906108c 11981{
a6d9a66e 11982 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11983 char *cond_string = NULL;
44feb3ce
TT
11984 catch_fork_kind fork_kind;
11985 int tempflag;
11986
11987 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11988 tempflag = (fork_kind == catch_fork_temporary
11989 || fork_kind == catch_vfork_temporary);
c5aa993b 11990
44feb3ce
TT
11991 if (!arg)
11992 arg = "";
e9cafbcc 11993 arg = skip_spaces (arg);
c5aa993b 11994
c906108c 11995 /* The allowed syntax is:
c5aa993b
JM
11996 catch [v]fork
11997 catch [v]fork if <cond>
11998
4a64f543 11999 First, check if there's an if clause. */
c906108c 12000 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 12001
c906108c 12002 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 12003 error (_("Junk at end of arguments."));
c5aa993b 12004
c906108c 12005 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 12006 and enable reporting of such events. */
c5aa993b
JM
12007 switch (fork_kind)
12008 {
44feb3ce
TT
12009 case catch_fork_temporary:
12010 case catch_fork_permanent:
a6d9a66e 12011 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 12012 &catch_fork_breakpoint_ops);
c906108c 12013 break;
44feb3ce
TT
12014 case catch_vfork_temporary:
12015 case catch_vfork_permanent:
a6d9a66e 12016 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 12017 &catch_vfork_breakpoint_ops);
c906108c 12018 break;
c5aa993b 12019 default:
8a3fe4f8 12020 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 12021 break;
c5aa993b 12022 }
c906108c
SS
12023}
12024
12025static void
cc59ec59
MS
12026catch_exec_command_1 (char *arg, int from_tty,
12027 struct cmd_list_element *command)
c906108c 12028{
b4d90040 12029 struct exec_catchpoint *c;
a6d9a66e 12030 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 12031 int tempflag;
c5aa993b 12032 char *cond_string = NULL;
c906108c 12033
44feb3ce
TT
12034 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12035
12036 if (!arg)
12037 arg = "";
e9cafbcc 12038 arg = skip_spaces (arg);
c906108c
SS
12039
12040 /* The allowed syntax is:
c5aa993b
JM
12041 catch exec
12042 catch exec if <cond>
c906108c 12043
4a64f543 12044 First, check if there's an if clause. */
c906108c
SS
12045 cond_string = ep_parse_optional_if_clause (&arg);
12046
12047 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 12048 error (_("Junk at end of arguments."));
c906108c 12049
b4d90040
PA
12050 c = XNEW (struct exec_catchpoint);
12051 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
12052 &catch_exec_breakpoint_ops);
12053 c->exec_pathname = NULL;
12054
3ea46bff 12055 install_breakpoint (0, &c->base, 1);
c906108c 12056}
c5aa993b 12057
9ac4176b 12058void
28010a5d
PA
12059init_ada_exception_breakpoint (struct breakpoint *b,
12060 struct gdbarch *gdbarch,
12061 struct symtab_and_line sal,
12062 char *addr_string,
c0a91b2b 12063 const struct breakpoint_ops *ops,
28010a5d 12064 int tempflag,
349774ef 12065 int enabled,
28010a5d 12066 int from_tty)
f7f9143b 12067{
f7f9143b
JB
12068 if (from_tty)
12069 {
5af949e3
UW
12070 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
12071 if (!loc_gdbarch)
12072 loc_gdbarch = gdbarch;
12073
6c95b8df
PA
12074 describe_other_breakpoints (loc_gdbarch,
12075 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
12076 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
12077 version for exception catchpoints, because two catchpoints
12078 used for different exception names will use the same address.
12079 In this case, a "breakpoint ... also set at..." warning is
4a64f543 12080 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 12081 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
12082 the user what type of catchpoint it is. The above is good
12083 enough for now, though. */
12084 }
12085
28010a5d 12086 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 12087
349774ef 12088 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 12089 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
12090 b->addr_string = addr_string;
12091 b->language = language_ada;
f7f9143b
JB
12092}
12093
a96d9b2e
SDJ
12094/* Splits the argument using space as delimiter. Returns an xmalloc'd
12095 filter list, or NULL if no filtering is required. */
12096static VEC(int) *
12097catch_syscall_split_args (char *arg)
12098{
12099 VEC(int) *result = NULL;
29d0bb3d 12100 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
12101
12102 while (*arg != '\0')
12103 {
12104 int i, syscall_number;
12105 char *endptr;
12106 char cur_name[128];
12107 struct syscall s;
12108
12109 /* Skip whitespace. */
529480d0 12110 arg = skip_spaces (arg);
a96d9b2e
SDJ
12111
12112 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
12113 cur_name[i] = arg[i];
12114 cur_name[i] = '\0';
12115 arg += i;
12116
12117 /* Check if the user provided a syscall name or a number. */
12118 syscall_number = (int) strtol (cur_name, &endptr, 0);
12119 if (*endptr == '\0')
bccd0dd2 12120 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
12121 else
12122 {
12123 /* We have a name. Let's check if it's valid and convert it
12124 to a number. */
12125 get_syscall_by_name (cur_name, &s);
12126
12127 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
12128 /* Here we have to issue an error instead of a warning,
12129 because GDB cannot do anything useful if there's no
12130 syscall number to be caught. */
a96d9b2e
SDJ
12131 error (_("Unknown syscall name '%s'."), cur_name);
12132 }
12133
12134 /* Ok, it's valid. */
12135 VEC_safe_push (int, result, s.number);
12136 }
12137
12138 discard_cleanups (cleanup);
12139 return result;
12140}
12141
12142/* Implement the "catch syscall" command. */
12143
12144static void
cc59ec59
MS
12145catch_syscall_command_1 (char *arg, int from_tty,
12146 struct cmd_list_element *command)
a96d9b2e
SDJ
12147{
12148 int tempflag;
12149 VEC(int) *filter;
12150 struct syscall s;
12151 struct gdbarch *gdbarch = get_current_arch ();
12152
12153 /* Checking if the feature if supported. */
12154 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
12155 error (_("The feature 'catch syscall' is not supported on \
ea666128 12156this architecture yet."));
a96d9b2e
SDJ
12157
12158 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12159
e9cafbcc 12160 arg = skip_spaces (arg);
a96d9b2e
SDJ
12161
12162 /* We need to do this first "dummy" translation in order
12163 to get the syscall XML file loaded or, most important,
12164 to display a warning to the user if there's no XML file
12165 for his/her architecture. */
12166 get_syscall_by_number (0, &s);
12167
12168 /* The allowed syntax is:
12169 catch syscall
12170 catch syscall <name | number> [<name | number> ... <name | number>]
12171
12172 Let's check if there's a syscall name. */
12173
12174 if (arg != NULL)
12175 filter = catch_syscall_split_args (arg);
12176 else
12177 filter = NULL;
12178
12179 create_syscall_event_catchpoint (tempflag, filter,
12180 &catch_syscall_breakpoint_ops);
12181}
12182
c906108c 12183static void
fba45db2 12184catch_command (char *arg, int from_tty)
c906108c 12185{
44feb3ce 12186 error (_("Catch requires an event name."));
c906108c
SS
12187}
12188\f
12189
12190static void
fba45db2 12191tcatch_command (char *arg, int from_tty)
c906108c 12192{
44feb3ce 12193 error (_("Catch requires an event name."));
c906108c
SS
12194}
12195
8a2c437b
TT
12196/* A qsort comparison function that sorts breakpoints in order. */
12197
12198static int
12199compare_breakpoints (const void *a, const void *b)
12200{
12201 const breakpoint_p *ba = a;
12202 uintptr_t ua = (uintptr_t) *ba;
12203 const breakpoint_p *bb = b;
12204 uintptr_t ub = (uintptr_t) *bb;
12205
12206 if ((*ba)->number < (*bb)->number)
12207 return -1;
12208 else if ((*ba)->number > (*bb)->number)
12209 return 1;
12210
12211 /* Now sort by address, in case we see, e..g, two breakpoints with
12212 the number 0. */
12213 if (ua < ub)
12214 return -1;
94b0e70d 12215 return ua > ub ? 1 : 0;
8a2c437b
TT
12216}
12217
80f8a6eb 12218/* Delete breakpoints by address or line. */
c906108c
SS
12219
12220static void
fba45db2 12221clear_command (char *arg, int from_tty)
c906108c 12222{
8a2c437b 12223 struct breakpoint *b, *prev;
d6e956e5
VP
12224 VEC(breakpoint_p) *found = 0;
12225 int ix;
c906108c
SS
12226 int default_match;
12227 struct symtabs_and_lines sals;
12228 struct symtab_and_line sal;
c906108c 12229 int i;
8a2c437b 12230 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12231
12232 if (arg)
12233 {
39cf75f7
DE
12234 sals = decode_line_with_current_source (arg,
12235 (DECODE_LINE_FUNFIRSTLINE
12236 | DECODE_LINE_LIST_MODE));
cf4ded82 12237 make_cleanup (xfree, sals.sals);
c906108c
SS
12238 default_match = 0;
12239 }
12240 else
12241 {
c5aa993b 12242 sals.sals = (struct symtab_and_line *)
c906108c 12243 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 12244 make_cleanup (xfree, sals.sals);
4a64f543 12245 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12246
12247 /* Set sal's line, symtab, pc, and pspace to the values
12248 corresponding to the last call to print_frame_info. If the
12249 codepoint is not valid, this will set all the fields to 0. */
12250 get_last_displayed_sal (&sal);
c906108c 12251 if (sal.symtab == 0)
8a3fe4f8 12252 error (_("No source file specified."));
c906108c
SS
12253
12254 sals.sals[0] = sal;
12255 sals.nelts = 1;
12256
12257 default_match = 1;
12258 }
12259
4a64f543
MS
12260 /* We don't call resolve_sal_pc here. That's not as bad as it
12261 seems, because all existing breakpoints typically have both
12262 file/line and pc set. So, if clear is given file/line, we can
12263 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12264
12265 We only support clearing given the address explicitly
12266 present in breakpoint table. Say, we've set breakpoint
4a64f543 12267 at file:line. There were several PC values for that file:line,
ed0616c6 12268 due to optimization, all in one block.
4a64f543
MS
12269
12270 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12271 PC corresponding to the same file:line, the breakpoint won't
12272 be cleared. We probably can still clear the breakpoint, but
12273 since the other PC value is never presented to user, user
12274 can only find it by guessing, and it does not seem important
12275 to support that. */
12276
4a64f543
MS
12277 /* For each line spec given, delete bps which correspond to it. Do
12278 it in two passes, solely to preserve the current behavior that
12279 from_tty is forced true if we delete more than one
12280 breakpoint. */
c906108c 12281
80f8a6eb 12282 found = NULL;
8a2c437b 12283 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12284 for (i = 0; i < sals.nelts; i++)
12285 {
05cba821
JK
12286 const char *sal_fullname;
12287
c906108c 12288 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12289 If line given (pc == 0), clear all bpts on specified line.
12290 If defaulting, clear all bpts on default line
c906108c 12291 or at default pc.
c5aa993b
JM
12292
12293 defaulting sal.pc != 0 tests to do
12294
12295 0 1 pc
12296 1 1 pc _and_ line
12297 0 0 line
12298 1 0 <can't happen> */
c906108c
SS
12299
12300 sal = sals.sals[i];
05cba821
JK
12301 sal_fullname = (sal.symtab == NULL
12302 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12303
4a64f543 12304 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12305 ALL_BREAKPOINTS (b)
c5aa993b 12306 {
0d381245 12307 int match = 0;
4a64f543 12308 /* Are we going to delete b? */
cc60f2e3 12309 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12310 {
12311 struct bp_location *loc = b->loc;
12312 for (; loc; loc = loc->next)
12313 {
f8eba3c6
TT
12314 /* If the user specified file:line, don't allow a PC
12315 match. This matches historical gdb behavior. */
12316 int pc_match = (!sal.explicit_line
12317 && sal.pc
12318 && (loc->pspace == sal.pspace)
12319 && (loc->address == sal.pc)
12320 && (!section_is_overlay (loc->section)
12321 || loc->section == sal.section));
4aac40c8
TT
12322 int line_match = 0;
12323
12324 if ((default_match || sal.explicit_line)
2f202fde 12325 && loc->symtab != NULL
05cba821 12326 && sal_fullname != NULL
4aac40c8 12327 && sal.pspace == loc->pspace
05cba821
JK
12328 && loc->line_number == sal.line
12329 && filename_cmp (symtab_to_fullname (loc->symtab),
12330 sal_fullname) == 0)
12331 line_match = 1;
4aac40c8 12332
0d381245
VP
12333 if (pc_match || line_match)
12334 {
12335 match = 1;
12336 break;
12337 }
12338 }
12339 }
12340
12341 if (match)
d6e956e5 12342 VEC_safe_push(breakpoint_p, found, b);
c906108c 12343 }
80f8a6eb 12344 }
8a2c437b 12345
80f8a6eb 12346 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12347 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12348 {
12349 if (arg)
8a3fe4f8 12350 error (_("No breakpoint at %s."), arg);
80f8a6eb 12351 else
8a3fe4f8 12352 error (_("No breakpoint at this line."));
80f8a6eb 12353 }
c906108c 12354
8a2c437b
TT
12355 /* Remove duplicates from the vec. */
12356 qsort (VEC_address (breakpoint_p, found),
12357 VEC_length (breakpoint_p, found),
12358 sizeof (breakpoint_p),
12359 compare_breakpoints);
12360 prev = VEC_index (breakpoint_p, found, 0);
12361 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12362 {
12363 if (b == prev)
12364 {
12365 VEC_ordered_remove (breakpoint_p, found, ix);
12366 --ix;
12367 }
12368 }
12369
d6e956e5 12370 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12371 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12372 if (from_tty)
a3f17187 12373 {
d6e956e5 12374 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12375 printf_unfiltered (_("Deleted breakpoint "));
12376 else
12377 printf_unfiltered (_("Deleted breakpoints "));
12378 }
d6e956e5
VP
12379
12380 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12381 {
c5aa993b 12382 if (from_tty)
d6e956e5
VP
12383 printf_unfiltered ("%d ", b->number);
12384 delete_breakpoint (b);
c906108c 12385 }
80f8a6eb
MS
12386 if (from_tty)
12387 putchar_unfiltered ('\n');
8a2c437b
TT
12388
12389 do_cleanups (cleanups);
c906108c
SS
12390}
12391\f
12392/* Delete breakpoint in BS if they are `delete' breakpoints and
12393 all breakpoints that are marked for deletion, whether hit or not.
12394 This is called after any breakpoint is hit, or after errors. */
12395
12396void
fba45db2 12397breakpoint_auto_delete (bpstat bs)
c906108c 12398{
35df4500 12399 struct breakpoint *b, *b_tmp;
c906108c
SS
12400
12401 for (; bs; bs = bs->next)
f431efe5
PA
12402 if (bs->breakpoint_at
12403 && bs->breakpoint_at->disposition == disp_del
c906108c 12404 && bs->stop)
f431efe5 12405 delete_breakpoint (bs->breakpoint_at);
c906108c 12406
35df4500 12407 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12408 {
b5de0fa7 12409 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12410 delete_breakpoint (b);
12411 }
c906108c
SS
12412}
12413
4a64f543
MS
12414/* A comparison function for bp_location AP and BP being interfaced to
12415 qsort. Sort elements primarily by their ADDRESS (no matter what
12416 does breakpoint_address_is_meaningful say for its OWNER),
12417 secondarily by ordering first bp_permanent OWNERed elements and
12418 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12419 qsort being an unstable algorithm. */
876fa593
JK
12420
12421static int
494cfb0f 12422bp_location_compare (const void *ap, const void *bp)
876fa593 12423{
494cfb0f
JK
12424 struct bp_location *a = *(void **) ap;
12425 struct bp_location *b = *(void **) bp;
2bdf28a0 12426 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
12427 int a_perm = a->owner->enable_state == bp_permanent;
12428 int b_perm = b->owner->enable_state == bp_permanent;
12429
12430 if (a->address != b->address)
12431 return (a->address > b->address) - (a->address < b->address);
12432
dea2aa5f
LM
12433 /* Sort locations at the same address by their pspace number, keeping
12434 locations of the same inferior (in a multi-inferior environment)
12435 grouped. */
12436
12437 if (a->pspace->num != b->pspace->num)
12438 return ((a->pspace->num > b->pspace->num)
12439 - (a->pspace->num < b->pspace->num));
12440
876fa593
JK
12441 /* Sort permanent breakpoints first. */
12442 if (a_perm != b_perm)
12443 return (a_perm < b_perm) - (a_perm > b_perm);
12444
c56a97f9
JK
12445 /* Make the internal GDB representation stable across GDB runs
12446 where A and B memory inside GDB can differ. Breakpoint locations of
12447 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12448
12449 if (a->owner->number != b->owner->number)
c56a97f9
JK
12450 return ((a->owner->number > b->owner->number)
12451 - (a->owner->number < b->owner->number));
876fa593
JK
12452
12453 return (a > b) - (a < b);
12454}
12455
876fa593 12456/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12457 bp_location_shadow_len_after_address_max according to the current
12458 content of the bp_location array. */
f7545552
TT
12459
12460static void
876fa593 12461bp_location_target_extensions_update (void)
f7545552 12462{
876fa593
JK
12463 struct bp_location *bl, **blp_tmp;
12464
12465 bp_location_placed_address_before_address_max = 0;
12466 bp_location_shadow_len_after_address_max = 0;
12467
12468 ALL_BP_LOCATIONS (bl, blp_tmp)
12469 {
12470 CORE_ADDR start, end, addr;
12471
12472 if (!bp_location_has_shadow (bl))
12473 continue;
12474
12475 start = bl->target_info.placed_address;
12476 end = start + bl->target_info.shadow_len;
12477
12478 gdb_assert (bl->address >= start);
12479 addr = bl->address - start;
12480 if (addr > bp_location_placed_address_before_address_max)
12481 bp_location_placed_address_before_address_max = addr;
12482
12483 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12484
12485 gdb_assert (bl->address < end);
12486 addr = end - bl->address;
12487 if (addr > bp_location_shadow_len_after_address_max)
12488 bp_location_shadow_len_after_address_max = addr;
12489 }
f7545552
TT
12490}
12491
1e4d1764
YQ
12492/* Download tracepoint locations if they haven't been. */
12493
12494static void
12495download_tracepoint_locations (void)
12496{
7ed2c994 12497 struct breakpoint *b;
1e4d1764
YQ
12498 struct cleanup *old_chain;
12499
12500 if (!target_can_download_tracepoint ())
12501 return;
12502
12503 old_chain = save_current_space_and_thread ();
12504
7ed2c994 12505 ALL_TRACEPOINTS (b)
1e4d1764 12506 {
7ed2c994 12507 struct bp_location *bl;
1e4d1764 12508 struct tracepoint *t;
f2a8bc8a 12509 int bp_location_downloaded = 0;
1e4d1764 12510
7ed2c994 12511 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12512 ? !may_insert_fast_tracepoints
12513 : !may_insert_tracepoints))
12514 continue;
12515
7ed2c994
YQ
12516 for (bl = b->loc; bl; bl = bl->next)
12517 {
12518 /* In tracepoint, locations are _never_ duplicated, so
12519 should_be_inserted is equivalent to
12520 unduplicated_should_be_inserted. */
12521 if (!should_be_inserted (bl) || bl->inserted)
12522 continue;
1e4d1764 12523
7ed2c994 12524 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12525
7ed2c994 12526 target_download_tracepoint (bl);
1e4d1764 12527
7ed2c994 12528 bl->inserted = 1;
f2a8bc8a 12529 bp_location_downloaded = 1;
7ed2c994
YQ
12530 }
12531 t = (struct tracepoint *) b;
12532 t->number_on_target = b->number;
f2a8bc8a
YQ
12533 if (bp_location_downloaded)
12534 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12535 }
12536
12537 do_cleanups (old_chain);
12538}
12539
934709f0
PW
12540/* Swap the insertion/duplication state between two locations. */
12541
12542static void
12543swap_insertion (struct bp_location *left, struct bp_location *right)
12544{
12545 const int left_inserted = left->inserted;
12546 const int left_duplicate = left->duplicate;
b775012e 12547 const int left_needs_update = left->needs_update;
934709f0
PW
12548 const struct bp_target_info left_target_info = left->target_info;
12549
1e4d1764
YQ
12550 /* Locations of tracepoints can never be duplicated. */
12551 if (is_tracepoint (left->owner))
12552 gdb_assert (!left->duplicate);
12553 if (is_tracepoint (right->owner))
12554 gdb_assert (!right->duplicate);
12555
934709f0
PW
12556 left->inserted = right->inserted;
12557 left->duplicate = right->duplicate;
b775012e 12558 left->needs_update = right->needs_update;
934709f0
PW
12559 left->target_info = right->target_info;
12560 right->inserted = left_inserted;
12561 right->duplicate = left_duplicate;
b775012e 12562 right->needs_update = left_needs_update;
934709f0
PW
12563 right->target_info = left_target_info;
12564}
12565
b775012e
LM
12566/* Force the re-insertion of the locations at ADDRESS. This is called
12567 once a new/deleted/modified duplicate location is found and we are evaluating
12568 conditions on the target's side. Such conditions need to be updated on
12569 the target. */
12570
12571static void
12572force_breakpoint_reinsertion (struct bp_location *bl)
12573{
12574 struct bp_location **locp = NULL, **loc2p;
12575 struct bp_location *loc;
12576 CORE_ADDR address = 0;
12577 int pspace_num;
12578
12579 address = bl->address;
12580 pspace_num = bl->pspace->num;
12581
12582 /* This is only meaningful if the target is
12583 evaluating conditions and if the user has
12584 opted for condition evaluation on the target's
12585 side. */
12586 if (gdb_evaluates_breakpoint_condition_p ()
12587 || !target_supports_evaluation_of_breakpoint_conditions ())
12588 return;
12589
12590 /* Flag all breakpoint locations with this address and
12591 the same program space as the location
12592 as "its condition has changed". We need to
12593 update the conditions on the target's side. */
12594 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12595 {
12596 loc = *loc2p;
12597
12598 if (!is_breakpoint (loc->owner)
12599 || pspace_num != loc->pspace->num)
12600 continue;
12601
12602 /* Flag the location appropriately. We use a different state to
12603 let everyone know that we already updated the set of locations
12604 with addr bl->address and program space bl->pspace. This is so
12605 we don't have to keep calling these functions just to mark locations
12606 that have already been marked. */
12607 loc->condition_changed = condition_updated;
12608
12609 /* Free the agent expression bytecode as well. We will compute
12610 it later on. */
12611 if (loc->cond_bytecode)
12612 {
12613 free_agent_expr (loc->cond_bytecode);
12614 loc->cond_bytecode = NULL;
12615 }
12616 }
12617}
44702360
PA
12618/* Called whether new breakpoints are created, or existing breakpoints
12619 deleted, to update the global location list and recompute which
12620 locations are duplicate of which.
b775012e 12621
04086b45
PA
12622 The INSERT_MODE flag determines whether locations may not, may, or
12623 shall be inserted now. See 'enum ugll_insert_mode' for more
12624 info. */
b60e7edf 12625
0d381245 12626static void
44702360 12627update_global_location_list (enum ugll_insert_mode insert_mode)
0d381245 12628{
74960c60 12629 struct breakpoint *b;
876fa593 12630 struct bp_location **locp, *loc;
f7545552 12631 struct cleanup *cleanups;
b775012e
LM
12632 /* Last breakpoint location address that was marked for update. */
12633 CORE_ADDR last_addr = 0;
12634 /* Last breakpoint location program space that was marked for update. */
12635 int last_pspace_num = -1;
f7545552 12636
2d134ed3
PA
12637 /* Used in the duplicates detection below. When iterating over all
12638 bp_locations, points to the first bp_location of a given address.
12639 Breakpoints and watchpoints of different types are never
12640 duplicates of each other. Keep one pointer for each type of
12641 breakpoint/watchpoint, so we only need to loop over all locations
12642 once. */
12643 struct bp_location *bp_loc_first; /* breakpoint */
12644 struct bp_location *wp_loc_first; /* hardware watchpoint */
12645 struct bp_location *awp_loc_first; /* access watchpoint */
12646 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12647
4a64f543
MS
12648 /* Saved former bp_location array which we compare against the newly
12649 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12650 struct bp_location **old_location, **old_locp;
12651 unsigned old_location_count;
12652
12653 old_location = bp_location;
12654 old_location_count = bp_location_count;
12655 bp_location = NULL;
12656 bp_location_count = 0;
12657 cleanups = make_cleanup (xfree, old_location);
0d381245 12658
74960c60 12659 ALL_BREAKPOINTS (b)
876fa593
JK
12660 for (loc = b->loc; loc; loc = loc->next)
12661 bp_location_count++;
12662
12663 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12664 locp = bp_location;
12665 ALL_BREAKPOINTS (b)
12666 for (loc = b->loc; loc; loc = loc->next)
12667 *locp++ = loc;
12668 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12669 bp_location_compare);
876fa593
JK
12670
12671 bp_location_target_extensions_update ();
74960c60 12672
4a64f543
MS
12673 /* Identify bp_location instances that are no longer present in the
12674 new list, and therefore should be freed. Note that it's not
12675 necessary that those locations should be removed from inferior --
12676 if there's another location at the same address (previously
12677 marked as duplicate), we don't need to remove/insert the
12678 location.
876fa593 12679
4a64f543
MS
12680 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12681 and former bp_location array state respectively. */
876fa593
JK
12682
12683 locp = bp_location;
12684 for (old_locp = old_location; old_locp < old_location + old_location_count;
12685 old_locp++)
74960c60 12686 {
876fa593 12687 struct bp_location *old_loc = *old_locp;
c7d46a38 12688 struct bp_location **loc2p;
876fa593 12689
e5dd4106 12690 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12691 not, we have to free it. */
c7d46a38 12692 int found_object = 0;
20874c92
VP
12693 /* Tells if the location should remain inserted in the target. */
12694 int keep_in_target = 0;
12695 int removed = 0;
876fa593 12696
4a64f543
MS
12697 /* Skip LOCP entries which will definitely never be needed.
12698 Stop either at or being the one matching OLD_LOC. */
876fa593 12699 while (locp < bp_location + bp_location_count
c7d46a38 12700 && (*locp)->address < old_loc->address)
876fa593 12701 locp++;
c7d46a38
PA
12702
12703 for (loc2p = locp;
12704 (loc2p < bp_location + bp_location_count
12705 && (*loc2p)->address == old_loc->address);
12706 loc2p++)
12707 {
b775012e
LM
12708 /* Check if this is a new/duplicated location or a duplicated
12709 location that had its condition modified. If so, we want to send
12710 its condition to the target if evaluation of conditions is taking
12711 place there. */
12712 if ((*loc2p)->condition_changed == condition_modified
12713 && (last_addr != old_loc->address
12714 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12715 {
b775012e
LM
12716 force_breakpoint_reinsertion (*loc2p);
12717 last_pspace_num = old_loc->pspace->num;
c7d46a38 12718 }
b775012e
LM
12719
12720 if (*loc2p == old_loc)
12721 found_object = 1;
c7d46a38 12722 }
74960c60 12723
b775012e
LM
12724 /* We have already handled this address, update it so that we don't
12725 have to go through updates again. */
12726 last_addr = old_loc->address;
12727
12728 /* Target-side condition evaluation: Handle deleted locations. */
12729 if (!found_object)
12730 force_breakpoint_reinsertion (old_loc);
12731
4a64f543
MS
12732 /* If this location is no longer present, and inserted, look if
12733 there's maybe a new location at the same address. If so,
12734 mark that one inserted, and don't remove this one. This is
12735 needed so that we don't have a time window where a breakpoint
12736 at certain location is not inserted. */
74960c60 12737
876fa593 12738 if (old_loc->inserted)
0d381245 12739 {
4a64f543
MS
12740 /* If the location is inserted now, we might have to remove
12741 it. */
74960c60 12742
876fa593 12743 if (found_object && should_be_inserted (old_loc))
74960c60 12744 {
4a64f543
MS
12745 /* The location is still present in the location list,
12746 and still should be inserted. Don't do anything. */
20874c92 12747 keep_in_target = 1;
74960c60
VP
12748 }
12749 else
12750 {
b775012e
LM
12751 /* This location still exists, but it won't be kept in the
12752 target since it may have been disabled. We proceed to
12753 remove its target-side condition. */
12754
4a64f543
MS
12755 /* The location is either no longer present, or got
12756 disabled. See if there's another location at the
12757 same address, in which case we don't need to remove
12758 this one from the target. */
876fa593 12759
2bdf28a0 12760 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12761 if (breakpoint_address_is_meaningful (old_loc->owner))
12762 {
876fa593 12763 for (loc2p = locp;
c7d46a38
PA
12764 (loc2p < bp_location + bp_location_count
12765 && (*loc2p)->address == old_loc->address);
876fa593
JK
12766 loc2p++)
12767 {
12768 struct bp_location *loc2 = *loc2p;
12769
2d134ed3 12770 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12771 {
85d721b8
PA
12772 /* Read watchpoint locations are switched to
12773 access watchpoints, if the former are not
12774 supported, but the latter are. */
12775 if (is_hardware_watchpoint (old_loc->owner))
12776 {
12777 gdb_assert (is_hardware_watchpoint (loc2->owner));
12778 loc2->watchpoint_type = old_loc->watchpoint_type;
12779 }
12780
934709f0
PW
12781 /* loc2 is a duplicated location. We need to check
12782 if it should be inserted in case it will be
12783 unduplicated. */
12784 if (loc2 != old_loc
12785 && unduplicated_should_be_inserted (loc2))
c7d46a38 12786 {
934709f0 12787 swap_insertion (old_loc, loc2);
c7d46a38
PA
12788 keep_in_target = 1;
12789 break;
12790 }
876fa593
JK
12791 }
12792 }
12793 }
74960c60
VP
12794 }
12795
20874c92
VP
12796 if (!keep_in_target)
12797 {
876fa593 12798 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12799 {
4a64f543
MS
12800 /* This is just about all we can do. We could keep
12801 this location on the global list, and try to
12802 remove it next time, but there's no particular
12803 reason why we will succeed next time.
20874c92 12804
4a64f543
MS
12805 Note that at this point, old_loc->owner is still
12806 valid, as delete_breakpoint frees the breakpoint
12807 only after calling us. */
3e43a32a
MS
12808 printf_filtered (_("warning: Error removing "
12809 "breakpoint %d\n"),
876fa593 12810 old_loc->owner->number);
20874c92
VP
12811 }
12812 removed = 1;
12813 }
0d381245 12814 }
74960c60
VP
12815
12816 if (!found_object)
1c5cfe86 12817 {
db82e815
PA
12818 if (removed && non_stop
12819 && breakpoint_address_is_meaningful (old_loc->owner)
12820 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12821 {
db82e815
PA
12822 /* This location was removed from the target. In
12823 non-stop mode, a race condition is possible where
12824 we've removed a breakpoint, but stop events for that
12825 breakpoint are already queued and will arrive later.
12826 We apply an heuristic to be able to distinguish such
12827 SIGTRAPs from other random SIGTRAPs: we keep this
12828 breakpoint location for a bit, and will retire it
12829 after we see some number of events. The theory here
12830 is that reporting of events should, "on the average",
12831 be fair, so after a while we'll see events from all
12832 threads that have anything of interest, and no longer
12833 need to keep this breakpoint location around. We
12834 don't hold locations forever so to reduce chances of
12835 mistaking a non-breakpoint SIGTRAP for a breakpoint
12836 SIGTRAP.
12837
12838 The heuristic failing can be disastrous on
12839 decr_pc_after_break targets.
12840
12841 On decr_pc_after_break targets, like e.g., x86-linux,
12842 if we fail to recognize a late breakpoint SIGTRAP,
12843 because events_till_retirement has reached 0 too
12844 soon, we'll fail to do the PC adjustment, and report
12845 a random SIGTRAP to the user. When the user resumes
12846 the inferior, it will most likely immediately crash
2dec564e 12847 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12848 corrupted, because of being resumed e.g., in the
12849 middle of a multi-byte instruction, or skipped a
12850 one-byte instruction. This was actually seen happen
12851 on native x86-linux, and should be less rare on
12852 targets that do not support new thread events, like
12853 remote, due to the heuristic depending on
12854 thread_count.
12855
12856 Mistaking a random SIGTRAP for a breakpoint trap
12857 causes similar symptoms (PC adjustment applied when
12858 it shouldn't), but then again, playing with SIGTRAPs
12859 behind the debugger's back is asking for trouble.
12860
12861 Since hardware watchpoint traps are always
12862 distinguishable from other traps, so we don't need to
12863 apply keep hardware watchpoint moribund locations
12864 around. We simply always ignore hardware watchpoint
12865 traps we can no longer explain. */
12866
876fa593
JK
12867 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12868 old_loc->owner = NULL;
20874c92 12869
876fa593 12870 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12871 }
12872 else
f431efe5
PA
12873 {
12874 old_loc->owner = NULL;
12875 decref_bp_location (&old_loc);
12876 }
20874c92 12877 }
74960c60 12878 }
1c5cfe86 12879
348d480f
PA
12880 /* Rescan breakpoints at the same address and section, marking the
12881 first one as "first" and any others as "duplicates". This is so
12882 that the bpt instruction is only inserted once. If we have a
12883 permanent breakpoint at the same place as BPT, make that one the
12884 official one, and the rest as duplicates. Permanent breakpoints
12885 are sorted first for the same address.
12886
12887 Do the same for hardware watchpoints, but also considering the
12888 watchpoint's type (regular/access/read) and length. */
12889
12890 bp_loc_first = NULL;
12891 wp_loc_first = NULL;
12892 awp_loc_first = NULL;
12893 rwp_loc_first = NULL;
12894 ALL_BP_LOCATIONS (loc, locp)
12895 {
12896 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12897 non-NULL. */
348d480f 12898 struct bp_location **loc_first_p;
d3fbdd86 12899 b = loc->owner;
348d480f 12900
6f380991 12901 if (!unduplicated_should_be_inserted (loc)
348d480f 12902 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12903 /* Don't detect duplicate for tracepoint locations because they are
12904 never duplicated. See the comments in field `duplicate' of
12905 `struct bp_location'. */
348d480f 12906 || is_tracepoint (b))
b775012e
LM
12907 {
12908 /* Clear the condition modification flag. */
12909 loc->condition_changed = condition_unchanged;
12910 continue;
12911 }
348d480f
PA
12912
12913 /* Permanent breakpoint should always be inserted. */
12914 if (b->enable_state == bp_permanent && ! loc->inserted)
12915 internal_error (__FILE__, __LINE__,
12916 _("allegedly permanent breakpoint is not "
12917 "actually inserted"));
12918
12919 if (b->type == bp_hardware_watchpoint)
12920 loc_first_p = &wp_loc_first;
12921 else if (b->type == bp_read_watchpoint)
12922 loc_first_p = &rwp_loc_first;
12923 else if (b->type == bp_access_watchpoint)
12924 loc_first_p = &awp_loc_first;
12925 else
12926 loc_first_p = &bp_loc_first;
12927
12928 if (*loc_first_p == NULL
12929 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12930 || !breakpoint_locations_match (loc, *loc_first_p))
12931 {
12932 *loc_first_p = loc;
12933 loc->duplicate = 0;
b775012e
LM
12934
12935 if (is_breakpoint (loc->owner) && loc->condition_changed)
12936 {
12937 loc->needs_update = 1;
12938 /* Clear the condition modification flag. */
12939 loc->condition_changed = condition_unchanged;
12940 }
348d480f
PA
12941 continue;
12942 }
12943
934709f0
PW
12944
12945 /* This and the above ensure the invariant that the first location
12946 is not duplicated, and is the inserted one.
12947 All following are marked as duplicated, and are not inserted. */
12948 if (loc->inserted)
12949 swap_insertion (loc, *loc_first_p);
348d480f
PA
12950 loc->duplicate = 1;
12951
b775012e
LM
12952 /* Clear the condition modification flag. */
12953 loc->condition_changed = condition_unchanged;
12954
348d480f
PA
12955 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12956 && b->enable_state != bp_permanent)
12957 internal_error (__FILE__, __LINE__,
12958 _("another breakpoint was inserted on top of "
12959 "a permanent breakpoint"));
12960 }
12961
a25a5a45 12962 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
b775012e 12963 {
04086b45 12964 if (insert_mode != UGLL_DONT_INSERT)
b775012e
LM
12965 insert_breakpoint_locations ();
12966 else
12967 {
44702360
PA
12968 /* Even though the caller told us to not insert new
12969 locations, we may still need to update conditions on the
12970 target's side of breakpoints that were already inserted
12971 if the target is evaluating breakpoint conditions. We
b775012e
LM
12972 only update conditions for locations that are marked
12973 "needs_update". */
12974 update_inserted_breakpoint_locations ();
12975 }
12976 }
348d480f 12977
04086b45 12978 if (insert_mode != UGLL_DONT_INSERT)
1e4d1764
YQ
12979 download_tracepoint_locations ();
12980
348d480f
PA
12981 do_cleanups (cleanups);
12982}
12983
12984void
12985breakpoint_retire_moribund (void)
12986{
12987 struct bp_location *loc;
12988 int ix;
12989
12990 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12991 if (--(loc->events_till_retirement) == 0)
12992 {
12993 decref_bp_location (&loc);
12994 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12995 --ix;
12996 }
12997}
12998
12999static void
44702360 13000update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
348d480f 13001{
bfd189b1 13002 volatile struct gdb_exception e;
348d480f
PA
13003
13004 TRY_CATCH (e, RETURN_MASK_ERROR)
44702360 13005 update_global_location_list (insert_mode);
348d480f
PA
13006}
13007
13008/* Clear BKP from a BPS. */
13009
13010static void
13011bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
13012{
13013 bpstat bs;
13014
13015 for (bs = bps; bs; bs = bs->next)
13016 if (bs->breakpoint_at == bpt)
13017 {
13018 bs->breakpoint_at = NULL;
13019 bs->old_val = NULL;
13020 /* bs->commands will be freed later. */
13021 }
13022}
13023
13024/* Callback for iterate_over_threads. */
13025static int
13026bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
13027{
13028 struct breakpoint *bpt = data;
13029
13030 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
13031 return 0;
13032}
13033
13034/* Helper for breakpoint and tracepoint breakpoint_ops->mention
13035 callbacks. */
13036
13037static void
13038say_where (struct breakpoint *b)
13039{
13040 struct value_print_options opts;
13041
13042 get_user_print_options (&opts);
13043
13044 /* i18n: cagney/2005-02-11: Below needs to be merged into a
13045 single string. */
13046 if (b->loc == NULL)
13047 {
13048 printf_filtered (_(" (%s) pending."), b->addr_string);
13049 }
13050 else
13051 {
2f202fde 13052 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
13053 {
13054 printf_filtered (" at ");
13055 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
13056 gdb_stdout);
13057 }
2f202fde 13058 if (b->loc->symtab != NULL)
f8eba3c6
TT
13059 {
13060 /* If there is a single location, we can print the location
13061 more nicely. */
13062 if (b->loc->next == NULL)
13063 printf_filtered (": file %s, line %d.",
05cba821
JK
13064 symtab_to_filename_for_display (b->loc->symtab),
13065 b->loc->line_number);
f8eba3c6
TT
13066 else
13067 /* This is not ideal, but each location may have a
13068 different file name, and this at least reflects the
13069 real situation somewhat. */
13070 printf_filtered (": %s.", b->addr_string);
13071 }
348d480f
PA
13072
13073 if (b->loc->next)
13074 {
13075 struct bp_location *loc = b->loc;
13076 int n = 0;
13077 for (; loc; loc = loc->next)
13078 ++n;
13079 printf_filtered (" (%d locations)", n);
13080 }
13081 }
13082}
13083
348d480f
PA
13084/* Default bp_location_ops methods. */
13085
13086static void
13087bp_location_dtor (struct bp_location *self)
13088{
13089 xfree (self->cond);
b775012e
LM
13090 if (self->cond_bytecode)
13091 free_agent_expr (self->cond_bytecode);
348d480f 13092 xfree (self->function_name);
8b4f3082
PA
13093
13094 VEC_free (agent_expr_p, self->target_info.conditions);
13095 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
13096}
13097
13098static const struct bp_location_ops bp_location_ops =
13099{
13100 bp_location_dtor
13101};
13102
2060206e
PA
13103/* Default breakpoint_ops methods all breakpoint_ops ultimately
13104 inherit from. */
348d480f 13105
2060206e
PA
13106static void
13107base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
13108{
13109 decref_counted_command_line (&self->commands);
13110 xfree (self->cond_string);
fb81d016 13111 xfree (self->extra_string);
348d480f 13112 xfree (self->addr_string);
f8eba3c6 13113 xfree (self->filter);
348d480f 13114 xfree (self->addr_string_range_end);
348d480f
PA
13115}
13116
2060206e
PA
13117static struct bp_location *
13118base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
13119{
13120 struct bp_location *loc;
13121
13122 loc = XNEW (struct bp_location);
13123 init_bp_location (loc, &bp_location_ops, self);
13124 return loc;
13125}
13126
2060206e
PA
13127static void
13128base_breakpoint_re_set (struct breakpoint *b)
13129{
13130 /* Nothing to re-set. */
13131}
13132
13133#define internal_error_pure_virtual_called() \
13134 gdb_assert_not_reached ("pure virtual function called")
13135
13136static int
13137base_breakpoint_insert_location (struct bp_location *bl)
13138{
13139 internal_error_pure_virtual_called ();
13140}
13141
13142static int
13143base_breakpoint_remove_location (struct bp_location *bl)
13144{
13145 internal_error_pure_virtual_called ();
13146}
13147
13148static int
13149base_breakpoint_breakpoint_hit (const struct bp_location *bl,
13150 struct address_space *aspace,
09ac7c10
TT
13151 CORE_ADDR bp_addr,
13152 const struct target_waitstatus *ws)
2060206e
PA
13153{
13154 internal_error_pure_virtual_called ();
13155}
13156
13157static void
13158base_breakpoint_check_status (bpstat bs)
13159{
13160 /* Always stop. */
13161}
13162
13163/* A "works_in_software_mode" breakpoint_ops method that just internal
13164 errors. */
13165
13166static int
13167base_breakpoint_works_in_software_mode (const struct breakpoint *b)
13168{
13169 internal_error_pure_virtual_called ();
13170}
13171
13172/* A "resources_needed" breakpoint_ops method that just internal
13173 errors. */
13174
13175static int
13176base_breakpoint_resources_needed (const struct bp_location *bl)
13177{
13178 internal_error_pure_virtual_called ();
13179}
13180
13181static enum print_stop_action
13182base_breakpoint_print_it (bpstat bs)
13183{
13184 internal_error_pure_virtual_called ();
13185}
13186
13187static void
13188base_breakpoint_print_one_detail (const struct breakpoint *self,
13189 struct ui_out *uiout)
13190{
13191 /* nothing */
13192}
13193
13194static void
13195base_breakpoint_print_mention (struct breakpoint *b)
13196{
13197 internal_error_pure_virtual_called ();
13198}
13199
13200static void
13201base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13202{
13203 internal_error_pure_virtual_called ();
13204}
13205
983af33b
SDJ
13206static void
13207base_breakpoint_create_sals_from_address (char **arg,
13208 struct linespec_result *canonical,
13209 enum bptype type_wanted,
13210 char *addr_start,
13211 char **copy_arg)
13212{
13213 internal_error_pure_virtual_called ();
13214}
13215
13216static void
13217base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13218 struct linespec_result *c,
983af33b 13219 char *cond_string,
e7e0cddf 13220 char *extra_string,
983af33b
SDJ
13221 enum bptype type_wanted,
13222 enum bpdisp disposition,
13223 int thread,
13224 int task, int ignore_count,
13225 const struct breakpoint_ops *o,
13226 int from_tty, int enabled,
44f238bb 13227 int internal, unsigned flags)
983af33b
SDJ
13228{
13229 internal_error_pure_virtual_called ();
13230}
13231
13232static void
13233base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13234 struct symtabs_and_lines *sals)
13235{
13236 internal_error_pure_virtual_called ();
13237}
13238
ab04a2af
TT
13239/* The default 'explains_signal' method. */
13240
47591c29 13241static int
427cd150 13242base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13243{
47591c29 13244 return 1;
ab04a2af
TT
13245}
13246
9d6e6e84
HZ
13247/* The default "after_condition_true" method. */
13248
13249static void
13250base_breakpoint_after_condition_true (struct bpstats *bs)
13251{
13252 /* Nothing to do. */
13253}
13254
ab04a2af 13255struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13256{
13257 base_breakpoint_dtor,
13258 base_breakpoint_allocate_location,
13259 base_breakpoint_re_set,
13260 base_breakpoint_insert_location,
13261 base_breakpoint_remove_location,
13262 base_breakpoint_breakpoint_hit,
13263 base_breakpoint_check_status,
13264 base_breakpoint_resources_needed,
13265 base_breakpoint_works_in_software_mode,
13266 base_breakpoint_print_it,
13267 NULL,
13268 base_breakpoint_print_one_detail,
13269 base_breakpoint_print_mention,
983af33b
SDJ
13270 base_breakpoint_print_recreate,
13271 base_breakpoint_create_sals_from_address,
13272 base_breakpoint_create_breakpoints_sal,
13273 base_breakpoint_decode_linespec,
9d6e6e84
HZ
13274 base_breakpoint_explains_signal,
13275 base_breakpoint_after_condition_true,
2060206e
PA
13276};
13277
13278/* Default breakpoint_ops methods. */
13279
13280static void
348d480f
PA
13281bkpt_re_set (struct breakpoint *b)
13282{
06edf0c0
PA
13283 /* FIXME: is this still reachable? */
13284 if (b->addr_string == NULL)
13285 {
13286 /* Anything without a string can't be re-set. */
348d480f 13287 delete_breakpoint (b);
06edf0c0 13288 return;
348d480f 13289 }
06edf0c0
PA
13290
13291 breakpoint_re_set_default (b);
348d480f
PA
13292}
13293
ef370185
JB
13294/* Copy SRC's shadow buffer and whatever else we'd set if we actually
13295 inserted DEST, so we can remove it later, in case SRC is removed
13296 first. */
13297
13298static void
13299bp_target_info_copy_insertion_state (struct bp_target_info *dest,
13300 const struct bp_target_info *src)
13301{
13302 dest->shadow_len = src->shadow_len;
13303 memcpy (dest->shadow_contents, src->shadow_contents, src->shadow_len);
0d5ed153 13304 dest->placed_address = src->placed_address;
ef370185
JB
13305 dest->placed_size = src->placed_size;
13306}
13307
2060206e 13308static int
348d480f
PA
13309bkpt_insert_location (struct bp_location *bl)
13310{
13311 if (bl->loc_type == bp_loc_hardware_breakpoint)
13312 return target_insert_hw_breakpoint (bl->gdbarch,
13313 &bl->target_info);
13314 else
ef370185
JB
13315 {
13316 struct bp_target_info *bp_tgt = &bl->target_info;
13317 int ret;
13318 int sss_slot;
13319
13320 /* There is no need to insert a breakpoint if an unconditional
13321 raw/sss breakpoint is already inserted at that location. */
13322 sss_slot = find_single_step_breakpoint (bp_tgt->placed_address_space,
0d5ed153 13323 bp_tgt->reqstd_address);
ef370185
JB
13324 if (sss_slot >= 0)
13325 {
13326 struct bp_target_info *sss_bp_tgt = single_step_breakpoints[sss_slot];
13327
13328 bp_target_info_copy_insertion_state (bp_tgt, sss_bp_tgt);
13329 return 0;
13330 }
13331
13332 return target_insert_breakpoint (bl->gdbarch, bp_tgt);
13333 }
348d480f
PA
13334}
13335
2060206e 13336static int
348d480f
PA
13337bkpt_remove_location (struct bp_location *bl)
13338{
13339 if (bl->loc_type == bp_loc_hardware_breakpoint)
13340 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13341 else
ef370185
JB
13342 {
13343 struct bp_target_info *bp_tgt = &bl->target_info;
13344 struct address_space *aspace = bp_tgt->placed_address_space;
0d5ed153 13345 CORE_ADDR address = bp_tgt->reqstd_address;
ef370185
JB
13346
13347 /* Only remove the breakpoint if there is no raw/sss breakpoint
13348 still inserted at this location. Otherwise, we would be
13349 effectively disabling the raw/sss breakpoint. */
13350 if (single_step_breakpoint_inserted_here_p (aspace, address))
13351 return 0;
13352
13353 return target_remove_breakpoint (bl->gdbarch, bp_tgt);
13354 }
348d480f
PA
13355}
13356
2060206e 13357static int
348d480f 13358bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13359 struct address_space *aspace, CORE_ADDR bp_addr,
13360 const struct target_waitstatus *ws)
348d480f 13361{
09ac7c10 13362 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13363 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13364 return 0;
13365
348d480f
PA
13366 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13367 aspace, bp_addr))
13368 return 0;
13369
13370 if (overlay_debugging /* unmapped overlay section */
13371 && section_is_overlay (bl->section)
13372 && !section_is_mapped (bl->section))
13373 return 0;
13374
13375 return 1;
13376}
13377
cd1608cc
PA
13378static int
13379dprintf_breakpoint_hit (const struct bp_location *bl,
13380 struct address_space *aspace, CORE_ADDR bp_addr,
13381 const struct target_waitstatus *ws)
13382{
13383 if (dprintf_style == dprintf_style_agent
13384 && target_can_run_breakpoint_commands ())
13385 {
13386 /* An agent-style dprintf never causes a stop. If we see a trap
13387 for this address it must be for a breakpoint that happens to
13388 be set at the same address. */
13389 return 0;
13390 }
13391
13392 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13393}
13394
2060206e 13395static int
348d480f
PA
13396bkpt_resources_needed (const struct bp_location *bl)
13397{
13398 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13399
13400 return 1;
13401}
13402
2060206e 13403static enum print_stop_action
348d480f
PA
13404bkpt_print_it (bpstat bs)
13405{
348d480f
PA
13406 struct breakpoint *b;
13407 const struct bp_location *bl;
001c8c33 13408 int bp_temp;
79a45e25 13409 struct ui_out *uiout = current_uiout;
348d480f
PA
13410
13411 gdb_assert (bs->bp_location_at != NULL);
13412
13413 bl = bs->bp_location_at;
13414 b = bs->breakpoint_at;
13415
001c8c33
PA
13416 bp_temp = b->disposition == disp_del;
13417 if (bl->address != bl->requested_address)
13418 breakpoint_adjustment_warning (bl->requested_address,
13419 bl->address,
13420 b->number, 1);
13421 annotate_breakpoint (b->number);
13422 if (bp_temp)
13423 ui_out_text (uiout, "\nTemporary breakpoint ");
13424 else
13425 ui_out_text (uiout, "\nBreakpoint ");
13426 if (ui_out_is_mi_like_p (uiout))
348d480f 13427 {
001c8c33
PA
13428 ui_out_field_string (uiout, "reason",
13429 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13430 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13431 }
001c8c33
PA
13432 ui_out_field_int (uiout, "bkptno", b->number);
13433 ui_out_text (uiout, ", ");
06edf0c0 13434
001c8c33 13435 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13436}
13437
2060206e 13438static void
06edf0c0
PA
13439bkpt_print_mention (struct breakpoint *b)
13440{
79a45e25 13441 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13442 return;
13443
13444 switch (b->type)
13445 {
13446 case bp_breakpoint:
13447 case bp_gnu_ifunc_resolver:
13448 if (b->disposition == disp_del)
13449 printf_filtered (_("Temporary breakpoint"));
13450 else
13451 printf_filtered (_("Breakpoint"));
13452 printf_filtered (_(" %d"), b->number);
13453 if (b->type == bp_gnu_ifunc_resolver)
13454 printf_filtered (_(" at gnu-indirect-function resolver"));
13455 break;
13456 case bp_hardware_breakpoint:
13457 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13458 break;
e7e0cddf
SS
13459 case bp_dprintf:
13460 printf_filtered (_("Dprintf %d"), b->number);
13461 break;
06edf0c0
PA
13462 }
13463
13464 say_where (b);
13465}
13466
2060206e 13467static void
06edf0c0
PA
13468bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13469{
13470 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13471 fprintf_unfiltered (fp, "tbreak");
13472 else if (tp->type == bp_breakpoint)
13473 fprintf_unfiltered (fp, "break");
13474 else if (tp->type == bp_hardware_breakpoint
13475 && tp->disposition == disp_del)
13476 fprintf_unfiltered (fp, "thbreak");
13477 else if (tp->type == bp_hardware_breakpoint)
13478 fprintf_unfiltered (fp, "hbreak");
13479 else
13480 internal_error (__FILE__, __LINE__,
13481 _("unhandled breakpoint type %d"), (int) tp->type);
13482
2060206e 13483 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 13484 print_recreate_thread (tp, fp);
06edf0c0
PA
13485}
13486
983af33b
SDJ
13487static void
13488bkpt_create_sals_from_address (char **arg,
13489 struct linespec_result *canonical,
13490 enum bptype type_wanted,
13491 char *addr_start, char **copy_arg)
13492{
13493 create_sals_from_address_default (arg, canonical, type_wanted,
13494 addr_start, copy_arg);
13495}
13496
13497static void
13498bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13499 struct linespec_result *canonical,
983af33b 13500 char *cond_string,
e7e0cddf 13501 char *extra_string,
983af33b
SDJ
13502 enum bptype type_wanted,
13503 enum bpdisp disposition,
13504 int thread,
13505 int task, int ignore_count,
13506 const struct breakpoint_ops *ops,
13507 int from_tty, int enabled,
44f238bb 13508 int internal, unsigned flags)
983af33b 13509{
023fa29b 13510 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13511 cond_string, extra_string,
13512 type_wanted,
983af33b
SDJ
13513 disposition, thread, task,
13514 ignore_count, ops, from_tty,
44f238bb 13515 enabled, internal, flags);
983af33b
SDJ
13516}
13517
13518static void
13519bkpt_decode_linespec (struct breakpoint *b, char **s,
13520 struct symtabs_and_lines *sals)
13521{
13522 decode_linespec_default (b, s, sals);
13523}
13524
06edf0c0
PA
13525/* Virtual table for internal breakpoints. */
13526
13527static void
13528internal_bkpt_re_set (struct breakpoint *b)
13529{
13530 switch (b->type)
13531 {
13532 /* Delete overlay event and longjmp master breakpoints; they
13533 will be reset later by breakpoint_re_set. */
13534 case bp_overlay_event:
13535 case bp_longjmp_master:
13536 case bp_std_terminate_master:
13537 case bp_exception_master:
13538 delete_breakpoint (b);
13539 break;
13540
13541 /* This breakpoint is special, it's set up when the inferior
13542 starts and we really don't want to touch it. */
13543 case bp_shlib_event:
13544
13545 /* Like bp_shlib_event, this breakpoint type is special. Once
13546 it is set up, we do not want to touch it. */
13547 case bp_thread_event:
13548 break;
13549 }
13550}
13551
13552static void
13553internal_bkpt_check_status (bpstat bs)
13554{
a9b3a50f
PA
13555 if (bs->breakpoint_at->type == bp_shlib_event)
13556 {
13557 /* If requested, stop when the dynamic linker notifies GDB of
13558 events. This allows the user to get control and place
13559 breakpoints in initializer routines for dynamically loaded
13560 objects (among other things). */
13561 bs->stop = stop_on_solib_events;
13562 bs->print = stop_on_solib_events;
13563 }
13564 else
13565 bs->stop = 0;
06edf0c0
PA
13566}
13567
13568static enum print_stop_action
13569internal_bkpt_print_it (bpstat bs)
13570{
06edf0c0 13571 struct breakpoint *b;
06edf0c0 13572
06edf0c0
PA
13573 b = bs->breakpoint_at;
13574
06edf0c0
PA
13575 switch (b->type)
13576 {
348d480f
PA
13577 case bp_shlib_event:
13578 /* Did we stop because the user set the stop_on_solib_events
13579 variable? (If so, we report this as a generic, "Stopped due
13580 to shlib event" message.) */
edcc5120 13581 print_solib_event (0);
348d480f
PA
13582 break;
13583
13584 case bp_thread_event:
13585 /* Not sure how we will get here.
13586 GDB should not stop for these breakpoints. */
13587 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13588 break;
13589
13590 case bp_overlay_event:
13591 /* By analogy with the thread event, GDB should not stop for these. */
13592 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13593 break;
13594
13595 case bp_longjmp_master:
13596 /* These should never be enabled. */
13597 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13598 break;
13599
13600 case bp_std_terminate_master:
13601 /* These should never be enabled. */
13602 printf_filtered (_("std::terminate Master Breakpoint: "
13603 "gdb should not stop!\n"));
348d480f
PA
13604 break;
13605
13606 case bp_exception_master:
13607 /* These should never be enabled. */
13608 printf_filtered (_("Exception Master Breakpoint: "
13609 "gdb should not stop!\n"));
06edf0c0
PA
13610 break;
13611 }
13612
001c8c33 13613 return PRINT_NOTHING;
06edf0c0
PA
13614}
13615
13616static void
13617internal_bkpt_print_mention (struct breakpoint *b)
13618{
13619 /* Nothing to mention. These breakpoints are internal. */
13620}
13621
06edf0c0
PA
13622/* Virtual table for momentary breakpoints */
13623
13624static void
13625momentary_bkpt_re_set (struct breakpoint *b)
13626{
13627 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13628 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13629 Otherwise these should have been blown away via the cleanup chain
13630 or by breakpoint_init_inferior when we rerun the executable. */
13631}
13632
13633static void
13634momentary_bkpt_check_status (bpstat bs)
13635{
13636 /* Nothing. The point of these breakpoints is causing a stop. */
13637}
13638
13639static enum print_stop_action
13640momentary_bkpt_print_it (bpstat bs)
13641{
79a45e25
PA
13642 struct ui_out *uiout = current_uiout;
13643
001c8c33 13644 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13645 {
001c8c33 13646 struct breakpoint *b = bs->breakpoint_at;
348d480f 13647
001c8c33
PA
13648 switch (b->type)
13649 {
13650 case bp_finish:
13651 ui_out_field_string
13652 (uiout, "reason",
13653 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13654 break;
348d480f 13655
001c8c33
PA
13656 case bp_until:
13657 ui_out_field_string
13658 (uiout, "reason",
13659 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13660 break;
13661 }
348d480f
PA
13662 }
13663
001c8c33 13664 return PRINT_UNKNOWN;
348d480f
PA
13665}
13666
06edf0c0
PA
13667static void
13668momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13669{
06edf0c0 13670 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13671}
13672
e2e4d78b
JK
13673/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13674
13675 It gets cleared already on the removal of the first one of such placed
13676 breakpoints. This is OK as they get all removed altogether. */
13677
13678static void
13679longjmp_bkpt_dtor (struct breakpoint *self)
13680{
13681 struct thread_info *tp = find_thread_id (self->thread);
13682
13683 if (tp)
13684 tp->initiating_frame = null_frame_id;
13685
13686 momentary_breakpoint_ops.dtor (self);
13687}
13688
55aa24fb
SDJ
13689/* Specific methods for probe breakpoints. */
13690
13691static int
13692bkpt_probe_insert_location (struct bp_location *bl)
13693{
13694 int v = bkpt_insert_location (bl);
13695
13696 if (v == 0)
13697 {
13698 /* The insertion was successful, now let's set the probe's semaphore
13699 if needed. */
729662a5
TT
13700 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13701 bl->probe.objfile,
13702 bl->gdbarch);
55aa24fb
SDJ
13703 }
13704
13705 return v;
13706}
13707
13708static int
13709bkpt_probe_remove_location (struct bp_location *bl)
13710{
13711 /* Let's clear the semaphore before removing the location. */
729662a5
TT
13712 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13713 bl->probe.objfile,
13714 bl->gdbarch);
55aa24fb
SDJ
13715
13716 return bkpt_remove_location (bl);
13717}
13718
13719static void
13720bkpt_probe_create_sals_from_address (char **arg,
13721 struct linespec_result *canonical,
13722 enum bptype type_wanted,
13723 char *addr_start, char **copy_arg)
13724{
13725 struct linespec_sals lsal;
13726
13727 lsal.sals = parse_probes (arg, canonical);
13728
13729 *copy_arg = xstrdup (canonical->addr_string);
13730 lsal.canonical = xstrdup (*copy_arg);
13731
13732 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13733}
13734
13735static void
13736bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13737 struct symtabs_and_lines *sals)
13738{
13739 *sals = parse_probes (s, NULL);
13740 if (!sals->sals)
13741 error (_("probe not found"));
13742}
13743
348d480f 13744/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13745
348d480f
PA
13746static void
13747tracepoint_re_set (struct breakpoint *b)
13748{
13749 breakpoint_re_set_default (b);
13750}
876fa593 13751
348d480f
PA
13752static int
13753tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13754 struct address_space *aspace, CORE_ADDR bp_addr,
13755 const struct target_waitstatus *ws)
348d480f
PA
13756{
13757 /* By definition, the inferior does not report stops at
13758 tracepoints. */
13759 return 0;
74960c60
VP
13760}
13761
13762static void
348d480f
PA
13763tracepoint_print_one_detail (const struct breakpoint *self,
13764 struct ui_out *uiout)
74960c60 13765{
d9b3f62e
PA
13766 struct tracepoint *tp = (struct tracepoint *) self;
13767 if (tp->static_trace_marker_id)
348d480f
PA
13768 {
13769 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13770
348d480f
PA
13771 ui_out_text (uiout, "\tmarker id is ");
13772 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13773 tp->static_trace_marker_id);
348d480f
PA
13774 ui_out_text (uiout, "\n");
13775 }
0d381245
VP
13776}
13777
a474d7c2 13778static void
348d480f 13779tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13780{
79a45e25 13781 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13782 return;
cc59ec59 13783
348d480f
PA
13784 switch (b->type)
13785 {
13786 case bp_tracepoint:
13787 printf_filtered (_("Tracepoint"));
13788 printf_filtered (_(" %d"), b->number);
13789 break;
13790 case bp_fast_tracepoint:
13791 printf_filtered (_("Fast tracepoint"));
13792 printf_filtered (_(" %d"), b->number);
13793 break;
13794 case bp_static_tracepoint:
13795 printf_filtered (_("Static tracepoint"));
13796 printf_filtered (_(" %d"), b->number);
13797 break;
13798 default:
13799 internal_error (__FILE__, __LINE__,
13800 _("unhandled tracepoint type %d"), (int) b->type);
13801 }
13802
13803 say_where (b);
a474d7c2
PA
13804}
13805
348d480f 13806static void
d9b3f62e 13807tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13808{
d9b3f62e
PA
13809 struct tracepoint *tp = (struct tracepoint *) self;
13810
13811 if (self->type == bp_fast_tracepoint)
348d480f 13812 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13813 if (self->type == bp_static_tracepoint)
348d480f 13814 fprintf_unfiltered (fp, "strace");
d9b3f62e 13815 else if (self->type == bp_tracepoint)
348d480f
PA
13816 fprintf_unfiltered (fp, "trace");
13817 else
13818 internal_error (__FILE__, __LINE__,
d9b3f62e 13819 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13820
d9b3f62e
PA
13821 fprintf_unfiltered (fp, " %s", self->addr_string);
13822 print_recreate_thread (self, fp);
13823
13824 if (tp->pass_count)
13825 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13826}
13827
983af33b
SDJ
13828static void
13829tracepoint_create_sals_from_address (char **arg,
13830 struct linespec_result *canonical,
13831 enum bptype type_wanted,
13832 char *addr_start, char **copy_arg)
13833{
13834 create_sals_from_address_default (arg, canonical, type_wanted,
13835 addr_start, copy_arg);
13836}
13837
13838static void
13839tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13840 struct linespec_result *canonical,
983af33b 13841 char *cond_string,
e7e0cddf 13842 char *extra_string,
983af33b
SDJ
13843 enum bptype type_wanted,
13844 enum bpdisp disposition,
13845 int thread,
13846 int task, int ignore_count,
13847 const struct breakpoint_ops *ops,
13848 int from_tty, int enabled,
44f238bb 13849 int internal, unsigned flags)
983af33b 13850{
023fa29b 13851 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13852 cond_string, extra_string,
13853 type_wanted,
983af33b
SDJ
13854 disposition, thread, task,
13855 ignore_count, ops, from_tty,
44f238bb 13856 enabled, internal, flags);
983af33b
SDJ
13857}
13858
13859static void
13860tracepoint_decode_linespec (struct breakpoint *b, char **s,
13861 struct symtabs_and_lines *sals)
13862{
13863 decode_linespec_default (b, s, sals);
13864}
13865
2060206e 13866struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13867
55aa24fb
SDJ
13868/* The breakpoint_ops structure to be use on tracepoints placed in a
13869 static probe. */
13870
13871static void
13872tracepoint_probe_create_sals_from_address (char **arg,
13873 struct linespec_result *canonical,
13874 enum bptype type_wanted,
13875 char *addr_start, char **copy_arg)
13876{
13877 /* We use the same method for breakpoint on probes. */
13878 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13879 addr_start, copy_arg);
13880}
13881
13882static void
13883tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13884 struct symtabs_and_lines *sals)
13885{
13886 /* We use the same method for breakpoint on probes. */
13887 bkpt_probe_decode_linespec (b, s, sals);
13888}
13889
13890static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13891
5c2b4418
HZ
13892/* Dprintf breakpoint_ops methods. */
13893
13894static void
13895dprintf_re_set (struct breakpoint *b)
13896{
13897 breakpoint_re_set_default (b);
13898
13899 /* This breakpoint could have been pending, and be resolved now, and
13900 if so, we should now have the extra string. If we don't, the
13901 dprintf was malformed when created, but we couldn't tell because
13902 we can't extract the extra string until the location is
13903 resolved. */
13904 if (b->loc != NULL && b->extra_string == NULL)
13905 error (_("Format string required"));
13906
13907 /* 1 - connect to target 1, that can run breakpoint commands.
13908 2 - create a dprintf, which resolves fine.
13909 3 - disconnect from target 1
13910 4 - connect to target 2, that can NOT run breakpoint commands.
13911
13912 After steps #3/#4, you'll want the dprintf command list to
13913 be updated, because target 1 and 2 may well return different
13914 answers for target_can_run_breakpoint_commands().
13915 Given absence of finer grained resetting, we get to do
13916 it all the time. */
13917 if (b->extra_string != NULL)
13918 update_dprintf_command_list (b);
13919}
13920
2d9442cc
HZ
13921/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13922
13923static void
13924dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13925{
13926 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13927 tp->extra_string);
13928 print_recreate_thread (tp, fp);
13929}
13930
9d6e6e84
HZ
13931/* Implement the "after_condition_true" breakpoint_ops method for
13932 dprintf.
13933
13934 dprintf's are implemented with regular commands in their command
13935 list, but we run the commands here instead of before presenting the
13936 stop to the user, as dprintf's don't actually cause a stop. This
13937 also makes it so that the commands of multiple dprintfs at the same
13938 address are all handled. */
13939
13940static void
13941dprintf_after_condition_true (struct bpstats *bs)
13942{
13943 struct cleanup *old_chain;
13944 struct bpstats tmp_bs = { NULL };
13945 struct bpstats *tmp_bs_p = &tmp_bs;
13946
13947 /* dprintf's never cause a stop. This wasn't set in the
13948 check_status hook instead because that would make the dprintf's
13949 condition not be evaluated. */
13950 bs->stop = 0;
13951
13952 /* Run the command list here. Take ownership of it instead of
13953 copying. We never want these commands to run later in
13954 bpstat_do_actions, if a breakpoint that causes a stop happens to
13955 be set at same address as this dprintf, or even if running the
13956 commands here throws. */
13957 tmp_bs.commands = bs->commands;
13958 bs->commands = NULL;
13959 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13960
13961 bpstat_do_actions_1 (&tmp_bs_p);
13962
13963 /* 'tmp_bs.commands' will usually be NULL by now, but
13964 bpstat_do_actions_1 may return early without processing the whole
13965 list. */
13966 do_cleanups (old_chain);
13967}
13968
983af33b
SDJ
13969/* The breakpoint_ops structure to be used on static tracepoints with
13970 markers (`-m'). */
13971
13972static void
13973strace_marker_create_sals_from_address (char **arg,
13974 struct linespec_result *canonical,
13975 enum bptype type_wanted,
13976 char *addr_start, char **copy_arg)
13977{
13978 struct linespec_sals lsal;
13979
13980 lsal.sals = decode_static_tracepoint_spec (arg);
13981
13982 *copy_arg = savestring (addr_start, *arg - addr_start);
13983
13984 canonical->addr_string = xstrdup (*copy_arg);
13985 lsal.canonical = xstrdup (*copy_arg);
13986 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13987}
13988
13989static void
13990strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13991 struct linespec_result *canonical,
983af33b 13992 char *cond_string,
e7e0cddf 13993 char *extra_string,
983af33b
SDJ
13994 enum bptype type_wanted,
13995 enum bpdisp disposition,
13996 int thread,
13997 int task, int ignore_count,
13998 const struct breakpoint_ops *ops,
13999 int from_tty, int enabled,
44f238bb 14000 int internal, unsigned flags)
983af33b
SDJ
14001{
14002 int i;
52d361e1
YQ
14003 struct linespec_sals *lsal = VEC_index (linespec_sals,
14004 canonical->sals, 0);
983af33b
SDJ
14005
14006 /* If the user is creating a static tracepoint by marker id
14007 (strace -m MARKER_ID), then store the sals index, so that
14008 breakpoint_re_set can try to match up which of the newly
14009 found markers corresponds to this one, and, don't try to
14010 expand multiple locations for each sal, given than SALS
14011 already should contain all sals for MARKER_ID. */
14012
14013 for (i = 0; i < lsal->sals.nelts; ++i)
14014 {
14015 struct symtabs_and_lines expanded;
14016 struct tracepoint *tp;
14017 struct cleanup *old_chain;
14018 char *addr_string;
14019
14020 expanded.nelts = 1;
14021 expanded.sals = &lsal->sals.sals[i];
14022
14023 addr_string = xstrdup (canonical->addr_string);
14024 old_chain = make_cleanup (xfree, addr_string);
14025
14026 tp = XCNEW (struct tracepoint);
14027 init_breakpoint_sal (&tp->base, gdbarch, expanded,
14028 addr_string, NULL,
e7e0cddf
SS
14029 cond_string, extra_string,
14030 type_wanted, disposition,
983af33b 14031 thread, task, ignore_count, ops,
44f238bb 14032 from_tty, enabled, internal, flags,
983af33b
SDJ
14033 canonical->special_display);
14034 /* Given that its possible to have multiple markers with
14035 the same string id, if the user is creating a static
14036 tracepoint by marker id ("strace -m MARKER_ID"), then
14037 store the sals index, so that breakpoint_re_set can
14038 try to match up which of the newly found markers
14039 corresponds to this one */
14040 tp->static_trace_marker_id_idx = i;
14041
14042 install_breakpoint (internal, &tp->base, 0);
14043
14044 discard_cleanups (old_chain);
14045 }
14046}
14047
14048static void
14049strace_marker_decode_linespec (struct breakpoint *b, char **s,
14050 struct symtabs_and_lines *sals)
14051{
14052 struct tracepoint *tp = (struct tracepoint *) b;
14053
14054 *sals = decode_static_tracepoint_spec (s);
14055 if (sals->nelts > tp->static_trace_marker_id_idx)
14056 {
14057 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
14058 sals->nelts = 1;
14059 }
14060 else
14061 error (_("marker %s not found"), tp->static_trace_marker_id);
14062}
14063
14064static struct breakpoint_ops strace_marker_breakpoint_ops;
14065
14066static int
14067strace_marker_p (struct breakpoint *b)
14068{
14069 return b->ops == &strace_marker_breakpoint_ops;
14070}
14071
53a5351d 14072/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 14073 structures. */
c906108c
SS
14074
14075void
fba45db2 14076delete_breakpoint (struct breakpoint *bpt)
c906108c 14077{
52f0bd74 14078 struct breakpoint *b;
c906108c 14079
8a3fe4f8 14080 gdb_assert (bpt != NULL);
c906108c 14081
4a64f543
MS
14082 /* Has this bp already been deleted? This can happen because
14083 multiple lists can hold pointers to bp's. bpstat lists are
14084 especial culprits.
14085
14086 One example of this happening is a watchpoint's scope bp. When
14087 the scope bp triggers, we notice that the watchpoint is out of
14088 scope, and delete it. We also delete its scope bp. But the
14089 scope bp is marked "auto-deleting", and is already on a bpstat.
14090 That bpstat is then checked for auto-deleting bp's, which are
14091 deleted.
14092
14093 A real solution to this problem might involve reference counts in
14094 bp's, and/or giving them pointers back to their referencing
14095 bpstat's, and teaching delete_breakpoint to only free a bp's
14096 storage when no more references were extent. A cheaper bandaid
14097 was chosen. */
c906108c
SS
14098 if (bpt->type == bp_none)
14099 return;
14100
4a64f543
MS
14101 /* At least avoid this stale reference until the reference counting
14102 of breakpoints gets resolved. */
d0fb5eae 14103 if (bpt->related_breakpoint != bpt)
e5a0a904 14104 {
d0fb5eae 14105 struct breakpoint *related;
3a5c3e22 14106 struct watchpoint *w;
d0fb5eae
JK
14107
14108 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 14109 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 14110 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
14111 w = (struct watchpoint *) bpt;
14112 else
14113 w = NULL;
14114 if (w != NULL)
14115 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
14116
14117 /* Unlink bpt from the bpt->related_breakpoint ring. */
14118 for (related = bpt; related->related_breakpoint != bpt;
14119 related = related->related_breakpoint);
14120 related->related_breakpoint = bpt->related_breakpoint;
14121 bpt->related_breakpoint = bpt;
e5a0a904
JK
14122 }
14123
a9634178
TJB
14124 /* watch_command_1 creates a watchpoint but only sets its number if
14125 update_watchpoint succeeds in creating its bp_locations. If there's
14126 a problem in that process, we'll be asked to delete the half-created
14127 watchpoint. In that case, don't announce the deletion. */
14128 if (bpt->number)
14129 observer_notify_breakpoint_deleted (bpt);
c906108c 14130
c906108c
SS
14131 if (breakpoint_chain == bpt)
14132 breakpoint_chain = bpt->next;
14133
c906108c
SS
14134 ALL_BREAKPOINTS (b)
14135 if (b->next == bpt)
c5aa993b
JM
14136 {
14137 b->next = bpt->next;
14138 break;
14139 }
c906108c 14140
f431efe5
PA
14141 /* Be sure no bpstat's are pointing at the breakpoint after it's
14142 been freed. */
14143 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 14144 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
14145 pointing at bpt from the stop_bpstat list entirely, as breakpoint
14146 commands are associated with the bpstat; if we remove it here,
14147 then the later call to bpstat_do_actions (&stop_bpstat); in
14148 event-top.c won't do anything, and temporary breakpoints with
14149 commands won't work. */
14150
14151 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
14152
4a64f543
MS
14153 /* Now that breakpoint is removed from breakpoint list, update the
14154 global location list. This will remove locations that used to
14155 belong to this breakpoint. Do this before freeing the breakpoint
14156 itself, since remove_breakpoint looks at location's owner. It
14157 might be better design to have location completely
14158 self-contained, but it's not the case now. */
44702360 14159 update_global_location_list (UGLL_DONT_INSERT);
74960c60 14160
348d480f 14161 bpt->ops->dtor (bpt);
4a64f543
MS
14162 /* On the chance that someone will soon try again to delete this
14163 same bp, we mark it as deleted before freeing its storage. */
c906108c 14164 bpt->type = bp_none;
b8c9b27d 14165 xfree (bpt);
c906108c
SS
14166}
14167
4d6140d9
AC
14168static void
14169do_delete_breakpoint_cleanup (void *b)
14170{
14171 delete_breakpoint (b);
14172}
14173
14174struct cleanup *
14175make_cleanup_delete_breakpoint (struct breakpoint *b)
14176{
14177 return make_cleanup (do_delete_breakpoint_cleanup, b);
14178}
14179
51be5b68
PA
14180/* Iterator function to call a user-provided callback function once
14181 for each of B and its related breakpoints. */
14182
14183static void
14184iterate_over_related_breakpoints (struct breakpoint *b,
14185 void (*function) (struct breakpoint *,
14186 void *),
14187 void *data)
14188{
14189 struct breakpoint *related;
14190
14191 related = b;
14192 do
14193 {
14194 struct breakpoint *next;
14195
14196 /* FUNCTION may delete RELATED. */
14197 next = related->related_breakpoint;
14198
14199 if (next == related)
14200 {
14201 /* RELATED is the last ring entry. */
14202 function (related, data);
14203
14204 /* FUNCTION may have deleted it, so we'd never reach back to
14205 B. There's nothing left to do anyway, so just break
14206 out. */
14207 break;
14208 }
14209 else
14210 function (related, data);
14211
14212 related = next;
14213 }
14214 while (related != b);
14215}
95a42b64
TT
14216
14217static void
14218do_delete_breakpoint (struct breakpoint *b, void *ignore)
14219{
14220 delete_breakpoint (b);
14221}
14222
51be5b68
PA
14223/* A callback for map_breakpoint_numbers that calls
14224 delete_breakpoint. */
14225
14226static void
14227do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14228{
14229 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14230}
14231
c906108c 14232void
fba45db2 14233delete_command (char *arg, int from_tty)
c906108c 14234{
35df4500 14235 struct breakpoint *b, *b_tmp;
c906108c 14236
ea9365bb
TT
14237 dont_repeat ();
14238
c906108c
SS
14239 if (arg == 0)
14240 {
14241 int breaks_to_delete = 0;
14242
46c6471b
PA
14243 /* Delete all breakpoints if no argument. Do not delete
14244 internal breakpoints, these have to be deleted with an
14245 explicit breakpoint number argument. */
c5aa993b 14246 ALL_BREAKPOINTS (b)
46c6471b 14247 if (user_breakpoint_p (b))
973d738b
DJ
14248 {
14249 breaks_to_delete = 1;
14250 break;
14251 }
c906108c
SS
14252
14253 /* Ask user only if there are some breakpoints to delete. */
14254 if (!from_tty
e2e0b3e5 14255 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 14256 {
35df4500 14257 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14258 if (user_breakpoint_p (b))
c5aa993b 14259 delete_breakpoint (b);
c906108c
SS
14260 }
14261 }
14262 else
51be5b68 14263 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14264}
14265
0d381245
VP
14266static int
14267all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 14268{
0d381245 14269 for (; loc; loc = loc->next)
8645ff69
UW
14270 if (!loc->shlib_disabled
14271 && !loc->pspace->executing_startup)
0d381245
VP
14272 return 0;
14273 return 1;
fe3f5fa8
VP
14274}
14275
776592bf
DE
14276/* Subroutine of update_breakpoint_locations to simplify it.
14277 Return non-zero if multiple fns in list LOC have the same name.
14278 Null names are ignored. */
14279
14280static int
14281ambiguous_names_p (struct bp_location *loc)
14282{
14283 struct bp_location *l;
14284 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14285 (int (*) (const void *,
14286 const void *)) streq,
776592bf
DE
14287 NULL, xcalloc, xfree);
14288
14289 for (l = loc; l != NULL; l = l->next)
14290 {
14291 const char **slot;
14292 const char *name = l->function_name;
14293
14294 /* Allow for some names to be NULL, ignore them. */
14295 if (name == NULL)
14296 continue;
14297
14298 slot = (const char **) htab_find_slot (htab, (const void *) name,
14299 INSERT);
4a64f543
MS
14300 /* NOTE: We can assume slot != NULL here because xcalloc never
14301 returns NULL. */
776592bf
DE
14302 if (*slot != NULL)
14303 {
14304 htab_delete (htab);
14305 return 1;
14306 }
14307 *slot = name;
14308 }
14309
14310 htab_delete (htab);
14311 return 0;
14312}
14313
0fb4aa4b
PA
14314/* When symbols change, it probably means the sources changed as well,
14315 and it might mean the static tracepoint markers are no longer at
14316 the same address or line numbers they used to be at last we
14317 checked. Losing your static tracepoints whenever you rebuild is
14318 undesirable. This function tries to resync/rematch gdb static
14319 tracepoints with the markers on the target, for static tracepoints
14320 that have not been set by marker id. Static tracepoint that have
14321 been set by marker id are reset by marker id in breakpoint_re_set.
14322 The heuristic is:
14323
14324 1) For a tracepoint set at a specific address, look for a marker at
14325 the old PC. If one is found there, assume to be the same marker.
14326 If the name / string id of the marker found is different from the
14327 previous known name, assume that means the user renamed the marker
14328 in the sources, and output a warning.
14329
14330 2) For a tracepoint set at a given line number, look for a marker
14331 at the new address of the old line number. If one is found there,
14332 assume to be the same marker. If the name / string id of the
14333 marker found is different from the previous known name, assume that
14334 means the user renamed the marker in the sources, and output a
14335 warning.
14336
14337 3) If a marker is no longer found at the same address or line, it
14338 may mean the marker no longer exists. But it may also just mean
14339 the code changed a bit. Maybe the user added a few lines of code
14340 that made the marker move up or down (in line number terms). Ask
14341 the target for info about the marker with the string id as we knew
14342 it. If found, update line number and address in the matching
14343 static tracepoint. This will get confused if there's more than one
14344 marker with the same ID (possible in UST, although unadvised
14345 precisely because it confuses tools). */
14346
14347static struct symtab_and_line
14348update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14349{
d9b3f62e 14350 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14351 struct static_tracepoint_marker marker;
14352 CORE_ADDR pc;
0fb4aa4b
PA
14353
14354 pc = sal.pc;
14355 if (sal.line)
14356 find_line_pc (sal.symtab, sal.line, &pc);
14357
14358 if (target_static_tracepoint_marker_at (pc, &marker))
14359 {
d9b3f62e 14360 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14361 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14362 b->number,
d9b3f62e 14363 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14364
d9b3f62e
PA
14365 xfree (tp->static_trace_marker_id);
14366 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14367 release_static_tracepoint_marker (&marker);
14368
14369 return sal;
14370 }
14371
14372 /* Old marker wasn't found on target at lineno. Try looking it up
14373 by string ID. */
14374 if (!sal.explicit_pc
14375 && sal.line != 0
14376 && sal.symtab != NULL
d9b3f62e 14377 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14378 {
14379 VEC(static_tracepoint_marker_p) *markers;
14380
14381 markers
d9b3f62e 14382 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14383
14384 if (!VEC_empty(static_tracepoint_marker_p, markers))
14385 {
80e1d417 14386 struct symtab_and_line sal2;
0fb4aa4b 14387 struct symbol *sym;
80e1d417 14388 struct static_tracepoint_marker *tpmarker;
79a45e25 14389 struct ui_out *uiout = current_uiout;
0fb4aa4b 14390
80e1d417 14391 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14392
d9b3f62e 14393 xfree (tp->static_trace_marker_id);
80e1d417 14394 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14395
14396 warning (_("marker for static tracepoint %d (%s) not "
14397 "found at previous line number"),
d9b3f62e 14398 b->number, tp->static_trace_marker_id);
0fb4aa4b 14399
80e1d417 14400 init_sal (&sal2);
0fb4aa4b 14401
80e1d417 14402 sal2.pc = tpmarker->address;
0fb4aa4b 14403
80e1d417
AS
14404 sal2 = find_pc_line (tpmarker->address, 0);
14405 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14406 ui_out_text (uiout, "Now in ");
14407 if (sym)
14408 {
14409 ui_out_field_string (uiout, "func",
14410 SYMBOL_PRINT_NAME (sym));
14411 ui_out_text (uiout, " at ");
14412 }
05cba821
JK
14413 ui_out_field_string (uiout, "file",
14414 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14415 ui_out_text (uiout, ":");
14416
14417 if (ui_out_is_mi_like_p (uiout))
14418 {
0b0865da 14419 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14420
f35a17b5 14421 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14422 }
14423
80e1d417 14424 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14425 ui_out_text (uiout, "\n");
14426
80e1d417 14427 b->loc->line_number = sal2.line;
2f202fde 14428 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
14429
14430 xfree (b->addr_string);
14431 b->addr_string = xstrprintf ("%s:%d",
05cba821 14432 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 14433 b->loc->line_number);
0fb4aa4b
PA
14434
14435 /* Might be nice to check if function changed, and warn if
14436 so. */
14437
80e1d417 14438 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14439 }
14440 }
14441 return sal;
14442}
14443
8d3788bd
VP
14444/* Returns 1 iff locations A and B are sufficiently same that
14445 we don't need to report breakpoint as changed. */
14446
14447static int
14448locations_are_equal (struct bp_location *a, struct bp_location *b)
14449{
14450 while (a && b)
14451 {
14452 if (a->address != b->address)
14453 return 0;
14454
14455 if (a->shlib_disabled != b->shlib_disabled)
14456 return 0;
14457
14458 if (a->enabled != b->enabled)
14459 return 0;
14460
14461 a = a->next;
14462 b = b->next;
14463 }
14464
14465 if ((a == NULL) != (b == NULL))
14466 return 0;
14467
14468 return 1;
14469}
14470
f1310107
TJB
14471/* Create new breakpoint locations for B (a hardware or software breakpoint)
14472 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14473 a ranged breakpoint. */
14474
0e30163f 14475void
0d381245 14476update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14477 struct symtabs_and_lines sals,
14478 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14479{
14480 int i;
0d381245
VP
14481 struct bp_location *existing_locations = b->loc;
14482
f8eba3c6
TT
14483 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14484 {
14485 /* Ranged breakpoints have only one start location and one end
14486 location. */
14487 b->enable_state = bp_disabled;
44702360 14488 update_global_location_list (UGLL_MAY_INSERT);
f8eba3c6
TT
14489 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14490 "multiple locations found\n"),
14491 b->number);
14492 return;
14493 }
f1310107 14494
4a64f543
MS
14495 /* If there's no new locations, and all existing locations are
14496 pending, don't do anything. This optimizes the common case where
14497 all locations are in the same shared library, that was unloaded.
14498 We'd like to retain the location, so that when the library is
14499 loaded again, we don't loose the enabled/disabled status of the
14500 individual locations. */
0d381245 14501 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14502 return;
14503
fe3f5fa8
VP
14504 b->loc = NULL;
14505
0d381245 14506 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14507 {
f8eba3c6
TT
14508 struct bp_location *new_loc;
14509
14510 switch_to_program_space_and_thread (sals.sals[i].pspace);
14511
14512 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14513
0d381245
VP
14514 /* Reparse conditions, they might contain references to the
14515 old symtab. */
14516 if (b->cond_string != NULL)
14517 {
bbc13ae3 14518 const char *s;
bfd189b1 14519 volatile struct gdb_exception e;
fe3f5fa8 14520
0d381245
VP
14521 s = b->cond_string;
14522 TRY_CATCH (e, RETURN_MASK_ERROR)
14523 {
1bb9788d
TT
14524 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14525 block_for_pc (sals.sals[i].pc),
0d381245
VP
14526 0);
14527 }
14528 if (e.reason < 0)
14529 {
3e43a32a
MS
14530 warning (_("failed to reevaluate condition "
14531 "for breakpoint %d: %s"),
0d381245
VP
14532 b->number, e.message);
14533 new_loc->enabled = 0;
14534 }
14535 }
fe3f5fa8 14536
f1310107
TJB
14537 if (sals_end.nelts)
14538 {
14539 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14540
14541 new_loc->length = end - sals.sals[0].pc + 1;
14542 }
0d381245 14543 }
fe3f5fa8 14544
514f746b
AR
14545 /* Update locations of permanent breakpoints. */
14546 if (b->enable_state == bp_permanent)
14547 make_breakpoint_permanent (b);
14548
4a64f543
MS
14549 /* If possible, carry over 'disable' status from existing
14550 breakpoints. */
0d381245
VP
14551 {
14552 struct bp_location *e = existing_locations;
776592bf
DE
14553 /* If there are multiple breakpoints with the same function name,
14554 e.g. for inline functions, comparing function names won't work.
14555 Instead compare pc addresses; this is just a heuristic as things
14556 may have moved, but in practice it gives the correct answer
14557 often enough until a better solution is found. */
14558 int have_ambiguous_names = ambiguous_names_p (b->loc);
14559
0d381245
VP
14560 for (; e; e = e->next)
14561 {
14562 if (!e->enabled && e->function_name)
14563 {
14564 struct bp_location *l = b->loc;
776592bf
DE
14565 if (have_ambiguous_names)
14566 {
14567 for (; l; l = l->next)
f1310107 14568 if (breakpoint_locations_match (e, l))
776592bf
DE
14569 {
14570 l->enabled = 0;
14571 break;
14572 }
14573 }
14574 else
14575 {
14576 for (; l; l = l->next)
14577 if (l->function_name
14578 && strcmp (e->function_name, l->function_name) == 0)
14579 {
14580 l->enabled = 0;
14581 break;
14582 }
14583 }
0d381245
VP
14584 }
14585 }
14586 }
fe3f5fa8 14587
8d3788bd
VP
14588 if (!locations_are_equal (existing_locations, b->loc))
14589 observer_notify_breakpoint_modified (b);
14590
44702360 14591 update_global_location_list (UGLL_MAY_INSERT);
fe3f5fa8
VP
14592}
14593
ef23e705
TJB
14594/* Find the SaL locations corresponding to the given ADDR_STRING.
14595 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14596
14597static struct symtabs_and_lines
14598addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14599{
14600 char *s;
02d20e4a 14601 struct symtabs_and_lines sals = {0};
f8eba3c6 14602 volatile struct gdb_exception e;
ef23e705 14603
983af33b 14604 gdb_assert (b->ops != NULL);
ef23e705 14605 s = addr_string;
ef23e705
TJB
14606
14607 TRY_CATCH (e, RETURN_MASK_ERROR)
14608 {
983af33b 14609 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
14610 }
14611 if (e.reason < 0)
14612 {
14613 int not_found_and_ok = 0;
14614 /* For pending breakpoints, it's expected that parsing will
14615 fail until the right shared library is loaded. User has
14616 already told to create pending breakpoints and don't need
14617 extra messages. If breakpoint is in bp_shlib_disabled
14618 state, then user already saw the message about that
14619 breakpoint being disabled, and don't want to see more
14620 errors. */
58438ac1 14621 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14622 && (b->condition_not_parsed
14623 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14624 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14625 || b->enable_state == bp_disabled))
14626 not_found_and_ok = 1;
14627
14628 if (!not_found_and_ok)
14629 {
14630 /* We surely don't want to warn about the same breakpoint
14631 10 times. One solution, implemented here, is disable
14632 the breakpoint on error. Another solution would be to
14633 have separate 'warning emitted' flag. Since this
14634 happens only when a binary has changed, I don't know
14635 which approach is better. */
14636 b->enable_state = bp_disabled;
14637 throw_exception (e);
14638 }
14639 }
14640
58438ac1 14641 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 14642 {
f8eba3c6 14643 int i;
ef23e705 14644
f8eba3c6
TT
14645 for (i = 0; i < sals.nelts; ++i)
14646 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14647 if (b->condition_not_parsed && s && s[0])
14648 {
ed1d1739
KS
14649 char *cond_string, *extra_string;
14650 int thread, task;
ef23e705
TJB
14651
14652 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14653 &cond_string, &thread, &task,
14654 &extra_string);
ef23e705
TJB
14655 if (cond_string)
14656 b->cond_string = cond_string;
14657 b->thread = thread;
14658 b->task = task;
e7e0cddf
SS
14659 if (extra_string)
14660 b->extra_string = extra_string;
ef23e705
TJB
14661 b->condition_not_parsed = 0;
14662 }
14663
983af33b 14664 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14665 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14666
58438ac1
TT
14667 *found = 1;
14668 }
14669 else
14670 *found = 0;
ef23e705
TJB
14671
14672 return sals;
14673}
14674
348d480f
PA
14675/* The default re_set method, for typical hardware or software
14676 breakpoints. Reevaluate the breakpoint and recreate its
14677 locations. */
14678
14679static void
28010a5d 14680breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14681{
14682 int found;
f1310107 14683 struct symtabs_and_lines sals, sals_end;
ef23e705 14684 struct symtabs_and_lines expanded = {0};
f1310107 14685 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14686
14687 sals = addr_string_to_sals (b, b->addr_string, &found);
14688 if (found)
14689 {
14690 make_cleanup (xfree, sals.sals);
f8eba3c6 14691 expanded = sals;
ef23e705
TJB
14692 }
14693
f1310107
TJB
14694 if (b->addr_string_range_end)
14695 {
14696 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14697 if (found)
14698 {
14699 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14700 expanded_end = sals_end;
f1310107
TJB
14701 }
14702 }
14703
14704 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14705}
14706
983af33b
SDJ
14707/* Default method for creating SALs from an address string. It basically
14708 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14709
14710static void
14711create_sals_from_address_default (char **arg,
14712 struct linespec_result *canonical,
14713 enum bptype type_wanted,
14714 char *addr_start, char **copy_arg)
14715{
14716 parse_breakpoint_sals (arg, canonical);
14717}
14718
14719/* Call create_breakpoints_sal for the given arguments. This is the default
14720 function for the `create_breakpoints_sal' method of
14721 breakpoint_ops. */
14722
14723static void
14724create_breakpoints_sal_default (struct gdbarch *gdbarch,
14725 struct linespec_result *canonical,
983af33b 14726 char *cond_string,
e7e0cddf 14727 char *extra_string,
983af33b
SDJ
14728 enum bptype type_wanted,
14729 enum bpdisp disposition,
14730 int thread,
14731 int task, int ignore_count,
14732 const struct breakpoint_ops *ops,
14733 int from_tty, int enabled,
44f238bb 14734 int internal, unsigned flags)
983af33b
SDJ
14735{
14736 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14737 extra_string,
983af33b
SDJ
14738 type_wanted, disposition,
14739 thread, task, ignore_count, ops, from_tty,
44f238bb 14740 enabled, internal, flags);
983af33b
SDJ
14741}
14742
14743/* Decode the line represented by S by calling decode_line_full. This is the
14744 default function for the `decode_linespec' method of breakpoint_ops. */
14745
14746static void
14747decode_linespec_default (struct breakpoint *b, char **s,
14748 struct symtabs_and_lines *sals)
14749{
14750 struct linespec_result canonical;
14751
14752 init_linespec_result (&canonical);
14753 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14754 (struct symtab *) NULL, 0,
14755 &canonical, multiple_symbols_all,
14756 b->filter);
14757
14758 /* We should get 0 or 1 resulting SALs. */
14759 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14760
14761 if (VEC_length (linespec_sals, canonical.sals) > 0)
14762 {
14763 struct linespec_sals *lsal;
14764
14765 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14766 *sals = lsal->sals;
14767 /* Arrange it so the destructor does not free the
14768 contents. */
14769 lsal->sals.sals = NULL;
14770 }
14771
14772 destroy_linespec_result (&canonical);
14773}
14774
28010a5d
PA
14775/* Prepare the global context for a re-set of breakpoint B. */
14776
14777static struct cleanup *
14778prepare_re_set_context (struct breakpoint *b)
14779{
14780 struct cleanup *cleanups;
14781
14782 input_radix = b->input_radix;
14783 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14784 if (b->pspace != NULL)
14785 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14786 set_language (b->language);
14787
14788 return cleanups;
ef23e705
TJB
14789}
14790
c906108c
SS
14791/* Reset a breakpoint given it's struct breakpoint * BINT.
14792 The value we return ends up being the return value from catch_errors.
14793 Unused in this case. */
14794
14795static int
4efb68b1 14796breakpoint_re_set_one (void *bint)
c906108c 14797{
4a64f543 14798 /* Get past catch_errs. */
53a5351d 14799 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14800 struct cleanup *cleanups;
c906108c 14801
348d480f
PA
14802 cleanups = prepare_re_set_context (b);
14803 b->ops->re_set (b);
14804 do_cleanups (cleanups);
c906108c
SS
14805 return 0;
14806}
14807
69de3c6a 14808/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14809void
69de3c6a 14810breakpoint_re_set (void)
c906108c 14811{
35df4500 14812 struct breakpoint *b, *b_tmp;
c906108c
SS
14813 enum language save_language;
14814 int save_input_radix;
6c95b8df 14815 struct cleanup *old_chain;
c5aa993b 14816
c906108c
SS
14817 save_language = current_language->la_language;
14818 save_input_radix = input_radix;
6c95b8df
PA
14819 old_chain = save_current_program_space ();
14820
35df4500 14821 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14822 {
4a64f543 14823 /* Format possible error msg. */
fe3f5fa8 14824 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14825 b->number);
14826 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14827 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14828 do_cleanups (cleanups);
c5aa993b 14829 }
c906108c
SS
14830 set_language (save_language);
14831 input_radix = save_input_radix;
e62c965a 14832
0756c555 14833 jit_breakpoint_re_set ();
4efc6507 14834
6c95b8df
PA
14835 do_cleanups (old_chain);
14836
af02033e
PP
14837 create_overlay_event_breakpoint ();
14838 create_longjmp_master_breakpoint ();
14839 create_std_terminate_master_breakpoint ();
186c406b 14840 create_exception_master_breakpoint ();
c906108c
SS
14841}
14842\f
c906108c
SS
14843/* Reset the thread number of this breakpoint:
14844
14845 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14846 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14847void
fba45db2 14848breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14849{
14850 if (b->thread != -1)
14851 {
39f77062
KB
14852 if (in_thread_list (inferior_ptid))
14853 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14854
14855 /* We're being called after following a fork. The new fork is
14856 selected as current, and unless this was a vfork will have a
14857 different program space from the original thread. Reset that
14858 as well. */
14859 b->loc->pspace = current_program_space;
c906108c
SS
14860 }
14861}
14862
03ac34d5
MS
14863/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14864 If from_tty is nonzero, it prints a message to that effect,
14865 which ends with a period (no newline). */
14866
c906108c 14867void
fba45db2 14868set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14869{
52f0bd74 14870 struct breakpoint *b;
c906108c
SS
14871
14872 if (count < 0)
14873 count = 0;
14874
14875 ALL_BREAKPOINTS (b)
14876 if (b->number == bptnum)
c5aa993b 14877 {
d77f58be
SS
14878 if (is_tracepoint (b))
14879 {
14880 if (from_tty && count != 0)
14881 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14882 bptnum);
14883 return;
14884 }
14885
c5aa993b 14886 b->ignore_count = count;
221ea385
KS
14887 if (from_tty)
14888 {
14889 if (count == 0)
3e43a32a
MS
14890 printf_filtered (_("Will stop next time "
14891 "breakpoint %d is reached."),
221ea385
KS
14892 bptnum);
14893 else if (count == 1)
a3f17187 14894 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14895 bptnum);
14896 else
3e43a32a
MS
14897 printf_filtered (_("Will ignore next %d "
14898 "crossings of breakpoint %d."),
221ea385
KS
14899 count, bptnum);
14900 }
8d3788bd 14901 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14902 return;
14903 }
c906108c 14904
8a3fe4f8 14905 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14906}
14907
c906108c
SS
14908/* Command to set ignore-count of breakpoint N to COUNT. */
14909
14910static void
fba45db2 14911ignore_command (char *args, int from_tty)
c906108c
SS
14912{
14913 char *p = args;
52f0bd74 14914 int num;
c906108c
SS
14915
14916 if (p == 0)
e2e0b3e5 14917 error_no_arg (_("a breakpoint number"));
c5aa993b 14918
c906108c 14919 num = get_number (&p);
5c44784c 14920 if (num == 0)
8a3fe4f8 14921 error (_("bad breakpoint number: '%s'"), args);
c906108c 14922 if (*p == 0)
8a3fe4f8 14923 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14924
14925 set_ignore_count (num,
14926 longest_to_int (value_as_long (parse_and_eval (p))),
14927 from_tty);
221ea385
KS
14928 if (from_tty)
14929 printf_filtered ("\n");
c906108c
SS
14930}
14931\f
14932/* Call FUNCTION on each of the breakpoints
14933 whose numbers are given in ARGS. */
14934
14935static void
95a42b64
TT
14936map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14937 void *),
14938 void *data)
c906108c 14939{
52f0bd74
AC
14940 int num;
14941 struct breakpoint *b, *tmp;
11cf8741 14942 int match;
197f0a60 14943 struct get_number_or_range_state state;
c906108c 14944
197f0a60 14945 if (args == 0)
e2e0b3e5 14946 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14947
197f0a60
TT
14948 init_number_or_range (&state, args);
14949
14950 while (!state.finished)
c906108c 14951 {
e799154c 14952 const char *p = state.string;
197f0a60 14953
11cf8741 14954 match = 0;
c5aa993b 14955
197f0a60 14956 num = get_number_or_range (&state);
5c44784c 14957 if (num == 0)
c5aa993b 14958 {
8a3fe4f8 14959 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14960 }
14961 else
14962 {
14963 ALL_BREAKPOINTS_SAFE (b, tmp)
14964 if (b->number == num)
14965 {
11cf8741 14966 match = 1;
cdac0397 14967 function (b, data);
11cf8741 14968 break;
5c44784c 14969 }
11cf8741 14970 if (match == 0)
a3f17187 14971 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14972 }
c906108c
SS
14973 }
14974}
14975
0d381245
VP
14976static struct bp_location *
14977find_location_by_number (char *number)
14978{
14979 char *dot = strchr (number, '.');
14980 char *p1;
14981 int bp_num;
14982 int loc_num;
14983 struct breakpoint *b;
14984 struct bp_location *loc;
14985
14986 *dot = '\0';
14987
14988 p1 = number;
197f0a60 14989 bp_num = get_number (&p1);
0d381245
VP
14990 if (bp_num == 0)
14991 error (_("Bad breakpoint number '%s'"), number);
14992
14993 ALL_BREAKPOINTS (b)
14994 if (b->number == bp_num)
14995 {
14996 break;
14997 }
14998
14999 if (!b || b->number != bp_num)
15000 error (_("Bad breakpoint number '%s'"), number);
15001
15002 p1 = dot+1;
197f0a60 15003 loc_num = get_number (&p1);
0d381245
VP
15004 if (loc_num == 0)
15005 error (_("Bad breakpoint location number '%s'"), number);
15006
15007 --loc_num;
15008 loc = b->loc;
15009 for (;loc_num && loc; --loc_num, loc = loc->next)
15010 ;
15011 if (!loc)
15012 error (_("Bad breakpoint location number '%s'"), dot+1);
15013
15014 return loc;
15015}
15016
15017
1900040c
MS
15018/* Set ignore-count of breakpoint number BPTNUM to COUNT.
15019 If from_tty is nonzero, it prints a message to that effect,
15020 which ends with a period (no newline). */
15021
c906108c 15022void
fba45db2 15023disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
15024{
15025 /* Never disable a watchpoint scope breakpoint; we want to
15026 hit them when we leave scope so we can delete both the
15027 watchpoint and its scope breakpoint at that time. */
15028 if (bpt->type == bp_watchpoint_scope)
15029 return;
15030
c2c6d25f 15031 /* You can't disable permanent breakpoints. */
b5de0fa7 15032 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
15033 return;
15034
b5de0fa7 15035 bpt->enable_state = bp_disabled;
c906108c 15036
b775012e
LM
15037 /* Mark breakpoint locations modified. */
15038 mark_breakpoint_modified (bpt);
15039
d248b706
KY
15040 if (target_supports_enable_disable_tracepoint ()
15041 && current_trace_status ()->running && is_tracepoint (bpt))
15042 {
15043 struct bp_location *location;
15044
15045 for (location = bpt->loc; location; location = location->next)
15046 target_disable_tracepoint (location);
15047 }
15048
44702360 15049 update_global_location_list (UGLL_DONT_INSERT);
c906108c 15050
8d3788bd 15051 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15052}
15053
51be5b68
PA
15054/* A callback for iterate_over_related_breakpoints. */
15055
15056static void
15057do_disable_breakpoint (struct breakpoint *b, void *ignore)
15058{
15059 disable_breakpoint (b);
15060}
15061
95a42b64
TT
15062/* A callback for map_breakpoint_numbers that calls
15063 disable_breakpoint. */
15064
15065static void
15066do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
15067{
51be5b68 15068 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
15069}
15070
c906108c 15071static void
fba45db2 15072disable_command (char *args, int from_tty)
c906108c 15073{
c906108c 15074 if (args == 0)
46c6471b
PA
15075 {
15076 struct breakpoint *bpt;
15077
15078 ALL_BREAKPOINTS (bpt)
15079 if (user_breakpoint_p (bpt))
15080 disable_breakpoint (bpt);
15081 }
9eaabc75 15082 else
0d381245 15083 {
9eaabc75
MW
15084 char *num = extract_arg (&args);
15085
15086 while (num)
d248b706 15087 {
9eaabc75 15088 if (strchr (num, '.'))
b775012e 15089 {
9eaabc75
MW
15090 struct bp_location *loc = find_location_by_number (num);
15091
15092 if (loc)
15093 {
15094 if (loc->enabled)
15095 {
15096 loc->enabled = 0;
15097 mark_breakpoint_location_modified (loc);
15098 }
15099 if (target_supports_enable_disable_tracepoint ()
15100 && current_trace_status ()->running && loc->owner
15101 && is_tracepoint (loc->owner))
15102 target_disable_tracepoint (loc);
15103 }
44702360 15104 update_global_location_list (UGLL_DONT_INSERT);
b775012e 15105 }
9eaabc75
MW
15106 else
15107 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
15108 num = extract_arg (&args);
d248b706 15109 }
0d381245 15110 }
c906108c
SS
15111}
15112
15113static void
816338b5
SS
15114enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
15115 int count)
c906108c 15116{
afe38095 15117 int target_resources_ok;
c906108c
SS
15118
15119 if (bpt->type == bp_hardware_breakpoint)
15120 {
15121 int i;
c5aa993b 15122 i = hw_breakpoint_used_count ();
53a5351d 15123 target_resources_ok =
d92524f1 15124 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 15125 i + 1, 0);
c906108c 15126 if (target_resources_ok == 0)
8a3fe4f8 15127 error (_("No hardware breakpoint support in the target."));
c906108c 15128 else if (target_resources_ok < 0)
8a3fe4f8 15129 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
15130 }
15131
cc60f2e3 15132 if (is_watchpoint (bpt))
c906108c 15133 {
d07205c2
JK
15134 /* Initialize it just to avoid a GCC false warning. */
15135 enum enable_state orig_enable_state = 0;
bfd189b1 15136 volatile struct gdb_exception e;
dde02812
ES
15137
15138 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 15139 {
3a5c3e22
PA
15140 struct watchpoint *w = (struct watchpoint *) bpt;
15141
1e718ff1
TJB
15142 orig_enable_state = bpt->enable_state;
15143 bpt->enable_state = bp_enabled;
3a5c3e22 15144 update_watchpoint (w, 1 /* reparse */);
c906108c 15145 }
dde02812 15146 if (e.reason < 0)
c5aa993b 15147 {
1e718ff1 15148 bpt->enable_state = orig_enable_state;
dde02812
ES
15149 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
15150 bpt->number);
15151 return;
c5aa993b 15152 }
c906108c 15153 }
0101ce28 15154
b4c291bb
KH
15155 if (bpt->enable_state != bp_permanent)
15156 bpt->enable_state = bp_enabled;
d248b706 15157
b775012e
LM
15158 bpt->enable_state = bp_enabled;
15159
15160 /* Mark breakpoint locations modified. */
15161 mark_breakpoint_modified (bpt);
15162
d248b706
KY
15163 if (target_supports_enable_disable_tracepoint ()
15164 && current_trace_status ()->running && is_tracepoint (bpt))
15165 {
15166 struct bp_location *location;
15167
15168 for (location = bpt->loc; location; location = location->next)
15169 target_enable_tracepoint (location);
15170 }
15171
b4c291bb 15172 bpt->disposition = disposition;
816338b5 15173 bpt->enable_count = count;
44702360 15174 update_global_location_list (UGLL_MAY_INSERT);
9c97429f 15175
8d3788bd 15176 observer_notify_breakpoint_modified (bpt);
c906108c
SS
15177}
15178
fe3f5fa8 15179
c906108c 15180void
fba45db2 15181enable_breakpoint (struct breakpoint *bpt)
c906108c 15182{
816338b5 15183 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
15184}
15185
15186static void
15187do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15188{
15189 enable_breakpoint (bpt);
c906108c
SS
15190}
15191
95a42b64
TT
15192/* A callback for map_breakpoint_numbers that calls
15193 enable_breakpoint. */
15194
15195static void
15196do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15197{
51be5b68 15198 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
15199}
15200
c906108c
SS
15201/* The enable command enables the specified breakpoints (or all defined
15202 breakpoints) so they once again become (or continue to be) effective
1272ad14 15203 in stopping the inferior. */
c906108c 15204
c906108c 15205static void
fba45db2 15206enable_command (char *args, int from_tty)
c906108c 15207{
c906108c 15208 if (args == 0)
46c6471b
PA
15209 {
15210 struct breakpoint *bpt;
15211
15212 ALL_BREAKPOINTS (bpt)
15213 if (user_breakpoint_p (bpt))
15214 enable_breakpoint (bpt);
15215 }
9eaabc75 15216 else
0d381245 15217 {
9eaabc75
MW
15218 char *num = extract_arg (&args);
15219
15220 while (num)
d248b706 15221 {
9eaabc75 15222 if (strchr (num, '.'))
b775012e 15223 {
9eaabc75
MW
15224 struct bp_location *loc = find_location_by_number (num);
15225
15226 if (loc)
15227 {
15228 if (!loc->enabled)
15229 {
15230 loc->enabled = 1;
15231 mark_breakpoint_location_modified (loc);
15232 }
15233 if (target_supports_enable_disable_tracepoint ()
15234 && current_trace_status ()->running && loc->owner
15235 && is_tracepoint (loc->owner))
15236 target_enable_tracepoint (loc);
15237 }
44702360 15238 update_global_location_list (UGLL_MAY_INSERT);
b775012e 15239 }
9eaabc75
MW
15240 else
15241 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15242 num = extract_arg (&args);
d248b706 15243 }
0d381245 15244 }
c906108c
SS
15245}
15246
816338b5
SS
15247/* This struct packages up disposition data for application to multiple
15248 breakpoints. */
15249
15250struct disp_data
15251{
15252 enum bpdisp disp;
15253 int count;
15254};
15255
c906108c 15256static void
51be5b68
PA
15257do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15258{
816338b5 15259 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15260
816338b5 15261 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15262}
15263
15264static void
15265do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15266{
816338b5 15267 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15268
15269 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15270}
15271
c906108c 15272static void
fba45db2 15273enable_once_command (char *args, int from_tty)
c906108c 15274{
51be5b68 15275 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15276}
15277
816338b5
SS
15278static void
15279do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15280{
15281 struct disp_data disp = { disp_disable, *(int *) countptr };
15282
15283 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15284}
15285
15286static void
15287enable_count_command (char *args, int from_tty)
15288{
15289 int count = get_number (&args);
15290
15291 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15292}
15293
c906108c 15294static void
51be5b68 15295do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15296{
816338b5 15297 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15298
15299 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15300}
15301
c906108c 15302static void
fba45db2 15303enable_delete_command (char *args, int from_tty)
c906108c 15304{
51be5b68 15305 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15306}
15307\f
fa8d40ab
JJ
15308static void
15309set_breakpoint_cmd (char *args, int from_tty)
15310{
15311}
15312
15313static void
15314show_breakpoint_cmd (char *args, int from_tty)
15315{
15316}
15317
1f3b5d1b
PP
15318/* Invalidate last known value of any hardware watchpoint if
15319 the memory which that value represents has been written to by
15320 GDB itself. */
15321
15322static void
8de0566d
YQ
15323invalidate_bp_value_on_memory_change (struct inferior *inferior,
15324 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15325 const bfd_byte *data)
15326{
15327 struct breakpoint *bp;
15328
15329 ALL_BREAKPOINTS (bp)
15330 if (bp->enable_state == bp_enabled
3a5c3e22 15331 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15332 {
3a5c3e22 15333 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15334
3a5c3e22
PA
15335 if (wp->val_valid && wp->val)
15336 {
15337 struct bp_location *loc;
15338
15339 for (loc = bp->loc; loc != NULL; loc = loc->next)
15340 if (loc->loc_type == bp_loc_hardware_watchpoint
15341 && loc->address + loc->length > addr
15342 && addr + len > loc->address)
15343 {
15344 value_free (wp->val);
15345 wp->val = NULL;
15346 wp->val_valid = 0;
15347 }
15348 }
1f3b5d1b
PP
15349 }
15350}
15351
8181d85f
DJ
15352/* Create and insert a raw software breakpoint at PC. Return an
15353 identifier, which should be used to remove the breakpoint later.
15354 In general, places which call this should be using something on the
15355 breakpoint chain instead; this function should be eliminated
15356 someday. */
15357
15358void *
6c95b8df
PA
15359deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
15360 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
15361{
15362 struct bp_target_info *bp_tgt;
ef370185 15363 struct bp_location *bl;
8181d85f 15364
41bf6aca 15365 bp_tgt = XCNEW (struct bp_target_info);
8181d85f 15366
6c95b8df 15367 bp_tgt->placed_address_space = aspace;
0d5ed153 15368 bp_tgt->reqstd_address = pc;
6c95b8df 15369
ef370185
JB
15370 /* If an unconditional non-raw breakpoint is already inserted at
15371 that location, there's no need to insert another. However, with
15372 target-side evaluation of breakpoint conditions, if the
15373 breakpoint that is currently inserted on the target is
15374 conditional, we need to make it unconditional. Note that a
15375 breakpoint with target-side commands is not reported even if
15376 unconditional, so we need to remove the commands from the target
15377 as well. */
15378 bl = find_non_raw_software_breakpoint_inserted_here (aspace, pc);
15379 if (bl != NULL
15380 && VEC_empty (agent_expr_p, bl->target_info.conditions)
15381 && VEC_empty (agent_expr_p, bl->target_info.tcommands))
15382 {
15383 bp_target_info_copy_insertion_state (bp_tgt, &bl->target_info);
15384 return bp_tgt;
15385 }
15386
a6d9a66e 15387 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
15388 {
15389 /* Could not insert the breakpoint. */
15390 xfree (bp_tgt);
15391 return NULL;
15392 }
15393
15394 return bp_tgt;
15395}
15396
4a64f543
MS
15397/* Remove a breakpoint BP inserted by
15398 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
15399
15400int
a6d9a66e 15401deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
15402{
15403 struct bp_target_info *bp_tgt = bp;
ef370185 15404 struct address_space *aspace = bp_tgt->placed_address_space;
0d5ed153 15405 CORE_ADDR address = bp_tgt->reqstd_address;
ef370185 15406 struct bp_location *bl;
8181d85f
DJ
15407 int ret;
15408
ef370185
JB
15409 bl = find_non_raw_software_breakpoint_inserted_here (aspace, address);
15410
15411 /* Only remove the raw breakpoint if there are no other non-raw
15412 breakpoints still inserted at this location. Otherwise, we would
15413 be effectively disabling those breakpoints. */
15414 if (bl == NULL)
15415 ret = target_remove_breakpoint (gdbarch, bp_tgt);
15416 else if (!VEC_empty (agent_expr_p, bl->target_info.conditions)
15417 || !VEC_empty (agent_expr_p, bl->target_info.tcommands))
15418 {
15419 /* The target is evaluating conditions, and when we inserted the
15420 software single-step breakpoint, we had made the breakpoint
15421 unconditional and command-less on the target side. Reinsert
15422 to restore the conditions/commands. */
15423 ret = target_insert_breakpoint (bl->gdbarch, &bl->target_info);
15424 }
15425 else
15426 ret = 0;
15427
8181d85f
DJ
15428 xfree (bp_tgt);
15429
15430 return ret;
15431}
15432
8181d85f
DJ
15433/* Create and insert a breakpoint for software single step. */
15434
15435void
6c95b8df 15436insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15437 struct address_space *aspace,
15438 CORE_ADDR next_pc)
8181d85f
DJ
15439{
15440 void **bpt_p;
15441
15442 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
15443 {
15444 bpt_p = &single_step_breakpoints[0];
15445 single_step_gdbarch[0] = gdbarch;
15446 }
8181d85f
DJ
15447 else
15448 {
15449 gdb_assert (single_step_breakpoints[1] == NULL);
15450 bpt_p = &single_step_breakpoints[1];
a6d9a66e 15451 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
15452 }
15453
4a64f543
MS
15454 /* NOTE drow/2006-04-11: A future improvement to this function would
15455 be to only create the breakpoints once, and actually put them on
15456 the breakpoint chain. That would let us use set_raw_breakpoint.
15457 We could adjust the addresses each time they were needed. Doing
15458 this requires corresponding changes elsewhere where single step
15459 breakpoints are handled, however. So, for now, we use this. */
8181d85f 15460
6c95b8df 15461 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 15462 if (*bpt_p == NULL)
5af949e3
UW
15463 error (_("Could not insert single-step breakpoint at %s"),
15464 paddress (gdbarch, next_pc));
8181d85f
DJ
15465}
15466
f02253f1
HZ
15467/* Check if the breakpoints used for software single stepping
15468 were inserted or not. */
15469
15470int
15471single_step_breakpoints_inserted (void)
15472{
15473 return (single_step_breakpoints[0] != NULL
15474 || single_step_breakpoints[1] != NULL);
15475}
15476
8181d85f
DJ
15477/* Remove and delete any breakpoints used for software single step. */
15478
15479void
15480remove_single_step_breakpoints (void)
15481{
15482 gdb_assert (single_step_breakpoints[0] != NULL);
15483
15484 /* See insert_single_step_breakpoint for more about this deprecated
15485 call. */
a6d9a66e
UW
15486 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
15487 single_step_breakpoints[0]);
15488 single_step_gdbarch[0] = NULL;
8181d85f
DJ
15489 single_step_breakpoints[0] = NULL;
15490
15491 if (single_step_breakpoints[1] != NULL)
15492 {
a6d9a66e
UW
15493 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
15494 single_step_breakpoints[1]);
15495 single_step_gdbarch[1] = NULL;
8181d85f
DJ
15496 single_step_breakpoints[1] = NULL;
15497 }
15498}
15499
d03285ec
UW
15500/* Delete software single step breakpoints without removing them from
15501 the inferior. This is intended to be used if the inferior's address
15502 space where they were inserted is already gone, e.g. after exit or
15503 exec. */
15504
15505void
15506cancel_single_step_breakpoints (void)
15507{
15508 int i;
15509
15510 for (i = 0; i < 2; i++)
15511 if (single_step_breakpoints[i])
15512 {
15513 xfree (single_step_breakpoints[i]);
15514 single_step_breakpoints[i] = NULL;
15515 single_step_gdbarch[i] = NULL;
15516 }
15517}
15518
15519/* Detach software single-step breakpoints from INFERIOR_PTID without
15520 removing them. */
15521
15522static void
15523detach_single_step_breakpoints (void)
15524{
15525 int i;
15526
15527 for (i = 0; i < 2; i++)
15528 if (single_step_breakpoints[i])
15529 target_remove_breakpoint (single_step_gdbarch[i],
15530 single_step_breakpoints[i]);
15531}
15532
ef370185
JB
15533/* Find the software single-step breakpoint that inserted at PC.
15534 Returns its slot if found, and -1 if not found. */
1aafd4da 15535
ef370185
JB
15536static int
15537find_single_step_breakpoint (struct address_space *aspace,
15538 CORE_ADDR pc)
1aafd4da
UW
15539{
15540 int i;
15541
15542 for (i = 0; i < 2; i++)
15543 {
15544 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
15545 if (bp_tgt
15546 && breakpoint_address_match (bp_tgt->placed_address_space,
0d5ed153 15547 bp_tgt->reqstd_address,
6c95b8df 15548 aspace, pc))
ef370185 15549 return i;
1aafd4da
UW
15550 }
15551
ef370185
JB
15552 return -1;
15553}
15554
15555/* Check whether a software single-step breakpoint is inserted at
15556 PC. */
15557
15558int
15559single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15560 CORE_ADDR pc)
15561{
15562 return find_single_step_breakpoint (aspace, pc) >= 0;
1aafd4da
UW
15563}
15564
a96d9b2e
SDJ
15565/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15566 non-zero otherwise. */
15567static int
15568is_syscall_catchpoint_enabled (struct breakpoint *bp)
15569{
15570 if (syscall_catchpoint_p (bp)
15571 && bp->enable_state != bp_disabled
15572 && bp->enable_state != bp_call_disabled)
15573 return 1;
15574 else
15575 return 0;
15576}
15577
15578int
15579catch_syscall_enabled (void)
15580{
fa3064dd
YQ
15581 struct catch_syscall_inferior_data *inf_data
15582 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 15583
fa3064dd 15584 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
15585}
15586
15587int
15588catching_syscall_number (int syscall_number)
15589{
15590 struct breakpoint *bp;
15591
15592 ALL_BREAKPOINTS (bp)
15593 if (is_syscall_catchpoint_enabled (bp))
15594 {
be5c67c1
PA
15595 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15596
15597 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
15598 {
15599 int i, iter;
15600 for (i = 0;
be5c67c1 15601 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
15602 i++)
15603 if (syscall_number == iter)
15604 return 1;
15605 }
15606 else
15607 return 1;
15608 }
15609
15610 return 0;
15611}
15612
15613/* Complete syscall names. Used by "catch syscall". */
49c4e619 15614static VEC (char_ptr) *
a96d9b2e 15615catch_syscall_completer (struct cmd_list_element *cmd,
6f937416 15616 const char *text, const char *word)
a96d9b2e
SDJ
15617{
15618 const char **list = get_syscall_names ();
49c4e619 15619 VEC (char_ptr) *retlist
b45627a0 15620 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
cc59ec59 15621
c38eea1a
MS
15622 xfree (list);
15623 return retlist;
a96d9b2e
SDJ
15624}
15625
1042e4c0
SS
15626/* Tracepoint-specific operations. */
15627
15628/* Set tracepoint count to NUM. */
15629static void
15630set_tracepoint_count (int num)
15631{
15632 tracepoint_count = num;
4fa62494 15633 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15634}
15635
70221824 15636static void
1042e4c0
SS
15637trace_command (char *arg, int from_tty)
15638{
55aa24fb
SDJ
15639 struct breakpoint_ops *ops;
15640 const char *arg_cp = arg;
15641
15642 if (arg && probe_linespec_to_ops (&arg_cp))
15643 ops = &tracepoint_probe_breakpoint_ops;
15644 else
15645 ops = &tracepoint_breakpoint_ops;
15646
558a9d82
YQ
15647 create_breakpoint (get_current_arch (),
15648 arg,
15649 NULL, 0, NULL, 1 /* parse arg */,
15650 0 /* tempflag */,
15651 bp_tracepoint /* type_wanted */,
15652 0 /* Ignore count */,
15653 pending_break_support,
15654 ops,
15655 from_tty,
15656 1 /* enabled */,
15657 0 /* internal */, 0);
1042e4c0
SS
15658}
15659
70221824 15660static void
7a697b8d
SS
15661ftrace_command (char *arg, int from_tty)
15662{
558a9d82
YQ
15663 create_breakpoint (get_current_arch (),
15664 arg,
15665 NULL, 0, NULL, 1 /* parse arg */,
15666 0 /* tempflag */,
15667 bp_fast_tracepoint /* type_wanted */,
15668 0 /* Ignore count */,
15669 pending_break_support,
15670 &tracepoint_breakpoint_ops,
15671 from_tty,
15672 1 /* enabled */,
15673 0 /* internal */, 0);
0fb4aa4b
PA
15674}
15675
15676/* strace command implementation. Creates a static tracepoint. */
15677
70221824 15678static void
0fb4aa4b
PA
15679strace_command (char *arg, int from_tty)
15680{
983af33b
SDJ
15681 struct breakpoint_ops *ops;
15682
15683 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15684 or with a normal static tracepoint. */
15685 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15686 ops = &strace_marker_breakpoint_ops;
15687 else
15688 ops = &tracepoint_breakpoint_ops;
15689
558a9d82
YQ
15690 create_breakpoint (get_current_arch (),
15691 arg,
15692 NULL, 0, NULL, 1 /* parse arg */,
15693 0 /* tempflag */,
15694 bp_static_tracepoint /* type_wanted */,
15695 0 /* Ignore count */,
15696 pending_break_support,
15697 ops,
15698 from_tty,
15699 1 /* enabled */,
15700 0 /* internal */, 0);
7a697b8d
SS
15701}
15702
409873ef
SS
15703/* Set up a fake reader function that gets command lines from a linked
15704 list that was acquired during tracepoint uploading. */
15705
15706static struct uploaded_tp *this_utp;
3149d8c1 15707static int next_cmd;
409873ef
SS
15708
15709static char *
15710read_uploaded_action (void)
15711{
15712 char *rslt;
15713
3149d8c1 15714 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15715
3149d8c1 15716 next_cmd++;
409873ef
SS
15717
15718 return rslt;
15719}
15720
00bf0b85
SS
15721/* Given information about a tracepoint as recorded on a target (which
15722 can be either a live system or a trace file), attempt to create an
15723 equivalent GDB tracepoint. This is not a reliable process, since
15724 the target does not necessarily have all the information used when
15725 the tracepoint was originally defined. */
15726
d9b3f62e 15727struct tracepoint *
00bf0b85 15728create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15729{
409873ef 15730 char *addr_str, small_buf[100];
d9b3f62e 15731 struct tracepoint *tp;
fd9b8c24 15732
409873ef
SS
15733 if (utp->at_string)
15734 addr_str = utp->at_string;
15735 else
15736 {
15737 /* In the absence of a source location, fall back to raw
15738 address. Since there is no way to confirm that the address
15739 means the same thing as when the trace was started, warn the
15740 user. */
3e43a32a
MS
15741 warning (_("Uploaded tracepoint %d has no "
15742 "source location, using raw address"),
409873ef 15743 utp->number);
8c042590 15744 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15745 addr_str = small_buf;
15746 }
15747
15748 /* There's not much we can do with a sequence of bytecodes. */
15749 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15750 warning (_("Uploaded tracepoint %d condition "
15751 "has no source form, ignoring it"),
409873ef 15752 utp->number);
d5551862 15753
8cdf0e15 15754 if (!create_breakpoint (get_current_arch (),
409873ef 15755 addr_str,
e7e0cddf
SS
15756 utp->cond_string, -1, NULL,
15757 0 /* parse cond/thread */,
8cdf0e15 15758 0 /* tempflag */,
0fb4aa4b 15759 utp->type /* type_wanted */,
8cdf0e15
VP
15760 0 /* Ignore count */,
15761 pending_break_support,
348d480f 15762 &tracepoint_breakpoint_ops,
8cdf0e15 15763 0 /* from_tty */,
84f4c1fe 15764 utp->enabled /* enabled */,
44f238bb
PA
15765 0 /* internal */,
15766 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15767 return NULL;
15768
409873ef 15769 /* Get the tracepoint we just created. */
fd9b8c24
PA
15770 tp = get_tracepoint (tracepoint_count);
15771 gdb_assert (tp != NULL);
d5551862 15772
00bf0b85
SS
15773 if (utp->pass > 0)
15774 {
8c042590
PM
15775 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15776 tp->base.number);
00bf0b85 15777
409873ef 15778 trace_pass_command (small_buf, 0);
00bf0b85
SS
15779 }
15780
409873ef
SS
15781 /* If we have uploaded versions of the original commands, set up a
15782 special-purpose "reader" function and call the usual command line
15783 reader, then pass the result to the breakpoint command-setting
15784 function. */
3149d8c1 15785 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15786 {
409873ef 15787 struct command_line *cmd_list;
00bf0b85 15788
409873ef 15789 this_utp = utp;
3149d8c1 15790 next_cmd = 0;
d5551862 15791
409873ef
SS
15792 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15793
d9b3f62e 15794 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15795 }
3149d8c1
SS
15796 else if (!VEC_empty (char_ptr, utp->actions)
15797 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15798 warning (_("Uploaded tracepoint %d actions "
15799 "have no source form, ignoring them"),
409873ef 15800 utp->number);
00bf0b85 15801
f196051f
SS
15802 /* Copy any status information that might be available. */
15803 tp->base.hit_count = utp->hit_count;
15804 tp->traceframe_usage = utp->traceframe_usage;
15805
00bf0b85 15806 return tp;
d9b3f62e 15807}
00bf0b85 15808
1042e4c0
SS
15809/* Print information on tracepoint number TPNUM_EXP, or all if
15810 omitted. */
15811
15812static void
e5a67952 15813tracepoints_info (char *args, int from_tty)
1042e4c0 15814{
79a45e25 15815 struct ui_out *uiout = current_uiout;
e5a67952 15816 int num_printed;
1042e4c0 15817
e5a67952 15818 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15819
15820 if (num_printed == 0)
1042e4c0 15821 {
e5a67952 15822 if (args == NULL || *args == '\0')
d77f58be
SS
15823 ui_out_message (uiout, 0, "No tracepoints.\n");
15824 else
e5a67952 15825 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15826 }
ad443146
SS
15827
15828 default_collect_info ();
1042e4c0
SS
15829}
15830
4a64f543 15831/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15832 Not supported by all targets. */
15833static void
15834enable_trace_command (char *args, int from_tty)
15835{
15836 enable_command (args, from_tty);
15837}
15838
4a64f543 15839/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15840 Not supported by all targets. */
15841static void
15842disable_trace_command (char *args, int from_tty)
15843{
15844 disable_command (args, from_tty);
15845}
15846
4a64f543 15847/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15848static void
15849delete_trace_command (char *arg, int from_tty)
15850{
35df4500 15851 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15852
15853 dont_repeat ();
15854
15855 if (arg == 0)
15856 {
15857 int breaks_to_delete = 0;
15858
15859 /* Delete all breakpoints if no argument.
15860 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15861 have to be deleted with an explicit breakpoint number
15862 argument. */
1042e4c0 15863 ALL_TRACEPOINTS (b)
46c6471b 15864 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15865 {
15866 breaks_to_delete = 1;
15867 break;
15868 }
1042e4c0
SS
15869
15870 /* Ask user only if there are some breakpoints to delete. */
15871 if (!from_tty
15872 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15873 {
35df4500 15874 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15875 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15876 delete_breakpoint (b);
1042e4c0
SS
15877 }
15878 }
15879 else
51be5b68 15880 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15881}
15882
197f0a60
TT
15883/* Helper function for trace_pass_command. */
15884
15885static void
d9b3f62e 15886trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15887{
d9b3f62e 15888 tp->pass_count = count;
6f6484cd 15889 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15890 if (from_tty)
15891 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15892 tp->base.number, count);
197f0a60
TT
15893}
15894
1042e4c0
SS
15895/* Set passcount for tracepoint.
15896
15897 First command argument is passcount, second is tracepoint number.
15898 If tracepoint number omitted, apply to most recently defined.
15899 Also accepts special argument "all". */
15900
15901static void
15902trace_pass_command (char *args, int from_tty)
15903{
d9b3f62e 15904 struct tracepoint *t1;
1042e4c0 15905 unsigned int count;
1042e4c0
SS
15906
15907 if (args == 0 || *args == 0)
3e43a32a
MS
15908 error (_("passcount command requires an "
15909 "argument (count + optional TP num)"));
1042e4c0 15910
4a64f543 15911 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15912
529480d0 15913 args = skip_spaces (args);
1042e4c0
SS
15914 if (*args && strncasecmp (args, "all", 3) == 0)
15915 {
d9b3f62e
PA
15916 struct breakpoint *b;
15917
1042e4c0 15918 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15919 if (*args)
15920 error (_("Junk at end of arguments."));
1042e4c0 15921
d9b3f62e 15922 ALL_TRACEPOINTS (b)
197f0a60 15923 {
d9b3f62e 15924 t1 = (struct tracepoint *) b;
197f0a60
TT
15925 trace_pass_set_count (t1, count, from_tty);
15926 }
15927 }
15928 else if (*args == '\0')
1042e4c0 15929 {
5fa1d40e 15930 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15931 if (t1)
197f0a60
TT
15932 trace_pass_set_count (t1, count, from_tty);
15933 }
15934 else
15935 {
15936 struct get_number_or_range_state state;
15937
15938 init_number_or_range (&state, args);
15939 while (!state.finished)
1042e4c0 15940 {
5fa1d40e 15941 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15942 if (t1)
15943 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15944 }
15945 }
1042e4c0
SS
15946}
15947
d9b3f62e 15948struct tracepoint *
1042e4c0
SS
15949get_tracepoint (int num)
15950{
15951 struct breakpoint *t;
15952
15953 ALL_TRACEPOINTS (t)
15954 if (t->number == num)
d9b3f62e 15955 return (struct tracepoint *) t;
1042e4c0
SS
15956
15957 return NULL;
15958}
15959
d5551862
SS
15960/* Find the tracepoint with the given target-side number (which may be
15961 different from the tracepoint number after disconnecting and
15962 reconnecting). */
15963
d9b3f62e 15964struct tracepoint *
d5551862
SS
15965get_tracepoint_by_number_on_target (int num)
15966{
d9b3f62e 15967 struct breakpoint *b;
d5551862 15968
d9b3f62e
PA
15969 ALL_TRACEPOINTS (b)
15970 {
15971 struct tracepoint *t = (struct tracepoint *) b;
15972
15973 if (t->number_on_target == num)
15974 return t;
15975 }
d5551862
SS
15976
15977 return NULL;
15978}
15979
1042e4c0 15980/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15981 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15982 If the argument is missing, the most recent tracepoint
15983 (tracepoint_count) is returned. */
15984
d9b3f62e 15985struct tracepoint *
197f0a60 15986get_tracepoint_by_number (char **arg,
5fa1d40e 15987 struct get_number_or_range_state *state)
1042e4c0 15988{
1042e4c0
SS
15989 struct breakpoint *t;
15990 int tpnum;
15991 char *instring = arg == NULL ? NULL : *arg;
15992
197f0a60
TT
15993 if (state)
15994 {
15995 gdb_assert (!state->finished);
15996 tpnum = get_number_or_range (state);
15997 }
15998 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15999 tpnum = tracepoint_count;
1042e4c0 16000 else
197f0a60 16001 tpnum = get_number (arg);
1042e4c0
SS
16002
16003 if (tpnum <= 0)
16004 {
16005 if (instring && *instring)
16006 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
16007 instring);
16008 else
5fa1d40e 16009 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
16010 return NULL;
16011 }
16012
16013 ALL_TRACEPOINTS (t)
16014 if (t->number == tpnum)
16015 {
d9b3f62e 16016 return (struct tracepoint *) t;
1042e4c0
SS
16017 }
16018
1042e4c0
SS
16019 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
16020 return NULL;
16021}
16022
d9b3f62e
PA
16023void
16024print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
16025{
16026 if (b->thread != -1)
16027 fprintf_unfiltered (fp, " thread %d", b->thread);
16028
16029 if (b->task != 0)
16030 fprintf_unfiltered (fp, " task %d", b->task);
16031
16032 fprintf_unfiltered (fp, "\n");
16033}
16034
6149aea9
PA
16035/* Save information on user settable breakpoints (watchpoints, etc) to
16036 a new script file named FILENAME. If FILTER is non-NULL, call it
16037 on each breakpoint and only include the ones for which it returns
16038 non-zero. */
16039
1042e4c0 16040static void
6149aea9
PA
16041save_breakpoints (char *filename, int from_tty,
16042 int (*filter) (const struct breakpoint *))
1042e4c0
SS
16043{
16044 struct breakpoint *tp;
6149aea9 16045 int any = 0;
1042e4c0 16046 struct cleanup *cleanup;
a7bdde9e 16047 struct ui_file *fp;
6149aea9 16048 int extra_trace_bits = 0;
1042e4c0 16049
6149aea9
PA
16050 if (filename == 0 || *filename == 0)
16051 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
16052
16053 /* See if we have anything to save. */
6149aea9 16054 ALL_BREAKPOINTS (tp)
1042e4c0 16055 {
6149aea9 16056 /* Skip internal and momentary breakpoints. */
09d682a4 16057 if (!user_breakpoint_p (tp))
6149aea9
PA
16058 continue;
16059
16060 /* If we have a filter, only save the breakpoints it accepts. */
16061 if (filter && !filter (tp))
16062 continue;
16063
16064 any = 1;
16065
16066 if (is_tracepoint (tp))
16067 {
16068 extra_trace_bits = 1;
16069
16070 /* We can stop searching. */
16071 break;
16072 }
1042e4c0 16073 }
6149aea9
PA
16074
16075 if (!any)
1042e4c0 16076 {
6149aea9 16077 warning (_("Nothing to save."));
1042e4c0
SS
16078 return;
16079 }
16080
c718be47
PA
16081 filename = tilde_expand (filename);
16082 cleanup = make_cleanup (xfree, filename);
16083 fp = gdb_fopen (filename, "w");
059fb39f 16084 if (!fp)
6149aea9
PA
16085 error (_("Unable to open file '%s' for saving (%s)"),
16086 filename, safe_strerror (errno));
a7bdde9e 16087 make_cleanup_ui_file_delete (fp);
8bf6485c 16088
6149aea9
PA
16089 if (extra_trace_bits)
16090 save_trace_state_variables (fp);
8bf6485c 16091
6149aea9 16092 ALL_BREAKPOINTS (tp)
1042e4c0 16093 {
6149aea9 16094 /* Skip internal and momentary breakpoints. */
09d682a4 16095 if (!user_breakpoint_p (tp))
6149aea9 16096 continue;
8bf6485c 16097
6149aea9
PA
16098 /* If we have a filter, only save the breakpoints it accepts. */
16099 if (filter && !filter (tp))
16100 continue;
16101
348d480f 16102 tp->ops->print_recreate (tp, fp);
1042e4c0 16103
6149aea9
PA
16104 /* Note, we can't rely on tp->number for anything, as we can't
16105 assume the recreated breakpoint numbers will match. Use $bpnum
16106 instead. */
16107
16108 if (tp->cond_string)
16109 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
16110
16111 if (tp->ignore_count)
16112 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
16113
2d9442cc 16114 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 16115 {
a7bdde9e
VP
16116 volatile struct gdb_exception ex;
16117
6149aea9 16118 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 16119
79a45e25 16120 ui_out_redirect (current_uiout, fp);
14dba4b4 16121 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 16122 {
79a45e25 16123 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 16124 }
79a45e25 16125 ui_out_redirect (current_uiout, NULL);
1042e4c0 16126
a7bdde9e
VP
16127 if (ex.reason < 0)
16128 throw_exception (ex);
1042e4c0 16129
a7bdde9e 16130 fprintf_unfiltered (fp, " end\n");
1042e4c0 16131 }
6149aea9
PA
16132
16133 if (tp->enable_state == bp_disabled)
16134 fprintf_unfiltered (fp, "disable\n");
16135
16136 /* If this is a multi-location breakpoint, check if the locations
16137 should be individually disabled. Watchpoint locations are
16138 special, and not user visible. */
16139 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
16140 {
16141 struct bp_location *loc;
16142 int n = 1;
16143
16144 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
16145 if (!loc->enabled)
16146 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
16147 }
1042e4c0 16148 }
8bf6485c 16149
6149aea9 16150 if (extra_trace_bits && *default_collect)
8bf6485c
SS
16151 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
16152
1042e4c0 16153 if (from_tty)
6149aea9 16154 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 16155 do_cleanups (cleanup);
6149aea9
PA
16156}
16157
16158/* The `save breakpoints' command. */
16159
16160static void
16161save_breakpoints_command (char *args, int from_tty)
16162{
16163 save_breakpoints (args, from_tty, NULL);
16164}
16165
16166/* The `save tracepoints' command. */
16167
16168static void
16169save_tracepoints_command (char *args, int from_tty)
16170{
16171 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
16172}
16173
16174/* Create a vector of all tracepoints. */
16175
16176VEC(breakpoint_p) *
eeae04df 16177all_tracepoints (void)
1042e4c0
SS
16178{
16179 VEC(breakpoint_p) *tp_vec = 0;
16180 struct breakpoint *tp;
16181
16182 ALL_TRACEPOINTS (tp)
16183 {
16184 VEC_safe_push (breakpoint_p, tp_vec, tp);
16185 }
16186
16187 return tp_vec;
16188}
16189
c906108c 16190\f
4a64f543
MS
16191/* This help string is used for the break, hbreak, tbreak and thbreak
16192 commands. It is defined as a macro to prevent duplication.
16193 COMMAND should be a string constant containing the name of the
16194 command. */
31e2b00f 16195#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
16196command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
16197PROBE_MODIFIER shall be present if the command is to be placed in a\n\
16198probe point. Accepted values are `-probe' (for a generic, automatically\n\
16199guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
31e2b00f
AS
16200LOCATION may be a line number, function name, or \"*\" and an address.\n\
16201If a line number is specified, break at start of code for that line.\n\
16202If a function is specified, break at start of code for that function.\n\
16203If an address is specified, break at that exact address.\n\
dc10affe
PA
16204With no LOCATION, uses current execution address of the selected\n\
16205stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
16206\n\
16207THREADNUM is the number from \"info threads\".\n\
16208CONDITION is a boolean expression.\n\
16209\n\
d41c0fc8
PA
16210Multiple breakpoints at one place are permitted, and useful if their\n\
16211conditions are different.\n\
31e2b00f
AS
16212\n\
16213Do \"help breakpoints\" for info on other commands dealing with breakpoints."
16214
44feb3ce
TT
16215/* List of subcommands for "catch". */
16216static struct cmd_list_element *catch_cmdlist;
16217
16218/* List of subcommands for "tcatch". */
16219static struct cmd_list_element *tcatch_cmdlist;
16220
9ac4176b 16221void
44feb3ce 16222add_catch_command (char *name, char *docstring,
82ae6c8d 16223 cmd_sfunc_ftype *sfunc,
625e8578 16224 completer_ftype *completer,
44feb3ce
TT
16225 void *user_data_catch,
16226 void *user_data_tcatch)
16227{
16228 struct cmd_list_element *command;
16229
16230 command = add_cmd (name, class_breakpoint, NULL, docstring,
16231 &catch_cmdlist);
16232 set_cmd_sfunc (command, sfunc);
16233 set_cmd_context (command, user_data_catch);
a96d9b2e 16234 set_cmd_completer (command, completer);
44feb3ce
TT
16235
16236 command = add_cmd (name, class_breakpoint, NULL, docstring,
16237 &tcatch_cmdlist);
16238 set_cmd_sfunc (command, sfunc);
16239 set_cmd_context (command, user_data_tcatch);
a96d9b2e 16240 set_cmd_completer (command, completer);
44feb3ce
TT
16241}
16242
6c95b8df 16243static void
a79b8f6e 16244clear_syscall_counts (struct inferior *inf)
6c95b8df 16245{
fa3064dd
YQ
16246 struct catch_syscall_inferior_data *inf_data
16247 = get_catch_syscall_inferior_data (inf);
16248
16249 inf_data->total_syscalls_count = 0;
16250 inf_data->any_syscall_count = 0;
16251 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
16252}
16253
6149aea9
PA
16254static void
16255save_command (char *arg, int from_tty)
16256{
3e43a32a
MS
16257 printf_unfiltered (_("\"save\" must be followed by "
16258 "the name of a save subcommand.\n"));
635c7e8a 16259 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
6149aea9
PA
16260}
16261
84f4c1fe
PM
16262struct breakpoint *
16263iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
16264 void *data)
16265{
35df4500 16266 struct breakpoint *b, *b_tmp;
84f4c1fe 16267
35df4500 16268 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
16269 {
16270 if ((*callback) (b, data))
16271 return b;
16272 }
16273
16274 return NULL;
16275}
16276
0574c78f
GB
16277/* Zero if any of the breakpoint's locations could be a location where
16278 functions have been inlined, nonzero otherwise. */
16279
16280static int
16281is_non_inline_function (struct breakpoint *b)
16282{
16283 /* The shared library event breakpoint is set on the address of a
16284 non-inline function. */
16285 if (b->type == bp_shlib_event)
16286 return 1;
16287
16288 return 0;
16289}
16290
16291/* Nonzero if the specified PC cannot be a location where functions
16292 have been inlined. */
16293
16294int
09ac7c10
TT
16295pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
16296 const struct target_waitstatus *ws)
0574c78f
GB
16297{
16298 struct breakpoint *b;
16299 struct bp_location *bl;
16300
16301 ALL_BREAKPOINTS (b)
16302 {
16303 if (!is_non_inline_function (b))
16304 continue;
16305
16306 for (bl = b->loc; bl != NULL; bl = bl->next)
16307 {
16308 if (!bl->shlib_disabled
09ac7c10 16309 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
16310 return 1;
16311 }
16312 }
16313
16314 return 0;
16315}
16316
2f202fde
JK
16317/* Remove any references to OBJFILE which is going to be freed. */
16318
16319void
16320breakpoint_free_objfile (struct objfile *objfile)
16321{
16322 struct bp_location **locp, *loc;
16323
16324 ALL_BP_LOCATIONS (loc, locp)
16325 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
16326 loc->symtab = NULL;
16327}
16328
2060206e
PA
16329void
16330initialize_breakpoint_ops (void)
16331{
16332 static int initialized = 0;
16333
16334 struct breakpoint_ops *ops;
16335
16336 if (initialized)
16337 return;
16338 initialized = 1;
16339
16340 /* The breakpoint_ops structure to be inherit by all kinds of
16341 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16342 internal and momentary breakpoints, etc.). */
16343 ops = &bkpt_base_breakpoint_ops;
16344 *ops = base_breakpoint_ops;
16345 ops->re_set = bkpt_re_set;
16346 ops->insert_location = bkpt_insert_location;
16347 ops->remove_location = bkpt_remove_location;
16348 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
16349 ops->create_sals_from_address = bkpt_create_sals_from_address;
16350 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16351 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
16352
16353 /* The breakpoint_ops structure to be used in regular breakpoints. */
16354 ops = &bkpt_breakpoint_ops;
16355 *ops = bkpt_base_breakpoint_ops;
16356 ops->re_set = bkpt_re_set;
16357 ops->resources_needed = bkpt_resources_needed;
16358 ops->print_it = bkpt_print_it;
16359 ops->print_mention = bkpt_print_mention;
16360 ops->print_recreate = bkpt_print_recreate;
16361
16362 /* Ranged breakpoints. */
16363 ops = &ranged_breakpoint_ops;
16364 *ops = bkpt_breakpoint_ops;
16365 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16366 ops->resources_needed = resources_needed_ranged_breakpoint;
16367 ops->print_it = print_it_ranged_breakpoint;
16368 ops->print_one = print_one_ranged_breakpoint;
16369 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16370 ops->print_mention = print_mention_ranged_breakpoint;
16371 ops->print_recreate = print_recreate_ranged_breakpoint;
16372
16373 /* Internal breakpoints. */
16374 ops = &internal_breakpoint_ops;
16375 *ops = bkpt_base_breakpoint_ops;
16376 ops->re_set = internal_bkpt_re_set;
16377 ops->check_status = internal_bkpt_check_status;
16378 ops->print_it = internal_bkpt_print_it;
16379 ops->print_mention = internal_bkpt_print_mention;
16380
16381 /* Momentary breakpoints. */
16382 ops = &momentary_breakpoint_ops;
16383 *ops = bkpt_base_breakpoint_ops;
16384 ops->re_set = momentary_bkpt_re_set;
16385 ops->check_status = momentary_bkpt_check_status;
16386 ops->print_it = momentary_bkpt_print_it;
16387 ops->print_mention = momentary_bkpt_print_mention;
16388
e2e4d78b
JK
16389 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16390 ops = &longjmp_breakpoint_ops;
16391 *ops = momentary_breakpoint_ops;
16392 ops->dtor = longjmp_bkpt_dtor;
16393
55aa24fb
SDJ
16394 /* Probe breakpoints. */
16395 ops = &bkpt_probe_breakpoint_ops;
16396 *ops = bkpt_breakpoint_ops;
16397 ops->insert_location = bkpt_probe_insert_location;
16398 ops->remove_location = bkpt_probe_remove_location;
16399 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16400 ops->decode_linespec = bkpt_probe_decode_linespec;
16401
2060206e
PA
16402 /* Watchpoints. */
16403 ops = &watchpoint_breakpoint_ops;
16404 *ops = base_breakpoint_ops;
3a5c3e22 16405 ops->dtor = dtor_watchpoint;
2060206e
PA
16406 ops->re_set = re_set_watchpoint;
16407 ops->insert_location = insert_watchpoint;
16408 ops->remove_location = remove_watchpoint;
16409 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16410 ops->check_status = check_status_watchpoint;
16411 ops->resources_needed = resources_needed_watchpoint;
16412 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16413 ops->print_it = print_it_watchpoint;
16414 ops->print_mention = print_mention_watchpoint;
16415 ops->print_recreate = print_recreate_watchpoint;
427cd150 16416 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16417
16418 /* Masked watchpoints. */
16419 ops = &masked_watchpoint_breakpoint_ops;
16420 *ops = watchpoint_breakpoint_ops;
16421 ops->insert_location = insert_masked_watchpoint;
16422 ops->remove_location = remove_masked_watchpoint;
16423 ops->resources_needed = resources_needed_masked_watchpoint;
16424 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16425 ops->print_it = print_it_masked_watchpoint;
16426 ops->print_one_detail = print_one_detail_masked_watchpoint;
16427 ops->print_mention = print_mention_masked_watchpoint;
16428 ops->print_recreate = print_recreate_masked_watchpoint;
16429
16430 /* Tracepoints. */
16431 ops = &tracepoint_breakpoint_ops;
16432 *ops = base_breakpoint_ops;
16433 ops->re_set = tracepoint_re_set;
16434 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16435 ops->print_one_detail = tracepoint_print_one_detail;
16436 ops->print_mention = tracepoint_print_mention;
16437 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
16438 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16439 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16440 ops->decode_linespec = tracepoint_decode_linespec;
16441
55aa24fb
SDJ
16442 /* Probe tracepoints. */
16443 ops = &tracepoint_probe_breakpoint_ops;
16444 *ops = tracepoint_breakpoint_ops;
16445 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16446 ops->decode_linespec = tracepoint_probe_decode_linespec;
16447
983af33b
SDJ
16448 /* Static tracepoints with marker (`-m'). */
16449 ops = &strace_marker_breakpoint_ops;
16450 *ops = tracepoint_breakpoint_ops;
16451 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16452 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16453 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
16454
16455 /* Fork catchpoints. */
16456 ops = &catch_fork_breakpoint_ops;
16457 *ops = base_breakpoint_ops;
16458 ops->insert_location = insert_catch_fork;
16459 ops->remove_location = remove_catch_fork;
16460 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16461 ops->print_it = print_it_catch_fork;
16462 ops->print_one = print_one_catch_fork;
16463 ops->print_mention = print_mention_catch_fork;
16464 ops->print_recreate = print_recreate_catch_fork;
16465
16466 /* Vfork catchpoints. */
16467 ops = &catch_vfork_breakpoint_ops;
16468 *ops = base_breakpoint_ops;
16469 ops->insert_location = insert_catch_vfork;
16470 ops->remove_location = remove_catch_vfork;
16471 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16472 ops->print_it = print_it_catch_vfork;
16473 ops->print_one = print_one_catch_vfork;
16474 ops->print_mention = print_mention_catch_vfork;
16475 ops->print_recreate = print_recreate_catch_vfork;
16476
16477 /* Exec catchpoints. */
16478 ops = &catch_exec_breakpoint_ops;
16479 *ops = base_breakpoint_ops;
16480 ops->dtor = dtor_catch_exec;
16481 ops->insert_location = insert_catch_exec;
16482 ops->remove_location = remove_catch_exec;
16483 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16484 ops->print_it = print_it_catch_exec;
16485 ops->print_one = print_one_catch_exec;
16486 ops->print_mention = print_mention_catch_exec;
16487 ops->print_recreate = print_recreate_catch_exec;
16488
16489 /* Syscall catchpoints. */
16490 ops = &catch_syscall_breakpoint_ops;
16491 *ops = base_breakpoint_ops;
16492 ops->dtor = dtor_catch_syscall;
16493 ops->insert_location = insert_catch_syscall;
16494 ops->remove_location = remove_catch_syscall;
16495 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16496 ops->print_it = print_it_catch_syscall;
16497 ops->print_one = print_one_catch_syscall;
16498 ops->print_mention = print_mention_catch_syscall;
16499 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
16500
16501 /* Solib-related catchpoints. */
16502 ops = &catch_solib_breakpoint_ops;
16503 *ops = base_breakpoint_ops;
16504 ops->dtor = dtor_catch_solib;
16505 ops->insert_location = insert_catch_solib;
16506 ops->remove_location = remove_catch_solib;
16507 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16508 ops->check_status = check_status_catch_solib;
16509 ops->print_it = print_it_catch_solib;
16510 ops->print_one = print_one_catch_solib;
16511 ops->print_mention = print_mention_catch_solib;
16512 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16513
16514 ops = &dprintf_breakpoint_ops;
16515 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16516 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16517 ops->resources_needed = bkpt_resources_needed;
16518 ops->print_it = bkpt_print_it;
16519 ops->print_mention = bkpt_print_mention;
2d9442cc 16520 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16521 ops->after_condition_true = dprintf_after_condition_true;
cd1608cc 16522 ops->breakpoint_hit = dprintf_breakpoint_hit;
2060206e
PA
16523}
16524
8bfd80db
YQ
16525/* Chain containing all defined "enable breakpoint" subcommands. */
16526
16527static struct cmd_list_element *enablebreaklist = NULL;
16528
c906108c 16529void
fba45db2 16530_initialize_breakpoint (void)
c906108c
SS
16531{
16532 struct cmd_list_element *c;
16533
2060206e
PA
16534 initialize_breakpoint_ops ();
16535
84acb35a 16536 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16537 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
6c95b8df 16538 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 16539 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16540
55aa24fb
SDJ
16541 breakpoint_objfile_key
16542 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16543
fa3064dd 16544 catch_syscall_inferior_data
8e260fc0
TT
16545 = register_inferior_data_with_cleanup (NULL,
16546 catch_syscall_inferior_data_cleanup);
fa3064dd 16547
c906108c
SS
16548 breakpoint_chain = 0;
16549 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16550 before a breakpoint is set. */
16551 breakpoint_count = 0;
16552
1042e4c0
SS
16553 tracepoint_count = 0;
16554
1bedd215
AC
16555 add_com ("ignore", class_breakpoint, ignore_command, _("\
16556Set ignore-count of breakpoint number N to COUNT.\n\
16557Usage is `ignore N COUNT'."));
c906108c 16558 if (xdb_commands)
c5aa993b 16559 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 16560
1bedd215
AC
16561 add_com ("commands", class_breakpoint, commands_command, _("\
16562Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16563Give breakpoint number as argument after \"commands\".\n\
16564With no argument, the targeted breakpoint is the last one set.\n\
16565The commands themselves follow starting on the next line.\n\
16566Type a line containing \"end\" to indicate the end of them.\n\
16567Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16568then no output is printed when it is hit, except what the commands print."));
c906108c 16569
d55637df 16570 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16571Specify breakpoint number N to break only if COND is true.\n\
c906108c 16572Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16573expression to be evaluated whenever breakpoint N is reached."));
d55637df 16574 set_cmd_completer (c, condition_completer);
c906108c 16575
1bedd215 16576 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16577Set a temporary breakpoint.\n\
c906108c
SS
16578Like \"break\" except the breakpoint is only temporary,\n\
16579so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16580by using \"enable delete\" on the breakpoint number.\n\
16581\n"
16582BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16583 set_cmd_completer (c, location_completer);
c94fdfd0 16584
1bedd215 16585 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16586Set a hardware assisted breakpoint.\n\
c906108c 16587Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16588some target hardware may not have this support.\n\
16589\n"
16590BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16591 set_cmd_completer (c, location_completer);
c906108c 16592
1bedd215 16593 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16594Set a temporary hardware assisted breakpoint.\n\
c906108c 16595Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16596so it will be deleted when hit.\n\
16597\n"
16598BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16599 set_cmd_completer (c, location_completer);
c906108c 16600
1bedd215
AC
16601 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16602Enable some breakpoints.\n\
c906108c
SS
16603Give breakpoint numbers (separated by spaces) as arguments.\n\
16604With no subcommand, breakpoints are enabled until you command otherwise.\n\
16605This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16606With a subcommand you can enable temporarily."),
c906108c
SS
16607 &enablelist, "enable ", 1, &cmdlist);
16608 if (xdb_commands)
1bedd215
AC
16609 add_com ("ab", class_breakpoint, enable_command, _("\
16610Enable some breakpoints.\n\
c906108c
SS
16611Give breakpoint numbers (separated by spaces) as arguments.\n\
16612With no subcommand, breakpoints are enabled until you command otherwise.\n\
16613This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16614With a subcommand you can enable temporarily."));
c906108c
SS
16615
16616 add_com_alias ("en", "enable", class_breakpoint, 1);
16617
84951ab5 16618 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16619Enable some breakpoints.\n\
c906108c
SS
16620Give breakpoint numbers (separated by spaces) as arguments.\n\
16621This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16622May be abbreviated to simply \"enable\".\n"),
c5aa993b 16623 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16624
1a966eab
AC
16625 add_cmd ("once", no_class, enable_once_command, _("\
16626Enable breakpoints for one hit. Give breakpoint numbers.\n\
16627If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16628 &enablebreaklist);
16629
1a966eab
AC
16630 add_cmd ("delete", no_class, enable_delete_command, _("\
16631Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16632If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16633 &enablebreaklist);
16634
816338b5
SS
16635 add_cmd ("count", no_class, enable_count_command, _("\
16636Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16637If a breakpoint is hit while enabled in this fashion,\n\
16638the count is decremented; when it reaches zero, the breakpoint is disabled."),
16639 &enablebreaklist);
16640
1a966eab
AC
16641 add_cmd ("delete", no_class, enable_delete_command, _("\
16642Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16643If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16644 &enablelist);
16645
1a966eab
AC
16646 add_cmd ("once", no_class, enable_once_command, _("\
16647Enable breakpoints for one hit. Give breakpoint numbers.\n\
16648If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16649 &enablelist);
16650
16651 add_cmd ("count", no_class, enable_count_command, _("\
16652Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16653If a breakpoint is hit while enabled in this fashion,\n\
16654the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16655 &enablelist);
16656
1bedd215
AC
16657 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16658Disable some breakpoints.\n\
c906108c
SS
16659Arguments are breakpoint numbers with spaces in between.\n\
16660To disable all breakpoints, give no argument.\n\
64b9b334 16661A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16662 &disablelist, "disable ", 1, &cmdlist);
16663 add_com_alias ("dis", "disable", class_breakpoint, 1);
16664 add_com_alias ("disa", "disable", class_breakpoint, 1);
16665 if (xdb_commands)
1bedd215
AC
16666 add_com ("sb", class_breakpoint, disable_command, _("\
16667Disable some breakpoints.\n\
c906108c
SS
16668Arguments are breakpoint numbers with spaces in between.\n\
16669To disable all breakpoints, give no argument.\n\
64b9b334 16670A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 16671
1a966eab
AC
16672 add_cmd ("breakpoints", class_alias, disable_command, _("\
16673Disable some breakpoints.\n\
c906108c
SS
16674Arguments are breakpoint numbers with spaces in between.\n\
16675To disable all breakpoints, give no argument.\n\
64b9b334 16676A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16677This command may be abbreviated \"disable\"."),
c906108c
SS
16678 &disablelist);
16679
1bedd215
AC
16680 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16681Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16682Arguments are breakpoint numbers with spaces in between.\n\
16683To delete all breakpoints, give no argument.\n\
16684\n\
16685Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16686The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16687 &deletelist, "delete ", 1, &cmdlist);
16688 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16689 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16690 if (xdb_commands)
1bedd215
AC
16691 add_com ("db", class_breakpoint, delete_command, _("\
16692Delete some breakpoints.\n\
c906108c 16693Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 16694To delete all breakpoints, give no argument.\n"));
c906108c 16695
1a966eab
AC
16696 add_cmd ("breakpoints", class_alias, delete_command, _("\
16697Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16698Arguments are breakpoint numbers with spaces in between.\n\
16699To delete all breakpoints, give no argument.\n\
1a966eab 16700This command may be abbreviated \"delete\"."),
c906108c
SS
16701 &deletelist);
16702
1bedd215
AC
16703 add_com ("clear", class_breakpoint, clear_command, _("\
16704Clear breakpoint at specified line or function.\n\
c906108c
SS
16705Argument may be line number, function name, or \"*\" and an address.\n\
16706If line number is specified, all breakpoints in that line are cleared.\n\
16707If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16708If an address is specified, breakpoints at that address are cleared.\n\
16709\n\
16710With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16711is executing in.\n\
16712\n\
1bedd215 16713See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16714 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16715
1bedd215 16716 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16717Set breakpoint at specified line or function.\n"
16718BREAK_ARGS_HELP ("break")));
5ba2abeb 16719 set_cmd_completer (c, location_completer);
c94fdfd0 16720
c906108c
SS
16721 add_com_alias ("b", "break", class_run, 1);
16722 add_com_alias ("br", "break", class_run, 1);
16723 add_com_alias ("bre", "break", class_run, 1);
16724 add_com_alias ("brea", "break", class_run, 1);
16725
7681d515
PM
16726 if (xdb_commands)
16727 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
16728
16729 if (dbx_commands)
16730 {
1bedd215
AC
16731 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16732Break in function/address or break at a line in the current file."),
c5aa993b
JM
16733 &stoplist, "stop ", 1, &cmdlist);
16734 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16735 _("Break in function or address."), &stoplist);
c5aa993b 16736 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16737 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16738 add_com ("status", class_info, breakpoints_info, _("\
16739Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16740The \"Type\" column indicates one of:\n\
16741\tbreakpoint - normal breakpoint\n\
16742\twatchpoint - watchpoint\n\
16743The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16744the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16745breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16746address and file/line number respectively.\n\
16747\n\
16748Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16749are set to the address of the last breakpoint listed unless the command\n\
16750is prefixed with \"server \".\n\n\
c906108c 16751Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16752breakpoint set."));
c906108c
SS
16753 }
16754
1bedd215 16755 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16756Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16757The \"Type\" column indicates one of:\n\
16758\tbreakpoint - normal breakpoint\n\
16759\twatchpoint - watchpoint\n\
16760The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16761the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16762breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16763address and file/line number respectively.\n\
16764\n\
16765Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16766are set to the address of the last breakpoint listed unless the command\n\
16767is prefixed with \"server \".\n\n\
c906108c 16768Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16769breakpoint set."));
c906108c 16770
6b04bdb7
MS
16771 add_info_alias ("b", "breakpoints", 1);
16772
c906108c 16773 if (xdb_commands)
1bedd215
AC
16774 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16775Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16776The \"Type\" column indicates one of:\n\
16777\tbreakpoint - normal breakpoint\n\
16778\twatchpoint - watchpoint\n\
16779The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16780the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16781breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16782address and file/line number respectively.\n\
16783\n\
16784Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16785are set to the address of the last breakpoint listed unless the command\n\
16786is prefixed with \"server \".\n\n\
c906108c 16787Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16788breakpoint set."));
c906108c 16789
1a966eab
AC
16790 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16791Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16792The \"Type\" column indicates one of:\n\
16793\tbreakpoint - normal breakpoint\n\
16794\twatchpoint - watchpoint\n\
16795\tlongjmp - internal breakpoint used to step through longjmp()\n\
16796\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16797\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16798\tfinish - internal breakpoint used by the \"finish\" command\n\
16799The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16800the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16801breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16802address and file/line number respectively.\n\
16803\n\
16804Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16805are set to the address of the last breakpoint listed unless the command\n\
16806is prefixed with \"server \".\n\n\
c906108c 16807Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16808breakpoint set."),
c906108c
SS
16809 &maintenanceinfolist);
16810
44feb3ce
TT
16811 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16812Set catchpoints to catch events."),
16813 &catch_cmdlist, "catch ",
16814 0/*allow-unknown*/, &cmdlist);
16815
16816 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16817Set temporary catchpoints to catch events."),
16818 &tcatch_cmdlist, "tcatch ",
16819 0/*allow-unknown*/, &cmdlist);
16820
44feb3ce
TT
16821 add_catch_command ("fork", _("Catch calls to fork."),
16822 catch_fork_command_1,
a96d9b2e 16823 NULL,
44feb3ce
TT
16824 (void *) (uintptr_t) catch_fork_permanent,
16825 (void *) (uintptr_t) catch_fork_temporary);
16826 add_catch_command ("vfork", _("Catch calls to vfork."),
16827 catch_fork_command_1,
a96d9b2e 16828 NULL,
44feb3ce
TT
16829 (void *) (uintptr_t) catch_vfork_permanent,
16830 (void *) (uintptr_t) catch_vfork_temporary);
16831 add_catch_command ("exec", _("Catch calls to exec."),
16832 catch_exec_command_1,
a96d9b2e
SDJ
16833 NULL,
16834 CATCH_PERMANENT,
16835 CATCH_TEMPORARY);
edcc5120
TT
16836 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16837Usage: catch load [REGEX]\n\
16838If REGEX is given, only stop for libraries matching the regular expression."),
16839 catch_load_command_1,
16840 NULL,
16841 CATCH_PERMANENT,
16842 CATCH_TEMPORARY);
16843 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16844Usage: catch unload [REGEX]\n\
16845If REGEX is given, only stop for libraries matching the regular expression."),
16846 catch_unload_command_1,
16847 NULL,
16848 CATCH_PERMANENT,
16849 CATCH_TEMPORARY);
a96d9b2e
SDJ
16850 add_catch_command ("syscall", _("\
16851Catch system calls by their names and/or numbers.\n\
16852Arguments say which system calls to catch. If no arguments\n\
16853are given, every system call will be caught.\n\
16854Arguments, if given, should be one or more system call names\n\
16855(if your system supports that), or system call numbers."),
16856 catch_syscall_command_1,
16857 catch_syscall_completer,
44feb3ce
TT
16858 CATCH_PERMANENT,
16859 CATCH_TEMPORARY);
c5aa993b 16860
1bedd215
AC
16861 c = add_com ("watch", class_breakpoint, watch_command, _("\
16862Set a watchpoint for an expression.\n\
06a64a0b 16863Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16864A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16865an expression changes.\n\
16866If -l or -location is given, this evaluates EXPRESSION and watches\n\
16867the memory to which it refers."));
65d12d83 16868 set_cmd_completer (c, expression_completer);
c906108c 16869
1bedd215
AC
16870 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16871Set a read watchpoint for an expression.\n\
06a64a0b 16872Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16873A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16874an expression is read.\n\
16875If -l or -location is given, this evaluates EXPRESSION and watches\n\
16876the memory to which it refers."));
65d12d83 16877 set_cmd_completer (c, expression_completer);
c906108c 16878
1bedd215
AC
16879 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16880Set a watchpoint for an expression.\n\
06a64a0b 16881Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16882A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16883an expression is either read or written.\n\
16884If -l or -location is given, this evaluates EXPRESSION and watches\n\
16885the memory to which it refers."));
65d12d83 16886 set_cmd_completer (c, expression_completer);
c906108c 16887
d77f58be 16888 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16889Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16890
920d2a44
AC
16891 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16892 respond to changes - contrary to the description. */
85c07804
AC
16893 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16894 &can_use_hw_watchpoints, _("\
16895Set debugger's willingness to use watchpoint hardware."), _("\
16896Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16897If zero, gdb will not use hardware for new watchpoints, even if\n\
16898such is available. (However, any hardware watchpoints that were\n\
16899created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16900hardware.)"),
16901 NULL,
920d2a44 16902 show_can_use_hw_watchpoints,
85c07804 16903 &setlist, &showlist);
c906108c
SS
16904
16905 can_use_hw_watchpoints = 1;
fa8d40ab 16906
1042e4c0
SS
16907 /* Tracepoint manipulation commands. */
16908
16909 c = add_com ("trace", class_breakpoint, trace_command, _("\
16910Set a tracepoint at specified line or function.\n\
16911\n"
16912BREAK_ARGS_HELP ("trace") "\n\
16913Do \"help tracepoints\" for info on other tracepoint commands."));
16914 set_cmd_completer (c, location_completer);
16915
16916 add_com_alias ("tp", "trace", class_alias, 0);
16917 add_com_alias ("tr", "trace", class_alias, 1);
16918 add_com_alias ("tra", "trace", class_alias, 1);
16919 add_com_alias ("trac", "trace", class_alias, 1);
16920
7a697b8d
SS
16921 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16922Set a fast tracepoint at specified line or function.\n\
16923\n"
16924BREAK_ARGS_HELP ("ftrace") "\n\
16925Do \"help tracepoints\" for info on other tracepoint commands."));
16926 set_cmd_completer (c, location_completer);
16927
0fb4aa4b
PA
16928 c = add_com ("strace", class_breakpoint, strace_command, _("\
16929Set a static tracepoint at specified line, function or marker.\n\
16930\n\
16931strace [LOCATION] [if CONDITION]\n\
16932LOCATION may be a line number, function name, \"*\" and an address,\n\
16933or -m MARKER_ID.\n\
16934If a line number is specified, probe the marker at start of code\n\
16935for that line. If a function is specified, probe the marker at start\n\
16936of code for that function. If an address is specified, probe the marker\n\
16937at that exact address. If a marker id is specified, probe the marker\n\
16938with that name. With no LOCATION, uses current execution address of\n\
16939the selected stack frame.\n\
16940Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16941This collects arbitrary user data passed in the probe point call to the\n\
16942tracing library. You can inspect it when analyzing the trace buffer,\n\
16943by printing the $_sdata variable like any other convenience variable.\n\
16944\n\
16945CONDITION is a boolean expression.\n\
16946\n\
d41c0fc8
PA
16947Multiple tracepoints at one place are permitted, and useful if their\n\
16948conditions are different.\n\
0fb4aa4b
PA
16949\n\
16950Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16951Do \"help tracepoints\" for info on other tracepoint commands."));
16952 set_cmd_completer (c, location_completer);
16953
1042e4c0 16954 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16955Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16956Convenience variable \"$tpnum\" contains the number of the\n\
16957last tracepoint set."));
16958
16959 add_info_alias ("tp", "tracepoints", 1);
16960
16961 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16962Delete specified tracepoints.\n\
16963Arguments are tracepoint numbers, separated by spaces.\n\
16964No argument means delete all tracepoints."),
16965 &deletelist);
7e20dfcd 16966 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16967
16968 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16969Disable specified tracepoints.\n\
16970Arguments are tracepoint numbers, separated by spaces.\n\
16971No argument means disable all tracepoints."),
16972 &disablelist);
16973 deprecate_cmd (c, "disable");
16974
16975 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16976Enable specified tracepoints.\n\
16977Arguments are tracepoint numbers, separated by spaces.\n\
16978No argument means enable all tracepoints."),
16979 &enablelist);
16980 deprecate_cmd (c, "enable");
16981
16982 add_com ("passcount", class_trace, trace_pass_command, _("\
16983Set the passcount for a tracepoint.\n\
16984The trace will end when the tracepoint has been passed 'count' times.\n\
16985Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16986if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16987
6149aea9
PA
16988 add_prefix_cmd ("save", class_breakpoint, save_command,
16989 _("Save breakpoint definitions as a script."),
16990 &save_cmdlist, "save ",
16991 0/*allow-unknown*/, &cmdlist);
16992
16993 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16994Save current breakpoint definitions as a script.\n\
cce7e648 16995This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16996catchpoints, tracepoints). Use the 'source' command in another debug\n\
16997session to restore them."),
16998 &save_cmdlist);
16999 set_cmd_completer (c, filename_completer);
17000
17001 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 17002Save current tracepoint definitions as a script.\n\
6149aea9
PA
17003Use the 'source' command in another debug session to restore them."),
17004 &save_cmdlist);
1042e4c0
SS
17005 set_cmd_completer (c, filename_completer);
17006
6149aea9
PA
17007 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
17008 deprecate_cmd (c, "save tracepoints");
17009
1bedd215 17010 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
17011Breakpoint specific settings\n\
17012Configure various breakpoint-specific variables such as\n\
1bedd215 17013pending breakpoint behavior"),
fa8d40ab
JJ
17014 &breakpoint_set_cmdlist, "set breakpoint ",
17015 0/*allow-unknown*/, &setlist);
1bedd215 17016 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
17017Breakpoint specific settings\n\
17018Configure various breakpoint-specific variables such as\n\
1bedd215 17019pending breakpoint behavior"),
fa8d40ab
JJ
17020 &breakpoint_show_cmdlist, "show breakpoint ",
17021 0/*allow-unknown*/, &showlist);
17022
7915a72c
AC
17023 add_setshow_auto_boolean_cmd ("pending", no_class,
17024 &pending_break_support, _("\
17025Set debugger's behavior regarding pending breakpoints."), _("\
17026Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
17027If on, an unrecognized breakpoint location will cause gdb to create a\n\
17028pending breakpoint. If off, an unrecognized breakpoint location results in\n\
17029an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 17030user-query to see if a pending breakpoint should be created."),
2c5b56ce 17031 NULL,
920d2a44 17032 show_pending_break_support,
6e1d7d6c
AC
17033 &breakpoint_set_cmdlist,
17034 &breakpoint_show_cmdlist);
fa8d40ab
JJ
17035
17036 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
17037
17038 add_setshow_boolean_cmd ("auto-hw", no_class,
17039 &automatic_hardware_breakpoints, _("\
17040Set automatic usage of hardware breakpoints."), _("\
17041Show automatic usage of hardware breakpoints."), _("\
17042If set, the debugger will automatically use hardware breakpoints for\n\
17043breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
17044a warning will be emitted for such breakpoints."),
17045 NULL,
17046 show_automatic_hardware_breakpoints,
17047 &breakpoint_set_cmdlist,
17048 &breakpoint_show_cmdlist);
74960c60 17049
a25a5a45
PA
17050 add_setshow_boolean_cmd ("always-inserted", class_support,
17051 &always_inserted_mode, _("\
74960c60
VP
17052Set mode for inserting breakpoints."), _("\
17053Show mode for inserting breakpoints."), _("\
a25a5a45
PA
17054When this mode is on, breakpoints are inserted immediately as soon as\n\
17055they're created, kept inserted even when execution stops, and removed\n\
17056only when the user deletes them. When this mode is off (the default),\n\
17057breakpoints are inserted only when execution continues, and removed\n\
17058when execution stops."),
72d0e2c5
YQ
17059 NULL,
17060 &show_always_inserted_mode,
17061 &breakpoint_set_cmdlist,
17062 &breakpoint_show_cmdlist);
f1310107 17063
b775012e
LM
17064 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
17065 condition_evaluation_enums,
17066 &condition_evaluation_mode_1, _("\
17067Set mode of breakpoint condition evaluation."), _("\
17068Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 17069When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
17070evaluated on the host's side by GDB. When it is set to \"target\",\n\
17071breakpoint conditions will be downloaded to the target (if the target\n\
17072supports such feature) and conditions will be evaluated on the target's side.\n\
17073If this is set to \"auto\" (default), this will be automatically set to\n\
17074\"target\" if it supports condition evaluation, otherwise it will\n\
17075be set to \"gdb\""),
17076 &set_condition_evaluation_mode,
17077 &show_condition_evaluation_mode,
17078 &breakpoint_set_cmdlist,
17079 &breakpoint_show_cmdlist);
17080
f1310107
TJB
17081 add_com ("break-range", class_breakpoint, break_range_command, _("\
17082Set a breakpoint for an address range.\n\
17083break-range START-LOCATION, END-LOCATION\n\
17084where START-LOCATION and END-LOCATION can be one of the following:\n\
17085 LINENUM, for that line in the current file,\n\
17086 FILE:LINENUM, for that line in that file,\n\
17087 +OFFSET, for that number of lines after the current line\n\
17088 or the start of the range\n\
17089 FUNCTION, for the first line in that function,\n\
17090 FILE:FUNCTION, to distinguish among like-named static functions.\n\
17091 *ADDRESS, for the instruction at that address.\n\
17092\n\
17093The breakpoint will stop execution of the inferior whenever it executes\n\
17094an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
17095range (including START-LOCATION and END-LOCATION)."));
17096
e7e0cddf
SS
17097 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
17098Set a dynamic printf at specified line or function.\n\
17099dprintf location,format string,arg1,arg2,...\n\
17100location may be a line number, function name, or \"*\" and an address.\n\
17101If a line number is specified, break at start of code for that line.\n\
0e4777df 17102If a function is specified, break at start of code for that function."));
e7e0cddf
SS
17103 set_cmd_completer (c, location_completer);
17104
17105 add_setshow_enum_cmd ("dprintf-style", class_support,
17106 dprintf_style_enums, &dprintf_style, _("\
17107Set the style of usage for dynamic printf."), _("\
17108Show the style of usage for dynamic printf."), _("\
17109This setting chooses how GDB will do a dynamic printf.\n\
17110If the value is \"gdb\", then the printing is done by GDB to its own\n\
17111console, as with the \"printf\" command.\n\
17112If the value is \"call\", the print is done by calling a function in your\n\
17113program; by default printf(), but you can choose a different function or\n\
17114output stream by setting dprintf-function and dprintf-channel."),
17115 update_dprintf_commands, NULL,
17116 &setlist, &showlist);
17117
17118 dprintf_function = xstrdup ("printf");
17119 add_setshow_string_cmd ("dprintf-function", class_support,
17120 &dprintf_function, _("\
17121Set the function to use for dynamic printf"), _("\
17122Show the function to use for dynamic printf"), NULL,
17123 update_dprintf_commands, NULL,
17124 &setlist, &showlist);
17125
17126 dprintf_channel = xstrdup ("");
17127 add_setshow_string_cmd ("dprintf-channel", class_support,
17128 &dprintf_channel, _("\
17129Set the channel to use for dynamic printf"), _("\
17130Show the channel to use for dynamic printf"), NULL,
17131 update_dprintf_commands, NULL,
17132 &setlist, &showlist);
17133
d3ce09f5
SS
17134 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
17135 &disconnected_dprintf, _("\
17136Set whether dprintf continues after GDB disconnects."), _("\
17137Show whether dprintf continues after GDB disconnects."), _("\
17138Use this to let dprintf commands continue to hit and produce output\n\
17139even if GDB disconnects or detaches from the target."),
17140 NULL,
17141 NULL,
17142 &setlist, &showlist);
17143
17144 add_com ("agent-printf", class_vars, agent_printf_command, _("\
17145agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
17146(target agent only) This is useful for formatted output in user-defined commands."));
17147
765dc015 17148 automatic_hardware_breakpoints = 1;
f3b1572e
PA
17149
17150 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 17151 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 17152}
This page took 2.563465 seconds and 4 git commands to generate.