Copy file1.txt to remote host in dw2-filename.exp and dw2-anonymous-func.exp
[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"
35#include "gdbthread.h"
36#include "target.h"
37#include "language.h"
0e9f083f 38#include <string.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"
0225421b 50#include "gdb_assert.h"
fe898f56 51#include "block.h"
a77053c2 52#include "solib.h"
84acb35a
JJ
53#include "solist.h"
54#include "observer.h"
60250e8b 55#include "exceptions.h"
765dc015 56#include "memattr.h"
f7f9143b 57#include "ada-lang.h"
d1aa2f50 58#include "top.h"
79a45b7d 59#include "valprint.h"
4efc6507 60#include "jit.h"
a96d9b2e 61#include "xml-syscall.h"
65d79d4b 62#include "parser-defs.h"
55aa24fb
SDJ
63#include "gdb_regex.h"
64#include "probe.h"
e9cafbcc 65#include "cli/cli-utils.h"
be34f849 66#include "continuations.h"
1bfeeb0f
JL
67#include "stack.h"
68#include "skip.h"
b775012e 69#include "ax-gdb.h"
e2e4d78b 70#include "dummy-frame.h"
c906108c 71
d3ce09f5
SS
72#include "format.h"
73
1042e4c0
SS
74/* readline include files */
75#include "readline/readline.h"
76#include "readline/history.h"
77
78/* readline defines this. */
79#undef savestring
80
034dad6f 81#include "mi/mi-common.h"
6dddc817 82#include "extension.h"
104c1213 83
e7e8980f
YQ
84/* Enums for exception-handling support. */
85enum exception_event_kind
86{
87 EX_EVENT_THROW,
591f19e8 88 EX_EVENT_RETHROW,
e7e8980f
YQ
89 EX_EVENT_CATCH
90};
91
4a64f543 92/* Prototypes for local functions. */
c906108c 93
a14ed312 94static void enable_delete_command (char *, int);
c906108c 95
a14ed312 96static void enable_once_command (char *, int);
c906108c 97
816338b5
SS
98static void enable_count_command (char *, int);
99
a14ed312 100static void disable_command (char *, int);
c906108c 101
a14ed312 102static void enable_command (char *, int);
c906108c 103
95a42b64
TT
104static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
105 void *),
106 void *);
c906108c 107
a14ed312 108static void ignore_command (char *, int);
c906108c 109
4efb68b1 110static int breakpoint_re_set_one (void *);
c906108c 111
348d480f
PA
112static void breakpoint_re_set_default (struct breakpoint *);
113
983af33b
SDJ
114static void create_sals_from_address_default (char **,
115 struct linespec_result *,
116 enum bptype, char *,
117 char **);
118
119static void create_breakpoints_sal_default (struct gdbarch *,
120 struct linespec_result *,
e7e0cddf 121 char *, char *, enum bptype,
983af33b
SDJ
122 enum bpdisp, int, int,
123 int,
124 const struct breakpoint_ops *,
44f238bb 125 int, int, int, unsigned);
983af33b
SDJ
126
127static void decode_linespec_default (struct breakpoint *, char **,
128 struct symtabs_and_lines *);
129
a14ed312 130static void clear_command (char *, int);
c906108c 131
a14ed312 132static void catch_command (char *, int);
c906108c 133
a9634178 134static int can_use_hardware_watchpoint (struct value *);
c906108c 135
98deb0da 136static void break_command_1 (char *, int, int);
c906108c 137
a14ed312 138static void mention (struct breakpoint *);
c906108c 139
348d480f
PA
140static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
141 enum bptype,
c0a91b2b 142 const struct breakpoint_ops *);
3742cc8b
YQ
143static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
144 const struct symtab_and_line *);
145
4a64f543
MS
146/* This function is used in gdbtk sources and thus can not be made
147 static. */
63c252f8 148struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 149 struct symtab_and_line,
c0a91b2b
TT
150 enum bptype,
151 const struct breakpoint_ops *);
c906108c 152
06edf0c0
PA
153static struct breakpoint *
154 momentary_breakpoint_from_master (struct breakpoint *orig,
155 enum bptype type,
c0a91b2b 156 const struct breakpoint_ops *ops);
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
fe3f5fa8 231static void free_bp_location (struct bp_location *loc);
f431efe5
PA
232static void incref_bp_location (struct bp_location *loc);
233static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 234
39d61571 235static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 236
b60e7edf 237static void update_global_location_list (int);
a5606eee 238
b60e7edf 239static void update_global_location_list_nothrow (int);
74960c60 240
d77f58be 241static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
242
243static void insert_breakpoint_locations (void);
a5606eee 244
a96d9b2e
SDJ
245static int syscall_catchpoint_p (struct breakpoint *b);
246
1042e4c0
SS
247static void tracepoints_info (char *, int);
248
249static void delete_trace_command (char *, int);
250
251static void enable_trace_command (char *, int);
252
253static void disable_trace_command (char *, int);
254
255static void trace_pass_command (char *, int);
256
558a9d82
YQ
257static void set_tracepoint_count (int num);
258
9c06b0b4
TJB
259static int is_masked_watchpoint (const struct breakpoint *b);
260
b775012e
LM
261static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
262
983af33b
SDJ
263/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
264 otherwise. */
265
266static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 267
2060206e
PA
268/* The abstract base class all breakpoint_ops structures inherit
269 from. */
ab04a2af 270struct breakpoint_ops base_breakpoint_ops;
2060206e
PA
271
272/* The breakpoint_ops structure to be inherited by all breakpoint_ops
273 that are implemented on top of software or hardware breakpoints
274 (user breakpoints, internal and momentary breakpoints, etc.). */
275static struct breakpoint_ops bkpt_base_breakpoint_ops;
276
277/* Internal breakpoints class type. */
06edf0c0 278static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
279
280/* Momentary breakpoints class type. */
06edf0c0
PA
281static struct breakpoint_ops momentary_breakpoint_ops;
282
e2e4d78b
JK
283/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
284static struct breakpoint_ops longjmp_breakpoint_ops;
285
2060206e
PA
286/* The breakpoint_ops structure to be used in regular user created
287 breakpoints. */
288struct breakpoint_ops bkpt_breakpoint_ops;
289
55aa24fb
SDJ
290/* Breakpoints set on probes. */
291static struct breakpoint_ops bkpt_probe_breakpoint_ops;
292
e7e0cddf 293/* Dynamic printf class type. */
c5867ab6 294struct breakpoint_ops dprintf_breakpoint_ops;
e7e0cddf 295
9d497a19
PA
296/* One (or perhaps two) breakpoints used for software single
297 stepping. */
298
299static void *single_step_breakpoints[2];
300static struct gdbarch *single_step_gdbarch[2];
301
d3ce09f5
SS
302/* The style in which to perform a dynamic printf. This is a user
303 option because different output options have different tradeoffs;
304 if GDB does the printing, there is better error handling if there
305 is a problem with any of the arguments, but using an inferior
306 function lets you have special-purpose printers and sending of
307 output to the same place as compiled-in print functions. */
308
309static const char dprintf_style_gdb[] = "gdb";
310static const char dprintf_style_call[] = "call";
311static const char dprintf_style_agent[] = "agent";
312static const char *const dprintf_style_enums[] = {
313 dprintf_style_gdb,
314 dprintf_style_call,
315 dprintf_style_agent,
316 NULL
317};
318static const char *dprintf_style = dprintf_style_gdb;
319
320/* The function to use for dynamic printf if the preferred style is to
321 call into the inferior. The value is simply a string that is
322 copied into the command, so it can be anything that GDB can
323 evaluate to a callable address, not necessarily a function name. */
324
325static char *dprintf_function = "";
326
327/* The channel to use for dynamic printf if the preferred style is to
328 call into the inferior; if a nonempty string, it will be passed to
329 the call as the first argument, with the format string as the
330 second. As with the dprintf function, this can be anything that
331 GDB knows how to evaluate, so in addition to common choices like
332 "stderr", this could be an app-specific expression like
333 "mystreams[curlogger]". */
334
335static char *dprintf_channel = "";
336
337/* True if dprintf commands should continue to operate even if GDB
338 has disconnected. */
339static int disconnected_dprintf = 1;
340
5cea2a26
PA
341/* A reference-counted struct command_line. This lets multiple
342 breakpoints share a single command list. */
343struct counted_command_line
344{
345 /* The reference count. */
346 int refc;
347
348 /* The command list. */
349 struct command_line *commands;
350};
351
352struct command_line *
353breakpoint_commands (struct breakpoint *b)
354{
355 return b->commands ? b->commands->commands : NULL;
356}
3daf8fe5 357
f3b1572e
PA
358/* Flag indicating that a command has proceeded the inferior past the
359 current breakpoint. */
360
361static int breakpoint_proceeded;
362
956a9fb9 363const char *
2cec12e5
AR
364bpdisp_text (enum bpdisp disp)
365{
4a64f543
MS
366 /* NOTE: the following values are a part of MI protocol and
367 represent values of 'disp' field returned when inferior stops at
368 a breakpoint. */
bc043ef3 369 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 370
2cec12e5
AR
371 return bpdisps[(int) disp];
372}
c906108c 373
4a64f543 374/* Prototypes for exported functions. */
c906108c 375/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 376 if such is available. */
c906108c
SS
377static int can_use_hw_watchpoints;
378
920d2a44
AC
379static void
380show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
381 struct cmd_list_element *c,
382 const char *value)
383{
3e43a32a
MS
384 fprintf_filtered (file,
385 _("Debugger's willingness to use "
386 "watchpoint hardware is %s.\n"),
920d2a44
AC
387 value);
388}
389
fa8d40ab
JJ
390/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
391 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 392 for unrecognized breakpoint locations.
fa8d40ab
JJ
393 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
394static enum auto_boolean pending_break_support;
920d2a44
AC
395static void
396show_pending_break_support (struct ui_file *file, int from_tty,
397 struct cmd_list_element *c,
398 const char *value)
399{
3e43a32a
MS
400 fprintf_filtered (file,
401 _("Debugger's behavior regarding "
402 "pending breakpoints is %s.\n"),
920d2a44
AC
403 value);
404}
fa8d40ab 405
765dc015 406/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 407 set with "break" but falling in read-only memory.
765dc015
VP
408 If 0, gdb will warn about such breakpoints, but won't automatically
409 use hardware breakpoints. */
410static int automatic_hardware_breakpoints;
411static void
412show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
413 struct cmd_list_element *c,
414 const char *value)
415{
3e43a32a
MS
416 fprintf_filtered (file,
417 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
418 value);
419}
420
33e5cbd6
PA
421/* If on, gdb will keep breakpoints inserted even as inferior is
422 stopped, and immediately insert any new breakpoints. If off, gdb
423 will insert breakpoints into inferior only when resuming it, and
424 will remove breakpoints upon stop. If auto, GDB will behave as ON
425 if in non-stop mode, and as OFF if all-stop mode.*/
426
72d0e2c5
YQ
427static enum auto_boolean always_inserted_mode = AUTO_BOOLEAN_AUTO;
428
33e5cbd6 429static void
74960c60 430show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 431 struct cmd_list_element *c, const char *value)
74960c60 432{
72d0e2c5 433 if (always_inserted_mode == AUTO_BOOLEAN_AUTO)
3e43a32a
MS
434 fprintf_filtered (file,
435 _("Always inserted breakpoint "
436 "mode is %s (currently %s).\n"),
33e5cbd6
PA
437 value,
438 breakpoints_always_inserted_mode () ? "on" : "off");
439 else
3e43a32a
MS
440 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
441 value);
74960c60
VP
442}
443
33e5cbd6
PA
444int
445breakpoints_always_inserted_mode (void)
446{
72d0e2c5
YQ
447 return (always_inserted_mode == AUTO_BOOLEAN_TRUE
448 || (always_inserted_mode == AUTO_BOOLEAN_AUTO && non_stop));
33e5cbd6 449}
765dc015 450
b775012e
LM
451static const char condition_evaluation_both[] = "host or target";
452
453/* Modes for breakpoint condition evaluation. */
454static const char condition_evaluation_auto[] = "auto";
455static const char condition_evaluation_host[] = "host";
456static const char condition_evaluation_target[] = "target";
457static const char *const condition_evaluation_enums[] = {
458 condition_evaluation_auto,
459 condition_evaluation_host,
460 condition_evaluation_target,
461 NULL
462};
463
464/* Global that holds the current mode for breakpoint condition evaluation. */
465static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
466
467/* Global that we use to display information to the user (gets its value from
468 condition_evaluation_mode_1. */
469static const char *condition_evaluation_mode = condition_evaluation_auto;
470
471/* Translate a condition evaluation mode MODE into either "host"
472 or "target". This is used mostly to translate from "auto" to the
473 real setting that is being used. It returns the translated
474 evaluation mode. */
475
476static const char *
477translate_condition_evaluation_mode (const char *mode)
478{
479 if (mode == condition_evaluation_auto)
480 {
481 if (target_supports_evaluation_of_breakpoint_conditions ())
482 return condition_evaluation_target;
483 else
484 return condition_evaluation_host;
485 }
486 else
487 return mode;
488}
489
490/* Discovers what condition_evaluation_auto translates to. */
491
492static const char *
493breakpoint_condition_evaluation_mode (void)
494{
495 return translate_condition_evaluation_mode (condition_evaluation_mode);
496}
497
498/* Return true if GDB should evaluate breakpoint conditions or false
499 otherwise. */
500
501static int
502gdb_evaluates_breakpoint_condition_p (void)
503{
504 const char *mode = breakpoint_condition_evaluation_mode ();
505
506 return (mode == condition_evaluation_host);
507}
508
a14ed312 509void _initialize_breakpoint (void);
c906108c 510
c906108c
SS
511/* Are we executing breakpoint commands? */
512static int executing_breakpoint_commands;
513
c02f5703
MS
514/* Are overlay event breakpoints enabled? */
515static int overlay_events_enabled;
516
e09342b5
TJB
517/* See description in breakpoint.h. */
518int target_exact_watchpoints = 0;
519
c906108c 520/* Walk the following statement or block through all breakpoints.
e5dd4106 521 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 522 current breakpoint. */
c906108c 523
5c44784c 524#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 525
5c44784c
JM
526#define ALL_BREAKPOINTS_SAFE(B,TMP) \
527 for (B = breakpoint_chain; \
528 B ? (TMP=B->next, 1): 0; \
529 B = TMP)
c906108c 530
4a64f543
MS
531/* Similar iterator for the low-level breakpoints. SAFE variant is
532 not provided so update_global_location_list must not be called
533 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 534
876fa593
JK
535#define ALL_BP_LOCATIONS(B,BP_TMP) \
536 for (BP_TMP = bp_location; \
537 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
538 BP_TMP++)
7cc221ef 539
b775012e
LM
540/* Iterates through locations with address ADDRESS for the currently selected
541 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
542 to where the loop should start from.
543 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
544 appropriate location to start with. */
545
546#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
547 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
548 BP_LOCP_TMP = BP_LOCP_START; \
549 BP_LOCP_START \
550 && (BP_LOCP_TMP < bp_location + bp_location_count \
551 && (*BP_LOCP_TMP)->address == ADDRESS); \
552 BP_LOCP_TMP++)
553
1042e4c0
SS
554/* Iterator for tracepoints only. */
555
556#define ALL_TRACEPOINTS(B) \
557 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 558 if (is_tracepoint (B))
1042e4c0 559
7cc221ef 560/* Chains of all breakpoints defined. */
c906108c
SS
561
562struct breakpoint *breakpoint_chain;
563
876fa593
JK
564/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
565
566static struct bp_location **bp_location;
567
568/* Number of elements of BP_LOCATION. */
569
570static unsigned bp_location_count;
571
4a64f543
MS
572/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
573 ADDRESS for the current elements of BP_LOCATION which get a valid
574 result from bp_location_has_shadow. You can use it for roughly
575 limiting the subrange of BP_LOCATION to scan for shadow bytes for
576 an address you need to read. */
876fa593
JK
577
578static CORE_ADDR bp_location_placed_address_before_address_max;
579
4a64f543
MS
580/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
581 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
582 BP_LOCATION which get a valid result from bp_location_has_shadow.
583 You can use it for roughly limiting the subrange of BP_LOCATION to
584 scan for shadow bytes for an address you need to read. */
876fa593
JK
585
586static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 587
4a64f543
MS
588/* The locations that no longer correspond to any breakpoint, unlinked
589 from bp_location array, but for which a hit may still be reported
590 by a target. */
20874c92
VP
591VEC(bp_location_p) *moribund_locations = NULL;
592
c906108c
SS
593/* Number of last breakpoint made. */
594
95a42b64
TT
595static int breakpoint_count;
596
86b17b60
PA
597/* The value of `breakpoint_count' before the last command that
598 created breakpoints. If the last (break-like) command created more
599 than one breakpoint, then the difference between BREAKPOINT_COUNT
600 and PREV_BREAKPOINT_COUNT is more than one. */
601static int prev_breakpoint_count;
c906108c 602
1042e4c0
SS
603/* Number of last tracepoint made. */
604
95a42b64 605static int tracepoint_count;
1042e4c0 606
6149aea9
PA
607static struct cmd_list_element *breakpoint_set_cmdlist;
608static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 609struct cmd_list_element *save_cmdlist;
6149aea9 610
468d015d
JJ
611/* Return whether a breakpoint is an active enabled breakpoint. */
612static int
613breakpoint_enabled (struct breakpoint *b)
614{
0d381245 615 return (b->enable_state == bp_enabled);
468d015d
JJ
616}
617
c906108c
SS
618/* Set breakpoint count to NUM. */
619
95a42b64 620static void
fba45db2 621set_breakpoint_count (int num)
c906108c 622{
86b17b60 623 prev_breakpoint_count = breakpoint_count;
c906108c 624 breakpoint_count = num;
4fa62494 625 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
626}
627
86b17b60
PA
628/* Used by `start_rbreak_breakpoints' below, to record the current
629 breakpoint count before "rbreak" creates any breakpoint. */
630static int rbreak_start_breakpoint_count;
631
95a42b64
TT
632/* Called at the start an "rbreak" command to record the first
633 breakpoint made. */
86b17b60 634
95a42b64
TT
635void
636start_rbreak_breakpoints (void)
637{
86b17b60 638 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
639}
640
641/* Called at the end of an "rbreak" command to record the last
642 breakpoint made. */
86b17b60 643
95a42b64
TT
644void
645end_rbreak_breakpoints (void)
646{
86b17b60 647 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
648}
649
4a64f543 650/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
651
652void
fba45db2 653clear_breakpoint_hit_counts (void)
c906108c
SS
654{
655 struct breakpoint *b;
656
657 ALL_BREAKPOINTS (b)
658 b->hit_count = 0;
659}
660
9add0f1b
TT
661/* Allocate a new counted_command_line with reference count of 1.
662 The new structure owns COMMANDS. */
663
664static struct counted_command_line *
665alloc_counted_command_line (struct command_line *commands)
666{
667 struct counted_command_line *result
668 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 669
9add0f1b
TT
670 result->refc = 1;
671 result->commands = commands;
672 return result;
673}
674
675/* Increment reference count. This does nothing if CMD is NULL. */
676
677static void
678incref_counted_command_line (struct counted_command_line *cmd)
679{
680 if (cmd)
681 ++cmd->refc;
682}
683
684/* Decrement reference count. If the reference count reaches 0,
685 destroy the counted_command_line. Sets *CMDP to NULL. This does
686 nothing if *CMDP is NULL. */
687
688static void
689decref_counted_command_line (struct counted_command_line **cmdp)
690{
691 if (*cmdp)
692 {
693 if (--(*cmdp)->refc == 0)
694 {
695 free_command_lines (&(*cmdp)->commands);
696 xfree (*cmdp);
697 }
698 *cmdp = NULL;
699 }
700}
701
702/* A cleanup function that calls decref_counted_command_line. */
703
704static void
705do_cleanup_counted_command_line (void *arg)
706{
707 decref_counted_command_line (arg);
708}
709
710/* Create a cleanup that calls decref_counted_command_line on the
711 argument. */
712
713static struct cleanup *
714make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
715{
716 return make_cleanup (do_cleanup_counted_command_line, cmdp);
717}
718
c906108c 719\f
48cb2d85
VP
720/* Return the breakpoint with the specified number, or NULL
721 if the number does not refer to an existing breakpoint. */
722
723struct breakpoint *
724get_breakpoint (int num)
725{
726 struct breakpoint *b;
727
728 ALL_BREAKPOINTS (b)
729 if (b->number == num)
730 return b;
731
732 return NULL;
733}
5c44784c 734
c906108c 735\f
adc36818 736
b775012e
LM
737/* Mark locations as "conditions have changed" in case the target supports
738 evaluating conditions on its side. */
739
740static void
741mark_breakpoint_modified (struct breakpoint *b)
742{
743 struct bp_location *loc;
744
745 /* This is only meaningful if the target is
746 evaluating conditions and if the user has
747 opted for condition evaluation on the target's
748 side. */
749 if (gdb_evaluates_breakpoint_condition_p ()
750 || !target_supports_evaluation_of_breakpoint_conditions ())
751 return;
752
753 if (!is_breakpoint (b))
754 return;
755
756 for (loc = b->loc; loc; loc = loc->next)
757 loc->condition_changed = condition_modified;
758}
759
760/* Mark location as "conditions have changed" in case the target supports
761 evaluating conditions on its side. */
762
763static void
764mark_breakpoint_location_modified (struct bp_location *loc)
765{
766 /* This is only meaningful if the target is
767 evaluating conditions and if the user has
768 opted for condition evaluation on the target's
769 side. */
770 if (gdb_evaluates_breakpoint_condition_p ()
771 || !target_supports_evaluation_of_breakpoint_conditions ())
772
773 return;
774
775 if (!is_breakpoint (loc->owner))
776 return;
777
778 loc->condition_changed = condition_modified;
779}
780
781/* Sets the condition-evaluation mode using the static global
782 condition_evaluation_mode. */
783
784static void
785set_condition_evaluation_mode (char *args, int from_tty,
786 struct cmd_list_element *c)
787{
b775012e
LM
788 const char *old_mode, *new_mode;
789
790 if ((condition_evaluation_mode_1 == condition_evaluation_target)
791 && !target_supports_evaluation_of_breakpoint_conditions ())
792 {
793 condition_evaluation_mode_1 = condition_evaluation_mode;
794 warning (_("Target does not support breakpoint condition evaluation.\n"
795 "Using host evaluation mode instead."));
796 return;
797 }
798
799 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
800 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
801
abf1152a
JK
802 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
803 settings was "auto". */
804 condition_evaluation_mode = condition_evaluation_mode_1;
805
b775012e
LM
806 /* Only update the mode if the user picked a different one. */
807 if (new_mode != old_mode)
808 {
809 struct bp_location *loc, **loc_tmp;
810 /* If the user switched to a different evaluation mode, we
811 need to synch the changes with the target as follows:
812
813 "host" -> "target": Send all (valid) conditions to the target.
814 "target" -> "host": Remove all the conditions from the target.
815 */
816
b775012e
LM
817 if (new_mode == condition_evaluation_target)
818 {
819 /* Mark everything modified and synch conditions with the
820 target. */
821 ALL_BP_LOCATIONS (loc, loc_tmp)
822 mark_breakpoint_location_modified (loc);
823 }
824 else
825 {
826 /* Manually mark non-duplicate locations to synch conditions
827 with the target. We do this to remove all the conditions the
828 target knows about. */
829 ALL_BP_LOCATIONS (loc, loc_tmp)
830 if (is_breakpoint (loc->owner) && loc->inserted)
831 loc->needs_update = 1;
832 }
833
834 /* Do the update. */
835 update_global_location_list (1);
836 }
837
838 return;
839}
840
841/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
842 what "auto" is translating to. */
843
844static void
845show_condition_evaluation_mode (struct ui_file *file, int from_tty,
846 struct cmd_list_element *c, const char *value)
847{
848 if (condition_evaluation_mode == condition_evaluation_auto)
849 fprintf_filtered (file,
850 _("Breakpoint condition evaluation "
851 "mode is %s (currently %s).\n"),
852 value,
853 breakpoint_condition_evaluation_mode ());
854 else
855 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
856 value);
857}
858
859/* A comparison function for bp_location AP and BP that is used by
860 bsearch. This comparison function only cares about addresses, unlike
861 the more general bp_location_compare function. */
862
863static int
864bp_location_compare_addrs (const void *ap, const void *bp)
865{
866 struct bp_location *a = *(void **) ap;
867 struct bp_location *b = *(void **) bp;
868
869 if (a->address == b->address)
870 return 0;
871 else
872 return ((a->address > b->address) - (a->address < b->address));
873}
874
875/* Helper function to skip all bp_locations with addresses
876 less than ADDRESS. It returns the first bp_location that
877 is greater than or equal to ADDRESS. If none is found, just
878 return NULL. */
879
880static struct bp_location **
881get_first_locp_gte_addr (CORE_ADDR address)
882{
883 struct bp_location dummy_loc;
884 struct bp_location *dummy_locp = &dummy_loc;
885 struct bp_location **locp_found = NULL;
886
887 /* Initialize the dummy location's address field. */
888 memset (&dummy_loc, 0, sizeof (struct bp_location));
889 dummy_loc.address = address;
890
891 /* Find a close match to the first location at ADDRESS. */
892 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
893 sizeof (struct bp_location **),
894 bp_location_compare_addrs);
895
896 /* Nothing was found, nothing left to do. */
897 if (locp_found == NULL)
898 return NULL;
899
900 /* We may have found a location that is at ADDRESS but is not the first in the
901 location's list. Go backwards (if possible) and locate the first one. */
902 while ((locp_found - 1) >= bp_location
903 && (*(locp_found - 1))->address == address)
904 locp_found--;
905
906 return locp_found;
907}
908
adc36818
PM
909void
910set_breakpoint_condition (struct breakpoint *b, char *exp,
911 int from_tty)
912{
3a5c3e22
PA
913 xfree (b->cond_string);
914 b->cond_string = NULL;
adc36818 915
3a5c3e22 916 if (is_watchpoint (b))
adc36818 917 {
3a5c3e22
PA
918 struct watchpoint *w = (struct watchpoint *) b;
919
920 xfree (w->cond_exp);
921 w->cond_exp = NULL;
922 }
923 else
924 {
925 struct bp_location *loc;
926
927 for (loc = b->loc; loc; loc = loc->next)
928 {
929 xfree (loc->cond);
930 loc->cond = NULL;
b775012e
LM
931
932 /* No need to free the condition agent expression
933 bytecode (if we have one). We will handle this
934 when we go through update_global_location_list. */
3a5c3e22 935 }
adc36818 936 }
adc36818
PM
937
938 if (*exp == 0)
939 {
940 if (from_tty)
941 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
942 }
943 else
944 {
bbc13ae3 945 const char *arg = exp;
cc59ec59 946
adc36818
PM
947 /* I don't know if it matters whether this is the string the user
948 typed in or the decompiled expression. */
949 b->cond_string = xstrdup (arg);
950 b->condition_not_parsed = 0;
951
952 if (is_watchpoint (b))
953 {
3a5c3e22
PA
954 struct watchpoint *w = (struct watchpoint *) b;
955
adc36818
PM
956 innermost_block = NULL;
957 arg = exp;
1bb9788d 958 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
adc36818
PM
959 if (*arg)
960 error (_("Junk at end of expression"));
3a5c3e22 961 w->cond_exp_valid_block = innermost_block;
adc36818
PM
962 }
963 else
964 {
3a5c3e22
PA
965 struct bp_location *loc;
966
adc36818
PM
967 for (loc = b->loc; loc; loc = loc->next)
968 {
969 arg = exp;
970 loc->cond =
1bb9788d
TT
971 parse_exp_1 (&arg, loc->address,
972 block_for_pc (loc->address), 0);
adc36818
PM
973 if (*arg)
974 error (_("Junk at end of expression"));
975 }
976 }
977 }
b775012e
LM
978 mark_breakpoint_modified (b);
979
8d3788bd 980 observer_notify_breakpoint_modified (b);
adc36818
PM
981}
982
d55637df
TT
983/* Completion for the "condition" command. */
984
985static VEC (char_ptr) *
6f937416
PA
986condition_completer (struct cmd_list_element *cmd,
987 const char *text, const char *word)
d55637df 988{
6f937416 989 const char *space;
d55637df 990
6f937416
PA
991 text = skip_spaces_const (text);
992 space = skip_to_space_const (text);
d55637df
TT
993 if (*space == '\0')
994 {
995 int len;
996 struct breakpoint *b;
997 VEC (char_ptr) *result = NULL;
998
999 if (text[0] == '$')
1000 {
1001 /* We don't support completion of history indices. */
1002 if (isdigit (text[1]))
1003 return NULL;
1004 return complete_internalvar (&text[1]);
1005 }
1006
1007 /* We're completing the breakpoint number. */
1008 len = strlen (text);
1009
1010 ALL_BREAKPOINTS (b)
58ce7251
SDJ
1011 {
1012 char number[50];
1013
1014 xsnprintf (number, sizeof (number), "%d", b->number);
1015
1016 if (strncmp (number, text, len) == 0)
1017 VEC_safe_push (char_ptr, result, xstrdup (number));
1018 }
d55637df
TT
1019
1020 return result;
1021 }
1022
1023 /* We're completing the expression part. */
6f937416 1024 text = skip_spaces_const (space);
d55637df
TT
1025 return expression_completer (cmd, text, word);
1026}
1027
c906108c
SS
1028/* condition N EXP -- set break condition of breakpoint N to EXP. */
1029
1030static void
fba45db2 1031condition_command (char *arg, int from_tty)
c906108c 1032{
52f0bd74 1033 struct breakpoint *b;
c906108c 1034 char *p;
52f0bd74 1035 int bnum;
c906108c
SS
1036
1037 if (arg == 0)
e2e0b3e5 1038 error_no_arg (_("breakpoint number"));
c906108c
SS
1039
1040 p = arg;
1041 bnum = get_number (&p);
5c44784c 1042 if (bnum == 0)
8a3fe4f8 1043 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1044
1045 ALL_BREAKPOINTS (b)
1046 if (b->number == bnum)
2f069f6f 1047 {
6dddc817
DE
1048 /* Check if this breakpoint has a "stop" method implemented in an
1049 extension language. This method and conditions entered into GDB
1050 from the CLI are mutually exclusive. */
1051 const struct extension_language_defn *extlang
1052 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1053
1054 if (extlang != NULL)
1055 {
1056 error (_("Only one stop condition allowed. There is currently"
1057 " a %s stop condition defined for this breakpoint."),
1058 ext_lang_capitalized_name (extlang));
1059 }
2566ad2d 1060 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1061
1062 if (is_breakpoint (b))
1063 update_global_location_list (1);
1064
2f069f6f
JB
1065 return;
1066 }
c906108c 1067
8a3fe4f8 1068 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1069}
1070
a7bdde9e
VP
1071/* Check that COMMAND do not contain commands that are suitable
1072 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1073 Throw if any such commands is found. */
1074
a7bdde9e
VP
1075static void
1076check_no_tracepoint_commands (struct command_line *commands)
1077{
1078 struct command_line *c;
cc59ec59 1079
a7bdde9e
VP
1080 for (c = commands; c; c = c->next)
1081 {
1082 int i;
1083
1084 if (c->control_type == while_stepping_control)
3e43a32a
MS
1085 error (_("The 'while-stepping' command can "
1086 "only be used for tracepoints"));
a7bdde9e
VP
1087
1088 for (i = 0; i < c->body_count; ++i)
1089 check_no_tracepoint_commands ((c->body_list)[i]);
1090
1091 /* Not that command parsing removes leading whitespace and comment
4a64f543 1092 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1093 command directly. */
1094 if (strstr (c->line, "collect ") == c->line)
1095 error (_("The 'collect' command can only be used for tracepoints"));
1096
51661e93
VP
1097 if (strstr (c->line, "teval ") == c->line)
1098 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1099 }
1100}
1101
d77f58be
SS
1102/* Encapsulate tests for different types of tracepoints. */
1103
d9b3f62e
PA
1104static int
1105is_tracepoint_type (enum bptype type)
1106{
1107 return (type == bp_tracepoint
1108 || type == bp_fast_tracepoint
1109 || type == bp_static_tracepoint);
1110}
1111
a7bdde9e 1112int
d77f58be 1113is_tracepoint (const struct breakpoint *b)
a7bdde9e 1114{
d9b3f62e 1115 return is_tracepoint_type (b->type);
a7bdde9e 1116}
d9b3f62e 1117
e5dd4106 1118/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1119 breakpoint. This function will throw an exception if a problem is
1120 found. */
48cb2d85 1121
95a42b64
TT
1122static void
1123validate_commands_for_breakpoint (struct breakpoint *b,
1124 struct command_line *commands)
48cb2d85 1125{
d77f58be 1126 if (is_tracepoint (b))
a7bdde9e 1127 {
c9a6ce02 1128 struct tracepoint *t = (struct tracepoint *) b;
a7bdde9e
VP
1129 struct command_line *c;
1130 struct command_line *while_stepping = 0;
c9a6ce02
PA
1131
1132 /* Reset the while-stepping step count. The previous commands
1133 might have included a while-stepping action, while the new
1134 ones might not. */
1135 t->step_count = 0;
1136
1137 /* We need to verify that each top-level element of commands is
1138 valid for tracepoints, that there's at most one
1139 while-stepping element, and that the while-stepping's body
1140 has valid tracing commands excluding nested while-stepping.
1141 We also need to validate the tracepoint action line in the
1142 context of the tracepoint --- validate_actionline actually
1143 has side effects, like setting the tracepoint's
1144 while-stepping STEP_COUNT, in addition to checking if the
1145 collect/teval actions parse and make sense in the
1146 tracepoint's context. */
a7bdde9e
VP
1147 for (c = commands; c; c = c->next)
1148 {
a7bdde9e
VP
1149 if (c->control_type == while_stepping_control)
1150 {
1151 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1152 error (_("The 'while-stepping' command "
1153 "cannot be used for fast tracepoint"));
0fb4aa4b 1154 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1155 error (_("The 'while-stepping' command "
1156 "cannot be used for static tracepoint"));
a7bdde9e
VP
1157
1158 if (while_stepping)
3e43a32a
MS
1159 error (_("The 'while-stepping' command "
1160 "can be used only once"));
a7bdde9e
VP
1161 else
1162 while_stepping = c;
1163 }
c9a6ce02
PA
1164
1165 validate_actionline (c->line, b);
a7bdde9e
VP
1166 }
1167 if (while_stepping)
1168 {
1169 struct command_line *c2;
1170
1171 gdb_assert (while_stepping->body_count == 1);
1172 c2 = while_stepping->body_list[0];
1173 for (; c2; c2 = c2->next)
1174 {
a7bdde9e
VP
1175 if (c2->control_type == while_stepping_control)
1176 error (_("The 'while-stepping' command cannot be nested"));
1177 }
1178 }
1179 }
1180 else
1181 {
1182 check_no_tracepoint_commands (commands);
1183 }
95a42b64
TT
1184}
1185
0fb4aa4b
PA
1186/* Return a vector of all the static tracepoints set at ADDR. The
1187 caller is responsible for releasing the vector. */
1188
1189VEC(breakpoint_p) *
1190static_tracepoints_here (CORE_ADDR addr)
1191{
1192 struct breakpoint *b;
1193 VEC(breakpoint_p) *found = 0;
1194 struct bp_location *loc;
1195
1196 ALL_BREAKPOINTS (b)
1197 if (b->type == bp_static_tracepoint)
1198 {
1199 for (loc = b->loc; loc; loc = loc->next)
1200 if (loc->address == addr)
1201 VEC_safe_push(breakpoint_p, found, b);
1202 }
1203
1204 return found;
1205}
1206
95a42b64 1207/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1208 validate that only allowed commands are included. */
95a42b64
TT
1209
1210void
4a64f543
MS
1211breakpoint_set_commands (struct breakpoint *b,
1212 struct command_line *commands)
95a42b64
TT
1213{
1214 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1215
9add0f1b
TT
1216 decref_counted_command_line (&b->commands);
1217 b->commands = alloc_counted_command_line (commands);
8d3788bd 1218 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1219}
1220
45a43567
TT
1221/* Set the internal `silent' flag on the breakpoint. Note that this
1222 is not the same as the "silent" that may appear in the breakpoint's
1223 commands. */
1224
1225void
1226breakpoint_set_silent (struct breakpoint *b, int silent)
1227{
1228 int old_silent = b->silent;
1229
1230 b->silent = silent;
1231 if (old_silent != silent)
8d3788bd 1232 observer_notify_breakpoint_modified (b);
45a43567
TT
1233}
1234
1235/* Set the thread for this breakpoint. If THREAD is -1, make the
1236 breakpoint work for any thread. */
1237
1238void
1239breakpoint_set_thread (struct breakpoint *b, int thread)
1240{
1241 int old_thread = b->thread;
1242
1243 b->thread = thread;
1244 if (old_thread != thread)
8d3788bd 1245 observer_notify_breakpoint_modified (b);
45a43567
TT
1246}
1247
1248/* Set the task for this breakpoint. If TASK is 0, make the
1249 breakpoint work for any task. */
1250
1251void
1252breakpoint_set_task (struct breakpoint *b, int task)
1253{
1254 int old_task = b->task;
1255
1256 b->task = task;
1257 if (old_task != task)
8d3788bd 1258 observer_notify_breakpoint_modified (b);
45a43567
TT
1259}
1260
95a42b64
TT
1261void
1262check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1263{
1264 struct breakpoint *b = closure;
cc59ec59 1265
6f937416 1266 validate_actionline (line, b);
a7bdde9e
VP
1267}
1268
95a42b64
TT
1269/* A structure used to pass information through
1270 map_breakpoint_numbers. */
1271
1272struct commands_info
1273{
1274 /* True if the command was typed at a tty. */
1275 int from_tty;
86b17b60
PA
1276
1277 /* The breakpoint range spec. */
1278 char *arg;
1279
95a42b64
TT
1280 /* Non-NULL if the body of the commands are being read from this
1281 already-parsed command. */
1282 struct command_line *control;
86b17b60 1283
95a42b64
TT
1284 /* The command lines read from the user, or NULL if they have not
1285 yet been read. */
1286 struct counted_command_line *cmd;
1287};
1288
1289/* A callback for map_breakpoint_numbers that sets the commands for
1290 commands_command. */
1291
c906108c 1292static void
95a42b64 1293do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1294{
95a42b64 1295 struct commands_info *info = data;
c906108c 1296
95a42b64
TT
1297 if (info->cmd == NULL)
1298 {
1299 struct command_line *l;
5c44784c 1300
95a42b64
TT
1301 if (info->control != NULL)
1302 l = copy_command_lines (info->control->body_list[0]);
1303 else
86b17b60
PA
1304 {
1305 struct cleanup *old_chain;
1306 char *str;
c5aa993b 1307
3e43a32a
MS
1308 str = xstrprintf (_("Type commands for breakpoint(s) "
1309 "%s, one per line."),
86b17b60
PA
1310 info->arg);
1311
1312 old_chain = make_cleanup (xfree, str);
1313
1314 l = read_command_lines (str,
1315 info->from_tty, 1,
d77f58be 1316 (is_tracepoint (b)
86b17b60
PA
1317 ? check_tracepoint_command : 0),
1318 b);
1319
1320 do_cleanups (old_chain);
1321 }
a7bdde9e 1322
95a42b64
TT
1323 info->cmd = alloc_counted_command_line (l);
1324 }
1325
1326 /* If a breakpoint was on the list more than once, we don't need to
1327 do anything. */
1328 if (b->commands != info->cmd)
1329 {
1330 validate_commands_for_breakpoint (b, info->cmd->commands);
1331 incref_counted_command_line (info->cmd);
1332 decref_counted_command_line (&b->commands);
1333 b->commands = info->cmd;
8d3788bd 1334 observer_notify_breakpoint_modified (b);
c5aa993b 1335 }
95a42b64
TT
1336}
1337
1338static void
4a64f543
MS
1339commands_command_1 (char *arg, int from_tty,
1340 struct command_line *control)
95a42b64
TT
1341{
1342 struct cleanup *cleanups;
1343 struct commands_info info;
1344
1345 info.from_tty = from_tty;
1346 info.control = control;
1347 info.cmd = NULL;
1348 /* If we read command lines from the user, then `info' will hold an
1349 extra reference to the commands that we must clean up. */
1350 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1351
1352 if (arg == NULL || !*arg)
1353 {
86b17b60 1354 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1355 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1356 breakpoint_count);
95a42b64
TT
1357 else if (breakpoint_count > 0)
1358 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1359 else
1360 {
1361 /* So that we don't try to free the incoming non-NULL
1362 argument in the cleanup below. Mapping breakpoint
1363 numbers will fail in this case. */
1364 arg = NULL;
1365 }
95a42b64 1366 }
9766ced4
SS
1367 else
1368 /* The command loop has some static state, so we need to preserve
1369 our argument. */
1370 arg = xstrdup (arg);
86b17b60
PA
1371
1372 if (arg != NULL)
1373 make_cleanup (xfree, arg);
1374
1375 info.arg = arg;
95a42b64
TT
1376
1377 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1378
1379 if (info.cmd == NULL)
1380 error (_("No breakpoints specified."));
1381
1382 do_cleanups (cleanups);
1383}
1384
1385static void
1386commands_command (char *arg, int from_tty)
1387{
1388 commands_command_1 (arg, from_tty, NULL);
c906108c 1389}
40c03ae8
EZ
1390
1391/* Like commands_command, but instead of reading the commands from
1392 input stream, takes them from an already parsed command structure.
1393
1394 This is used by cli-script.c to DTRT with breakpoint commands
1395 that are part of if and while bodies. */
1396enum command_control_type
1397commands_from_control_command (char *arg, struct command_line *cmd)
1398{
95a42b64
TT
1399 commands_command_1 (arg, 0, cmd);
1400 return simple_control;
40c03ae8 1401}
876fa593
JK
1402
1403/* Return non-zero if BL->TARGET_INFO contains valid information. */
1404
1405static int
1406bp_location_has_shadow (struct bp_location *bl)
1407{
1408 if (bl->loc_type != bp_loc_software_breakpoint)
1409 return 0;
1410 if (!bl->inserted)
1411 return 0;
1412 if (bl->target_info.shadow_len == 0)
e5dd4106 1413 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1414 return 0;
1415 return 1;
1416}
1417
9d497a19
PA
1418/* Update BUF, which is LEN bytes read from the target address
1419 MEMADDR, by replacing a memory breakpoint with its shadowed
1420 contents.
1421
1422 If READBUF is not NULL, this buffer must not overlap with the of
1423 the breakpoint location's shadow_contents buffer. Otherwise, a
1424 failed assertion internal error will be raised. */
1425
1426static void
1427one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1428 const gdb_byte *writebuf_org,
1429 ULONGEST memaddr, LONGEST len,
1430 struct bp_target_info *target_info,
1431 struct gdbarch *gdbarch)
1432{
1433 /* Now do full processing of the found relevant range of elements. */
1434 CORE_ADDR bp_addr = 0;
1435 int bp_size = 0;
1436 int bptoffset = 0;
1437
1438 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1439 current_program_space->aspace, 0))
1440 {
1441 /* The breakpoint is inserted in a different address space. */
1442 return;
1443 }
1444
1445 /* Addresses and length of the part of the breakpoint that
1446 we need to copy. */
1447 bp_addr = target_info->placed_address;
1448 bp_size = target_info->shadow_len;
1449
1450 if (bp_addr + bp_size <= memaddr)
1451 {
1452 /* The breakpoint is entirely before the chunk of memory we are
1453 reading. */
1454 return;
1455 }
1456
1457 if (bp_addr >= memaddr + len)
1458 {
1459 /* The breakpoint is entirely after the chunk of memory we are
1460 reading. */
1461 return;
1462 }
1463
1464 /* Offset within shadow_contents. */
1465 if (bp_addr < memaddr)
1466 {
1467 /* Only copy the second part of the breakpoint. */
1468 bp_size -= memaddr - bp_addr;
1469 bptoffset = memaddr - bp_addr;
1470 bp_addr = memaddr;
1471 }
1472
1473 if (bp_addr + bp_size > memaddr + len)
1474 {
1475 /* Only copy the first part of the breakpoint. */
1476 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1477 }
1478
1479 if (readbuf != NULL)
1480 {
1481 /* Verify that the readbuf buffer does not overlap with the
1482 shadow_contents buffer. */
1483 gdb_assert (target_info->shadow_contents >= readbuf + len
1484 || readbuf >= (target_info->shadow_contents
1485 + target_info->shadow_len));
1486
1487 /* Update the read buffer with this inserted breakpoint's
1488 shadow. */
1489 memcpy (readbuf + bp_addr - memaddr,
1490 target_info->shadow_contents + bptoffset, bp_size);
1491 }
1492 else
1493 {
1494 const unsigned char *bp;
1495 CORE_ADDR placed_address = target_info->placed_address;
1496 int placed_size = target_info->placed_size;
1497
1498 /* Update the shadow with what we want to write to memory. */
1499 memcpy (target_info->shadow_contents + bptoffset,
1500 writebuf_org + bp_addr - memaddr, bp_size);
1501
1502 /* Determine appropriate breakpoint contents and size for this
1503 address. */
1504 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1505
1506 /* Update the final write buffer with this inserted
1507 breakpoint's INSN. */
1508 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1509 }
1510}
1511
8defab1a 1512/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1513 by replacing any memory breakpoints with their shadowed contents.
1514
35c63cd8
JB
1515 If READBUF is not NULL, this buffer must not overlap with any of
1516 the breakpoint location's shadow_contents buffers. Otherwise,
1517 a failed assertion internal error will be raised.
1518
876fa593 1519 The range of shadowed area by each bp_location is:
35df4500
TJB
1520 bl->address - bp_location_placed_address_before_address_max
1521 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1522 The range we were requested to resolve shadows for is:
1523 memaddr ... memaddr + len
1524 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1525 memaddr + len <= (bl->address
1526 - bp_location_placed_address_before_address_max)
876fa593 1527 and:
35df4500 1528 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1529
8defab1a 1530void
f0ba3972
PA
1531breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1532 const gdb_byte *writebuf_org,
1533 ULONGEST memaddr, LONGEST len)
c906108c 1534{
4a64f543
MS
1535 /* Left boundary, right boundary and median element of our binary
1536 search. */
876fa593 1537 unsigned bc_l, bc_r, bc;
9d497a19 1538 size_t i;
876fa593 1539
4a64f543
MS
1540 /* Find BC_L which is a leftmost element which may affect BUF
1541 content. It is safe to report lower value but a failure to
1542 report higher one. */
876fa593
JK
1543
1544 bc_l = 0;
1545 bc_r = bp_location_count;
1546 while (bc_l + 1 < bc_r)
1547 {
35df4500 1548 struct bp_location *bl;
876fa593
JK
1549
1550 bc = (bc_l + bc_r) / 2;
35df4500 1551 bl = bp_location[bc];
876fa593 1552
4a64f543
MS
1553 /* Check first BL->ADDRESS will not overflow due to the added
1554 constant. Then advance the left boundary only if we are sure
1555 the BC element can in no way affect the BUF content (MEMADDR
1556 to MEMADDR + LEN range).
876fa593 1557
4a64f543
MS
1558 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1559 offset so that we cannot miss a breakpoint with its shadow
1560 range tail still reaching MEMADDR. */
c5aa993b 1561
35df4500
TJB
1562 if ((bl->address + bp_location_shadow_len_after_address_max
1563 >= bl->address)
1564 && (bl->address + bp_location_shadow_len_after_address_max
1565 <= memaddr))
876fa593
JK
1566 bc_l = bc;
1567 else
1568 bc_r = bc;
1569 }
1570
128070bb
PA
1571 /* Due to the binary search above, we need to make sure we pick the
1572 first location that's at BC_L's address. E.g., if there are
1573 multiple locations at the same address, BC_L may end up pointing
1574 at a duplicate location, and miss the "master"/"inserted"
1575 location. Say, given locations L1, L2 and L3 at addresses A and
1576 B:
1577
1578 L1@A, L2@A, L3@B, ...
1579
1580 BC_L could end up pointing at location L2, while the "master"
1581 location could be L1. Since the `loc->inserted' flag is only set
1582 on "master" locations, we'd forget to restore the shadow of L1
1583 and L2. */
1584 while (bc_l > 0
1585 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1586 bc_l--;
1587
876fa593
JK
1588 /* Now do full processing of the found relevant range of elements. */
1589
1590 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1591 {
35df4500 1592 struct bp_location *bl = bp_location[bc];
876fa593
JK
1593 CORE_ADDR bp_addr = 0;
1594 int bp_size = 0;
1595 int bptoffset = 0;
1596
35df4500
TJB
1597 /* bp_location array has BL->OWNER always non-NULL. */
1598 if (bl->owner->type == bp_none)
8a3fe4f8 1599 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1600 bl->owner->number);
ffce0d52 1601
e5dd4106 1602 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1603 content. */
1604
35df4500
TJB
1605 if (bl->address >= bp_location_placed_address_before_address_max
1606 && memaddr + len <= (bl->address
1607 - bp_location_placed_address_before_address_max))
876fa593
JK
1608 break;
1609
35df4500 1610 if (!bp_location_has_shadow (bl))
c5aa993b 1611 continue;
6c95b8df 1612
9d497a19
PA
1613 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1614 memaddr, len, &bl->target_info, bl->gdbarch);
1615 }
c5aa993b 1616
9d497a19
PA
1617 /* Now process single-step breakpoints. These are not found in the
1618 bp_location array. */
1619 for (i = 0; i < 2; i++)
1620 {
1621 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
c5aa993b 1622
9d497a19
PA
1623 if (bp_tgt != NULL)
1624 {
1625 struct gdbarch *gdbarch = single_step_gdbarch[i];
c5aa993b 1626
9d497a19
PA
1627 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1628 memaddr, len, bp_tgt, gdbarch);
1629 }
1630 }
c906108c 1631}
9d497a19 1632
c906108c 1633\f
c5aa993b 1634
b775012e
LM
1635/* Return true if BPT is either a software breakpoint or a hardware
1636 breakpoint. */
1637
1638int
1639is_breakpoint (const struct breakpoint *bpt)
1640{
1641 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1642 || bpt->type == bp_hardware_breakpoint
1643 || bpt->type == bp_dprintf);
b775012e
LM
1644}
1645
60e1c644
PA
1646/* Return true if BPT is of any hardware watchpoint kind. */
1647
a5606eee 1648static int
d77f58be 1649is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1650{
1651 return (bpt->type == bp_hardware_watchpoint
1652 || bpt->type == bp_read_watchpoint
1653 || bpt->type == bp_access_watchpoint);
1654}
7270d8f2 1655
60e1c644
PA
1656/* Return true if BPT is of any watchpoint kind, hardware or
1657 software. */
1658
3a5c3e22 1659int
d77f58be 1660is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1661{
1662 return (is_hardware_watchpoint (bpt)
1663 || bpt->type == bp_watchpoint);
1664}
1665
3a5c3e22
PA
1666/* Returns true if the current thread and its running state are safe
1667 to evaluate or update watchpoint B. Watchpoints on local
1668 expressions need to be evaluated in the context of the thread that
1669 was current when the watchpoint was created, and, that thread needs
1670 to be stopped to be able to select the correct frame context.
1671 Watchpoints on global expressions can be evaluated on any thread,
1672 and in any state. It is presently left to the target allowing
1673 memory accesses when threads are running. */
f6bc2008
PA
1674
1675static int
3a5c3e22 1676watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1677{
d0d8b0c6
JK
1678 return (b->base.pspace == current_program_space
1679 && (ptid_equal (b->watchpoint_thread, null_ptid)
1680 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1681 && !is_executing (inferior_ptid))));
f6bc2008
PA
1682}
1683
d0fb5eae
JK
1684/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1685 associated bp_watchpoint_scope breakpoint. */
1686
1687static void
3a5c3e22 1688watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1689{
3a5c3e22 1690 struct breakpoint *b = &w->base;
d0fb5eae
JK
1691
1692 if (b->related_breakpoint != b)
1693 {
1694 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1695 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1696 b->related_breakpoint->disposition = disp_del_at_next_stop;
1697 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1698 b->related_breakpoint = b;
1699 }
1700 b->disposition = disp_del_at_next_stop;
1701}
1702
567e1b4e
JB
1703/* Assuming that B is a watchpoint:
1704 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1705 - Evaluate expression and store the result in B->val
567e1b4e
JB
1706 - Evaluate the condition if there is one, and store the result
1707 in b->loc->cond.
a5606eee
VP
1708 - Update the list of values that must be watched in B->loc.
1709
4a64f543
MS
1710 If the watchpoint disposition is disp_del_at_next_stop, then do
1711 nothing. If this is local watchpoint that is out of scope, delete
1712 it.
1713
1714 Even with `set breakpoint always-inserted on' the watchpoints are
1715 removed + inserted on each stop here. Normal breakpoints must
1716 never be removed because they might be missed by a running thread
1717 when debugging in non-stop mode. On the other hand, hardware
1718 watchpoints (is_hardware_watchpoint; processed here) are specific
1719 to each LWP since they are stored in each LWP's hardware debug
1720 registers. Therefore, such LWP must be stopped first in order to
1721 be able to modify its hardware watchpoints.
1722
1723 Hardware watchpoints must be reset exactly once after being
1724 presented to the user. It cannot be done sooner, because it would
1725 reset the data used to present the watchpoint hit to the user. And
1726 it must not be done later because it could display the same single
1727 watchpoint hit during multiple GDB stops. Note that the latter is
1728 relevant only to the hardware watchpoint types bp_read_watchpoint
1729 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1730 not user-visible - its hit is suppressed if the memory content has
1731 not changed.
1732
1733 The following constraints influence the location where we can reset
1734 hardware watchpoints:
1735
1736 * target_stopped_by_watchpoint and target_stopped_data_address are
1737 called several times when GDB stops.
1738
1739 [linux]
1740 * Multiple hardware watchpoints can be hit at the same time,
1741 causing GDB to stop. GDB only presents one hardware watchpoint
1742 hit at a time as the reason for stopping, and all the other hits
1743 are presented later, one after the other, each time the user
1744 requests the execution to be resumed. Execution is not resumed
1745 for the threads still having pending hit event stored in
1746 LWP_INFO->STATUS. While the watchpoint is already removed from
1747 the inferior on the first stop the thread hit event is kept being
1748 reported from its cached value by linux_nat_stopped_data_address
1749 until the real thread resume happens after the watchpoint gets
1750 presented and thus its LWP_INFO->STATUS gets reset.
1751
1752 Therefore the hardware watchpoint hit can get safely reset on the
1753 watchpoint removal from inferior. */
a79d3c27 1754
b40ce68a 1755static void
3a5c3e22 1756update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1757{
a5606eee 1758 int within_current_scope;
a5606eee 1759 struct frame_id saved_frame_id;
66076460 1760 int frame_saved;
a5606eee 1761
f6bc2008
PA
1762 /* If this is a local watchpoint, we only want to check if the
1763 watchpoint frame is in scope if the current thread is the thread
1764 that was used to create the watchpoint. */
1765 if (!watchpoint_in_thread_scope (b))
1766 return;
1767
3a5c3e22 1768 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1769 return;
1770
66076460 1771 frame_saved = 0;
a5606eee
VP
1772
1773 /* Determine if the watchpoint is within scope. */
1774 if (b->exp_valid_block == NULL)
1775 within_current_scope = 1;
1776 else
1777 {
b5db5dfc
UW
1778 struct frame_info *fi = get_current_frame ();
1779 struct gdbarch *frame_arch = get_frame_arch (fi);
1780 CORE_ADDR frame_pc = get_frame_pc (fi);
1781
1782 /* If we're in a function epilogue, unwinding may not work
1783 properly, so do not attempt to recreate locations at this
1784 point. See similar comments in watchpoint_check. */
1785 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1786 return;
66076460
DJ
1787
1788 /* Save the current frame's ID so we can restore it after
1789 evaluating the watchpoint expression on its own frame. */
1790 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1791 took a frame parameter, so that we didn't have to change the
1792 selected frame. */
1793 frame_saved = 1;
1794 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1795
a5606eee
VP
1796 fi = frame_find_by_id (b->watchpoint_frame);
1797 within_current_scope = (fi != NULL);
1798 if (within_current_scope)
1799 select_frame (fi);
1800 }
1801
b5db5dfc
UW
1802 /* We don't free locations. They are stored in the bp_location array
1803 and update_global_location_list will eventually delete them and
1804 remove breakpoints if needed. */
3a5c3e22 1805 b->base.loc = NULL;
b5db5dfc 1806
a5606eee
VP
1807 if (within_current_scope && reparse)
1808 {
bbc13ae3 1809 const char *s;
d63d0675 1810
a5606eee
VP
1811 if (b->exp)
1812 {
1813 xfree (b->exp);
1814 b->exp = NULL;
1815 }
d63d0675 1816 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1817 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1818 /* If the meaning of expression itself changed, the old value is
1819 no longer relevant. We don't want to report a watchpoint hit
1820 to the user when the old value and the new value may actually
1821 be completely different objects. */
1822 value_free (b->val);
fa4727a6
DJ
1823 b->val = NULL;
1824 b->val_valid = 0;
60e1c644
PA
1825
1826 /* Note that unlike with breakpoints, the watchpoint's condition
1827 expression is stored in the breakpoint object, not in the
1828 locations (re)created below. */
3a5c3e22 1829 if (b->base.cond_string != NULL)
60e1c644
PA
1830 {
1831 if (b->cond_exp != NULL)
1832 {
1833 xfree (b->cond_exp);
1834 b->cond_exp = NULL;
1835 }
1836
3a5c3e22 1837 s = b->base.cond_string;
1bb9788d 1838 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1839 }
a5606eee 1840 }
a5606eee
VP
1841
1842 /* If we failed to parse the expression, for example because
1843 it refers to a global variable in a not-yet-loaded shared library,
1844 don't try to insert watchpoint. We don't automatically delete
1845 such watchpoint, though, since failure to parse expression
1846 is different from out-of-scope watchpoint. */
e8369a73 1847 if (!target_has_execution)
2d134ed3
PA
1848 {
1849 /* Without execution, memory can't change. No use to try and
1850 set watchpoint locations. The watchpoint will be reset when
1851 the target gains execution, through breakpoint_re_set. */
e8369a73
AB
1852 if (!can_use_hw_watchpoints)
1853 {
1854 if (b->base.ops->works_in_software_mode (&b->base))
1855 b->base.type = bp_watchpoint;
1856 else
638aa5a1
AB
1857 error (_("Can't set read/access watchpoint when "
1858 "hardware watchpoints are disabled."));
e8369a73 1859 }
2d134ed3
PA
1860 }
1861 else if (within_current_scope && b->exp)
a5606eee 1862 {
0cf6dd15 1863 int pc = 0;
fa4727a6 1864 struct value *val_chain, *v, *result, *next;
2d134ed3 1865 struct program_space *frame_pspace;
a5606eee 1866
3a1115a0 1867 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
a5606eee 1868
a5606eee
VP
1869 /* Avoid setting b->val if it's already set. The meaning of
1870 b->val is 'the last value' user saw, and we should update
1871 it only if we reported that last value to user. As it
9c06b0b4
TJB
1872 happens, the code that reports it updates b->val directly.
1873 We don't keep track of the memory value for masked
1874 watchpoints. */
3a5c3e22 1875 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1876 {
1877 b->val = v;
1878 b->val_valid = 1;
1879 }
a5606eee 1880
2d134ed3
PA
1881 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1882
a5606eee 1883 /* Look at each value on the value chain. */
9fa40276 1884 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1885 {
1886 /* If it's a memory location, and GDB actually needed
1887 its contents to evaluate the expression, then we
fa4727a6
DJ
1888 must watch it. If the first value returned is
1889 still lazy, that means an error occurred reading it;
1890 watch it anyway in case it becomes readable. */
a5606eee 1891 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1892 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1893 {
1894 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1895
a5606eee
VP
1896 /* We only watch structs and arrays if user asked
1897 for it explicitly, never if they just happen to
1898 appear in the middle of some value chain. */
fa4727a6 1899 if (v == result
a5606eee
VP
1900 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1901 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1902 {
1903 CORE_ADDR addr;
744a8059 1904 int type;
a5606eee
VP
1905 struct bp_location *loc, **tmp;
1906
42ae5230 1907 addr = value_address (v);
a5606eee 1908 type = hw_write;
3a5c3e22 1909 if (b->base.type == bp_read_watchpoint)
a5606eee 1910 type = hw_read;
3a5c3e22 1911 else if (b->base.type == bp_access_watchpoint)
a5606eee 1912 type = hw_access;
3a5c3e22
PA
1913
1914 loc = allocate_bp_location (&b->base);
1915 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1916 ;
1917 *tmp = loc;
a6d9a66e 1918 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1919
1920 loc->pspace = frame_pspace;
a5606eee 1921 loc->address = addr;
744a8059 1922 loc->length = TYPE_LENGTH (value_type (v));
a5606eee
VP
1923 loc->watchpoint_type = type;
1924 }
1925 }
9fa40276
TJB
1926 }
1927
1928 /* Change the type of breakpoint between hardware assisted or
1929 an ordinary watchpoint depending on the hardware support
1930 and free hardware slots. REPARSE is set when the inferior
1931 is started. */
a9634178 1932 if (reparse)
9fa40276 1933 {
e09342b5 1934 int reg_cnt;
9fa40276
TJB
1935 enum bp_loc_type loc_type;
1936 struct bp_location *bl;
a5606eee 1937
a9634178 1938 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1939
1940 if (reg_cnt)
9fa40276
TJB
1941 {
1942 int i, target_resources_ok, other_type_used;
a1398e0c 1943 enum bptype type;
9fa40276 1944
a9634178
TJB
1945 /* Use an exact watchpoint when there's only one memory region to be
1946 watched, and only one debug register is needed to watch it. */
1947 b->exact = target_exact_watchpoints && reg_cnt == 1;
1948
9fa40276 1949 /* We need to determine how many resources are already
e09342b5
TJB
1950 used for all other hardware watchpoints plus this one
1951 to see if we still have enough resources to also fit
a1398e0c
PA
1952 this watchpoint in as well. */
1953
1954 /* If this is a software watchpoint, we try to turn it
1955 to a hardware one -- count resources as if B was of
1956 hardware watchpoint type. */
1957 type = b->base.type;
1958 if (type == bp_watchpoint)
1959 type = bp_hardware_watchpoint;
1960
1961 /* This watchpoint may or may not have been placed on
1962 the list yet at this point (it won't be in the list
1963 if we're trying to create it for the first time,
1964 through watch_command), so always account for it
1965 manually. */
1966
1967 /* Count resources used by all watchpoints except B. */
1968 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1969
1970 /* Add in the resources needed for B. */
1971 i += hw_watchpoint_use_count (&b->base);
1972
1973 target_resources_ok
1974 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1975 if (target_resources_ok <= 0)
a9634178 1976 {
3a5c3e22 1977 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1978
1979 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1980 error (_("Target does not support this type of "
1981 "hardware watchpoint."));
9c06b0b4
TJB
1982 else if (target_resources_ok < 0 && !sw_mode)
1983 error (_("There are not enough available hardware "
1984 "resources for this watchpoint."));
a1398e0c
PA
1985
1986 /* Downgrade to software watchpoint. */
1987 b->base.type = bp_watchpoint;
1988 }
1989 else
1990 {
1991 /* If this was a software watchpoint, we've just
1992 found we have enough resources to turn it to a
1993 hardware watchpoint. Otherwise, this is a
1994 nop. */
1995 b->base.type = type;
a9634178 1996 }
9fa40276 1997 }
3a5c3e22 1998 else if (!b->base.ops->works_in_software_mode (&b->base))
638aa5a1
AB
1999 {
2000 if (!can_use_hw_watchpoints)
2001 error (_("Can't set read/access watchpoint when "
2002 "hardware watchpoints are disabled."));
2003 else
2004 error (_("Expression cannot be implemented with "
2005 "read/access watchpoint."));
2006 }
9fa40276 2007 else
3a5c3e22 2008 b->base.type = bp_watchpoint;
9fa40276 2009
3a5c3e22 2010 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 2011 : bp_loc_hardware_watchpoint);
3a5c3e22 2012 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
2013 bl->loc_type = loc_type;
2014 }
2015
2016 for (v = val_chain; v; v = next)
2017 {
a5606eee
VP
2018 next = value_next (v);
2019 if (v != b->val)
2020 value_free (v);
2021 }
2022
c7437ca6
PA
2023 /* If a software watchpoint is not watching any memory, then the
2024 above left it without any location set up. But,
2025 bpstat_stop_status requires a location to be able to report
2026 stops, so make sure there's at least a dummy one. */
3a5c3e22 2027 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 2028 {
3a5c3e22
PA
2029 struct breakpoint *base = &b->base;
2030 base->loc = allocate_bp_location (base);
2031 base->loc->pspace = frame_pspace;
2032 base->loc->address = -1;
2033 base->loc->length = -1;
2034 base->loc->watchpoint_type = -1;
c7437ca6 2035 }
a5606eee
VP
2036 }
2037 else if (!within_current_scope)
7270d8f2 2038 {
ac74f770
MS
2039 printf_filtered (_("\
2040Watchpoint %d deleted because the program has left the block\n\
2041in which its expression is valid.\n"),
3a5c3e22 2042 b->base.number);
d0fb5eae 2043 watchpoint_del_at_next_stop (b);
7270d8f2 2044 }
a5606eee
VP
2045
2046 /* Restore the selected frame. */
66076460
DJ
2047 if (frame_saved)
2048 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
2049}
2050
a5606eee 2051
74960c60 2052/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
2053 inserted in the inferior. We don't differentiate the type of BL's owner
2054 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2055 breakpoint_ops is not defined, because in insert_bp_location,
2056 tracepoint's insert_location will not be called. */
74960c60 2057static int
35df4500 2058should_be_inserted (struct bp_location *bl)
74960c60 2059{
35df4500 2060 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
2061 return 0;
2062
35df4500 2063 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
2064 return 0;
2065
35df4500 2066 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
2067 return 0;
2068
f8eba3c6
TT
2069 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2070 return 0;
2071
56710373
PA
2072 /* This is set for example, when we're attached to the parent of a
2073 vfork, and have detached from the child. The child is running
2074 free, and we expect it to do an exec or exit, at which point the
2075 OS makes the parent schedulable again (and the target reports
2076 that the vfork is done). Until the child is done with the shared
2077 memory region, do not insert breakpoints in the parent, otherwise
2078 the child could still trip on the parent's breakpoints. Since
2079 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 2080 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
2081 return 0;
2082
31e77af2
PA
2083 /* Don't insert a breakpoint if we're trying to step past its
2084 location. */
2085 if ((bl->loc_type == bp_loc_software_breakpoint
2086 || bl->loc_type == bp_loc_hardware_breakpoint)
2087 && stepping_past_instruction_at (bl->pspace->aspace,
2088 bl->address))
2089 return 0;
2090
74960c60
VP
2091 return 1;
2092}
2093
934709f0
PW
2094/* Same as should_be_inserted but does the check assuming
2095 that the location is not duplicated. */
2096
2097static int
2098unduplicated_should_be_inserted (struct bp_location *bl)
2099{
2100 int result;
2101 const int save_duplicate = bl->duplicate;
2102
2103 bl->duplicate = 0;
2104 result = should_be_inserted (bl);
2105 bl->duplicate = save_duplicate;
2106 return result;
2107}
2108
b775012e
LM
2109/* Parses a conditional described by an expression COND into an
2110 agent expression bytecode suitable for evaluation
2111 by the bytecode interpreter. Return NULL if there was
2112 any error during parsing. */
2113
2114static struct agent_expr *
2115parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2116{
2117 struct agent_expr *aexpr = NULL;
b775012e
LM
2118 volatile struct gdb_exception ex;
2119
2120 if (!cond)
2121 return NULL;
2122
2123 /* We don't want to stop processing, so catch any errors
2124 that may show up. */
2125 TRY_CATCH (ex, RETURN_MASK_ERROR)
2126 {
2127 aexpr = gen_eval_for_expr (scope, cond);
2128 }
2129
2130 if (ex.reason < 0)
2131 {
2132 /* If we got here, it means the condition could not be parsed to a valid
2133 bytecode expression and thus can't be evaluated on the target's side.
2134 It's no use iterating through the conditions. */
2135 return NULL;
2136 }
2137
2138 /* We have a valid agent expression. */
2139 return aexpr;
2140}
2141
2142/* Based on location BL, create a list of breakpoint conditions to be
2143 passed on to the target. If we have duplicated locations with different
2144 conditions, we will add such conditions to the list. The idea is that the
2145 target will evaluate the list of conditions and will only notify GDB when
2146 one of them is true. */
2147
2148static void
2149build_target_condition_list (struct bp_location *bl)
2150{
2151 struct bp_location **locp = NULL, **loc2p;
2152 int null_condition_or_parse_error = 0;
2153 int modified = bl->needs_update;
2154 struct bp_location *loc;
2155
8b4f3082
PA
2156 /* Release conditions left over from a previous insert. */
2157 VEC_free (agent_expr_p, bl->target_info.conditions);
2158
b775012e
LM
2159 /* This is only meaningful if the target is
2160 evaluating conditions and if the user has
2161 opted for condition evaluation on the target's
2162 side. */
2163 if (gdb_evaluates_breakpoint_condition_p ()
2164 || !target_supports_evaluation_of_breakpoint_conditions ())
2165 return;
2166
2167 /* Do a first pass to check for locations with no assigned
2168 conditions or conditions that fail to parse to a valid agent expression
2169 bytecode. If any of these happen, then it's no use to send conditions
2170 to the target since this location will always trigger and generate a
2171 response back to GDB. */
2172 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2173 {
2174 loc = (*loc2p);
2175 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2176 {
2177 if (modified)
2178 {
2179 struct agent_expr *aexpr;
2180
2181 /* Re-parse the conditions since something changed. In that
2182 case we already freed the condition bytecodes (see
2183 force_breakpoint_reinsertion). We just
2184 need to parse the condition to bytecodes again. */
2185 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2186 loc->cond_bytecode = aexpr;
2187
2188 /* Check if we managed to parse the conditional expression
2189 correctly. If not, we will not send this condition
2190 to the target. */
2191 if (aexpr)
2192 continue;
2193 }
2194
2195 /* If we have a NULL bytecode expression, it means something
2196 went wrong or we have a null condition expression. */
2197 if (!loc->cond_bytecode)
2198 {
2199 null_condition_or_parse_error = 1;
2200 break;
2201 }
2202 }
2203 }
2204
2205 /* If any of these happened, it means we will have to evaluate the conditions
2206 for the location's address on gdb's side. It is no use keeping bytecodes
2207 for all the other duplicate locations, thus we free all of them here.
2208
2209 This is so we have a finer control over which locations' conditions are
2210 being evaluated by GDB or the remote stub. */
2211 if (null_condition_or_parse_error)
2212 {
2213 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2214 {
2215 loc = (*loc2p);
2216 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2217 {
2218 /* Only go as far as the first NULL bytecode is
2219 located. */
2220 if (!loc->cond_bytecode)
2221 return;
2222
2223 free_agent_expr (loc->cond_bytecode);
2224 loc->cond_bytecode = NULL;
2225 }
2226 }
2227 }
2228
2229 /* No NULL conditions or failed bytecode generation. Build a condition list
2230 for this location's address. */
2231 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2232 {
2233 loc = (*loc2p);
2234 if (loc->cond
2235 && is_breakpoint (loc->owner)
2236 && loc->pspace->num == bl->pspace->num
2237 && loc->owner->enable_state == bp_enabled
2238 && loc->enabled)
2239 /* Add the condition to the vector. This will be used later to send the
2240 conditions to the target. */
2241 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2242 loc->cond_bytecode);
2243 }
2244
2245 return;
2246}
2247
d3ce09f5
SS
2248/* Parses a command described by string CMD into an agent expression
2249 bytecode suitable for evaluation by the bytecode interpreter.
2250 Return NULL if there was any error during parsing. */
2251
2252static struct agent_expr *
2253parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2254{
2255 struct cleanup *old_cleanups = 0;
2256 struct expression *expr, **argvec;
2257 struct agent_expr *aexpr = NULL;
d3ce09f5 2258 volatile struct gdb_exception ex;
bbc13ae3
KS
2259 const char *cmdrest;
2260 const char *format_start, *format_end;
d3ce09f5
SS
2261 struct format_piece *fpieces;
2262 int nargs;
2263 struct gdbarch *gdbarch = get_current_arch ();
2264
2265 if (!cmd)
2266 return NULL;
2267
2268 cmdrest = cmd;
2269
2270 if (*cmdrest == ',')
2271 ++cmdrest;
bbc13ae3 2272 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2273
2274 if (*cmdrest++ != '"')
2275 error (_("No format string following the location"));
2276
2277 format_start = cmdrest;
2278
2279 fpieces = parse_format_string (&cmdrest);
2280
2281 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2282
2283 format_end = cmdrest;
2284
2285 if (*cmdrest++ != '"')
2286 error (_("Bad format string, non-terminated '\"'."));
2287
bbc13ae3 2288 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2289
2290 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2291 error (_("Invalid argument syntax"));
2292
2293 if (*cmdrest == ',')
2294 cmdrest++;
bbc13ae3 2295 cmdrest = skip_spaces_const (cmdrest);
d3ce09f5
SS
2296
2297 /* For each argument, make an expression. */
2298
2299 argvec = (struct expression **) alloca (strlen (cmd)
2300 * sizeof (struct expression *));
2301
2302 nargs = 0;
2303 while (*cmdrest != '\0')
2304 {
bbc13ae3 2305 const char *cmd1;
d3ce09f5
SS
2306
2307 cmd1 = cmdrest;
2308 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2309 argvec[nargs++] = expr;
2310 cmdrest = cmd1;
2311 if (*cmdrest == ',')
2312 ++cmdrest;
2313 }
2314
2315 /* We don't want to stop processing, so catch any errors
2316 that may show up. */
2317 TRY_CATCH (ex, RETURN_MASK_ERROR)
2318 {
2319 aexpr = gen_printf (scope, gdbarch, 0, 0,
2320 format_start, format_end - format_start,
2321 fpieces, nargs, argvec);
2322 }
2323
752eb8b4
TT
2324 do_cleanups (old_cleanups);
2325
d3ce09f5
SS
2326 if (ex.reason < 0)
2327 {
2328 /* If we got here, it means the command could not be parsed to a valid
2329 bytecode expression and thus can't be evaluated on the target's side.
2330 It's no use iterating through the other commands. */
2331 return NULL;
2332 }
2333
d3ce09f5
SS
2334 /* We have a valid agent expression, return it. */
2335 return aexpr;
2336}
2337
2338/* Based on location BL, create a list of breakpoint commands to be
2339 passed on to the target. If we have duplicated locations with
2340 different commands, we will add any such to the list. */
2341
2342static void
2343build_target_command_list (struct bp_location *bl)
2344{
2345 struct bp_location **locp = NULL, **loc2p;
2346 int null_command_or_parse_error = 0;
2347 int modified = bl->needs_update;
2348 struct bp_location *loc;
2349
8b4f3082
PA
2350 /* Release commands left over from a previous insert. */
2351 VEC_free (agent_expr_p, bl->target_info.tcommands);
2352
d3ce09f5
SS
2353 /* For now, limit to agent-style dprintf breakpoints. */
2354 if (bl->owner->type != bp_dprintf
2355 || strcmp (dprintf_style, dprintf_style_agent) != 0)
2356 return;
2357
2358 if (!target_can_run_breakpoint_commands ())
2359 return;
2360
2361 /* Do a first pass to check for locations with no assigned
2362 conditions or conditions that fail to parse to a valid agent expression
2363 bytecode. If any of these happen, then it's no use to send conditions
2364 to the target since this location will always trigger and generate a
2365 response back to GDB. */
2366 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2367 {
2368 loc = (*loc2p);
2369 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2370 {
2371 if (modified)
2372 {
2373 struct agent_expr *aexpr;
2374
2375 /* Re-parse the commands since something changed. In that
2376 case we already freed the command bytecodes (see
2377 force_breakpoint_reinsertion). We just
2378 need to parse the command to bytecodes again. */
2379 aexpr = parse_cmd_to_aexpr (bl->address,
2380 loc->owner->extra_string);
2381 loc->cmd_bytecode = aexpr;
2382
2383 if (!aexpr)
2384 continue;
2385 }
2386
2387 /* If we have a NULL bytecode expression, it means something
2388 went wrong or we have a null command expression. */
2389 if (!loc->cmd_bytecode)
2390 {
2391 null_command_or_parse_error = 1;
2392 break;
2393 }
2394 }
2395 }
2396
2397 /* If anything failed, then we're not doing target-side commands,
2398 and so clean up. */
2399 if (null_command_or_parse_error)
2400 {
2401 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2402 {
2403 loc = (*loc2p);
2404 if (is_breakpoint (loc->owner)
2405 && loc->pspace->num == bl->pspace->num)
2406 {
2407 /* Only go as far as the first NULL bytecode is
2408 located. */
40fb6c5e 2409 if (loc->cmd_bytecode == NULL)
d3ce09f5
SS
2410 return;
2411
40fb6c5e
HZ
2412 free_agent_expr (loc->cmd_bytecode);
2413 loc->cmd_bytecode = NULL;
d3ce09f5
SS
2414 }
2415 }
2416 }
2417
2418 /* No NULL commands or failed bytecode generation. Build a command list
2419 for this location's address. */
2420 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2421 {
2422 loc = (*loc2p);
2423 if (loc->owner->extra_string
2424 && is_breakpoint (loc->owner)
2425 && loc->pspace->num == bl->pspace->num
2426 && loc->owner->enable_state == bp_enabled
2427 && loc->enabled)
2428 /* Add the command to the vector. This will be used later
2429 to send the commands to the target. */
2430 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2431 loc->cmd_bytecode);
2432 }
2433
2434 bl->target_info.persist = 0;
2435 /* Maybe flag this location as persistent. */
2436 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2437 bl->target_info.persist = 1;
2438}
2439
35df4500
TJB
2440/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2441 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2442 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2443 Returns 0 for success, 1 if the bp_location type is not supported or
2444 -1 for failure.
879bfdc2 2445
4a64f543
MS
2446 NOTE drow/2003-09-09: This routine could be broken down to an
2447 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2448static int
35df4500 2449insert_bp_location (struct bp_location *bl,
26bb91f3 2450 struct ui_file *tmp_error_stream,
3fbb6ffa 2451 int *disabled_breaks,
dd61ec5c
MW
2452 int *hw_breakpoint_error,
2453 int *hw_bp_error_explained_already)
879bfdc2 2454{
0000e5cc
PA
2455 enum errors bp_err = GDB_NO_ERROR;
2456 const char *bp_err_message = NULL;
c90a6fb7 2457 volatile struct gdb_exception e;
879bfdc2 2458
b775012e 2459 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2460 return 0;
2461
35c63cd8
JB
2462 /* Note we don't initialize bl->target_info, as that wipes out
2463 the breakpoint location's shadow_contents if the breakpoint
2464 is still inserted at that location. This in turn breaks
2465 target_read_memory which depends on these buffers when
2466 a memory read is requested at the breakpoint location:
2467 Once the target_info has been wiped, we fail to see that
2468 we have a breakpoint inserted at that address and thus
2469 read the breakpoint instead of returning the data saved in
2470 the breakpoint location's shadow contents. */
35df4500
TJB
2471 bl->target_info.placed_address = bl->address;
2472 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2473 bl->target_info.length = bl->length;
8181d85f 2474
b775012e
LM
2475 /* When working with target-side conditions, we must pass all the conditions
2476 for the same breakpoint address down to the target since GDB will not
2477 insert those locations. With a list of breakpoint conditions, the target
2478 can decide when to stop and notify GDB. */
2479
2480 if (is_breakpoint (bl->owner))
2481 {
2482 build_target_condition_list (bl);
d3ce09f5
SS
2483 build_target_command_list (bl);
2484 /* Reset the modification marker. */
b775012e
LM
2485 bl->needs_update = 0;
2486 }
2487
35df4500
TJB
2488 if (bl->loc_type == bp_loc_software_breakpoint
2489 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2490 {
35df4500 2491 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2492 {
2493 /* If the explicitly specified breakpoint type
2494 is not hardware breakpoint, check the memory map to see
2495 if the breakpoint address is in read only memory or not.
4a64f543 2496
765dc015
VP
2497 Two important cases are:
2498 - location type is not hardware breakpoint, memory
2499 is readonly. We change the type of the location to
2500 hardware breakpoint.
4a64f543
MS
2501 - location type is hardware breakpoint, memory is
2502 read-write. This means we've previously made the
2503 location hardware one, but then the memory map changed,
2504 so we undo.
765dc015 2505
4a64f543
MS
2506 When breakpoints are removed, remove_breakpoints will use
2507 location types we've just set here, the only possible
2508 problem is that memory map has changed during running
2509 program, but it's not going to work anyway with current
2510 gdb. */
765dc015 2511 struct mem_region *mr
35df4500 2512 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
2513
2514 if (mr)
2515 {
2516 if (automatic_hardware_breakpoints)
2517 {
765dc015
VP
2518 enum bp_loc_type new_type;
2519
2520 if (mr->attrib.mode != MEM_RW)
2521 new_type = bp_loc_hardware_breakpoint;
2522 else
2523 new_type = bp_loc_software_breakpoint;
2524
35df4500 2525 if (new_type != bl->loc_type)
765dc015
VP
2526 {
2527 static int said = 0;
cc59ec59 2528
35df4500 2529 bl->loc_type = new_type;
765dc015
VP
2530 if (!said)
2531 {
3e43a32a
MS
2532 fprintf_filtered (gdb_stdout,
2533 _("Note: automatically using "
2534 "hardware breakpoints for "
2535 "read-only addresses.\n"));
765dc015
VP
2536 said = 1;
2537 }
2538 }
2539 }
35df4500 2540 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 2541 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
2542 warning (_("cannot set software breakpoint "
2543 "at readonly address %s"),
35df4500 2544 paddress (bl->gdbarch, bl->address));
765dc015
VP
2545 }
2546 }
2547
879bfdc2
DJ
2548 /* First check to see if we have to handle an overlay. */
2549 if (overlay_debugging == ovly_off
35df4500
TJB
2550 || bl->section == NULL
2551 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2552 {
2553 /* No overlay handling: just set the breakpoint. */
dd61ec5c
MW
2554 TRY_CATCH (e, RETURN_MASK_ALL)
2555 {
0000e5cc
PA
2556 int val;
2557
dd61ec5c 2558 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2559 if (val)
2560 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2561 }
2562 if (e.reason < 0)
2563 {
0000e5cc
PA
2564 bp_err = e.error;
2565 bp_err_message = e.message;
dd61ec5c 2566 }
879bfdc2
DJ
2567 }
2568 else
2569 {
4a64f543 2570 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2571 Shall we set a breakpoint at the LMA? */
2572 if (!overlay_events_enabled)
2573 {
2574 /* Yes -- overlay event support is not active,
2575 so we must try to set a breakpoint at the LMA.
2576 This will not work for a hardware breakpoint. */
35df4500 2577 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2578 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2579 bl->owner->number);
879bfdc2
DJ
2580 else
2581 {
35df4500
TJB
2582 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2583 bl->section);
879bfdc2 2584 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
2585 bl->overlay_target_info = bl->target_info;
2586 bl->overlay_target_info.placed_address = addr;
0000e5cc
PA
2587
2588 /* No overlay handling: just set the breakpoint. */
2589 TRY_CATCH (e, RETURN_MASK_ALL)
2590 {
2591 int val;
2592
2593 val = target_insert_breakpoint (bl->gdbarch,
2594 &bl->overlay_target_info);
2595 if (val)
2596 bp_err = GENERIC_ERROR;
2597 }
2598 if (e.reason < 0)
2599 {
2600 bp_err = e.error;
2601 bp_err_message = e.message;
2602 }
2603
2604 if (bp_err != GDB_NO_ERROR)
99361f52 2605 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2606 "Overlay breakpoint %d "
2607 "failed: in ROM?\n",
35df4500 2608 bl->owner->number);
879bfdc2
DJ
2609 }
2610 }
2611 /* Shall we set a breakpoint at the VMA? */
35df4500 2612 if (section_is_mapped (bl->section))
879bfdc2
DJ
2613 {
2614 /* Yes. This overlay section is mapped into memory. */
dd61ec5c
MW
2615 TRY_CATCH (e, RETURN_MASK_ALL)
2616 {
0000e5cc
PA
2617 int val;
2618
dd61ec5c 2619 val = bl->owner->ops->insert_location (bl);
0000e5cc
PA
2620 if (val)
2621 bp_err = GENERIC_ERROR;
dd61ec5c
MW
2622 }
2623 if (e.reason < 0)
2624 {
0000e5cc
PA
2625 bp_err = e.error;
2626 bp_err_message = e.message;
dd61ec5c 2627 }
879bfdc2
DJ
2628 }
2629 else
2630 {
2631 /* No. This breakpoint will not be inserted.
2632 No error, but do not mark the bp as 'inserted'. */
2633 return 0;
2634 }
2635 }
2636
0000e5cc 2637 if (bp_err != GDB_NO_ERROR)
879bfdc2
DJ
2638 {
2639 /* Can't set the breakpoint. */
0000e5cc
PA
2640
2641 /* In some cases, we might not be able to insert a
2642 breakpoint in a shared library that has already been
2643 removed, but we have not yet processed the shlib unload
2644 event. Unfortunately, some targets that implement
076855f9
PA
2645 breakpoint insertion themselves can't tell why the
2646 breakpoint insertion failed (e.g., the remote target
2647 doesn't define error codes), so we must treat generic
2648 errors as memory errors. */
0000e5cc 2649 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
076855f9 2650 && bl->loc_type == bp_loc_software_breakpoint
08351840
PA
2651 && (solib_name_from_address (bl->pspace, bl->address)
2652 || userloaded_objfile_contains_address_p (bl->pspace,
2653 bl->address)))
879bfdc2 2654 {
4a64f543 2655 /* See also: disable_breakpoints_in_shlibs. */
35df4500 2656 bl->shlib_disabled = 1;
8d3788bd 2657 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2658 if (!*disabled_breaks)
2659 {
2660 fprintf_unfiltered (tmp_error_stream,
2661 "Cannot insert breakpoint %d.\n",
2662 bl->owner->number);
2663 fprintf_unfiltered (tmp_error_stream,
2664 "Temporarily disabling shared "
2665 "library breakpoints:\n");
2666 }
2667 *disabled_breaks = 1;
879bfdc2 2668 fprintf_unfiltered (tmp_error_stream,
35df4500 2669 "breakpoint #%d\n", bl->owner->number);
0000e5cc 2670 return 0;
879bfdc2
DJ
2671 }
2672 else
879bfdc2 2673 {
35df4500 2674 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2675 {
0000e5cc
PA
2676 *hw_breakpoint_error = 1;
2677 *hw_bp_error_explained_already = bp_err_message != NULL;
dd61ec5c
MW
2678 fprintf_unfiltered (tmp_error_stream,
2679 "Cannot insert hardware breakpoint %d%s",
0000e5cc
PA
2680 bl->owner->number, bp_err_message ? ":" : ".\n");
2681 if (bp_err_message != NULL)
2682 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
879bfdc2
DJ
2683 }
2684 else
2685 {
0000e5cc
PA
2686 if (bp_err_message == NULL)
2687 {
2688 char *message
2689 = memory_error_message (TARGET_XFER_E_IO,
2690 bl->gdbarch, bl->address);
2691 struct cleanup *old_chain = make_cleanup (xfree, message);
2692
2693 fprintf_unfiltered (tmp_error_stream,
2694 "Cannot insert breakpoint %d.\n"
2695 "%s\n",
2696 bl->owner->number, message);
2697 do_cleanups (old_chain);
2698 }
2699 else
2700 {
2701 fprintf_unfiltered (tmp_error_stream,
2702 "Cannot insert breakpoint %d: %s\n",
2703 bl->owner->number,
2704 bp_err_message);
2705 }
879bfdc2 2706 }
0000e5cc 2707 return 1;
879bfdc2
DJ
2708
2709 }
2710 }
2711 else
35df4500 2712 bl->inserted = 1;
879bfdc2 2713
0000e5cc 2714 return 0;
879bfdc2
DJ
2715 }
2716
35df4500 2717 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2718 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2719 watchpoints. It's not clear that it's necessary... */
35df4500 2720 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2721 {
0000e5cc
PA
2722 int val;
2723
77b06cd7
TJB
2724 gdb_assert (bl->owner->ops != NULL
2725 && bl->owner->ops->insert_location != NULL);
2726
2727 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2728
2729 /* If trying to set a read-watchpoint, and it turns out it's not
2730 supported, try emulating one with an access watchpoint. */
35df4500 2731 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2732 {
2733 struct bp_location *loc, **loc_temp;
2734
2735 /* But don't try to insert it, if there's already another
2736 hw_access location that would be considered a duplicate
2737 of this one. */
2738 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2739 if (loc != bl
85d721b8 2740 && loc->watchpoint_type == hw_access
35df4500 2741 && watchpoint_locations_match (bl, loc))
85d721b8 2742 {
35df4500
TJB
2743 bl->duplicate = 1;
2744 bl->inserted = 1;
2745 bl->target_info = loc->target_info;
2746 bl->watchpoint_type = hw_access;
85d721b8
PA
2747 val = 0;
2748 break;
2749 }
2750
2751 if (val == 1)
2752 {
77b06cd7
TJB
2753 bl->watchpoint_type = hw_access;
2754 val = bl->owner->ops->insert_location (bl);
2755
2756 if (val)
2757 /* Back to the original value. */
2758 bl->watchpoint_type = hw_read;
85d721b8
PA
2759 }
2760 }
2761
35df4500 2762 bl->inserted = (val == 0);
879bfdc2
DJ
2763 }
2764
35df4500 2765 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2766 {
0000e5cc
PA
2767 int val;
2768
77b06cd7
TJB
2769 gdb_assert (bl->owner->ops != NULL
2770 && bl->owner->ops->insert_location != NULL);
2771
2772 val = bl->owner->ops->insert_location (bl);
2773 if (val)
2774 {
2775 bl->owner->enable_state = bp_disabled;
2776
2777 if (val == 1)
2778 warning (_("\
2779Error inserting catchpoint %d: Your system does not support this type\n\
2780of catchpoint."), bl->owner->number);
2781 else
2782 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2783 }
2784
2785 bl->inserted = (val == 0);
1640b821
DJ
2786
2787 /* We've already printed an error message if there was a problem
2788 inserting this catchpoint, and we've disabled the catchpoint,
2789 so just return success. */
2790 return 0;
879bfdc2
DJ
2791 }
2792
2793 return 0;
2794}
2795
6c95b8df
PA
2796/* This function is called when program space PSPACE is about to be
2797 deleted. It takes care of updating breakpoints to not reference
2798 PSPACE anymore. */
2799
2800void
2801breakpoint_program_space_exit (struct program_space *pspace)
2802{
2803 struct breakpoint *b, *b_temp;
876fa593 2804 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2805
2806 /* Remove any breakpoint that was set through this program space. */
2807 ALL_BREAKPOINTS_SAFE (b, b_temp)
2808 {
2809 if (b->pspace == pspace)
2810 delete_breakpoint (b);
2811 }
2812
2813 /* Breakpoints set through other program spaces could have locations
2814 bound to PSPACE as well. Remove those. */
876fa593 2815 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2816 {
2817 struct bp_location *tmp;
2818
2819 if (loc->pspace == pspace)
2820 {
2bdf28a0 2821 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2822 if (loc->owner->loc == loc)
2823 loc->owner->loc = loc->next;
2824 else
2825 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2826 if (tmp->next == loc)
2827 {
2828 tmp->next = loc->next;
2829 break;
2830 }
2831 }
2832 }
2833
2834 /* Now update the global location list to permanently delete the
2835 removed locations above. */
2836 update_global_location_list (0);
2837}
2838
74960c60
VP
2839/* Make sure all breakpoints are inserted in inferior.
2840 Throws exception on any error.
2841 A breakpoint that is already inserted won't be inserted
2842 again, so calling this function twice is safe. */
2843void
2844insert_breakpoints (void)
2845{
2846 struct breakpoint *bpt;
2847
2848 ALL_BREAKPOINTS (bpt)
2849 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2850 {
2851 struct watchpoint *w = (struct watchpoint *) bpt;
2852
2853 update_watchpoint (w, 0 /* don't reparse. */);
2854 }
74960c60 2855
b60e7edf 2856 update_global_location_list (1);
74960c60 2857
c35b1492
PA
2858 /* update_global_location_list does not insert breakpoints when
2859 always_inserted_mode is not enabled. Explicitly insert them
2860 now. */
2861 if (!breakpoints_always_inserted_mode ())
74960c60
VP
2862 insert_breakpoint_locations ();
2863}
2864
20388dd6
YQ
2865/* Invoke CALLBACK for each of bp_location. */
2866
2867void
2868iterate_over_bp_locations (walk_bp_location_callback callback)
2869{
2870 struct bp_location *loc, **loc_tmp;
2871
2872 ALL_BP_LOCATIONS (loc, loc_tmp)
2873 {
2874 callback (loc, NULL);
2875 }
2876}
2877
b775012e
LM
2878/* This is used when we need to synch breakpoint conditions between GDB and the
2879 target. It is the case with deleting and disabling of breakpoints when using
2880 always-inserted mode. */
2881
2882static void
2883update_inserted_breakpoint_locations (void)
2884{
2885 struct bp_location *bl, **blp_tmp;
2886 int error_flag = 0;
2887 int val = 0;
2888 int disabled_breaks = 0;
2889 int hw_breakpoint_error = 0;
dd61ec5c 2890 int hw_bp_details_reported = 0;
b775012e
LM
2891
2892 struct ui_file *tmp_error_stream = mem_fileopen ();
2893 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2894
2895 /* Explicitly mark the warning -- this will only be printed if
2896 there was an error. */
2897 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2898
2899 save_current_space_and_thread ();
2900
2901 ALL_BP_LOCATIONS (bl, blp_tmp)
2902 {
2903 /* We only want to update software breakpoints and hardware
2904 breakpoints. */
2905 if (!is_breakpoint (bl->owner))
2906 continue;
2907
2908 /* We only want to update locations that are already inserted
2909 and need updating. This is to avoid unwanted insertion during
2910 deletion of breakpoints. */
2911 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2912 continue;
2913
2914 switch_to_program_space_and_thread (bl->pspace);
2915
2916 /* For targets that support global breakpoints, there's no need
2917 to select an inferior to insert breakpoint to. In fact, even
2918 if we aren't attached to any process yet, we should still
2919 insert breakpoints. */
f5656ead 2920 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
b775012e
LM
2921 && ptid_equal (inferior_ptid, null_ptid))
2922 continue;
2923
2924 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 2925 &hw_breakpoint_error, &hw_bp_details_reported);
b775012e
LM
2926 if (val)
2927 error_flag = val;
2928 }
2929
2930 if (error_flag)
2931 {
2932 target_terminal_ours_for_output ();
2933 error_stream (tmp_error_stream);
2934 }
2935
2936 do_cleanups (cleanups);
2937}
2938
c30eee59 2939/* Used when starting or continuing the program. */
c906108c 2940
74960c60
VP
2941static void
2942insert_breakpoint_locations (void)
c906108c 2943{
a5606eee 2944 struct breakpoint *bpt;
35df4500 2945 struct bp_location *bl, **blp_tmp;
eacd795a 2946 int error_flag = 0;
c906108c 2947 int val = 0;
3fbb6ffa 2948 int disabled_breaks = 0;
81d0cc19 2949 int hw_breakpoint_error = 0;
dd61ec5c 2950 int hw_bp_error_explained_already = 0;
c906108c 2951
81d0cc19 2952 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 2953 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 2954
81d0cc19
GS
2955 /* Explicitly mark the warning -- this will only be printed if
2956 there was an error. */
2957 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
2958
2959 save_current_space_and_thread ();
2960
35df4500 2961 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2962 {
b775012e 2963 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2964 continue;
2965
4a64f543
MS
2966 /* There is no point inserting thread-specific breakpoints if
2967 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2968 has BL->OWNER always non-NULL. */
35df4500
TJB
2969 if (bl->owner->thread != -1
2970 && !valid_thread_id (bl->owner->thread))
f365de73
AS
2971 continue;
2972
35df4500 2973 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2974
2975 /* For targets that support global breakpoints, there's no need
2976 to select an inferior to insert breakpoint to. In fact, even
2977 if we aren't attached to any process yet, we should still
2978 insert breakpoints. */
f5656ead 2979 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
2980 && ptid_equal (inferior_ptid, null_ptid))
2981 continue;
2982
3fbb6ffa 2983 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
dd61ec5c 2984 &hw_breakpoint_error, &hw_bp_error_explained_already);
879bfdc2 2985 if (val)
eacd795a 2986 error_flag = val;
879bfdc2 2987 }
c906108c 2988
4a64f543
MS
2989 /* If we failed to insert all locations of a watchpoint, remove
2990 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2991 ALL_BREAKPOINTS (bpt)
2992 {
2993 int some_failed = 0;
2994 struct bp_location *loc;
2995
2996 if (!is_hardware_watchpoint (bpt))
2997 continue;
2998
d6b74ac4 2999 if (!breakpoint_enabled (bpt))
a5606eee 3000 continue;
74960c60
VP
3001
3002 if (bpt->disposition == disp_del_at_next_stop)
3003 continue;
a5606eee
VP
3004
3005 for (loc = bpt->loc; loc; loc = loc->next)
56710373 3006 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
3007 {
3008 some_failed = 1;
3009 break;
3010 }
3011 if (some_failed)
3012 {
3013 for (loc = bpt->loc; loc; loc = loc->next)
3014 if (loc->inserted)
3015 remove_breakpoint (loc, mark_uninserted);
3016
3017 hw_breakpoint_error = 1;
3018 fprintf_unfiltered (tmp_error_stream,
3019 "Could not insert hardware watchpoint %d.\n",
3020 bpt->number);
eacd795a 3021 error_flag = -1;
a5606eee
VP
3022 }
3023 }
3024
eacd795a 3025 if (error_flag)
81d0cc19
GS
3026 {
3027 /* If a hardware breakpoint or watchpoint was inserted, add a
3028 message about possibly exhausted resources. */
dd61ec5c 3029 if (hw_breakpoint_error && !hw_bp_error_explained_already)
81d0cc19 3030 {
c6510018
MS
3031 fprintf_unfiltered (tmp_error_stream,
3032 "Could not insert hardware breakpoints:\n\
3033You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 3034 }
81d0cc19
GS
3035 target_terminal_ours_for_output ();
3036 error_stream (tmp_error_stream);
3037 }
f7545552
TT
3038
3039 do_cleanups (cleanups);
c906108c
SS
3040}
3041
c30eee59
TJB
3042/* Used when the program stops.
3043 Returns zero if successful, or non-zero if there was a problem
3044 removing a breakpoint location. */
3045
c906108c 3046int
fba45db2 3047remove_breakpoints (void)
c906108c 3048{
35df4500 3049 struct bp_location *bl, **blp_tmp;
3a1bae8e 3050 int val = 0;
c906108c 3051
35df4500 3052 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3053 {
1e4d1764 3054 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 3055 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 3056 }
3a1bae8e 3057 return val;
c906108c
SS
3058}
3059
49fa26b0
PA
3060/* When a thread exits, remove breakpoints that are related to
3061 that thread. */
3062
3063static void
3064remove_threaded_breakpoints (struct thread_info *tp, int silent)
3065{
3066 struct breakpoint *b, *b_tmp;
3067
3068 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3069 {
96181529 3070 if (b->thread == tp->num && user_breakpoint_p (b))
49fa26b0
PA
3071 {
3072 b->disposition = disp_del_at_next_stop;
3073
3074 printf_filtered (_("\
46ecd527 3075Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
49fa26b0
PA
3076 b->number, tp->num);
3077
3078 /* Hide it from the user. */
3079 b->number = 0;
3080 }
3081 }
3082}
3083
6c95b8df
PA
3084/* Remove breakpoints of process PID. */
3085
3086int
3087remove_breakpoints_pid (int pid)
3088{
35df4500 3089 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
3090 int val;
3091 struct inferior *inf = find_inferior_pid (pid);
3092
35df4500 3093 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3094 {
35df4500 3095 if (bl->pspace != inf->pspace)
6c95b8df
PA
3096 continue;
3097
d3ce09f5
SS
3098 if (bl->owner->type == bp_dprintf)
3099 continue;
3100
35df4500 3101 if (bl->inserted)
6c95b8df 3102 {
35df4500 3103 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
3104 if (val != 0)
3105 return val;
3106 }
3107 }
3108 return 0;
3109}
3110
c906108c 3111int
fba45db2 3112reattach_breakpoints (int pid)
c906108c 3113{
6c95b8df 3114 struct cleanup *old_chain;
35df4500 3115 struct bp_location *bl, **blp_tmp;
c906108c 3116 int val;
86b887df 3117 struct ui_file *tmp_error_stream;
dd61ec5c 3118 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
6c95b8df
PA
3119 struct inferior *inf;
3120 struct thread_info *tp;
3121
3122 tp = any_live_thread_of_process (pid);
3123 if (tp == NULL)
3124 return 1;
3125
3126 inf = find_inferior_pid (pid);
3127 old_chain = save_inferior_ptid ();
3128
3129 inferior_ptid = tp->ptid;
a4954f26 3130
86b887df 3131 tmp_error_stream = mem_fileopen ();
a4954f26 3132 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 3133
35df4500 3134 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3135 {
35df4500 3136 if (bl->pspace != inf->pspace)
6c95b8df
PA
3137 continue;
3138
35df4500 3139 if (bl->inserted)
c5aa993b 3140 {
35df4500 3141 bl->inserted = 0;
dd61ec5c 3142 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
c5aa993b
JM
3143 if (val != 0)
3144 {
ce696e05 3145 do_cleanups (old_chain);
c5aa993b
JM
3146 return val;
3147 }
3148 }
3149 }
ce696e05 3150 do_cleanups (old_chain);
c906108c
SS
3151 return 0;
3152}
3153
e58b0e63
PA
3154static int internal_breakpoint_number = -1;
3155
84f4c1fe
PM
3156/* Set the breakpoint number of B, depending on the value of INTERNAL.
3157 If INTERNAL is non-zero, the breakpoint number will be populated
3158 from internal_breakpoint_number and that variable decremented.
e5dd4106 3159 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
3160 breakpoint_count and that value incremented. Internal breakpoints
3161 do not set the internal var bpnum. */
3162static void
3163set_breakpoint_number (int internal, struct breakpoint *b)
3164{
3165 if (internal)
3166 b->number = internal_breakpoint_number--;
3167 else
3168 {
3169 set_breakpoint_count (breakpoint_count + 1);
3170 b->number = breakpoint_count;
3171 }
3172}
3173
e62c965a 3174static struct breakpoint *
a6d9a66e 3175create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 3176 CORE_ADDR address, enum bptype type,
c0a91b2b 3177 const struct breakpoint_ops *ops)
e62c965a 3178{
e62c965a
PP
3179 struct symtab_and_line sal;
3180 struct breakpoint *b;
3181
4a64f543 3182 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
3183
3184 sal.pc = address;
3185 sal.section = find_pc_overlay (sal.pc);
6c95b8df 3186 sal.pspace = current_program_space;
e62c965a 3187
06edf0c0 3188 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
3189 b->number = internal_breakpoint_number--;
3190 b->disposition = disp_donttouch;
3191
3192 return b;
3193}
3194
17450429
PP
3195static const char *const longjmp_names[] =
3196 {
3197 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3198 };
3199#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3200
3201/* Per-objfile data private to breakpoint.c. */
3202struct breakpoint_objfile_data
3203{
3204 /* Minimal symbol for "_ovly_debug_event" (if any). */
3b7344d5 3205 struct bound_minimal_symbol overlay_msym;
17450429
PP
3206
3207 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3b7344d5 3208 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
17450429 3209
28106bc2
SDJ
3210 /* True if we have looked for longjmp probes. */
3211 int longjmp_searched;
3212
3213 /* SystemTap probe points for longjmp (if any). */
3214 VEC (probe_p) *longjmp_probes;
3215
17450429 3216 /* Minimal symbol for "std::terminate()" (if any). */
3b7344d5 3217 struct bound_minimal_symbol terminate_msym;
17450429
PP
3218
3219 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3b7344d5 3220 struct bound_minimal_symbol exception_msym;
28106bc2
SDJ
3221
3222 /* True if we have looked for exception probes. */
3223 int exception_searched;
3224
3225 /* SystemTap probe points for unwinding (if any). */
3226 VEC (probe_p) *exception_probes;
17450429
PP
3227};
3228
3229static const struct objfile_data *breakpoint_objfile_key;
3230
3231/* Minimal symbol not found sentinel. */
3232static struct minimal_symbol msym_not_found;
3233
3234/* Returns TRUE if MSYM point to the "not found" sentinel. */
3235
3236static int
3237msym_not_found_p (const struct minimal_symbol *msym)
3238{
3239 return msym == &msym_not_found;
3240}
3241
3242/* Return per-objfile data needed by breakpoint.c.
3243 Allocate the data if necessary. */
3244
3245static struct breakpoint_objfile_data *
3246get_breakpoint_objfile_data (struct objfile *objfile)
3247{
3248 struct breakpoint_objfile_data *bp_objfile_data;
3249
3250 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3251 if (bp_objfile_data == NULL)
3252 {
3253 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3254 sizeof (*bp_objfile_data));
3255
3256 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3257 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3258 }
3259 return bp_objfile_data;
3260}
3261
28106bc2
SDJ
3262static void
3263free_breakpoint_probes (struct objfile *obj, void *data)
3264{
3265 struct breakpoint_objfile_data *bp_objfile_data = data;
3266
3267 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3268 VEC_free (probe_p, bp_objfile_data->exception_probes);
3269}
3270
e62c965a 3271static void
af02033e 3272create_overlay_event_breakpoint (void)
e62c965a 3273{
69de3c6a 3274 struct objfile *objfile;
af02033e 3275 const char *const func_name = "_ovly_debug_event";
e62c965a 3276
69de3c6a
PP
3277 ALL_OBJFILES (objfile)
3278 {
3279 struct breakpoint *b;
17450429
PP
3280 struct breakpoint_objfile_data *bp_objfile_data;
3281 CORE_ADDR addr;
69de3c6a 3282
17450429
PP
3283 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3284
3b7344d5 3285 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
17450429
PP
3286 continue;
3287
3b7344d5 3288 if (bp_objfile_data->overlay_msym.minsym == NULL)
17450429 3289 {
3b7344d5 3290 struct bound_minimal_symbol m;
17450429
PP
3291
3292 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3293 if (m.minsym == NULL)
17450429
PP
3294 {
3295 /* Avoid future lookups in this objfile. */
3b7344d5 3296 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
17450429
PP
3297 continue;
3298 }
3299 bp_objfile_data->overlay_msym = m;
3300 }
e62c965a 3301
77e371c0 3302 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
17450429 3303 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3304 bp_overlay_event,
3305 &internal_breakpoint_ops);
69de3c6a 3306 b->addr_string = xstrdup (func_name);
e62c965a 3307
69de3c6a
PP
3308 if (overlay_debugging == ovly_auto)
3309 {
3310 b->enable_state = bp_enabled;
3311 overlay_events_enabled = 1;
3312 }
3313 else
3314 {
3315 b->enable_state = bp_disabled;
3316 overlay_events_enabled = 0;
3317 }
e62c965a
PP
3318 }
3319 update_global_location_list (1);
3320}
3321
0fd8e87f 3322static void
af02033e 3323create_longjmp_master_breakpoint (void)
0fd8e87f 3324{
6c95b8df 3325 struct program_space *pspace;
6c95b8df
PA
3326 struct cleanup *old_chain;
3327
3328 old_chain = save_current_program_space ();
0fd8e87f 3329
6c95b8df 3330 ALL_PSPACES (pspace)
af02033e
PP
3331 {
3332 struct objfile *objfile;
3333
3334 set_current_program_space (pspace);
3335
3336 ALL_OBJFILES (objfile)
0fd8e87f 3337 {
af02033e
PP
3338 int i;
3339 struct gdbarch *gdbarch;
17450429 3340 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 3341
af02033e 3342 gdbarch = get_objfile_arch (objfile);
0fd8e87f 3343
17450429
PP
3344 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3345
28106bc2
SDJ
3346 if (!bp_objfile_data->longjmp_searched)
3347 {
25f9533e
SDJ
3348 VEC (probe_p) *ret;
3349
3350 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3351 if (ret != NULL)
3352 {
3353 /* We are only interested in checking one element. */
3354 struct probe *p = VEC_index (probe_p, ret, 0);
3355
3356 if (!can_evaluate_probe_arguments (p))
3357 {
3358 /* We cannot use the probe interface here, because it does
3359 not know how to evaluate arguments. */
3360 VEC_free (probe_p, ret);
3361 ret = NULL;
3362 }
3363 }
3364 bp_objfile_data->longjmp_probes = ret;
28106bc2
SDJ
3365 bp_objfile_data->longjmp_searched = 1;
3366 }
3367
3368 if (bp_objfile_data->longjmp_probes != NULL)
3369 {
3370 int i;
3371 struct probe *probe;
3372 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3373
3374 for (i = 0;
3375 VEC_iterate (probe_p,
3376 bp_objfile_data->longjmp_probes,
3377 i, probe);
3378 ++i)
3379 {
3380 struct breakpoint *b;
3381
729662a5
TT
3382 b = create_internal_breakpoint (gdbarch,
3383 get_probe_address (probe,
3384 objfile),
28106bc2
SDJ
3385 bp_longjmp_master,
3386 &internal_breakpoint_ops);
3387 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3388 b->enable_state = bp_disabled;
3389 }
3390
3391 continue;
3392 }
3393
0569175e
TSD
3394 if (!gdbarch_get_longjmp_target_p (gdbarch))
3395 continue;
3396
17450429 3397 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
3398 {
3399 struct breakpoint *b;
af02033e 3400 const char *func_name;
17450429 3401 CORE_ADDR addr;
6c95b8df 3402
3b7344d5 3403 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
af02033e 3404 continue;
0fd8e87f 3405
17450429 3406 func_name = longjmp_names[i];
3b7344d5 3407 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
17450429 3408 {
3b7344d5 3409 struct bound_minimal_symbol m;
17450429
PP
3410
3411 m = lookup_minimal_symbol_text (func_name, objfile);
3b7344d5 3412 if (m.minsym == NULL)
17450429
PP
3413 {
3414 /* Prevent future lookups in this objfile. */
3b7344d5 3415 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
17450429
PP
3416 continue;
3417 }
3418 bp_objfile_data->longjmp_msym[i] = m;
3419 }
3420
77e371c0 3421 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3422 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3423 &internal_breakpoint_ops);
af02033e
PP
3424 b->addr_string = xstrdup (func_name);
3425 b->enable_state = bp_disabled;
3426 }
0fd8e87f 3427 }
af02033e 3428 }
0fd8e87f 3429 update_global_location_list (1);
6c95b8df
PA
3430
3431 do_cleanups (old_chain);
0fd8e87f
UW
3432}
3433
af02033e 3434/* Create a master std::terminate breakpoint. */
aa7d318d 3435static void
af02033e 3436create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3437{
3438 struct program_space *pspace;
aa7d318d 3439 struct cleanup *old_chain;
af02033e 3440 const char *const func_name = "std::terminate()";
aa7d318d
TT
3441
3442 old_chain = save_current_program_space ();
3443
3444 ALL_PSPACES (pspace)
17450429
PP
3445 {
3446 struct objfile *objfile;
3447 CORE_ADDR addr;
3448
3449 set_current_program_space (pspace);
3450
aa7d318d
TT
3451 ALL_OBJFILES (objfile)
3452 {
3453 struct breakpoint *b;
17450429 3454 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3455
17450429 3456 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3457
3b7344d5 3458 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
17450429
PP
3459 continue;
3460
3b7344d5 3461 if (bp_objfile_data->terminate_msym.minsym == NULL)
17450429 3462 {
3b7344d5 3463 struct bound_minimal_symbol m;
17450429
PP
3464
3465 m = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5
TT
3466 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3467 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
17450429
PP
3468 {
3469 /* Prevent future lookups in this objfile. */
3b7344d5 3470 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
17450429
PP
3471 continue;
3472 }
3473 bp_objfile_data->terminate_msym = m;
3474 }
aa7d318d 3475
77e371c0 3476 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
17450429 3477 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3478 bp_std_terminate_master,
3479 &internal_breakpoint_ops);
aa7d318d
TT
3480 b->addr_string = xstrdup (func_name);
3481 b->enable_state = bp_disabled;
3482 }
17450429
PP
3483 }
3484
aa7d318d
TT
3485 update_global_location_list (1);
3486
3487 do_cleanups (old_chain);
3488}
3489
186c406b
TT
3490/* Install a master breakpoint on the unwinder's debug hook. */
3491
70221824 3492static void
186c406b
TT
3493create_exception_master_breakpoint (void)
3494{
3495 struct objfile *objfile;
17450429 3496 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3497
3498 ALL_OBJFILES (objfile)
3499 {
17450429
PP
3500 struct breakpoint *b;
3501 struct gdbarch *gdbarch;
3502 struct breakpoint_objfile_data *bp_objfile_data;
3503 CORE_ADDR addr;
3504
3505 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3506
28106bc2
SDJ
3507 /* We prefer the SystemTap probe point if it exists. */
3508 if (!bp_objfile_data->exception_searched)
3509 {
25f9533e
SDJ
3510 VEC (probe_p) *ret;
3511
3512 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3513
3514 if (ret != NULL)
3515 {
3516 /* We are only interested in checking one element. */
3517 struct probe *p = VEC_index (probe_p, ret, 0);
3518
3519 if (!can_evaluate_probe_arguments (p))
3520 {
3521 /* We cannot use the probe interface here, because it does
3522 not know how to evaluate arguments. */
3523 VEC_free (probe_p, ret);
3524 ret = NULL;
3525 }
3526 }
3527 bp_objfile_data->exception_probes = ret;
28106bc2
SDJ
3528 bp_objfile_data->exception_searched = 1;
3529 }
3530
3531 if (bp_objfile_data->exception_probes != NULL)
3532 {
3533 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3534 int i;
3535 struct probe *probe;
3536
3537 for (i = 0;
3538 VEC_iterate (probe_p,
3539 bp_objfile_data->exception_probes,
3540 i, probe);
3541 ++i)
3542 {
3543 struct breakpoint *b;
3544
729662a5
TT
3545 b = create_internal_breakpoint (gdbarch,
3546 get_probe_address (probe,
3547 objfile),
28106bc2
SDJ
3548 bp_exception_master,
3549 &internal_breakpoint_ops);
3550 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3551 b->enable_state = bp_disabled;
3552 }
3553
3554 continue;
3555 }
3556
3557 /* Otherwise, try the hook function. */
3558
3b7344d5 3559 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
17450429
PP
3560 continue;
3561
3562 gdbarch = get_objfile_arch (objfile);
186c406b 3563
3b7344d5 3564 if (bp_objfile_data->exception_msym.minsym == NULL)
186c406b 3565 {
3b7344d5 3566 struct bound_minimal_symbol debug_hook;
186c406b 3567
17450429 3568 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3b7344d5 3569 if (debug_hook.minsym == NULL)
17450429 3570 {
3b7344d5 3571 bp_objfile_data->exception_msym.minsym = &msym_not_found;
17450429
PP
3572 continue;
3573 }
3574
3575 bp_objfile_data->exception_msym = debug_hook;
186c406b 3576 }
17450429 3577
77e371c0 3578 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
17450429
PP
3579 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3580 &current_target);
06edf0c0
PA
3581 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3582 &internal_breakpoint_ops);
17450429
PP
3583 b->addr_string = xstrdup (func_name);
3584 b->enable_state = bp_disabled;
186c406b
TT
3585 }
3586
3587 update_global_location_list (1);
3588}
3589
c906108c 3590void
fba45db2 3591update_breakpoints_after_exec (void)
c906108c 3592{
35df4500 3593 struct breakpoint *b, *b_tmp;
876fa593 3594 struct bp_location *bploc, **bplocp_tmp;
c906108c 3595
25b22b0a
PA
3596 /* We're about to delete breakpoints from GDB's lists. If the
3597 INSERTED flag is true, GDB will try to lift the breakpoints by
3598 writing the breakpoints' "shadow contents" back into memory. The
3599 "shadow contents" are NOT valid after an exec, so GDB should not
3600 do that. Instead, the target is responsible from marking
3601 breakpoints out as soon as it detects an exec. We don't do that
3602 here instead, because there may be other attempts to delete
3603 breakpoints after detecting an exec and before reaching here. */
876fa593 3604 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3605 if (bploc->pspace == current_program_space)
3606 gdb_assert (!bploc->inserted);
c906108c 3607
35df4500 3608 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3609 {
6c95b8df
PA
3610 if (b->pspace != current_program_space)
3611 continue;
3612
4a64f543 3613 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3614 if (b->type == bp_shlib_event)
3615 {
3616 delete_breakpoint (b);
3617 continue;
3618 }
c906108c 3619
4a64f543 3620 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3621 if (b->type == bp_jit_event)
3622 {
3623 delete_breakpoint (b);
3624 continue;
3625 }
3626
1900040c 3627 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3628 as must overlay event and longjmp master breakpoints. */
3629 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3630 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3631 || b->type == bp_exception_master)
c4093a6a
JM
3632 {
3633 delete_breakpoint (b);
3634 continue;
3635 }
3636
4a64f543 3637 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3638 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3639 {
3640 delete_breakpoint (b);
3641 continue;
3642 }
3643
611c83ae
PA
3644 /* Longjmp and longjmp-resume breakpoints are also meaningless
3645 after an exec. */
186c406b 3646 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3647 || b->type == bp_longjmp_call_dummy
186c406b 3648 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3649 {
3650 delete_breakpoint (b);
3651 continue;
3652 }
3653
ce78b96d
JB
3654 if (b->type == bp_catchpoint)
3655 {
3656 /* For now, none of the bp_catchpoint breakpoints need to
3657 do anything at this point. In the future, if some of
3658 the catchpoints need to something, we will need to add
3659 a new method, and call this method from here. */
3660 continue;
3661 }
3662
c5aa993b
JM
3663 /* bp_finish is a special case. The only way we ought to be able
3664 to see one of these when an exec() has happened, is if the user
3665 caught a vfork, and then said "finish". Ordinarily a finish just
3666 carries them to the call-site of the current callee, by setting
3667 a temporary bp there and resuming. But in this case, the finish
3668 will carry them entirely through the vfork & exec.
3669
3670 We don't want to allow a bp_finish to remain inserted now. But
3671 we can't safely delete it, 'cause finish_command has a handle to
3672 the bp on a bpstat, and will later want to delete it. There's a
3673 chance (and I've seen it happen) that if we delete the bp_finish
3674 here, that its storage will get reused by the time finish_command
3675 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3676 We really must allow finish_command to delete a bp_finish.
3677
e5dd4106 3678 In the absence of a general solution for the "how do we know
53a5351d
JM
3679 it's safe to delete something others may have handles to?"
3680 problem, what we'll do here is just uninsert the bp_finish, and
3681 let finish_command delete it.
3682
3683 (We know the bp_finish is "doomed" in the sense that it's
3684 momentary, and will be deleted as soon as finish_command sees
3685 the inferior stopped. So it doesn't matter that the bp's
3686 address is probably bogus in the new a.out, unlike e.g., the
3687 solib breakpoints.) */
c5aa993b 3688
c5aa993b
JM
3689 if (b->type == bp_finish)
3690 {
3691 continue;
3692 }
3693
3694 /* Without a symbolic address, we have little hope of the
3695 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3696 a.out. */
c5aa993b
JM
3697 if (b->addr_string == NULL)
3698 {
3699 delete_breakpoint (b);
3700 continue;
3701 }
c5aa993b 3702 }
1900040c 3703 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
3704 create_overlay_event_breakpoint ();
3705 create_longjmp_master_breakpoint ();
3706 create_std_terminate_master_breakpoint ();
186c406b 3707 create_exception_master_breakpoint ();
c906108c
SS
3708}
3709
3710int
d80ee84f 3711detach_breakpoints (ptid_t ptid)
c906108c 3712{
35df4500 3713 struct bp_location *bl, **blp_tmp;
3a1bae8e 3714 int val = 0;
ce696e05 3715 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3716 struct inferior *inf = current_inferior ();
c5aa993b 3717
dfd4cc63 3718 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
8a3fe4f8 3719 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3720
6c95b8df 3721 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
d80ee84f 3722 inferior_ptid = ptid;
35df4500 3723 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3724 {
35df4500 3725 if (bl->pspace != inf->pspace)
6c95b8df
PA
3726 continue;
3727
bd9673a4
PW
3728 /* This function must physically remove breakpoints locations
3729 from the specified ptid, without modifying the breakpoint
3730 package's state. Locations of type bp_loc_other are only
3731 maintained at GDB side. So, there is no need to remove
3732 these bp_loc_other locations. Moreover, removing these
3733 would modify the breakpoint package's state. */
3734 if (bl->loc_type == bp_loc_other)
3735 continue;
3736
35df4500
TJB
3737 if (bl->inserted)
3738 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3739 }
d03285ec
UW
3740
3741 /* Detach single-step breakpoints as well. */
3742 detach_single_step_breakpoints ();
3743
ce696e05 3744 do_cleanups (old_chain);
3a1bae8e 3745 return val;
c906108c
SS
3746}
3747
35df4500 3748/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3749 Note that this is used to detach breakpoints from a child fork.
3750 When we get here, the child isn't in the inferior list, and neither
3751 do we have objects to represent its address space --- we should
35df4500 3752 *not* look at bl->pspace->aspace here. */
6c95b8df 3753
c906108c 3754static int
35df4500 3755remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3756{
3757 int val;
c5aa993b 3758
35df4500
TJB
3759 /* BL is never in moribund_locations by our callers. */
3760 gdb_assert (bl->owner != NULL);
2bdf28a0 3761
35df4500 3762 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3763 /* Permanent breakpoints cannot be inserted or removed. */
3764 return 0;
3765
74960c60
VP
3766 /* The type of none suggests that owner is actually deleted.
3767 This should not ever happen. */
35df4500 3768 gdb_assert (bl->owner->type != bp_none);
0bde7532 3769
35df4500
TJB
3770 if (bl->loc_type == bp_loc_software_breakpoint
3771 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3772 {
c02f5703
MS
3773 /* "Normal" instruction breakpoint: either the standard
3774 trap-instruction bp (bp_breakpoint), or a
3775 bp_hardware_breakpoint. */
3776
3777 /* First check to see if we have to handle an overlay. */
3778 if (overlay_debugging == ovly_off
35df4500
TJB
3779 || bl->section == NULL
3780 || !(section_is_overlay (bl->section)))
c02f5703
MS
3781 {
3782 /* No overlay handling: just remove the breakpoint. */
08351840
PA
3783
3784 /* If we're trying to uninsert a memory breakpoint that we
3785 know is set in a dynamic object that is marked
3786 shlib_disabled, then either the dynamic object was
3787 removed with "remove-symbol-file" or with
3788 "nosharedlibrary". In the former case, we don't know
3789 whether another dynamic object might have loaded over the
3790 breakpoint's address -- the user might well let us know
3791 about it next with add-symbol-file (the whole point of
3792 OBJF_USERLOADED is letting the user manually maintain a
3793 list of dynamically loaded objects). If we have the
3794 breakpoint's shadow memory, that is, this is a software
3795 breakpoint managed by GDB, check whether the breakpoint
3796 is still inserted in memory, to avoid overwriting wrong
3797 code with stale saved shadow contents. Note that HW
3798 breakpoints don't have shadow memory, as they're
3799 implemented using a mechanism that is not dependent on
3800 being able to modify the target's memory, and as such
3801 they should always be removed. */
3802 if (bl->shlib_disabled
3803 && bl->target_info.shadow_len != 0
3804 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3805 val = 0;
3806 else
3807 val = bl->owner->ops->remove_location (bl);
c02f5703 3808 }
c906108c
SS
3809 else
3810 {
4a64f543 3811 /* This breakpoint is in an overlay section.
c02f5703
MS
3812 Did we set a breakpoint at the LMA? */
3813 if (!overlay_events_enabled)
3814 {
3815 /* Yes -- overlay event support is not active, so we
3816 should have set a breakpoint at the LMA. Remove it.
3817 */
c02f5703
MS
3818 /* Ignore any failures: if the LMA is in ROM, we will
3819 have already warned when we failed to insert it. */
35df4500
TJB
3820 if (bl->loc_type == bp_loc_hardware_breakpoint)
3821 target_remove_hw_breakpoint (bl->gdbarch,
3822 &bl->overlay_target_info);
c02f5703 3823 else
35df4500
TJB
3824 target_remove_breakpoint (bl->gdbarch,
3825 &bl->overlay_target_info);
c02f5703
MS
3826 }
3827 /* Did we set a breakpoint at the VMA?
3828 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3829 if (bl->inserted)
c906108c 3830 {
c02f5703
MS
3831 /* Yes -- remove it. Previously we did not bother to
3832 remove the breakpoint if the section had been
3833 unmapped, but let's not rely on that being safe. We
3834 don't know what the overlay manager might do. */
aa67235e
UW
3835
3836 /* However, we should remove *software* breakpoints only
3837 if the section is still mapped, or else we overwrite
3838 wrong code with the saved shadow contents. */
348d480f
PA
3839 if (bl->loc_type == bp_loc_hardware_breakpoint
3840 || section_is_mapped (bl->section))
3841 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3842 else
3843 val = 0;
c906108c 3844 }
c02f5703
MS
3845 else
3846 {
3847 /* No -- not inserted, so no need to remove. No error. */
3848 val = 0;
3849 }
c906108c 3850 }
879d1e6b 3851
08351840
PA
3852 /* In some cases, we might not be able to remove a breakpoint in
3853 a shared library that has already been removed, but we have
3854 not yet processed the shlib unload event. Similarly for an
3855 unloaded add-symbol-file object - the user might not yet have
3856 had the chance to remove-symbol-file it. shlib_disabled will
3857 be set if the library/object has already been removed, but
3858 the breakpoint hasn't been uninserted yet, e.g., after
3859 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3860 always-inserted mode. */
076855f9 3861 if (val
08351840
PA
3862 && (bl->loc_type == bp_loc_software_breakpoint
3863 && (bl->shlib_disabled
3864 || solib_name_from_address (bl->pspace, bl->address)
3865 || userloaded_objfile_contains_address_p (bl->pspace,
3866 bl->address))))
879d1e6b
UW
3867 val = 0;
3868
c906108c
SS
3869 if (val)
3870 return val;
35df4500 3871 bl->inserted = (is == mark_inserted);
c906108c 3872 }
35df4500 3873 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3874 {
77b06cd7
TJB
3875 gdb_assert (bl->owner->ops != NULL
3876 && bl->owner->ops->remove_location != NULL);
3877
35df4500 3878 bl->inserted = (is == mark_inserted);
77b06cd7 3879 bl->owner->ops->remove_location (bl);
2e70b7b9 3880
c906108c 3881 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 3882 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 3883 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3884 bl->owner->number);
c906108c 3885 }
35df4500
TJB
3886 else if (bl->owner->type == bp_catchpoint
3887 && breakpoint_enabled (bl->owner)
3888 && !bl->duplicate)
ce78b96d 3889 {
77b06cd7
TJB
3890 gdb_assert (bl->owner->ops != NULL
3891 && bl->owner->ops->remove_location != NULL);
ce78b96d 3892
77b06cd7 3893 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
3894 if (val)
3895 return val;
77b06cd7 3896
35df4500 3897 bl->inserted = (is == mark_inserted);
ce78b96d 3898 }
c906108c
SS
3899
3900 return 0;
3901}
3902
6c95b8df 3903static int
35df4500 3904remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
3905{
3906 int ret;
3907 struct cleanup *old_chain;
3908
35df4500
TJB
3909 /* BL is never in moribund_locations by our callers. */
3910 gdb_assert (bl->owner != NULL);
2bdf28a0 3911
35df4500 3912 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
3913 /* Permanent breakpoints cannot be inserted or removed. */
3914 return 0;
3915
3916 /* The type of none suggests that owner is actually deleted.
3917 This should not ever happen. */
35df4500 3918 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
3919
3920 old_chain = save_current_space_and_thread ();
3921
35df4500 3922 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3923
35df4500 3924 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
3925
3926 do_cleanups (old_chain);
3927 return ret;
3928}
3929
c906108c
SS
3930/* Clear the "inserted" flag in all breakpoints. */
3931
25b22b0a 3932void
fba45db2 3933mark_breakpoints_out (void)
c906108c 3934{
35df4500 3935 struct bp_location *bl, **blp_tmp;
c906108c 3936
35df4500
TJB
3937 ALL_BP_LOCATIONS (bl, blp_tmp)
3938 if (bl->pspace == current_program_space)
3939 bl->inserted = 0;
c906108c
SS
3940}
3941
53a5351d
JM
3942/* Clear the "inserted" flag in all breakpoints and delete any
3943 breakpoints which should go away between runs of the program.
c906108c
SS
3944
3945 Plus other such housekeeping that has to be done for breakpoints
3946 between runs.
3947
53a5351d
JM
3948 Note: this function gets called at the end of a run (by
3949 generic_mourn_inferior) and when a run begins (by
4a64f543 3950 init_wait_for_inferior). */
c906108c
SS
3951
3952
3953
3954void
fba45db2 3955breakpoint_init_inferior (enum inf_context context)
c906108c 3956{
35df4500
TJB
3957 struct breakpoint *b, *b_tmp;
3958 struct bp_location *bl, **blp_tmp;
1c5cfe86 3959 int ix;
6c95b8df 3960 struct program_space *pspace = current_program_space;
c906108c 3961
50c71eaf
PA
3962 /* If breakpoint locations are shared across processes, then there's
3963 nothing to do. */
f5656ead 3964 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
50c71eaf
PA
3965 return;
3966
35df4500 3967 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3968 {
35df4500
TJB
3969 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3970 if (bl->pspace == pspace
3971 && bl->owner->enable_state != bp_permanent)
3972 bl->inserted = 0;
6c95b8df 3973 }
075f6582 3974
35df4500 3975 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3976 {
6c95b8df
PA
3977 if (b->loc && b->loc->pspace != pspace)
3978 continue;
3979
c5aa993b
JM
3980 switch (b->type)
3981 {
3982 case bp_call_dummy:
e2e4d78b 3983 case bp_longjmp_call_dummy:
c906108c 3984
c5aa993b 3985 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3986 cause problems when the inferior is rerun, so we better get
3987 rid of it. */
3988
3989 case bp_watchpoint_scope:
3990
3991 /* Also get rid of scope breakpoints. */
3992
3993 case bp_shlib_event:
3994
3995 /* Also remove solib event breakpoints. Their addresses may
3996 have changed since the last time we ran the program.
3997 Actually we may now be debugging against different target;
3998 and so the solib backend that installed this breakpoint may
3999 not be used in by the target. E.g.,
4000
4001 (gdb) file prog-linux
4002 (gdb) run # native linux target
4003 ...
4004 (gdb) kill
4005 (gdb) file prog-win.exe
4006 (gdb) tar rem :9999 # remote Windows gdbserver.
4007 */
c906108c 4008
f59f708a
PA
4009 case bp_step_resume:
4010
4011 /* Also remove step-resume breakpoints. */
4012
c5aa993b
JM
4013 delete_breakpoint (b);
4014 break;
c906108c 4015
c5aa993b
JM
4016 case bp_watchpoint:
4017 case bp_hardware_watchpoint:
4018 case bp_read_watchpoint:
4019 case bp_access_watchpoint:
3a5c3e22
PA
4020 {
4021 struct watchpoint *w = (struct watchpoint *) b;
c906108c 4022
3a5c3e22
PA
4023 /* Likewise for watchpoints on local expressions. */
4024 if (w->exp_valid_block != NULL)
4025 delete_breakpoint (b);
4026 else if (context == inf_starting)
4027 {
4028 /* Reset val field to force reread of starting value in
4029 insert_breakpoints. */
4030 if (w->val)
4031 value_free (w->val);
4032 w->val = NULL;
4033 w->val_valid = 0;
c860120c 4034 }
3a5c3e22 4035 }
c5aa993b
JM
4036 break;
4037 default:
c5aa993b
JM
4038 break;
4039 }
4040 }
1c5cfe86
PA
4041
4042 /* Get rid of the moribund locations. */
35df4500
TJB
4043 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4044 decref_bp_location (&bl);
1c5cfe86 4045 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
4046}
4047
6c95b8df
PA
4048/* These functions concern about actual breakpoints inserted in the
4049 target --- to e.g. check if we need to do decr_pc adjustment or if
4050 we need to hop over the bkpt --- so we check for address space
4051 match, not program space. */
4052
c2c6d25f
JM
4053/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4054 exists at PC. It returns ordinary_breakpoint_here if it's an
4055 ordinary breakpoint, or permanent_breakpoint_here if it's a
4056 permanent breakpoint.
4057 - When continuing from a location with an ordinary breakpoint, we
4058 actually single step once before calling insert_breakpoints.
e5dd4106 4059 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
4060 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4061 the target, to advance the PC past the breakpoint. */
c906108c 4062
c2c6d25f 4063enum breakpoint_here
6c95b8df 4064breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 4065{
35df4500 4066 struct bp_location *bl, **blp_tmp;
c2c6d25f 4067 int any_breakpoint_here = 0;
c906108c 4068
35df4500 4069 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 4070 {
35df4500
TJB
4071 if (bl->loc_type != bp_loc_software_breakpoint
4072 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4073 continue;
4074
f1310107 4075 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
4076 if ((breakpoint_enabled (bl->owner)
4077 || bl->owner->enable_state == bp_permanent)
f1310107 4078 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4079 {
4080 if (overlay_debugging
35df4500
TJB
4081 && section_is_overlay (bl->section)
4082 && !section_is_mapped (bl->section))
075f6582 4083 continue; /* unmapped overlay -- can't be a match */
35df4500 4084 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
4085 return permanent_breakpoint_here;
4086 else
4087 any_breakpoint_here = 1;
4088 }
4089 }
c906108c 4090
c2c6d25f 4091 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
4092}
4093
1c5cfe86
PA
4094/* Return true if there's a moribund breakpoint at PC. */
4095
4096int
6c95b8df 4097moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
4098{
4099 struct bp_location *loc;
4100 int ix;
4101
4102 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 4103 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
4104 return 1;
4105
4106 return 0;
4107}
c2c6d25f 4108
c36b740a 4109/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
4110 inserted using regular breakpoint_chain / bp_location array
4111 mechanism. This does not check for single-step breakpoints, which
4112 are inserted and removed using direct target manipulation. */
c906108c
SS
4113
4114int
4a64f543
MS
4115regular_breakpoint_inserted_here_p (struct address_space *aspace,
4116 CORE_ADDR pc)
c906108c 4117{
35df4500 4118 struct bp_location *bl, **blp_tmp;
c906108c 4119
35df4500 4120 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4121 {
35df4500
TJB
4122 if (bl->loc_type != bp_loc_software_breakpoint
4123 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4124 continue;
4125
35df4500 4126 if (bl->inserted
f1310107 4127 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
4128 {
4129 if (overlay_debugging
35df4500
TJB
4130 && section_is_overlay (bl->section)
4131 && !section_is_mapped (bl->section))
075f6582
DJ
4132 continue; /* unmapped overlay -- can't be a match */
4133 else
4134 return 1;
4135 }
c5aa993b 4136 }
c36b740a
VP
4137 return 0;
4138}
4139
4140/* Returns non-zero iff there's either regular breakpoint
4141 or a single step breakpoint inserted at PC. */
4142
4143int
6c95b8df 4144breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 4145{
6c95b8df 4146 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 4147 return 1;
c906108c 4148
6c95b8df 4149 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4150 return 1;
4151
c906108c
SS
4152 return 0;
4153}
4154
4fa8626c
DJ
4155/* This function returns non-zero iff there is a software breakpoint
4156 inserted at PC. */
4157
4158int
3e43a32a
MS
4159software_breakpoint_inserted_here_p (struct address_space *aspace,
4160 CORE_ADDR pc)
4fa8626c 4161{
35df4500 4162 struct bp_location *bl, **blp_tmp;
4fa8626c 4163
35df4500 4164 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 4165 {
35df4500 4166 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
4167 continue;
4168
35df4500
TJB
4169 if (bl->inserted
4170 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 4171 aspace, pc))
4fa8626c
DJ
4172 {
4173 if (overlay_debugging
35df4500
TJB
4174 && section_is_overlay (bl->section)
4175 && !section_is_mapped (bl->section))
4fa8626c
DJ
4176 continue; /* unmapped overlay -- can't be a match */
4177 else
4178 return 1;
4179 }
4180 }
4181
1aafd4da 4182 /* Also check for software single-step breakpoints. */
6c95b8df 4183 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
4184 return 1;
4185
4fa8626c
DJ
4186 return 0;
4187}
4188
9093389c
PA
4189int
4190hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4191 CORE_ADDR addr, ULONGEST len)
4192{
4193 struct breakpoint *bpt;
4194
4195 ALL_BREAKPOINTS (bpt)
4196 {
4197 struct bp_location *loc;
4198
4199 if (bpt->type != bp_hardware_watchpoint
4200 && bpt->type != bp_access_watchpoint)
4201 continue;
4202
4203 if (!breakpoint_enabled (bpt))
4204 continue;
4205
4206 for (loc = bpt->loc; loc; loc = loc->next)
4207 if (loc->pspace->aspace == aspace && loc->inserted)
4208 {
4209 CORE_ADDR l, h;
4210
4211 /* Check for intersection. */
4212 l = max (loc->address, addr);
4213 h = min (loc->address + loc->length, addr + len);
4214 if (l < h)
4215 return 1;
4216 }
4217 }
4218 return 0;
4219}
4220
075f6582
DJ
4221/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4222 PC is valid for process/thread PTID. */
c906108c
SS
4223
4224int
6c95b8df
PA
4225breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4226 ptid_t ptid)
c906108c 4227{
35df4500 4228 struct bp_location *bl, **blp_tmp;
4a306c9a 4229 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 4230 int thread = -1;
4a306c9a 4231 int task = 0;
a6f1cd96 4232
35df4500 4233 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 4234 {
35df4500
TJB
4235 if (bl->loc_type != bp_loc_software_breakpoint
4236 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
4237 continue;
4238
35df4500
TJB
4239 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
4240 if (!breakpoint_enabled (bl->owner)
4241 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
4242 continue;
4243
f1310107 4244 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
4245 continue;
4246
35df4500 4247 if (bl->owner->thread != -1)
075f6582 4248 {
a6f1cd96
JB
4249 /* This is a thread-specific breakpoint. Check that ptid
4250 matches that thread. If thread hasn't been computed yet,
4251 it is now time to do so. */
4252 if (thread == -1)
4253 thread = pid_to_thread_id (ptid);
35df4500 4254 if (bl->owner->thread != thread)
a6f1cd96 4255 continue;
075f6582 4256 }
a6f1cd96 4257
35df4500 4258 if (bl->owner->task != 0)
4a306c9a
JB
4259 {
4260 /* This is a task-specific breakpoint. Check that ptid
4261 matches that task. If task hasn't been computed yet,
4262 it is now time to do so. */
4263 if (task == 0)
4264 task = ada_get_task_number (ptid);
35df4500 4265 if (bl->owner->task != task)
4a306c9a
JB
4266 continue;
4267 }
4268
a6f1cd96 4269 if (overlay_debugging
35df4500
TJB
4270 && section_is_overlay (bl->section)
4271 && !section_is_mapped (bl->section))
a6f1cd96
JB
4272 continue; /* unmapped overlay -- can't be a match */
4273
4274 return 1;
c5aa993b 4275 }
c906108c
SS
4276
4277 return 0;
4278}
c906108c 4279\f
c5aa993b 4280
c906108c
SS
4281/* bpstat stuff. External routines' interfaces are documented
4282 in breakpoint.h. */
4283
4284int
c326b90e 4285is_catchpoint (struct breakpoint *ep)
c906108c 4286{
533be4dd 4287 return (ep->type == bp_catchpoint);
c906108c
SS
4288}
4289
f431efe5
PA
4290/* Frees any storage that is part of a bpstat. Does not walk the
4291 'next' chain. */
4292
4293static void
198757a8
VP
4294bpstat_free (bpstat bs)
4295{
4296 if (bs->old_val != NULL)
4297 value_free (bs->old_val);
9add0f1b 4298 decref_counted_command_line (&bs->commands);
f431efe5 4299 decref_bp_location (&bs->bp_location_at);
198757a8
VP
4300 xfree (bs);
4301}
4302
c906108c
SS
4303/* Clear a bpstat so that it says we are not at any breakpoint.
4304 Also free any storage that is part of a bpstat. */
4305
4306void
fba45db2 4307bpstat_clear (bpstat *bsp)
c906108c
SS
4308{
4309 bpstat p;
4310 bpstat q;
4311
4312 if (bsp == 0)
4313 return;
4314 p = *bsp;
4315 while (p != NULL)
4316 {
4317 q = p->next;
198757a8 4318 bpstat_free (p);
c906108c
SS
4319 p = q;
4320 }
4321 *bsp = NULL;
4322}
4323
4324/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4325 is part of the bpstat is copied as well. */
4326
4327bpstat
fba45db2 4328bpstat_copy (bpstat bs)
c906108c
SS
4329{
4330 bpstat p = NULL;
4331 bpstat tmp;
4332 bpstat retval = NULL;
4333
4334 if (bs == NULL)
4335 return bs;
4336
4337 for (; bs != NULL; bs = bs->next)
4338 {
4339 tmp = (bpstat) xmalloc (sizeof (*tmp));
4340 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 4341 incref_counted_command_line (tmp->commands);
f431efe5 4342 incref_bp_location (tmp->bp_location_at);
31cc81e9 4343 if (bs->old_val != NULL)
3c3185ac
JK
4344 {
4345 tmp->old_val = value_copy (bs->old_val);
4346 release_value (tmp->old_val);
4347 }
31cc81e9 4348
c906108c
SS
4349 if (p == NULL)
4350 /* This is the first thing in the chain. */
4351 retval = tmp;
4352 else
4353 p->next = tmp;
4354 p = tmp;
4355 }
4356 p->next = NULL;
4357 return retval;
4358}
4359
4a64f543 4360/* Find the bpstat associated with this breakpoint. */
c906108c
SS
4361
4362bpstat
fba45db2 4363bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 4364{
c5aa993b
JM
4365 if (bsp == NULL)
4366 return NULL;
c906108c 4367
c5aa993b
JM
4368 for (; bsp != NULL; bsp = bsp->next)
4369 {
f431efe5 4370 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
4371 return bsp;
4372 }
c906108c
SS
4373 return NULL;
4374}
4375
ab04a2af
TT
4376/* See breakpoint.h. */
4377
47591c29 4378int
427cd150 4379bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
ab04a2af 4380{
ab04a2af
TT
4381 for (; bsp != NULL; bsp = bsp->next)
4382 {
427cd150
TT
4383 if (bsp->breakpoint_at == NULL)
4384 {
4385 /* A moribund location can never explain a signal other than
4386 GDB_SIGNAL_TRAP. */
4387 if (sig == GDB_SIGNAL_TRAP)
47591c29 4388 return 1;
427cd150
TT
4389 }
4390 else
47591c29
PA
4391 {
4392 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4393 sig))
4394 return 1;
4395 }
ab04a2af
TT
4396 }
4397
47591c29 4398 return 0;
ab04a2af
TT
4399}
4400
4a64f543
MS
4401/* Put in *NUM the breakpoint number of the first breakpoint we are
4402 stopped at. *BSP upon return is a bpstat which points to the
4403 remaining breakpoints stopped at (but which is not guaranteed to be
4404 good for anything but further calls to bpstat_num).
4405
8671a17b
PA
4406 Return 0 if passed a bpstat which does not indicate any breakpoints.
4407 Return -1 if stopped at a breakpoint that has been deleted since
4408 we set it.
4409 Return 1 otherwise. */
c906108c
SS
4410
4411int
8671a17b 4412bpstat_num (bpstat *bsp, int *num)
c906108c
SS
4413{
4414 struct breakpoint *b;
4415
4416 if ((*bsp) == NULL)
4417 return 0; /* No more breakpoint values */
8671a17b 4418
4a64f543
MS
4419 /* We assume we'll never have several bpstats that correspond to a
4420 single breakpoint -- otherwise, this function might return the
4421 same number more than once and this will look ugly. */
f431efe5 4422 b = (*bsp)->breakpoint_at;
8671a17b
PA
4423 *bsp = (*bsp)->next;
4424 if (b == NULL)
4425 return -1; /* breakpoint that's been deleted since */
4426
4427 *num = b->number; /* We have its number */
4428 return 1;
c906108c
SS
4429}
4430
e93ca019 4431/* See breakpoint.h. */
c906108c
SS
4432
4433void
e93ca019 4434bpstat_clear_actions (void)
c906108c 4435{
e93ca019
JK
4436 struct thread_info *tp;
4437 bpstat bs;
4438
4439 if (ptid_equal (inferior_ptid, null_ptid))
4440 return;
4441
4442 tp = find_thread_ptid (inferior_ptid);
4443 if (tp == NULL)
4444 return;
4445
4446 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 4447 {
9add0f1b 4448 decref_counted_command_line (&bs->commands);
abf85f46 4449
c906108c
SS
4450 if (bs->old_val != NULL)
4451 {
4452 value_free (bs->old_val);
4453 bs->old_val = NULL;
4454 }
4455 }
4456}
4457
f3b1572e
PA
4458/* Called when a command is about to proceed the inferior. */
4459
4460static void
4461breakpoint_about_to_proceed (void)
4462{
4463 if (!ptid_equal (inferior_ptid, null_ptid))
4464 {
4465 struct thread_info *tp = inferior_thread ();
4466
4467 /* Allow inferior function calls in breakpoint commands to not
4468 interrupt the command list. When the call finishes
4469 successfully, the inferior will be standing at the same
4470 breakpoint as if nothing happened. */
16c381f0 4471 if (tp->control.in_infcall)
f3b1572e
PA
4472 return;
4473 }
4474
4475 breakpoint_proceeded = 1;
4476}
4477
4a64f543
MS
4478/* Stub for cleaning up our state if we error-out of a breakpoint
4479 command. */
c906108c 4480static void
4efb68b1 4481cleanup_executing_breakpoints (void *ignore)
c906108c
SS
4482{
4483 executing_breakpoint_commands = 0;
4484}
4485
abf85f46
JK
4486/* Return non-zero iff CMD as the first line of a command sequence is `silent'
4487 or its equivalent. */
4488
4489static int
4490command_line_is_silent (struct command_line *cmd)
4491{
4492 return cmd && (strcmp ("silent", cmd->line) == 0
4493 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4494}
4495
4a64f543
MS
4496/* Execute all the commands associated with all the breakpoints at
4497 this location. Any of these commands could cause the process to
4498 proceed beyond this point, etc. We look out for such changes by
4499 checking the global "breakpoint_proceeded" after each command.
c906108c 4500
347bddb7
PA
4501 Returns true if a breakpoint command resumed the inferior. In that
4502 case, it is the caller's responsibility to recall it again with the
4503 bpstat of the current thread. */
4504
4505static int
4506bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4507{
4508 bpstat bs;
4509 struct cleanup *old_chain;
347bddb7 4510 int again = 0;
c906108c
SS
4511
4512 /* Avoid endless recursion if a `source' command is contained
4513 in bs->commands. */
4514 if (executing_breakpoint_commands)
347bddb7 4515 return 0;
c906108c
SS
4516
4517 executing_breakpoint_commands = 1;
4518 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4519
cf6c5ffb
TT
4520 prevent_dont_repeat ();
4521
4a64f543 4522 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4523 bs = *bsp;
4524
4525 breakpoint_proceeded = 0;
4526 for (; bs != NULL; bs = bs->next)
4527 {
9add0f1b 4528 struct counted_command_line *ccmd;
6c50ab1c
JB
4529 struct command_line *cmd;
4530 struct cleanup *this_cmd_tree_chain;
4531
4532 /* Take ownership of the BSP's command tree, if it has one.
4533
4534 The command tree could legitimately contain commands like
4535 'step' and 'next', which call clear_proceed_status, which
4536 frees stop_bpstat's command tree. To make sure this doesn't
4537 free the tree we're executing out from under us, we need to
4538 take ownership of the tree ourselves. Since a given bpstat's
4539 commands are only executed once, we don't need to copy it; we
4540 can clear the pointer in the bpstat, and make sure we free
4541 the tree when we're done. */
9add0f1b
TT
4542 ccmd = bs->commands;
4543 bs->commands = NULL;
abf85f46
JK
4544 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4545 cmd = ccmd ? ccmd->commands : NULL;
4546 if (command_line_is_silent (cmd))
4547 {
4548 /* The action has been already done by bpstat_stop_status. */
4549 cmd = cmd->next;
4550 }
6c50ab1c 4551
c906108c
SS
4552 while (cmd != NULL)
4553 {
4554 execute_control_command (cmd);
4555
4556 if (breakpoint_proceeded)
4557 break;
4558 else
4559 cmd = cmd->next;
4560 }
6c50ab1c
JB
4561
4562 /* We can free this command tree now. */
4563 do_cleanups (this_cmd_tree_chain);
4564
c906108c 4565 if (breakpoint_proceeded)
32c1e744
VP
4566 {
4567 if (target_can_async_p ())
347bddb7
PA
4568 /* If we are in async mode, then the target might be still
4569 running, not stopped at any breakpoint, so nothing for
4570 us to do here -- just return to the event loop. */
4571 ;
32c1e744
VP
4572 else
4573 /* In sync mode, when execute_control_command returns
4574 we're already standing on the next breakpoint.
347bddb7
PA
4575 Breakpoint commands for that stop were not run, since
4576 execute_command does not run breakpoint commands --
4577 only command_line_handler does, but that one is not
4578 involved in execution of breakpoint commands. So, we
4579 can now execute breakpoint commands. It should be
4580 noted that making execute_command do bpstat actions is
4581 not an option -- in this case we'll have recursive
4582 invocation of bpstat for each breakpoint with a
4583 command, and can easily blow up GDB stack. Instead, we
4584 return true, which will trigger the caller to recall us
4585 with the new stop_bpstat. */
4586 again = 1;
4587 break;
32c1e744 4588 }
c906108c 4589 }
c2b8ed2c 4590 do_cleanups (old_chain);
347bddb7
PA
4591 return again;
4592}
4593
4594void
4595bpstat_do_actions (void)
4596{
353d1d73
JK
4597 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4598
347bddb7
PA
4599 /* Do any commands attached to breakpoint we are stopped at. */
4600 while (!ptid_equal (inferior_ptid, null_ptid)
4601 && target_has_execution
4602 && !is_exited (inferior_ptid)
4603 && !is_executing (inferior_ptid))
4604 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4605 and only return when it is stopped at the next breakpoint, we
4606 keep doing breakpoint actions until it returns false to
4607 indicate the inferior was not resumed. */
16c381f0 4608 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4609 break;
353d1d73
JK
4610
4611 discard_cleanups (cleanup_if_error);
c906108c
SS
4612}
4613
fa4727a6
DJ
4614/* Print out the (old or new) value associated with a watchpoint. */
4615
4616static void
4617watchpoint_value_print (struct value *val, struct ui_file *stream)
4618{
4619 if (val == NULL)
4620 fprintf_unfiltered (stream, _("<unreadable>"));
4621 else
79a45b7d
TT
4622 {
4623 struct value_print_options opts;
4624 get_user_print_options (&opts);
4625 value_print (val, stream, &opts);
4626 }
fa4727a6
DJ
4627}
4628
e514a9d6 4629/* Generic routine for printing messages indicating why we
4a64f543 4630 stopped. The behavior of this function depends on the value
e514a9d6
JM
4631 'print_it' in the bpstat structure. Under some circumstances we
4632 may decide not to print anything here and delegate the task to
4a64f543 4633 normal_stop(). */
e514a9d6
JM
4634
4635static enum print_stop_action
4636print_bp_stop_message (bpstat bs)
4637{
4638 switch (bs->print_it)
4639 {
4640 case print_it_noop:
4a64f543 4641 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4642 return PRINT_UNKNOWN;
4643 break;
4644
4645 case print_it_done:
4646 /* We still want to print the frame, but we already printed the
4a64f543 4647 relevant messages. */
e514a9d6
JM
4648 return PRINT_SRC_AND_LOC;
4649 break;
4650
4651 case print_it_normal:
4f8d1dc6 4652 {
f431efe5
PA
4653 struct breakpoint *b = bs->breakpoint_at;
4654
1a6a67de
TJB
4655 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4656 which has since been deleted. */
4657 if (b == NULL)
4658 return PRINT_UNKNOWN;
4659
348d480f
PA
4660 /* Normal case. Call the breakpoint's print_it method. */
4661 return b->ops->print_it (bs);
4f8d1dc6 4662 }
348d480f 4663 break;
3086aeae 4664
e514a9d6 4665 default:
8e65ff28 4666 internal_error (__FILE__, __LINE__,
e2e0b3e5 4667 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4668 break;
c906108c 4669 }
c906108c
SS
4670}
4671
edcc5120
TT
4672/* A helper function that prints a shared library stopped event. */
4673
4674static void
4675print_solib_event (int is_catchpoint)
4676{
4677 int any_deleted
4678 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4679 int any_added
4680 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4681
4682 if (!is_catchpoint)
4683 {
4684 if (any_added || any_deleted)
4685 ui_out_text (current_uiout,
4686 _("Stopped due to shared library event:\n"));
4687 else
4688 ui_out_text (current_uiout,
4689 _("Stopped due to shared library event (no "
4690 "libraries added or removed)\n"));
4691 }
4692
4693 if (ui_out_is_mi_like_p (current_uiout))
4694 ui_out_field_string (current_uiout, "reason",
4695 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4696
4697 if (any_deleted)
4698 {
4699 struct cleanup *cleanup;
4700 char *name;
4701 int ix;
4702
4703 ui_out_text (current_uiout, _(" Inferior unloaded "));
4704 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4705 "removed");
4706 for (ix = 0;
4707 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4708 ix, name);
4709 ++ix)
4710 {
4711 if (ix > 0)
4712 ui_out_text (current_uiout, " ");
4713 ui_out_field_string (current_uiout, "library", name);
4714 ui_out_text (current_uiout, "\n");
4715 }
4716
4717 do_cleanups (cleanup);
4718 }
4719
4720 if (any_added)
4721 {
4722 struct so_list *iter;
4723 int ix;
4724 struct cleanup *cleanup;
4725
4726 ui_out_text (current_uiout, _(" Inferior loaded "));
4727 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4728 "added");
4729 for (ix = 0;
4730 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4731 ix, iter);
4732 ++ix)
4733 {
4734 if (ix > 0)
4735 ui_out_text (current_uiout, " ");
4736 ui_out_field_string (current_uiout, "library", iter->so_name);
4737 ui_out_text (current_uiout, "\n");
4738 }
4739
4740 do_cleanups (cleanup);
4741 }
4742}
4743
e514a9d6
JM
4744/* Print a message indicating what happened. This is called from
4745 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4746 list - a list of the eventpoints that caused this stop. KIND is
4747 the target_waitkind for the stopping event. This
e514a9d6
JM
4748 routine calls the generic print routine for printing a message
4749 about reasons for stopping. This will print (for example) the
4750 "Breakpoint n," part of the output. The return value of this
4751 routine is one of:
c906108c 4752
4a64f543 4753 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4754 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4755 code to print the location. An example is
c5aa993b
JM
4756 "Breakpoint 1, " which should be followed by
4757 the location.
917317f4 4758 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4759 to also print the location part of the message.
4760 An example is the catch/throw messages, which
4a64f543 4761 don't require a location appended to the end.
917317f4 4762 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4763 further info to be printed. */
c906108c 4764
917317f4 4765enum print_stop_action
36dfb11c 4766bpstat_print (bpstat bs, int kind)
c906108c
SS
4767{
4768 int val;
c5aa993b 4769
c906108c 4770 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4771 (Currently all watchpoints go on the bpstat whether hit or not.
4772 That probably could (should) be changed, provided care is taken
c906108c 4773 with respect to bpstat_explains_signal). */
e514a9d6
JM
4774 for (; bs; bs = bs->next)
4775 {
4776 val = print_bp_stop_message (bs);
4777 if (val == PRINT_SRC_ONLY
4778 || val == PRINT_SRC_AND_LOC
4779 || val == PRINT_NOTHING)
4780 return val;
4781 }
c906108c 4782
36dfb11c
TT
4783 /* If we had hit a shared library event breakpoint,
4784 print_bp_stop_message would print out this message. If we hit an
4785 OS-level shared library event, do the same thing. */
4786 if (kind == TARGET_WAITKIND_LOADED)
4787 {
edcc5120 4788 print_solib_event (0);
36dfb11c
TT
4789 return PRINT_NOTHING;
4790 }
4791
e514a9d6 4792 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4793 with and nothing was printed. */
917317f4 4794 return PRINT_UNKNOWN;
c906108c
SS
4795}
4796
c42bd95a
DE
4797/* Evaluate the expression EXP and return 1 if value is zero.
4798 This returns the inverse of the condition because it is called
4799 from catch_errors which returns 0 if an exception happened, and if an
4800 exception happens we want execution to stop.
4a64f543 4801 The argument is a "struct expression *" that has been cast to a
c42bd95a 4802 "void *" to make it pass through catch_errors. */
c906108c
SS
4803
4804static int
4efb68b1 4805breakpoint_cond_eval (void *exp)
c906108c 4806{
278cd55f 4807 struct value *mark = value_mark ();
c5aa993b 4808 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4809
c906108c
SS
4810 value_free_to_mark (mark);
4811 return i;
4812}
4813
5760d0ab 4814/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4815
4816static bpstat
5760d0ab 4817bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4818{
4819 bpstat bs;
4820
4821 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4822 bs->next = NULL;
4823 **bs_link_pointer = bs;
4824 *bs_link_pointer = &bs->next;
f431efe5
PA
4825 bs->breakpoint_at = bl->owner;
4826 bs->bp_location_at = bl;
4827 incref_bp_location (bl);
c906108c
SS
4828 /* If the condition is false, etc., don't do the commands. */
4829 bs->commands = NULL;
4830 bs->old_val = NULL;
4831 bs->print_it = print_it_normal;
4832 return bs;
4833}
4834\f
d983da9c
DJ
4835/* The target has stopped with waitstatus WS. Check if any hardware
4836 watchpoints have triggered, according to the target. */
4837
4838int
4839watchpoints_triggered (struct target_waitstatus *ws)
4840{
d92524f1 4841 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4842 CORE_ADDR addr;
4843 struct breakpoint *b;
4844
4845 if (!stopped_by_watchpoint)
4846 {
4847 /* We were not stopped by a watchpoint. Mark all watchpoints
4848 as not triggered. */
4849 ALL_BREAKPOINTS (b)
cc60f2e3 4850 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4851 {
4852 struct watchpoint *w = (struct watchpoint *) b;
4853
4854 w->watchpoint_triggered = watch_triggered_no;
4855 }
d983da9c
DJ
4856
4857 return 0;
4858 }
4859
4860 if (!target_stopped_data_address (&current_target, &addr))
4861 {
4862 /* We were stopped by a watchpoint, but we don't know where.
4863 Mark all watchpoints as unknown. */
4864 ALL_BREAKPOINTS (b)
cc60f2e3 4865 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4866 {
4867 struct watchpoint *w = (struct watchpoint *) b;
4868
4869 w->watchpoint_triggered = watch_triggered_unknown;
4870 }
d983da9c 4871
3c4797ba 4872 return 1;
d983da9c
DJ
4873 }
4874
4875 /* The target could report the data address. Mark watchpoints
4876 affected by this data address as triggered, and all others as not
4877 triggered. */
4878
4879 ALL_BREAKPOINTS (b)
cc60f2e3 4880 if (is_hardware_watchpoint (b))
d983da9c 4881 {
3a5c3e22 4882 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4883 struct bp_location *loc;
d983da9c 4884
3a5c3e22 4885 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4886 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4887 {
3a5c3e22 4888 if (is_masked_watchpoint (b))
9c06b0b4 4889 {
3a5c3e22
PA
4890 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4891 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4892
4893 if (newaddr == start)
4894 {
3a5c3e22 4895 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4896 break;
4897 }
4898 }
4899 /* Exact match not required. Within range is sufficient. */
4900 else if (target_watchpoint_addr_within_range (&current_target,
4901 addr, loc->address,
4902 loc->length))
4903 {
3a5c3e22 4904 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4905 break;
4906 }
4907 }
d983da9c
DJ
4908 }
4909
4910 return 1;
4911}
4912
c906108c
SS
4913/* Possible return values for watchpoint_check (this can't be an enum
4914 because of check_errors). */
4915/* The watchpoint has been deleted. */
4916#define WP_DELETED 1
4917/* The value has changed. */
4918#define WP_VALUE_CHANGED 2
4919/* The value has not changed. */
4920#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4921/* Ignore this watchpoint, no matter if the value changed or not. */
4922#define WP_IGNORE 4
c906108c
SS
4923
4924#define BP_TEMPFLAG 1
4925#define BP_HARDWAREFLAG 2
4926
4a64f543
MS
4927/* Evaluate watchpoint condition expression and check if its value
4928 changed.
553e4c11
JB
4929
4930 P should be a pointer to struct bpstat, but is defined as a void *
4931 in order for this function to be usable with catch_errors. */
c906108c
SS
4932
4933static int
4efb68b1 4934watchpoint_check (void *p)
c906108c
SS
4935{
4936 bpstat bs = (bpstat) p;
3a5c3e22 4937 struct watchpoint *b;
c906108c
SS
4938 struct frame_info *fr;
4939 int within_current_scope;
4940
f431efe5 4941 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4942 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4943 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4944
f6bc2008
PA
4945 /* If this is a local watchpoint, we only want to check if the
4946 watchpoint frame is in scope if the current thread is the thread
4947 that was used to create the watchpoint. */
4948 if (!watchpoint_in_thread_scope (b))
60e1c644 4949 return WP_IGNORE;
f6bc2008 4950
c906108c
SS
4951 if (b->exp_valid_block == NULL)
4952 within_current_scope = 1;
4953 else
4954 {
edb3359d
DJ
4955 struct frame_info *frame = get_current_frame ();
4956 struct gdbarch *frame_arch = get_frame_arch (frame);
4957 CORE_ADDR frame_pc = get_frame_pc (frame);
4958
4a64f543
MS
4959 /* in_function_epilogue_p() returns a non-zero value if we're
4960 still in the function but the stack frame has already been
4961 invalidated. Since we can't rely on the values of local
4962 variables after the stack has been destroyed, we are treating
4963 the watchpoint in that state as `not changed' without further
4964 checking. Don't mark watchpoints as changed if the current
4965 frame is in an epilogue - even if they are in some other
4966 frame, our view of the stack is likely to be wrong and
4967 frame_find_by_id could error out. */
a0f49112 4968 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 4969 return WP_IGNORE;
a0f49112 4970
101dcfbe 4971 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4972 within_current_scope = (fr != NULL);
69fbadd5
DJ
4973
4974 /* If we've gotten confused in the unwinder, we might have
4975 returned a frame that can't describe this variable. */
edb3359d
DJ
4976 if (within_current_scope)
4977 {
4978 struct symbol *function;
4979
4980 function = get_frame_function (fr);
4981 if (function == NULL
4982 || !contained_in (b->exp_valid_block,
4983 SYMBOL_BLOCK_VALUE (function)))
4984 within_current_scope = 0;
4985 }
69fbadd5 4986
edb3359d 4987 if (within_current_scope)
c906108c
SS
4988 /* If we end up stopping, the current frame will get selected
4989 in normal_stop. So this call to select_frame won't affect
4990 the user. */
0f7d239c 4991 select_frame (fr);
c906108c 4992 }
c5aa993b 4993
c906108c
SS
4994 if (within_current_scope)
4995 {
4a64f543
MS
4996 /* We use value_{,free_to_}mark because it could be a *long*
4997 time before we return to the command level and call
4998 free_all_values. We can't call free_all_values because we
4999 might be in the middle of evaluating a function call. */
c906108c 5000
0cf6dd15 5001 int pc = 0;
9c06b0b4 5002 struct value *mark;
fa4727a6
DJ
5003 struct value *new_val;
5004
3a5c3e22 5005 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
5006 /* Since we don't know the exact trigger address (from
5007 stopped_data_address), just tell the user we've triggered
5008 a mask watchpoint. */
5009 return WP_VALUE_CHANGED;
5010
5011 mark = value_mark ();
3a1115a0 5012 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
218d2fc6 5013
4a64f543
MS
5014 /* We use value_equal_contents instead of value_equal because
5015 the latter coerces an array to a pointer, thus comparing just
5016 the address of the array instead of its contents. This is
5017 not what we want. */
fa4727a6 5018 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 5019 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 5020 {
fa4727a6
DJ
5021 if (new_val != NULL)
5022 {
5023 release_value (new_val);
5024 value_free_to_mark (mark);
5025 }
c906108c
SS
5026 bs->old_val = b->val;
5027 b->val = new_val;
fa4727a6 5028 b->val_valid = 1;
c906108c
SS
5029 return WP_VALUE_CHANGED;
5030 }
5031 else
5032 {
60e1c644 5033 /* Nothing changed. */
c906108c 5034 value_free_to_mark (mark);
c906108c
SS
5035 return WP_VALUE_NOT_CHANGED;
5036 }
5037 }
5038 else
5039 {
79a45e25
PA
5040 struct ui_out *uiout = current_uiout;
5041
c906108c 5042 /* This seems like the only logical thing to do because
c5aa993b
JM
5043 if we temporarily ignored the watchpoint, then when
5044 we reenter the block in which it is valid it contains
5045 garbage (in the case of a function, it may have two
5046 garbage values, one before and one after the prologue).
5047 So we can't even detect the first assignment to it and
5048 watch after that (since the garbage may or may not equal
5049 the first value assigned). */
348d480f
PA
5050 /* We print all the stop information in
5051 breakpoint_ops->print_it, but in this case, by the time we
5052 call breakpoint_ops->print_it this bp will be deleted
5053 already. So we have no choice but print the information
5054 here. */
9dc5e2a9 5055 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
5056 ui_out_field_string
5057 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 5058 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 5059 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
5060 ui_out_text (uiout,
5061 " deleted because the program has left the block in\n\
8b93c638 5062which its expression is valid.\n");
4ce44c66 5063
cdac0397 5064 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 5065 decref_counted_command_line (&b->base.commands);
d0fb5eae 5066 watchpoint_del_at_next_stop (b);
c906108c
SS
5067
5068 return WP_DELETED;
5069 }
5070}
5071
18a18393 5072/* Return true if it looks like target has stopped due to hitting
348d480f
PA
5073 breakpoint location BL. This function does not check if we should
5074 stop, only if BL explains the stop. */
5075
18a18393 5076static int
6c95b8df 5077bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
5078 struct address_space *aspace, CORE_ADDR bp_addr,
5079 const struct target_waitstatus *ws)
18a18393
VP
5080{
5081 struct breakpoint *b = bl->owner;
5082
348d480f 5083 /* BL is from an existing breakpoint. */
2bdf28a0
JK
5084 gdb_assert (b != NULL);
5085
09ac7c10 5086 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
5087}
5088
3a5c3e22
PA
5089/* Determine if the watched values have actually changed, and we
5090 should stop. If not, set BS->stop to 0. */
5091
18a18393
VP
5092static void
5093bpstat_check_watchpoint (bpstat bs)
5094{
2bdf28a0 5095 const struct bp_location *bl;
3a5c3e22 5096 struct watchpoint *b;
2bdf28a0
JK
5097
5098 /* BS is built for existing struct breakpoint. */
f431efe5 5099 bl = bs->bp_location_at;
2bdf28a0 5100 gdb_assert (bl != NULL);
3a5c3e22 5101 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 5102 gdb_assert (b != NULL);
18a18393 5103
18a18393 5104 {
18a18393
VP
5105 int must_check_value = 0;
5106
3a5c3e22 5107 if (b->base.type == bp_watchpoint)
18a18393
VP
5108 /* For a software watchpoint, we must always check the
5109 watched value. */
5110 must_check_value = 1;
5111 else if (b->watchpoint_triggered == watch_triggered_yes)
5112 /* We have a hardware watchpoint (read, write, or access)
5113 and the target earlier reported an address watched by
5114 this watchpoint. */
5115 must_check_value = 1;
5116 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 5117 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
5118 /* We were stopped by a hardware watchpoint, but the target could
5119 not report the data address. We must check the watchpoint's
5120 value. Access and read watchpoints are out of luck; without
5121 a data address, we can't figure it out. */
5122 must_check_value = 1;
3a5c3e22 5123
18a18393
VP
5124 if (must_check_value)
5125 {
3e43a32a
MS
5126 char *message
5127 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 5128 b->base.number);
18a18393
VP
5129 struct cleanup *cleanups = make_cleanup (xfree, message);
5130 int e = catch_errors (watchpoint_check, bs, message,
5131 RETURN_MASK_ALL);
5132 do_cleanups (cleanups);
5133 switch (e)
5134 {
5135 case WP_DELETED:
5136 /* We've already printed what needs to be printed. */
5137 bs->print_it = print_it_done;
5138 /* Stop. */
5139 break;
60e1c644
PA
5140 case WP_IGNORE:
5141 bs->print_it = print_it_noop;
5142 bs->stop = 0;
5143 break;
18a18393 5144 case WP_VALUE_CHANGED:
3a5c3e22 5145 if (b->base.type == bp_read_watchpoint)
18a18393 5146 {
85d721b8
PA
5147 /* There are two cases to consider here:
5148
4a64f543 5149 1. We're watching the triggered memory for reads.
85d721b8
PA
5150 In that case, trust the target, and always report
5151 the watchpoint hit to the user. Even though
5152 reads don't cause value changes, the value may
5153 have changed since the last time it was read, and
5154 since we're not trapping writes, we will not see
5155 those, and as such we should ignore our notion of
5156 old value.
5157
4a64f543 5158 2. We're watching the triggered memory for both
85d721b8
PA
5159 reads and writes. There are two ways this may
5160 happen:
5161
4a64f543 5162 2.1. This is a target that can't break on data
85d721b8
PA
5163 reads only, but can break on accesses (reads or
5164 writes), such as e.g., x86. We detect this case
5165 at the time we try to insert read watchpoints.
5166
4a64f543 5167 2.2. Otherwise, the target supports read
85d721b8
PA
5168 watchpoints, but, the user set an access or write
5169 watchpoint watching the same memory as this read
5170 watchpoint.
5171
5172 If we're watching memory writes as well as reads,
5173 ignore watchpoint hits when we find that the
5174 value hasn't changed, as reads don't cause
5175 changes. This still gives false positives when
5176 the program writes the same value to memory as
5177 what there was already in memory (we will confuse
5178 it for a read), but it's much better than
5179 nothing. */
5180
5181 int other_write_watchpoint = 0;
5182
5183 if (bl->watchpoint_type == hw_read)
5184 {
5185 struct breakpoint *other_b;
5186
5187 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
5188 if (other_b->type == bp_hardware_watchpoint
5189 || other_b->type == bp_access_watchpoint)
85d721b8 5190 {
3a5c3e22
PA
5191 struct watchpoint *other_w =
5192 (struct watchpoint *) other_b;
5193
5194 if (other_w->watchpoint_triggered
5195 == watch_triggered_yes)
5196 {
5197 other_write_watchpoint = 1;
5198 break;
5199 }
85d721b8
PA
5200 }
5201 }
5202
5203 if (other_write_watchpoint
5204 || bl->watchpoint_type == hw_access)
5205 {
5206 /* We're watching the same memory for writes,
5207 and the value changed since the last time we
5208 updated it, so this trap must be for a write.
5209 Ignore it. */
5210 bs->print_it = print_it_noop;
5211 bs->stop = 0;
5212 }
18a18393
VP
5213 }
5214 break;
5215 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
5216 if (b->base.type == bp_hardware_watchpoint
5217 || b->base.type == bp_watchpoint)
18a18393
VP
5218 {
5219 /* Don't stop: write watchpoints shouldn't fire if
5220 the value hasn't changed. */
5221 bs->print_it = print_it_noop;
5222 bs->stop = 0;
5223 }
5224 /* Stop. */
5225 break;
5226 default:
5227 /* Can't happen. */
5228 case 0:
5229 /* Error from catch_errors. */
3a5c3e22 5230 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 5231 watchpoint_del_at_next_stop (b);
18a18393
VP
5232 /* We've already printed what needs to be printed. */
5233 bs->print_it = print_it_done;
5234 break;
5235 }
5236 }
5237 else /* must_check_value == 0 */
5238 {
5239 /* This is a case where some watchpoint(s) triggered, but
5240 not at the address of this watchpoint, or else no
5241 watchpoint triggered after all. So don't print
5242 anything for this watchpoint. */
5243 bs->print_it = print_it_noop;
5244 bs->stop = 0;
5245 }
5246 }
5247}
5248
7d4df6a4
DE
5249/* For breakpoints that are currently marked as telling gdb to stop,
5250 check conditions (condition proper, frame, thread and ignore count)
18a18393
VP
5251 of breakpoint referred to by BS. If we should not stop for this
5252 breakpoint, set BS->stop to 0. */
f431efe5 5253
18a18393
VP
5254static void
5255bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5256{
2bdf28a0
JK
5257 const struct bp_location *bl;
5258 struct breakpoint *b;
7d4df6a4
DE
5259 int value_is_zero = 0;
5260 struct expression *cond;
5261
5262 gdb_assert (bs->stop);
2bdf28a0
JK
5263
5264 /* BS is built for existing struct breakpoint. */
f431efe5 5265 bl = bs->bp_location_at;
2bdf28a0 5266 gdb_assert (bl != NULL);
f431efe5 5267 b = bs->breakpoint_at;
2bdf28a0 5268 gdb_assert (b != NULL);
18a18393 5269
b775012e
LM
5270 /* Even if the target evaluated the condition on its end and notified GDB, we
5271 need to do so again since GDB does not know if we stopped due to a
5272 breakpoint or a single step breakpoint. */
5273
18a18393 5274 if (frame_id_p (b->frame_id)
edb3359d 5275 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393 5276 {
7d4df6a4
DE
5277 bs->stop = 0;
5278 return;
5279 }
60e1c644 5280
12ab52e9
PA
5281 /* If this is a thread/task-specific breakpoint, don't waste cpu
5282 evaluating the condition if this isn't the specified
5283 thread/task. */
5284 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5285 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5286
6c1b0f7b
DE
5287 {
5288 bs->stop = 0;
5289 return;
5290 }
5291
6dddc817
DE
5292 /* Evaluate extension language breakpoints that have a "stop" method
5293 implemented. */
5294 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
7371cf6d 5295
7d4df6a4
DE
5296 if (is_watchpoint (b))
5297 {
5298 struct watchpoint *w = (struct watchpoint *) b;
3a5c3e22 5299
7d4df6a4
DE
5300 cond = w->cond_exp;
5301 }
5302 else
5303 cond = bl->cond;
60e1c644 5304
7d4df6a4
DE
5305 if (cond && b->disposition != disp_del_at_next_stop)
5306 {
5307 int within_current_scope = 1;
5308 struct watchpoint * w;
60e1c644 5309
7d4df6a4
DE
5310 /* We use value_mark and value_free_to_mark because it could
5311 be a long time before we return to the command level and
5312 call free_all_values. We can't call free_all_values
5313 because we might be in the middle of evaluating a
5314 function call. */
5315 struct value *mark = value_mark ();
5316
5317 if (is_watchpoint (b))
5318 w = (struct watchpoint *) b;
5319 else
5320 w = NULL;
5321
5322 /* Need to select the frame, with all that implies so that
5323 the conditions will have the right context. Because we
5324 use the frame, we will not see an inlined function's
5325 variables when we arrive at a breakpoint at the start
5326 of the inlined function; the current frame will be the
5327 call site. */
5328 if (w == NULL || w->cond_exp_valid_block == NULL)
5329 select_frame (get_current_frame ());
5330 else
18a18393 5331 {
7d4df6a4
DE
5332 struct frame_info *frame;
5333
5334 /* For local watchpoint expressions, which particular
5335 instance of a local is being watched matters, so we
5336 keep track of the frame to evaluate the expression
5337 in. To evaluate the condition however, it doesn't
5338 really matter which instantiation of the function
5339 where the condition makes sense triggers the
5340 watchpoint. This allows an expression like "watch
5341 global if q > 10" set in `func', catch writes to
5342 global on all threads that call `func', or catch
5343 writes on all recursive calls of `func' by a single
5344 thread. We simply always evaluate the condition in
5345 the innermost frame that's executing where it makes
5346 sense to evaluate the condition. It seems
5347 intuitive. */
5348 frame = block_innermost_frame (w->cond_exp_valid_block);
5349 if (frame != NULL)
5350 select_frame (frame);
5351 else
5352 within_current_scope = 0;
18a18393 5353 }
7d4df6a4
DE
5354 if (within_current_scope)
5355 value_is_zero
5356 = catch_errors (breakpoint_cond_eval, cond,
5357 "Error in testing breakpoint condition:\n",
5358 RETURN_MASK_ALL);
5359 else
18a18393 5360 {
7d4df6a4
DE
5361 warning (_("Watchpoint condition cannot be tested "
5362 "in the current scope"));
5363 /* If we failed to set the right context for this
5364 watchpoint, unconditionally report it. */
5365 value_is_zero = 0;
18a18393 5366 }
7d4df6a4
DE
5367 /* FIXME-someday, should give breakpoint #. */
5368 value_free_to_mark (mark);
18a18393 5369 }
7d4df6a4
DE
5370
5371 if (cond && value_is_zero)
5372 {
5373 bs->stop = 0;
5374 }
7d4df6a4
DE
5375 else if (b->ignore_count > 0)
5376 {
5377 b->ignore_count--;
5378 bs->stop = 0;
5379 /* Increase the hit count even though we don't stop. */
5380 ++(b->hit_count);
5381 observer_notify_breakpoint_modified (b);
5382 }
18a18393
VP
5383}
5384
5385
9709f61c 5386/* Get a bpstat associated with having just stopped at address
d983da9c 5387 BP_ADDR in thread PTID.
c906108c 5388
d983da9c 5389 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
5390 don't understand this stop. Result is a chain of bpstat's such
5391 that:
c906108c 5392
c5aa993b 5393 if we don't understand the stop, the result is a null pointer.
c906108c 5394
c5aa993b 5395 if we understand why we stopped, the result is not null.
c906108c 5396
c5aa993b
JM
5397 Each element of the chain refers to a particular breakpoint or
5398 watchpoint at which we have stopped. (We may have stopped for
5399 several reasons concurrently.)
c906108c 5400
c5aa993b
JM
5401 Each element of the chain has valid next, breakpoint_at,
5402 commands, FIXME??? fields. */
c906108c
SS
5403
5404bpstat
6c95b8df 5405bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
5406 CORE_ADDR bp_addr, ptid_t ptid,
5407 const struct target_waitstatus *ws)
c906108c 5408{
0d381245 5409 struct breakpoint *b = NULL;
afe38095 5410 struct bp_location *bl;
20874c92 5411 struct bp_location *loc;
5760d0ab
JK
5412 /* First item of allocated bpstat's. */
5413 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 5414 /* Pointer to the last thing in the chain currently. */
5760d0ab 5415 bpstat bs;
20874c92 5416 int ix;
429374b8 5417 int need_remove_insert;
f431efe5 5418 int removed_any;
c906108c 5419
f431efe5
PA
5420 /* First, build the bpstat chain with locations that explain a
5421 target stop, while being careful to not set the target running,
5422 as that may invalidate locations (in particular watchpoint
5423 locations are recreated). Resuming will happen here with
5424 breakpoint conditions or watchpoint expressions that include
5425 inferior function calls. */
c5aa993b 5426
429374b8
JK
5427 ALL_BREAKPOINTS (b)
5428 {
5429 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5430 continue;
a5606eee 5431
429374b8
JK
5432 for (bl = b->loc; bl != NULL; bl = bl->next)
5433 {
4a64f543
MS
5434 /* For hardware watchpoints, we look only at the first
5435 location. The watchpoint_check function will work on the
5436 entire expression, not the individual locations. For
5437 read watchpoints, the watchpoints_triggered function has
5438 checked all locations already. */
429374b8
JK
5439 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5440 break;
18a18393 5441
f6592439 5442 if (!bl->enabled || bl->shlib_disabled)
429374b8 5443 continue;
c5aa993b 5444
09ac7c10 5445 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 5446 continue;
c5aa993b 5447
4a64f543
MS
5448 /* Come here if it's a watchpoint, or if the break address
5449 matches. */
c5aa993b 5450
4a64f543
MS
5451 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5452 explain stop. */
c5aa993b 5453
f431efe5
PA
5454 /* Assume we stop. Should we find a watchpoint that is not
5455 actually triggered, or if the condition of the breakpoint
5456 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
5457 bs->stop = 1;
5458 bs->print = 1;
d983da9c 5459
f431efe5
PA
5460 /* If this is a scope breakpoint, mark the associated
5461 watchpoint as triggered so that we will handle the
5462 out-of-scope event. We'll get to the watchpoint next
5463 iteration. */
d0fb5eae 5464 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
5465 {
5466 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5467
5468 w->watchpoint_triggered = watch_triggered_yes;
5469 }
f431efe5
PA
5470 }
5471 }
5472
5473 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5474 {
f1310107 5475 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 5476 {
5760d0ab 5477 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
5478 /* For hits of moribund locations, we should just proceed. */
5479 bs->stop = 0;
5480 bs->print = 0;
5481 bs->print_it = print_it_noop;
5482 }
5483 }
5484
edcc5120
TT
5485 /* A bit of special processing for shlib breakpoints. We need to
5486 process solib loading here, so that the lists of loaded and
5487 unloaded libraries are correct before we handle "catch load" and
5488 "catch unload". */
5489 for (bs = bs_head; bs != NULL; bs = bs->next)
5490 {
5d268276 5491 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
5492 {
5493 handle_solib_event ();
5494 break;
5495 }
5496 }
5497
f431efe5
PA
5498 /* Now go through the locations that caused the target to stop, and
5499 check whether we're interested in reporting this stop to higher
5500 layers, or whether we should resume the target transparently. */
5501
5502 removed_any = 0;
5503
5760d0ab 5504 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
5505 {
5506 if (!bs->stop)
5507 continue;
5508
f431efe5 5509 b = bs->breakpoint_at;
348d480f
PA
5510 b->ops->check_status (bs);
5511 if (bs->stop)
28010a5d 5512 {
348d480f 5513 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5514
429374b8
JK
5515 if (bs->stop)
5516 {
5517 ++(b->hit_count);
8d3788bd 5518 observer_notify_breakpoint_modified (b);
c906108c 5519
4a64f543 5520 /* We will stop here. */
429374b8
JK
5521 if (b->disposition == disp_disable)
5522 {
816338b5
SS
5523 --(b->enable_count);
5524 if (b->enable_count <= 0
5525 && b->enable_state != bp_permanent)
429374b8 5526 b->enable_state = bp_disabled;
f431efe5 5527 removed_any = 1;
429374b8
JK
5528 }
5529 if (b->silent)
5530 bs->print = 0;
5531 bs->commands = b->commands;
9add0f1b 5532 incref_counted_command_line (bs->commands);
abf85f46
JK
5533 if (command_line_is_silent (bs->commands
5534 ? bs->commands->commands : NULL))
5535 bs->print = 0;
9d6e6e84
HZ
5536
5537 b->ops->after_condition_true (bs);
429374b8
JK
5538 }
5539
348d480f 5540 }
a9b3a50f
PA
5541
5542 /* Print nothing for this entry if we don't stop or don't
5543 print. */
5544 if (!bs->stop || !bs->print)
5545 bs->print_it = print_it_noop;
429374b8 5546 }
876fa593 5547
d983da9c
DJ
5548 /* If we aren't stopping, the value of some hardware watchpoint may
5549 not have changed, but the intermediate memory locations we are
5550 watching may have. Don't bother if we're stopping; this will get
5551 done later. */
d832cb68 5552 need_remove_insert = 0;
5760d0ab
JK
5553 if (! bpstat_causes_stop (bs_head))
5554 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5555 if (!bs->stop
f431efe5
PA
5556 && bs->breakpoint_at
5557 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5558 {
3a5c3e22
PA
5559 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5560
5561 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5562 need_remove_insert = 1;
d983da9c
DJ
5563 }
5564
d832cb68 5565 if (need_remove_insert)
2d134ed3 5566 update_global_location_list (1);
f431efe5
PA
5567 else if (removed_any)
5568 update_global_location_list (0);
d832cb68 5569
5760d0ab 5570 return bs_head;
c906108c 5571}
628fe4e4
JK
5572
5573static void
5574handle_jit_event (void)
5575{
5576 struct frame_info *frame;
5577 struct gdbarch *gdbarch;
5578
5579 /* Switch terminal for any messages produced by
5580 breakpoint_re_set. */
5581 target_terminal_ours_for_output ();
5582
5583 frame = get_current_frame ();
5584 gdbarch = get_frame_arch (frame);
5585
5586 jit_event_handler (gdbarch);
5587
5588 target_terminal_inferior ();
5589}
5590
5591/* Prepare WHAT final decision for infrun. */
5592
5593/* Decide what infrun needs to do with this bpstat. */
5594
c906108c 5595struct bpstat_what
0e30163f 5596bpstat_what (bpstat bs_head)
c906108c 5597{
c906108c 5598 struct bpstat_what retval;
628fe4e4 5599 int jit_event = 0;
0e30163f 5600 bpstat bs;
c906108c 5601
628fe4e4 5602 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5603 retval.call_dummy = STOP_NONE;
186c406b 5604 retval.is_longjmp = 0;
628fe4e4 5605
0e30163f 5606 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5607 {
628fe4e4
JK
5608 /* Extract this BS's action. After processing each BS, we check
5609 if its action overrides all we've seem so far. */
5610 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5611 enum bptype bptype;
5612
c906108c 5613 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5614 {
5615 /* I suspect this can happen if it was a momentary
5616 breakpoint which has since been deleted. */
5617 bptype = bp_none;
5618 }
20874c92 5619 else
f431efe5 5620 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5621
5622 switch (bptype)
c906108c
SS
5623 {
5624 case bp_none:
628fe4e4 5625 break;
c906108c
SS
5626 case bp_breakpoint:
5627 case bp_hardware_breakpoint:
5628 case bp_until:
5629 case bp_finish:
a9b3a50f 5630 case bp_shlib_event:
c906108c
SS
5631 if (bs->stop)
5632 {
5633 if (bs->print)
628fe4e4 5634 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5635 else
628fe4e4 5636 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5637 }
5638 else
628fe4e4 5639 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5640 break;
5641 case bp_watchpoint:
5642 case bp_hardware_watchpoint:
5643 case bp_read_watchpoint:
5644 case bp_access_watchpoint:
5645 if (bs->stop)
5646 {
5647 if (bs->print)
628fe4e4 5648 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5649 else
628fe4e4 5650 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5651 }
5652 else
628fe4e4
JK
5653 {
5654 /* There was a watchpoint, but we're not stopping.
5655 This requires no further action. */
5656 }
c906108c
SS
5657 break;
5658 case bp_longjmp:
e2e4d78b 5659 case bp_longjmp_call_dummy:
186c406b 5660 case bp_exception:
628fe4e4 5661 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5662 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5663 break;
5664 case bp_longjmp_resume:
186c406b 5665 case bp_exception_resume:
628fe4e4 5666 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5667 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5668 break;
5669 case bp_step_resume:
5670 if (bs->stop)
628fe4e4
JK
5671 this_action = BPSTAT_WHAT_STEP_RESUME;
5672 else
c906108c 5673 {
628fe4e4
JK
5674 /* It is for the wrong frame. */
5675 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5676 }
c906108c 5677 break;
2c03e5be
PA
5678 case bp_hp_step_resume:
5679 if (bs->stop)
5680 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5681 else
5682 {
5683 /* It is for the wrong frame. */
5684 this_action = BPSTAT_WHAT_SINGLE;
5685 }
5686 break;
c906108c 5687 case bp_watchpoint_scope:
c4093a6a 5688 case bp_thread_event:
1900040c 5689 case bp_overlay_event:
0fd8e87f 5690 case bp_longjmp_master:
aa7d318d 5691 case bp_std_terminate_master:
186c406b 5692 case bp_exception_master:
628fe4e4 5693 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5694 break;
ce78b96d 5695 case bp_catchpoint:
c5aa993b
JM
5696 if (bs->stop)
5697 {
5698 if (bs->print)
628fe4e4 5699 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5700 else
628fe4e4 5701 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5702 }
5703 else
628fe4e4
JK
5704 {
5705 /* There was a catchpoint, but we're not stopping.
5706 This requires no further action. */
5707 }
5708 break;
628fe4e4
JK
5709 case bp_jit_event:
5710 jit_event = 1;
5711 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5712 break;
c906108c 5713 case bp_call_dummy:
53a5351d
JM
5714 /* Make sure the action is stop (silent or noisy),
5715 so infrun.c pops the dummy frame. */
aa7d318d 5716 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5717 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5718 break;
5719 case bp_std_terminate:
5720 /* Make sure the action is stop (silent or noisy),
5721 so infrun.c pops the dummy frame. */
aa7d318d 5722 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5723 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5724 break;
1042e4c0 5725 case bp_tracepoint:
7a697b8d 5726 case bp_fast_tracepoint:
0fb4aa4b 5727 case bp_static_tracepoint:
1042e4c0
SS
5728 /* Tracepoint hits should not be reported back to GDB, and
5729 if one got through somehow, it should have been filtered
5730 out already. */
5731 internal_error (__FILE__, __LINE__,
7a697b8d 5732 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5733 break;
5734 case bp_gnu_ifunc_resolver:
5735 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5736 this_action = BPSTAT_WHAT_SINGLE;
5737 break;
5738 case bp_gnu_ifunc_resolver_return:
5739 /* The breakpoint will be removed, execution will restart from the
5740 PC of the former breakpoint. */
5741 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5742 break;
e7e0cddf
SS
5743
5744 case bp_dprintf:
a11cfd87
HZ
5745 if (bs->stop)
5746 this_action = BPSTAT_WHAT_STOP_SILENT;
5747 else
5748 this_action = BPSTAT_WHAT_SINGLE;
e7e0cddf
SS
5749 break;
5750
628fe4e4
JK
5751 default:
5752 internal_error (__FILE__, __LINE__,
5753 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5754 }
628fe4e4
JK
5755
5756 retval.main_action = max (retval.main_action, this_action);
c906108c 5757 }
628fe4e4 5758
0e30163f
JK
5759 /* These operations may affect the bs->breakpoint_at state so they are
5760 delayed after MAIN_ACTION is decided above. */
5761
628fe4e4
JK
5762 if (jit_event)
5763 {
5764 if (debug_infrun)
5765 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5766
5767 handle_jit_event ();
5768 }
5769
0e30163f
JK
5770 for (bs = bs_head; bs != NULL; bs = bs->next)
5771 {
5772 struct breakpoint *b = bs->breakpoint_at;
5773
5774 if (b == NULL)
5775 continue;
5776 switch (b->type)
5777 {
5778 case bp_gnu_ifunc_resolver:
5779 gnu_ifunc_resolver_stop (b);
5780 break;
5781 case bp_gnu_ifunc_resolver_return:
5782 gnu_ifunc_resolver_return_stop (b);
5783 break;
5784 }
5785 }
5786
c906108c
SS
5787 return retval;
5788}
5789
5790/* Nonzero if we should step constantly (e.g. watchpoints on machines
5791 without hardware support). This isn't related to a specific bpstat,
5792 just to things like whether watchpoints are set. */
5793
c5aa993b 5794int
fba45db2 5795bpstat_should_step (void)
c906108c
SS
5796{
5797 struct breakpoint *b;
cc59ec59 5798
c906108c 5799 ALL_BREAKPOINTS (b)
717a8278 5800 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5801 return 1;
c906108c
SS
5802 return 0;
5803}
5804
67822962
PA
5805int
5806bpstat_causes_stop (bpstat bs)
5807{
5808 for (; bs != NULL; bs = bs->next)
5809 if (bs->stop)
5810 return 1;
5811
5812 return 0;
5813}
5814
c906108c 5815\f
c5aa993b 5816
170b53b2
UW
5817/* Compute a string of spaces suitable to indent the next line
5818 so it starts at the position corresponding to the table column
5819 named COL_NAME in the currently active table of UIOUT. */
5820
5821static char *
5822wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5823{
5824 static char wrap_indent[80];
5825 int i, total_width, width, align;
5826 char *text;
5827
5828 total_width = 0;
5829 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5830 {
5831 if (strcmp (text, col_name) == 0)
5832 {
5833 gdb_assert (total_width < sizeof wrap_indent);
5834 memset (wrap_indent, ' ', total_width);
5835 wrap_indent[total_width] = 0;
5836
5837 return wrap_indent;
5838 }
5839
5840 total_width += width + 1;
5841 }
5842
5843 return NULL;
5844}
5845
b775012e
LM
5846/* Determine if the locations of this breakpoint will have their conditions
5847 evaluated by the target, host or a mix of both. Returns the following:
5848
5849 "host": Host evals condition.
5850 "host or target": Host or Target evals condition.
5851 "target": Target evals condition.
5852*/
5853
5854static const char *
5855bp_condition_evaluator (struct breakpoint *b)
5856{
5857 struct bp_location *bl;
5858 char host_evals = 0;
5859 char target_evals = 0;
5860
5861 if (!b)
5862 return NULL;
5863
5864 if (!is_breakpoint (b))
5865 return NULL;
5866
5867 if (gdb_evaluates_breakpoint_condition_p ()
5868 || !target_supports_evaluation_of_breakpoint_conditions ())
5869 return condition_evaluation_host;
5870
5871 for (bl = b->loc; bl; bl = bl->next)
5872 {
5873 if (bl->cond_bytecode)
5874 target_evals++;
5875 else
5876 host_evals++;
5877 }
5878
5879 if (host_evals && target_evals)
5880 return condition_evaluation_both;
5881 else if (target_evals)
5882 return condition_evaluation_target;
5883 else
5884 return condition_evaluation_host;
5885}
5886
5887/* Determine the breakpoint location's condition evaluator. This is
5888 similar to bp_condition_evaluator, but for locations. */
5889
5890static const char *
5891bp_location_condition_evaluator (struct bp_location *bl)
5892{
5893 if (bl && !is_breakpoint (bl->owner))
5894 return NULL;
5895
5896 if (gdb_evaluates_breakpoint_condition_p ()
5897 || !target_supports_evaluation_of_breakpoint_conditions ())
5898 return condition_evaluation_host;
5899
5900 if (bl && bl->cond_bytecode)
5901 return condition_evaluation_target;
5902 else
5903 return condition_evaluation_host;
5904}
5905
859825b8
JK
5906/* Print the LOC location out of the list of B->LOC locations. */
5907
170b53b2
UW
5908static void
5909print_breakpoint_location (struct breakpoint *b,
5910 struct bp_location *loc)
0d381245 5911{
79a45e25 5912 struct ui_out *uiout = current_uiout;
6c95b8df
PA
5913 struct cleanup *old_chain = save_current_program_space ();
5914
859825b8
JK
5915 if (loc != NULL && loc->shlib_disabled)
5916 loc = NULL;
5917
6c95b8df
PA
5918 if (loc != NULL)
5919 set_current_program_space (loc->pspace);
5920
56435ebe
TT
5921 if (b->display_canonical)
5922 ui_out_field_string (uiout, "what", b->addr_string);
2f202fde 5923 else if (loc && loc->symtab)
0d381245
VP
5924 {
5925 struct symbol *sym
5926 = find_pc_sect_function (loc->address, loc->section);
5927 if (sym)
5928 {
5929 ui_out_text (uiout, "in ");
5930 ui_out_field_string (uiout, "func",
5931 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
5932 ui_out_text (uiout, " ");
5933 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5934 ui_out_text (uiout, "at ");
0d381245 5935 }
05cba821
JK
5936 ui_out_field_string (uiout, "file",
5937 symtab_to_filename_for_display (loc->symtab));
0d381245 5938 ui_out_text (uiout, ":");
05cba821 5939
0d381245 5940 if (ui_out_is_mi_like_p (uiout))
2f202fde
JK
5941 ui_out_field_string (uiout, "fullname",
5942 symtab_to_fullname (loc->symtab));
0d381245 5943
f8eba3c6 5944 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 5945 }
859825b8 5946 else if (loc)
0d381245 5947 {
f99d8bf4
PA
5948 struct ui_file *stb = mem_fileopen ();
5949 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 5950
f99d8bf4 5951 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 5952 demangle, "");
0d381245 5953 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
5954
5955 do_cleanups (stb_chain);
0d381245 5956 }
859825b8
JK
5957 else
5958 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 5959
b775012e
LM
5960 if (loc && is_breakpoint (b)
5961 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5962 && bp_condition_evaluator (b) == condition_evaluation_both)
5963 {
5964 ui_out_text (uiout, " (");
5965 ui_out_field_string (uiout, "evaluated-by",
5966 bp_location_condition_evaluator (loc));
5967 ui_out_text (uiout, ")");
5968 }
5969
6c95b8df 5970 do_cleanups (old_chain);
0d381245
VP
5971}
5972
269b11a2
PA
5973static const char *
5974bptype_string (enum bptype type)
c906108c 5975{
c4093a6a
JM
5976 struct ep_type_description
5977 {
5978 enum bptype type;
5979 char *description;
5980 };
5981 static struct ep_type_description bptypes[] =
c906108c 5982 {
c5aa993b
JM
5983 {bp_none, "?deleted?"},
5984 {bp_breakpoint, "breakpoint"},
c906108c 5985 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
5986 {bp_until, "until"},
5987 {bp_finish, "finish"},
5988 {bp_watchpoint, "watchpoint"},
c906108c 5989 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5990 {bp_read_watchpoint, "read watchpoint"},
5991 {bp_access_watchpoint, "acc watchpoint"},
5992 {bp_longjmp, "longjmp"},
5993 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5994 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5995 {bp_exception, "exception"},
5996 {bp_exception_resume, "exception resume"},
c5aa993b 5997 {bp_step_resume, "step resume"},
2c03e5be 5998 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5999 {bp_watchpoint_scope, "watchpoint scope"},
6000 {bp_call_dummy, "call dummy"},
aa7d318d 6001 {bp_std_terminate, "std::terminate"},
c5aa993b 6002 {bp_shlib_event, "shlib events"},
c4093a6a 6003 {bp_thread_event, "thread events"},
1900040c 6004 {bp_overlay_event, "overlay events"},
0fd8e87f 6005 {bp_longjmp_master, "longjmp master"},
aa7d318d 6006 {bp_std_terminate_master, "std::terminate master"},
186c406b 6007 {bp_exception_master, "exception master"},
ce78b96d 6008 {bp_catchpoint, "catchpoint"},
1042e4c0 6009 {bp_tracepoint, "tracepoint"},
7a697b8d 6010 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 6011 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 6012 {bp_dprintf, "dprintf"},
4efc6507 6013 {bp_jit_event, "jit events"},
0e30163f
JK
6014 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6015 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 6016 };
269b11a2
PA
6017
6018 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6019 || ((int) type != bptypes[(int) type].type))
6020 internal_error (__FILE__, __LINE__,
6021 _("bptypes table does not describe type #%d."),
6022 (int) type);
6023
6024 return bptypes[(int) type].description;
6025}
6026
998580f1
MK
6027/* For MI, output a field named 'thread-groups' with a list as the value.
6028 For CLI, prefix the list with the string 'inf'. */
6029
6030static void
6031output_thread_groups (struct ui_out *uiout,
6032 const char *field_name,
6033 VEC(int) *inf_num,
6034 int mi_only)
6035{
752eb8b4 6036 struct cleanup *back_to;
998580f1
MK
6037 int is_mi = ui_out_is_mi_like_p (uiout);
6038 int inf;
6039 int i;
6040
6041 /* For backward compatibility, don't display inferiors in CLI unless
6042 there are several. Always display them for MI. */
6043 if (!is_mi && mi_only)
6044 return;
6045
752eb8b4
TT
6046 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6047
998580f1
MK
6048 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6049 {
6050 if (is_mi)
6051 {
6052 char mi_group[10];
6053
6054 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6055 ui_out_field_string (uiout, NULL, mi_group);
6056 }
6057 else
6058 {
6059 if (i == 0)
6060 ui_out_text (uiout, " inf ");
6061 else
6062 ui_out_text (uiout, ", ");
6063
6064 ui_out_text (uiout, plongest (inf));
6065 }
6066 }
6067
6068 do_cleanups (back_to);
6069}
6070
269b11a2
PA
6071/* Print B to gdb_stdout. */
6072
6073static void
6074print_one_breakpoint_location (struct breakpoint *b,
6075 struct bp_location *loc,
6076 int loc_number,
6077 struct bp_location **last_loc,
269b11a2
PA
6078 int allflag)
6079{
6080 struct command_line *l;
c2c6d25f 6081 static char bpenables[] = "nynny";
c906108c 6082
79a45e25 6083 struct ui_out *uiout = current_uiout;
0d381245
VP
6084 int header_of_multiple = 0;
6085 int part_of_multiple = (loc != NULL);
79a45b7d
TT
6086 struct value_print_options opts;
6087
6088 get_user_print_options (&opts);
0d381245
VP
6089
6090 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
6091 /* See comment in print_one_breakpoint concerning treatment of
6092 breakpoints with single disabled location. */
0d381245
VP
6093 if (loc == NULL
6094 && (b->loc != NULL
6095 && (b->loc->next != NULL || !b->loc->enabled)))
6096 header_of_multiple = 1;
6097 if (loc == NULL)
6098 loc = b->loc;
6099
c4093a6a
JM
6100 annotate_record ();
6101
6102 /* 1 */
6103 annotate_field (0);
0d381245
VP
6104 if (part_of_multiple)
6105 {
6106 char *formatted;
0c6773c1 6107 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
6108 ui_out_field_string (uiout, "number", formatted);
6109 xfree (formatted);
6110 }
6111 else
6112 {
6113 ui_out_field_int (uiout, "number", b->number);
6114 }
c4093a6a
JM
6115
6116 /* 2 */
6117 annotate_field (1);
0d381245
VP
6118 if (part_of_multiple)
6119 ui_out_field_skip (uiout, "type");
269b11a2
PA
6120 else
6121 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
6122
6123 /* 3 */
6124 annotate_field (2);
0d381245
VP
6125 if (part_of_multiple)
6126 ui_out_field_skip (uiout, "disp");
6127 else
2cec12e5 6128 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 6129
c4093a6a
JM
6130
6131 /* 4 */
6132 annotate_field (3);
0d381245 6133 if (part_of_multiple)
54e52265 6134 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 6135 else
4a64f543
MS
6136 ui_out_field_fmt (uiout, "enabled", "%c",
6137 bpenables[(int) b->enable_state]);
54e52265 6138 ui_out_spaces (uiout, 2);
0d381245 6139
c4093a6a
JM
6140
6141 /* 5 and 6 */
3086aeae 6142 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 6143 {
4a64f543
MS
6144 /* Although the print_one can possibly print all locations,
6145 calling it here is not likely to get any nice result. So,
6146 make sure there's just one location. */
0d381245 6147 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 6148 b->ops->print_one (b, last_loc);
0d381245 6149 }
3086aeae
DJ
6150 else
6151 switch (b->type)
6152 {
6153 case bp_none:
6154 internal_error (__FILE__, __LINE__,
e2e0b3e5 6155 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 6156 break;
c906108c 6157
3086aeae
DJ
6158 case bp_watchpoint:
6159 case bp_hardware_watchpoint:
6160 case bp_read_watchpoint:
6161 case bp_access_watchpoint:
3a5c3e22
PA
6162 {
6163 struct watchpoint *w = (struct watchpoint *) b;
6164
6165 /* Field 4, the address, is omitted (which makes the columns
6166 not line up too nicely with the headers, but the effect
6167 is relatively readable). */
6168 if (opts.addressprint)
6169 ui_out_field_skip (uiout, "addr");
6170 annotate_field (5);
6171 ui_out_field_string (uiout, "what", w->exp_string);
6172 }
3086aeae
DJ
6173 break;
6174
3086aeae
DJ
6175 case bp_breakpoint:
6176 case bp_hardware_breakpoint:
6177 case bp_until:
6178 case bp_finish:
6179 case bp_longjmp:
6180 case bp_longjmp_resume:
e2e4d78b 6181 case bp_longjmp_call_dummy:
186c406b
TT
6182 case bp_exception:
6183 case bp_exception_resume:
3086aeae 6184 case bp_step_resume:
2c03e5be 6185 case bp_hp_step_resume:
3086aeae
DJ
6186 case bp_watchpoint_scope:
6187 case bp_call_dummy:
aa7d318d 6188 case bp_std_terminate:
3086aeae
DJ
6189 case bp_shlib_event:
6190 case bp_thread_event:
6191 case bp_overlay_event:
0fd8e87f 6192 case bp_longjmp_master:
aa7d318d 6193 case bp_std_terminate_master:
186c406b 6194 case bp_exception_master:
1042e4c0 6195 case bp_tracepoint:
7a697b8d 6196 case bp_fast_tracepoint:
0fb4aa4b 6197 case bp_static_tracepoint:
e7e0cddf 6198 case bp_dprintf:
4efc6507 6199 case bp_jit_event:
0e30163f
JK
6200 case bp_gnu_ifunc_resolver:
6201 case bp_gnu_ifunc_resolver_return:
79a45b7d 6202 if (opts.addressprint)
3086aeae
DJ
6203 {
6204 annotate_field (4);
54e52265 6205 if (header_of_multiple)
0d381245 6206 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 6207 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 6208 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 6209 else
5af949e3
UW
6210 ui_out_field_core_addr (uiout, "addr",
6211 loc->gdbarch, loc->address);
3086aeae
DJ
6212 }
6213 annotate_field (5);
0d381245 6214 if (!header_of_multiple)
170b53b2 6215 print_breakpoint_location (b, loc);
0d381245 6216 if (b->loc)
a6d9a66e 6217 *last_loc = b->loc;
3086aeae
DJ
6218 break;
6219 }
c906108c 6220
6c95b8df 6221
998580f1 6222 if (loc != NULL && !header_of_multiple)
6c95b8df
PA
6223 {
6224 struct inferior *inf;
998580f1
MK
6225 VEC(int) *inf_num = NULL;
6226 int mi_only = 1;
6c95b8df 6227
998580f1 6228 ALL_INFERIORS (inf)
6c95b8df
PA
6229 {
6230 if (inf->pspace == loc->pspace)
998580f1 6231 VEC_safe_push (int, inf_num, inf->num);
6c95b8df 6232 }
998580f1
MK
6233
6234 /* For backward compatibility, don't display inferiors in CLI unless
6235 there are several. Always display for MI. */
6236 if (allflag
6237 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6238 && (number_of_program_spaces () > 1
6239 || number_of_inferiors () > 1)
6240 /* LOC is for existing B, it cannot be in
6241 moribund_locations and thus having NULL OWNER. */
6242 && loc->owner->type != bp_catchpoint))
6243 mi_only = 0;
6244 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6245 VEC_free (int, inf_num);
6c95b8df
PA
6246 }
6247
4a306c9a 6248 if (!part_of_multiple)
c4093a6a 6249 {
4a306c9a
JB
6250 if (b->thread != -1)
6251 {
6252 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 6253 "stop only in" line a little further down. */
4a306c9a
JB
6254 ui_out_text (uiout, " thread ");
6255 ui_out_field_int (uiout, "thread", b->thread);
6256 }
6257 else if (b->task != 0)
6258 {
6259 ui_out_text (uiout, " task ");
6260 ui_out_field_int (uiout, "task", b->task);
6261 }
c4093a6a 6262 }
f1310107 6263
8b93c638 6264 ui_out_text (uiout, "\n");
f1310107 6265
348d480f 6266 if (!part_of_multiple)
f1310107
TJB
6267 b->ops->print_one_detail (b, uiout);
6268
0d381245 6269 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
6270 {
6271 annotate_field (6);
8b93c638 6272 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 6273 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 6274 the frame ID. */
5af949e3
UW
6275 ui_out_field_core_addr (uiout, "frame",
6276 b->gdbarch, b->frame_id.stack_addr);
8b93c638 6277 ui_out_text (uiout, "\n");
c4093a6a
JM
6278 }
6279
28010a5d 6280 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
6281 {
6282 annotate_field (7);
d77f58be 6283 if (is_tracepoint (b))
1042e4c0
SS
6284 ui_out_text (uiout, "\ttrace only if ");
6285 else
6286 ui_out_text (uiout, "\tstop only if ");
0101ce28 6287 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
6288
6289 /* Print whether the target is doing the breakpoint's condition
6290 evaluation. If GDB is doing the evaluation, don't print anything. */
6291 if (is_breakpoint (b)
6292 && breakpoint_condition_evaluation_mode ()
6293 == condition_evaluation_target)
6294 {
6295 ui_out_text (uiout, " (");
6296 ui_out_field_string (uiout, "evaluated-by",
6297 bp_condition_evaluator (b));
6298 ui_out_text (uiout, " evals)");
6299 }
0101ce28
JJ
6300 ui_out_text (uiout, "\n");
6301 }
6302
0d381245 6303 if (!part_of_multiple && b->thread != -1)
c4093a6a 6304 {
4a64f543 6305 /* FIXME should make an annotation for this. */
8b93c638
JM
6306 ui_out_text (uiout, "\tstop only in thread ");
6307 ui_out_field_int (uiout, "thread", b->thread);
6308 ui_out_text (uiout, "\n");
c4093a6a
JM
6309 }
6310
556ec64d
YQ
6311 if (!part_of_multiple)
6312 {
6313 if (b->hit_count)
31f56a27
YQ
6314 {
6315 /* FIXME should make an annotation for this. */
6316 if (is_catchpoint (b))
6317 ui_out_text (uiout, "\tcatchpoint");
6318 else if (is_tracepoint (b))
6319 ui_out_text (uiout, "\ttracepoint");
6320 else
6321 ui_out_text (uiout, "\tbreakpoint");
6322 ui_out_text (uiout, " already hit ");
6323 ui_out_field_int (uiout, "times", b->hit_count);
6324 if (b->hit_count == 1)
6325 ui_out_text (uiout, " time\n");
6326 else
6327 ui_out_text (uiout, " times\n");
6328 }
556ec64d
YQ
6329 else
6330 {
31f56a27
YQ
6331 /* Output the count also if it is zero, but only if this is mi. */
6332 if (ui_out_is_mi_like_p (uiout))
6333 ui_out_field_int (uiout, "times", b->hit_count);
556ec64d
YQ
6334 }
6335 }
8b93c638 6336
0d381245 6337 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
6338 {
6339 annotate_field (8);
8b93c638
JM
6340 ui_out_text (uiout, "\tignore next ");
6341 ui_out_field_int (uiout, "ignore", b->ignore_count);
6342 ui_out_text (uiout, " hits\n");
c4093a6a 6343 }
059fb39f 6344
816338b5
SS
6345 /* Note that an enable count of 1 corresponds to "enable once"
6346 behavior, which is reported by the combination of enablement and
6347 disposition, so we don't need to mention it here. */
6348 if (!part_of_multiple && b->enable_count > 1)
6349 {
6350 annotate_field (8);
6351 ui_out_text (uiout, "\tdisable after ");
6352 /* Tweak the wording to clarify that ignore and enable counts
6353 are distinct, and have additive effect. */
6354 if (b->ignore_count)
6355 ui_out_text (uiout, "additional ");
6356 else
6357 ui_out_text (uiout, "next ");
6358 ui_out_field_int (uiout, "enable", b->enable_count);
6359 ui_out_text (uiout, " hits\n");
6360 }
6361
f196051f
SS
6362 if (!part_of_multiple && is_tracepoint (b))
6363 {
6364 struct tracepoint *tp = (struct tracepoint *) b;
6365
6366 if (tp->traceframe_usage)
6367 {
6368 ui_out_text (uiout, "\ttrace buffer usage ");
6369 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6370 ui_out_text (uiout, " bytes\n");
6371 }
6372 }
d3ce09f5 6373
9add0f1b 6374 l = b->commands ? b->commands->commands : NULL;
059fb39f 6375 if (!part_of_multiple && l)
c4093a6a 6376 {
3b31d625
EZ
6377 struct cleanup *script_chain;
6378
c4093a6a 6379 annotate_field (9);
3b31d625 6380 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 6381 print_command_lines (uiout, l, 4);
3b31d625 6382 do_cleanups (script_chain);
c4093a6a 6383 }
d24317b4 6384
d9b3f62e 6385 if (is_tracepoint (b))
1042e4c0 6386 {
d9b3f62e
PA
6387 struct tracepoint *t = (struct tracepoint *) b;
6388
6389 if (!part_of_multiple && t->pass_count)
6390 {
6391 annotate_field (10);
6392 ui_out_text (uiout, "\tpass count ");
6393 ui_out_field_int (uiout, "pass", t->pass_count);
6394 ui_out_text (uiout, " \n");
6395 }
f2a8bc8a
YQ
6396
6397 /* Don't display it when tracepoint or tracepoint location is
6398 pending. */
6399 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6400 {
6401 annotate_field (11);
6402
6403 if (ui_out_is_mi_like_p (uiout))
6404 ui_out_field_string (uiout, "installed",
6405 loc->inserted ? "y" : "n");
6406 else
6407 {
6408 if (loc->inserted)
6409 ui_out_text (uiout, "\t");
6410 else
6411 ui_out_text (uiout, "\tnot ");
6412 ui_out_text (uiout, "installed on target\n");
6413 }
6414 }
1042e4c0
SS
6415 }
6416
d24317b4
VP
6417 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6418 {
3a5c3e22
PA
6419 if (is_watchpoint (b))
6420 {
6421 struct watchpoint *w = (struct watchpoint *) b;
6422
6423 ui_out_field_string (uiout, "original-location", w->exp_string);
6424 }
6425 else if (b->addr_string)
d24317b4 6426 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 6427 }
c4093a6a 6428}
c5aa993b 6429
0d381245
VP
6430static void
6431print_one_breakpoint (struct breakpoint *b,
4a64f543 6432 struct bp_location **last_loc,
6c95b8df 6433 int allflag)
0d381245 6434{
8d3788bd 6435 struct cleanup *bkpt_chain;
79a45e25 6436 struct ui_out *uiout = current_uiout;
8d3788bd
VP
6437
6438 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6439
12c5a436 6440 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 6441 do_cleanups (bkpt_chain);
0d381245
VP
6442
6443 /* If this breakpoint has custom print function,
6444 it's already printed. Otherwise, print individual
6445 locations, if any. */
6446 if (b->ops == NULL || b->ops->print_one == NULL)
6447 {
4a64f543
MS
6448 /* If breakpoint has a single location that is disabled, we
6449 print it as if it had several locations, since otherwise it's
6450 hard to represent "breakpoint enabled, location disabled"
6451 situation.
6452
6453 Note that while hardware watchpoints have several locations
a3be7890 6454 internally, that's not a property exposed to user. */
0d381245 6455 if (b->loc
a5606eee 6456 && !is_hardware_watchpoint (b)
8d3788bd 6457 && (b->loc->next || !b->loc->enabled))
0d381245
VP
6458 {
6459 struct bp_location *loc;
6460 int n = 1;
8d3788bd 6461
0d381245 6462 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
6463 {
6464 struct cleanup *inner2 =
6465 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6466 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6467 do_cleanups (inner2);
6468 }
0d381245
VP
6469 }
6470 }
6471}
6472
a6d9a66e
UW
6473static int
6474breakpoint_address_bits (struct breakpoint *b)
6475{
6476 int print_address_bits = 0;
6477 struct bp_location *loc;
6478
6479 for (loc = b->loc; loc; loc = loc->next)
6480 {
c7437ca6
PA
6481 int addr_bit;
6482
6483 /* Software watchpoints that aren't watching memory don't have
6484 an address to print. */
6485 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6486 continue;
6487
6488 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
6489 if (addr_bit > print_address_bits)
6490 print_address_bits = addr_bit;
6491 }
6492
6493 return print_address_bits;
6494}
0d381245 6495
c4093a6a
JM
6496struct captured_breakpoint_query_args
6497 {
6498 int bnum;
6499 };
c5aa993b 6500
c4093a6a 6501static int
2b65245e 6502do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
6503{
6504 struct captured_breakpoint_query_args *args = data;
52f0bd74 6505 struct breakpoint *b;
a6d9a66e 6506 struct bp_location *dummy_loc = NULL;
cc59ec59 6507
c4093a6a
JM
6508 ALL_BREAKPOINTS (b)
6509 {
6510 if (args->bnum == b->number)
c5aa993b 6511 {
12c5a436 6512 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 6513 return GDB_RC_OK;
c5aa993b 6514 }
c4093a6a
JM
6515 }
6516 return GDB_RC_NONE;
6517}
c5aa993b 6518
c4093a6a 6519enum gdb_rc
4a64f543
MS
6520gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6521 char **error_message)
c4093a6a
JM
6522{
6523 struct captured_breakpoint_query_args args;
cc59ec59 6524
c4093a6a
JM
6525 args.bnum = bnum;
6526 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 6527 an error. */
b0b13bb4
DJ
6528 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6529 error_message, RETURN_MASK_ALL) < 0)
6530 return GDB_RC_FAIL;
6531 else
6532 return GDB_RC_OK;
c4093a6a 6533}
c5aa993b 6534
09d682a4
TT
6535/* Return true if this breakpoint was set by the user, false if it is
6536 internal or momentary. */
6537
6538int
6539user_breakpoint_p (struct breakpoint *b)
6540{
46c6471b 6541 return b->number > 0;
09d682a4
TT
6542}
6543
7f3b0473 6544/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
6545 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6546 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6547 FILTER is non-NULL, call it on each breakpoint and only include the
6548 ones for which it returns non-zero. Return the total number of
6549 breakpoints listed. */
c906108c 6550
d77f58be 6551static int
e5a67952 6552breakpoint_1 (char *args, int allflag,
4a64f543 6553 int (*filter) (const struct breakpoint *))
c4093a6a 6554{
52f0bd74 6555 struct breakpoint *b;
a6d9a66e 6556 struct bp_location *last_loc = NULL;
7f3b0473 6557 int nr_printable_breakpoints;
3b31d625 6558 struct cleanup *bkpttbl_chain;
79a45b7d 6559 struct value_print_options opts;
a6d9a66e 6560 int print_address_bits = 0;
269b11a2 6561 int print_type_col_width = 14;
79a45e25 6562 struct ui_out *uiout = current_uiout;
269b11a2 6563
79a45b7d
TT
6564 get_user_print_options (&opts);
6565
4a64f543
MS
6566 /* Compute the number of rows in the table, as well as the size
6567 required for address fields. */
7f3b0473
AC
6568 nr_printable_breakpoints = 0;
6569 ALL_BREAKPOINTS (b)
e5a67952
MS
6570 {
6571 /* If we have a filter, only list the breakpoints it accepts. */
6572 if (filter && !filter (b))
6573 continue;
6574
6575 /* If we have an "args" string, it is a list of breakpoints to
6576 accept. Skip the others. */
6577 if (args != NULL && *args != '\0')
6578 {
6579 if (allflag && parse_and_eval_long (args) != b->number)
6580 continue;
6581 if (!allflag && !number_is_in_list (args, b->number))
6582 continue;
6583 }
269b11a2 6584
e5a67952
MS
6585 if (allflag || user_breakpoint_p (b))
6586 {
6587 int addr_bit, type_len;
a6d9a66e 6588
e5a67952
MS
6589 addr_bit = breakpoint_address_bits (b);
6590 if (addr_bit > print_address_bits)
6591 print_address_bits = addr_bit;
269b11a2 6592
e5a67952
MS
6593 type_len = strlen (bptype_string (b->type));
6594 if (type_len > print_type_col_width)
6595 print_type_col_width = type_len;
6596
6597 nr_printable_breakpoints++;
6598 }
6599 }
7f3b0473 6600
79a45b7d 6601 if (opts.addressprint)
3b31d625 6602 bkpttbl_chain
3e43a32a
MS
6603 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6604 nr_printable_breakpoints,
3b31d625 6605 "BreakpointTable");
8b93c638 6606 else
3b31d625 6607 bkpttbl_chain
3e43a32a
MS
6608 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6609 nr_printable_breakpoints,
3b31d625 6610 "BreakpointTable");
8b93c638 6611
7f3b0473 6612 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6613 annotate_breakpoints_headers ();
6614 if (nr_printable_breakpoints > 0)
6615 annotate_field (0);
4a64f543 6616 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6617 if (nr_printable_breakpoints > 0)
6618 annotate_field (1);
269b11a2 6619 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6620 "type", "Type"); /* 2 */
d7faa9e7
AC
6621 if (nr_printable_breakpoints > 0)
6622 annotate_field (2);
4a64f543 6623 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6624 if (nr_printable_breakpoints > 0)
6625 annotate_field (3);
54e52265 6626 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6627 if (opts.addressprint)
e5a67952
MS
6628 {
6629 if (nr_printable_breakpoints > 0)
6630 annotate_field (4);
6631 if (print_address_bits <= 32)
6632 ui_out_table_header (uiout, 10, ui_left,
6633 "addr", "Address"); /* 5 */
6634 else
6635 ui_out_table_header (uiout, 18, ui_left,
6636 "addr", "Address"); /* 5 */
6637 }
d7faa9e7
AC
6638 if (nr_printable_breakpoints > 0)
6639 annotate_field (5);
6640 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6641 ui_out_table_body (uiout);
6642 if (nr_printable_breakpoints > 0)
6643 annotate_breakpoints_table ();
7f3b0473 6644
c4093a6a 6645 ALL_BREAKPOINTS (b)
e5a67952
MS
6646 {
6647 QUIT;
6648 /* If we have a filter, only list the breakpoints it accepts. */
6649 if (filter && !filter (b))
6650 continue;
6651
6652 /* If we have an "args" string, it is a list of breakpoints to
6653 accept. Skip the others. */
6654
6655 if (args != NULL && *args != '\0')
6656 {
6657 if (allflag) /* maintenance info breakpoint */
6658 {
6659 if (parse_and_eval_long (args) != b->number)
6660 continue;
6661 }
6662 else /* all others */
6663 {
6664 if (!number_is_in_list (args, b->number))
6665 continue;
6666 }
6667 }
6668 /* We only print out user settable breakpoints unless the
6669 allflag is set. */
6670 if (allflag || user_breakpoint_p (b))
12c5a436 6671 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6672 }
6673
3b31d625 6674 do_cleanups (bkpttbl_chain);
698384cd 6675
7f3b0473 6676 if (nr_printable_breakpoints == 0)
c906108c 6677 {
4a64f543
MS
6678 /* If there's a filter, let the caller decide how to report
6679 empty list. */
d77f58be
SS
6680 if (!filter)
6681 {
e5a67952 6682 if (args == NULL || *args == '\0')
d77f58be
SS
6683 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6684 else
4a64f543 6685 ui_out_message (uiout, 0,
e5a67952
MS
6686 "No breakpoint or watchpoint matching '%s'.\n",
6687 args);
d77f58be 6688 }
c906108c
SS
6689 }
6690 else
c4093a6a 6691 {
a6d9a66e
UW
6692 if (last_loc && !server_command)
6693 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6694 }
c906108c 6695
4a64f543 6696 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6697 there have been breakpoints? */
c906108c 6698 annotate_breakpoints_table_end ();
d77f58be
SS
6699
6700 return nr_printable_breakpoints;
c906108c
SS
6701}
6702
ad443146
SS
6703/* Display the value of default-collect in a way that is generally
6704 compatible with the breakpoint list. */
6705
6706static void
6707default_collect_info (void)
6708{
79a45e25
PA
6709 struct ui_out *uiout = current_uiout;
6710
ad443146
SS
6711 /* If it has no value (which is frequently the case), say nothing; a
6712 message like "No default-collect." gets in user's face when it's
6713 not wanted. */
6714 if (!*default_collect)
6715 return;
6716
6717 /* The following phrase lines up nicely with per-tracepoint collect
6718 actions. */
6719 ui_out_text (uiout, "default collect ");
6720 ui_out_field_string (uiout, "default-collect", default_collect);
6721 ui_out_text (uiout, " \n");
6722}
6723
c906108c 6724static void
e5a67952 6725breakpoints_info (char *args, int from_tty)
c906108c 6726{
e5a67952 6727 breakpoint_1 (args, 0, NULL);
ad443146
SS
6728
6729 default_collect_info ();
d77f58be
SS
6730}
6731
6732static void
e5a67952 6733watchpoints_info (char *args, int from_tty)
d77f58be 6734{
e5a67952 6735 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6736 struct ui_out *uiout = current_uiout;
d77f58be
SS
6737
6738 if (num_printed == 0)
6739 {
e5a67952 6740 if (args == NULL || *args == '\0')
d77f58be
SS
6741 ui_out_message (uiout, 0, "No watchpoints.\n");
6742 else
e5a67952 6743 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6744 }
c906108c
SS
6745}
6746
7a292a7a 6747static void
e5a67952 6748maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6749{
e5a67952 6750 breakpoint_1 (args, 1, NULL);
ad443146
SS
6751
6752 default_collect_info ();
c906108c
SS
6753}
6754
0d381245 6755static int
714835d5 6756breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6757 struct program_space *pspace,
714835d5 6758 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6759{
6760 struct bp_location *bl = b->loc;
cc59ec59 6761
0d381245
VP
6762 for (; bl; bl = bl->next)
6763 {
6c95b8df
PA
6764 if (bl->pspace == pspace
6765 && bl->address == pc
0d381245
VP
6766 && (!overlay_debugging || bl->section == section))
6767 return 1;
6768 }
6769 return 0;
6770}
6771
672f9b60 6772/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6773 concerns with logical breakpoints, so we match program spaces, not
6774 address spaces. */
c906108c
SS
6775
6776static void
6c95b8df
PA
6777describe_other_breakpoints (struct gdbarch *gdbarch,
6778 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6779 struct obj_section *section, int thread)
c906108c 6780{
52f0bd74
AC
6781 int others = 0;
6782 struct breakpoint *b;
c906108c
SS
6783
6784 ALL_BREAKPOINTS (b)
672f9b60
KP
6785 others += (user_breakpoint_p (b)
6786 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6787 if (others > 0)
6788 {
a3f17187
AC
6789 if (others == 1)
6790 printf_filtered (_("Note: breakpoint "));
6791 else /* if (others == ???) */
6792 printf_filtered (_("Note: breakpoints "));
c906108c 6793 ALL_BREAKPOINTS (b)
672f9b60 6794 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6795 {
6796 others--;
6797 printf_filtered ("%d", b->number);
6798 if (b->thread == -1 && thread != -1)
6799 printf_filtered (" (all threads)");
6800 else if (b->thread != -1)
6801 printf_filtered (" (thread %d)", b->thread);
6802 printf_filtered ("%s%s ",
059fb39f 6803 ((b->enable_state == bp_disabled
f8eba3c6 6804 || b->enable_state == bp_call_disabled)
0d381245
VP
6805 ? " (disabled)"
6806 : b->enable_state == bp_permanent
6807 ? " (permanent)"
6808 : ""),
6809 (others > 1) ? ","
6810 : ((others == 1) ? " and" : ""));
6811 }
a3f17187 6812 printf_filtered (_("also set at pc "));
5af949e3 6813 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6814 printf_filtered (".\n");
6815 }
6816}
6817\f
c906108c 6818
e4f237da
KB
6819/* Return true iff it is meaningful to use the address member of
6820 BPT. For some breakpoint types, the address member is irrelevant
6821 and it makes no sense to attempt to compare it to other addresses
6822 (or use it for any other purpose either).
6823
4a64f543
MS
6824 More specifically, each of the following breakpoint types will
6825 always have a zero valued address and we don't want to mark
6826 breakpoints of any of these types to be a duplicate of an actual
6827 breakpoint at address zero:
e4f237da
KB
6828
6829 bp_watchpoint
2d134ed3
PA
6830 bp_catchpoint
6831
6832*/
e4f237da
KB
6833
6834static int
6835breakpoint_address_is_meaningful (struct breakpoint *bpt)
6836{
6837 enum bptype type = bpt->type;
6838
2d134ed3
PA
6839 return (type != bp_watchpoint && type != bp_catchpoint);
6840}
6841
6842/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6843 true if LOC1 and LOC2 represent the same watchpoint location. */
6844
6845static int
4a64f543
MS
6846watchpoint_locations_match (struct bp_location *loc1,
6847 struct bp_location *loc2)
2d134ed3 6848{
3a5c3e22
PA
6849 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6850 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6851
6852 /* Both of them must exist. */
6853 gdb_assert (w1 != NULL);
6854 gdb_assert (w2 != NULL);
2bdf28a0 6855
4a64f543
MS
6856 /* If the target can evaluate the condition expression in hardware,
6857 then we we need to insert both watchpoints even if they are at
6858 the same place. Otherwise the watchpoint will only trigger when
6859 the condition of whichever watchpoint was inserted evaluates to
6860 true, not giving a chance for GDB to check the condition of the
6861 other watchpoint. */
3a5c3e22 6862 if ((w1->cond_exp
4a64f543
MS
6863 && target_can_accel_watchpoint_condition (loc1->address,
6864 loc1->length,
0cf6dd15 6865 loc1->watchpoint_type,
3a5c3e22
PA
6866 w1->cond_exp))
6867 || (w2->cond_exp
4a64f543
MS
6868 && target_can_accel_watchpoint_condition (loc2->address,
6869 loc2->length,
0cf6dd15 6870 loc2->watchpoint_type,
3a5c3e22 6871 w2->cond_exp)))
0cf6dd15
TJB
6872 return 0;
6873
85d721b8
PA
6874 /* Note that this checks the owner's type, not the location's. In
6875 case the target does not support read watchpoints, but does
6876 support access watchpoints, we'll have bp_read_watchpoint
6877 watchpoints with hw_access locations. Those should be considered
6878 duplicates of hw_read locations. The hw_read locations will
6879 become hw_access locations later. */
2d134ed3
PA
6880 return (loc1->owner->type == loc2->owner->type
6881 && loc1->pspace->aspace == loc2->pspace->aspace
6882 && loc1->address == loc2->address
6883 && loc1->length == loc2->length);
e4f237da
KB
6884}
6885
31e77af2 6886/* See breakpoint.h. */
6c95b8df 6887
31e77af2 6888int
6c95b8df
PA
6889breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6890 struct address_space *aspace2, CORE_ADDR addr2)
6891{
f5656ead 6892 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6c95b8df
PA
6893 || aspace1 == aspace2)
6894 && addr1 == addr2);
6895}
6896
f1310107
TJB
6897/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6898 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6899 matches ASPACE2. On targets that have global breakpoints, the address
6900 space doesn't really matter. */
6901
6902static int
6903breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6904 int len1, struct address_space *aspace2,
6905 CORE_ADDR addr2)
6906{
f5656ead 6907 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
f1310107
TJB
6908 || aspace1 == aspace2)
6909 && addr2 >= addr1 && addr2 < addr1 + len1);
6910}
6911
6912/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6913 a ranged breakpoint. In most targets, a match happens only if ASPACE
6914 matches the breakpoint's address space. On targets that have global
6915 breakpoints, the address space doesn't really matter. */
6916
6917static int
6918breakpoint_location_address_match (struct bp_location *bl,
6919 struct address_space *aspace,
6920 CORE_ADDR addr)
6921{
6922 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6923 aspace, addr)
6924 || (bl->length
6925 && breakpoint_address_match_range (bl->pspace->aspace,
6926 bl->address, bl->length,
6927 aspace, addr)));
6928}
6929
1e4d1764
YQ
6930/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6931 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6932 true, otherwise returns false. */
6933
6934static int
6935tracepoint_locations_match (struct bp_location *loc1,
6936 struct bp_location *loc2)
6937{
6938 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6939 /* Since tracepoint locations are never duplicated with others', tracepoint
6940 locations at the same address of different tracepoints are regarded as
6941 different locations. */
6942 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6943 else
6944 return 0;
6945}
6946
2d134ed3
PA
6947/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6948 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6949 represent the same location. */
6950
6951static int
4a64f543
MS
6952breakpoint_locations_match (struct bp_location *loc1,
6953 struct bp_location *loc2)
2d134ed3 6954{
2bdf28a0
JK
6955 int hw_point1, hw_point2;
6956
6957 /* Both of them must not be in moribund_locations. */
6958 gdb_assert (loc1->owner != NULL);
6959 gdb_assert (loc2->owner != NULL);
6960
6961 hw_point1 = is_hardware_watchpoint (loc1->owner);
6962 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6963
6964 if (hw_point1 != hw_point2)
6965 return 0;
6966 else if (hw_point1)
6967 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6968 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6969 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6970 else
f1310107
TJB
6971 /* We compare bp_location.length in order to cover ranged breakpoints. */
6972 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6973 loc2->pspace->aspace, loc2->address)
6974 && loc1->length == loc2->length);
2d134ed3
PA
6975}
6976
76897487
KB
6977static void
6978breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6979 int bnum, int have_bnum)
6980{
f63fbe86
MS
6981 /* The longest string possibly returned by hex_string_custom
6982 is 50 chars. These must be at least that big for safety. */
6983 char astr1[64];
6984 char astr2[64];
76897487 6985
bb599908
PH
6986 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6987 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6988 if (have_bnum)
8a3fe4f8 6989 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6990 bnum, astr1, astr2);
6991 else
8a3fe4f8 6992 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6993}
6994
4a64f543
MS
6995/* Adjust a breakpoint's address to account for architectural
6996 constraints on breakpoint placement. Return the adjusted address.
6997 Note: Very few targets require this kind of adjustment. For most
6998 targets, this function is simply the identity function. */
76897487
KB
6999
7000static CORE_ADDR
a6d9a66e
UW
7001adjust_breakpoint_address (struct gdbarch *gdbarch,
7002 CORE_ADDR bpaddr, enum bptype bptype)
76897487 7003{
a6d9a66e 7004 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
7005 {
7006 /* Very few targets need any kind of breakpoint adjustment. */
7007 return bpaddr;
7008 }
88f7da05
KB
7009 else if (bptype == bp_watchpoint
7010 || bptype == bp_hardware_watchpoint
7011 || bptype == bp_read_watchpoint
7012 || bptype == bp_access_watchpoint
fe798b75 7013 || bptype == bp_catchpoint)
88f7da05
KB
7014 {
7015 /* Watchpoints and the various bp_catch_* eventpoints should not
7016 have their addresses modified. */
7017 return bpaddr;
7018 }
76897487
KB
7019 else
7020 {
7021 CORE_ADDR adjusted_bpaddr;
7022
7023 /* Some targets have architectural constraints on the placement
7024 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 7025 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
7026
7027 /* An adjusted breakpoint address can significantly alter
7028 a user's expectations. Print a warning if an adjustment
7029 is required. */
7030 if (adjusted_bpaddr != bpaddr)
7031 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7032
7033 return adjusted_bpaddr;
7034 }
7035}
7036
28010a5d
PA
7037void
7038init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7039 struct breakpoint *owner)
7cc221ef 7040{
7cc221ef
DJ
7041 memset (loc, 0, sizeof (*loc));
7042
348d480f
PA
7043 gdb_assert (ops != NULL);
7044
28010a5d
PA
7045 loc->ops = ops;
7046 loc->owner = owner;
511a6cd4 7047 loc->cond = NULL;
b775012e 7048 loc->cond_bytecode = NULL;
0d381245
VP
7049 loc->shlib_disabled = 0;
7050 loc->enabled = 1;
e049a4b5 7051
28010a5d 7052 switch (owner->type)
e049a4b5
DJ
7053 {
7054 case bp_breakpoint:
7055 case bp_until:
7056 case bp_finish:
7057 case bp_longjmp:
7058 case bp_longjmp_resume:
e2e4d78b 7059 case bp_longjmp_call_dummy:
186c406b
TT
7060 case bp_exception:
7061 case bp_exception_resume:
e049a4b5 7062 case bp_step_resume:
2c03e5be 7063 case bp_hp_step_resume:
e049a4b5
DJ
7064 case bp_watchpoint_scope:
7065 case bp_call_dummy:
aa7d318d 7066 case bp_std_terminate:
e049a4b5
DJ
7067 case bp_shlib_event:
7068 case bp_thread_event:
7069 case bp_overlay_event:
4efc6507 7070 case bp_jit_event:
0fd8e87f 7071 case bp_longjmp_master:
aa7d318d 7072 case bp_std_terminate_master:
186c406b 7073 case bp_exception_master:
0e30163f
JK
7074 case bp_gnu_ifunc_resolver:
7075 case bp_gnu_ifunc_resolver_return:
e7e0cddf 7076 case bp_dprintf:
e049a4b5 7077 loc->loc_type = bp_loc_software_breakpoint;
b775012e 7078 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7079 break;
7080 case bp_hardware_breakpoint:
7081 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 7082 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
7083 break;
7084 case bp_hardware_watchpoint:
7085 case bp_read_watchpoint:
7086 case bp_access_watchpoint:
7087 loc->loc_type = bp_loc_hardware_watchpoint;
7088 break;
7089 case bp_watchpoint:
ce78b96d 7090 case bp_catchpoint:
15c3d785
PA
7091 case bp_tracepoint:
7092 case bp_fast_tracepoint:
0fb4aa4b 7093 case bp_static_tracepoint:
e049a4b5
DJ
7094 loc->loc_type = bp_loc_other;
7095 break;
7096 default:
e2e0b3e5 7097 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
7098 }
7099
f431efe5 7100 loc->refc = 1;
28010a5d
PA
7101}
7102
7103/* Allocate a struct bp_location. */
7104
7105static struct bp_location *
7106allocate_bp_location (struct breakpoint *bpt)
7107{
348d480f
PA
7108 return bpt->ops->allocate_location (bpt);
7109}
7cc221ef 7110
f431efe5
PA
7111static void
7112free_bp_location (struct bp_location *loc)
fe3f5fa8 7113{
348d480f 7114 loc->ops->dtor (loc);
fe3f5fa8
VP
7115 xfree (loc);
7116}
7117
f431efe5
PA
7118/* Increment reference count. */
7119
7120static void
7121incref_bp_location (struct bp_location *bl)
7122{
7123 ++bl->refc;
7124}
7125
7126/* Decrement reference count. If the reference count reaches 0,
7127 destroy the bp_location. Sets *BLP to NULL. */
7128
7129static void
7130decref_bp_location (struct bp_location **blp)
7131{
0807b50c
PA
7132 gdb_assert ((*blp)->refc > 0);
7133
f431efe5
PA
7134 if (--(*blp)->refc == 0)
7135 free_bp_location (*blp);
7136 *blp = NULL;
7137}
7138
346774a9 7139/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 7140
346774a9
PA
7141static void
7142add_to_breakpoint_chain (struct breakpoint *b)
c906108c 7143{
346774a9 7144 struct breakpoint *b1;
c906108c 7145
346774a9
PA
7146 /* Add this breakpoint to the end of the chain so that a list of
7147 breakpoints will come out in order of increasing numbers. */
7148
7149 b1 = breakpoint_chain;
7150 if (b1 == 0)
7151 breakpoint_chain = b;
7152 else
7153 {
7154 while (b1->next)
7155 b1 = b1->next;
7156 b1->next = b;
7157 }
7158}
7159
7160/* Initializes breakpoint B with type BPTYPE and no locations yet. */
7161
7162static void
7163init_raw_breakpoint_without_location (struct breakpoint *b,
7164 struct gdbarch *gdbarch,
28010a5d 7165 enum bptype bptype,
c0a91b2b 7166 const struct breakpoint_ops *ops)
346774a9 7167{
c906108c 7168 memset (b, 0, sizeof (*b));
2219d63c 7169
348d480f
PA
7170 gdb_assert (ops != NULL);
7171
28010a5d 7172 b->ops = ops;
4d28f7a8 7173 b->type = bptype;
a6d9a66e 7174 b->gdbarch = gdbarch;
c906108c
SS
7175 b->language = current_language->la_language;
7176 b->input_radix = input_radix;
7177 b->thread = -1;
b5de0fa7 7178 b->enable_state = bp_enabled;
c906108c
SS
7179 b->next = 0;
7180 b->silent = 0;
7181 b->ignore_count = 0;
7182 b->commands = NULL;
818dd999 7183 b->frame_id = null_frame_id;
0d381245 7184 b->condition_not_parsed = 0;
84f4c1fe 7185 b->py_bp_object = NULL;
d0fb5eae 7186 b->related_breakpoint = b;
346774a9
PA
7187}
7188
7189/* Helper to set_raw_breakpoint below. Creates a breakpoint
7190 that has type BPTYPE and has no locations as yet. */
346774a9
PA
7191
7192static struct breakpoint *
7193set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 7194 enum bptype bptype,
c0a91b2b 7195 const struct breakpoint_ops *ops)
346774a9
PA
7196{
7197 struct breakpoint *b = XNEW (struct breakpoint);
7198
348d480f 7199 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 7200 add_to_breakpoint_chain (b);
0d381245
VP
7201 return b;
7202}
7203
0e30163f
JK
7204/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7205 resolutions should be made as the user specified the location explicitly
7206 enough. */
7207
0d381245 7208static void
0e30163f 7209set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 7210{
2bdf28a0
JK
7211 gdb_assert (loc->owner != NULL);
7212
0d381245 7213 if (loc->owner->type == bp_breakpoint
1042e4c0 7214 || loc->owner->type == bp_hardware_breakpoint
d77f58be 7215 || is_tracepoint (loc->owner))
0d381245 7216 {
0e30163f 7217 int is_gnu_ifunc;
2c02bd72 7218 const char *function_name;
6a3a010b 7219 CORE_ADDR func_addr;
0e30163f 7220
2c02bd72 7221 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 7222 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
7223
7224 if (is_gnu_ifunc && !explicit_loc)
7225 {
7226 struct breakpoint *b = loc->owner;
7227
7228 gdb_assert (loc->pspace == current_program_space);
2c02bd72 7229 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
7230 &loc->requested_address))
7231 {
7232 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7233 loc->address = adjust_breakpoint_address (loc->gdbarch,
7234 loc->requested_address,
7235 b->type);
7236 }
7237 else if (b->type == bp_breakpoint && b->loc == loc
7238 && loc->next == NULL && b->related_breakpoint == b)
7239 {
7240 /* Create only the whole new breakpoint of this type but do not
7241 mess more complicated breakpoints with multiple locations. */
7242 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
7243 /* Remember the resolver's address for use by the return
7244 breakpoint. */
7245 loc->related_address = func_addr;
0e30163f
JK
7246 }
7247 }
7248
2c02bd72
DE
7249 if (function_name)
7250 loc->function_name = xstrdup (function_name);
0d381245
VP
7251 }
7252}
7253
a6d9a66e 7254/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 7255struct gdbarch *
a6d9a66e
UW
7256get_sal_arch (struct symtab_and_line sal)
7257{
7258 if (sal.section)
7259 return get_objfile_arch (sal.section->objfile);
7260 if (sal.symtab)
7261 return get_objfile_arch (sal.symtab->objfile);
7262
7263 return NULL;
7264}
7265
346774a9
PA
7266/* Low level routine for partially initializing a breakpoint of type
7267 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 7268 file name, and line number are provided by SAL.
0d381245
VP
7269
7270 It is expected that the caller will complete the initialization of
7271 the newly created breakpoint struct as well as output any status
c56053d2 7272 information regarding the creation of a new breakpoint. */
0d381245 7273
346774a9
PA
7274static void
7275init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 7276 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7277 const struct breakpoint_ops *ops)
0d381245 7278{
28010a5d 7279 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 7280
3742cc8b 7281 add_location_to_breakpoint (b, &sal);
0d381245 7282
6c95b8df
PA
7283 if (bptype != bp_catchpoint)
7284 gdb_assert (sal.pspace != NULL);
7285
f8eba3c6
TT
7286 /* Store the program space that was used to set the breakpoint,
7287 except for ordinary breakpoints, which are independent of the
7288 program space. */
7289 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7290 b->pspace = sal.pspace;
346774a9 7291}
c906108c 7292
346774a9
PA
7293/* set_raw_breakpoint is a low level routine for allocating and
7294 partially initializing a breakpoint of type BPTYPE. The newly
7295 created breakpoint's address, section, source file name, and line
7296 number are provided by SAL. The newly created and partially
7297 initialized breakpoint is added to the breakpoint chain and
7298 is also returned as the value of this function.
7299
7300 It is expected that the caller will complete the initialization of
7301 the newly created breakpoint struct as well as output any status
7302 information regarding the creation of a new breakpoint. In
7303 particular, set_raw_breakpoint does NOT set the breakpoint
7304 number! Care should be taken to not allow an error to occur
7305 prior to completing the initialization of the breakpoint. If this
7306 should happen, a bogus breakpoint will be left on the chain. */
7307
7308struct breakpoint *
7309set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 7310 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 7311 const struct breakpoint_ops *ops)
346774a9
PA
7312{
7313 struct breakpoint *b = XNEW (struct breakpoint);
7314
348d480f 7315 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 7316 add_to_breakpoint_chain (b);
c906108c
SS
7317 return b;
7318}
7319
c2c6d25f
JM
7320
7321/* Note that the breakpoint object B describes a permanent breakpoint
7322 instruction, hard-wired into the inferior's code. */
7323void
7324make_breakpoint_permanent (struct breakpoint *b)
7325{
0d381245 7326 struct bp_location *bl;
cc59ec59 7327
b5de0fa7 7328 b->enable_state = bp_permanent;
c2c6d25f 7329
4a64f543
MS
7330 /* By definition, permanent breakpoints are already present in the
7331 code. Mark all locations as inserted. For now,
7332 make_breakpoint_permanent is called in just one place, so it's
7333 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 7334 multiple locations or not, but it's easy to implement. */
0d381245
VP
7335 for (bl = b->loc; bl; bl = bl->next)
7336 bl->inserted = 1;
c2c6d25f
JM
7337}
7338
53a5351d 7339/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
7340 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7341 initiated the operation. */
c906108c
SS
7342
7343void
186c406b 7344set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 7345{
35df4500 7346 struct breakpoint *b, *b_tmp;
186c406b 7347 int thread = tp->num;
0fd8e87f
UW
7348
7349 /* To avoid having to rescan all objfile symbols at every step,
7350 we maintain a list of continually-inserted but always disabled
7351 longjmp "master" breakpoints. Here, we simply create momentary
7352 clones of those and enable them for the requested thread. */
35df4500 7353 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 7354 if (b->pspace == current_program_space
186c406b
TT
7355 && (b->type == bp_longjmp_master
7356 || b->type == bp_exception_master))
0fd8e87f 7357 {
06edf0c0
PA
7358 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7359 struct breakpoint *clone;
cc59ec59 7360
e2e4d78b
JK
7361 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7362 after their removal. */
06edf0c0 7363 clone = momentary_breakpoint_from_master (b, type,
e2e4d78b 7364 &longjmp_breakpoint_ops);
0fd8e87f
UW
7365 clone->thread = thread;
7366 }
186c406b
TT
7367
7368 tp->initiating_frame = frame;
c906108c
SS
7369}
7370
611c83ae 7371/* Delete all longjmp breakpoints from THREAD. */
c906108c 7372void
611c83ae 7373delete_longjmp_breakpoint (int thread)
c906108c 7374{
35df4500 7375 struct breakpoint *b, *b_tmp;
c906108c 7376
35df4500 7377 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 7378 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
7379 {
7380 if (b->thread == thread)
7381 delete_breakpoint (b);
7382 }
c906108c
SS
7383}
7384
f59f708a
PA
7385void
7386delete_longjmp_breakpoint_at_next_stop (int thread)
7387{
7388 struct breakpoint *b, *b_tmp;
7389
7390 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7391 if (b->type == bp_longjmp || b->type == bp_exception)
7392 {
7393 if (b->thread == thread)
7394 b->disposition = disp_del_at_next_stop;
7395 }
7396}
7397
e2e4d78b
JK
7398/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7399 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7400 pointer to any of them. Return NULL if this system cannot place longjmp
7401 breakpoints. */
7402
7403struct breakpoint *
7404set_longjmp_breakpoint_for_call_dummy (void)
7405{
7406 struct breakpoint *b, *retval = NULL;
7407
7408 ALL_BREAKPOINTS (b)
7409 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7410 {
7411 struct breakpoint *new_b;
7412
7413 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7414 &momentary_breakpoint_ops);
7415 new_b->thread = pid_to_thread_id (inferior_ptid);
7416
7417 /* Link NEW_B into the chain of RETVAL breakpoints. */
7418
7419 gdb_assert (new_b->related_breakpoint == new_b);
7420 if (retval == NULL)
7421 retval = new_b;
7422 new_b->related_breakpoint = retval;
7423 while (retval->related_breakpoint != new_b->related_breakpoint)
7424 retval = retval->related_breakpoint;
7425 retval->related_breakpoint = new_b;
7426 }
7427
7428 return retval;
7429}
7430
7431/* Verify all existing dummy frames and their associated breakpoints for
7432 THREAD. Remove those which can no longer be found in the current frame
7433 stack.
7434
7435 You should call this function only at places where it is safe to currently
7436 unwind the whole stack. Failed stack unwind would discard live dummy
7437 frames. */
7438
7439void
7440check_longjmp_breakpoint_for_call_dummy (int thread)
7441{
7442 struct breakpoint *b, *b_tmp;
7443
7444 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7445 if (b->type == bp_longjmp_call_dummy && b->thread == thread)
7446 {
7447 struct breakpoint *dummy_b = b->related_breakpoint;
7448
7449 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7450 dummy_b = dummy_b->related_breakpoint;
7451 if (dummy_b->type != bp_call_dummy
7452 || frame_find_by_id (dummy_b->frame_id) != NULL)
7453 continue;
7454
7455 dummy_frame_discard (dummy_b->frame_id);
7456
7457 while (b->related_breakpoint != b)
7458 {
7459 if (b_tmp == b->related_breakpoint)
7460 b_tmp = b->related_breakpoint->next;
7461 delete_breakpoint (b->related_breakpoint);
7462 }
7463 delete_breakpoint (b);
7464 }
7465}
7466
1900040c
MS
7467void
7468enable_overlay_breakpoints (void)
7469{
52f0bd74 7470 struct breakpoint *b;
1900040c
MS
7471
7472 ALL_BREAKPOINTS (b)
7473 if (b->type == bp_overlay_event)
7474 {
7475 b->enable_state = bp_enabled;
b60e7edf 7476 update_global_location_list (1);
c02f5703 7477 overlay_events_enabled = 1;
1900040c
MS
7478 }
7479}
7480
7481void
7482disable_overlay_breakpoints (void)
7483{
52f0bd74 7484 struct breakpoint *b;
1900040c
MS
7485
7486 ALL_BREAKPOINTS (b)
7487 if (b->type == bp_overlay_event)
7488 {
7489 b->enable_state = bp_disabled;
b60e7edf 7490 update_global_location_list (0);
c02f5703 7491 overlay_events_enabled = 0;
1900040c
MS
7492 }
7493}
7494
aa7d318d
TT
7495/* Set an active std::terminate breakpoint for each std::terminate
7496 master breakpoint. */
7497void
7498set_std_terminate_breakpoint (void)
7499{
35df4500 7500 struct breakpoint *b, *b_tmp;
aa7d318d 7501
35df4500 7502 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7503 if (b->pspace == current_program_space
7504 && b->type == bp_std_terminate_master)
7505 {
06edf0c0
PA
7506 momentary_breakpoint_from_master (b, bp_std_terminate,
7507 &momentary_breakpoint_ops);
aa7d318d
TT
7508 }
7509}
7510
7511/* Delete all the std::terminate breakpoints. */
7512void
7513delete_std_terminate_breakpoint (void)
7514{
35df4500 7515 struct breakpoint *b, *b_tmp;
aa7d318d 7516
35df4500 7517 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
7518 if (b->type == bp_std_terminate)
7519 delete_breakpoint (b);
7520}
7521
c4093a6a 7522struct breakpoint *
a6d9a66e 7523create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
7524{
7525 struct breakpoint *b;
c4093a6a 7526
06edf0c0
PA
7527 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7528 &internal_breakpoint_ops);
7529
b5de0fa7 7530 b->enable_state = bp_enabled;
c4093a6a 7531 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
7532 b->addr_string
7533 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 7534
b60e7edf 7535 update_global_location_list_nothrow (1);
74960c60 7536
c4093a6a
JM
7537 return b;
7538}
7539
7540void
7541remove_thread_event_breakpoints (void)
7542{
35df4500 7543 struct breakpoint *b, *b_tmp;
c4093a6a 7544
35df4500 7545 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7546 if (b->type == bp_thread_event
7547 && b->loc->pspace == current_program_space)
c4093a6a
JM
7548 delete_breakpoint (b);
7549}
7550
0101ce28
JJ
7551struct lang_and_radix
7552 {
7553 enum language lang;
7554 int radix;
7555 };
7556
4efc6507
DE
7557/* Create a breakpoint for JIT code registration and unregistration. */
7558
7559struct breakpoint *
7560create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7561{
7562 struct breakpoint *b;
7563
06edf0c0
PA
7564 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7565 &internal_breakpoint_ops);
4efc6507
DE
7566 update_global_location_list_nothrow (1);
7567 return b;
7568}
0101ce28 7569
03673fc7
PP
7570/* Remove JIT code registration and unregistration breakpoint(s). */
7571
7572void
7573remove_jit_event_breakpoints (void)
7574{
7575 struct breakpoint *b, *b_tmp;
7576
7577 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7578 if (b->type == bp_jit_event
7579 && b->loc->pspace == current_program_space)
7580 delete_breakpoint (b);
7581}
7582
cae688ec
JJ
7583void
7584remove_solib_event_breakpoints (void)
7585{
35df4500 7586 struct breakpoint *b, *b_tmp;
cae688ec 7587
35df4500 7588 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7589 if (b->type == bp_shlib_event
7590 && b->loc->pspace == current_program_space)
cae688ec
JJ
7591 delete_breakpoint (b);
7592}
7593
7594struct breakpoint *
a6d9a66e 7595create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
7596{
7597 struct breakpoint *b;
7598
06edf0c0
PA
7599 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7600 &internal_breakpoint_ops);
b60e7edf 7601 update_global_location_list_nothrow (1);
cae688ec
JJ
7602 return b;
7603}
7604
7605/* Disable any breakpoints that are on code in shared libraries. Only
7606 apply to enabled breakpoints, disabled ones can just stay disabled. */
7607
7608void
cb851954 7609disable_breakpoints_in_shlibs (void)
cae688ec 7610{
876fa593 7611 struct bp_location *loc, **locp_tmp;
cae688ec 7612
876fa593 7613 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7614 {
2bdf28a0 7615 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7616 struct breakpoint *b = loc->owner;
2bdf28a0 7617
4a64f543
MS
7618 /* We apply the check to all breakpoints, including disabled for
7619 those with loc->duplicate set. This is so that when breakpoint
7620 becomes enabled, or the duplicate is removed, gdb will try to
7621 insert all breakpoints. If we don't set shlib_disabled here,
7622 we'll try to insert those breakpoints and fail. */
1042e4c0 7623 if (((b->type == bp_breakpoint)
508ccb1f 7624 || (b->type == bp_jit_event)
1042e4c0 7625 || (b->type == bp_hardware_breakpoint)
d77f58be 7626 || (is_tracepoint (b)))
6c95b8df 7627 && loc->pspace == current_program_space
0d381245 7628 && !loc->shlib_disabled
6c95b8df 7629 && solib_name_from_address (loc->pspace, loc->address)
a77053c2 7630 )
0d381245
VP
7631 {
7632 loc->shlib_disabled = 1;
7633 }
cae688ec
JJ
7634 }
7635}
7636
63644780
NB
7637/* Disable any breakpoints and tracepoints that are in SOLIB upon
7638 notification of unloaded_shlib. Only apply to enabled breakpoints,
7639 disabled ones can just stay disabled. */
84acb35a 7640
75149521 7641static void
84acb35a
JJ
7642disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7643{
876fa593 7644 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7645 int disabled_shlib_breaks = 0;
7646
c86cf029
VP
7647 /* SunOS a.out shared libraries are always mapped, so do not
7648 disable breakpoints; they will only be reported as unloaded
7649 through clear_solib when GDB discards its shared library
7650 list. See clear_solib for more information. */
7651 if (exec_bfd != NULL
7652 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7653 return;
7654
876fa593 7655 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7656 {
2bdf28a0 7657 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7658 struct breakpoint *b = loc->owner;
cc59ec59 7659
1e4d1764 7660 if (solib->pspace == loc->pspace
e2dd7057 7661 && !loc->shlib_disabled
1e4d1764
YQ
7662 && (((b->type == bp_breakpoint
7663 || b->type == bp_jit_event
7664 || b->type == bp_hardware_breakpoint)
7665 && (loc->loc_type == bp_loc_hardware_breakpoint
7666 || loc->loc_type == bp_loc_software_breakpoint))
7667 || is_tracepoint (b))
e2dd7057 7668 && solib_contains_address_p (solib, loc->address))
84acb35a 7669 {
e2dd7057
PP
7670 loc->shlib_disabled = 1;
7671 /* At this point, we cannot rely on remove_breakpoint
7672 succeeding so we must mark the breakpoint as not inserted
7673 to prevent future errors occurring in remove_breakpoints. */
7674 loc->inserted = 0;
8d3788bd
VP
7675
7676 /* This may cause duplicate notifications for the same breakpoint. */
7677 observer_notify_breakpoint_modified (b);
7678
e2dd7057
PP
7679 if (!disabled_shlib_breaks)
7680 {
7681 target_terminal_ours_for_output ();
3e43a32a
MS
7682 warning (_("Temporarily disabling breakpoints "
7683 "for unloaded shared library \"%s\""),
e2dd7057 7684 solib->so_name);
84acb35a 7685 }
e2dd7057 7686 disabled_shlib_breaks = 1;
84acb35a
JJ
7687 }
7688 }
84acb35a
JJ
7689}
7690
63644780
NB
7691/* Disable any breakpoints and tracepoints in OBJFILE upon
7692 notification of free_objfile. Only apply to enabled breakpoints,
7693 disabled ones can just stay disabled. */
7694
7695static void
7696disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7697{
7698 struct breakpoint *b;
7699
7700 if (objfile == NULL)
7701 return;
7702
08351840
PA
7703 /* OBJF_USERLOADED are dynamic modules manually managed by the user
7704 with add-symbol-file/remove-symbol-file. Similarly to how
7705 breakpoints in shared libraries are handled in response to
7706 "nosharedlibrary", mark breakpoints in OBJF_USERLOADED modules
7707 shlib_disabled so they end up uninserted on the next global
7708 location list update. Shared libraries not loaded by the user
7709 aren't handled here -- they're already handled in
7710 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7711 solib_unloaded observer. We skip objfiles that are not
7712 OBJF_USERLOADED (nor OBJF_SHARED) as those aren't considered
7713 dynamic objects (e.g. the main objfile). */
7714 if ((objfile->flags & OBJF_USERLOADED) == 0)
63644780
NB
7715 return;
7716
7717 ALL_BREAKPOINTS (b)
7718 {
7719 struct bp_location *loc;
7720 int bp_modified = 0;
7721
7722 if (!is_breakpoint (b) && !is_tracepoint (b))
7723 continue;
7724
7725 for (loc = b->loc; loc != NULL; loc = loc->next)
7726 {
7727 CORE_ADDR loc_addr = loc->address;
7728
7729 if (loc->loc_type != bp_loc_hardware_breakpoint
7730 && loc->loc_type != bp_loc_software_breakpoint)
7731 continue;
7732
7733 if (loc->shlib_disabled != 0)
7734 continue;
7735
7736 if (objfile->pspace != loc->pspace)
7737 continue;
7738
7739 if (loc->loc_type != bp_loc_hardware_breakpoint
7740 && loc->loc_type != bp_loc_software_breakpoint)
7741 continue;
7742
7743 if (is_addr_in_objfile (loc_addr, objfile))
7744 {
7745 loc->shlib_disabled = 1;
08351840
PA
7746 /* At this point, we don't know whether the object was
7747 unmapped from the inferior or not, so leave the
7748 inserted flag alone. We'll handle failure to
7749 uninsert quietly, in case the object was indeed
7750 unmapped. */
63644780
NB
7751
7752 mark_breakpoint_location_modified (loc);
7753
7754 bp_modified = 1;
7755 }
7756 }
7757
7758 if (bp_modified)
7759 observer_notify_breakpoint_modified (b);
7760 }
7761}
7762
ce78b96d
JB
7763/* FORK & VFORK catchpoints. */
7764
e29a4733
PA
7765/* An instance of this type is used to represent a fork or vfork
7766 catchpoint. It includes a "struct breakpoint" as a kind of base
7767 class; users downcast to "struct breakpoint *" when needed. A
7768 breakpoint is really of this type iff its ops pointer points to
7769 CATCH_FORK_BREAKPOINT_OPS. */
7770
7771struct fork_catchpoint
7772{
7773 /* The base class. */
7774 struct breakpoint base;
7775
7776 /* Process id of a child process whose forking triggered this
7777 catchpoint. This field is only valid immediately after this
7778 catchpoint has triggered. */
7779 ptid_t forked_inferior_pid;
7780};
7781
4a64f543
MS
7782/* Implement the "insert" breakpoint_ops method for fork
7783 catchpoints. */
ce78b96d 7784
77b06cd7
TJB
7785static int
7786insert_catch_fork (struct bp_location *bl)
ce78b96d 7787{
dfd4cc63 7788 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7789}
7790
4a64f543
MS
7791/* Implement the "remove" breakpoint_ops method for fork
7792 catchpoints. */
ce78b96d
JB
7793
7794static int
77b06cd7 7795remove_catch_fork (struct bp_location *bl)
ce78b96d 7796{
dfd4cc63 7797 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7798}
7799
7800/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7801 catchpoints. */
7802
7803static int
f1310107 7804breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7805 struct address_space *aspace, CORE_ADDR bp_addr,
7806 const struct target_waitstatus *ws)
ce78b96d 7807{
e29a4733
PA
7808 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7809
f90263c1
TT
7810 if (ws->kind != TARGET_WAITKIND_FORKED)
7811 return 0;
7812
7813 c->forked_inferior_pid = ws->value.related_pid;
7814 return 1;
ce78b96d
JB
7815}
7816
4a64f543
MS
7817/* Implement the "print_it" breakpoint_ops method for fork
7818 catchpoints. */
ce78b96d
JB
7819
7820static enum print_stop_action
348d480f 7821print_it_catch_fork (bpstat bs)
ce78b96d 7822{
36dfb11c 7823 struct ui_out *uiout = current_uiout;
348d480f
PA
7824 struct breakpoint *b = bs->breakpoint_at;
7825 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7826
ce78b96d 7827 annotate_catchpoint (b->number);
36dfb11c
TT
7828 if (b->disposition == disp_del)
7829 ui_out_text (uiout, "\nTemporary catchpoint ");
7830 else
7831 ui_out_text (uiout, "\nCatchpoint ");
7832 if (ui_out_is_mi_like_p (uiout))
7833 {
7834 ui_out_field_string (uiout, "reason",
7835 async_reason_lookup (EXEC_ASYNC_FORK));
7836 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7837 }
7838 ui_out_field_int (uiout, "bkptno", b->number);
7839 ui_out_text (uiout, " (forked process ");
7840 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7841 ui_out_text (uiout, "), ");
ce78b96d
JB
7842 return PRINT_SRC_AND_LOC;
7843}
7844
4a64f543
MS
7845/* Implement the "print_one" breakpoint_ops method for fork
7846 catchpoints. */
ce78b96d
JB
7847
7848static void
a6d9a66e 7849print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7850{
e29a4733 7851 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7852 struct value_print_options opts;
79a45e25 7853 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7854
7855 get_user_print_options (&opts);
7856
4a64f543
MS
7857 /* Field 4, the address, is omitted (which makes the columns not
7858 line up too nicely with the headers, but the effect is relatively
7859 readable). */
79a45b7d 7860 if (opts.addressprint)
ce78b96d
JB
7861 ui_out_field_skip (uiout, "addr");
7862 annotate_field (5);
7863 ui_out_text (uiout, "fork");
e29a4733 7864 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7865 {
7866 ui_out_text (uiout, ", process ");
7867 ui_out_field_int (uiout, "what",
e29a4733 7868 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7869 ui_out_spaces (uiout, 1);
7870 }
8ac3646f
TT
7871
7872 if (ui_out_is_mi_like_p (uiout))
7873 ui_out_field_string (uiout, "catch-type", "fork");
ce78b96d
JB
7874}
7875
7876/* Implement the "print_mention" breakpoint_ops method for fork
7877 catchpoints. */
7878
7879static void
7880print_mention_catch_fork (struct breakpoint *b)
7881{
7882 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7883}
7884
6149aea9
PA
7885/* Implement the "print_recreate" breakpoint_ops method for fork
7886 catchpoints. */
7887
7888static void
7889print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7890{
7891 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7892 print_recreate_thread (b, fp);
6149aea9
PA
7893}
7894
ce78b96d
JB
7895/* The breakpoint_ops structure to be used in fork catchpoints. */
7896
2060206e 7897static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7898
4a64f543
MS
7899/* Implement the "insert" breakpoint_ops method for vfork
7900 catchpoints. */
ce78b96d 7901
77b06cd7
TJB
7902static int
7903insert_catch_vfork (struct bp_location *bl)
ce78b96d 7904{
dfd4cc63 7905 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7906}
7907
4a64f543
MS
7908/* Implement the "remove" breakpoint_ops method for vfork
7909 catchpoints. */
ce78b96d
JB
7910
7911static int
77b06cd7 7912remove_catch_vfork (struct bp_location *bl)
ce78b96d 7913{
dfd4cc63 7914 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
ce78b96d
JB
7915}
7916
7917/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7918 catchpoints. */
7919
7920static int
f1310107 7921breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
7922 struct address_space *aspace, CORE_ADDR bp_addr,
7923 const struct target_waitstatus *ws)
ce78b96d 7924{
e29a4733
PA
7925 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7926
f90263c1
TT
7927 if (ws->kind != TARGET_WAITKIND_VFORKED)
7928 return 0;
7929
7930 c->forked_inferior_pid = ws->value.related_pid;
7931 return 1;
ce78b96d
JB
7932}
7933
4a64f543
MS
7934/* Implement the "print_it" breakpoint_ops method for vfork
7935 catchpoints. */
ce78b96d
JB
7936
7937static enum print_stop_action
348d480f 7938print_it_catch_vfork (bpstat bs)
ce78b96d 7939{
36dfb11c 7940 struct ui_out *uiout = current_uiout;
348d480f 7941 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7942 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7943
ce78b96d 7944 annotate_catchpoint (b->number);
36dfb11c
TT
7945 if (b->disposition == disp_del)
7946 ui_out_text (uiout, "\nTemporary catchpoint ");
7947 else
7948 ui_out_text (uiout, "\nCatchpoint ");
7949 if (ui_out_is_mi_like_p (uiout))
7950 {
7951 ui_out_field_string (uiout, "reason",
7952 async_reason_lookup (EXEC_ASYNC_VFORK));
7953 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7954 }
7955 ui_out_field_int (uiout, "bkptno", b->number);
7956 ui_out_text (uiout, " (vforked process ");
7957 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7958 ui_out_text (uiout, "), ");
ce78b96d
JB
7959 return PRINT_SRC_AND_LOC;
7960}
7961
4a64f543
MS
7962/* Implement the "print_one" breakpoint_ops method for vfork
7963 catchpoints. */
ce78b96d
JB
7964
7965static void
a6d9a66e 7966print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7967{
e29a4733 7968 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7969 struct value_print_options opts;
79a45e25 7970 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7971
7972 get_user_print_options (&opts);
4a64f543
MS
7973 /* Field 4, the address, is omitted (which makes the columns not
7974 line up too nicely with the headers, but the effect is relatively
7975 readable). */
79a45b7d 7976 if (opts.addressprint)
ce78b96d
JB
7977 ui_out_field_skip (uiout, "addr");
7978 annotate_field (5);
7979 ui_out_text (uiout, "vfork");
e29a4733 7980 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7981 {
7982 ui_out_text (uiout, ", process ");
7983 ui_out_field_int (uiout, "what",
e29a4733 7984 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7985 ui_out_spaces (uiout, 1);
7986 }
8ac3646f
TT
7987
7988 if (ui_out_is_mi_like_p (uiout))
7989 ui_out_field_string (uiout, "catch-type", "vfork");
ce78b96d
JB
7990}
7991
7992/* Implement the "print_mention" breakpoint_ops method for vfork
7993 catchpoints. */
7994
7995static void
7996print_mention_catch_vfork (struct breakpoint *b)
7997{
7998 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7999}
8000
6149aea9
PA
8001/* Implement the "print_recreate" breakpoint_ops method for vfork
8002 catchpoints. */
8003
8004static void
8005print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8006{
8007 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 8008 print_recreate_thread (b, fp);
6149aea9
PA
8009}
8010
ce78b96d
JB
8011/* The breakpoint_ops structure to be used in vfork catchpoints. */
8012
2060206e 8013static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 8014
edcc5120
TT
8015/* An instance of this type is used to represent an solib catchpoint.
8016 It includes a "struct breakpoint" as a kind of base class; users
8017 downcast to "struct breakpoint *" when needed. A breakpoint is
8018 really of this type iff its ops pointer points to
8019 CATCH_SOLIB_BREAKPOINT_OPS. */
8020
8021struct solib_catchpoint
8022{
8023 /* The base class. */
8024 struct breakpoint base;
8025
8026 /* True for "catch load", false for "catch unload". */
8027 unsigned char is_load;
8028
8029 /* Regular expression to match, if any. COMPILED is only valid when
8030 REGEX is non-NULL. */
8031 char *regex;
8032 regex_t compiled;
8033};
8034
8035static void
8036dtor_catch_solib (struct breakpoint *b)
8037{
8038 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8039
8040 if (self->regex)
8041 regfree (&self->compiled);
8042 xfree (self->regex);
8043
8044 base_breakpoint_ops.dtor (b);
8045}
8046
8047static int
8048insert_catch_solib (struct bp_location *ignore)
8049{
8050 return 0;
8051}
8052
8053static int
8054remove_catch_solib (struct bp_location *ignore)
8055{
8056 return 0;
8057}
8058
8059static int
8060breakpoint_hit_catch_solib (const struct bp_location *bl,
8061 struct address_space *aspace,
8062 CORE_ADDR bp_addr,
8063 const struct target_waitstatus *ws)
8064{
8065 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8066 struct breakpoint *other;
8067
8068 if (ws->kind == TARGET_WAITKIND_LOADED)
8069 return 1;
8070
8071 ALL_BREAKPOINTS (other)
8072 {
8073 struct bp_location *other_bl;
8074
8075 if (other == bl->owner)
8076 continue;
8077
8078 if (other->type != bp_shlib_event)
8079 continue;
8080
8081 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8082 continue;
8083
8084 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8085 {
8086 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8087 return 1;
8088 }
8089 }
8090
8091 return 0;
8092}
8093
8094static void
8095check_status_catch_solib (struct bpstats *bs)
8096{
8097 struct solib_catchpoint *self
8098 = (struct solib_catchpoint *) bs->breakpoint_at;
8099 int ix;
8100
8101 if (self->is_load)
8102 {
8103 struct so_list *iter;
8104
8105 for (ix = 0;
8106 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8107 ix, iter);
8108 ++ix)
8109 {
8110 if (!self->regex
8111 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8112 return;
8113 }
8114 }
8115 else
8116 {
8117 char *iter;
8118
8119 for (ix = 0;
8120 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8121 ix, iter);
8122 ++ix)
8123 {
8124 if (!self->regex
8125 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8126 return;
8127 }
8128 }
8129
8130 bs->stop = 0;
8131 bs->print_it = print_it_noop;
8132}
8133
8134static enum print_stop_action
8135print_it_catch_solib (bpstat bs)
8136{
8137 struct breakpoint *b = bs->breakpoint_at;
8138 struct ui_out *uiout = current_uiout;
8139
8140 annotate_catchpoint (b->number);
8141 if (b->disposition == disp_del)
8142 ui_out_text (uiout, "\nTemporary catchpoint ");
8143 else
8144 ui_out_text (uiout, "\nCatchpoint ");
8145 ui_out_field_int (uiout, "bkptno", b->number);
8146 ui_out_text (uiout, "\n");
8147 if (ui_out_is_mi_like_p (uiout))
8148 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8149 print_solib_event (1);
8150 return PRINT_SRC_AND_LOC;
8151}
8152
8153static void
8154print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8155{
8156 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8157 struct value_print_options opts;
8158 struct ui_out *uiout = current_uiout;
8159 char *msg;
8160
8161 get_user_print_options (&opts);
8162 /* Field 4, the address, is omitted (which makes the columns not
8163 line up too nicely with the headers, but the effect is relatively
8164 readable). */
8165 if (opts.addressprint)
8166 {
8167 annotate_field (4);
8168 ui_out_field_skip (uiout, "addr");
8169 }
8170
8171 annotate_field (5);
8172 if (self->is_load)
8173 {
8174 if (self->regex)
8175 msg = xstrprintf (_("load of library matching %s"), self->regex);
8176 else
8177 msg = xstrdup (_("load of library"));
8178 }
8179 else
8180 {
8181 if (self->regex)
8182 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8183 else
8184 msg = xstrdup (_("unload of library"));
8185 }
8186 ui_out_field_string (uiout, "what", msg);
8187 xfree (msg);
8ac3646f
TT
8188
8189 if (ui_out_is_mi_like_p (uiout))
8190 ui_out_field_string (uiout, "catch-type",
8191 self->is_load ? "load" : "unload");
edcc5120
TT
8192}
8193
8194static void
8195print_mention_catch_solib (struct breakpoint *b)
8196{
8197 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8198
8199 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8200 self->is_load ? "load" : "unload");
8201}
8202
8203static void
8204print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8205{
8206 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8207
8208 fprintf_unfiltered (fp, "%s %s",
8209 b->disposition == disp_del ? "tcatch" : "catch",
8210 self->is_load ? "load" : "unload");
8211 if (self->regex)
8212 fprintf_unfiltered (fp, " %s", self->regex);
8213 fprintf_unfiltered (fp, "\n");
8214}
8215
8216static struct breakpoint_ops catch_solib_breakpoint_ops;
8217
91985142
MG
8218/* Shared helper function (MI and CLI) for creating and installing
8219 a shared object event catchpoint. If IS_LOAD is non-zero then
8220 the events to be caught are load events, otherwise they are
8221 unload events. If IS_TEMP is non-zero the catchpoint is a
8222 temporary one. If ENABLED is non-zero the catchpoint is
8223 created in an enabled state. */
edcc5120 8224
91985142
MG
8225void
8226add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
edcc5120
TT
8227{
8228 struct solib_catchpoint *c;
8229 struct gdbarch *gdbarch = get_current_arch ();
edcc5120
TT
8230 struct cleanup *cleanup;
8231
edcc5120
TT
8232 if (!arg)
8233 arg = "";
8234 arg = skip_spaces (arg);
8235
8236 c = XCNEW (struct solib_catchpoint);
8237 cleanup = make_cleanup (xfree, c);
8238
8239 if (*arg != '\0')
8240 {
8241 int errcode;
8242
8243 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8244 if (errcode != 0)
8245 {
8246 char *err = get_regcomp_error (errcode, &c->compiled);
8247
8248 make_cleanup (xfree, err);
8249 error (_("Invalid regexp (%s): %s"), err, arg);
8250 }
8251 c->regex = xstrdup (arg);
8252 }
8253
8254 c->is_load = is_load;
91985142 8255 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
edcc5120
TT
8256 &catch_solib_breakpoint_ops);
8257
91985142
MG
8258 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8259
edcc5120
TT
8260 discard_cleanups (cleanup);
8261 install_breakpoint (0, &c->base, 1);
8262}
8263
91985142
MG
8264/* A helper function that does all the work for "catch load" and
8265 "catch unload". */
8266
8267static void
8268catch_load_or_unload (char *arg, int from_tty, int is_load,
8269 struct cmd_list_element *command)
8270{
8271 int tempflag;
8272 const int enabled = 1;
8273
8274 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8275
8276 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8277}
8278
edcc5120
TT
8279static void
8280catch_load_command_1 (char *arg, int from_tty,
8281 struct cmd_list_element *command)
8282{
8283 catch_load_or_unload (arg, from_tty, 1, command);
8284}
8285
8286static void
8287catch_unload_command_1 (char *arg, int from_tty,
8288 struct cmd_list_element *command)
8289{
8290 catch_load_or_unload (arg, from_tty, 0, command);
8291}
8292
be5c67c1
PA
8293/* An instance of this type is used to represent a syscall catchpoint.
8294 It includes a "struct breakpoint" as a kind of base class; users
8295 downcast to "struct breakpoint *" when needed. A breakpoint is
8296 really of this type iff its ops pointer points to
8297 CATCH_SYSCALL_BREAKPOINT_OPS. */
8298
8299struct syscall_catchpoint
8300{
8301 /* The base class. */
8302 struct breakpoint base;
8303
8304 /* Syscall numbers used for the 'catch syscall' feature. If no
8305 syscall has been specified for filtering, its value is NULL.
8306 Otherwise, it holds a list of all syscalls to be caught. The
8307 list elements are allocated with xmalloc. */
8308 VEC(int) *syscalls_to_be_caught;
8309};
8310
8311/* Implement the "dtor" breakpoint_ops method for syscall
8312 catchpoints. */
8313
8314static void
8315dtor_catch_syscall (struct breakpoint *b)
8316{
8317 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8318
8319 VEC_free (int, c->syscalls_to_be_caught);
348d480f 8320
2060206e 8321 base_breakpoint_ops.dtor (b);
be5c67c1
PA
8322}
8323
fa3064dd
YQ
8324static const struct inferior_data *catch_syscall_inferior_data = NULL;
8325
8326struct catch_syscall_inferior_data
8327{
8328 /* We keep a count of the number of times the user has requested a
8329 particular syscall to be tracked, and pass this information to the
8330 target. This lets capable targets implement filtering directly. */
8331
8332 /* Number of times that "any" syscall is requested. */
8333 int any_syscall_count;
8334
8335 /* Count of each system call. */
8336 VEC(int) *syscalls_counts;
8337
8338 /* This counts all syscall catch requests, so we can readily determine
8339 if any catching is necessary. */
8340 int total_syscalls_count;
8341};
8342
8343static struct catch_syscall_inferior_data*
8344get_catch_syscall_inferior_data (struct inferior *inf)
8345{
8346 struct catch_syscall_inferior_data *inf_data;
8347
8348 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8349 if (inf_data == NULL)
8350 {
41bf6aca 8351 inf_data = XCNEW (struct catch_syscall_inferior_data);
fa3064dd
YQ
8352 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8353 }
8354
8355 return inf_data;
8356}
8357
8358static void
8359catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8360{
8361 xfree (arg);
8362}
8363
8364
a96d9b2e
SDJ
8365/* Implement the "insert" breakpoint_ops method for syscall
8366 catchpoints. */
8367
77b06cd7
TJB
8368static int
8369insert_catch_syscall (struct bp_location *bl)
a96d9b2e 8370{
be5c67c1 8371 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8372 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8373 struct catch_syscall_inferior_data *inf_data
8374 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8375
fa3064dd 8376 ++inf_data->total_syscalls_count;
be5c67c1 8377 if (!c->syscalls_to_be_caught)
fa3064dd 8378 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
8379 else
8380 {
8381 int i, iter;
cc59ec59 8382
a96d9b2e 8383 for (i = 0;
be5c67c1 8384 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8385 i++)
8386 {
8387 int elem;
cc59ec59 8388
fa3064dd 8389 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 8390 {
fa3064dd 8391 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
8392 uintptr_t vec_addr_offset
8393 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 8394 uintptr_t vec_addr;
fa3064dd
YQ
8395 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8396 vec_addr = ((uintptr_t) VEC_address (int,
8397 inf_data->syscalls_counts)
8398 + vec_addr_offset);
a96d9b2e
SDJ
8399 memset ((void *) vec_addr, 0,
8400 (iter + 1 - old_size) * sizeof (int));
8401 }
fa3064dd
YQ
8402 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8403 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
8404 }
8405 }
8406
dfd4cc63 8407 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8408 inf_data->total_syscalls_count != 0,
8409 inf_data->any_syscall_count,
8410 VEC_length (int,
8411 inf_data->syscalls_counts),
8412 VEC_address (int,
8413 inf_data->syscalls_counts));
a96d9b2e
SDJ
8414}
8415
8416/* Implement the "remove" breakpoint_ops method for syscall
8417 catchpoints. */
8418
8419static int
77b06cd7 8420remove_catch_syscall (struct bp_location *bl)
a96d9b2e 8421{
be5c67c1 8422 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 8423 struct inferior *inf = current_inferior ();
fa3064dd
YQ
8424 struct catch_syscall_inferior_data *inf_data
8425 = get_catch_syscall_inferior_data (inf);
a96d9b2e 8426
fa3064dd 8427 --inf_data->total_syscalls_count;
be5c67c1 8428 if (!c->syscalls_to_be_caught)
fa3064dd 8429 --inf_data->any_syscall_count;
a96d9b2e
SDJ
8430 else
8431 {
8432 int i, iter;
cc59ec59 8433
a96d9b2e 8434 for (i = 0;
be5c67c1 8435 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8436 i++)
8437 {
8438 int elem;
fa3064dd 8439 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
8440 /* Shouldn't happen. */
8441 continue;
fa3064dd
YQ
8442 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8443 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
8444 }
8445 }
8446
dfd4cc63 8447 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
fa3064dd
YQ
8448 inf_data->total_syscalls_count != 0,
8449 inf_data->any_syscall_count,
8450 VEC_length (int,
8451 inf_data->syscalls_counts),
3e43a32a 8452 VEC_address (int,
fa3064dd 8453 inf_data->syscalls_counts));
a96d9b2e
SDJ
8454}
8455
8456/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8457 catchpoints. */
8458
8459static int
f1310107 8460breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
8461 struct address_space *aspace, CORE_ADDR bp_addr,
8462 const struct target_waitstatus *ws)
a96d9b2e 8463{
4a64f543
MS
8464 /* We must check if we are catching specific syscalls in this
8465 breakpoint. If we are, then we must guarantee that the called
8466 syscall is the same syscall we are catching. */
a96d9b2e 8467 int syscall_number = 0;
be5c67c1
PA
8468 const struct syscall_catchpoint *c
8469 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 8470
f90263c1
TT
8471 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8472 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
8473 return 0;
8474
f90263c1
TT
8475 syscall_number = ws->value.syscall_number;
8476
a96d9b2e 8477 /* Now, checking if the syscall is the same. */
be5c67c1 8478 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8479 {
8480 int i, iter;
cc59ec59 8481
a96d9b2e 8482 for (i = 0;
be5c67c1 8483 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8484 i++)
8485 if (syscall_number == iter)
4924df79
GKB
8486 return 1;
8487
8488 return 0;
a96d9b2e
SDJ
8489 }
8490
8491 return 1;
8492}
8493
8494/* Implement the "print_it" breakpoint_ops method for syscall
8495 catchpoints. */
8496
8497static enum print_stop_action
348d480f 8498print_it_catch_syscall (bpstat bs)
a96d9b2e 8499{
36dfb11c 8500 struct ui_out *uiout = current_uiout;
348d480f 8501 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
8502 /* These are needed because we want to know in which state a
8503 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8504 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8505 must print "called syscall" or "returned from syscall". */
8506 ptid_t ptid;
8507 struct target_waitstatus last;
8508 struct syscall s;
a96d9b2e
SDJ
8509
8510 get_last_target_status (&ptid, &last);
8511
8512 get_syscall_by_number (last.value.syscall_number, &s);
8513
8514 annotate_catchpoint (b->number);
8515
36dfb11c
TT
8516 if (b->disposition == disp_del)
8517 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 8518 else
36dfb11c
TT
8519 ui_out_text (uiout, "\nCatchpoint ");
8520 if (ui_out_is_mi_like_p (uiout))
8521 {
8522 ui_out_field_string (uiout, "reason",
8523 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8524 ? EXEC_ASYNC_SYSCALL_ENTRY
8525 : EXEC_ASYNC_SYSCALL_RETURN));
8526 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8527 }
8528 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
8529
8530 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
8531 ui_out_text (uiout, " (call to syscall ");
8532 else
8533 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 8534
36dfb11c
TT
8535 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8536 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8537 if (s.name != NULL)
8538 ui_out_field_string (uiout, "syscall-name", s.name);
8539
8540 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
8541
8542 return PRINT_SRC_AND_LOC;
8543}
8544
8545/* Implement the "print_one" breakpoint_ops method for syscall
8546 catchpoints. */
8547
8548static void
8549print_one_catch_syscall (struct breakpoint *b,
f1310107 8550 struct bp_location **last_loc)
a96d9b2e 8551{
be5c67c1 8552 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 8553 struct value_print_options opts;
79a45e25 8554 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
8555
8556 get_user_print_options (&opts);
4a64f543
MS
8557 /* Field 4, the address, is omitted (which makes the columns not
8558 line up too nicely with the headers, but the effect is relatively
8559 readable). */
a96d9b2e
SDJ
8560 if (opts.addressprint)
8561 ui_out_field_skip (uiout, "addr");
8562 annotate_field (5);
8563
be5c67c1
PA
8564 if (c->syscalls_to_be_caught
8565 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8566 ui_out_text (uiout, "syscalls \"");
8567 else
8568 ui_out_text (uiout, "syscall \"");
8569
be5c67c1 8570 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8571 {
8572 int i, iter;
8573 char *text = xstrprintf ("%s", "");
cc59ec59 8574
a96d9b2e 8575 for (i = 0;
be5c67c1 8576 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8577 i++)
8578 {
8579 char *x = text;
8580 struct syscall s;
8581 get_syscall_by_number (iter, &s);
8582
8583 if (s.name != NULL)
8584 text = xstrprintf ("%s%s, ", text, s.name);
8585 else
8586 text = xstrprintf ("%s%d, ", text, iter);
8587
8588 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 8589 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
8590 on every call. */
8591 xfree (x);
8592 }
8593 /* Remove the last comma. */
8594 text[strlen (text) - 2] = '\0';
8595 ui_out_field_string (uiout, "what", text);
8596 }
8597 else
8598 ui_out_field_string (uiout, "what", "<any syscall>");
8599 ui_out_text (uiout, "\" ");
8ac3646f
TT
8600
8601 if (ui_out_is_mi_like_p (uiout))
8602 ui_out_field_string (uiout, "catch-type", "syscall");
a96d9b2e
SDJ
8603}
8604
8605/* Implement the "print_mention" breakpoint_ops method for syscall
8606 catchpoints. */
8607
8608static void
8609print_mention_catch_syscall (struct breakpoint *b)
8610{
be5c67c1
PA
8611 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8612
8613 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
8614 {
8615 int i, iter;
8616
be5c67c1 8617 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
8618 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8619 else
8620 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8621
8622 for (i = 0;
be5c67c1 8623 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
8624 i++)
8625 {
8626 struct syscall s;
8627 get_syscall_by_number (iter, &s);
8628
8629 if (s.name)
8630 printf_filtered (" '%s' [%d]", s.name, s.number);
8631 else
8632 printf_filtered (" %d", s.number);
8633 }
8634 printf_filtered (")");
8635 }
8636 else
8637 printf_filtered (_("Catchpoint %d (any syscall)"),
8638 b->number);
8639}
8640
6149aea9
PA
8641/* Implement the "print_recreate" breakpoint_ops method for syscall
8642 catchpoints. */
8643
8644static void
8645print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8646{
be5c67c1
PA
8647 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8648
6149aea9
PA
8649 fprintf_unfiltered (fp, "catch syscall");
8650
be5c67c1 8651 if (c->syscalls_to_be_caught)
6149aea9
PA
8652 {
8653 int i, iter;
8654
8655 for (i = 0;
be5c67c1 8656 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8657 i++)
8658 {
8659 struct syscall s;
8660
8661 get_syscall_by_number (iter, &s);
8662 if (s.name)
8663 fprintf_unfiltered (fp, " %s", s.name);
8664 else
8665 fprintf_unfiltered (fp, " %d", s.number);
8666 }
8667 }
d9b3f62e 8668 print_recreate_thread (b, fp);
6149aea9
PA
8669}
8670
a96d9b2e
SDJ
8671/* The breakpoint_ops structure to be used in syscall catchpoints. */
8672
2060206e 8673static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8674
8675/* Returns non-zero if 'b' is a syscall catchpoint. */
8676
8677static int
8678syscall_catchpoint_p (struct breakpoint *b)
8679{
8680 return (b->ops == &catch_syscall_breakpoint_ops);
8681}
8682
346774a9
PA
8683/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8684 is non-zero, then make the breakpoint temporary. If COND_STRING is
8685 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8686 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8687
ab04a2af 8688void
346774a9
PA
8689init_catchpoint (struct breakpoint *b,
8690 struct gdbarch *gdbarch, int tempflag,
8691 char *cond_string,
c0a91b2b 8692 const struct breakpoint_ops *ops)
c906108c 8693{
c5aa993b 8694 struct symtab_and_line sal;
346774a9 8695
fe39c653 8696 init_sal (&sal);
6c95b8df 8697 sal.pspace = current_program_space;
c5aa993b 8698
28010a5d 8699 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8700
1b36a34b 8701 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8702 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8703}
8704
28010a5d 8705void
3ea46bff 8706install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8707{
8708 add_to_breakpoint_chain (b);
3a5c3e22 8709 set_breakpoint_number (internal, b);
558a9d82
YQ
8710 if (is_tracepoint (b))
8711 set_tracepoint_count (breakpoint_count);
3a5c3e22
PA
8712 if (!internal)
8713 mention (b);
c56053d2 8714 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8715
8716 if (update_gll)
8717 update_global_location_list (1);
c56053d2
PA
8718}
8719
9b70b993 8720static void
a6d9a66e
UW
8721create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8722 int tempflag, char *cond_string,
c0a91b2b 8723 const struct breakpoint_ops *ops)
c906108c 8724{
e29a4733 8725 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8726
e29a4733
PA
8727 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8728
8729 c->forked_inferior_pid = null_ptid;
8730
3ea46bff 8731 install_breakpoint (0, &c->base, 1);
c906108c
SS
8732}
8733
fe798b75
JB
8734/* Exec catchpoints. */
8735
b4d90040
PA
8736/* An instance of this type is used to represent an exec catchpoint.
8737 It includes a "struct breakpoint" as a kind of base class; users
8738 downcast to "struct breakpoint *" when needed. A breakpoint is
8739 really of this type iff its ops pointer points to
8740 CATCH_EXEC_BREAKPOINT_OPS. */
8741
8742struct exec_catchpoint
8743{
8744 /* The base class. */
8745 struct breakpoint base;
8746
8747 /* Filename of a program whose exec triggered this catchpoint.
8748 This field is only valid immediately after this catchpoint has
8749 triggered. */
8750 char *exec_pathname;
8751};
8752
8753/* Implement the "dtor" breakpoint_ops method for exec
8754 catchpoints. */
8755
8756static void
8757dtor_catch_exec (struct breakpoint *b)
8758{
8759 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8760
8761 xfree (c->exec_pathname);
348d480f 8762
2060206e 8763 base_breakpoint_ops.dtor (b);
b4d90040
PA
8764}
8765
77b06cd7
TJB
8766static int
8767insert_catch_exec (struct bp_location *bl)
c906108c 8768{
dfd4cc63 8769 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8770}
c906108c 8771
fe798b75 8772static int
77b06cd7 8773remove_catch_exec (struct bp_location *bl)
fe798b75 8774{
dfd4cc63 8775 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
fe798b75 8776}
c906108c 8777
fe798b75 8778static int
f1310107 8779breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8780 struct address_space *aspace, CORE_ADDR bp_addr,
8781 const struct target_waitstatus *ws)
fe798b75 8782{
b4d90040
PA
8783 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8784
f90263c1
TT
8785 if (ws->kind != TARGET_WAITKIND_EXECD)
8786 return 0;
8787
8788 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8789 return 1;
fe798b75 8790}
c906108c 8791
fe798b75 8792static enum print_stop_action
348d480f 8793print_it_catch_exec (bpstat bs)
fe798b75 8794{
36dfb11c 8795 struct ui_out *uiout = current_uiout;
348d480f 8796 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8797 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8798
fe798b75 8799 annotate_catchpoint (b->number);
36dfb11c
TT
8800 if (b->disposition == disp_del)
8801 ui_out_text (uiout, "\nTemporary catchpoint ");
8802 else
8803 ui_out_text (uiout, "\nCatchpoint ");
8804 if (ui_out_is_mi_like_p (uiout))
8805 {
8806 ui_out_field_string (uiout, "reason",
8807 async_reason_lookup (EXEC_ASYNC_EXEC));
8808 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8809 }
8810 ui_out_field_int (uiout, "bkptno", b->number);
8811 ui_out_text (uiout, " (exec'd ");
8812 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8813 ui_out_text (uiout, "), ");
8814
fe798b75 8815 return PRINT_SRC_AND_LOC;
c906108c
SS
8816}
8817
fe798b75 8818static void
a6d9a66e 8819print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8820{
b4d90040 8821 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8822 struct value_print_options opts;
79a45e25 8823 struct ui_out *uiout = current_uiout;
fe798b75
JB
8824
8825 get_user_print_options (&opts);
8826
8827 /* Field 4, the address, is omitted (which makes the columns
8828 not line up too nicely with the headers, but the effect
8829 is relatively readable). */
8830 if (opts.addressprint)
8831 ui_out_field_skip (uiout, "addr");
8832 annotate_field (5);
8833 ui_out_text (uiout, "exec");
b4d90040 8834 if (c->exec_pathname != NULL)
fe798b75
JB
8835 {
8836 ui_out_text (uiout, ", program \"");
b4d90040 8837 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8838 ui_out_text (uiout, "\" ");
8839 }
8ac3646f
TT
8840
8841 if (ui_out_is_mi_like_p (uiout))
8842 ui_out_field_string (uiout, "catch-type", "exec");
fe798b75
JB
8843}
8844
8845static void
8846print_mention_catch_exec (struct breakpoint *b)
8847{
8848 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8849}
8850
6149aea9
PA
8851/* Implement the "print_recreate" breakpoint_ops method for exec
8852 catchpoints. */
8853
8854static void
8855print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8856{
8857 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8858 print_recreate_thread (b, fp);
6149aea9
PA
8859}
8860
2060206e 8861static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8862
a96d9b2e
SDJ
8863static void
8864create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 8865 const struct breakpoint_ops *ops)
a96d9b2e 8866{
be5c67c1 8867 struct syscall_catchpoint *c;
a96d9b2e 8868 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 8869
be5c67c1
PA
8870 c = XNEW (struct syscall_catchpoint);
8871 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8872 c->syscalls_to_be_caught = filter;
a96d9b2e 8873
3ea46bff 8874 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
8875}
8876
c906108c 8877static int
fba45db2 8878hw_breakpoint_used_count (void)
c906108c 8879{
c906108c 8880 int i = 0;
f1310107
TJB
8881 struct breakpoint *b;
8882 struct bp_location *bl;
c906108c
SS
8883
8884 ALL_BREAKPOINTS (b)
c5aa993b 8885 {
d6b74ac4 8886 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8887 for (bl = b->loc; bl; bl = bl->next)
8888 {
8889 /* Special types of hardware breakpoints may use more than
8890 one register. */
348d480f 8891 i += b->ops->resources_needed (bl);
f1310107 8892 }
c5aa993b 8893 }
c906108c
SS
8894
8895 return i;
8896}
8897
a1398e0c
PA
8898/* Returns the resources B would use if it were a hardware
8899 watchpoint. */
8900
c906108c 8901static int
a1398e0c 8902hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8903{
c906108c 8904 int i = 0;
e09342b5 8905 struct bp_location *bl;
c906108c 8906
a1398e0c
PA
8907 if (!breakpoint_enabled (b))
8908 return 0;
8909
8910 for (bl = b->loc; bl; bl = bl->next)
8911 {
8912 /* Special types of hardware watchpoints may use more than
8913 one register. */
8914 i += b->ops->resources_needed (bl);
8915 }
8916
8917 return i;
8918}
8919
8920/* Returns the sum the used resources of all hardware watchpoints of
8921 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8922 the sum of the used resources of all hardware watchpoints of other
8923 types _not_ TYPE. */
8924
8925static int
8926hw_watchpoint_used_count_others (struct breakpoint *except,
8927 enum bptype type, int *other_type_used)
8928{
8929 int i = 0;
8930 struct breakpoint *b;
8931
c906108c
SS
8932 *other_type_used = 0;
8933 ALL_BREAKPOINTS (b)
e09342b5 8934 {
a1398e0c
PA
8935 if (b == except)
8936 continue;
e09342b5
TJB
8937 if (!breakpoint_enabled (b))
8938 continue;
8939
a1398e0c
PA
8940 if (b->type == type)
8941 i += hw_watchpoint_use_count (b);
8942 else if (is_hardware_watchpoint (b))
8943 *other_type_used = 1;
e09342b5
TJB
8944 }
8945
c906108c
SS
8946 return i;
8947}
8948
c906108c 8949void
fba45db2 8950disable_watchpoints_before_interactive_call_start (void)
c906108c 8951{
c5aa993b 8952 struct breakpoint *b;
c906108c
SS
8953
8954 ALL_BREAKPOINTS (b)
c5aa993b 8955 {
cc60f2e3 8956 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8957 {
b5de0fa7 8958 b->enable_state = bp_call_disabled;
b60e7edf 8959 update_global_location_list (0);
c5aa993b
JM
8960 }
8961 }
c906108c
SS
8962}
8963
8964void
fba45db2 8965enable_watchpoints_after_interactive_call_stop (void)
c906108c 8966{
c5aa993b 8967 struct breakpoint *b;
c906108c
SS
8968
8969 ALL_BREAKPOINTS (b)
c5aa993b 8970 {
cc60f2e3 8971 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8972 {
b5de0fa7 8973 b->enable_state = bp_enabled;
b60e7edf 8974 update_global_location_list (1);
c5aa993b
JM
8975 }
8976 }
c906108c
SS
8977}
8978
8bea4e01
UW
8979void
8980disable_breakpoints_before_startup (void)
8981{
6c95b8df 8982 current_program_space->executing_startup = 1;
f8eba3c6 8983 update_global_location_list (0);
8bea4e01
UW
8984}
8985
8986void
8987enable_breakpoints_after_startup (void)
8988{
6c95b8df 8989 current_program_space->executing_startup = 0;
f8eba3c6 8990 breakpoint_re_set ();
8bea4e01
UW
8991}
8992
c906108c
SS
8993
8994/* Set a breakpoint that will evaporate an end of command
8995 at address specified by SAL.
8996 Restrict it to frame FRAME if FRAME is nonzero. */
8997
8998struct breakpoint *
a6d9a66e
UW
8999set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9000 struct frame_id frame_id, enum bptype type)
c906108c 9001{
52f0bd74 9002 struct breakpoint *b;
edb3359d 9003
193facb3
JK
9004 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9005 tail-called one. */
9006 gdb_assert (!frame_id_artificial_p (frame_id));
edb3359d 9007
06edf0c0 9008 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
9009 b->enable_state = bp_enabled;
9010 b->disposition = disp_donttouch;
818dd999 9011 b->frame_id = frame_id;
c906108c 9012
4a64f543
MS
9013 /* If we're debugging a multi-threaded program, then we want
9014 momentary breakpoints to be active in only a single thread of
9015 control. */
39f77062
KB
9016 if (in_thread_list (inferior_ptid))
9017 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 9018
b60e7edf 9019 update_global_location_list_nothrow (1);
74960c60 9020
c906108c
SS
9021 return b;
9022}
611c83ae 9023
06edf0c0
PA
9024/* Make a momentary breakpoint based on the master breakpoint ORIG.
9025 The new breakpoint will have type TYPE, and use OPS as it
9026 breakpoint_ops. */
e58b0e63 9027
06edf0c0
PA
9028static struct breakpoint *
9029momentary_breakpoint_from_master (struct breakpoint *orig,
9030 enum bptype type,
c0a91b2b 9031 const struct breakpoint_ops *ops)
e58b0e63
PA
9032{
9033 struct breakpoint *copy;
9034
06edf0c0 9035 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 9036 copy->loc = allocate_bp_location (copy);
0e30163f 9037 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 9038
a6d9a66e 9039 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
9040 copy->loc->requested_address = orig->loc->requested_address;
9041 copy->loc->address = orig->loc->address;
9042 copy->loc->section = orig->loc->section;
6c95b8df 9043 copy->loc->pspace = orig->loc->pspace;
55aa24fb 9044 copy->loc->probe = orig->loc->probe;
f8eba3c6 9045 copy->loc->line_number = orig->loc->line_number;
2f202fde 9046 copy->loc->symtab = orig->loc->symtab;
e58b0e63
PA
9047 copy->frame_id = orig->frame_id;
9048 copy->thread = orig->thread;
6c95b8df 9049 copy->pspace = orig->pspace;
e58b0e63
PA
9050
9051 copy->enable_state = bp_enabled;
9052 copy->disposition = disp_donttouch;
9053 copy->number = internal_breakpoint_number--;
9054
9055 update_global_location_list_nothrow (0);
9056 return copy;
9057}
9058
06edf0c0
PA
9059/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9060 ORIG is NULL. */
9061
9062struct breakpoint *
9063clone_momentary_breakpoint (struct breakpoint *orig)
9064{
9065 /* If there's nothing to clone, then return nothing. */
9066 if (orig == NULL)
9067 return NULL;
9068
9069 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
9070}
9071
611c83ae 9072struct breakpoint *
a6d9a66e
UW
9073set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9074 enum bptype type)
611c83ae
PA
9075{
9076 struct symtab_and_line sal;
9077
9078 sal = find_pc_line (pc, 0);
9079 sal.pc = pc;
9080 sal.section = find_pc_overlay (pc);
9081 sal.explicit_pc = 1;
9082
a6d9a66e 9083 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 9084}
c906108c 9085\f
c5aa993b 9086
c906108c
SS
9087/* Tell the user we have just set a breakpoint B. */
9088
9089static void
fba45db2 9090mention (struct breakpoint *b)
c906108c 9091{
348d480f 9092 b->ops->print_mention (b);
79a45e25 9093 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 9094 return;
c906108c
SS
9095 printf_filtered ("\n");
9096}
c906108c 9097\f
c5aa993b 9098
0d381245 9099static struct bp_location *
39d61571 9100add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
9101 const struct symtab_and_line *sal)
9102{
9103 struct bp_location *loc, **tmp;
3742cc8b
YQ
9104 CORE_ADDR adjusted_address;
9105 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9106
9107 if (loc_gdbarch == NULL)
9108 loc_gdbarch = b->gdbarch;
9109
9110 /* Adjust the breakpoint's address prior to allocating a location.
9111 Once we call allocate_bp_location(), that mostly uninitialized
9112 location will be placed on the location chain. Adjustment of the
9113 breakpoint may cause target_read_memory() to be called and we do
9114 not want its scan of the location chain to find a breakpoint and
9115 location that's only been partially initialized. */
9116 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9117 sal->pc, b->type);
0d381245 9118
d30113d4 9119 /* Sort the locations by their ADDRESS. */
39d61571 9120 loc = allocate_bp_location (b);
d30113d4
JK
9121 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9122 tmp = &((*tmp)->next))
0d381245 9123 ;
d30113d4 9124 loc->next = *tmp;
0d381245 9125 *tmp = loc;
3742cc8b 9126
0d381245 9127 loc->requested_address = sal->pc;
3742cc8b 9128 loc->address = adjusted_address;
6c95b8df 9129 loc->pspace = sal->pspace;
729662a5
TT
9130 loc->probe.probe = sal->probe;
9131 loc->probe.objfile = sal->objfile;
6c95b8df 9132 gdb_assert (loc->pspace != NULL);
0d381245 9133 loc->section = sal->section;
3742cc8b 9134 loc->gdbarch = loc_gdbarch;
f8eba3c6 9135 loc->line_number = sal->line;
2f202fde 9136 loc->symtab = sal->symtab;
f8eba3c6 9137
0e30163f
JK
9138 set_breakpoint_location_function (loc,
9139 sal->explicit_pc || sal->explicit_line);
0d381245
VP
9140 return loc;
9141}
514f746b
AR
9142\f
9143
9144/* Return 1 if LOC is pointing to a permanent breakpoint,
9145 return 0 otherwise. */
9146
9147static int
9148bp_loc_is_permanent (struct bp_location *loc)
9149{
9150 int len;
9151 CORE_ADDR addr;
1afeeb75 9152 const gdb_byte *bpoint;
514f746b 9153 gdb_byte *target_mem;
939c61fa
JK
9154 struct cleanup *cleanup;
9155 int retval = 0;
514f746b
AR
9156
9157 gdb_assert (loc != NULL);
9158
9159 addr = loc->address;
1afeeb75 9160 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 9161
939c61fa 9162 /* Software breakpoints unsupported? */
1afeeb75 9163 if (bpoint == NULL)
939c61fa
JK
9164 return 0;
9165
514f746b
AR
9166 target_mem = alloca (len);
9167
939c61fa
JK
9168 /* Enable the automatic memory restoration from breakpoints while
9169 we read the memory. Otherwise we could say about our temporary
9170 breakpoints they are permanent. */
6c95b8df
PA
9171 cleanup = save_current_space_and_thread ();
9172
9173 switch_to_program_space_and_thread (loc->pspace);
9174 make_show_memory_breakpoints_cleanup (0);
939c61fa 9175
514f746b 9176 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 9177 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 9178 retval = 1;
514f746b 9179
939c61fa
JK
9180 do_cleanups (cleanup);
9181
9182 return retval;
514f746b
AR
9183}
9184
e7e0cddf
SS
9185/* Build a command list for the dprintf corresponding to the current
9186 settings of the dprintf style options. */
9187
9188static void
9189update_dprintf_command_list (struct breakpoint *b)
9190{
9191 char *dprintf_args = b->extra_string;
9192 char *printf_line = NULL;
9193
9194 if (!dprintf_args)
9195 return;
9196
9197 dprintf_args = skip_spaces (dprintf_args);
9198
9199 /* Allow a comma, as it may have terminated a location, but don't
9200 insist on it. */
9201 if (*dprintf_args == ',')
9202 ++dprintf_args;
9203 dprintf_args = skip_spaces (dprintf_args);
9204
9205 if (*dprintf_args != '"')
9206 error (_("Bad format string, missing '\"'."));
9207
d3ce09f5 9208 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
e7e0cddf 9209 printf_line = xstrprintf ("printf %s", dprintf_args);
d3ce09f5 9210 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
e7e0cddf
SS
9211 {
9212 if (!dprintf_function)
9213 error (_("No function supplied for dprintf call"));
9214
9215 if (dprintf_channel && strlen (dprintf_channel) > 0)
9216 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9217 dprintf_function,
9218 dprintf_channel,
9219 dprintf_args);
9220 else
9221 printf_line = xstrprintf ("call (void) %s (%s)",
9222 dprintf_function,
9223 dprintf_args);
9224 }
d3ce09f5
SS
9225 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9226 {
9227 if (target_can_run_breakpoint_commands ())
9228 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9229 else
9230 {
9231 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9232 printf_line = xstrprintf ("printf %s", dprintf_args);
9233 }
9234 }
e7e0cddf
SS
9235 else
9236 internal_error (__FILE__, __LINE__,
9237 _("Invalid dprintf style."));
9238
f28045c2 9239 gdb_assert (printf_line != NULL);
9d6e6e84 9240 /* Manufacture a printf sequence. */
f28045c2 9241 {
9d6e6e84
HZ
9242 struct command_line *printf_cmd_line
9243 = xmalloc (sizeof (struct command_line));
e7e0cddf 9244
f28045c2
YQ
9245 printf_cmd_line = xmalloc (sizeof (struct command_line));
9246 printf_cmd_line->control_type = simple_control;
9247 printf_cmd_line->body_count = 0;
9248 printf_cmd_line->body_list = NULL;
9d6e6e84 9249 printf_cmd_line->next = NULL;
f28045c2 9250 printf_cmd_line->line = printf_line;
e7e0cddf 9251
f28045c2
YQ
9252 breakpoint_set_commands (b, printf_cmd_line);
9253 }
e7e0cddf
SS
9254}
9255
9256/* Update all dprintf commands, making their command lists reflect
9257 current style settings. */
9258
9259static void
9260update_dprintf_commands (char *args, int from_tty,
9261 struct cmd_list_element *c)
9262{
9263 struct breakpoint *b;
9264
9265 ALL_BREAKPOINTS (b)
9266 {
9267 if (b->type == bp_dprintf)
9268 update_dprintf_command_list (b);
9269 }
9270}
c3f6f71d 9271
018d34a4
VP
9272/* Create a breakpoint with SAL as location. Use ADDR_STRING
9273 as textual description of the location, and COND_STRING
db107f19 9274 as condition expression. */
018d34a4
VP
9275
9276static void
d9b3f62e
PA
9277init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9278 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9279 char *filter, char *cond_string,
e7e0cddf 9280 char *extra_string,
d9b3f62e
PA
9281 enum bptype type, enum bpdisp disposition,
9282 int thread, int task, int ignore_count,
c0a91b2b 9283 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9284 int enabled, int internal, unsigned flags,
9285 int display_canonical)
018d34a4 9286{
0d381245 9287 int i;
018d34a4
VP
9288
9289 if (type == bp_hardware_breakpoint)
9290 {
fbbd034e
AS
9291 int target_resources_ok;
9292
9293 i = hw_breakpoint_used_count ();
9294 target_resources_ok =
9295 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
9296 i + 1, 0);
9297 if (target_resources_ok == 0)
9298 error (_("No hardware breakpoint support in the target."));
9299 else if (target_resources_ok < 0)
9300 error (_("Hardware breakpoints used exceeds limit."));
9301 }
9302
6c95b8df
PA
9303 gdb_assert (sals.nelts > 0);
9304
0d381245
VP
9305 for (i = 0; i < sals.nelts; ++i)
9306 {
9307 struct symtab_and_line sal = sals.sals[i];
9308 struct bp_location *loc;
9309
9310 if (from_tty)
5af949e3
UW
9311 {
9312 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9313 if (!loc_gdbarch)
9314 loc_gdbarch = gdbarch;
9315
9316 describe_other_breakpoints (loc_gdbarch,
6c95b8df 9317 sal.pspace, sal.pc, sal.section, thread);
5af949e3 9318 }
0d381245
VP
9319
9320 if (i == 0)
9321 {
d9b3f62e 9322 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 9323 b->thread = thread;
4a306c9a 9324 b->task = task;
855a6e68 9325
0d381245 9326 b->cond_string = cond_string;
e7e0cddf 9327 b->extra_string = extra_string;
0d381245 9328 b->ignore_count = ignore_count;
41447f92 9329 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 9330 b->disposition = disposition;
6c95b8df 9331
44f238bb
PA
9332 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9333 b->loc->inserted = 1;
9334
0fb4aa4b
PA
9335 if (type == bp_static_tracepoint)
9336 {
d9b3f62e 9337 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
9338 struct static_tracepoint_marker marker;
9339
983af33b 9340 if (strace_marker_p (b))
0fb4aa4b
PA
9341 {
9342 /* We already know the marker exists, otherwise, we
9343 wouldn't see a sal for it. */
9344 char *p = &addr_string[3];
9345 char *endp;
9346 char *marker_str;
0fb4aa4b 9347
e9cafbcc 9348 p = skip_spaces (p);
0fb4aa4b 9349
e9cafbcc 9350 endp = skip_to_space (p);
0fb4aa4b
PA
9351
9352 marker_str = savestring (p, endp - p);
d9b3f62e 9353 t->static_trace_marker_id = marker_str;
0fb4aa4b 9354
3e43a32a
MS
9355 printf_filtered (_("Probed static tracepoint "
9356 "marker \"%s\"\n"),
d9b3f62e 9357 t->static_trace_marker_id);
0fb4aa4b
PA
9358 }
9359 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9360 {
d9b3f62e 9361 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
9362 release_static_tracepoint_marker (&marker);
9363
3e43a32a
MS
9364 printf_filtered (_("Probed static tracepoint "
9365 "marker \"%s\"\n"),
d9b3f62e 9366 t->static_trace_marker_id);
0fb4aa4b
PA
9367 }
9368 else
3e43a32a
MS
9369 warning (_("Couldn't determine the static "
9370 "tracepoint marker to probe"));
0fb4aa4b
PA
9371 }
9372
0d381245
VP
9373 loc = b->loc;
9374 }
9375 else
018d34a4 9376 {
39d61571 9377 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
9378 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9379 loc->inserted = 1;
0d381245
VP
9380 }
9381
514f746b
AR
9382 if (bp_loc_is_permanent (loc))
9383 make_breakpoint_permanent (b);
9384
0d381245
VP
9385 if (b->cond_string)
9386 {
bbc13ae3
KS
9387 const char *arg = b->cond_string;
9388
1bb9788d
TT
9389 loc->cond = parse_exp_1 (&arg, loc->address,
9390 block_for_pc (loc->address), 0);
0d381245 9391 if (*arg)
588ae58c 9392 error (_("Garbage '%s' follows condition"), arg);
018d34a4 9393 }
e7e0cddf
SS
9394
9395 /* Dynamic printf requires and uses additional arguments on the
9396 command line, otherwise it's an error. */
9397 if (type == bp_dprintf)
9398 {
9399 if (b->extra_string)
9400 update_dprintf_command_list (b);
9401 else
9402 error (_("Format string required"));
9403 }
9404 else if (b->extra_string)
588ae58c 9405 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 9406 }
018d34a4 9407
56435ebe 9408 b->display_canonical = display_canonical;
018d34a4
VP
9409 if (addr_string)
9410 b->addr_string = addr_string;
9411 else
9412 /* addr_string has to be used or breakpoint_re_set will delete
9413 me. */
5af949e3
UW
9414 b->addr_string
9415 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 9416 b->filter = filter;
d9b3f62e 9417}
018d34a4 9418
d9b3f62e
PA
9419static void
9420create_breakpoint_sal (struct gdbarch *gdbarch,
9421 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 9422 char *filter, char *cond_string,
e7e0cddf 9423 char *extra_string,
d9b3f62e
PA
9424 enum bptype type, enum bpdisp disposition,
9425 int thread, int task, int ignore_count,
c0a91b2b 9426 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
9427 int enabled, int internal, unsigned flags,
9428 int display_canonical)
d9b3f62e
PA
9429{
9430 struct breakpoint *b;
9431 struct cleanup *old_chain;
9432
9433 if (is_tracepoint_type (type))
9434 {
9435 struct tracepoint *t;
9436
9437 t = XCNEW (struct tracepoint);
9438 b = &t->base;
9439 }
9440 else
9441 b = XNEW (struct breakpoint);
9442
9443 old_chain = make_cleanup (xfree, b);
9444
9445 init_breakpoint_sal (b, gdbarch,
9446 sals, addr_string,
e7e0cddf 9447 filter, cond_string, extra_string,
d9b3f62e
PA
9448 type, disposition,
9449 thread, task, ignore_count,
9450 ops, from_tty,
44f238bb
PA
9451 enabled, internal, flags,
9452 display_canonical);
d9b3f62e
PA
9453 discard_cleanups (old_chain);
9454
3ea46bff 9455 install_breakpoint (internal, b, 0);
018d34a4
VP
9456}
9457
9458/* Add SALS.nelts breakpoints to the breakpoint table. For each
9459 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9460 value. COND_STRING, if not NULL, specified the condition to be
9461 used for all breakpoints. Essentially the only case where
9462 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9463 function. In that case, it's still not possible to specify
9464 separate conditions for different overloaded functions, so
9465 we take just a single condition string.
9466
c3f6f71d 9467 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 9468 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
9469 array contents). If the function fails (error() is called), the
9470 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 9471 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
9472
9473static void
8cdf0e15 9474create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 9475 struct linespec_result *canonical,
e7e0cddf 9476 char *cond_string, char *extra_string,
8cdf0e15
VP
9477 enum bptype type, enum bpdisp disposition,
9478 int thread, int task, int ignore_count,
c0a91b2b 9479 const struct breakpoint_ops *ops, int from_tty,
44f238bb 9480 int enabled, int internal, unsigned flags)
c906108c 9481{
018d34a4 9482 int i;
f8eba3c6 9483 struct linespec_sals *lsal;
cc59ec59 9484
f8eba3c6
TT
9485 if (canonical->pre_expanded)
9486 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9487
9488 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 9489 {
f8eba3c6
TT
9490 /* Note that 'addr_string' can be NULL in the case of a plain
9491 'break', without arguments. */
9492 char *addr_string = (canonical->addr_string
9493 ? xstrdup (canonical->addr_string)
9494 : NULL);
9495 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9496 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 9497
f8eba3c6
TT
9498 make_cleanup (xfree, filter_string);
9499 create_breakpoint_sal (gdbarch, lsal->sals,
9500 addr_string,
9501 filter_string,
e7e0cddf
SS
9502 cond_string, extra_string,
9503 type, disposition,
84f4c1fe 9504 thread, task, ignore_count, ops,
44f238bb 9505 from_tty, enabled, internal, flags,
56435ebe 9506 canonical->special_display);
f8eba3c6 9507 discard_cleanups (inner);
c3f6f71d 9508 }
c3f6f71d 9509}
c906108c 9510
9998af43 9511/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 9512 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 9513 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
9514 address strings. ADDRESS points to the end of the SAL.
9515
9516 The array and the line spec strings are allocated on the heap, it is
9517 the caller's responsibility to free them. */
c906108c 9518
b9362cc7 9519static void
c3f6f71d 9520parse_breakpoint_sals (char **address,
58438ac1 9521 struct linespec_result *canonical)
c3f6f71d 9522{
c3f6f71d 9523 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 9524 breakpoint. */
c3f6f71d
JM
9525 if ((*address) == NULL
9526 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 9527 {
1bfeeb0f
JL
9528 /* The last displayed codepoint, if it's valid, is our default breakpoint
9529 address. */
9530 if (last_displayed_sal_is_valid ())
c906108c 9531 {
f8eba3c6 9532 struct linespec_sals lsal;
c3f6f71d 9533 struct symtab_and_line sal;
1c8cdcb1 9534 CORE_ADDR pc;
cc59ec59 9535
4a64f543 9536 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 9537 lsal.sals.sals = (struct symtab_and_line *)
c906108c 9538 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
9539
9540 /* Set sal's pspace, pc, symtab, and line to the values
1c8cdcb1
JK
9541 corresponding to the last call to print_frame_info.
9542 Be sure to reinitialize LINE with NOTCURRENT == 0
9543 as the breakpoint line number is inappropriate otherwise.
9544 find_pc_line would adjust PC, re-set it back. */
1bfeeb0f 9545 get_last_displayed_sal (&sal);
1c8cdcb1
JK
9546 pc = sal.pc;
9547 sal = find_pc_line (pc, 0);
00903456 9548
4a64f543 9549 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
9550 where PC is the last displayed codepoint's address. So
9551 make sure to set sal.explicit_pc to prevent GDB from
9552 trying to expand the list of sals to include all other
9553 instances with the same symtab and line. */
1c8cdcb1 9554 sal.pc = pc;
00903456
JK
9555 sal.explicit_pc = 1;
9556
f8eba3c6
TT
9557 lsal.sals.sals[0] = sal;
9558 lsal.sals.nelts = 1;
9559 lsal.canonical = NULL;
9560
9561 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
9562 }
9563 else
8a3fe4f8 9564 error (_("No default breakpoint address now."));
c906108c
SS
9565 }
9566 else
9567 {
cc80f267
JK
9568 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9569
c906108c 9570 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
9571 current_source_symtab (which is decode_line_1's default).
9572 This should produce the results we want almost all of the
cc80f267
JK
9573 time while leaving default_breakpoint_* alone.
9574
9575 ObjC: However, don't match an Objective-C method name which
9576 may have a '+' or '-' succeeded by a '['. */
9577 if (last_displayed_sal_is_valid ()
9578 && (!cursal.symtab
9579 || ((strchr ("+-", (*address)[0]) != NULL)
9580 && ((*address)[1] != '['))))
f8eba3c6
TT
9581 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9582 get_last_displayed_symtab (),
9583 get_last_displayed_line (),
9584 canonical, NULL, NULL);
c906108c 9585 else
f8eba3c6 9586 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 9587 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 9588 }
c3f6f71d 9589}
c906108c 9590
c906108c 9591
c3f6f71d 9592/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 9593 inserted as a breakpoint. If it can't throw an error. */
c906108c 9594
b9362cc7 9595static void
23e7acfb 9596breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
9597{
9598 int i;
cc59ec59 9599
c3f6f71d 9600 for (i = 0; i < sals->nelts; i++)
ee53e872 9601 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
9602}
9603
7a697b8d
SS
9604/* Fast tracepoints may have restrictions on valid locations. For
9605 instance, a fast tracepoint using a jump instead of a trap will
9606 likely have to overwrite more bytes than a trap would, and so can
9607 only be placed where the instruction is longer than the jump, or a
9608 multi-instruction sequence does not have a jump into the middle of
9609 it, etc. */
9610
9611static void
9612check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9613 struct symtabs_and_lines *sals)
9614{
9615 int i, rslt;
9616 struct symtab_and_line *sal;
9617 char *msg;
9618 struct cleanup *old_chain;
9619
9620 for (i = 0; i < sals->nelts; i++)
9621 {
f8eba3c6
TT
9622 struct gdbarch *sarch;
9623
7a697b8d
SS
9624 sal = &sals->sals[i];
9625
f8eba3c6
TT
9626 sarch = get_sal_arch (*sal);
9627 /* We fall back to GDBARCH if there is no architecture
9628 associated with SAL. */
9629 if (sarch == NULL)
9630 sarch = gdbarch;
9631 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9632 NULL, &msg);
9633 old_chain = make_cleanup (xfree, msg);
9634
9635 if (!rslt)
9636 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9637 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9638
9639 do_cleanups (old_chain);
9640 }
9641}
9642
af4908ba
KS
9643/* Issue an invalid thread ID error. */
9644
9645static void ATTRIBUTE_NORETURN
9646invalid_thread_id_error (int id)
9647{
9648 error (_("Unknown thread %d."), id);
9649}
9650
018d34a4
VP
9651/* Given TOK, a string specification of condition and thread, as
9652 accepted by the 'break' command, extract the condition
9653 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9654 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9655 If no condition is found, *COND_STRING is set to NULL.
9656 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9657
9658static void
bbc13ae3 9659find_condition_and_thread (const char *tok, CORE_ADDR pc,
e7e0cddf
SS
9660 char **cond_string, int *thread, int *task,
9661 char **rest)
018d34a4
VP
9662{
9663 *cond_string = NULL;
9664 *thread = -1;
ed1d1739
KS
9665 *task = 0;
9666 *rest = NULL;
9667
018d34a4
VP
9668 while (tok && *tok)
9669 {
bbc13ae3 9670 const char *end_tok;
018d34a4 9671 int toklen;
bbc13ae3
KS
9672 const char *cond_start = NULL;
9673 const char *cond_end = NULL;
cc59ec59 9674
bbc13ae3 9675 tok = skip_spaces_const (tok);
e7e0cddf
SS
9676
9677 if ((*tok == '"' || *tok == ',') && rest)
9678 {
9679 *rest = savestring (tok, strlen (tok));
9680 return;
9681 }
9682
bbc13ae3 9683 end_tok = skip_to_space_const (tok);
d634f2de 9684
018d34a4 9685 toklen = end_tok - tok;
d634f2de 9686
018d34a4
VP
9687 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9688 {
f7545552
TT
9689 struct expression *expr;
9690
018d34a4 9691 tok = cond_start = end_tok + 1;
1bb9788d 9692 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9693 xfree (expr);
018d34a4 9694 cond_end = tok;
d634f2de 9695 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9696 }
9697 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9698 {
9699 char *tmptok;
d634f2de 9700
018d34a4 9701 tok = end_tok + 1;
bbc13ae3 9702 *thread = strtol (tok, &tmptok, 0);
018d34a4
VP
9703 if (tok == tmptok)
9704 error (_("Junk after thread keyword."));
9705 if (!valid_thread_id (*thread))
af4908ba 9706 invalid_thread_id_error (*thread);
bbc13ae3 9707 tok = tmptok;
018d34a4 9708 }
4a306c9a
JB
9709 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9710 {
9711 char *tmptok;
9712
9713 tok = end_tok + 1;
bbc13ae3 9714 *task = strtol (tok, &tmptok, 0);
4a306c9a
JB
9715 if (tok == tmptok)
9716 error (_("Junk after task keyword."));
9717 if (!valid_task_id (*task))
b6199126 9718 error (_("Unknown task %d."), *task);
bbc13ae3 9719 tok = tmptok;
4a306c9a 9720 }
e7e0cddf
SS
9721 else if (rest)
9722 {
9723 *rest = savestring (tok, strlen (tok));
ccab2054 9724 return;
e7e0cddf 9725 }
018d34a4
VP
9726 else
9727 error (_("Junk at end of arguments."));
9728 }
9729}
9730
0fb4aa4b
PA
9731/* Decode a static tracepoint marker spec. */
9732
9733static struct symtabs_and_lines
9734decode_static_tracepoint_spec (char **arg_p)
9735{
9736 VEC(static_tracepoint_marker_p) *markers = NULL;
9737 struct symtabs_and_lines sals;
0fb4aa4b
PA
9738 struct cleanup *old_chain;
9739 char *p = &(*arg_p)[3];
9740 char *endp;
9741 char *marker_str;
9742 int i;
9743
e9cafbcc 9744 p = skip_spaces (p);
0fb4aa4b 9745
e9cafbcc 9746 endp = skip_to_space (p);
0fb4aa4b
PA
9747
9748 marker_str = savestring (p, endp - p);
9749 old_chain = make_cleanup (xfree, marker_str);
9750
9751 markers = target_static_tracepoint_markers_by_strid (marker_str);
9752 if (VEC_empty(static_tracepoint_marker_p, markers))
9753 error (_("No known static tracepoint marker named %s"), marker_str);
9754
9755 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9756 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9757
9758 for (i = 0; i < sals.nelts; i++)
9759 {
9760 struct static_tracepoint_marker *marker;
9761
9762 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9763
9764 init_sal (&sals.sals[i]);
9765
9766 sals.sals[i] = find_pc_line (marker->address, 0);
9767 sals.sals[i].pc = marker->address;
9768
9769 release_static_tracepoint_marker (marker);
9770 }
9771
9772 do_cleanups (old_chain);
9773
9774 *arg_p = endp;
9775 return sals;
9776}
9777
fd9b8c24
PA
9778/* Set a breakpoint. This function is shared between CLI and MI
9779 functions for setting a breakpoint. This function has two major
f6de8ec2
PA
9780 modes of operations, selected by the PARSE_ARG parameter. If
9781 non-zero, the function will parse ARG, extracting location,
9782 condition, thread and extra string. Otherwise, ARG is just the
9783 breakpoint's location, with condition, thread, and extra string
9784 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9785 If INTERNAL is non-zero, the breakpoint number will be allocated
9786 from the internal breakpoint count. Returns true if any breakpoint
9787 was created; false otherwise. */
0101ce28 9788
8cdf0e15
VP
9789int
9790create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9791 char *arg, char *cond_string,
9792 int thread, char *extra_string,
f6de8ec2 9793 int parse_arg,
0fb4aa4b 9794 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9795 int ignore_count,
9796 enum auto_boolean pending_break_support,
c0a91b2b 9797 const struct breakpoint_ops *ops,
44f238bb
PA
9798 int from_tty, int enabled, int internal,
9799 unsigned flags)
c3f6f71d 9800{
b78a6381 9801 volatile struct gdb_exception e;
f8eba3c6 9802 char *copy_arg = NULL;
c3f6f71d 9803 char *addr_start = arg;
7efd8fc2 9804 struct linespec_result canonical;
c3f6f71d 9805 struct cleanup *old_chain;
80c99de1 9806 struct cleanup *bkpt_chain = NULL;
0101ce28 9807 int pending = 0;
4a306c9a 9808 int task = 0;
86b17b60 9809 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9810
348d480f
PA
9811 gdb_assert (ops != NULL);
9812
7efd8fc2 9813 init_linespec_result (&canonical);
c3f6f71d 9814
b78a6381
TT
9815 TRY_CATCH (e, RETURN_MASK_ALL)
9816 {
983af33b
SDJ
9817 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9818 addr_start, &copy_arg);
b78a6381 9819 }
0101ce28
JJ
9820
9821 /* If caller is interested in rc value from parse, set value. */
05ff989b 9822 switch (e.reason)
0101ce28 9823 {
983af33b
SDJ
9824 case GDB_NO_ERROR:
9825 if (VEC_empty (linespec_sals, canonical.sals))
9826 return 0;
9827 break;
05ff989b
AC
9828 case RETURN_ERROR:
9829 switch (e.error)
0101ce28 9830 {
05ff989b 9831 case NOT_FOUND_ERROR:
0101ce28 9832
05ff989b
AC
9833 /* If pending breakpoint support is turned off, throw
9834 error. */
fa8d40ab
JJ
9835
9836 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9837 throw_exception (e);
9838
9839 exception_print (gdb_stderr, e);
fa8d40ab 9840
05ff989b
AC
9841 /* If pending breakpoint support is auto query and the user
9842 selects no, then simply return the error code. */
059fb39f 9843 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9844 && !nquery (_("Make %s pending on future shared library load? "),
9845 bptype_string (type_wanted)))
fd9b8c24 9846 return 0;
fa8d40ab 9847
05ff989b
AC
9848 /* At this point, either the user was queried about setting
9849 a pending breakpoint and selected yes, or pending
9850 breakpoint behavior is on and thus a pending breakpoint
9851 is defaulted on behalf of the user. */
f8eba3c6
TT
9852 {
9853 struct linespec_sals lsal;
9854
9855 copy_arg = xstrdup (addr_start);
9856 lsal.canonical = xstrdup (copy_arg);
9857 lsal.sals.nelts = 1;
9858 lsal.sals.sals = XNEW (struct symtab_and_line);
9859 init_sal (&lsal.sals.sals[0]);
9860 pending = 1;
9861 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9862 }
05ff989b
AC
9863 break;
9864 default:
98deb0da 9865 throw_exception (e);
0101ce28 9866 }
2abae994 9867 break;
05ff989b 9868 default:
983af33b 9869 throw_exception (e);
0101ce28 9870 }
c3f6f71d 9871
4a64f543 9872 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9873 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9874
c3f6f71d
JM
9875 /* ----------------------------- SNIP -----------------------------
9876 Anything added to the cleanup chain beyond this point is assumed
9877 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9878 then the memory is not reclaimed. */
9879 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9880
c3f6f71d
JM
9881 /* Resolve all line numbers to PC's and verify that the addresses
9882 are ok for the target. */
0101ce28 9883 if (!pending)
f8eba3c6
TT
9884 {
9885 int ix;
9886 struct linespec_sals *iter;
9887
9888 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9889 breakpoint_sals_to_pc (&iter->sals);
9890 }
c3f6f71d 9891
7a697b8d 9892 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9893 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9894 {
9895 int ix;
9896 struct linespec_sals *iter;
9897
9898 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9899 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9900 }
7a697b8d 9901
c3f6f71d
JM
9902 /* Verify that condition can be parsed, before setting any
9903 breakpoints. Allocate a separate condition expression for each
4a64f543 9904 breakpoint. */
0101ce28 9905 if (!pending)
c3f6f71d 9906 {
f6de8ec2 9907 if (parse_arg)
72b2ff0e 9908 {
0878d0fa 9909 char *rest;
52d361e1
YQ
9910 struct linespec_sals *lsal;
9911
9912 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9913
0878d0fa
YQ
9914 /* Here we only parse 'arg' to separate condition
9915 from thread number, so parsing in context of first
9916 sal is OK. When setting the breakpoint we'll
9917 re-parse it in context of each sal. */
9918
9919 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9920 &thread, &task, &rest);
9921 if (cond_string)
9922 make_cleanup (xfree, cond_string);
9923 if (rest)
9924 make_cleanup (xfree, rest);
9925 if (rest)
9926 extra_string = rest;
72b2ff0e 9927 }
2f069f6f 9928 else
72b2ff0e 9929 {
0878d0fa
YQ
9930 if (*arg != '\0')
9931 error (_("Garbage '%s' at end of location"), arg);
9932
9933 /* Create a private copy of condition string. */
9934 if (cond_string)
9935 {
9936 cond_string = xstrdup (cond_string);
9937 make_cleanup (xfree, cond_string);
9938 }
9939 /* Create a private copy of any extra string. */
9940 if (extra_string)
9941 {
9942 extra_string = xstrdup (extra_string);
9943 make_cleanup (xfree, extra_string);
9944 }
72b2ff0e 9945 }
0fb4aa4b 9946
52d361e1 9947 ops->create_breakpoints_sal (gdbarch, &canonical,
e7e0cddf 9948 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9949 tempflag ? disp_del : disp_donttouch,
9950 thread, task, ignore_count, ops,
44f238bb 9951 from_tty, enabled, internal, flags);
c906108c 9952 }
0101ce28
JJ
9953 else
9954 {
0101ce28
JJ
9955 struct breakpoint *b;
9956
0101ce28
JJ
9957 make_cleanup (xfree, copy_arg);
9958
bfccc43c
YQ
9959 if (is_tracepoint_type (type_wanted))
9960 {
9961 struct tracepoint *t;
9962
9963 t = XCNEW (struct tracepoint);
9964 b = &t->base;
9965 }
9966 else
9967 b = XNEW (struct breakpoint);
9968
9969 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9970
f8eba3c6 9971 b->addr_string = copy_arg;
f6de8ec2 9972 if (parse_arg)
e12c7713
MK
9973 b->cond_string = NULL;
9974 else
9975 {
9976 /* Create a private copy of condition string. */
9977 if (cond_string)
9978 {
9979 cond_string = xstrdup (cond_string);
9980 make_cleanup (xfree, cond_string);
9981 }
9982 b->cond_string = cond_string;
9983 }
e7e0cddf 9984 b->extra_string = NULL;
0101ce28 9985 b->ignore_count = ignore_count;
0101ce28 9986 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9987 b->condition_not_parsed = 1;
41447f92 9988 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9989 if ((type_wanted != bp_breakpoint
9990 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9991 b->pspace = current_program_space;
8bea4e01 9992
bfccc43c 9993 install_breakpoint (internal, b, 0);
0101ce28
JJ
9994 }
9995
f8eba3c6 9996 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9997 {
3e43a32a
MS
9998 warning (_("Multiple breakpoints were set.\nUse the "
9999 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 10000 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
10001 }
10002
80c99de1
PA
10003 /* That's it. Discard the cleanups for data inserted into the
10004 breakpoint. */
10005 discard_cleanups (bkpt_chain);
10006 /* But cleanup everything else. */
c3f6f71d 10007 do_cleanups (old_chain);
217dc9e2 10008
80c99de1 10009 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 10010 update_global_location_list (1);
fd9b8c24
PA
10011
10012 return 1;
c3f6f71d 10013}
c906108c 10014
348d480f 10015/* Set a breakpoint.
72b2ff0e
VP
10016 ARG is a string describing breakpoint address,
10017 condition, and thread.
10018 FLAG specifies if a breakpoint is hardware on,
10019 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10020 and BP_TEMPFLAG. */
348d480f 10021
98deb0da 10022static void
72b2ff0e 10023break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 10024{
72b2ff0e 10025 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
10026 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10027 ? bp_hardware_breakpoint
10028 : bp_breakpoint);
55aa24fb
SDJ
10029 struct breakpoint_ops *ops;
10030 const char *arg_cp = arg;
10031
10032 /* Matching breakpoints on probes. */
10033 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10034 ops = &bkpt_probe_breakpoint_ops;
10035 else
10036 ops = &bkpt_breakpoint_ops;
c3f6f71d 10037
8cdf0e15
VP
10038 create_breakpoint (get_current_arch (),
10039 arg,
e7e0cddf 10040 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 10041 tempflag, type_wanted,
8cdf0e15
VP
10042 0 /* Ignore count */,
10043 pending_break_support,
55aa24fb 10044 ops,
8cdf0e15 10045 from_tty,
84f4c1fe 10046 1 /* enabled */,
44f238bb
PA
10047 0 /* internal */,
10048 0);
c906108c
SS
10049}
10050
c906108c
SS
10051/* Helper function for break_command_1 and disassemble_command. */
10052
10053void
fba45db2 10054resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
10055{
10056 CORE_ADDR pc;
10057
10058 if (sal->pc == 0 && sal->symtab != NULL)
10059 {
10060 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 10061 error (_("No line %d in file \"%s\"."),
05cba821 10062 sal->line, symtab_to_filename_for_display (sal->symtab));
c906108c 10063 sal->pc = pc;
6a048695 10064
4a64f543
MS
10065 /* If this SAL corresponds to a breakpoint inserted using a line
10066 number, then skip the function prologue if necessary. */
6a048695 10067 if (sal->explicit_line)
059acae7 10068 skip_prologue_sal (sal);
c906108c
SS
10069 }
10070
10071 if (sal->section == 0 && sal->symtab != NULL)
10072 {
10073 struct blockvector *bv;
c5aa993b
JM
10074 struct block *b;
10075 struct symbol *sym;
c906108c 10076
801e3a5b 10077 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
10078 if (bv != NULL)
10079 {
7f0df278 10080 sym = block_linkage_function (b);
c906108c
SS
10081 if (sym != NULL)
10082 {
10083 fixup_symbol_section (sym, sal->symtab->objfile);
e27d198c 10084 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
c906108c
SS
10085 }
10086 else
10087 {
4a64f543
MS
10088 /* It really is worthwhile to have the section, so we'll
10089 just have to look harder. This case can be executed
10090 if we have line numbers but no functions (as can
10091 happen in assembly source). */
c906108c 10092
7cbd4a93 10093 struct bound_minimal_symbol msym;
6c95b8df
PA
10094 struct cleanup *old_chain = save_current_space_and_thread ();
10095
10096 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
10097
10098 msym = lookup_minimal_symbol_by_pc (sal->pc);
7cbd4a93 10099 if (msym.minsym)
efd66ac6 10100 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
6c95b8df
PA
10101
10102 do_cleanups (old_chain);
c906108c
SS
10103 }
10104 }
10105 }
10106}
10107
10108void
fba45db2 10109break_command (char *arg, int from_tty)
c906108c 10110{
db107f19 10111 break_command_1 (arg, 0, from_tty);
c906108c
SS
10112}
10113
c906108c 10114void
fba45db2 10115tbreak_command (char *arg, int from_tty)
c906108c 10116{
db107f19 10117 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
10118}
10119
c906108c 10120static void
fba45db2 10121hbreak_command (char *arg, int from_tty)
c906108c 10122{
db107f19 10123 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
10124}
10125
10126static void
fba45db2 10127thbreak_command (char *arg, int from_tty)
c906108c 10128{
db107f19 10129 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
10130}
10131
10132static void
fba45db2 10133stop_command (char *arg, int from_tty)
c906108c 10134{
a3f17187 10135 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 10136Usage: stop in <function | address>\n\
a3f17187 10137 stop at <line>\n"));
c906108c
SS
10138}
10139
10140static void
fba45db2 10141stopin_command (char *arg, int from_tty)
c906108c
SS
10142{
10143 int badInput = 0;
10144
c5aa993b 10145 if (arg == (char *) NULL)
c906108c
SS
10146 badInput = 1;
10147 else if (*arg != '*')
10148 {
10149 char *argptr = arg;
10150 int hasColon = 0;
10151
4a64f543 10152 /* Look for a ':'. If this is a line number specification, then
53a5351d 10153 say it is bad, otherwise, it should be an address or
4a64f543 10154 function/method name. */
c906108c 10155 while (*argptr && !hasColon)
c5aa993b
JM
10156 {
10157 hasColon = (*argptr == ':');
10158 argptr++;
10159 }
c906108c
SS
10160
10161 if (hasColon)
c5aa993b 10162 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 10163 else
c5aa993b 10164 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
10165 }
10166
10167 if (badInput)
a3f17187 10168 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 10169 else
db107f19 10170 break_command_1 (arg, 0, from_tty);
c906108c
SS
10171}
10172
10173static void
fba45db2 10174stopat_command (char *arg, int from_tty)
c906108c
SS
10175{
10176 int badInput = 0;
10177
c5aa993b 10178 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
10179 badInput = 1;
10180 else
10181 {
10182 char *argptr = arg;
10183 int hasColon = 0;
10184
4a64f543
MS
10185 /* Look for a ':'. If there is a '::' then get out, otherwise
10186 it is probably a line number. */
c906108c 10187 while (*argptr && !hasColon)
c5aa993b
JM
10188 {
10189 hasColon = (*argptr == ':');
10190 argptr++;
10191 }
c906108c
SS
10192
10193 if (hasColon)
c5aa993b 10194 badInput = (*argptr == ':'); /* we have class::method */
c906108c 10195 else
c5aa993b 10196 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
10197 }
10198
10199 if (badInput)
a3f17187 10200 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 10201 else
db107f19 10202 break_command_1 (arg, 0, from_tty);
c906108c
SS
10203}
10204
e7e0cddf
SS
10205/* The dynamic printf command is mostly like a regular breakpoint, but
10206 with a prewired command list consisting of a single output command,
10207 built from extra arguments supplied on the dprintf command
10208 line. */
10209
da821c7b 10210static void
e7e0cddf
SS
10211dprintf_command (char *arg, int from_tty)
10212{
10213 create_breakpoint (get_current_arch (),
10214 arg,
10215 NULL, 0, NULL, 1 /* parse arg */,
10216 0, bp_dprintf,
10217 0 /* Ignore count */,
10218 pending_break_support,
10219 &dprintf_breakpoint_ops,
10220 from_tty,
10221 1 /* enabled */,
10222 0 /* internal */,
10223 0);
10224}
10225
d3ce09f5
SS
10226static void
10227agent_printf_command (char *arg, int from_tty)
10228{
10229 error (_("May only run agent-printf on the target"));
10230}
10231
f1310107
TJB
10232/* Implement the "breakpoint_hit" breakpoint_ops method for
10233 ranged breakpoints. */
10234
10235static int
10236breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10237 struct address_space *aspace,
09ac7c10
TT
10238 CORE_ADDR bp_addr,
10239 const struct target_waitstatus *ws)
f1310107 10240{
09ac7c10 10241 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 10242 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
10243 return 0;
10244
f1310107
TJB
10245 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10246 bl->length, aspace, bp_addr);
10247}
10248
10249/* Implement the "resources_needed" breakpoint_ops method for
10250 ranged breakpoints. */
10251
10252static int
10253resources_needed_ranged_breakpoint (const struct bp_location *bl)
10254{
10255 return target_ranged_break_num_registers ();
10256}
10257
10258/* Implement the "print_it" breakpoint_ops method for
10259 ranged breakpoints. */
10260
10261static enum print_stop_action
348d480f 10262print_it_ranged_breakpoint (bpstat bs)
f1310107 10263{
348d480f 10264 struct breakpoint *b = bs->breakpoint_at;
f1310107 10265 struct bp_location *bl = b->loc;
79a45e25 10266 struct ui_out *uiout = current_uiout;
f1310107
TJB
10267
10268 gdb_assert (b->type == bp_hardware_breakpoint);
10269
10270 /* Ranged breakpoints have only one location. */
10271 gdb_assert (bl && bl->next == NULL);
10272
10273 annotate_breakpoint (b->number);
10274 if (b->disposition == disp_del)
10275 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10276 else
10277 ui_out_text (uiout, "\nRanged breakpoint ");
10278 if (ui_out_is_mi_like_p (uiout))
10279 {
10280 ui_out_field_string (uiout, "reason",
10281 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10282 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10283 }
10284 ui_out_field_int (uiout, "bkptno", b->number);
10285 ui_out_text (uiout, ", ");
10286
10287 return PRINT_SRC_AND_LOC;
10288}
10289
10290/* Implement the "print_one" breakpoint_ops method for
10291 ranged breakpoints. */
10292
10293static void
10294print_one_ranged_breakpoint (struct breakpoint *b,
10295 struct bp_location **last_loc)
10296{
10297 struct bp_location *bl = b->loc;
10298 struct value_print_options opts;
79a45e25 10299 struct ui_out *uiout = current_uiout;
f1310107
TJB
10300
10301 /* Ranged breakpoints have only one location. */
10302 gdb_assert (bl && bl->next == NULL);
10303
10304 get_user_print_options (&opts);
10305
10306 if (opts.addressprint)
10307 /* We don't print the address range here, it will be printed later
10308 by print_one_detail_ranged_breakpoint. */
10309 ui_out_field_skip (uiout, "addr");
10310 annotate_field (5);
10311 print_breakpoint_location (b, bl);
10312 *last_loc = bl;
10313}
10314
10315/* Implement the "print_one_detail" breakpoint_ops method for
10316 ranged breakpoints. */
10317
10318static void
10319print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10320 struct ui_out *uiout)
10321{
10322 CORE_ADDR address_start, address_end;
10323 struct bp_location *bl = b->loc;
f99d8bf4
PA
10324 struct ui_file *stb = mem_fileopen ();
10325 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
10326
10327 gdb_assert (bl);
10328
10329 address_start = bl->address;
10330 address_end = address_start + bl->length - 1;
10331
10332 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 10333 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
10334 print_core_address (bl->gdbarch, address_start),
10335 print_core_address (bl->gdbarch, address_end));
10336 ui_out_field_stream (uiout, "addr", stb);
10337 ui_out_text (uiout, "\n");
10338
10339 do_cleanups (cleanup);
10340}
10341
10342/* Implement the "print_mention" breakpoint_ops method for
10343 ranged breakpoints. */
10344
10345static void
10346print_mention_ranged_breakpoint (struct breakpoint *b)
10347{
10348 struct bp_location *bl = b->loc;
79a45e25 10349 struct ui_out *uiout = current_uiout;
f1310107
TJB
10350
10351 gdb_assert (bl);
10352 gdb_assert (b->type == bp_hardware_breakpoint);
10353
10354 if (ui_out_is_mi_like_p (uiout))
10355 return;
10356
10357 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10358 b->number, paddress (bl->gdbarch, bl->address),
10359 paddress (bl->gdbarch, bl->address + bl->length - 1));
10360}
10361
10362/* Implement the "print_recreate" breakpoint_ops method for
10363 ranged breakpoints. */
10364
10365static void
10366print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10367{
10368 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10369 b->addr_string_range_end);
d9b3f62e 10370 print_recreate_thread (b, fp);
f1310107
TJB
10371}
10372
10373/* The breakpoint_ops structure to be used in ranged breakpoints. */
10374
2060206e 10375static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
10376
10377/* Find the address where the end of the breakpoint range should be
10378 placed, given the SAL of the end of the range. This is so that if
10379 the user provides a line number, the end of the range is set to the
10380 last instruction of the given line. */
10381
10382static CORE_ADDR
10383find_breakpoint_range_end (struct symtab_and_line sal)
10384{
10385 CORE_ADDR end;
10386
10387 /* If the user provided a PC value, use it. Otherwise,
10388 find the address of the end of the given location. */
10389 if (sal.explicit_pc)
10390 end = sal.pc;
10391 else
10392 {
10393 int ret;
10394 CORE_ADDR start;
10395
10396 ret = find_line_pc_range (sal, &start, &end);
10397 if (!ret)
10398 error (_("Could not find location of the end of the range."));
10399
10400 /* find_line_pc_range returns the start of the next line. */
10401 end--;
10402 }
10403
10404 return end;
10405}
10406
10407/* Implement the "break-range" CLI command. */
10408
10409static void
10410break_range_command (char *arg, int from_tty)
10411{
10412 char *arg_start, *addr_string_start, *addr_string_end;
10413 struct linespec_result canonical_start, canonical_end;
10414 int bp_count, can_use_bp, length;
10415 CORE_ADDR end;
10416 struct breakpoint *b;
10417 struct symtab_and_line sal_start, sal_end;
f1310107 10418 struct cleanup *cleanup_bkpt;
f8eba3c6 10419 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
10420
10421 /* We don't support software ranged breakpoints. */
10422 if (target_ranged_break_num_registers () < 0)
10423 error (_("This target does not support hardware ranged breakpoints."));
10424
10425 bp_count = hw_breakpoint_used_count ();
10426 bp_count += target_ranged_break_num_registers ();
10427 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10428 bp_count, 0);
10429 if (can_use_bp < 0)
10430 error (_("Hardware breakpoints used exceeds limit."));
10431
f8eba3c6 10432 arg = skip_spaces (arg);
f1310107
TJB
10433 if (arg == NULL || arg[0] == '\0')
10434 error(_("No address range specified."));
10435
f1310107
TJB
10436 init_linespec_result (&canonical_start);
10437
f8eba3c6
TT
10438 arg_start = arg;
10439 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 10440
f8eba3c6 10441 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
10442
10443 if (arg[0] != ',')
10444 error (_("Too few arguments."));
f8eba3c6 10445 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 10446 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
10447
10448 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10449
10450 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10451 || lsal_start->sals.nelts != 1)
f1310107
TJB
10452 error (_("Cannot create a ranged breakpoint with multiple locations."));
10453
f8eba3c6
TT
10454 sal_start = lsal_start->sals.sals[0];
10455 addr_string_start = savestring (arg_start, arg - arg_start);
10456 make_cleanup (xfree, addr_string_start);
f1310107
TJB
10457
10458 arg++; /* Skip the comma. */
f8eba3c6 10459 arg = skip_spaces (arg);
f1310107
TJB
10460
10461 /* Parse the end location. */
10462
f1310107
TJB
10463 init_linespec_result (&canonical_end);
10464 arg_start = arg;
10465
f8eba3c6 10466 /* We call decode_line_full directly here instead of using
f1310107
TJB
10467 parse_breakpoint_sals because we need to specify the start location's
10468 symtab and line as the default symtab and line for the end of the
10469 range. This makes it possible to have ranges like "foo.c:27, +14",
10470 where +14 means 14 lines from the start location. */
f8eba3c6
TT
10471 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10472 sal_start.symtab, sal_start.line,
10473 &canonical_end, NULL, NULL);
10474
10475 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 10476
f8eba3c6 10477 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 10478 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
10479
10480 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10481 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10482 || lsal_end->sals.nelts != 1)
f1310107
TJB
10483 error (_("Cannot create a ranged breakpoint with multiple locations."));
10484
f8eba3c6
TT
10485 sal_end = lsal_end->sals.sals[0];
10486 addr_string_end = savestring (arg_start, arg - arg_start);
10487 make_cleanup (xfree, addr_string_end);
f1310107
TJB
10488
10489 end = find_breakpoint_range_end (sal_end);
10490 if (sal_start.pc > end)
177b42fe 10491 error (_("Invalid address range, end precedes start."));
f1310107
TJB
10492
10493 length = end - sal_start.pc + 1;
10494 if (length < 0)
10495 /* Length overflowed. */
10496 error (_("Address range too large."));
10497 else if (length == 1)
10498 {
10499 /* This range is simple enough to be handled by
10500 the `hbreak' command. */
10501 hbreak_command (addr_string_start, 1);
10502
10503 do_cleanups (cleanup_bkpt);
10504
10505 return;
10506 }
10507
10508 /* Now set up the breakpoint. */
10509 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 10510 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
10511 set_breakpoint_count (breakpoint_count + 1);
10512 b->number = breakpoint_count;
10513 b->disposition = disp_donttouch;
f8eba3c6
TT
10514 b->addr_string = xstrdup (addr_string_start);
10515 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
10516 b->loc->length = length;
10517
f8eba3c6 10518 do_cleanups (cleanup_bkpt);
f1310107
TJB
10519
10520 mention (b);
8d3788bd 10521 observer_notify_breakpoint_created (b);
f1310107
TJB
10522 update_global_location_list (1);
10523}
10524
4a64f543
MS
10525/* Return non-zero if EXP is verified as constant. Returned zero
10526 means EXP is variable. Also the constant detection may fail for
10527 some constant expressions and in such case still falsely return
10528 zero. */
2e6e3d9c 10529
65d79d4b
SDJ
10530static int
10531watchpoint_exp_is_const (const struct expression *exp)
10532{
10533 int i = exp->nelts;
10534
10535 while (i > 0)
10536 {
10537 int oplenp, argsp;
10538
10539 /* We are only interested in the descriptor of each element. */
10540 operator_length (exp, i, &oplenp, &argsp);
10541 i -= oplenp;
10542
10543 switch (exp->elts[i].opcode)
10544 {
10545 case BINOP_ADD:
10546 case BINOP_SUB:
10547 case BINOP_MUL:
10548 case BINOP_DIV:
10549 case BINOP_REM:
10550 case BINOP_MOD:
10551 case BINOP_LSH:
10552 case BINOP_RSH:
10553 case BINOP_LOGICAL_AND:
10554 case BINOP_LOGICAL_OR:
10555 case BINOP_BITWISE_AND:
10556 case BINOP_BITWISE_IOR:
10557 case BINOP_BITWISE_XOR:
10558 case BINOP_EQUAL:
10559 case BINOP_NOTEQUAL:
10560 case BINOP_LESS:
10561 case BINOP_GTR:
10562 case BINOP_LEQ:
10563 case BINOP_GEQ:
10564 case BINOP_REPEAT:
10565 case BINOP_COMMA:
10566 case BINOP_EXP:
10567 case BINOP_MIN:
10568 case BINOP_MAX:
10569 case BINOP_INTDIV:
10570 case BINOP_CONCAT:
10571 case BINOP_IN:
10572 case BINOP_RANGE:
10573 case TERNOP_COND:
10574 case TERNOP_SLICE:
65d79d4b
SDJ
10575
10576 case OP_LONG:
10577 case OP_DOUBLE:
10578 case OP_DECFLOAT:
10579 case OP_LAST:
10580 case OP_COMPLEX:
10581 case OP_STRING:
65d79d4b
SDJ
10582 case OP_ARRAY:
10583 case OP_TYPE:
608b4967
TT
10584 case OP_TYPEOF:
10585 case OP_DECLTYPE:
6e72ca20 10586 case OP_TYPEID:
65d79d4b
SDJ
10587 case OP_NAME:
10588 case OP_OBJC_NSSTRING:
10589
10590 case UNOP_NEG:
10591 case UNOP_LOGICAL_NOT:
10592 case UNOP_COMPLEMENT:
10593 case UNOP_ADDR:
10594 case UNOP_HIGH:
aeaa2474 10595 case UNOP_CAST:
9eaf6705
TT
10596
10597 case UNOP_CAST_TYPE:
10598 case UNOP_REINTERPRET_CAST:
10599 case UNOP_DYNAMIC_CAST:
4a64f543
MS
10600 /* Unary, binary and ternary operators: We have to check
10601 their operands. If they are constant, then so is the
10602 result of that operation. For instance, if A and B are
10603 determined to be constants, then so is "A + B".
10604
10605 UNOP_IND is one exception to the rule above, because the
10606 value of *ADDR is not necessarily a constant, even when
10607 ADDR is. */
65d79d4b
SDJ
10608 break;
10609
10610 case OP_VAR_VALUE:
10611 /* Check whether the associated symbol is a constant.
4a64f543 10612
65d79d4b 10613 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
10614 possible that a buggy compiler could mark a variable as
10615 constant even when it is not, and TYPE_CONST would return
10616 true in this case, while SYMBOL_CLASS wouldn't.
10617
10618 We also have to check for function symbols because they
10619 are always constant. */
65d79d4b
SDJ
10620 {
10621 struct symbol *s = exp->elts[i + 2].symbol;
10622
10623 if (SYMBOL_CLASS (s) != LOC_BLOCK
10624 && SYMBOL_CLASS (s) != LOC_CONST
10625 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10626 return 0;
10627 break;
10628 }
10629
10630 /* The default action is to return 0 because we are using
10631 the optimistic approach here: If we don't know something,
10632 then it is not a constant. */
10633 default:
10634 return 0;
10635 }
10636 }
10637
10638 return 1;
10639}
10640
3a5c3e22
PA
10641/* Implement the "dtor" breakpoint_ops method for watchpoints. */
10642
10643static void
10644dtor_watchpoint (struct breakpoint *self)
10645{
10646 struct watchpoint *w = (struct watchpoint *) self;
10647
10648 xfree (w->cond_exp);
10649 xfree (w->exp);
10650 xfree (w->exp_string);
10651 xfree (w->exp_string_reparse);
10652 value_free (w->val);
10653
10654 base_breakpoint_ops.dtor (self);
10655}
10656
348d480f
PA
10657/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10658
10659static void
10660re_set_watchpoint (struct breakpoint *b)
10661{
3a5c3e22
PA
10662 struct watchpoint *w = (struct watchpoint *) b;
10663
348d480f
PA
10664 /* Watchpoint can be either on expression using entirely global
10665 variables, or it can be on local variables.
10666
10667 Watchpoints of the first kind are never auto-deleted, and even
10668 persist across program restarts. Since they can use variables
10669 from shared libraries, we need to reparse expression as libraries
10670 are loaded and unloaded.
10671
10672 Watchpoints on local variables can also change meaning as result
10673 of solib event. For example, if a watchpoint uses both a local
10674 and a global variables in expression, it's a local watchpoint,
10675 but unloading of a shared library will make the expression
10676 invalid. This is not a very common use case, but we still
10677 re-evaluate expression, to avoid surprises to the user.
10678
10679 Note that for local watchpoints, we re-evaluate it only if
10680 watchpoints frame id is still valid. If it's not, it means the
10681 watchpoint is out of scope and will be deleted soon. In fact,
10682 I'm not sure we'll ever be called in this case.
10683
10684 If a local watchpoint's frame id is still valid, then
3a5c3e22 10685 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10686
3a5c3e22
PA
10687 Don't do anything about disabled watchpoints, since they will be
10688 reevaluated again when enabled. */
10689 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10690}
10691
77b06cd7
TJB
10692/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10693
10694static int
10695insert_watchpoint (struct bp_location *bl)
10696{
3a5c3e22
PA
10697 struct watchpoint *w = (struct watchpoint *) bl->owner;
10698 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10699
10700 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10701 w->cond_exp);
77b06cd7
TJB
10702}
10703
10704/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10705
10706static int
10707remove_watchpoint (struct bp_location *bl)
10708{
3a5c3e22
PA
10709 struct watchpoint *w = (struct watchpoint *) bl->owner;
10710 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10711
10712 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10713 w->cond_exp);
e09342b5
TJB
10714}
10715
e09342b5 10716static int
348d480f 10717breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10718 struct address_space *aspace, CORE_ADDR bp_addr,
10719 const struct target_waitstatus *ws)
e09342b5 10720{
348d480f 10721 struct breakpoint *b = bl->owner;
3a5c3e22 10722 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10723
348d480f
PA
10724 /* Continuable hardware watchpoints are treated as non-existent if the
10725 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10726 some data address). Otherwise gdb won't stop on a break instruction
10727 in the code (not from a breakpoint) when a hardware watchpoint has
10728 been defined. Also skip watchpoints which we know did not trigger
10729 (did not match the data address). */
10730 if (is_hardware_watchpoint (b)
3a5c3e22 10731 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10732 return 0;
9c06b0b4 10733
348d480f 10734 return 1;
9c06b0b4
TJB
10735}
10736
348d480f
PA
10737static void
10738check_status_watchpoint (bpstat bs)
9c06b0b4 10739{
348d480f 10740 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10741
348d480f 10742 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10743}
10744
10745/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10746 hardware watchpoints. */
9c06b0b4
TJB
10747
10748static int
348d480f 10749resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10750{
3a5c3e22
PA
10751 struct watchpoint *w = (struct watchpoint *) bl->owner;
10752 int length = w->exact? 1 : bl->length;
348d480f
PA
10753
10754 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10755}
10756
10757/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10758 hardware watchpoints. */
9c06b0b4
TJB
10759
10760static int
348d480f 10761works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10762{
efa80663
PA
10763 /* Read and access watchpoints only work with hardware support. */
10764 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10765}
10766
9c06b0b4 10767static enum print_stop_action
348d480f 10768print_it_watchpoint (bpstat bs)
9c06b0b4 10769{
348d480f
PA
10770 struct cleanup *old_chain;
10771 struct breakpoint *b;
f99d8bf4 10772 struct ui_file *stb;
348d480f 10773 enum print_stop_action result;
3a5c3e22 10774 struct watchpoint *w;
79a45e25 10775 struct ui_out *uiout = current_uiout;
348d480f
PA
10776
10777 gdb_assert (bs->bp_location_at != NULL);
10778
348d480f 10779 b = bs->breakpoint_at;
3a5c3e22 10780 w = (struct watchpoint *) b;
348d480f 10781
f99d8bf4
PA
10782 stb = mem_fileopen ();
10783 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10784
10785 switch (b->type)
10786 {
348d480f 10787 case bp_watchpoint:
9c06b0b4
TJB
10788 case bp_hardware_watchpoint:
10789 annotate_watchpoint (b->number);
10790 if (ui_out_is_mi_like_p (uiout))
10791 ui_out_field_string
10792 (uiout, "reason",
10793 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10794 mention (b);
10795 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10796 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10797 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10798 ui_out_field_stream (uiout, "old", stb);
10799 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10800 watchpoint_value_print (w->val, stb);
348d480f
PA
10801 ui_out_field_stream (uiout, "new", stb);
10802 ui_out_text (uiout, "\n");
10803 /* More than one watchpoint may have been triggered. */
10804 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10805 break;
10806
10807 case bp_read_watchpoint:
10808 if (ui_out_is_mi_like_p (uiout))
10809 ui_out_field_string
10810 (uiout, "reason",
10811 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10812 mention (b);
10813 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10814 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10815 watchpoint_value_print (w->val, stb);
348d480f
PA
10816 ui_out_field_stream (uiout, "value", stb);
10817 ui_out_text (uiout, "\n");
10818 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10819 break;
10820
10821 case bp_access_watchpoint:
348d480f
PA
10822 if (bs->old_val != NULL)
10823 {
10824 annotate_watchpoint (b->number);
10825 if (ui_out_is_mi_like_p (uiout))
10826 ui_out_field_string
10827 (uiout, "reason",
10828 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10829 mention (b);
10830 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10831 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10832 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10833 ui_out_field_stream (uiout, "old", stb);
10834 ui_out_text (uiout, "\nNew value = ");
10835 }
10836 else
10837 {
10838 mention (b);
10839 if (ui_out_is_mi_like_p (uiout))
10840 ui_out_field_string
10841 (uiout, "reason",
10842 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10843 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10844 ui_out_text (uiout, "\nValue = ");
10845 }
f99d8bf4 10846 watchpoint_value_print (w->val, stb);
348d480f
PA
10847 ui_out_field_stream (uiout, "new", stb);
10848 ui_out_text (uiout, "\n");
10849 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10850 break;
10851 default:
348d480f 10852 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10853 }
10854
348d480f
PA
10855 do_cleanups (old_chain);
10856 return result;
10857}
10858
10859/* Implement the "print_mention" breakpoint_ops method for hardware
10860 watchpoints. */
10861
10862static void
10863print_mention_watchpoint (struct breakpoint *b)
10864{
10865 struct cleanup *ui_out_chain;
3a5c3e22 10866 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10867 struct ui_out *uiout = current_uiout;
348d480f
PA
10868
10869 switch (b->type)
10870 {
10871 case bp_watchpoint:
10872 ui_out_text (uiout, "Watchpoint ");
10873 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10874 break;
10875 case bp_hardware_watchpoint:
10876 ui_out_text (uiout, "Hardware watchpoint ");
10877 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10878 break;
10879 case bp_read_watchpoint:
10880 ui_out_text (uiout, "Hardware read watchpoint ");
10881 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10882 break;
10883 case bp_access_watchpoint:
10884 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10885 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10886 break;
10887 default:
10888 internal_error (__FILE__, __LINE__,
10889 _("Invalid hardware watchpoint type."));
10890 }
10891
10892 ui_out_field_int (uiout, "number", b->number);
10893 ui_out_text (uiout, ": ");
3a5c3e22 10894 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10895 do_cleanups (ui_out_chain);
10896}
10897
10898/* Implement the "print_recreate" breakpoint_ops method for
10899 watchpoints. */
10900
10901static void
10902print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10903{
3a5c3e22
PA
10904 struct watchpoint *w = (struct watchpoint *) b;
10905
348d480f
PA
10906 switch (b->type)
10907 {
10908 case bp_watchpoint:
10909 case bp_hardware_watchpoint:
10910 fprintf_unfiltered (fp, "watch");
10911 break;
10912 case bp_read_watchpoint:
10913 fprintf_unfiltered (fp, "rwatch");
10914 break;
10915 case bp_access_watchpoint:
10916 fprintf_unfiltered (fp, "awatch");
10917 break;
10918 default:
10919 internal_error (__FILE__, __LINE__,
10920 _("Invalid watchpoint type."));
10921 }
10922
3a5c3e22 10923 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10924 print_recreate_thread (b, fp);
348d480f
PA
10925}
10926
427cd150
TT
10927/* Implement the "explains_signal" breakpoint_ops method for
10928 watchpoints. */
10929
47591c29 10930static int
427cd150
TT
10931explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10932{
10933 /* A software watchpoint cannot cause a signal other than
10934 GDB_SIGNAL_TRAP. */
10935 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
47591c29 10936 return 0;
427cd150 10937
47591c29 10938 return 1;
427cd150
TT
10939}
10940
348d480f
PA
10941/* The breakpoint_ops structure to be used in hardware watchpoints. */
10942
2060206e 10943static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10944
10945/* Implement the "insert" breakpoint_ops method for
10946 masked hardware watchpoints. */
10947
10948static int
10949insert_masked_watchpoint (struct bp_location *bl)
10950{
3a5c3e22
PA
10951 struct watchpoint *w = (struct watchpoint *) bl->owner;
10952
10953 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10954 bl->watchpoint_type);
10955}
10956
10957/* Implement the "remove" breakpoint_ops method for
10958 masked hardware watchpoints. */
10959
10960static int
10961remove_masked_watchpoint (struct bp_location *bl)
10962{
3a5c3e22
PA
10963 struct watchpoint *w = (struct watchpoint *) bl->owner;
10964
10965 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10966 bl->watchpoint_type);
10967}
10968
10969/* Implement the "resources_needed" breakpoint_ops method for
10970 masked hardware watchpoints. */
10971
10972static int
10973resources_needed_masked_watchpoint (const struct bp_location *bl)
10974{
3a5c3e22
PA
10975 struct watchpoint *w = (struct watchpoint *) bl->owner;
10976
10977 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10978}
10979
10980/* Implement the "works_in_software_mode" breakpoint_ops method for
10981 masked hardware watchpoints. */
10982
10983static int
10984works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10985{
10986 return 0;
10987}
10988
10989/* Implement the "print_it" breakpoint_ops method for
10990 masked hardware watchpoints. */
10991
10992static enum print_stop_action
10993print_it_masked_watchpoint (bpstat bs)
10994{
10995 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10996 struct ui_out *uiout = current_uiout;
348d480f
PA
10997
10998 /* Masked watchpoints have only one location. */
10999 gdb_assert (b->loc && b->loc->next == NULL);
11000
11001 switch (b->type)
11002 {
11003 case bp_hardware_watchpoint:
11004 annotate_watchpoint (b->number);
11005 if (ui_out_is_mi_like_p (uiout))
11006 ui_out_field_string
11007 (uiout, "reason",
11008 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11009 break;
11010
11011 case bp_read_watchpoint:
11012 if (ui_out_is_mi_like_p (uiout))
11013 ui_out_field_string
11014 (uiout, "reason",
11015 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11016 break;
11017
11018 case bp_access_watchpoint:
11019 if (ui_out_is_mi_like_p (uiout))
11020 ui_out_field_string
11021 (uiout, "reason",
11022 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11023 break;
11024 default:
11025 internal_error (__FILE__, __LINE__,
11026 _("Invalid hardware watchpoint type."));
11027 }
11028
11029 mention (b);
9c06b0b4
TJB
11030 ui_out_text (uiout, _("\n\
11031Check the underlying instruction at PC for the memory\n\
11032address and value which triggered this watchpoint.\n"));
11033 ui_out_text (uiout, "\n");
11034
11035 /* More than one watchpoint may have been triggered. */
11036 return PRINT_UNKNOWN;
11037}
11038
11039/* Implement the "print_one_detail" breakpoint_ops method for
11040 masked hardware watchpoints. */
11041
11042static void
11043print_one_detail_masked_watchpoint (const struct breakpoint *b,
11044 struct ui_out *uiout)
11045{
3a5c3e22
PA
11046 struct watchpoint *w = (struct watchpoint *) b;
11047
9c06b0b4
TJB
11048 /* Masked watchpoints have only one location. */
11049 gdb_assert (b->loc && b->loc->next == NULL);
11050
11051 ui_out_text (uiout, "\tmask ");
3a5c3e22 11052 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
11053 ui_out_text (uiout, "\n");
11054}
11055
11056/* Implement the "print_mention" breakpoint_ops method for
11057 masked hardware watchpoints. */
11058
11059static void
11060print_mention_masked_watchpoint (struct breakpoint *b)
11061{
3a5c3e22 11062 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 11063 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
11064 struct cleanup *ui_out_chain;
11065
11066 switch (b->type)
11067 {
11068 case bp_hardware_watchpoint:
11069 ui_out_text (uiout, "Masked hardware watchpoint ");
11070 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11071 break;
11072 case bp_read_watchpoint:
11073 ui_out_text (uiout, "Masked hardware read watchpoint ");
11074 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11075 break;
11076 case bp_access_watchpoint:
11077 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11078 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11079 break;
11080 default:
11081 internal_error (__FILE__, __LINE__,
11082 _("Invalid hardware watchpoint type."));
11083 }
11084
11085 ui_out_field_int (uiout, "number", b->number);
11086 ui_out_text (uiout, ": ");
3a5c3e22 11087 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
11088 do_cleanups (ui_out_chain);
11089}
11090
11091/* Implement the "print_recreate" breakpoint_ops method for
11092 masked hardware watchpoints. */
11093
11094static void
11095print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11096{
3a5c3e22 11097 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
11098 char tmp[40];
11099
11100 switch (b->type)
11101 {
11102 case bp_hardware_watchpoint:
11103 fprintf_unfiltered (fp, "watch");
11104 break;
11105 case bp_read_watchpoint:
11106 fprintf_unfiltered (fp, "rwatch");
11107 break;
11108 case bp_access_watchpoint:
11109 fprintf_unfiltered (fp, "awatch");
11110 break;
11111 default:
11112 internal_error (__FILE__, __LINE__,
11113 _("Invalid hardware watchpoint type."));
11114 }
11115
3a5c3e22
PA
11116 sprintf_vma (tmp, w->hw_wp_mask);
11117 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 11118 print_recreate_thread (b, fp);
9c06b0b4
TJB
11119}
11120
11121/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11122
2060206e 11123static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
11124
11125/* Tell whether the given watchpoint is a masked hardware watchpoint. */
11126
11127static int
11128is_masked_watchpoint (const struct breakpoint *b)
11129{
11130 return b->ops == &masked_watchpoint_breakpoint_ops;
11131}
11132
53a5351d
JM
11133/* accessflag: hw_write: watch write,
11134 hw_read: watch read,
11135 hw_access: watch access (read or write) */
c906108c 11136static void
bbc13ae3 11137watch_command_1 (const char *arg, int accessflag, int from_tty,
84f4c1fe 11138 int just_location, int internal)
c906108c 11139{
a9634178 11140 volatile struct gdb_exception e;
d983da9c 11141 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 11142 struct expression *exp;
270140bd 11143 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 11144 struct value *val, *mark, *result;
c906108c 11145 struct frame_info *frame;
bbc13ae3
KS
11146 const char *exp_start = NULL;
11147 const char *exp_end = NULL;
11148 const char *tok, *end_tok;
9c06b0b4 11149 int toklen = -1;
bbc13ae3
KS
11150 const char *cond_start = NULL;
11151 const char *cond_end = NULL;
c906108c 11152 enum bptype bp_type;
37e4754d 11153 int thread = -1;
0cf6dd15 11154 int pc = 0;
9c06b0b4
TJB
11155 /* Flag to indicate whether we are going to use masks for
11156 the hardware watchpoint. */
11157 int use_mask = 0;
11158 CORE_ADDR mask = 0;
3a5c3e22 11159 struct watchpoint *w;
bbc13ae3
KS
11160 char *expression;
11161 struct cleanup *back_to;
c906108c 11162
37e4754d
LM
11163 /* Make sure that we actually have parameters to parse. */
11164 if (arg != NULL && arg[0] != '\0')
11165 {
bbc13ae3
KS
11166 const char *value_start;
11167
11168 exp_end = arg + strlen (arg);
37e4754d 11169
9c06b0b4
TJB
11170 /* Look for "parameter value" pairs at the end
11171 of the arguments string. */
bbc13ae3 11172 for (tok = exp_end - 1; tok > arg; tok--)
9c06b0b4
TJB
11173 {
11174 /* Skip whitespace at the end of the argument list. */
11175 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11176 tok--;
11177
11178 /* Find the beginning of the last token.
11179 This is the value of the parameter. */
11180 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11181 tok--;
11182 value_start = tok + 1;
11183
11184 /* Skip whitespace. */
11185 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11186 tok--;
11187
11188 end_tok = tok;
11189
11190 /* Find the beginning of the second to last token.
11191 This is the parameter itself. */
11192 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11193 tok--;
11194 tok++;
11195 toklen = end_tok - tok + 1;
11196
11197 if (toklen == 6 && !strncmp (tok, "thread", 6))
11198 {
11199 /* At this point we've found a "thread" token, which means
11200 the user is trying to set a watchpoint that triggers
11201 only in a specific thread. */
11202 char *endp;
37e4754d 11203
9c06b0b4
TJB
11204 if (thread != -1)
11205 error(_("You can specify only one thread."));
37e4754d 11206
9c06b0b4
TJB
11207 /* Extract the thread ID from the next token. */
11208 thread = strtol (value_start, &endp, 0);
37e4754d 11209
9c06b0b4
TJB
11210 /* Check if the user provided a valid numeric value for the
11211 thread ID. */
11212 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11213 error (_("Invalid thread ID specification %s."), value_start);
11214
11215 /* Check if the thread actually exists. */
11216 if (!valid_thread_id (thread))
af4908ba 11217 invalid_thread_id_error (thread);
9c06b0b4
TJB
11218 }
11219 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11220 {
11221 /* We've found a "mask" token, which means the user wants to
11222 create a hardware watchpoint that is going to have the mask
11223 facility. */
11224 struct value *mask_value, *mark;
37e4754d 11225
9c06b0b4
TJB
11226 if (use_mask)
11227 error(_("You can specify only one mask."));
37e4754d 11228
9c06b0b4 11229 use_mask = just_location = 1;
37e4754d 11230
9c06b0b4
TJB
11231 mark = value_mark ();
11232 mask_value = parse_to_comma_and_eval (&value_start);
11233 mask = value_as_address (mask_value);
11234 value_free_to_mark (mark);
11235 }
11236 else
11237 /* We didn't recognize what we found. We should stop here. */
11238 break;
37e4754d 11239
9c06b0b4
TJB
11240 /* Truncate the string and get rid of the "parameter value" pair before
11241 the arguments string is parsed by the parse_exp_1 function. */
bbc13ae3 11242 exp_end = tok;
9c06b0b4 11243 }
37e4754d 11244 }
bbc13ae3
KS
11245 else
11246 exp_end = arg;
37e4754d 11247
bbc13ae3
KS
11248 /* Parse the rest of the arguments. From here on out, everything
11249 is in terms of a newly allocated string instead of the original
11250 ARG. */
c906108c 11251 innermost_block = NULL;
bbc13ae3
KS
11252 expression = savestring (arg, exp_end - arg);
11253 back_to = make_cleanup (xfree, expression);
11254 exp_start = arg = expression;
1bb9788d 11255 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 11256 exp_end = arg;
fa8a61dc
TT
11257 /* Remove trailing whitespace from the expression before saving it.
11258 This makes the eventual display of the expression string a bit
11259 prettier. */
11260 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11261 --exp_end;
11262
65d79d4b
SDJ
11263 /* Checking if the expression is not constant. */
11264 if (watchpoint_exp_is_const (exp))
11265 {
11266 int len;
11267
11268 len = exp_end - exp_start;
11269 while (len > 0 && isspace (exp_start[len - 1]))
11270 len--;
11271 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11272 }
11273
c906108c
SS
11274 exp_valid_block = innermost_block;
11275 mark = value_mark ();
3a1115a0 11276 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
06a64a0b
TT
11277
11278 if (just_location)
11279 {
9c06b0b4
TJB
11280 int ret;
11281
06a64a0b 11282 exp_valid_block = NULL;
a1442452 11283 val = value_addr (result);
06a64a0b
TT
11284 release_value (val);
11285 value_free_to_mark (mark);
9c06b0b4
TJB
11286
11287 if (use_mask)
11288 {
11289 ret = target_masked_watch_num_registers (value_as_address (val),
11290 mask);
11291 if (ret == -1)
11292 error (_("This target does not support masked watchpoints."));
11293 else if (ret == -2)
11294 error (_("Invalid mask or memory region."));
11295 }
06a64a0b
TT
11296 }
11297 else if (val != NULL)
fa4727a6 11298 release_value (val);
c906108c 11299
bbc13ae3
KS
11300 tok = skip_spaces_const (arg);
11301 end_tok = skip_to_space_const (tok);
c906108c
SS
11302
11303 toklen = end_tok - tok;
11304 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11305 {
2d134ed3
PA
11306 struct expression *cond;
11307
60e1c644 11308 innermost_block = NULL;
c906108c 11309 tok = cond_start = end_tok + 1;
1bb9788d 11310 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
11311
11312 /* The watchpoint expression may not be local, but the condition
11313 may still be. E.g.: `watch global if local > 0'. */
11314 cond_exp_valid_block = innermost_block;
11315
2d134ed3 11316 xfree (cond);
c906108c
SS
11317 cond_end = tok;
11318 }
11319 if (*tok)
8a3fe4f8 11320 error (_("Junk at end of command."));
c906108c 11321
d983da9c 11322 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
11323
11324 /* If the expression is "local", then set up a "watchpoint scope"
11325 breakpoint at the point where we've left the scope of the watchpoint
11326 expression. Create the scope breakpoint before the watchpoint, so
11327 that we will encounter it first in bpstat_stop_status. */
60e1c644 11328 if (exp_valid_block && frame)
d983da9c 11329 {
edb3359d
DJ
11330 if (frame_id_p (frame_unwind_caller_id (frame)))
11331 {
11332 scope_breakpoint
a6d9a66e
UW
11333 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11334 frame_unwind_caller_pc (frame),
06edf0c0
PA
11335 bp_watchpoint_scope,
11336 &momentary_breakpoint_ops);
d983da9c 11337
edb3359d 11338 scope_breakpoint->enable_state = bp_enabled;
d983da9c 11339
edb3359d
DJ
11340 /* Automatically delete the breakpoint when it hits. */
11341 scope_breakpoint->disposition = disp_del;
d983da9c 11342
edb3359d
DJ
11343 /* Only break in the proper frame (help with recursion). */
11344 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 11345
edb3359d 11346 /* Set the address at which we will stop. */
a6d9a66e
UW
11347 scope_breakpoint->loc->gdbarch
11348 = frame_unwind_caller_arch (frame);
edb3359d
DJ
11349 scope_breakpoint->loc->requested_address
11350 = frame_unwind_caller_pc (frame);
11351 scope_breakpoint->loc->address
a6d9a66e
UW
11352 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11353 scope_breakpoint->loc->requested_address,
edb3359d
DJ
11354 scope_breakpoint->type);
11355 }
d983da9c
DJ
11356 }
11357
e8369a73
AB
11358 /* Now set up the breakpoint. We create all watchpoints as hardware
11359 watchpoints here even if hardware watchpoints are turned off, a call
11360 to update_watchpoint later in this function will cause the type to
11361 drop back to bp_watchpoint (software watchpoint) if required. */
11362
11363 if (accessflag == hw_read)
11364 bp_type = bp_read_watchpoint;
11365 else if (accessflag == hw_access)
11366 bp_type = bp_access_watchpoint;
11367 else
11368 bp_type = bp_hardware_watchpoint;
3a5c3e22
PA
11369
11370 w = XCNEW (struct watchpoint);
11371 b = &w->base;
348d480f 11372 if (use_mask)
3a5c3e22
PA
11373 init_raw_breakpoint_without_location (b, NULL, bp_type,
11374 &masked_watchpoint_breakpoint_ops);
348d480f 11375 else
3a5c3e22
PA
11376 init_raw_breakpoint_without_location (b, NULL, bp_type,
11377 &watchpoint_breakpoint_ops);
37e4754d 11378 b->thread = thread;
b5de0fa7 11379 b->disposition = disp_donttouch;
348d480f 11380 b->pspace = current_program_space;
3a5c3e22
PA
11381 w->exp = exp;
11382 w->exp_valid_block = exp_valid_block;
11383 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
11384 if (just_location)
11385 {
11386 struct type *t = value_type (val);
11387 CORE_ADDR addr = value_as_address (val);
11388 char *name;
11389
11390 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11391 name = type_to_string (t);
11392
3a5c3e22 11393 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 11394 core_addr_to_string (addr));
06a64a0b
TT
11395 xfree (name);
11396
3a5c3e22 11397 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
11398 (int) (exp_end - exp_start), exp_start);
11399
06a64a0b
TT
11400 /* The above expression is in C. */
11401 b->language = language_c;
11402 }
11403 else
3a5c3e22 11404 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
11405
11406 if (use_mask)
11407 {
3a5c3e22 11408 w->hw_wp_mask = mask;
9c06b0b4
TJB
11409 }
11410 else
11411 {
3a5c3e22
PA
11412 w->val = val;
11413 w->val_valid = 1;
9c06b0b4 11414 }
77b06cd7 11415
c906108c
SS
11416 if (cond_start)
11417 b->cond_string = savestring (cond_start, cond_end - cond_start);
11418 else
11419 b->cond_string = 0;
c5aa993b 11420
c906108c 11421 if (frame)
f6bc2008 11422 {
3a5c3e22
PA
11423 w->watchpoint_frame = get_frame_id (frame);
11424 w->watchpoint_thread = inferior_ptid;
f6bc2008 11425 }
c906108c 11426 else
f6bc2008 11427 {
3a5c3e22
PA
11428 w->watchpoint_frame = null_frame_id;
11429 w->watchpoint_thread = null_ptid;
f6bc2008 11430 }
c906108c 11431
d983da9c 11432 if (scope_breakpoint != NULL)
c906108c 11433 {
d983da9c
DJ
11434 /* The scope breakpoint is related to the watchpoint. We will
11435 need to act on them together. */
11436 b->related_breakpoint = scope_breakpoint;
11437 scope_breakpoint->related_breakpoint = b;
c906108c 11438 }
d983da9c 11439
06a64a0b
TT
11440 if (!just_location)
11441 value_free_to_mark (mark);
2d134ed3 11442
a9634178
TJB
11443 TRY_CATCH (e, RETURN_MASK_ALL)
11444 {
11445 /* Finally update the new watchpoint. This creates the locations
11446 that should be inserted. */
3a5c3e22 11447 update_watchpoint (w, 1);
a9634178
TJB
11448 }
11449 if (e.reason < 0)
11450 {
11451 delete_breakpoint (b);
11452 throw_exception (e);
11453 }
11454
3ea46bff 11455 install_breakpoint (internal, b, 1);
bbc13ae3 11456 do_cleanups (back_to);
c906108c
SS
11457}
11458
e09342b5 11459/* Return count of debug registers needed to watch the given expression.
e09342b5 11460 If the watchpoint cannot be handled in hardware return zero. */
c906108c 11461
c906108c 11462static int
a9634178 11463can_use_hardware_watchpoint (struct value *v)
c906108c
SS
11464{
11465 int found_memory_cnt = 0;
2e70b7b9 11466 struct value *head = v;
c906108c
SS
11467
11468 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 11469 if (!can_use_hw_watchpoints)
c906108c 11470 return 0;
c5aa993b 11471
5c44784c
JM
11472 /* Make sure that the value of the expression depends only upon
11473 memory contents, and values computed from them within GDB. If we
11474 find any register references or function calls, we can't use a
11475 hardware watchpoint.
11476
11477 The idea here is that evaluating an expression generates a series
11478 of values, one holding the value of every subexpression. (The
11479 expression a*b+c has five subexpressions: a, b, a*b, c, and
11480 a*b+c.) GDB's values hold almost enough information to establish
11481 the criteria given above --- they identify memory lvalues,
11482 register lvalues, computed values, etcetera. So we can evaluate
11483 the expression, and then scan the chain of values that leaves
11484 behind to decide whether we can detect any possible change to the
11485 expression's final value using only hardware watchpoints.
11486
11487 However, I don't think that the values returned by inferior
11488 function calls are special in any way. So this function may not
11489 notice that an expression involving an inferior function call
11490 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 11491 for (; v; v = value_next (v))
c906108c 11492 {
5c44784c 11493 if (VALUE_LVAL (v) == lval_memory)
c906108c 11494 {
8464be76
DJ
11495 if (v != head && value_lazy (v))
11496 /* A lazy memory lvalue in the chain is one that GDB never
11497 needed to fetch; we either just used its address (e.g.,
11498 `a' in `a.b') or we never needed it at all (e.g., `a'
11499 in `a,b'). This doesn't apply to HEAD; if that is
11500 lazy then it was not readable, but watch it anyway. */
5c44784c 11501 ;
53a5351d 11502 else
5c44784c
JM
11503 {
11504 /* Ahh, memory we actually used! Check if we can cover
11505 it with hardware watchpoints. */
df407dfe 11506 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
11507
11508 /* We only watch structs and arrays if user asked for it
11509 explicitly, never if they just happen to appear in a
11510 middle of some value chain. */
11511 if (v == head
11512 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11513 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11514 {
42ae5230 11515 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
11516 int len;
11517 int num_regs;
11518
a9634178 11519 len = (target_exact_watchpoints
e09342b5
TJB
11520 && is_scalar_type_recursive (vtype))?
11521 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 11522
e09342b5
TJB
11523 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11524 if (!num_regs)
2e70b7b9
MS
11525 return 0;
11526 else
e09342b5 11527 found_memory_cnt += num_regs;
2e70b7b9 11528 }
5c44784c 11529 }
c5aa993b 11530 }
5086187c
AC
11531 else if (VALUE_LVAL (v) != not_lval
11532 && deprecated_value_modifiable (v) == 0)
38b6c3b3 11533 return 0; /* These are values from the history (e.g., $1). */
5086187c 11534 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 11535 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
11536 }
11537
11538 /* The expression itself looks suitable for using a hardware
11539 watchpoint, but give the target machine a chance to reject it. */
11540 return found_memory_cnt;
11541}
11542
8b93c638 11543void
84f4c1fe 11544watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11545{
84f4c1fe 11546 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
11547}
11548
06a64a0b
TT
11549/* A helper function that looks for the "-location" argument and then
11550 calls watch_command_1. */
11551
11552static void
11553watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11554{
11555 int just_location = 0;
11556
11557 if (arg
11558 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11559 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11560 {
e9cafbcc 11561 arg = skip_spaces (arg);
06a64a0b
TT
11562 just_location = 1;
11563 }
11564
84f4c1fe 11565 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 11566}
8926118c 11567
c5aa993b 11568static void
fba45db2 11569watch_command (char *arg, int from_tty)
c906108c 11570{
06a64a0b 11571 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
11572}
11573
8b93c638 11574void
84f4c1fe 11575rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11576{
84f4c1fe 11577 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 11578}
8926118c 11579
c5aa993b 11580static void
fba45db2 11581rwatch_command (char *arg, int from_tty)
c906108c 11582{
06a64a0b 11583 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
11584}
11585
8b93c638 11586void
84f4c1fe 11587awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 11588{
84f4c1fe 11589 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 11590}
8926118c 11591
c5aa993b 11592static void
fba45db2 11593awatch_command (char *arg, int from_tty)
c906108c 11594{
06a64a0b 11595 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 11596}
c906108c 11597\f
c5aa993b 11598
43ff13b4 11599/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
11600 because it uses the mechanisms of breakpoints. */
11601
bfec99b2
PA
11602struct until_break_command_continuation_args
11603{
11604 struct breakpoint *breakpoint;
11605 struct breakpoint *breakpoint2;
186c406b 11606 int thread_num;
bfec99b2
PA
11607};
11608
43ff13b4 11609/* This function is called by fetch_inferior_event via the
4a64f543 11610 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 11611 care of cleaning up the temporary breakpoints set up by the until
4a64f543 11612 command. */
c2c6d25f 11613static void
fa4cd53f 11614until_break_command_continuation (void *arg, int err)
43ff13b4 11615{
bfec99b2
PA
11616 struct until_break_command_continuation_args *a = arg;
11617
11618 delete_breakpoint (a->breakpoint);
11619 if (a->breakpoint2)
11620 delete_breakpoint (a->breakpoint2);
186c406b 11621 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
11622}
11623
c906108c 11624void
ae66c1fc 11625until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
11626{
11627 struct symtabs_and_lines sals;
11628 struct symtab_and_line sal;
8556afb4
PA
11629 struct frame_info *frame;
11630 struct gdbarch *frame_gdbarch;
11631 struct frame_id stack_frame_id;
11632 struct frame_id caller_frame_id;
c906108c 11633 struct breakpoint *breakpoint;
f107f563 11634 struct breakpoint *breakpoint2 = NULL;
c906108c 11635 struct cleanup *old_chain;
186c406b
TT
11636 int thread;
11637 struct thread_info *tp;
c906108c
SS
11638
11639 clear_proceed_status ();
11640
11641 /* Set a breakpoint where the user wants it and at return from
4a64f543 11642 this function. */
c5aa993b 11643
1bfeeb0f 11644 if (last_displayed_sal_is_valid ())
f8eba3c6 11645 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 11646 get_last_displayed_symtab (),
f8eba3c6 11647 get_last_displayed_line ());
c906108c 11648 else
f8eba3c6
TT
11649 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11650 (struct symtab *) NULL, 0);
c5aa993b 11651
c906108c 11652 if (sals.nelts != 1)
8a3fe4f8 11653 error (_("Couldn't get information on specified line."));
c5aa993b 11654
c906108c 11655 sal = sals.sals[0];
4a64f543 11656 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 11657
c906108c 11658 if (*arg)
8a3fe4f8 11659 error (_("Junk at end of arguments."));
c5aa993b 11660
c906108c 11661 resolve_sal_pc (&sal);
c5aa993b 11662
186c406b
TT
11663 tp = inferior_thread ();
11664 thread = tp->num;
11665
883bc8d1
PA
11666 old_chain = make_cleanup (null_cleanup, NULL);
11667
8556afb4
PA
11668 /* Note linespec handling above invalidates the frame chain.
11669 Installing a breakpoint also invalidates the frame chain (as it
11670 may need to switch threads), so do any frame handling before
11671 that. */
11672
11673 frame = get_selected_frame (NULL);
11674 frame_gdbarch = get_frame_arch (frame);
11675 stack_frame_id = get_stack_frame_id (frame);
11676 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11677
ae66c1fc
EZ
11678 /* Keep within the current frame, or in frames called by the current
11679 one. */
edb3359d 11680
883bc8d1 11681 if (frame_id_p (caller_frame_id))
c906108c 11682 {
883bc8d1
PA
11683 struct symtab_and_line sal2;
11684
11685 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11686 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11687 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11688 sal2,
11689 caller_frame_id,
f107f563
VP
11690 bp_until);
11691 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11692
883bc8d1 11693 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11694 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11695 }
c5aa993b 11696
c70a6932
JK
11697 /* set_momentary_breakpoint could invalidate FRAME. */
11698 frame = NULL;
11699
883bc8d1
PA
11700 if (anywhere)
11701 /* If the user told us to continue until a specified location,
11702 we don't specify a frame at which we need to stop. */
11703 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11704 null_frame_id, bp_until);
11705 else
11706 /* Otherwise, specify the selected frame, because we want to stop
11707 only at the very same frame. */
11708 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11709 stack_frame_id, bp_until);
11710 make_cleanup_delete_breakpoint (breakpoint);
11711
a493e3e2 11712 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11713
4a64f543
MS
11714 /* If we are running asynchronously, and proceed call above has
11715 actually managed to start the target, arrange for breakpoints to
11716 be deleted when the target stops. Otherwise, we're already
11717 stopped and delete breakpoints via cleanup chain. */
f107f563 11718
8ea051c5 11719 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11720 {
bfec99b2
PA
11721 struct until_break_command_continuation_args *args;
11722 args = xmalloc (sizeof (*args));
f107f563 11723
bfec99b2
PA
11724 args->breakpoint = breakpoint;
11725 args->breakpoint2 = breakpoint2;
186c406b 11726 args->thread_num = thread;
f107f563
VP
11727
11728 discard_cleanups (old_chain);
95e54da7
PA
11729 add_continuation (inferior_thread (),
11730 until_break_command_continuation, args,
604ead4a 11731 xfree);
f107f563
VP
11732 }
11733 else
c5aa993b 11734 do_cleanups (old_chain);
c906108c 11735}
ae66c1fc 11736
c906108c
SS
11737/* This function attempts to parse an optional "if <cond>" clause
11738 from the arg string. If one is not found, it returns NULL.
c5aa993b 11739
c906108c
SS
11740 Else, it returns a pointer to the condition string. (It does not
11741 attempt to evaluate the string against a particular block.) And,
11742 it updates arg to point to the first character following the parsed
4a64f543 11743 if clause in the arg string. */
53a5351d 11744
916703c0 11745char *
fba45db2 11746ep_parse_optional_if_clause (char **arg)
c906108c 11747{
c5aa993b
JM
11748 char *cond_string;
11749
11750 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11751 return NULL;
c5aa993b 11752
4a64f543 11753 /* Skip the "if" keyword. */
c906108c 11754 (*arg) += 2;
c5aa993b 11755
c906108c 11756 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11757 condition string. */
e9cafbcc 11758 *arg = skip_spaces (*arg);
c906108c 11759 cond_string = *arg;
c5aa993b 11760
4a64f543
MS
11761 /* Assume that the condition occupies the remainder of the arg
11762 string. */
c906108c 11763 (*arg) += strlen (cond_string);
c5aa993b 11764
c906108c
SS
11765 return cond_string;
11766}
c5aa993b 11767
c906108c
SS
11768/* Commands to deal with catching events, such as signals, exceptions,
11769 process start/exit, etc. */
c5aa993b
JM
11770
11771typedef enum
11772{
44feb3ce
TT
11773 catch_fork_temporary, catch_vfork_temporary,
11774 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11775}
11776catch_fork_kind;
11777
c906108c 11778static void
cc59ec59
MS
11779catch_fork_command_1 (char *arg, int from_tty,
11780 struct cmd_list_element *command)
c906108c 11781{
a6d9a66e 11782 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11783 char *cond_string = NULL;
44feb3ce
TT
11784 catch_fork_kind fork_kind;
11785 int tempflag;
11786
11787 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11788 tempflag = (fork_kind == catch_fork_temporary
11789 || fork_kind == catch_vfork_temporary);
c5aa993b 11790
44feb3ce
TT
11791 if (!arg)
11792 arg = "";
e9cafbcc 11793 arg = skip_spaces (arg);
c5aa993b 11794
c906108c 11795 /* The allowed syntax is:
c5aa993b
JM
11796 catch [v]fork
11797 catch [v]fork if <cond>
11798
4a64f543 11799 First, check if there's an if clause. */
c906108c 11800 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11801
c906108c 11802 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11803 error (_("Junk at end of arguments."));
c5aa993b 11804
c906108c 11805 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11806 and enable reporting of such events. */
c5aa993b
JM
11807 switch (fork_kind)
11808 {
44feb3ce
TT
11809 case catch_fork_temporary:
11810 case catch_fork_permanent:
a6d9a66e 11811 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11812 &catch_fork_breakpoint_ops);
c906108c 11813 break;
44feb3ce
TT
11814 case catch_vfork_temporary:
11815 case catch_vfork_permanent:
a6d9a66e 11816 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11817 &catch_vfork_breakpoint_ops);
c906108c 11818 break;
c5aa993b 11819 default:
8a3fe4f8 11820 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11821 break;
c5aa993b 11822 }
c906108c
SS
11823}
11824
11825static void
cc59ec59
MS
11826catch_exec_command_1 (char *arg, int from_tty,
11827 struct cmd_list_element *command)
c906108c 11828{
b4d90040 11829 struct exec_catchpoint *c;
a6d9a66e 11830 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11831 int tempflag;
c5aa993b 11832 char *cond_string = NULL;
c906108c 11833
44feb3ce
TT
11834 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11835
11836 if (!arg)
11837 arg = "";
e9cafbcc 11838 arg = skip_spaces (arg);
c906108c
SS
11839
11840 /* The allowed syntax is:
c5aa993b
JM
11841 catch exec
11842 catch exec if <cond>
c906108c 11843
4a64f543 11844 First, check if there's an if clause. */
c906108c
SS
11845 cond_string = ep_parse_optional_if_clause (&arg);
11846
11847 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11848 error (_("Junk at end of arguments."));
c906108c 11849
b4d90040
PA
11850 c = XNEW (struct exec_catchpoint);
11851 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11852 &catch_exec_breakpoint_ops);
11853 c->exec_pathname = NULL;
11854
3ea46bff 11855 install_breakpoint (0, &c->base, 1);
c906108c 11856}
c5aa993b 11857
9ac4176b 11858void
28010a5d
PA
11859init_ada_exception_breakpoint (struct breakpoint *b,
11860 struct gdbarch *gdbarch,
11861 struct symtab_and_line sal,
11862 char *addr_string,
c0a91b2b 11863 const struct breakpoint_ops *ops,
28010a5d 11864 int tempflag,
349774ef 11865 int enabled,
28010a5d 11866 int from_tty)
f7f9143b 11867{
f7f9143b
JB
11868 if (from_tty)
11869 {
5af949e3
UW
11870 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11871 if (!loc_gdbarch)
11872 loc_gdbarch = gdbarch;
11873
6c95b8df
PA
11874 describe_other_breakpoints (loc_gdbarch,
11875 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11876 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11877 version for exception catchpoints, because two catchpoints
11878 used for different exception names will use the same address.
11879 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11880 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11881 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11882 the user what type of catchpoint it is. The above is good
11883 enough for now, though. */
11884 }
11885
28010a5d 11886 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b 11887
349774ef 11888 b->enable_state = enabled ? bp_enabled : bp_disabled;
f7f9143b 11889 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
11890 b->addr_string = addr_string;
11891 b->language = language_ada;
f7f9143b
JB
11892}
11893
a96d9b2e
SDJ
11894/* Splits the argument using space as delimiter. Returns an xmalloc'd
11895 filter list, or NULL if no filtering is required. */
11896static VEC(int) *
11897catch_syscall_split_args (char *arg)
11898{
11899 VEC(int) *result = NULL;
29d0bb3d 11900 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
11901
11902 while (*arg != '\0')
11903 {
11904 int i, syscall_number;
11905 char *endptr;
11906 char cur_name[128];
11907 struct syscall s;
11908
11909 /* Skip whitespace. */
529480d0 11910 arg = skip_spaces (arg);
a96d9b2e
SDJ
11911
11912 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11913 cur_name[i] = arg[i];
11914 cur_name[i] = '\0';
11915 arg += i;
11916
11917 /* Check if the user provided a syscall name or a number. */
11918 syscall_number = (int) strtol (cur_name, &endptr, 0);
11919 if (*endptr == '\0')
bccd0dd2 11920 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
11921 else
11922 {
11923 /* We have a name. Let's check if it's valid and convert it
11924 to a number. */
11925 get_syscall_by_name (cur_name, &s);
11926
11927 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
11928 /* Here we have to issue an error instead of a warning,
11929 because GDB cannot do anything useful if there's no
11930 syscall number to be caught. */
a96d9b2e
SDJ
11931 error (_("Unknown syscall name '%s'."), cur_name);
11932 }
11933
11934 /* Ok, it's valid. */
11935 VEC_safe_push (int, result, s.number);
11936 }
11937
11938 discard_cleanups (cleanup);
11939 return result;
11940}
11941
11942/* Implement the "catch syscall" command. */
11943
11944static void
cc59ec59
MS
11945catch_syscall_command_1 (char *arg, int from_tty,
11946 struct cmd_list_element *command)
a96d9b2e
SDJ
11947{
11948 int tempflag;
11949 VEC(int) *filter;
11950 struct syscall s;
11951 struct gdbarch *gdbarch = get_current_arch ();
11952
11953 /* Checking if the feature if supported. */
11954 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11955 error (_("The feature 'catch syscall' is not supported on \
ea666128 11956this architecture yet."));
a96d9b2e
SDJ
11957
11958 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11959
e9cafbcc 11960 arg = skip_spaces (arg);
a96d9b2e
SDJ
11961
11962 /* We need to do this first "dummy" translation in order
11963 to get the syscall XML file loaded or, most important,
11964 to display a warning to the user if there's no XML file
11965 for his/her architecture. */
11966 get_syscall_by_number (0, &s);
11967
11968 /* The allowed syntax is:
11969 catch syscall
11970 catch syscall <name | number> [<name | number> ... <name | number>]
11971
11972 Let's check if there's a syscall name. */
11973
11974 if (arg != NULL)
11975 filter = catch_syscall_split_args (arg);
11976 else
11977 filter = NULL;
11978
11979 create_syscall_event_catchpoint (tempflag, filter,
11980 &catch_syscall_breakpoint_ops);
11981}
11982
c906108c 11983static void
fba45db2 11984catch_command (char *arg, int from_tty)
c906108c 11985{
44feb3ce 11986 error (_("Catch requires an event name."));
c906108c
SS
11987}
11988\f
11989
11990static void
fba45db2 11991tcatch_command (char *arg, int from_tty)
c906108c 11992{
44feb3ce 11993 error (_("Catch requires an event name."));
c906108c
SS
11994}
11995
8a2c437b
TT
11996/* A qsort comparison function that sorts breakpoints in order. */
11997
11998static int
11999compare_breakpoints (const void *a, const void *b)
12000{
12001 const breakpoint_p *ba = a;
12002 uintptr_t ua = (uintptr_t) *ba;
12003 const breakpoint_p *bb = b;
12004 uintptr_t ub = (uintptr_t) *bb;
12005
12006 if ((*ba)->number < (*bb)->number)
12007 return -1;
12008 else if ((*ba)->number > (*bb)->number)
12009 return 1;
12010
12011 /* Now sort by address, in case we see, e..g, two breakpoints with
12012 the number 0. */
12013 if (ua < ub)
12014 return -1;
94b0e70d 12015 return ua > ub ? 1 : 0;
8a2c437b
TT
12016}
12017
80f8a6eb 12018/* Delete breakpoints by address or line. */
c906108c
SS
12019
12020static void
fba45db2 12021clear_command (char *arg, int from_tty)
c906108c 12022{
8a2c437b 12023 struct breakpoint *b, *prev;
d6e956e5
VP
12024 VEC(breakpoint_p) *found = 0;
12025 int ix;
c906108c
SS
12026 int default_match;
12027 struct symtabs_and_lines sals;
12028 struct symtab_and_line sal;
c906108c 12029 int i;
8a2c437b 12030 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
12031
12032 if (arg)
12033 {
39cf75f7
DE
12034 sals = decode_line_with_current_source (arg,
12035 (DECODE_LINE_FUNFIRSTLINE
12036 | DECODE_LINE_LIST_MODE));
cf4ded82 12037 make_cleanup (xfree, sals.sals);
c906108c
SS
12038 default_match = 0;
12039 }
12040 else
12041 {
c5aa993b 12042 sals.sals = (struct symtab_and_line *)
c906108c 12043 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 12044 make_cleanup (xfree, sals.sals);
4a64f543 12045 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
12046
12047 /* Set sal's line, symtab, pc, and pspace to the values
12048 corresponding to the last call to print_frame_info. If the
12049 codepoint is not valid, this will set all the fields to 0. */
12050 get_last_displayed_sal (&sal);
c906108c 12051 if (sal.symtab == 0)
8a3fe4f8 12052 error (_("No source file specified."));
c906108c
SS
12053
12054 sals.sals[0] = sal;
12055 sals.nelts = 1;
12056
12057 default_match = 1;
12058 }
12059
4a64f543
MS
12060 /* We don't call resolve_sal_pc here. That's not as bad as it
12061 seems, because all existing breakpoints typically have both
12062 file/line and pc set. So, if clear is given file/line, we can
12063 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
12064
12065 We only support clearing given the address explicitly
12066 present in breakpoint table. Say, we've set breakpoint
4a64f543 12067 at file:line. There were several PC values for that file:line,
ed0616c6 12068 due to optimization, all in one block.
4a64f543
MS
12069
12070 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
12071 PC corresponding to the same file:line, the breakpoint won't
12072 be cleared. We probably can still clear the breakpoint, but
12073 since the other PC value is never presented to user, user
12074 can only find it by guessing, and it does not seem important
12075 to support that. */
12076
4a64f543
MS
12077 /* For each line spec given, delete bps which correspond to it. Do
12078 it in two passes, solely to preserve the current behavior that
12079 from_tty is forced true if we delete more than one
12080 breakpoint. */
c906108c 12081
80f8a6eb 12082 found = NULL;
8a2c437b 12083 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
12084 for (i = 0; i < sals.nelts; i++)
12085 {
05cba821
JK
12086 const char *sal_fullname;
12087
c906108c 12088 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
12089 If line given (pc == 0), clear all bpts on specified line.
12090 If defaulting, clear all bpts on default line
c906108c 12091 or at default pc.
c5aa993b
JM
12092
12093 defaulting sal.pc != 0 tests to do
12094
12095 0 1 pc
12096 1 1 pc _and_ line
12097 0 0 line
12098 1 0 <can't happen> */
c906108c
SS
12099
12100 sal = sals.sals[i];
05cba821
JK
12101 sal_fullname = (sal.symtab == NULL
12102 ? NULL : symtab_to_fullname (sal.symtab));
c906108c 12103
4a64f543 12104 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 12105 ALL_BREAKPOINTS (b)
c5aa993b 12106 {
0d381245 12107 int match = 0;
4a64f543 12108 /* Are we going to delete b? */
cc60f2e3 12109 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
12110 {
12111 struct bp_location *loc = b->loc;
12112 for (; loc; loc = loc->next)
12113 {
f8eba3c6
TT
12114 /* If the user specified file:line, don't allow a PC
12115 match. This matches historical gdb behavior. */
12116 int pc_match = (!sal.explicit_line
12117 && sal.pc
12118 && (loc->pspace == sal.pspace)
12119 && (loc->address == sal.pc)
12120 && (!section_is_overlay (loc->section)
12121 || loc->section == sal.section));
4aac40c8
TT
12122 int line_match = 0;
12123
12124 if ((default_match || sal.explicit_line)
2f202fde 12125 && loc->symtab != NULL
05cba821 12126 && sal_fullname != NULL
4aac40c8 12127 && sal.pspace == loc->pspace
05cba821
JK
12128 && loc->line_number == sal.line
12129 && filename_cmp (symtab_to_fullname (loc->symtab),
12130 sal_fullname) == 0)
12131 line_match = 1;
4aac40c8 12132
0d381245
VP
12133 if (pc_match || line_match)
12134 {
12135 match = 1;
12136 break;
12137 }
12138 }
12139 }
12140
12141 if (match)
d6e956e5 12142 VEC_safe_push(breakpoint_p, found, b);
c906108c 12143 }
80f8a6eb 12144 }
8a2c437b 12145
80f8a6eb 12146 /* Now go thru the 'found' chain and delete them. */
d6e956e5 12147 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
12148 {
12149 if (arg)
8a3fe4f8 12150 error (_("No breakpoint at %s."), arg);
80f8a6eb 12151 else
8a3fe4f8 12152 error (_("No breakpoint at this line."));
80f8a6eb 12153 }
c906108c 12154
8a2c437b
TT
12155 /* Remove duplicates from the vec. */
12156 qsort (VEC_address (breakpoint_p, found),
12157 VEC_length (breakpoint_p, found),
12158 sizeof (breakpoint_p),
12159 compare_breakpoints);
12160 prev = VEC_index (breakpoint_p, found, 0);
12161 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12162 {
12163 if (b == prev)
12164 {
12165 VEC_ordered_remove (breakpoint_p, found, ix);
12166 --ix;
12167 }
12168 }
12169
d6e956e5 12170 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 12171 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 12172 if (from_tty)
a3f17187 12173 {
d6e956e5 12174 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
12175 printf_unfiltered (_("Deleted breakpoint "));
12176 else
12177 printf_unfiltered (_("Deleted breakpoints "));
12178 }
d6e956e5
VP
12179
12180 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 12181 {
c5aa993b 12182 if (from_tty)
d6e956e5
VP
12183 printf_unfiltered ("%d ", b->number);
12184 delete_breakpoint (b);
c906108c 12185 }
80f8a6eb
MS
12186 if (from_tty)
12187 putchar_unfiltered ('\n');
8a2c437b
TT
12188
12189 do_cleanups (cleanups);
c906108c
SS
12190}
12191\f
12192/* Delete breakpoint in BS if they are `delete' breakpoints and
12193 all breakpoints that are marked for deletion, whether hit or not.
12194 This is called after any breakpoint is hit, or after errors. */
12195
12196void
fba45db2 12197breakpoint_auto_delete (bpstat bs)
c906108c 12198{
35df4500 12199 struct breakpoint *b, *b_tmp;
c906108c
SS
12200
12201 for (; bs; bs = bs->next)
f431efe5
PA
12202 if (bs->breakpoint_at
12203 && bs->breakpoint_at->disposition == disp_del
c906108c 12204 && bs->stop)
f431efe5 12205 delete_breakpoint (bs->breakpoint_at);
c906108c 12206
35df4500 12207 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 12208 {
b5de0fa7 12209 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
12210 delete_breakpoint (b);
12211 }
c906108c
SS
12212}
12213
4a64f543
MS
12214/* A comparison function for bp_location AP and BP being interfaced to
12215 qsort. Sort elements primarily by their ADDRESS (no matter what
12216 does breakpoint_address_is_meaningful say for its OWNER),
12217 secondarily by ordering first bp_permanent OWNERed elements and
12218 terciarily just ensuring the array is sorted stable way despite
e5dd4106 12219 qsort being an unstable algorithm. */
876fa593
JK
12220
12221static int
494cfb0f 12222bp_location_compare (const void *ap, const void *bp)
876fa593 12223{
494cfb0f
JK
12224 struct bp_location *a = *(void **) ap;
12225 struct bp_location *b = *(void **) bp;
2bdf28a0 12226 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
12227 int a_perm = a->owner->enable_state == bp_permanent;
12228 int b_perm = b->owner->enable_state == bp_permanent;
12229
12230 if (a->address != b->address)
12231 return (a->address > b->address) - (a->address < b->address);
12232
dea2aa5f
LM
12233 /* Sort locations at the same address by their pspace number, keeping
12234 locations of the same inferior (in a multi-inferior environment)
12235 grouped. */
12236
12237 if (a->pspace->num != b->pspace->num)
12238 return ((a->pspace->num > b->pspace->num)
12239 - (a->pspace->num < b->pspace->num));
12240
876fa593
JK
12241 /* Sort permanent breakpoints first. */
12242 if (a_perm != b_perm)
12243 return (a_perm < b_perm) - (a_perm > b_perm);
12244
c56a97f9
JK
12245 /* Make the internal GDB representation stable across GDB runs
12246 where A and B memory inside GDB can differ. Breakpoint locations of
12247 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
12248
12249 if (a->owner->number != b->owner->number)
c56a97f9
JK
12250 return ((a->owner->number > b->owner->number)
12251 - (a->owner->number < b->owner->number));
876fa593
JK
12252
12253 return (a > b) - (a < b);
12254}
12255
876fa593 12256/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
12257 bp_location_shadow_len_after_address_max according to the current
12258 content of the bp_location array. */
f7545552
TT
12259
12260static void
876fa593 12261bp_location_target_extensions_update (void)
f7545552 12262{
876fa593
JK
12263 struct bp_location *bl, **blp_tmp;
12264
12265 bp_location_placed_address_before_address_max = 0;
12266 bp_location_shadow_len_after_address_max = 0;
12267
12268 ALL_BP_LOCATIONS (bl, blp_tmp)
12269 {
12270 CORE_ADDR start, end, addr;
12271
12272 if (!bp_location_has_shadow (bl))
12273 continue;
12274
12275 start = bl->target_info.placed_address;
12276 end = start + bl->target_info.shadow_len;
12277
12278 gdb_assert (bl->address >= start);
12279 addr = bl->address - start;
12280 if (addr > bp_location_placed_address_before_address_max)
12281 bp_location_placed_address_before_address_max = addr;
12282
12283 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12284
12285 gdb_assert (bl->address < end);
12286 addr = end - bl->address;
12287 if (addr > bp_location_shadow_len_after_address_max)
12288 bp_location_shadow_len_after_address_max = addr;
12289 }
f7545552
TT
12290}
12291
1e4d1764
YQ
12292/* Download tracepoint locations if they haven't been. */
12293
12294static void
12295download_tracepoint_locations (void)
12296{
7ed2c994 12297 struct breakpoint *b;
1e4d1764
YQ
12298 struct cleanup *old_chain;
12299
12300 if (!target_can_download_tracepoint ())
12301 return;
12302
12303 old_chain = save_current_space_and_thread ();
12304
7ed2c994 12305 ALL_TRACEPOINTS (b)
1e4d1764 12306 {
7ed2c994 12307 struct bp_location *bl;
1e4d1764 12308 struct tracepoint *t;
f2a8bc8a 12309 int bp_location_downloaded = 0;
1e4d1764 12310
7ed2c994 12311 if ((b->type == bp_fast_tracepoint
1e4d1764
YQ
12312 ? !may_insert_fast_tracepoints
12313 : !may_insert_tracepoints))
12314 continue;
12315
7ed2c994
YQ
12316 for (bl = b->loc; bl; bl = bl->next)
12317 {
12318 /* In tracepoint, locations are _never_ duplicated, so
12319 should_be_inserted is equivalent to
12320 unduplicated_should_be_inserted. */
12321 if (!should_be_inserted (bl) || bl->inserted)
12322 continue;
1e4d1764 12323
7ed2c994 12324 switch_to_program_space_and_thread (bl->pspace);
1e4d1764 12325
7ed2c994 12326 target_download_tracepoint (bl);
1e4d1764 12327
7ed2c994 12328 bl->inserted = 1;
f2a8bc8a 12329 bp_location_downloaded = 1;
7ed2c994
YQ
12330 }
12331 t = (struct tracepoint *) b;
12332 t->number_on_target = b->number;
f2a8bc8a
YQ
12333 if (bp_location_downloaded)
12334 observer_notify_breakpoint_modified (b);
1e4d1764
YQ
12335 }
12336
12337 do_cleanups (old_chain);
12338}
12339
934709f0
PW
12340/* Swap the insertion/duplication state between two locations. */
12341
12342static void
12343swap_insertion (struct bp_location *left, struct bp_location *right)
12344{
12345 const int left_inserted = left->inserted;
12346 const int left_duplicate = left->duplicate;
b775012e 12347 const int left_needs_update = left->needs_update;
934709f0
PW
12348 const struct bp_target_info left_target_info = left->target_info;
12349
1e4d1764
YQ
12350 /* Locations of tracepoints can never be duplicated. */
12351 if (is_tracepoint (left->owner))
12352 gdb_assert (!left->duplicate);
12353 if (is_tracepoint (right->owner))
12354 gdb_assert (!right->duplicate);
12355
934709f0
PW
12356 left->inserted = right->inserted;
12357 left->duplicate = right->duplicate;
b775012e 12358 left->needs_update = right->needs_update;
934709f0
PW
12359 left->target_info = right->target_info;
12360 right->inserted = left_inserted;
12361 right->duplicate = left_duplicate;
b775012e 12362 right->needs_update = left_needs_update;
934709f0
PW
12363 right->target_info = left_target_info;
12364}
12365
b775012e
LM
12366/* Force the re-insertion of the locations at ADDRESS. This is called
12367 once a new/deleted/modified duplicate location is found and we are evaluating
12368 conditions on the target's side. Such conditions need to be updated on
12369 the target. */
12370
12371static void
12372force_breakpoint_reinsertion (struct bp_location *bl)
12373{
12374 struct bp_location **locp = NULL, **loc2p;
12375 struct bp_location *loc;
12376 CORE_ADDR address = 0;
12377 int pspace_num;
12378
12379 address = bl->address;
12380 pspace_num = bl->pspace->num;
12381
12382 /* This is only meaningful if the target is
12383 evaluating conditions and if the user has
12384 opted for condition evaluation on the target's
12385 side. */
12386 if (gdb_evaluates_breakpoint_condition_p ()
12387 || !target_supports_evaluation_of_breakpoint_conditions ())
12388 return;
12389
12390 /* Flag all breakpoint locations with this address and
12391 the same program space as the location
12392 as "its condition has changed". We need to
12393 update the conditions on the target's side. */
12394 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12395 {
12396 loc = *loc2p;
12397
12398 if (!is_breakpoint (loc->owner)
12399 || pspace_num != loc->pspace->num)
12400 continue;
12401
12402 /* Flag the location appropriately. We use a different state to
12403 let everyone know that we already updated the set of locations
12404 with addr bl->address and program space bl->pspace. This is so
12405 we don't have to keep calling these functions just to mark locations
12406 that have already been marked. */
12407 loc->condition_changed = condition_updated;
12408
12409 /* Free the agent expression bytecode as well. We will compute
12410 it later on. */
12411 if (loc->cond_bytecode)
12412 {
12413 free_agent_expr (loc->cond_bytecode);
12414 loc->cond_bytecode = NULL;
12415 }
12416 }
12417}
12418
4cd9bd08 12419/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
12420 into the inferior, only remove already-inserted locations that no
12421 longer should be inserted. Functions that delete a breakpoint or
12422 breakpoints should pass false, so that deleting a breakpoint
12423 doesn't have the side effect of inserting the locations of other
12424 breakpoints that are marked not-inserted, but should_be_inserted
12425 returns true on them.
12426
12427 This behaviour is useful is situations close to tear-down -- e.g.,
12428 after an exec, while the target still has execution, but breakpoint
12429 shadows of the previous executable image should *NOT* be restored
12430 to the new image; or before detaching, where the target still has
12431 execution and wants to delete breakpoints from GDB's lists, and all
12432 breakpoints had already been removed from the inferior. */
12433
0d381245 12434static void
b60e7edf 12435update_global_location_list (int should_insert)
0d381245 12436{
74960c60 12437 struct breakpoint *b;
876fa593 12438 struct bp_location **locp, *loc;
f7545552 12439 struct cleanup *cleanups;
b775012e
LM
12440 /* Last breakpoint location address that was marked for update. */
12441 CORE_ADDR last_addr = 0;
12442 /* Last breakpoint location program space that was marked for update. */
12443 int last_pspace_num = -1;
f7545552 12444
2d134ed3
PA
12445 /* Used in the duplicates detection below. When iterating over all
12446 bp_locations, points to the first bp_location of a given address.
12447 Breakpoints and watchpoints of different types are never
12448 duplicates of each other. Keep one pointer for each type of
12449 breakpoint/watchpoint, so we only need to loop over all locations
12450 once. */
12451 struct bp_location *bp_loc_first; /* breakpoint */
12452 struct bp_location *wp_loc_first; /* hardware watchpoint */
12453 struct bp_location *awp_loc_first; /* access watchpoint */
12454 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 12455
4a64f543
MS
12456 /* Saved former bp_location array which we compare against the newly
12457 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
12458 struct bp_location **old_location, **old_locp;
12459 unsigned old_location_count;
12460
12461 old_location = bp_location;
12462 old_location_count = bp_location_count;
12463 bp_location = NULL;
12464 bp_location_count = 0;
12465 cleanups = make_cleanup (xfree, old_location);
0d381245 12466
74960c60 12467 ALL_BREAKPOINTS (b)
876fa593
JK
12468 for (loc = b->loc; loc; loc = loc->next)
12469 bp_location_count++;
12470
12471 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12472 locp = bp_location;
12473 ALL_BREAKPOINTS (b)
12474 for (loc = b->loc; loc; loc = loc->next)
12475 *locp++ = loc;
12476 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 12477 bp_location_compare);
876fa593
JK
12478
12479 bp_location_target_extensions_update ();
74960c60 12480
4a64f543
MS
12481 /* Identify bp_location instances that are no longer present in the
12482 new list, and therefore should be freed. Note that it's not
12483 necessary that those locations should be removed from inferior --
12484 if there's another location at the same address (previously
12485 marked as duplicate), we don't need to remove/insert the
12486 location.
876fa593 12487
4a64f543
MS
12488 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12489 and former bp_location array state respectively. */
876fa593
JK
12490
12491 locp = bp_location;
12492 for (old_locp = old_location; old_locp < old_location + old_location_count;
12493 old_locp++)
74960c60 12494 {
876fa593 12495 struct bp_location *old_loc = *old_locp;
c7d46a38 12496 struct bp_location **loc2p;
876fa593 12497
e5dd4106 12498 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12499 not, we have to free it. */
c7d46a38 12500 int found_object = 0;
20874c92
VP
12501 /* Tells if the location should remain inserted in the target. */
12502 int keep_in_target = 0;
12503 int removed = 0;
876fa593 12504
4a64f543
MS
12505 /* Skip LOCP entries which will definitely never be needed.
12506 Stop either at or being the one matching OLD_LOC. */
876fa593 12507 while (locp < bp_location + bp_location_count
c7d46a38 12508 && (*locp)->address < old_loc->address)
876fa593 12509 locp++;
c7d46a38
PA
12510
12511 for (loc2p = locp;
12512 (loc2p < bp_location + bp_location_count
12513 && (*loc2p)->address == old_loc->address);
12514 loc2p++)
12515 {
b775012e
LM
12516 /* Check if this is a new/duplicated location or a duplicated
12517 location that had its condition modified. If so, we want to send
12518 its condition to the target if evaluation of conditions is taking
12519 place there. */
12520 if ((*loc2p)->condition_changed == condition_modified
12521 && (last_addr != old_loc->address
12522 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12523 {
b775012e
LM
12524 force_breakpoint_reinsertion (*loc2p);
12525 last_pspace_num = old_loc->pspace->num;
c7d46a38 12526 }
b775012e
LM
12527
12528 if (*loc2p == old_loc)
12529 found_object = 1;
c7d46a38 12530 }
74960c60 12531
b775012e
LM
12532 /* We have already handled this address, update it so that we don't
12533 have to go through updates again. */
12534 last_addr = old_loc->address;
12535
12536 /* Target-side condition evaluation: Handle deleted locations. */
12537 if (!found_object)
12538 force_breakpoint_reinsertion (old_loc);
12539
4a64f543
MS
12540 /* If this location is no longer present, and inserted, look if
12541 there's maybe a new location at the same address. If so,
12542 mark that one inserted, and don't remove this one. This is
12543 needed so that we don't have a time window where a breakpoint
12544 at certain location is not inserted. */
74960c60 12545
876fa593 12546 if (old_loc->inserted)
0d381245 12547 {
4a64f543
MS
12548 /* If the location is inserted now, we might have to remove
12549 it. */
74960c60 12550
876fa593 12551 if (found_object && should_be_inserted (old_loc))
74960c60 12552 {
4a64f543
MS
12553 /* The location is still present in the location list,
12554 and still should be inserted. Don't do anything. */
20874c92 12555 keep_in_target = 1;
74960c60
VP
12556 }
12557 else
12558 {
b775012e
LM
12559 /* This location still exists, but it won't be kept in the
12560 target since it may have been disabled. We proceed to
12561 remove its target-side condition. */
12562
4a64f543
MS
12563 /* The location is either no longer present, or got
12564 disabled. See if there's another location at the
12565 same address, in which case we don't need to remove
12566 this one from the target. */
876fa593 12567
2bdf28a0 12568 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12569 if (breakpoint_address_is_meaningful (old_loc->owner))
12570 {
876fa593 12571 for (loc2p = locp;
c7d46a38
PA
12572 (loc2p < bp_location + bp_location_count
12573 && (*loc2p)->address == old_loc->address);
876fa593
JK
12574 loc2p++)
12575 {
12576 struct bp_location *loc2 = *loc2p;
12577
2d134ed3 12578 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12579 {
85d721b8
PA
12580 /* Read watchpoint locations are switched to
12581 access watchpoints, if the former are not
12582 supported, but the latter are. */
12583 if (is_hardware_watchpoint (old_loc->owner))
12584 {
12585 gdb_assert (is_hardware_watchpoint (loc2->owner));
12586 loc2->watchpoint_type = old_loc->watchpoint_type;
12587 }
12588
934709f0
PW
12589 /* loc2 is a duplicated location. We need to check
12590 if it should be inserted in case it will be
12591 unduplicated. */
12592 if (loc2 != old_loc
12593 && unduplicated_should_be_inserted (loc2))
c7d46a38 12594 {
934709f0 12595 swap_insertion (old_loc, loc2);
c7d46a38
PA
12596 keep_in_target = 1;
12597 break;
12598 }
876fa593
JK
12599 }
12600 }
12601 }
74960c60
VP
12602 }
12603
20874c92
VP
12604 if (!keep_in_target)
12605 {
876fa593 12606 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12607 {
4a64f543
MS
12608 /* This is just about all we can do. We could keep
12609 this location on the global list, and try to
12610 remove it next time, but there's no particular
12611 reason why we will succeed next time.
20874c92 12612
4a64f543
MS
12613 Note that at this point, old_loc->owner is still
12614 valid, as delete_breakpoint frees the breakpoint
12615 only after calling us. */
3e43a32a
MS
12616 printf_filtered (_("warning: Error removing "
12617 "breakpoint %d\n"),
876fa593 12618 old_loc->owner->number);
20874c92
VP
12619 }
12620 removed = 1;
12621 }
0d381245 12622 }
74960c60
VP
12623
12624 if (!found_object)
1c5cfe86 12625 {
db82e815
PA
12626 if (removed && non_stop
12627 && breakpoint_address_is_meaningful (old_loc->owner)
12628 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12629 {
db82e815
PA
12630 /* This location was removed from the target. In
12631 non-stop mode, a race condition is possible where
12632 we've removed a breakpoint, but stop events for that
12633 breakpoint are already queued and will arrive later.
12634 We apply an heuristic to be able to distinguish such
12635 SIGTRAPs from other random SIGTRAPs: we keep this
12636 breakpoint location for a bit, and will retire it
12637 after we see some number of events. The theory here
12638 is that reporting of events should, "on the average",
12639 be fair, so after a while we'll see events from all
12640 threads that have anything of interest, and no longer
12641 need to keep this breakpoint location around. We
12642 don't hold locations forever so to reduce chances of
12643 mistaking a non-breakpoint SIGTRAP for a breakpoint
12644 SIGTRAP.
12645
12646 The heuristic failing can be disastrous on
12647 decr_pc_after_break targets.
12648
12649 On decr_pc_after_break targets, like e.g., x86-linux,
12650 if we fail to recognize a late breakpoint SIGTRAP,
12651 because events_till_retirement has reached 0 too
12652 soon, we'll fail to do the PC adjustment, and report
12653 a random SIGTRAP to the user. When the user resumes
12654 the inferior, it will most likely immediately crash
2dec564e 12655 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12656 corrupted, because of being resumed e.g., in the
12657 middle of a multi-byte instruction, or skipped a
12658 one-byte instruction. This was actually seen happen
12659 on native x86-linux, and should be less rare on
12660 targets that do not support new thread events, like
12661 remote, due to the heuristic depending on
12662 thread_count.
12663
12664 Mistaking a random SIGTRAP for a breakpoint trap
12665 causes similar symptoms (PC adjustment applied when
12666 it shouldn't), but then again, playing with SIGTRAPs
12667 behind the debugger's back is asking for trouble.
12668
12669 Since hardware watchpoint traps are always
12670 distinguishable from other traps, so we don't need to
12671 apply keep hardware watchpoint moribund locations
12672 around. We simply always ignore hardware watchpoint
12673 traps we can no longer explain. */
12674
876fa593
JK
12675 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12676 old_loc->owner = NULL;
20874c92 12677
876fa593 12678 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12679 }
12680 else
f431efe5
PA
12681 {
12682 old_loc->owner = NULL;
12683 decref_bp_location (&old_loc);
12684 }
20874c92 12685 }
74960c60 12686 }
1c5cfe86 12687
348d480f
PA
12688 /* Rescan breakpoints at the same address and section, marking the
12689 first one as "first" and any others as "duplicates". This is so
12690 that the bpt instruction is only inserted once. If we have a
12691 permanent breakpoint at the same place as BPT, make that one the
12692 official one, and the rest as duplicates. Permanent breakpoints
12693 are sorted first for the same address.
12694
12695 Do the same for hardware watchpoints, but also considering the
12696 watchpoint's type (regular/access/read) and length. */
12697
12698 bp_loc_first = NULL;
12699 wp_loc_first = NULL;
12700 awp_loc_first = NULL;
12701 rwp_loc_first = NULL;
12702 ALL_BP_LOCATIONS (loc, locp)
12703 {
12704 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12705 non-NULL. */
348d480f 12706 struct bp_location **loc_first_p;
d3fbdd86 12707 b = loc->owner;
348d480f 12708
6f380991 12709 if (!unduplicated_should_be_inserted (loc)
348d480f 12710 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12711 /* Don't detect duplicate for tracepoint locations because they are
12712 never duplicated. See the comments in field `duplicate' of
12713 `struct bp_location'. */
348d480f 12714 || is_tracepoint (b))
b775012e
LM
12715 {
12716 /* Clear the condition modification flag. */
12717 loc->condition_changed = condition_unchanged;
12718 continue;
12719 }
348d480f
PA
12720
12721 /* Permanent breakpoint should always be inserted. */
12722 if (b->enable_state == bp_permanent && ! loc->inserted)
12723 internal_error (__FILE__, __LINE__,
12724 _("allegedly permanent breakpoint is not "
12725 "actually inserted"));
12726
12727 if (b->type == bp_hardware_watchpoint)
12728 loc_first_p = &wp_loc_first;
12729 else if (b->type == bp_read_watchpoint)
12730 loc_first_p = &rwp_loc_first;
12731 else if (b->type == bp_access_watchpoint)
12732 loc_first_p = &awp_loc_first;
12733 else
12734 loc_first_p = &bp_loc_first;
12735
12736 if (*loc_first_p == NULL
12737 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12738 || !breakpoint_locations_match (loc, *loc_first_p))
12739 {
12740 *loc_first_p = loc;
12741 loc->duplicate = 0;
b775012e
LM
12742
12743 if (is_breakpoint (loc->owner) && loc->condition_changed)
12744 {
12745 loc->needs_update = 1;
12746 /* Clear the condition modification flag. */
12747 loc->condition_changed = condition_unchanged;
12748 }
348d480f
PA
12749 continue;
12750 }
12751
934709f0
PW
12752
12753 /* This and the above ensure the invariant that the first location
12754 is not duplicated, and is the inserted one.
12755 All following are marked as duplicated, and are not inserted. */
12756 if (loc->inserted)
12757 swap_insertion (loc, *loc_first_p);
348d480f
PA
12758 loc->duplicate = 1;
12759
b775012e
LM
12760 /* Clear the condition modification flag. */
12761 loc->condition_changed = condition_unchanged;
12762
348d480f
PA
12763 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12764 && b->enable_state != bp_permanent)
12765 internal_error (__FILE__, __LINE__,
12766 _("another breakpoint was inserted on top of "
12767 "a permanent breakpoint"));
12768 }
12769
b775012e 12770 if (breakpoints_always_inserted_mode ()
348d480f 12771 && (have_live_inferiors ()
f5656ead 12772 || (gdbarch_has_global_breakpoints (target_gdbarch ()))))
b775012e
LM
12773 {
12774 if (should_insert)
12775 insert_breakpoint_locations ();
12776 else
12777 {
12778 /* Though should_insert is false, we may need to update conditions
12779 on the target's side if it is evaluating such conditions. We
12780 only update conditions for locations that are marked
12781 "needs_update". */
12782 update_inserted_breakpoint_locations ();
12783 }
12784 }
348d480f 12785
1e4d1764
YQ
12786 if (should_insert)
12787 download_tracepoint_locations ();
12788
348d480f
PA
12789 do_cleanups (cleanups);
12790}
12791
12792void
12793breakpoint_retire_moribund (void)
12794{
12795 struct bp_location *loc;
12796 int ix;
12797
12798 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12799 if (--(loc->events_till_retirement) == 0)
12800 {
12801 decref_bp_location (&loc);
12802 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12803 --ix;
12804 }
12805}
12806
12807static void
12808update_global_location_list_nothrow (int inserting)
12809{
bfd189b1 12810 volatile struct gdb_exception e;
348d480f
PA
12811
12812 TRY_CATCH (e, RETURN_MASK_ERROR)
12813 update_global_location_list (inserting);
12814}
12815
12816/* Clear BKP from a BPS. */
12817
12818static void
12819bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12820{
12821 bpstat bs;
12822
12823 for (bs = bps; bs; bs = bs->next)
12824 if (bs->breakpoint_at == bpt)
12825 {
12826 bs->breakpoint_at = NULL;
12827 bs->old_val = NULL;
12828 /* bs->commands will be freed later. */
12829 }
12830}
12831
12832/* Callback for iterate_over_threads. */
12833static int
12834bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12835{
12836 struct breakpoint *bpt = data;
12837
12838 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12839 return 0;
12840}
12841
12842/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12843 callbacks. */
12844
12845static void
12846say_where (struct breakpoint *b)
12847{
12848 struct value_print_options opts;
12849
12850 get_user_print_options (&opts);
12851
12852 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12853 single string. */
12854 if (b->loc == NULL)
12855 {
12856 printf_filtered (_(" (%s) pending."), b->addr_string);
12857 }
12858 else
12859 {
2f202fde 12860 if (opts.addressprint || b->loc->symtab == NULL)
348d480f
PA
12861 {
12862 printf_filtered (" at ");
12863 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12864 gdb_stdout);
12865 }
2f202fde 12866 if (b->loc->symtab != NULL)
f8eba3c6
TT
12867 {
12868 /* If there is a single location, we can print the location
12869 more nicely. */
12870 if (b->loc->next == NULL)
12871 printf_filtered (": file %s, line %d.",
05cba821
JK
12872 symtab_to_filename_for_display (b->loc->symtab),
12873 b->loc->line_number);
f8eba3c6
TT
12874 else
12875 /* This is not ideal, but each location may have a
12876 different file name, and this at least reflects the
12877 real situation somewhat. */
12878 printf_filtered (": %s.", b->addr_string);
12879 }
348d480f
PA
12880
12881 if (b->loc->next)
12882 {
12883 struct bp_location *loc = b->loc;
12884 int n = 0;
12885 for (; loc; loc = loc->next)
12886 ++n;
12887 printf_filtered (" (%d locations)", n);
12888 }
12889 }
12890}
12891
348d480f
PA
12892/* Default bp_location_ops methods. */
12893
12894static void
12895bp_location_dtor (struct bp_location *self)
12896{
12897 xfree (self->cond);
b775012e
LM
12898 if (self->cond_bytecode)
12899 free_agent_expr (self->cond_bytecode);
348d480f 12900 xfree (self->function_name);
8b4f3082
PA
12901
12902 VEC_free (agent_expr_p, self->target_info.conditions);
12903 VEC_free (agent_expr_p, self->target_info.tcommands);
348d480f
PA
12904}
12905
12906static const struct bp_location_ops bp_location_ops =
12907{
12908 bp_location_dtor
12909};
12910
2060206e
PA
12911/* Default breakpoint_ops methods all breakpoint_ops ultimately
12912 inherit from. */
348d480f 12913
2060206e
PA
12914static void
12915base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12916{
12917 decref_counted_command_line (&self->commands);
12918 xfree (self->cond_string);
fb81d016 12919 xfree (self->extra_string);
348d480f 12920 xfree (self->addr_string);
f8eba3c6 12921 xfree (self->filter);
348d480f 12922 xfree (self->addr_string_range_end);
348d480f
PA
12923}
12924
2060206e
PA
12925static struct bp_location *
12926base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12927{
12928 struct bp_location *loc;
12929
12930 loc = XNEW (struct bp_location);
12931 init_bp_location (loc, &bp_location_ops, self);
12932 return loc;
12933}
12934
2060206e
PA
12935static void
12936base_breakpoint_re_set (struct breakpoint *b)
12937{
12938 /* Nothing to re-set. */
12939}
12940
12941#define internal_error_pure_virtual_called() \
12942 gdb_assert_not_reached ("pure virtual function called")
12943
12944static int
12945base_breakpoint_insert_location (struct bp_location *bl)
12946{
12947 internal_error_pure_virtual_called ();
12948}
12949
12950static int
12951base_breakpoint_remove_location (struct bp_location *bl)
12952{
12953 internal_error_pure_virtual_called ();
12954}
12955
12956static int
12957base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12958 struct address_space *aspace,
09ac7c10
TT
12959 CORE_ADDR bp_addr,
12960 const struct target_waitstatus *ws)
2060206e
PA
12961{
12962 internal_error_pure_virtual_called ();
12963}
12964
12965static void
12966base_breakpoint_check_status (bpstat bs)
12967{
12968 /* Always stop. */
12969}
12970
12971/* A "works_in_software_mode" breakpoint_ops method that just internal
12972 errors. */
12973
12974static int
12975base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12976{
12977 internal_error_pure_virtual_called ();
12978}
12979
12980/* A "resources_needed" breakpoint_ops method that just internal
12981 errors. */
12982
12983static int
12984base_breakpoint_resources_needed (const struct bp_location *bl)
12985{
12986 internal_error_pure_virtual_called ();
12987}
12988
12989static enum print_stop_action
12990base_breakpoint_print_it (bpstat bs)
12991{
12992 internal_error_pure_virtual_called ();
12993}
12994
12995static void
12996base_breakpoint_print_one_detail (const struct breakpoint *self,
12997 struct ui_out *uiout)
12998{
12999 /* nothing */
13000}
13001
13002static void
13003base_breakpoint_print_mention (struct breakpoint *b)
13004{
13005 internal_error_pure_virtual_called ();
13006}
13007
13008static void
13009base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13010{
13011 internal_error_pure_virtual_called ();
13012}
13013
983af33b
SDJ
13014static void
13015base_breakpoint_create_sals_from_address (char **arg,
13016 struct linespec_result *canonical,
13017 enum bptype type_wanted,
13018 char *addr_start,
13019 char **copy_arg)
13020{
13021 internal_error_pure_virtual_called ();
13022}
13023
13024static void
13025base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13026 struct linespec_result *c,
983af33b 13027 char *cond_string,
e7e0cddf 13028 char *extra_string,
983af33b
SDJ
13029 enum bptype type_wanted,
13030 enum bpdisp disposition,
13031 int thread,
13032 int task, int ignore_count,
13033 const struct breakpoint_ops *o,
13034 int from_tty, int enabled,
44f238bb 13035 int internal, unsigned flags)
983af33b
SDJ
13036{
13037 internal_error_pure_virtual_called ();
13038}
13039
13040static void
13041base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13042 struct symtabs_and_lines *sals)
13043{
13044 internal_error_pure_virtual_called ();
13045}
13046
ab04a2af
TT
13047/* The default 'explains_signal' method. */
13048
47591c29 13049static int
427cd150 13050base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
ab04a2af 13051{
47591c29 13052 return 1;
ab04a2af
TT
13053}
13054
9d6e6e84
HZ
13055/* The default "after_condition_true" method. */
13056
13057static void
13058base_breakpoint_after_condition_true (struct bpstats *bs)
13059{
13060 /* Nothing to do. */
13061}
13062
ab04a2af 13063struct breakpoint_ops base_breakpoint_ops =
2060206e
PA
13064{
13065 base_breakpoint_dtor,
13066 base_breakpoint_allocate_location,
13067 base_breakpoint_re_set,
13068 base_breakpoint_insert_location,
13069 base_breakpoint_remove_location,
13070 base_breakpoint_breakpoint_hit,
13071 base_breakpoint_check_status,
13072 base_breakpoint_resources_needed,
13073 base_breakpoint_works_in_software_mode,
13074 base_breakpoint_print_it,
13075 NULL,
13076 base_breakpoint_print_one_detail,
13077 base_breakpoint_print_mention,
983af33b
SDJ
13078 base_breakpoint_print_recreate,
13079 base_breakpoint_create_sals_from_address,
13080 base_breakpoint_create_breakpoints_sal,
13081 base_breakpoint_decode_linespec,
9d6e6e84
HZ
13082 base_breakpoint_explains_signal,
13083 base_breakpoint_after_condition_true,
2060206e
PA
13084};
13085
13086/* Default breakpoint_ops methods. */
13087
13088static void
348d480f
PA
13089bkpt_re_set (struct breakpoint *b)
13090{
06edf0c0
PA
13091 /* FIXME: is this still reachable? */
13092 if (b->addr_string == NULL)
13093 {
13094 /* Anything without a string can't be re-set. */
348d480f 13095 delete_breakpoint (b);
06edf0c0 13096 return;
348d480f 13097 }
06edf0c0
PA
13098
13099 breakpoint_re_set_default (b);
348d480f
PA
13100}
13101
2060206e 13102static int
348d480f
PA
13103bkpt_insert_location (struct bp_location *bl)
13104{
13105 if (bl->loc_type == bp_loc_hardware_breakpoint)
13106 return target_insert_hw_breakpoint (bl->gdbarch,
13107 &bl->target_info);
13108 else
13109 return target_insert_breakpoint (bl->gdbarch,
13110 &bl->target_info);
13111}
13112
2060206e 13113static int
348d480f
PA
13114bkpt_remove_location (struct bp_location *bl)
13115{
13116 if (bl->loc_type == bp_loc_hardware_breakpoint)
13117 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13118 else
13119 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
13120}
13121
2060206e 13122static int
348d480f 13123bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13124 struct address_space *aspace, CORE_ADDR bp_addr,
13125 const struct target_waitstatus *ws)
348d480f 13126{
09ac7c10 13127 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 13128 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
13129 return 0;
13130
348d480f
PA
13131 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13132 aspace, bp_addr))
13133 return 0;
13134
13135 if (overlay_debugging /* unmapped overlay section */
13136 && section_is_overlay (bl->section)
13137 && !section_is_mapped (bl->section))
13138 return 0;
13139
13140 return 1;
13141}
13142
2060206e 13143static int
348d480f
PA
13144bkpt_resources_needed (const struct bp_location *bl)
13145{
13146 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13147
13148 return 1;
13149}
13150
2060206e 13151static enum print_stop_action
348d480f
PA
13152bkpt_print_it (bpstat bs)
13153{
348d480f
PA
13154 struct breakpoint *b;
13155 const struct bp_location *bl;
001c8c33 13156 int bp_temp;
79a45e25 13157 struct ui_out *uiout = current_uiout;
348d480f
PA
13158
13159 gdb_assert (bs->bp_location_at != NULL);
13160
13161 bl = bs->bp_location_at;
13162 b = bs->breakpoint_at;
13163
001c8c33
PA
13164 bp_temp = b->disposition == disp_del;
13165 if (bl->address != bl->requested_address)
13166 breakpoint_adjustment_warning (bl->requested_address,
13167 bl->address,
13168 b->number, 1);
13169 annotate_breakpoint (b->number);
13170 if (bp_temp)
13171 ui_out_text (uiout, "\nTemporary breakpoint ");
13172 else
13173 ui_out_text (uiout, "\nBreakpoint ");
13174 if (ui_out_is_mi_like_p (uiout))
348d480f 13175 {
001c8c33
PA
13176 ui_out_field_string (uiout, "reason",
13177 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13178 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 13179 }
001c8c33
PA
13180 ui_out_field_int (uiout, "bkptno", b->number);
13181 ui_out_text (uiout, ", ");
06edf0c0 13182
001c8c33 13183 return PRINT_SRC_AND_LOC;
06edf0c0
PA
13184}
13185
2060206e 13186static void
06edf0c0
PA
13187bkpt_print_mention (struct breakpoint *b)
13188{
79a45e25 13189 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
13190 return;
13191
13192 switch (b->type)
13193 {
13194 case bp_breakpoint:
13195 case bp_gnu_ifunc_resolver:
13196 if (b->disposition == disp_del)
13197 printf_filtered (_("Temporary breakpoint"));
13198 else
13199 printf_filtered (_("Breakpoint"));
13200 printf_filtered (_(" %d"), b->number);
13201 if (b->type == bp_gnu_ifunc_resolver)
13202 printf_filtered (_(" at gnu-indirect-function resolver"));
13203 break;
13204 case bp_hardware_breakpoint:
13205 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13206 break;
e7e0cddf
SS
13207 case bp_dprintf:
13208 printf_filtered (_("Dprintf %d"), b->number);
13209 break;
06edf0c0
PA
13210 }
13211
13212 say_where (b);
13213}
13214
2060206e 13215static void
06edf0c0
PA
13216bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13217{
13218 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13219 fprintf_unfiltered (fp, "tbreak");
13220 else if (tp->type == bp_breakpoint)
13221 fprintf_unfiltered (fp, "break");
13222 else if (tp->type == bp_hardware_breakpoint
13223 && tp->disposition == disp_del)
13224 fprintf_unfiltered (fp, "thbreak");
13225 else if (tp->type == bp_hardware_breakpoint)
13226 fprintf_unfiltered (fp, "hbreak");
13227 else
13228 internal_error (__FILE__, __LINE__,
13229 _("unhandled breakpoint type %d"), (int) tp->type);
13230
2060206e 13231 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 13232 print_recreate_thread (tp, fp);
06edf0c0
PA
13233}
13234
983af33b
SDJ
13235static void
13236bkpt_create_sals_from_address (char **arg,
13237 struct linespec_result *canonical,
13238 enum bptype type_wanted,
13239 char *addr_start, char **copy_arg)
13240{
13241 create_sals_from_address_default (arg, canonical, type_wanted,
13242 addr_start, copy_arg);
13243}
13244
13245static void
13246bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13247 struct linespec_result *canonical,
983af33b 13248 char *cond_string,
e7e0cddf 13249 char *extra_string,
983af33b
SDJ
13250 enum bptype type_wanted,
13251 enum bpdisp disposition,
13252 int thread,
13253 int task, int ignore_count,
13254 const struct breakpoint_ops *ops,
13255 int from_tty, int enabled,
44f238bb 13256 int internal, unsigned flags)
983af33b 13257{
023fa29b 13258 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13259 cond_string, extra_string,
13260 type_wanted,
983af33b
SDJ
13261 disposition, thread, task,
13262 ignore_count, ops, from_tty,
44f238bb 13263 enabled, internal, flags);
983af33b
SDJ
13264}
13265
13266static void
13267bkpt_decode_linespec (struct breakpoint *b, char **s,
13268 struct symtabs_and_lines *sals)
13269{
13270 decode_linespec_default (b, s, sals);
13271}
13272
06edf0c0
PA
13273/* Virtual table for internal breakpoints. */
13274
13275static void
13276internal_bkpt_re_set (struct breakpoint *b)
13277{
13278 switch (b->type)
13279 {
13280 /* Delete overlay event and longjmp master breakpoints; they
13281 will be reset later by breakpoint_re_set. */
13282 case bp_overlay_event:
13283 case bp_longjmp_master:
13284 case bp_std_terminate_master:
13285 case bp_exception_master:
13286 delete_breakpoint (b);
13287 break;
13288
13289 /* This breakpoint is special, it's set up when the inferior
13290 starts and we really don't want to touch it. */
13291 case bp_shlib_event:
13292
13293 /* Like bp_shlib_event, this breakpoint type is special. Once
13294 it is set up, we do not want to touch it. */
13295 case bp_thread_event:
13296 break;
13297 }
13298}
13299
13300static void
13301internal_bkpt_check_status (bpstat bs)
13302{
a9b3a50f
PA
13303 if (bs->breakpoint_at->type == bp_shlib_event)
13304 {
13305 /* If requested, stop when the dynamic linker notifies GDB of
13306 events. This allows the user to get control and place
13307 breakpoints in initializer routines for dynamically loaded
13308 objects (among other things). */
13309 bs->stop = stop_on_solib_events;
13310 bs->print = stop_on_solib_events;
13311 }
13312 else
13313 bs->stop = 0;
06edf0c0
PA
13314}
13315
13316static enum print_stop_action
13317internal_bkpt_print_it (bpstat bs)
13318{
06edf0c0 13319 struct breakpoint *b;
06edf0c0 13320
06edf0c0
PA
13321 b = bs->breakpoint_at;
13322
06edf0c0
PA
13323 switch (b->type)
13324 {
348d480f
PA
13325 case bp_shlib_event:
13326 /* Did we stop because the user set the stop_on_solib_events
13327 variable? (If so, we report this as a generic, "Stopped due
13328 to shlib event" message.) */
edcc5120 13329 print_solib_event (0);
348d480f
PA
13330 break;
13331
13332 case bp_thread_event:
13333 /* Not sure how we will get here.
13334 GDB should not stop for these breakpoints. */
13335 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13336 break;
13337
13338 case bp_overlay_event:
13339 /* By analogy with the thread event, GDB should not stop for these. */
13340 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
13341 break;
13342
13343 case bp_longjmp_master:
13344 /* These should never be enabled. */
13345 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
13346 break;
13347
13348 case bp_std_terminate_master:
13349 /* These should never be enabled. */
13350 printf_filtered (_("std::terminate Master Breakpoint: "
13351 "gdb should not stop!\n"));
348d480f
PA
13352 break;
13353
13354 case bp_exception_master:
13355 /* These should never be enabled. */
13356 printf_filtered (_("Exception Master Breakpoint: "
13357 "gdb should not stop!\n"));
06edf0c0
PA
13358 break;
13359 }
13360
001c8c33 13361 return PRINT_NOTHING;
06edf0c0
PA
13362}
13363
13364static void
13365internal_bkpt_print_mention (struct breakpoint *b)
13366{
13367 /* Nothing to mention. These breakpoints are internal. */
13368}
13369
06edf0c0
PA
13370/* Virtual table for momentary breakpoints */
13371
13372static void
13373momentary_bkpt_re_set (struct breakpoint *b)
13374{
13375 /* Keep temporary breakpoints, which can be encountered when we step
4d1eb6b4 13376 over a dlopen call and solib_add is resetting the breakpoints.
06edf0c0
PA
13377 Otherwise these should have been blown away via the cleanup chain
13378 or by breakpoint_init_inferior when we rerun the executable. */
13379}
13380
13381static void
13382momentary_bkpt_check_status (bpstat bs)
13383{
13384 /* Nothing. The point of these breakpoints is causing a stop. */
13385}
13386
13387static enum print_stop_action
13388momentary_bkpt_print_it (bpstat bs)
13389{
79a45e25
PA
13390 struct ui_out *uiout = current_uiout;
13391
001c8c33 13392 if (ui_out_is_mi_like_p (uiout))
06edf0c0 13393 {
001c8c33 13394 struct breakpoint *b = bs->breakpoint_at;
348d480f 13395
001c8c33
PA
13396 switch (b->type)
13397 {
13398 case bp_finish:
13399 ui_out_field_string
13400 (uiout, "reason",
13401 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13402 break;
348d480f 13403
001c8c33
PA
13404 case bp_until:
13405 ui_out_field_string
13406 (uiout, "reason",
13407 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13408 break;
13409 }
348d480f
PA
13410 }
13411
001c8c33 13412 return PRINT_UNKNOWN;
348d480f
PA
13413}
13414
06edf0c0
PA
13415static void
13416momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 13417{
06edf0c0 13418 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
13419}
13420
e2e4d78b
JK
13421/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13422
13423 It gets cleared already on the removal of the first one of such placed
13424 breakpoints. This is OK as they get all removed altogether. */
13425
13426static void
13427longjmp_bkpt_dtor (struct breakpoint *self)
13428{
13429 struct thread_info *tp = find_thread_id (self->thread);
13430
13431 if (tp)
13432 tp->initiating_frame = null_frame_id;
13433
13434 momentary_breakpoint_ops.dtor (self);
13435}
13436
55aa24fb
SDJ
13437/* Specific methods for probe breakpoints. */
13438
13439static int
13440bkpt_probe_insert_location (struct bp_location *bl)
13441{
13442 int v = bkpt_insert_location (bl);
13443
13444 if (v == 0)
13445 {
13446 /* The insertion was successful, now let's set the probe's semaphore
13447 if needed. */
729662a5
TT
13448 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13449 bl->probe.objfile,
13450 bl->gdbarch);
55aa24fb
SDJ
13451 }
13452
13453 return v;
13454}
13455
13456static int
13457bkpt_probe_remove_location (struct bp_location *bl)
13458{
13459 /* Let's clear the semaphore before removing the location. */
729662a5
TT
13460 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13461 bl->probe.objfile,
13462 bl->gdbarch);
55aa24fb
SDJ
13463
13464 return bkpt_remove_location (bl);
13465}
13466
13467static void
13468bkpt_probe_create_sals_from_address (char **arg,
13469 struct linespec_result *canonical,
13470 enum bptype type_wanted,
13471 char *addr_start, char **copy_arg)
13472{
13473 struct linespec_sals lsal;
13474
13475 lsal.sals = parse_probes (arg, canonical);
13476
13477 *copy_arg = xstrdup (canonical->addr_string);
13478 lsal.canonical = xstrdup (*copy_arg);
13479
13480 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13481}
13482
13483static void
13484bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13485 struct symtabs_and_lines *sals)
13486{
13487 *sals = parse_probes (s, NULL);
13488 if (!sals->sals)
13489 error (_("probe not found"));
13490}
13491
348d480f 13492/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 13493
348d480f
PA
13494static void
13495tracepoint_re_set (struct breakpoint *b)
13496{
13497 breakpoint_re_set_default (b);
13498}
876fa593 13499
348d480f
PA
13500static int
13501tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
13502 struct address_space *aspace, CORE_ADDR bp_addr,
13503 const struct target_waitstatus *ws)
348d480f
PA
13504{
13505 /* By definition, the inferior does not report stops at
13506 tracepoints. */
13507 return 0;
74960c60
VP
13508}
13509
13510static void
348d480f
PA
13511tracepoint_print_one_detail (const struct breakpoint *self,
13512 struct ui_out *uiout)
74960c60 13513{
d9b3f62e
PA
13514 struct tracepoint *tp = (struct tracepoint *) self;
13515 if (tp->static_trace_marker_id)
348d480f
PA
13516 {
13517 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13518
348d480f
PA
13519 ui_out_text (uiout, "\tmarker id is ");
13520 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13521 tp->static_trace_marker_id);
348d480f
PA
13522 ui_out_text (uiout, "\n");
13523 }
0d381245
VP
13524}
13525
a474d7c2 13526static void
348d480f 13527tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13528{
79a45e25 13529 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13530 return;
cc59ec59 13531
348d480f
PA
13532 switch (b->type)
13533 {
13534 case bp_tracepoint:
13535 printf_filtered (_("Tracepoint"));
13536 printf_filtered (_(" %d"), b->number);
13537 break;
13538 case bp_fast_tracepoint:
13539 printf_filtered (_("Fast tracepoint"));
13540 printf_filtered (_(" %d"), b->number);
13541 break;
13542 case bp_static_tracepoint:
13543 printf_filtered (_("Static tracepoint"));
13544 printf_filtered (_(" %d"), b->number);
13545 break;
13546 default:
13547 internal_error (__FILE__, __LINE__,
13548 _("unhandled tracepoint type %d"), (int) b->type);
13549 }
13550
13551 say_where (b);
a474d7c2
PA
13552}
13553
348d480f 13554static void
d9b3f62e 13555tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13556{
d9b3f62e
PA
13557 struct tracepoint *tp = (struct tracepoint *) self;
13558
13559 if (self->type == bp_fast_tracepoint)
348d480f 13560 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13561 if (self->type == bp_static_tracepoint)
348d480f 13562 fprintf_unfiltered (fp, "strace");
d9b3f62e 13563 else if (self->type == bp_tracepoint)
348d480f
PA
13564 fprintf_unfiltered (fp, "trace");
13565 else
13566 internal_error (__FILE__, __LINE__,
d9b3f62e 13567 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13568
d9b3f62e
PA
13569 fprintf_unfiltered (fp, " %s", self->addr_string);
13570 print_recreate_thread (self, fp);
13571
13572 if (tp->pass_count)
13573 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13574}
13575
983af33b
SDJ
13576static void
13577tracepoint_create_sals_from_address (char **arg,
13578 struct linespec_result *canonical,
13579 enum bptype type_wanted,
13580 char *addr_start, char **copy_arg)
13581{
13582 create_sals_from_address_default (arg, canonical, type_wanted,
13583 addr_start, copy_arg);
13584}
13585
13586static void
13587tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13588 struct linespec_result *canonical,
983af33b 13589 char *cond_string,
e7e0cddf 13590 char *extra_string,
983af33b
SDJ
13591 enum bptype type_wanted,
13592 enum bpdisp disposition,
13593 int thread,
13594 int task, int ignore_count,
13595 const struct breakpoint_ops *ops,
13596 int from_tty, int enabled,
44f238bb 13597 int internal, unsigned flags)
983af33b 13598{
023fa29b 13599 create_breakpoints_sal_default (gdbarch, canonical,
e7e0cddf
SS
13600 cond_string, extra_string,
13601 type_wanted,
983af33b
SDJ
13602 disposition, thread, task,
13603 ignore_count, ops, from_tty,
44f238bb 13604 enabled, internal, flags);
983af33b
SDJ
13605}
13606
13607static void
13608tracepoint_decode_linespec (struct breakpoint *b, char **s,
13609 struct symtabs_and_lines *sals)
13610{
13611 decode_linespec_default (b, s, sals);
13612}
13613
2060206e 13614struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13615
55aa24fb
SDJ
13616/* The breakpoint_ops structure to be use on tracepoints placed in a
13617 static probe. */
13618
13619static void
13620tracepoint_probe_create_sals_from_address (char **arg,
13621 struct linespec_result *canonical,
13622 enum bptype type_wanted,
13623 char *addr_start, char **copy_arg)
13624{
13625 /* We use the same method for breakpoint on probes. */
13626 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13627 addr_start, copy_arg);
13628}
13629
13630static void
13631tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13632 struct symtabs_and_lines *sals)
13633{
13634 /* We use the same method for breakpoint on probes. */
13635 bkpt_probe_decode_linespec (b, s, sals);
13636}
13637
13638static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13639
5c2b4418
HZ
13640/* Dprintf breakpoint_ops methods. */
13641
13642static void
13643dprintf_re_set (struct breakpoint *b)
13644{
13645 breakpoint_re_set_default (b);
13646
13647 /* This breakpoint could have been pending, and be resolved now, and
13648 if so, we should now have the extra string. If we don't, the
13649 dprintf was malformed when created, but we couldn't tell because
13650 we can't extract the extra string until the location is
13651 resolved. */
13652 if (b->loc != NULL && b->extra_string == NULL)
13653 error (_("Format string required"));
13654
13655 /* 1 - connect to target 1, that can run breakpoint commands.
13656 2 - create a dprintf, which resolves fine.
13657 3 - disconnect from target 1
13658 4 - connect to target 2, that can NOT run breakpoint commands.
13659
13660 After steps #3/#4, you'll want the dprintf command list to
13661 be updated, because target 1 and 2 may well return different
13662 answers for target_can_run_breakpoint_commands().
13663 Given absence of finer grained resetting, we get to do
13664 it all the time. */
13665 if (b->extra_string != NULL)
13666 update_dprintf_command_list (b);
13667}
13668
2d9442cc
HZ
13669/* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13670
13671static void
13672dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13673{
13674 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13675 tp->extra_string);
13676 print_recreate_thread (tp, fp);
13677}
13678
9d6e6e84
HZ
13679/* Implement the "after_condition_true" breakpoint_ops method for
13680 dprintf.
13681
13682 dprintf's are implemented with regular commands in their command
13683 list, but we run the commands here instead of before presenting the
13684 stop to the user, as dprintf's don't actually cause a stop. This
13685 also makes it so that the commands of multiple dprintfs at the same
13686 address are all handled. */
13687
13688static void
13689dprintf_after_condition_true (struct bpstats *bs)
13690{
13691 struct cleanup *old_chain;
13692 struct bpstats tmp_bs = { NULL };
13693 struct bpstats *tmp_bs_p = &tmp_bs;
13694
13695 /* dprintf's never cause a stop. This wasn't set in the
13696 check_status hook instead because that would make the dprintf's
13697 condition not be evaluated. */
13698 bs->stop = 0;
13699
13700 /* Run the command list here. Take ownership of it instead of
13701 copying. We never want these commands to run later in
13702 bpstat_do_actions, if a breakpoint that causes a stop happens to
13703 be set at same address as this dprintf, or even if running the
13704 commands here throws. */
13705 tmp_bs.commands = bs->commands;
13706 bs->commands = NULL;
13707 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13708
13709 bpstat_do_actions_1 (&tmp_bs_p);
13710
13711 /* 'tmp_bs.commands' will usually be NULL by now, but
13712 bpstat_do_actions_1 may return early without processing the whole
13713 list. */
13714 do_cleanups (old_chain);
13715}
13716
983af33b
SDJ
13717/* The breakpoint_ops structure to be used on static tracepoints with
13718 markers (`-m'). */
13719
13720static void
13721strace_marker_create_sals_from_address (char **arg,
13722 struct linespec_result *canonical,
13723 enum bptype type_wanted,
13724 char *addr_start, char **copy_arg)
13725{
13726 struct linespec_sals lsal;
13727
13728 lsal.sals = decode_static_tracepoint_spec (arg);
13729
13730 *copy_arg = savestring (addr_start, *arg - addr_start);
13731
13732 canonical->addr_string = xstrdup (*copy_arg);
13733 lsal.canonical = xstrdup (*copy_arg);
13734 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13735}
13736
13737static void
13738strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13739 struct linespec_result *canonical,
983af33b 13740 char *cond_string,
e7e0cddf 13741 char *extra_string,
983af33b
SDJ
13742 enum bptype type_wanted,
13743 enum bpdisp disposition,
13744 int thread,
13745 int task, int ignore_count,
13746 const struct breakpoint_ops *ops,
13747 int from_tty, int enabled,
44f238bb 13748 int internal, unsigned flags)
983af33b
SDJ
13749{
13750 int i;
52d361e1
YQ
13751 struct linespec_sals *lsal = VEC_index (linespec_sals,
13752 canonical->sals, 0);
983af33b
SDJ
13753
13754 /* If the user is creating a static tracepoint by marker id
13755 (strace -m MARKER_ID), then store the sals index, so that
13756 breakpoint_re_set can try to match up which of the newly
13757 found markers corresponds to this one, and, don't try to
13758 expand multiple locations for each sal, given than SALS
13759 already should contain all sals for MARKER_ID. */
13760
13761 for (i = 0; i < lsal->sals.nelts; ++i)
13762 {
13763 struct symtabs_and_lines expanded;
13764 struct tracepoint *tp;
13765 struct cleanup *old_chain;
13766 char *addr_string;
13767
13768 expanded.nelts = 1;
13769 expanded.sals = &lsal->sals.sals[i];
13770
13771 addr_string = xstrdup (canonical->addr_string);
13772 old_chain = make_cleanup (xfree, addr_string);
13773
13774 tp = XCNEW (struct tracepoint);
13775 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13776 addr_string, NULL,
e7e0cddf
SS
13777 cond_string, extra_string,
13778 type_wanted, disposition,
983af33b 13779 thread, task, ignore_count, ops,
44f238bb 13780 from_tty, enabled, internal, flags,
983af33b
SDJ
13781 canonical->special_display);
13782 /* Given that its possible to have multiple markers with
13783 the same string id, if the user is creating a static
13784 tracepoint by marker id ("strace -m MARKER_ID"), then
13785 store the sals index, so that breakpoint_re_set can
13786 try to match up which of the newly found markers
13787 corresponds to this one */
13788 tp->static_trace_marker_id_idx = i;
13789
13790 install_breakpoint (internal, &tp->base, 0);
13791
13792 discard_cleanups (old_chain);
13793 }
13794}
13795
13796static void
13797strace_marker_decode_linespec (struct breakpoint *b, char **s,
13798 struct symtabs_and_lines *sals)
13799{
13800 struct tracepoint *tp = (struct tracepoint *) b;
13801
13802 *sals = decode_static_tracepoint_spec (s);
13803 if (sals->nelts > tp->static_trace_marker_id_idx)
13804 {
13805 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13806 sals->nelts = 1;
13807 }
13808 else
13809 error (_("marker %s not found"), tp->static_trace_marker_id);
13810}
13811
13812static struct breakpoint_ops strace_marker_breakpoint_ops;
13813
13814static int
13815strace_marker_p (struct breakpoint *b)
13816{
13817 return b->ops == &strace_marker_breakpoint_ops;
13818}
13819
53a5351d 13820/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13821 structures. */
c906108c
SS
13822
13823void
fba45db2 13824delete_breakpoint (struct breakpoint *bpt)
c906108c 13825{
52f0bd74 13826 struct breakpoint *b;
c906108c 13827
8a3fe4f8 13828 gdb_assert (bpt != NULL);
c906108c 13829
4a64f543
MS
13830 /* Has this bp already been deleted? This can happen because
13831 multiple lists can hold pointers to bp's. bpstat lists are
13832 especial culprits.
13833
13834 One example of this happening is a watchpoint's scope bp. When
13835 the scope bp triggers, we notice that the watchpoint is out of
13836 scope, and delete it. We also delete its scope bp. But the
13837 scope bp is marked "auto-deleting", and is already on a bpstat.
13838 That bpstat is then checked for auto-deleting bp's, which are
13839 deleted.
13840
13841 A real solution to this problem might involve reference counts in
13842 bp's, and/or giving them pointers back to their referencing
13843 bpstat's, and teaching delete_breakpoint to only free a bp's
13844 storage when no more references were extent. A cheaper bandaid
13845 was chosen. */
c906108c
SS
13846 if (bpt->type == bp_none)
13847 return;
13848
4a64f543
MS
13849 /* At least avoid this stale reference until the reference counting
13850 of breakpoints gets resolved. */
d0fb5eae 13851 if (bpt->related_breakpoint != bpt)
e5a0a904 13852 {
d0fb5eae 13853 struct breakpoint *related;
3a5c3e22 13854 struct watchpoint *w;
d0fb5eae
JK
13855
13856 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13857 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13858 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13859 w = (struct watchpoint *) bpt;
13860 else
13861 w = NULL;
13862 if (w != NULL)
13863 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13864
13865 /* Unlink bpt from the bpt->related_breakpoint ring. */
13866 for (related = bpt; related->related_breakpoint != bpt;
13867 related = related->related_breakpoint);
13868 related->related_breakpoint = bpt->related_breakpoint;
13869 bpt->related_breakpoint = bpt;
e5a0a904
JK
13870 }
13871
a9634178
TJB
13872 /* watch_command_1 creates a watchpoint but only sets its number if
13873 update_watchpoint succeeds in creating its bp_locations. If there's
13874 a problem in that process, we'll be asked to delete the half-created
13875 watchpoint. In that case, don't announce the deletion. */
13876 if (bpt->number)
13877 observer_notify_breakpoint_deleted (bpt);
c906108c 13878
c906108c
SS
13879 if (breakpoint_chain == bpt)
13880 breakpoint_chain = bpt->next;
13881
c906108c
SS
13882 ALL_BREAKPOINTS (b)
13883 if (b->next == bpt)
c5aa993b
JM
13884 {
13885 b->next = bpt->next;
13886 break;
13887 }
c906108c 13888
f431efe5
PA
13889 /* Be sure no bpstat's are pointing at the breakpoint after it's
13890 been freed. */
13891 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13892 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13893 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13894 commands are associated with the bpstat; if we remove it here,
13895 then the later call to bpstat_do_actions (&stop_bpstat); in
13896 event-top.c won't do anything, and temporary breakpoints with
13897 commands won't work. */
13898
13899 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13900
4a64f543
MS
13901 /* Now that breakpoint is removed from breakpoint list, update the
13902 global location list. This will remove locations that used to
13903 belong to this breakpoint. Do this before freeing the breakpoint
13904 itself, since remove_breakpoint looks at location's owner. It
13905 might be better design to have location completely
13906 self-contained, but it's not the case now. */
b60e7edf 13907 update_global_location_list (0);
74960c60 13908
348d480f 13909 bpt->ops->dtor (bpt);
4a64f543
MS
13910 /* On the chance that someone will soon try again to delete this
13911 same bp, we mark it as deleted before freeing its storage. */
c906108c 13912 bpt->type = bp_none;
b8c9b27d 13913 xfree (bpt);
c906108c
SS
13914}
13915
4d6140d9
AC
13916static void
13917do_delete_breakpoint_cleanup (void *b)
13918{
13919 delete_breakpoint (b);
13920}
13921
13922struct cleanup *
13923make_cleanup_delete_breakpoint (struct breakpoint *b)
13924{
13925 return make_cleanup (do_delete_breakpoint_cleanup, b);
13926}
13927
51be5b68
PA
13928/* Iterator function to call a user-provided callback function once
13929 for each of B and its related breakpoints. */
13930
13931static void
13932iterate_over_related_breakpoints (struct breakpoint *b,
13933 void (*function) (struct breakpoint *,
13934 void *),
13935 void *data)
13936{
13937 struct breakpoint *related;
13938
13939 related = b;
13940 do
13941 {
13942 struct breakpoint *next;
13943
13944 /* FUNCTION may delete RELATED. */
13945 next = related->related_breakpoint;
13946
13947 if (next == related)
13948 {
13949 /* RELATED is the last ring entry. */
13950 function (related, data);
13951
13952 /* FUNCTION may have deleted it, so we'd never reach back to
13953 B. There's nothing left to do anyway, so just break
13954 out. */
13955 break;
13956 }
13957 else
13958 function (related, data);
13959
13960 related = next;
13961 }
13962 while (related != b);
13963}
95a42b64
TT
13964
13965static void
13966do_delete_breakpoint (struct breakpoint *b, void *ignore)
13967{
13968 delete_breakpoint (b);
13969}
13970
51be5b68
PA
13971/* A callback for map_breakpoint_numbers that calls
13972 delete_breakpoint. */
13973
13974static void
13975do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13976{
13977 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13978}
13979
c906108c 13980void
fba45db2 13981delete_command (char *arg, int from_tty)
c906108c 13982{
35df4500 13983 struct breakpoint *b, *b_tmp;
c906108c 13984
ea9365bb
TT
13985 dont_repeat ();
13986
c906108c
SS
13987 if (arg == 0)
13988 {
13989 int breaks_to_delete = 0;
13990
46c6471b
PA
13991 /* Delete all breakpoints if no argument. Do not delete
13992 internal breakpoints, these have to be deleted with an
13993 explicit breakpoint number argument. */
c5aa993b 13994 ALL_BREAKPOINTS (b)
46c6471b 13995 if (user_breakpoint_p (b))
973d738b
DJ
13996 {
13997 breaks_to_delete = 1;
13998 break;
13999 }
c906108c
SS
14000
14001 /* Ask user only if there are some breakpoints to delete. */
14002 if (!from_tty
e2e0b3e5 14003 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 14004 {
35df4500 14005 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14006 if (user_breakpoint_p (b))
c5aa993b 14007 delete_breakpoint (b);
c906108c
SS
14008 }
14009 }
14010 else
51be5b68 14011 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
14012}
14013
0d381245
VP
14014static int
14015all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 14016{
0d381245 14017 for (; loc; loc = loc->next)
8645ff69
UW
14018 if (!loc->shlib_disabled
14019 && !loc->pspace->executing_startup)
0d381245
VP
14020 return 0;
14021 return 1;
fe3f5fa8
VP
14022}
14023
776592bf
DE
14024/* Subroutine of update_breakpoint_locations to simplify it.
14025 Return non-zero if multiple fns in list LOC have the same name.
14026 Null names are ignored. */
14027
14028static int
14029ambiguous_names_p (struct bp_location *loc)
14030{
14031 struct bp_location *l;
14032 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
14033 (int (*) (const void *,
14034 const void *)) streq,
776592bf
DE
14035 NULL, xcalloc, xfree);
14036
14037 for (l = loc; l != NULL; l = l->next)
14038 {
14039 const char **slot;
14040 const char *name = l->function_name;
14041
14042 /* Allow for some names to be NULL, ignore them. */
14043 if (name == NULL)
14044 continue;
14045
14046 slot = (const char **) htab_find_slot (htab, (const void *) name,
14047 INSERT);
4a64f543
MS
14048 /* NOTE: We can assume slot != NULL here because xcalloc never
14049 returns NULL. */
776592bf
DE
14050 if (*slot != NULL)
14051 {
14052 htab_delete (htab);
14053 return 1;
14054 }
14055 *slot = name;
14056 }
14057
14058 htab_delete (htab);
14059 return 0;
14060}
14061
0fb4aa4b
PA
14062/* When symbols change, it probably means the sources changed as well,
14063 and it might mean the static tracepoint markers are no longer at
14064 the same address or line numbers they used to be at last we
14065 checked. Losing your static tracepoints whenever you rebuild is
14066 undesirable. This function tries to resync/rematch gdb static
14067 tracepoints with the markers on the target, for static tracepoints
14068 that have not been set by marker id. Static tracepoint that have
14069 been set by marker id are reset by marker id in breakpoint_re_set.
14070 The heuristic is:
14071
14072 1) For a tracepoint set at a specific address, look for a marker at
14073 the old PC. If one is found there, assume to be the same marker.
14074 If the name / string id of the marker found is different from the
14075 previous known name, assume that means the user renamed the marker
14076 in the sources, and output a warning.
14077
14078 2) For a tracepoint set at a given line number, look for a marker
14079 at the new address of the old line number. If one is found there,
14080 assume to be the same marker. If the name / string id of the
14081 marker found is different from the previous known name, assume that
14082 means the user renamed the marker in the sources, and output a
14083 warning.
14084
14085 3) If a marker is no longer found at the same address or line, it
14086 may mean the marker no longer exists. But it may also just mean
14087 the code changed a bit. Maybe the user added a few lines of code
14088 that made the marker move up or down (in line number terms). Ask
14089 the target for info about the marker with the string id as we knew
14090 it. If found, update line number and address in the matching
14091 static tracepoint. This will get confused if there's more than one
14092 marker with the same ID (possible in UST, although unadvised
14093 precisely because it confuses tools). */
14094
14095static struct symtab_and_line
14096update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14097{
d9b3f62e 14098 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
14099 struct static_tracepoint_marker marker;
14100 CORE_ADDR pc;
0fb4aa4b
PA
14101
14102 pc = sal.pc;
14103 if (sal.line)
14104 find_line_pc (sal.symtab, sal.line, &pc);
14105
14106 if (target_static_tracepoint_marker_at (pc, &marker))
14107 {
d9b3f62e 14108 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
14109 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14110 b->number,
d9b3f62e 14111 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 14112
d9b3f62e
PA
14113 xfree (tp->static_trace_marker_id);
14114 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
14115 release_static_tracepoint_marker (&marker);
14116
14117 return sal;
14118 }
14119
14120 /* Old marker wasn't found on target at lineno. Try looking it up
14121 by string ID. */
14122 if (!sal.explicit_pc
14123 && sal.line != 0
14124 && sal.symtab != NULL
d9b3f62e 14125 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
14126 {
14127 VEC(static_tracepoint_marker_p) *markers;
14128
14129 markers
d9b3f62e 14130 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
14131
14132 if (!VEC_empty(static_tracepoint_marker_p, markers))
14133 {
80e1d417 14134 struct symtab_and_line sal2;
0fb4aa4b 14135 struct symbol *sym;
80e1d417 14136 struct static_tracepoint_marker *tpmarker;
79a45e25 14137 struct ui_out *uiout = current_uiout;
0fb4aa4b 14138
80e1d417 14139 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 14140
d9b3f62e 14141 xfree (tp->static_trace_marker_id);
80e1d417 14142 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
14143
14144 warning (_("marker for static tracepoint %d (%s) not "
14145 "found at previous line number"),
d9b3f62e 14146 b->number, tp->static_trace_marker_id);
0fb4aa4b 14147
80e1d417 14148 init_sal (&sal2);
0fb4aa4b 14149
80e1d417 14150 sal2.pc = tpmarker->address;
0fb4aa4b 14151
80e1d417
AS
14152 sal2 = find_pc_line (tpmarker->address, 0);
14153 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
14154 ui_out_text (uiout, "Now in ");
14155 if (sym)
14156 {
14157 ui_out_field_string (uiout, "func",
14158 SYMBOL_PRINT_NAME (sym));
14159 ui_out_text (uiout, " at ");
14160 }
05cba821
JK
14161 ui_out_field_string (uiout, "file",
14162 symtab_to_filename_for_display (sal2.symtab));
0fb4aa4b
PA
14163 ui_out_text (uiout, ":");
14164
14165 if (ui_out_is_mi_like_p (uiout))
14166 {
0b0865da 14167 const char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b 14168
f35a17b5 14169 ui_out_field_string (uiout, "fullname", fullname);
0fb4aa4b
PA
14170 }
14171
80e1d417 14172 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
14173 ui_out_text (uiout, "\n");
14174
80e1d417 14175 b->loc->line_number = sal2.line;
2f202fde 14176 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
0fb4aa4b
PA
14177
14178 xfree (b->addr_string);
14179 b->addr_string = xstrprintf ("%s:%d",
05cba821 14180 symtab_to_filename_for_display (sal2.symtab),
f8eba3c6 14181 b->loc->line_number);
0fb4aa4b
PA
14182
14183 /* Might be nice to check if function changed, and warn if
14184 so. */
14185
80e1d417 14186 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
14187 }
14188 }
14189 return sal;
14190}
14191
8d3788bd
VP
14192/* Returns 1 iff locations A and B are sufficiently same that
14193 we don't need to report breakpoint as changed. */
14194
14195static int
14196locations_are_equal (struct bp_location *a, struct bp_location *b)
14197{
14198 while (a && b)
14199 {
14200 if (a->address != b->address)
14201 return 0;
14202
14203 if (a->shlib_disabled != b->shlib_disabled)
14204 return 0;
14205
14206 if (a->enabled != b->enabled)
14207 return 0;
14208
14209 a = a->next;
14210 b = b->next;
14211 }
14212
14213 if ((a == NULL) != (b == NULL))
14214 return 0;
14215
14216 return 1;
14217}
14218
f1310107
TJB
14219/* Create new breakpoint locations for B (a hardware or software breakpoint)
14220 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14221 a ranged breakpoint. */
14222
0e30163f 14223void
0d381245 14224update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
14225 struct symtabs_and_lines sals,
14226 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
14227{
14228 int i;
0d381245
VP
14229 struct bp_location *existing_locations = b->loc;
14230
f8eba3c6
TT
14231 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14232 {
14233 /* Ranged breakpoints have only one start location and one end
14234 location. */
14235 b->enable_state = bp_disabled;
14236 update_global_location_list (1);
14237 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14238 "multiple locations found\n"),
14239 b->number);
14240 return;
14241 }
f1310107 14242
4a64f543
MS
14243 /* If there's no new locations, and all existing locations are
14244 pending, don't do anything. This optimizes the common case where
14245 all locations are in the same shared library, that was unloaded.
14246 We'd like to retain the location, so that when the library is
14247 loaded again, we don't loose the enabled/disabled status of the
14248 individual locations. */
0d381245 14249 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
14250 return;
14251
fe3f5fa8
VP
14252 b->loc = NULL;
14253
0d381245 14254 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 14255 {
f8eba3c6
TT
14256 struct bp_location *new_loc;
14257
14258 switch_to_program_space_and_thread (sals.sals[i].pspace);
14259
14260 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 14261
0d381245
VP
14262 /* Reparse conditions, they might contain references to the
14263 old symtab. */
14264 if (b->cond_string != NULL)
14265 {
bbc13ae3 14266 const char *s;
bfd189b1 14267 volatile struct gdb_exception e;
fe3f5fa8 14268
0d381245
VP
14269 s = b->cond_string;
14270 TRY_CATCH (e, RETURN_MASK_ERROR)
14271 {
1bb9788d
TT
14272 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14273 block_for_pc (sals.sals[i].pc),
0d381245
VP
14274 0);
14275 }
14276 if (e.reason < 0)
14277 {
3e43a32a
MS
14278 warning (_("failed to reevaluate condition "
14279 "for breakpoint %d: %s"),
0d381245
VP
14280 b->number, e.message);
14281 new_loc->enabled = 0;
14282 }
14283 }
fe3f5fa8 14284
f1310107
TJB
14285 if (sals_end.nelts)
14286 {
14287 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14288
14289 new_loc->length = end - sals.sals[0].pc + 1;
14290 }
0d381245 14291 }
fe3f5fa8 14292
514f746b
AR
14293 /* Update locations of permanent breakpoints. */
14294 if (b->enable_state == bp_permanent)
14295 make_breakpoint_permanent (b);
14296
4a64f543
MS
14297 /* If possible, carry over 'disable' status from existing
14298 breakpoints. */
0d381245
VP
14299 {
14300 struct bp_location *e = existing_locations;
776592bf
DE
14301 /* If there are multiple breakpoints with the same function name,
14302 e.g. for inline functions, comparing function names won't work.
14303 Instead compare pc addresses; this is just a heuristic as things
14304 may have moved, but in practice it gives the correct answer
14305 often enough until a better solution is found. */
14306 int have_ambiguous_names = ambiguous_names_p (b->loc);
14307
0d381245
VP
14308 for (; e; e = e->next)
14309 {
14310 if (!e->enabled && e->function_name)
14311 {
14312 struct bp_location *l = b->loc;
776592bf
DE
14313 if (have_ambiguous_names)
14314 {
14315 for (; l; l = l->next)
f1310107 14316 if (breakpoint_locations_match (e, l))
776592bf
DE
14317 {
14318 l->enabled = 0;
14319 break;
14320 }
14321 }
14322 else
14323 {
14324 for (; l; l = l->next)
14325 if (l->function_name
14326 && strcmp (e->function_name, l->function_name) == 0)
14327 {
14328 l->enabled = 0;
14329 break;
14330 }
14331 }
0d381245
VP
14332 }
14333 }
14334 }
fe3f5fa8 14335
8d3788bd
VP
14336 if (!locations_are_equal (existing_locations, b->loc))
14337 observer_notify_breakpoint_modified (b);
14338
b60e7edf 14339 update_global_location_list (1);
fe3f5fa8
VP
14340}
14341
ef23e705
TJB
14342/* Find the SaL locations corresponding to the given ADDR_STRING.
14343 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14344
14345static struct symtabs_and_lines
14346addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14347{
14348 char *s;
02d20e4a 14349 struct symtabs_and_lines sals = {0};
f8eba3c6 14350 volatile struct gdb_exception e;
ef23e705 14351
983af33b 14352 gdb_assert (b->ops != NULL);
ef23e705 14353 s = addr_string;
ef23e705
TJB
14354
14355 TRY_CATCH (e, RETURN_MASK_ERROR)
14356 {
983af33b 14357 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
14358 }
14359 if (e.reason < 0)
14360 {
14361 int not_found_and_ok = 0;
14362 /* For pending breakpoints, it's expected that parsing will
14363 fail until the right shared library is loaded. User has
14364 already told to create pending breakpoints and don't need
14365 extra messages. If breakpoint is in bp_shlib_disabled
14366 state, then user already saw the message about that
14367 breakpoint being disabled, and don't want to see more
14368 errors. */
58438ac1 14369 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
14370 && (b->condition_not_parsed
14371 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 14372 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
14373 || b->enable_state == bp_disabled))
14374 not_found_and_ok = 1;
14375
14376 if (!not_found_and_ok)
14377 {
14378 /* We surely don't want to warn about the same breakpoint
14379 10 times. One solution, implemented here, is disable
14380 the breakpoint on error. Another solution would be to
14381 have separate 'warning emitted' flag. Since this
14382 happens only when a binary has changed, I don't know
14383 which approach is better. */
14384 b->enable_state = bp_disabled;
14385 throw_exception (e);
14386 }
14387 }
14388
58438ac1 14389 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 14390 {
f8eba3c6 14391 int i;
ef23e705 14392
f8eba3c6
TT
14393 for (i = 0; i < sals.nelts; ++i)
14394 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
14395 if (b->condition_not_parsed && s && s[0])
14396 {
ed1d1739
KS
14397 char *cond_string, *extra_string;
14398 int thread, task;
ef23e705
TJB
14399
14400 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
14401 &cond_string, &thread, &task,
14402 &extra_string);
ef23e705
TJB
14403 if (cond_string)
14404 b->cond_string = cond_string;
14405 b->thread = thread;
14406 b->task = task;
e7e0cddf
SS
14407 if (extra_string)
14408 b->extra_string = extra_string;
ef23e705
TJB
14409 b->condition_not_parsed = 0;
14410 }
14411
983af33b 14412 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 14413 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 14414
58438ac1
TT
14415 *found = 1;
14416 }
14417 else
14418 *found = 0;
ef23e705
TJB
14419
14420 return sals;
14421}
14422
348d480f
PA
14423/* The default re_set method, for typical hardware or software
14424 breakpoints. Reevaluate the breakpoint and recreate its
14425 locations. */
14426
14427static void
28010a5d 14428breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
14429{
14430 int found;
f1310107 14431 struct symtabs_and_lines sals, sals_end;
ef23e705 14432 struct symtabs_and_lines expanded = {0};
f1310107 14433 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
14434
14435 sals = addr_string_to_sals (b, b->addr_string, &found);
14436 if (found)
14437 {
14438 make_cleanup (xfree, sals.sals);
f8eba3c6 14439 expanded = sals;
ef23e705
TJB
14440 }
14441
f1310107
TJB
14442 if (b->addr_string_range_end)
14443 {
14444 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14445 if (found)
14446 {
14447 make_cleanup (xfree, sals_end.sals);
f8eba3c6 14448 expanded_end = sals_end;
f1310107
TJB
14449 }
14450 }
14451
14452 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
14453}
14454
983af33b
SDJ
14455/* Default method for creating SALs from an address string. It basically
14456 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14457
14458static void
14459create_sals_from_address_default (char **arg,
14460 struct linespec_result *canonical,
14461 enum bptype type_wanted,
14462 char *addr_start, char **copy_arg)
14463{
14464 parse_breakpoint_sals (arg, canonical);
14465}
14466
14467/* Call create_breakpoints_sal for the given arguments. This is the default
14468 function for the `create_breakpoints_sal' method of
14469 breakpoint_ops. */
14470
14471static void
14472create_breakpoints_sal_default (struct gdbarch *gdbarch,
14473 struct linespec_result *canonical,
983af33b 14474 char *cond_string,
e7e0cddf 14475 char *extra_string,
983af33b
SDJ
14476 enum bptype type_wanted,
14477 enum bpdisp disposition,
14478 int thread,
14479 int task, int ignore_count,
14480 const struct breakpoint_ops *ops,
14481 int from_tty, int enabled,
44f238bb 14482 int internal, unsigned flags)
983af33b
SDJ
14483{
14484 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 14485 extra_string,
983af33b
SDJ
14486 type_wanted, disposition,
14487 thread, task, ignore_count, ops, from_tty,
44f238bb 14488 enabled, internal, flags);
983af33b
SDJ
14489}
14490
14491/* Decode the line represented by S by calling decode_line_full. This is the
14492 default function for the `decode_linespec' method of breakpoint_ops. */
14493
14494static void
14495decode_linespec_default (struct breakpoint *b, char **s,
14496 struct symtabs_and_lines *sals)
14497{
14498 struct linespec_result canonical;
14499
14500 init_linespec_result (&canonical);
14501 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14502 (struct symtab *) NULL, 0,
14503 &canonical, multiple_symbols_all,
14504 b->filter);
14505
14506 /* We should get 0 or 1 resulting SALs. */
14507 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14508
14509 if (VEC_length (linespec_sals, canonical.sals) > 0)
14510 {
14511 struct linespec_sals *lsal;
14512
14513 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14514 *sals = lsal->sals;
14515 /* Arrange it so the destructor does not free the
14516 contents. */
14517 lsal->sals.sals = NULL;
14518 }
14519
14520 destroy_linespec_result (&canonical);
14521}
14522
28010a5d
PA
14523/* Prepare the global context for a re-set of breakpoint B. */
14524
14525static struct cleanup *
14526prepare_re_set_context (struct breakpoint *b)
14527{
14528 struct cleanup *cleanups;
14529
14530 input_radix = b->input_radix;
14531 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
14532 if (b->pspace != NULL)
14533 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
14534 set_language (b->language);
14535
14536 return cleanups;
ef23e705
TJB
14537}
14538
c906108c
SS
14539/* Reset a breakpoint given it's struct breakpoint * BINT.
14540 The value we return ends up being the return value from catch_errors.
14541 Unused in this case. */
14542
14543static int
4efb68b1 14544breakpoint_re_set_one (void *bint)
c906108c 14545{
4a64f543 14546 /* Get past catch_errs. */
53a5351d 14547 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 14548 struct cleanup *cleanups;
c906108c 14549
348d480f
PA
14550 cleanups = prepare_re_set_context (b);
14551 b->ops->re_set (b);
14552 do_cleanups (cleanups);
c906108c
SS
14553 return 0;
14554}
14555
69de3c6a 14556/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 14557void
69de3c6a 14558breakpoint_re_set (void)
c906108c 14559{
35df4500 14560 struct breakpoint *b, *b_tmp;
c906108c
SS
14561 enum language save_language;
14562 int save_input_radix;
6c95b8df 14563 struct cleanup *old_chain;
c5aa993b 14564
c906108c
SS
14565 save_language = current_language->la_language;
14566 save_input_radix = input_radix;
6c95b8df
PA
14567 old_chain = save_current_program_space ();
14568
35df4500 14569 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 14570 {
4a64f543 14571 /* Format possible error msg. */
fe3f5fa8 14572 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
14573 b->number);
14574 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 14575 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 14576 do_cleanups (cleanups);
c5aa993b 14577 }
c906108c
SS
14578 set_language (save_language);
14579 input_radix = save_input_radix;
e62c965a 14580
0756c555 14581 jit_breakpoint_re_set ();
4efc6507 14582
6c95b8df
PA
14583 do_cleanups (old_chain);
14584
af02033e
PP
14585 create_overlay_event_breakpoint ();
14586 create_longjmp_master_breakpoint ();
14587 create_std_terminate_master_breakpoint ();
186c406b 14588 create_exception_master_breakpoint ();
c906108c
SS
14589}
14590\f
c906108c
SS
14591/* Reset the thread number of this breakpoint:
14592
14593 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14594 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14595void
fba45db2 14596breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14597{
14598 if (b->thread != -1)
14599 {
39f77062
KB
14600 if (in_thread_list (inferior_ptid))
14601 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14602
14603 /* We're being called after following a fork. The new fork is
14604 selected as current, and unless this was a vfork will have a
14605 different program space from the original thread. Reset that
14606 as well. */
14607 b->loc->pspace = current_program_space;
c906108c
SS
14608 }
14609}
14610
03ac34d5
MS
14611/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14612 If from_tty is nonzero, it prints a message to that effect,
14613 which ends with a period (no newline). */
14614
c906108c 14615void
fba45db2 14616set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14617{
52f0bd74 14618 struct breakpoint *b;
c906108c
SS
14619
14620 if (count < 0)
14621 count = 0;
14622
14623 ALL_BREAKPOINTS (b)
14624 if (b->number == bptnum)
c5aa993b 14625 {
d77f58be
SS
14626 if (is_tracepoint (b))
14627 {
14628 if (from_tty && count != 0)
14629 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14630 bptnum);
14631 return;
14632 }
14633
c5aa993b 14634 b->ignore_count = count;
221ea385
KS
14635 if (from_tty)
14636 {
14637 if (count == 0)
3e43a32a
MS
14638 printf_filtered (_("Will stop next time "
14639 "breakpoint %d is reached."),
221ea385
KS
14640 bptnum);
14641 else if (count == 1)
a3f17187 14642 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14643 bptnum);
14644 else
3e43a32a
MS
14645 printf_filtered (_("Will ignore next %d "
14646 "crossings of breakpoint %d."),
221ea385
KS
14647 count, bptnum);
14648 }
8d3788bd 14649 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14650 return;
14651 }
c906108c 14652
8a3fe4f8 14653 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14654}
14655
c906108c
SS
14656/* Command to set ignore-count of breakpoint N to COUNT. */
14657
14658static void
fba45db2 14659ignore_command (char *args, int from_tty)
c906108c
SS
14660{
14661 char *p = args;
52f0bd74 14662 int num;
c906108c
SS
14663
14664 if (p == 0)
e2e0b3e5 14665 error_no_arg (_("a breakpoint number"));
c5aa993b 14666
c906108c 14667 num = get_number (&p);
5c44784c 14668 if (num == 0)
8a3fe4f8 14669 error (_("bad breakpoint number: '%s'"), args);
c906108c 14670 if (*p == 0)
8a3fe4f8 14671 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14672
14673 set_ignore_count (num,
14674 longest_to_int (value_as_long (parse_and_eval (p))),
14675 from_tty);
221ea385
KS
14676 if (from_tty)
14677 printf_filtered ("\n");
c906108c
SS
14678}
14679\f
14680/* Call FUNCTION on each of the breakpoints
14681 whose numbers are given in ARGS. */
14682
14683static void
95a42b64
TT
14684map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14685 void *),
14686 void *data)
c906108c 14687{
52f0bd74
AC
14688 int num;
14689 struct breakpoint *b, *tmp;
11cf8741 14690 int match;
197f0a60 14691 struct get_number_or_range_state state;
c906108c 14692
197f0a60 14693 if (args == 0)
e2e0b3e5 14694 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14695
197f0a60
TT
14696 init_number_or_range (&state, args);
14697
14698 while (!state.finished)
c906108c 14699 {
197f0a60
TT
14700 char *p = state.string;
14701
11cf8741 14702 match = 0;
c5aa993b 14703
197f0a60 14704 num = get_number_or_range (&state);
5c44784c 14705 if (num == 0)
c5aa993b 14706 {
8a3fe4f8 14707 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14708 }
14709 else
14710 {
14711 ALL_BREAKPOINTS_SAFE (b, tmp)
14712 if (b->number == num)
14713 {
11cf8741 14714 match = 1;
cdac0397 14715 function (b, data);
11cf8741 14716 break;
5c44784c 14717 }
11cf8741 14718 if (match == 0)
a3f17187 14719 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14720 }
c906108c
SS
14721 }
14722}
14723
0d381245
VP
14724static struct bp_location *
14725find_location_by_number (char *number)
14726{
14727 char *dot = strchr (number, '.');
14728 char *p1;
14729 int bp_num;
14730 int loc_num;
14731 struct breakpoint *b;
14732 struct bp_location *loc;
14733
14734 *dot = '\0';
14735
14736 p1 = number;
197f0a60 14737 bp_num = get_number (&p1);
0d381245
VP
14738 if (bp_num == 0)
14739 error (_("Bad breakpoint number '%s'"), number);
14740
14741 ALL_BREAKPOINTS (b)
14742 if (b->number == bp_num)
14743 {
14744 break;
14745 }
14746
14747 if (!b || b->number != bp_num)
14748 error (_("Bad breakpoint number '%s'"), number);
14749
14750 p1 = dot+1;
197f0a60 14751 loc_num = get_number (&p1);
0d381245
VP
14752 if (loc_num == 0)
14753 error (_("Bad breakpoint location number '%s'"), number);
14754
14755 --loc_num;
14756 loc = b->loc;
14757 for (;loc_num && loc; --loc_num, loc = loc->next)
14758 ;
14759 if (!loc)
14760 error (_("Bad breakpoint location number '%s'"), dot+1);
14761
14762 return loc;
14763}
14764
14765
1900040c
MS
14766/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14767 If from_tty is nonzero, it prints a message to that effect,
14768 which ends with a period (no newline). */
14769
c906108c 14770void
fba45db2 14771disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14772{
14773 /* Never disable a watchpoint scope breakpoint; we want to
14774 hit them when we leave scope so we can delete both the
14775 watchpoint and its scope breakpoint at that time. */
14776 if (bpt->type == bp_watchpoint_scope)
14777 return;
14778
c2c6d25f 14779 /* You can't disable permanent breakpoints. */
b5de0fa7 14780 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
14781 return;
14782
b5de0fa7 14783 bpt->enable_state = bp_disabled;
c906108c 14784
b775012e
LM
14785 /* Mark breakpoint locations modified. */
14786 mark_breakpoint_modified (bpt);
14787
d248b706
KY
14788 if (target_supports_enable_disable_tracepoint ()
14789 && current_trace_status ()->running && is_tracepoint (bpt))
14790 {
14791 struct bp_location *location;
14792
14793 for (location = bpt->loc; location; location = location->next)
14794 target_disable_tracepoint (location);
14795 }
14796
b60e7edf 14797 update_global_location_list (0);
c906108c 14798
8d3788bd 14799 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14800}
14801
51be5b68
PA
14802/* A callback for iterate_over_related_breakpoints. */
14803
14804static void
14805do_disable_breakpoint (struct breakpoint *b, void *ignore)
14806{
14807 disable_breakpoint (b);
14808}
14809
95a42b64
TT
14810/* A callback for map_breakpoint_numbers that calls
14811 disable_breakpoint. */
14812
14813static void
14814do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14815{
51be5b68 14816 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14817}
14818
c906108c 14819static void
fba45db2 14820disable_command (char *args, int from_tty)
c906108c 14821{
c906108c 14822 if (args == 0)
46c6471b
PA
14823 {
14824 struct breakpoint *bpt;
14825
14826 ALL_BREAKPOINTS (bpt)
14827 if (user_breakpoint_p (bpt))
14828 disable_breakpoint (bpt);
14829 }
9eaabc75 14830 else
0d381245 14831 {
9eaabc75
MW
14832 char *num = extract_arg (&args);
14833
14834 while (num)
d248b706 14835 {
9eaabc75 14836 if (strchr (num, '.'))
b775012e 14837 {
9eaabc75
MW
14838 struct bp_location *loc = find_location_by_number (num);
14839
14840 if (loc)
14841 {
14842 if (loc->enabled)
14843 {
14844 loc->enabled = 0;
14845 mark_breakpoint_location_modified (loc);
14846 }
14847 if (target_supports_enable_disable_tracepoint ()
14848 && current_trace_status ()->running && loc->owner
14849 && is_tracepoint (loc->owner))
14850 target_disable_tracepoint (loc);
14851 }
14852 update_global_location_list (0);
b775012e 14853 }
9eaabc75
MW
14854 else
14855 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14856 num = extract_arg (&args);
d248b706 14857 }
0d381245 14858 }
c906108c
SS
14859}
14860
14861static void
816338b5
SS
14862enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14863 int count)
c906108c 14864{
afe38095 14865 int target_resources_ok;
c906108c
SS
14866
14867 if (bpt->type == bp_hardware_breakpoint)
14868 {
14869 int i;
c5aa993b 14870 i = hw_breakpoint_used_count ();
53a5351d 14871 target_resources_ok =
d92524f1 14872 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14873 i + 1, 0);
c906108c 14874 if (target_resources_ok == 0)
8a3fe4f8 14875 error (_("No hardware breakpoint support in the target."));
c906108c 14876 else if (target_resources_ok < 0)
8a3fe4f8 14877 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14878 }
14879
cc60f2e3 14880 if (is_watchpoint (bpt))
c906108c 14881 {
d07205c2
JK
14882 /* Initialize it just to avoid a GCC false warning. */
14883 enum enable_state orig_enable_state = 0;
bfd189b1 14884 volatile struct gdb_exception e;
dde02812
ES
14885
14886 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 14887 {
3a5c3e22
PA
14888 struct watchpoint *w = (struct watchpoint *) bpt;
14889
1e718ff1
TJB
14890 orig_enable_state = bpt->enable_state;
14891 bpt->enable_state = bp_enabled;
3a5c3e22 14892 update_watchpoint (w, 1 /* reparse */);
c906108c 14893 }
dde02812 14894 if (e.reason < 0)
c5aa993b 14895 {
1e718ff1 14896 bpt->enable_state = orig_enable_state;
dde02812
ES
14897 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14898 bpt->number);
14899 return;
c5aa993b 14900 }
c906108c 14901 }
0101ce28 14902
b4c291bb
KH
14903 if (bpt->enable_state != bp_permanent)
14904 bpt->enable_state = bp_enabled;
d248b706 14905
b775012e
LM
14906 bpt->enable_state = bp_enabled;
14907
14908 /* Mark breakpoint locations modified. */
14909 mark_breakpoint_modified (bpt);
14910
d248b706
KY
14911 if (target_supports_enable_disable_tracepoint ()
14912 && current_trace_status ()->running && is_tracepoint (bpt))
14913 {
14914 struct bp_location *location;
14915
14916 for (location = bpt->loc; location; location = location->next)
14917 target_enable_tracepoint (location);
14918 }
14919
b4c291bb 14920 bpt->disposition = disposition;
816338b5 14921 bpt->enable_count = count;
b60e7edf 14922 update_global_location_list (1);
9c97429f 14923
8d3788bd 14924 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14925}
14926
fe3f5fa8 14927
c906108c 14928void
fba45db2 14929enable_breakpoint (struct breakpoint *bpt)
c906108c 14930{
816338b5 14931 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14932}
14933
14934static void
14935do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14936{
14937 enable_breakpoint (bpt);
c906108c
SS
14938}
14939
95a42b64
TT
14940/* A callback for map_breakpoint_numbers that calls
14941 enable_breakpoint. */
14942
14943static void
14944do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14945{
51be5b68 14946 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14947}
14948
c906108c
SS
14949/* The enable command enables the specified breakpoints (or all defined
14950 breakpoints) so they once again become (or continue to be) effective
1272ad14 14951 in stopping the inferior. */
c906108c 14952
c906108c 14953static void
fba45db2 14954enable_command (char *args, int from_tty)
c906108c 14955{
c906108c 14956 if (args == 0)
46c6471b
PA
14957 {
14958 struct breakpoint *bpt;
14959
14960 ALL_BREAKPOINTS (bpt)
14961 if (user_breakpoint_p (bpt))
14962 enable_breakpoint (bpt);
14963 }
9eaabc75 14964 else
0d381245 14965 {
9eaabc75
MW
14966 char *num = extract_arg (&args);
14967
14968 while (num)
d248b706 14969 {
9eaabc75 14970 if (strchr (num, '.'))
b775012e 14971 {
9eaabc75
MW
14972 struct bp_location *loc = find_location_by_number (num);
14973
14974 if (loc)
14975 {
14976 if (!loc->enabled)
14977 {
14978 loc->enabled = 1;
14979 mark_breakpoint_location_modified (loc);
14980 }
14981 if (target_supports_enable_disable_tracepoint ()
14982 && current_trace_status ()->running && loc->owner
14983 && is_tracepoint (loc->owner))
14984 target_enable_tracepoint (loc);
14985 }
14986 update_global_location_list (1);
b775012e 14987 }
9eaabc75
MW
14988 else
14989 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14990 num = extract_arg (&args);
d248b706 14991 }
0d381245 14992 }
c906108c
SS
14993}
14994
816338b5
SS
14995/* This struct packages up disposition data for application to multiple
14996 breakpoints. */
14997
14998struct disp_data
14999{
15000 enum bpdisp disp;
15001 int count;
15002};
15003
c906108c 15004static void
51be5b68
PA
15005do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15006{
816338b5 15007 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 15008
816338b5 15009 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
15010}
15011
15012static void
15013do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15014{
816338b5 15015 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
15016
15017 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15018}
15019
c906108c 15020static void
fba45db2 15021enable_once_command (char *args, int from_tty)
c906108c 15022{
51be5b68 15023 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
15024}
15025
816338b5
SS
15026static void
15027do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15028{
15029 struct disp_data disp = { disp_disable, *(int *) countptr };
15030
15031 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15032}
15033
15034static void
15035enable_count_command (char *args, int from_tty)
15036{
15037 int count = get_number (&args);
15038
15039 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15040}
15041
c906108c 15042static void
51be5b68 15043do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 15044{
816338b5 15045 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
15046
15047 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
15048}
15049
c906108c 15050static void
fba45db2 15051enable_delete_command (char *args, int from_tty)
c906108c 15052{
51be5b68 15053 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
15054}
15055\f
fa8d40ab
JJ
15056static void
15057set_breakpoint_cmd (char *args, int from_tty)
15058{
15059}
15060
15061static void
15062show_breakpoint_cmd (char *args, int from_tty)
15063{
15064}
15065
1f3b5d1b
PP
15066/* Invalidate last known value of any hardware watchpoint if
15067 the memory which that value represents has been written to by
15068 GDB itself. */
15069
15070static void
8de0566d
YQ
15071invalidate_bp_value_on_memory_change (struct inferior *inferior,
15072 CORE_ADDR addr, ssize_t len,
1f3b5d1b
PP
15073 const bfd_byte *data)
15074{
15075 struct breakpoint *bp;
15076
15077 ALL_BREAKPOINTS (bp)
15078 if (bp->enable_state == bp_enabled
3a5c3e22 15079 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 15080 {
3a5c3e22 15081 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 15082
3a5c3e22
PA
15083 if (wp->val_valid && wp->val)
15084 {
15085 struct bp_location *loc;
15086
15087 for (loc = bp->loc; loc != NULL; loc = loc->next)
15088 if (loc->loc_type == bp_loc_hardware_watchpoint
15089 && loc->address + loc->length > addr
15090 && addr + len > loc->address)
15091 {
15092 value_free (wp->val);
15093 wp->val = NULL;
15094 wp->val_valid = 0;
15095 }
15096 }
1f3b5d1b
PP
15097 }
15098}
15099
8181d85f
DJ
15100/* Create and insert a raw software breakpoint at PC. Return an
15101 identifier, which should be used to remove the breakpoint later.
15102 In general, places which call this should be using something on the
15103 breakpoint chain instead; this function should be eliminated
15104 someday. */
15105
15106void *
6c95b8df
PA
15107deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
15108 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
15109{
15110 struct bp_target_info *bp_tgt;
15111
41bf6aca 15112 bp_tgt = XCNEW (struct bp_target_info);
8181d85f 15113
6c95b8df 15114 bp_tgt->placed_address_space = aspace;
8181d85f 15115 bp_tgt->placed_address = pc;
6c95b8df 15116
a6d9a66e 15117 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
15118 {
15119 /* Could not insert the breakpoint. */
15120 xfree (bp_tgt);
15121 return NULL;
15122 }
15123
15124 return bp_tgt;
15125}
15126
4a64f543
MS
15127/* Remove a breakpoint BP inserted by
15128 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
15129
15130int
a6d9a66e 15131deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
15132{
15133 struct bp_target_info *bp_tgt = bp;
15134 int ret;
15135
a6d9a66e 15136 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
15137 xfree (bp_tgt);
15138
15139 return ret;
15140}
15141
8181d85f
DJ
15142/* Create and insert a breakpoint for software single step. */
15143
15144void
6c95b8df 15145insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
15146 struct address_space *aspace,
15147 CORE_ADDR next_pc)
8181d85f
DJ
15148{
15149 void **bpt_p;
15150
15151 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
15152 {
15153 bpt_p = &single_step_breakpoints[0];
15154 single_step_gdbarch[0] = gdbarch;
15155 }
8181d85f
DJ
15156 else
15157 {
15158 gdb_assert (single_step_breakpoints[1] == NULL);
15159 bpt_p = &single_step_breakpoints[1];
a6d9a66e 15160 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
15161 }
15162
4a64f543
MS
15163 /* NOTE drow/2006-04-11: A future improvement to this function would
15164 be to only create the breakpoints once, and actually put them on
15165 the breakpoint chain. That would let us use set_raw_breakpoint.
15166 We could adjust the addresses each time they were needed. Doing
15167 this requires corresponding changes elsewhere where single step
15168 breakpoints are handled, however. So, for now, we use this. */
8181d85f 15169
6c95b8df 15170 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 15171 if (*bpt_p == NULL)
5af949e3
UW
15172 error (_("Could not insert single-step breakpoint at %s"),
15173 paddress (gdbarch, next_pc));
8181d85f
DJ
15174}
15175
f02253f1
HZ
15176/* Check if the breakpoints used for software single stepping
15177 were inserted or not. */
15178
15179int
15180single_step_breakpoints_inserted (void)
15181{
15182 return (single_step_breakpoints[0] != NULL
15183 || single_step_breakpoints[1] != NULL);
15184}
15185
8181d85f
DJ
15186/* Remove and delete any breakpoints used for software single step. */
15187
15188void
15189remove_single_step_breakpoints (void)
15190{
15191 gdb_assert (single_step_breakpoints[0] != NULL);
15192
15193 /* See insert_single_step_breakpoint for more about this deprecated
15194 call. */
a6d9a66e
UW
15195 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
15196 single_step_breakpoints[0]);
15197 single_step_gdbarch[0] = NULL;
8181d85f
DJ
15198 single_step_breakpoints[0] = NULL;
15199
15200 if (single_step_breakpoints[1] != NULL)
15201 {
a6d9a66e
UW
15202 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
15203 single_step_breakpoints[1]);
15204 single_step_gdbarch[1] = NULL;
8181d85f
DJ
15205 single_step_breakpoints[1] = NULL;
15206 }
15207}
15208
d03285ec
UW
15209/* Delete software single step breakpoints without removing them from
15210 the inferior. This is intended to be used if the inferior's address
15211 space where they were inserted is already gone, e.g. after exit or
15212 exec. */
15213
15214void
15215cancel_single_step_breakpoints (void)
15216{
15217 int i;
15218
15219 for (i = 0; i < 2; i++)
15220 if (single_step_breakpoints[i])
15221 {
15222 xfree (single_step_breakpoints[i]);
15223 single_step_breakpoints[i] = NULL;
15224 single_step_gdbarch[i] = NULL;
15225 }
15226}
15227
15228/* Detach software single-step breakpoints from INFERIOR_PTID without
15229 removing them. */
15230
15231static void
15232detach_single_step_breakpoints (void)
15233{
15234 int i;
15235
15236 for (i = 0; i < 2; i++)
15237 if (single_step_breakpoints[i])
15238 target_remove_breakpoint (single_step_gdbarch[i],
15239 single_step_breakpoints[i]);
15240}
15241
4a64f543
MS
15242/* Check whether a software single-step breakpoint is inserted at
15243 PC. */
1aafd4da 15244
2adfaa28 15245int
cc59ec59
MS
15246single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15247 CORE_ADDR pc)
1aafd4da
UW
15248{
15249 int i;
15250
15251 for (i = 0; i < 2; i++)
15252 {
15253 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
15254 if (bp_tgt
15255 && breakpoint_address_match (bp_tgt->placed_address_space,
15256 bp_tgt->placed_address,
15257 aspace, pc))
1aafd4da
UW
15258 return 1;
15259 }
15260
15261 return 0;
15262}
15263
a96d9b2e
SDJ
15264/* Returns 0 if 'bp' is NOT a syscall catchpoint,
15265 non-zero otherwise. */
15266static int
15267is_syscall_catchpoint_enabled (struct breakpoint *bp)
15268{
15269 if (syscall_catchpoint_p (bp)
15270 && bp->enable_state != bp_disabled
15271 && bp->enable_state != bp_call_disabled)
15272 return 1;
15273 else
15274 return 0;
15275}
15276
15277int
15278catch_syscall_enabled (void)
15279{
fa3064dd
YQ
15280 struct catch_syscall_inferior_data *inf_data
15281 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 15282
fa3064dd 15283 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
15284}
15285
15286int
15287catching_syscall_number (int syscall_number)
15288{
15289 struct breakpoint *bp;
15290
15291 ALL_BREAKPOINTS (bp)
15292 if (is_syscall_catchpoint_enabled (bp))
15293 {
be5c67c1
PA
15294 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15295
15296 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
15297 {
15298 int i, iter;
15299 for (i = 0;
be5c67c1 15300 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
15301 i++)
15302 if (syscall_number == iter)
15303 return 1;
15304 }
15305 else
15306 return 1;
15307 }
15308
15309 return 0;
15310}
15311
15312/* Complete syscall names. Used by "catch syscall". */
49c4e619 15313static VEC (char_ptr) *
a96d9b2e 15314catch_syscall_completer (struct cmd_list_element *cmd,
6f937416 15315 const char *text, const char *word)
a96d9b2e
SDJ
15316{
15317 const char **list = get_syscall_names ();
49c4e619 15318 VEC (char_ptr) *retlist
b45627a0 15319 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
cc59ec59 15320
c38eea1a
MS
15321 xfree (list);
15322 return retlist;
a96d9b2e
SDJ
15323}
15324
1042e4c0
SS
15325/* Tracepoint-specific operations. */
15326
15327/* Set tracepoint count to NUM. */
15328static void
15329set_tracepoint_count (int num)
15330{
15331 tracepoint_count = num;
4fa62494 15332 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
15333}
15334
70221824 15335static void
1042e4c0
SS
15336trace_command (char *arg, int from_tty)
15337{
55aa24fb
SDJ
15338 struct breakpoint_ops *ops;
15339 const char *arg_cp = arg;
15340
15341 if (arg && probe_linespec_to_ops (&arg_cp))
15342 ops = &tracepoint_probe_breakpoint_ops;
15343 else
15344 ops = &tracepoint_breakpoint_ops;
15345
558a9d82
YQ
15346 create_breakpoint (get_current_arch (),
15347 arg,
15348 NULL, 0, NULL, 1 /* parse arg */,
15349 0 /* tempflag */,
15350 bp_tracepoint /* type_wanted */,
15351 0 /* Ignore count */,
15352 pending_break_support,
15353 ops,
15354 from_tty,
15355 1 /* enabled */,
15356 0 /* internal */, 0);
1042e4c0
SS
15357}
15358
70221824 15359static void
7a697b8d
SS
15360ftrace_command (char *arg, int from_tty)
15361{
558a9d82
YQ
15362 create_breakpoint (get_current_arch (),
15363 arg,
15364 NULL, 0, NULL, 1 /* parse arg */,
15365 0 /* tempflag */,
15366 bp_fast_tracepoint /* type_wanted */,
15367 0 /* Ignore count */,
15368 pending_break_support,
15369 &tracepoint_breakpoint_ops,
15370 from_tty,
15371 1 /* enabled */,
15372 0 /* internal */, 0);
0fb4aa4b
PA
15373}
15374
15375/* strace command implementation. Creates a static tracepoint. */
15376
70221824 15377static void
0fb4aa4b
PA
15378strace_command (char *arg, int from_tty)
15379{
983af33b
SDJ
15380 struct breakpoint_ops *ops;
15381
15382 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15383 or with a normal static tracepoint. */
15384 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15385 ops = &strace_marker_breakpoint_ops;
15386 else
15387 ops = &tracepoint_breakpoint_ops;
15388
558a9d82
YQ
15389 create_breakpoint (get_current_arch (),
15390 arg,
15391 NULL, 0, NULL, 1 /* parse arg */,
15392 0 /* tempflag */,
15393 bp_static_tracepoint /* type_wanted */,
15394 0 /* Ignore count */,
15395 pending_break_support,
15396 ops,
15397 from_tty,
15398 1 /* enabled */,
15399 0 /* internal */, 0);
7a697b8d
SS
15400}
15401
409873ef
SS
15402/* Set up a fake reader function that gets command lines from a linked
15403 list that was acquired during tracepoint uploading. */
15404
15405static struct uploaded_tp *this_utp;
3149d8c1 15406static int next_cmd;
409873ef
SS
15407
15408static char *
15409read_uploaded_action (void)
15410{
15411 char *rslt;
15412
3149d8c1 15413 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 15414
3149d8c1 15415 next_cmd++;
409873ef
SS
15416
15417 return rslt;
15418}
15419
00bf0b85
SS
15420/* Given information about a tracepoint as recorded on a target (which
15421 can be either a live system or a trace file), attempt to create an
15422 equivalent GDB tracepoint. This is not a reliable process, since
15423 the target does not necessarily have all the information used when
15424 the tracepoint was originally defined. */
15425
d9b3f62e 15426struct tracepoint *
00bf0b85 15427create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 15428{
409873ef 15429 char *addr_str, small_buf[100];
d9b3f62e 15430 struct tracepoint *tp;
fd9b8c24 15431
409873ef
SS
15432 if (utp->at_string)
15433 addr_str = utp->at_string;
15434 else
15435 {
15436 /* In the absence of a source location, fall back to raw
15437 address. Since there is no way to confirm that the address
15438 means the same thing as when the trace was started, warn the
15439 user. */
3e43a32a
MS
15440 warning (_("Uploaded tracepoint %d has no "
15441 "source location, using raw address"),
409873ef 15442 utp->number);
8c042590 15443 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
409873ef
SS
15444 addr_str = small_buf;
15445 }
15446
15447 /* There's not much we can do with a sequence of bytecodes. */
15448 if (utp->cond && !utp->cond_string)
3e43a32a
MS
15449 warning (_("Uploaded tracepoint %d condition "
15450 "has no source form, ignoring it"),
409873ef 15451 utp->number);
d5551862 15452
8cdf0e15 15453 if (!create_breakpoint (get_current_arch (),
409873ef 15454 addr_str,
e7e0cddf
SS
15455 utp->cond_string, -1, NULL,
15456 0 /* parse cond/thread */,
8cdf0e15 15457 0 /* tempflag */,
0fb4aa4b 15458 utp->type /* type_wanted */,
8cdf0e15
VP
15459 0 /* Ignore count */,
15460 pending_break_support,
348d480f 15461 &tracepoint_breakpoint_ops,
8cdf0e15 15462 0 /* from_tty */,
84f4c1fe 15463 utp->enabled /* enabled */,
44f238bb
PA
15464 0 /* internal */,
15465 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
15466 return NULL;
15467
409873ef 15468 /* Get the tracepoint we just created. */
fd9b8c24
PA
15469 tp = get_tracepoint (tracepoint_count);
15470 gdb_assert (tp != NULL);
d5551862 15471
00bf0b85
SS
15472 if (utp->pass > 0)
15473 {
8c042590
PM
15474 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15475 tp->base.number);
00bf0b85 15476
409873ef 15477 trace_pass_command (small_buf, 0);
00bf0b85
SS
15478 }
15479
409873ef
SS
15480 /* If we have uploaded versions of the original commands, set up a
15481 special-purpose "reader" function and call the usual command line
15482 reader, then pass the result to the breakpoint command-setting
15483 function. */
3149d8c1 15484 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 15485 {
409873ef 15486 struct command_line *cmd_list;
00bf0b85 15487
409873ef 15488 this_utp = utp;
3149d8c1 15489 next_cmd = 0;
d5551862 15490
409873ef
SS
15491 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15492
d9b3f62e 15493 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 15494 }
3149d8c1
SS
15495 else if (!VEC_empty (char_ptr, utp->actions)
15496 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
15497 warning (_("Uploaded tracepoint %d actions "
15498 "have no source form, ignoring them"),
409873ef 15499 utp->number);
00bf0b85 15500
f196051f
SS
15501 /* Copy any status information that might be available. */
15502 tp->base.hit_count = utp->hit_count;
15503 tp->traceframe_usage = utp->traceframe_usage;
15504
00bf0b85 15505 return tp;
d9b3f62e 15506}
00bf0b85 15507
1042e4c0
SS
15508/* Print information on tracepoint number TPNUM_EXP, or all if
15509 omitted. */
15510
15511static void
e5a67952 15512tracepoints_info (char *args, int from_tty)
1042e4c0 15513{
79a45e25 15514 struct ui_out *uiout = current_uiout;
e5a67952 15515 int num_printed;
1042e4c0 15516
e5a67952 15517 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
15518
15519 if (num_printed == 0)
1042e4c0 15520 {
e5a67952 15521 if (args == NULL || *args == '\0')
d77f58be
SS
15522 ui_out_message (uiout, 0, "No tracepoints.\n");
15523 else
e5a67952 15524 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 15525 }
ad443146
SS
15526
15527 default_collect_info ();
1042e4c0
SS
15528}
15529
4a64f543 15530/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
15531 Not supported by all targets. */
15532static void
15533enable_trace_command (char *args, int from_tty)
15534{
15535 enable_command (args, from_tty);
15536}
15537
4a64f543 15538/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
15539 Not supported by all targets. */
15540static void
15541disable_trace_command (char *args, int from_tty)
15542{
15543 disable_command (args, from_tty);
15544}
15545
4a64f543 15546/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
15547static void
15548delete_trace_command (char *arg, int from_tty)
15549{
35df4500 15550 struct breakpoint *b, *b_tmp;
1042e4c0
SS
15551
15552 dont_repeat ();
15553
15554 if (arg == 0)
15555 {
15556 int breaks_to_delete = 0;
15557
15558 /* Delete all breakpoints if no argument.
15559 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
15560 have to be deleted with an explicit breakpoint number
15561 argument. */
1042e4c0 15562 ALL_TRACEPOINTS (b)
46c6471b 15563 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
15564 {
15565 breaks_to_delete = 1;
15566 break;
15567 }
1042e4c0
SS
15568
15569 /* Ask user only if there are some breakpoints to delete. */
15570 if (!from_tty
15571 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15572 {
35df4500 15573 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 15574 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15575 delete_breakpoint (b);
1042e4c0
SS
15576 }
15577 }
15578 else
51be5b68 15579 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15580}
15581
197f0a60
TT
15582/* Helper function for trace_pass_command. */
15583
15584static void
d9b3f62e 15585trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15586{
d9b3f62e 15587 tp->pass_count = count;
6f6484cd 15588 observer_notify_breakpoint_modified (&tp->base);
197f0a60
TT
15589 if (from_tty)
15590 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15591 tp->base.number, count);
197f0a60
TT
15592}
15593
1042e4c0
SS
15594/* Set passcount for tracepoint.
15595
15596 First command argument is passcount, second is tracepoint number.
15597 If tracepoint number omitted, apply to most recently defined.
15598 Also accepts special argument "all". */
15599
15600static void
15601trace_pass_command (char *args, int from_tty)
15602{
d9b3f62e 15603 struct tracepoint *t1;
1042e4c0 15604 unsigned int count;
1042e4c0
SS
15605
15606 if (args == 0 || *args == 0)
3e43a32a
MS
15607 error (_("passcount command requires an "
15608 "argument (count + optional TP num)"));
1042e4c0 15609
4a64f543 15610 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0 15611
529480d0 15612 args = skip_spaces (args);
1042e4c0
SS
15613 if (*args && strncasecmp (args, "all", 3) == 0)
15614 {
d9b3f62e
PA
15615 struct breakpoint *b;
15616
1042e4c0 15617 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15618 if (*args)
15619 error (_("Junk at end of arguments."));
1042e4c0 15620
d9b3f62e 15621 ALL_TRACEPOINTS (b)
197f0a60 15622 {
d9b3f62e 15623 t1 = (struct tracepoint *) b;
197f0a60
TT
15624 trace_pass_set_count (t1, count, from_tty);
15625 }
15626 }
15627 else if (*args == '\0')
1042e4c0 15628 {
5fa1d40e 15629 t1 = get_tracepoint_by_number (&args, NULL);
1042e4c0 15630 if (t1)
197f0a60
TT
15631 trace_pass_set_count (t1, count, from_tty);
15632 }
15633 else
15634 {
15635 struct get_number_or_range_state state;
15636
15637 init_number_or_range (&state, args);
15638 while (!state.finished)
1042e4c0 15639 {
5fa1d40e 15640 t1 = get_tracepoint_by_number (&args, &state);
197f0a60
TT
15641 if (t1)
15642 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15643 }
15644 }
1042e4c0
SS
15645}
15646
d9b3f62e 15647struct tracepoint *
1042e4c0
SS
15648get_tracepoint (int num)
15649{
15650 struct breakpoint *t;
15651
15652 ALL_TRACEPOINTS (t)
15653 if (t->number == num)
d9b3f62e 15654 return (struct tracepoint *) t;
1042e4c0
SS
15655
15656 return NULL;
15657}
15658
d5551862
SS
15659/* Find the tracepoint with the given target-side number (which may be
15660 different from the tracepoint number after disconnecting and
15661 reconnecting). */
15662
d9b3f62e 15663struct tracepoint *
d5551862
SS
15664get_tracepoint_by_number_on_target (int num)
15665{
d9b3f62e 15666 struct breakpoint *b;
d5551862 15667
d9b3f62e
PA
15668 ALL_TRACEPOINTS (b)
15669 {
15670 struct tracepoint *t = (struct tracepoint *) b;
15671
15672 if (t->number_on_target == num)
15673 return t;
15674 }
d5551862
SS
15675
15676 return NULL;
15677}
15678
1042e4c0 15679/* Utility: parse a tracepoint number and look it up in the list.
197f0a60 15680 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
5fa1d40e
YQ
15681 If the argument is missing, the most recent tracepoint
15682 (tracepoint_count) is returned. */
15683
d9b3f62e 15684struct tracepoint *
197f0a60 15685get_tracepoint_by_number (char **arg,
5fa1d40e 15686 struct get_number_or_range_state *state)
1042e4c0 15687{
1042e4c0
SS
15688 struct breakpoint *t;
15689 int tpnum;
15690 char *instring = arg == NULL ? NULL : *arg;
15691
197f0a60
TT
15692 if (state)
15693 {
15694 gdb_assert (!state->finished);
15695 tpnum = get_number_or_range (state);
15696 }
15697 else if (arg == NULL || *arg == NULL || ! **arg)
5fa1d40e 15698 tpnum = tracepoint_count;
1042e4c0 15699 else
197f0a60 15700 tpnum = get_number (arg);
1042e4c0
SS
15701
15702 if (tpnum <= 0)
15703 {
15704 if (instring && *instring)
15705 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15706 instring);
15707 else
5fa1d40e 15708 printf_filtered (_("No previous tracepoint\n"));
1042e4c0
SS
15709 return NULL;
15710 }
15711
15712 ALL_TRACEPOINTS (t)
15713 if (t->number == tpnum)
15714 {
d9b3f62e 15715 return (struct tracepoint *) t;
1042e4c0
SS
15716 }
15717
1042e4c0
SS
15718 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15719 return NULL;
15720}
15721
d9b3f62e
PA
15722void
15723print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15724{
15725 if (b->thread != -1)
15726 fprintf_unfiltered (fp, " thread %d", b->thread);
15727
15728 if (b->task != 0)
15729 fprintf_unfiltered (fp, " task %d", b->task);
15730
15731 fprintf_unfiltered (fp, "\n");
15732}
15733
6149aea9
PA
15734/* Save information on user settable breakpoints (watchpoints, etc) to
15735 a new script file named FILENAME. If FILTER is non-NULL, call it
15736 on each breakpoint and only include the ones for which it returns
15737 non-zero. */
15738
1042e4c0 15739static void
6149aea9
PA
15740save_breakpoints (char *filename, int from_tty,
15741 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15742{
15743 struct breakpoint *tp;
6149aea9 15744 int any = 0;
1042e4c0 15745 struct cleanup *cleanup;
a7bdde9e 15746 struct ui_file *fp;
6149aea9 15747 int extra_trace_bits = 0;
1042e4c0 15748
6149aea9
PA
15749 if (filename == 0 || *filename == 0)
15750 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15751
15752 /* See if we have anything to save. */
6149aea9 15753 ALL_BREAKPOINTS (tp)
1042e4c0 15754 {
6149aea9 15755 /* Skip internal and momentary breakpoints. */
09d682a4 15756 if (!user_breakpoint_p (tp))
6149aea9
PA
15757 continue;
15758
15759 /* If we have a filter, only save the breakpoints it accepts. */
15760 if (filter && !filter (tp))
15761 continue;
15762
15763 any = 1;
15764
15765 if (is_tracepoint (tp))
15766 {
15767 extra_trace_bits = 1;
15768
15769 /* We can stop searching. */
15770 break;
15771 }
1042e4c0 15772 }
6149aea9
PA
15773
15774 if (!any)
1042e4c0 15775 {
6149aea9 15776 warning (_("Nothing to save."));
1042e4c0
SS
15777 return;
15778 }
15779
c718be47
PA
15780 filename = tilde_expand (filename);
15781 cleanup = make_cleanup (xfree, filename);
15782 fp = gdb_fopen (filename, "w");
059fb39f 15783 if (!fp)
6149aea9
PA
15784 error (_("Unable to open file '%s' for saving (%s)"),
15785 filename, safe_strerror (errno));
a7bdde9e 15786 make_cleanup_ui_file_delete (fp);
8bf6485c 15787
6149aea9
PA
15788 if (extra_trace_bits)
15789 save_trace_state_variables (fp);
8bf6485c 15790
6149aea9 15791 ALL_BREAKPOINTS (tp)
1042e4c0 15792 {
6149aea9 15793 /* Skip internal and momentary breakpoints. */
09d682a4 15794 if (!user_breakpoint_p (tp))
6149aea9 15795 continue;
8bf6485c 15796
6149aea9
PA
15797 /* If we have a filter, only save the breakpoints it accepts. */
15798 if (filter && !filter (tp))
15799 continue;
15800
348d480f 15801 tp->ops->print_recreate (tp, fp);
1042e4c0 15802
6149aea9
PA
15803 /* Note, we can't rely on tp->number for anything, as we can't
15804 assume the recreated breakpoint numbers will match. Use $bpnum
15805 instead. */
15806
15807 if (tp->cond_string)
15808 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15809
15810 if (tp->ignore_count)
15811 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15812
2d9442cc 15813 if (tp->type != bp_dprintf && tp->commands)
1042e4c0 15814 {
a7bdde9e
VP
15815 volatile struct gdb_exception ex;
15816
6149aea9 15817 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15818
79a45e25 15819 ui_out_redirect (current_uiout, fp);
14dba4b4 15820 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 15821 {
79a45e25 15822 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15823 }
79a45e25 15824 ui_out_redirect (current_uiout, NULL);
1042e4c0 15825
a7bdde9e
VP
15826 if (ex.reason < 0)
15827 throw_exception (ex);
1042e4c0 15828
a7bdde9e 15829 fprintf_unfiltered (fp, " end\n");
1042e4c0 15830 }
6149aea9
PA
15831
15832 if (tp->enable_state == bp_disabled)
15833 fprintf_unfiltered (fp, "disable\n");
15834
15835 /* If this is a multi-location breakpoint, check if the locations
15836 should be individually disabled. Watchpoint locations are
15837 special, and not user visible. */
15838 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15839 {
15840 struct bp_location *loc;
15841 int n = 1;
15842
15843 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15844 if (!loc->enabled)
15845 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15846 }
1042e4c0 15847 }
8bf6485c 15848
6149aea9 15849 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15850 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15851
1042e4c0 15852 if (from_tty)
6149aea9 15853 printf_filtered (_("Saved to file '%s'.\n"), filename);
c718be47 15854 do_cleanups (cleanup);
6149aea9
PA
15855}
15856
15857/* The `save breakpoints' command. */
15858
15859static void
15860save_breakpoints_command (char *args, int from_tty)
15861{
15862 save_breakpoints (args, from_tty, NULL);
15863}
15864
15865/* The `save tracepoints' command. */
15866
15867static void
15868save_tracepoints_command (char *args, int from_tty)
15869{
15870 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15871}
15872
15873/* Create a vector of all tracepoints. */
15874
15875VEC(breakpoint_p) *
eeae04df 15876all_tracepoints (void)
1042e4c0
SS
15877{
15878 VEC(breakpoint_p) *tp_vec = 0;
15879 struct breakpoint *tp;
15880
15881 ALL_TRACEPOINTS (tp)
15882 {
15883 VEC_safe_push (breakpoint_p, tp_vec, tp);
15884 }
15885
15886 return tp_vec;
15887}
15888
c906108c 15889\f
4a64f543
MS
15890/* This help string is used for the break, hbreak, tbreak and thbreak
15891 commands. It is defined as a macro to prevent duplication.
15892 COMMAND should be a string constant containing the name of the
15893 command. */
31e2b00f 15894#define BREAK_ARGS_HELP(command) \
fb7b5af4
SDJ
15895command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15896PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15897probe point. Accepted values are `-probe' (for a generic, automatically\n\
15898guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
31e2b00f
AS
15899LOCATION may be a line number, function name, or \"*\" and an address.\n\
15900If a line number is specified, break at start of code for that line.\n\
15901If a function is specified, break at start of code for that function.\n\
15902If an address is specified, break at that exact address.\n\
dc10affe
PA
15903With no LOCATION, uses current execution address of the selected\n\
15904stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15905\n\
15906THREADNUM is the number from \"info threads\".\n\
15907CONDITION is a boolean expression.\n\
15908\n\
d41c0fc8
PA
15909Multiple breakpoints at one place are permitted, and useful if their\n\
15910conditions are different.\n\
31e2b00f
AS
15911\n\
15912Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15913
44feb3ce
TT
15914/* List of subcommands for "catch". */
15915static struct cmd_list_element *catch_cmdlist;
15916
15917/* List of subcommands for "tcatch". */
15918static struct cmd_list_element *tcatch_cmdlist;
15919
9ac4176b 15920void
44feb3ce
TT
15921add_catch_command (char *name, char *docstring,
15922 void (*sfunc) (char *args, int from_tty,
15923 struct cmd_list_element *command),
625e8578 15924 completer_ftype *completer,
44feb3ce
TT
15925 void *user_data_catch,
15926 void *user_data_tcatch)
15927{
15928 struct cmd_list_element *command;
15929
15930 command = add_cmd (name, class_breakpoint, NULL, docstring,
15931 &catch_cmdlist);
15932 set_cmd_sfunc (command, sfunc);
15933 set_cmd_context (command, user_data_catch);
a96d9b2e 15934 set_cmd_completer (command, completer);
44feb3ce
TT
15935
15936 command = add_cmd (name, class_breakpoint, NULL, docstring,
15937 &tcatch_cmdlist);
15938 set_cmd_sfunc (command, sfunc);
15939 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15940 set_cmd_completer (command, completer);
44feb3ce
TT
15941}
15942
6c95b8df 15943static void
a79b8f6e 15944clear_syscall_counts (struct inferior *inf)
6c95b8df 15945{
fa3064dd
YQ
15946 struct catch_syscall_inferior_data *inf_data
15947 = get_catch_syscall_inferior_data (inf);
15948
15949 inf_data->total_syscalls_count = 0;
15950 inf_data->any_syscall_count = 0;
15951 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
15952}
15953
6149aea9
PA
15954static void
15955save_command (char *arg, int from_tty)
15956{
3e43a32a
MS
15957 printf_unfiltered (_("\"save\" must be followed by "
15958 "the name of a save subcommand.\n"));
6149aea9
PA
15959 help_list (save_cmdlist, "save ", -1, gdb_stdout);
15960}
15961
84f4c1fe
PM
15962struct breakpoint *
15963iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15964 void *data)
15965{
35df4500 15966 struct breakpoint *b, *b_tmp;
84f4c1fe 15967
35df4500 15968 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15969 {
15970 if ((*callback) (b, data))
15971 return b;
15972 }
15973
15974 return NULL;
15975}
15976
0574c78f
GB
15977/* Zero if any of the breakpoint's locations could be a location where
15978 functions have been inlined, nonzero otherwise. */
15979
15980static int
15981is_non_inline_function (struct breakpoint *b)
15982{
15983 /* The shared library event breakpoint is set on the address of a
15984 non-inline function. */
15985 if (b->type == bp_shlib_event)
15986 return 1;
15987
15988 return 0;
15989}
15990
15991/* Nonzero if the specified PC cannot be a location where functions
15992 have been inlined. */
15993
15994int
09ac7c10
TT
15995pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15996 const struct target_waitstatus *ws)
0574c78f
GB
15997{
15998 struct breakpoint *b;
15999 struct bp_location *bl;
16000
16001 ALL_BREAKPOINTS (b)
16002 {
16003 if (!is_non_inline_function (b))
16004 continue;
16005
16006 for (bl = b->loc; bl != NULL; bl = bl->next)
16007 {
16008 if (!bl->shlib_disabled
09ac7c10 16009 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
16010 return 1;
16011 }
16012 }
16013
16014 return 0;
16015}
16016
2f202fde
JK
16017/* Remove any references to OBJFILE which is going to be freed. */
16018
16019void
16020breakpoint_free_objfile (struct objfile *objfile)
16021{
16022 struct bp_location **locp, *loc;
16023
16024 ALL_BP_LOCATIONS (loc, locp)
16025 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
16026 loc->symtab = NULL;
16027}
16028
2060206e
PA
16029void
16030initialize_breakpoint_ops (void)
16031{
16032 static int initialized = 0;
16033
16034 struct breakpoint_ops *ops;
16035
16036 if (initialized)
16037 return;
16038 initialized = 1;
16039
16040 /* The breakpoint_ops structure to be inherit by all kinds of
16041 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16042 internal and momentary breakpoints, etc.). */
16043 ops = &bkpt_base_breakpoint_ops;
16044 *ops = base_breakpoint_ops;
16045 ops->re_set = bkpt_re_set;
16046 ops->insert_location = bkpt_insert_location;
16047 ops->remove_location = bkpt_remove_location;
16048 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
16049 ops->create_sals_from_address = bkpt_create_sals_from_address;
16050 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16051 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
16052
16053 /* The breakpoint_ops structure to be used in regular breakpoints. */
16054 ops = &bkpt_breakpoint_ops;
16055 *ops = bkpt_base_breakpoint_ops;
16056 ops->re_set = bkpt_re_set;
16057 ops->resources_needed = bkpt_resources_needed;
16058 ops->print_it = bkpt_print_it;
16059 ops->print_mention = bkpt_print_mention;
16060 ops->print_recreate = bkpt_print_recreate;
16061
16062 /* Ranged breakpoints. */
16063 ops = &ranged_breakpoint_ops;
16064 *ops = bkpt_breakpoint_ops;
16065 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16066 ops->resources_needed = resources_needed_ranged_breakpoint;
16067 ops->print_it = print_it_ranged_breakpoint;
16068 ops->print_one = print_one_ranged_breakpoint;
16069 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16070 ops->print_mention = print_mention_ranged_breakpoint;
16071 ops->print_recreate = print_recreate_ranged_breakpoint;
16072
16073 /* Internal breakpoints. */
16074 ops = &internal_breakpoint_ops;
16075 *ops = bkpt_base_breakpoint_ops;
16076 ops->re_set = internal_bkpt_re_set;
16077 ops->check_status = internal_bkpt_check_status;
16078 ops->print_it = internal_bkpt_print_it;
16079 ops->print_mention = internal_bkpt_print_mention;
16080
16081 /* Momentary breakpoints. */
16082 ops = &momentary_breakpoint_ops;
16083 *ops = bkpt_base_breakpoint_ops;
16084 ops->re_set = momentary_bkpt_re_set;
16085 ops->check_status = momentary_bkpt_check_status;
16086 ops->print_it = momentary_bkpt_print_it;
16087 ops->print_mention = momentary_bkpt_print_mention;
16088
e2e4d78b
JK
16089 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16090 ops = &longjmp_breakpoint_ops;
16091 *ops = momentary_breakpoint_ops;
16092 ops->dtor = longjmp_bkpt_dtor;
16093
55aa24fb
SDJ
16094 /* Probe breakpoints. */
16095 ops = &bkpt_probe_breakpoint_ops;
16096 *ops = bkpt_breakpoint_ops;
16097 ops->insert_location = bkpt_probe_insert_location;
16098 ops->remove_location = bkpt_probe_remove_location;
16099 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16100 ops->decode_linespec = bkpt_probe_decode_linespec;
16101
2060206e
PA
16102 /* Watchpoints. */
16103 ops = &watchpoint_breakpoint_ops;
16104 *ops = base_breakpoint_ops;
3a5c3e22 16105 ops->dtor = dtor_watchpoint;
2060206e
PA
16106 ops->re_set = re_set_watchpoint;
16107 ops->insert_location = insert_watchpoint;
16108 ops->remove_location = remove_watchpoint;
16109 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16110 ops->check_status = check_status_watchpoint;
16111 ops->resources_needed = resources_needed_watchpoint;
16112 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16113 ops->print_it = print_it_watchpoint;
16114 ops->print_mention = print_mention_watchpoint;
16115 ops->print_recreate = print_recreate_watchpoint;
427cd150 16116 ops->explains_signal = explains_signal_watchpoint;
2060206e
PA
16117
16118 /* Masked watchpoints. */
16119 ops = &masked_watchpoint_breakpoint_ops;
16120 *ops = watchpoint_breakpoint_ops;
16121 ops->insert_location = insert_masked_watchpoint;
16122 ops->remove_location = remove_masked_watchpoint;
16123 ops->resources_needed = resources_needed_masked_watchpoint;
16124 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16125 ops->print_it = print_it_masked_watchpoint;
16126 ops->print_one_detail = print_one_detail_masked_watchpoint;
16127 ops->print_mention = print_mention_masked_watchpoint;
16128 ops->print_recreate = print_recreate_masked_watchpoint;
16129
16130 /* Tracepoints. */
16131 ops = &tracepoint_breakpoint_ops;
16132 *ops = base_breakpoint_ops;
16133 ops->re_set = tracepoint_re_set;
16134 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16135 ops->print_one_detail = tracepoint_print_one_detail;
16136 ops->print_mention = tracepoint_print_mention;
16137 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
16138 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16139 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16140 ops->decode_linespec = tracepoint_decode_linespec;
16141
55aa24fb
SDJ
16142 /* Probe tracepoints. */
16143 ops = &tracepoint_probe_breakpoint_ops;
16144 *ops = tracepoint_breakpoint_ops;
16145 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16146 ops->decode_linespec = tracepoint_probe_decode_linespec;
16147
983af33b
SDJ
16148 /* Static tracepoints with marker (`-m'). */
16149 ops = &strace_marker_breakpoint_ops;
16150 *ops = tracepoint_breakpoint_ops;
16151 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16152 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16153 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
16154
16155 /* Fork catchpoints. */
16156 ops = &catch_fork_breakpoint_ops;
16157 *ops = base_breakpoint_ops;
16158 ops->insert_location = insert_catch_fork;
16159 ops->remove_location = remove_catch_fork;
16160 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16161 ops->print_it = print_it_catch_fork;
16162 ops->print_one = print_one_catch_fork;
16163 ops->print_mention = print_mention_catch_fork;
16164 ops->print_recreate = print_recreate_catch_fork;
16165
16166 /* Vfork catchpoints. */
16167 ops = &catch_vfork_breakpoint_ops;
16168 *ops = base_breakpoint_ops;
16169 ops->insert_location = insert_catch_vfork;
16170 ops->remove_location = remove_catch_vfork;
16171 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16172 ops->print_it = print_it_catch_vfork;
16173 ops->print_one = print_one_catch_vfork;
16174 ops->print_mention = print_mention_catch_vfork;
16175 ops->print_recreate = print_recreate_catch_vfork;
16176
16177 /* Exec catchpoints. */
16178 ops = &catch_exec_breakpoint_ops;
16179 *ops = base_breakpoint_ops;
16180 ops->dtor = dtor_catch_exec;
16181 ops->insert_location = insert_catch_exec;
16182 ops->remove_location = remove_catch_exec;
16183 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16184 ops->print_it = print_it_catch_exec;
16185 ops->print_one = print_one_catch_exec;
16186 ops->print_mention = print_mention_catch_exec;
16187 ops->print_recreate = print_recreate_catch_exec;
16188
16189 /* Syscall catchpoints. */
16190 ops = &catch_syscall_breakpoint_ops;
16191 *ops = base_breakpoint_ops;
16192 ops->dtor = dtor_catch_syscall;
16193 ops->insert_location = insert_catch_syscall;
16194 ops->remove_location = remove_catch_syscall;
16195 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16196 ops->print_it = print_it_catch_syscall;
16197 ops->print_one = print_one_catch_syscall;
16198 ops->print_mention = print_mention_catch_syscall;
16199 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
16200
16201 /* Solib-related catchpoints. */
16202 ops = &catch_solib_breakpoint_ops;
16203 *ops = base_breakpoint_ops;
16204 ops->dtor = dtor_catch_solib;
16205 ops->insert_location = insert_catch_solib;
16206 ops->remove_location = remove_catch_solib;
16207 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16208 ops->check_status = check_status_catch_solib;
16209 ops->print_it = print_it_catch_solib;
16210 ops->print_one = print_one_catch_solib;
16211 ops->print_mention = print_mention_catch_solib;
16212 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
16213
16214 ops = &dprintf_breakpoint_ops;
16215 *ops = bkpt_base_breakpoint_ops;
5c2b4418 16216 ops->re_set = dprintf_re_set;
e7e0cddf
SS
16217 ops->resources_needed = bkpt_resources_needed;
16218 ops->print_it = bkpt_print_it;
16219 ops->print_mention = bkpt_print_mention;
2d9442cc 16220 ops->print_recreate = dprintf_print_recreate;
9d6e6e84 16221 ops->after_condition_true = dprintf_after_condition_true;
2060206e
PA
16222}
16223
8bfd80db
YQ
16224/* Chain containing all defined "enable breakpoint" subcommands. */
16225
16226static struct cmd_list_element *enablebreaklist = NULL;
16227
c906108c 16228void
fba45db2 16229_initialize_breakpoint (void)
c906108c
SS
16230{
16231 struct cmd_list_element *c;
16232
2060206e
PA
16233 initialize_breakpoint_ops ();
16234
84acb35a 16235 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
63644780 16236 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
6c95b8df 16237 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 16238 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 16239
55aa24fb
SDJ
16240 breakpoint_objfile_key
16241 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 16242
fa3064dd 16243 catch_syscall_inferior_data
8e260fc0
TT
16244 = register_inferior_data_with_cleanup (NULL,
16245 catch_syscall_inferior_data_cleanup);
fa3064dd 16246
c906108c
SS
16247 breakpoint_chain = 0;
16248 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16249 before a breakpoint is set. */
16250 breakpoint_count = 0;
16251
1042e4c0
SS
16252 tracepoint_count = 0;
16253
1bedd215
AC
16254 add_com ("ignore", class_breakpoint, ignore_command, _("\
16255Set ignore-count of breakpoint number N to COUNT.\n\
16256Usage is `ignore N COUNT'."));
c906108c 16257 if (xdb_commands)
c5aa993b 16258 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 16259
1bedd215
AC
16260 add_com ("commands", class_breakpoint, commands_command, _("\
16261Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
16262Give breakpoint number as argument after \"commands\".\n\
16263With no argument, the targeted breakpoint is the last one set.\n\
16264The commands themselves follow starting on the next line.\n\
16265Type a line containing \"end\" to indicate the end of them.\n\
16266Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 16267then no output is printed when it is hit, except what the commands print."));
c906108c 16268
d55637df 16269 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 16270Specify breakpoint number N to break only if COND is true.\n\
c906108c 16271Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 16272expression to be evaluated whenever breakpoint N is reached."));
d55637df 16273 set_cmd_completer (c, condition_completer);
c906108c 16274
1bedd215 16275 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 16276Set a temporary breakpoint.\n\
c906108c
SS
16277Like \"break\" except the breakpoint is only temporary,\n\
16278so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
16279by using \"enable delete\" on the breakpoint number.\n\
16280\n"
16281BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 16282 set_cmd_completer (c, location_completer);
c94fdfd0 16283
1bedd215 16284 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 16285Set a hardware assisted breakpoint.\n\
c906108c 16286Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
16287some target hardware may not have this support.\n\
16288\n"
16289BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 16290 set_cmd_completer (c, location_completer);
c906108c 16291
1bedd215 16292 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 16293Set a temporary hardware assisted breakpoint.\n\
c906108c 16294Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
16295so it will be deleted when hit.\n\
16296\n"
16297BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 16298 set_cmd_completer (c, location_completer);
c906108c 16299
1bedd215
AC
16300 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16301Enable some breakpoints.\n\
c906108c
SS
16302Give breakpoint numbers (separated by spaces) as arguments.\n\
16303With no subcommand, breakpoints are enabled until you command otherwise.\n\
16304This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16305With a subcommand you can enable temporarily."),
c906108c
SS
16306 &enablelist, "enable ", 1, &cmdlist);
16307 if (xdb_commands)
1bedd215
AC
16308 add_com ("ab", class_breakpoint, enable_command, _("\
16309Enable some breakpoints.\n\
c906108c
SS
16310Give breakpoint numbers (separated by spaces) as arguments.\n\
16311With no subcommand, breakpoints are enabled until you command otherwise.\n\
16312This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16313With a subcommand you can enable temporarily."));
c906108c
SS
16314
16315 add_com_alias ("en", "enable", class_breakpoint, 1);
16316
84951ab5 16317 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 16318Enable some breakpoints.\n\
c906108c
SS
16319Give breakpoint numbers (separated by spaces) as arguments.\n\
16320This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 16321May be abbreviated to simply \"enable\".\n"),
c5aa993b 16322 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 16323
1a966eab
AC
16324 add_cmd ("once", no_class, enable_once_command, _("\
16325Enable breakpoints for one hit. Give breakpoint numbers.\n\
16326If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
16327 &enablebreaklist);
16328
1a966eab
AC
16329 add_cmd ("delete", no_class, enable_delete_command, _("\
16330Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16331If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16332 &enablebreaklist);
16333
816338b5
SS
16334 add_cmd ("count", no_class, enable_count_command, _("\
16335Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16336If a breakpoint is hit while enabled in this fashion,\n\
16337the count is decremented; when it reaches zero, the breakpoint is disabled."),
16338 &enablebreaklist);
16339
1a966eab
AC
16340 add_cmd ("delete", no_class, enable_delete_command, _("\
16341Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16342If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
16343 &enablelist);
16344
1a966eab
AC
16345 add_cmd ("once", no_class, enable_once_command, _("\
16346Enable breakpoints for one hit. Give breakpoint numbers.\n\
16347If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
16348 &enablelist);
16349
16350 add_cmd ("count", no_class, enable_count_command, _("\
16351Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16352If a breakpoint is hit while enabled in this fashion,\n\
16353the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
16354 &enablelist);
16355
1bedd215
AC
16356 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16357Disable some breakpoints.\n\
c906108c
SS
16358Arguments are breakpoint numbers with spaces in between.\n\
16359To disable all breakpoints, give no argument.\n\
64b9b334 16360A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
16361 &disablelist, "disable ", 1, &cmdlist);
16362 add_com_alias ("dis", "disable", class_breakpoint, 1);
16363 add_com_alias ("disa", "disable", class_breakpoint, 1);
16364 if (xdb_commands)
1bedd215
AC
16365 add_com ("sb", class_breakpoint, disable_command, _("\
16366Disable some breakpoints.\n\
c906108c
SS
16367Arguments are breakpoint numbers with spaces in between.\n\
16368To disable all breakpoints, give no argument.\n\
64b9b334 16369A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 16370
1a966eab
AC
16371 add_cmd ("breakpoints", class_alias, disable_command, _("\
16372Disable some breakpoints.\n\
c906108c
SS
16373Arguments are breakpoint numbers with spaces in between.\n\
16374To disable all breakpoints, give no argument.\n\
64b9b334 16375A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 16376This command may be abbreviated \"disable\"."),
c906108c
SS
16377 &disablelist);
16378
1bedd215
AC
16379 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16380Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16381Arguments are breakpoint numbers with spaces in between.\n\
16382To delete all breakpoints, give no argument.\n\
16383\n\
16384Also a prefix command for deletion of other GDB objects.\n\
1bedd215 16385The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
16386 &deletelist, "delete ", 1, &cmdlist);
16387 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 16388 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 16389 if (xdb_commands)
1bedd215
AC
16390 add_com ("db", class_breakpoint, delete_command, _("\
16391Delete some breakpoints.\n\
c906108c 16392Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 16393To delete all breakpoints, give no argument.\n"));
c906108c 16394
1a966eab
AC
16395 add_cmd ("breakpoints", class_alias, delete_command, _("\
16396Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
16397Arguments are breakpoint numbers with spaces in between.\n\
16398To delete all breakpoints, give no argument.\n\
1a966eab 16399This command may be abbreviated \"delete\"."),
c906108c
SS
16400 &deletelist);
16401
1bedd215
AC
16402 add_com ("clear", class_breakpoint, clear_command, _("\
16403Clear breakpoint at specified line or function.\n\
c906108c
SS
16404Argument may be line number, function name, or \"*\" and an address.\n\
16405If line number is specified, all breakpoints in that line are cleared.\n\
16406If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
16407If an address is specified, breakpoints at that address are cleared.\n\
16408\n\
16409With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
16410is executing in.\n\
16411\n\
1bedd215 16412See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 16413 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 16414
1bedd215 16415 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
16416Set breakpoint at specified line or function.\n"
16417BREAK_ARGS_HELP ("break")));
5ba2abeb 16418 set_cmd_completer (c, location_completer);
c94fdfd0 16419
c906108c
SS
16420 add_com_alias ("b", "break", class_run, 1);
16421 add_com_alias ("br", "break", class_run, 1);
16422 add_com_alias ("bre", "break", class_run, 1);
16423 add_com_alias ("brea", "break", class_run, 1);
16424
7681d515
PM
16425 if (xdb_commands)
16426 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
16427
16428 if (dbx_commands)
16429 {
1bedd215
AC
16430 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16431Break in function/address or break at a line in the current file."),
c5aa993b
JM
16432 &stoplist, "stop ", 1, &cmdlist);
16433 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 16434 _("Break in function or address."), &stoplist);
c5aa993b 16435 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 16436 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
16437 add_com ("status", class_info, breakpoints_info, _("\
16438Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16439The \"Type\" column indicates one of:\n\
16440\tbreakpoint - normal breakpoint\n\
16441\twatchpoint - watchpoint\n\
16442The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16443the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16444breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16445address and file/line number respectively.\n\
16446\n\
16447Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16448are set to the address of the last breakpoint listed unless the command\n\
16449is prefixed with \"server \".\n\n\
c906108c 16450Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16451breakpoint set."));
c906108c
SS
16452 }
16453
1bedd215 16454 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 16455Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
16456The \"Type\" column indicates one of:\n\
16457\tbreakpoint - normal breakpoint\n\
16458\twatchpoint - watchpoint\n\
16459The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16460the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16461breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16462address and file/line number respectively.\n\
16463\n\
16464Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16465are set to the address of the last breakpoint listed unless the command\n\
16466is prefixed with \"server \".\n\n\
c906108c 16467Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16468breakpoint set."));
c906108c 16469
6b04bdb7
MS
16470 add_info_alias ("b", "breakpoints", 1);
16471
c906108c 16472 if (xdb_commands)
1bedd215
AC
16473 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16474Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16475The \"Type\" column indicates one of:\n\
16476\tbreakpoint - normal breakpoint\n\
16477\twatchpoint - watchpoint\n\
16478The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16479the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16480breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
16481address and file/line number respectively.\n\
16482\n\
16483Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16484are set to the address of the last breakpoint listed unless the command\n\
16485is prefixed with \"server \".\n\n\
c906108c 16486Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 16487breakpoint set."));
c906108c 16488
1a966eab
AC
16489 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16490Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
16491The \"Type\" column indicates one of:\n\
16492\tbreakpoint - normal breakpoint\n\
16493\twatchpoint - watchpoint\n\
16494\tlongjmp - internal breakpoint used to step through longjmp()\n\
16495\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16496\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
16497\tfinish - internal breakpoint used by the \"finish\" command\n\
16498The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
16499the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16500breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
16501address and file/line number respectively.\n\
16502\n\
16503Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
16504are set to the address of the last breakpoint listed unless the command\n\
16505is prefixed with \"server \".\n\n\
c906108c 16506Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 16507breakpoint set."),
c906108c
SS
16508 &maintenanceinfolist);
16509
44feb3ce
TT
16510 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16511Set catchpoints to catch events."),
16512 &catch_cmdlist, "catch ",
16513 0/*allow-unknown*/, &cmdlist);
16514
16515 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16516Set temporary catchpoints to catch events."),
16517 &tcatch_cmdlist, "tcatch ",
16518 0/*allow-unknown*/, &cmdlist);
16519
44feb3ce
TT
16520 add_catch_command ("fork", _("Catch calls to fork."),
16521 catch_fork_command_1,
a96d9b2e 16522 NULL,
44feb3ce
TT
16523 (void *) (uintptr_t) catch_fork_permanent,
16524 (void *) (uintptr_t) catch_fork_temporary);
16525 add_catch_command ("vfork", _("Catch calls to vfork."),
16526 catch_fork_command_1,
a96d9b2e 16527 NULL,
44feb3ce
TT
16528 (void *) (uintptr_t) catch_vfork_permanent,
16529 (void *) (uintptr_t) catch_vfork_temporary);
16530 add_catch_command ("exec", _("Catch calls to exec."),
16531 catch_exec_command_1,
a96d9b2e
SDJ
16532 NULL,
16533 CATCH_PERMANENT,
16534 CATCH_TEMPORARY);
edcc5120
TT
16535 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16536Usage: catch load [REGEX]\n\
16537If REGEX is given, only stop for libraries matching the regular expression."),
16538 catch_load_command_1,
16539 NULL,
16540 CATCH_PERMANENT,
16541 CATCH_TEMPORARY);
16542 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16543Usage: catch unload [REGEX]\n\
16544If REGEX is given, only stop for libraries matching the regular expression."),
16545 catch_unload_command_1,
16546 NULL,
16547 CATCH_PERMANENT,
16548 CATCH_TEMPORARY);
a96d9b2e
SDJ
16549 add_catch_command ("syscall", _("\
16550Catch system calls by their names and/or numbers.\n\
16551Arguments say which system calls to catch. If no arguments\n\
16552are given, every system call will be caught.\n\
16553Arguments, if given, should be one or more system call names\n\
16554(if your system supports that), or system call numbers."),
16555 catch_syscall_command_1,
16556 catch_syscall_completer,
44feb3ce
TT
16557 CATCH_PERMANENT,
16558 CATCH_TEMPORARY);
c5aa993b 16559
1bedd215
AC
16560 c = add_com ("watch", class_breakpoint, watch_command, _("\
16561Set a watchpoint for an expression.\n\
06a64a0b 16562Usage: watch [-l|-location] EXPRESSION\n\
c906108c 16563A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16564an expression changes.\n\
16565If -l or -location is given, this evaluates EXPRESSION and watches\n\
16566the memory to which it refers."));
65d12d83 16567 set_cmd_completer (c, expression_completer);
c906108c 16568
1bedd215
AC
16569 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16570Set a read watchpoint for an expression.\n\
06a64a0b 16571Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16572A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16573an expression is read.\n\
16574If -l or -location is given, this evaluates EXPRESSION and watches\n\
16575the memory to which it refers."));
65d12d83 16576 set_cmd_completer (c, expression_completer);
c906108c 16577
1bedd215
AC
16578 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16579Set a watchpoint for an expression.\n\
06a64a0b 16580Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16581A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16582an expression is either read or written.\n\
16583If -l or -location is given, this evaluates EXPRESSION and watches\n\
16584the memory to which it refers."));
65d12d83 16585 set_cmd_completer (c, expression_completer);
c906108c 16586
d77f58be 16587 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16588Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16589
920d2a44
AC
16590 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16591 respond to changes - contrary to the description. */
85c07804
AC
16592 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16593 &can_use_hw_watchpoints, _("\
16594Set debugger's willingness to use watchpoint hardware."), _("\
16595Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16596If zero, gdb will not use hardware for new watchpoints, even if\n\
16597such is available. (However, any hardware watchpoints that were\n\
16598created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16599hardware.)"),
16600 NULL,
920d2a44 16601 show_can_use_hw_watchpoints,
85c07804 16602 &setlist, &showlist);
c906108c
SS
16603
16604 can_use_hw_watchpoints = 1;
fa8d40ab 16605
1042e4c0
SS
16606 /* Tracepoint manipulation commands. */
16607
16608 c = add_com ("trace", class_breakpoint, trace_command, _("\
16609Set a tracepoint at specified line or function.\n\
16610\n"
16611BREAK_ARGS_HELP ("trace") "\n\
16612Do \"help tracepoints\" for info on other tracepoint commands."));
16613 set_cmd_completer (c, location_completer);
16614
16615 add_com_alias ("tp", "trace", class_alias, 0);
16616 add_com_alias ("tr", "trace", class_alias, 1);
16617 add_com_alias ("tra", "trace", class_alias, 1);
16618 add_com_alias ("trac", "trace", class_alias, 1);
16619
7a697b8d
SS
16620 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16621Set a fast tracepoint at specified line or function.\n\
16622\n"
16623BREAK_ARGS_HELP ("ftrace") "\n\
16624Do \"help tracepoints\" for info on other tracepoint commands."));
16625 set_cmd_completer (c, location_completer);
16626
0fb4aa4b
PA
16627 c = add_com ("strace", class_breakpoint, strace_command, _("\
16628Set a static tracepoint at specified line, function or marker.\n\
16629\n\
16630strace [LOCATION] [if CONDITION]\n\
16631LOCATION may be a line number, function name, \"*\" and an address,\n\
16632or -m MARKER_ID.\n\
16633If a line number is specified, probe the marker at start of code\n\
16634for that line. If a function is specified, probe the marker at start\n\
16635of code for that function. If an address is specified, probe the marker\n\
16636at that exact address. If a marker id is specified, probe the marker\n\
16637with that name. With no LOCATION, uses current execution address of\n\
16638the selected stack frame.\n\
16639Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16640This collects arbitrary user data passed in the probe point call to the\n\
16641tracing library. You can inspect it when analyzing the trace buffer,\n\
16642by printing the $_sdata variable like any other convenience variable.\n\
16643\n\
16644CONDITION is a boolean expression.\n\
16645\n\
d41c0fc8
PA
16646Multiple tracepoints at one place are permitted, and useful if their\n\
16647conditions are different.\n\
0fb4aa4b
PA
16648\n\
16649Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16650Do \"help tracepoints\" for info on other tracepoint commands."));
16651 set_cmd_completer (c, location_completer);
16652
1042e4c0 16653 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16654Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16655Convenience variable \"$tpnum\" contains the number of the\n\
16656last tracepoint set."));
16657
16658 add_info_alias ("tp", "tracepoints", 1);
16659
16660 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16661Delete specified tracepoints.\n\
16662Arguments are tracepoint numbers, separated by spaces.\n\
16663No argument means delete all tracepoints."),
16664 &deletelist);
7e20dfcd 16665 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
1042e4c0
SS
16666
16667 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16668Disable specified tracepoints.\n\
16669Arguments are tracepoint numbers, separated by spaces.\n\
16670No argument means disable all tracepoints."),
16671 &disablelist);
16672 deprecate_cmd (c, "disable");
16673
16674 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16675Enable specified tracepoints.\n\
16676Arguments are tracepoint numbers, separated by spaces.\n\
16677No argument means enable all tracepoints."),
16678 &enablelist);
16679 deprecate_cmd (c, "enable");
16680
16681 add_com ("passcount", class_trace, trace_pass_command, _("\
16682Set the passcount for a tracepoint.\n\
16683The trace will end when the tracepoint has been passed 'count' times.\n\
16684Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16685if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16686
6149aea9
PA
16687 add_prefix_cmd ("save", class_breakpoint, save_command,
16688 _("Save breakpoint definitions as a script."),
16689 &save_cmdlist, "save ",
16690 0/*allow-unknown*/, &cmdlist);
16691
16692 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16693Save current breakpoint definitions as a script.\n\
cce7e648 16694This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16695catchpoints, tracepoints). Use the 'source' command in another debug\n\
16696session to restore them."),
16697 &save_cmdlist);
16698 set_cmd_completer (c, filename_completer);
16699
16700 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16701Save current tracepoint definitions as a script.\n\
6149aea9
PA
16702Use the 'source' command in another debug session to restore them."),
16703 &save_cmdlist);
1042e4c0
SS
16704 set_cmd_completer (c, filename_completer);
16705
6149aea9
PA
16706 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16707 deprecate_cmd (c, "save tracepoints");
16708
1bedd215 16709 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16710Breakpoint specific settings\n\
16711Configure various breakpoint-specific variables such as\n\
1bedd215 16712pending breakpoint behavior"),
fa8d40ab
JJ
16713 &breakpoint_set_cmdlist, "set breakpoint ",
16714 0/*allow-unknown*/, &setlist);
1bedd215 16715 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16716Breakpoint specific settings\n\
16717Configure various breakpoint-specific variables such as\n\
1bedd215 16718pending breakpoint behavior"),
fa8d40ab
JJ
16719 &breakpoint_show_cmdlist, "show breakpoint ",
16720 0/*allow-unknown*/, &showlist);
16721
7915a72c
AC
16722 add_setshow_auto_boolean_cmd ("pending", no_class,
16723 &pending_break_support, _("\
16724Set debugger's behavior regarding pending breakpoints."), _("\
16725Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16726If on, an unrecognized breakpoint location will cause gdb to create a\n\
16727pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16728an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16729user-query to see if a pending breakpoint should be created."),
2c5b56ce 16730 NULL,
920d2a44 16731 show_pending_break_support,
6e1d7d6c
AC
16732 &breakpoint_set_cmdlist,
16733 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16734
16735 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16736
16737 add_setshow_boolean_cmd ("auto-hw", no_class,
16738 &automatic_hardware_breakpoints, _("\
16739Set automatic usage of hardware breakpoints."), _("\
16740Show automatic usage of hardware breakpoints."), _("\
16741If set, the debugger will automatically use hardware breakpoints for\n\
16742breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16743a warning will be emitted for such breakpoints."),
16744 NULL,
16745 show_automatic_hardware_breakpoints,
16746 &breakpoint_set_cmdlist,
16747 &breakpoint_show_cmdlist);
74960c60 16748
72d0e2c5
YQ
16749 add_setshow_auto_boolean_cmd ("always-inserted", class_support,
16750 &always_inserted_mode, _("\
74960c60
VP
16751Set mode for inserting breakpoints."), _("\
16752Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
16753When this mode is off, breakpoints are inserted in inferior when it is\n\
16754resumed, and removed when execution stops. When this mode is on,\n\
16755breakpoints are inserted immediately and removed only when the user\n\
16756deletes the breakpoint. When this mode is auto (which is the default),\n\
16757the behaviour depends on the non-stop setting (see help set non-stop).\n\
16758In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16759behaves as if always-inserted mode is on; if gdb is controlling the\n\
16760inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
72d0e2c5
YQ
16761 NULL,
16762 &show_always_inserted_mode,
16763 &breakpoint_set_cmdlist,
16764 &breakpoint_show_cmdlist);
f1310107 16765
b775012e
LM
16766 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16767 condition_evaluation_enums,
16768 &condition_evaluation_mode_1, _("\
16769Set mode of breakpoint condition evaluation."), _("\
16770Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16771When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16772evaluated on the host's side by GDB. When it is set to \"target\",\n\
16773breakpoint conditions will be downloaded to the target (if the target\n\
16774supports such feature) and conditions will be evaluated on the target's side.\n\
16775If this is set to \"auto\" (default), this will be automatically set to\n\
16776\"target\" if it supports condition evaluation, otherwise it will\n\
16777be set to \"gdb\""),
16778 &set_condition_evaluation_mode,
16779 &show_condition_evaluation_mode,
16780 &breakpoint_set_cmdlist,
16781 &breakpoint_show_cmdlist);
16782
f1310107
TJB
16783 add_com ("break-range", class_breakpoint, break_range_command, _("\
16784Set a breakpoint for an address range.\n\
16785break-range START-LOCATION, END-LOCATION\n\
16786where START-LOCATION and END-LOCATION can be one of the following:\n\
16787 LINENUM, for that line in the current file,\n\
16788 FILE:LINENUM, for that line in that file,\n\
16789 +OFFSET, for that number of lines after the current line\n\
16790 or the start of the range\n\
16791 FUNCTION, for the first line in that function,\n\
16792 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16793 *ADDRESS, for the instruction at that address.\n\
16794\n\
16795The breakpoint will stop execution of the inferior whenever it executes\n\
16796an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16797range (including START-LOCATION and END-LOCATION)."));
16798
e7e0cddf
SS
16799 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16800Set a dynamic printf at specified line or function.\n\
16801dprintf location,format string,arg1,arg2,...\n\
16802location may be a line number, function name, or \"*\" and an address.\n\
16803If a line number is specified, break at start of code for that line.\n\
0e4777df 16804If a function is specified, break at start of code for that function."));
e7e0cddf
SS
16805 set_cmd_completer (c, location_completer);
16806
16807 add_setshow_enum_cmd ("dprintf-style", class_support,
16808 dprintf_style_enums, &dprintf_style, _("\
16809Set the style of usage for dynamic printf."), _("\
16810Show the style of usage for dynamic printf."), _("\
16811This setting chooses how GDB will do a dynamic printf.\n\
16812If the value is \"gdb\", then the printing is done by GDB to its own\n\
16813console, as with the \"printf\" command.\n\
16814If the value is \"call\", the print is done by calling a function in your\n\
16815program; by default printf(), but you can choose a different function or\n\
16816output stream by setting dprintf-function and dprintf-channel."),
16817 update_dprintf_commands, NULL,
16818 &setlist, &showlist);
16819
16820 dprintf_function = xstrdup ("printf");
16821 add_setshow_string_cmd ("dprintf-function", class_support,
16822 &dprintf_function, _("\
16823Set the function to use for dynamic printf"), _("\
16824Show the function to use for dynamic printf"), NULL,
16825 update_dprintf_commands, NULL,
16826 &setlist, &showlist);
16827
16828 dprintf_channel = xstrdup ("");
16829 add_setshow_string_cmd ("dprintf-channel", class_support,
16830 &dprintf_channel, _("\
16831Set the channel to use for dynamic printf"), _("\
16832Show the channel to use for dynamic printf"), NULL,
16833 update_dprintf_commands, NULL,
16834 &setlist, &showlist);
16835
d3ce09f5
SS
16836 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16837 &disconnected_dprintf, _("\
16838Set whether dprintf continues after GDB disconnects."), _("\
16839Show whether dprintf continues after GDB disconnects."), _("\
16840Use this to let dprintf commands continue to hit and produce output\n\
16841even if GDB disconnects or detaches from the target."),
16842 NULL,
16843 NULL,
16844 &setlist, &showlist);
16845
16846 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16847agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16848(target agent only) This is useful for formatted output in user-defined commands."));
16849
765dc015 16850 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16851
16852 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
49fa26b0 16853 observer_attach_thread_exit (remove_threaded_breakpoints);
c906108c 16854}
This page took 2.663454 seconds and 4 git commands to generate.