* lib/gdb.exp (default_gdb_init): Set gdb_test_file_name.
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
CommitLineData
c906108c 1/* Everything about breakpoints, for GDB.
8926118c 2
0b302171 3 Copyright (C) 1986-2012 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"
38#include "gdb_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"
edcc5120 69#include "gdb_regex.h"
b775012e 70#include "ax-gdb.h"
e2e4d78b 71#include "dummy-frame.h"
c906108c 72
1042e4c0
SS
73/* readline include files */
74#include "readline/readline.h"
75#include "readline/history.h"
76
77/* readline defines this. */
78#undef savestring
79
034dad6f 80#include "mi/mi-common.h"
7371cf6d 81#include "python/python.h"
104c1213 82
4a64f543 83/* Prototypes for local functions. */
c906108c 84
a14ed312 85static void enable_delete_command (char *, int);
c906108c 86
a14ed312 87static void enable_once_command (char *, int);
c906108c 88
816338b5
SS
89static void enable_count_command (char *, int);
90
a14ed312 91static void disable_command (char *, int);
c906108c 92
a14ed312 93static void enable_command (char *, int);
c906108c 94
95a42b64
TT
95static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
96 void *),
97 void *);
c906108c 98
a14ed312 99static void ignore_command (char *, int);
c906108c 100
4efb68b1 101static int breakpoint_re_set_one (void *);
c906108c 102
348d480f
PA
103static void breakpoint_re_set_default (struct breakpoint *);
104
983af33b
SDJ
105static void create_sals_from_address_default (char **,
106 struct linespec_result *,
107 enum bptype, char *,
108 char **);
109
110static void create_breakpoints_sal_default (struct gdbarch *,
111 struct linespec_result *,
112 struct linespec_sals *,
e7e0cddf 113 char *, char *, enum bptype,
983af33b
SDJ
114 enum bpdisp, int, int,
115 int,
116 const struct breakpoint_ops *,
44f238bb 117 int, int, int, unsigned);
983af33b
SDJ
118
119static void decode_linespec_default (struct breakpoint *, char **,
120 struct symtabs_and_lines *);
121
a14ed312 122static void clear_command (char *, int);
c906108c 123
a14ed312 124static void catch_command (char *, int);
c906108c 125
a9634178 126static int can_use_hardware_watchpoint (struct value *);
c906108c 127
98deb0da 128static void break_command_1 (char *, int, int);
c906108c 129
a14ed312 130static void mention (struct breakpoint *);
c906108c 131
348d480f
PA
132static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
133 enum bptype,
c0a91b2b 134 const struct breakpoint_ops *);
3742cc8b
YQ
135static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
136 const struct symtab_and_line *);
137
4a64f543
MS
138/* This function is used in gdbtk sources and thus can not be made
139 static. */
63c252f8 140struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 141 struct symtab_and_line,
c0a91b2b
TT
142 enum bptype,
143 const struct breakpoint_ops *);
c906108c 144
06edf0c0
PA
145static struct breakpoint *
146 momentary_breakpoint_from_master (struct breakpoint *orig,
147 enum bptype type,
c0a91b2b 148 const struct breakpoint_ops *ops);
06edf0c0 149
76897487
KB
150static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
151
a6d9a66e
UW
152static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
153 CORE_ADDR bpaddr,
88f7da05 154 enum bptype bptype);
76897487 155
6c95b8df
PA
156static void describe_other_breakpoints (struct gdbarch *,
157 struct program_space *, CORE_ADDR,
5af949e3 158 struct obj_section *, int);
c906108c 159
6c95b8df
PA
160static int breakpoint_address_match (struct address_space *aspace1,
161 CORE_ADDR addr1,
162 struct address_space *aspace2,
163 CORE_ADDR addr2);
164
85d721b8
PA
165static int watchpoint_locations_match (struct bp_location *loc1,
166 struct bp_location *loc2);
167
f1310107
TJB
168static int breakpoint_location_address_match (struct bp_location *bl,
169 struct address_space *aspace,
170 CORE_ADDR addr);
171
a14ed312 172static void breakpoints_info (char *, int);
c906108c 173
d77f58be
SS
174static void watchpoints_info (char *, int);
175
e5a67952
MS
176static int breakpoint_1 (char *, int,
177 int (*) (const struct breakpoint *));
c906108c 178
4efb68b1 179static int breakpoint_cond_eval (void *);
c906108c 180
4efb68b1 181static void cleanup_executing_breakpoints (void *);
c906108c 182
a14ed312 183static void commands_command (char *, int);
c906108c 184
a14ed312 185static void condition_command (char *, int);
c906108c 186
c5aa993b
JM
187typedef enum
188 {
189 mark_inserted,
190 mark_uninserted
191 }
192insertion_state_t;
c906108c 193
0bde7532 194static int remove_breakpoint (struct bp_location *, insertion_state_t);
6c95b8df 195static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
c906108c 196
e514a9d6 197static enum print_stop_action print_bp_stop_message (bpstat bs);
c906108c 198
4efb68b1 199static int watchpoint_check (void *);
c906108c 200
a14ed312 201static void maintenance_info_breakpoints (char *, int);
c906108c 202
a14ed312 203static int hw_breakpoint_used_count (void);
c906108c 204
a1398e0c
PA
205static int hw_watchpoint_use_count (struct breakpoint *);
206
207static int hw_watchpoint_used_count_others (struct breakpoint *except,
208 enum bptype type,
209 int *other_type_used);
c906108c 210
a14ed312 211static void hbreak_command (char *, int);
c906108c 212
a14ed312 213static void thbreak_command (char *, int);
c906108c 214
816338b5
SS
215static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
216 int count);
c906108c 217
a14ed312 218static void stop_command (char *arg, int from_tty);
7a292a7a 219
a14ed312 220static void stopin_command (char *arg, int from_tty);
7a292a7a 221
a14ed312 222static void stopat_command (char *arg, int from_tty);
7a292a7a 223
a14ed312 224static char *ep_parse_optional_if_clause (char **arg);
7a292a7a 225
d85310f7
MS
226static void catch_exception_command_1 (enum exception_event_kind ex_event,
227 char *arg, int tempflag, int from_tty);
7a292a7a 228
a14ed312 229static void tcatch_command (char *arg, int from_tty);
7a292a7a 230
d03285ec
UW
231static void detach_single_step_breakpoints (void);
232
6c95b8df
PA
233static int single_step_breakpoint_inserted_here_p (struct address_space *,
234 CORE_ADDR pc);
1aafd4da 235
fe3f5fa8 236static void free_bp_location (struct bp_location *loc);
f431efe5
PA
237static void incref_bp_location (struct bp_location *loc);
238static void decref_bp_location (struct bp_location **loc);
fe3f5fa8 239
39d61571 240static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
a5606eee 241
b60e7edf 242static void update_global_location_list (int);
a5606eee 243
b60e7edf 244static void update_global_location_list_nothrow (int);
74960c60 245
d77f58be 246static int is_hardware_watchpoint (const struct breakpoint *bpt);
74960c60
VP
247
248static void insert_breakpoint_locations (void);
a5606eee 249
a96d9b2e
SDJ
250static int syscall_catchpoint_p (struct breakpoint *b);
251
1042e4c0
SS
252static void tracepoints_info (char *, int);
253
254static void delete_trace_command (char *, int);
255
256static void enable_trace_command (char *, int);
257
258static void disable_trace_command (char *, int);
259
260static void trace_pass_command (char *, int);
261
9c06b0b4
TJB
262static int is_masked_watchpoint (const struct breakpoint *b);
263
b775012e
LM
264static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
265
983af33b
SDJ
266/* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
267 otherwise. */
268
269static int strace_marker_p (struct breakpoint *b);
0fb4aa4b 270
edcc5120
TT
271static void init_catchpoint (struct breakpoint *b,
272 struct gdbarch *gdbarch, int tempflag,
273 char *cond_string,
274 const struct breakpoint_ops *ops);
275
2060206e
PA
276/* The abstract base class all breakpoint_ops structures inherit
277 from. */
278static struct breakpoint_ops base_breakpoint_ops;
279
280/* The breakpoint_ops structure to be inherited by all breakpoint_ops
281 that are implemented on top of software or hardware breakpoints
282 (user breakpoints, internal and momentary breakpoints, etc.). */
283static struct breakpoint_ops bkpt_base_breakpoint_ops;
284
285/* Internal breakpoints class type. */
06edf0c0 286static struct breakpoint_ops internal_breakpoint_ops;
2060206e
PA
287
288/* Momentary breakpoints class type. */
06edf0c0
PA
289static struct breakpoint_ops momentary_breakpoint_ops;
290
e2e4d78b
JK
291/* Momentary breakpoints for bp_longjmp and bp_exception class type. */
292static struct breakpoint_ops longjmp_breakpoint_ops;
293
2060206e
PA
294/* The breakpoint_ops structure to be used in regular user created
295 breakpoints. */
296struct breakpoint_ops bkpt_breakpoint_ops;
297
55aa24fb
SDJ
298/* Breakpoints set on probes. */
299static struct breakpoint_ops bkpt_probe_breakpoint_ops;
300
e7e0cddf
SS
301/* Dynamic printf class type. */
302static struct breakpoint_ops dprintf_breakpoint_ops;
303
5cea2a26
PA
304/* A reference-counted struct command_line. This lets multiple
305 breakpoints share a single command list. */
306struct counted_command_line
307{
308 /* The reference count. */
309 int refc;
310
311 /* The command list. */
312 struct command_line *commands;
313};
314
315struct command_line *
316breakpoint_commands (struct breakpoint *b)
317{
318 return b->commands ? b->commands->commands : NULL;
319}
3daf8fe5 320
f3b1572e
PA
321/* Flag indicating that a command has proceeded the inferior past the
322 current breakpoint. */
323
324static int breakpoint_proceeded;
325
956a9fb9 326const char *
2cec12e5
AR
327bpdisp_text (enum bpdisp disp)
328{
4a64f543
MS
329 /* NOTE: the following values are a part of MI protocol and
330 represent values of 'disp' field returned when inferior stops at
331 a breakpoint. */
bc043ef3 332 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
cc59ec59 333
2cec12e5
AR
334 return bpdisps[(int) disp];
335}
c906108c 336
4a64f543 337/* Prototypes for exported functions. */
c906108c 338/* If FALSE, gdb will not use hardware support for watchpoints, even
4a64f543 339 if such is available. */
c906108c
SS
340static int can_use_hw_watchpoints;
341
920d2a44
AC
342static void
343show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
344 struct cmd_list_element *c,
345 const char *value)
346{
3e43a32a
MS
347 fprintf_filtered (file,
348 _("Debugger's willingness to use "
349 "watchpoint hardware is %s.\n"),
920d2a44
AC
350 value);
351}
352
fa8d40ab
JJ
353/* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
354 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
4a64f543 355 for unrecognized breakpoint locations.
fa8d40ab
JJ
356 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
357static enum auto_boolean pending_break_support;
920d2a44
AC
358static void
359show_pending_break_support (struct ui_file *file, int from_tty,
360 struct cmd_list_element *c,
361 const char *value)
362{
3e43a32a
MS
363 fprintf_filtered (file,
364 _("Debugger's behavior regarding "
365 "pending breakpoints is %s.\n"),
920d2a44
AC
366 value);
367}
fa8d40ab 368
765dc015 369/* If 1, gdb will automatically use hardware breakpoints for breakpoints
4a64f543 370 set with "break" but falling in read-only memory.
765dc015
VP
371 If 0, gdb will warn about such breakpoints, but won't automatically
372 use hardware breakpoints. */
373static int automatic_hardware_breakpoints;
374static void
375show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
376 struct cmd_list_element *c,
377 const char *value)
378{
3e43a32a
MS
379 fprintf_filtered (file,
380 _("Automatic usage of hardware breakpoints is %s.\n"),
765dc015
VP
381 value);
382}
383
33e5cbd6
PA
384/* If on, gdb will keep breakpoints inserted even as inferior is
385 stopped, and immediately insert any new breakpoints. If off, gdb
386 will insert breakpoints into inferior only when resuming it, and
387 will remove breakpoints upon stop. If auto, GDB will behave as ON
388 if in non-stop mode, and as OFF if all-stop mode.*/
389
390static const char always_inserted_auto[] = "auto";
391static const char always_inserted_on[] = "on";
392static const char always_inserted_off[] = "off";
40478521 393static const char *const always_inserted_enums[] = {
33e5cbd6
PA
394 always_inserted_auto,
395 always_inserted_off,
396 always_inserted_on,
397 NULL
398};
399static const char *always_inserted_mode = always_inserted_auto;
400static void
74960c60 401show_always_inserted_mode (struct ui_file *file, int from_tty,
33e5cbd6 402 struct cmd_list_element *c, const char *value)
74960c60 403{
33e5cbd6 404 if (always_inserted_mode == always_inserted_auto)
3e43a32a
MS
405 fprintf_filtered (file,
406 _("Always inserted breakpoint "
407 "mode is %s (currently %s).\n"),
33e5cbd6
PA
408 value,
409 breakpoints_always_inserted_mode () ? "on" : "off");
410 else
3e43a32a
MS
411 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
412 value);
74960c60
VP
413}
414
33e5cbd6
PA
415int
416breakpoints_always_inserted_mode (void)
417{
20388dd6
YQ
418 return (always_inserted_mode == always_inserted_on
419 || (always_inserted_mode == always_inserted_auto && non_stop));
33e5cbd6 420}
765dc015 421
b775012e
LM
422static const char condition_evaluation_both[] = "host or target";
423
424/* Modes for breakpoint condition evaluation. */
425static const char condition_evaluation_auto[] = "auto";
426static const char condition_evaluation_host[] = "host";
427static const char condition_evaluation_target[] = "target";
428static const char *const condition_evaluation_enums[] = {
429 condition_evaluation_auto,
430 condition_evaluation_host,
431 condition_evaluation_target,
432 NULL
433};
434
435/* Global that holds the current mode for breakpoint condition evaluation. */
436static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
437
438/* Global that we use to display information to the user (gets its value from
439 condition_evaluation_mode_1. */
440static const char *condition_evaluation_mode = condition_evaluation_auto;
441
442/* Translate a condition evaluation mode MODE into either "host"
443 or "target". This is used mostly to translate from "auto" to the
444 real setting that is being used. It returns the translated
445 evaluation mode. */
446
447static const char *
448translate_condition_evaluation_mode (const char *mode)
449{
450 if (mode == condition_evaluation_auto)
451 {
452 if (target_supports_evaluation_of_breakpoint_conditions ())
453 return condition_evaluation_target;
454 else
455 return condition_evaluation_host;
456 }
457 else
458 return mode;
459}
460
461/* Discovers what condition_evaluation_auto translates to. */
462
463static const char *
464breakpoint_condition_evaluation_mode (void)
465{
466 return translate_condition_evaluation_mode (condition_evaluation_mode);
467}
468
469/* Return true if GDB should evaluate breakpoint conditions or false
470 otherwise. */
471
472static int
473gdb_evaluates_breakpoint_condition_p (void)
474{
475 const char *mode = breakpoint_condition_evaluation_mode ();
476
477 return (mode == condition_evaluation_host);
478}
479
a14ed312 480void _initialize_breakpoint (void);
c906108c 481
c906108c
SS
482/* Are we executing breakpoint commands? */
483static int executing_breakpoint_commands;
484
c02f5703
MS
485/* Are overlay event breakpoints enabled? */
486static int overlay_events_enabled;
487
e09342b5
TJB
488/* See description in breakpoint.h. */
489int target_exact_watchpoints = 0;
490
c906108c 491/* Walk the following statement or block through all breakpoints.
e5dd4106 492 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
4a64f543 493 current breakpoint. */
c906108c 494
5c44784c 495#define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
c906108c 496
5c44784c
JM
497#define ALL_BREAKPOINTS_SAFE(B,TMP) \
498 for (B = breakpoint_chain; \
499 B ? (TMP=B->next, 1): 0; \
500 B = TMP)
c906108c 501
4a64f543
MS
502/* Similar iterator for the low-level breakpoints. SAFE variant is
503 not provided so update_global_location_list must not be called
504 while executing the block of ALL_BP_LOCATIONS. */
7cc221ef 505
876fa593
JK
506#define ALL_BP_LOCATIONS(B,BP_TMP) \
507 for (BP_TMP = bp_location; \
508 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
509 BP_TMP++)
7cc221ef 510
b775012e
LM
511/* Iterates through locations with address ADDRESS for the currently selected
512 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
513 to where the loop should start from.
514 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
515 appropriate location to start with. */
516
517#define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
518 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
519 BP_LOCP_TMP = BP_LOCP_START; \
520 BP_LOCP_START \
521 && (BP_LOCP_TMP < bp_location + bp_location_count \
522 && (*BP_LOCP_TMP)->address == ADDRESS); \
523 BP_LOCP_TMP++)
524
1042e4c0
SS
525/* Iterator for tracepoints only. */
526
527#define ALL_TRACEPOINTS(B) \
528 for (B = breakpoint_chain; B; B = B->next) \
d77f58be 529 if (is_tracepoint (B))
1042e4c0 530
7cc221ef 531/* Chains of all breakpoints defined. */
c906108c
SS
532
533struct breakpoint *breakpoint_chain;
534
876fa593
JK
535/* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
536
537static struct bp_location **bp_location;
538
539/* Number of elements of BP_LOCATION. */
540
541static unsigned bp_location_count;
542
4a64f543
MS
543/* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
544 ADDRESS for the current elements of BP_LOCATION which get a valid
545 result from bp_location_has_shadow. You can use it for roughly
546 limiting the subrange of BP_LOCATION to scan for shadow bytes for
547 an address you need to read. */
876fa593
JK
548
549static CORE_ADDR bp_location_placed_address_before_address_max;
550
4a64f543
MS
551/* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
552 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
553 BP_LOCATION which get a valid result from bp_location_has_shadow.
554 You can use it for roughly limiting the subrange of BP_LOCATION to
555 scan for shadow bytes for an address you need to read. */
876fa593
JK
556
557static CORE_ADDR bp_location_shadow_len_after_address_max;
7cc221ef 558
4a64f543
MS
559/* The locations that no longer correspond to any breakpoint, unlinked
560 from bp_location array, but for which a hit may still be reported
561 by a target. */
20874c92
VP
562VEC(bp_location_p) *moribund_locations = NULL;
563
c906108c
SS
564/* Number of last breakpoint made. */
565
95a42b64
TT
566static int breakpoint_count;
567
86b17b60
PA
568/* The value of `breakpoint_count' before the last command that
569 created breakpoints. If the last (break-like) command created more
570 than one breakpoint, then the difference between BREAKPOINT_COUNT
571 and PREV_BREAKPOINT_COUNT is more than one. */
572static int prev_breakpoint_count;
c906108c 573
1042e4c0
SS
574/* Number of last tracepoint made. */
575
95a42b64 576static int tracepoint_count;
1042e4c0 577
6149aea9
PA
578static struct cmd_list_element *breakpoint_set_cmdlist;
579static struct cmd_list_element *breakpoint_show_cmdlist;
9291a0cd 580struct cmd_list_element *save_cmdlist;
6149aea9 581
468d015d
JJ
582/* Return whether a breakpoint is an active enabled breakpoint. */
583static int
584breakpoint_enabled (struct breakpoint *b)
585{
0d381245 586 return (b->enable_state == bp_enabled);
468d015d
JJ
587}
588
c906108c
SS
589/* Set breakpoint count to NUM. */
590
95a42b64 591static void
fba45db2 592set_breakpoint_count (int num)
c906108c 593{
86b17b60 594 prev_breakpoint_count = breakpoint_count;
c906108c 595 breakpoint_count = num;
4fa62494 596 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
c906108c
SS
597}
598
86b17b60
PA
599/* Used by `start_rbreak_breakpoints' below, to record the current
600 breakpoint count before "rbreak" creates any breakpoint. */
601static int rbreak_start_breakpoint_count;
602
95a42b64
TT
603/* Called at the start an "rbreak" command to record the first
604 breakpoint made. */
86b17b60 605
95a42b64
TT
606void
607start_rbreak_breakpoints (void)
608{
86b17b60 609 rbreak_start_breakpoint_count = breakpoint_count;
95a42b64
TT
610}
611
612/* Called at the end of an "rbreak" command to record the last
613 breakpoint made. */
86b17b60 614
95a42b64
TT
615void
616end_rbreak_breakpoints (void)
617{
86b17b60 618 prev_breakpoint_count = rbreak_start_breakpoint_count;
95a42b64
TT
619}
620
4a64f543 621/* Used in run_command to zero the hit count when a new run starts. */
c906108c
SS
622
623void
fba45db2 624clear_breakpoint_hit_counts (void)
c906108c
SS
625{
626 struct breakpoint *b;
627
628 ALL_BREAKPOINTS (b)
629 b->hit_count = 0;
630}
631
9add0f1b
TT
632/* Allocate a new counted_command_line with reference count of 1.
633 The new structure owns COMMANDS. */
634
635static struct counted_command_line *
636alloc_counted_command_line (struct command_line *commands)
637{
638 struct counted_command_line *result
639 = xmalloc (sizeof (struct counted_command_line));
cc59ec59 640
9add0f1b
TT
641 result->refc = 1;
642 result->commands = commands;
643 return result;
644}
645
646/* Increment reference count. This does nothing if CMD is NULL. */
647
648static void
649incref_counted_command_line (struct counted_command_line *cmd)
650{
651 if (cmd)
652 ++cmd->refc;
653}
654
655/* Decrement reference count. If the reference count reaches 0,
656 destroy the counted_command_line. Sets *CMDP to NULL. This does
657 nothing if *CMDP is NULL. */
658
659static void
660decref_counted_command_line (struct counted_command_line **cmdp)
661{
662 if (*cmdp)
663 {
664 if (--(*cmdp)->refc == 0)
665 {
666 free_command_lines (&(*cmdp)->commands);
667 xfree (*cmdp);
668 }
669 *cmdp = NULL;
670 }
671}
672
673/* A cleanup function that calls decref_counted_command_line. */
674
675static void
676do_cleanup_counted_command_line (void *arg)
677{
678 decref_counted_command_line (arg);
679}
680
681/* Create a cleanup that calls decref_counted_command_line on the
682 argument. */
683
684static struct cleanup *
685make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
686{
687 return make_cleanup (do_cleanup_counted_command_line, cmdp);
688}
689
c906108c 690\f
48cb2d85
VP
691/* Return the breakpoint with the specified number, or NULL
692 if the number does not refer to an existing breakpoint. */
693
694struct breakpoint *
695get_breakpoint (int num)
696{
697 struct breakpoint *b;
698
699 ALL_BREAKPOINTS (b)
700 if (b->number == num)
701 return b;
702
703 return NULL;
704}
5c44784c 705
c906108c 706\f
adc36818 707
b775012e
LM
708/* Mark locations as "conditions have changed" in case the target supports
709 evaluating conditions on its side. */
710
711static void
712mark_breakpoint_modified (struct breakpoint *b)
713{
714 struct bp_location *loc;
715
716 /* This is only meaningful if the target is
717 evaluating conditions and if the user has
718 opted for condition evaluation on the target's
719 side. */
720 if (gdb_evaluates_breakpoint_condition_p ()
721 || !target_supports_evaluation_of_breakpoint_conditions ())
722 return;
723
724 if (!is_breakpoint (b))
725 return;
726
727 for (loc = b->loc; loc; loc = loc->next)
728 loc->condition_changed = condition_modified;
729}
730
731/* Mark location as "conditions have changed" in case the target supports
732 evaluating conditions on its side. */
733
734static void
735mark_breakpoint_location_modified (struct bp_location *loc)
736{
737 /* This is only meaningful if the target is
738 evaluating conditions and if the user has
739 opted for condition evaluation on the target's
740 side. */
741 if (gdb_evaluates_breakpoint_condition_p ()
742 || !target_supports_evaluation_of_breakpoint_conditions ())
743
744 return;
745
746 if (!is_breakpoint (loc->owner))
747 return;
748
749 loc->condition_changed = condition_modified;
750}
751
752/* Sets the condition-evaluation mode using the static global
753 condition_evaluation_mode. */
754
755static void
756set_condition_evaluation_mode (char *args, int from_tty,
757 struct cmd_list_element *c)
758{
b775012e
LM
759 const char *old_mode, *new_mode;
760
761 if ((condition_evaluation_mode_1 == condition_evaluation_target)
762 && !target_supports_evaluation_of_breakpoint_conditions ())
763 {
764 condition_evaluation_mode_1 = condition_evaluation_mode;
765 warning (_("Target does not support breakpoint condition evaluation.\n"
766 "Using host evaluation mode instead."));
767 return;
768 }
769
770 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
771 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
772
abf1152a
JK
773 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
774 settings was "auto". */
775 condition_evaluation_mode = condition_evaluation_mode_1;
776
b775012e
LM
777 /* Only update the mode if the user picked a different one. */
778 if (new_mode != old_mode)
779 {
780 struct bp_location *loc, **loc_tmp;
781 /* If the user switched to a different evaluation mode, we
782 need to synch the changes with the target as follows:
783
784 "host" -> "target": Send all (valid) conditions to the target.
785 "target" -> "host": Remove all the conditions from the target.
786 */
787
b775012e
LM
788 if (new_mode == condition_evaluation_target)
789 {
790 /* Mark everything modified and synch conditions with the
791 target. */
792 ALL_BP_LOCATIONS (loc, loc_tmp)
793 mark_breakpoint_location_modified (loc);
794 }
795 else
796 {
797 /* Manually mark non-duplicate locations to synch conditions
798 with the target. We do this to remove all the conditions the
799 target knows about. */
800 ALL_BP_LOCATIONS (loc, loc_tmp)
801 if (is_breakpoint (loc->owner) && loc->inserted)
802 loc->needs_update = 1;
803 }
804
805 /* Do the update. */
806 update_global_location_list (1);
807 }
808
809 return;
810}
811
812/* Shows the current mode of breakpoint condition evaluation. Explicitly shows
813 what "auto" is translating to. */
814
815static void
816show_condition_evaluation_mode (struct ui_file *file, int from_tty,
817 struct cmd_list_element *c, const char *value)
818{
819 if (condition_evaluation_mode == condition_evaluation_auto)
820 fprintf_filtered (file,
821 _("Breakpoint condition evaluation "
822 "mode is %s (currently %s).\n"),
823 value,
824 breakpoint_condition_evaluation_mode ());
825 else
826 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
827 value);
828}
829
830/* A comparison function for bp_location AP and BP that is used by
831 bsearch. This comparison function only cares about addresses, unlike
832 the more general bp_location_compare function. */
833
834static int
835bp_location_compare_addrs (const void *ap, const void *bp)
836{
837 struct bp_location *a = *(void **) ap;
838 struct bp_location *b = *(void **) bp;
839
840 if (a->address == b->address)
841 return 0;
842 else
843 return ((a->address > b->address) - (a->address < b->address));
844}
845
846/* Helper function to skip all bp_locations with addresses
847 less than ADDRESS. It returns the first bp_location that
848 is greater than or equal to ADDRESS. If none is found, just
849 return NULL. */
850
851static struct bp_location **
852get_first_locp_gte_addr (CORE_ADDR address)
853{
854 struct bp_location dummy_loc;
855 struct bp_location *dummy_locp = &dummy_loc;
856 struct bp_location **locp_found = NULL;
857
858 /* Initialize the dummy location's address field. */
859 memset (&dummy_loc, 0, sizeof (struct bp_location));
860 dummy_loc.address = address;
861
862 /* Find a close match to the first location at ADDRESS. */
863 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
864 sizeof (struct bp_location **),
865 bp_location_compare_addrs);
866
867 /* Nothing was found, nothing left to do. */
868 if (locp_found == NULL)
869 return NULL;
870
871 /* We may have found a location that is at ADDRESS but is not the first in the
872 location's list. Go backwards (if possible) and locate the first one. */
873 while ((locp_found - 1) >= bp_location
874 && (*(locp_found - 1))->address == address)
875 locp_found--;
876
877 return locp_found;
878}
879
adc36818
PM
880void
881set_breakpoint_condition (struct breakpoint *b, char *exp,
882 int from_tty)
883{
3a5c3e22
PA
884 xfree (b->cond_string);
885 b->cond_string = NULL;
adc36818 886
3a5c3e22 887 if (is_watchpoint (b))
adc36818 888 {
3a5c3e22
PA
889 struct watchpoint *w = (struct watchpoint *) b;
890
891 xfree (w->cond_exp);
892 w->cond_exp = NULL;
893 }
894 else
895 {
896 struct bp_location *loc;
897
898 for (loc = b->loc; loc; loc = loc->next)
899 {
900 xfree (loc->cond);
901 loc->cond = NULL;
b775012e
LM
902
903 /* No need to free the condition agent expression
904 bytecode (if we have one). We will handle this
905 when we go through update_global_location_list. */
3a5c3e22 906 }
adc36818 907 }
adc36818
PM
908
909 if (*exp == 0)
910 {
911 if (from_tty)
912 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
913 }
914 else
915 {
916 char *arg = exp;
cc59ec59 917
adc36818
PM
918 /* I don't know if it matters whether this is the string the user
919 typed in or the decompiled expression. */
920 b->cond_string = xstrdup (arg);
921 b->condition_not_parsed = 0;
922
923 if (is_watchpoint (b))
924 {
3a5c3e22
PA
925 struct watchpoint *w = (struct watchpoint *) b;
926
adc36818
PM
927 innermost_block = NULL;
928 arg = exp;
3a5c3e22 929 w->cond_exp = parse_exp_1 (&arg, 0, 0);
adc36818
PM
930 if (*arg)
931 error (_("Junk at end of expression"));
3a5c3e22 932 w->cond_exp_valid_block = innermost_block;
adc36818
PM
933 }
934 else
935 {
3a5c3e22
PA
936 struct bp_location *loc;
937
adc36818
PM
938 for (loc = b->loc; loc; loc = loc->next)
939 {
940 arg = exp;
941 loc->cond =
942 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
943 if (*arg)
944 error (_("Junk at end of expression"));
945 }
946 }
947 }
b775012e
LM
948 mark_breakpoint_modified (b);
949
adc36818 950 breakpoints_changed ();
8d3788bd 951 observer_notify_breakpoint_modified (b);
adc36818
PM
952}
953
d55637df
TT
954/* Completion for the "condition" command. */
955
956static VEC (char_ptr) *
957condition_completer (struct cmd_list_element *cmd, char *text, char *word)
958{
959 char *space;
960
961 text = skip_spaces (text);
962 space = skip_to_space (text);
963 if (*space == '\0')
964 {
965 int len;
966 struct breakpoint *b;
967 VEC (char_ptr) *result = NULL;
968
969 if (text[0] == '$')
970 {
971 /* We don't support completion of history indices. */
972 if (isdigit (text[1]))
973 return NULL;
974 return complete_internalvar (&text[1]);
975 }
976
977 /* We're completing the breakpoint number. */
978 len = strlen (text);
979
980 ALL_BREAKPOINTS (b)
981 {
982 int single = b->loc->next == NULL;
983 struct bp_location *loc;
984 int count = 1;
985
986 for (loc = b->loc; loc; loc = loc->next)
987 {
988 char location[50];
989
990 if (single)
991 sprintf (location, "%d", b->number);
992 else
993 sprintf (location, "%d.%d", b->number, count);
994
995 if (strncmp (location, text, len) == 0)
996 VEC_safe_push (char_ptr, result, xstrdup (location));
997
998 ++count;
999 }
1000 }
1001
1002 return result;
1003 }
1004
1005 /* We're completing the expression part. */
1006 text = skip_spaces (space);
1007 return expression_completer (cmd, text, word);
1008}
1009
c906108c
SS
1010/* condition N EXP -- set break condition of breakpoint N to EXP. */
1011
1012static void
fba45db2 1013condition_command (char *arg, int from_tty)
c906108c 1014{
52f0bd74 1015 struct breakpoint *b;
c906108c 1016 char *p;
52f0bd74 1017 int bnum;
c906108c
SS
1018
1019 if (arg == 0)
e2e0b3e5 1020 error_no_arg (_("breakpoint number"));
c906108c
SS
1021
1022 p = arg;
1023 bnum = get_number (&p);
5c44784c 1024 if (bnum == 0)
8a3fe4f8 1025 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1026
1027 ALL_BREAKPOINTS (b)
1028 if (b->number == bnum)
2f069f6f 1029 {
7371cf6d
PM
1030 /* Check if this breakpoint has a Python object assigned to
1031 it, and if it has a definition of the "stop"
1032 method. This method and conditions entered into GDB from
1033 the CLI are mutually exclusive. */
1034 if (b->py_bp_object
1035 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
1036 error (_("Cannot set a condition where a Python 'stop' "
1037 "method has been defined in the breakpoint."));
2566ad2d 1038 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1039
1040 if (is_breakpoint (b))
1041 update_global_location_list (1);
1042
2f069f6f
JB
1043 return;
1044 }
c906108c 1045
8a3fe4f8 1046 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1047}
1048
a7bdde9e
VP
1049/* Check that COMMAND do not contain commands that are suitable
1050 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1051 Throw if any such commands is found. */
1052
a7bdde9e
VP
1053static void
1054check_no_tracepoint_commands (struct command_line *commands)
1055{
1056 struct command_line *c;
cc59ec59 1057
a7bdde9e
VP
1058 for (c = commands; c; c = c->next)
1059 {
1060 int i;
1061
1062 if (c->control_type == while_stepping_control)
3e43a32a
MS
1063 error (_("The 'while-stepping' command can "
1064 "only be used for tracepoints"));
a7bdde9e
VP
1065
1066 for (i = 0; i < c->body_count; ++i)
1067 check_no_tracepoint_commands ((c->body_list)[i]);
1068
1069 /* Not that command parsing removes leading whitespace and comment
4a64f543 1070 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1071 command directly. */
1072 if (strstr (c->line, "collect ") == c->line)
1073 error (_("The 'collect' command can only be used for tracepoints"));
1074
51661e93
VP
1075 if (strstr (c->line, "teval ") == c->line)
1076 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1077 }
1078}
1079
d77f58be
SS
1080/* Encapsulate tests for different types of tracepoints. */
1081
d9b3f62e
PA
1082static int
1083is_tracepoint_type (enum bptype type)
1084{
1085 return (type == bp_tracepoint
1086 || type == bp_fast_tracepoint
1087 || type == bp_static_tracepoint);
1088}
1089
a7bdde9e 1090int
d77f58be 1091is_tracepoint (const struct breakpoint *b)
a7bdde9e 1092{
d9b3f62e 1093 return is_tracepoint_type (b->type);
a7bdde9e 1094}
d9b3f62e 1095
e5dd4106 1096/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1097 breakpoint. This function will throw an exception if a problem is
1098 found. */
48cb2d85 1099
95a42b64
TT
1100static void
1101validate_commands_for_breakpoint (struct breakpoint *b,
1102 struct command_line *commands)
48cb2d85 1103{
d77f58be 1104 if (is_tracepoint (b))
a7bdde9e 1105 {
4a64f543
MS
1106 /* We need to verify that each top-level element of commands is
1107 valid for tracepoints, that there's at most one
1108 while-stepping element, and that while-stepping's body has
1109 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
1110 struct command_line *c;
1111 struct command_line *while_stepping = 0;
1112 for (c = commands; c; c = c->next)
1113 {
a7bdde9e
VP
1114 if (c->control_type == while_stepping_control)
1115 {
1116 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1117 error (_("The 'while-stepping' command "
1118 "cannot be used for fast tracepoint"));
0fb4aa4b 1119 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1120 error (_("The 'while-stepping' command "
1121 "cannot be used for static tracepoint"));
a7bdde9e
VP
1122
1123 if (while_stepping)
3e43a32a
MS
1124 error (_("The 'while-stepping' command "
1125 "can be used only once"));
a7bdde9e
VP
1126 else
1127 while_stepping = c;
1128 }
1129 }
1130 if (while_stepping)
1131 {
1132 struct command_line *c2;
1133
1134 gdb_assert (while_stepping->body_count == 1);
1135 c2 = while_stepping->body_list[0];
1136 for (; c2; c2 = c2->next)
1137 {
a7bdde9e
VP
1138 if (c2->control_type == while_stepping_control)
1139 error (_("The 'while-stepping' command cannot be nested"));
1140 }
1141 }
1142 }
1143 else
1144 {
1145 check_no_tracepoint_commands (commands);
1146 }
95a42b64
TT
1147}
1148
0fb4aa4b
PA
1149/* Return a vector of all the static tracepoints set at ADDR. The
1150 caller is responsible for releasing the vector. */
1151
1152VEC(breakpoint_p) *
1153static_tracepoints_here (CORE_ADDR addr)
1154{
1155 struct breakpoint *b;
1156 VEC(breakpoint_p) *found = 0;
1157 struct bp_location *loc;
1158
1159 ALL_BREAKPOINTS (b)
1160 if (b->type == bp_static_tracepoint)
1161 {
1162 for (loc = b->loc; loc; loc = loc->next)
1163 if (loc->address == addr)
1164 VEC_safe_push(breakpoint_p, found, b);
1165 }
1166
1167 return found;
1168}
1169
95a42b64 1170/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1171 validate that only allowed commands are included. */
95a42b64
TT
1172
1173void
4a64f543
MS
1174breakpoint_set_commands (struct breakpoint *b,
1175 struct command_line *commands)
95a42b64
TT
1176{
1177 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1178
9add0f1b
TT
1179 decref_counted_command_line (&b->commands);
1180 b->commands = alloc_counted_command_line (commands);
48cb2d85 1181 breakpoints_changed ();
8d3788bd 1182 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1183}
1184
45a43567
TT
1185/* Set the internal `silent' flag on the breakpoint. Note that this
1186 is not the same as the "silent" that may appear in the breakpoint's
1187 commands. */
1188
1189void
1190breakpoint_set_silent (struct breakpoint *b, int silent)
1191{
1192 int old_silent = b->silent;
1193
1194 b->silent = silent;
1195 if (old_silent != silent)
8d3788bd 1196 observer_notify_breakpoint_modified (b);
45a43567
TT
1197}
1198
1199/* Set the thread for this breakpoint. If THREAD is -1, make the
1200 breakpoint work for any thread. */
1201
1202void
1203breakpoint_set_thread (struct breakpoint *b, int thread)
1204{
1205 int old_thread = b->thread;
1206
1207 b->thread = thread;
1208 if (old_thread != thread)
8d3788bd 1209 observer_notify_breakpoint_modified (b);
45a43567
TT
1210}
1211
1212/* Set the task for this breakpoint. If TASK is 0, make the
1213 breakpoint work for any task. */
1214
1215void
1216breakpoint_set_task (struct breakpoint *b, int task)
1217{
1218 int old_task = b->task;
1219
1220 b->task = task;
1221 if (old_task != task)
8d3788bd 1222 observer_notify_breakpoint_modified (b);
45a43567
TT
1223}
1224
95a42b64
TT
1225void
1226check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1227{
1228 struct breakpoint *b = closure;
cc59ec59 1229
a7bdde9e
VP
1230 validate_actionline (&line, b);
1231}
1232
95a42b64
TT
1233/* A structure used to pass information through
1234 map_breakpoint_numbers. */
1235
1236struct commands_info
1237{
1238 /* True if the command was typed at a tty. */
1239 int from_tty;
86b17b60
PA
1240
1241 /* The breakpoint range spec. */
1242 char *arg;
1243
95a42b64
TT
1244 /* Non-NULL if the body of the commands are being read from this
1245 already-parsed command. */
1246 struct command_line *control;
86b17b60 1247
95a42b64
TT
1248 /* The command lines read from the user, or NULL if they have not
1249 yet been read. */
1250 struct counted_command_line *cmd;
1251};
1252
1253/* A callback for map_breakpoint_numbers that sets the commands for
1254 commands_command. */
1255
c906108c 1256static void
95a42b64 1257do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1258{
95a42b64 1259 struct commands_info *info = data;
c906108c 1260
95a42b64
TT
1261 if (info->cmd == NULL)
1262 {
1263 struct command_line *l;
5c44784c 1264
95a42b64
TT
1265 if (info->control != NULL)
1266 l = copy_command_lines (info->control->body_list[0]);
1267 else
86b17b60
PA
1268 {
1269 struct cleanup *old_chain;
1270 char *str;
c5aa993b 1271
3e43a32a
MS
1272 str = xstrprintf (_("Type commands for breakpoint(s) "
1273 "%s, one per line."),
86b17b60
PA
1274 info->arg);
1275
1276 old_chain = make_cleanup (xfree, str);
1277
1278 l = read_command_lines (str,
1279 info->from_tty, 1,
d77f58be 1280 (is_tracepoint (b)
86b17b60
PA
1281 ? check_tracepoint_command : 0),
1282 b);
1283
1284 do_cleanups (old_chain);
1285 }
a7bdde9e 1286
95a42b64
TT
1287 info->cmd = alloc_counted_command_line (l);
1288 }
1289
1290 /* If a breakpoint was on the list more than once, we don't need to
1291 do anything. */
1292 if (b->commands != info->cmd)
1293 {
1294 validate_commands_for_breakpoint (b, info->cmd->commands);
1295 incref_counted_command_line (info->cmd);
1296 decref_counted_command_line (&b->commands);
1297 b->commands = info->cmd;
1298 breakpoints_changed ();
8d3788bd 1299 observer_notify_breakpoint_modified (b);
c5aa993b 1300 }
95a42b64
TT
1301}
1302
1303static void
4a64f543
MS
1304commands_command_1 (char *arg, int from_tty,
1305 struct command_line *control)
95a42b64
TT
1306{
1307 struct cleanup *cleanups;
1308 struct commands_info info;
1309
1310 info.from_tty = from_tty;
1311 info.control = control;
1312 info.cmd = NULL;
1313 /* If we read command lines from the user, then `info' will hold an
1314 extra reference to the commands that we must clean up. */
1315 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1316
1317 if (arg == NULL || !*arg)
1318 {
86b17b60 1319 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1320 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1321 breakpoint_count);
95a42b64
TT
1322 else if (breakpoint_count > 0)
1323 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1324 else
1325 {
1326 /* So that we don't try to free the incoming non-NULL
1327 argument in the cleanup below. Mapping breakpoint
1328 numbers will fail in this case. */
1329 arg = NULL;
1330 }
95a42b64 1331 }
9766ced4
SS
1332 else
1333 /* The command loop has some static state, so we need to preserve
1334 our argument. */
1335 arg = xstrdup (arg);
86b17b60
PA
1336
1337 if (arg != NULL)
1338 make_cleanup (xfree, arg);
1339
1340 info.arg = arg;
95a42b64
TT
1341
1342 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1343
1344 if (info.cmd == NULL)
1345 error (_("No breakpoints specified."));
1346
1347 do_cleanups (cleanups);
1348}
1349
1350static void
1351commands_command (char *arg, int from_tty)
1352{
1353 commands_command_1 (arg, from_tty, NULL);
c906108c 1354}
40c03ae8
EZ
1355
1356/* Like commands_command, but instead of reading the commands from
1357 input stream, takes them from an already parsed command structure.
1358
1359 This is used by cli-script.c to DTRT with breakpoint commands
1360 that are part of if and while bodies. */
1361enum command_control_type
1362commands_from_control_command (char *arg, struct command_line *cmd)
1363{
95a42b64
TT
1364 commands_command_1 (arg, 0, cmd);
1365 return simple_control;
40c03ae8 1366}
876fa593
JK
1367
1368/* Return non-zero if BL->TARGET_INFO contains valid information. */
1369
1370static int
1371bp_location_has_shadow (struct bp_location *bl)
1372{
1373 if (bl->loc_type != bp_loc_software_breakpoint)
1374 return 0;
1375 if (!bl->inserted)
1376 return 0;
1377 if (bl->target_info.shadow_len == 0)
e5dd4106 1378 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1379 return 0;
1380 return 1;
1381}
1382
8defab1a 1383/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1384 by replacing any memory breakpoints with their shadowed contents.
1385
35c63cd8
JB
1386 If READBUF is not NULL, this buffer must not overlap with any of
1387 the breakpoint location's shadow_contents buffers. Otherwise,
1388 a failed assertion internal error will be raised.
1389
876fa593 1390 The range of shadowed area by each bp_location is:
35df4500
TJB
1391 bl->address - bp_location_placed_address_before_address_max
1392 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1393 The range we were requested to resolve shadows for is:
1394 memaddr ... memaddr + len
1395 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1396 memaddr + len <= (bl->address
1397 - bp_location_placed_address_before_address_max)
876fa593 1398 and:
35df4500 1399 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1400
8defab1a 1401void
f0ba3972
PA
1402breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1403 const gdb_byte *writebuf_org,
1404 ULONGEST memaddr, LONGEST len)
c906108c 1405{
4a64f543
MS
1406 /* Left boundary, right boundary and median element of our binary
1407 search. */
876fa593
JK
1408 unsigned bc_l, bc_r, bc;
1409
4a64f543
MS
1410 /* Find BC_L which is a leftmost element which may affect BUF
1411 content. It is safe to report lower value but a failure to
1412 report higher one. */
876fa593
JK
1413
1414 bc_l = 0;
1415 bc_r = bp_location_count;
1416 while (bc_l + 1 < bc_r)
1417 {
35df4500 1418 struct bp_location *bl;
876fa593
JK
1419
1420 bc = (bc_l + bc_r) / 2;
35df4500 1421 bl = bp_location[bc];
876fa593 1422
4a64f543
MS
1423 /* Check first BL->ADDRESS will not overflow due to the added
1424 constant. Then advance the left boundary only if we are sure
1425 the BC element can in no way affect the BUF content (MEMADDR
1426 to MEMADDR + LEN range).
876fa593 1427
4a64f543
MS
1428 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1429 offset so that we cannot miss a breakpoint with its shadow
1430 range tail still reaching MEMADDR. */
c5aa993b 1431
35df4500
TJB
1432 if ((bl->address + bp_location_shadow_len_after_address_max
1433 >= bl->address)
1434 && (bl->address + bp_location_shadow_len_after_address_max
1435 <= memaddr))
876fa593
JK
1436 bc_l = bc;
1437 else
1438 bc_r = bc;
1439 }
1440
128070bb
PA
1441 /* Due to the binary search above, we need to make sure we pick the
1442 first location that's at BC_L's address. E.g., if there are
1443 multiple locations at the same address, BC_L may end up pointing
1444 at a duplicate location, and miss the "master"/"inserted"
1445 location. Say, given locations L1, L2 and L3 at addresses A and
1446 B:
1447
1448 L1@A, L2@A, L3@B, ...
1449
1450 BC_L could end up pointing at location L2, while the "master"
1451 location could be L1. Since the `loc->inserted' flag is only set
1452 on "master" locations, we'd forget to restore the shadow of L1
1453 and L2. */
1454 while (bc_l > 0
1455 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1456 bc_l--;
1457
876fa593
JK
1458 /* Now do full processing of the found relevant range of elements. */
1459
1460 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1461 {
35df4500 1462 struct bp_location *bl = bp_location[bc];
876fa593
JK
1463 CORE_ADDR bp_addr = 0;
1464 int bp_size = 0;
1465 int bptoffset = 0;
1466
35df4500
TJB
1467 /* bp_location array has BL->OWNER always non-NULL. */
1468 if (bl->owner->type == bp_none)
8a3fe4f8 1469 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1470 bl->owner->number);
ffce0d52 1471
e5dd4106 1472 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1473 content. */
1474
35df4500
TJB
1475 if (bl->address >= bp_location_placed_address_before_address_max
1476 && memaddr + len <= (bl->address
1477 - bp_location_placed_address_before_address_max))
876fa593
JK
1478 break;
1479
35df4500 1480 if (!bp_location_has_shadow (bl))
c5aa993b 1481 continue;
35df4500 1482 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1483 current_program_space->aspace, 0))
1484 continue;
1485
c5aa993b
JM
1486 /* Addresses and length of the part of the breakpoint that
1487 we need to copy. */
35df4500
TJB
1488 bp_addr = bl->target_info.placed_address;
1489 bp_size = bl->target_info.shadow_len;
8defab1a 1490
c5aa993b
JM
1491 if (bp_addr + bp_size <= memaddr)
1492 /* The breakpoint is entirely before the chunk of memory we
1493 are reading. */
1494 continue;
8defab1a 1495
c5aa993b
JM
1496 if (bp_addr >= memaddr + len)
1497 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1498 reading. */
c5aa993b 1499 continue;
c5aa993b 1500
8defab1a
DJ
1501 /* Offset within shadow_contents. */
1502 if (bp_addr < memaddr)
1503 {
1504 /* Only copy the second part of the breakpoint. */
1505 bp_size -= memaddr - bp_addr;
1506 bptoffset = memaddr - bp_addr;
1507 bp_addr = memaddr;
1508 }
c5aa993b 1509
8defab1a
DJ
1510 if (bp_addr + bp_size > memaddr + len)
1511 {
1512 /* Only copy the first part of the breakpoint. */
1513 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1514 }
c5aa993b 1515
f0ba3972
PA
1516 if (readbuf != NULL)
1517 {
35c63cd8
JB
1518 /* Verify that the readbuf buffer does not overlap with
1519 the shadow_contents buffer. */
1520 gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1521 || readbuf >= (bl->target_info.shadow_contents
1522 + bl->target_info.shadow_len));
1523
f0ba3972
PA
1524 /* Update the read buffer with this inserted breakpoint's
1525 shadow. */
1526 memcpy (readbuf + bp_addr - memaddr,
1527 bl->target_info.shadow_contents + bptoffset, bp_size);
1528 }
1529 else
1530 {
1531 struct gdbarch *gdbarch = bl->gdbarch;
1532 const unsigned char *bp;
1533 CORE_ADDR placed_address = bl->target_info.placed_address;
1534 unsigned placed_size = bl->target_info.placed_size;
1535
1536 /* Update the shadow with what we want to write to memory. */
1537 memcpy (bl->target_info.shadow_contents + bptoffset,
1538 writebuf_org + bp_addr - memaddr, bp_size);
1539
1540 /* Determine appropriate breakpoint contents and size for this
1541 address. */
1542 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1543
1544 /* Update the final write buffer with this inserted
1545 breakpoint's INSN. */
1546 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1547 }
c5aa993b 1548 }
c906108c 1549}
c906108c 1550\f
c5aa993b 1551
b775012e
LM
1552/* Return true if BPT is either a software breakpoint or a hardware
1553 breakpoint. */
1554
1555int
1556is_breakpoint (const struct breakpoint *bpt)
1557{
1558 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1559 || bpt->type == bp_hardware_breakpoint
1560 || bpt->type == bp_dprintf);
b775012e
LM
1561}
1562
60e1c644
PA
1563/* Return true if BPT is of any hardware watchpoint kind. */
1564
a5606eee 1565static int
d77f58be 1566is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1567{
1568 return (bpt->type == bp_hardware_watchpoint
1569 || bpt->type == bp_read_watchpoint
1570 || bpt->type == bp_access_watchpoint);
1571}
7270d8f2 1572
60e1c644
PA
1573/* Return true if BPT is of any watchpoint kind, hardware or
1574 software. */
1575
3a5c3e22 1576int
d77f58be 1577is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1578{
1579 return (is_hardware_watchpoint (bpt)
1580 || bpt->type == bp_watchpoint);
1581}
1582
3a5c3e22
PA
1583/* Returns true if the current thread and its running state are safe
1584 to evaluate or update watchpoint B. Watchpoints on local
1585 expressions need to be evaluated in the context of the thread that
1586 was current when the watchpoint was created, and, that thread needs
1587 to be stopped to be able to select the correct frame context.
1588 Watchpoints on global expressions can be evaluated on any thread,
1589 and in any state. It is presently left to the target allowing
1590 memory accesses when threads are running. */
f6bc2008
PA
1591
1592static int
3a5c3e22 1593watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1594{
d0d8b0c6
JK
1595 return (b->base.pspace == current_program_space
1596 && (ptid_equal (b->watchpoint_thread, null_ptid)
1597 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1598 && !is_executing (inferior_ptid))));
f6bc2008
PA
1599}
1600
d0fb5eae
JK
1601/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1602 associated bp_watchpoint_scope breakpoint. */
1603
1604static void
3a5c3e22 1605watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1606{
3a5c3e22 1607 struct breakpoint *b = &w->base;
d0fb5eae
JK
1608
1609 if (b->related_breakpoint != b)
1610 {
1611 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1612 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1613 b->related_breakpoint->disposition = disp_del_at_next_stop;
1614 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1615 b->related_breakpoint = b;
1616 }
1617 b->disposition = disp_del_at_next_stop;
1618}
1619
567e1b4e
JB
1620/* Assuming that B is a watchpoint:
1621 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1622 - Evaluate expression and store the result in B->val
567e1b4e
JB
1623 - Evaluate the condition if there is one, and store the result
1624 in b->loc->cond.
a5606eee
VP
1625 - Update the list of values that must be watched in B->loc.
1626
4a64f543
MS
1627 If the watchpoint disposition is disp_del_at_next_stop, then do
1628 nothing. If this is local watchpoint that is out of scope, delete
1629 it.
1630
1631 Even with `set breakpoint always-inserted on' the watchpoints are
1632 removed + inserted on each stop here. Normal breakpoints must
1633 never be removed because they might be missed by a running thread
1634 when debugging in non-stop mode. On the other hand, hardware
1635 watchpoints (is_hardware_watchpoint; processed here) are specific
1636 to each LWP since they are stored in each LWP's hardware debug
1637 registers. Therefore, such LWP must be stopped first in order to
1638 be able to modify its hardware watchpoints.
1639
1640 Hardware watchpoints must be reset exactly once after being
1641 presented to the user. It cannot be done sooner, because it would
1642 reset the data used to present the watchpoint hit to the user. And
1643 it must not be done later because it could display the same single
1644 watchpoint hit during multiple GDB stops. Note that the latter is
1645 relevant only to the hardware watchpoint types bp_read_watchpoint
1646 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1647 not user-visible - its hit is suppressed if the memory content has
1648 not changed.
1649
1650 The following constraints influence the location where we can reset
1651 hardware watchpoints:
1652
1653 * target_stopped_by_watchpoint and target_stopped_data_address are
1654 called several times when GDB stops.
1655
1656 [linux]
1657 * Multiple hardware watchpoints can be hit at the same time,
1658 causing GDB to stop. GDB only presents one hardware watchpoint
1659 hit at a time as the reason for stopping, and all the other hits
1660 are presented later, one after the other, each time the user
1661 requests the execution to be resumed. Execution is not resumed
1662 for the threads still having pending hit event stored in
1663 LWP_INFO->STATUS. While the watchpoint is already removed from
1664 the inferior on the first stop the thread hit event is kept being
1665 reported from its cached value by linux_nat_stopped_data_address
1666 until the real thread resume happens after the watchpoint gets
1667 presented and thus its LWP_INFO->STATUS gets reset.
1668
1669 Therefore the hardware watchpoint hit can get safely reset on the
1670 watchpoint removal from inferior. */
a79d3c27 1671
b40ce68a 1672static void
3a5c3e22 1673update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1674{
a5606eee 1675 int within_current_scope;
a5606eee 1676 struct frame_id saved_frame_id;
66076460 1677 int frame_saved;
a5606eee 1678
f6bc2008
PA
1679 /* If this is a local watchpoint, we only want to check if the
1680 watchpoint frame is in scope if the current thread is the thread
1681 that was used to create the watchpoint. */
1682 if (!watchpoint_in_thread_scope (b))
1683 return;
1684
3a5c3e22 1685 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1686 return;
1687
66076460 1688 frame_saved = 0;
a5606eee
VP
1689
1690 /* Determine if the watchpoint is within scope. */
1691 if (b->exp_valid_block == NULL)
1692 within_current_scope = 1;
1693 else
1694 {
b5db5dfc
UW
1695 struct frame_info *fi = get_current_frame ();
1696 struct gdbarch *frame_arch = get_frame_arch (fi);
1697 CORE_ADDR frame_pc = get_frame_pc (fi);
1698
1699 /* If we're in a function epilogue, unwinding may not work
1700 properly, so do not attempt to recreate locations at this
1701 point. See similar comments in watchpoint_check. */
1702 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1703 return;
66076460
DJ
1704
1705 /* Save the current frame's ID so we can restore it after
1706 evaluating the watchpoint expression on its own frame. */
1707 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1708 took a frame parameter, so that we didn't have to change the
1709 selected frame. */
1710 frame_saved = 1;
1711 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1712
a5606eee
VP
1713 fi = frame_find_by_id (b->watchpoint_frame);
1714 within_current_scope = (fi != NULL);
1715 if (within_current_scope)
1716 select_frame (fi);
1717 }
1718
b5db5dfc
UW
1719 /* We don't free locations. They are stored in the bp_location array
1720 and update_global_location_list will eventually delete them and
1721 remove breakpoints if needed. */
3a5c3e22 1722 b->base.loc = NULL;
b5db5dfc 1723
a5606eee
VP
1724 if (within_current_scope && reparse)
1725 {
1726 char *s;
d63d0675 1727
a5606eee
VP
1728 if (b->exp)
1729 {
1730 xfree (b->exp);
1731 b->exp = NULL;
1732 }
d63d0675 1733 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
a5606eee
VP
1734 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1735 /* If the meaning of expression itself changed, the old value is
1736 no longer relevant. We don't want to report a watchpoint hit
1737 to the user when the old value and the new value may actually
1738 be completely different objects. */
1739 value_free (b->val);
fa4727a6
DJ
1740 b->val = NULL;
1741 b->val_valid = 0;
60e1c644
PA
1742
1743 /* Note that unlike with breakpoints, the watchpoint's condition
1744 expression is stored in the breakpoint object, not in the
1745 locations (re)created below. */
3a5c3e22 1746 if (b->base.cond_string != NULL)
60e1c644
PA
1747 {
1748 if (b->cond_exp != NULL)
1749 {
1750 xfree (b->cond_exp);
1751 b->cond_exp = NULL;
1752 }
1753
3a5c3e22 1754 s = b->base.cond_string;
60e1c644
PA
1755 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1756 }
a5606eee 1757 }
a5606eee
VP
1758
1759 /* If we failed to parse the expression, for example because
1760 it refers to a global variable in a not-yet-loaded shared library,
1761 don't try to insert watchpoint. We don't automatically delete
1762 such watchpoint, though, since failure to parse expression
1763 is different from out-of-scope watchpoint. */
2d134ed3
PA
1764 if ( !target_has_execution)
1765 {
1766 /* Without execution, memory can't change. No use to try and
1767 set watchpoint locations. The watchpoint will be reset when
1768 the target gains execution, through breakpoint_re_set. */
1769 }
1770 else if (within_current_scope && b->exp)
a5606eee 1771 {
0cf6dd15 1772 int pc = 0;
fa4727a6 1773 struct value *val_chain, *v, *result, *next;
2d134ed3 1774 struct program_space *frame_pspace;
a5606eee 1775
0cf6dd15 1776 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1777
a5606eee
VP
1778 /* Avoid setting b->val if it's already set. The meaning of
1779 b->val is 'the last value' user saw, and we should update
1780 it only if we reported that last value to user. As it
9c06b0b4
TJB
1781 happens, the code that reports it updates b->val directly.
1782 We don't keep track of the memory value for masked
1783 watchpoints. */
3a5c3e22 1784 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1785 {
1786 b->val = v;
1787 b->val_valid = 1;
1788 }
a5606eee 1789
2d134ed3
PA
1790 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1791
a5606eee 1792 /* Look at each value on the value chain. */
9fa40276 1793 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1794 {
1795 /* If it's a memory location, and GDB actually needed
1796 its contents to evaluate the expression, then we
fa4727a6
DJ
1797 must watch it. If the first value returned is
1798 still lazy, that means an error occurred reading it;
1799 watch it anyway in case it becomes readable. */
a5606eee 1800 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1801 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1802 {
1803 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1804
a5606eee
VP
1805 /* We only watch structs and arrays if user asked
1806 for it explicitly, never if they just happen to
1807 appear in the middle of some value chain. */
fa4727a6 1808 if (v == result
a5606eee
VP
1809 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1810 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1811 {
1812 CORE_ADDR addr;
1813 int len, type;
1814 struct bp_location *loc, **tmp;
1815
42ae5230 1816 addr = value_address (v);
a5606eee
VP
1817 len = TYPE_LENGTH (value_type (v));
1818 type = hw_write;
3a5c3e22 1819 if (b->base.type == bp_read_watchpoint)
a5606eee 1820 type = hw_read;
3a5c3e22 1821 else if (b->base.type == bp_access_watchpoint)
a5606eee 1822 type = hw_access;
3a5c3e22
PA
1823
1824 loc = allocate_bp_location (&b->base);
1825 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1826 ;
1827 *tmp = loc;
a6d9a66e 1828 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1829
1830 loc->pspace = frame_pspace;
a5606eee
VP
1831 loc->address = addr;
1832 loc->length = len;
1833 loc->watchpoint_type = type;
1834 }
1835 }
9fa40276
TJB
1836 }
1837
1838 /* Change the type of breakpoint between hardware assisted or
1839 an ordinary watchpoint depending on the hardware support
1840 and free hardware slots. REPARSE is set when the inferior
1841 is started. */
a9634178 1842 if (reparse)
9fa40276 1843 {
e09342b5 1844 int reg_cnt;
9fa40276
TJB
1845 enum bp_loc_type loc_type;
1846 struct bp_location *bl;
a5606eee 1847
a9634178 1848 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1849
1850 if (reg_cnt)
9fa40276
TJB
1851 {
1852 int i, target_resources_ok, other_type_used;
a1398e0c 1853 enum bptype type;
9fa40276 1854
a9634178
TJB
1855 /* Use an exact watchpoint when there's only one memory region to be
1856 watched, and only one debug register is needed to watch it. */
1857 b->exact = target_exact_watchpoints && reg_cnt == 1;
1858
9fa40276 1859 /* We need to determine how many resources are already
e09342b5
TJB
1860 used for all other hardware watchpoints plus this one
1861 to see if we still have enough resources to also fit
a1398e0c
PA
1862 this watchpoint in as well. */
1863
1864 /* If this is a software watchpoint, we try to turn it
1865 to a hardware one -- count resources as if B was of
1866 hardware watchpoint type. */
1867 type = b->base.type;
1868 if (type == bp_watchpoint)
1869 type = bp_hardware_watchpoint;
1870
1871 /* This watchpoint may or may not have been placed on
1872 the list yet at this point (it won't be in the list
1873 if we're trying to create it for the first time,
1874 through watch_command), so always account for it
1875 manually. */
1876
1877 /* Count resources used by all watchpoints except B. */
1878 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1879
1880 /* Add in the resources needed for B. */
1881 i += hw_watchpoint_use_count (&b->base);
1882
1883 target_resources_ok
1884 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1885 if (target_resources_ok <= 0)
a9634178 1886 {
3a5c3e22 1887 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1888
1889 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1890 error (_("Target does not support this type of "
1891 "hardware watchpoint."));
9c06b0b4
TJB
1892 else if (target_resources_ok < 0 && !sw_mode)
1893 error (_("There are not enough available hardware "
1894 "resources for this watchpoint."));
a1398e0c
PA
1895
1896 /* Downgrade to software watchpoint. */
1897 b->base.type = bp_watchpoint;
1898 }
1899 else
1900 {
1901 /* If this was a software watchpoint, we've just
1902 found we have enough resources to turn it to a
1903 hardware watchpoint. Otherwise, this is a
1904 nop. */
1905 b->base.type = type;
a9634178 1906 }
9fa40276 1907 }
3a5c3e22 1908 else if (!b->base.ops->works_in_software_mode (&b->base))
a9634178
TJB
1909 error (_("Expression cannot be implemented with "
1910 "read/access watchpoint."));
9fa40276 1911 else
3a5c3e22 1912 b->base.type = bp_watchpoint;
9fa40276 1913
3a5c3e22 1914 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1915 : bp_loc_hardware_watchpoint);
3a5c3e22 1916 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1917 bl->loc_type = loc_type;
1918 }
1919
1920 for (v = val_chain; v; v = next)
1921 {
a5606eee
VP
1922 next = value_next (v);
1923 if (v != b->val)
1924 value_free (v);
1925 }
1926
c7437ca6
PA
1927 /* If a software watchpoint is not watching any memory, then the
1928 above left it without any location set up. But,
1929 bpstat_stop_status requires a location to be able to report
1930 stops, so make sure there's at least a dummy one. */
3a5c3e22 1931 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1932 {
3a5c3e22
PA
1933 struct breakpoint *base = &b->base;
1934 base->loc = allocate_bp_location (base);
1935 base->loc->pspace = frame_pspace;
1936 base->loc->address = -1;
1937 base->loc->length = -1;
1938 base->loc->watchpoint_type = -1;
c7437ca6 1939 }
a5606eee
VP
1940 }
1941 else if (!within_current_scope)
7270d8f2 1942 {
ac74f770
MS
1943 printf_filtered (_("\
1944Watchpoint %d deleted because the program has left the block\n\
1945in which its expression is valid.\n"),
3a5c3e22 1946 b->base.number);
d0fb5eae 1947 watchpoint_del_at_next_stop (b);
7270d8f2 1948 }
a5606eee
VP
1949
1950 /* Restore the selected frame. */
66076460
DJ
1951 if (frame_saved)
1952 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1953}
1954
a5606eee 1955
74960c60 1956/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1957 inserted in the inferior. We don't differentiate the type of BL's owner
1958 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1959 breakpoint_ops is not defined, because in insert_bp_location,
1960 tracepoint's insert_location will not be called. */
74960c60 1961static int
35df4500 1962should_be_inserted (struct bp_location *bl)
74960c60 1963{
35df4500 1964 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1965 return 0;
1966
35df4500 1967 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1968 return 0;
1969
35df4500 1970 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1971 return 0;
1972
f8eba3c6
TT
1973 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1974 return 0;
1975
56710373
PA
1976 /* This is set for example, when we're attached to the parent of a
1977 vfork, and have detached from the child. The child is running
1978 free, and we expect it to do an exec or exit, at which point the
1979 OS makes the parent schedulable again (and the target reports
1980 that the vfork is done). Until the child is done with the shared
1981 memory region, do not insert breakpoints in the parent, otherwise
1982 the child could still trip on the parent's breakpoints. Since
1983 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1984 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1985 return 0;
1986
74960c60
VP
1987 return 1;
1988}
1989
934709f0
PW
1990/* Same as should_be_inserted but does the check assuming
1991 that the location is not duplicated. */
1992
1993static int
1994unduplicated_should_be_inserted (struct bp_location *bl)
1995{
1996 int result;
1997 const int save_duplicate = bl->duplicate;
1998
1999 bl->duplicate = 0;
2000 result = should_be_inserted (bl);
2001 bl->duplicate = save_duplicate;
2002 return result;
2003}
2004
b775012e
LM
2005/* Parses a conditional described by an expression COND into an
2006 agent expression bytecode suitable for evaluation
2007 by the bytecode interpreter. Return NULL if there was
2008 any error during parsing. */
2009
2010static struct agent_expr *
2011parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2012{
2013 struct agent_expr *aexpr = NULL;
2014 struct cleanup *old_chain = NULL;
2015 volatile struct gdb_exception ex;
2016
2017 if (!cond)
2018 return NULL;
2019
2020 /* We don't want to stop processing, so catch any errors
2021 that may show up. */
2022 TRY_CATCH (ex, RETURN_MASK_ERROR)
2023 {
2024 aexpr = gen_eval_for_expr (scope, cond);
2025 }
2026
2027 if (ex.reason < 0)
2028 {
2029 /* If we got here, it means the condition could not be parsed to a valid
2030 bytecode expression and thus can't be evaluated on the target's side.
2031 It's no use iterating through the conditions. */
2032 return NULL;
2033 }
2034
2035 /* We have a valid agent expression. */
2036 return aexpr;
2037}
2038
2039/* Based on location BL, create a list of breakpoint conditions to be
2040 passed on to the target. If we have duplicated locations with different
2041 conditions, we will add such conditions to the list. The idea is that the
2042 target will evaluate the list of conditions and will only notify GDB when
2043 one of them is true. */
2044
2045static void
2046build_target_condition_list (struct bp_location *bl)
2047{
2048 struct bp_location **locp = NULL, **loc2p;
2049 int null_condition_or_parse_error = 0;
2050 int modified = bl->needs_update;
2051 struct bp_location *loc;
2052
2053 /* This is only meaningful if the target is
2054 evaluating conditions and if the user has
2055 opted for condition evaluation on the target's
2056 side. */
2057 if (gdb_evaluates_breakpoint_condition_p ()
2058 || !target_supports_evaluation_of_breakpoint_conditions ())
2059 return;
2060
2061 /* Do a first pass to check for locations with no assigned
2062 conditions or conditions that fail to parse to a valid agent expression
2063 bytecode. If any of these happen, then it's no use to send conditions
2064 to the target since this location will always trigger and generate a
2065 response back to GDB. */
2066 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2067 {
2068 loc = (*loc2p);
2069 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2070 {
2071 if (modified)
2072 {
2073 struct agent_expr *aexpr;
2074
2075 /* Re-parse the conditions since something changed. In that
2076 case we already freed the condition bytecodes (see
2077 force_breakpoint_reinsertion). We just
2078 need to parse the condition to bytecodes again. */
2079 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2080 loc->cond_bytecode = aexpr;
2081
2082 /* Check if we managed to parse the conditional expression
2083 correctly. If not, we will not send this condition
2084 to the target. */
2085 if (aexpr)
2086 continue;
2087 }
2088
2089 /* If we have a NULL bytecode expression, it means something
2090 went wrong or we have a null condition expression. */
2091 if (!loc->cond_bytecode)
2092 {
2093 null_condition_or_parse_error = 1;
2094 break;
2095 }
2096 }
2097 }
2098
2099 /* If any of these happened, it means we will have to evaluate the conditions
2100 for the location's address on gdb's side. It is no use keeping bytecodes
2101 for all the other duplicate locations, thus we free all of them here.
2102
2103 This is so we have a finer control over which locations' conditions are
2104 being evaluated by GDB or the remote stub. */
2105 if (null_condition_or_parse_error)
2106 {
2107 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2108 {
2109 loc = (*loc2p);
2110 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2111 {
2112 /* Only go as far as the first NULL bytecode is
2113 located. */
2114 if (!loc->cond_bytecode)
2115 return;
2116
2117 free_agent_expr (loc->cond_bytecode);
2118 loc->cond_bytecode = NULL;
2119 }
2120 }
2121 }
2122
2123 /* No NULL conditions or failed bytecode generation. Build a condition list
2124 for this location's address. */
2125 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2126 {
2127 loc = (*loc2p);
2128 if (loc->cond
2129 && is_breakpoint (loc->owner)
2130 && loc->pspace->num == bl->pspace->num
2131 && loc->owner->enable_state == bp_enabled
2132 && loc->enabled)
2133 /* Add the condition to the vector. This will be used later to send the
2134 conditions to the target. */
2135 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2136 loc->cond_bytecode);
2137 }
2138
2139 return;
2140}
2141
35df4500
TJB
2142/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2143 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2144 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2145 Returns 0 for success, 1 if the bp_location type is not supported or
2146 -1 for failure.
879bfdc2 2147
4a64f543
MS
2148 NOTE drow/2003-09-09: This routine could be broken down to an
2149 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2150static int
35df4500 2151insert_bp_location (struct bp_location *bl,
26bb91f3 2152 struct ui_file *tmp_error_stream,
3fbb6ffa 2153 int *disabled_breaks,
26bb91f3 2154 int *hw_breakpoint_error)
879bfdc2
DJ
2155{
2156 int val = 0;
2157
b775012e 2158 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2159 return 0;
2160
35c63cd8
JB
2161 /* Note we don't initialize bl->target_info, as that wipes out
2162 the breakpoint location's shadow_contents if the breakpoint
2163 is still inserted at that location. This in turn breaks
2164 target_read_memory which depends on these buffers when
2165 a memory read is requested at the breakpoint location:
2166 Once the target_info has been wiped, we fail to see that
2167 we have a breakpoint inserted at that address and thus
2168 read the breakpoint instead of returning the data saved in
2169 the breakpoint location's shadow contents. */
35df4500
TJB
2170 bl->target_info.placed_address = bl->address;
2171 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2172 bl->target_info.length = bl->length;
8181d85f 2173
b775012e
LM
2174 /* When working with target-side conditions, we must pass all the conditions
2175 for the same breakpoint address down to the target since GDB will not
2176 insert those locations. With a list of breakpoint conditions, the target
2177 can decide when to stop and notify GDB. */
2178
2179 if (is_breakpoint (bl->owner))
2180 {
2181 build_target_condition_list (bl);
2182 /* Reset the condition modification marker. */
2183 bl->needs_update = 0;
2184 }
2185
35df4500
TJB
2186 if (bl->loc_type == bp_loc_software_breakpoint
2187 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2188 {
35df4500 2189 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2190 {
2191 /* If the explicitly specified breakpoint type
2192 is not hardware breakpoint, check the memory map to see
2193 if the breakpoint address is in read only memory or not.
4a64f543 2194
765dc015
VP
2195 Two important cases are:
2196 - location type is not hardware breakpoint, memory
2197 is readonly. We change the type of the location to
2198 hardware breakpoint.
4a64f543
MS
2199 - location type is hardware breakpoint, memory is
2200 read-write. This means we've previously made the
2201 location hardware one, but then the memory map changed,
2202 so we undo.
765dc015 2203
4a64f543
MS
2204 When breakpoints are removed, remove_breakpoints will use
2205 location types we've just set here, the only possible
2206 problem is that memory map has changed during running
2207 program, but it's not going to work anyway with current
2208 gdb. */
765dc015 2209 struct mem_region *mr
35df4500 2210 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
2211
2212 if (mr)
2213 {
2214 if (automatic_hardware_breakpoints)
2215 {
765dc015
VP
2216 enum bp_loc_type new_type;
2217
2218 if (mr->attrib.mode != MEM_RW)
2219 new_type = bp_loc_hardware_breakpoint;
2220 else
2221 new_type = bp_loc_software_breakpoint;
2222
35df4500 2223 if (new_type != bl->loc_type)
765dc015
VP
2224 {
2225 static int said = 0;
cc59ec59 2226
35df4500 2227 bl->loc_type = new_type;
765dc015
VP
2228 if (!said)
2229 {
3e43a32a
MS
2230 fprintf_filtered (gdb_stdout,
2231 _("Note: automatically using "
2232 "hardware breakpoints for "
2233 "read-only addresses.\n"));
765dc015
VP
2234 said = 1;
2235 }
2236 }
2237 }
35df4500 2238 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 2239 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
2240 warning (_("cannot set software breakpoint "
2241 "at readonly address %s"),
35df4500 2242 paddress (bl->gdbarch, bl->address));
765dc015
VP
2243 }
2244 }
2245
879bfdc2
DJ
2246 /* First check to see if we have to handle an overlay. */
2247 if (overlay_debugging == ovly_off
35df4500
TJB
2248 || bl->section == NULL
2249 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2250 {
2251 /* No overlay handling: just set the breakpoint. */
2252
348d480f 2253 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
2254 }
2255 else
2256 {
4a64f543 2257 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2258 Shall we set a breakpoint at the LMA? */
2259 if (!overlay_events_enabled)
2260 {
2261 /* Yes -- overlay event support is not active,
2262 so we must try to set a breakpoint at the LMA.
2263 This will not work for a hardware breakpoint. */
35df4500 2264 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2265 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2266 bl->owner->number);
879bfdc2
DJ
2267 else
2268 {
35df4500
TJB
2269 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2270 bl->section);
879bfdc2 2271 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
2272 bl->overlay_target_info = bl->target_info;
2273 bl->overlay_target_info.placed_address = addr;
2274 val = target_insert_breakpoint (bl->gdbarch,
2275 &bl->overlay_target_info);
879bfdc2 2276 if (val != 0)
99361f52 2277 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2278 "Overlay breakpoint %d "
2279 "failed: in ROM?\n",
35df4500 2280 bl->owner->number);
879bfdc2
DJ
2281 }
2282 }
2283 /* Shall we set a breakpoint at the VMA? */
35df4500 2284 if (section_is_mapped (bl->section))
879bfdc2
DJ
2285 {
2286 /* Yes. This overlay section is mapped into memory. */
348d480f 2287 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
2288 }
2289 else
2290 {
2291 /* No. This breakpoint will not be inserted.
2292 No error, but do not mark the bp as 'inserted'. */
2293 return 0;
2294 }
2295 }
2296
2297 if (val)
2298 {
2299 /* Can't set the breakpoint. */
35df4500 2300 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 2301 {
4a64f543 2302 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 2303 val = 0;
35df4500 2304 bl->shlib_disabled = 1;
8d3788bd 2305 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2306 if (!*disabled_breaks)
2307 {
2308 fprintf_unfiltered (tmp_error_stream,
2309 "Cannot insert breakpoint %d.\n",
2310 bl->owner->number);
2311 fprintf_unfiltered (tmp_error_stream,
2312 "Temporarily disabling shared "
2313 "library breakpoints:\n");
2314 }
2315 *disabled_breaks = 1;
879bfdc2 2316 fprintf_unfiltered (tmp_error_stream,
35df4500 2317 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
2318 }
2319 else
879bfdc2 2320 {
35df4500 2321 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
2322 {
2323 *hw_breakpoint_error = 1;
3e43a32a
MS
2324 fprintf_unfiltered (tmp_error_stream,
2325 "Cannot insert hardware "
2326 "breakpoint %d.\n",
35df4500 2327 bl->owner->number);
879bfdc2
DJ
2328 }
2329 else
2330 {
2331 fprintf_unfiltered (tmp_error_stream,
2332 "Cannot insert breakpoint %d.\n",
35df4500 2333 bl->owner->number);
879bfdc2
DJ
2334 fprintf_filtered (tmp_error_stream,
2335 "Error accessing memory address ");
35df4500 2336 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 2337 tmp_error_stream);
879bfdc2
DJ
2338 fprintf_filtered (tmp_error_stream, ": %s.\n",
2339 safe_strerror (val));
2340 }
2341
2342 }
2343 }
2344 else
35df4500 2345 bl->inserted = 1;
879bfdc2
DJ
2346
2347 return val;
2348 }
2349
35df4500 2350 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2351 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2352 watchpoints. It's not clear that it's necessary... */
35df4500 2353 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2354 {
77b06cd7
TJB
2355 gdb_assert (bl->owner->ops != NULL
2356 && bl->owner->ops->insert_location != NULL);
2357
2358 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2359
2360 /* If trying to set a read-watchpoint, and it turns out it's not
2361 supported, try emulating one with an access watchpoint. */
35df4500 2362 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2363 {
2364 struct bp_location *loc, **loc_temp;
2365
2366 /* But don't try to insert it, if there's already another
2367 hw_access location that would be considered a duplicate
2368 of this one. */
2369 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2370 if (loc != bl
85d721b8 2371 && loc->watchpoint_type == hw_access
35df4500 2372 && watchpoint_locations_match (bl, loc))
85d721b8 2373 {
35df4500
TJB
2374 bl->duplicate = 1;
2375 bl->inserted = 1;
2376 bl->target_info = loc->target_info;
2377 bl->watchpoint_type = hw_access;
85d721b8
PA
2378 val = 0;
2379 break;
2380 }
2381
2382 if (val == 1)
2383 {
77b06cd7
TJB
2384 bl->watchpoint_type = hw_access;
2385 val = bl->owner->ops->insert_location (bl);
2386
2387 if (val)
2388 /* Back to the original value. */
2389 bl->watchpoint_type = hw_read;
85d721b8
PA
2390 }
2391 }
2392
35df4500 2393 bl->inserted = (val == 0);
879bfdc2
DJ
2394 }
2395
35df4500 2396 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2397 {
77b06cd7
TJB
2398 gdb_assert (bl->owner->ops != NULL
2399 && bl->owner->ops->insert_location != NULL);
2400
2401 val = bl->owner->ops->insert_location (bl);
2402 if (val)
2403 {
2404 bl->owner->enable_state = bp_disabled;
2405
2406 if (val == 1)
2407 warning (_("\
2408Error inserting catchpoint %d: Your system does not support this type\n\
2409of catchpoint."), bl->owner->number);
2410 else
2411 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2412 }
2413
2414 bl->inserted = (val == 0);
1640b821
DJ
2415
2416 /* We've already printed an error message if there was a problem
2417 inserting this catchpoint, and we've disabled the catchpoint,
2418 so just return success. */
2419 return 0;
879bfdc2
DJ
2420 }
2421
2422 return 0;
2423}
2424
6c95b8df
PA
2425/* This function is called when program space PSPACE is about to be
2426 deleted. It takes care of updating breakpoints to not reference
2427 PSPACE anymore. */
2428
2429void
2430breakpoint_program_space_exit (struct program_space *pspace)
2431{
2432 struct breakpoint *b, *b_temp;
876fa593 2433 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2434
2435 /* Remove any breakpoint that was set through this program space. */
2436 ALL_BREAKPOINTS_SAFE (b, b_temp)
2437 {
2438 if (b->pspace == pspace)
2439 delete_breakpoint (b);
2440 }
2441
2442 /* Breakpoints set through other program spaces could have locations
2443 bound to PSPACE as well. Remove those. */
876fa593 2444 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2445 {
2446 struct bp_location *tmp;
2447
2448 if (loc->pspace == pspace)
2449 {
2bdf28a0 2450 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2451 if (loc->owner->loc == loc)
2452 loc->owner->loc = loc->next;
2453 else
2454 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2455 if (tmp->next == loc)
2456 {
2457 tmp->next = loc->next;
2458 break;
2459 }
2460 }
2461 }
2462
2463 /* Now update the global location list to permanently delete the
2464 removed locations above. */
2465 update_global_location_list (0);
2466}
2467
74960c60
VP
2468/* Make sure all breakpoints are inserted in inferior.
2469 Throws exception on any error.
2470 A breakpoint that is already inserted won't be inserted
2471 again, so calling this function twice is safe. */
2472void
2473insert_breakpoints (void)
2474{
2475 struct breakpoint *bpt;
2476
2477 ALL_BREAKPOINTS (bpt)
2478 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2479 {
2480 struct watchpoint *w = (struct watchpoint *) bpt;
2481
2482 update_watchpoint (w, 0 /* don't reparse. */);
2483 }
74960c60 2484
b60e7edf 2485 update_global_location_list (1);
74960c60 2486
c35b1492
PA
2487 /* update_global_location_list does not insert breakpoints when
2488 always_inserted_mode is not enabled. Explicitly insert them
2489 now. */
2490 if (!breakpoints_always_inserted_mode ())
74960c60
VP
2491 insert_breakpoint_locations ();
2492}
2493
20388dd6
YQ
2494/* Invoke CALLBACK for each of bp_location. */
2495
2496void
2497iterate_over_bp_locations (walk_bp_location_callback callback)
2498{
2499 struct bp_location *loc, **loc_tmp;
2500
2501 ALL_BP_LOCATIONS (loc, loc_tmp)
2502 {
2503 callback (loc, NULL);
2504 }
2505}
2506
b775012e
LM
2507/* This is used when we need to synch breakpoint conditions between GDB and the
2508 target. It is the case with deleting and disabling of breakpoints when using
2509 always-inserted mode. */
2510
2511static void
2512update_inserted_breakpoint_locations (void)
2513{
2514 struct bp_location *bl, **blp_tmp;
2515 int error_flag = 0;
2516 int val = 0;
2517 int disabled_breaks = 0;
2518 int hw_breakpoint_error = 0;
2519
2520 struct ui_file *tmp_error_stream = mem_fileopen ();
2521 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2522
2523 /* Explicitly mark the warning -- this will only be printed if
2524 there was an error. */
2525 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2526
2527 save_current_space_and_thread ();
2528
2529 ALL_BP_LOCATIONS (bl, blp_tmp)
2530 {
2531 /* We only want to update software breakpoints and hardware
2532 breakpoints. */
2533 if (!is_breakpoint (bl->owner))
2534 continue;
2535
2536 /* We only want to update locations that are already inserted
2537 and need updating. This is to avoid unwanted insertion during
2538 deletion of breakpoints. */
2539 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2540 continue;
2541
2542 switch_to_program_space_and_thread (bl->pspace);
2543
2544 /* For targets that support global breakpoints, there's no need
2545 to select an inferior to insert breakpoint to. In fact, even
2546 if we aren't attached to any process yet, we should still
2547 insert breakpoints. */
2548 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2549 && ptid_equal (inferior_ptid, null_ptid))
2550 continue;
2551
2552 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2553 &hw_breakpoint_error);
2554 if (val)
2555 error_flag = val;
2556 }
2557
2558 if (error_flag)
2559 {
2560 target_terminal_ours_for_output ();
2561 error_stream (tmp_error_stream);
2562 }
2563
2564 do_cleanups (cleanups);
2565}
2566
c30eee59 2567/* Used when starting or continuing the program. */
c906108c 2568
74960c60
VP
2569static void
2570insert_breakpoint_locations (void)
c906108c 2571{
a5606eee 2572 struct breakpoint *bpt;
35df4500 2573 struct bp_location *bl, **blp_tmp;
eacd795a 2574 int error_flag = 0;
c906108c 2575 int val = 0;
3fbb6ffa 2576 int disabled_breaks = 0;
81d0cc19 2577 int hw_breakpoint_error = 0;
c906108c 2578
81d0cc19 2579 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 2580 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 2581
81d0cc19
GS
2582 /* Explicitly mark the warning -- this will only be printed if
2583 there was an error. */
2584 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
2585
2586 save_current_space_and_thread ();
2587
35df4500 2588 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2589 {
b775012e 2590 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2591 continue;
2592
4a64f543
MS
2593 /* There is no point inserting thread-specific breakpoints if
2594 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2595 has BL->OWNER always non-NULL. */
35df4500
TJB
2596 if (bl->owner->thread != -1
2597 && !valid_thread_id (bl->owner->thread))
f365de73
AS
2598 continue;
2599
35df4500 2600 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2601
2602 /* For targets that support global breakpoints, there's no need
2603 to select an inferior to insert breakpoint to. In fact, even
2604 if we aren't attached to any process yet, we should still
2605 insert breakpoints. */
2606 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2607 && ptid_equal (inferior_ptid, null_ptid))
2608 continue;
2609
3fbb6ffa
TJB
2610 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2611 &hw_breakpoint_error);
879bfdc2 2612 if (val)
eacd795a 2613 error_flag = val;
879bfdc2 2614 }
c906108c 2615
4a64f543
MS
2616 /* If we failed to insert all locations of a watchpoint, remove
2617 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2618 ALL_BREAKPOINTS (bpt)
2619 {
2620 int some_failed = 0;
2621 struct bp_location *loc;
2622
2623 if (!is_hardware_watchpoint (bpt))
2624 continue;
2625
d6b74ac4 2626 if (!breakpoint_enabled (bpt))
a5606eee 2627 continue;
74960c60
VP
2628
2629 if (bpt->disposition == disp_del_at_next_stop)
2630 continue;
a5606eee
VP
2631
2632 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2633 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2634 {
2635 some_failed = 1;
2636 break;
2637 }
2638 if (some_failed)
2639 {
2640 for (loc = bpt->loc; loc; loc = loc->next)
2641 if (loc->inserted)
2642 remove_breakpoint (loc, mark_uninserted);
2643
2644 hw_breakpoint_error = 1;
2645 fprintf_unfiltered (tmp_error_stream,
2646 "Could not insert hardware watchpoint %d.\n",
2647 bpt->number);
eacd795a 2648 error_flag = -1;
a5606eee
VP
2649 }
2650 }
2651
eacd795a 2652 if (error_flag)
81d0cc19
GS
2653 {
2654 /* If a hardware breakpoint or watchpoint was inserted, add a
2655 message about possibly exhausted resources. */
879bfdc2 2656 if (hw_breakpoint_error)
81d0cc19 2657 {
c6510018
MS
2658 fprintf_unfiltered (tmp_error_stream,
2659 "Could not insert hardware breakpoints:\n\
2660You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2661 }
81d0cc19
GS
2662 target_terminal_ours_for_output ();
2663 error_stream (tmp_error_stream);
2664 }
f7545552
TT
2665
2666 do_cleanups (cleanups);
c906108c
SS
2667}
2668
c30eee59
TJB
2669/* Used when the program stops.
2670 Returns zero if successful, or non-zero if there was a problem
2671 removing a breakpoint location. */
2672
c906108c 2673int
fba45db2 2674remove_breakpoints (void)
c906108c 2675{
35df4500 2676 struct bp_location *bl, **blp_tmp;
3a1bae8e 2677 int val = 0;
c906108c 2678
35df4500 2679 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2680 {
1e4d1764 2681 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 2682 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2683 }
3a1bae8e 2684 return val;
c906108c
SS
2685}
2686
6c95b8df
PA
2687/* Remove breakpoints of process PID. */
2688
2689int
2690remove_breakpoints_pid (int pid)
2691{
35df4500 2692 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2693 int val;
2694 struct inferior *inf = find_inferior_pid (pid);
2695
35df4500 2696 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2697 {
35df4500 2698 if (bl->pspace != inf->pspace)
6c95b8df
PA
2699 continue;
2700
35df4500 2701 if (bl->inserted)
6c95b8df 2702 {
35df4500 2703 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2704 if (val != 0)
2705 return val;
2706 }
2707 }
2708 return 0;
2709}
2710
c906108c 2711int
fba45db2 2712reattach_breakpoints (int pid)
c906108c 2713{
6c95b8df 2714 struct cleanup *old_chain;
35df4500 2715 struct bp_location *bl, **blp_tmp;
c906108c 2716 int val;
86b887df 2717 struct ui_file *tmp_error_stream;
3fbb6ffa 2718 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2719 struct inferior *inf;
2720 struct thread_info *tp;
2721
2722 tp = any_live_thread_of_process (pid);
2723 if (tp == NULL)
2724 return 1;
2725
2726 inf = find_inferior_pid (pid);
2727 old_chain = save_inferior_ptid ();
2728
2729 inferior_ptid = tp->ptid;
a4954f26 2730
86b887df 2731 tmp_error_stream = mem_fileopen ();
a4954f26 2732 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2733
35df4500 2734 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2735 {
35df4500 2736 if (bl->pspace != inf->pspace)
6c95b8df
PA
2737 continue;
2738
35df4500 2739 if (bl->inserted)
c5aa993b 2740 {
35df4500 2741 bl->inserted = 0;
3fbb6ffa 2742 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2743 if (val != 0)
2744 {
ce696e05 2745 do_cleanups (old_chain);
c5aa993b
JM
2746 return val;
2747 }
2748 }
2749 }
ce696e05 2750 do_cleanups (old_chain);
c906108c
SS
2751 return 0;
2752}
2753
e58b0e63
PA
2754static int internal_breakpoint_number = -1;
2755
84f4c1fe
PM
2756/* Set the breakpoint number of B, depending on the value of INTERNAL.
2757 If INTERNAL is non-zero, the breakpoint number will be populated
2758 from internal_breakpoint_number and that variable decremented.
e5dd4106 2759 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2760 breakpoint_count and that value incremented. Internal breakpoints
2761 do not set the internal var bpnum. */
2762static void
2763set_breakpoint_number (int internal, struct breakpoint *b)
2764{
2765 if (internal)
2766 b->number = internal_breakpoint_number--;
2767 else
2768 {
2769 set_breakpoint_count (breakpoint_count + 1);
2770 b->number = breakpoint_count;
2771 }
2772}
2773
e62c965a 2774static struct breakpoint *
a6d9a66e 2775create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 2776 CORE_ADDR address, enum bptype type,
c0a91b2b 2777 const struct breakpoint_ops *ops)
e62c965a 2778{
e62c965a
PP
2779 struct symtab_and_line sal;
2780 struct breakpoint *b;
2781
4a64f543 2782 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2783
2784 sal.pc = address;
2785 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2786 sal.pspace = current_program_space;
e62c965a 2787
06edf0c0 2788 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2789 b->number = internal_breakpoint_number--;
2790 b->disposition = disp_donttouch;
2791
2792 return b;
2793}
2794
17450429
PP
2795static const char *const longjmp_names[] =
2796 {
2797 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2798 };
2799#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2800
2801/* Per-objfile data private to breakpoint.c. */
2802struct breakpoint_objfile_data
2803{
2804 /* Minimal symbol for "_ovly_debug_event" (if any). */
2805 struct minimal_symbol *overlay_msym;
2806
2807 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2808 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2809
28106bc2
SDJ
2810 /* True if we have looked for longjmp probes. */
2811 int longjmp_searched;
2812
2813 /* SystemTap probe points for longjmp (if any). */
2814 VEC (probe_p) *longjmp_probes;
2815
17450429
PP
2816 /* Minimal symbol for "std::terminate()" (if any). */
2817 struct minimal_symbol *terminate_msym;
2818
2819 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2820 struct minimal_symbol *exception_msym;
28106bc2
SDJ
2821
2822 /* True if we have looked for exception probes. */
2823 int exception_searched;
2824
2825 /* SystemTap probe points for unwinding (if any). */
2826 VEC (probe_p) *exception_probes;
17450429
PP
2827};
2828
2829static const struct objfile_data *breakpoint_objfile_key;
2830
2831/* Minimal symbol not found sentinel. */
2832static struct minimal_symbol msym_not_found;
2833
2834/* Returns TRUE if MSYM point to the "not found" sentinel. */
2835
2836static int
2837msym_not_found_p (const struct minimal_symbol *msym)
2838{
2839 return msym == &msym_not_found;
2840}
2841
2842/* Return per-objfile data needed by breakpoint.c.
2843 Allocate the data if necessary. */
2844
2845static struct breakpoint_objfile_data *
2846get_breakpoint_objfile_data (struct objfile *objfile)
2847{
2848 struct breakpoint_objfile_data *bp_objfile_data;
2849
2850 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2851 if (bp_objfile_data == NULL)
2852 {
2853 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2854 sizeof (*bp_objfile_data));
2855
2856 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2857 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2858 }
2859 return bp_objfile_data;
2860}
2861
28106bc2
SDJ
2862static void
2863free_breakpoint_probes (struct objfile *obj, void *data)
2864{
2865 struct breakpoint_objfile_data *bp_objfile_data = data;
2866
2867 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
2868 VEC_free (probe_p, bp_objfile_data->exception_probes);
2869}
2870
e62c965a 2871static void
af02033e 2872create_overlay_event_breakpoint (void)
e62c965a 2873{
69de3c6a 2874 struct objfile *objfile;
af02033e 2875 const char *const func_name = "_ovly_debug_event";
e62c965a 2876
69de3c6a
PP
2877 ALL_OBJFILES (objfile)
2878 {
2879 struct breakpoint *b;
17450429
PP
2880 struct breakpoint_objfile_data *bp_objfile_data;
2881 CORE_ADDR addr;
69de3c6a 2882
17450429
PP
2883 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2884
2885 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2886 continue;
2887
2888 if (bp_objfile_data->overlay_msym == NULL)
2889 {
2890 struct minimal_symbol *m;
2891
2892 m = lookup_minimal_symbol_text (func_name, objfile);
2893 if (m == NULL)
2894 {
2895 /* Avoid future lookups in this objfile. */
2896 bp_objfile_data->overlay_msym = &msym_not_found;
2897 continue;
2898 }
2899 bp_objfile_data->overlay_msym = m;
2900 }
e62c965a 2901
17450429
PP
2902 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2903 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2904 bp_overlay_event,
2905 &internal_breakpoint_ops);
69de3c6a 2906 b->addr_string = xstrdup (func_name);
e62c965a 2907
69de3c6a
PP
2908 if (overlay_debugging == ovly_auto)
2909 {
2910 b->enable_state = bp_enabled;
2911 overlay_events_enabled = 1;
2912 }
2913 else
2914 {
2915 b->enable_state = bp_disabled;
2916 overlay_events_enabled = 0;
2917 }
e62c965a
PP
2918 }
2919 update_global_location_list (1);
2920}
2921
0fd8e87f 2922static void
af02033e 2923create_longjmp_master_breakpoint (void)
0fd8e87f 2924{
6c95b8df 2925 struct program_space *pspace;
6c95b8df
PA
2926 struct cleanup *old_chain;
2927
2928 old_chain = save_current_program_space ();
0fd8e87f 2929
6c95b8df 2930 ALL_PSPACES (pspace)
af02033e
PP
2931 {
2932 struct objfile *objfile;
2933
2934 set_current_program_space (pspace);
2935
2936 ALL_OBJFILES (objfile)
0fd8e87f 2937 {
af02033e
PP
2938 int i;
2939 struct gdbarch *gdbarch;
17450429 2940 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2941
af02033e
PP
2942 gdbarch = get_objfile_arch (objfile);
2943 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2944 continue;
2945
17450429
PP
2946 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2947
28106bc2
SDJ
2948 if (!bp_objfile_data->longjmp_searched)
2949 {
2950 bp_objfile_data->longjmp_probes
2951 = find_probes_in_objfile (objfile, "libc", "longjmp");
2952 bp_objfile_data->longjmp_searched = 1;
2953 }
2954
2955 if (bp_objfile_data->longjmp_probes != NULL)
2956 {
2957 int i;
2958 struct probe *probe;
2959 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2960
2961 for (i = 0;
2962 VEC_iterate (probe_p,
2963 bp_objfile_data->longjmp_probes,
2964 i, probe);
2965 ++i)
2966 {
2967 struct breakpoint *b;
2968
2969 b = create_internal_breakpoint (gdbarch, probe->address,
2970 bp_longjmp_master,
2971 &internal_breakpoint_ops);
2972 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
2973 b->enable_state = bp_disabled;
2974 }
2975
2976 continue;
2977 }
2978
17450429 2979 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2980 {
2981 struct breakpoint *b;
af02033e 2982 const char *func_name;
17450429 2983 CORE_ADDR addr;
6c95b8df 2984
17450429 2985 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2986 continue;
0fd8e87f 2987
17450429
PP
2988 func_name = longjmp_names[i];
2989 if (bp_objfile_data->longjmp_msym[i] == NULL)
2990 {
2991 struct minimal_symbol *m;
2992
2993 m = lookup_minimal_symbol_text (func_name, objfile);
2994 if (m == NULL)
2995 {
2996 /* Prevent future lookups in this objfile. */
2997 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2998 continue;
2999 }
3000 bp_objfile_data->longjmp_msym[i] = m;
3001 }
3002
3003 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3004 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3005 &internal_breakpoint_ops);
af02033e
PP
3006 b->addr_string = xstrdup (func_name);
3007 b->enable_state = bp_disabled;
3008 }
0fd8e87f 3009 }
af02033e 3010 }
0fd8e87f 3011 update_global_location_list (1);
6c95b8df
PA
3012
3013 do_cleanups (old_chain);
0fd8e87f
UW
3014}
3015
af02033e 3016/* Create a master std::terminate breakpoint. */
aa7d318d 3017static void
af02033e 3018create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3019{
3020 struct program_space *pspace;
aa7d318d 3021 struct cleanup *old_chain;
af02033e 3022 const char *const func_name = "std::terminate()";
aa7d318d
TT
3023
3024 old_chain = save_current_program_space ();
3025
3026 ALL_PSPACES (pspace)
17450429
PP
3027 {
3028 struct objfile *objfile;
3029 CORE_ADDR addr;
3030
3031 set_current_program_space (pspace);
3032
aa7d318d
TT
3033 ALL_OBJFILES (objfile)
3034 {
3035 struct breakpoint *b;
17450429 3036 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3037
17450429 3038 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3039
17450429
PP
3040 if (msym_not_found_p (bp_objfile_data->terminate_msym))
3041 continue;
3042
3043 if (bp_objfile_data->terminate_msym == NULL)
3044 {
3045 struct minimal_symbol *m;
3046
3047 m = lookup_minimal_symbol (func_name, NULL, objfile);
3048 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
3049 && MSYMBOL_TYPE (m) != mst_file_text))
3050 {
3051 /* Prevent future lookups in this objfile. */
3052 bp_objfile_data->terminate_msym = &msym_not_found;
3053 continue;
3054 }
3055 bp_objfile_data->terminate_msym = m;
3056 }
aa7d318d 3057
17450429
PP
3058 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3059 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3060 bp_std_terminate_master,
3061 &internal_breakpoint_ops);
aa7d318d
TT
3062 b->addr_string = xstrdup (func_name);
3063 b->enable_state = bp_disabled;
3064 }
17450429
PP
3065 }
3066
aa7d318d
TT
3067 update_global_location_list (1);
3068
3069 do_cleanups (old_chain);
3070}
3071
186c406b
TT
3072/* Install a master breakpoint on the unwinder's debug hook. */
3073
70221824 3074static void
186c406b
TT
3075create_exception_master_breakpoint (void)
3076{
3077 struct objfile *objfile;
17450429 3078 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3079
3080 ALL_OBJFILES (objfile)
3081 {
17450429
PP
3082 struct breakpoint *b;
3083 struct gdbarch *gdbarch;
3084 struct breakpoint_objfile_data *bp_objfile_data;
3085 CORE_ADDR addr;
3086
3087 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3088
28106bc2
SDJ
3089 /* We prefer the SystemTap probe point if it exists. */
3090 if (!bp_objfile_data->exception_searched)
3091 {
3092 bp_objfile_data->exception_probes
3093 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3094 bp_objfile_data->exception_searched = 1;
3095 }
3096
3097 if (bp_objfile_data->exception_probes != NULL)
3098 {
3099 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3100 int i;
3101 struct probe *probe;
3102
3103 for (i = 0;
3104 VEC_iterate (probe_p,
3105 bp_objfile_data->exception_probes,
3106 i, probe);
3107 ++i)
3108 {
3109 struct breakpoint *b;
3110
3111 b = create_internal_breakpoint (gdbarch, probe->address,
3112 bp_exception_master,
3113 &internal_breakpoint_ops);
3114 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3115 b->enable_state = bp_disabled;
3116 }
3117
3118 continue;
3119 }
3120
3121 /* Otherwise, try the hook function. */
3122
17450429
PP
3123 if (msym_not_found_p (bp_objfile_data->exception_msym))
3124 continue;
3125
3126 gdbarch = get_objfile_arch (objfile);
186c406b 3127
17450429 3128 if (bp_objfile_data->exception_msym == NULL)
186c406b 3129 {
17450429 3130 struct minimal_symbol *debug_hook;
186c406b 3131
17450429
PP
3132 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3133 if (debug_hook == NULL)
3134 {
3135 bp_objfile_data->exception_msym = &msym_not_found;
3136 continue;
3137 }
3138
3139 bp_objfile_data->exception_msym = debug_hook;
186c406b 3140 }
17450429
PP
3141
3142 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3143 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3144 &current_target);
06edf0c0
PA
3145 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3146 &internal_breakpoint_ops);
17450429
PP
3147 b->addr_string = xstrdup (func_name);
3148 b->enable_state = bp_disabled;
186c406b
TT
3149 }
3150
3151 update_global_location_list (1);
3152}
3153
c906108c 3154void
fba45db2 3155update_breakpoints_after_exec (void)
c906108c 3156{
35df4500 3157 struct breakpoint *b, *b_tmp;
876fa593 3158 struct bp_location *bploc, **bplocp_tmp;
c906108c 3159
25b22b0a
PA
3160 /* We're about to delete breakpoints from GDB's lists. If the
3161 INSERTED flag is true, GDB will try to lift the breakpoints by
3162 writing the breakpoints' "shadow contents" back into memory. The
3163 "shadow contents" are NOT valid after an exec, so GDB should not
3164 do that. Instead, the target is responsible from marking
3165 breakpoints out as soon as it detects an exec. We don't do that
3166 here instead, because there may be other attempts to delete
3167 breakpoints after detecting an exec and before reaching here. */
876fa593 3168 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3169 if (bploc->pspace == current_program_space)
3170 gdb_assert (!bploc->inserted);
c906108c 3171
35df4500 3172 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3173 {
6c95b8df
PA
3174 if (b->pspace != current_program_space)
3175 continue;
3176
4a64f543 3177 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3178 if (b->type == bp_shlib_event)
3179 {
3180 delete_breakpoint (b);
3181 continue;
3182 }
c906108c 3183
4a64f543 3184 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3185 if (b->type == bp_jit_event)
3186 {
3187 delete_breakpoint (b);
3188 continue;
3189 }
3190
1900040c 3191 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3192 as must overlay event and longjmp master breakpoints. */
3193 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3194 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3195 || b->type == bp_exception_master)
c4093a6a
JM
3196 {
3197 delete_breakpoint (b);
3198 continue;
3199 }
3200
4a64f543 3201 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3202 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3203 {
3204 delete_breakpoint (b);
3205 continue;
3206 }
3207
611c83ae
PA
3208 /* Longjmp and longjmp-resume breakpoints are also meaningless
3209 after an exec. */
186c406b 3210 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3211 || b->type == bp_longjmp_call_dummy
186c406b 3212 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3213 {
3214 delete_breakpoint (b);
3215 continue;
3216 }
3217
ce78b96d
JB
3218 if (b->type == bp_catchpoint)
3219 {
3220 /* For now, none of the bp_catchpoint breakpoints need to
3221 do anything at this point. In the future, if some of
3222 the catchpoints need to something, we will need to add
3223 a new method, and call this method from here. */
3224 continue;
3225 }
3226
c5aa993b
JM
3227 /* bp_finish is a special case. The only way we ought to be able
3228 to see one of these when an exec() has happened, is if the user
3229 caught a vfork, and then said "finish". Ordinarily a finish just
3230 carries them to the call-site of the current callee, by setting
3231 a temporary bp there and resuming. But in this case, the finish
3232 will carry them entirely through the vfork & exec.
3233
3234 We don't want to allow a bp_finish to remain inserted now. But
3235 we can't safely delete it, 'cause finish_command has a handle to
3236 the bp on a bpstat, and will later want to delete it. There's a
3237 chance (and I've seen it happen) that if we delete the bp_finish
3238 here, that its storage will get reused by the time finish_command
3239 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3240 We really must allow finish_command to delete a bp_finish.
3241
e5dd4106 3242 In the absence of a general solution for the "how do we know
53a5351d
JM
3243 it's safe to delete something others may have handles to?"
3244 problem, what we'll do here is just uninsert the bp_finish, and
3245 let finish_command delete it.
3246
3247 (We know the bp_finish is "doomed" in the sense that it's
3248 momentary, and will be deleted as soon as finish_command sees
3249 the inferior stopped. So it doesn't matter that the bp's
3250 address is probably bogus in the new a.out, unlike e.g., the
3251 solib breakpoints.) */
c5aa993b 3252
c5aa993b
JM
3253 if (b->type == bp_finish)
3254 {
3255 continue;
3256 }
3257
3258 /* Without a symbolic address, we have little hope of the
3259 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3260 a.out. */
c5aa993b
JM
3261 if (b->addr_string == NULL)
3262 {
3263 delete_breakpoint (b);
3264 continue;
3265 }
c5aa993b 3266 }
1900040c 3267 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
3268 create_overlay_event_breakpoint ();
3269 create_longjmp_master_breakpoint ();
3270 create_std_terminate_master_breakpoint ();
186c406b 3271 create_exception_master_breakpoint ();
c906108c
SS
3272}
3273
3274int
fba45db2 3275detach_breakpoints (int pid)
c906108c 3276{
35df4500 3277 struct bp_location *bl, **blp_tmp;
3a1bae8e 3278 int val = 0;
ce696e05 3279 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3280 struct inferior *inf = current_inferior ();
c5aa993b 3281
39f77062 3282 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 3283 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3284
6c95b8df 3285 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 3286 inferior_ptid = pid_to_ptid (pid);
35df4500 3287 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3288 {
35df4500 3289 if (bl->pspace != inf->pspace)
6c95b8df
PA
3290 continue;
3291
35df4500
TJB
3292 if (bl->inserted)
3293 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3294 }
d03285ec
UW
3295
3296 /* Detach single-step breakpoints as well. */
3297 detach_single_step_breakpoints ();
3298
ce696e05 3299 do_cleanups (old_chain);
3a1bae8e 3300 return val;
c906108c
SS
3301}
3302
35df4500 3303/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3304 Note that this is used to detach breakpoints from a child fork.
3305 When we get here, the child isn't in the inferior list, and neither
3306 do we have objects to represent its address space --- we should
35df4500 3307 *not* look at bl->pspace->aspace here. */
6c95b8df 3308
c906108c 3309static int
35df4500 3310remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3311{
3312 int val;
c5aa993b 3313
35df4500
TJB
3314 /* BL is never in moribund_locations by our callers. */
3315 gdb_assert (bl->owner != NULL);
2bdf28a0 3316
35df4500 3317 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3318 /* Permanent breakpoints cannot be inserted or removed. */
3319 return 0;
3320
74960c60
VP
3321 /* The type of none suggests that owner is actually deleted.
3322 This should not ever happen. */
35df4500 3323 gdb_assert (bl->owner->type != bp_none);
0bde7532 3324
35df4500
TJB
3325 if (bl->loc_type == bp_loc_software_breakpoint
3326 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3327 {
c02f5703
MS
3328 /* "Normal" instruction breakpoint: either the standard
3329 trap-instruction bp (bp_breakpoint), or a
3330 bp_hardware_breakpoint. */
3331
3332 /* First check to see if we have to handle an overlay. */
3333 if (overlay_debugging == ovly_off
35df4500
TJB
3334 || bl->section == NULL
3335 || !(section_is_overlay (bl->section)))
c02f5703
MS
3336 {
3337 /* No overlay handling: just remove the breakpoint. */
348d480f 3338 val = bl->owner->ops->remove_location (bl);
c02f5703 3339 }
c906108c
SS
3340 else
3341 {
4a64f543 3342 /* This breakpoint is in an overlay section.
c02f5703
MS
3343 Did we set a breakpoint at the LMA? */
3344 if (!overlay_events_enabled)
3345 {
3346 /* Yes -- overlay event support is not active, so we
3347 should have set a breakpoint at the LMA. Remove it.
3348 */
c02f5703
MS
3349 /* Ignore any failures: if the LMA is in ROM, we will
3350 have already warned when we failed to insert it. */
35df4500
TJB
3351 if (bl->loc_type == bp_loc_hardware_breakpoint)
3352 target_remove_hw_breakpoint (bl->gdbarch,
3353 &bl->overlay_target_info);
c02f5703 3354 else
35df4500
TJB
3355 target_remove_breakpoint (bl->gdbarch,
3356 &bl->overlay_target_info);
c02f5703
MS
3357 }
3358 /* Did we set a breakpoint at the VMA?
3359 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3360 if (bl->inserted)
c906108c 3361 {
c02f5703
MS
3362 /* Yes -- remove it. Previously we did not bother to
3363 remove the breakpoint if the section had been
3364 unmapped, but let's not rely on that being safe. We
3365 don't know what the overlay manager might do. */
aa67235e
UW
3366
3367 /* However, we should remove *software* breakpoints only
3368 if the section is still mapped, or else we overwrite
3369 wrong code with the saved shadow contents. */
348d480f
PA
3370 if (bl->loc_type == bp_loc_hardware_breakpoint
3371 || section_is_mapped (bl->section))
3372 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3373 else
3374 val = 0;
c906108c 3375 }
c02f5703
MS
3376 else
3377 {
3378 /* No -- not inserted, so no need to remove. No error. */
3379 val = 0;
3380 }
c906108c 3381 }
879d1e6b
UW
3382
3383 /* In some cases, we might not be able to remove a breakpoint
3384 in a shared library that has already been removed, but we
3385 have not yet processed the shlib unload event. */
35df4500 3386 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
3387 val = 0;
3388
c906108c
SS
3389 if (val)
3390 return val;
35df4500 3391 bl->inserted = (is == mark_inserted);
c906108c 3392 }
35df4500 3393 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3394 {
77b06cd7
TJB
3395 gdb_assert (bl->owner->ops != NULL
3396 && bl->owner->ops->remove_location != NULL);
3397
35df4500 3398 bl->inserted = (is == mark_inserted);
77b06cd7 3399 bl->owner->ops->remove_location (bl);
2e70b7b9 3400
c906108c 3401 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 3402 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 3403 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3404 bl->owner->number);
c906108c 3405 }
35df4500
TJB
3406 else if (bl->owner->type == bp_catchpoint
3407 && breakpoint_enabled (bl->owner)
3408 && !bl->duplicate)
ce78b96d 3409 {
77b06cd7
TJB
3410 gdb_assert (bl->owner->ops != NULL
3411 && bl->owner->ops->remove_location != NULL);
ce78b96d 3412
77b06cd7 3413 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
3414 if (val)
3415 return val;
77b06cd7 3416
35df4500 3417 bl->inserted = (is == mark_inserted);
ce78b96d 3418 }
c906108c
SS
3419
3420 return 0;
3421}
3422
6c95b8df 3423static int
35df4500 3424remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
3425{
3426 int ret;
3427 struct cleanup *old_chain;
3428
35df4500
TJB
3429 /* BL is never in moribund_locations by our callers. */
3430 gdb_assert (bl->owner != NULL);
2bdf28a0 3431
35df4500 3432 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
3433 /* Permanent breakpoints cannot be inserted or removed. */
3434 return 0;
3435
3436 /* The type of none suggests that owner is actually deleted.
3437 This should not ever happen. */
35df4500 3438 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
3439
3440 old_chain = save_current_space_and_thread ();
3441
35df4500 3442 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3443
35df4500 3444 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
3445
3446 do_cleanups (old_chain);
3447 return ret;
3448}
3449
c906108c
SS
3450/* Clear the "inserted" flag in all breakpoints. */
3451
25b22b0a 3452void
fba45db2 3453mark_breakpoints_out (void)
c906108c 3454{
35df4500 3455 struct bp_location *bl, **blp_tmp;
c906108c 3456
35df4500
TJB
3457 ALL_BP_LOCATIONS (bl, blp_tmp)
3458 if (bl->pspace == current_program_space)
3459 bl->inserted = 0;
c906108c
SS
3460}
3461
53a5351d
JM
3462/* Clear the "inserted" flag in all breakpoints and delete any
3463 breakpoints which should go away between runs of the program.
c906108c
SS
3464
3465 Plus other such housekeeping that has to be done for breakpoints
3466 between runs.
3467
53a5351d
JM
3468 Note: this function gets called at the end of a run (by
3469 generic_mourn_inferior) and when a run begins (by
4a64f543 3470 init_wait_for_inferior). */
c906108c
SS
3471
3472
3473
3474void
fba45db2 3475breakpoint_init_inferior (enum inf_context context)
c906108c 3476{
35df4500
TJB
3477 struct breakpoint *b, *b_tmp;
3478 struct bp_location *bl, **blp_tmp;
1c5cfe86 3479 int ix;
6c95b8df 3480 struct program_space *pspace = current_program_space;
c906108c 3481
50c71eaf
PA
3482 /* If breakpoint locations are shared across processes, then there's
3483 nothing to do. */
2567c7d9 3484 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
3485 return;
3486
35df4500 3487 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3488 {
35df4500
TJB
3489 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3490 if (bl->pspace == pspace
3491 && bl->owner->enable_state != bp_permanent)
3492 bl->inserted = 0;
6c95b8df 3493 }
075f6582 3494
35df4500 3495 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3496 {
6c95b8df
PA
3497 if (b->loc && b->loc->pspace != pspace)
3498 continue;
3499
c5aa993b
JM
3500 switch (b->type)
3501 {
3502 case bp_call_dummy:
e2e4d78b 3503 case bp_longjmp_call_dummy:
c906108c 3504
c5aa993b 3505 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3506 cause problems when the inferior is rerun, so we better get
3507 rid of it. */
3508
3509 case bp_watchpoint_scope:
3510
3511 /* Also get rid of scope breakpoints. */
3512
3513 case bp_shlib_event:
3514
3515 /* Also remove solib event breakpoints. Their addresses may
3516 have changed since the last time we ran the program.
3517 Actually we may now be debugging against different target;
3518 and so the solib backend that installed this breakpoint may
3519 not be used in by the target. E.g.,
3520
3521 (gdb) file prog-linux
3522 (gdb) run # native linux target
3523 ...
3524 (gdb) kill
3525 (gdb) file prog-win.exe
3526 (gdb) tar rem :9999 # remote Windows gdbserver.
3527 */
c906108c 3528
f59f708a
PA
3529 case bp_step_resume:
3530
3531 /* Also remove step-resume breakpoints. */
3532
c5aa993b
JM
3533 delete_breakpoint (b);
3534 break;
c906108c 3535
c5aa993b
JM
3536 case bp_watchpoint:
3537 case bp_hardware_watchpoint:
3538 case bp_read_watchpoint:
3539 case bp_access_watchpoint:
3a5c3e22
PA
3540 {
3541 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3542
3a5c3e22
PA
3543 /* Likewise for watchpoints on local expressions. */
3544 if (w->exp_valid_block != NULL)
3545 delete_breakpoint (b);
3546 else if (context == inf_starting)
3547 {
3548 /* Reset val field to force reread of starting value in
3549 insert_breakpoints. */
3550 if (w->val)
3551 value_free (w->val);
3552 w->val = NULL;
3553 w->val_valid = 0;
c860120c 3554 }
3a5c3e22 3555 }
c5aa993b
JM
3556 break;
3557 default:
c5aa993b
JM
3558 break;
3559 }
3560 }
1c5cfe86
PA
3561
3562 /* Get rid of the moribund locations. */
35df4500
TJB
3563 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3564 decref_bp_location (&bl);
1c5cfe86 3565 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
3566}
3567
6c95b8df
PA
3568/* These functions concern about actual breakpoints inserted in the
3569 target --- to e.g. check if we need to do decr_pc adjustment or if
3570 we need to hop over the bkpt --- so we check for address space
3571 match, not program space. */
3572
c2c6d25f
JM
3573/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3574 exists at PC. It returns ordinary_breakpoint_here if it's an
3575 ordinary breakpoint, or permanent_breakpoint_here if it's a
3576 permanent breakpoint.
3577 - When continuing from a location with an ordinary breakpoint, we
3578 actually single step once before calling insert_breakpoints.
e5dd4106 3579 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
3580 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3581 the target, to advance the PC past the breakpoint. */
c906108c 3582
c2c6d25f 3583enum breakpoint_here
6c95b8df 3584breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 3585{
35df4500 3586 struct bp_location *bl, **blp_tmp;
c2c6d25f 3587 int any_breakpoint_here = 0;
c906108c 3588
35df4500 3589 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 3590 {
35df4500
TJB
3591 if (bl->loc_type != bp_loc_software_breakpoint
3592 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3593 continue;
3594
f1310107 3595 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
3596 if ((breakpoint_enabled (bl->owner)
3597 || bl->owner->enable_state == bp_permanent)
f1310107 3598 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3599 {
3600 if (overlay_debugging
35df4500
TJB
3601 && section_is_overlay (bl->section)
3602 && !section_is_mapped (bl->section))
075f6582 3603 continue; /* unmapped overlay -- can't be a match */
35df4500 3604 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
3605 return permanent_breakpoint_here;
3606 else
3607 any_breakpoint_here = 1;
3608 }
3609 }
c906108c 3610
c2c6d25f 3611 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
3612}
3613
1c5cfe86
PA
3614/* Return true if there's a moribund breakpoint at PC. */
3615
3616int
6c95b8df 3617moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
3618{
3619 struct bp_location *loc;
3620 int ix;
3621
3622 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 3623 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
3624 return 1;
3625
3626 return 0;
3627}
c2c6d25f 3628
c36b740a 3629/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
3630 inserted using regular breakpoint_chain / bp_location array
3631 mechanism. This does not check for single-step breakpoints, which
3632 are inserted and removed using direct target manipulation. */
c906108c
SS
3633
3634int
4a64f543
MS
3635regular_breakpoint_inserted_here_p (struct address_space *aspace,
3636 CORE_ADDR pc)
c906108c 3637{
35df4500 3638 struct bp_location *bl, **blp_tmp;
c906108c 3639
35df4500 3640 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3641 {
35df4500
TJB
3642 if (bl->loc_type != bp_loc_software_breakpoint
3643 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3644 continue;
3645
35df4500 3646 if (bl->inserted
f1310107 3647 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3648 {
3649 if (overlay_debugging
35df4500
TJB
3650 && section_is_overlay (bl->section)
3651 && !section_is_mapped (bl->section))
075f6582
DJ
3652 continue; /* unmapped overlay -- can't be a match */
3653 else
3654 return 1;
3655 }
c5aa993b 3656 }
c36b740a
VP
3657 return 0;
3658}
3659
3660/* Returns non-zero iff there's either regular breakpoint
3661 or a single step breakpoint inserted at PC. */
3662
3663int
6c95b8df 3664breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 3665{
6c95b8df 3666 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 3667 return 1;
c906108c 3668
6c95b8df 3669 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3670 return 1;
3671
c906108c
SS
3672 return 0;
3673}
3674
4fa8626c
DJ
3675/* This function returns non-zero iff there is a software breakpoint
3676 inserted at PC. */
3677
3678int
3e43a32a
MS
3679software_breakpoint_inserted_here_p (struct address_space *aspace,
3680 CORE_ADDR pc)
4fa8626c 3681{
35df4500 3682 struct bp_location *bl, **blp_tmp;
4fa8626c 3683
35df4500 3684 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 3685 {
35df4500 3686 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
3687 continue;
3688
35df4500
TJB
3689 if (bl->inserted
3690 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 3691 aspace, pc))
4fa8626c
DJ
3692 {
3693 if (overlay_debugging
35df4500
TJB
3694 && section_is_overlay (bl->section)
3695 && !section_is_mapped (bl->section))
4fa8626c
DJ
3696 continue; /* unmapped overlay -- can't be a match */
3697 else
3698 return 1;
3699 }
3700 }
3701
1aafd4da 3702 /* Also check for software single-step breakpoints. */
6c95b8df 3703 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3704 return 1;
3705
4fa8626c
DJ
3706 return 0;
3707}
3708
9093389c
PA
3709int
3710hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3711 CORE_ADDR addr, ULONGEST len)
3712{
3713 struct breakpoint *bpt;
3714
3715 ALL_BREAKPOINTS (bpt)
3716 {
3717 struct bp_location *loc;
3718
3719 if (bpt->type != bp_hardware_watchpoint
3720 && bpt->type != bp_access_watchpoint)
3721 continue;
3722
3723 if (!breakpoint_enabled (bpt))
3724 continue;
3725
3726 for (loc = bpt->loc; loc; loc = loc->next)
3727 if (loc->pspace->aspace == aspace && loc->inserted)
3728 {
3729 CORE_ADDR l, h;
3730
3731 /* Check for intersection. */
3732 l = max (loc->address, addr);
3733 h = min (loc->address + loc->length, addr + len);
3734 if (l < h)
3735 return 1;
3736 }
3737 }
3738 return 0;
3739}
3740
075f6582
DJ
3741/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3742 PC is valid for process/thread PTID. */
c906108c
SS
3743
3744int
6c95b8df
PA
3745breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3746 ptid_t ptid)
c906108c 3747{
35df4500 3748 struct bp_location *bl, **blp_tmp;
4a306c9a 3749 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 3750 int thread = -1;
4a306c9a 3751 int task = 0;
a6f1cd96 3752
35df4500 3753 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3754 {
35df4500
TJB
3755 if (bl->loc_type != bp_loc_software_breakpoint
3756 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3757 continue;
3758
35df4500
TJB
3759 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3760 if (!breakpoint_enabled (bl->owner)
3761 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
3762 continue;
3763
f1310107 3764 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
3765 continue;
3766
35df4500 3767 if (bl->owner->thread != -1)
075f6582 3768 {
a6f1cd96
JB
3769 /* This is a thread-specific breakpoint. Check that ptid
3770 matches that thread. If thread hasn't been computed yet,
3771 it is now time to do so. */
3772 if (thread == -1)
3773 thread = pid_to_thread_id (ptid);
35df4500 3774 if (bl->owner->thread != thread)
a6f1cd96 3775 continue;
075f6582 3776 }
a6f1cd96 3777
35df4500 3778 if (bl->owner->task != 0)
4a306c9a
JB
3779 {
3780 /* This is a task-specific breakpoint. Check that ptid
3781 matches that task. If task hasn't been computed yet,
3782 it is now time to do so. */
3783 if (task == 0)
3784 task = ada_get_task_number (ptid);
35df4500 3785 if (bl->owner->task != task)
4a306c9a
JB
3786 continue;
3787 }
3788
a6f1cd96 3789 if (overlay_debugging
35df4500
TJB
3790 && section_is_overlay (bl->section)
3791 && !section_is_mapped (bl->section))
a6f1cd96
JB
3792 continue; /* unmapped overlay -- can't be a match */
3793
3794 return 1;
c5aa993b 3795 }
c906108c
SS
3796
3797 return 0;
3798}
c906108c 3799\f
c5aa993b 3800
c906108c
SS
3801/* bpstat stuff. External routines' interfaces are documented
3802 in breakpoint.h. */
3803
3804int
c326b90e 3805is_catchpoint (struct breakpoint *ep)
c906108c 3806{
533be4dd 3807 return (ep->type == bp_catchpoint);
c906108c
SS
3808}
3809
f431efe5
PA
3810/* Frees any storage that is part of a bpstat. Does not walk the
3811 'next' chain. */
3812
3813static void
198757a8
VP
3814bpstat_free (bpstat bs)
3815{
3816 if (bs->old_val != NULL)
3817 value_free (bs->old_val);
9add0f1b 3818 decref_counted_command_line (&bs->commands);
f431efe5 3819 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3820 xfree (bs);
3821}
3822
c906108c
SS
3823/* Clear a bpstat so that it says we are not at any breakpoint.
3824 Also free any storage that is part of a bpstat. */
3825
3826void
fba45db2 3827bpstat_clear (bpstat *bsp)
c906108c
SS
3828{
3829 bpstat p;
3830 bpstat q;
3831
3832 if (bsp == 0)
3833 return;
3834 p = *bsp;
3835 while (p != NULL)
3836 {
3837 q = p->next;
198757a8 3838 bpstat_free (p);
c906108c
SS
3839 p = q;
3840 }
3841 *bsp = NULL;
3842}
3843
3844/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3845 is part of the bpstat is copied as well. */
3846
3847bpstat
fba45db2 3848bpstat_copy (bpstat bs)
c906108c
SS
3849{
3850 bpstat p = NULL;
3851 bpstat tmp;
3852 bpstat retval = NULL;
3853
3854 if (bs == NULL)
3855 return bs;
3856
3857 for (; bs != NULL; bs = bs->next)
3858 {
3859 tmp = (bpstat) xmalloc (sizeof (*tmp));
3860 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3861 incref_counted_command_line (tmp->commands);
f431efe5 3862 incref_bp_location (tmp->bp_location_at);
31cc81e9 3863 if (bs->old_val != NULL)
3c3185ac
JK
3864 {
3865 tmp->old_val = value_copy (bs->old_val);
3866 release_value (tmp->old_val);
3867 }
31cc81e9 3868
c906108c
SS
3869 if (p == NULL)
3870 /* This is the first thing in the chain. */
3871 retval = tmp;
3872 else
3873 p->next = tmp;
3874 p = tmp;
3875 }
3876 p->next = NULL;
3877 return retval;
3878}
3879
4a64f543 3880/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3881
3882bpstat
fba45db2 3883bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3884{
c5aa993b
JM
3885 if (bsp == NULL)
3886 return NULL;
c906108c 3887
c5aa993b
JM
3888 for (; bsp != NULL; bsp = bsp->next)
3889 {
f431efe5 3890 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3891 return bsp;
3892 }
c906108c
SS
3893 return NULL;
3894}
3895
4a64f543
MS
3896/* Put in *NUM the breakpoint number of the first breakpoint we are
3897 stopped at. *BSP upon return is a bpstat which points to the
3898 remaining breakpoints stopped at (but which is not guaranteed to be
3899 good for anything but further calls to bpstat_num).
3900
8671a17b
PA
3901 Return 0 if passed a bpstat which does not indicate any breakpoints.
3902 Return -1 if stopped at a breakpoint that has been deleted since
3903 we set it.
3904 Return 1 otherwise. */
c906108c
SS
3905
3906int
8671a17b 3907bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3908{
3909 struct breakpoint *b;
3910
3911 if ((*bsp) == NULL)
3912 return 0; /* No more breakpoint values */
8671a17b 3913
4a64f543
MS
3914 /* We assume we'll never have several bpstats that correspond to a
3915 single breakpoint -- otherwise, this function might return the
3916 same number more than once and this will look ugly. */
f431efe5 3917 b = (*bsp)->breakpoint_at;
8671a17b
PA
3918 *bsp = (*bsp)->next;
3919 if (b == NULL)
3920 return -1; /* breakpoint that's been deleted since */
3921
3922 *num = b->number; /* We have its number */
3923 return 1;
c906108c
SS
3924}
3925
e93ca019 3926/* See breakpoint.h. */
c906108c
SS
3927
3928void
e93ca019 3929bpstat_clear_actions (void)
c906108c 3930{
e93ca019
JK
3931 struct thread_info *tp;
3932 bpstat bs;
3933
3934 if (ptid_equal (inferior_ptid, null_ptid))
3935 return;
3936
3937 tp = find_thread_ptid (inferior_ptid);
3938 if (tp == NULL)
3939 return;
3940
3941 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 3942 {
9add0f1b 3943 decref_counted_command_line (&bs->commands);
abf85f46 3944
c906108c
SS
3945 if (bs->old_val != NULL)
3946 {
3947 value_free (bs->old_val);
3948 bs->old_val = NULL;
3949 }
3950 }
3951}
3952
f3b1572e
PA
3953/* Called when a command is about to proceed the inferior. */
3954
3955static void
3956breakpoint_about_to_proceed (void)
3957{
3958 if (!ptid_equal (inferior_ptid, null_ptid))
3959 {
3960 struct thread_info *tp = inferior_thread ();
3961
3962 /* Allow inferior function calls in breakpoint commands to not
3963 interrupt the command list. When the call finishes
3964 successfully, the inferior will be standing at the same
3965 breakpoint as if nothing happened. */
16c381f0 3966 if (tp->control.in_infcall)
f3b1572e
PA
3967 return;
3968 }
3969
3970 breakpoint_proceeded = 1;
3971}
3972
4a64f543
MS
3973/* Stub for cleaning up our state if we error-out of a breakpoint
3974 command. */
c906108c 3975static void
4efb68b1 3976cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3977{
3978 executing_breakpoint_commands = 0;
3979}
3980
abf85f46
JK
3981/* Return non-zero iff CMD as the first line of a command sequence is `silent'
3982 or its equivalent. */
3983
3984static int
3985command_line_is_silent (struct command_line *cmd)
3986{
3987 return cmd && (strcmp ("silent", cmd->line) == 0
3988 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3989}
3990
4a64f543
MS
3991/* Execute all the commands associated with all the breakpoints at
3992 this location. Any of these commands could cause the process to
3993 proceed beyond this point, etc. We look out for such changes by
3994 checking the global "breakpoint_proceeded" after each command.
c906108c 3995
347bddb7
PA
3996 Returns true if a breakpoint command resumed the inferior. In that
3997 case, it is the caller's responsibility to recall it again with the
3998 bpstat of the current thread. */
3999
4000static int
4001bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4002{
4003 bpstat bs;
4004 struct cleanup *old_chain;
347bddb7 4005 int again = 0;
c906108c
SS
4006
4007 /* Avoid endless recursion if a `source' command is contained
4008 in bs->commands. */
4009 if (executing_breakpoint_commands)
347bddb7 4010 return 0;
c906108c
SS
4011
4012 executing_breakpoint_commands = 1;
4013 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4014
cf6c5ffb
TT
4015 prevent_dont_repeat ();
4016
4a64f543 4017 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4018 bs = *bsp;
4019
4020 breakpoint_proceeded = 0;
4021 for (; bs != NULL; bs = bs->next)
4022 {
9add0f1b 4023 struct counted_command_line *ccmd;
6c50ab1c
JB
4024 struct command_line *cmd;
4025 struct cleanup *this_cmd_tree_chain;
4026
4027 /* Take ownership of the BSP's command tree, if it has one.
4028
4029 The command tree could legitimately contain commands like
4030 'step' and 'next', which call clear_proceed_status, which
4031 frees stop_bpstat's command tree. To make sure this doesn't
4032 free the tree we're executing out from under us, we need to
4033 take ownership of the tree ourselves. Since a given bpstat's
4034 commands are only executed once, we don't need to copy it; we
4035 can clear the pointer in the bpstat, and make sure we free
4036 the tree when we're done. */
9add0f1b
TT
4037 ccmd = bs->commands;
4038 bs->commands = NULL;
abf85f46
JK
4039 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4040 cmd = ccmd ? ccmd->commands : NULL;
4041 if (command_line_is_silent (cmd))
4042 {
4043 /* The action has been already done by bpstat_stop_status. */
4044 cmd = cmd->next;
4045 }
6c50ab1c 4046
c906108c
SS
4047 while (cmd != NULL)
4048 {
4049 execute_control_command (cmd);
4050
4051 if (breakpoint_proceeded)
4052 break;
4053 else
4054 cmd = cmd->next;
4055 }
6c50ab1c
JB
4056
4057 /* We can free this command tree now. */
4058 do_cleanups (this_cmd_tree_chain);
4059
c906108c 4060 if (breakpoint_proceeded)
32c1e744
VP
4061 {
4062 if (target_can_async_p ())
347bddb7
PA
4063 /* If we are in async mode, then the target might be still
4064 running, not stopped at any breakpoint, so nothing for
4065 us to do here -- just return to the event loop. */
4066 ;
32c1e744
VP
4067 else
4068 /* In sync mode, when execute_control_command returns
4069 we're already standing on the next breakpoint.
347bddb7
PA
4070 Breakpoint commands for that stop were not run, since
4071 execute_command does not run breakpoint commands --
4072 only command_line_handler does, but that one is not
4073 involved in execution of breakpoint commands. So, we
4074 can now execute breakpoint commands. It should be
4075 noted that making execute_command do bpstat actions is
4076 not an option -- in this case we'll have recursive
4077 invocation of bpstat for each breakpoint with a
4078 command, and can easily blow up GDB stack. Instead, we
4079 return true, which will trigger the caller to recall us
4080 with the new stop_bpstat. */
4081 again = 1;
4082 break;
32c1e744 4083 }
c906108c 4084 }
c2b8ed2c 4085 do_cleanups (old_chain);
347bddb7
PA
4086 return again;
4087}
4088
4089void
4090bpstat_do_actions (void)
4091{
353d1d73
JK
4092 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4093
347bddb7
PA
4094 /* Do any commands attached to breakpoint we are stopped at. */
4095 while (!ptid_equal (inferior_ptid, null_ptid)
4096 && target_has_execution
4097 && !is_exited (inferior_ptid)
4098 && !is_executing (inferior_ptid))
4099 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4100 and only return when it is stopped at the next breakpoint, we
4101 keep doing breakpoint actions until it returns false to
4102 indicate the inferior was not resumed. */
16c381f0 4103 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4104 break;
353d1d73
JK
4105
4106 discard_cleanups (cleanup_if_error);
c906108c
SS
4107}
4108
fa4727a6
DJ
4109/* Print out the (old or new) value associated with a watchpoint. */
4110
4111static void
4112watchpoint_value_print (struct value *val, struct ui_file *stream)
4113{
4114 if (val == NULL)
4115 fprintf_unfiltered (stream, _("<unreadable>"));
4116 else
79a45b7d
TT
4117 {
4118 struct value_print_options opts;
4119 get_user_print_options (&opts);
4120 value_print (val, stream, &opts);
4121 }
fa4727a6
DJ
4122}
4123
e514a9d6 4124/* Generic routine for printing messages indicating why we
4a64f543 4125 stopped. The behavior of this function depends on the value
e514a9d6
JM
4126 'print_it' in the bpstat structure. Under some circumstances we
4127 may decide not to print anything here and delegate the task to
4a64f543 4128 normal_stop(). */
e514a9d6
JM
4129
4130static enum print_stop_action
4131print_bp_stop_message (bpstat bs)
4132{
4133 switch (bs->print_it)
4134 {
4135 case print_it_noop:
4a64f543 4136 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4137 return PRINT_UNKNOWN;
4138 break;
4139
4140 case print_it_done:
4141 /* We still want to print the frame, but we already printed the
4a64f543 4142 relevant messages. */
e514a9d6
JM
4143 return PRINT_SRC_AND_LOC;
4144 break;
4145
4146 case print_it_normal:
4f8d1dc6 4147 {
f431efe5
PA
4148 struct breakpoint *b = bs->breakpoint_at;
4149
1a6a67de
TJB
4150 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4151 which has since been deleted. */
4152 if (b == NULL)
4153 return PRINT_UNKNOWN;
4154
348d480f
PA
4155 /* Normal case. Call the breakpoint's print_it method. */
4156 return b->ops->print_it (bs);
4f8d1dc6 4157 }
348d480f 4158 break;
3086aeae 4159
e514a9d6 4160 default:
8e65ff28 4161 internal_error (__FILE__, __LINE__,
e2e0b3e5 4162 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4163 break;
c906108c 4164 }
c906108c
SS
4165}
4166
edcc5120
TT
4167/* A helper function that prints a shared library stopped event. */
4168
4169static void
4170print_solib_event (int is_catchpoint)
4171{
4172 int any_deleted
4173 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4174 int any_added
4175 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4176
4177 if (!is_catchpoint)
4178 {
4179 if (any_added || any_deleted)
4180 ui_out_text (current_uiout,
4181 _("Stopped due to shared library event:\n"));
4182 else
4183 ui_out_text (current_uiout,
4184 _("Stopped due to shared library event (no "
4185 "libraries added or removed)\n"));
4186 }
4187
4188 if (ui_out_is_mi_like_p (current_uiout))
4189 ui_out_field_string (current_uiout, "reason",
4190 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4191
4192 if (any_deleted)
4193 {
4194 struct cleanup *cleanup;
4195 char *name;
4196 int ix;
4197
4198 ui_out_text (current_uiout, _(" Inferior unloaded "));
4199 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4200 "removed");
4201 for (ix = 0;
4202 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4203 ix, name);
4204 ++ix)
4205 {
4206 if (ix > 0)
4207 ui_out_text (current_uiout, " ");
4208 ui_out_field_string (current_uiout, "library", name);
4209 ui_out_text (current_uiout, "\n");
4210 }
4211
4212 do_cleanups (cleanup);
4213 }
4214
4215 if (any_added)
4216 {
4217 struct so_list *iter;
4218 int ix;
4219 struct cleanup *cleanup;
4220
4221 ui_out_text (current_uiout, _(" Inferior loaded "));
4222 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4223 "added");
4224 for (ix = 0;
4225 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4226 ix, iter);
4227 ++ix)
4228 {
4229 if (ix > 0)
4230 ui_out_text (current_uiout, " ");
4231 ui_out_field_string (current_uiout, "library", iter->so_name);
4232 ui_out_text (current_uiout, "\n");
4233 }
4234
4235 do_cleanups (cleanup);
4236 }
4237}
4238
e514a9d6
JM
4239/* Print a message indicating what happened. This is called from
4240 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4241 list - a list of the eventpoints that caused this stop. KIND is
4242 the target_waitkind for the stopping event. This
e514a9d6
JM
4243 routine calls the generic print routine for printing a message
4244 about reasons for stopping. This will print (for example) the
4245 "Breakpoint n," part of the output. The return value of this
4246 routine is one of:
c906108c 4247
4a64f543 4248 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4249 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4250 code to print the location. An example is
c5aa993b
JM
4251 "Breakpoint 1, " which should be followed by
4252 the location.
917317f4 4253 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4254 to also print the location part of the message.
4255 An example is the catch/throw messages, which
4a64f543 4256 don't require a location appended to the end.
917317f4 4257 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4258 further info to be printed. */
c906108c 4259
917317f4 4260enum print_stop_action
36dfb11c 4261bpstat_print (bpstat bs, int kind)
c906108c
SS
4262{
4263 int val;
c5aa993b 4264
c906108c 4265 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4266 (Currently all watchpoints go on the bpstat whether hit or not.
4267 That probably could (should) be changed, provided care is taken
c906108c 4268 with respect to bpstat_explains_signal). */
e514a9d6
JM
4269 for (; bs; bs = bs->next)
4270 {
4271 val = print_bp_stop_message (bs);
4272 if (val == PRINT_SRC_ONLY
4273 || val == PRINT_SRC_AND_LOC
4274 || val == PRINT_NOTHING)
4275 return val;
4276 }
c906108c 4277
36dfb11c
TT
4278 /* If we had hit a shared library event breakpoint,
4279 print_bp_stop_message would print out this message. If we hit an
4280 OS-level shared library event, do the same thing. */
4281 if (kind == TARGET_WAITKIND_LOADED)
4282 {
edcc5120 4283 print_solib_event (0);
36dfb11c
TT
4284 return PRINT_NOTHING;
4285 }
4286
e514a9d6 4287 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4288 with and nothing was printed. */
917317f4 4289 return PRINT_UNKNOWN;
c906108c
SS
4290}
4291
4a64f543
MS
4292/* Evaluate the expression EXP and return 1 if value is zero. This is
4293 used inside a catch_errors to evaluate the breakpoint condition.
4294 The argument is a "struct expression *" that has been cast to a
4295 "char *" to make it pass through catch_errors. */
c906108c
SS
4296
4297static int
4efb68b1 4298breakpoint_cond_eval (void *exp)
c906108c 4299{
278cd55f 4300 struct value *mark = value_mark ();
c5aa993b 4301 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4302
c906108c
SS
4303 value_free_to_mark (mark);
4304 return i;
4305}
4306
5760d0ab 4307/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4308
4309static bpstat
5760d0ab 4310bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4311{
4312 bpstat bs;
4313
4314 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4315 bs->next = NULL;
4316 **bs_link_pointer = bs;
4317 *bs_link_pointer = &bs->next;
f431efe5
PA
4318 bs->breakpoint_at = bl->owner;
4319 bs->bp_location_at = bl;
4320 incref_bp_location (bl);
c906108c
SS
4321 /* If the condition is false, etc., don't do the commands. */
4322 bs->commands = NULL;
4323 bs->old_val = NULL;
4324 bs->print_it = print_it_normal;
4325 return bs;
4326}
4327\f
d983da9c
DJ
4328/* The target has stopped with waitstatus WS. Check if any hardware
4329 watchpoints have triggered, according to the target. */
4330
4331int
4332watchpoints_triggered (struct target_waitstatus *ws)
4333{
d92524f1 4334 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4335 CORE_ADDR addr;
4336 struct breakpoint *b;
4337
4338 if (!stopped_by_watchpoint)
4339 {
4340 /* We were not stopped by a watchpoint. Mark all watchpoints
4341 as not triggered. */
4342 ALL_BREAKPOINTS (b)
cc60f2e3 4343 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4344 {
4345 struct watchpoint *w = (struct watchpoint *) b;
4346
4347 w->watchpoint_triggered = watch_triggered_no;
4348 }
d983da9c
DJ
4349
4350 return 0;
4351 }
4352
4353 if (!target_stopped_data_address (&current_target, &addr))
4354 {
4355 /* We were stopped by a watchpoint, but we don't know where.
4356 Mark all watchpoints as unknown. */
4357 ALL_BREAKPOINTS (b)
cc60f2e3 4358 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4359 {
4360 struct watchpoint *w = (struct watchpoint *) b;
4361
4362 w->watchpoint_triggered = watch_triggered_unknown;
4363 }
d983da9c
DJ
4364
4365 return stopped_by_watchpoint;
4366 }
4367
4368 /* The target could report the data address. Mark watchpoints
4369 affected by this data address as triggered, and all others as not
4370 triggered. */
4371
4372 ALL_BREAKPOINTS (b)
cc60f2e3 4373 if (is_hardware_watchpoint (b))
d983da9c 4374 {
3a5c3e22 4375 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4376 struct bp_location *loc;
d983da9c 4377
3a5c3e22 4378 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4379 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4380 {
3a5c3e22 4381 if (is_masked_watchpoint (b))
9c06b0b4 4382 {
3a5c3e22
PA
4383 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4384 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4385
4386 if (newaddr == start)
4387 {
3a5c3e22 4388 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4389 break;
4390 }
4391 }
4392 /* Exact match not required. Within range is sufficient. */
4393 else if (target_watchpoint_addr_within_range (&current_target,
4394 addr, loc->address,
4395 loc->length))
4396 {
3a5c3e22 4397 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4398 break;
4399 }
4400 }
d983da9c
DJ
4401 }
4402
4403 return 1;
4404}
4405
c906108c
SS
4406/* Possible return values for watchpoint_check (this can't be an enum
4407 because of check_errors). */
4408/* The watchpoint has been deleted. */
4409#define WP_DELETED 1
4410/* The value has changed. */
4411#define WP_VALUE_CHANGED 2
4412/* The value has not changed. */
4413#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4414/* Ignore this watchpoint, no matter if the value changed or not. */
4415#define WP_IGNORE 4
c906108c
SS
4416
4417#define BP_TEMPFLAG 1
4418#define BP_HARDWAREFLAG 2
4419
4a64f543
MS
4420/* Evaluate watchpoint condition expression and check if its value
4421 changed.
553e4c11
JB
4422
4423 P should be a pointer to struct bpstat, but is defined as a void *
4424 in order for this function to be usable with catch_errors. */
c906108c
SS
4425
4426static int
4efb68b1 4427watchpoint_check (void *p)
c906108c
SS
4428{
4429 bpstat bs = (bpstat) p;
3a5c3e22 4430 struct watchpoint *b;
c906108c
SS
4431 struct frame_info *fr;
4432 int within_current_scope;
4433
f431efe5 4434 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4435 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4436 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4437
f6bc2008
PA
4438 /* If this is a local watchpoint, we only want to check if the
4439 watchpoint frame is in scope if the current thread is the thread
4440 that was used to create the watchpoint. */
4441 if (!watchpoint_in_thread_scope (b))
60e1c644 4442 return WP_IGNORE;
f6bc2008 4443
c906108c
SS
4444 if (b->exp_valid_block == NULL)
4445 within_current_scope = 1;
4446 else
4447 {
edb3359d
DJ
4448 struct frame_info *frame = get_current_frame ();
4449 struct gdbarch *frame_arch = get_frame_arch (frame);
4450 CORE_ADDR frame_pc = get_frame_pc (frame);
4451
4a64f543
MS
4452 /* in_function_epilogue_p() returns a non-zero value if we're
4453 still in the function but the stack frame has already been
4454 invalidated. Since we can't rely on the values of local
4455 variables after the stack has been destroyed, we are treating
4456 the watchpoint in that state as `not changed' without further
4457 checking. Don't mark watchpoints as changed if the current
4458 frame is in an epilogue - even if they are in some other
4459 frame, our view of the stack is likely to be wrong and
4460 frame_find_by_id could error out. */
a0f49112 4461 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 4462 return WP_IGNORE;
a0f49112 4463
101dcfbe 4464 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4465 within_current_scope = (fr != NULL);
69fbadd5
DJ
4466
4467 /* If we've gotten confused in the unwinder, we might have
4468 returned a frame that can't describe this variable. */
edb3359d
DJ
4469 if (within_current_scope)
4470 {
4471 struct symbol *function;
4472
4473 function = get_frame_function (fr);
4474 if (function == NULL
4475 || !contained_in (b->exp_valid_block,
4476 SYMBOL_BLOCK_VALUE (function)))
4477 within_current_scope = 0;
4478 }
69fbadd5 4479
edb3359d 4480 if (within_current_scope)
c906108c
SS
4481 /* If we end up stopping, the current frame will get selected
4482 in normal_stop. So this call to select_frame won't affect
4483 the user. */
0f7d239c 4484 select_frame (fr);
c906108c 4485 }
c5aa993b 4486
c906108c
SS
4487 if (within_current_scope)
4488 {
4a64f543
MS
4489 /* We use value_{,free_to_}mark because it could be a *long*
4490 time before we return to the command level and call
4491 free_all_values. We can't call free_all_values because we
4492 might be in the middle of evaluating a function call. */
c906108c 4493
0cf6dd15 4494 int pc = 0;
9c06b0b4 4495 struct value *mark;
fa4727a6
DJ
4496 struct value *new_val;
4497
3a5c3e22 4498 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
4499 /* Since we don't know the exact trigger address (from
4500 stopped_data_address), just tell the user we've triggered
4501 a mask watchpoint. */
4502 return WP_VALUE_CHANGED;
4503
4504 mark = value_mark ();
0cf6dd15 4505 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 4506
4a64f543
MS
4507 /* We use value_equal_contents instead of value_equal because
4508 the latter coerces an array to a pointer, thus comparing just
4509 the address of the array instead of its contents. This is
4510 not what we want. */
fa4727a6 4511 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 4512 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 4513 {
fa4727a6
DJ
4514 if (new_val != NULL)
4515 {
4516 release_value (new_val);
4517 value_free_to_mark (mark);
4518 }
c906108c
SS
4519 bs->old_val = b->val;
4520 b->val = new_val;
fa4727a6 4521 b->val_valid = 1;
c906108c
SS
4522 return WP_VALUE_CHANGED;
4523 }
4524 else
4525 {
60e1c644 4526 /* Nothing changed. */
c906108c 4527 value_free_to_mark (mark);
c906108c
SS
4528 return WP_VALUE_NOT_CHANGED;
4529 }
4530 }
4531 else
4532 {
79a45e25
PA
4533 struct ui_out *uiout = current_uiout;
4534
c906108c 4535 /* This seems like the only logical thing to do because
c5aa993b
JM
4536 if we temporarily ignored the watchpoint, then when
4537 we reenter the block in which it is valid it contains
4538 garbage (in the case of a function, it may have two
4539 garbage values, one before and one after the prologue).
4540 So we can't even detect the first assignment to it and
4541 watch after that (since the garbage may or may not equal
4542 the first value assigned). */
348d480f
PA
4543 /* We print all the stop information in
4544 breakpoint_ops->print_it, but in this case, by the time we
4545 call breakpoint_ops->print_it this bp will be deleted
4546 already. So we have no choice but print the information
4547 here. */
9dc5e2a9 4548 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
4549 ui_out_field_string
4550 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 4551 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 4552 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
4553 ui_out_text (uiout,
4554 " deleted because the program has left the block in\n\
8b93c638 4555which its expression is valid.\n");
4ce44c66 4556
cdac0397 4557 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 4558 decref_counted_command_line (&b->base.commands);
d0fb5eae 4559 watchpoint_del_at_next_stop (b);
c906108c
SS
4560
4561 return WP_DELETED;
4562 }
4563}
4564
18a18393 4565/* Return true if it looks like target has stopped due to hitting
348d480f
PA
4566 breakpoint location BL. This function does not check if we should
4567 stop, only if BL explains the stop. */
4568
18a18393 4569static int
6c95b8df 4570bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
4571 struct address_space *aspace, CORE_ADDR bp_addr,
4572 const struct target_waitstatus *ws)
18a18393
VP
4573{
4574 struct breakpoint *b = bl->owner;
4575
348d480f 4576 /* BL is from an existing breakpoint. */
2bdf28a0
JK
4577 gdb_assert (b != NULL);
4578
09ac7c10 4579 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
4580}
4581
3a5c3e22
PA
4582/* Determine if the watched values have actually changed, and we
4583 should stop. If not, set BS->stop to 0. */
4584
18a18393
VP
4585static void
4586bpstat_check_watchpoint (bpstat bs)
4587{
2bdf28a0 4588 const struct bp_location *bl;
3a5c3e22 4589 struct watchpoint *b;
2bdf28a0
JK
4590
4591 /* BS is built for existing struct breakpoint. */
f431efe5 4592 bl = bs->bp_location_at;
2bdf28a0 4593 gdb_assert (bl != NULL);
3a5c3e22 4594 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 4595 gdb_assert (b != NULL);
18a18393 4596
18a18393 4597 {
18a18393
VP
4598 int must_check_value = 0;
4599
3a5c3e22 4600 if (b->base.type == bp_watchpoint)
18a18393
VP
4601 /* For a software watchpoint, we must always check the
4602 watched value. */
4603 must_check_value = 1;
4604 else if (b->watchpoint_triggered == watch_triggered_yes)
4605 /* We have a hardware watchpoint (read, write, or access)
4606 and the target earlier reported an address watched by
4607 this watchpoint. */
4608 must_check_value = 1;
4609 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 4610 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
4611 /* We were stopped by a hardware watchpoint, but the target could
4612 not report the data address. We must check the watchpoint's
4613 value. Access and read watchpoints are out of luck; without
4614 a data address, we can't figure it out. */
4615 must_check_value = 1;
3a5c3e22 4616
18a18393
VP
4617 if (must_check_value)
4618 {
3e43a32a
MS
4619 char *message
4620 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 4621 b->base.number);
18a18393
VP
4622 struct cleanup *cleanups = make_cleanup (xfree, message);
4623 int e = catch_errors (watchpoint_check, bs, message,
4624 RETURN_MASK_ALL);
4625 do_cleanups (cleanups);
4626 switch (e)
4627 {
4628 case WP_DELETED:
4629 /* We've already printed what needs to be printed. */
4630 bs->print_it = print_it_done;
4631 /* Stop. */
4632 break;
60e1c644
PA
4633 case WP_IGNORE:
4634 bs->print_it = print_it_noop;
4635 bs->stop = 0;
4636 break;
18a18393 4637 case WP_VALUE_CHANGED:
3a5c3e22 4638 if (b->base.type == bp_read_watchpoint)
18a18393 4639 {
85d721b8
PA
4640 /* There are two cases to consider here:
4641
4a64f543 4642 1. We're watching the triggered memory for reads.
85d721b8
PA
4643 In that case, trust the target, and always report
4644 the watchpoint hit to the user. Even though
4645 reads don't cause value changes, the value may
4646 have changed since the last time it was read, and
4647 since we're not trapping writes, we will not see
4648 those, and as such we should ignore our notion of
4649 old value.
4650
4a64f543 4651 2. We're watching the triggered memory for both
85d721b8
PA
4652 reads and writes. There are two ways this may
4653 happen:
4654
4a64f543 4655 2.1. This is a target that can't break on data
85d721b8
PA
4656 reads only, but can break on accesses (reads or
4657 writes), such as e.g., x86. We detect this case
4658 at the time we try to insert read watchpoints.
4659
4a64f543 4660 2.2. Otherwise, the target supports read
85d721b8
PA
4661 watchpoints, but, the user set an access or write
4662 watchpoint watching the same memory as this read
4663 watchpoint.
4664
4665 If we're watching memory writes as well as reads,
4666 ignore watchpoint hits when we find that the
4667 value hasn't changed, as reads don't cause
4668 changes. This still gives false positives when
4669 the program writes the same value to memory as
4670 what there was already in memory (we will confuse
4671 it for a read), but it's much better than
4672 nothing. */
4673
4674 int other_write_watchpoint = 0;
4675
4676 if (bl->watchpoint_type == hw_read)
4677 {
4678 struct breakpoint *other_b;
4679
4680 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
4681 if (other_b->type == bp_hardware_watchpoint
4682 || other_b->type == bp_access_watchpoint)
85d721b8 4683 {
3a5c3e22
PA
4684 struct watchpoint *other_w =
4685 (struct watchpoint *) other_b;
4686
4687 if (other_w->watchpoint_triggered
4688 == watch_triggered_yes)
4689 {
4690 other_write_watchpoint = 1;
4691 break;
4692 }
85d721b8
PA
4693 }
4694 }
4695
4696 if (other_write_watchpoint
4697 || bl->watchpoint_type == hw_access)
4698 {
4699 /* We're watching the same memory for writes,
4700 and the value changed since the last time we
4701 updated it, so this trap must be for a write.
4702 Ignore it. */
4703 bs->print_it = print_it_noop;
4704 bs->stop = 0;
4705 }
18a18393
VP
4706 }
4707 break;
4708 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
4709 if (b->base.type == bp_hardware_watchpoint
4710 || b->base.type == bp_watchpoint)
18a18393
VP
4711 {
4712 /* Don't stop: write watchpoints shouldn't fire if
4713 the value hasn't changed. */
4714 bs->print_it = print_it_noop;
4715 bs->stop = 0;
4716 }
4717 /* Stop. */
4718 break;
4719 default:
4720 /* Can't happen. */
4721 case 0:
4722 /* Error from catch_errors. */
3a5c3e22 4723 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 4724 watchpoint_del_at_next_stop (b);
18a18393
VP
4725 /* We've already printed what needs to be printed. */
4726 bs->print_it = print_it_done;
4727 break;
4728 }
4729 }
4730 else /* must_check_value == 0 */
4731 {
4732 /* This is a case where some watchpoint(s) triggered, but
4733 not at the address of this watchpoint, or else no
4734 watchpoint triggered after all. So don't print
4735 anything for this watchpoint. */
4736 bs->print_it = print_it_noop;
4737 bs->stop = 0;
4738 }
4739 }
4740}
4741
4742
4743/* Check conditions (condition proper, frame, thread and ignore count)
4744 of breakpoint referred to by BS. If we should not stop for this
4745 breakpoint, set BS->stop to 0. */
f431efe5 4746
18a18393
VP
4747static void
4748bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4749{
4750 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
4751 const struct bp_location *bl;
4752 struct breakpoint *b;
4753
4754 /* BS is built for existing struct breakpoint. */
f431efe5 4755 bl = bs->bp_location_at;
2bdf28a0 4756 gdb_assert (bl != NULL);
f431efe5 4757 b = bs->breakpoint_at;
2bdf28a0 4758 gdb_assert (b != NULL);
18a18393 4759
b775012e
LM
4760 /* Even if the target evaluated the condition on its end and notified GDB, we
4761 need to do so again since GDB does not know if we stopped due to a
4762 breakpoint or a single step breakpoint. */
4763
18a18393 4764 if (frame_id_p (b->frame_id)
edb3359d 4765 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
4766 bs->stop = 0;
4767 else if (bs->stop)
4768 {
4769 int value_is_zero = 0;
60e1c644
PA
4770 struct expression *cond;
4771
7371cf6d
PM
4772 /* Evaluate Python breakpoints that have a "stop"
4773 method implemented. */
4774 if (b->py_bp_object)
4775 bs->stop = gdbpy_should_stop (b->py_bp_object);
4776
60e1c644 4777 if (is_watchpoint (b))
3a5c3e22
PA
4778 {
4779 struct watchpoint *w = (struct watchpoint *) b;
4780
4781 cond = w->cond_exp;
4782 }
60e1c644
PA
4783 else
4784 cond = bl->cond;
4785
f431efe5 4786 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 4787 {
60e1c644 4788 int within_current_scope = 1;
3a5c3e22 4789 struct watchpoint * w;
60e1c644 4790
c5bc3a77
DJ
4791 /* We use value_mark and value_free_to_mark because it could
4792 be a long time before we return to the command level and
4793 call free_all_values. We can't call free_all_values
4794 because we might be in the middle of evaluating a
4795 function call. */
4796 struct value *mark = value_mark ();
4797
3a5c3e22
PA
4798 if (is_watchpoint (b))
4799 w = (struct watchpoint *) b;
4800 else
4801 w = NULL;
4802
edb3359d
DJ
4803 /* Need to select the frame, with all that implies so that
4804 the conditions will have the right context. Because we
4805 use the frame, we will not see an inlined function's
4806 variables when we arrive at a breakpoint at the start
4807 of the inlined function; the current frame will be the
4808 call site. */
3a5c3e22 4809 if (w == NULL || w->cond_exp_valid_block == NULL)
60e1c644
PA
4810 select_frame (get_current_frame ());
4811 else
4812 {
4813 struct frame_info *frame;
4814
4815 /* For local watchpoint expressions, which particular
4816 instance of a local is being watched matters, so we
4817 keep track of the frame to evaluate the expression
4818 in. To evaluate the condition however, it doesn't
4819 really matter which instantiation of the function
4820 where the condition makes sense triggers the
4821 watchpoint. This allows an expression like "watch
4822 global if q > 10" set in `func', catch writes to
4823 global on all threads that call `func', or catch
4824 writes on all recursive calls of `func' by a single
4825 thread. We simply always evaluate the condition in
4826 the innermost frame that's executing where it makes
4827 sense to evaluate the condition. It seems
4828 intuitive. */
3a5c3e22 4829 frame = block_innermost_frame (w->cond_exp_valid_block);
60e1c644
PA
4830 if (frame != NULL)
4831 select_frame (frame);
4832 else
4833 within_current_scope = 0;
4834 }
4835 if (within_current_scope)
4836 value_is_zero
4837 = catch_errors (breakpoint_cond_eval, cond,
4838 "Error in testing breakpoint condition:\n",
4839 RETURN_MASK_ALL);
4840 else
4841 {
4842 warning (_("Watchpoint condition cannot be tested "
4843 "in the current scope"));
4844 /* If we failed to set the right context for this
4845 watchpoint, unconditionally report it. */
4846 value_is_zero = 0;
4847 }
4a64f543 4848 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4849 value_free_to_mark (mark);
18a18393 4850 }
60e1c644
PA
4851
4852 if (cond && value_is_zero)
18a18393
VP
4853 {
4854 bs->stop = 0;
4855 }
4856 else if (b->thread != -1 && b->thread != thread_id)
4857 {
4858 bs->stop = 0;
4859 }
4860 else if (b->ignore_count > 0)
4861 {
4862 b->ignore_count--;
4863 annotate_ignore_count_change ();
4864 bs->stop = 0;
4a64f543 4865 /* Increase the hit count even though we don't stop. */
18a18393 4866 ++(b->hit_count);
8d3788bd 4867 observer_notify_breakpoint_modified (b);
18a18393
VP
4868 }
4869 }
4870}
4871
4872
9709f61c 4873/* Get a bpstat associated with having just stopped at address
d983da9c 4874 BP_ADDR in thread PTID.
c906108c 4875
d983da9c 4876 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4877 don't understand this stop. Result is a chain of bpstat's such
4878 that:
c906108c 4879
c5aa993b 4880 if we don't understand the stop, the result is a null pointer.
c906108c 4881
c5aa993b 4882 if we understand why we stopped, the result is not null.
c906108c 4883
c5aa993b
JM
4884 Each element of the chain refers to a particular breakpoint or
4885 watchpoint at which we have stopped. (We may have stopped for
4886 several reasons concurrently.)
c906108c 4887
c5aa993b
JM
4888 Each element of the chain has valid next, breakpoint_at,
4889 commands, FIXME??? fields. */
c906108c
SS
4890
4891bpstat
6c95b8df 4892bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
4893 CORE_ADDR bp_addr, ptid_t ptid,
4894 const struct target_waitstatus *ws)
c906108c 4895{
0d381245 4896 struct breakpoint *b = NULL;
afe38095 4897 struct bp_location *bl;
20874c92 4898 struct bp_location *loc;
5760d0ab
JK
4899 /* First item of allocated bpstat's. */
4900 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4901 /* Pointer to the last thing in the chain currently. */
5760d0ab 4902 bpstat bs;
20874c92 4903 int ix;
429374b8 4904 int need_remove_insert;
f431efe5 4905 int removed_any;
c906108c 4906
f431efe5
PA
4907 /* First, build the bpstat chain with locations that explain a
4908 target stop, while being careful to not set the target running,
4909 as that may invalidate locations (in particular watchpoint
4910 locations are recreated). Resuming will happen here with
4911 breakpoint conditions or watchpoint expressions that include
4912 inferior function calls. */
c5aa993b 4913
429374b8
JK
4914 ALL_BREAKPOINTS (b)
4915 {
4916 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4917 continue;
a5606eee 4918
429374b8
JK
4919 for (bl = b->loc; bl != NULL; bl = bl->next)
4920 {
4a64f543
MS
4921 /* For hardware watchpoints, we look only at the first
4922 location. The watchpoint_check function will work on the
4923 entire expression, not the individual locations. For
4924 read watchpoints, the watchpoints_triggered function has
4925 checked all locations already. */
429374b8
JK
4926 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4927 break;
18a18393 4928
429374b8
JK
4929 if (bl->shlib_disabled)
4930 continue;
c5aa993b 4931
09ac7c10 4932 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 4933 continue;
c5aa993b 4934
4a64f543
MS
4935 /* Come here if it's a watchpoint, or if the break address
4936 matches. */
c5aa993b 4937
4a64f543
MS
4938 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4939 explain stop. */
c5aa993b 4940
f431efe5
PA
4941 /* Assume we stop. Should we find a watchpoint that is not
4942 actually triggered, or if the condition of the breakpoint
4943 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4944 bs->stop = 1;
4945 bs->print = 1;
d983da9c 4946
f431efe5
PA
4947 /* If this is a scope breakpoint, mark the associated
4948 watchpoint as triggered so that we will handle the
4949 out-of-scope event. We'll get to the watchpoint next
4950 iteration. */
d0fb5eae 4951 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
4952 {
4953 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4954
4955 w->watchpoint_triggered = watch_triggered_yes;
4956 }
f431efe5
PA
4957 }
4958 }
4959
4960 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4961 {
f1310107 4962 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4963 {
5760d0ab 4964 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4965 /* For hits of moribund locations, we should just proceed. */
4966 bs->stop = 0;
4967 bs->print = 0;
4968 bs->print_it = print_it_noop;
4969 }
4970 }
4971
edcc5120
TT
4972 /* A bit of special processing for shlib breakpoints. We need to
4973 process solib loading here, so that the lists of loaded and
4974 unloaded libraries are correct before we handle "catch load" and
4975 "catch unload". */
4976 for (bs = bs_head; bs != NULL; bs = bs->next)
4977 {
5d268276 4978 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
4979 {
4980 handle_solib_event ();
4981 break;
4982 }
4983 }
4984
f431efe5
PA
4985 /* Now go through the locations that caused the target to stop, and
4986 check whether we're interested in reporting this stop to higher
4987 layers, or whether we should resume the target transparently. */
4988
4989 removed_any = 0;
4990
5760d0ab 4991 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4992 {
4993 if (!bs->stop)
4994 continue;
4995
f431efe5 4996 b = bs->breakpoint_at;
348d480f
PA
4997 b->ops->check_status (bs);
4998 if (bs->stop)
28010a5d 4999 {
348d480f 5000 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5001
429374b8
JK
5002 if (bs->stop)
5003 {
5004 ++(b->hit_count);
8d3788bd 5005 observer_notify_breakpoint_modified (b);
c906108c 5006
4a64f543 5007 /* We will stop here. */
429374b8
JK
5008 if (b->disposition == disp_disable)
5009 {
816338b5
SS
5010 --(b->enable_count);
5011 if (b->enable_count <= 0
5012 && b->enable_state != bp_permanent)
429374b8 5013 b->enable_state = bp_disabled;
f431efe5 5014 removed_any = 1;
429374b8
JK
5015 }
5016 if (b->silent)
5017 bs->print = 0;
5018 bs->commands = b->commands;
9add0f1b 5019 incref_counted_command_line (bs->commands);
abf85f46
JK
5020 if (command_line_is_silent (bs->commands
5021 ? bs->commands->commands : NULL))
5022 bs->print = 0;
429374b8
JK
5023 }
5024
348d480f 5025 }
a9b3a50f
PA
5026
5027 /* Print nothing for this entry if we don't stop or don't
5028 print. */
5029 if (!bs->stop || !bs->print)
5030 bs->print_it = print_it_noop;
429374b8 5031 }
876fa593 5032
d983da9c
DJ
5033 /* If we aren't stopping, the value of some hardware watchpoint may
5034 not have changed, but the intermediate memory locations we are
5035 watching may have. Don't bother if we're stopping; this will get
5036 done later. */
d832cb68 5037 need_remove_insert = 0;
5760d0ab
JK
5038 if (! bpstat_causes_stop (bs_head))
5039 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5040 if (!bs->stop
f431efe5
PA
5041 && bs->breakpoint_at
5042 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5043 {
3a5c3e22
PA
5044 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5045
5046 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5047 need_remove_insert = 1;
d983da9c
DJ
5048 }
5049
d832cb68 5050 if (need_remove_insert)
2d134ed3 5051 update_global_location_list (1);
f431efe5
PA
5052 else if (removed_any)
5053 update_global_location_list (0);
d832cb68 5054
5760d0ab 5055 return bs_head;
c906108c 5056}
628fe4e4
JK
5057
5058static void
5059handle_jit_event (void)
5060{
5061 struct frame_info *frame;
5062 struct gdbarch *gdbarch;
5063
5064 /* Switch terminal for any messages produced by
5065 breakpoint_re_set. */
5066 target_terminal_ours_for_output ();
5067
5068 frame = get_current_frame ();
5069 gdbarch = get_frame_arch (frame);
5070
5071 jit_event_handler (gdbarch);
5072
5073 target_terminal_inferior ();
5074}
5075
edcc5120
TT
5076/* Handle an solib event by calling solib_add. */
5077
5078void
5079handle_solib_event (void)
5080{
5081 clear_program_space_solib_cache (current_inferior ()->pspace);
5082
5083 /* Check for any newly added shared libraries if we're supposed to
5084 be adding them automatically. Switch terminal for any messages
5085 produced by breakpoint_re_set. */
5086 target_terminal_ours_for_output ();
5087#ifdef SOLIB_ADD
5088 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
5089#else
5090 solib_add (NULL, 0, &current_target, auto_solib_add);
5091#endif
5092 target_terminal_inferior ();
5093}
5094
628fe4e4
JK
5095/* Prepare WHAT final decision for infrun. */
5096
5097/* Decide what infrun needs to do with this bpstat. */
5098
c906108c 5099struct bpstat_what
0e30163f 5100bpstat_what (bpstat bs_head)
c906108c 5101{
c906108c 5102 struct bpstat_what retval;
628fe4e4 5103 int jit_event = 0;
0e30163f 5104 bpstat bs;
c906108c 5105
628fe4e4 5106 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5107 retval.call_dummy = STOP_NONE;
186c406b 5108 retval.is_longjmp = 0;
628fe4e4 5109
0e30163f 5110 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5111 {
628fe4e4
JK
5112 /* Extract this BS's action. After processing each BS, we check
5113 if its action overrides all we've seem so far. */
5114 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5115 enum bptype bptype;
5116
c906108c 5117 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5118 {
5119 /* I suspect this can happen if it was a momentary
5120 breakpoint which has since been deleted. */
5121 bptype = bp_none;
5122 }
20874c92 5123 else
f431efe5 5124 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5125
5126 switch (bptype)
c906108c
SS
5127 {
5128 case bp_none:
628fe4e4 5129 break;
c906108c
SS
5130 case bp_breakpoint:
5131 case bp_hardware_breakpoint:
5132 case bp_until:
5133 case bp_finish:
a9b3a50f 5134 case bp_shlib_event:
c906108c
SS
5135 if (bs->stop)
5136 {
5137 if (bs->print)
628fe4e4 5138 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5139 else
628fe4e4 5140 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5141 }
5142 else
628fe4e4 5143 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5144 break;
5145 case bp_watchpoint:
5146 case bp_hardware_watchpoint:
5147 case bp_read_watchpoint:
5148 case bp_access_watchpoint:
5149 if (bs->stop)
5150 {
5151 if (bs->print)
628fe4e4 5152 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5153 else
628fe4e4 5154 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5155 }
5156 else
628fe4e4
JK
5157 {
5158 /* There was a watchpoint, but we're not stopping.
5159 This requires no further action. */
5160 }
c906108c
SS
5161 break;
5162 case bp_longjmp:
e2e4d78b 5163 case bp_longjmp_call_dummy:
186c406b 5164 case bp_exception:
628fe4e4 5165 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5166 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5167 break;
5168 case bp_longjmp_resume:
186c406b 5169 case bp_exception_resume:
628fe4e4 5170 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5171 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5172 break;
5173 case bp_step_resume:
5174 if (bs->stop)
628fe4e4
JK
5175 this_action = BPSTAT_WHAT_STEP_RESUME;
5176 else
c906108c 5177 {
628fe4e4
JK
5178 /* It is for the wrong frame. */
5179 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5180 }
c906108c 5181 break;
2c03e5be
PA
5182 case bp_hp_step_resume:
5183 if (bs->stop)
5184 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5185 else
5186 {
5187 /* It is for the wrong frame. */
5188 this_action = BPSTAT_WHAT_SINGLE;
5189 }
5190 break;
c906108c 5191 case bp_watchpoint_scope:
c4093a6a 5192 case bp_thread_event:
1900040c 5193 case bp_overlay_event:
0fd8e87f 5194 case bp_longjmp_master:
aa7d318d 5195 case bp_std_terminate_master:
186c406b 5196 case bp_exception_master:
628fe4e4 5197 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5198 break;
ce78b96d 5199 case bp_catchpoint:
c5aa993b
JM
5200 if (bs->stop)
5201 {
5202 if (bs->print)
628fe4e4 5203 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5204 else
628fe4e4 5205 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5206 }
5207 else
628fe4e4
JK
5208 {
5209 /* There was a catchpoint, but we're not stopping.
5210 This requires no further action. */
5211 }
5212 break;
628fe4e4
JK
5213 case bp_jit_event:
5214 jit_event = 1;
5215 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5216 break;
c906108c 5217 case bp_call_dummy:
53a5351d
JM
5218 /* Make sure the action is stop (silent or noisy),
5219 so infrun.c pops the dummy frame. */
aa7d318d 5220 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5221 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5222 break;
5223 case bp_std_terminate:
5224 /* Make sure the action is stop (silent or noisy),
5225 so infrun.c pops the dummy frame. */
aa7d318d 5226 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5227 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5228 break;
1042e4c0 5229 case bp_tracepoint:
7a697b8d 5230 case bp_fast_tracepoint:
0fb4aa4b 5231 case bp_static_tracepoint:
1042e4c0
SS
5232 /* Tracepoint hits should not be reported back to GDB, and
5233 if one got through somehow, it should have been filtered
5234 out already. */
5235 internal_error (__FILE__, __LINE__,
7a697b8d 5236 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5237 break;
5238 case bp_gnu_ifunc_resolver:
5239 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5240 this_action = BPSTAT_WHAT_SINGLE;
5241 break;
5242 case bp_gnu_ifunc_resolver_return:
5243 /* The breakpoint will be removed, execution will restart from the
5244 PC of the former breakpoint. */
5245 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5246 break;
e7e0cddf
SS
5247
5248 case bp_dprintf:
5249 this_action = BPSTAT_WHAT_STOP_SILENT;
5250 break;
5251
628fe4e4
JK
5252 default:
5253 internal_error (__FILE__, __LINE__,
5254 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5255 }
628fe4e4
JK
5256
5257 retval.main_action = max (retval.main_action, this_action);
c906108c 5258 }
628fe4e4 5259
0e30163f
JK
5260 /* These operations may affect the bs->breakpoint_at state so they are
5261 delayed after MAIN_ACTION is decided above. */
5262
628fe4e4
JK
5263 if (jit_event)
5264 {
5265 if (debug_infrun)
5266 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5267
5268 handle_jit_event ();
5269 }
5270
0e30163f
JK
5271 for (bs = bs_head; bs != NULL; bs = bs->next)
5272 {
5273 struct breakpoint *b = bs->breakpoint_at;
5274
5275 if (b == NULL)
5276 continue;
5277 switch (b->type)
5278 {
5279 case bp_gnu_ifunc_resolver:
5280 gnu_ifunc_resolver_stop (b);
5281 break;
5282 case bp_gnu_ifunc_resolver_return:
5283 gnu_ifunc_resolver_return_stop (b);
5284 break;
5285 }
5286 }
5287
c906108c
SS
5288 return retval;
5289}
5290
5291/* Nonzero if we should step constantly (e.g. watchpoints on machines
5292 without hardware support). This isn't related to a specific bpstat,
5293 just to things like whether watchpoints are set. */
5294
c5aa993b 5295int
fba45db2 5296bpstat_should_step (void)
c906108c
SS
5297{
5298 struct breakpoint *b;
cc59ec59 5299
c906108c 5300 ALL_BREAKPOINTS (b)
717a8278 5301 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5302 return 1;
c906108c
SS
5303 return 0;
5304}
5305
67822962
PA
5306int
5307bpstat_causes_stop (bpstat bs)
5308{
5309 for (; bs != NULL; bs = bs->next)
5310 if (bs->stop)
5311 return 1;
5312
5313 return 0;
5314}
5315
c906108c 5316\f
c5aa993b 5317
170b53b2
UW
5318/* Compute a string of spaces suitable to indent the next line
5319 so it starts at the position corresponding to the table column
5320 named COL_NAME in the currently active table of UIOUT. */
5321
5322static char *
5323wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5324{
5325 static char wrap_indent[80];
5326 int i, total_width, width, align;
5327 char *text;
5328
5329 total_width = 0;
5330 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5331 {
5332 if (strcmp (text, col_name) == 0)
5333 {
5334 gdb_assert (total_width < sizeof wrap_indent);
5335 memset (wrap_indent, ' ', total_width);
5336 wrap_indent[total_width] = 0;
5337
5338 return wrap_indent;
5339 }
5340
5341 total_width += width + 1;
5342 }
5343
5344 return NULL;
5345}
5346
b775012e
LM
5347/* Determine if the locations of this breakpoint will have their conditions
5348 evaluated by the target, host or a mix of both. Returns the following:
5349
5350 "host": Host evals condition.
5351 "host or target": Host or Target evals condition.
5352 "target": Target evals condition.
5353*/
5354
5355static const char *
5356bp_condition_evaluator (struct breakpoint *b)
5357{
5358 struct bp_location *bl;
5359 char host_evals = 0;
5360 char target_evals = 0;
5361
5362 if (!b)
5363 return NULL;
5364
5365 if (!is_breakpoint (b))
5366 return NULL;
5367
5368 if (gdb_evaluates_breakpoint_condition_p ()
5369 || !target_supports_evaluation_of_breakpoint_conditions ())
5370 return condition_evaluation_host;
5371
5372 for (bl = b->loc; bl; bl = bl->next)
5373 {
5374 if (bl->cond_bytecode)
5375 target_evals++;
5376 else
5377 host_evals++;
5378 }
5379
5380 if (host_evals && target_evals)
5381 return condition_evaluation_both;
5382 else if (target_evals)
5383 return condition_evaluation_target;
5384 else
5385 return condition_evaluation_host;
5386}
5387
5388/* Determine the breakpoint location's condition evaluator. This is
5389 similar to bp_condition_evaluator, but for locations. */
5390
5391static const char *
5392bp_location_condition_evaluator (struct bp_location *bl)
5393{
5394 if (bl && !is_breakpoint (bl->owner))
5395 return NULL;
5396
5397 if (gdb_evaluates_breakpoint_condition_p ()
5398 || !target_supports_evaluation_of_breakpoint_conditions ())
5399 return condition_evaluation_host;
5400
5401 if (bl && bl->cond_bytecode)
5402 return condition_evaluation_target;
5403 else
5404 return condition_evaluation_host;
5405}
5406
859825b8
JK
5407/* Print the LOC location out of the list of B->LOC locations. */
5408
170b53b2
UW
5409static void
5410print_breakpoint_location (struct breakpoint *b,
5411 struct bp_location *loc)
0d381245 5412{
79a45e25 5413 struct ui_out *uiout = current_uiout;
6c95b8df
PA
5414 struct cleanup *old_chain = save_current_program_space ();
5415
859825b8
JK
5416 if (loc != NULL && loc->shlib_disabled)
5417 loc = NULL;
5418
6c95b8df
PA
5419 if (loc != NULL)
5420 set_current_program_space (loc->pspace);
5421
56435ebe
TT
5422 if (b->display_canonical)
5423 ui_out_field_string (uiout, "what", b->addr_string);
f8eba3c6 5424 else if (loc && loc->source_file)
0d381245
VP
5425 {
5426 struct symbol *sym
5427 = find_pc_sect_function (loc->address, loc->section);
5428 if (sym)
5429 {
5430 ui_out_text (uiout, "in ");
5431 ui_out_field_string (uiout, "func",
5432 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
5433 ui_out_text (uiout, " ");
5434 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5435 ui_out_text (uiout, "at ");
0d381245 5436 }
f8eba3c6 5437 ui_out_field_string (uiout, "file", loc->source_file);
0d381245
VP
5438 ui_out_text (uiout, ":");
5439
5440 if (ui_out_is_mi_like_p (uiout))
5441 {
5442 struct symtab_and_line sal = find_pc_line (loc->address, 0);
5443 char *fullname = symtab_to_fullname (sal.symtab);
5444
5445 if (fullname)
5446 ui_out_field_string (uiout, "fullname", fullname);
5447 }
5448
f8eba3c6 5449 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 5450 }
859825b8 5451 else if (loc)
0d381245 5452 {
f99d8bf4
PA
5453 struct ui_file *stb = mem_fileopen ();
5454 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 5455
f99d8bf4 5456 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 5457 demangle, "");
0d381245 5458 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
5459
5460 do_cleanups (stb_chain);
0d381245 5461 }
859825b8
JK
5462 else
5463 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 5464
b775012e
LM
5465 if (loc && is_breakpoint (b)
5466 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5467 && bp_condition_evaluator (b) == condition_evaluation_both)
5468 {
5469 ui_out_text (uiout, " (");
5470 ui_out_field_string (uiout, "evaluated-by",
5471 bp_location_condition_evaluator (loc));
5472 ui_out_text (uiout, ")");
5473 }
5474
6c95b8df 5475 do_cleanups (old_chain);
0d381245
VP
5476}
5477
269b11a2
PA
5478static const char *
5479bptype_string (enum bptype type)
c906108c 5480{
c4093a6a
JM
5481 struct ep_type_description
5482 {
5483 enum bptype type;
5484 char *description;
5485 };
5486 static struct ep_type_description bptypes[] =
c906108c 5487 {
c5aa993b
JM
5488 {bp_none, "?deleted?"},
5489 {bp_breakpoint, "breakpoint"},
c906108c 5490 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
5491 {bp_until, "until"},
5492 {bp_finish, "finish"},
5493 {bp_watchpoint, "watchpoint"},
c906108c 5494 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5495 {bp_read_watchpoint, "read watchpoint"},
5496 {bp_access_watchpoint, "acc watchpoint"},
5497 {bp_longjmp, "longjmp"},
5498 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5499 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5500 {bp_exception, "exception"},
5501 {bp_exception_resume, "exception resume"},
c5aa993b 5502 {bp_step_resume, "step resume"},
2c03e5be 5503 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5504 {bp_watchpoint_scope, "watchpoint scope"},
5505 {bp_call_dummy, "call dummy"},
aa7d318d 5506 {bp_std_terminate, "std::terminate"},
c5aa993b 5507 {bp_shlib_event, "shlib events"},
c4093a6a 5508 {bp_thread_event, "thread events"},
1900040c 5509 {bp_overlay_event, "overlay events"},
0fd8e87f 5510 {bp_longjmp_master, "longjmp master"},
aa7d318d 5511 {bp_std_terminate_master, "std::terminate master"},
186c406b 5512 {bp_exception_master, "exception master"},
ce78b96d 5513 {bp_catchpoint, "catchpoint"},
1042e4c0 5514 {bp_tracepoint, "tracepoint"},
7a697b8d 5515 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 5516 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 5517 {bp_dprintf, "dprintf"},
4efc6507 5518 {bp_jit_event, "jit events"},
0e30163f
JK
5519 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5520 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 5521 };
269b11a2
PA
5522
5523 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5524 || ((int) type != bptypes[(int) type].type))
5525 internal_error (__FILE__, __LINE__,
5526 _("bptypes table does not describe type #%d."),
5527 (int) type);
5528
5529 return bptypes[(int) type].description;
5530}
5531
5532/* Print B to gdb_stdout. */
5533
5534static void
5535print_one_breakpoint_location (struct breakpoint *b,
5536 struct bp_location *loc,
5537 int loc_number,
5538 struct bp_location **last_loc,
269b11a2
PA
5539 int allflag)
5540{
5541 struct command_line *l;
c2c6d25f 5542 static char bpenables[] = "nynny";
c906108c 5543
79a45e25 5544 struct ui_out *uiout = current_uiout;
0d381245
VP
5545 int header_of_multiple = 0;
5546 int part_of_multiple = (loc != NULL);
79a45b7d
TT
5547 struct value_print_options opts;
5548
5549 get_user_print_options (&opts);
0d381245
VP
5550
5551 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
5552 /* See comment in print_one_breakpoint concerning treatment of
5553 breakpoints with single disabled location. */
0d381245
VP
5554 if (loc == NULL
5555 && (b->loc != NULL
5556 && (b->loc->next != NULL || !b->loc->enabled)))
5557 header_of_multiple = 1;
5558 if (loc == NULL)
5559 loc = b->loc;
5560
c4093a6a
JM
5561 annotate_record ();
5562
5563 /* 1 */
5564 annotate_field (0);
0d381245
VP
5565 if (part_of_multiple)
5566 {
5567 char *formatted;
0c6773c1 5568 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
5569 ui_out_field_string (uiout, "number", formatted);
5570 xfree (formatted);
5571 }
5572 else
5573 {
5574 ui_out_field_int (uiout, "number", b->number);
5575 }
c4093a6a
JM
5576
5577 /* 2 */
5578 annotate_field (1);
0d381245
VP
5579 if (part_of_multiple)
5580 ui_out_field_skip (uiout, "type");
269b11a2
PA
5581 else
5582 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
5583
5584 /* 3 */
5585 annotate_field (2);
0d381245
VP
5586 if (part_of_multiple)
5587 ui_out_field_skip (uiout, "disp");
5588 else
2cec12e5 5589 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 5590
c4093a6a
JM
5591
5592 /* 4 */
5593 annotate_field (3);
0d381245 5594 if (part_of_multiple)
54e52265 5595 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 5596 else
4a64f543
MS
5597 ui_out_field_fmt (uiout, "enabled", "%c",
5598 bpenables[(int) b->enable_state]);
54e52265 5599 ui_out_spaces (uiout, 2);
0d381245 5600
c4093a6a
JM
5601
5602 /* 5 and 6 */
3086aeae 5603 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 5604 {
4a64f543
MS
5605 /* Although the print_one can possibly print all locations,
5606 calling it here is not likely to get any nice result. So,
5607 make sure there's just one location. */
0d381245 5608 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 5609 b->ops->print_one (b, last_loc);
0d381245 5610 }
3086aeae
DJ
5611 else
5612 switch (b->type)
5613 {
5614 case bp_none:
5615 internal_error (__FILE__, __LINE__,
e2e0b3e5 5616 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 5617 break;
c906108c 5618
3086aeae
DJ
5619 case bp_watchpoint:
5620 case bp_hardware_watchpoint:
5621 case bp_read_watchpoint:
5622 case bp_access_watchpoint:
3a5c3e22
PA
5623 {
5624 struct watchpoint *w = (struct watchpoint *) b;
5625
5626 /* Field 4, the address, is omitted (which makes the columns
5627 not line up too nicely with the headers, but the effect
5628 is relatively readable). */
5629 if (opts.addressprint)
5630 ui_out_field_skip (uiout, "addr");
5631 annotate_field (5);
5632 ui_out_field_string (uiout, "what", w->exp_string);
5633 }
3086aeae
DJ
5634 break;
5635
3086aeae
DJ
5636 case bp_breakpoint:
5637 case bp_hardware_breakpoint:
5638 case bp_until:
5639 case bp_finish:
5640 case bp_longjmp:
5641 case bp_longjmp_resume:
e2e4d78b 5642 case bp_longjmp_call_dummy:
186c406b
TT
5643 case bp_exception:
5644 case bp_exception_resume:
3086aeae 5645 case bp_step_resume:
2c03e5be 5646 case bp_hp_step_resume:
3086aeae
DJ
5647 case bp_watchpoint_scope:
5648 case bp_call_dummy:
aa7d318d 5649 case bp_std_terminate:
3086aeae
DJ
5650 case bp_shlib_event:
5651 case bp_thread_event:
5652 case bp_overlay_event:
0fd8e87f 5653 case bp_longjmp_master:
aa7d318d 5654 case bp_std_terminate_master:
186c406b 5655 case bp_exception_master:
1042e4c0 5656 case bp_tracepoint:
7a697b8d 5657 case bp_fast_tracepoint:
0fb4aa4b 5658 case bp_static_tracepoint:
e7e0cddf 5659 case bp_dprintf:
4efc6507 5660 case bp_jit_event:
0e30163f
JK
5661 case bp_gnu_ifunc_resolver:
5662 case bp_gnu_ifunc_resolver_return:
79a45b7d 5663 if (opts.addressprint)
3086aeae
DJ
5664 {
5665 annotate_field (4);
54e52265 5666 if (header_of_multiple)
0d381245 5667 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 5668 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 5669 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 5670 else
5af949e3
UW
5671 ui_out_field_core_addr (uiout, "addr",
5672 loc->gdbarch, loc->address);
3086aeae
DJ
5673 }
5674 annotate_field (5);
0d381245 5675 if (!header_of_multiple)
170b53b2 5676 print_breakpoint_location (b, loc);
0d381245 5677 if (b->loc)
a6d9a66e 5678 *last_loc = b->loc;
3086aeae
DJ
5679 break;
5680 }
c906108c 5681
6c95b8df
PA
5682
5683 /* For backward compatibility, don't display inferiors unless there
5684 are several. */
5685 if (loc != NULL
5686 && !header_of_multiple
5687 && (allflag
5688 || (!gdbarch_has_global_breakpoints (target_gdbarch)
5689 && (number_of_program_spaces () > 1
5690 || number_of_inferiors () > 1)
4a64f543
MS
5691 /* LOC is for existing B, it cannot be in
5692 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
5693 && loc->owner->type != bp_catchpoint)))
5694 {
5695 struct inferior *inf;
5696 int first = 1;
5697
5698 for (inf = inferior_list; inf != NULL; inf = inf->next)
5699 {
5700 if (inf->pspace == loc->pspace)
5701 {
5702 if (first)
5703 {
5704 first = 0;
5705 ui_out_text (uiout, " inf ");
5706 }
5707 else
5708 ui_out_text (uiout, ", ");
5709 ui_out_text (uiout, plongest (inf->num));
5710 }
5711 }
5712 }
5713
4a306c9a 5714 if (!part_of_multiple)
c4093a6a 5715 {
4a306c9a
JB
5716 if (b->thread != -1)
5717 {
5718 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 5719 "stop only in" line a little further down. */
4a306c9a
JB
5720 ui_out_text (uiout, " thread ");
5721 ui_out_field_int (uiout, "thread", b->thread);
5722 }
5723 else if (b->task != 0)
5724 {
5725 ui_out_text (uiout, " task ");
5726 ui_out_field_int (uiout, "task", b->task);
5727 }
c4093a6a 5728 }
f1310107 5729
8b93c638 5730 ui_out_text (uiout, "\n");
f1310107 5731
348d480f 5732 if (!part_of_multiple)
f1310107
TJB
5733 b->ops->print_one_detail (b, uiout);
5734
0d381245 5735 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
5736 {
5737 annotate_field (6);
8b93c638 5738 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 5739 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 5740 the frame ID. */
5af949e3
UW
5741 ui_out_field_core_addr (uiout, "frame",
5742 b->gdbarch, b->frame_id.stack_addr);
8b93c638 5743 ui_out_text (uiout, "\n");
c4093a6a
JM
5744 }
5745
28010a5d 5746 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
5747 {
5748 annotate_field (7);
d77f58be 5749 if (is_tracepoint (b))
1042e4c0
SS
5750 ui_out_text (uiout, "\ttrace only if ");
5751 else
5752 ui_out_text (uiout, "\tstop only if ");
0101ce28 5753 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
5754
5755 /* Print whether the target is doing the breakpoint's condition
5756 evaluation. If GDB is doing the evaluation, don't print anything. */
5757 if (is_breakpoint (b)
5758 && breakpoint_condition_evaluation_mode ()
5759 == condition_evaluation_target)
5760 {
5761 ui_out_text (uiout, " (");
5762 ui_out_field_string (uiout, "evaluated-by",
5763 bp_condition_evaluator (b));
5764 ui_out_text (uiout, " evals)");
5765 }
0101ce28
JJ
5766 ui_out_text (uiout, "\n");
5767 }
5768
0d381245 5769 if (!part_of_multiple && b->thread != -1)
c4093a6a 5770 {
4a64f543 5771 /* FIXME should make an annotation for this. */
8b93c638
JM
5772 ui_out_text (uiout, "\tstop only in thread ");
5773 ui_out_field_int (uiout, "thread", b->thread);
5774 ui_out_text (uiout, "\n");
c4093a6a
JM
5775 }
5776
63c715c6 5777 if (!part_of_multiple && b->hit_count)
c4093a6a 5778 {
4a64f543 5779 /* FIXME should make an annotation for this. */
c326b90e 5780 if (is_catchpoint (b))
8b93c638 5781 ui_out_text (uiout, "\tcatchpoint");
f196051f
SS
5782 else if (is_tracepoint (b))
5783 ui_out_text (uiout, "\ttracepoint");
8b93c638
JM
5784 else
5785 ui_out_text (uiout, "\tbreakpoint");
5786 ui_out_text (uiout, " already hit ");
5787 ui_out_field_int (uiout, "times", b->hit_count);
5788 if (b->hit_count == 1)
5789 ui_out_text (uiout, " time\n");
5790 else
5791 ui_out_text (uiout, " times\n");
c4093a6a
JM
5792 }
5793
4a64f543
MS
5794 /* Output the count also if it is zero, but only if this is mi.
5795 FIXME: Should have a better test for this. */
9dc5e2a9 5796 if (ui_out_is_mi_like_p (uiout))
63c715c6 5797 if (!part_of_multiple && b->hit_count == 0)
fb40c209 5798 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 5799
0d381245 5800 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
5801 {
5802 annotate_field (8);
8b93c638
JM
5803 ui_out_text (uiout, "\tignore next ");
5804 ui_out_field_int (uiout, "ignore", b->ignore_count);
5805 ui_out_text (uiout, " hits\n");
c4093a6a 5806 }
059fb39f 5807
816338b5
SS
5808 /* Note that an enable count of 1 corresponds to "enable once"
5809 behavior, which is reported by the combination of enablement and
5810 disposition, so we don't need to mention it here. */
5811 if (!part_of_multiple && b->enable_count > 1)
5812 {
5813 annotate_field (8);
5814 ui_out_text (uiout, "\tdisable after ");
5815 /* Tweak the wording to clarify that ignore and enable counts
5816 are distinct, and have additive effect. */
5817 if (b->ignore_count)
5818 ui_out_text (uiout, "additional ");
5819 else
5820 ui_out_text (uiout, "next ");
5821 ui_out_field_int (uiout, "enable", b->enable_count);
5822 ui_out_text (uiout, " hits\n");
5823 }
5824
f196051f
SS
5825 if (!part_of_multiple && is_tracepoint (b))
5826 {
5827 struct tracepoint *tp = (struct tracepoint *) b;
5828
5829 if (tp->traceframe_usage)
5830 {
5831 ui_out_text (uiout, "\ttrace buffer usage ");
5832 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
5833 ui_out_text (uiout, " bytes\n");
5834 }
5835 }
5836
9add0f1b 5837 l = b->commands ? b->commands->commands : NULL;
059fb39f 5838 if (!part_of_multiple && l)
c4093a6a 5839 {
3b31d625
EZ
5840 struct cleanup *script_chain;
5841
c4093a6a 5842 annotate_field (9);
3b31d625 5843 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 5844 print_command_lines (uiout, l, 4);
3b31d625 5845 do_cleanups (script_chain);
c4093a6a 5846 }
d24317b4 5847
d9b3f62e 5848 if (is_tracepoint (b))
1042e4c0 5849 {
d9b3f62e
PA
5850 struct tracepoint *t = (struct tracepoint *) b;
5851
5852 if (!part_of_multiple && t->pass_count)
5853 {
5854 annotate_field (10);
5855 ui_out_text (uiout, "\tpass count ");
5856 ui_out_field_int (uiout, "pass", t->pass_count);
5857 ui_out_text (uiout, " \n");
5858 }
1042e4c0
SS
5859 }
5860
d24317b4
VP
5861 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5862 {
3a5c3e22
PA
5863 if (is_watchpoint (b))
5864 {
5865 struct watchpoint *w = (struct watchpoint *) b;
5866
5867 ui_out_field_string (uiout, "original-location", w->exp_string);
5868 }
5869 else if (b->addr_string)
d24317b4 5870 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 5871 }
c4093a6a 5872}
c5aa993b 5873
0d381245
VP
5874static void
5875print_one_breakpoint (struct breakpoint *b,
4a64f543 5876 struct bp_location **last_loc,
6c95b8df 5877 int allflag)
0d381245 5878{
8d3788bd 5879 struct cleanup *bkpt_chain;
79a45e25 5880 struct ui_out *uiout = current_uiout;
8d3788bd
VP
5881
5882 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
5883
12c5a436 5884 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 5885 do_cleanups (bkpt_chain);
0d381245
VP
5886
5887 /* If this breakpoint has custom print function,
5888 it's already printed. Otherwise, print individual
5889 locations, if any. */
5890 if (b->ops == NULL || b->ops->print_one == NULL)
5891 {
4a64f543
MS
5892 /* If breakpoint has a single location that is disabled, we
5893 print it as if it had several locations, since otherwise it's
5894 hard to represent "breakpoint enabled, location disabled"
5895 situation.
5896
5897 Note that while hardware watchpoints have several locations
a3be7890 5898 internally, that's not a property exposed to user. */
0d381245 5899 if (b->loc
a5606eee 5900 && !is_hardware_watchpoint (b)
8d3788bd 5901 && (b->loc->next || !b->loc->enabled))
0d381245
VP
5902 {
5903 struct bp_location *loc;
5904 int n = 1;
8d3788bd 5905
0d381245 5906 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
5907 {
5908 struct cleanup *inner2 =
5909 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5910 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5911 do_cleanups (inner2);
5912 }
0d381245
VP
5913 }
5914 }
5915}
5916
a6d9a66e
UW
5917static int
5918breakpoint_address_bits (struct breakpoint *b)
5919{
5920 int print_address_bits = 0;
5921 struct bp_location *loc;
5922
5923 for (loc = b->loc; loc; loc = loc->next)
5924 {
c7437ca6
PA
5925 int addr_bit;
5926
5927 /* Software watchpoints that aren't watching memory don't have
5928 an address to print. */
5929 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5930 continue;
5931
5932 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5933 if (addr_bit > print_address_bits)
5934 print_address_bits = addr_bit;
5935 }
5936
5937 return print_address_bits;
5938}
0d381245 5939
c4093a6a
JM
5940struct captured_breakpoint_query_args
5941 {
5942 int bnum;
5943 };
c5aa993b 5944
c4093a6a 5945static int
2b65245e 5946do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5947{
5948 struct captured_breakpoint_query_args *args = data;
52f0bd74 5949 struct breakpoint *b;
a6d9a66e 5950 struct bp_location *dummy_loc = NULL;
cc59ec59 5951
c4093a6a
JM
5952 ALL_BREAKPOINTS (b)
5953 {
5954 if (args->bnum == b->number)
c5aa993b 5955 {
12c5a436 5956 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5957 return GDB_RC_OK;
c5aa993b 5958 }
c4093a6a
JM
5959 }
5960 return GDB_RC_NONE;
5961}
c5aa993b 5962
c4093a6a 5963enum gdb_rc
4a64f543
MS
5964gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5965 char **error_message)
c4093a6a
JM
5966{
5967 struct captured_breakpoint_query_args args;
cc59ec59 5968
c4093a6a
JM
5969 args.bnum = bnum;
5970 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5971 an error. */
b0b13bb4
DJ
5972 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5973 error_message, RETURN_MASK_ALL) < 0)
5974 return GDB_RC_FAIL;
5975 else
5976 return GDB_RC_OK;
c4093a6a 5977}
c5aa993b 5978
09d682a4
TT
5979/* Return true if this breakpoint was set by the user, false if it is
5980 internal or momentary. */
5981
5982int
5983user_breakpoint_p (struct breakpoint *b)
5984{
46c6471b 5985 return b->number > 0;
09d682a4
TT
5986}
5987
7f3b0473 5988/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5989 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5990 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5991 FILTER is non-NULL, call it on each breakpoint and only include the
5992 ones for which it returns non-zero. Return the total number of
5993 breakpoints listed. */
c906108c 5994
d77f58be 5995static int
e5a67952 5996breakpoint_1 (char *args, int allflag,
4a64f543 5997 int (*filter) (const struct breakpoint *))
c4093a6a 5998{
52f0bd74 5999 struct breakpoint *b;
a6d9a66e 6000 struct bp_location *last_loc = NULL;
7f3b0473 6001 int nr_printable_breakpoints;
3b31d625 6002 struct cleanup *bkpttbl_chain;
79a45b7d 6003 struct value_print_options opts;
a6d9a66e 6004 int print_address_bits = 0;
269b11a2 6005 int print_type_col_width = 14;
79a45e25 6006 struct ui_out *uiout = current_uiout;
269b11a2 6007
79a45b7d
TT
6008 get_user_print_options (&opts);
6009
4a64f543
MS
6010 /* Compute the number of rows in the table, as well as the size
6011 required for address fields. */
7f3b0473
AC
6012 nr_printable_breakpoints = 0;
6013 ALL_BREAKPOINTS (b)
e5a67952
MS
6014 {
6015 /* If we have a filter, only list the breakpoints it accepts. */
6016 if (filter && !filter (b))
6017 continue;
6018
6019 /* If we have an "args" string, it is a list of breakpoints to
6020 accept. Skip the others. */
6021 if (args != NULL && *args != '\0')
6022 {
6023 if (allflag && parse_and_eval_long (args) != b->number)
6024 continue;
6025 if (!allflag && !number_is_in_list (args, b->number))
6026 continue;
6027 }
269b11a2 6028
e5a67952
MS
6029 if (allflag || user_breakpoint_p (b))
6030 {
6031 int addr_bit, type_len;
a6d9a66e 6032
e5a67952
MS
6033 addr_bit = breakpoint_address_bits (b);
6034 if (addr_bit > print_address_bits)
6035 print_address_bits = addr_bit;
269b11a2 6036
e5a67952
MS
6037 type_len = strlen (bptype_string (b->type));
6038 if (type_len > print_type_col_width)
6039 print_type_col_width = type_len;
6040
6041 nr_printable_breakpoints++;
6042 }
6043 }
7f3b0473 6044
79a45b7d 6045 if (opts.addressprint)
3b31d625 6046 bkpttbl_chain
3e43a32a
MS
6047 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6048 nr_printable_breakpoints,
3b31d625 6049 "BreakpointTable");
8b93c638 6050 else
3b31d625 6051 bkpttbl_chain
3e43a32a
MS
6052 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6053 nr_printable_breakpoints,
3b31d625 6054 "BreakpointTable");
8b93c638 6055
7f3b0473 6056 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6057 annotate_breakpoints_headers ();
6058 if (nr_printable_breakpoints > 0)
6059 annotate_field (0);
4a64f543 6060 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6061 if (nr_printable_breakpoints > 0)
6062 annotate_field (1);
269b11a2 6063 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6064 "type", "Type"); /* 2 */
d7faa9e7
AC
6065 if (nr_printable_breakpoints > 0)
6066 annotate_field (2);
4a64f543 6067 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6068 if (nr_printable_breakpoints > 0)
6069 annotate_field (3);
54e52265 6070 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6071 if (opts.addressprint)
e5a67952
MS
6072 {
6073 if (nr_printable_breakpoints > 0)
6074 annotate_field (4);
6075 if (print_address_bits <= 32)
6076 ui_out_table_header (uiout, 10, ui_left,
6077 "addr", "Address"); /* 5 */
6078 else
6079 ui_out_table_header (uiout, 18, ui_left,
6080 "addr", "Address"); /* 5 */
6081 }
d7faa9e7
AC
6082 if (nr_printable_breakpoints > 0)
6083 annotate_field (5);
6084 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6085 ui_out_table_body (uiout);
6086 if (nr_printable_breakpoints > 0)
6087 annotate_breakpoints_table ();
7f3b0473 6088
c4093a6a 6089 ALL_BREAKPOINTS (b)
e5a67952
MS
6090 {
6091 QUIT;
6092 /* If we have a filter, only list the breakpoints it accepts. */
6093 if (filter && !filter (b))
6094 continue;
6095
6096 /* If we have an "args" string, it is a list of breakpoints to
6097 accept. Skip the others. */
6098
6099 if (args != NULL && *args != '\0')
6100 {
6101 if (allflag) /* maintenance info breakpoint */
6102 {
6103 if (parse_and_eval_long (args) != b->number)
6104 continue;
6105 }
6106 else /* all others */
6107 {
6108 if (!number_is_in_list (args, b->number))
6109 continue;
6110 }
6111 }
6112 /* We only print out user settable breakpoints unless the
6113 allflag is set. */
6114 if (allflag || user_breakpoint_p (b))
12c5a436 6115 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6116 }
6117
3b31d625 6118 do_cleanups (bkpttbl_chain);
698384cd 6119
7f3b0473 6120 if (nr_printable_breakpoints == 0)
c906108c 6121 {
4a64f543
MS
6122 /* If there's a filter, let the caller decide how to report
6123 empty list. */
d77f58be
SS
6124 if (!filter)
6125 {
e5a67952 6126 if (args == NULL || *args == '\0')
d77f58be
SS
6127 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6128 else
4a64f543 6129 ui_out_message (uiout, 0,
e5a67952
MS
6130 "No breakpoint or watchpoint matching '%s'.\n",
6131 args);
d77f58be 6132 }
c906108c
SS
6133 }
6134 else
c4093a6a 6135 {
a6d9a66e
UW
6136 if (last_loc && !server_command)
6137 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6138 }
c906108c 6139
4a64f543 6140 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6141 there have been breakpoints? */
c906108c 6142 annotate_breakpoints_table_end ();
d77f58be
SS
6143
6144 return nr_printable_breakpoints;
c906108c
SS
6145}
6146
ad443146
SS
6147/* Display the value of default-collect in a way that is generally
6148 compatible with the breakpoint list. */
6149
6150static void
6151default_collect_info (void)
6152{
79a45e25
PA
6153 struct ui_out *uiout = current_uiout;
6154
ad443146
SS
6155 /* If it has no value (which is frequently the case), say nothing; a
6156 message like "No default-collect." gets in user's face when it's
6157 not wanted. */
6158 if (!*default_collect)
6159 return;
6160
6161 /* The following phrase lines up nicely with per-tracepoint collect
6162 actions. */
6163 ui_out_text (uiout, "default collect ");
6164 ui_out_field_string (uiout, "default-collect", default_collect);
6165 ui_out_text (uiout, " \n");
6166}
6167
c906108c 6168static void
e5a67952 6169breakpoints_info (char *args, int from_tty)
c906108c 6170{
e5a67952 6171 breakpoint_1 (args, 0, NULL);
ad443146
SS
6172
6173 default_collect_info ();
d77f58be
SS
6174}
6175
6176static void
e5a67952 6177watchpoints_info (char *args, int from_tty)
d77f58be 6178{
e5a67952 6179 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6180 struct ui_out *uiout = current_uiout;
d77f58be
SS
6181
6182 if (num_printed == 0)
6183 {
e5a67952 6184 if (args == NULL || *args == '\0')
d77f58be
SS
6185 ui_out_message (uiout, 0, "No watchpoints.\n");
6186 else
e5a67952 6187 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6188 }
c906108c
SS
6189}
6190
7a292a7a 6191static void
e5a67952 6192maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6193{
e5a67952 6194 breakpoint_1 (args, 1, NULL);
ad443146
SS
6195
6196 default_collect_info ();
c906108c
SS
6197}
6198
0d381245 6199static int
714835d5 6200breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6201 struct program_space *pspace,
714835d5 6202 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6203{
6204 struct bp_location *bl = b->loc;
cc59ec59 6205
0d381245
VP
6206 for (; bl; bl = bl->next)
6207 {
6c95b8df
PA
6208 if (bl->pspace == pspace
6209 && bl->address == pc
0d381245
VP
6210 && (!overlay_debugging || bl->section == section))
6211 return 1;
6212 }
6213 return 0;
6214}
6215
672f9b60 6216/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6217 concerns with logical breakpoints, so we match program spaces, not
6218 address spaces. */
c906108c
SS
6219
6220static void
6c95b8df
PA
6221describe_other_breakpoints (struct gdbarch *gdbarch,
6222 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6223 struct obj_section *section, int thread)
c906108c 6224{
52f0bd74
AC
6225 int others = 0;
6226 struct breakpoint *b;
c906108c
SS
6227
6228 ALL_BREAKPOINTS (b)
672f9b60
KP
6229 others += (user_breakpoint_p (b)
6230 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6231 if (others > 0)
6232 {
a3f17187
AC
6233 if (others == 1)
6234 printf_filtered (_("Note: breakpoint "));
6235 else /* if (others == ???) */
6236 printf_filtered (_("Note: breakpoints "));
c906108c 6237 ALL_BREAKPOINTS (b)
672f9b60 6238 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6239 {
6240 others--;
6241 printf_filtered ("%d", b->number);
6242 if (b->thread == -1 && thread != -1)
6243 printf_filtered (" (all threads)");
6244 else if (b->thread != -1)
6245 printf_filtered (" (thread %d)", b->thread);
6246 printf_filtered ("%s%s ",
059fb39f 6247 ((b->enable_state == bp_disabled
f8eba3c6 6248 || b->enable_state == bp_call_disabled)
0d381245
VP
6249 ? " (disabled)"
6250 : b->enable_state == bp_permanent
6251 ? " (permanent)"
6252 : ""),
6253 (others > 1) ? ","
6254 : ((others == 1) ? " and" : ""));
6255 }
a3f17187 6256 printf_filtered (_("also set at pc "));
5af949e3 6257 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6258 printf_filtered (".\n");
6259 }
6260}
6261\f
c906108c 6262
e4f237da
KB
6263/* Return true iff it is meaningful to use the address member of
6264 BPT. For some breakpoint types, the address member is irrelevant
6265 and it makes no sense to attempt to compare it to other addresses
6266 (or use it for any other purpose either).
6267
4a64f543
MS
6268 More specifically, each of the following breakpoint types will
6269 always have a zero valued address and we don't want to mark
6270 breakpoints of any of these types to be a duplicate of an actual
6271 breakpoint at address zero:
e4f237da
KB
6272
6273 bp_watchpoint
2d134ed3
PA
6274 bp_catchpoint
6275
6276*/
e4f237da
KB
6277
6278static int
6279breakpoint_address_is_meaningful (struct breakpoint *bpt)
6280{
6281 enum bptype type = bpt->type;
6282
2d134ed3
PA
6283 return (type != bp_watchpoint && type != bp_catchpoint);
6284}
6285
6286/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6287 true if LOC1 and LOC2 represent the same watchpoint location. */
6288
6289static int
4a64f543
MS
6290watchpoint_locations_match (struct bp_location *loc1,
6291 struct bp_location *loc2)
2d134ed3 6292{
3a5c3e22
PA
6293 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6294 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6295
6296 /* Both of them must exist. */
6297 gdb_assert (w1 != NULL);
6298 gdb_assert (w2 != NULL);
2bdf28a0 6299
4a64f543
MS
6300 /* If the target can evaluate the condition expression in hardware,
6301 then we we need to insert both watchpoints even if they are at
6302 the same place. Otherwise the watchpoint will only trigger when
6303 the condition of whichever watchpoint was inserted evaluates to
6304 true, not giving a chance for GDB to check the condition of the
6305 other watchpoint. */
3a5c3e22 6306 if ((w1->cond_exp
4a64f543
MS
6307 && target_can_accel_watchpoint_condition (loc1->address,
6308 loc1->length,
0cf6dd15 6309 loc1->watchpoint_type,
3a5c3e22
PA
6310 w1->cond_exp))
6311 || (w2->cond_exp
4a64f543
MS
6312 && target_can_accel_watchpoint_condition (loc2->address,
6313 loc2->length,
0cf6dd15 6314 loc2->watchpoint_type,
3a5c3e22 6315 w2->cond_exp)))
0cf6dd15
TJB
6316 return 0;
6317
85d721b8
PA
6318 /* Note that this checks the owner's type, not the location's. In
6319 case the target does not support read watchpoints, but does
6320 support access watchpoints, we'll have bp_read_watchpoint
6321 watchpoints with hw_access locations. Those should be considered
6322 duplicates of hw_read locations. The hw_read locations will
6323 become hw_access locations later. */
2d134ed3
PA
6324 return (loc1->owner->type == loc2->owner->type
6325 && loc1->pspace->aspace == loc2->pspace->aspace
6326 && loc1->address == loc2->address
6327 && loc1->length == loc2->length);
e4f237da
KB
6328}
6329
6c95b8df
PA
6330/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6331 same breakpoint location. In most targets, this can only be true
6332 if ASPACE1 matches ASPACE2. On targets that have global
6333 breakpoints, the address space doesn't really matter. */
6334
6335static int
6336breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6337 struct address_space *aspace2, CORE_ADDR addr2)
6338{
6339 return ((gdbarch_has_global_breakpoints (target_gdbarch)
6340 || aspace1 == aspace2)
6341 && addr1 == addr2);
6342}
6343
f1310107
TJB
6344/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6345 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6346 matches ASPACE2. On targets that have global breakpoints, the address
6347 space doesn't really matter. */
6348
6349static int
6350breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6351 int len1, struct address_space *aspace2,
6352 CORE_ADDR addr2)
6353{
6354 return ((gdbarch_has_global_breakpoints (target_gdbarch)
6355 || aspace1 == aspace2)
6356 && addr2 >= addr1 && addr2 < addr1 + len1);
6357}
6358
6359/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6360 a ranged breakpoint. In most targets, a match happens only if ASPACE
6361 matches the breakpoint's address space. On targets that have global
6362 breakpoints, the address space doesn't really matter. */
6363
6364static int
6365breakpoint_location_address_match (struct bp_location *bl,
6366 struct address_space *aspace,
6367 CORE_ADDR addr)
6368{
6369 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6370 aspace, addr)
6371 || (bl->length
6372 && breakpoint_address_match_range (bl->pspace->aspace,
6373 bl->address, bl->length,
6374 aspace, addr)));
6375}
6376
1e4d1764
YQ
6377/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6378 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6379 true, otherwise returns false. */
6380
6381static int
6382tracepoint_locations_match (struct bp_location *loc1,
6383 struct bp_location *loc2)
6384{
6385 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6386 /* Since tracepoint locations are never duplicated with others', tracepoint
6387 locations at the same address of different tracepoints are regarded as
6388 different locations. */
6389 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6390 else
6391 return 0;
6392}
6393
2d134ed3
PA
6394/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6395 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6396 represent the same location. */
6397
6398static int
4a64f543
MS
6399breakpoint_locations_match (struct bp_location *loc1,
6400 struct bp_location *loc2)
2d134ed3 6401{
2bdf28a0
JK
6402 int hw_point1, hw_point2;
6403
6404 /* Both of them must not be in moribund_locations. */
6405 gdb_assert (loc1->owner != NULL);
6406 gdb_assert (loc2->owner != NULL);
6407
6408 hw_point1 = is_hardware_watchpoint (loc1->owner);
6409 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6410
6411 if (hw_point1 != hw_point2)
6412 return 0;
6413 else if (hw_point1)
6414 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6415 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6416 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6417 else
f1310107
TJB
6418 /* We compare bp_location.length in order to cover ranged breakpoints. */
6419 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6420 loc2->pspace->aspace, loc2->address)
6421 && loc1->length == loc2->length);
2d134ed3
PA
6422}
6423
76897487
KB
6424static void
6425breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6426 int bnum, int have_bnum)
6427{
f63fbe86
MS
6428 /* The longest string possibly returned by hex_string_custom
6429 is 50 chars. These must be at least that big for safety. */
6430 char astr1[64];
6431 char astr2[64];
76897487 6432
bb599908
PH
6433 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6434 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6435 if (have_bnum)
8a3fe4f8 6436 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6437 bnum, astr1, astr2);
6438 else
8a3fe4f8 6439 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6440}
6441
4a64f543
MS
6442/* Adjust a breakpoint's address to account for architectural
6443 constraints on breakpoint placement. Return the adjusted address.
6444 Note: Very few targets require this kind of adjustment. For most
6445 targets, this function is simply the identity function. */
76897487
KB
6446
6447static CORE_ADDR
a6d9a66e
UW
6448adjust_breakpoint_address (struct gdbarch *gdbarch,
6449 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6450{
a6d9a66e 6451 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
6452 {
6453 /* Very few targets need any kind of breakpoint adjustment. */
6454 return bpaddr;
6455 }
88f7da05
KB
6456 else if (bptype == bp_watchpoint
6457 || bptype == bp_hardware_watchpoint
6458 || bptype == bp_read_watchpoint
6459 || bptype == bp_access_watchpoint
fe798b75 6460 || bptype == bp_catchpoint)
88f7da05
KB
6461 {
6462 /* Watchpoints and the various bp_catch_* eventpoints should not
6463 have their addresses modified. */
6464 return bpaddr;
6465 }
76897487
KB
6466 else
6467 {
6468 CORE_ADDR adjusted_bpaddr;
6469
6470 /* Some targets have architectural constraints on the placement
6471 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 6472 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
6473
6474 /* An adjusted breakpoint address can significantly alter
6475 a user's expectations. Print a warning if an adjustment
6476 is required. */
6477 if (adjusted_bpaddr != bpaddr)
6478 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6479
6480 return adjusted_bpaddr;
6481 }
6482}
6483
28010a5d
PA
6484void
6485init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6486 struct breakpoint *owner)
7cc221ef 6487{
7cc221ef
DJ
6488 memset (loc, 0, sizeof (*loc));
6489
348d480f
PA
6490 gdb_assert (ops != NULL);
6491
28010a5d
PA
6492 loc->ops = ops;
6493 loc->owner = owner;
511a6cd4 6494 loc->cond = NULL;
b775012e 6495 loc->cond_bytecode = NULL;
0d381245
VP
6496 loc->shlib_disabled = 0;
6497 loc->enabled = 1;
e049a4b5 6498
28010a5d 6499 switch (owner->type)
e049a4b5
DJ
6500 {
6501 case bp_breakpoint:
6502 case bp_until:
6503 case bp_finish:
6504 case bp_longjmp:
6505 case bp_longjmp_resume:
e2e4d78b 6506 case bp_longjmp_call_dummy:
186c406b
TT
6507 case bp_exception:
6508 case bp_exception_resume:
e049a4b5 6509 case bp_step_resume:
2c03e5be 6510 case bp_hp_step_resume:
e049a4b5
DJ
6511 case bp_watchpoint_scope:
6512 case bp_call_dummy:
aa7d318d 6513 case bp_std_terminate:
e049a4b5
DJ
6514 case bp_shlib_event:
6515 case bp_thread_event:
6516 case bp_overlay_event:
4efc6507 6517 case bp_jit_event:
0fd8e87f 6518 case bp_longjmp_master:
aa7d318d 6519 case bp_std_terminate_master:
186c406b 6520 case bp_exception_master:
0e30163f
JK
6521 case bp_gnu_ifunc_resolver:
6522 case bp_gnu_ifunc_resolver_return:
e7e0cddf 6523 case bp_dprintf:
e049a4b5 6524 loc->loc_type = bp_loc_software_breakpoint;
b775012e 6525 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
6526 break;
6527 case bp_hardware_breakpoint:
6528 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 6529 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
6530 break;
6531 case bp_hardware_watchpoint:
6532 case bp_read_watchpoint:
6533 case bp_access_watchpoint:
6534 loc->loc_type = bp_loc_hardware_watchpoint;
6535 break;
6536 case bp_watchpoint:
ce78b96d 6537 case bp_catchpoint:
15c3d785
PA
6538 case bp_tracepoint:
6539 case bp_fast_tracepoint:
0fb4aa4b 6540 case bp_static_tracepoint:
e049a4b5
DJ
6541 loc->loc_type = bp_loc_other;
6542 break;
6543 default:
e2e0b3e5 6544 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
6545 }
6546
f431efe5 6547 loc->refc = 1;
28010a5d
PA
6548}
6549
6550/* Allocate a struct bp_location. */
6551
6552static struct bp_location *
6553allocate_bp_location (struct breakpoint *bpt)
6554{
348d480f
PA
6555 return bpt->ops->allocate_location (bpt);
6556}
7cc221ef 6557
f431efe5
PA
6558static void
6559free_bp_location (struct bp_location *loc)
fe3f5fa8 6560{
348d480f 6561 loc->ops->dtor (loc);
fe3f5fa8
VP
6562 xfree (loc);
6563}
6564
f431efe5
PA
6565/* Increment reference count. */
6566
6567static void
6568incref_bp_location (struct bp_location *bl)
6569{
6570 ++bl->refc;
6571}
6572
6573/* Decrement reference count. If the reference count reaches 0,
6574 destroy the bp_location. Sets *BLP to NULL. */
6575
6576static void
6577decref_bp_location (struct bp_location **blp)
6578{
0807b50c
PA
6579 gdb_assert ((*blp)->refc > 0);
6580
f431efe5
PA
6581 if (--(*blp)->refc == 0)
6582 free_bp_location (*blp);
6583 *blp = NULL;
6584}
6585
346774a9 6586/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 6587
346774a9
PA
6588static void
6589add_to_breakpoint_chain (struct breakpoint *b)
c906108c 6590{
346774a9 6591 struct breakpoint *b1;
c906108c 6592
346774a9
PA
6593 /* Add this breakpoint to the end of the chain so that a list of
6594 breakpoints will come out in order of increasing numbers. */
6595
6596 b1 = breakpoint_chain;
6597 if (b1 == 0)
6598 breakpoint_chain = b;
6599 else
6600 {
6601 while (b1->next)
6602 b1 = b1->next;
6603 b1->next = b;
6604 }
6605}
6606
6607/* Initializes breakpoint B with type BPTYPE and no locations yet. */
6608
6609static void
6610init_raw_breakpoint_without_location (struct breakpoint *b,
6611 struct gdbarch *gdbarch,
28010a5d 6612 enum bptype bptype,
c0a91b2b 6613 const struct breakpoint_ops *ops)
346774a9 6614{
c906108c 6615 memset (b, 0, sizeof (*b));
2219d63c 6616
348d480f
PA
6617 gdb_assert (ops != NULL);
6618
28010a5d 6619 b->ops = ops;
4d28f7a8 6620 b->type = bptype;
a6d9a66e 6621 b->gdbarch = gdbarch;
c906108c
SS
6622 b->language = current_language->la_language;
6623 b->input_radix = input_radix;
6624 b->thread = -1;
b5de0fa7 6625 b->enable_state = bp_enabled;
c906108c
SS
6626 b->next = 0;
6627 b->silent = 0;
6628 b->ignore_count = 0;
6629 b->commands = NULL;
818dd999 6630 b->frame_id = null_frame_id;
0d381245 6631 b->condition_not_parsed = 0;
84f4c1fe 6632 b->py_bp_object = NULL;
d0fb5eae 6633 b->related_breakpoint = b;
346774a9
PA
6634}
6635
6636/* Helper to set_raw_breakpoint below. Creates a breakpoint
6637 that has type BPTYPE and has no locations as yet. */
346774a9
PA
6638
6639static struct breakpoint *
6640set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 6641 enum bptype bptype,
c0a91b2b 6642 const struct breakpoint_ops *ops)
346774a9
PA
6643{
6644 struct breakpoint *b = XNEW (struct breakpoint);
6645
348d480f 6646 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 6647 add_to_breakpoint_chain (b);
0d381245
VP
6648 return b;
6649}
6650
0e30163f
JK
6651/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
6652 resolutions should be made as the user specified the location explicitly
6653 enough. */
6654
0d381245 6655static void
0e30163f 6656set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 6657{
2bdf28a0
JK
6658 gdb_assert (loc->owner != NULL);
6659
0d381245 6660 if (loc->owner->type == bp_breakpoint
1042e4c0 6661 || loc->owner->type == bp_hardware_breakpoint
d77f58be 6662 || is_tracepoint (loc->owner))
0d381245 6663 {
0e30163f 6664 int is_gnu_ifunc;
2c02bd72 6665 const char *function_name;
6a3a010b 6666 CORE_ADDR func_addr;
0e30163f 6667
2c02bd72 6668 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 6669 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
6670
6671 if (is_gnu_ifunc && !explicit_loc)
6672 {
6673 struct breakpoint *b = loc->owner;
6674
6675 gdb_assert (loc->pspace == current_program_space);
2c02bd72 6676 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
6677 &loc->requested_address))
6678 {
6679 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
6680 loc->address = adjust_breakpoint_address (loc->gdbarch,
6681 loc->requested_address,
6682 b->type);
6683 }
6684 else if (b->type == bp_breakpoint && b->loc == loc
6685 && loc->next == NULL && b->related_breakpoint == b)
6686 {
6687 /* Create only the whole new breakpoint of this type but do not
6688 mess more complicated breakpoints with multiple locations. */
6689 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
6690 /* Remember the resolver's address for use by the return
6691 breakpoint. */
6692 loc->related_address = func_addr;
0e30163f
JK
6693 }
6694 }
6695
2c02bd72
DE
6696 if (function_name)
6697 loc->function_name = xstrdup (function_name);
0d381245
VP
6698 }
6699}
6700
a6d9a66e 6701/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 6702struct gdbarch *
a6d9a66e
UW
6703get_sal_arch (struct symtab_and_line sal)
6704{
6705 if (sal.section)
6706 return get_objfile_arch (sal.section->objfile);
6707 if (sal.symtab)
6708 return get_objfile_arch (sal.symtab->objfile);
6709
6710 return NULL;
6711}
6712
346774a9
PA
6713/* Low level routine for partially initializing a breakpoint of type
6714 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 6715 file name, and line number are provided by SAL.
0d381245
VP
6716
6717 It is expected that the caller will complete the initialization of
6718 the newly created breakpoint struct as well as output any status
c56053d2 6719 information regarding the creation of a new breakpoint. */
0d381245 6720
346774a9
PA
6721static void
6722init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 6723 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 6724 const struct breakpoint_ops *ops)
0d381245 6725{
28010a5d 6726 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 6727
3742cc8b 6728 add_location_to_breakpoint (b, &sal);
0d381245 6729
6c95b8df
PA
6730 if (bptype != bp_catchpoint)
6731 gdb_assert (sal.pspace != NULL);
6732
f8eba3c6
TT
6733 /* Store the program space that was used to set the breakpoint,
6734 except for ordinary breakpoints, which are independent of the
6735 program space. */
6736 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
6737 b->pspace = sal.pspace;
0d381245 6738
c906108c 6739 breakpoints_changed ();
346774a9 6740}
c906108c 6741
346774a9
PA
6742/* set_raw_breakpoint is a low level routine for allocating and
6743 partially initializing a breakpoint of type BPTYPE. The newly
6744 created breakpoint's address, section, source file name, and line
6745 number are provided by SAL. The newly created and partially
6746 initialized breakpoint is added to the breakpoint chain and
6747 is also returned as the value of this function.
6748
6749 It is expected that the caller will complete the initialization of
6750 the newly created breakpoint struct as well as output any status
6751 information regarding the creation of a new breakpoint. In
6752 particular, set_raw_breakpoint does NOT set the breakpoint
6753 number! Care should be taken to not allow an error to occur
6754 prior to completing the initialization of the breakpoint. If this
6755 should happen, a bogus breakpoint will be left on the chain. */
6756
6757struct breakpoint *
6758set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 6759 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 6760 const struct breakpoint_ops *ops)
346774a9
PA
6761{
6762 struct breakpoint *b = XNEW (struct breakpoint);
6763
348d480f 6764 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 6765 add_to_breakpoint_chain (b);
c906108c
SS
6766 return b;
6767}
6768
c2c6d25f
JM
6769
6770/* Note that the breakpoint object B describes a permanent breakpoint
6771 instruction, hard-wired into the inferior's code. */
6772void
6773make_breakpoint_permanent (struct breakpoint *b)
6774{
0d381245 6775 struct bp_location *bl;
cc59ec59 6776
b5de0fa7 6777 b->enable_state = bp_permanent;
c2c6d25f 6778
4a64f543
MS
6779 /* By definition, permanent breakpoints are already present in the
6780 code. Mark all locations as inserted. For now,
6781 make_breakpoint_permanent is called in just one place, so it's
6782 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 6783 multiple locations or not, but it's easy to implement. */
0d381245
VP
6784 for (bl = b->loc; bl; bl = bl->next)
6785 bl->inserted = 1;
c2c6d25f
JM
6786}
6787
53a5351d 6788/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
6789 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
6790 initiated the operation. */
c906108c
SS
6791
6792void
186c406b 6793set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 6794{
35df4500 6795 struct breakpoint *b, *b_tmp;
186c406b 6796 int thread = tp->num;
0fd8e87f
UW
6797
6798 /* To avoid having to rescan all objfile symbols at every step,
6799 we maintain a list of continually-inserted but always disabled
6800 longjmp "master" breakpoints. Here, we simply create momentary
6801 clones of those and enable them for the requested thread. */
35df4500 6802 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 6803 if (b->pspace == current_program_space
186c406b
TT
6804 && (b->type == bp_longjmp_master
6805 || b->type == bp_exception_master))
0fd8e87f 6806 {
06edf0c0
PA
6807 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
6808 struct breakpoint *clone;
cc59ec59 6809
e2e4d78b
JK
6810 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
6811 after their removal. */
06edf0c0 6812 clone = momentary_breakpoint_from_master (b, type,
e2e4d78b 6813 &longjmp_breakpoint_ops);
0fd8e87f
UW
6814 clone->thread = thread;
6815 }
186c406b
TT
6816
6817 tp->initiating_frame = frame;
c906108c
SS
6818}
6819
611c83ae 6820/* Delete all longjmp breakpoints from THREAD. */
c906108c 6821void
611c83ae 6822delete_longjmp_breakpoint (int thread)
c906108c 6823{
35df4500 6824 struct breakpoint *b, *b_tmp;
c906108c 6825
35df4500 6826 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 6827 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
6828 {
6829 if (b->thread == thread)
6830 delete_breakpoint (b);
6831 }
c906108c
SS
6832}
6833
f59f708a
PA
6834void
6835delete_longjmp_breakpoint_at_next_stop (int thread)
6836{
6837 struct breakpoint *b, *b_tmp;
6838
6839 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6840 if (b->type == bp_longjmp || b->type == bp_exception)
6841 {
6842 if (b->thread == thread)
6843 b->disposition = disp_del_at_next_stop;
6844 }
6845}
6846
e2e4d78b
JK
6847/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
6848 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
6849 pointer to any of them. Return NULL if this system cannot place longjmp
6850 breakpoints. */
6851
6852struct breakpoint *
6853set_longjmp_breakpoint_for_call_dummy (void)
6854{
6855 struct breakpoint *b, *retval = NULL;
6856
6857 ALL_BREAKPOINTS (b)
6858 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
6859 {
6860 struct breakpoint *new_b;
6861
6862 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
6863 &momentary_breakpoint_ops);
6864 new_b->thread = pid_to_thread_id (inferior_ptid);
6865
6866 /* Link NEW_B into the chain of RETVAL breakpoints. */
6867
6868 gdb_assert (new_b->related_breakpoint == new_b);
6869 if (retval == NULL)
6870 retval = new_b;
6871 new_b->related_breakpoint = retval;
6872 while (retval->related_breakpoint != new_b->related_breakpoint)
6873 retval = retval->related_breakpoint;
6874 retval->related_breakpoint = new_b;
6875 }
6876
6877 return retval;
6878}
6879
6880/* Verify all existing dummy frames and their associated breakpoints for
6881 THREAD. Remove those which can no longer be found in the current frame
6882 stack.
6883
6884 You should call this function only at places where it is safe to currently
6885 unwind the whole stack. Failed stack unwind would discard live dummy
6886 frames. */
6887
6888void
6889check_longjmp_breakpoint_for_call_dummy (int thread)
6890{
6891 struct breakpoint *b, *b_tmp;
6892
6893 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6894 if (b->type == bp_longjmp_call_dummy && b->thread == thread)
6895 {
6896 struct breakpoint *dummy_b = b->related_breakpoint;
6897
6898 while (dummy_b != b && dummy_b->type != bp_call_dummy)
6899 dummy_b = dummy_b->related_breakpoint;
6900 if (dummy_b->type != bp_call_dummy
6901 || frame_find_by_id (dummy_b->frame_id) != NULL)
6902 continue;
6903
6904 dummy_frame_discard (dummy_b->frame_id);
6905
6906 while (b->related_breakpoint != b)
6907 {
6908 if (b_tmp == b->related_breakpoint)
6909 b_tmp = b->related_breakpoint->next;
6910 delete_breakpoint (b->related_breakpoint);
6911 }
6912 delete_breakpoint (b);
6913 }
6914}
6915
1900040c
MS
6916void
6917enable_overlay_breakpoints (void)
6918{
52f0bd74 6919 struct breakpoint *b;
1900040c
MS
6920
6921 ALL_BREAKPOINTS (b)
6922 if (b->type == bp_overlay_event)
6923 {
6924 b->enable_state = bp_enabled;
b60e7edf 6925 update_global_location_list (1);
c02f5703 6926 overlay_events_enabled = 1;
1900040c
MS
6927 }
6928}
6929
6930void
6931disable_overlay_breakpoints (void)
6932{
52f0bd74 6933 struct breakpoint *b;
1900040c
MS
6934
6935 ALL_BREAKPOINTS (b)
6936 if (b->type == bp_overlay_event)
6937 {
6938 b->enable_state = bp_disabled;
b60e7edf 6939 update_global_location_list (0);
c02f5703 6940 overlay_events_enabled = 0;
1900040c
MS
6941 }
6942}
6943
aa7d318d
TT
6944/* Set an active std::terminate breakpoint for each std::terminate
6945 master breakpoint. */
6946void
6947set_std_terminate_breakpoint (void)
6948{
35df4500 6949 struct breakpoint *b, *b_tmp;
aa7d318d 6950
35df4500 6951 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6952 if (b->pspace == current_program_space
6953 && b->type == bp_std_terminate_master)
6954 {
06edf0c0
PA
6955 momentary_breakpoint_from_master (b, bp_std_terminate,
6956 &momentary_breakpoint_ops);
aa7d318d
TT
6957 }
6958}
6959
6960/* Delete all the std::terminate breakpoints. */
6961void
6962delete_std_terminate_breakpoint (void)
6963{
35df4500 6964 struct breakpoint *b, *b_tmp;
aa7d318d 6965
35df4500 6966 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6967 if (b->type == bp_std_terminate)
6968 delete_breakpoint (b);
6969}
6970
c4093a6a 6971struct breakpoint *
a6d9a66e 6972create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
6973{
6974 struct breakpoint *b;
c4093a6a 6975
06edf0c0
PA
6976 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
6977 &internal_breakpoint_ops);
6978
b5de0fa7 6979 b->enable_state = bp_enabled;
c4093a6a 6980 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
6981 b->addr_string
6982 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 6983
b60e7edf 6984 update_global_location_list_nothrow (1);
74960c60 6985
c4093a6a
JM
6986 return b;
6987}
6988
6989void
6990remove_thread_event_breakpoints (void)
6991{
35df4500 6992 struct breakpoint *b, *b_tmp;
c4093a6a 6993
35df4500 6994 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6995 if (b->type == bp_thread_event
6996 && b->loc->pspace == current_program_space)
c4093a6a
JM
6997 delete_breakpoint (b);
6998}
6999
0101ce28
JJ
7000struct lang_and_radix
7001 {
7002 enum language lang;
7003 int radix;
7004 };
7005
4efc6507
DE
7006/* Create a breakpoint for JIT code registration and unregistration. */
7007
7008struct breakpoint *
7009create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7010{
7011 struct breakpoint *b;
7012
06edf0c0
PA
7013 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7014 &internal_breakpoint_ops);
4efc6507
DE
7015 update_global_location_list_nothrow (1);
7016 return b;
7017}
0101ce28 7018
03673fc7
PP
7019/* Remove JIT code registration and unregistration breakpoint(s). */
7020
7021void
7022remove_jit_event_breakpoints (void)
7023{
7024 struct breakpoint *b, *b_tmp;
7025
7026 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7027 if (b->type == bp_jit_event
7028 && b->loc->pspace == current_program_space)
7029 delete_breakpoint (b);
7030}
7031
cae688ec
JJ
7032void
7033remove_solib_event_breakpoints (void)
7034{
35df4500 7035 struct breakpoint *b, *b_tmp;
cae688ec 7036
35df4500 7037 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7038 if (b->type == bp_shlib_event
7039 && b->loc->pspace == current_program_space)
cae688ec
JJ
7040 delete_breakpoint (b);
7041}
7042
7043struct breakpoint *
a6d9a66e 7044create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
7045{
7046 struct breakpoint *b;
7047
06edf0c0
PA
7048 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7049 &internal_breakpoint_ops);
b60e7edf 7050 update_global_location_list_nothrow (1);
cae688ec
JJ
7051 return b;
7052}
7053
7054/* Disable any breakpoints that are on code in shared libraries. Only
7055 apply to enabled breakpoints, disabled ones can just stay disabled. */
7056
7057void
cb851954 7058disable_breakpoints_in_shlibs (void)
cae688ec 7059{
876fa593 7060 struct bp_location *loc, **locp_tmp;
cae688ec 7061
876fa593 7062 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7063 {
2bdf28a0 7064 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7065 struct breakpoint *b = loc->owner;
2bdf28a0 7066
4a64f543
MS
7067 /* We apply the check to all breakpoints, including disabled for
7068 those with loc->duplicate set. This is so that when breakpoint
7069 becomes enabled, or the duplicate is removed, gdb will try to
7070 insert all breakpoints. If we don't set shlib_disabled here,
7071 we'll try to insert those breakpoints and fail. */
1042e4c0 7072 if (((b->type == bp_breakpoint)
508ccb1f 7073 || (b->type == bp_jit_event)
1042e4c0 7074 || (b->type == bp_hardware_breakpoint)
d77f58be 7075 || (is_tracepoint (b)))
6c95b8df 7076 && loc->pspace == current_program_space
0d381245 7077 && !loc->shlib_disabled
a77053c2 7078#ifdef PC_SOLIB
0d381245 7079 && PC_SOLIB (loc->address)
a77053c2 7080#else
6c95b8df 7081 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
7082#endif
7083 )
0d381245
VP
7084 {
7085 loc->shlib_disabled = 1;
7086 }
cae688ec
JJ
7087 }
7088}
7089
1e4d1764
YQ
7090/* Disable any breakpoints and tracepoints that are in an unloaded shared
7091 library. Only apply to enabled breakpoints, disabled ones can just stay
4a64f543 7092 disabled. */
84acb35a 7093
75149521 7094static void
84acb35a
JJ
7095disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7096{
876fa593 7097 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7098 int disabled_shlib_breaks = 0;
7099
c86cf029
VP
7100 /* SunOS a.out shared libraries are always mapped, so do not
7101 disable breakpoints; they will only be reported as unloaded
7102 through clear_solib when GDB discards its shared library
7103 list. See clear_solib for more information. */
7104 if (exec_bfd != NULL
7105 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7106 return;
7107
876fa593 7108 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7109 {
2bdf28a0 7110 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7111 struct breakpoint *b = loc->owner;
cc59ec59 7112
1e4d1764 7113 if (solib->pspace == loc->pspace
e2dd7057 7114 && !loc->shlib_disabled
1e4d1764
YQ
7115 && (((b->type == bp_breakpoint
7116 || b->type == bp_jit_event
7117 || b->type == bp_hardware_breakpoint)
7118 && (loc->loc_type == bp_loc_hardware_breakpoint
7119 || loc->loc_type == bp_loc_software_breakpoint))
7120 || is_tracepoint (b))
e2dd7057 7121 && solib_contains_address_p (solib, loc->address))
84acb35a 7122 {
e2dd7057
PP
7123 loc->shlib_disabled = 1;
7124 /* At this point, we cannot rely on remove_breakpoint
7125 succeeding so we must mark the breakpoint as not inserted
7126 to prevent future errors occurring in remove_breakpoints. */
7127 loc->inserted = 0;
8d3788bd
VP
7128
7129 /* This may cause duplicate notifications for the same breakpoint. */
7130 observer_notify_breakpoint_modified (b);
7131
e2dd7057
PP
7132 if (!disabled_shlib_breaks)
7133 {
7134 target_terminal_ours_for_output ();
3e43a32a
MS
7135 warning (_("Temporarily disabling breakpoints "
7136 "for unloaded shared library \"%s\""),
e2dd7057 7137 solib->so_name);
84acb35a 7138 }
e2dd7057 7139 disabled_shlib_breaks = 1;
84acb35a
JJ
7140 }
7141 }
84acb35a
JJ
7142}
7143
ce78b96d
JB
7144/* FORK & VFORK catchpoints. */
7145
e29a4733
PA
7146/* An instance of this type is used to represent a fork or vfork
7147 catchpoint. It includes a "struct breakpoint" as a kind of base
7148 class; users downcast to "struct breakpoint *" when needed. A
7149 breakpoint is really of this type iff its ops pointer points to
7150 CATCH_FORK_BREAKPOINT_OPS. */
7151
7152struct fork_catchpoint
7153{
7154 /* The base class. */
7155 struct breakpoint base;
7156
7157 /* Process id of a child process whose forking triggered this
7158 catchpoint. This field is only valid immediately after this
7159 catchpoint has triggered. */
7160 ptid_t forked_inferior_pid;
7161};
7162
4a64f543
MS
7163/* Implement the "insert" breakpoint_ops method for fork
7164 catchpoints. */
ce78b96d 7165
77b06cd7
TJB
7166static int
7167insert_catch_fork (struct bp_location *bl)
ce78b96d 7168{
77b06cd7 7169 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
7170}
7171
4a64f543
MS
7172/* Implement the "remove" breakpoint_ops method for fork
7173 catchpoints. */
ce78b96d
JB
7174
7175static int
77b06cd7 7176remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
7177{
7178 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
7179}
7180
7181/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7182 catchpoints. */
7183
7184static int
f1310107 7185breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7186 struct address_space *aspace, CORE_ADDR bp_addr,
7187 const struct target_waitstatus *ws)
ce78b96d 7188{
e29a4733
PA
7189 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7190
f90263c1
TT
7191 if (ws->kind != TARGET_WAITKIND_FORKED)
7192 return 0;
7193
7194 c->forked_inferior_pid = ws->value.related_pid;
7195 return 1;
ce78b96d
JB
7196}
7197
4a64f543
MS
7198/* Implement the "print_it" breakpoint_ops method for fork
7199 catchpoints. */
ce78b96d
JB
7200
7201static enum print_stop_action
348d480f 7202print_it_catch_fork (bpstat bs)
ce78b96d 7203{
36dfb11c 7204 struct ui_out *uiout = current_uiout;
348d480f
PA
7205 struct breakpoint *b = bs->breakpoint_at;
7206 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7207
ce78b96d 7208 annotate_catchpoint (b->number);
36dfb11c
TT
7209 if (b->disposition == disp_del)
7210 ui_out_text (uiout, "\nTemporary catchpoint ");
7211 else
7212 ui_out_text (uiout, "\nCatchpoint ");
7213 if (ui_out_is_mi_like_p (uiout))
7214 {
7215 ui_out_field_string (uiout, "reason",
7216 async_reason_lookup (EXEC_ASYNC_FORK));
7217 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7218 }
7219 ui_out_field_int (uiout, "bkptno", b->number);
7220 ui_out_text (uiout, " (forked process ");
7221 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7222 ui_out_text (uiout, "), ");
ce78b96d
JB
7223 return PRINT_SRC_AND_LOC;
7224}
7225
4a64f543
MS
7226/* Implement the "print_one" breakpoint_ops method for fork
7227 catchpoints. */
ce78b96d
JB
7228
7229static void
a6d9a66e 7230print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7231{
e29a4733 7232 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7233 struct value_print_options opts;
79a45e25 7234 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7235
7236 get_user_print_options (&opts);
7237
4a64f543
MS
7238 /* Field 4, the address, is omitted (which makes the columns not
7239 line up too nicely with the headers, but the effect is relatively
7240 readable). */
79a45b7d 7241 if (opts.addressprint)
ce78b96d
JB
7242 ui_out_field_skip (uiout, "addr");
7243 annotate_field (5);
7244 ui_out_text (uiout, "fork");
e29a4733 7245 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7246 {
7247 ui_out_text (uiout, ", process ");
7248 ui_out_field_int (uiout, "what",
e29a4733 7249 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7250 ui_out_spaces (uiout, 1);
7251 }
7252}
7253
7254/* Implement the "print_mention" breakpoint_ops method for fork
7255 catchpoints. */
7256
7257static void
7258print_mention_catch_fork (struct breakpoint *b)
7259{
7260 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7261}
7262
6149aea9
PA
7263/* Implement the "print_recreate" breakpoint_ops method for fork
7264 catchpoints. */
7265
7266static void
7267print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7268{
7269 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7270 print_recreate_thread (b, fp);
6149aea9
PA
7271}
7272
ce78b96d
JB
7273/* The breakpoint_ops structure to be used in fork catchpoints. */
7274
2060206e 7275static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7276
4a64f543
MS
7277/* Implement the "insert" breakpoint_ops method for vfork
7278 catchpoints. */
ce78b96d 7279
77b06cd7
TJB
7280static int
7281insert_catch_vfork (struct bp_location *bl)
ce78b96d 7282{
77b06cd7 7283 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
7284}
7285
4a64f543
MS
7286/* Implement the "remove" breakpoint_ops method for vfork
7287 catchpoints. */
ce78b96d
JB
7288
7289static int
77b06cd7 7290remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
7291{
7292 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
7293}
7294
7295/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7296 catchpoints. */
7297
7298static int
f1310107 7299breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
7300 struct address_space *aspace, CORE_ADDR bp_addr,
7301 const struct target_waitstatus *ws)
ce78b96d 7302{
e29a4733
PA
7303 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7304
f90263c1
TT
7305 if (ws->kind != TARGET_WAITKIND_VFORKED)
7306 return 0;
7307
7308 c->forked_inferior_pid = ws->value.related_pid;
7309 return 1;
ce78b96d
JB
7310}
7311
4a64f543
MS
7312/* Implement the "print_it" breakpoint_ops method for vfork
7313 catchpoints. */
ce78b96d
JB
7314
7315static enum print_stop_action
348d480f 7316print_it_catch_vfork (bpstat bs)
ce78b96d 7317{
36dfb11c 7318 struct ui_out *uiout = current_uiout;
348d480f 7319 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7320 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7321
ce78b96d 7322 annotate_catchpoint (b->number);
36dfb11c
TT
7323 if (b->disposition == disp_del)
7324 ui_out_text (uiout, "\nTemporary catchpoint ");
7325 else
7326 ui_out_text (uiout, "\nCatchpoint ");
7327 if (ui_out_is_mi_like_p (uiout))
7328 {
7329 ui_out_field_string (uiout, "reason",
7330 async_reason_lookup (EXEC_ASYNC_VFORK));
7331 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7332 }
7333 ui_out_field_int (uiout, "bkptno", b->number);
7334 ui_out_text (uiout, " (vforked process ");
7335 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7336 ui_out_text (uiout, "), ");
ce78b96d
JB
7337 return PRINT_SRC_AND_LOC;
7338}
7339
4a64f543
MS
7340/* Implement the "print_one" breakpoint_ops method for vfork
7341 catchpoints. */
ce78b96d
JB
7342
7343static void
a6d9a66e 7344print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7345{
e29a4733 7346 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7347 struct value_print_options opts;
79a45e25 7348 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7349
7350 get_user_print_options (&opts);
4a64f543
MS
7351 /* Field 4, the address, is omitted (which makes the columns not
7352 line up too nicely with the headers, but the effect is relatively
7353 readable). */
79a45b7d 7354 if (opts.addressprint)
ce78b96d
JB
7355 ui_out_field_skip (uiout, "addr");
7356 annotate_field (5);
7357 ui_out_text (uiout, "vfork");
e29a4733 7358 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7359 {
7360 ui_out_text (uiout, ", process ");
7361 ui_out_field_int (uiout, "what",
e29a4733 7362 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7363 ui_out_spaces (uiout, 1);
7364 }
7365}
7366
7367/* Implement the "print_mention" breakpoint_ops method for vfork
7368 catchpoints. */
7369
7370static void
7371print_mention_catch_vfork (struct breakpoint *b)
7372{
7373 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7374}
7375
6149aea9
PA
7376/* Implement the "print_recreate" breakpoint_ops method for vfork
7377 catchpoints. */
7378
7379static void
7380print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7381{
7382 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7383 print_recreate_thread (b, fp);
6149aea9
PA
7384}
7385
ce78b96d
JB
7386/* The breakpoint_ops structure to be used in vfork catchpoints. */
7387
2060206e 7388static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7389
edcc5120
TT
7390/* An instance of this type is used to represent an solib catchpoint.
7391 It includes a "struct breakpoint" as a kind of base class; users
7392 downcast to "struct breakpoint *" when needed. A breakpoint is
7393 really of this type iff its ops pointer points to
7394 CATCH_SOLIB_BREAKPOINT_OPS. */
7395
7396struct solib_catchpoint
7397{
7398 /* The base class. */
7399 struct breakpoint base;
7400
7401 /* True for "catch load", false for "catch unload". */
7402 unsigned char is_load;
7403
7404 /* Regular expression to match, if any. COMPILED is only valid when
7405 REGEX is non-NULL. */
7406 char *regex;
7407 regex_t compiled;
7408};
7409
7410static void
7411dtor_catch_solib (struct breakpoint *b)
7412{
7413 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7414
7415 if (self->regex)
7416 regfree (&self->compiled);
7417 xfree (self->regex);
7418
7419 base_breakpoint_ops.dtor (b);
7420}
7421
7422static int
7423insert_catch_solib (struct bp_location *ignore)
7424{
7425 return 0;
7426}
7427
7428static int
7429remove_catch_solib (struct bp_location *ignore)
7430{
7431 return 0;
7432}
7433
7434static int
7435breakpoint_hit_catch_solib (const struct bp_location *bl,
7436 struct address_space *aspace,
7437 CORE_ADDR bp_addr,
7438 const struct target_waitstatus *ws)
7439{
7440 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7441 struct breakpoint *other;
7442
7443 if (ws->kind == TARGET_WAITKIND_LOADED)
7444 return 1;
7445
7446 ALL_BREAKPOINTS (other)
7447 {
7448 struct bp_location *other_bl;
7449
7450 if (other == bl->owner)
7451 continue;
7452
7453 if (other->type != bp_shlib_event)
7454 continue;
7455
7456 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7457 continue;
7458
7459 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7460 {
7461 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7462 return 1;
7463 }
7464 }
7465
7466 return 0;
7467}
7468
7469static void
7470check_status_catch_solib (struct bpstats *bs)
7471{
7472 struct solib_catchpoint *self
7473 = (struct solib_catchpoint *) bs->breakpoint_at;
7474 int ix;
7475
7476 if (self->is_load)
7477 {
7478 struct so_list *iter;
7479
7480 for (ix = 0;
7481 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
7482 ix, iter);
7483 ++ix)
7484 {
7485 if (!self->regex
7486 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
7487 return;
7488 }
7489 }
7490 else
7491 {
7492 char *iter;
7493
7494 for (ix = 0;
7495 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
7496 ix, iter);
7497 ++ix)
7498 {
7499 if (!self->regex
7500 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
7501 return;
7502 }
7503 }
7504
7505 bs->stop = 0;
7506 bs->print_it = print_it_noop;
7507}
7508
7509static enum print_stop_action
7510print_it_catch_solib (bpstat bs)
7511{
7512 struct breakpoint *b = bs->breakpoint_at;
7513 struct ui_out *uiout = current_uiout;
7514
7515 annotate_catchpoint (b->number);
7516 if (b->disposition == disp_del)
7517 ui_out_text (uiout, "\nTemporary catchpoint ");
7518 else
7519 ui_out_text (uiout, "\nCatchpoint ");
7520 ui_out_field_int (uiout, "bkptno", b->number);
7521 ui_out_text (uiout, "\n");
7522 if (ui_out_is_mi_like_p (uiout))
7523 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7524 print_solib_event (1);
7525 return PRINT_SRC_AND_LOC;
7526}
7527
7528static void
7529print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7530{
7531 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7532 struct value_print_options opts;
7533 struct ui_out *uiout = current_uiout;
7534 char *msg;
7535
7536 get_user_print_options (&opts);
7537 /* Field 4, the address, is omitted (which makes the columns not
7538 line up too nicely with the headers, but the effect is relatively
7539 readable). */
7540 if (opts.addressprint)
7541 {
7542 annotate_field (4);
7543 ui_out_field_skip (uiout, "addr");
7544 }
7545
7546 annotate_field (5);
7547 if (self->is_load)
7548 {
7549 if (self->regex)
7550 msg = xstrprintf (_("load of library matching %s"), self->regex);
7551 else
7552 msg = xstrdup (_("load of library"));
7553 }
7554 else
7555 {
7556 if (self->regex)
7557 msg = xstrprintf (_("unload of library matching %s"), self->regex);
7558 else
7559 msg = xstrdup (_("unload of library"));
7560 }
7561 ui_out_field_string (uiout, "what", msg);
7562 xfree (msg);
7563}
7564
7565static void
7566print_mention_catch_solib (struct breakpoint *b)
7567{
7568 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7569
7570 printf_filtered (_("Catchpoint %d (%s)"), b->number,
7571 self->is_load ? "load" : "unload");
7572}
7573
7574static void
7575print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
7576{
7577 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7578
7579 fprintf_unfiltered (fp, "%s %s",
7580 b->disposition == disp_del ? "tcatch" : "catch",
7581 self->is_load ? "load" : "unload");
7582 if (self->regex)
7583 fprintf_unfiltered (fp, " %s", self->regex);
7584 fprintf_unfiltered (fp, "\n");
7585}
7586
7587static struct breakpoint_ops catch_solib_breakpoint_ops;
7588
7589/* A helper function that does all the work for "catch load" and
7590 "catch unload". */
7591
7592static void
7593catch_load_or_unload (char *arg, int from_tty, int is_load,
7594 struct cmd_list_element *command)
7595{
7596 struct solib_catchpoint *c;
7597 struct gdbarch *gdbarch = get_current_arch ();
7598 int tempflag;
edcc5120
TT
7599 struct cleanup *cleanup;
7600
7601 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7602
7603 if (!arg)
7604 arg = "";
7605 arg = skip_spaces (arg);
7606
7607 c = XCNEW (struct solib_catchpoint);
7608 cleanup = make_cleanup (xfree, c);
7609
7610 if (*arg != '\0')
7611 {
7612 int errcode;
7613
7614 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
7615 if (errcode != 0)
7616 {
7617 char *err = get_regcomp_error (errcode, &c->compiled);
7618
7619 make_cleanup (xfree, err);
7620 error (_("Invalid regexp (%s): %s"), err, arg);
7621 }
7622 c->regex = xstrdup (arg);
7623 }
7624
7625 c->is_load = is_load;
7626 init_catchpoint (&c->base, gdbarch, tempflag, NULL,
7627 &catch_solib_breakpoint_ops);
7628
7629 discard_cleanups (cleanup);
7630 install_breakpoint (0, &c->base, 1);
7631}
7632
7633static void
7634catch_load_command_1 (char *arg, int from_tty,
7635 struct cmd_list_element *command)
7636{
7637 catch_load_or_unload (arg, from_tty, 1, command);
7638}
7639
7640static void
7641catch_unload_command_1 (char *arg, int from_tty,
7642 struct cmd_list_element *command)
7643{
7644 catch_load_or_unload (arg, from_tty, 0, command);
7645}
7646
fa3064dd
YQ
7647DEF_VEC_I(int);
7648
be5c67c1
PA
7649/* An instance of this type is used to represent a syscall catchpoint.
7650 It includes a "struct breakpoint" as a kind of base class; users
7651 downcast to "struct breakpoint *" when needed. A breakpoint is
7652 really of this type iff its ops pointer points to
7653 CATCH_SYSCALL_BREAKPOINT_OPS. */
7654
7655struct syscall_catchpoint
7656{
7657 /* The base class. */
7658 struct breakpoint base;
7659
7660 /* Syscall numbers used for the 'catch syscall' feature. If no
7661 syscall has been specified for filtering, its value is NULL.
7662 Otherwise, it holds a list of all syscalls to be caught. The
7663 list elements are allocated with xmalloc. */
7664 VEC(int) *syscalls_to_be_caught;
7665};
7666
7667/* Implement the "dtor" breakpoint_ops method for syscall
7668 catchpoints. */
7669
7670static void
7671dtor_catch_syscall (struct breakpoint *b)
7672{
7673 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7674
7675 VEC_free (int, c->syscalls_to_be_caught);
348d480f 7676
2060206e 7677 base_breakpoint_ops.dtor (b);
be5c67c1
PA
7678}
7679
fa3064dd
YQ
7680static const struct inferior_data *catch_syscall_inferior_data = NULL;
7681
7682struct catch_syscall_inferior_data
7683{
7684 /* We keep a count of the number of times the user has requested a
7685 particular syscall to be tracked, and pass this information to the
7686 target. This lets capable targets implement filtering directly. */
7687
7688 /* Number of times that "any" syscall is requested. */
7689 int any_syscall_count;
7690
7691 /* Count of each system call. */
7692 VEC(int) *syscalls_counts;
7693
7694 /* This counts all syscall catch requests, so we can readily determine
7695 if any catching is necessary. */
7696 int total_syscalls_count;
7697};
7698
7699static struct catch_syscall_inferior_data*
7700get_catch_syscall_inferior_data (struct inferior *inf)
7701{
7702 struct catch_syscall_inferior_data *inf_data;
7703
7704 inf_data = inferior_data (inf, catch_syscall_inferior_data);
7705 if (inf_data == NULL)
7706 {
7707 inf_data = XZALLOC (struct catch_syscall_inferior_data);
7708 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
7709 }
7710
7711 return inf_data;
7712}
7713
7714static void
7715catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
7716{
7717 xfree (arg);
7718}
7719
7720
a96d9b2e
SDJ
7721/* Implement the "insert" breakpoint_ops method for syscall
7722 catchpoints. */
7723
77b06cd7
TJB
7724static int
7725insert_catch_syscall (struct bp_location *bl)
a96d9b2e 7726{
be5c67c1 7727 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 7728 struct inferior *inf = current_inferior ();
fa3064dd
YQ
7729 struct catch_syscall_inferior_data *inf_data
7730 = get_catch_syscall_inferior_data (inf);
a96d9b2e 7731
fa3064dd 7732 ++inf_data->total_syscalls_count;
be5c67c1 7733 if (!c->syscalls_to_be_caught)
fa3064dd 7734 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
7735 else
7736 {
7737 int i, iter;
cc59ec59 7738
a96d9b2e 7739 for (i = 0;
be5c67c1 7740 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7741 i++)
7742 {
7743 int elem;
cc59ec59 7744
fa3064dd 7745 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 7746 {
fa3064dd 7747 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
7748 uintptr_t vec_addr_offset
7749 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 7750 uintptr_t vec_addr;
fa3064dd
YQ
7751 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
7752 vec_addr = ((uintptr_t) VEC_address (int,
7753 inf_data->syscalls_counts)
7754 + vec_addr_offset);
a96d9b2e
SDJ
7755 memset ((void *) vec_addr, 0,
7756 (iter + 1 - old_size) * sizeof (int));
7757 }
fa3064dd
YQ
7758 elem = VEC_index (int, inf_data->syscalls_counts, iter);
7759 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
7760 }
7761 }
7762
77b06cd7 7763 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
fa3064dd
YQ
7764 inf_data->total_syscalls_count != 0,
7765 inf_data->any_syscall_count,
7766 VEC_length (int,
7767 inf_data->syscalls_counts),
7768 VEC_address (int,
7769 inf_data->syscalls_counts));
a96d9b2e
SDJ
7770}
7771
7772/* Implement the "remove" breakpoint_ops method for syscall
7773 catchpoints. */
7774
7775static int
77b06cd7 7776remove_catch_syscall (struct bp_location *bl)
a96d9b2e 7777{
be5c67c1 7778 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 7779 struct inferior *inf = current_inferior ();
fa3064dd
YQ
7780 struct catch_syscall_inferior_data *inf_data
7781 = get_catch_syscall_inferior_data (inf);
a96d9b2e 7782
fa3064dd 7783 --inf_data->total_syscalls_count;
be5c67c1 7784 if (!c->syscalls_to_be_caught)
fa3064dd 7785 --inf_data->any_syscall_count;
a96d9b2e
SDJ
7786 else
7787 {
7788 int i, iter;
cc59ec59 7789
a96d9b2e 7790 for (i = 0;
be5c67c1 7791 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7792 i++)
7793 {
7794 int elem;
fa3064dd 7795 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
7796 /* Shouldn't happen. */
7797 continue;
fa3064dd
YQ
7798 elem = VEC_index (int, inf_data->syscalls_counts, iter);
7799 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
7800 }
7801 }
7802
7803 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
fa3064dd
YQ
7804 inf_data->total_syscalls_count != 0,
7805 inf_data->any_syscall_count,
7806 VEC_length (int,
7807 inf_data->syscalls_counts),
3e43a32a 7808 VEC_address (int,
fa3064dd 7809 inf_data->syscalls_counts));
a96d9b2e
SDJ
7810}
7811
7812/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
7813 catchpoints. */
7814
7815static int
f1310107 7816breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
7817 struct address_space *aspace, CORE_ADDR bp_addr,
7818 const struct target_waitstatus *ws)
a96d9b2e 7819{
4a64f543
MS
7820 /* We must check if we are catching specific syscalls in this
7821 breakpoint. If we are, then we must guarantee that the called
7822 syscall is the same syscall we are catching. */
a96d9b2e 7823 int syscall_number = 0;
be5c67c1
PA
7824 const struct syscall_catchpoint *c
7825 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 7826
f90263c1
TT
7827 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
7828 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
7829 return 0;
7830
f90263c1
TT
7831 syscall_number = ws->value.syscall_number;
7832
a96d9b2e 7833 /* Now, checking if the syscall is the same. */
be5c67c1 7834 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7835 {
7836 int i, iter;
cc59ec59 7837
a96d9b2e 7838 for (i = 0;
be5c67c1 7839 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7840 i++)
7841 if (syscall_number == iter)
7842 break;
7843 /* Not the same. */
7844 if (!iter)
7845 return 0;
7846 }
7847
7848 return 1;
7849}
7850
7851/* Implement the "print_it" breakpoint_ops method for syscall
7852 catchpoints. */
7853
7854static enum print_stop_action
348d480f 7855print_it_catch_syscall (bpstat bs)
a96d9b2e 7856{
36dfb11c 7857 struct ui_out *uiout = current_uiout;
348d480f 7858 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
7859 /* These are needed because we want to know in which state a
7860 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
7861 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
7862 must print "called syscall" or "returned from syscall". */
7863 ptid_t ptid;
7864 struct target_waitstatus last;
7865 struct syscall s;
a96d9b2e
SDJ
7866
7867 get_last_target_status (&ptid, &last);
7868
7869 get_syscall_by_number (last.value.syscall_number, &s);
7870
7871 annotate_catchpoint (b->number);
7872
36dfb11c
TT
7873 if (b->disposition == disp_del)
7874 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 7875 else
36dfb11c
TT
7876 ui_out_text (uiout, "\nCatchpoint ");
7877 if (ui_out_is_mi_like_p (uiout))
7878 {
7879 ui_out_field_string (uiout, "reason",
7880 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
7881 ? EXEC_ASYNC_SYSCALL_ENTRY
7882 : EXEC_ASYNC_SYSCALL_RETURN));
7883 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7884 }
7885 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
7886
7887 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
7888 ui_out_text (uiout, " (call to syscall ");
7889 else
7890 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 7891
36dfb11c
TT
7892 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
7893 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
7894 if (s.name != NULL)
7895 ui_out_field_string (uiout, "syscall-name", s.name);
7896
7897 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
7898
7899 return PRINT_SRC_AND_LOC;
7900}
7901
7902/* Implement the "print_one" breakpoint_ops method for syscall
7903 catchpoints. */
7904
7905static void
7906print_one_catch_syscall (struct breakpoint *b,
f1310107 7907 struct bp_location **last_loc)
a96d9b2e 7908{
be5c67c1 7909 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 7910 struct value_print_options opts;
79a45e25 7911 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
7912
7913 get_user_print_options (&opts);
4a64f543
MS
7914 /* Field 4, the address, is omitted (which makes the columns not
7915 line up too nicely with the headers, but the effect is relatively
7916 readable). */
a96d9b2e
SDJ
7917 if (opts.addressprint)
7918 ui_out_field_skip (uiout, "addr");
7919 annotate_field (5);
7920
be5c67c1
PA
7921 if (c->syscalls_to_be_caught
7922 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
7923 ui_out_text (uiout, "syscalls \"");
7924 else
7925 ui_out_text (uiout, "syscall \"");
7926
be5c67c1 7927 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7928 {
7929 int i, iter;
7930 char *text = xstrprintf ("%s", "");
cc59ec59 7931
a96d9b2e 7932 for (i = 0;
be5c67c1 7933 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7934 i++)
7935 {
7936 char *x = text;
7937 struct syscall s;
7938 get_syscall_by_number (iter, &s);
7939
7940 if (s.name != NULL)
7941 text = xstrprintf ("%s%s, ", text, s.name);
7942 else
7943 text = xstrprintf ("%s%d, ", text, iter);
7944
7945 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 7946 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
7947 on every call. */
7948 xfree (x);
7949 }
7950 /* Remove the last comma. */
7951 text[strlen (text) - 2] = '\0';
7952 ui_out_field_string (uiout, "what", text);
7953 }
7954 else
7955 ui_out_field_string (uiout, "what", "<any syscall>");
7956 ui_out_text (uiout, "\" ");
7957}
7958
7959/* Implement the "print_mention" breakpoint_ops method for syscall
7960 catchpoints. */
7961
7962static void
7963print_mention_catch_syscall (struct breakpoint *b)
7964{
be5c67c1
PA
7965 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7966
7967 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7968 {
7969 int i, iter;
7970
be5c67c1 7971 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
7972 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
7973 else
7974 printf_filtered (_("Catchpoint %d (syscall"), b->number);
7975
7976 for (i = 0;
be5c67c1 7977 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7978 i++)
7979 {
7980 struct syscall s;
7981 get_syscall_by_number (iter, &s);
7982
7983 if (s.name)
7984 printf_filtered (" '%s' [%d]", s.name, s.number);
7985 else
7986 printf_filtered (" %d", s.number);
7987 }
7988 printf_filtered (")");
7989 }
7990 else
7991 printf_filtered (_("Catchpoint %d (any syscall)"),
7992 b->number);
7993}
7994
6149aea9
PA
7995/* Implement the "print_recreate" breakpoint_ops method for syscall
7996 catchpoints. */
7997
7998static void
7999print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8000{
be5c67c1
PA
8001 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8002
6149aea9
PA
8003 fprintf_unfiltered (fp, "catch syscall");
8004
be5c67c1 8005 if (c->syscalls_to_be_caught)
6149aea9
PA
8006 {
8007 int i, iter;
8008
8009 for (i = 0;
be5c67c1 8010 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8011 i++)
8012 {
8013 struct syscall s;
8014
8015 get_syscall_by_number (iter, &s);
8016 if (s.name)
8017 fprintf_unfiltered (fp, " %s", s.name);
8018 else
8019 fprintf_unfiltered (fp, " %d", s.number);
8020 }
8021 }
d9b3f62e 8022 print_recreate_thread (b, fp);
6149aea9
PA
8023}
8024
a96d9b2e
SDJ
8025/* The breakpoint_ops structure to be used in syscall catchpoints. */
8026
2060206e 8027static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8028
8029/* Returns non-zero if 'b' is a syscall catchpoint. */
8030
8031static int
8032syscall_catchpoint_p (struct breakpoint *b)
8033{
8034 return (b->ops == &catch_syscall_breakpoint_ops);
8035}
8036
346774a9
PA
8037/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8038 is non-zero, then make the breakpoint temporary. If COND_STRING is
8039 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8040 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8041
346774a9
PA
8042static void
8043init_catchpoint (struct breakpoint *b,
8044 struct gdbarch *gdbarch, int tempflag,
8045 char *cond_string,
c0a91b2b 8046 const struct breakpoint_ops *ops)
c906108c 8047{
c5aa993b 8048 struct symtab_and_line sal;
346774a9 8049
fe39c653 8050 init_sal (&sal);
6c95b8df 8051 sal.pspace = current_program_space;
c5aa993b 8052
28010a5d 8053 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8054
1b36a34b 8055 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8056 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8057}
8058
28010a5d 8059void
3ea46bff 8060install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8061{
8062 add_to_breakpoint_chain (b);
3a5c3e22
PA
8063 set_breakpoint_number (internal, b);
8064 if (!internal)
8065 mention (b);
c56053d2 8066 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8067
8068 if (update_gll)
8069 update_global_location_list (1);
c56053d2
PA
8070}
8071
9b70b993 8072static void
a6d9a66e
UW
8073create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8074 int tempflag, char *cond_string,
c0a91b2b 8075 const struct breakpoint_ops *ops)
c906108c 8076{
e29a4733 8077 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8078
e29a4733
PA
8079 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8080
8081 c->forked_inferior_pid = null_ptid;
8082
3ea46bff 8083 install_breakpoint (0, &c->base, 1);
c906108c
SS
8084}
8085
fe798b75
JB
8086/* Exec catchpoints. */
8087
b4d90040
PA
8088/* An instance of this type is used to represent an exec catchpoint.
8089 It includes a "struct breakpoint" as a kind of base class; users
8090 downcast to "struct breakpoint *" when needed. A breakpoint is
8091 really of this type iff its ops pointer points to
8092 CATCH_EXEC_BREAKPOINT_OPS. */
8093
8094struct exec_catchpoint
8095{
8096 /* The base class. */
8097 struct breakpoint base;
8098
8099 /* Filename of a program whose exec triggered this catchpoint.
8100 This field is only valid immediately after this catchpoint has
8101 triggered. */
8102 char *exec_pathname;
8103};
8104
8105/* Implement the "dtor" breakpoint_ops method for exec
8106 catchpoints. */
8107
8108static void
8109dtor_catch_exec (struct breakpoint *b)
8110{
8111 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8112
8113 xfree (c->exec_pathname);
348d480f 8114
2060206e 8115 base_breakpoint_ops.dtor (b);
b4d90040
PA
8116}
8117
77b06cd7
TJB
8118static int
8119insert_catch_exec (struct bp_location *bl)
c906108c 8120{
77b06cd7 8121 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 8122}
c906108c 8123
fe798b75 8124static int
77b06cd7 8125remove_catch_exec (struct bp_location *bl)
fe798b75
JB
8126{
8127 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
8128}
c906108c 8129
fe798b75 8130static int
f1310107 8131breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8132 struct address_space *aspace, CORE_ADDR bp_addr,
8133 const struct target_waitstatus *ws)
fe798b75 8134{
b4d90040
PA
8135 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8136
f90263c1
TT
8137 if (ws->kind != TARGET_WAITKIND_EXECD)
8138 return 0;
8139
8140 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8141 return 1;
fe798b75 8142}
c906108c 8143
fe798b75 8144static enum print_stop_action
348d480f 8145print_it_catch_exec (bpstat bs)
fe798b75 8146{
36dfb11c 8147 struct ui_out *uiout = current_uiout;
348d480f 8148 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8149 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8150
fe798b75 8151 annotate_catchpoint (b->number);
36dfb11c
TT
8152 if (b->disposition == disp_del)
8153 ui_out_text (uiout, "\nTemporary catchpoint ");
8154 else
8155 ui_out_text (uiout, "\nCatchpoint ");
8156 if (ui_out_is_mi_like_p (uiout))
8157 {
8158 ui_out_field_string (uiout, "reason",
8159 async_reason_lookup (EXEC_ASYNC_EXEC));
8160 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8161 }
8162 ui_out_field_int (uiout, "bkptno", b->number);
8163 ui_out_text (uiout, " (exec'd ");
8164 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8165 ui_out_text (uiout, "), ");
8166
fe798b75 8167 return PRINT_SRC_AND_LOC;
c906108c
SS
8168}
8169
fe798b75 8170static void
a6d9a66e 8171print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8172{
b4d90040 8173 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8174 struct value_print_options opts;
79a45e25 8175 struct ui_out *uiout = current_uiout;
fe798b75
JB
8176
8177 get_user_print_options (&opts);
8178
8179 /* Field 4, the address, is omitted (which makes the columns
8180 not line up too nicely with the headers, but the effect
8181 is relatively readable). */
8182 if (opts.addressprint)
8183 ui_out_field_skip (uiout, "addr");
8184 annotate_field (5);
8185 ui_out_text (uiout, "exec");
b4d90040 8186 if (c->exec_pathname != NULL)
fe798b75
JB
8187 {
8188 ui_out_text (uiout, ", program \"");
b4d90040 8189 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8190 ui_out_text (uiout, "\" ");
8191 }
8192}
8193
8194static void
8195print_mention_catch_exec (struct breakpoint *b)
8196{
8197 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8198}
8199
6149aea9
PA
8200/* Implement the "print_recreate" breakpoint_ops method for exec
8201 catchpoints. */
8202
8203static void
8204print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8205{
8206 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8207 print_recreate_thread (b, fp);
6149aea9
PA
8208}
8209
2060206e 8210static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8211
a96d9b2e
SDJ
8212static void
8213create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 8214 const struct breakpoint_ops *ops)
a96d9b2e 8215{
be5c67c1 8216 struct syscall_catchpoint *c;
a96d9b2e 8217 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 8218
be5c67c1
PA
8219 c = XNEW (struct syscall_catchpoint);
8220 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8221 c->syscalls_to_be_caught = filter;
a96d9b2e 8222
3ea46bff 8223 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
8224}
8225
c906108c 8226static int
fba45db2 8227hw_breakpoint_used_count (void)
c906108c 8228{
c906108c 8229 int i = 0;
f1310107
TJB
8230 struct breakpoint *b;
8231 struct bp_location *bl;
c906108c
SS
8232
8233 ALL_BREAKPOINTS (b)
c5aa993b 8234 {
d6b74ac4 8235 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8236 for (bl = b->loc; bl; bl = bl->next)
8237 {
8238 /* Special types of hardware breakpoints may use more than
8239 one register. */
348d480f 8240 i += b->ops->resources_needed (bl);
f1310107 8241 }
c5aa993b 8242 }
c906108c
SS
8243
8244 return i;
8245}
8246
a1398e0c
PA
8247/* Returns the resources B would use if it were a hardware
8248 watchpoint. */
8249
c906108c 8250static int
a1398e0c 8251hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8252{
c906108c 8253 int i = 0;
e09342b5 8254 struct bp_location *bl;
c906108c 8255
a1398e0c
PA
8256 if (!breakpoint_enabled (b))
8257 return 0;
8258
8259 for (bl = b->loc; bl; bl = bl->next)
8260 {
8261 /* Special types of hardware watchpoints may use more than
8262 one register. */
8263 i += b->ops->resources_needed (bl);
8264 }
8265
8266 return i;
8267}
8268
8269/* Returns the sum the used resources of all hardware watchpoints of
8270 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8271 the sum of the used resources of all hardware watchpoints of other
8272 types _not_ TYPE. */
8273
8274static int
8275hw_watchpoint_used_count_others (struct breakpoint *except,
8276 enum bptype type, int *other_type_used)
8277{
8278 int i = 0;
8279 struct breakpoint *b;
8280
c906108c
SS
8281 *other_type_used = 0;
8282 ALL_BREAKPOINTS (b)
e09342b5 8283 {
a1398e0c
PA
8284 if (b == except)
8285 continue;
e09342b5
TJB
8286 if (!breakpoint_enabled (b))
8287 continue;
8288
a1398e0c
PA
8289 if (b->type == type)
8290 i += hw_watchpoint_use_count (b);
8291 else if (is_hardware_watchpoint (b))
8292 *other_type_used = 1;
e09342b5
TJB
8293 }
8294
c906108c
SS
8295 return i;
8296}
8297
c906108c 8298void
fba45db2 8299disable_watchpoints_before_interactive_call_start (void)
c906108c 8300{
c5aa993b 8301 struct breakpoint *b;
c906108c
SS
8302
8303 ALL_BREAKPOINTS (b)
c5aa993b 8304 {
cc60f2e3 8305 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8306 {
b5de0fa7 8307 b->enable_state = bp_call_disabled;
b60e7edf 8308 update_global_location_list (0);
c5aa993b
JM
8309 }
8310 }
c906108c
SS
8311}
8312
8313void
fba45db2 8314enable_watchpoints_after_interactive_call_stop (void)
c906108c 8315{
c5aa993b 8316 struct breakpoint *b;
c906108c
SS
8317
8318 ALL_BREAKPOINTS (b)
c5aa993b 8319 {
cc60f2e3 8320 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8321 {
b5de0fa7 8322 b->enable_state = bp_enabled;
b60e7edf 8323 update_global_location_list (1);
c5aa993b
JM
8324 }
8325 }
c906108c
SS
8326}
8327
8bea4e01
UW
8328void
8329disable_breakpoints_before_startup (void)
8330{
6c95b8df 8331 current_program_space->executing_startup = 1;
f8eba3c6 8332 update_global_location_list (0);
8bea4e01
UW
8333}
8334
8335void
8336enable_breakpoints_after_startup (void)
8337{
6c95b8df 8338 current_program_space->executing_startup = 0;
f8eba3c6 8339 breakpoint_re_set ();
8bea4e01
UW
8340}
8341
c906108c
SS
8342
8343/* Set a breakpoint that will evaporate an end of command
8344 at address specified by SAL.
8345 Restrict it to frame FRAME if FRAME is nonzero. */
8346
8347struct breakpoint *
a6d9a66e
UW
8348set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8349 struct frame_id frame_id, enum bptype type)
c906108c 8350{
52f0bd74 8351 struct breakpoint *b;
edb3359d
DJ
8352
8353 /* If FRAME_ID is valid, it should be a real frame, not an inlined
8354 one. */
8355 gdb_assert (!frame_id_inlined_p (frame_id));
8356
06edf0c0 8357 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8358 b->enable_state = bp_enabled;
8359 b->disposition = disp_donttouch;
818dd999 8360 b->frame_id = frame_id;
c906108c 8361
4a64f543
MS
8362 /* If we're debugging a multi-threaded program, then we want
8363 momentary breakpoints to be active in only a single thread of
8364 control. */
39f77062
KB
8365 if (in_thread_list (inferior_ptid))
8366 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 8367
b60e7edf 8368 update_global_location_list_nothrow (1);
74960c60 8369
c906108c
SS
8370 return b;
8371}
611c83ae 8372
06edf0c0
PA
8373/* Make a momentary breakpoint based on the master breakpoint ORIG.
8374 The new breakpoint will have type TYPE, and use OPS as it
8375 breakpoint_ops. */
e58b0e63 8376
06edf0c0
PA
8377static struct breakpoint *
8378momentary_breakpoint_from_master (struct breakpoint *orig,
8379 enum bptype type,
c0a91b2b 8380 const struct breakpoint_ops *ops)
e58b0e63
PA
8381{
8382 struct breakpoint *copy;
8383
06edf0c0 8384 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8385 copy->loc = allocate_bp_location (copy);
0e30163f 8386 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8387
a6d9a66e 8388 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8389 copy->loc->requested_address = orig->loc->requested_address;
8390 copy->loc->address = orig->loc->address;
8391 copy->loc->section = orig->loc->section;
6c95b8df 8392 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8393 copy->loc->probe = orig->loc->probe;
e58b0e63 8394
f8eba3c6
TT
8395 if (orig->loc->source_file != NULL)
8396 copy->loc->source_file = xstrdup (orig->loc->source_file);
e58b0e63 8397
f8eba3c6 8398 copy->loc->line_number = orig->loc->line_number;
e58b0e63
PA
8399 copy->frame_id = orig->frame_id;
8400 copy->thread = orig->thread;
6c95b8df 8401 copy->pspace = orig->pspace;
e58b0e63
PA
8402
8403 copy->enable_state = bp_enabled;
8404 copy->disposition = disp_donttouch;
8405 copy->number = internal_breakpoint_number--;
8406
8407 update_global_location_list_nothrow (0);
8408 return copy;
8409}
8410
06edf0c0
PA
8411/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8412 ORIG is NULL. */
8413
8414struct breakpoint *
8415clone_momentary_breakpoint (struct breakpoint *orig)
8416{
8417 /* If there's nothing to clone, then return nothing. */
8418 if (orig == NULL)
8419 return NULL;
8420
8421 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8422}
8423
611c83ae 8424struct breakpoint *
a6d9a66e
UW
8425set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8426 enum bptype type)
611c83ae
PA
8427{
8428 struct symtab_and_line sal;
8429
8430 sal = find_pc_line (pc, 0);
8431 sal.pc = pc;
8432 sal.section = find_pc_overlay (pc);
8433 sal.explicit_pc = 1;
8434
a6d9a66e 8435 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8436}
c906108c 8437\f
c5aa993b 8438
c906108c
SS
8439/* Tell the user we have just set a breakpoint B. */
8440
8441static void
fba45db2 8442mention (struct breakpoint *b)
c906108c 8443{
348d480f 8444 b->ops->print_mention (b);
79a45e25 8445 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 8446 return;
c906108c
SS
8447 printf_filtered ("\n");
8448}
c906108c 8449\f
c5aa993b 8450
0d381245 8451static struct bp_location *
39d61571 8452add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8453 const struct symtab_and_line *sal)
8454{
8455 struct bp_location *loc, **tmp;
3742cc8b
YQ
8456 CORE_ADDR adjusted_address;
8457 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8458
8459 if (loc_gdbarch == NULL)
8460 loc_gdbarch = b->gdbarch;
8461
8462 /* Adjust the breakpoint's address prior to allocating a location.
8463 Once we call allocate_bp_location(), that mostly uninitialized
8464 location will be placed on the location chain. Adjustment of the
8465 breakpoint may cause target_read_memory() to be called and we do
8466 not want its scan of the location chain to find a breakpoint and
8467 location that's only been partially initialized. */
8468 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8469 sal->pc, b->type);
0d381245 8470
39d61571 8471 loc = allocate_bp_location (b);
0d381245
VP
8472 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
8473 ;
8474 *tmp = loc;
3742cc8b 8475
0d381245 8476 loc->requested_address = sal->pc;
3742cc8b 8477 loc->address = adjusted_address;
6c95b8df 8478 loc->pspace = sal->pspace;
55aa24fb 8479 loc->probe = sal->probe;
6c95b8df 8480 gdb_assert (loc->pspace != NULL);
0d381245 8481 loc->section = sal->section;
3742cc8b 8482 loc->gdbarch = loc_gdbarch;
f8eba3c6
TT
8483
8484 if (sal->symtab != NULL)
8485 loc->source_file = xstrdup (sal->symtab->filename);
8486 loc->line_number = sal->line;
8487
0e30163f
JK
8488 set_breakpoint_location_function (loc,
8489 sal->explicit_pc || sal->explicit_line);
0d381245
VP
8490 return loc;
8491}
514f746b
AR
8492\f
8493
8494/* Return 1 if LOC is pointing to a permanent breakpoint,
8495 return 0 otherwise. */
8496
8497static int
8498bp_loc_is_permanent (struct bp_location *loc)
8499{
8500 int len;
8501 CORE_ADDR addr;
1afeeb75 8502 const gdb_byte *bpoint;
514f746b 8503 gdb_byte *target_mem;
939c61fa
JK
8504 struct cleanup *cleanup;
8505 int retval = 0;
514f746b
AR
8506
8507 gdb_assert (loc != NULL);
8508
8509 addr = loc->address;
1afeeb75 8510 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 8511
939c61fa 8512 /* Software breakpoints unsupported? */
1afeeb75 8513 if (bpoint == NULL)
939c61fa
JK
8514 return 0;
8515
514f746b
AR
8516 target_mem = alloca (len);
8517
939c61fa
JK
8518 /* Enable the automatic memory restoration from breakpoints while
8519 we read the memory. Otherwise we could say about our temporary
8520 breakpoints they are permanent. */
6c95b8df
PA
8521 cleanup = save_current_space_and_thread ();
8522
8523 switch_to_program_space_and_thread (loc->pspace);
8524 make_show_memory_breakpoints_cleanup (0);
939c61fa 8525
514f746b 8526 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 8527 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 8528 retval = 1;
514f746b 8529
939c61fa
JK
8530 do_cleanups (cleanup);
8531
8532 return retval;
514f746b
AR
8533}
8534
e7e0cddf
SS
8535/* The style in which to perform a dynamic printf. This is a user
8536 option because different output options have different tradeoffs;
8537 if GDB does the printing, there is better error handling if there
8538 is a problem with any of the arguments, but using an inferior
8539 function lets you have special-purpose printers and sending of
8540 output to the same place as compiled-in print functions. (Future
8541 styles may include the ability to do a target-side printf.) */
8542
8543static const char dprintf_style_gdb[] = "gdb";
8544static const char dprintf_style_call[] = "call";
8545static const char *const dprintf_style_enums[] = {
8546 dprintf_style_gdb,
8547 dprintf_style_call,
8548 NULL
8549};
8550static const char *dprintf_style = dprintf_style_gdb;
8551
8552/* The function to use for dynamic printf if the preferred style is to
8553 call into the inferior. The value is simply a string that is
8554 copied into the command, so it can be anything that GDB can
8555 evaluate to a callable address, not necessarily a function name. */
8556
8557static char *dprintf_function = "";
8558
8559/* The channel to use for dynamic printf if the preferred style is to
8560 call into the inferior; if a nonempty string, it will be passed to
8561 the call as the first argument, with the format string as the
8562 second. As with the dprintf function, this can be anything that
8563 GDB knows how to evaluate, so in addition to common choices like
8564 "stderr", this could be an app-specific expression like
8565 "mystreams[curlogger]". */
514f746b 8566
e7e0cddf
SS
8567static char *dprintf_channel = "";
8568
8569/* Build a command list for the dprintf corresponding to the current
8570 settings of the dprintf style options. */
8571
8572static void
8573update_dprintf_command_list (struct breakpoint *b)
8574{
8575 char *dprintf_args = b->extra_string;
8576 char *printf_line = NULL;
8577
8578 if (!dprintf_args)
8579 return;
8580
8581 dprintf_args = skip_spaces (dprintf_args);
8582
8583 /* Allow a comma, as it may have terminated a location, but don't
8584 insist on it. */
8585 if (*dprintf_args == ',')
8586 ++dprintf_args;
8587 dprintf_args = skip_spaces (dprintf_args);
8588
8589 if (*dprintf_args != '"')
8590 error (_("Bad format string, missing '\"'."));
8591
8592 if (strcmp (dprintf_style, "gdb") == 0)
8593 printf_line = xstrprintf ("printf %s", dprintf_args);
8594 else if (strcmp (dprintf_style, "call") == 0)
8595 {
8596 if (!dprintf_function)
8597 error (_("No function supplied for dprintf call"));
8598
8599 if (dprintf_channel && strlen (dprintf_channel) > 0)
8600 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8601 dprintf_function,
8602 dprintf_channel,
8603 dprintf_args);
8604 else
8605 printf_line = xstrprintf ("call (void) %s (%s)",
8606 dprintf_function,
8607 dprintf_args);
8608 }
8609 else
8610 internal_error (__FILE__, __LINE__,
8611 _("Invalid dprintf style."));
8612
8613 /* Manufacture a printf/continue sequence. */
8614 if (printf_line)
8615 {
8616 struct command_line *printf_cmd_line, *cont_cmd_line = NULL;
8617
8618 cont_cmd_line = xmalloc (sizeof (struct command_line));
8619 cont_cmd_line->control_type = simple_control;
8620 cont_cmd_line->body_count = 0;
8621 cont_cmd_line->body_list = NULL;
8622 cont_cmd_line->next = NULL;
8623 cont_cmd_line->line = xstrdup ("continue");
8624
8625 printf_cmd_line = xmalloc (sizeof (struct command_line));
8626 printf_cmd_line->control_type = simple_control;
8627 printf_cmd_line->body_count = 0;
8628 printf_cmd_line->body_list = NULL;
8629 printf_cmd_line->next = cont_cmd_line;
8630 printf_cmd_line->line = printf_line;
8631
8632 breakpoint_set_commands (b, printf_cmd_line);
8633 }
8634}
8635
8636/* Update all dprintf commands, making their command lists reflect
8637 current style settings. */
8638
8639static void
8640update_dprintf_commands (char *args, int from_tty,
8641 struct cmd_list_element *c)
8642{
8643 struct breakpoint *b;
8644
8645 ALL_BREAKPOINTS (b)
8646 {
8647 if (b->type == bp_dprintf)
8648 update_dprintf_command_list (b);
8649 }
8650}
c3f6f71d 8651
018d34a4
VP
8652/* Create a breakpoint with SAL as location. Use ADDR_STRING
8653 as textual description of the location, and COND_STRING
db107f19 8654 as condition expression. */
018d34a4
VP
8655
8656static void
d9b3f62e
PA
8657init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8658 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 8659 char *filter, char *cond_string,
e7e0cddf 8660 char *extra_string,
d9b3f62e
PA
8661 enum bptype type, enum bpdisp disposition,
8662 int thread, int task, int ignore_count,
c0a91b2b 8663 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8664 int enabled, int internal, unsigned flags,
8665 int display_canonical)
018d34a4 8666{
0d381245 8667 int i;
018d34a4
VP
8668
8669 if (type == bp_hardware_breakpoint)
8670 {
fbbd034e
AS
8671 int target_resources_ok;
8672
8673 i = hw_breakpoint_used_count ();
8674 target_resources_ok =
8675 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8676 i + 1, 0);
8677 if (target_resources_ok == 0)
8678 error (_("No hardware breakpoint support in the target."));
8679 else if (target_resources_ok < 0)
8680 error (_("Hardware breakpoints used exceeds limit."));
8681 }
8682
6c95b8df
PA
8683 gdb_assert (sals.nelts > 0);
8684
0d381245
VP
8685 for (i = 0; i < sals.nelts; ++i)
8686 {
8687 struct symtab_and_line sal = sals.sals[i];
8688 struct bp_location *loc;
8689
8690 if (from_tty)
5af949e3
UW
8691 {
8692 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8693 if (!loc_gdbarch)
8694 loc_gdbarch = gdbarch;
8695
8696 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8697 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8698 }
0d381245
VP
8699
8700 if (i == 0)
8701 {
d9b3f62e 8702 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8703 b->thread = thread;
4a306c9a 8704 b->task = task;
855a6e68 8705
0d381245 8706 b->cond_string = cond_string;
e7e0cddf 8707 b->extra_string = extra_string;
0d381245 8708 b->ignore_count = ignore_count;
41447f92 8709 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8710 b->disposition = disposition;
6c95b8df 8711
44f238bb
PA
8712 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8713 b->loc->inserted = 1;
8714
0fb4aa4b
PA
8715 if (type == bp_static_tracepoint)
8716 {
d9b3f62e 8717 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8718 struct static_tracepoint_marker marker;
8719
983af33b 8720 if (strace_marker_p (b))
0fb4aa4b
PA
8721 {
8722 /* We already know the marker exists, otherwise, we
8723 wouldn't see a sal for it. */
8724 char *p = &addr_string[3];
8725 char *endp;
8726 char *marker_str;
0fb4aa4b 8727
e9cafbcc 8728 p = skip_spaces (p);
0fb4aa4b 8729
e9cafbcc 8730 endp = skip_to_space (p);
0fb4aa4b
PA
8731
8732 marker_str = savestring (p, endp - p);
d9b3f62e 8733 t->static_trace_marker_id = marker_str;
0fb4aa4b 8734
3e43a32a
MS
8735 printf_filtered (_("Probed static tracepoint "
8736 "marker \"%s\"\n"),
d9b3f62e 8737 t->static_trace_marker_id);
0fb4aa4b
PA
8738 }
8739 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8740 {
d9b3f62e 8741 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
8742 release_static_tracepoint_marker (&marker);
8743
3e43a32a
MS
8744 printf_filtered (_("Probed static tracepoint "
8745 "marker \"%s\"\n"),
d9b3f62e 8746 t->static_trace_marker_id);
0fb4aa4b
PA
8747 }
8748 else
3e43a32a
MS
8749 warning (_("Couldn't determine the static "
8750 "tracepoint marker to probe"));
0fb4aa4b
PA
8751 }
8752
0d381245
VP
8753 loc = b->loc;
8754 }
8755 else
018d34a4 8756 {
39d61571 8757 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8758 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8759 loc->inserted = 1;
0d381245
VP
8760 }
8761
514f746b
AR
8762 if (bp_loc_is_permanent (loc))
8763 make_breakpoint_permanent (b);
8764
0d381245
VP
8765 if (b->cond_string)
8766 {
8767 char *arg = b->cond_string;
d32a6982 8768 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
0d381245 8769 if (*arg)
588ae58c 8770 error (_("Garbage '%s' follows condition"), arg);
018d34a4 8771 }
e7e0cddf
SS
8772
8773 /* Dynamic printf requires and uses additional arguments on the
8774 command line, otherwise it's an error. */
8775 if (type == bp_dprintf)
8776 {
8777 if (b->extra_string)
8778 update_dprintf_command_list (b);
8779 else
8780 error (_("Format string required"));
8781 }
8782 else if (b->extra_string)
588ae58c 8783 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 8784 }
018d34a4 8785
56435ebe 8786 b->display_canonical = display_canonical;
018d34a4
VP
8787 if (addr_string)
8788 b->addr_string = addr_string;
8789 else
8790 /* addr_string has to be used or breakpoint_re_set will delete
8791 me. */
5af949e3
UW
8792 b->addr_string
8793 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 8794 b->filter = filter;
d9b3f62e 8795}
018d34a4 8796
d9b3f62e
PA
8797static void
8798create_breakpoint_sal (struct gdbarch *gdbarch,
8799 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 8800 char *filter, char *cond_string,
e7e0cddf 8801 char *extra_string,
d9b3f62e
PA
8802 enum bptype type, enum bpdisp disposition,
8803 int thread, int task, int ignore_count,
c0a91b2b 8804 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8805 int enabled, int internal, unsigned flags,
8806 int display_canonical)
d9b3f62e
PA
8807{
8808 struct breakpoint *b;
8809 struct cleanup *old_chain;
8810
8811 if (is_tracepoint_type (type))
8812 {
8813 struct tracepoint *t;
8814
8815 t = XCNEW (struct tracepoint);
8816 b = &t->base;
8817 }
8818 else
8819 b = XNEW (struct breakpoint);
8820
8821 old_chain = make_cleanup (xfree, b);
8822
8823 init_breakpoint_sal (b, gdbarch,
8824 sals, addr_string,
e7e0cddf 8825 filter, cond_string, extra_string,
d9b3f62e
PA
8826 type, disposition,
8827 thread, task, ignore_count,
8828 ops, from_tty,
44f238bb
PA
8829 enabled, internal, flags,
8830 display_canonical);
d9b3f62e
PA
8831 discard_cleanups (old_chain);
8832
3ea46bff 8833 install_breakpoint (internal, b, 0);
018d34a4
VP
8834}
8835
8836/* Add SALS.nelts breakpoints to the breakpoint table. For each
8837 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8838 value. COND_STRING, if not NULL, specified the condition to be
8839 used for all breakpoints. Essentially the only case where
8840 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8841 function. In that case, it's still not possible to specify
8842 separate conditions for different overloaded functions, so
8843 we take just a single condition string.
8844
c3f6f71d 8845 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8846 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8847 array contents). If the function fails (error() is called), the
8848 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8849 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8850
8851static void
8cdf0e15 8852create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8853 struct linespec_result *canonical,
e7e0cddf 8854 char *cond_string, char *extra_string,
8cdf0e15
VP
8855 enum bptype type, enum bpdisp disposition,
8856 int thread, int task, int ignore_count,
c0a91b2b 8857 const struct breakpoint_ops *ops, int from_tty,
44f238bb 8858 int enabled, int internal, unsigned flags)
c906108c 8859{
018d34a4 8860 int i;
f8eba3c6 8861 struct linespec_sals *lsal;
cc59ec59 8862
f8eba3c6
TT
8863 if (canonical->pre_expanded)
8864 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
8865
8866 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 8867 {
f8eba3c6
TT
8868 /* Note that 'addr_string' can be NULL in the case of a plain
8869 'break', without arguments. */
8870 char *addr_string = (canonical->addr_string
8871 ? xstrdup (canonical->addr_string)
8872 : NULL);
8873 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
8874 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 8875
f8eba3c6
TT
8876 make_cleanup (xfree, filter_string);
8877 create_breakpoint_sal (gdbarch, lsal->sals,
8878 addr_string,
8879 filter_string,
e7e0cddf
SS
8880 cond_string, extra_string,
8881 type, disposition,
84f4c1fe 8882 thread, task, ignore_count, ops,
44f238bb 8883 from_tty, enabled, internal, flags,
56435ebe 8884 canonical->special_display);
f8eba3c6 8885 discard_cleanups (inner);
c3f6f71d 8886 }
c3f6f71d 8887}
c906108c 8888
9998af43 8889/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 8890 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 8891 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
8892 address strings. ADDRESS points to the end of the SAL.
8893
8894 The array and the line spec strings are allocated on the heap, it is
8895 the caller's responsibility to free them. */
c906108c 8896
b9362cc7 8897static void
c3f6f71d 8898parse_breakpoint_sals (char **address,
58438ac1 8899 struct linespec_result *canonical)
c3f6f71d
JM
8900{
8901 char *addr_start = *address;
cc59ec59 8902
c3f6f71d 8903 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 8904 breakpoint. */
c3f6f71d
JM
8905 if ((*address) == NULL
8906 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 8907 {
1bfeeb0f
JL
8908 /* The last displayed codepoint, if it's valid, is our default breakpoint
8909 address. */
8910 if (last_displayed_sal_is_valid ())
c906108c 8911 {
f8eba3c6 8912 struct linespec_sals lsal;
c3f6f71d 8913 struct symtab_and_line sal;
cc59ec59 8914
4a64f543 8915 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 8916 lsal.sals.sals = (struct symtab_and_line *)
c906108c 8917 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
8918
8919 /* Set sal's pspace, pc, symtab, and line to the values
8920 corresponding to the last call to print_frame_info. */
8921 get_last_displayed_sal (&sal);
8922 sal.section = find_pc_overlay (sal.pc);
00903456 8923
4a64f543 8924 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
8925 where PC is the last displayed codepoint's address. So
8926 make sure to set sal.explicit_pc to prevent GDB from
8927 trying to expand the list of sals to include all other
8928 instances with the same symtab and line. */
00903456
JK
8929 sal.explicit_pc = 1;
8930
f8eba3c6
TT
8931 lsal.sals.sals[0] = sal;
8932 lsal.sals.nelts = 1;
8933 lsal.canonical = NULL;
8934
8935 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
8936 }
8937 else
8a3fe4f8 8938 error (_("No default breakpoint address now."));
c906108c
SS
8939 }
8940 else
8941 {
cc80f267
JK
8942 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
8943
c906108c 8944 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
8945 current_source_symtab (which is decode_line_1's default).
8946 This should produce the results we want almost all of the
cc80f267
JK
8947 time while leaving default_breakpoint_* alone.
8948
8949 ObjC: However, don't match an Objective-C method name which
8950 may have a '+' or '-' succeeded by a '['. */
8951 if (last_displayed_sal_is_valid ()
8952 && (!cursal.symtab
8953 || ((strchr ("+-", (*address)[0]) != NULL)
8954 && ((*address)[1] != '['))))
f8eba3c6
TT
8955 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
8956 get_last_displayed_symtab (),
8957 get_last_displayed_line (),
8958 canonical, NULL, NULL);
c906108c 8959 else
f8eba3c6 8960 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 8961 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 8962 }
c3f6f71d 8963}
c906108c 8964
c906108c 8965
c3f6f71d 8966/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 8967 inserted as a breakpoint. If it can't throw an error. */
c906108c 8968
b9362cc7 8969static void
23e7acfb 8970breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
8971{
8972 int i;
cc59ec59 8973
c3f6f71d 8974 for (i = 0; i < sals->nelts; i++)
ee53e872 8975 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
8976}
8977
7a697b8d
SS
8978/* Fast tracepoints may have restrictions on valid locations. For
8979 instance, a fast tracepoint using a jump instead of a trap will
8980 likely have to overwrite more bytes than a trap would, and so can
8981 only be placed where the instruction is longer than the jump, or a
8982 multi-instruction sequence does not have a jump into the middle of
8983 it, etc. */
8984
8985static void
8986check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8987 struct symtabs_and_lines *sals)
8988{
8989 int i, rslt;
8990 struct symtab_and_line *sal;
8991 char *msg;
8992 struct cleanup *old_chain;
8993
8994 for (i = 0; i < sals->nelts; i++)
8995 {
f8eba3c6
TT
8996 struct gdbarch *sarch;
8997
7a697b8d
SS
8998 sal = &sals->sals[i];
8999
f8eba3c6
TT
9000 sarch = get_sal_arch (*sal);
9001 /* We fall back to GDBARCH if there is no architecture
9002 associated with SAL. */
9003 if (sarch == NULL)
9004 sarch = gdbarch;
9005 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9006 NULL, &msg);
9007 old_chain = make_cleanup (xfree, msg);
9008
9009 if (!rslt)
9010 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9011 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9012
9013 do_cleanups (old_chain);
9014 }
9015}
9016
018d34a4
VP
9017/* Given TOK, a string specification of condition and thread, as
9018 accepted by the 'break' command, extract the condition
9019 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9020 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9021 If no condition is found, *COND_STRING is set to NULL.
9022 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9023
9024static void
9025find_condition_and_thread (char *tok, CORE_ADDR pc,
e7e0cddf
SS
9026 char **cond_string, int *thread, int *task,
9027 char **rest)
018d34a4
VP
9028{
9029 *cond_string = NULL;
9030 *thread = -1;
9031 while (tok && *tok)
9032 {
9033 char *end_tok;
9034 int toklen;
9035 char *cond_start = NULL;
9036 char *cond_end = NULL;
cc59ec59 9037
e9cafbcc 9038 tok = skip_spaces (tok);
e7e0cddf
SS
9039
9040 if ((*tok == '"' || *tok == ',') && rest)
9041 {
9042 *rest = savestring (tok, strlen (tok));
9043 return;
9044 }
9045
e9cafbcc 9046 end_tok = skip_to_space (tok);
d634f2de 9047
018d34a4 9048 toklen = end_tok - tok;
d634f2de 9049
018d34a4
VP
9050 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9051 {
f7545552
TT
9052 struct expression *expr;
9053
018d34a4 9054 tok = cond_start = end_tok + 1;
f7545552
TT
9055 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
9056 xfree (expr);
018d34a4 9057 cond_end = tok;
d634f2de 9058 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9059 }
9060 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9061 {
9062 char *tmptok;
d634f2de 9063
018d34a4
VP
9064 tok = end_tok + 1;
9065 tmptok = tok;
9066 *thread = strtol (tok, &tok, 0);
9067 if (tok == tmptok)
9068 error (_("Junk after thread keyword."));
9069 if (!valid_thread_id (*thread))
9070 error (_("Unknown thread %d."), *thread);
9071 }
4a306c9a
JB
9072 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9073 {
9074 char *tmptok;
9075
9076 tok = end_tok + 1;
9077 tmptok = tok;
9078 *task = strtol (tok, &tok, 0);
9079 if (tok == tmptok)
9080 error (_("Junk after task keyword."));
9081 if (!valid_task_id (*task))
b6199126 9082 error (_("Unknown task %d."), *task);
4a306c9a 9083 }
e7e0cddf
SS
9084 else if (rest)
9085 {
9086 *rest = savestring (tok, strlen (tok));
ccab2054 9087 return;
e7e0cddf 9088 }
018d34a4
VP
9089 else
9090 error (_("Junk at end of arguments."));
9091 }
9092}
9093
0fb4aa4b
PA
9094/* Decode a static tracepoint marker spec. */
9095
9096static struct symtabs_and_lines
9097decode_static_tracepoint_spec (char **arg_p)
9098{
9099 VEC(static_tracepoint_marker_p) *markers = NULL;
9100 struct symtabs_and_lines sals;
0fb4aa4b
PA
9101 struct cleanup *old_chain;
9102 char *p = &(*arg_p)[3];
9103 char *endp;
9104 char *marker_str;
9105 int i;
9106
e9cafbcc 9107 p = skip_spaces (p);
0fb4aa4b 9108
e9cafbcc 9109 endp = skip_to_space (p);
0fb4aa4b
PA
9110
9111 marker_str = savestring (p, endp - p);
9112 old_chain = make_cleanup (xfree, marker_str);
9113
9114 markers = target_static_tracepoint_markers_by_strid (marker_str);
9115 if (VEC_empty(static_tracepoint_marker_p, markers))
9116 error (_("No known static tracepoint marker named %s"), marker_str);
9117
9118 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9119 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9120
9121 for (i = 0; i < sals.nelts; i++)
9122 {
9123 struct static_tracepoint_marker *marker;
9124
9125 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9126
9127 init_sal (&sals.sals[i]);
9128
9129 sals.sals[i] = find_pc_line (marker->address, 0);
9130 sals.sals[i].pc = marker->address;
9131
9132 release_static_tracepoint_marker (marker);
9133 }
9134
9135 do_cleanups (old_chain);
9136
9137 *arg_p = endp;
9138 return sals;
9139}
9140
fd9b8c24
PA
9141/* Set a breakpoint. This function is shared between CLI and MI
9142 functions for setting a breakpoint. This function has two major
9143 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
9144 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
9145 breakpoint location, address and thread. Otherwise, ARG is just
9146 the location of breakpoint, with condition and thread specified by
9147 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
9148 the breakpoint number will be allocated from the internal
9149 breakpoint count. Returns true if any breakpoint was created;
9150 false otherwise. */
0101ce28 9151
8cdf0e15
VP
9152int
9153create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9154 char *arg, char *cond_string,
9155 int thread, char *extra_string,
8cdf0e15 9156 int parse_condition_and_thread,
0fb4aa4b 9157 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9158 int ignore_count,
9159 enum auto_boolean pending_break_support,
c0a91b2b 9160 const struct breakpoint_ops *ops,
44f238bb
PA
9161 int from_tty, int enabled, int internal,
9162 unsigned flags)
c3f6f71d 9163{
b78a6381 9164 volatile struct gdb_exception e;
f8eba3c6 9165 char *copy_arg = NULL;
c3f6f71d 9166 char *addr_start = arg;
7efd8fc2 9167 struct linespec_result canonical;
c3f6f71d 9168 struct cleanup *old_chain;
80c99de1 9169 struct cleanup *bkpt_chain = NULL;
0101ce28 9170 int pending = 0;
4a306c9a 9171 int task = 0;
86b17b60 9172 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9173
348d480f
PA
9174 gdb_assert (ops != NULL);
9175
7efd8fc2 9176 init_linespec_result (&canonical);
c3f6f71d 9177
b78a6381
TT
9178 TRY_CATCH (e, RETURN_MASK_ALL)
9179 {
983af33b
SDJ
9180 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9181 addr_start, &copy_arg);
b78a6381 9182 }
0101ce28
JJ
9183
9184 /* If caller is interested in rc value from parse, set value. */
05ff989b 9185 switch (e.reason)
0101ce28 9186 {
983af33b
SDJ
9187 case GDB_NO_ERROR:
9188 if (VEC_empty (linespec_sals, canonical.sals))
9189 return 0;
9190 break;
05ff989b
AC
9191 case RETURN_ERROR:
9192 switch (e.error)
0101ce28 9193 {
05ff989b 9194 case NOT_FOUND_ERROR:
0101ce28 9195
05ff989b
AC
9196 /* If pending breakpoint support is turned off, throw
9197 error. */
fa8d40ab
JJ
9198
9199 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9200 throw_exception (e);
9201
9202 exception_print (gdb_stderr, e);
fa8d40ab 9203
05ff989b
AC
9204 /* If pending breakpoint support is auto query and the user
9205 selects no, then simply return the error code. */
059fb39f 9206 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9207 && !nquery (_("Make %s pending on future shared library load? "),
9208 bptype_string (type_wanted)))
fd9b8c24 9209 return 0;
fa8d40ab 9210
05ff989b
AC
9211 /* At this point, either the user was queried about setting
9212 a pending breakpoint and selected yes, or pending
9213 breakpoint behavior is on and thus a pending breakpoint
9214 is defaulted on behalf of the user. */
f8eba3c6
TT
9215 {
9216 struct linespec_sals lsal;
9217
9218 copy_arg = xstrdup (addr_start);
9219 lsal.canonical = xstrdup (copy_arg);
9220 lsal.sals.nelts = 1;
9221 lsal.sals.sals = XNEW (struct symtab_and_line);
9222 init_sal (&lsal.sals.sals[0]);
9223 pending = 1;
9224 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9225 }
05ff989b
AC
9226 break;
9227 default:
98deb0da 9228 throw_exception (e);
0101ce28 9229 }
2abae994 9230 break;
05ff989b 9231 default:
983af33b 9232 throw_exception (e);
0101ce28 9233 }
c3f6f71d 9234
4a64f543 9235 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9236 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9237
c3f6f71d
JM
9238 /* ----------------------------- SNIP -----------------------------
9239 Anything added to the cleanup chain beyond this point is assumed
9240 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9241 then the memory is not reclaimed. */
9242 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9243
c3f6f71d
JM
9244 /* Resolve all line numbers to PC's and verify that the addresses
9245 are ok for the target. */
0101ce28 9246 if (!pending)
f8eba3c6
TT
9247 {
9248 int ix;
9249 struct linespec_sals *iter;
9250
9251 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9252 breakpoint_sals_to_pc (&iter->sals);
9253 }
c3f6f71d 9254
7a697b8d 9255 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9256 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9257 {
9258 int ix;
9259 struct linespec_sals *iter;
9260
9261 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9262 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9263 }
7a697b8d 9264
c3f6f71d
JM
9265 /* Verify that condition can be parsed, before setting any
9266 breakpoints. Allocate a separate condition expression for each
4a64f543 9267 breakpoint. */
0101ce28 9268 if (!pending)
c3f6f71d 9269 {
f8eba3c6
TT
9270 struct linespec_sals *lsal;
9271
9272 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9273
2f069f6f 9274 if (parse_condition_and_thread)
72b2ff0e 9275 {
e7e0cddf 9276 char *rest;
72b2ff0e
VP
9277 /* Here we only parse 'arg' to separate condition
9278 from thread number, so parsing in context of first
9279 sal is OK. When setting the breakpoint we'll
9280 re-parse it in context of each sal. */
9281 cond_string = NULL;
9282 thread = -1;
e7e0cddf 9283 rest = NULL;
f8eba3c6 9284 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
e7e0cddf 9285 &thread, &task, &rest);
72b2ff0e
VP
9286 if (cond_string)
9287 make_cleanup (xfree, cond_string);
e7e0cddf
SS
9288 if (rest)
9289 make_cleanup (xfree, rest);
9290 if (rest)
9291 extra_string = rest;
72b2ff0e 9292 }
2f069f6f 9293 else
72b2ff0e
VP
9294 {
9295 /* Create a private copy of condition string. */
9296 if (cond_string)
9297 {
9298 cond_string = xstrdup (cond_string);
9299 make_cleanup (xfree, cond_string);
9300 }
e7e0cddf
SS
9301 /* Create a private copy of any extra string. */
9302 if (extra_string)
9303 {
9304 extra_string = xstrdup (extra_string);
9305 make_cleanup (xfree, extra_string);
9306 }
72b2ff0e 9307 }
0fb4aa4b 9308
983af33b 9309 ops->create_breakpoints_sal (gdbarch, &canonical, lsal,
e7e0cddf 9310 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9311 tempflag ? disp_del : disp_donttouch,
9312 thread, task, ignore_count, ops,
44f238bb 9313 from_tty, enabled, internal, flags);
c906108c 9314 }
0101ce28
JJ
9315 else
9316 {
0101ce28
JJ
9317 struct breakpoint *b;
9318
0101ce28
JJ
9319 make_cleanup (xfree, copy_arg);
9320
bfccc43c
YQ
9321 if (is_tracepoint_type (type_wanted))
9322 {
9323 struct tracepoint *t;
9324
9325 t = XCNEW (struct tracepoint);
9326 b = &t->base;
9327 }
9328 else
9329 b = XNEW (struct breakpoint);
9330
9331 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9332
f8eba3c6 9333 b->addr_string = copy_arg;
72b2ff0e 9334 b->cond_string = NULL;
e7e0cddf 9335 b->extra_string = NULL;
0101ce28 9336 b->ignore_count = ignore_count;
0101ce28 9337 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9338 b->condition_not_parsed = 1;
41447f92 9339 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9340 if ((type_wanted != bp_breakpoint
9341 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9342 b->pspace = current_program_space;
8bea4e01 9343
bfccc43c 9344 install_breakpoint (internal, b, 0);
0101ce28
JJ
9345 }
9346
f8eba3c6 9347 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9348 {
3e43a32a
MS
9349 warning (_("Multiple breakpoints were set.\nUse the "
9350 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9351 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9352 }
9353
80c99de1
PA
9354 /* That's it. Discard the cleanups for data inserted into the
9355 breakpoint. */
9356 discard_cleanups (bkpt_chain);
9357 /* But cleanup everything else. */
c3f6f71d 9358 do_cleanups (old_chain);
217dc9e2 9359
80c99de1 9360 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 9361 update_global_location_list (1);
fd9b8c24
PA
9362
9363 return 1;
c3f6f71d 9364}
c906108c 9365
348d480f 9366/* Set a breakpoint.
72b2ff0e
VP
9367 ARG is a string describing breakpoint address,
9368 condition, and thread.
9369 FLAG specifies if a breakpoint is hardware on,
9370 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9371 and BP_TEMPFLAG. */
348d480f 9372
98deb0da 9373static void
72b2ff0e 9374break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9375{
72b2ff0e 9376 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9377 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9378 ? bp_hardware_breakpoint
9379 : bp_breakpoint);
55aa24fb
SDJ
9380 struct breakpoint_ops *ops;
9381 const char *arg_cp = arg;
9382
9383 /* Matching breakpoints on probes. */
9384 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9385 ops = &bkpt_probe_breakpoint_ops;
9386 else
9387 ops = &bkpt_breakpoint_ops;
c3f6f71d 9388
8cdf0e15
VP
9389 create_breakpoint (get_current_arch (),
9390 arg,
e7e0cddf 9391 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 9392 tempflag, type_wanted,
8cdf0e15
VP
9393 0 /* Ignore count */,
9394 pending_break_support,
55aa24fb 9395 ops,
8cdf0e15 9396 from_tty,
84f4c1fe 9397 1 /* enabled */,
44f238bb
PA
9398 0 /* internal */,
9399 0);
c906108c
SS
9400}
9401
c906108c
SS
9402/* Helper function for break_command_1 and disassemble_command. */
9403
9404void
fba45db2 9405resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9406{
9407 CORE_ADDR pc;
9408
9409 if (sal->pc == 0 && sal->symtab != NULL)
9410 {
9411 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9412 error (_("No line %d in file \"%s\"."),
c906108c
SS
9413 sal->line, sal->symtab->filename);
9414 sal->pc = pc;
6a048695 9415
4a64f543
MS
9416 /* If this SAL corresponds to a breakpoint inserted using a line
9417 number, then skip the function prologue if necessary. */
6a048695 9418 if (sal->explicit_line)
059acae7 9419 skip_prologue_sal (sal);
c906108c
SS
9420 }
9421
9422 if (sal->section == 0 && sal->symtab != NULL)
9423 {
9424 struct blockvector *bv;
c5aa993b
JM
9425 struct block *b;
9426 struct symbol *sym;
c906108c 9427
801e3a5b 9428 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
9429 if (bv != NULL)
9430 {
7f0df278 9431 sym = block_linkage_function (b);
c906108c
SS
9432 if (sym != NULL)
9433 {
9434 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 9435 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
9436 }
9437 else
9438 {
4a64f543
MS
9439 /* It really is worthwhile to have the section, so we'll
9440 just have to look harder. This case can be executed
9441 if we have line numbers but no functions (as can
9442 happen in assembly source). */
c906108c 9443
c5aa993b 9444 struct minimal_symbol *msym;
6c95b8df
PA
9445 struct cleanup *old_chain = save_current_space_and_thread ();
9446
9447 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
9448
9449 msym = lookup_minimal_symbol_by_pc (sal->pc);
9450 if (msym)
714835d5 9451 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
9452
9453 do_cleanups (old_chain);
c906108c
SS
9454 }
9455 }
9456 }
9457}
9458
9459void
fba45db2 9460break_command (char *arg, int from_tty)
c906108c 9461{
db107f19 9462 break_command_1 (arg, 0, from_tty);
c906108c
SS
9463}
9464
c906108c 9465void
fba45db2 9466tbreak_command (char *arg, int from_tty)
c906108c 9467{
db107f19 9468 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9469}
9470
c906108c 9471static void
fba45db2 9472hbreak_command (char *arg, int from_tty)
c906108c 9473{
db107f19 9474 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9475}
9476
9477static void
fba45db2 9478thbreak_command (char *arg, int from_tty)
c906108c 9479{
db107f19 9480 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9481}
9482
9483static void
fba45db2 9484stop_command (char *arg, int from_tty)
c906108c 9485{
a3f17187 9486 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9487Usage: stop in <function | address>\n\
a3f17187 9488 stop at <line>\n"));
c906108c
SS
9489}
9490
9491static void
fba45db2 9492stopin_command (char *arg, int from_tty)
c906108c
SS
9493{
9494 int badInput = 0;
9495
c5aa993b 9496 if (arg == (char *) NULL)
c906108c
SS
9497 badInput = 1;
9498 else if (*arg != '*')
9499 {
9500 char *argptr = arg;
9501 int hasColon = 0;
9502
4a64f543 9503 /* Look for a ':'. If this is a line number specification, then
53a5351d 9504 say it is bad, otherwise, it should be an address or
4a64f543 9505 function/method name. */
c906108c 9506 while (*argptr && !hasColon)
c5aa993b
JM
9507 {
9508 hasColon = (*argptr == ':');
9509 argptr++;
9510 }
c906108c
SS
9511
9512 if (hasColon)
c5aa993b 9513 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9514 else
c5aa993b 9515 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9516 }
9517
9518 if (badInput)
a3f17187 9519 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9520 else
db107f19 9521 break_command_1 (arg, 0, from_tty);
c906108c
SS
9522}
9523
9524static void
fba45db2 9525stopat_command (char *arg, int from_tty)
c906108c
SS
9526{
9527 int badInput = 0;
9528
c5aa993b 9529 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9530 badInput = 1;
9531 else
9532 {
9533 char *argptr = arg;
9534 int hasColon = 0;
9535
4a64f543
MS
9536 /* Look for a ':'. If there is a '::' then get out, otherwise
9537 it is probably a line number. */
c906108c 9538 while (*argptr && !hasColon)
c5aa993b
JM
9539 {
9540 hasColon = (*argptr == ':');
9541 argptr++;
9542 }
c906108c
SS
9543
9544 if (hasColon)
c5aa993b 9545 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9546 else
c5aa993b 9547 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9548 }
9549
9550 if (badInput)
a3f17187 9551 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 9552 else
db107f19 9553 break_command_1 (arg, 0, from_tty);
c906108c
SS
9554}
9555
e7e0cddf
SS
9556void dprintf_command (char *arg, int from_tty);
9557
9558/* The dynamic printf command is mostly like a regular breakpoint, but
9559 with a prewired command list consisting of a single output command,
9560 built from extra arguments supplied on the dprintf command
9561 line. */
9562
9563void
9564dprintf_command (char *arg, int from_tty)
9565{
9566 create_breakpoint (get_current_arch (),
9567 arg,
9568 NULL, 0, NULL, 1 /* parse arg */,
9569 0, bp_dprintf,
9570 0 /* Ignore count */,
9571 pending_break_support,
9572 &dprintf_breakpoint_ops,
9573 from_tty,
9574 1 /* enabled */,
9575 0 /* internal */,
9576 0);
9577}
9578
f1310107
TJB
9579/* Implement the "breakpoint_hit" breakpoint_ops method for
9580 ranged breakpoints. */
9581
9582static int
9583breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9584 struct address_space *aspace,
09ac7c10
TT
9585 CORE_ADDR bp_addr,
9586 const struct target_waitstatus *ws)
f1310107 9587{
09ac7c10 9588 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9589 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9590 return 0;
9591
f1310107
TJB
9592 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9593 bl->length, aspace, bp_addr);
9594}
9595
9596/* Implement the "resources_needed" breakpoint_ops method for
9597 ranged breakpoints. */
9598
9599static int
9600resources_needed_ranged_breakpoint (const struct bp_location *bl)
9601{
9602 return target_ranged_break_num_registers ();
9603}
9604
9605/* Implement the "print_it" breakpoint_ops method for
9606 ranged breakpoints. */
9607
9608static enum print_stop_action
348d480f 9609print_it_ranged_breakpoint (bpstat bs)
f1310107 9610{
348d480f 9611 struct breakpoint *b = bs->breakpoint_at;
f1310107 9612 struct bp_location *bl = b->loc;
79a45e25 9613 struct ui_out *uiout = current_uiout;
f1310107
TJB
9614
9615 gdb_assert (b->type == bp_hardware_breakpoint);
9616
9617 /* Ranged breakpoints have only one location. */
9618 gdb_assert (bl && bl->next == NULL);
9619
9620 annotate_breakpoint (b->number);
9621 if (b->disposition == disp_del)
9622 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
9623 else
9624 ui_out_text (uiout, "\nRanged breakpoint ");
9625 if (ui_out_is_mi_like_p (uiout))
9626 {
9627 ui_out_field_string (uiout, "reason",
9628 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9629 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9630 }
9631 ui_out_field_int (uiout, "bkptno", b->number);
9632 ui_out_text (uiout, ", ");
9633
9634 return PRINT_SRC_AND_LOC;
9635}
9636
9637/* Implement the "print_one" breakpoint_ops method for
9638 ranged breakpoints. */
9639
9640static void
9641print_one_ranged_breakpoint (struct breakpoint *b,
9642 struct bp_location **last_loc)
9643{
9644 struct bp_location *bl = b->loc;
9645 struct value_print_options opts;
79a45e25 9646 struct ui_out *uiout = current_uiout;
f1310107
TJB
9647
9648 /* Ranged breakpoints have only one location. */
9649 gdb_assert (bl && bl->next == NULL);
9650
9651 get_user_print_options (&opts);
9652
9653 if (opts.addressprint)
9654 /* We don't print the address range here, it will be printed later
9655 by print_one_detail_ranged_breakpoint. */
9656 ui_out_field_skip (uiout, "addr");
9657 annotate_field (5);
9658 print_breakpoint_location (b, bl);
9659 *last_loc = bl;
9660}
9661
9662/* Implement the "print_one_detail" breakpoint_ops method for
9663 ranged breakpoints. */
9664
9665static void
9666print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9667 struct ui_out *uiout)
9668{
9669 CORE_ADDR address_start, address_end;
9670 struct bp_location *bl = b->loc;
f99d8bf4
PA
9671 struct ui_file *stb = mem_fileopen ();
9672 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
9673
9674 gdb_assert (bl);
9675
9676 address_start = bl->address;
9677 address_end = address_start + bl->length - 1;
9678
9679 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 9680 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
9681 print_core_address (bl->gdbarch, address_start),
9682 print_core_address (bl->gdbarch, address_end));
9683 ui_out_field_stream (uiout, "addr", stb);
9684 ui_out_text (uiout, "\n");
9685
9686 do_cleanups (cleanup);
9687}
9688
9689/* Implement the "print_mention" breakpoint_ops method for
9690 ranged breakpoints. */
9691
9692static void
9693print_mention_ranged_breakpoint (struct breakpoint *b)
9694{
9695 struct bp_location *bl = b->loc;
79a45e25 9696 struct ui_out *uiout = current_uiout;
f1310107
TJB
9697
9698 gdb_assert (bl);
9699 gdb_assert (b->type == bp_hardware_breakpoint);
9700
9701 if (ui_out_is_mi_like_p (uiout))
9702 return;
9703
9704 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9705 b->number, paddress (bl->gdbarch, bl->address),
9706 paddress (bl->gdbarch, bl->address + bl->length - 1));
9707}
9708
9709/* Implement the "print_recreate" breakpoint_ops method for
9710 ranged breakpoints. */
9711
9712static void
9713print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9714{
9715 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
9716 b->addr_string_range_end);
d9b3f62e 9717 print_recreate_thread (b, fp);
f1310107
TJB
9718}
9719
9720/* The breakpoint_ops structure to be used in ranged breakpoints. */
9721
2060206e 9722static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9723
9724/* Find the address where the end of the breakpoint range should be
9725 placed, given the SAL of the end of the range. This is so that if
9726 the user provides a line number, the end of the range is set to the
9727 last instruction of the given line. */
9728
9729static CORE_ADDR
9730find_breakpoint_range_end (struct symtab_and_line sal)
9731{
9732 CORE_ADDR end;
9733
9734 /* If the user provided a PC value, use it. Otherwise,
9735 find the address of the end of the given location. */
9736 if (sal.explicit_pc)
9737 end = sal.pc;
9738 else
9739 {
9740 int ret;
9741 CORE_ADDR start;
9742
9743 ret = find_line_pc_range (sal, &start, &end);
9744 if (!ret)
9745 error (_("Could not find location of the end of the range."));
9746
9747 /* find_line_pc_range returns the start of the next line. */
9748 end--;
9749 }
9750
9751 return end;
9752}
9753
9754/* Implement the "break-range" CLI command. */
9755
9756static void
9757break_range_command (char *arg, int from_tty)
9758{
9759 char *arg_start, *addr_string_start, *addr_string_end;
9760 struct linespec_result canonical_start, canonical_end;
9761 int bp_count, can_use_bp, length;
9762 CORE_ADDR end;
9763 struct breakpoint *b;
9764 struct symtab_and_line sal_start, sal_end;
f1310107 9765 struct cleanup *cleanup_bkpt;
f8eba3c6 9766 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
9767
9768 /* We don't support software ranged breakpoints. */
9769 if (target_ranged_break_num_registers () < 0)
9770 error (_("This target does not support hardware ranged breakpoints."));
9771
9772 bp_count = hw_breakpoint_used_count ();
9773 bp_count += target_ranged_break_num_registers ();
9774 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9775 bp_count, 0);
9776 if (can_use_bp < 0)
9777 error (_("Hardware breakpoints used exceeds limit."));
9778
f8eba3c6 9779 arg = skip_spaces (arg);
f1310107
TJB
9780 if (arg == NULL || arg[0] == '\0')
9781 error(_("No address range specified."));
9782
f1310107
TJB
9783 init_linespec_result (&canonical_start);
9784
f8eba3c6
TT
9785 arg_start = arg;
9786 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 9787
f8eba3c6 9788 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
9789
9790 if (arg[0] != ',')
9791 error (_("Too few arguments."));
f8eba3c6 9792 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 9793 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
9794
9795 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
9796
9797 if (VEC_length (linespec_sals, canonical_start.sals) > 1
9798 || lsal_start->sals.nelts != 1)
f1310107
TJB
9799 error (_("Cannot create a ranged breakpoint with multiple locations."));
9800
f8eba3c6
TT
9801 sal_start = lsal_start->sals.sals[0];
9802 addr_string_start = savestring (arg_start, arg - arg_start);
9803 make_cleanup (xfree, addr_string_start);
f1310107
TJB
9804
9805 arg++; /* Skip the comma. */
f8eba3c6 9806 arg = skip_spaces (arg);
f1310107
TJB
9807
9808 /* Parse the end location. */
9809
f1310107
TJB
9810 init_linespec_result (&canonical_end);
9811 arg_start = arg;
9812
f8eba3c6 9813 /* We call decode_line_full directly here instead of using
f1310107
TJB
9814 parse_breakpoint_sals because we need to specify the start location's
9815 symtab and line as the default symtab and line for the end of the
9816 range. This makes it possible to have ranges like "foo.c:27, +14",
9817 where +14 means 14 lines from the start location. */
f8eba3c6
TT
9818 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
9819 sal_start.symtab, sal_start.line,
9820 &canonical_end, NULL, NULL);
9821
9822 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 9823
f8eba3c6 9824 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 9825 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
9826
9827 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
9828 if (VEC_length (linespec_sals, canonical_end.sals) > 1
9829 || lsal_end->sals.nelts != 1)
f1310107
TJB
9830 error (_("Cannot create a ranged breakpoint with multiple locations."));
9831
f8eba3c6
TT
9832 sal_end = lsal_end->sals.sals[0];
9833 addr_string_end = savestring (arg_start, arg - arg_start);
9834 make_cleanup (xfree, addr_string_end);
f1310107
TJB
9835
9836 end = find_breakpoint_range_end (sal_end);
9837 if (sal_start.pc > end)
177b42fe 9838 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9839
9840 length = end - sal_start.pc + 1;
9841 if (length < 0)
9842 /* Length overflowed. */
9843 error (_("Address range too large."));
9844 else if (length == 1)
9845 {
9846 /* This range is simple enough to be handled by
9847 the `hbreak' command. */
9848 hbreak_command (addr_string_start, 1);
9849
9850 do_cleanups (cleanup_bkpt);
9851
9852 return;
9853 }
9854
9855 /* Now set up the breakpoint. */
9856 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 9857 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
9858 set_breakpoint_count (breakpoint_count + 1);
9859 b->number = breakpoint_count;
9860 b->disposition = disp_donttouch;
f8eba3c6
TT
9861 b->addr_string = xstrdup (addr_string_start);
9862 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
9863 b->loc->length = length;
9864
f8eba3c6 9865 do_cleanups (cleanup_bkpt);
f1310107
TJB
9866
9867 mention (b);
8d3788bd 9868 observer_notify_breakpoint_created (b);
f1310107
TJB
9869 update_global_location_list (1);
9870}
9871
4a64f543
MS
9872/* Return non-zero if EXP is verified as constant. Returned zero
9873 means EXP is variable. Also the constant detection may fail for
9874 some constant expressions and in such case still falsely return
9875 zero. */
2e6e3d9c 9876
65d79d4b
SDJ
9877static int
9878watchpoint_exp_is_const (const struct expression *exp)
9879{
9880 int i = exp->nelts;
9881
9882 while (i > 0)
9883 {
9884 int oplenp, argsp;
9885
9886 /* We are only interested in the descriptor of each element. */
9887 operator_length (exp, i, &oplenp, &argsp);
9888 i -= oplenp;
9889
9890 switch (exp->elts[i].opcode)
9891 {
9892 case BINOP_ADD:
9893 case BINOP_SUB:
9894 case BINOP_MUL:
9895 case BINOP_DIV:
9896 case BINOP_REM:
9897 case BINOP_MOD:
9898 case BINOP_LSH:
9899 case BINOP_RSH:
9900 case BINOP_LOGICAL_AND:
9901 case BINOP_LOGICAL_OR:
9902 case BINOP_BITWISE_AND:
9903 case BINOP_BITWISE_IOR:
9904 case BINOP_BITWISE_XOR:
9905 case BINOP_EQUAL:
9906 case BINOP_NOTEQUAL:
9907 case BINOP_LESS:
9908 case BINOP_GTR:
9909 case BINOP_LEQ:
9910 case BINOP_GEQ:
9911 case BINOP_REPEAT:
9912 case BINOP_COMMA:
9913 case BINOP_EXP:
9914 case BINOP_MIN:
9915 case BINOP_MAX:
9916 case BINOP_INTDIV:
9917 case BINOP_CONCAT:
9918 case BINOP_IN:
9919 case BINOP_RANGE:
9920 case TERNOP_COND:
9921 case TERNOP_SLICE:
9922 case TERNOP_SLICE_COUNT:
9923
9924 case OP_LONG:
9925 case OP_DOUBLE:
9926 case OP_DECFLOAT:
9927 case OP_LAST:
9928 case OP_COMPLEX:
9929 case OP_STRING:
9930 case OP_BITSTRING:
9931 case OP_ARRAY:
9932 case OP_TYPE:
9933 case OP_NAME:
9934 case OP_OBJC_NSSTRING:
9935
9936 case UNOP_NEG:
9937 case UNOP_LOGICAL_NOT:
9938 case UNOP_COMPLEMENT:
9939 case UNOP_ADDR:
9940 case UNOP_HIGH:
aeaa2474 9941 case UNOP_CAST:
4a64f543
MS
9942 /* Unary, binary and ternary operators: We have to check
9943 their operands. If they are constant, then so is the
9944 result of that operation. For instance, if A and B are
9945 determined to be constants, then so is "A + B".
9946
9947 UNOP_IND is one exception to the rule above, because the
9948 value of *ADDR is not necessarily a constant, even when
9949 ADDR is. */
65d79d4b
SDJ
9950 break;
9951
9952 case OP_VAR_VALUE:
9953 /* Check whether the associated symbol is a constant.
4a64f543 9954
65d79d4b 9955 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
9956 possible that a buggy compiler could mark a variable as
9957 constant even when it is not, and TYPE_CONST would return
9958 true in this case, while SYMBOL_CLASS wouldn't.
9959
9960 We also have to check for function symbols because they
9961 are always constant. */
65d79d4b
SDJ
9962 {
9963 struct symbol *s = exp->elts[i + 2].symbol;
9964
9965 if (SYMBOL_CLASS (s) != LOC_BLOCK
9966 && SYMBOL_CLASS (s) != LOC_CONST
9967 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9968 return 0;
9969 break;
9970 }
9971
9972 /* The default action is to return 0 because we are using
9973 the optimistic approach here: If we don't know something,
9974 then it is not a constant. */
9975 default:
9976 return 0;
9977 }
9978 }
9979
9980 return 1;
9981}
9982
3a5c3e22
PA
9983/* Implement the "dtor" breakpoint_ops method for watchpoints. */
9984
9985static void
9986dtor_watchpoint (struct breakpoint *self)
9987{
9988 struct watchpoint *w = (struct watchpoint *) self;
9989
9990 xfree (w->cond_exp);
9991 xfree (w->exp);
9992 xfree (w->exp_string);
9993 xfree (w->exp_string_reparse);
9994 value_free (w->val);
9995
9996 base_breakpoint_ops.dtor (self);
9997}
9998
348d480f
PA
9999/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10000
10001static void
10002re_set_watchpoint (struct breakpoint *b)
10003{
3a5c3e22
PA
10004 struct watchpoint *w = (struct watchpoint *) b;
10005
348d480f
PA
10006 /* Watchpoint can be either on expression using entirely global
10007 variables, or it can be on local variables.
10008
10009 Watchpoints of the first kind are never auto-deleted, and even
10010 persist across program restarts. Since they can use variables
10011 from shared libraries, we need to reparse expression as libraries
10012 are loaded and unloaded.
10013
10014 Watchpoints on local variables can also change meaning as result
10015 of solib event. For example, if a watchpoint uses both a local
10016 and a global variables in expression, it's a local watchpoint,
10017 but unloading of a shared library will make the expression
10018 invalid. This is not a very common use case, but we still
10019 re-evaluate expression, to avoid surprises to the user.
10020
10021 Note that for local watchpoints, we re-evaluate it only if
10022 watchpoints frame id is still valid. If it's not, it means the
10023 watchpoint is out of scope and will be deleted soon. In fact,
10024 I'm not sure we'll ever be called in this case.
10025
10026 If a local watchpoint's frame id is still valid, then
3a5c3e22 10027 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10028
3a5c3e22
PA
10029 Don't do anything about disabled watchpoints, since they will be
10030 reevaluated again when enabled. */
10031 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10032}
10033
77b06cd7
TJB
10034/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10035
10036static int
10037insert_watchpoint (struct bp_location *bl)
10038{
3a5c3e22
PA
10039 struct watchpoint *w = (struct watchpoint *) bl->owner;
10040 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10041
10042 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10043 w->cond_exp);
77b06cd7
TJB
10044}
10045
10046/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10047
10048static int
10049remove_watchpoint (struct bp_location *bl)
10050{
3a5c3e22
PA
10051 struct watchpoint *w = (struct watchpoint *) bl->owner;
10052 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10053
10054 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10055 w->cond_exp);
e09342b5
TJB
10056}
10057
e09342b5 10058static int
348d480f 10059breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10060 struct address_space *aspace, CORE_ADDR bp_addr,
10061 const struct target_waitstatus *ws)
e09342b5 10062{
348d480f 10063 struct breakpoint *b = bl->owner;
3a5c3e22 10064 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10065
348d480f
PA
10066 /* Continuable hardware watchpoints are treated as non-existent if the
10067 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10068 some data address). Otherwise gdb won't stop on a break instruction
10069 in the code (not from a breakpoint) when a hardware watchpoint has
10070 been defined. Also skip watchpoints which we know did not trigger
10071 (did not match the data address). */
10072 if (is_hardware_watchpoint (b)
3a5c3e22 10073 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10074 return 0;
9c06b0b4 10075
348d480f 10076 return 1;
9c06b0b4
TJB
10077}
10078
348d480f
PA
10079static void
10080check_status_watchpoint (bpstat bs)
9c06b0b4 10081{
348d480f 10082 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10083
348d480f 10084 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10085}
10086
10087/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10088 hardware watchpoints. */
9c06b0b4
TJB
10089
10090static int
348d480f 10091resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10092{
3a5c3e22
PA
10093 struct watchpoint *w = (struct watchpoint *) bl->owner;
10094 int length = w->exact? 1 : bl->length;
348d480f
PA
10095
10096 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10097}
10098
10099/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10100 hardware watchpoints. */
9c06b0b4
TJB
10101
10102static int
348d480f 10103works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10104{
efa80663
PA
10105 /* Read and access watchpoints only work with hardware support. */
10106 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10107}
10108
9c06b0b4 10109static enum print_stop_action
348d480f 10110print_it_watchpoint (bpstat bs)
9c06b0b4 10111{
348d480f
PA
10112 struct cleanup *old_chain;
10113 struct breakpoint *b;
10114 const struct bp_location *bl;
f99d8bf4 10115 struct ui_file *stb;
348d480f 10116 enum print_stop_action result;
3a5c3e22 10117 struct watchpoint *w;
79a45e25 10118 struct ui_out *uiout = current_uiout;
348d480f
PA
10119
10120 gdb_assert (bs->bp_location_at != NULL);
10121
10122 bl = bs->bp_location_at;
10123 b = bs->breakpoint_at;
3a5c3e22 10124 w = (struct watchpoint *) b;
348d480f 10125
f99d8bf4
PA
10126 stb = mem_fileopen ();
10127 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10128
10129 switch (b->type)
10130 {
348d480f 10131 case bp_watchpoint:
9c06b0b4
TJB
10132 case bp_hardware_watchpoint:
10133 annotate_watchpoint (b->number);
10134 if (ui_out_is_mi_like_p (uiout))
10135 ui_out_field_string
10136 (uiout, "reason",
10137 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10138 mention (b);
10139 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10140 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10141 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10142 ui_out_field_stream (uiout, "old", stb);
10143 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10144 watchpoint_value_print (w->val, stb);
348d480f
PA
10145 ui_out_field_stream (uiout, "new", stb);
10146 ui_out_text (uiout, "\n");
10147 /* More than one watchpoint may have been triggered. */
10148 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10149 break;
10150
10151 case bp_read_watchpoint:
10152 if (ui_out_is_mi_like_p (uiout))
10153 ui_out_field_string
10154 (uiout, "reason",
10155 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10156 mention (b);
10157 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10158 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10159 watchpoint_value_print (w->val, stb);
348d480f
PA
10160 ui_out_field_stream (uiout, "value", stb);
10161 ui_out_text (uiout, "\n");
10162 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10163 break;
10164
10165 case bp_access_watchpoint:
348d480f
PA
10166 if (bs->old_val != NULL)
10167 {
10168 annotate_watchpoint (b->number);
10169 if (ui_out_is_mi_like_p (uiout))
10170 ui_out_field_string
10171 (uiout, "reason",
10172 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10173 mention (b);
10174 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10175 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10176 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10177 ui_out_field_stream (uiout, "old", stb);
10178 ui_out_text (uiout, "\nNew value = ");
10179 }
10180 else
10181 {
10182 mention (b);
10183 if (ui_out_is_mi_like_p (uiout))
10184 ui_out_field_string
10185 (uiout, "reason",
10186 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10187 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10188 ui_out_text (uiout, "\nValue = ");
10189 }
f99d8bf4 10190 watchpoint_value_print (w->val, stb);
348d480f
PA
10191 ui_out_field_stream (uiout, "new", stb);
10192 ui_out_text (uiout, "\n");
10193 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10194 break;
10195 default:
348d480f 10196 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10197 }
10198
348d480f
PA
10199 do_cleanups (old_chain);
10200 return result;
10201}
10202
10203/* Implement the "print_mention" breakpoint_ops method for hardware
10204 watchpoints. */
10205
10206static void
10207print_mention_watchpoint (struct breakpoint *b)
10208{
10209 struct cleanup *ui_out_chain;
3a5c3e22 10210 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10211 struct ui_out *uiout = current_uiout;
348d480f
PA
10212
10213 switch (b->type)
10214 {
10215 case bp_watchpoint:
10216 ui_out_text (uiout, "Watchpoint ");
10217 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10218 break;
10219 case bp_hardware_watchpoint:
10220 ui_out_text (uiout, "Hardware watchpoint ");
10221 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10222 break;
10223 case bp_read_watchpoint:
10224 ui_out_text (uiout, "Hardware read watchpoint ");
10225 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10226 break;
10227 case bp_access_watchpoint:
10228 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10229 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10230 break;
10231 default:
10232 internal_error (__FILE__, __LINE__,
10233 _("Invalid hardware watchpoint type."));
10234 }
10235
10236 ui_out_field_int (uiout, "number", b->number);
10237 ui_out_text (uiout, ": ");
3a5c3e22 10238 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10239 do_cleanups (ui_out_chain);
10240}
10241
10242/* Implement the "print_recreate" breakpoint_ops method for
10243 watchpoints. */
10244
10245static void
10246print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10247{
3a5c3e22
PA
10248 struct watchpoint *w = (struct watchpoint *) b;
10249
348d480f
PA
10250 switch (b->type)
10251 {
10252 case bp_watchpoint:
10253 case bp_hardware_watchpoint:
10254 fprintf_unfiltered (fp, "watch");
10255 break;
10256 case bp_read_watchpoint:
10257 fprintf_unfiltered (fp, "rwatch");
10258 break;
10259 case bp_access_watchpoint:
10260 fprintf_unfiltered (fp, "awatch");
10261 break;
10262 default:
10263 internal_error (__FILE__, __LINE__,
10264 _("Invalid watchpoint type."));
10265 }
10266
3a5c3e22 10267 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10268 print_recreate_thread (b, fp);
348d480f
PA
10269}
10270
10271/* The breakpoint_ops structure to be used in hardware watchpoints. */
10272
2060206e 10273static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10274
10275/* Implement the "insert" breakpoint_ops method for
10276 masked hardware watchpoints. */
10277
10278static int
10279insert_masked_watchpoint (struct bp_location *bl)
10280{
3a5c3e22
PA
10281 struct watchpoint *w = (struct watchpoint *) bl->owner;
10282
10283 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10284 bl->watchpoint_type);
10285}
10286
10287/* Implement the "remove" breakpoint_ops method for
10288 masked hardware watchpoints. */
10289
10290static int
10291remove_masked_watchpoint (struct bp_location *bl)
10292{
3a5c3e22
PA
10293 struct watchpoint *w = (struct watchpoint *) bl->owner;
10294
10295 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10296 bl->watchpoint_type);
10297}
10298
10299/* Implement the "resources_needed" breakpoint_ops method for
10300 masked hardware watchpoints. */
10301
10302static int
10303resources_needed_masked_watchpoint (const struct bp_location *bl)
10304{
3a5c3e22
PA
10305 struct watchpoint *w = (struct watchpoint *) bl->owner;
10306
10307 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10308}
10309
10310/* Implement the "works_in_software_mode" breakpoint_ops method for
10311 masked hardware watchpoints. */
10312
10313static int
10314works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10315{
10316 return 0;
10317}
10318
10319/* Implement the "print_it" breakpoint_ops method for
10320 masked hardware watchpoints. */
10321
10322static enum print_stop_action
10323print_it_masked_watchpoint (bpstat bs)
10324{
10325 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10326 struct ui_out *uiout = current_uiout;
348d480f
PA
10327
10328 /* Masked watchpoints have only one location. */
10329 gdb_assert (b->loc && b->loc->next == NULL);
10330
10331 switch (b->type)
10332 {
10333 case bp_hardware_watchpoint:
10334 annotate_watchpoint (b->number);
10335 if (ui_out_is_mi_like_p (uiout))
10336 ui_out_field_string
10337 (uiout, "reason",
10338 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10339 break;
10340
10341 case bp_read_watchpoint:
10342 if (ui_out_is_mi_like_p (uiout))
10343 ui_out_field_string
10344 (uiout, "reason",
10345 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10346 break;
10347
10348 case bp_access_watchpoint:
10349 if (ui_out_is_mi_like_p (uiout))
10350 ui_out_field_string
10351 (uiout, "reason",
10352 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10353 break;
10354 default:
10355 internal_error (__FILE__, __LINE__,
10356 _("Invalid hardware watchpoint type."));
10357 }
10358
10359 mention (b);
9c06b0b4
TJB
10360 ui_out_text (uiout, _("\n\
10361Check the underlying instruction at PC for the memory\n\
10362address and value which triggered this watchpoint.\n"));
10363 ui_out_text (uiout, "\n");
10364
10365 /* More than one watchpoint may have been triggered. */
10366 return PRINT_UNKNOWN;
10367}
10368
10369/* Implement the "print_one_detail" breakpoint_ops method for
10370 masked hardware watchpoints. */
10371
10372static void
10373print_one_detail_masked_watchpoint (const struct breakpoint *b,
10374 struct ui_out *uiout)
10375{
3a5c3e22
PA
10376 struct watchpoint *w = (struct watchpoint *) b;
10377
9c06b0b4
TJB
10378 /* Masked watchpoints have only one location. */
10379 gdb_assert (b->loc && b->loc->next == NULL);
10380
10381 ui_out_text (uiout, "\tmask ");
3a5c3e22 10382 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
10383 ui_out_text (uiout, "\n");
10384}
10385
10386/* Implement the "print_mention" breakpoint_ops method for
10387 masked hardware watchpoints. */
10388
10389static void
10390print_mention_masked_watchpoint (struct breakpoint *b)
10391{
3a5c3e22 10392 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10393 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
10394 struct cleanup *ui_out_chain;
10395
10396 switch (b->type)
10397 {
10398 case bp_hardware_watchpoint:
10399 ui_out_text (uiout, "Masked hardware watchpoint ");
10400 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10401 break;
10402 case bp_read_watchpoint:
10403 ui_out_text (uiout, "Masked hardware read watchpoint ");
10404 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10405 break;
10406 case bp_access_watchpoint:
10407 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10408 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10409 break;
10410 default:
10411 internal_error (__FILE__, __LINE__,
10412 _("Invalid hardware watchpoint type."));
10413 }
10414
10415 ui_out_field_int (uiout, "number", b->number);
10416 ui_out_text (uiout, ": ");
3a5c3e22 10417 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
10418 do_cleanups (ui_out_chain);
10419}
10420
10421/* Implement the "print_recreate" breakpoint_ops method for
10422 masked hardware watchpoints. */
10423
10424static void
10425print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10426{
3a5c3e22 10427 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10428 char tmp[40];
10429
10430 switch (b->type)
10431 {
10432 case bp_hardware_watchpoint:
10433 fprintf_unfiltered (fp, "watch");
10434 break;
10435 case bp_read_watchpoint:
10436 fprintf_unfiltered (fp, "rwatch");
10437 break;
10438 case bp_access_watchpoint:
10439 fprintf_unfiltered (fp, "awatch");
10440 break;
10441 default:
10442 internal_error (__FILE__, __LINE__,
10443 _("Invalid hardware watchpoint type."));
10444 }
10445
3a5c3e22
PA
10446 sprintf_vma (tmp, w->hw_wp_mask);
10447 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10448 print_recreate_thread (b, fp);
9c06b0b4
TJB
10449}
10450
10451/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10452
2060206e 10453static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10454
10455/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10456
10457static int
10458is_masked_watchpoint (const struct breakpoint *b)
10459{
10460 return b->ops == &masked_watchpoint_breakpoint_ops;
10461}
10462
53a5351d
JM
10463/* accessflag: hw_write: watch write,
10464 hw_read: watch read,
10465 hw_access: watch access (read or write) */
c906108c 10466static void
84f4c1fe
PM
10467watch_command_1 (char *arg, int accessflag, int from_tty,
10468 int just_location, int internal)
c906108c 10469{
a9634178 10470 volatile struct gdb_exception e;
d983da9c 10471 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 10472 struct expression *exp;
60e1c644 10473 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10474 struct value *val, *mark, *result;
c906108c 10475 struct frame_info *frame;
c906108c
SS
10476 char *exp_start = NULL;
10477 char *exp_end = NULL;
9c06b0b4
TJB
10478 char *tok, *end_tok;
10479 int toklen = -1;
c906108c
SS
10480 char *cond_start = NULL;
10481 char *cond_end = NULL;
c906108c 10482 enum bptype bp_type;
37e4754d 10483 int thread = -1;
0cf6dd15 10484 int pc = 0;
9c06b0b4
TJB
10485 /* Flag to indicate whether we are going to use masks for
10486 the hardware watchpoint. */
10487 int use_mask = 0;
10488 CORE_ADDR mask = 0;
3a5c3e22 10489 struct watchpoint *w;
c906108c 10490
37e4754d
LM
10491 /* Make sure that we actually have parameters to parse. */
10492 if (arg != NULL && arg[0] != '\0')
10493 {
9c06b0b4 10494 char *value_start;
37e4754d 10495
9c06b0b4
TJB
10496 /* Look for "parameter value" pairs at the end
10497 of the arguments string. */
10498 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
10499 {
10500 /* Skip whitespace at the end of the argument list. */
10501 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10502 tok--;
10503
10504 /* Find the beginning of the last token.
10505 This is the value of the parameter. */
10506 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10507 tok--;
10508 value_start = tok + 1;
10509
10510 /* Skip whitespace. */
10511 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10512 tok--;
10513
10514 end_tok = tok;
10515
10516 /* Find the beginning of the second to last token.
10517 This is the parameter itself. */
10518 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10519 tok--;
10520 tok++;
10521 toklen = end_tok - tok + 1;
10522
10523 if (toklen == 6 && !strncmp (tok, "thread", 6))
10524 {
10525 /* At this point we've found a "thread" token, which means
10526 the user is trying to set a watchpoint that triggers
10527 only in a specific thread. */
10528 char *endp;
37e4754d 10529
9c06b0b4
TJB
10530 if (thread != -1)
10531 error(_("You can specify only one thread."));
37e4754d 10532
9c06b0b4
TJB
10533 /* Extract the thread ID from the next token. */
10534 thread = strtol (value_start, &endp, 0);
37e4754d 10535
9c06b0b4
TJB
10536 /* Check if the user provided a valid numeric value for the
10537 thread ID. */
10538 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10539 error (_("Invalid thread ID specification %s."), value_start);
10540
10541 /* Check if the thread actually exists. */
10542 if (!valid_thread_id (thread))
10543 error (_("Unknown thread %d."), thread);
10544 }
10545 else if (toklen == 4 && !strncmp (tok, "mask", 4))
10546 {
10547 /* We've found a "mask" token, which means the user wants to
10548 create a hardware watchpoint that is going to have the mask
10549 facility. */
10550 struct value *mask_value, *mark;
37e4754d 10551
9c06b0b4
TJB
10552 if (use_mask)
10553 error(_("You can specify only one mask."));
37e4754d 10554
9c06b0b4 10555 use_mask = just_location = 1;
37e4754d 10556
9c06b0b4
TJB
10557 mark = value_mark ();
10558 mask_value = parse_to_comma_and_eval (&value_start);
10559 mask = value_as_address (mask_value);
10560 value_free_to_mark (mark);
10561 }
10562 else
10563 /* We didn't recognize what we found. We should stop here. */
10564 break;
37e4754d 10565
9c06b0b4
TJB
10566 /* Truncate the string and get rid of the "parameter value" pair before
10567 the arguments string is parsed by the parse_exp_1 function. */
10568 *tok = '\0';
10569 }
37e4754d
LM
10570 }
10571
10572 /* Parse the rest of the arguments. */
c906108c
SS
10573 innermost_block = NULL;
10574 exp_start = arg;
10575 exp = parse_exp_1 (&arg, 0, 0);
10576 exp_end = arg;
fa8a61dc
TT
10577 /* Remove trailing whitespace from the expression before saving it.
10578 This makes the eventual display of the expression string a bit
10579 prettier. */
10580 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10581 --exp_end;
10582
65d79d4b
SDJ
10583 /* Checking if the expression is not constant. */
10584 if (watchpoint_exp_is_const (exp))
10585 {
10586 int len;
10587
10588 len = exp_end - exp_start;
10589 while (len > 0 && isspace (exp_start[len - 1]))
10590 len--;
10591 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10592 }
10593
c906108c
SS
10594 exp_valid_block = innermost_block;
10595 mark = value_mark ();
a1442452 10596 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
10597
10598 if (just_location)
10599 {
9c06b0b4
TJB
10600 int ret;
10601
06a64a0b 10602 exp_valid_block = NULL;
a1442452 10603 val = value_addr (result);
06a64a0b
TT
10604 release_value (val);
10605 value_free_to_mark (mark);
9c06b0b4
TJB
10606
10607 if (use_mask)
10608 {
10609 ret = target_masked_watch_num_registers (value_as_address (val),
10610 mask);
10611 if (ret == -1)
10612 error (_("This target does not support masked watchpoints."));
10613 else if (ret == -2)
10614 error (_("Invalid mask or memory region."));
10615 }
06a64a0b
TT
10616 }
10617 else if (val != NULL)
fa4727a6 10618 release_value (val);
c906108c 10619
e9cafbcc
TT
10620 tok = skip_spaces (arg);
10621 end_tok = skip_to_space (tok);
c906108c
SS
10622
10623 toklen = end_tok - tok;
10624 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10625 {
2d134ed3
PA
10626 struct expression *cond;
10627
60e1c644 10628 innermost_block = NULL;
c906108c
SS
10629 tok = cond_start = end_tok + 1;
10630 cond = parse_exp_1 (&tok, 0, 0);
60e1c644
PA
10631
10632 /* The watchpoint expression may not be local, but the condition
10633 may still be. E.g.: `watch global if local > 0'. */
10634 cond_exp_valid_block = innermost_block;
10635
2d134ed3 10636 xfree (cond);
c906108c
SS
10637 cond_end = tok;
10638 }
10639 if (*tok)
8a3fe4f8 10640 error (_("Junk at end of command."));
c906108c 10641
53a5351d 10642 if (accessflag == hw_read)
c5aa993b 10643 bp_type = bp_read_watchpoint;
53a5351d 10644 else if (accessflag == hw_access)
c5aa993b
JM
10645 bp_type = bp_access_watchpoint;
10646 else
10647 bp_type = bp_hardware_watchpoint;
c906108c 10648
d983da9c 10649 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
10650
10651 /* If the expression is "local", then set up a "watchpoint scope"
10652 breakpoint at the point where we've left the scope of the watchpoint
10653 expression. Create the scope breakpoint before the watchpoint, so
10654 that we will encounter it first in bpstat_stop_status. */
60e1c644 10655 if (exp_valid_block && frame)
d983da9c 10656 {
edb3359d
DJ
10657 if (frame_id_p (frame_unwind_caller_id (frame)))
10658 {
10659 scope_breakpoint
a6d9a66e
UW
10660 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
10661 frame_unwind_caller_pc (frame),
06edf0c0
PA
10662 bp_watchpoint_scope,
10663 &momentary_breakpoint_ops);
d983da9c 10664
edb3359d 10665 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10666
edb3359d
DJ
10667 /* Automatically delete the breakpoint when it hits. */
10668 scope_breakpoint->disposition = disp_del;
d983da9c 10669
edb3359d
DJ
10670 /* Only break in the proper frame (help with recursion). */
10671 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 10672
edb3359d 10673 /* Set the address at which we will stop. */
a6d9a66e
UW
10674 scope_breakpoint->loc->gdbarch
10675 = frame_unwind_caller_arch (frame);
edb3359d
DJ
10676 scope_breakpoint->loc->requested_address
10677 = frame_unwind_caller_pc (frame);
10678 scope_breakpoint->loc->address
a6d9a66e
UW
10679 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10680 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10681 scope_breakpoint->type);
10682 }
d983da9c
DJ
10683 }
10684
c906108c 10685 /* Now set up the breakpoint. */
3a5c3e22
PA
10686
10687 w = XCNEW (struct watchpoint);
10688 b = &w->base;
348d480f 10689 if (use_mask)
3a5c3e22
PA
10690 init_raw_breakpoint_without_location (b, NULL, bp_type,
10691 &masked_watchpoint_breakpoint_ops);
348d480f 10692 else
3a5c3e22
PA
10693 init_raw_breakpoint_without_location (b, NULL, bp_type,
10694 &watchpoint_breakpoint_ops);
37e4754d 10695 b->thread = thread;
b5de0fa7 10696 b->disposition = disp_donttouch;
348d480f 10697 b->pspace = current_program_space;
3a5c3e22
PA
10698 w->exp = exp;
10699 w->exp_valid_block = exp_valid_block;
10700 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10701 if (just_location)
10702 {
10703 struct type *t = value_type (val);
10704 CORE_ADDR addr = value_as_address (val);
10705 char *name;
10706
10707 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
10708 name = type_to_string (t);
10709
3a5c3e22 10710 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 10711 core_addr_to_string (addr));
06a64a0b
TT
10712 xfree (name);
10713
3a5c3e22 10714 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
10715 (int) (exp_end - exp_start), exp_start);
10716
06a64a0b
TT
10717 /* The above expression is in C. */
10718 b->language = language_c;
10719 }
10720 else
3a5c3e22 10721 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10722
10723 if (use_mask)
10724 {
3a5c3e22 10725 w->hw_wp_mask = mask;
9c06b0b4
TJB
10726 }
10727 else
10728 {
3a5c3e22
PA
10729 w->val = val;
10730 w->val_valid = 1;
9c06b0b4 10731 }
77b06cd7 10732
c906108c
SS
10733 if (cond_start)
10734 b->cond_string = savestring (cond_start, cond_end - cond_start);
10735 else
10736 b->cond_string = 0;
c5aa993b 10737
c906108c 10738 if (frame)
f6bc2008 10739 {
3a5c3e22
PA
10740 w->watchpoint_frame = get_frame_id (frame);
10741 w->watchpoint_thread = inferior_ptid;
f6bc2008 10742 }
c906108c 10743 else
f6bc2008 10744 {
3a5c3e22
PA
10745 w->watchpoint_frame = null_frame_id;
10746 w->watchpoint_thread = null_ptid;
f6bc2008 10747 }
c906108c 10748
d983da9c 10749 if (scope_breakpoint != NULL)
c906108c 10750 {
d983da9c
DJ
10751 /* The scope breakpoint is related to the watchpoint. We will
10752 need to act on them together. */
10753 b->related_breakpoint = scope_breakpoint;
10754 scope_breakpoint->related_breakpoint = b;
c906108c 10755 }
d983da9c 10756
06a64a0b
TT
10757 if (!just_location)
10758 value_free_to_mark (mark);
2d134ed3 10759
a9634178
TJB
10760 TRY_CATCH (e, RETURN_MASK_ALL)
10761 {
10762 /* Finally update the new watchpoint. This creates the locations
10763 that should be inserted. */
3a5c3e22 10764 update_watchpoint (w, 1);
a9634178
TJB
10765 }
10766 if (e.reason < 0)
10767 {
10768 delete_breakpoint (b);
10769 throw_exception (e);
10770 }
10771
3ea46bff 10772 install_breakpoint (internal, b, 1);
c906108c
SS
10773}
10774
e09342b5 10775/* Return count of debug registers needed to watch the given expression.
e09342b5 10776 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10777
c906108c 10778static int
a9634178 10779can_use_hardware_watchpoint (struct value *v)
c906108c
SS
10780{
10781 int found_memory_cnt = 0;
2e70b7b9 10782 struct value *head = v;
c906108c
SS
10783
10784 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10785 if (!can_use_hw_watchpoints)
c906108c 10786 return 0;
c5aa993b 10787
5c44784c
JM
10788 /* Make sure that the value of the expression depends only upon
10789 memory contents, and values computed from them within GDB. If we
10790 find any register references or function calls, we can't use a
10791 hardware watchpoint.
10792
10793 The idea here is that evaluating an expression generates a series
10794 of values, one holding the value of every subexpression. (The
10795 expression a*b+c has five subexpressions: a, b, a*b, c, and
10796 a*b+c.) GDB's values hold almost enough information to establish
10797 the criteria given above --- they identify memory lvalues,
10798 register lvalues, computed values, etcetera. So we can evaluate
10799 the expression, and then scan the chain of values that leaves
10800 behind to decide whether we can detect any possible change to the
10801 expression's final value using only hardware watchpoints.
10802
10803 However, I don't think that the values returned by inferior
10804 function calls are special in any way. So this function may not
10805 notice that an expression involving an inferior function call
10806 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 10807 for (; v; v = value_next (v))
c906108c 10808 {
5c44784c 10809 if (VALUE_LVAL (v) == lval_memory)
c906108c 10810 {
8464be76
DJ
10811 if (v != head && value_lazy (v))
10812 /* A lazy memory lvalue in the chain is one that GDB never
10813 needed to fetch; we either just used its address (e.g.,
10814 `a' in `a.b') or we never needed it at all (e.g., `a'
10815 in `a,b'). This doesn't apply to HEAD; if that is
10816 lazy then it was not readable, but watch it anyway. */
5c44784c 10817 ;
53a5351d 10818 else
5c44784c
JM
10819 {
10820 /* Ahh, memory we actually used! Check if we can cover
10821 it with hardware watchpoints. */
df407dfe 10822 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10823
10824 /* We only watch structs and arrays if user asked for it
10825 explicitly, never if they just happen to appear in a
10826 middle of some value chain. */
10827 if (v == head
10828 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10829 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10830 {
42ae5230 10831 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10832 int len;
10833 int num_regs;
10834
a9634178 10835 len = (target_exact_watchpoints
e09342b5
TJB
10836 && is_scalar_type_recursive (vtype))?
10837 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10838
e09342b5
TJB
10839 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10840 if (!num_regs)
2e70b7b9
MS
10841 return 0;
10842 else
e09342b5 10843 found_memory_cnt += num_regs;
2e70b7b9 10844 }
5c44784c 10845 }
c5aa993b 10846 }
5086187c
AC
10847 else if (VALUE_LVAL (v) != not_lval
10848 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10849 return 0; /* These are values from the history (e.g., $1). */
5086187c 10850 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10851 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10852 }
10853
10854 /* The expression itself looks suitable for using a hardware
10855 watchpoint, but give the target machine a chance to reject it. */
10856 return found_memory_cnt;
10857}
10858
8b93c638 10859void
84f4c1fe 10860watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 10861{
84f4c1fe 10862 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10863}
10864
10865/* A helper function that looks for an argument at the start of a
10866 string. The argument must also either be at the end of the string,
10867 or be followed by whitespace. Returns 1 if it finds the argument,
10868 0 otherwise. If the argument is found, it updates *STR. */
10869
10870static int
10871check_for_argument (char **str, char *arg, int arg_len)
10872{
10873 if (strncmp (*str, arg, arg_len) == 0
10874 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
10875 {
10876 *str += arg_len;
10877 return 1;
10878 }
10879 return 0;
10880}
10881
10882/* A helper function that looks for the "-location" argument and then
10883 calls watch_command_1. */
10884
10885static void
10886watch_maybe_just_location (char *arg, int accessflag, int from_tty)
10887{
10888 int just_location = 0;
10889
10890 if (arg
10891 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10892 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10893 {
e9cafbcc 10894 arg = skip_spaces (arg);
06a64a0b
TT
10895 just_location = 1;
10896 }
10897
84f4c1fe 10898 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 10899}
8926118c 10900
c5aa993b 10901static void
fba45db2 10902watch_command (char *arg, int from_tty)
c906108c 10903{
06a64a0b 10904 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10905}
10906
8b93c638 10907void
84f4c1fe 10908rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 10909{
84f4c1fe 10910 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10911}
8926118c 10912
c5aa993b 10913static void
fba45db2 10914rwatch_command (char *arg, int from_tty)
c906108c 10915{
06a64a0b 10916 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10917}
10918
8b93c638 10919void
84f4c1fe 10920awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 10921{
84f4c1fe 10922 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10923}
8926118c 10924
c5aa993b 10925static void
fba45db2 10926awatch_command (char *arg, int from_tty)
c906108c 10927{
06a64a0b 10928 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10929}
c906108c 10930\f
c5aa993b 10931
43ff13b4 10932/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
10933 because it uses the mechanisms of breakpoints. */
10934
bfec99b2
PA
10935struct until_break_command_continuation_args
10936{
10937 struct breakpoint *breakpoint;
10938 struct breakpoint *breakpoint2;
186c406b 10939 int thread_num;
bfec99b2
PA
10940};
10941
43ff13b4 10942/* This function is called by fetch_inferior_event via the
4a64f543 10943 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 10944 care of cleaning up the temporary breakpoints set up by the until
4a64f543 10945 command. */
c2c6d25f 10946static void
fa4cd53f 10947until_break_command_continuation (void *arg, int err)
43ff13b4 10948{
bfec99b2
PA
10949 struct until_break_command_continuation_args *a = arg;
10950
10951 delete_breakpoint (a->breakpoint);
10952 if (a->breakpoint2)
10953 delete_breakpoint (a->breakpoint2);
186c406b 10954 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
10955}
10956
c906108c 10957void
ae66c1fc 10958until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
10959{
10960 struct symtabs_and_lines sals;
10961 struct symtab_and_line sal;
8556afb4
PA
10962 struct frame_info *frame;
10963 struct gdbarch *frame_gdbarch;
10964 struct frame_id stack_frame_id;
10965 struct frame_id caller_frame_id;
c906108c 10966 struct breakpoint *breakpoint;
f107f563 10967 struct breakpoint *breakpoint2 = NULL;
c906108c 10968 struct cleanup *old_chain;
186c406b
TT
10969 int thread;
10970 struct thread_info *tp;
c906108c
SS
10971
10972 clear_proceed_status ();
10973
10974 /* Set a breakpoint where the user wants it and at return from
4a64f543 10975 this function. */
c5aa993b 10976
1bfeeb0f 10977 if (last_displayed_sal_is_valid ())
f8eba3c6 10978 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 10979 get_last_displayed_symtab (),
f8eba3c6 10980 get_last_displayed_line ());
c906108c 10981 else
f8eba3c6
TT
10982 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
10983 (struct symtab *) NULL, 0);
c5aa993b 10984
c906108c 10985 if (sals.nelts != 1)
8a3fe4f8 10986 error (_("Couldn't get information on specified line."));
c5aa993b 10987
c906108c 10988 sal = sals.sals[0];
4a64f543 10989 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 10990
c906108c 10991 if (*arg)
8a3fe4f8 10992 error (_("Junk at end of arguments."));
c5aa993b 10993
c906108c 10994 resolve_sal_pc (&sal);
c5aa993b 10995
186c406b
TT
10996 tp = inferior_thread ();
10997 thread = tp->num;
10998
883bc8d1
PA
10999 old_chain = make_cleanup (null_cleanup, NULL);
11000
8556afb4
PA
11001 /* Note linespec handling above invalidates the frame chain.
11002 Installing a breakpoint also invalidates the frame chain (as it
11003 may need to switch threads), so do any frame handling before
11004 that. */
11005
11006 frame = get_selected_frame (NULL);
11007 frame_gdbarch = get_frame_arch (frame);
11008 stack_frame_id = get_stack_frame_id (frame);
11009 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11010
ae66c1fc
EZ
11011 /* Keep within the current frame, or in frames called by the current
11012 one. */
edb3359d 11013
883bc8d1 11014 if (frame_id_p (caller_frame_id))
c906108c 11015 {
883bc8d1
PA
11016 struct symtab_and_line sal2;
11017
11018 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11019 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11020 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11021 sal2,
11022 caller_frame_id,
f107f563
VP
11023 bp_until);
11024 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11025
883bc8d1 11026 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11027 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11028 }
c5aa993b 11029
c70a6932
JK
11030 /* set_momentary_breakpoint could invalidate FRAME. */
11031 frame = NULL;
11032
883bc8d1
PA
11033 if (anywhere)
11034 /* If the user told us to continue until a specified location,
11035 we don't specify a frame at which we need to stop. */
11036 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11037 null_frame_id, bp_until);
11038 else
11039 /* Otherwise, specify the selected frame, because we want to stop
11040 only at the very same frame. */
11041 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11042 stack_frame_id, bp_until);
11043 make_cleanup_delete_breakpoint (breakpoint);
11044
a493e3e2 11045 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11046
4a64f543
MS
11047 /* If we are running asynchronously, and proceed call above has
11048 actually managed to start the target, arrange for breakpoints to
11049 be deleted when the target stops. Otherwise, we're already
11050 stopped and delete breakpoints via cleanup chain. */
f107f563 11051
8ea051c5 11052 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11053 {
bfec99b2
PA
11054 struct until_break_command_continuation_args *args;
11055 args = xmalloc (sizeof (*args));
f107f563 11056
bfec99b2
PA
11057 args->breakpoint = breakpoint;
11058 args->breakpoint2 = breakpoint2;
186c406b 11059 args->thread_num = thread;
f107f563
VP
11060
11061 discard_cleanups (old_chain);
95e54da7
PA
11062 add_continuation (inferior_thread (),
11063 until_break_command_continuation, args,
604ead4a 11064 xfree);
f107f563
VP
11065 }
11066 else
c5aa993b 11067 do_cleanups (old_chain);
c906108c 11068}
ae66c1fc 11069
c906108c
SS
11070/* This function attempts to parse an optional "if <cond>" clause
11071 from the arg string. If one is not found, it returns NULL.
c5aa993b 11072
c906108c
SS
11073 Else, it returns a pointer to the condition string. (It does not
11074 attempt to evaluate the string against a particular block.) And,
11075 it updates arg to point to the first character following the parsed
4a64f543 11076 if clause in the arg string. */
53a5351d 11077
c906108c 11078static char *
fba45db2 11079ep_parse_optional_if_clause (char **arg)
c906108c 11080{
c5aa993b
JM
11081 char *cond_string;
11082
11083 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11084 return NULL;
c5aa993b 11085
4a64f543 11086 /* Skip the "if" keyword. */
c906108c 11087 (*arg) += 2;
c5aa993b 11088
c906108c 11089 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11090 condition string. */
e9cafbcc 11091 *arg = skip_spaces (*arg);
c906108c 11092 cond_string = *arg;
c5aa993b 11093
4a64f543
MS
11094 /* Assume that the condition occupies the remainder of the arg
11095 string. */
c906108c 11096 (*arg) += strlen (cond_string);
c5aa993b 11097
c906108c
SS
11098 return cond_string;
11099}
c5aa993b 11100
c906108c
SS
11101/* Commands to deal with catching events, such as signals, exceptions,
11102 process start/exit, etc. */
c5aa993b
JM
11103
11104typedef enum
11105{
44feb3ce
TT
11106 catch_fork_temporary, catch_vfork_temporary,
11107 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11108}
11109catch_fork_kind;
11110
c906108c 11111static void
cc59ec59
MS
11112catch_fork_command_1 (char *arg, int from_tty,
11113 struct cmd_list_element *command)
c906108c 11114{
a6d9a66e 11115 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11116 char *cond_string = NULL;
44feb3ce
TT
11117 catch_fork_kind fork_kind;
11118 int tempflag;
11119
11120 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11121 tempflag = (fork_kind == catch_fork_temporary
11122 || fork_kind == catch_vfork_temporary);
c5aa993b 11123
44feb3ce
TT
11124 if (!arg)
11125 arg = "";
e9cafbcc 11126 arg = skip_spaces (arg);
c5aa993b 11127
c906108c 11128 /* The allowed syntax is:
c5aa993b
JM
11129 catch [v]fork
11130 catch [v]fork if <cond>
11131
4a64f543 11132 First, check if there's an if clause. */
c906108c 11133 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11134
c906108c 11135 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11136 error (_("Junk at end of arguments."));
c5aa993b 11137
c906108c 11138 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11139 and enable reporting of such events. */
c5aa993b
JM
11140 switch (fork_kind)
11141 {
44feb3ce
TT
11142 case catch_fork_temporary:
11143 case catch_fork_permanent:
a6d9a66e 11144 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11145 &catch_fork_breakpoint_ops);
c906108c 11146 break;
44feb3ce
TT
11147 case catch_vfork_temporary:
11148 case catch_vfork_permanent:
a6d9a66e 11149 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11150 &catch_vfork_breakpoint_ops);
c906108c 11151 break;
c5aa993b 11152 default:
8a3fe4f8 11153 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11154 break;
c5aa993b 11155 }
c906108c
SS
11156}
11157
11158static void
cc59ec59
MS
11159catch_exec_command_1 (char *arg, int from_tty,
11160 struct cmd_list_element *command)
c906108c 11161{
b4d90040 11162 struct exec_catchpoint *c;
a6d9a66e 11163 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11164 int tempflag;
c5aa993b 11165 char *cond_string = NULL;
c906108c 11166
44feb3ce
TT
11167 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11168
11169 if (!arg)
11170 arg = "";
e9cafbcc 11171 arg = skip_spaces (arg);
c906108c
SS
11172
11173 /* The allowed syntax is:
c5aa993b
JM
11174 catch exec
11175 catch exec if <cond>
c906108c 11176
4a64f543 11177 First, check if there's an if clause. */
c906108c
SS
11178 cond_string = ep_parse_optional_if_clause (&arg);
11179
11180 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11181 error (_("Junk at end of arguments."));
c906108c 11182
b4d90040
PA
11183 c = XNEW (struct exec_catchpoint);
11184 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11185 &catch_exec_breakpoint_ops);
11186 c->exec_pathname = NULL;
11187
3ea46bff 11188 install_breakpoint (0, &c->base, 1);
c906108c 11189}
c5aa993b 11190
3086aeae 11191static enum print_stop_action
348d480f 11192print_it_exception_catchpoint (bpstat bs)
3086aeae 11193{
79a45e25 11194 struct ui_out *uiout = current_uiout;
348d480f 11195 struct breakpoint *b = bs->breakpoint_at;
ade92717 11196 int bp_temp, bp_throw;
3086aeae 11197
ade92717 11198 annotate_catchpoint (b->number);
3086aeae 11199
ade92717
AR
11200 bp_throw = strstr (b->addr_string, "throw") != NULL;
11201 if (b->loc->address != b->loc->requested_address)
11202 breakpoint_adjustment_warning (b->loc->requested_address,
11203 b->loc->address,
11204 b->number, 1);
df2b6d2d 11205 bp_temp = b->disposition == disp_del;
ade92717
AR
11206 ui_out_text (uiout,
11207 bp_temp ? "Temporary catchpoint "
11208 : "Catchpoint ");
11209 if (!ui_out_is_mi_like_p (uiout))
11210 ui_out_field_int (uiout, "bkptno", b->number);
11211 ui_out_text (uiout,
c0b37c48
AR
11212 bp_throw ? " (exception thrown), "
11213 : " (exception caught), ");
ade92717
AR
11214 if (ui_out_is_mi_like_p (uiout))
11215 {
11216 ui_out_field_string (uiout, "reason",
11217 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11218 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
11219 ui_out_field_int (uiout, "bkptno", b->number);
11220 }
3086aeae
DJ
11221 return PRINT_SRC_AND_LOC;
11222}
11223
11224static void
cc59ec59
MS
11225print_one_exception_catchpoint (struct breakpoint *b,
11226 struct bp_location **last_loc)
3086aeae 11227{
79a45b7d 11228 struct value_print_options opts;
79a45e25 11229 struct ui_out *uiout = current_uiout;
cc59ec59 11230
79a45b7d
TT
11231 get_user_print_options (&opts);
11232 if (opts.addressprint)
3086aeae
DJ
11233 {
11234 annotate_field (4);
604133b5
AR
11235 if (b->loc == NULL || b->loc->shlib_disabled)
11236 ui_out_field_string (uiout, "addr", "<PENDING>");
11237 else
5af949e3
UW
11238 ui_out_field_core_addr (uiout, "addr",
11239 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
11240 }
11241 annotate_field (5);
604133b5 11242 if (b->loc)
a6d9a66e 11243 *last_loc = b->loc;
3086aeae
DJ
11244 if (strstr (b->addr_string, "throw") != NULL)
11245 ui_out_field_string (uiout, "what", "exception throw");
11246 else
11247 ui_out_field_string (uiout, "what", "exception catch");
11248}
11249
11250static void
11251print_mention_exception_catchpoint (struct breakpoint *b)
11252{
79a45e25 11253 struct ui_out *uiout = current_uiout;
ade92717
AR
11254 int bp_temp;
11255 int bp_throw;
11256
df2b6d2d 11257 bp_temp = b->disposition == disp_del;
ade92717
AR
11258 bp_throw = strstr (b->addr_string, "throw") != NULL;
11259 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
11260 : _("Catchpoint "));
11261 ui_out_field_int (uiout, "bkptno", b->number);
11262 ui_out_text (uiout, bp_throw ? _(" (throw)")
11263 : _(" (catch)"));
3086aeae
DJ
11264}
11265
6149aea9
PA
11266/* Implement the "print_recreate" breakpoint_ops method for throw and
11267 catch catchpoints. */
11268
11269static void
4a64f543
MS
11270print_recreate_exception_catchpoint (struct breakpoint *b,
11271 struct ui_file *fp)
6149aea9
PA
11272{
11273 int bp_temp;
11274 int bp_throw;
11275
11276 bp_temp = b->disposition == disp_del;
11277 bp_throw = strstr (b->addr_string, "throw") != NULL;
11278 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
11279 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
d9b3f62e 11280 print_recreate_thread (b, fp);
6149aea9
PA
11281}
11282
2060206e 11283static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
3086aeae
DJ
11284
11285static int
11286handle_gnu_v3_exceptions (int tempflag, char *cond_string,
11287 enum exception_event_kind ex_event, int from_tty)
11288{
604133b5
AR
11289 char *trigger_func_name;
11290
3086aeae 11291 if (ex_event == EX_EVENT_CATCH)
604133b5 11292 trigger_func_name = "__cxa_begin_catch";
3086aeae 11293 else
604133b5 11294 trigger_func_name = "__cxa_throw";
3086aeae 11295
8cdf0e15 11296 create_breakpoint (get_current_arch (),
e7e0cddf 11297 trigger_func_name, cond_string, -1, NULL,
8cdf0e15 11298 0 /* condition and thread are valid. */,
0fb4aa4b 11299 tempflag, bp_breakpoint,
8cdf0e15
VP
11300 0,
11301 AUTO_BOOLEAN_TRUE /* pending */,
11302 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe 11303 1 /* enabled */,
44f238bb
PA
11304 0 /* internal */,
11305 0);
3086aeae 11306
3086aeae
DJ
11307 return 1;
11308}
11309
4a64f543 11310/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
11311
11312static void
fba45db2
KB
11313catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
11314 int tempflag, int from_tty)
c906108c 11315{
c5aa993b 11316 char *cond_string = NULL;
c5aa993b 11317
44feb3ce
TT
11318 if (!arg)
11319 arg = "";
e9cafbcc 11320 arg = skip_spaces (arg);
c5aa993b 11321
c906108c
SS
11322 cond_string = ep_parse_optional_if_clause (&arg);
11323
11324 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11325 error (_("Junk at end of arguments."));
c906108c 11326
059fb39f
PM
11327 if (ex_event != EX_EVENT_THROW
11328 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 11329 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 11330
3086aeae
DJ
11331 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
11332 return;
11333
8a3fe4f8 11334 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
11335}
11336
44feb3ce
TT
11337/* Implementation of "catch catch" command. */
11338
11339static void
11340catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
11341{
11342 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 11343
44feb3ce
TT
11344 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
11345}
11346
11347/* Implementation of "catch throw" command. */
11348
11349static void
11350catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
11351{
11352 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 11353
44feb3ce
TT
11354 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
11355}
11356
9ac4176b 11357void
28010a5d
PA
11358init_ada_exception_breakpoint (struct breakpoint *b,
11359 struct gdbarch *gdbarch,
11360 struct symtab_and_line sal,
11361 char *addr_string,
c0a91b2b 11362 const struct breakpoint_ops *ops,
28010a5d
PA
11363 int tempflag,
11364 int from_tty)
f7f9143b 11365{
f7f9143b
JB
11366 if (from_tty)
11367 {
5af949e3
UW
11368 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11369 if (!loc_gdbarch)
11370 loc_gdbarch = gdbarch;
11371
6c95b8df
PA
11372 describe_other_breakpoints (loc_gdbarch,
11373 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11374 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11375 version for exception catchpoints, because two catchpoints
11376 used for different exception names will use the same address.
11377 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11378 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11379 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11380 the user what type of catchpoint it is. The above is good
11381 enough for now, though. */
11382 }
11383
28010a5d 11384 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
11385
11386 b->enable_state = bp_enabled;
11387 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
11388 b->addr_string = addr_string;
11389 b->language = language_ada;
f7f9143b
JB
11390}
11391
a96d9b2e
SDJ
11392/* Splits the argument using space as delimiter. Returns an xmalloc'd
11393 filter list, or NULL if no filtering is required. */
11394static VEC(int) *
11395catch_syscall_split_args (char *arg)
11396{
11397 VEC(int) *result = NULL;
29d0bb3d 11398 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
11399
11400 while (*arg != '\0')
11401 {
11402 int i, syscall_number;
11403 char *endptr;
11404 char cur_name[128];
11405 struct syscall s;
11406
11407 /* Skip whitespace. */
11408 while (isspace (*arg))
11409 arg++;
11410
11411 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11412 cur_name[i] = arg[i];
11413 cur_name[i] = '\0';
11414 arg += i;
11415
11416 /* Check if the user provided a syscall name or a number. */
11417 syscall_number = (int) strtol (cur_name, &endptr, 0);
11418 if (*endptr == '\0')
bccd0dd2 11419 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
11420 else
11421 {
11422 /* We have a name. Let's check if it's valid and convert it
11423 to a number. */
11424 get_syscall_by_name (cur_name, &s);
11425
11426 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
11427 /* Here we have to issue an error instead of a warning,
11428 because GDB cannot do anything useful if there's no
11429 syscall number to be caught. */
a96d9b2e
SDJ
11430 error (_("Unknown syscall name '%s'."), cur_name);
11431 }
11432
11433 /* Ok, it's valid. */
11434 VEC_safe_push (int, result, s.number);
11435 }
11436
11437 discard_cleanups (cleanup);
11438 return result;
11439}
11440
11441/* Implement the "catch syscall" command. */
11442
11443static void
cc59ec59
MS
11444catch_syscall_command_1 (char *arg, int from_tty,
11445 struct cmd_list_element *command)
a96d9b2e
SDJ
11446{
11447 int tempflag;
11448 VEC(int) *filter;
11449 struct syscall s;
11450 struct gdbarch *gdbarch = get_current_arch ();
11451
11452 /* Checking if the feature if supported. */
11453 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11454 error (_("The feature 'catch syscall' is not supported on \
ea666128 11455this architecture yet."));
a96d9b2e
SDJ
11456
11457 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11458
e9cafbcc 11459 arg = skip_spaces (arg);
a96d9b2e
SDJ
11460
11461 /* We need to do this first "dummy" translation in order
11462 to get the syscall XML file loaded or, most important,
11463 to display a warning to the user if there's no XML file
11464 for his/her architecture. */
11465 get_syscall_by_number (0, &s);
11466
11467 /* The allowed syntax is:
11468 catch syscall
11469 catch syscall <name | number> [<name | number> ... <name | number>]
11470
11471 Let's check if there's a syscall name. */
11472
11473 if (arg != NULL)
11474 filter = catch_syscall_split_args (arg);
11475 else
11476 filter = NULL;
11477
11478 create_syscall_event_catchpoint (tempflag, filter,
11479 &catch_syscall_breakpoint_ops);
11480}
11481
c906108c 11482static void
fba45db2 11483catch_command (char *arg, int from_tty)
c906108c 11484{
44feb3ce 11485 error (_("Catch requires an event name."));
c906108c
SS
11486}
11487\f
11488
11489static void
fba45db2 11490tcatch_command (char *arg, int from_tty)
c906108c 11491{
44feb3ce 11492 error (_("Catch requires an event name."));
c906108c
SS
11493}
11494
8a2c437b
TT
11495/* A qsort comparison function that sorts breakpoints in order. */
11496
11497static int
11498compare_breakpoints (const void *a, const void *b)
11499{
11500 const breakpoint_p *ba = a;
11501 uintptr_t ua = (uintptr_t) *ba;
11502 const breakpoint_p *bb = b;
11503 uintptr_t ub = (uintptr_t) *bb;
11504
11505 if ((*ba)->number < (*bb)->number)
11506 return -1;
11507 else if ((*ba)->number > (*bb)->number)
11508 return 1;
11509
11510 /* Now sort by address, in case we see, e..g, two breakpoints with
11511 the number 0. */
11512 if (ua < ub)
11513 return -1;
11514 return ub > ub ? 1 : 0;
11515}
11516
80f8a6eb 11517/* Delete breakpoints by address or line. */
c906108c
SS
11518
11519static void
fba45db2 11520clear_command (char *arg, int from_tty)
c906108c 11521{
8a2c437b 11522 struct breakpoint *b, *prev;
d6e956e5
VP
11523 VEC(breakpoint_p) *found = 0;
11524 int ix;
c906108c
SS
11525 int default_match;
11526 struct symtabs_and_lines sals;
11527 struct symtab_and_line sal;
c906108c 11528 int i;
8a2c437b 11529 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11530
11531 if (arg)
11532 {
f8eba3c6
TT
11533 sals = decode_line_spec (arg, (DECODE_LINE_FUNFIRSTLINE
11534 | DECODE_LINE_LIST_MODE));
c906108c
SS
11535 default_match = 0;
11536 }
11537 else
11538 {
c5aa993b 11539 sals.sals = (struct symtab_and_line *)
c906108c 11540 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 11541 make_cleanup (xfree, sals.sals);
4a64f543 11542 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11543
11544 /* Set sal's line, symtab, pc, and pspace to the values
11545 corresponding to the last call to print_frame_info. If the
11546 codepoint is not valid, this will set all the fields to 0. */
11547 get_last_displayed_sal (&sal);
c906108c 11548 if (sal.symtab == 0)
8a3fe4f8 11549 error (_("No source file specified."));
c906108c
SS
11550
11551 sals.sals[0] = sal;
11552 sals.nelts = 1;
11553
11554 default_match = 1;
11555 }
11556
4a64f543
MS
11557 /* We don't call resolve_sal_pc here. That's not as bad as it
11558 seems, because all existing breakpoints typically have both
11559 file/line and pc set. So, if clear is given file/line, we can
11560 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11561
11562 We only support clearing given the address explicitly
11563 present in breakpoint table. Say, we've set breakpoint
4a64f543 11564 at file:line. There were several PC values for that file:line,
ed0616c6 11565 due to optimization, all in one block.
4a64f543
MS
11566
11567 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11568 PC corresponding to the same file:line, the breakpoint won't
11569 be cleared. We probably can still clear the breakpoint, but
11570 since the other PC value is never presented to user, user
11571 can only find it by guessing, and it does not seem important
11572 to support that. */
11573
4a64f543
MS
11574 /* For each line spec given, delete bps which correspond to it. Do
11575 it in two passes, solely to preserve the current behavior that
11576 from_tty is forced true if we delete more than one
11577 breakpoint. */
c906108c 11578
80f8a6eb 11579 found = NULL;
8a2c437b 11580 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11581 for (i = 0; i < sals.nelts; i++)
11582 {
4aac40c8
TT
11583 int is_abs, sal_name_len;
11584
c906108c 11585 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11586 If line given (pc == 0), clear all bpts on specified line.
11587 If defaulting, clear all bpts on default line
c906108c 11588 or at default pc.
c5aa993b
JM
11589
11590 defaulting sal.pc != 0 tests to do
11591
11592 0 1 pc
11593 1 1 pc _and_ line
11594 0 0 line
11595 1 0 <can't happen> */
c906108c
SS
11596
11597 sal = sals.sals[i];
4aac40c8
TT
11598 is_abs = sal.symtab == NULL ? 1 : IS_ABSOLUTE_PATH (sal.symtab->filename);
11599 sal_name_len = is_abs ? 0 : strlen (sal.symtab->filename);
c906108c 11600
4a64f543 11601 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11602 ALL_BREAKPOINTS (b)
c5aa993b 11603 {
0d381245 11604 int match = 0;
4a64f543 11605 /* Are we going to delete b? */
cc60f2e3 11606 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11607 {
11608 struct bp_location *loc = b->loc;
11609 for (; loc; loc = loc->next)
11610 {
f8eba3c6
TT
11611 /* If the user specified file:line, don't allow a PC
11612 match. This matches historical gdb behavior. */
11613 int pc_match = (!sal.explicit_line
11614 && sal.pc
11615 && (loc->pspace == sal.pspace)
11616 && (loc->address == sal.pc)
11617 && (!section_is_overlay (loc->section)
11618 || loc->section == sal.section));
4aac40c8
TT
11619 int line_match = 0;
11620
11621 if ((default_match || sal.explicit_line)
11622 && loc->source_file != NULL
11623 && sal.symtab != NULL
11624 && sal.pspace == loc->pspace
11625 && loc->line_number == sal.line)
11626 {
11627 if (filename_cmp (loc->source_file,
11628 sal.symtab->filename) == 0)
11629 line_match = 1;
11630 else if (!IS_ABSOLUTE_PATH (sal.symtab->filename)
11631 && compare_filenames_for_search (loc->source_file,
11632 sal.symtab->filename,
11633 sal_name_len))
11634 line_match = 1;
11635 }
11636
0d381245
VP
11637 if (pc_match || line_match)
11638 {
11639 match = 1;
11640 break;
11641 }
11642 }
11643 }
11644
11645 if (match)
d6e956e5 11646 VEC_safe_push(breakpoint_p, found, b);
c906108c 11647 }
80f8a6eb 11648 }
8a2c437b 11649
80f8a6eb 11650 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11651 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11652 {
11653 if (arg)
8a3fe4f8 11654 error (_("No breakpoint at %s."), arg);
80f8a6eb 11655 else
8a3fe4f8 11656 error (_("No breakpoint at this line."));
80f8a6eb 11657 }
c906108c 11658
8a2c437b
TT
11659 /* Remove duplicates from the vec. */
11660 qsort (VEC_address (breakpoint_p, found),
11661 VEC_length (breakpoint_p, found),
11662 sizeof (breakpoint_p),
11663 compare_breakpoints);
11664 prev = VEC_index (breakpoint_p, found, 0);
11665 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11666 {
11667 if (b == prev)
11668 {
11669 VEC_ordered_remove (breakpoint_p, found, ix);
11670 --ix;
11671 }
11672 }
11673
d6e956e5 11674 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11675 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11676 if (from_tty)
a3f17187 11677 {
d6e956e5 11678 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11679 printf_unfiltered (_("Deleted breakpoint "));
11680 else
11681 printf_unfiltered (_("Deleted breakpoints "));
11682 }
80f8a6eb 11683 breakpoints_changed ();
d6e956e5
VP
11684
11685 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11686 {
c5aa993b 11687 if (from_tty)
d6e956e5
VP
11688 printf_unfiltered ("%d ", b->number);
11689 delete_breakpoint (b);
c906108c 11690 }
80f8a6eb
MS
11691 if (from_tty)
11692 putchar_unfiltered ('\n');
8a2c437b
TT
11693
11694 do_cleanups (cleanups);
c906108c
SS
11695}
11696\f
11697/* Delete breakpoint in BS if they are `delete' breakpoints and
11698 all breakpoints that are marked for deletion, whether hit or not.
11699 This is called after any breakpoint is hit, or after errors. */
11700
11701void
fba45db2 11702breakpoint_auto_delete (bpstat bs)
c906108c 11703{
35df4500 11704 struct breakpoint *b, *b_tmp;
c906108c
SS
11705
11706 for (; bs; bs = bs->next)
f431efe5
PA
11707 if (bs->breakpoint_at
11708 && bs->breakpoint_at->disposition == disp_del
c906108c 11709 && bs->stop)
f431efe5 11710 delete_breakpoint (bs->breakpoint_at);
c906108c 11711
35df4500 11712 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11713 {
b5de0fa7 11714 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11715 delete_breakpoint (b);
11716 }
c906108c
SS
11717}
11718
4a64f543
MS
11719/* A comparison function for bp_location AP and BP being interfaced to
11720 qsort. Sort elements primarily by their ADDRESS (no matter what
11721 does breakpoint_address_is_meaningful say for its OWNER),
11722 secondarily by ordering first bp_permanent OWNERed elements and
11723 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11724 qsort being an unstable algorithm. */
876fa593
JK
11725
11726static int
494cfb0f 11727bp_location_compare (const void *ap, const void *bp)
876fa593 11728{
494cfb0f
JK
11729 struct bp_location *a = *(void **) ap;
11730 struct bp_location *b = *(void **) bp;
2bdf28a0 11731 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
11732 int a_perm = a->owner->enable_state == bp_permanent;
11733 int b_perm = b->owner->enable_state == bp_permanent;
11734
11735 if (a->address != b->address)
11736 return (a->address > b->address) - (a->address < b->address);
11737
dea2aa5f
LM
11738 /* Sort locations at the same address by their pspace number, keeping
11739 locations of the same inferior (in a multi-inferior environment)
11740 grouped. */
11741
11742 if (a->pspace->num != b->pspace->num)
11743 return ((a->pspace->num > b->pspace->num)
11744 - (a->pspace->num < b->pspace->num));
11745
876fa593
JK
11746 /* Sort permanent breakpoints first. */
11747 if (a_perm != b_perm)
11748 return (a_perm < b_perm) - (a_perm > b_perm);
11749
c56a97f9
JK
11750 /* Make the internal GDB representation stable across GDB runs
11751 where A and B memory inside GDB can differ. Breakpoint locations of
11752 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11753
11754 if (a->owner->number != b->owner->number)
c56a97f9
JK
11755 return ((a->owner->number > b->owner->number)
11756 - (a->owner->number < b->owner->number));
876fa593
JK
11757
11758 return (a > b) - (a < b);
11759}
11760
876fa593 11761/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
11762 bp_location_shadow_len_after_address_max according to the current
11763 content of the bp_location array. */
f7545552
TT
11764
11765static void
876fa593 11766bp_location_target_extensions_update (void)
f7545552 11767{
876fa593
JK
11768 struct bp_location *bl, **blp_tmp;
11769
11770 bp_location_placed_address_before_address_max = 0;
11771 bp_location_shadow_len_after_address_max = 0;
11772
11773 ALL_BP_LOCATIONS (bl, blp_tmp)
11774 {
11775 CORE_ADDR start, end, addr;
11776
11777 if (!bp_location_has_shadow (bl))
11778 continue;
11779
11780 start = bl->target_info.placed_address;
11781 end = start + bl->target_info.shadow_len;
11782
11783 gdb_assert (bl->address >= start);
11784 addr = bl->address - start;
11785 if (addr > bp_location_placed_address_before_address_max)
11786 bp_location_placed_address_before_address_max = addr;
11787
11788 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11789
11790 gdb_assert (bl->address < end);
11791 addr = end - bl->address;
11792 if (addr > bp_location_shadow_len_after_address_max)
11793 bp_location_shadow_len_after_address_max = addr;
11794 }
f7545552
TT
11795}
11796
1e4d1764
YQ
11797/* Download tracepoint locations if they haven't been. */
11798
11799static void
11800download_tracepoint_locations (void)
11801{
11802 struct bp_location *bl, **blp_tmp;
11803 struct cleanup *old_chain;
11804
11805 if (!target_can_download_tracepoint ())
11806 return;
11807
11808 old_chain = save_current_space_and_thread ();
11809
11810 ALL_BP_LOCATIONS (bl, blp_tmp)
11811 {
11812 struct tracepoint *t;
11813
11814 if (!is_tracepoint (bl->owner))
11815 continue;
11816
11817 if ((bl->owner->type == bp_fast_tracepoint
11818 ? !may_insert_fast_tracepoints
11819 : !may_insert_tracepoints))
11820 continue;
11821
11822 /* In tracepoint, locations are _never_ duplicated, so
11823 should_be_inserted is equivalent to
11824 unduplicated_should_be_inserted. */
11825 if (!should_be_inserted (bl) || bl->inserted)
11826 continue;
11827
11828 switch_to_program_space_and_thread (bl->pspace);
11829
11830 target_download_tracepoint (bl);
11831
11832 bl->inserted = 1;
11833 t = (struct tracepoint *) bl->owner;
11834 t->number_on_target = bl->owner->number;
11835 }
11836
11837 do_cleanups (old_chain);
11838}
11839
934709f0
PW
11840/* Swap the insertion/duplication state between two locations. */
11841
11842static void
11843swap_insertion (struct bp_location *left, struct bp_location *right)
11844{
11845 const int left_inserted = left->inserted;
11846 const int left_duplicate = left->duplicate;
b775012e 11847 const int left_needs_update = left->needs_update;
934709f0
PW
11848 const struct bp_target_info left_target_info = left->target_info;
11849
1e4d1764
YQ
11850 /* Locations of tracepoints can never be duplicated. */
11851 if (is_tracepoint (left->owner))
11852 gdb_assert (!left->duplicate);
11853 if (is_tracepoint (right->owner))
11854 gdb_assert (!right->duplicate);
11855
934709f0
PW
11856 left->inserted = right->inserted;
11857 left->duplicate = right->duplicate;
b775012e 11858 left->needs_update = right->needs_update;
934709f0
PW
11859 left->target_info = right->target_info;
11860 right->inserted = left_inserted;
11861 right->duplicate = left_duplicate;
b775012e 11862 right->needs_update = left_needs_update;
934709f0
PW
11863 right->target_info = left_target_info;
11864}
11865
b775012e
LM
11866/* Force the re-insertion of the locations at ADDRESS. This is called
11867 once a new/deleted/modified duplicate location is found and we are evaluating
11868 conditions on the target's side. Such conditions need to be updated on
11869 the target. */
11870
11871static void
11872force_breakpoint_reinsertion (struct bp_location *bl)
11873{
11874 struct bp_location **locp = NULL, **loc2p;
11875 struct bp_location *loc;
11876 CORE_ADDR address = 0;
11877 int pspace_num;
11878
11879 address = bl->address;
11880 pspace_num = bl->pspace->num;
11881
11882 /* This is only meaningful if the target is
11883 evaluating conditions and if the user has
11884 opted for condition evaluation on the target's
11885 side. */
11886 if (gdb_evaluates_breakpoint_condition_p ()
11887 || !target_supports_evaluation_of_breakpoint_conditions ())
11888 return;
11889
11890 /* Flag all breakpoint locations with this address and
11891 the same program space as the location
11892 as "its condition has changed". We need to
11893 update the conditions on the target's side. */
11894 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11895 {
11896 loc = *loc2p;
11897
11898 if (!is_breakpoint (loc->owner)
11899 || pspace_num != loc->pspace->num)
11900 continue;
11901
11902 /* Flag the location appropriately. We use a different state to
11903 let everyone know that we already updated the set of locations
11904 with addr bl->address and program space bl->pspace. This is so
11905 we don't have to keep calling these functions just to mark locations
11906 that have already been marked. */
11907 loc->condition_changed = condition_updated;
11908
11909 /* Free the agent expression bytecode as well. We will compute
11910 it later on. */
11911 if (loc->cond_bytecode)
11912 {
11913 free_agent_expr (loc->cond_bytecode);
11914 loc->cond_bytecode = NULL;
11915 }
11916 }
11917}
11918
4cd9bd08 11919/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
11920 into the inferior, only remove already-inserted locations that no
11921 longer should be inserted. Functions that delete a breakpoint or
11922 breakpoints should pass false, so that deleting a breakpoint
11923 doesn't have the side effect of inserting the locations of other
11924 breakpoints that are marked not-inserted, but should_be_inserted
11925 returns true on them.
11926
11927 This behaviour is useful is situations close to tear-down -- e.g.,
11928 after an exec, while the target still has execution, but breakpoint
11929 shadows of the previous executable image should *NOT* be restored
11930 to the new image; or before detaching, where the target still has
11931 execution and wants to delete breakpoints from GDB's lists, and all
11932 breakpoints had already been removed from the inferior. */
11933
0d381245 11934static void
b60e7edf 11935update_global_location_list (int should_insert)
0d381245 11936{
74960c60 11937 struct breakpoint *b;
876fa593 11938 struct bp_location **locp, *loc;
f7545552 11939 struct cleanup *cleanups;
b775012e
LM
11940 /* Last breakpoint location address that was marked for update. */
11941 CORE_ADDR last_addr = 0;
11942 /* Last breakpoint location program space that was marked for update. */
11943 int last_pspace_num = -1;
f7545552 11944
2d134ed3
PA
11945 /* Used in the duplicates detection below. When iterating over all
11946 bp_locations, points to the first bp_location of a given address.
11947 Breakpoints and watchpoints of different types are never
11948 duplicates of each other. Keep one pointer for each type of
11949 breakpoint/watchpoint, so we only need to loop over all locations
11950 once. */
11951 struct bp_location *bp_loc_first; /* breakpoint */
11952 struct bp_location *wp_loc_first; /* hardware watchpoint */
11953 struct bp_location *awp_loc_first; /* access watchpoint */
11954 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11955
4a64f543
MS
11956 /* Saved former bp_location array which we compare against the newly
11957 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
11958 struct bp_location **old_location, **old_locp;
11959 unsigned old_location_count;
11960
11961 old_location = bp_location;
11962 old_location_count = bp_location_count;
11963 bp_location = NULL;
11964 bp_location_count = 0;
11965 cleanups = make_cleanup (xfree, old_location);
0d381245 11966
74960c60 11967 ALL_BREAKPOINTS (b)
876fa593
JK
11968 for (loc = b->loc; loc; loc = loc->next)
11969 bp_location_count++;
11970
11971 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
11972 locp = bp_location;
11973 ALL_BREAKPOINTS (b)
11974 for (loc = b->loc; loc; loc = loc->next)
11975 *locp++ = loc;
11976 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 11977 bp_location_compare);
876fa593
JK
11978
11979 bp_location_target_extensions_update ();
74960c60 11980
4a64f543
MS
11981 /* Identify bp_location instances that are no longer present in the
11982 new list, and therefore should be freed. Note that it's not
11983 necessary that those locations should be removed from inferior --
11984 if there's another location at the same address (previously
11985 marked as duplicate), we don't need to remove/insert the
11986 location.
876fa593 11987
4a64f543
MS
11988 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11989 and former bp_location array state respectively. */
876fa593
JK
11990
11991 locp = bp_location;
11992 for (old_locp = old_location; old_locp < old_location + old_location_count;
11993 old_locp++)
74960c60 11994 {
876fa593 11995 struct bp_location *old_loc = *old_locp;
c7d46a38 11996 struct bp_location **loc2p;
876fa593 11997
e5dd4106 11998 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 11999 not, we have to free it. */
c7d46a38 12000 int found_object = 0;
20874c92
VP
12001 /* Tells if the location should remain inserted in the target. */
12002 int keep_in_target = 0;
12003 int removed = 0;
876fa593 12004
4a64f543
MS
12005 /* Skip LOCP entries which will definitely never be needed.
12006 Stop either at or being the one matching OLD_LOC. */
876fa593 12007 while (locp < bp_location + bp_location_count
c7d46a38 12008 && (*locp)->address < old_loc->address)
876fa593 12009 locp++;
c7d46a38
PA
12010
12011 for (loc2p = locp;
12012 (loc2p < bp_location + bp_location_count
12013 && (*loc2p)->address == old_loc->address);
12014 loc2p++)
12015 {
b775012e
LM
12016 /* Check if this is a new/duplicated location or a duplicated
12017 location that had its condition modified. If so, we want to send
12018 its condition to the target if evaluation of conditions is taking
12019 place there. */
12020 if ((*loc2p)->condition_changed == condition_modified
12021 && (last_addr != old_loc->address
12022 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12023 {
b775012e
LM
12024 force_breakpoint_reinsertion (*loc2p);
12025 last_pspace_num = old_loc->pspace->num;
c7d46a38 12026 }
b775012e
LM
12027
12028 if (*loc2p == old_loc)
12029 found_object = 1;
c7d46a38 12030 }
74960c60 12031
b775012e
LM
12032 /* We have already handled this address, update it so that we don't
12033 have to go through updates again. */
12034 last_addr = old_loc->address;
12035
12036 /* Target-side condition evaluation: Handle deleted locations. */
12037 if (!found_object)
12038 force_breakpoint_reinsertion (old_loc);
12039
4a64f543
MS
12040 /* If this location is no longer present, and inserted, look if
12041 there's maybe a new location at the same address. If so,
12042 mark that one inserted, and don't remove this one. This is
12043 needed so that we don't have a time window where a breakpoint
12044 at certain location is not inserted. */
74960c60 12045
876fa593 12046 if (old_loc->inserted)
0d381245 12047 {
4a64f543
MS
12048 /* If the location is inserted now, we might have to remove
12049 it. */
74960c60 12050
876fa593 12051 if (found_object && should_be_inserted (old_loc))
74960c60 12052 {
4a64f543
MS
12053 /* The location is still present in the location list,
12054 and still should be inserted. Don't do anything. */
20874c92 12055 keep_in_target = 1;
74960c60
VP
12056 }
12057 else
12058 {
b775012e
LM
12059 /* This location still exists, but it won't be kept in the
12060 target since it may have been disabled. We proceed to
12061 remove its target-side condition. */
12062
4a64f543
MS
12063 /* The location is either no longer present, or got
12064 disabled. See if there's another location at the
12065 same address, in which case we don't need to remove
12066 this one from the target. */
876fa593 12067
2bdf28a0 12068 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12069 if (breakpoint_address_is_meaningful (old_loc->owner))
12070 {
876fa593 12071 for (loc2p = locp;
c7d46a38
PA
12072 (loc2p < bp_location + bp_location_count
12073 && (*loc2p)->address == old_loc->address);
876fa593
JK
12074 loc2p++)
12075 {
12076 struct bp_location *loc2 = *loc2p;
12077
2d134ed3 12078 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12079 {
85d721b8
PA
12080 /* Read watchpoint locations are switched to
12081 access watchpoints, if the former are not
12082 supported, but the latter are. */
12083 if (is_hardware_watchpoint (old_loc->owner))
12084 {
12085 gdb_assert (is_hardware_watchpoint (loc2->owner));
12086 loc2->watchpoint_type = old_loc->watchpoint_type;
12087 }
12088
934709f0
PW
12089 /* loc2 is a duplicated location. We need to check
12090 if it should be inserted in case it will be
12091 unduplicated. */
12092 if (loc2 != old_loc
12093 && unduplicated_should_be_inserted (loc2))
c7d46a38 12094 {
934709f0 12095 swap_insertion (old_loc, loc2);
c7d46a38
PA
12096 keep_in_target = 1;
12097 break;
12098 }
876fa593
JK
12099 }
12100 }
12101 }
74960c60
VP
12102 }
12103
20874c92
VP
12104 if (!keep_in_target)
12105 {
876fa593 12106 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12107 {
4a64f543
MS
12108 /* This is just about all we can do. We could keep
12109 this location on the global list, and try to
12110 remove it next time, but there's no particular
12111 reason why we will succeed next time.
20874c92 12112
4a64f543
MS
12113 Note that at this point, old_loc->owner is still
12114 valid, as delete_breakpoint frees the breakpoint
12115 only after calling us. */
3e43a32a
MS
12116 printf_filtered (_("warning: Error removing "
12117 "breakpoint %d\n"),
876fa593 12118 old_loc->owner->number);
20874c92
VP
12119 }
12120 removed = 1;
12121 }
0d381245 12122 }
74960c60
VP
12123
12124 if (!found_object)
1c5cfe86 12125 {
db82e815
PA
12126 if (removed && non_stop
12127 && breakpoint_address_is_meaningful (old_loc->owner)
12128 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12129 {
db82e815
PA
12130 /* This location was removed from the target. In
12131 non-stop mode, a race condition is possible where
12132 we've removed a breakpoint, but stop events for that
12133 breakpoint are already queued and will arrive later.
12134 We apply an heuristic to be able to distinguish such
12135 SIGTRAPs from other random SIGTRAPs: we keep this
12136 breakpoint location for a bit, and will retire it
12137 after we see some number of events. The theory here
12138 is that reporting of events should, "on the average",
12139 be fair, so after a while we'll see events from all
12140 threads that have anything of interest, and no longer
12141 need to keep this breakpoint location around. We
12142 don't hold locations forever so to reduce chances of
12143 mistaking a non-breakpoint SIGTRAP for a breakpoint
12144 SIGTRAP.
12145
12146 The heuristic failing can be disastrous on
12147 decr_pc_after_break targets.
12148
12149 On decr_pc_after_break targets, like e.g., x86-linux,
12150 if we fail to recognize a late breakpoint SIGTRAP,
12151 because events_till_retirement has reached 0 too
12152 soon, we'll fail to do the PC adjustment, and report
12153 a random SIGTRAP to the user. When the user resumes
12154 the inferior, it will most likely immediately crash
2dec564e 12155 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12156 corrupted, because of being resumed e.g., in the
12157 middle of a multi-byte instruction, or skipped a
12158 one-byte instruction. This was actually seen happen
12159 on native x86-linux, and should be less rare on
12160 targets that do not support new thread events, like
12161 remote, due to the heuristic depending on
12162 thread_count.
12163
12164 Mistaking a random SIGTRAP for a breakpoint trap
12165 causes similar symptoms (PC adjustment applied when
12166 it shouldn't), but then again, playing with SIGTRAPs
12167 behind the debugger's back is asking for trouble.
12168
12169 Since hardware watchpoint traps are always
12170 distinguishable from other traps, so we don't need to
12171 apply keep hardware watchpoint moribund locations
12172 around. We simply always ignore hardware watchpoint
12173 traps we can no longer explain. */
12174
876fa593
JK
12175 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12176 old_loc->owner = NULL;
20874c92 12177
876fa593 12178 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12179 }
12180 else
f431efe5
PA
12181 {
12182 old_loc->owner = NULL;
12183 decref_bp_location (&old_loc);
12184 }
20874c92 12185 }
74960c60 12186 }
1c5cfe86 12187
348d480f
PA
12188 /* Rescan breakpoints at the same address and section, marking the
12189 first one as "first" and any others as "duplicates". This is so
12190 that the bpt instruction is only inserted once. If we have a
12191 permanent breakpoint at the same place as BPT, make that one the
12192 official one, and the rest as duplicates. Permanent breakpoints
12193 are sorted first for the same address.
12194
12195 Do the same for hardware watchpoints, but also considering the
12196 watchpoint's type (regular/access/read) and length. */
12197
12198 bp_loc_first = NULL;
12199 wp_loc_first = NULL;
12200 awp_loc_first = NULL;
12201 rwp_loc_first = NULL;
12202 ALL_BP_LOCATIONS (loc, locp)
12203 {
12204 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12205 non-NULL. */
348d480f 12206 struct bp_location **loc_first_p;
d3fbdd86 12207 b = loc->owner;
348d480f 12208
f8eba3c6 12209 if (!should_be_inserted (loc)
348d480f 12210 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12211 /* Don't detect duplicate for tracepoint locations because they are
12212 never duplicated. See the comments in field `duplicate' of
12213 `struct bp_location'. */
348d480f 12214 || is_tracepoint (b))
b775012e
LM
12215 {
12216 /* Clear the condition modification flag. */
12217 loc->condition_changed = condition_unchanged;
12218 continue;
12219 }
348d480f
PA
12220
12221 /* Permanent breakpoint should always be inserted. */
12222 if (b->enable_state == bp_permanent && ! loc->inserted)
12223 internal_error (__FILE__, __LINE__,
12224 _("allegedly permanent breakpoint is not "
12225 "actually inserted"));
12226
12227 if (b->type == bp_hardware_watchpoint)
12228 loc_first_p = &wp_loc_first;
12229 else if (b->type == bp_read_watchpoint)
12230 loc_first_p = &rwp_loc_first;
12231 else if (b->type == bp_access_watchpoint)
12232 loc_first_p = &awp_loc_first;
12233 else
12234 loc_first_p = &bp_loc_first;
12235
12236 if (*loc_first_p == NULL
12237 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12238 || !breakpoint_locations_match (loc, *loc_first_p))
12239 {
12240 *loc_first_p = loc;
12241 loc->duplicate = 0;
b775012e
LM
12242
12243 if (is_breakpoint (loc->owner) && loc->condition_changed)
12244 {
12245 loc->needs_update = 1;
12246 /* Clear the condition modification flag. */
12247 loc->condition_changed = condition_unchanged;
12248 }
348d480f
PA
12249 continue;
12250 }
12251
934709f0
PW
12252
12253 /* This and the above ensure the invariant that the first location
12254 is not duplicated, and is the inserted one.
12255 All following are marked as duplicated, and are not inserted. */
12256 if (loc->inserted)
12257 swap_insertion (loc, *loc_first_p);
348d480f
PA
12258 loc->duplicate = 1;
12259
b775012e
LM
12260 /* Clear the condition modification flag. */
12261 loc->condition_changed = condition_unchanged;
12262
348d480f
PA
12263 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12264 && b->enable_state != bp_permanent)
12265 internal_error (__FILE__, __LINE__,
12266 _("another breakpoint was inserted on top of "
12267 "a permanent breakpoint"));
12268 }
12269
b775012e 12270 if (breakpoints_always_inserted_mode ()
348d480f
PA
12271 && (have_live_inferiors ()
12272 || (gdbarch_has_global_breakpoints (target_gdbarch))))
b775012e
LM
12273 {
12274 if (should_insert)
12275 insert_breakpoint_locations ();
12276 else
12277 {
12278 /* Though should_insert is false, we may need to update conditions
12279 on the target's side if it is evaluating such conditions. We
12280 only update conditions for locations that are marked
12281 "needs_update". */
12282 update_inserted_breakpoint_locations ();
12283 }
12284 }
348d480f 12285
1e4d1764
YQ
12286 if (should_insert)
12287 download_tracepoint_locations ();
12288
348d480f
PA
12289 do_cleanups (cleanups);
12290}
12291
12292void
12293breakpoint_retire_moribund (void)
12294{
12295 struct bp_location *loc;
12296 int ix;
12297
12298 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12299 if (--(loc->events_till_retirement) == 0)
12300 {
12301 decref_bp_location (&loc);
12302 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12303 --ix;
12304 }
12305}
12306
12307static void
12308update_global_location_list_nothrow (int inserting)
12309{
bfd189b1 12310 volatile struct gdb_exception e;
348d480f
PA
12311
12312 TRY_CATCH (e, RETURN_MASK_ERROR)
12313 update_global_location_list (inserting);
12314}
12315
12316/* Clear BKP from a BPS. */
12317
12318static void
12319bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12320{
12321 bpstat bs;
12322
12323 for (bs = bps; bs; bs = bs->next)
12324 if (bs->breakpoint_at == bpt)
12325 {
12326 bs->breakpoint_at = NULL;
12327 bs->old_val = NULL;
12328 /* bs->commands will be freed later. */
12329 }
12330}
12331
12332/* Callback for iterate_over_threads. */
12333static int
12334bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12335{
12336 struct breakpoint *bpt = data;
12337
12338 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12339 return 0;
12340}
12341
12342/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12343 callbacks. */
12344
12345static void
12346say_where (struct breakpoint *b)
12347{
79a45e25 12348 struct ui_out *uiout = current_uiout;
348d480f
PA
12349 struct value_print_options opts;
12350
12351 get_user_print_options (&opts);
12352
12353 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12354 single string. */
12355 if (b->loc == NULL)
12356 {
12357 printf_filtered (_(" (%s) pending."), b->addr_string);
12358 }
12359 else
12360 {
f8eba3c6 12361 if (opts.addressprint || b->loc->source_file == NULL)
348d480f
PA
12362 {
12363 printf_filtered (" at ");
12364 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12365 gdb_stdout);
12366 }
f8eba3c6
TT
12367 if (b->loc->source_file)
12368 {
12369 /* If there is a single location, we can print the location
12370 more nicely. */
12371 if (b->loc->next == NULL)
12372 printf_filtered (": file %s, line %d.",
12373 b->loc->source_file, b->loc->line_number);
12374 else
12375 /* This is not ideal, but each location may have a
12376 different file name, and this at least reflects the
12377 real situation somewhat. */
12378 printf_filtered (": %s.", b->addr_string);
12379 }
348d480f
PA
12380
12381 if (b->loc->next)
12382 {
12383 struct bp_location *loc = b->loc;
12384 int n = 0;
12385 for (; loc; loc = loc->next)
12386 ++n;
12387 printf_filtered (" (%d locations)", n);
12388 }
12389 }
12390}
12391
348d480f
PA
12392/* Default bp_location_ops methods. */
12393
12394static void
12395bp_location_dtor (struct bp_location *self)
12396{
12397 xfree (self->cond);
b775012e
LM
12398 if (self->cond_bytecode)
12399 free_agent_expr (self->cond_bytecode);
348d480f 12400 xfree (self->function_name);
f8eba3c6 12401 xfree (self->source_file);
348d480f
PA
12402}
12403
12404static const struct bp_location_ops bp_location_ops =
12405{
12406 bp_location_dtor
12407};
12408
2060206e
PA
12409/* Default breakpoint_ops methods all breakpoint_ops ultimately
12410 inherit from. */
348d480f 12411
2060206e
PA
12412static void
12413base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12414{
12415 decref_counted_command_line (&self->commands);
12416 xfree (self->cond_string);
348d480f 12417 xfree (self->addr_string);
f8eba3c6 12418 xfree (self->filter);
348d480f 12419 xfree (self->addr_string_range_end);
348d480f
PA
12420}
12421
2060206e
PA
12422static struct bp_location *
12423base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12424{
12425 struct bp_location *loc;
12426
12427 loc = XNEW (struct bp_location);
12428 init_bp_location (loc, &bp_location_ops, self);
12429 return loc;
12430}
12431
2060206e
PA
12432static void
12433base_breakpoint_re_set (struct breakpoint *b)
12434{
12435 /* Nothing to re-set. */
12436}
12437
12438#define internal_error_pure_virtual_called() \
12439 gdb_assert_not_reached ("pure virtual function called")
12440
12441static int
12442base_breakpoint_insert_location (struct bp_location *bl)
12443{
12444 internal_error_pure_virtual_called ();
12445}
12446
12447static int
12448base_breakpoint_remove_location (struct bp_location *bl)
12449{
12450 internal_error_pure_virtual_called ();
12451}
12452
12453static int
12454base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12455 struct address_space *aspace,
09ac7c10
TT
12456 CORE_ADDR bp_addr,
12457 const struct target_waitstatus *ws)
2060206e
PA
12458{
12459 internal_error_pure_virtual_called ();
12460}
12461
12462static void
12463base_breakpoint_check_status (bpstat bs)
12464{
12465 /* Always stop. */
12466}
12467
12468/* A "works_in_software_mode" breakpoint_ops method that just internal
12469 errors. */
12470
12471static int
12472base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12473{
12474 internal_error_pure_virtual_called ();
12475}
12476
12477/* A "resources_needed" breakpoint_ops method that just internal
12478 errors. */
12479
12480static int
12481base_breakpoint_resources_needed (const struct bp_location *bl)
12482{
12483 internal_error_pure_virtual_called ();
12484}
12485
12486static enum print_stop_action
12487base_breakpoint_print_it (bpstat bs)
12488{
12489 internal_error_pure_virtual_called ();
12490}
12491
12492static void
12493base_breakpoint_print_one_detail (const struct breakpoint *self,
12494 struct ui_out *uiout)
12495{
12496 /* nothing */
12497}
12498
12499static void
12500base_breakpoint_print_mention (struct breakpoint *b)
12501{
12502 internal_error_pure_virtual_called ();
12503}
12504
12505static void
12506base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12507{
12508 internal_error_pure_virtual_called ();
12509}
12510
983af33b
SDJ
12511static void
12512base_breakpoint_create_sals_from_address (char **arg,
12513 struct linespec_result *canonical,
12514 enum bptype type_wanted,
12515 char *addr_start,
12516 char **copy_arg)
12517{
12518 internal_error_pure_virtual_called ();
12519}
12520
12521static void
12522base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12523 struct linespec_result *c,
12524 struct linespec_sals *lsal,
12525 char *cond_string,
e7e0cddf 12526 char *extra_string,
983af33b
SDJ
12527 enum bptype type_wanted,
12528 enum bpdisp disposition,
12529 int thread,
12530 int task, int ignore_count,
12531 const struct breakpoint_ops *o,
12532 int from_tty, int enabled,
44f238bb 12533 int internal, unsigned flags)
983af33b
SDJ
12534{
12535 internal_error_pure_virtual_called ();
12536}
12537
12538static void
12539base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12540 struct symtabs_and_lines *sals)
12541{
12542 internal_error_pure_virtual_called ();
12543}
12544
2060206e
PA
12545static struct breakpoint_ops base_breakpoint_ops =
12546{
12547 base_breakpoint_dtor,
12548 base_breakpoint_allocate_location,
12549 base_breakpoint_re_set,
12550 base_breakpoint_insert_location,
12551 base_breakpoint_remove_location,
12552 base_breakpoint_breakpoint_hit,
12553 base_breakpoint_check_status,
12554 base_breakpoint_resources_needed,
12555 base_breakpoint_works_in_software_mode,
12556 base_breakpoint_print_it,
12557 NULL,
12558 base_breakpoint_print_one_detail,
12559 base_breakpoint_print_mention,
983af33b
SDJ
12560 base_breakpoint_print_recreate,
12561 base_breakpoint_create_sals_from_address,
12562 base_breakpoint_create_breakpoints_sal,
12563 base_breakpoint_decode_linespec,
2060206e
PA
12564};
12565
12566/* Default breakpoint_ops methods. */
12567
12568static void
348d480f
PA
12569bkpt_re_set (struct breakpoint *b)
12570{
06edf0c0
PA
12571 /* FIXME: is this still reachable? */
12572 if (b->addr_string == NULL)
12573 {
12574 /* Anything without a string can't be re-set. */
348d480f 12575 delete_breakpoint (b);
06edf0c0 12576 return;
348d480f 12577 }
06edf0c0
PA
12578
12579 breakpoint_re_set_default (b);
348d480f
PA
12580}
12581
2060206e 12582static int
348d480f
PA
12583bkpt_insert_location (struct bp_location *bl)
12584{
12585 if (bl->loc_type == bp_loc_hardware_breakpoint)
12586 return target_insert_hw_breakpoint (bl->gdbarch,
12587 &bl->target_info);
12588 else
12589 return target_insert_breakpoint (bl->gdbarch,
12590 &bl->target_info);
12591}
12592
2060206e 12593static int
348d480f
PA
12594bkpt_remove_location (struct bp_location *bl)
12595{
12596 if (bl->loc_type == bp_loc_hardware_breakpoint)
12597 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12598 else
12599 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
12600}
12601
2060206e 12602static int
348d480f 12603bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12604 struct address_space *aspace, CORE_ADDR bp_addr,
12605 const struct target_waitstatus *ws)
348d480f
PA
12606{
12607 struct breakpoint *b = bl->owner;
12608
09ac7c10 12609 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12610 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12611 return 0;
12612
348d480f
PA
12613 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12614 aspace, bp_addr))
12615 return 0;
12616
12617 if (overlay_debugging /* unmapped overlay section */
12618 && section_is_overlay (bl->section)
12619 && !section_is_mapped (bl->section))
12620 return 0;
12621
12622 return 1;
12623}
12624
2060206e 12625static int
348d480f
PA
12626bkpt_resources_needed (const struct bp_location *bl)
12627{
12628 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12629
12630 return 1;
12631}
12632
2060206e 12633static enum print_stop_action
348d480f
PA
12634bkpt_print_it (bpstat bs)
12635{
348d480f
PA
12636 struct breakpoint *b;
12637 const struct bp_location *bl;
001c8c33 12638 int bp_temp;
79a45e25 12639 struct ui_out *uiout = current_uiout;
348d480f
PA
12640
12641 gdb_assert (bs->bp_location_at != NULL);
12642
12643 bl = bs->bp_location_at;
12644 b = bs->breakpoint_at;
12645
001c8c33
PA
12646 bp_temp = b->disposition == disp_del;
12647 if (bl->address != bl->requested_address)
12648 breakpoint_adjustment_warning (bl->requested_address,
12649 bl->address,
12650 b->number, 1);
12651 annotate_breakpoint (b->number);
12652 if (bp_temp)
12653 ui_out_text (uiout, "\nTemporary breakpoint ");
12654 else
12655 ui_out_text (uiout, "\nBreakpoint ");
12656 if (ui_out_is_mi_like_p (uiout))
348d480f 12657 {
001c8c33
PA
12658 ui_out_field_string (uiout, "reason",
12659 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12660 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 12661 }
001c8c33
PA
12662 ui_out_field_int (uiout, "bkptno", b->number);
12663 ui_out_text (uiout, ", ");
06edf0c0 12664
001c8c33 12665 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12666}
12667
2060206e 12668static void
06edf0c0
PA
12669bkpt_print_mention (struct breakpoint *b)
12670{
79a45e25 12671 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
12672 return;
12673
12674 switch (b->type)
12675 {
12676 case bp_breakpoint:
12677 case bp_gnu_ifunc_resolver:
12678 if (b->disposition == disp_del)
12679 printf_filtered (_("Temporary breakpoint"));
12680 else
12681 printf_filtered (_("Breakpoint"));
12682 printf_filtered (_(" %d"), b->number);
12683 if (b->type == bp_gnu_ifunc_resolver)
12684 printf_filtered (_(" at gnu-indirect-function resolver"));
12685 break;
12686 case bp_hardware_breakpoint:
12687 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12688 break;
e7e0cddf
SS
12689 case bp_dprintf:
12690 printf_filtered (_("Dprintf %d"), b->number);
12691 break;
06edf0c0
PA
12692 }
12693
12694 say_where (b);
12695}
12696
2060206e 12697static void
06edf0c0
PA
12698bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12699{
12700 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12701 fprintf_unfiltered (fp, "tbreak");
12702 else if (tp->type == bp_breakpoint)
12703 fprintf_unfiltered (fp, "break");
12704 else if (tp->type == bp_hardware_breakpoint
12705 && tp->disposition == disp_del)
12706 fprintf_unfiltered (fp, "thbreak");
12707 else if (tp->type == bp_hardware_breakpoint)
12708 fprintf_unfiltered (fp, "hbreak");
12709 else
12710 internal_error (__FILE__, __LINE__,
12711 _("unhandled breakpoint type %d"), (int) tp->type);
12712
2060206e 12713 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 12714 print_recreate_thread (tp, fp);
06edf0c0
PA
12715}
12716
983af33b
SDJ
12717static void
12718bkpt_create_sals_from_address (char **arg,
12719 struct linespec_result *canonical,
12720 enum bptype type_wanted,
12721 char *addr_start, char **copy_arg)
12722{
12723 create_sals_from_address_default (arg, canonical, type_wanted,
12724 addr_start, copy_arg);
12725}
12726
12727static void
12728bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12729 struct linespec_result *canonical,
12730 struct linespec_sals *lsal,
12731 char *cond_string,
e7e0cddf 12732 char *extra_string,
983af33b
SDJ
12733 enum bptype type_wanted,
12734 enum bpdisp disposition,
12735 int thread,
12736 int task, int ignore_count,
12737 const struct breakpoint_ops *ops,
12738 int from_tty, int enabled,
44f238bb 12739 int internal, unsigned flags)
983af33b
SDJ
12740{
12741 create_breakpoints_sal_default (gdbarch, canonical, lsal,
e7e0cddf
SS
12742 cond_string, extra_string,
12743 type_wanted,
983af33b
SDJ
12744 disposition, thread, task,
12745 ignore_count, ops, from_tty,
44f238bb 12746 enabled, internal, flags);
983af33b
SDJ
12747}
12748
12749static void
12750bkpt_decode_linespec (struct breakpoint *b, char **s,
12751 struct symtabs_and_lines *sals)
12752{
12753 decode_linespec_default (b, s, sals);
12754}
12755
06edf0c0
PA
12756/* Virtual table for internal breakpoints. */
12757
12758static void
12759internal_bkpt_re_set (struct breakpoint *b)
12760{
12761 switch (b->type)
12762 {
12763 /* Delete overlay event and longjmp master breakpoints; they
12764 will be reset later by breakpoint_re_set. */
12765 case bp_overlay_event:
12766 case bp_longjmp_master:
12767 case bp_std_terminate_master:
12768 case bp_exception_master:
12769 delete_breakpoint (b);
12770 break;
12771
12772 /* This breakpoint is special, it's set up when the inferior
12773 starts and we really don't want to touch it. */
12774 case bp_shlib_event:
12775
12776 /* Like bp_shlib_event, this breakpoint type is special. Once
12777 it is set up, we do not want to touch it. */
12778 case bp_thread_event:
12779 break;
12780 }
12781}
12782
12783static void
12784internal_bkpt_check_status (bpstat bs)
12785{
a9b3a50f
PA
12786 if (bs->breakpoint_at->type == bp_shlib_event)
12787 {
12788 /* If requested, stop when the dynamic linker notifies GDB of
12789 events. This allows the user to get control and place
12790 breakpoints in initializer routines for dynamically loaded
12791 objects (among other things). */
12792 bs->stop = stop_on_solib_events;
12793 bs->print = stop_on_solib_events;
12794 }
12795 else
12796 bs->stop = 0;
06edf0c0
PA
12797}
12798
12799static enum print_stop_action
12800internal_bkpt_print_it (bpstat bs)
12801{
36dfb11c 12802 struct ui_out *uiout = current_uiout;
06edf0c0 12803 struct breakpoint *b;
06edf0c0 12804
06edf0c0
PA
12805 b = bs->breakpoint_at;
12806
06edf0c0
PA
12807 switch (b->type)
12808 {
348d480f
PA
12809 case bp_shlib_event:
12810 /* Did we stop because the user set the stop_on_solib_events
12811 variable? (If so, we report this as a generic, "Stopped due
12812 to shlib event" message.) */
edcc5120 12813 print_solib_event (0);
348d480f
PA
12814 break;
12815
12816 case bp_thread_event:
12817 /* Not sure how we will get here.
12818 GDB should not stop for these breakpoints. */
12819 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12820 break;
12821
12822 case bp_overlay_event:
12823 /* By analogy with the thread event, GDB should not stop for these. */
12824 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12825 break;
12826
12827 case bp_longjmp_master:
12828 /* These should never be enabled. */
12829 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12830 break;
12831
12832 case bp_std_terminate_master:
12833 /* These should never be enabled. */
12834 printf_filtered (_("std::terminate Master Breakpoint: "
12835 "gdb should not stop!\n"));
348d480f
PA
12836 break;
12837
12838 case bp_exception_master:
12839 /* These should never be enabled. */
12840 printf_filtered (_("Exception Master Breakpoint: "
12841 "gdb should not stop!\n"));
06edf0c0
PA
12842 break;
12843 }
12844
001c8c33 12845 return PRINT_NOTHING;
06edf0c0
PA
12846}
12847
12848static void
12849internal_bkpt_print_mention (struct breakpoint *b)
12850{
12851 /* Nothing to mention. These breakpoints are internal. */
12852}
12853
06edf0c0
PA
12854/* Virtual table for momentary breakpoints */
12855
12856static void
12857momentary_bkpt_re_set (struct breakpoint *b)
12858{
12859 /* Keep temporary breakpoints, which can be encountered when we step
12860 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
12861 Otherwise these should have been blown away via the cleanup chain
12862 or by breakpoint_init_inferior when we rerun the executable. */
12863}
12864
12865static void
12866momentary_bkpt_check_status (bpstat bs)
12867{
12868 /* Nothing. The point of these breakpoints is causing a stop. */
12869}
12870
12871static enum print_stop_action
12872momentary_bkpt_print_it (bpstat bs)
12873{
79a45e25
PA
12874 struct ui_out *uiout = current_uiout;
12875
001c8c33 12876 if (ui_out_is_mi_like_p (uiout))
06edf0c0 12877 {
001c8c33 12878 struct breakpoint *b = bs->breakpoint_at;
348d480f 12879
001c8c33
PA
12880 switch (b->type)
12881 {
12882 case bp_finish:
12883 ui_out_field_string
12884 (uiout, "reason",
12885 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
12886 break;
348d480f 12887
001c8c33
PA
12888 case bp_until:
12889 ui_out_field_string
12890 (uiout, "reason",
12891 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
12892 break;
12893 }
348d480f
PA
12894 }
12895
001c8c33 12896 return PRINT_UNKNOWN;
348d480f
PA
12897}
12898
06edf0c0
PA
12899static void
12900momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12901{
06edf0c0 12902 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12903}
12904
e2e4d78b
JK
12905/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12906
12907 It gets cleared already on the removal of the first one of such placed
12908 breakpoints. This is OK as they get all removed altogether. */
12909
12910static void
12911longjmp_bkpt_dtor (struct breakpoint *self)
12912{
12913 struct thread_info *tp = find_thread_id (self->thread);
12914
12915 if (tp)
12916 tp->initiating_frame = null_frame_id;
12917
12918 momentary_breakpoint_ops.dtor (self);
12919}
12920
55aa24fb
SDJ
12921/* Specific methods for probe breakpoints. */
12922
12923static int
12924bkpt_probe_insert_location (struct bp_location *bl)
12925{
12926 int v = bkpt_insert_location (bl);
12927
12928 if (v == 0)
12929 {
12930 /* The insertion was successful, now let's set the probe's semaphore
12931 if needed. */
12932 bl->probe->pops->set_semaphore (bl->probe, bl->gdbarch);
12933 }
12934
12935 return v;
12936}
12937
12938static int
12939bkpt_probe_remove_location (struct bp_location *bl)
12940{
12941 /* Let's clear the semaphore before removing the location. */
12942 bl->probe->pops->clear_semaphore (bl->probe, bl->gdbarch);
12943
12944 return bkpt_remove_location (bl);
12945}
12946
12947static void
12948bkpt_probe_create_sals_from_address (char **arg,
12949 struct linespec_result *canonical,
12950 enum bptype type_wanted,
12951 char *addr_start, char **copy_arg)
12952{
12953 struct linespec_sals lsal;
12954
12955 lsal.sals = parse_probes (arg, canonical);
12956
12957 *copy_arg = xstrdup (canonical->addr_string);
12958 lsal.canonical = xstrdup (*copy_arg);
12959
12960 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
12961}
12962
12963static void
12964bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
12965 struct symtabs_and_lines *sals)
12966{
12967 *sals = parse_probes (s, NULL);
12968 if (!sals->sals)
12969 error (_("probe not found"));
12970}
12971
348d480f 12972/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12973
348d480f
PA
12974static void
12975tracepoint_re_set (struct breakpoint *b)
12976{
12977 breakpoint_re_set_default (b);
12978}
876fa593 12979
348d480f
PA
12980static int
12981tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12982 struct address_space *aspace, CORE_ADDR bp_addr,
12983 const struct target_waitstatus *ws)
348d480f
PA
12984{
12985 /* By definition, the inferior does not report stops at
12986 tracepoints. */
12987 return 0;
74960c60
VP
12988}
12989
12990static void
348d480f
PA
12991tracepoint_print_one_detail (const struct breakpoint *self,
12992 struct ui_out *uiout)
74960c60 12993{
d9b3f62e
PA
12994 struct tracepoint *tp = (struct tracepoint *) self;
12995 if (tp->static_trace_marker_id)
348d480f
PA
12996 {
12997 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 12998
348d480f
PA
12999 ui_out_text (uiout, "\tmarker id is ");
13000 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13001 tp->static_trace_marker_id);
348d480f
PA
13002 ui_out_text (uiout, "\n");
13003 }
0d381245
VP
13004}
13005
a474d7c2 13006static void
348d480f 13007tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13008{
79a45e25 13009 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13010 return;
cc59ec59 13011
348d480f
PA
13012 switch (b->type)
13013 {
13014 case bp_tracepoint:
13015 printf_filtered (_("Tracepoint"));
13016 printf_filtered (_(" %d"), b->number);
13017 break;
13018 case bp_fast_tracepoint:
13019 printf_filtered (_("Fast tracepoint"));
13020 printf_filtered (_(" %d"), b->number);
13021 break;
13022 case bp_static_tracepoint:
13023 printf_filtered (_("Static tracepoint"));
13024 printf_filtered (_(" %d"), b->number);
13025 break;
13026 default:
13027 internal_error (__FILE__, __LINE__,
13028 _("unhandled tracepoint type %d"), (int) b->type);
13029 }
13030
13031 say_where (b);
a474d7c2
PA
13032}
13033
348d480f 13034static void
d9b3f62e 13035tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13036{
d9b3f62e
PA
13037 struct tracepoint *tp = (struct tracepoint *) self;
13038
13039 if (self->type == bp_fast_tracepoint)
348d480f 13040 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13041 if (self->type == bp_static_tracepoint)
348d480f 13042 fprintf_unfiltered (fp, "strace");
d9b3f62e 13043 else if (self->type == bp_tracepoint)
348d480f
PA
13044 fprintf_unfiltered (fp, "trace");
13045 else
13046 internal_error (__FILE__, __LINE__,
d9b3f62e 13047 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13048
d9b3f62e
PA
13049 fprintf_unfiltered (fp, " %s", self->addr_string);
13050 print_recreate_thread (self, fp);
13051
13052 if (tp->pass_count)
13053 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13054}
13055
983af33b
SDJ
13056static void
13057tracepoint_create_sals_from_address (char **arg,
13058 struct linespec_result *canonical,
13059 enum bptype type_wanted,
13060 char *addr_start, char **copy_arg)
13061{
13062 create_sals_from_address_default (arg, canonical, type_wanted,
13063 addr_start, copy_arg);
13064}
13065
13066static void
13067tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13068 struct linespec_result *canonical,
13069 struct linespec_sals *lsal,
13070 char *cond_string,
e7e0cddf 13071 char *extra_string,
983af33b
SDJ
13072 enum bptype type_wanted,
13073 enum bpdisp disposition,
13074 int thread,
13075 int task, int ignore_count,
13076 const struct breakpoint_ops *ops,
13077 int from_tty, int enabled,
44f238bb 13078 int internal, unsigned flags)
983af33b
SDJ
13079{
13080 create_breakpoints_sal_default (gdbarch, canonical, lsal,
e7e0cddf
SS
13081 cond_string, extra_string,
13082 type_wanted,
983af33b
SDJ
13083 disposition, thread, task,
13084 ignore_count, ops, from_tty,
44f238bb 13085 enabled, internal, flags);
983af33b
SDJ
13086}
13087
13088static void
13089tracepoint_decode_linespec (struct breakpoint *b, char **s,
13090 struct symtabs_and_lines *sals)
13091{
13092 decode_linespec_default (b, s, sals);
13093}
13094
2060206e 13095struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13096
55aa24fb
SDJ
13097/* The breakpoint_ops structure to be use on tracepoints placed in a
13098 static probe. */
13099
13100static void
13101tracepoint_probe_create_sals_from_address (char **arg,
13102 struct linespec_result *canonical,
13103 enum bptype type_wanted,
13104 char *addr_start, char **copy_arg)
13105{
13106 /* We use the same method for breakpoint on probes. */
13107 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13108 addr_start, copy_arg);
13109}
13110
13111static void
13112tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13113 struct symtabs_and_lines *sals)
13114{
13115 /* We use the same method for breakpoint on probes. */
13116 bkpt_probe_decode_linespec (b, s, sals);
13117}
13118
13119static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13120
983af33b
SDJ
13121/* The breakpoint_ops structure to be used on static tracepoints with
13122 markers (`-m'). */
13123
13124static void
13125strace_marker_create_sals_from_address (char **arg,
13126 struct linespec_result *canonical,
13127 enum bptype type_wanted,
13128 char *addr_start, char **copy_arg)
13129{
13130 struct linespec_sals lsal;
13131
13132 lsal.sals = decode_static_tracepoint_spec (arg);
13133
13134 *copy_arg = savestring (addr_start, *arg - addr_start);
13135
13136 canonical->addr_string = xstrdup (*copy_arg);
13137 lsal.canonical = xstrdup (*copy_arg);
13138 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13139}
13140
13141static void
13142strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13143 struct linespec_result *canonical,
13144 struct linespec_sals *lsal,
13145 char *cond_string,
e7e0cddf 13146 char *extra_string,
983af33b
SDJ
13147 enum bptype type_wanted,
13148 enum bpdisp disposition,
13149 int thread,
13150 int task, int ignore_count,
13151 const struct breakpoint_ops *ops,
13152 int from_tty, int enabled,
44f238bb 13153 int internal, unsigned flags)
983af33b
SDJ
13154{
13155 int i;
13156
13157 /* If the user is creating a static tracepoint by marker id
13158 (strace -m MARKER_ID), then store the sals index, so that
13159 breakpoint_re_set can try to match up which of the newly
13160 found markers corresponds to this one, and, don't try to
13161 expand multiple locations for each sal, given than SALS
13162 already should contain all sals for MARKER_ID. */
13163
13164 for (i = 0; i < lsal->sals.nelts; ++i)
13165 {
13166 struct symtabs_and_lines expanded;
13167 struct tracepoint *tp;
13168 struct cleanup *old_chain;
13169 char *addr_string;
13170
13171 expanded.nelts = 1;
13172 expanded.sals = &lsal->sals.sals[i];
13173
13174 addr_string = xstrdup (canonical->addr_string);
13175 old_chain = make_cleanup (xfree, addr_string);
13176
13177 tp = XCNEW (struct tracepoint);
13178 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13179 addr_string, NULL,
e7e0cddf
SS
13180 cond_string, extra_string,
13181 type_wanted, disposition,
983af33b 13182 thread, task, ignore_count, ops,
44f238bb 13183 from_tty, enabled, internal, flags,
983af33b
SDJ
13184 canonical->special_display);
13185 /* Given that its possible to have multiple markers with
13186 the same string id, if the user is creating a static
13187 tracepoint by marker id ("strace -m MARKER_ID"), then
13188 store the sals index, so that breakpoint_re_set can
13189 try to match up which of the newly found markers
13190 corresponds to this one */
13191 tp->static_trace_marker_id_idx = i;
13192
13193 install_breakpoint (internal, &tp->base, 0);
13194
13195 discard_cleanups (old_chain);
13196 }
13197}
13198
13199static void
13200strace_marker_decode_linespec (struct breakpoint *b, char **s,
13201 struct symtabs_and_lines *sals)
13202{
13203 struct tracepoint *tp = (struct tracepoint *) b;
13204
13205 *sals = decode_static_tracepoint_spec (s);
13206 if (sals->nelts > tp->static_trace_marker_id_idx)
13207 {
13208 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13209 sals->nelts = 1;
13210 }
13211 else
13212 error (_("marker %s not found"), tp->static_trace_marker_id);
13213}
13214
13215static struct breakpoint_ops strace_marker_breakpoint_ops;
13216
13217static int
13218strace_marker_p (struct breakpoint *b)
13219{
13220 return b->ops == &strace_marker_breakpoint_ops;
13221}
13222
53a5351d 13223/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13224 structures. */
c906108c
SS
13225
13226void
fba45db2 13227delete_breakpoint (struct breakpoint *bpt)
c906108c 13228{
52f0bd74 13229 struct breakpoint *b;
c906108c 13230
8a3fe4f8 13231 gdb_assert (bpt != NULL);
c906108c 13232
4a64f543
MS
13233 /* Has this bp already been deleted? This can happen because
13234 multiple lists can hold pointers to bp's. bpstat lists are
13235 especial culprits.
13236
13237 One example of this happening is a watchpoint's scope bp. When
13238 the scope bp triggers, we notice that the watchpoint is out of
13239 scope, and delete it. We also delete its scope bp. But the
13240 scope bp is marked "auto-deleting", and is already on a bpstat.
13241 That bpstat is then checked for auto-deleting bp's, which are
13242 deleted.
13243
13244 A real solution to this problem might involve reference counts in
13245 bp's, and/or giving them pointers back to their referencing
13246 bpstat's, and teaching delete_breakpoint to only free a bp's
13247 storage when no more references were extent. A cheaper bandaid
13248 was chosen. */
c906108c
SS
13249 if (bpt->type == bp_none)
13250 return;
13251
4a64f543
MS
13252 /* At least avoid this stale reference until the reference counting
13253 of breakpoints gets resolved. */
d0fb5eae 13254 if (bpt->related_breakpoint != bpt)
e5a0a904 13255 {
d0fb5eae 13256 struct breakpoint *related;
3a5c3e22 13257 struct watchpoint *w;
d0fb5eae
JK
13258
13259 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13260 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13261 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13262 w = (struct watchpoint *) bpt;
13263 else
13264 w = NULL;
13265 if (w != NULL)
13266 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13267
13268 /* Unlink bpt from the bpt->related_breakpoint ring. */
13269 for (related = bpt; related->related_breakpoint != bpt;
13270 related = related->related_breakpoint);
13271 related->related_breakpoint = bpt->related_breakpoint;
13272 bpt->related_breakpoint = bpt;
e5a0a904
JK
13273 }
13274
a9634178
TJB
13275 /* watch_command_1 creates a watchpoint but only sets its number if
13276 update_watchpoint succeeds in creating its bp_locations. If there's
13277 a problem in that process, we'll be asked to delete the half-created
13278 watchpoint. In that case, don't announce the deletion. */
13279 if (bpt->number)
13280 observer_notify_breakpoint_deleted (bpt);
c906108c 13281
c906108c
SS
13282 if (breakpoint_chain == bpt)
13283 breakpoint_chain = bpt->next;
13284
c906108c
SS
13285 ALL_BREAKPOINTS (b)
13286 if (b->next == bpt)
c5aa993b
JM
13287 {
13288 b->next = bpt->next;
13289 break;
13290 }
c906108c 13291
f431efe5
PA
13292 /* Be sure no bpstat's are pointing at the breakpoint after it's
13293 been freed. */
13294 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13295 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13296 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13297 commands are associated with the bpstat; if we remove it here,
13298 then the later call to bpstat_do_actions (&stop_bpstat); in
13299 event-top.c won't do anything, and temporary breakpoints with
13300 commands won't work. */
13301
13302 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13303
4a64f543
MS
13304 /* Now that breakpoint is removed from breakpoint list, update the
13305 global location list. This will remove locations that used to
13306 belong to this breakpoint. Do this before freeing the breakpoint
13307 itself, since remove_breakpoint looks at location's owner. It
13308 might be better design to have location completely
13309 self-contained, but it's not the case now. */
b60e7edf 13310 update_global_location_list (0);
74960c60 13311
348d480f 13312 bpt->ops->dtor (bpt);
4a64f543
MS
13313 /* On the chance that someone will soon try again to delete this
13314 same bp, we mark it as deleted before freeing its storage. */
c906108c 13315 bpt->type = bp_none;
b8c9b27d 13316 xfree (bpt);
c906108c
SS
13317}
13318
4d6140d9
AC
13319static void
13320do_delete_breakpoint_cleanup (void *b)
13321{
13322 delete_breakpoint (b);
13323}
13324
13325struct cleanup *
13326make_cleanup_delete_breakpoint (struct breakpoint *b)
13327{
13328 return make_cleanup (do_delete_breakpoint_cleanup, b);
13329}
13330
51be5b68
PA
13331/* Iterator function to call a user-provided callback function once
13332 for each of B and its related breakpoints. */
13333
13334static void
13335iterate_over_related_breakpoints (struct breakpoint *b,
13336 void (*function) (struct breakpoint *,
13337 void *),
13338 void *data)
13339{
13340 struct breakpoint *related;
13341
13342 related = b;
13343 do
13344 {
13345 struct breakpoint *next;
13346
13347 /* FUNCTION may delete RELATED. */
13348 next = related->related_breakpoint;
13349
13350 if (next == related)
13351 {
13352 /* RELATED is the last ring entry. */
13353 function (related, data);
13354
13355 /* FUNCTION may have deleted it, so we'd never reach back to
13356 B. There's nothing left to do anyway, so just break
13357 out. */
13358 break;
13359 }
13360 else
13361 function (related, data);
13362
13363 related = next;
13364 }
13365 while (related != b);
13366}
95a42b64
TT
13367
13368static void
13369do_delete_breakpoint (struct breakpoint *b, void *ignore)
13370{
13371 delete_breakpoint (b);
13372}
13373
51be5b68
PA
13374/* A callback for map_breakpoint_numbers that calls
13375 delete_breakpoint. */
13376
13377static void
13378do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13379{
13380 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13381}
13382
c906108c 13383void
fba45db2 13384delete_command (char *arg, int from_tty)
c906108c 13385{
35df4500 13386 struct breakpoint *b, *b_tmp;
c906108c 13387
ea9365bb
TT
13388 dont_repeat ();
13389
c906108c
SS
13390 if (arg == 0)
13391 {
13392 int breaks_to_delete = 0;
13393
46c6471b
PA
13394 /* Delete all breakpoints if no argument. Do not delete
13395 internal breakpoints, these have to be deleted with an
13396 explicit breakpoint number argument. */
c5aa993b 13397 ALL_BREAKPOINTS (b)
46c6471b 13398 if (user_breakpoint_p (b))
973d738b
DJ
13399 {
13400 breaks_to_delete = 1;
13401 break;
13402 }
c906108c
SS
13403
13404 /* Ask user only if there are some breakpoints to delete. */
13405 if (!from_tty
e2e0b3e5 13406 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13407 {
35df4500 13408 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13409 if (user_breakpoint_p (b))
c5aa993b 13410 delete_breakpoint (b);
c906108c
SS
13411 }
13412 }
13413 else
51be5b68 13414 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13415}
13416
0d381245
VP
13417static int
13418all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 13419{
0d381245 13420 for (; loc; loc = loc->next)
8645ff69
UW
13421 if (!loc->shlib_disabled
13422 && !loc->pspace->executing_startup)
0d381245
VP
13423 return 0;
13424 return 1;
fe3f5fa8
VP
13425}
13426
776592bf
DE
13427/* Subroutine of update_breakpoint_locations to simplify it.
13428 Return non-zero if multiple fns in list LOC have the same name.
13429 Null names are ignored. */
13430
13431static int
13432ambiguous_names_p (struct bp_location *loc)
13433{
13434 struct bp_location *l;
13435 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13436 (int (*) (const void *,
13437 const void *)) streq,
776592bf
DE
13438 NULL, xcalloc, xfree);
13439
13440 for (l = loc; l != NULL; l = l->next)
13441 {
13442 const char **slot;
13443 const char *name = l->function_name;
13444
13445 /* Allow for some names to be NULL, ignore them. */
13446 if (name == NULL)
13447 continue;
13448
13449 slot = (const char **) htab_find_slot (htab, (const void *) name,
13450 INSERT);
4a64f543
MS
13451 /* NOTE: We can assume slot != NULL here because xcalloc never
13452 returns NULL. */
776592bf
DE
13453 if (*slot != NULL)
13454 {
13455 htab_delete (htab);
13456 return 1;
13457 }
13458 *slot = name;
13459 }
13460
13461 htab_delete (htab);
13462 return 0;
13463}
13464
0fb4aa4b
PA
13465/* When symbols change, it probably means the sources changed as well,
13466 and it might mean the static tracepoint markers are no longer at
13467 the same address or line numbers they used to be at last we
13468 checked. Losing your static tracepoints whenever you rebuild is
13469 undesirable. This function tries to resync/rematch gdb static
13470 tracepoints with the markers on the target, for static tracepoints
13471 that have not been set by marker id. Static tracepoint that have
13472 been set by marker id are reset by marker id in breakpoint_re_set.
13473 The heuristic is:
13474
13475 1) For a tracepoint set at a specific address, look for a marker at
13476 the old PC. If one is found there, assume to be the same marker.
13477 If the name / string id of the marker found is different from the
13478 previous known name, assume that means the user renamed the marker
13479 in the sources, and output a warning.
13480
13481 2) For a tracepoint set at a given line number, look for a marker
13482 at the new address of the old line number. If one is found there,
13483 assume to be the same marker. If the name / string id of the
13484 marker found is different from the previous known name, assume that
13485 means the user renamed the marker in the sources, and output a
13486 warning.
13487
13488 3) If a marker is no longer found at the same address or line, it
13489 may mean the marker no longer exists. But it may also just mean
13490 the code changed a bit. Maybe the user added a few lines of code
13491 that made the marker move up or down (in line number terms). Ask
13492 the target for info about the marker with the string id as we knew
13493 it. If found, update line number and address in the matching
13494 static tracepoint. This will get confused if there's more than one
13495 marker with the same ID (possible in UST, although unadvised
13496 precisely because it confuses tools). */
13497
13498static struct symtab_and_line
13499update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13500{
d9b3f62e 13501 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13502 struct static_tracepoint_marker marker;
13503 CORE_ADDR pc;
0fb4aa4b
PA
13504
13505 pc = sal.pc;
13506 if (sal.line)
13507 find_line_pc (sal.symtab, sal.line, &pc);
13508
13509 if (target_static_tracepoint_marker_at (pc, &marker))
13510 {
d9b3f62e 13511 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13512 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13513 b->number,
d9b3f62e 13514 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13515
d9b3f62e
PA
13516 xfree (tp->static_trace_marker_id);
13517 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13518 release_static_tracepoint_marker (&marker);
13519
13520 return sal;
13521 }
13522
13523 /* Old marker wasn't found on target at lineno. Try looking it up
13524 by string ID. */
13525 if (!sal.explicit_pc
13526 && sal.line != 0
13527 && sal.symtab != NULL
d9b3f62e 13528 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13529 {
13530 VEC(static_tracepoint_marker_p) *markers;
13531
13532 markers
d9b3f62e 13533 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13534
13535 if (!VEC_empty(static_tracepoint_marker_p, markers))
13536 {
80e1d417 13537 struct symtab_and_line sal2;
0fb4aa4b 13538 struct symbol *sym;
80e1d417 13539 struct static_tracepoint_marker *tpmarker;
79a45e25 13540 struct ui_out *uiout = current_uiout;
0fb4aa4b 13541
80e1d417 13542 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13543
d9b3f62e 13544 xfree (tp->static_trace_marker_id);
80e1d417 13545 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13546
13547 warning (_("marker for static tracepoint %d (%s) not "
13548 "found at previous line number"),
d9b3f62e 13549 b->number, tp->static_trace_marker_id);
0fb4aa4b 13550
80e1d417 13551 init_sal (&sal2);
0fb4aa4b 13552
80e1d417 13553 sal2.pc = tpmarker->address;
0fb4aa4b 13554
80e1d417
AS
13555 sal2 = find_pc_line (tpmarker->address, 0);
13556 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
13557 ui_out_text (uiout, "Now in ");
13558 if (sym)
13559 {
13560 ui_out_field_string (uiout, "func",
13561 SYMBOL_PRINT_NAME (sym));
13562 ui_out_text (uiout, " at ");
13563 }
80e1d417 13564 ui_out_field_string (uiout, "file", sal2.symtab->filename);
0fb4aa4b
PA
13565 ui_out_text (uiout, ":");
13566
13567 if (ui_out_is_mi_like_p (uiout))
13568 {
80e1d417 13569 char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b
PA
13570
13571 if (fullname)
13572 ui_out_field_string (uiout, "fullname", fullname);
13573 }
13574
80e1d417 13575 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
13576 ui_out_text (uiout, "\n");
13577
80e1d417 13578 b->loc->line_number = sal2.line;
0fb4aa4b 13579
f8eba3c6 13580 xfree (b->loc->source_file);
0fb4aa4b 13581 if (sym)
80e1d417 13582 b->loc->source_file = xstrdup (sal2.symtab->filename);
0fb4aa4b 13583 else
f8eba3c6 13584 b->loc->source_file = NULL;
0fb4aa4b
PA
13585
13586 xfree (b->addr_string);
13587 b->addr_string = xstrprintf ("%s:%d",
80e1d417 13588 sal2.symtab->filename,
f8eba3c6 13589 b->loc->line_number);
0fb4aa4b
PA
13590
13591 /* Might be nice to check if function changed, and warn if
13592 so. */
13593
80e1d417 13594 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13595 }
13596 }
13597 return sal;
13598}
13599
8d3788bd
VP
13600/* Returns 1 iff locations A and B are sufficiently same that
13601 we don't need to report breakpoint as changed. */
13602
13603static int
13604locations_are_equal (struct bp_location *a, struct bp_location *b)
13605{
13606 while (a && b)
13607 {
13608 if (a->address != b->address)
13609 return 0;
13610
13611 if (a->shlib_disabled != b->shlib_disabled)
13612 return 0;
13613
13614 if (a->enabled != b->enabled)
13615 return 0;
13616
13617 a = a->next;
13618 b = b->next;
13619 }
13620
13621 if ((a == NULL) != (b == NULL))
13622 return 0;
13623
13624 return 1;
13625}
13626
f1310107
TJB
13627/* Create new breakpoint locations for B (a hardware or software breakpoint)
13628 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
13629 a ranged breakpoint. */
13630
0e30163f 13631void
0d381245 13632update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
13633 struct symtabs_and_lines sals,
13634 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
13635{
13636 int i;
0d381245
VP
13637 struct bp_location *existing_locations = b->loc;
13638
f8eba3c6
TT
13639 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
13640 {
13641 /* Ranged breakpoints have only one start location and one end
13642 location. */
13643 b->enable_state = bp_disabled;
13644 update_global_location_list (1);
13645 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13646 "multiple locations found\n"),
13647 b->number);
13648 return;
13649 }
f1310107 13650
4a64f543
MS
13651 /* If there's no new locations, and all existing locations are
13652 pending, don't do anything. This optimizes the common case where
13653 all locations are in the same shared library, that was unloaded.
13654 We'd like to retain the location, so that when the library is
13655 loaded again, we don't loose the enabled/disabled status of the
13656 individual locations. */
0d381245 13657 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
13658 return;
13659
fe3f5fa8
VP
13660 b->loc = NULL;
13661
0d381245 13662 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 13663 {
f8eba3c6
TT
13664 struct bp_location *new_loc;
13665
13666 switch_to_program_space_and_thread (sals.sals[i].pspace);
13667
13668 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 13669
0d381245
VP
13670 /* Reparse conditions, they might contain references to the
13671 old symtab. */
13672 if (b->cond_string != NULL)
13673 {
f1310107 13674 char *s;
bfd189b1 13675 volatile struct gdb_exception e;
fe3f5fa8 13676
0d381245
VP
13677 s = b->cond_string;
13678 TRY_CATCH (e, RETURN_MASK_ERROR)
13679 {
13680 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
13681 0);
13682 }
13683 if (e.reason < 0)
13684 {
3e43a32a
MS
13685 warning (_("failed to reevaluate condition "
13686 "for breakpoint %d: %s"),
0d381245
VP
13687 b->number, e.message);
13688 new_loc->enabled = 0;
13689 }
13690 }
fe3f5fa8 13691
f1310107
TJB
13692 if (sals_end.nelts)
13693 {
13694 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
13695
13696 new_loc->length = end - sals.sals[0].pc + 1;
13697 }
0d381245 13698 }
fe3f5fa8 13699
514f746b
AR
13700 /* Update locations of permanent breakpoints. */
13701 if (b->enable_state == bp_permanent)
13702 make_breakpoint_permanent (b);
13703
4a64f543
MS
13704 /* If possible, carry over 'disable' status from existing
13705 breakpoints. */
0d381245
VP
13706 {
13707 struct bp_location *e = existing_locations;
776592bf
DE
13708 /* If there are multiple breakpoints with the same function name,
13709 e.g. for inline functions, comparing function names won't work.
13710 Instead compare pc addresses; this is just a heuristic as things
13711 may have moved, but in practice it gives the correct answer
13712 often enough until a better solution is found. */
13713 int have_ambiguous_names = ambiguous_names_p (b->loc);
13714
0d381245
VP
13715 for (; e; e = e->next)
13716 {
13717 if (!e->enabled && e->function_name)
13718 {
13719 struct bp_location *l = b->loc;
776592bf
DE
13720 if (have_ambiguous_names)
13721 {
13722 for (; l; l = l->next)
f1310107 13723 if (breakpoint_locations_match (e, l))
776592bf
DE
13724 {
13725 l->enabled = 0;
13726 break;
13727 }
13728 }
13729 else
13730 {
13731 for (; l; l = l->next)
13732 if (l->function_name
13733 && strcmp (e->function_name, l->function_name) == 0)
13734 {
13735 l->enabled = 0;
13736 break;
13737 }
13738 }
0d381245
VP
13739 }
13740 }
13741 }
fe3f5fa8 13742
8d3788bd
VP
13743 if (!locations_are_equal (existing_locations, b->loc))
13744 observer_notify_breakpoint_modified (b);
13745
b60e7edf 13746 update_global_location_list (1);
fe3f5fa8
VP
13747}
13748
ef23e705
TJB
13749/* Find the SaL locations corresponding to the given ADDR_STRING.
13750 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13751
13752static struct symtabs_and_lines
13753addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
13754{
13755 char *s;
02d20e4a 13756 struct symtabs_and_lines sals = {0};
f8eba3c6 13757 volatile struct gdb_exception e;
ef23e705 13758
983af33b 13759 gdb_assert (b->ops != NULL);
ef23e705 13760 s = addr_string;
ef23e705
TJB
13761
13762 TRY_CATCH (e, RETURN_MASK_ERROR)
13763 {
983af33b 13764 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
13765 }
13766 if (e.reason < 0)
13767 {
13768 int not_found_and_ok = 0;
13769 /* For pending breakpoints, it's expected that parsing will
13770 fail until the right shared library is loaded. User has
13771 already told to create pending breakpoints and don't need
13772 extra messages. If breakpoint is in bp_shlib_disabled
13773 state, then user already saw the message about that
13774 breakpoint being disabled, and don't want to see more
13775 errors. */
58438ac1 13776 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
13777 && (b->condition_not_parsed
13778 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13779 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13780 || b->enable_state == bp_disabled))
13781 not_found_and_ok = 1;
13782
13783 if (!not_found_and_ok)
13784 {
13785 /* We surely don't want to warn about the same breakpoint
13786 10 times. One solution, implemented here, is disable
13787 the breakpoint on error. Another solution would be to
13788 have separate 'warning emitted' flag. Since this
13789 happens only when a binary has changed, I don't know
13790 which approach is better. */
13791 b->enable_state = bp_disabled;
13792 throw_exception (e);
13793 }
13794 }
13795
58438ac1 13796 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 13797 {
f8eba3c6 13798 int i;
ef23e705 13799
f8eba3c6
TT
13800 for (i = 0; i < sals.nelts; ++i)
13801 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
13802 if (b->condition_not_parsed && s && s[0])
13803 {
13804 char *cond_string = 0;
13805 int thread = -1;
13806 int task = 0;
e7e0cddf 13807 char *extra_string = NULL;
ef23e705
TJB
13808
13809 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
13810 &cond_string, &thread, &task,
13811 &extra_string);
ef23e705
TJB
13812 if (cond_string)
13813 b->cond_string = cond_string;
13814 b->thread = thread;
13815 b->task = task;
e7e0cddf
SS
13816 if (extra_string)
13817 b->extra_string = extra_string;
ef23e705
TJB
13818 b->condition_not_parsed = 0;
13819 }
13820
983af33b 13821 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 13822 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 13823
58438ac1
TT
13824 *found = 1;
13825 }
13826 else
13827 *found = 0;
ef23e705
TJB
13828
13829 return sals;
13830}
13831
348d480f
PA
13832/* The default re_set method, for typical hardware or software
13833 breakpoints. Reevaluate the breakpoint and recreate its
13834 locations. */
13835
13836static void
28010a5d 13837breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
13838{
13839 int found;
f1310107 13840 struct symtabs_and_lines sals, sals_end;
ef23e705 13841 struct symtabs_and_lines expanded = {0};
f1310107 13842 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
13843
13844 sals = addr_string_to_sals (b, b->addr_string, &found);
13845 if (found)
13846 {
13847 make_cleanup (xfree, sals.sals);
f8eba3c6 13848 expanded = sals;
ef23e705
TJB
13849 }
13850
f1310107
TJB
13851 if (b->addr_string_range_end)
13852 {
13853 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
13854 if (found)
13855 {
13856 make_cleanup (xfree, sals_end.sals);
f8eba3c6 13857 expanded_end = sals_end;
f1310107
TJB
13858 }
13859 }
13860
13861 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
13862}
13863
983af33b
SDJ
13864/* Default method for creating SALs from an address string. It basically
13865 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13866
13867static void
13868create_sals_from_address_default (char **arg,
13869 struct linespec_result *canonical,
13870 enum bptype type_wanted,
13871 char *addr_start, char **copy_arg)
13872{
13873 parse_breakpoint_sals (arg, canonical);
13874}
13875
13876/* Call create_breakpoints_sal for the given arguments. This is the default
13877 function for the `create_breakpoints_sal' method of
13878 breakpoint_ops. */
13879
13880static void
13881create_breakpoints_sal_default (struct gdbarch *gdbarch,
13882 struct linespec_result *canonical,
13883 struct linespec_sals *lsal,
13884 char *cond_string,
e7e0cddf 13885 char *extra_string,
983af33b
SDJ
13886 enum bptype type_wanted,
13887 enum bpdisp disposition,
13888 int thread,
13889 int task, int ignore_count,
13890 const struct breakpoint_ops *ops,
13891 int from_tty, int enabled,
44f238bb 13892 int internal, unsigned flags)
983af33b
SDJ
13893{
13894 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 13895 extra_string,
983af33b
SDJ
13896 type_wanted, disposition,
13897 thread, task, ignore_count, ops, from_tty,
44f238bb 13898 enabled, internal, flags);
983af33b
SDJ
13899}
13900
13901/* Decode the line represented by S by calling decode_line_full. This is the
13902 default function for the `decode_linespec' method of breakpoint_ops. */
13903
13904static void
13905decode_linespec_default (struct breakpoint *b, char **s,
13906 struct symtabs_and_lines *sals)
13907{
13908 struct linespec_result canonical;
13909
13910 init_linespec_result (&canonical);
13911 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
13912 (struct symtab *) NULL, 0,
13913 &canonical, multiple_symbols_all,
13914 b->filter);
13915
13916 /* We should get 0 or 1 resulting SALs. */
13917 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
13918
13919 if (VEC_length (linespec_sals, canonical.sals) > 0)
13920 {
13921 struct linespec_sals *lsal;
13922
13923 lsal = VEC_index (linespec_sals, canonical.sals, 0);
13924 *sals = lsal->sals;
13925 /* Arrange it so the destructor does not free the
13926 contents. */
13927 lsal->sals.sals = NULL;
13928 }
13929
13930 destroy_linespec_result (&canonical);
13931}
13932
28010a5d
PA
13933/* Prepare the global context for a re-set of breakpoint B. */
13934
13935static struct cleanup *
13936prepare_re_set_context (struct breakpoint *b)
13937{
13938 struct cleanup *cleanups;
13939
13940 input_radix = b->input_radix;
13941 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
13942 if (b->pspace != NULL)
13943 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
13944 set_language (b->language);
13945
13946 return cleanups;
ef23e705
TJB
13947}
13948
c906108c
SS
13949/* Reset a breakpoint given it's struct breakpoint * BINT.
13950 The value we return ends up being the return value from catch_errors.
13951 Unused in this case. */
13952
13953static int
4efb68b1 13954breakpoint_re_set_one (void *bint)
c906108c 13955{
4a64f543 13956 /* Get past catch_errs. */
53a5351d 13957 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 13958 struct cleanup *cleanups;
c906108c 13959
348d480f
PA
13960 cleanups = prepare_re_set_context (b);
13961 b->ops->re_set (b);
13962 do_cleanups (cleanups);
c906108c
SS
13963 return 0;
13964}
13965
69de3c6a 13966/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 13967void
69de3c6a 13968breakpoint_re_set (void)
c906108c 13969{
35df4500 13970 struct breakpoint *b, *b_tmp;
c906108c
SS
13971 enum language save_language;
13972 int save_input_radix;
6c95b8df 13973 struct cleanup *old_chain;
c5aa993b 13974
c906108c
SS
13975 save_language = current_language->la_language;
13976 save_input_radix = input_radix;
6c95b8df
PA
13977 old_chain = save_current_program_space ();
13978
35df4500 13979 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 13980 {
4a64f543 13981 /* Format possible error msg. */
fe3f5fa8 13982 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
13983 b->number);
13984 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 13985 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 13986 do_cleanups (cleanups);
c5aa993b 13987 }
c906108c
SS
13988 set_language (save_language);
13989 input_radix = save_input_radix;
e62c965a 13990
0756c555 13991 jit_breakpoint_re_set ();
4efc6507 13992
6c95b8df
PA
13993 do_cleanups (old_chain);
13994
af02033e
PP
13995 create_overlay_event_breakpoint ();
13996 create_longjmp_master_breakpoint ();
13997 create_std_terminate_master_breakpoint ();
186c406b 13998 create_exception_master_breakpoint ();
1bfeeb0f
JL
13999
14000 /* While we're at it, reset the skip list too. */
14001 skip_re_set ();
c906108c
SS
14002}
14003\f
c906108c
SS
14004/* Reset the thread number of this breakpoint:
14005
14006 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14007 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14008void
fba45db2 14009breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14010{
14011 if (b->thread != -1)
14012 {
39f77062
KB
14013 if (in_thread_list (inferior_ptid))
14014 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14015
14016 /* We're being called after following a fork. The new fork is
14017 selected as current, and unless this was a vfork will have a
14018 different program space from the original thread. Reset that
14019 as well. */
14020 b->loc->pspace = current_program_space;
c906108c
SS
14021 }
14022}
14023
03ac34d5
MS
14024/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14025 If from_tty is nonzero, it prints a message to that effect,
14026 which ends with a period (no newline). */
14027
c906108c 14028void
fba45db2 14029set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14030{
52f0bd74 14031 struct breakpoint *b;
c906108c
SS
14032
14033 if (count < 0)
14034 count = 0;
14035
14036 ALL_BREAKPOINTS (b)
14037 if (b->number == bptnum)
c5aa993b 14038 {
d77f58be
SS
14039 if (is_tracepoint (b))
14040 {
14041 if (from_tty && count != 0)
14042 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14043 bptnum);
14044 return;
14045 }
14046
c5aa993b 14047 b->ignore_count = count;
221ea385
KS
14048 if (from_tty)
14049 {
14050 if (count == 0)
3e43a32a
MS
14051 printf_filtered (_("Will stop next time "
14052 "breakpoint %d is reached."),
221ea385
KS
14053 bptnum);
14054 else if (count == 1)
a3f17187 14055 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14056 bptnum);
14057 else
3e43a32a
MS
14058 printf_filtered (_("Will ignore next %d "
14059 "crossings of breakpoint %d."),
221ea385
KS
14060 count, bptnum);
14061 }
c5aa993b 14062 breakpoints_changed ();
8d3788bd 14063 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14064 return;
14065 }
c906108c 14066
8a3fe4f8 14067 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14068}
14069
c906108c
SS
14070/* Command to set ignore-count of breakpoint N to COUNT. */
14071
14072static void
fba45db2 14073ignore_command (char *args, int from_tty)
c906108c
SS
14074{
14075 char *p = args;
52f0bd74 14076 int num;
c906108c
SS
14077
14078 if (p == 0)
e2e0b3e5 14079 error_no_arg (_("a breakpoint number"));
c5aa993b 14080
c906108c 14081 num = get_number (&p);
5c44784c 14082 if (num == 0)
8a3fe4f8 14083 error (_("bad breakpoint number: '%s'"), args);
c906108c 14084 if (*p == 0)
8a3fe4f8 14085 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14086
14087 set_ignore_count (num,
14088 longest_to_int (value_as_long (parse_and_eval (p))),
14089 from_tty);
221ea385
KS
14090 if (from_tty)
14091 printf_filtered ("\n");
c906108c
SS
14092}
14093\f
14094/* Call FUNCTION on each of the breakpoints
14095 whose numbers are given in ARGS. */
14096
14097static void
95a42b64
TT
14098map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14099 void *),
14100 void *data)
c906108c 14101{
52f0bd74
AC
14102 int num;
14103 struct breakpoint *b, *tmp;
11cf8741 14104 int match;
197f0a60 14105 struct get_number_or_range_state state;
c906108c 14106
197f0a60 14107 if (args == 0)
e2e0b3e5 14108 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14109
197f0a60
TT
14110 init_number_or_range (&state, args);
14111
14112 while (!state.finished)
c906108c 14113 {
197f0a60
TT
14114 char *p = state.string;
14115
11cf8741 14116 match = 0;
c5aa993b 14117
197f0a60 14118 num = get_number_or_range (&state);
5c44784c 14119 if (num == 0)
c5aa993b 14120 {
8a3fe4f8 14121 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14122 }
14123 else
14124 {
14125 ALL_BREAKPOINTS_SAFE (b, tmp)
14126 if (b->number == num)
14127 {
11cf8741 14128 match = 1;
cdac0397 14129 function (b, data);
11cf8741 14130 break;
5c44784c 14131 }
11cf8741 14132 if (match == 0)
a3f17187 14133 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14134 }
c906108c
SS
14135 }
14136}
14137
0d381245
VP
14138static struct bp_location *
14139find_location_by_number (char *number)
14140{
14141 char *dot = strchr (number, '.');
14142 char *p1;
14143 int bp_num;
14144 int loc_num;
14145 struct breakpoint *b;
14146 struct bp_location *loc;
14147
14148 *dot = '\0';
14149
14150 p1 = number;
197f0a60 14151 bp_num = get_number (&p1);
0d381245
VP
14152 if (bp_num == 0)
14153 error (_("Bad breakpoint number '%s'"), number);
14154
14155 ALL_BREAKPOINTS (b)
14156 if (b->number == bp_num)
14157 {
14158 break;
14159 }
14160
14161 if (!b || b->number != bp_num)
14162 error (_("Bad breakpoint number '%s'"), number);
14163
14164 p1 = dot+1;
197f0a60 14165 loc_num = get_number (&p1);
0d381245
VP
14166 if (loc_num == 0)
14167 error (_("Bad breakpoint location number '%s'"), number);
14168
14169 --loc_num;
14170 loc = b->loc;
14171 for (;loc_num && loc; --loc_num, loc = loc->next)
14172 ;
14173 if (!loc)
14174 error (_("Bad breakpoint location number '%s'"), dot+1);
14175
14176 return loc;
14177}
14178
14179
1900040c
MS
14180/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14181 If from_tty is nonzero, it prints a message to that effect,
14182 which ends with a period (no newline). */
14183
c906108c 14184void
fba45db2 14185disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14186{
14187 /* Never disable a watchpoint scope breakpoint; we want to
14188 hit them when we leave scope so we can delete both the
14189 watchpoint and its scope breakpoint at that time. */
14190 if (bpt->type == bp_watchpoint_scope)
14191 return;
14192
c2c6d25f 14193 /* You can't disable permanent breakpoints. */
b5de0fa7 14194 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
14195 return;
14196
b5de0fa7 14197 bpt->enable_state = bp_disabled;
c906108c 14198
b775012e
LM
14199 /* Mark breakpoint locations modified. */
14200 mark_breakpoint_modified (bpt);
14201
d248b706
KY
14202 if (target_supports_enable_disable_tracepoint ()
14203 && current_trace_status ()->running && is_tracepoint (bpt))
14204 {
14205 struct bp_location *location;
14206
14207 for (location = bpt->loc; location; location = location->next)
14208 target_disable_tracepoint (location);
14209 }
14210
b60e7edf 14211 update_global_location_list (0);
c906108c 14212
8d3788bd 14213 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14214}
14215
51be5b68
PA
14216/* A callback for iterate_over_related_breakpoints. */
14217
14218static void
14219do_disable_breakpoint (struct breakpoint *b, void *ignore)
14220{
14221 disable_breakpoint (b);
14222}
14223
95a42b64
TT
14224/* A callback for map_breakpoint_numbers that calls
14225 disable_breakpoint. */
14226
14227static void
14228do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14229{
51be5b68 14230 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14231}
14232
c906108c 14233static void
fba45db2 14234disable_command (char *args, int from_tty)
c906108c 14235{
c906108c 14236 if (args == 0)
46c6471b
PA
14237 {
14238 struct breakpoint *bpt;
14239
14240 ALL_BREAKPOINTS (bpt)
14241 if (user_breakpoint_p (bpt))
14242 disable_breakpoint (bpt);
14243 }
0d381245
VP
14244 else if (strchr (args, '.'))
14245 {
14246 struct bp_location *loc = find_location_by_number (args);
14247 if (loc)
d248b706 14248 {
b775012e
LM
14249 if (loc->enabled)
14250 {
14251 loc->enabled = 0;
14252 mark_breakpoint_location_modified (loc);
14253 }
d248b706
KY
14254 if (target_supports_enable_disable_tracepoint ()
14255 && current_trace_status ()->running && loc->owner
14256 && is_tracepoint (loc->owner))
14257 target_disable_tracepoint (loc);
14258 }
b60e7edf 14259 update_global_location_list (0);
0d381245 14260 }
c906108c 14261 else
95a42b64 14262 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
14263}
14264
14265static void
816338b5
SS
14266enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14267 int count)
c906108c 14268{
afe38095 14269 int target_resources_ok;
c906108c
SS
14270
14271 if (bpt->type == bp_hardware_breakpoint)
14272 {
14273 int i;
c5aa993b 14274 i = hw_breakpoint_used_count ();
53a5351d 14275 target_resources_ok =
d92524f1 14276 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14277 i + 1, 0);
c906108c 14278 if (target_resources_ok == 0)
8a3fe4f8 14279 error (_("No hardware breakpoint support in the target."));
c906108c 14280 else if (target_resources_ok < 0)
8a3fe4f8 14281 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14282 }
14283
cc60f2e3 14284 if (is_watchpoint (bpt))
c906108c 14285 {
d07205c2
JK
14286 /* Initialize it just to avoid a GCC false warning. */
14287 enum enable_state orig_enable_state = 0;
bfd189b1 14288 volatile struct gdb_exception e;
dde02812
ES
14289
14290 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 14291 {
3a5c3e22
PA
14292 struct watchpoint *w = (struct watchpoint *) bpt;
14293
1e718ff1
TJB
14294 orig_enable_state = bpt->enable_state;
14295 bpt->enable_state = bp_enabled;
3a5c3e22 14296 update_watchpoint (w, 1 /* reparse */);
c906108c 14297 }
dde02812 14298 if (e.reason < 0)
c5aa993b 14299 {
1e718ff1 14300 bpt->enable_state = orig_enable_state;
dde02812
ES
14301 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14302 bpt->number);
14303 return;
c5aa993b 14304 }
c906108c 14305 }
0101ce28 14306
b4c291bb
KH
14307 if (bpt->enable_state != bp_permanent)
14308 bpt->enable_state = bp_enabled;
d248b706 14309
b775012e
LM
14310 bpt->enable_state = bp_enabled;
14311
14312 /* Mark breakpoint locations modified. */
14313 mark_breakpoint_modified (bpt);
14314
d248b706
KY
14315 if (target_supports_enable_disable_tracepoint ()
14316 && current_trace_status ()->running && is_tracepoint (bpt))
14317 {
14318 struct bp_location *location;
14319
14320 for (location = bpt->loc; location; location = location->next)
14321 target_enable_tracepoint (location);
14322 }
14323
b4c291bb 14324 bpt->disposition = disposition;
816338b5 14325 bpt->enable_count = count;
b60e7edf 14326 update_global_location_list (1);
b4c291bb
KH
14327 breakpoints_changed ();
14328
8d3788bd 14329 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14330}
14331
fe3f5fa8 14332
c906108c 14333void
fba45db2 14334enable_breakpoint (struct breakpoint *bpt)
c906108c 14335{
816338b5 14336 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14337}
14338
14339static void
14340do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14341{
14342 enable_breakpoint (bpt);
c906108c
SS
14343}
14344
95a42b64
TT
14345/* A callback for map_breakpoint_numbers that calls
14346 enable_breakpoint. */
14347
14348static void
14349do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14350{
51be5b68 14351 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14352}
14353
c906108c
SS
14354/* The enable command enables the specified breakpoints (or all defined
14355 breakpoints) so they once again become (or continue to be) effective
1272ad14 14356 in stopping the inferior. */
c906108c 14357
c906108c 14358static void
fba45db2 14359enable_command (char *args, int from_tty)
c906108c 14360{
c906108c 14361 if (args == 0)
46c6471b
PA
14362 {
14363 struct breakpoint *bpt;
14364
14365 ALL_BREAKPOINTS (bpt)
14366 if (user_breakpoint_p (bpt))
14367 enable_breakpoint (bpt);
14368 }
0d381245
VP
14369 else if (strchr (args, '.'))
14370 {
14371 struct bp_location *loc = find_location_by_number (args);
14372 if (loc)
d248b706 14373 {
b775012e
LM
14374 if (!loc->enabled)
14375 {
14376 loc->enabled = 1;
14377 mark_breakpoint_location_modified (loc);
14378 }
d248b706
KY
14379 if (target_supports_enable_disable_tracepoint ()
14380 && current_trace_status ()->running && loc->owner
14381 && is_tracepoint (loc->owner))
14382 target_enable_tracepoint (loc);
14383 }
b60e7edf 14384 update_global_location_list (1);
0d381245 14385 }
c906108c 14386 else
95a42b64 14387 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
14388}
14389
816338b5
SS
14390/* This struct packages up disposition data for application to multiple
14391 breakpoints. */
14392
14393struct disp_data
14394{
14395 enum bpdisp disp;
14396 int count;
14397};
14398
c906108c 14399static void
51be5b68
PA
14400do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14401{
816338b5 14402 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14403
816338b5 14404 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14405}
14406
14407static void
14408do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14409{
816338b5 14410 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14411
14412 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14413}
14414
c906108c 14415static void
fba45db2 14416enable_once_command (char *args, int from_tty)
c906108c 14417{
51be5b68 14418 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14419}
14420
816338b5
SS
14421static void
14422do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14423{
14424 struct disp_data disp = { disp_disable, *(int *) countptr };
14425
14426 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14427}
14428
14429static void
14430enable_count_command (char *args, int from_tty)
14431{
14432 int count = get_number (&args);
14433
14434 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14435}
14436
c906108c 14437static void
51be5b68 14438do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14439{
816338b5 14440 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14441
14442 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14443}
14444
c906108c 14445static void
fba45db2 14446enable_delete_command (char *args, int from_tty)
c906108c 14447{
51be5b68 14448 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14449}
14450\f
fa8d40ab
JJ
14451static void
14452set_breakpoint_cmd (char *args, int from_tty)
14453{
14454}
14455
14456static void
14457show_breakpoint_cmd (char *args, int from_tty)
14458{
14459}
14460
1f3b5d1b
PP
14461/* Invalidate last known value of any hardware watchpoint if
14462 the memory which that value represents has been written to by
14463 GDB itself. */
14464
14465static void
14466invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
14467 const bfd_byte *data)
14468{
14469 struct breakpoint *bp;
14470
14471 ALL_BREAKPOINTS (bp)
14472 if (bp->enable_state == bp_enabled
3a5c3e22 14473 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14474 {
3a5c3e22 14475 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14476
3a5c3e22
PA
14477 if (wp->val_valid && wp->val)
14478 {
14479 struct bp_location *loc;
14480
14481 for (loc = bp->loc; loc != NULL; loc = loc->next)
14482 if (loc->loc_type == bp_loc_hardware_watchpoint
14483 && loc->address + loc->length > addr
14484 && addr + len > loc->address)
14485 {
14486 value_free (wp->val);
14487 wp->val = NULL;
14488 wp->val_valid = 0;
14489 }
14490 }
1f3b5d1b
PP
14491 }
14492}
14493
1bfeeb0f
JL
14494/* Use the last displayed codepoint's values, or nothing
14495 if they aren't valid. */
c906108c
SS
14496
14497struct symtabs_and_lines
f8eba3c6 14498decode_line_spec_1 (char *string, int flags)
c906108c
SS
14499{
14500 struct symtabs_and_lines sals;
cc59ec59 14501
c906108c 14502 if (string == 0)
8a3fe4f8 14503 error (_("Empty line specification."));
1bfeeb0f 14504 if (last_displayed_sal_is_valid ())
f8eba3c6 14505 sals = decode_line_1 (&string, flags,
1bfeeb0f 14506 get_last_displayed_symtab (),
f8eba3c6 14507 get_last_displayed_line ());
c906108c 14508 else
f8eba3c6 14509 sals = decode_line_1 (&string, flags, (struct symtab *) NULL, 0);
c906108c 14510 if (*string)
8a3fe4f8 14511 error (_("Junk at end of line specification: %s"), string);
c906108c
SS
14512 return sals;
14513}
8181d85f
DJ
14514
14515/* Create and insert a raw software breakpoint at PC. Return an
14516 identifier, which should be used to remove the breakpoint later.
14517 In general, places which call this should be using something on the
14518 breakpoint chain instead; this function should be eliminated
14519 someday. */
14520
14521void *
6c95b8df
PA
14522deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
14523 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
14524{
14525 struct bp_target_info *bp_tgt;
14526
6c95b8df 14527 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 14528
6c95b8df 14529 bp_tgt->placed_address_space = aspace;
8181d85f 14530 bp_tgt->placed_address = pc;
6c95b8df 14531
a6d9a66e 14532 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
14533 {
14534 /* Could not insert the breakpoint. */
14535 xfree (bp_tgt);
14536 return NULL;
14537 }
14538
14539 return bp_tgt;
14540}
14541
4a64f543
MS
14542/* Remove a breakpoint BP inserted by
14543 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
14544
14545int
a6d9a66e 14546deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
14547{
14548 struct bp_target_info *bp_tgt = bp;
14549 int ret;
14550
a6d9a66e 14551 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
14552 xfree (bp_tgt);
14553
14554 return ret;
14555}
14556
4a64f543
MS
14557/* One (or perhaps two) breakpoints used for software single
14558 stepping. */
8181d85f
DJ
14559
14560static void *single_step_breakpoints[2];
a6d9a66e 14561static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
14562
14563/* Create and insert a breakpoint for software single step. */
14564
14565void
6c95b8df 14566insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14567 struct address_space *aspace,
14568 CORE_ADDR next_pc)
8181d85f
DJ
14569{
14570 void **bpt_p;
14571
14572 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
14573 {
14574 bpt_p = &single_step_breakpoints[0];
14575 single_step_gdbarch[0] = gdbarch;
14576 }
8181d85f
DJ
14577 else
14578 {
14579 gdb_assert (single_step_breakpoints[1] == NULL);
14580 bpt_p = &single_step_breakpoints[1];
a6d9a66e 14581 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
14582 }
14583
4a64f543
MS
14584 /* NOTE drow/2006-04-11: A future improvement to this function would
14585 be to only create the breakpoints once, and actually put them on
14586 the breakpoint chain. That would let us use set_raw_breakpoint.
14587 We could adjust the addresses each time they were needed. Doing
14588 this requires corresponding changes elsewhere where single step
14589 breakpoints are handled, however. So, for now, we use this. */
8181d85f 14590
6c95b8df 14591 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 14592 if (*bpt_p == NULL)
5af949e3
UW
14593 error (_("Could not insert single-step breakpoint at %s"),
14594 paddress (gdbarch, next_pc));
8181d85f
DJ
14595}
14596
f02253f1
HZ
14597/* Check if the breakpoints used for software single stepping
14598 were inserted or not. */
14599
14600int
14601single_step_breakpoints_inserted (void)
14602{
14603 return (single_step_breakpoints[0] != NULL
14604 || single_step_breakpoints[1] != NULL);
14605}
14606
8181d85f
DJ
14607/* Remove and delete any breakpoints used for software single step. */
14608
14609void
14610remove_single_step_breakpoints (void)
14611{
14612 gdb_assert (single_step_breakpoints[0] != NULL);
14613
14614 /* See insert_single_step_breakpoint for more about this deprecated
14615 call. */
a6d9a66e
UW
14616 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
14617 single_step_breakpoints[0]);
14618 single_step_gdbarch[0] = NULL;
8181d85f
DJ
14619 single_step_breakpoints[0] = NULL;
14620
14621 if (single_step_breakpoints[1] != NULL)
14622 {
a6d9a66e
UW
14623 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
14624 single_step_breakpoints[1]);
14625 single_step_gdbarch[1] = NULL;
8181d85f
DJ
14626 single_step_breakpoints[1] = NULL;
14627 }
14628}
14629
d03285ec
UW
14630/* Delete software single step breakpoints without removing them from
14631 the inferior. This is intended to be used if the inferior's address
14632 space where they were inserted is already gone, e.g. after exit or
14633 exec. */
14634
14635void
14636cancel_single_step_breakpoints (void)
14637{
14638 int i;
14639
14640 for (i = 0; i < 2; i++)
14641 if (single_step_breakpoints[i])
14642 {
14643 xfree (single_step_breakpoints[i]);
14644 single_step_breakpoints[i] = NULL;
14645 single_step_gdbarch[i] = NULL;
14646 }
14647}
14648
14649/* Detach software single-step breakpoints from INFERIOR_PTID without
14650 removing them. */
14651
14652static void
14653detach_single_step_breakpoints (void)
14654{
14655 int i;
14656
14657 for (i = 0; i < 2; i++)
14658 if (single_step_breakpoints[i])
14659 target_remove_breakpoint (single_step_gdbarch[i],
14660 single_step_breakpoints[i]);
14661}
14662
4a64f543
MS
14663/* Check whether a software single-step breakpoint is inserted at
14664 PC. */
1aafd4da
UW
14665
14666static int
cc59ec59
MS
14667single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14668 CORE_ADDR pc)
1aafd4da
UW
14669{
14670 int i;
14671
14672 for (i = 0; i < 2; i++)
14673 {
14674 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
14675 if (bp_tgt
14676 && breakpoint_address_match (bp_tgt->placed_address_space,
14677 bp_tgt->placed_address,
14678 aspace, pc))
1aafd4da
UW
14679 return 1;
14680 }
14681
14682 return 0;
14683}
14684
a96d9b2e
SDJ
14685/* Returns 0 if 'bp' is NOT a syscall catchpoint,
14686 non-zero otherwise. */
14687static int
14688is_syscall_catchpoint_enabled (struct breakpoint *bp)
14689{
14690 if (syscall_catchpoint_p (bp)
14691 && bp->enable_state != bp_disabled
14692 && bp->enable_state != bp_call_disabled)
14693 return 1;
14694 else
14695 return 0;
14696}
14697
14698int
14699catch_syscall_enabled (void)
14700{
fa3064dd
YQ
14701 struct catch_syscall_inferior_data *inf_data
14702 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 14703
fa3064dd 14704 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
14705}
14706
14707int
14708catching_syscall_number (int syscall_number)
14709{
14710 struct breakpoint *bp;
14711
14712 ALL_BREAKPOINTS (bp)
14713 if (is_syscall_catchpoint_enabled (bp))
14714 {
be5c67c1
PA
14715 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
14716
14717 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
14718 {
14719 int i, iter;
14720 for (i = 0;
be5c67c1 14721 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
14722 i++)
14723 if (syscall_number == iter)
14724 return 1;
14725 }
14726 else
14727 return 1;
14728 }
14729
14730 return 0;
14731}
14732
14733/* Complete syscall names. Used by "catch syscall". */
49c4e619 14734static VEC (char_ptr) *
a96d9b2e
SDJ
14735catch_syscall_completer (struct cmd_list_element *cmd,
14736 char *text, char *word)
14737{
14738 const char **list = get_syscall_names ();
49c4e619 14739 VEC (char_ptr) *retlist
c38eea1a 14740 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 14741
c38eea1a
MS
14742 xfree (list);
14743 return retlist;
a96d9b2e
SDJ
14744}
14745
1042e4c0
SS
14746/* Tracepoint-specific operations. */
14747
14748/* Set tracepoint count to NUM. */
14749static void
14750set_tracepoint_count (int num)
14751{
14752 tracepoint_count = num;
4fa62494 14753 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14754}
14755
70221824 14756static void
1042e4c0
SS
14757trace_command (char *arg, int from_tty)
14758{
55aa24fb
SDJ
14759 struct breakpoint_ops *ops;
14760 const char *arg_cp = arg;
14761
14762 if (arg && probe_linespec_to_ops (&arg_cp))
14763 ops = &tracepoint_probe_breakpoint_ops;
14764 else
14765 ops = &tracepoint_breakpoint_ops;
14766
8cdf0e15
VP
14767 if (create_breakpoint (get_current_arch (),
14768 arg,
e7e0cddf 14769 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b
PA
14770 0 /* tempflag */,
14771 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
14772 0 /* Ignore count */,
14773 pending_break_support,
55aa24fb 14774 ops,
8cdf0e15 14775 from_tty,
84f4c1fe 14776 1 /* enabled */,
44f238bb 14777 0 /* internal */, 0))
fd9b8c24 14778 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
14779}
14780
70221824 14781static void
7a697b8d
SS
14782ftrace_command (char *arg, int from_tty)
14783{
8cdf0e15
VP
14784 if (create_breakpoint (get_current_arch (),
14785 arg,
e7e0cddf 14786 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b
PA
14787 0 /* tempflag */,
14788 bp_fast_tracepoint /* type_wanted */,
14789 0 /* Ignore count */,
14790 pending_break_support,
348d480f 14791 &tracepoint_breakpoint_ops,
0fb4aa4b 14792 from_tty,
84f4c1fe 14793 1 /* enabled */,
44f238bb 14794 0 /* internal */, 0))
0fb4aa4b
PA
14795 set_tracepoint_count (breakpoint_count);
14796}
14797
14798/* strace command implementation. Creates a static tracepoint. */
14799
70221824 14800static void
0fb4aa4b
PA
14801strace_command (char *arg, int from_tty)
14802{
983af33b
SDJ
14803 struct breakpoint_ops *ops;
14804
14805 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14806 or with a normal static tracepoint. */
14807 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
14808 ops = &strace_marker_breakpoint_ops;
14809 else
14810 ops = &tracepoint_breakpoint_ops;
14811
0fb4aa4b
PA
14812 if (create_breakpoint (get_current_arch (),
14813 arg,
e7e0cddf 14814 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b
PA
14815 0 /* tempflag */,
14816 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
14817 0 /* Ignore count */,
14818 pending_break_support,
983af33b 14819 ops,
8cdf0e15 14820 from_tty,
84f4c1fe 14821 1 /* enabled */,
44f238bb 14822 0 /* internal */, 0))
fd9b8c24 14823 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
14824}
14825
409873ef
SS
14826/* Set up a fake reader function that gets command lines from a linked
14827 list that was acquired during tracepoint uploading. */
14828
14829static struct uploaded_tp *this_utp;
3149d8c1 14830static int next_cmd;
409873ef
SS
14831
14832static char *
14833read_uploaded_action (void)
14834{
14835 char *rslt;
14836
3149d8c1 14837 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 14838
3149d8c1 14839 next_cmd++;
409873ef
SS
14840
14841 return rslt;
14842}
14843
00bf0b85
SS
14844/* Given information about a tracepoint as recorded on a target (which
14845 can be either a live system or a trace file), attempt to create an
14846 equivalent GDB tracepoint. This is not a reliable process, since
14847 the target does not necessarily have all the information used when
14848 the tracepoint was originally defined. */
14849
d9b3f62e 14850struct tracepoint *
00bf0b85 14851create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14852{
409873ef 14853 char *addr_str, small_buf[100];
d9b3f62e 14854 struct tracepoint *tp;
fd9b8c24 14855
409873ef
SS
14856 if (utp->at_string)
14857 addr_str = utp->at_string;
14858 else
14859 {
14860 /* In the absence of a source location, fall back to raw
14861 address. Since there is no way to confirm that the address
14862 means the same thing as when the trace was started, warn the
14863 user. */
3e43a32a
MS
14864 warning (_("Uploaded tracepoint %d has no "
14865 "source location, using raw address"),
409873ef
SS
14866 utp->number);
14867 sprintf (small_buf, "*%s", hex_string (utp->addr));
14868 addr_str = small_buf;
14869 }
14870
14871 /* There's not much we can do with a sequence of bytecodes. */
14872 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14873 warning (_("Uploaded tracepoint %d condition "
14874 "has no source form, ignoring it"),
409873ef 14875 utp->number);
d5551862 14876
8cdf0e15 14877 if (!create_breakpoint (get_current_arch (),
409873ef 14878 addr_str,
e7e0cddf
SS
14879 utp->cond_string, -1, NULL,
14880 0 /* parse cond/thread */,
8cdf0e15 14881 0 /* tempflag */,
0fb4aa4b 14882 utp->type /* type_wanted */,
8cdf0e15
VP
14883 0 /* Ignore count */,
14884 pending_break_support,
348d480f 14885 &tracepoint_breakpoint_ops,
8cdf0e15 14886 0 /* from_tty */,
84f4c1fe 14887 utp->enabled /* enabled */,
44f238bb
PA
14888 0 /* internal */,
14889 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
14890 return NULL;
14891
00bf0b85
SS
14892 set_tracepoint_count (breakpoint_count);
14893
409873ef 14894 /* Get the tracepoint we just created. */
fd9b8c24
PA
14895 tp = get_tracepoint (tracepoint_count);
14896 gdb_assert (tp != NULL);
d5551862 14897
00bf0b85
SS
14898 if (utp->pass > 0)
14899 {
d9b3f62e 14900 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
00bf0b85 14901
409873ef 14902 trace_pass_command (small_buf, 0);
00bf0b85
SS
14903 }
14904
409873ef
SS
14905 /* If we have uploaded versions of the original commands, set up a
14906 special-purpose "reader" function and call the usual command line
14907 reader, then pass the result to the breakpoint command-setting
14908 function. */
3149d8c1 14909 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 14910 {
409873ef 14911 struct command_line *cmd_list;
00bf0b85 14912
409873ef 14913 this_utp = utp;
3149d8c1 14914 next_cmd = 0;
d5551862 14915
409873ef
SS
14916 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14917
d9b3f62e 14918 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 14919 }
3149d8c1
SS
14920 else if (!VEC_empty (char_ptr, utp->actions)
14921 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
14922 warning (_("Uploaded tracepoint %d actions "
14923 "have no source form, ignoring them"),
409873ef 14924 utp->number);
00bf0b85 14925
f196051f
SS
14926 /* Copy any status information that might be available. */
14927 tp->base.hit_count = utp->hit_count;
14928 tp->traceframe_usage = utp->traceframe_usage;
14929
00bf0b85 14930 return tp;
d9b3f62e 14931}
00bf0b85 14932
1042e4c0
SS
14933/* Print information on tracepoint number TPNUM_EXP, or all if
14934 omitted. */
14935
14936static void
e5a67952 14937tracepoints_info (char *args, int from_tty)
1042e4c0 14938{
79a45e25 14939 struct ui_out *uiout = current_uiout;
e5a67952 14940 int num_printed;
1042e4c0 14941
e5a67952 14942 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
14943
14944 if (num_printed == 0)
1042e4c0 14945 {
e5a67952 14946 if (args == NULL || *args == '\0')
d77f58be
SS
14947 ui_out_message (uiout, 0, "No tracepoints.\n");
14948 else
e5a67952 14949 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 14950 }
ad443146
SS
14951
14952 default_collect_info ();
1042e4c0
SS
14953}
14954
4a64f543 14955/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14956 Not supported by all targets. */
14957static void
14958enable_trace_command (char *args, int from_tty)
14959{
14960 enable_command (args, from_tty);
14961}
14962
4a64f543 14963/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14964 Not supported by all targets. */
14965static void
14966disable_trace_command (char *args, int from_tty)
14967{
14968 disable_command (args, from_tty);
14969}
14970
4a64f543 14971/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
14972static void
14973delete_trace_command (char *arg, int from_tty)
14974{
35df4500 14975 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14976
14977 dont_repeat ();
14978
14979 if (arg == 0)
14980 {
14981 int breaks_to_delete = 0;
14982
14983 /* Delete all breakpoints if no argument.
14984 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14985 have to be deleted with an explicit breakpoint number
14986 argument. */
1042e4c0 14987 ALL_TRACEPOINTS (b)
46c6471b 14988 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14989 {
14990 breaks_to_delete = 1;
14991 break;
14992 }
1042e4c0
SS
14993
14994 /* Ask user only if there are some breakpoints to delete. */
14995 if (!from_tty
14996 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14997 {
35df4500 14998 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14999 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 15000 delete_breakpoint (b);
1042e4c0
SS
15001 }
15002 }
15003 else
51be5b68 15004 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
15005}
15006
197f0a60
TT
15007/* Helper function for trace_pass_command. */
15008
15009static void
d9b3f62e 15010trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 15011{
d9b3f62e
PA
15012 tp->pass_count = count;
15013 observer_notify_tracepoint_modified (tp->base.number);
197f0a60
TT
15014 if (from_tty)
15015 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 15016 tp->base.number, count);
197f0a60
TT
15017}
15018
1042e4c0
SS
15019/* Set passcount for tracepoint.
15020
15021 First command argument is passcount, second is tracepoint number.
15022 If tracepoint number omitted, apply to most recently defined.
15023 Also accepts special argument "all". */
15024
15025static void
15026trace_pass_command (char *args, int from_tty)
15027{
d9b3f62e 15028 struct tracepoint *t1;
1042e4c0 15029 unsigned int count;
1042e4c0
SS
15030
15031 if (args == 0 || *args == 0)
3e43a32a
MS
15032 error (_("passcount command requires an "
15033 "argument (count + optional TP num)"));
1042e4c0 15034
4a64f543 15035 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
15036
15037 while (*args && isspace ((int) *args))
15038 args++;
15039
15040 if (*args && strncasecmp (args, "all", 3) == 0)
15041 {
d9b3f62e
PA
15042 struct breakpoint *b;
15043
1042e4c0 15044 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15045 if (*args)
15046 error (_("Junk at end of arguments."));
1042e4c0 15047
d9b3f62e 15048 ALL_TRACEPOINTS (b)
197f0a60 15049 {
d9b3f62e 15050 t1 = (struct tracepoint *) b;
197f0a60
TT
15051 trace_pass_set_count (t1, count, from_tty);
15052 }
15053 }
15054 else if (*args == '\0')
1042e4c0 15055 {
197f0a60 15056 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 15057 if (t1)
197f0a60
TT
15058 trace_pass_set_count (t1, count, from_tty);
15059 }
15060 else
15061 {
15062 struct get_number_or_range_state state;
15063
15064 init_number_or_range (&state, args);
15065 while (!state.finished)
1042e4c0 15066 {
197f0a60
TT
15067 t1 = get_tracepoint_by_number (&args, &state, 1);
15068 if (t1)
15069 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15070 }
15071 }
1042e4c0
SS
15072}
15073
d9b3f62e 15074struct tracepoint *
1042e4c0
SS
15075get_tracepoint (int num)
15076{
15077 struct breakpoint *t;
15078
15079 ALL_TRACEPOINTS (t)
15080 if (t->number == num)
d9b3f62e 15081 return (struct tracepoint *) t;
1042e4c0
SS
15082
15083 return NULL;
15084}
15085
d5551862
SS
15086/* Find the tracepoint with the given target-side number (which may be
15087 different from the tracepoint number after disconnecting and
15088 reconnecting). */
15089
d9b3f62e 15090struct tracepoint *
d5551862
SS
15091get_tracepoint_by_number_on_target (int num)
15092{
d9b3f62e 15093 struct breakpoint *b;
d5551862 15094
d9b3f62e
PA
15095 ALL_TRACEPOINTS (b)
15096 {
15097 struct tracepoint *t = (struct tracepoint *) b;
15098
15099 if (t->number_on_target == num)
15100 return t;
15101 }
d5551862
SS
15102
15103 return NULL;
15104}
15105
1042e4c0 15106/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
15107 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15108 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 15109 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 15110struct tracepoint *
197f0a60
TT
15111get_tracepoint_by_number (char **arg,
15112 struct get_number_or_range_state *state,
15113 int optional_p)
1042e4c0
SS
15114{
15115 extern int tracepoint_count;
15116 struct breakpoint *t;
15117 int tpnum;
15118 char *instring = arg == NULL ? NULL : *arg;
15119
197f0a60
TT
15120 if (state)
15121 {
15122 gdb_assert (!state->finished);
15123 tpnum = get_number_or_range (state);
15124 }
15125 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
15126 {
15127 if (optional_p)
15128 tpnum = tracepoint_count;
15129 else
15130 error_no_arg (_("tracepoint number"));
15131 }
15132 else
197f0a60 15133 tpnum = get_number (arg);
1042e4c0
SS
15134
15135 if (tpnum <= 0)
15136 {
15137 if (instring && *instring)
15138 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15139 instring);
15140 else
3e43a32a
MS
15141 printf_filtered (_("Tracepoint argument missing "
15142 "and no previous tracepoint\n"));
1042e4c0
SS
15143 return NULL;
15144 }
15145
15146 ALL_TRACEPOINTS (t)
15147 if (t->number == tpnum)
15148 {
d9b3f62e 15149 return (struct tracepoint *) t;
1042e4c0
SS
15150 }
15151
1042e4c0
SS
15152 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15153 return NULL;
15154}
15155
d9b3f62e
PA
15156void
15157print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15158{
15159 if (b->thread != -1)
15160 fprintf_unfiltered (fp, " thread %d", b->thread);
15161
15162 if (b->task != 0)
15163 fprintf_unfiltered (fp, " task %d", b->task);
15164
15165 fprintf_unfiltered (fp, "\n");
15166}
15167
6149aea9
PA
15168/* Save information on user settable breakpoints (watchpoints, etc) to
15169 a new script file named FILENAME. If FILTER is non-NULL, call it
15170 on each breakpoint and only include the ones for which it returns
15171 non-zero. */
15172
1042e4c0 15173static void
6149aea9
PA
15174save_breakpoints (char *filename, int from_tty,
15175 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15176{
15177 struct breakpoint *tp;
6149aea9 15178 int any = 0;
a7bdde9e 15179 char *pathname;
1042e4c0 15180 struct cleanup *cleanup;
a7bdde9e 15181 struct ui_file *fp;
6149aea9 15182 int extra_trace_bits = 0;
1042e4c0 15183
6149aea9
PA
15184 if (filename == 0 || *filename == 0)
15185 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15186
15187 /* See if we have anything to save. */
6149aea9 15188 ALL_BREAKPOINTS (tp)
1042e4c0 15189 {
6149aea9 15190 /* Skip internal and momentary breakpoints. */
09d682a4 15191 if (!user_breakpoint_p (tp))
6149aea9
PA
15192 continue;
15193
15194 /* If we have a filter, only save the breakpoints it accepts. */
15195 if (filter && !filter (tp))
15196 continue;
15197
15198 any = 1;
15199
15200 if (is_tracepoint (tp))
15201 {
15202 extra_trace_bits = 1;
15203
15204 /* We can stop searching. */
15205 break;
15206 }
1042e4c0 15207 }
6149aea9
PA
15208
15209 if (!any)
1042e4c0 15210 {
6149aea9 15211 warning (_("Nothing to save."));
1042e4c0
SS
15212 return;
15213 }
15214
6149aea9 15215 pathname = tilde_expand (filename);
1042e4c0 15216 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 15217 fp = gdb_fopen (pathname, "w");
059fb39f 15218 if (!fp)
6149aea9
PA
15219 error (_("Unable to open file '%s' for saving (%s)"),
15220 filename, safe_strerror (errno));
a7bdde9e 15221 make_cleanup_ui_file_delete (fp);
8bf6485c 15222
6149aea9
PA
15223 if (extra_trace_bits)
15224 save_trace_state_variables (fp);
8bf6485c 15225
6149aea9 15226 ALL_BREAKPOINTS (tp)
1042e4c0 15227 {
6149aea9 15228 /* Skip internal and momentary breakpoints. */
09d682a4 15229 if (!user_breakpoint_p (tp))
6149aea9 15230 continue;
8bf6485c 15231
6149aea9
PA
15232 /* If we have a filter, only save the breakpoints it accepts. */
15233 if (filter && !filter (tp))
15234 continue;
15235
348d480f 15236 tp->ops->print_recreate (tp, fp);
1042e4c0 15237
6149aea9
PA
15238 /* Note, we can't rely on tp->number for anything, as we can't
15239 assume the recreated breakpoint numbers will match. Use $bpnum
15240 instead. */
15241
15242 if (tp->cond_string)
15243 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15244
15245 if (tp->ignore_count)
15246 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15247
a7bdde9e 15248 if (tp->commands)
1042e4c0 15249 {
a7bdde9e
VP
15250 volatile struct gdb_exception ex;
15251
6149aea9 15252 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15253
79a45e25 15254 ui_out_redirect (current_uiout, fp);
14dba4b4 15255 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 15256 {
79a45e25 15257 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15258 }
79a45e25 15259 ui_out_redirect (current_uiout, NULL);
1042e4c0 15260
a7bdde9e
VP
15261 if (ex.reason < 0)
15262 throw_exception (ex);
1042e4c0 15263
a7bdde9e 15264 fprintf_unfiltered (fp, " end\n");
1042e4c0 15265 }
6149aea9
PA
15266
15267 if (tp->enable_state == bp_disabled)
15268 fprintf_unfiltered (fp, "disable\n");
15269
15270 /* If this is a multi-location breakpoint, check if the locations
15271 should be individually disabled. Watchpoint locations are
15272 special, and not user visible. */
15273 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15274 {
15275 struct bp_location *loc;
15276 int n = 1;
15277
15278 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15279 if (!loc->enabled)
15280 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15281 }
1042e4c0 15282 }
8bf6485c 15283
6149aea9 15284 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15285 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15286
1042e4c0
SS
15287 do_cleanups (cleanup);
15288 if (from_tty)
6149aea9
PA
15289 printf_filtered (_("Saved to file '%s'.\n"), filename);
15290}
15291
15292/* The `save breakpoints' command. */
15293
15294static void
15295save_breakpoints_command (char *args, int from_tty)
15296{
15297 save_breakpoints (args, from_tty, NULL);
15298}
15299
15300/* The `save tracepoints' command. */
15301
15302static void
15303save_tracepoints_command (char *args, int from_tty)
15304{
15305 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15306}
15307
15308/* Create a vector of all tracepoints. */
15309
15310VEC(breakpoint_p) *
eeae04df 15311all_tracepoints (void)
1042e4c0
SS
15312{
15313 VEC(breakpoint_p) *tp_vec = 0;
15314 struct breakpoint *tp;
15315
15316 ALL_TRACEPOINTS (tp)
15317 {
15318 VEC_safe_push (breakpoint_p, tp_vec, tp);
15319 }
15320
15321 return tp_vec;
15322}
15323
c906108c 15324\f
4a64f543
MS
15325/* This help string is used for the break, hbreak, tbreak and thbreak
15326 commands. It is defined as a macro to prevent duplication.
15327 COMMAND should be a string constant containing the name of the
15328 command. */
31e2b00f
AS
15329#define BREAK_ARGS_HELP(command) \
15330command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15331LOCATION may be a line number, function name, or \"*\" and an address.\n\
15332If a line number is specified, break at start of code for that line.\n\
15333If a function is specified, break at start of code for that function.\n\
15334If an address is specified, break at that exact address.\n\
dc10affe
PA
15335With no LOCATION, uses current execution address of the selected\n\
15336stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15337\n\
15338THREADNUM is the number from \"info threads\".\n\
15339CONDITION is a boolean expression.\n\
15340\n\
d41c0fc8
PA
15341Multiple breakpoints at one place are permitted, and useful if their\n\
15342conditions are different.\n\
31e2b00f
AS
15343\n\
15344Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15345
44feb3ce
TT
15346/* List of subcommands for "catch". */
15347static struct cmd_list_element *catch_cmdlist;
15348
15349/* List of subcommands for "tcatch". */
15350static struct cmd_list_element *tcatch_cmdlist;
15351
9ac4176b 15352void
44feb3ce
TT
15353add_catch_command (char *name, char *docstring,
15354 void (*sfunc) (char *args, int from_tty,
15355 struct cmd_list_element *command),
625e8578 15356 completer_ftype *completer,
44feb3ce
TT
15357 void *user_data_catch,
15358 void *user_data_tcatch)
15359{
15360 struct cmd_list_element *command;
15361
15362 command = add_cmd (name, class_breakpoint, NULL, docstring,
15363 &catch_cmdlist);
15364 set_cmd_sfunc (command, sfunc);
15365 set_cmd_context (command, user_data_catch);
a96d9b2e 15366 set_cmd_completer (command, completer);
44feb3ce
TT
15367
15368 command = add_cmd (name, class_breakpoint, NULL, docstring,
15369 &tcatch_cmdlist);
15370 set_cmd_sfunc (command, sfunc);
15371 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15372 set_cmd_completer (command, completer);
44feb3ce
TT
15373}
15374
6c95b8df 15375static void
a79b8f6e 15376clear_syscall_counts (struct inferior *inf)
6c95b8df 15377{
fa3064dd
YQ
15378 struct catch_syscall_inferior_data *inf_data
15379 = get_catch_syscall_inferior_data (inf);
15380
15381 inf_data->total_syscalls_count = 0;
15382 inf_data->any_syscall_count = 0;
15383 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
15384}
15385
6149aea9
PA
15386static void
15387save_command (char *arg, int from_tty)
15388{
3e43a32a
MS
15389 printf_unfiltered (_("\"save\" must be followed by "
15390 "the name of a save subcommand.\n"));
6149aea9
PA
15391 help_list (save_cmdlist, "save ", -1, gdb_stdout);
15392}
15393
84f4c1fe
PM
15394struct breakpoint *
15395iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15396 void *data)
15397{
35df4500 15398 struct breakpoint *b, *b_tmp;
84f4c1fe 15399
35df4500 15400 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15401 {
15402 if ((*callback) (b, data))
15403 return b;
15404 }
15405
15406 return NULL;
15407}
15408
0574c78f
GB
15409/* Zero if any of the breakpoint's locations could be a location where
15410 functions have been inlined, nonzero otherwise. */
15411
15412static int
15413is_non_inline_function (struct breakpoint *b)
15414{
15415 /* The shared library event breakpoint is set on the address of a
15416 non-inline function. */
15417 if (b->type == bp_shlib_event)
15418 return 1;
15419
15420 return 0;
15421}
15422
15423/* Nonzero if the specified PC cannot be a location where functions
15424 have been inlined. */
15425
15426int
09ac7c10
TT
15427pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15428 const struct target_waitstatus *ws)
0574c78f
GB
15429{
15430 struct breakpoint *b;
15431 struct bp_location *bl;
15432
15433 ALL_BREAKPOINTS (b)
15434 {
15435 if (!is_non_inline_function (b))
15436 continue;
15437
15438 for (bl = b->loc; bl != NULL; bl = bl->next)
15439 {
15440 if (!bl->shlib_disabled
09ac7c10 15441 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15442 return 1;
15443 }
15444 }
15445
15446 return 0;
15447}
15448
2060206e
PA
15449void
15450initialize_breakpoint_ops (void)
15451{
15452 static int initialized = 0;
15453
15454 struct breakpoint_ops *ops;
15455
15456 if (initialized)
15457 return;
15458 initialized = 1;
15459
15460 /* The breakpoint_ops structure to be inherit by all kinds of
15461 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15462 internal and momentary breakpoints, etc.). */
15463 ops = &bkpt_base_breakpoint_ops;
15464 *ops = base_breakpoint_ops;
15465 ops->re_set = bkpt_re_set;
15466 ops->insert_location = bkpt_insert_location;
15467 ops->remove_location = bkpt_remove_location;
15468 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
15469 ops->create_sals_from_address = bkpt_create_sals_from_address;
15470 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15471 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
15472
15473 /* The breakpoint_ops structure to be used in regular breakpoints. */
15474 ops = &bkpt_breakpoint_ops;
15475 *ops = bkpt_base_breakpoint_ops;
15476 ops->re_set = bkpt_re_set;
15477 ops->resources_needed = bkpt_resources_needed;
15478 ops->print_it = bkpt_print_it;
15479 ops->print_mention = bkpt_print_mention;
15480 ops->print_recreate = bkpt_print_recreate;
15481
15482 /* Ranged breakpoints. */
15483 ops = &ranged_breakpoint_ops;
15484 *ops = bkpt_breakpoint_ops;
15485 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15486 ops->resources_needed = resources_needed_ranged_breakpoint;
15487 ops->print_it = print_it_ranged_breakpoint;
15488 ops->print_one = print_one_ranged_breakpoint;
15489 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15490 ops->print_mention = print_mention_ranged_breakpoint;
15491 ops->print_recreate = print_recreate_ranged_breakpoint;
15492
15493 /* Internal breakpoints. */
15494 ops = &internal_breakpoint_ops;
15495 *ops = bkpt_base_breakpoint_ops;
15496 ops->re_set = internal_bkpt_re_set;
15497 ops->check_status = internal_bkpt_check_status;
15498 ops->print_it = internal_bkpt_print_it;
15499 ops->print_mention = internal_bkpt_print_mention;
15500
15501 /* Momentary breakpoints. */
15502 ops = &momentary_breakpoint_ops;
15503 *ops = bkpt_base_breakpoint_ops;
15504 ops->re_set = momentary_bkpt_re_set;
15505 ops->check_status = momentary_bkpt_check_status;
15506 ops->print_it = momentary_bkpt_print_it;
15507 ops->print_mention = momentary_bkpt_print_mention;
15508
e2e4d78b
JK
15509 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15510 ops = &longjmp_breakpoint_ops;
15511 *ops = momentary_breakpoint_ops;
15512 ops->dtor = longjmp_bkpt_dtor;
15513
55aa24fb
SDJ
15514 /* Probe breakpoints. */
15515 ops = &bkpt_probe_breakpoint_ops;
15516 *ops = bkpt_breakpoint_ops;
15517 ops->insert_location = bkpt_probe_insert_location;
15518 ops->remove_location = bkpt_probe_remove_location;
15519 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15520 ops->decode_linespec = bkpt_probe_decode_linespec;
15521
2060206e
PA
15522 /* GNU v3 exception catchpoints. */
15523 ops = &gnu_v3_exception_catchpoint_ops;
15524 *ops = bkpt_breakpoint_ops;
15525 ops->print_it = print_it_exception_catchpoint;
15526 ops->print_one = print_one_exception_catchpoint;
15527 ops->print_mention = print_mention_exception_catchpoint;
15528 ops->print_recreate = print_recreate_exception_catchpoint;
15529
15530 /* Watchpoints. */
15531 ops = &watchpoint_breakpoint_ops;
15532 *ops = base_breakpoint_ops;
3a5c3e22 15533 ops->dtor = dtor_watchpoint;
2060206e
PA
15534 ops->re_set = re_set_watchpoint;
15535 ops->insert_location = insert_watchpoint;
15536 ops->remove_location = remove_watchpoint;
15537 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15538 ops->check_status = check_status_watchpoint;
15539 ops->resources_needed = resources_needed_watchpoint;
15540 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15541 ops->print_it = print_it_watchpoint;
15542 ops->print_mention = print_mention_watchpoint;
15543 ops->print_recreate = print_recreate_watchpoint;
15544
15545 /* Masked watchpoints. */
15546 ops = &masked_watchpoint_breakpoint_ops;
15547 *ops = watchpoint_breakpoint_ops;
15548 ops->insert_location = insert_masked_watchpoint;
15549 ops->remove_location = remove_masked_watchpoint;
15550 ops->resources_needed = resources_needed_masked_watchpoint;
15551 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15552 ops->print_it = print_it_masked_watchpoint;
15553 ops->print_one_detail = print_one_detail_masked_watchpoint;
15554 ops->print_mention = print_mention_masked_watchpoint;
15555 ops->print_recreate = print_recreate_masked_watchpoint;
15556
15557 /* Tracepoints. */
15558 ops = &tracepoint_breakpoint_ops;
15559 *ops = base_breakpoint_ops;
15560 ops->re_set = tracepoint_re_set;
15561 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15562 ops->print_one_detail = tracepoint_print_one_detail;
15563 ops->print_mention = tracepoint_print_mention;
15564 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
15565 ops->create_sals_from_address = tracepoint_create_sals_from_address;
15566 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15567 ops->decode_linespec = tracepoint_decode_linespec;
15568
55aa24fb
SDJ
15569 /* Probe tracepoints. */
15570 ops = &tracepoint_probe_breakpoint_ops;
15571 *ops = tracepoint_breakpoint_ops;
15572 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15573 ops->decode_linespec = tracepoint_probe_decode_linespec;
15574
983af33b
SDJ
15575 /* Static tracepoints with marker (`-m'). */
15576 ops = &strace_marker_breakpoint_ops;
15577 *ops = tracepoint_breakpoint_ops;
15578 ops->create_sals_from_address = strace_marker_create_sals_from_address;
15579 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15580 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
15581
15582 /* Fork catchpoints. */
15583 ops = &catch_fork_breakpoint_ops;
15584 *ops = base_breakpoint_ops;
15585 ops->insert_location = insert_catch_fork;
15586 ops->remove_location = remove_catch_fork;
15587 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15588 ops->print_it = print_it_catch_fork;
15589 ops->print_one = print_one_catch_fork;
15590 ops->print_mention = print_mention_catch_fork;
15591 ops->print_recreate = print_recreate_catch_fork;
15592
15593 /* Vfork catchpoints. */
15594 ops = &catch_vfork_breakpoint_ops;
15595 *ops = base_breakpoint_ops;
15596 ops->insert_location = insert_catch_vfork;
15597 ops->remove_location = remove_catch_vfork;
15598 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15599 ops->print_it = print_it_catch_vfork;
15600 ops->print_one = print_one_catch_vfork;
15601 ops->print_mention = print_mention_catch_vfork;
15602 ops->print_recreate = print_recreate_catch_vfork;
15603
15604 /* Exec catchpoints. */
15605 ops = &catch_exec_breakpoint_ops;
15606 *ops = base_breakpoint_ops;
15607 ops->dtor = dtor_catch_exec;
15608 ops->insert_location = insert_catch_exec;
15609 ops->remove_location = remove_catch_exec;
15610 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15611 ops->print_it = print_it_catch_exec;
15612 ops->print_one = print_one_catch_exec;
15613 ops->print_mention = print_mention_catch_exec;
15614 ops->print_recreate = print_recreate_catch_exec;
15615
15616 /* Syscall catchpoints. */
15617 ops = &catch_syscall_breakpoint_ops;
15618 *ops = base_breakpoint_ops;
15619 ops->dtor = dtor_catch_syscall;
15620 ops->insert_location = insert_catch_syscall;
15621 ops->remove_location = remove_catch_syscall;
15622 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
15623 ops->print_it = print_it_catch_syscall;
15624 ops->print_one = print_one_catch_syscall;
15625 ops->print_mention = print_mention_catch_syscall;
15626 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
15627
15628 /* Solib-related catchpoints. */
15629 ops = &catch_solib_breakpoint_ops;
15630 *ops = base_breakpoint_ops;
15631 ops->dtor = dtor_catch_solib;
15632 ops->insert_location = insert_catch_solib;
15633 ops->remove_location = remove_catch_solib;
15634 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15635 ops->check_status = check_status_catch_solib;
15636 ops->print_it = print_it_catch_solib;
15637 ops->print_one = print_one_catch_solib;
15638 ops->print_mention = print_mention_catch_solib;
15639 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15640
15641 ops = &dprintf_breakpoint_ops;
15642 *ops = bkpt_base_breakpoint_ops;
15643 ops->re_set = bkpt_re_set;
15644 ops->resources_needed = bkpt_resources_needed;
15645 ops->print_it = bkpt_print_it;
15646 ops->print_mention = bkpt_print_mention;
15647 ops->print_recreate = bkpt_print_recreate;
2060206e
PA
15648}
15649
c906108c 15650void
fba45db2 15651_initialize_breakpoint (void)
c906108c
SS
15652{
15653 struct cmd_list_element *c;
15654
2060206e
PA
15655 initialize_breakpoint_ops ();
15656
84acb35a 15657 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 15658 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 15659 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15660
55aa24fb
SDJ
15661 breakpoint_objfile_key
15662 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15663
fa3064dd
YQ
15664 catch_syscall_inferior_data
15665 = register_inferior_data_with_cleanup (catch_syscall_inferior_data_cleanup);
15666
c906108c
SS
15667 breakpoint_chain = 0;
15668 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15669 before a breakpoint is set. */
15670 breakpoint_count = 0;
15671
1042e4c0
SS
15672 tracepoint_count = 0;
15673
1bedd215
AC
15674 add_com ("ignore", class_breakpoint, ignore_command, _("\
15675Set ignore-count of breakpoint number N to COUNT.\n\
15676Usage is `ignore N COUNT'."));
c906108c 15677 if (xdb_commands)
c5aa993b 15678 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 15679
1bedd215
AC
15680 add_com ("commands", class_breakpoint, commands_command, _("\
15681Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
15682Give breakpoint number as argument after \"commands\".\n\
15683With no argument, the targeted breakpoint is the last one set.\n\
15684The commands themselves follow starting on the next line.\n\
15685Type a line containing \"end\" to indicate the end of them.\n\
15686Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15687then no output is printed when it is hit, except what the commands print."));
c906108c 15688
d55637df 15689 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15690Specify breakpoint number N to break only if COND is true.\n\
c906108c 15691Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15692expression to be evaluated whenever breakpoint N is reached."));
d55637df 15693 set_cmd_completer (c, condition_completer);
c906108c 15694
1bedd215 15695 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15696Set a temporary breakpoint.\n\
c906108c
SS
15697Like \"break\" except the breakpoint is only temporary,\n\
15698so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15699by using \"enable delete\" on the breakpoint number.\n\
15700\n"
15701BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15702 set_cmd_completer (c, location_completer);
c94fdfd0 15703
1bedd215 15704 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15705Set a hardware assisted breakpoint.\n\
c906108c 15706Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15707some target hardware may not have this support.\n\
15708\n"
15709BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15710 set_cmd_completer (c, location_completer);
c906108c 15711
1bedd215 15712 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15713Set a temporary hardware assisted breakpoint.\n\
c906108c 15714Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15715so it will be deleted when hit.\n\
15716\n"
15717BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15718 set_cmd_completer (c, location_completer);
c906108c 15719
1bedd215
AC
15720 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15721Enable some breakpoints.\n\
c906108c
SS
15722Give breakpoint numbers (separated by spaces) as arguments.\n\
15723With no subcommand, breakpoints are enabled until you command otherwise.\n\
15724This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15725With a subcommand you can enable temporarily."),
c906108c
SS
15726 &enablelist, "enable ", 1, &cmdlist);
15727 if (xdb_commands)
1bedd215
AC
15728 add_com ("ab", class_breakpoint, enable_command, _("\
15729Enable some breakpoints.\n\
c906108c
SS
15730Give breakpoint numbers (separated by spaces) as arguments.\n\
15731With no subcommand, breakpoints are enabled until you command otherwise.\n\
15732This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15733With a subcommand you can enable temporarily."));
c906108c
SS
15734
15735 add_com_alias ("en", "enable", class_breakpoint, 1);
15736
84951ab5 15737 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15738Enable some breakpoints.\n\
c906108c
SS
15739Give breakpoint numbers (separated by spaces) as arguments.\n\
15740This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15741May be abbreviated to simply \"enable\".\n"),
c5aa993b 15742 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15743
1a966eab
AC
15744 add_cmd ("once", no_class, enable_once_command, _("\
15745Enable breakpoints for one hit. Give breakpoint numbers.\n\
15746If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15747 &enablebreaklist);
15748
1a966eab
AC
15749 add_cmd ("delete", no_class, enable_delete_command, _("\
15750Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15751If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15752 &enablebreaklist);
15753
816338b5
SS
15754 add_cmd ("count", no_class, enable_count_command, _("\
15755Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15756If a breakpoint is hit while enabled in this fashion,\n\
15757the count is decremented; when it reaches zero, the breakpoint is disabled."),
15758 &enablebreaklist);
15759
1a966eab
AC
15760 add_cmd ("delete", no_class, enable_delete_command, _("\
15761Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15762If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15763 &enablelist);
15764
1a966eab
AC
15765 add_cmd ("once", no_class, enable_once_command, _("\
15766Enable breakpoints for one hit. Give breakpoint numbers.\n\
15767If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15768 &enablelist);
15769
15770 add_cmd ("count", no_class, enable_count_command, _("\
15771Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15772If a breakpoint is hit while enabled in this fashion,\n\
15773the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15774 &enablelist);
15775
1bedd215
AC
15776 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15777Disable some breakpoints.\n\
c906108c
SS
15778Arguments are breakpoint numbers with spaces in between.\n\
15779To disable all breakpoints, give no argument.\n\
64b9b334 15780A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15781 &disablelist, "disable ", 1, &cmdlist);
15782 add_com_alias ("dis", "disable", class_breakpoint, 1);
15783 add_com_alias ("disa", "disable", class_breakpoint, 1);
15784 if (xdb_commands)
1bedd215
AC
15785 add_com ("sb", class_breakpoint, disable_command, _("\
15786Disable some breakpoints.\n\
c906108c
SS
15787Arguments are breakpoint numbers with spaces in between.\n\
15788To disable all breakpoints, give no argument.\n\
64b9b334 15789A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 15790
1a966eab
AC
15791 add_cmd ("breakpoints", class_alias, disable_command, _("\
15792Disable some breakpoints.\n\
c906108c
SS
15793Arguments are breakpoint numbers with spaces in between.\n\
15794To disable all breakpoints, give no argument.\n\
64b9b334 15795A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15796This command may be abbreviated \"disable\"."),
c906108c
SS
15797 &disablelist);
15798
1bedd215
AC
15799 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15800Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15801Arguments are breakpoint numbers with spaces in between.\n\
15802To delete all breakpoints, give no argument.\n\
15803\n\
15804Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15805The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15806 &deletelist, "delete ", 1, &cmdlist);
15807 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15808 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15809 if (xdb_commands)
1bedd215
AC
15810 add_com ("db", class_breakpoint, delete_command, _("\
15811Delete some breakpoints.\n\
c906108c 15812Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 15813To delete all breakpoints, give no argument.\n"));
c906108c 15814
1a966eab
AC
15815 add_cmd ("breakpoints", class_alias, delete_command, _("\
15816Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15817Arguments are breakpoint numbers with spaces in between.\n\
15818To delete all breakpoints, give no argument.\n\
1a966eab 15819This command may be abbreviated \"delete\"."),
c906108c
SS
15820 &deletelist);
15821
1bedd215
AC
15822 add_com ("clear", class_breakpoint, clear_command, _("\
15823Clear breakpoint at specified line or function.\n\
c906108c
SS
15824Argument may be line number, function name, or \"*\" and an address.\n\
15825If line number is specified, all breakpoints in that line are cleared.\n\
15826If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
15827If an address is specified, breakpoints at that address are cleared.\n\
15828\n\
15829With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
15830is executing in.\n\
15831\n\
1bedd215 15832See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15833 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15834
1bedd215 15835 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
15836Set breakpoint at specified line or function.\n"
15837BREAK_ARGS_HELP ("break")));
5ba2abeb 15838 set_cmd_completer (c, location_completer);
c94fdfd0 15839
c906108c
SS
15840 add_com_alias ("b", "break", class_run, 1);
15841 add_com_alias ("br", "break", class_run, 1);
15842 add_com_alias ("bre", "break", class_run, 1);
15843 add_com_alias ("brea", "break", class_run, 1);
15844
7681d515
PM
15845 if (xdb_commands)
15846 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
15847
15848 if (dbx_commands)
15849 {
1bedd215
AC
15850 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15851Break in function/address or break at a line in the current file."),
c5aa993b
JM
15852 &stoplist, "stop ", 1, &cmdlist);
15853 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15854 _("Break in function or address."), &stoplist);
c5aa993b 15855 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15856 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
15857 add_com ("status", class_info, breakpoints_info, _("\
15858Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15859The \"Type\" column indicates one of:\n\
15860\tbreakpoint - normal breakpoint\n\
15861\twatchpoint - watchpoint\n\
15862The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15863the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15864breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15865address and file/line number respectively.\n\
15866\n\
15867Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15868are set to the address of the last breakpoint listed unless the command\n\
15869is prefixed with \"server \".\n\n\
c906108c 15870Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15871breakpoint set."));
c906108c
SS
15872 }
15873
1bedd215 15874 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 15875Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
c906108c
SS
15876The \"Type\" column indicates one of:\n\
15877\tbreakpoint - normal breakpoint\n\
15878\twatchpoint - watchpoint\n\
15879The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15880the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15881breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15882address and file/line number respectively.\n\
15883\n\
15884Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15885are set to the address of the last breakpoint listed unless the command\n\
15886is prefixed with \"server \".\n\n\
c906108c 15887Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15888breakpoint set."));
c906108c 15889
6b04bdb7
MS
15890 add_info_alias ("b", "breakpoints", 1);
15891
c906108c 15892 if (xdb_commands)
1bedd215
AC
15893 add_com ("lb", class_breakpoint, breakpoints_info, _("\
15894Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15895The \"Type\" column indicates one of:\n\
15896\tbreakpoint - normal breakpoint\n\
15897\twatchpoint - watchpoint\n\
15898The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15899the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15900breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15901address and file/line number respectively.\n\
15902\n\
15903Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15904are set to the address of the last breakpoint listed unless the command\n\
15905is prefixed with \"server \".\n\n\
c906108c 15906Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15907breakpoint set."));
c906108c 15908
1a966eab
AC
15909 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15910Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15911The \"Type\" column indicates one of:\n\
15912\tbreakpoint - normal breakpoint\n\
15913\twatchpoint - watchpoint\n\
15914\tlongjmp - internal breakpoint used to step through longjmp()\n\
15915\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15916\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15917\tfinish - internal breakpoint used by the \"finish\" command\n\
15918The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15919the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15920breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15921address and file/line number respectively.\n\
15922\n\
15923Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15924are set to the address of the last breakpoint listed unless the command\n\
15925is prefixed with \"server \".\n\n\
c906108c 15926Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15927breakpoint set."),
c906108c
SS
15928 &maintenanceinfolist);
15929
44feb3ce
TT
15930 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15931Set catchpoints to catch events."),
15932 &catch_cmdlist, "catch ",
15933 0/*allow-unknown*/, &cmdlist);
15934
15935 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15936Set temporary catchpoints to catch events."),
15937 &tcatch_cmdlist, "tcatch ",
15938 0/*allow-unknown*/, &cmdlist);
15939
15940 /* Add catch and tcatch sub-commands. */
15941 add_catch_command ("catch", _("\
88e7d25d 15942Catch an exception, when caught."),
44feb3ce 15943 catch_catch_command,
a96d9b2e 15944 NULL,
44feb3ce
TT
15945 CATCH_PERMANENT,
15946 CATCH_TEMPORARY);
15947 add_catch_command ("throw", _("\
88e7d25d 15948Catch an exception, when thrown."),
44feb3ce 15949 catch_throw_command,
a96d9b2e 15950 NULL,
44feb3ce
TT
15951 CATCH_PERMANENT,
15952 CATCH_TEMPORARY);
15953 add_catch_command ("fork", _("Catch calls to fork."),
15954 catch_fork_command_1,
a96d9b2e 15955 NULL,
44feb3ce
TT
15956 (void *) (uintptr_t) catch_fork_permanent,
15957 (void *) (uintptr_t) catch_fork_temporary);
15958 add_catch_command ("vfork", _("Catch calls to vfork."),
15959 catch_fork_command_1,
a96d9b2e 15960 NULL,
44feb3ce
TT
15961 (void *) (uintptr_t) catch_vfork_permanent,
15962 (void *) (uintptr_t) catch_vfork_temporary);
15963 add_catch_command ("exec", _("Catch calls to exec."),
15964 catch_exec_command_1,
a96d9b2e
SDJ
15965 NULL,
15966 CATCH_PERMANENT,
15967 CATCH_TEMPORARY);
edcc5120
TT
15968 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15969Usage: catch load [REGEX]\n\
15970If REGEX is given, only stop for libraries matching the regular expression."),
15971 catch_load_command_1,
15972 NULL,
15973 CATCH_PERMANENT,
15974 CATCH_TEMPORARY);
15975 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15976Usage: catch unload [REGEX]\n\
15977If REGEX is given, only stop for libraries matching the regular expression."),
15978 catch_unload_command_1,
15979 NULL,
15980 CATCH_PERMANENT,
15981 CATCH_TEMPORARY);
a96d9b2e
SDJ
15982 add_catch_command ("syscall", _("\
15983Catch system calls by their names and/or numbers.\n\
15984Arguments say which system calls to catch. If no arguments\n\
15985are given, every system call will be caught.\n\
15986Arguments, if given, should be one or more system call names\n\
15987(if your system supports that), or system call numbers."),
15988 catch_syscall_command_1,
15989 catch_syscall_completer,
44feb3ce
TT
15990 CATCH_PERMANENT,
15991 CATCH_TEMPORARY);
c5aa993b 15992
1bedd215
AC
15993 c = add_com ("watch", class_breakpoint, watch_command, _("\
15994Set a watchpoint for an expression.\n\
06a64a0b 15995Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15996A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15997an expression changes.\n\
15998If -l or -location is given, this evaluates EXPRESSION and watches\n\
15999the memory to which it refers."));
65d12d83 16000 set_cmd_completer (c, expression_completer);
c906108c 16001
1bedd215
AC
16002 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16003Set a read watchpoint for an expression.\n\
06a64a0b 16004Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 16005A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16006an expression is read.\n\
16007If -l or -location is given, this evaluates EXPRESSION and watches\n\
16008the memory to which it refers."));
65d12d83 16009 set_cmd_completer (c, expression_completer);
c906108c 16010
1bedd215
AC
16011 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16012Set a watchpoint for an expression.\n\
06a64a0b 16013Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 16014A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
16015an expression is either read or written.\n\
16016If -l or -location is given, this evaluates EXPRESSION and watches\n\
16017the memory to which it refers."));
65d12d83 16018 set_cmd_completer (c, expression_completer);
c906108c 16019
d77f58be 16020 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16021Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16022
920d2a44
AC
16023 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16024 respond to changes - contrary to the description. */
85c07804
AC
16025 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16026 &can_use_hw_watchpoints, _("\
16027Set debugger's willingness to use watchpoint hardware."), _("\
16028Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16029If zero, gdb will not use hardware for new watchpoints, even if\n\
16030such is available. (However, any hardware watchpoints that were\n\
16031created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16032hardware.)"),
16033 NULL,
920d2a44 16034 show_can_use_hw_watchpoints,
85c07804 16035 &setlist, &showlist);
c906108c
SS
16036
16037 can_use_hw_watchpoints = 1;
fa8d40ab 16038
1042e4c0
SS
16039 /* Tracepoint manipulation commands. */
16040
16041 c = add_com ("trace", class_breakpoint, trace_command, _("\
16042Set a tracepoint at specified line or function.\n\
16043\n"
16044BREAK_ARGS_HELP ("trace") "\n\
16045Do \"help tracepoints\" for info on other tracepoint commands."));
16046 set_cmd_completer (c, location_completer);
16047
16048 add_com_alias ("tp", "trace", class_alias, 0);
16049 add_com_alias ("tr", "trace", class_alias, 1);
16050 add_com_alias ("tra", "trace", class_alias, 1);
16051 add_com_alias ("trac", "trace", class_alias, 1);
16052
7a697b8d
SS
16053 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16054Set a fast tracepoint at specified line or function.\n\
16055\n"
16056BREAK_ARGS_HELP ("ftrace") "\n\
16057Do \"help tracepoints\" for info on other tracepoint commands."));
16058 set_cmd_completer (c, location_completer);
16059
0fb4aa4b
PA
16060 c = add_com ("strace", class_breakpoint, strace_command, _("\
16061Set a static tracepoint at specified line, function or marker.\n\
16062\n\
16063strace [LOCATION] [if CONDITION]\n\
16064LOCATION may be a line number, function name, \"*\" and an address,\n\
16065or -m MARKER_ID.\n\
16066If a line number is specified, probe the marker at start of code\n\
16067for that line. If a function is specified, probe the marker at start\n\
16068of code for that function. If an address is specified, probe the marker\n\
16069at that exact address. If a marker id is specified, probe the marker\n\
16070with that name. With no LOCATION, uses current execution address of\n\
16071the selected stack frame.\n\
16072Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16073This collects arbitrary user data passed in the probe point call to the\n\
16074tracing library. You can inspect it when analyzing the trace buffer,\n\
16075by printing the $_sdata variable like any other convenience variable.\n\
16076\n\
16077CONDITION is a boolean expression.\n\
16078\n\
d41c0fc8
PA
16079Multiple tracepoints at one place are permitted, and useful if their\n\
16080conditions are different.\n\
0fb4aa4b
PA
16081\n\
16082Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16083Do \"help tracepoints\" for info on other tracepoint commands."));
16084 set_cmd_completer (c, location_completer);
16085
1042e4c0 16086 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16087Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16088Convenience variable \"$tpnum\" contains the number of the\n\
16089last tracepoint set."));
16090
16091 add_info_alias ("tp", "tracepoints", 1);
16092
16093 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16094Delete specified tracepoints.\n\
16095Arguments are tracepoint numbers, separated by spaces.\n\
16096No argument means delete all tracepoints."),
16097 &deletelist);
16098
16099 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16100Disable specified tracepoints.\n\
16101Arguments are tracepoint numbers, separated by spaces.\n\
16102No argument means disable all tracepoints."),
16103 &disablelist);
16104 deprecate_cmd (c, "disable");
16105
16106 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16107Enable specified tracepoints.\n\
16108Arguments are tracepoint numbers, separated by spaces.\n\
16109No argument means enable all tracepoints."),
16110 &enablelist);
16111 deprecate_cmd (c, "enable");
16112
16113 add_com ("passcount", class_trace, trace_pass_command, _("\
16114Set the passcount for a tracepoint.\n\
16115The trace will end when the tracepoint has been passed 'count' times.\n\
16116Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16117if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16118
6149aea9
PA
16119 add_prefix_cmd ("save", class_breakpoint, save_command,
16120 _("Save breakpoint definitions as a script."),
16121 &save_cmdlist, "save ",
16122 0/*allow-unknown*/, &cmdlist);
16123
16124 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16125Save current breakpoint definitions as a script.\n\
cce7e648 16126This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16127catchpoints, tracepoints). Use the 'source' command in another debug\n\
16128session to restore them."),
16129 &save_cmdlist);
16130 set_cmd_completer (c, filename_completer);
16131
16132 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16133Save current tracepoint definitions as a script.\n\
6149aea9
PA
16134Use the 'source' command in another debug session to restore them."),
16135 &save_cmdlist);
1042e4c0
SS
16136 set_cmd_completer (c, filename_completer);
16137
6149aea9
PA
16138 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16139 deprecate_cmd (c, "save tracepoints");
16140
1bedd215 16141 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16142Breakpoint specific settings\n\
16143Configure various breakpoint-specific variables such as\n\
1bedd215 16144pending breakpoint behavior"),
fa8d40ab
JJ
16145 &breakpoint_set_cmdlist, "set breakpoint ",
16146 0/*allow-unknown*/, &setlist);
1bedd215 16147 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16148Breakpoint specific settings\n\
16149Configure various breakpoint-specific variables such as\n\
1bedd215 16150pending breakpoint behavior"),
fa8d40ab
JJ
16151 &breakpoint_show_cmdlist, "show breakpoint ",
16152 0/*allow-unknown*/, &showlist);
16153
7915a72c
AC
16154 add_setshow_auto_boolean_cmd ("pending", no_class,
16155 &pending_break_support, _("\
16156Set debugger's behavior regarding pending breakpoints."), _("\
16157Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16158If on, an unrecognized breakpoint location will cause gdb to create a\n\
16159pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16160an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16161user-query to see if a pending breakpoint should be created."),
2c5b56ce 16162 NULL,
920d2a44 16163 show_pending_break_support,
6e1d7d6c
AC
16164 &breakpoint_set_cmdlist,
16165 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16166
16167 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16168
16169 add_setshow_boolean_cmd ("auto-hw", no_class,
16170 &automatic_hardware_breakpoints, _("\
16171Set automatic usage of hardware breakpoints."), _("\
16172Show automatic usage of hardware breakpoints."), _("\
16173If set, the debugger will automatically use hardware breakpoints for\n\
16174breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16175a warning will be emitted for such breakpoints."),
16176 NULL,
16177 show_automatic_hardware_breakpoints,
16178 &breakpoint_set_cmdlist,
16179 &breakpoint_show_cmdlist);
74960c60 16180
33e5cbd6
PA
16181 add_setshow_enum_cmd ("always-inserted", class_support,
16182 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
16183Set mode for inserting breakpoints."), _("\
16184Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
16185When this mode is off, breakpoints are inserted in inferior when it is\n\
16186resumed, and removed when execution stops. When this mode is on,\n\
16187breakpoints are inserted immediately and removed only when the user\n\
16188deletes the breakpoint. When this mode is auto (which is the default),\n\
16189the behaviour depends on the non-stop setting (see help set non-stop).\n\
16190In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16191behaves as if always-inserted mode is on; if gdb is controlling the\n\
16192inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
16193 NULL,
16194 &show_always_inserted_mode,
16195 &breakpoint_set_cmdlist,
16196 &breakpoint_show_cmdlist);
f1310107 16197
b775012e
LM
16198 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16199 condition_evaluation_enums,
16200 &condition_evaluation_mode_1, _("\
16201Set mode of breakpoint condition evaluation."), _("\
16202Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16203When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16204evaluated on the host's side by GDB. When it is set to \"target\",\n\
16205breakpoint conditions will be downloaded to the target (if the target\n\
16206supports such feature) and conditions will be evaluated on the target's side.\n\
16207If this is set to \"auto\" (default), this will be automatically set to\n\
16208\"target\" if it supports condition evaluation, otherwise it will\n\
16209be set to \"gdb\""),
16210 &set_condition_evaluation_mode,
16211 &show_condition_evaluation_mode,
16212 &breakpoint_set_cmdlist,
16213 &breakpoint_show_cmdlist);
16214
f1310107
TJB
16215 add_com ("break-range", class_breakpoint, break_range_command, _("\
16216Set a breakpoint for an address range.\n\
16217break-range START-LOCATION, END-LOCATION\n\
16218where START-LOCATION and END-LOCATION can be one of the following:\n\
16219 LINENUM, for that line in the current file,\n\
16220 FILE:LINENUM, for that line in that file,\n\
16221 +OFFSET, for that number of lines after the current line\n\
16222 or the start of the range\n\
16223 FUNCTION, for the first line in that function,\n\
16224 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16225 *ADDRESS, for the instruction at that address.\n\
16226\n\
16227The breakpoint will stop execution of the inferior whenever it executes\n\
16228an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16229range (including START-LOCATION and END-LOCATION)."));
16230
e7e0cddf
SS
16231 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16232Set a dynamic printf at specified line or function.\n\
16233dprintf location,format string,arg1,arg2,...\n\
16234location may be a line number, function name, or \"*\" and an address.\n\
16235If a line number is specified, break at start of code for that line.\n\
16236If a function is specified, break at start of code for that function.\n\
16237"));
16238 set_cmd_completer (c, location_completer);
16239
16240 add_setshow_enum_cmd ("dprintf-style", class_support,
16241 dprintf_style_enums, &dprintf_style, _("\
16242Set the style of usage for dynamic printf."), _("\
16243Show the style of usage for dynamic printf."), _("\
16244This setting chooses how GDB will do a dynamic printf.\n\
16245If the value is \"gdb\", then the printing is done by GDB to its own\n\
16246console, as with the \"printf\" command.\n\
16247If the value is \"call\", the print is done by calling a function in your\n\
16248program; by default printf(), but you can choose a different function or\n\
16249output stream by setting dprintf-function and dprintf-channel."),
16250 update_dprintf_commands, NULL,
16251 &setlist, &showlist);
16252
16253 dprintf_function = xstrdup ("printf");
16254 add_setshow_string_cmd ("dprintf-function", class_support,
16255 &dprintf_function, _("\
16256Set the function to use for dynamic printf"), _("\
16257Show the function to use for dynamic printf"), NULL,
16258 update_dprintf_commands, NULL,
16259 &setlist, &showlist);
16260
16261 dprintf_channel = xstrdup ("");
16262 add_setshow_string_cmd ("dprintf-channel", class_support,
16263 &dprintf_channel, _("\
16264Set the channel to use for dynamic printf"), _("\
16265Show the channel to use for dynamic printf"), NULL,
16266 update_dprintf_commands, NULL,
16267 &setlist, &showlist);
16268
765dc015 16269 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16270
16271 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 16272}
This page took 2.1933 seconds and 4 git commands to generate.