* linespec.c: #include "stack.h".
[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;
1bb9788d 929 w->cond_exp = parse_exp_1 (&arg, 0, 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 =
1bb9788d
TT
942 parse_exp_1 (&arg, loc->address,
943 block_for_pc (loc->address), 0);
adc36818
PM
944 if (*arg)
945 error (_("Junk at end of expression"));
946 }
947 }
948 }
b775012e
LM
949 mark_breakpoint_modified (b);
950
adc36818 951 breakpoints_changed ();
8d3788bd 952 observer_notify_breakpoint_modified (b);
adc36818
PM
953}
954
d55637df
TT
955/* Completion for the "condition" command. */
956
957static VEC (char_ptr) *
958condition_completer (struct cmd_list_element *cmd, char *text, char *word)
959{
960 char *space;
961
962 text = skip_spaces (text);
963 space = skip_to_space (text);
964 if (*space == '\0')
965 {
966 int len;
967 struct breakpoint *b;
968 VEC (char_ptr) *result = NULL;
969
970 if (text[0] == '$')
971 {
972 /* We don't support completion of history indices. */
973 if (isdigit (text[1]))
974 return NULL;
975 return complete_internalvar (&text[1]);
976 }
977
978 /* We're completing the breakpoint number. */
979 len = strlen (text);
980
981 ALL_BREAKPOINTS (b)
982 {
983 int single = b->loc->next == NULL;
984 struct bp_location *loc;
985 int count = 1;
986
987 for (loc = b->loc; loc; loc = loc->next)
988 {
989 char location[50];
990
991 if (single)
992 sprintf (location, "%d", b->number);
993 else
994 sprintf (location, "%d.%d", b->number, count);
995
996 if (strncmp (location, text, len) == 0)
997 VEC_safe_push (char_ptr, result, xstrdup (location));
998
999 ++count;
1000 }
1001 }
1002
1003 return result;
1004 }
1005
1006 /* We're completing the expression part. */
1007 text = skip_spaces (space);
1008 return expression_completer (cmd, text, word);
1009}
1010
c906108c
SS
1011/* condition N EXP -- set break condition of breakpoint N to EXP. */
1012
1013static void
fba45db2 1014condition_command (char *arg, int from_tty)
c906108c 1015{
52f0bd74 1016 struct breakpoint *b;
c906108c 1017 char *p;
52f0bd74 1018 int bnum;
c906108c
SS
1019
1020 if (arg == 0)
e2e0b3e5 1021 error_no_arg (_("breakpoint number"));
c906108c
SS
1022
1023 p = arg;
1024 bnum = get_number (&p);
5c44784c 1025 if (bnum == 0)
8a3fe4f8 1026 error (_("Bad breakpoint argument: '%s'"), arg);
c906108c
SS
1027
1028 ALL_BREAKPOINTS (b)
1029 if (b->number == bnum)
2f069f6f 1030 {
7371cf6d
PM
1031 /* Check if this breakpoint has a Python object assigned to
1032 it, and if it has a definition of the "stop"
1033 method. This method and conditions entered into GDB from
1034 the CLI are mutually exclusive. */
1035 if (b->py_bp_object
1036 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
1037 error (_("Cannot set a condition where a Python 'stop' "
1038 "method has been defined in the breakpoint."));
2566ad2d 1039 set_breakpoint_condition (b, p, from_tty);
b775012e
LM
1040
1041 if (is_breakpoint (b))
1042 update_global_location_list (1);
1043
2f069f6f
JB
1044 return;
1045 }
c906108c 1046
8a3fe4f8 1047 error (_("No breakpoint number %d."), bnum);
c906108c
SS
1048}
1049
a7bdde9e
VP
1050/* Check that COMMAND do not contain commands that are suitable
1051 only for tracepoints and not suitable for ordinary breakpoints.
4a64f543
MS
1052 Throw if any such commands is found. */
1053
a7bdde9e
VP
1054static void
1055check_no_tracepoint_commands (struct command_line *commands)
1056{
1057 struct command_line *c;
cc59ec59 1058
a7bdde9e
VP
1059 for (c = commands; c; c = c->next)
1060 {
1061 int i;
1062
1063 if (c->control_type == while_stepping_control)
3e43a32a
MS
1064 error (_("The 'while-stepping' command can "
1065 "only be used for tracepoints"));
a7bdde9e
VP
1066
1067 for (i = 0; i < c->body_count; ++i)
1068 check_no_tracepoint_commands ((c->body_list)[i]);
1069
1070 /* Not that command parsing removes leading whitespace and comment
4a64f543 1071 lines and also empty lines. So, we only need to check for
a7bdde9e
VP
1072 command directly. */
1073 if (strstr (c->line, "collect ") == c->line)
1074 error (_("The 'collect' command can only be used for tracepoints"));
1075
51661e93
VP
1076 if (strstr (c->line, "teval ") == c->line)
1077 error (_("The 'teval' command can only be used for tracepoints"));
a7bdde9e
VP
1078 }
1079}
1080
d77f58be
SS
1081/* Encapsulate tests for different types of tracepoints. */
1082
d9b3f62e
PA
1083static int
1084is_tracepoint_type (enum bptype type)
1085{
1086 return (type == bp_tracepoint
1087 || type == bp_fast_tracepoint
1088 || type == bp_static_tracepoint);
1089}
1090
a7bdde9e 1091int
d77f58be 1092is_tracepoint (const struct breakpoint *b)
a7bdde9e 1093{
d9b3f62e 1094 return is_tracepoint_type (b->type);
a7bdde9e 1095}
d9b3f62e 1096
e5dd4106 1097/* A helper function that validates that COMMANDS are valid for a
95a42b64
TT
1098 breakpoint. This function will throw an exception if a problem is
1099 found. */
48cb2d85 1100
95a42b64
TT
1101static void
1102validate_commands_for_breakpoint (struct breakpoint *b,
1103 struct command_line *commands)
48cb2d85 1104{
d77f58be 1105 if (is_tracepoint (b))
a7bdde9e 1106 {
4a64f543
MS
1107 /* We need to verify that each top-level element of commands is
1108 valid for tracepoints, that there's at most one
1109 while-stepping element, and that while-stepping's body has
1110 valid tracing commands excluding nested while-stepping. */
a7bdde9e
VP
1111 struct command_line *c;
1112 struct command_line *while_stepping = 0;
1113 for (c = commands; c; c = c->next)
1114 {
a7bdde9e
VP
1115 if (c->control_type == while_stepping_control)
1116 {
1117 if (b->type == bp_fast_tracepoint)
3e43a32a
MS
1118 error (_("The 'while-stepping' command "
1119 "cannot be used for fast tracepoint"));
0fb4aa4b 1120 else if (b->type == bp_static_tracepoint)
3e43a32a
MS
1121 error (_("The 'while-stepping' command "
1122 "cannot be used for static tracepoint"));
a7bdde9e
VP
1123
1124 if (while_stepping)
3e43a32a
MS
1125 error (_("The 'while-stepping' command "
1126 "can be used only once"));
a7bdde9e
VP
1127 else
1128 while_stepping = c;
1129 }
1130 }
1131 if (while_stepping)
1132 {
1133 struct command_line *c2;
1134
1135 gdb_assert (while_stepping->body_count == 1);
1136 c2 = while_stepping->body_list[0];
1137 for (; c2; c2 = c2->next)
1138 {
a7bdde9e
VP
1139 if (c2->control_type == while_stepping_control)
1140 error (_("The 'while-stepping' command cannot be nested"));
1141 }
1142 }
1143 }
1144 else
1145 {
1146 check_no_tracepoint_commands (commands);
1147 }
95a42b64
TT
1148}
1149
0fb4aa4b
PA
1150/* Return a vector of all the static tracepoints set at ADDR. The
1151 caller is responsible for releasing the vector. */
1152
1153VEC(breakpoint_p) *
1154static_tracepoints_here (CORE_ADDR addr)
1155{
1156 struct breakpoint *b;
1157 VEC(breakpoint_p) *found = 0;
1158 struct bp_location *loc;
1159
1160 ALL_BREAKPOINTS (b)
1161 if (b->type == bp_static_tracepoint)
1162 {
1163 for (loc = b->loc; loc; loc = loc->next)
1164 if (loc->address == addr)
1165 VEC_safe_push(breakpoint_p, found, b);
1166 }
1167
1168 return found;
1169}
1170
95a42b64 1171/* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
4a64f543 1172 validate that only allowed commands are included. */
95a42b64
TT
1173
1174void
4a64f543
MS
1175breakpoint_set_commands (struct breakpoint *b,
1176 struct command_line *commands)
95a42b64
TT
1177{
1178 validate_commands_for_breakpoint (b, commands);
a7bdde9e 1179
9add0f1b
TT
1180 decref_counted_command_line (&b->commands);
1181 b->commands = alloc_counted_command_line (commands);
48cb2d85 1182 breakpoints_changed ();
8d3788bd 1183 observer_notify_breakpoint_modified (b);
48cb2d85
VP
1184}
1185
45a43567
TT
1186/* Set the internal `silent' flag on the breakpoint. Note that this
1187 is not the same as the "silent" that may appear in the breakpoint's
1188 commands. */
1189
1190void
1191breakpoint_set_silent (struct breakpoint *b, int silent)
1192{
1193 int old_silent = b->silent;
1194
1195 b->silent = silent;
1196 if (old_silent != silent)
8d3788bd 1197 observer_notify_breakpoint_modified (b);
45a43567
TT
1198}
1199
1200/* Set the thread for this breakpoint. If THREAD is -1, make the
1201 breakpoint work for any thread. */
1202
1203void
1204breakpoint_set_thread (struct breakpoint *b, int thread)
1205{
1206 int old_thread = b->thread;
1207
1208 b->thread = thread;
1209 if (old_thread != thread)
8d3788bd 1210 observer_notify_breakpoint_modified (b);
45a43567
TT
1211}
1212
1213/* Set the task for this breakpoint. If TASK is 0, make the
1214 breakpoint work for any task. */
1215
1216void
1217breakpoint_set_task (struct breakpoint *b, int task)
1218{
1219 int old_task = b->task;
1220
1221 b->task = task;
1222 if (old_task != task)
8d3788bd 1223 observer_notify_breakpoint_modified (b);
45a43567
TT
1224}
1225
95a42b64
TT
1226void
1227check_tracepoint_command (char *line, void *closure)
a7bdde9e
VP
1228{
1229 struct breakpoint *b = closure;
cc59ec59 1230
a7bdde9e
VP
1231 validate_actionline (&line, b);
1232}
1233
95a42b64
TT
1234/* A structure used to pass information through
1235 map_breakpoint_numbers. */
1236
1237struct commands_info
1238{
1239 /* True if the command was typed at a tty. */
1240 int from_tty;
86b17b60
PA
1241
1242 /* The breakpoint range spec. */
1243 char *arg;
1244
95a42b64
TT
1245 /* Non-NULL if the body of the commands are being read from this
1246 already-parsed command. */
1247 struct command_line *control;
86b17b60 1248
95a42b64
TT
1249 /* The command lines read from the user, or NULL if they have not
1250 yet been read. */
1251 struct counted_command_line *cmd;
1252};
1253
1254/* A callback for map_breakpoint_numbers that sets the commands for
1255 commands_command. */
1256
c906108c 1257static void
95a42b64 1258do_map_commands_command (struct breakpoint *b, void *data)
c906108c 1259{
95a42b64 1260 struct commands_info *info = data;
c906108c 1261
95a42b64
TT
1262 if (info->cmd == NULL)
1263 {
1264 struct command_line *l;
5c44784c 1265
95a42b64
TT
1266 if (info->control != NULL)
1267 l = copy_command_lines (info->control->body_list[0]);
1268 else
86b17b60
PA
1269 {
1270 struct cleanup *old_chain;
1271 char *str;
c5aa993b 1272
3e43a32a
MS
1273 str = xstrprintf (_("Type commands for breakpoint(s) "
1274 "%s, one per line."),
86b17b60
PA
1275 info->arg);
1276
1277 old_chain = make_cleanup (xfree, str);
1278
1279 l = read_command_lines (str,
1280 info->from_tty, 1,
d77f58be 1281 (is_tracepoint (b)
86b17b60
PA
1282 ? check_tracepoint_command : 0),
1283 b);
1284
1285 do_cleanups (old_chain);
1286 }
a7bdde9e 1287
95a42b64
TT
1288 info->cmd = alloc_counted_command_line (l);
1289 }
1290
1291 /* If a breakpoint was on the list more than once, we don't need to
1292 do anything. */
1293 if (b->commands != info->cmd)
1294 {
1295 validate_commands_for_breakpoint (b, info->cmd->commands);
1296 incref_counted_command_line (info->cmd);
1297 decref_counted_command_line (&b->commands);
1298 b->commands = info->cmd;
1299 breakpoints_changed ();
8d3788bd 1300 observer_notify_breakpoint_modified (b);
c5aa993b 1301 }
95a42b64
TT
1302}
1303
1304static void
4a64f543
MS
1305commands_command_1 (char *arg, int from_tty,
1306 struct command_line *control)
95a42b64
TT
1307{
1308 struct cleanup *cleanups;
1309 struct commands_info info;
1310
1311 info.from_tty = from_tty;
1312 info.control = control;
1313 info.cmd = NULL;
1314 /* If we read command lines from the user, then `info' will hold an
1315 extra reference to the commands that we must clean up. */
1316 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1317
1318 if (arg == NULL || !*arg)
1319 {
86b17b60 1320 if (breakpoint_count - prev_breakpoint_count > 1)
4a64f543
MS
1321 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1322 breakpoint_count);
95a42b64
TT
1323 else if (breakpoint_count > 0)
1324 arg = xstrprintf ("%d", breakpoint_count);
86b17b60
PA
1325 else
1326 {
1327 /* So that we don't try to free the incoming non-NULL
1328 argument in the cleanup below. Mapping breakpoint
1329 numbers will fail in this case. */
1330 arg = NULL;
1331 }
95a42b64 1332 }
9766ced4
SS
1333 else
1334 /* The command loop has some static state, so we need to preserve
1335 our argument. */
1336 arg = xstrdup (arg);
86b17b60
PA
1337
1338 if (arg != NULL)
1339 make_cleanup (xfree, arg);
1340
1341 info.arg = arg;
95a42b64
TT
1342
1343 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1344
1345 if (info.cmd == NULL)
1346 error (_("No breakpoints specified."));
1347
1348 do_cleanups (cleanups);
1349}
1350
1351static void
1352commands_command (char *arg, int from_tty)
1353{
1354 commands_command_1 (arg, from_tty, NULL);
c906108c 1355}
40c03ae8
EZ
1356
1357/* Like commands_command, but instead of reading the commands from
1358 input stream, takes them from an already parsed command structure.
1359
1360 This is used by cli-script.c to DTRT with breakpoint commands
1361 that are part of if and while bodies. */
1362enum command_control_type
1363commands_from_control_command (char *arg, struct command_line *cmd)
1364{
95a42b64
TT
1365 commands_command_1 (arg, 0, cmd);
1366 return simple_control;
40c03ae8 1367}
876fa593
JK
1368
1369/* Return non-zero if BL->TARGET_INFO contains valid information. */
1370
1371static int
1372bp_location_has_shadow (struct bp_location *bl)
1373{
1374 if (bl->loc_type != bp_loc_software_breakpoint)
1375 return 0;
1376 if (!bl->inserted)
1377 return 0;
1378 if (bl->target_info.shadow_len == 0)
e5dd4106 1379 /* BL isn't valid, or doesn't shadow memory. */
876fa593
JK
1380 return 0;
1381 return 1;
1382}
1383
8defab1a 1384/* Update BUF, which is LEN bytes read from the target address MEMADDR,
876fa593
JK
1385 by replacing any memory breakpoints with their shadowed contents.
1386
35c63cd8
JB
1387 If READBUF is not NULL, this buffer must not overlap with any of
1388 the breakpoint location's shadow_contents buffers. Otherwise,
1389 a failed assertion internal error will be raised.
1390
876fa593 1391 The range of shadowed area by each bp_location is:
35df4500
TJB
1392 bl->address - bp_location_placed_address_before_address_max
1393 up to bl->address + bp_location_shadow_len_after_address_max
876fa593
JK
1394 The range we were requested to resolve shadows for is:
1395 memaddr ... memaddr + len
1396 Thus the safe cutoff boundaries for performance optimization are
35df4500
TJB
1397 memaddr + len <= (bl->address
1398 - bp_location_placed_address_before_address_max)
876fa593 1399 and:
35df4500 1400 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
c906108c 1401
8defab1a 1402void
f0ba3972
PA
1403breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1404 const gdb_byte *writebuf_org,
1405 ULONGEST memaddr, LONGEST len)
c906108c 1406{
4a64f543
MS
1407 /* Left boundary, right boundary and median element of our binary
1408 search. */
876fa593
JK
1409 unsigned bc_l, bc_r, bc;
1410
4a64f543
MS
1411 /* Find BC_L which is a leftmost element which may affect BUF
1412 content. It is safe to report lower value but a failure to
1413 report higher one. */
876fa593
JK
1414
1415 bc_l = 0;
1416 bc_r = bp_location_count;
1417 while (bc_l + 1 < bc_r)
1418 {
35df4500 1419 struct bp_location *bl;
876fa593
JK
1420
1421 bc = (bc_l + bc_r) / 2;
35df4500 1422 bl = bp_location[bc];
876fa593 1423
4a64f543
MS
1424 /* Check first BL->ADDRESS will not overflow due to the added
1425 constant. Then advance the left boundary only if we are sure
1426 the BC element can in no way affect the BUF content (MEMADDR
1427 to MEMADDR + LEN range).
876fa593 1428
4a64f543
MS
1429 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1430 offset so that we cannot miss a breakpoint with its shadow
1431 range tail still reaching MEMADDR. */
c5aa993b 1432
35df4500
TJB
1433 if ((bl->address + bp_location_shadow_len_after_address_max
1434 >= bl->address)
1435 && (bl->address + bp_location_shadow_len_after_address_max
1436 <= memaddr))
876fa593
JK
1437 bc_l = bc;
1438 else
1439 bc_r = bc;
1440 }
1441
128070bb
PA
1442 /* Due to the binary search above, we need to make sure we pick the
1443 first location that's at BC_L's address. E.g., if there are
1444 multiple locations at the same address, BC_L may end up pointing
1445 at a duplicate location, and miss the "master"/"inserted"
1446 location. Say, given locations L1, L2 and L3 at addresses A and
1447 B:
1448
1449 L1@A, L2@A, L3@B, ...
1450
1451 BC_L could end up pointing at location L2, while the "master"
1452 location could be L1. Since the `loc->inserted' flag is only set
1453 on "master" locations, we'd forget to restore the shadow of L1
1454 and L2. */
1455 while (bc_l > 0
1456 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1457 bc_l--;
1458
876fa593
JK
1459 /* Now do full processing of the found relevant range of elements. */
1460
1461 for (bc = bc_l; bc < bp_location_count; bc++)
c5aa993b 1462 {
35df4500 1463 struct bp_location *bl = bp_location[bc];
876fa593
JK
1464 CORE_ADDR bp_addr = 0;
1465 int bp_size = 0;
1466 int bptoffset = 0;
1467
35df4500
TJB
1468 /* bp_location array has BL->OWNER always non-NULL. */
1469 if (bl->owner->type == bp_none)
8a3fe4f8 1470 warning (_("reading through apparently deleted breakpoint #%d?"),
35df4500 1471 bl->owner->number);
ffce0d52 1472
e5dd4106 1473 /* Performance optimization: any further element can no longer affect BUF
876fa593
JK
1474 content. */
1475
35df4500
TJB
1476 if (bl->address >= bp_location_placed_address_before_address_max
1477 && memaddr + len <= (bl->address
1478 - bp_location_placed_address_before_address_max))
876fa593
JK
1479 break;
1480
35df4500 1481 if (!bp_location_has_shadow (bl))
c5aa993b 1482 continue;
35df4500 1483 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
6c95b8df
PA
1484 current_program_space->aspace, 0))
1485 continue;
1486
c5aa993b
JM
1487 /* Addresses and length of the part of the breakpoint that
1488 we need to copy. */
35df4500
TJB
1489 bp_addr = bl->target_info.placed_address;
1490 bp_size = bl->target_info.shadow_len;
8defab1a 1491
c5aa993b
JM
1492 if (bp_addr + bp_size <= memaddr)
1493 /* The breakpoint is entirely before the chunk of memory we
1494 are reading. */
1495 continue;
8defab1a 1496
c5aa993b
JM
1497 if (bp_addr >= memaddr + len)
1498 /* The breakpoint is entirely after the chunk of memory we are
4a64f543 1499 reading. */
c5aa993b 1500 continue;
c5aa993b 1501
8defab1a
DJ
1502 /* Offset within shadow_contents. */
1503 if (bp_addr < memaddr)
1504 {
1505 /* Only copy the second part of the breakpoint. */
1506 bp_size -= memaddr - bp_addr;
1507 bptoffset = memaddr - bp_addr;
1508 bp_addr = memaddr;
1509 }
c5aa993b 1510
8defab1a
DJ
1511 if (bp_addr + bp_size > memaddr + len)
1512 {
1513 /* Only copy the first part of the breakpoint. */
1514 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1515 }
c5aa993b 1516
f0ba3972
PA
1517 if (readbuf != NULL)
1518 {
35c63cd8
JB
1519 /* Verify that the readbuf buffer does not overlap with
1520 the shadow_contents buffer. */
1521 gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1522 || readbuf >= (bl->target_info.shadow_contents
1523 + bl->target_info.shadow_len));
1524
f0ba3972
PA
1525 /* Update the read buffer with this inserted breakpoint's
1526 shadow. */
1527 memcpy (readbuf + bp_addr - memaddr,
1528 bl->target_info.shadow_contents + bptoffset, bp_size);
1529 }
1530 else
1531 {
1532 struct gdbarch *gdbarch = bl->gdbarch;
1533 const unsigned char *bp;
1534 CORE_ADDR placed_address = bl->target_info.placed_address;
1535 unsigned placed_size = bl->target_info.placed_size;
1536
1537 /* Update the shadow with what we want to write to memory. */
1538 memcpy (bl->target_info.shadow_contents + bptoffset,
1539 writebuf_org + bp_addr - memaddr, bp_size);
1540
1541 /* Determine appropriate breakpoint contents and size for this
1542 address. */
1543 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1544
1545 /* Update the final write buffer with this inserted
1546 breakpoint's INSN. */
1547 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1548 }
c5aa993b 1549 }
c906108c 1550}
c906108c 1551\f
c5aa993b 1552
b775012e
LM
1553/* Return true if BPT is either a software breakpoint or a hardware
1554 breakpoint. */
1555
1556int
1557is_breakpoint (const struct breakpoint *bpt)
1558{
1559 return (bpt->type == bp_breakpoint
e7e0cddf
SS
1560 || bpt->type == bp_hardware_breakpoint
1561 || bpt->type == bp_dprintf);
b775012e
LM
1562}
1563
60e1c644
PA
1564/* Return true if BPT is of any hardware watchpoint kind. */
1565
a5606eee 1566static int
d77f58be 1567is_hardware_watchpoint (const struct breakpoint *bpt)
a5606eee
VP
1568{
1569 return (bpt->type == bp_hardware_watchpoint
1570 || bpt->type == bp_read_watchpoint
1571 || bpt->type == bp_access_watchpoint);
1572}
7270d8f2 1573
60e1c644
PA
1574/* Return true if BPT is of any watchpoint kind, hardware or
1575 software. */
1576
3a5c3e22 1577int
d77f58be 1578is_watchpoint (const struct breakpoint *bpt)
60e1c644
PA
1579{
1580 return (is_hardware_watchpoint (bpt)
1581 || bpt->type == bp_watchpoint);
1582}
1583
3a5c3e22
PA
1584/* Returns true if the current thread and its running state are safe
1585 to evaluate or update watchpoint B. Watchpoints on local
1586 expressions need to be evaluated in the context of the thread that
1587 was current when the watchpoint was created, and, that thread needs
1588 to be stopped to be able to select the correct frame context.
1589 Watchpoints on global expressions can be evaluated on any thread,
1590 and in any state. It is presently left to the target allowing
1591 memory accesses when threads are running. */
f6bc2008
PA
1592
1593static int
3a5c3e22 1594watchpoint_in_thread_scope (struct watchpoint *b)
f6bc2008 1595{
d0d8b0c6
JK
1596 return (b->base.pspace == current_program_space
1597 && (ptid_equal (b->watchpoint_thread, null_ptid)
1598 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1599 && !is_executing (inferior_ptid))));
f6bc2008
PA
1600}
1601
d0fb5eae
JK
1602/* Set watchpoint B to disp_del_at_next_stop, even including its possible
1603 associated bp_watchpoint_scope breakpoint. */
1604
1605static void
3a5c3e22 1606watchpoint_del_at_next_stop (struct watchpoint *w)
d0fb5eae 1607{
3a5c3e22 1608 struct breakpoint *b = &w->base;
d0fb5eae
JK
1609
1610 if (b->related_breakpoint != b)
1611 {
1612 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1613 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1614 b->related_breakpoint->disposition = disp_del_at_next_stop;
1615 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1616 b->related_breakpoint = b;
1617 }
1618 b->disposition = disp_del_at_next_stop;
1619}
1620
567e1b4e
JB
1621/* Assuming that B is a watchpoint:
1622 - Reparse watchpoint expression, if REPARSE is non-zero
a5606eee 1623 - Evaluate expression and store the result in B->val
567e1b4e
JB
1624 - Evaluate the condition if there is one, and store the result
1625 in b->loc->cond.
a5606eee
VP
1626 - Update the list of values that must be watched in B->loc.
1627
4a64f543
MS
1628 If the watchpoint disposition is disp_del_at_next_stop, then do
1629 nothing. If this is local watchpoint that is out of scope, delete
1630 it.
1631
1632 Even with `set breakpoint always-inserted on' the watchpoints are
1633 removed + inserted on each stop here. Normal breakpoints must
1634 never be removed because they might be missed by a running thread
1635 when debugging in non-stop mode. On the other hand, hardware
1636 watchpoints (is_hardware_watchpoint; processed here) are specific
1637 to each LWP since they are stored in each LWP's hardware debug
1638 registers. Therefore, such LWP must be stopped first in order to
1639 be able to modify its hardware watchpoints.
1640
1641 Hardware watchpoints must be reset exactly once after being
1642 presented to the user. It cannot be done sooner, because it would
1643 reset the data used to present the watchpoint hit to the user. And
1644 it must not be done later because it could display the same single
1645 watchpoint hit during multiple GDB stops. Note that the latter is
1646 relevant only to the hardware watchpoint types bp_read_watchpoint
1647 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1648 not user-visible - its hit is suppressed if the memory content has
1649 not changed.
1650
1651 The following constraints influence the location where we can reset
1652 hardware watchpoints:
1653
1654 * target_stopped_by_watchpoint and target_stopped_data_address are
1655 called several times when GDB stops.
1656
1657 [linux]
1658 * Multiple hardware watchpoints can be hit at the same time,
1659 causing GDB to stop. GDB only presents one hardware watchpoint
1660 hit at a time as the reason for stopping, and all the other hits
1661 are presented later, one after the other, each time the user
1662 requests the execution to be resumed. Execution is not resumed
1663 for the threads still having pending hit event stored in
1664 LWP_INFO->STATUS. While the watchpoint is already removed from
1665 the inferior on the first stop the thread hit event is kept being
1666 reported from its cached value by linux_nat_stopped_data_address
1667 until the real thread resume happens after the watchpoint gets
1668 presented and thus its LWP_INFO->STATUS gets reset.
1669
1670 Therefore the hardware watchpoint hit can get safely reset on the
1671 watchpoint removal from inferior. */
a79d3c27 1672
b40ce68a 1673static void
3a5c3e22 1674update_watchpoint (struct watchpoint *b, int reparse)
7270d8f2 1675{
a5606eee 1676 int within_current_scope;
a5606eee 1677 struct frame_id saved_frame_id;
66076460 1678 int frame_saved;
a5606eee 1679
f6bc2008
PA
1680 /* If this is a local watchpoint, we only want to check if the
1681 watchpoint frame is in scope if the current thread is the thread
1682 that was used to create the watchpoint. */
1683 if (!watchpoint_in_thread_scope (b))
1684 return;
1685
3a5c3e22 1686 if (b->base.disposition == disp_del_at_next_stop)
a5606eee
VP
1687 return;
1688
66076460 1689 frame_saved = 0;
a5606eee
VP
1690
1691 /* Determine if the watchpoint is within scope. */
1692 if (b->exp_valid_block == NULL)
1693 within_current_scope = 1;
1694 else
1695 {
b5db5dfc
UW
1696 struct frame_info *fi = get_current_frame ();
1697 struct gdbarch *frame_arch = get_frame_arch (fi);
1698 CORE_ADDR frame_pc = get_frame_pc (fi);
1699
1700 /* If we're in a function epilogue, unwinding may not work
1701 properly, so do not attempt to recreate locations at this
1702 point. See similar comments in watchpoint_check. */
1703 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1704 return;
66076460
DJ
1705
1706 /* Save the current frame's ID so we can restore it after
1707 evaluating the watchpoint expression on its own frame. */
1708 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1709 took a frame parameter, so that we didn't have to change the
1710 selected frame. */
1711 frame_saved = 1;
1712 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1713
a5606eee
VP
1714 fi = frame_find_by_id (b->watchpoint_frame);
1715 within_current_scope = (fi != NULL);
1716 if (within_current_scope)
1717 select_frame (fi);
1718 }
1719
b5db5dfc
UW
1720 /* We don't free locations. They are stored in the bp_location array
1721 and update_global_location_list will eventually delete them and
1722 remove breakpoints if needed. */
3a5c3e22 1723 b->base.loc = NULL;
b5db5dfc 1724
a5606eee
VP
1725 if (within_current_scope && reparse)
1726 {
1727 char *s;
d63d0675 1728
a5606eee
VP
1729 if (b->exp)
1730 {
1731 xfree (b->exp);
1732 b->exp = NULL;
1733 }
d63d0675 1734 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1bb9788d 1735 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
a5606eee
VP
1736 /* If the meaning of expression itself changed, the old value is
1737 no longer relevant. We don't want to report a watchpoint hit
1738 to the user when the old value and the new value may actually
1739 be completely different objects. */
1740 value_free (b->val);
fa4727a6
DJ
1741 b->val = NULL;
1742 b->val_valid = 0;
60e1c644
PA
1743
1744 /* Note that unlike with breakpoints, the watchpoint's condition
1745 expression is stored in the breakpoint object, not in the
1746 locations (re)created below. */
3a5c3e22 1747 if (b->base.cond_string != NULL)
60e1c644
PA
1748 {
1749 if (b->cond_exp != NULL)
1750 {
1751 xfree (b->cond_exp);
1752 b->cond_exp = NULL;
1753 }
1754
3a5c3e22 1755 s = b->base.cond_string;
1bb9788d 1756 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
60e1c644 1757 }
a5606eee 1758 }
a5606eee
VP
1759
1760 /* If we failed to parse the expression, for example because
1761 it refers to a global variable in a not-yet-loaded shared library,
1762 don't try to insert watchpoint. We don't automatically delete
1763 such watchpoint, though, since failure to parse expression
1764 is different from out-of-scope watchpoint. */
2d134ed3
PA
1765 if ( !target_has_execution)
1766 {
1767 /* Without execution, memory can't change. No use to try and
1768 set watchpoint locations. The watchpoint will be reset when
1769 the target gains execution, through breakpoint_re_set. */
1770 }
1771 else if (within_current_scope && b->exp)
a5606eee 1772 {
0cf6dd15 1773 int pc = 0;
fa4727a6 1774 struct value *val_chain, *v, *result, *next;
2d134ed3 1775 struct program_space *frame_pspace;
a5606eee 1776
0cf6dd15 1777 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
a5606eee 1778
a5606eee
VP
1779 /* Avoid setting b->val if it's already set. The meaning of
1780 b->val is 'the last value' user saw, and we should update
1781 it only if we reported that last value to user. As it
9c06b0b4
TJB
1782 happens, the code that reports it updates b->val directly.
1783 We don't keep track of the memory value for masked
1784 watchpoints. */
3a5c3e22 1785 if (!b->val_valid && !is_masked_watchpoint (&b->base))
fa4727a6
DJ
1786 {
1787 b->val = v;
1788 b->val_valid = 1;
1789 }
a5606eee 1790
2d134ed3
PA
1791 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1792
a5606eee 1793 /* Look at each value on the value chain. */
9fa40276 1794 for (v = val_chain; v; v = value_next (v))
a5606eee
VP
1795 {
1796 /* If it's a memory location, and GDB actually needed
1797 its contents to evaluate the expression, then we
fa4727a6
DJ
1798 must watch it. If the first value returned is
1799 still lazy, that means an error occurred reading it;
1800 watch it anyway in case it becomes readable. */
a5606eee 1801 if (VALUE_LVAL (v) == lval_memory
fa4727a6 1802 && (v == val_chain || ! value_lazy (v)))
a5606eee
VP
1803 {
1804 struct type *vtype = check_typedef (value_type (v));
7270d8f2 1805
a5606eee
VP
1806 /* We only watch structs and arrays if user asked
1807 for it explicitly, never if they just happen to
1808 appear in the middle of some value chain. */
fa4727a6 1809 if (v == result
a5606eee
VP
1810 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1811 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1812 {
1813 CORE_ADDR addr;
1814 int len, type;
1815 struct bp_location *loc, **tmp;
1816
42ae5230 1817 addr = value_address (v);
a5606eee
VP
1818 len = TYPE_LENGTH (value_type (v));
1819 type = hw_write;
3a5c3e22 1820 if (b->base.type == bp_read_watchpoint)
a5606eee 1821 type = hw_read;
3a5c3e22 1822 else if (b->base.type == bp_access_watchpoint)
a5606eee 1823 type = hw_access;
3a5c3e22
PA
1824
1825 loc = allocate_bp_location (&b->base);
1826 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
a5606eee
VP
1827 ;
1828 *tmp = loc;
a6d9a66e 1829 loc->gdbarch = get_type_arch (value_type (v));
6c95b8df
PA
1830
1831 loc->pspace = frame_pspace;
a5606eee
VP
1832 loc->address = addr;
1833 loc->length = len;
1834 loc->watchpoint_type = type;
1835 }
1836 }
9fa40276
TJB
1837 }
1838
1839 /* Change the type of breakpoint between hardware assisted or
1840 an ordinary watchpoint depending on the hardware support
1841 and free hardware slots. REPARSE is set when the inferior
1842 is started. */
a9634178 1843 if (reparse)
9fa40276 1844 {
e09342b5 1845 int reg_cnt;
9fa40276
TJB
1846 enum bp_loc_type loc_type;
1847 struct bp_location *bl;
a5606eee 1848
a9634178 1849 reg_cnt = can_use_hardware_watchpoint (val_chain);
e09342b5
TJB
1850
1851 if (reg_cnt)
9fa40276
TJB
1852 {
1853 int i, target_resources_ok, other_type_used;
a1398e0c 1854 enum bptype type;
9fa40276 1855
a9634178
TJB
1856 /* Use an exact watchpoint when there's only one memory region to be
1857 watched, and only one debug register is needed to watch it. */
1858 b->exact = target_exact_watchpoints && reg_cnt == 1;
1859
9fa40276 1860 /* We need to determine how many resources are already
e09342b5
TJB
1861 used for all other hardware watchpoints plus this one
1862 to see if we still have enough resources to also fit
a1398e0c
PA
1863 this watchpoint in as well. */
1864
1865 /* If this is a software watchpoint, we try to turn it
1866 to a hardware one -- count resources as if B was of
1867 hardware watchpoint type. */
1868 type = b->base.type;
1869 if (type == bp_watchpoint)
1870 type = bp_hardware_watchpoint;
1871
1872 /* This watchpoint may or may not have been placed on
1873 the list yet at this point (it won't be in the list
1874 if we're trying to create it for the first time,
1875 through watch_command), so always account for it
1876 manually. */
1877
1878 /* Count resources used by all watchpoints except B. */
1879 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1880
1881 /* Add in the resources needed for B. */
1882 i += hw_watchpoint_use_count (&b->base);
1883
1884 target_resources_ok
1885 = target_can_use_hardware_watchpoint (type, i, other_type_used);
e09342b5 1886 if (target_resources_ok <= 0)
a9634178 1887 {
3a5c3e22 1888 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
9c06b0b4
TJB
1889
1890 if (target_resources_ok == 0 && !sw_mode)
a9634178
TJB
1891 error (_("Target does not support this type of "
1892 "hardware watchpoint."));
9c06b0b4
TJB
1893 else if (target_resources_ok < 0 && !sw_mode)
1894 error (_("There are not enough available hardware "
1895 "resources for this watchpoint."));
a1398e0c
PA
1896
1897 /* Downgrade to software watchpoint. */
1898 b->base.type = bp_watchpoint;
1899 }
1900 else
1901 {
1902 /* If this was a software watchpoint, we've just
1903 found we have enough resources to turn it to a
1904 hardware watchpoint. Otherwise, this is a
1905 nop. */
1906 b->base.type = type;
a9634178 1907 }
9fa40276 1908 }
3a5c3e22 1909 else if (!b->base.ops->works_in_software_mode (&b->base))
a9634178
TJB
1910 error (_("Expression cannot be implemented with "
1911 "read/access watchpoint."));
9fa40276 1912 else
3a5c3e22 1913 b->base.type = bp_watchpoint;
9fa40276 1914
3a5c3e22 1915 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
9fa40276 1916 : bp_loc_hardware_watchpoint);
3a5c3e22 1917 for (bl = b->base.loc; bl; bl = bl->next)
9fa40276
TJB
1918 bl->loc_type = loc_type;
1919 }
1920
1921 for (v = val_chain; v; v = next)
1922 {
a5606eee
VP
1923 next = value_next (v);
1924 if (v != b->val)
1925 value_free (v);
1926 }
1927
c7437ca6
PA
1928 /* If a software watchpoint is not watching any memory, then the
1929 above left it without any location set up. But,
1930 bpstat_stop_status requires a location to be able to report
1931 stops, so make sure there's at least a dummy one. */
3a5c3e22 1932 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
c7437ca6 1933 {
3a5c3e22
PA
1934 struct breakpoint *base = &b->base;
1935 base->loc = allocate_bp_location (base);
1936 base->loc->pspace = frame_pspace;
1937 base->loc->address = -1;
1938 base->loc->length = -1;
1939 base->loc->watchpoint_type = -1;
c7437ca6 1940 }
a5606eee
VP
1941 }
1942 else if (!within_current_scope)
7270d8f2 1943 {
ac74f770
MS
1944 printf_filtered (_("\
1945Watchpoint %d deleted because the program has left the block\n\
1946in which its expression is valid.\n"),
3a5c3e22 1947 b->base.number);
d0fb5eae 1948 watchpoint_del_at_next_stop (b);
7270d8f2 1949 }
a5606eee
VP
1950
1951 /* Restore the selected frame. */
66076460
DJ
1952 if (frame_saved)
1953 select_frame (frame_find_by_id (saved_frame_id));
7270d8f2
OF
1954}
1955
a5606eee 1956
74960c60 1957/* Returns 1 iff breakpoint location should be
1e4d1764
YQ
1958 inserted in the inferior. We don't differentiate the type of BL's owner
1959 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1960 breakpoint_ops is not defined, because in insert_bp_location,
1961 tracepoint's insert_location will not be called. */
74960c60 1962static int
35df4500 1963should_be_inserted (struct bp_location *bl)
74960c60 1964{
35df4500 1965 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
74960c60
VP
1966 return 0;
1967
35df4500 1968 if (bl->owner->disposition == disp_del_at_next_stop)
74960c60
VP
1969 return 0;
1970
35df4500 1971 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
74960c60
VP
1972 return 0;
1973
f8eba3c6
TT
1974 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1975 return 0;
1976
56710373
PA
1977 /* This is set for example, when we're attached to the parent of a
1978 vfork, and have detached from the child. The child is running
1979 free, and we expect it to do an exec or exit, at which point the
1980 OS makes the parent schedulable again (and the target reports
1981 that the vfork is done). Until the child is done with the shared
1982 memory region, do not insert breakpoints in the parent, otherwise
1983 the child could still trip on the parent's breakpoints. Since
1984 the parent is blocked anyway, it won't miss any breakpoint. */
35df4500 1985 if (bl->pspace->breakpoints_not_allowed)
56710373
PA
1986 return 0;
1987
74960c60
VP
1988 return 1;
1989}
1990
934709f0
PW
1991/* Same as should_be_inserted but does the check assuming
1992 that the location is not duplicated. */
1993
1994static int
1995unduplicated_should_be_inserted (struct bp_location *bl)
1996{
1997 int result;
1998 const int save_duplicate = bl->duplicate;
1999
2000 bl->duplicate = 0;
2001 result = should_be_inserted (bl);
2002 bl->duplicate = save_duplicate;
2003 return result;
2004}
2005
b775012e
LM
2006/* Parses a conditional described by an expression COND into an
2007 agent expression bytecode suitable for evaluation
2008 by the bytecode interpreter. Return NULL if there was
2009 any error during parsing. */
2010
2011static struct agent_expr *
2012parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2013{
2014 struct agent_expr *aexpr = NULL;
2015 struct cleanup *old_chain = NULL;
2016 volatile struct gdb_exception ex;
2017
2018 if (!cond)
2019 return NULL;
2020
2021 /* We don't want to stop processing, so catch any errors
2022 that may show up. */
2023 TRY_CATCH (ex, RETURN_MASK_ERROR)
2024 {
2025 aexpr = gen_eval_for_expr (scope, cond);
2026 }
2027
2028 if (ex.reason < 0)
2029 {
2030 /* If we got here, it means the condition could not be parsed to a valid
2031 bytecode expression and thus can't be evaluated on the target's side.
2032 It's no use iterating through the conditions. */
2033 return NULL;
2034 }
2035
2036 /* We have a valid agent expression. */
2037 return aexpr;
2038}
2039
2040/* Based on location BL, create a list of breakpoint conditions to be
2041 passed on to the target. If we have duplicated locations with different
2042 conditions, we will add such conditions to the list. The idea is that the
2043 target will evaluate the list of conditions and will only notify GDB when
2044 one of them is true. */
2045
2046static void
2047build_target_condition_list (struct bp_location *bl)
2048{
2049 struct bp_location **locp = NULL, **loc2p;
2050 int null_condition_or_parse_error = 0;
2051 int modified = bl->needs_update;
2052 struct bp_location *loc;
2053
2054 /* This is only meaningful if the target is
2055 evaluating conditions and if the user has
2056 opted for condition evaluation on the target's
2057 side. */
2058 if (gdb_evaluates_breakpoint_condition_p ()
2059 || !target_supports_evaluation_of_breakpoint_conditions ())
2060 return;
2061
2062 /* Do a first pass to check for locations with no assigned
2063 conditions or conditions that fail to parse to a valid agent expression
2064 bytecode. If any of these happen, then it's no use to send conditions
2065 to the target since this location will always trigger and generate a
2066 response back to GDB. */
2067 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2068 {
2069 loc = (*loc2p);
2070 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2071 {
2072 if (modified)
2073 {
2074 struct agent_expr *aexpr;
2075
2076 /* Re-parse the conditions since something changed. In that
2077 case we already freed the condition bytecodes (see
2078 force_breakpoint_reinsertion). We just
2079 need to parse the condition to bytecodes again. */
2080 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2081 loc->cond_bytecode = aexpr;
2082
2083 /* Check if we managed to parse the conditional expression
2084 correctly. If not, we will not send this condition
2085 to the target. */
2086 if (aexpr)
2087 continue;
2088 }
2089
2090 /* If we have a NULL bytecode expression, it means something
2091 went wrong or we have a null condition expression. */
2092 if (!loc->cond_bytecode)
2093 {
2094 null_condition_or_parse_error = 1;
2095 break;
2096 }
2097 }
2098 }
2099
2100 /* If any of these happened, it means we will have to evaluate the conditions
2101 for the location's address on gdb's side. It is no use keeping bytecodes
2102 for all the other duplicate locations, thus we free all of them here.
2103
2104 This is so we have a finer control over which locations' conditions are
2105 being evaluated by GDB or the remote stub. */
2106 if (null_condition_or_parse_error)
2107 {
2108 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2109 {
2110 loc = (*loc2p);
2111 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2112 {
2113 /* Only go as far as the first NULL bytecode is
2114 located. */
2115 if (!loc->cond_bytecode)
2116 return;
2117
2118 free_agent_expr (loc->cond_bytecode);
2119 loc->cond_bytecode = NULL;
2120 }
2121 }
2122 }
2123
2124 /* No NULL conditions or failed bytecode generation. Build a condition list
2125 for this location's address. */
2126 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2127 {
2128 loc = (*loc2p);
2129 if (loc->cond
2130 && is_breakpoint (loc->owner)
2131 && loc->pspace->num == bl->pspace->num
2132 && loc->owner->enable_state == bp_enabled
2133 && loc->enabled)
2134 /* Add the condition to the vector. This will be used later to send the
2135 conditions to the target. */
2136 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2137 loc->cond_bytecode);
2138 }
2139
2140 return;
2141}
2142
35df4500
TJB
2143/* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2144 location. Any error messages are printed to TMP_ERROR_STREAM; and
3fbb6ffa 2145 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
c30eee59
TJB
2146 Returns 0 for success, 1 if the bp_location type is not supported or
2147 -1 for failure.
879bfdc2 2148
4a64f543
MS
2149 NOTE drow/2003-09-09: This routine could be broken down to an
2150 object-style method for each breakpoint or catchpoint type. */
26bb91f3 2151static int
35df4500 2152insert_bp_location (struct bp_location *bl,
26bb91f3 2153 struct ui_file *tmp_error_stream,
3fbb6ffa 2154 int *disabled_breaks,
26bb91f3 2155 int *hw_breakpoint_error)
879bfdc2
DJ
2156{
2157 int val = 0;
2158
b775012e 2159 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2160 return 0;
2161
35c63cd8
JB
2162 /* Note we don't initialize bl->target_info, as that wipes out
2163 the breakpoint location's shadow_contents if the breakpoint
2164 is still inserted at that location. This in turn breaks
2165 target_read_memory which depends on these buffers when
2166 a memory read is requested at the breakpoint location:
2167 Once the target_info has been wiped, we fail to see that
2168 we have a breakpoint inserted at that address and thus
2169 read the breakpoint instead of returning the data saved in
2170 the breakpoint location's shadow contents. */
35df4500
TJB
2171 bl->target_info.placed_address = bl->address;
2172 bl->target_info.placed_address_space = bl->pspace->aspace;
f1310107 2173 bl->target_info.length = bl->length;
8181d85f 2174
b775012e
LM
2175 /* When working with target-side conditions, we must pass all the conditions
2176 for the same breakpoint address down to the target since GDB will not
2177 insert those locations. With a list of breakpoint conditions, the target
2178 can decide when to stop and notify GDB. */
2179
2180 if (is_breakpoint (bl->owner))
2181 {
2182 build_target_condition_list (bl);
2183 /* Reset the condition modification marker. */
2184 bl->needs_update = 0;
2185 }
2186
35df4500
TJB
2187 if (bl->loc_type == bp_loc_software_breakpoint
2188 || bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2 2189 {
35df4500 2190 if (bl->owner->type != bp_hardware_breakpoint)
765dc015
VP
2191 {
2192 /* If the explicitly specified breakpoint type
2193 is not hardware breakpoint, check the memory map to see
2194 if the breakpoint address is in read only memory or not.
4a64f543 2195
765dc015
VP
2196 Two important cases are:
2197 - location type is not hardware breakpoint, memory
2198 is readonly. We change the type of the location to
2199 hardware breakpoint.
4a64f543
MS
2200 - location type is hardware breakpoint, memory is
2201 read-write. This means we've previously made the
2202 location hardware one, but then the memory map changed,
2203 so we undo.
765dc015 2204
4a64f543
MS
2205 When breakpoints are removed, remove_breakpoints will use
2206 location types we've just set here, the only possible
2207 problem is that memory map has changed during running
2208 program, but it's not going to work anyway with current
2209 gdb. */
765dc015 2210 struct mem_region *mr
35df4500 2211 = lookup_mem_region (bl->target_info.placed_address);
765dc015
VP
2212
2213 if (mr)
2214 {
2215 if (automatic_hardware_breakpoints)
2216 {
765dc015
VP
2217 enum bp_loc_type new_type;
2218
2219 if (mr->attrib.mode != MEM_RW)
2220 new_type = bp_loc_hardware_breakpoint;
2221 else
2222 new_type = bp_loc_software_breakpoint;
2223
35df4500 2224 if (new_type != bl->loc_type)
765dc015
VP
2225 {
2226 static int said = 0;
cc59ec59 2227
35df4500 2228 bl->loc_type = new_type;
765dc015
VP
2229 if (!said)
2230 {
3e43a32a
MS
2231 fprintf_filtered (gdb_stdout,
2232 _("Note: automatically using "
2233 "hardware breakpoints for "
2234 "read-only addresses.\n"));
765dc015
VP
2235 said = 1;
2236 }
2237 }
2238 }
35df4500 2239 else if (bl->loc_type == bp_loc_software_breakpoint
765dc015 2240 && mr->attrib.mode != MEM_RW)
3e43a32a
MS
2241 warning (_("cannot set software breakpoint "
2242 "at readonly address %s"),
35df4500 2243 paddress (bl->gdbarch, bl->address));
765dc015
VP
2244 }
2245 }
2246
879bfdc2
DJ
2247 /* First check to see if we have to handle an overlay. */
2248 if (overlay_debugging == ovly_off
35df4500
TJB
2249 || bl->section == NULL
2250 || !(section_is_overlay (bl->section)))
879bfdc2
DJ
2251 {
2252 /* No overlay handling: just set the breakpoint. */
2253
348d480f 2254 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
2255 }
2256 else
2257 {
4a64f543 2258 /* This breakpoint is in an overlay section.
879bfdc2
DJ
2259 Shall we set a breakpoint at the LMA? */
2260 if (!overlay_events_enabled)
2261 {
2262 /* Yes -- overlay event support is not active,
2263 so we must try to set a breakpoint at the LMA.
2264 This will not work for a hardware breakpoint. */
35df4500 2265 if (bl->loc_type == bp_loc_hardware_breakpoint)
8a3fe4f8 2266 warning (_("hardware breakpoint %d not supported in overlay!"),
35df4500 2267 bl->owner->number);
879bfdc2
DJ
2268 else
2269 {
35df4500
TJB
2270 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2271 bl->section);
879bfdc2 2272 /* Set a software (trap) breakpoint at the LMA. */
35df4500
TJB
2273 bl->overlay_target_info = bl->target_info;
2274 bl->overlay_target_info.placed_address = addr;
2275 val = target_insert_breakpoint (bl->gdbarch,
2276 &bl->overlay_target_info);
879bfdc2 2277 if (val != 0)
99361f52 2278 fprintf_unfiltered (tmp_error_stream,
3e43a32a
MS
2279 "Overlay breakpoint %d "
2280 "failed: in ROM?\n",
35df4500 2281 bl->owner->number);
879bfdc2
DJ
2282 }
2283 }
2284 /* Shall we set a breakpoint at the VMA? */
35df4500 2285 if (section_is_mapped (bl->section))
879bfdc2
DJ
2286 {
2287 /* Yes. This overlay section is mapped into memory. */
348d480f 2288 val = bl->owner->ops->insert_location (bl);
879bfdc2
DJ
2289 }
2290 else
2291 {
2292 /* No. This breakpoint will not be inserted.
2293 No error, but do not mark the bp as 'inserted'. */
2294 return 0;
2295 }
2296 }
2297
2298 if (val)
2299 {
2300 /* Can't set the breakpoint. */
35df4500 2301 if (solib_name_from_address (bl->pspace, bl->address))
879bfdc2 2302 {
4a64f543 2303 /* See also: disable_breakpoints_in_shlibs. */
879bfdc2 2304 val = 0;
35df4500 2305 bl->shlib_disabled = 1;
8d3788bd 2306 observer_notify_breakpoint_modified (bl->owner);
3fbb6ffa
TJB
2307 if (!*disabled_breaks)
2308 {
2309 fprintf_unfiltered (tmp_error_stream,
2310 "Cannot insert breakpoint %d.\n",
2311 bl->owner->number);
2312 fprintf_unfiltered (tmp_error_stream,
2313 "Temporarily disabling shared "
2314 "library breakpoints:\n");
2315 }
2316 *disabled_breaks = 1;
879bfdc2 2317 fprintf_unfiltered (tmp_error_stream,
35df4500 2318 "breakpoint #%d\n", bl->owner->number);
879bfdc2
DJ
2319 }
2320 else
879bfdc2 2321 {
35df4500 2322 if (bl->loc_type == bp_loc_hardware_breakpoint)
879bfdc2
DJ
2323 {
2324 *hw_breakpoint_error = 1;
3e43a32a
MS
2325 fprintf_unfiltered (tmp_error_stream,
2326 "Cannot insert hardware "
2327 "breakpoint %d.\n",
35df4500 2328 bl->owner->number);
879bfdc2
DJ
2329 }
2330 else
2331 {
2332 fprintf_unfiltered (tmp_error_stream,
2333 "Cannot insert breakpoint %d.\n",
35df4500 2334 bl->owner->number);
879bfdc2
DJ
2335 fprintf_filtered (tmp_error_stream,
2336 "Error accessing memory address ");
35df4500 2337 fputs_filtered (paddress (bl->gdbarch, bl->address),
5af949e3 2338 tmp_error_stream);
879bfdc2
DJ
2339 fprintf_filtered (tmp_error_stream, ": %s.\n",
2340 safe_strerror (val));
2341 }
2342
2343 }
2344 }
2345 else
35df4500 2346 bl->inserted = 1;
879bfdc2
DJ
2347
2348 return val;
2349 }
2350
35df4500 2351 else if (bl->loc_type == bp_loc_hardware_watchpoint
879bfdc2 2352 /* NOTE drow/2003-09-08: This state only exists for removing
4a64f543 2353 watchpoints. It's not clear that it's necessary... */
35df4500 2354 && bl->owner->disposition != disp_del_at_next_stop)
879bfdc2 2355 {
77b06cd7
TJB
2356 gdb_assert (bl->owner->ops != NULL
2357 && bl->owner->ops->insert_location != NULL);
2358
2359 val = bl->owner->ops->insert_location (bl);
85d721b8
PA
2360
2361 /* If trying to set a read-watchpoint, and it turns out it's not
2362 supported, try emulating one with an access watchpoint. */
35df4500 2363 if (val == 1 && bl->watchpoint_type == hw_read)
85d721b8
PA
2364 {
2365 struct bp_location *loc, **loc_temp;
2366
2367 /* But don't try to insert it, if there's already another
2368 hw_access location that would be considered a duplicate
2369 of this one. */
2370 ALL_BP_LOCATIONS (loc, loc_temp)
35df4500 2371 if (loc != bl
85d721b8 2372 && loc->watchpoint_type == hw_access
35df4500 2373 && watchpoint_locations_match (bl, loc))
85d721b8 2374 {
35df4500
TJB
2375 bl->duplicate = 1;
2376 bl->inserted = 1;
2377 bl->target_info = loc->target_info;
2378 bl->watchpoint_type = hw_access;
85d721b8
PA
2379 val = 0;
2380 break;
2381 }
2382
2383 if (val == 1)
2384 {
77b06cd7
TJB
2385 bl->watchpoint_type = hw_access;
2386 val = bl->owner->ops->insert_location (bl);
2387
2388 if (val)
2389 /* Back to the original value. */
2390 bl->watchpoint_type = hw_read;
85d721b8
PA
2391 }
2392 }
2393
35df4500 2394 bl->inserted = (val == 0);
879bfdc2
DJ
2395 }
2396
35df4500 2397 else if (bl->owner->type == bp_catchpoint)
879bfdc2 2398 {
77b06cd7
TJB
2399 gdb_assert (bl->owner->ops != NULL
2400 && bl->owner->ops->insert_location != NULL);
2401
2402 val = bl->owner->ops->insert_location (bl);
2403 if (val)
2404 {
2405 bl->owner->enable_state = bp_disabled;
2406
2407 if (val == 1)
2408 warning (_("\
2409Error inserting catchpoint %d: Your system does not support this type\n\
2410of catchpoint."), bl->owner->number);
2411 else
2412 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2413 }
2414
2415 bl->inserted = (val == 0);
1640b821
DJ
2416
2417 /* We've already printed an error message if there was a problem
2418 inserting this catchpoint, and we've disabled the catchpoint,
2419 so just return success. */
2420 return 0;
879bfdc2
DJ
2421 }
2422
2423 return 0;
2424}
2425
6c95b8df
PA
2426/* This function is called when program space PSPACE is about to be
2427 deleted. It takes care of updating breakpoints to not reference
2428 PSPACE anymore. */
2429
2430void
2431breakpoint_program_space_exit (struct program_space *pspace)
2432{
2433 struct breakpoint *b, *b_temp;
876fa593 2434 struct bp_location *loc, **loc_temp;
6c95b8df
PA
2435
2436 /* Remove any breakpoint that was set through this program space. */
2437 ALL_BREAKPOINTS_SAFE (b, b_temp)
2438 {
2439 if (b->pspace == pspace)
2440 delete_breakpoint (b);
2441 }
2442
2443 /* Breakpoints set through other program spaces could have locations
2444 bound to PSPACE as well. Remove those. */
876fa593 2445 ALL_BP_LOCATIONS (loc, loc_temp)
6c95b8df
PA
2446 {
2447 struct bp_location *tmp;
2448
2449 if (loc->pspace == pspace)
2450 {
2bdf28a0 2451 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6c95b8df
PA
2452 if (loc->owner->loc == loc)
2453 loc->owner->loc = loc->next;
2454 else
2455 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2456 if (tmp->next == loc)
2457 {
2458 tmp->next = loc->next;
2459 break;
2460 }
2461 }
2462 }
2463
2464 /* Now update the global location list to permanently delete the
2465 removed locations above. */
2466 update_global_location_list (0);
2467}
2468
74960c60
VP
2469/* Make sure all breakpoints are inserted in inferior.
2470 Throws exception on any error.
2471 A breakpoint that is already inserted won't be inserted
2472 again, so calling this function twice is safe. */
2473void
2474insert_breakpoints (void)
2475{
2476 struct breakpoint *bpt;
2477
2478 ALL_BREAKPOINTS (bpt)
2479 if (is_hardware_watchpoint (bpt))
3a5c3e22
PA
2480 {
2481 struct watchpoint *w = (struct watchpoint *) bpt;
2482
2483 update_watchpoint (w, 0 /* don't reparse. */);
2484 }
74960c60 2485
b60e7edf 2486 update_global_location_list (1);
74960c60 2487
c35b1492
PA
2488 /* update_global_location_list does not insert breakpoints when
2489 always_inserted_mode is not enabled. Explicitly insert them
2490 now. */
2491 if (!breakpoints_always_inserted_mode ())
74960c60
VP
2492 insert_breakpoint_locations ();
2493}
2494
20388dd6
YQ
2495/* Invoke CALLBACK for each of bp_location. */
2496
2497void
2498iterate_over_bp_locations (walk_bp_location_callback callback)
2499{
2500 struct bp_location *loc, **loc_tmp;
2501
2502 ALL_BP_LOCATIONS (loc, loc_tmp)
2503 {
2504 callback (loc, NULL);
2505 }
2506}
2507
b775012e
LM
2508/* This is used when we need to synch breakpoint conditions between GDB and the
2509 target. It is the case with deleting and disabling of breakpoints when using
2510 always-inserted mode. */
2511
2512static void
2513update_inserted_breakpoint_locations (void)
2514{
2515 struct bp_location *bl, **blp_tmp;
2516 int error_flag = 0;
2517 int val = 0;
2518 int disabled_breaks = 0;
2519 int hw_breakpoint_error = 0;
2520
2521 struct ui_file *tmp_error_stream = mem_fileopen ();
2522 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2523
2524 /* Explicitly mark the warning -- this will only be printed if
2525 there was an error. */
2526 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2527
2528 save_current_space_and_thread ();
2529
2530 ALL_BP_LOCATIONS (bl, blp_tmp)
2531 {
2532 /* We only want to update software breakpoints and hardware
2533 breakpoints. */
2534 if (!is_breakpoint (bl->owner))
2535 continue;
2536
2537 /* We only want to update locations that are already inserted
2538 and need updating. This is to avoid unwanted insertion during
2539 deletion of breakpoints. */
2540 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2541 continue;
2542
2543 switch_to_program_space_and_thread (bl->pspace);
2544
2545 /* For targets that support global breakpoints, there's no need
2546 to select an inferior to insert breakpoint to. In fact, even
2547 if we aren't attached to any process yet, we should still
2548 insert breakpoints. */
2549 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2550 && ptid_equal (inferior_ptid, null_ptid))
2551 continue;
2552
2553 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2554 &hw_breakpoint_error);
2555 if (val)
2556 error_flag = val;
2557 }
2558
2559 if (error_flag)
2560 {
2561 target_terminal_ours_for_output ();
2562 error_stream (tmp_error_stream);
2563 }
2564
2565 do_cleanups (cleanups);
2566}
2567
c30eee59 2568/* Used when starting or continuing the program. */
c906108c 2569
74960c60
VP
2570static void
2571insert_breakpoint_locations (void)
c906108c 2572{
a5606eee 2573 struct breakpoint *bpt;
35df4500 2574 struct bp_location *bl, **blp_tmp;
eacd795a 2575 int error_flag = 0;
c906108c 2576 int val = 0;
3fbb6ffa 2577 int disabled_breaks = 0;
81d0cc19 2578 int hw_breakpoint_error = 0;
c906108c 2579
81d0cc19 2580 struct ui_file *tmp_error_stream = mem_fileopen ();
f7545552 2581 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
74960c60 2582
81d0cc19
GS
2583 /* Explicitly mark the warning -- this will only be printed if
2584 there was an error. */
2585 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
6c95b8df
PA
2586
2587 save_current_space_and_thread ();
2588
35df4500 2589 ALL_BP_LOCATIONS (bl, blp_tmp)
879bfdc2 2590 {
b775012e 2591 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
879bfdc2
DJ
2592 continue;
2593
4a64f543
MS
2594 /* There is no point inserting thread-specific breakpoints if
2595 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2596 has BL->OWNER always non-NULL. */
35df4500
TJB
2597 if (bl->owner->thread != -1
2598 && !valid_thread_id (bl->owner->thread))
f365de73
AS
2599 continue;
2600
35df4500 2601 switch_to_program_space_and_thread (bl->pspace);
6c95b8df
PA
2602
2603 /* For targets that support global breakpoints, there's no need
2604 to select an inferior to insert breakpoint to. In fact, even
2605 if we aren't attached to any process yet, we should still
2606 insert breakpoints. */
2607 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2608 && ptid_equal (inferior_ptid, null_ptid))
2609 continue;
2610
3fbb6ffa
TJB
2611 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2612 &hw_breakpoint_error);
879bfdc2 2613 if (val)
eacd795a 2614 error_flag = val;
879bfdc2 2615 }
c906108c 2616
4a64f543
MS
2617 /* If we failed to insert all locations of a watchpoint, remove
2618 them, as half-inserted watchpoint is of limited use. */
a5606eee
VP
2619 ALL_BREAKPOINTS (bpt)
2620 {
2621 int some_failed = 0;
2622 struct bp_location *loc;
2623
2624 if (!is_hardware_watchpoint (bpt))
2625 continue;
2626
d6b74ac4 2627 if (!breakpoint_enabled (bpt))
a5606eee 2628 continue;
74960c60
VP
2629
2630 if (bpt->disposition == disp_del_at_next_stop)
2631 continue;
a5606eee
VP
2632
2633 for (loc = bpt->loc; loc; loc = loc->next)
56710373 2634 if (!loc->inserted && should_be_inserted (loc))
a5606eee
VP
2635 {
2636 some_failed = 1;
2637 break;
2638 }
2639 if (some_failed)
2640 {
2641 for (loc = bpt->loc; loc; loc = loc->next)
2642 if (loc->inserted)
2643 remove_breakpoint (loc, mark_uninserted);
2644
2645 hw_breakpoint_error = 1;
2646 fprintf_unfiltered (tmp_error_stream,
2647 "Could not insert hardware watchpoint %d.\n",
2648 bpt->number);
eacd795a 2649 error_flag = -1;
a5606eee
VP
2650 }
2651 }
2652
eacd795a 2653 if (error_flag)
81d0cc19
GS
2654 {
2655 /* If a hardware breakpoint or watchpoint was inserted, add a
2656 message about possibly exhausted resources. */
879bfdc2 2657 if (hw_breakpoint_error)
81d0cc19 2658 {
c6510018
MS
2659 fprintf_unfiltered (tmp_error_stream,
2660 "Could not insert hardware breakpoints:\n\
2661You may have requested too many hardware breakpoints/watchpoints.\n");
81d0cc19 2662 }
81d0cc19
GS
2663 target_terminal_ours_for_output ();
2664 error_stream (tmp_error_stream);
2665 }
f7545552
TT
2666
2667 do_cleanups (cleanups);
c906108c
SS
2668}
2669
c30eee59
TJB
2670/* Used when the program stops.
2671 Returns zero if successful, or non-zero if there was a problem
2672 removing a breakpoint location. */
2673
c906108c 2674int
fba45db2 2675remove_breakpoints (void)
c906108c 2676{
35df4500 2677 struct bp_location *bl, **blp_tmp;
3a1bae8e 2678 int val = 0;
c906108c 2679
35df4500 2680 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2681 {
1e4d1764 2682 if (bl->inserted && !is_tracepoint (bl->owner))
35df4500 2683 val |= remove_breakpoint (bl, mark_uninserted);
c5aa993b 2684 }
3a1bae8e 2685 return val;
c906108c
SS
2686}
2687
6c95b8df
PA
2688/* Remove breakpoints of process PID. */
2689
2690int
2691remove_breakpoints_pid (int pid)
2692{
35df4500 2693 struct bp_location *bl, **blp_tmp;
6c95b8df
PA
2694 int val;
2695 struct inferior *inf = find_inferior_pid (pid);
2696
35df4500 2697 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 2698 {
35df4500 2699 if (bl->pspace != inf->pspace)
6c95b8df
PA
2700 continue;
2701
35df4500 2702 if (bl->inserted)
6c95b8df 2703 {
35df4500 2704 val = remove_breakpoint (bl, mark_uninserted);
6c95b8df
PA
2705 if (val != 0)
2706 return val;
2707 }
2708 }
2709 return 0;
2710}
2711
c906108c 2712int
fba45db2 2713reattach_breakpoints (int pid)
c906108c 2714{
6c95b8df 2715 struct cleanup *old_chain;
35df4500 2716 struct bp_location *bl, **blp_tmp;
c906108c 2717 int val;
86b887df 2718 struct ui_file *tmp_error_stream;
3fbb6ffa 2719 int dummy1 = 0, dummy2 = 0;
6c95b8df
PA
2720 struct inferior *inf;
2721 struct thread_info *tp;
2722
2723 tp = any_live_thread_of_process (pid);
2724 if (tp == NULL)
2725 return 1;
2726
2727 inf = find_inferior_pid (pid);
2728 old_chain = save_inferior_ptid ();
2729
2730 inferior_ptid = tp->ptid;
a4954f26 2731
86b887df 2732 tmp_error_stream = mem_fileopen ();
a4954f26 2733 make_cleanup_ui_file_delete (tmp_error_stream);
c906108c 2734
35df4500 2735 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 2736 {
35df4500 2737 if (bl->pspace != inf->pspace)
6c95b8df
PA
2738 continue;
2739
35df4500 2740 if (bl->inserted)
c5aa993b 2741 {
35df4500 2742 bl->inserted = 0;
3fbb6ffa 2743 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
c5aa993b
JM
2744 if (val != 0)
2745 {
ce696e05 2746 do_cleanups (old_chain);
c5aa993b
JM
2747 return val;
2748 }
2749 }
2750 }
ce696e05 2751 do_cleanups (old_chain);
c906108c
SS
2752 return 0;
2753}
2754
e58b0e63
PA
2755static int internal_breakpoint_number = -1;
2756
84f4c1fe
PM
2757/* Set the breakpoint number of B, depending on the value of INTERNAL.
2758 If INTERNAL is non-zero, the breakpoint number will be populated
2759 from internal_breakpoint_number and that variable decremented.
e5dd4106 2760 Otherwise the breakpoint number will be populated from
84f4c1fe
PM
2761 breakpoint_count and that value incremented. Internal breakpoints
2762 do not set the internal var bpnum. */
2763static void
2764set_breakpoint_number (int internal, struct breakpoint *b)
2765{
2766 if (internal)
2767 b->number = internal_breakpoint_number--;
2768 else
2769 {
2770 set_breakpoint_count (breakpoint_count + 1);
2771 b->number = breakpoint_count;
2772 }
2773}
2774
e62c965a 2775static struct breakpoint *
a6d9a66e 2776create_internal_breakpoint (struct gdbarch *gdbarch,
06edf0c0 2777 CORE_ADDR address, enum bptype type,
c0a91b2b 2778 const struct breakpoint_ops *ops)
e62c965a 2779{
e62c965a
PP
2780 struct symtab_and_line sal;
2781 struct breakpoint *b;
2782
4a64f543 2783 init_sal (&sal); /* Initialize to zeroes. */
e62c965a
PP
2784
2785 sal.pc = address;
2786 sal.section = find_pc_overlay (sal.pc);
6c95b8df 2787 sal.pspace = current_program_space;
e62c965a 2788
06edf0c0 2789 b = set_raw_breakpoint (gdbarch, sal, type, ops);
e62c965a
PP
2790 b->number = internal_breakpoint_number--;
2791 b->disposition = disp_donttouch;
2792
2793 return b;
2794}
2795
17450429
PP
2796static const char *const longjmp_names[] =
2797 {
2798 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2799 };
2800#define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2801
2802/* Per-objfile data private to breakpoint.c. */
2803struct breakpoint_objfile_data
2804{
2805 /* Minimal symbol for "_ovly_debug_event" (if any). */
2806 struct minimal_symbol *overlay_msym;
2807
2808 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2809 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2810
28106bc2
SDJ
2811 /* True if we have looked for longjmp probes. */
2812 int longjmp_searched;
2813
2814 /* SystemTap probe points for longjmp (if any). */
2815 VEC (probe_p) *longjmp_probes;
2816
17450429
PP
2817 /* Minimal symbol for "std::terminate()" (if any). */
2818 struct minimal_symbol *terminate_msym;
2819
2820 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2821 struct minimal_symbol *exception_msym;
28106bc2
SDJ
2822
2823 /* True if we have looked for exception probes. */
2824 int exception_searched;
2825
2826 /* SystemTap probe points for unwinding (if any). */
2827 VEC (probe_p) *exception_probes;
17450429
PP
2828};
2829
2830static const struct objfile_data *breakpoint_objfile_key;
2831
2832/* Minimal symbol not found sentinel. */
2833static struct minimal_symbol msym_not_found;
2834
2835/* Returns TRUE if MSYM point to the "not found" sentinel. */
2836
2837static int
2838msym_not_found_p (const struct minimal_symbol *msym)
2839{
2840 return msym == &msym_not_found;
2841}
2842
2843/* Return per-objfile data needed by breakpoint.c.
2844 Allocate the data if necessary. */
2845
2846static struct breakpoint_objfile_data *
2847get_breakpoint_objfile_data (struct objfile *objfile)
2848{
2849 struct breakpoint_objfile_data *bp_objfile_data;
2850
2851 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2852 if (bp_objfile_data == NULL)
2853 {
2854 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2855 sizeof (*bp_objfile_data));
2856
2857 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2858 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2859 }
2860 return bp_objfile_data;
2861}
2862
28106bc2
SDJ
2863static void
2864free_breakpoint_probes (struct objfile *obj, void *data)
2865{
2866 struct breakpoint_objfile_data *bp_objfile_data = data;
2867
2868 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
2869 VEC_free (probe_p, bp_objfile_data->exception_probes);
2870}
2871
e62c965a 2872static void
af02033e 2873create_overlay_event_breakpoint (void)
e62c965a 2874{
69de3c6a 2875 struct objfile *objfile;
af02033e 2876 const char *const func_name = "_ovly_debug_event";
e62c965a 2877
69de3c6a
PP
2878 ALL_OBJFILES (objfile)
2879 {
2880 struct breakpoint *b;
17450429
PP
2881 struct breakpoint_objfile_data *bp_objfile_data;
2882 CORE_ADDR addr;
69de3c6a 2883
17450429
PP
2884 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2885
2886 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2887 continue;
2888
2889 if (bp_objfile_data->overlay_msym == NULL)
2890 {
2891 struct minimal_symbol *m;
2892
2893 m = lookup_minimal_symbol_text (func_name, objfile);
2894 if (m == NULL)
2895 {
2896 /* Avoid future lookups in this objfile. */
2897 bp_objfile_data->overlay_msym = &msym_not_found;
2898 continue;
2899 }
2900 bp_objfile_data->overlay_msym = m;
2901 }
e62c965a 2902
17450429
PP
2903 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2904 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
2905 bp_overlay_event,
2906 &internal_breakpoint_ops);
69de3c6a 2907 b->addr_string = xstrdup (func_name);
e62c965a 2908
69de3c6a
PP
2909 if (overlay_debugging == ovly_auto)
2910 {
2911 b->enable_state = bp_enabled;
2912 overlay_events_enabled = 1;
2913 }
2914 else
2915 {
2916 b->enable_state = bp_disabled;
2917 overlay_events_enabled = 0;
2918 }
e62c965a
PP
2919 }
2920 update_global_location_list (1);
2921}
2922
0fd8e87f 2923static void
af02033e 2924create_longjmp_master_breakpoint (void)
0fd8e87f 2925{
6c95b8df 2926 struct program_space *pspace;
6c95b8df
PA
2927 struct cleanup *old_chain;
2928
2929 old_chain = save_current_program_space ();
0fd8e87f 2930
6c95b8df 2931 ALL_PSPACES (pspace)
af02033e
PP
2932 {
2933 struct objfile *objfile;
2934
2935 set_current_program_space (pspace);
2936
2937 ALL_OBJFILES (objfile)
0fd8e87f 2938 {
af02033e
PP
2939 int i;
2940 struct gdbarch *gdbarch;
17450429 2941 struct breakpoint_objfile_data *bp_objfile_data;
0fd8e87f 2942
af02033e
PP
2943 gdbarch = get_objfile_arch (objfile);
2944 if (!gdbarch_get_longjmp_target_p (gdbarch))
0fd8e87f
UW
2945 continue;
2946
17450429
PP
2947 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2948
28106bc2
SDJ
2949 if (!bp_objfile_data->longjmp_searched)
2950 {
2951 bp_objfile_data->longjmp_probes
2952 = find_probes_in_objfile (objfile, "libc", "longjmp");
2953 bp_objfile_data->longjmp_searched = 1;
2954 }
2955
2956 if (bp_objfile_data->longjmp_probes != NULL)
2957 {
2958 int i;
2959 struct probe *probe;
2960 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2961
2962 for (i = 0;
2963 VEC_iterate (probe_p,
2964 bp_objfile_data->longjmp_probes,
2965 i, probe);
2966 ++i)
2967 {
2968 struct breakpoint *b;
2969
2970 b = create_internal_breakpoint (gdbarch, probe->address,
2971 bp_longjmp_master,
2972 &internal_breakpoint_ops);
2973 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
2974 b->enable_state = bp_disabled;
2975 }
2976
2977 continue;
2978 }
2979
17450429 2980 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
af02033e
PP
2981 {
2982 struct breakpoint *b;
af02033e 2983 const char *func_name;
17450429 2984 CORE_ADDR addr;
6c95b8df 2985
17450429 2986 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
af02033e 2987 continue;
0fd8e87f 2988
17450429
PP
2989 func_name = longjmp_names[i];
2990 if (bp_objfile_data->longjmp_msym[i] == NULL)
2991 {
2992 struct minimal_symbol *m;
2993
2994 m = lookup_minimal_symbol_text (func_name, objfile);
2995 if (m == NULL)
2996 {
2997 /* Prevent future lookups in this objfile. */
2998 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2999 continue;
3000 }
3001 bp_objfile_data->longjmp_msym[i] = m;
3002 }
3003
3004 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
06edf0c0
PA
3005 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3006 &internal_breakpoint_ops);
af02033e
PP
3007 b->addr_string = xstrdup (func_name);
3008 b->enable_state = bp_disabled;
3009 }
0fd8e87f 3010 }
af02033e 3011 }
0fd8e87f 3012 update_global_location_list (1);
6c95b8df
PA
3013
3014 do_cleanups (old_chain);
0fd8e87f
UW
3015}
3016
af02033e 3017/* Create a master std::terminate breakpoint. */
aa7d318d 3018static void
af02033e 3019create_std_terminate_master_breakpoint (void)
aa7d318d
TT
3020{
3021 struct program_space *pspace;
aa7d318d 3022 struct cleanup *old_chain;
af02033e 3023 const char *const func_name = "std::terminate()";
aa7d318d
TT
3024
3025 old_chain = save_current_program_space ();
3026
3027 ALL_PSPACES (pspace)
17450429
PP
3028 {
3029 struct objfile *objfile;
3030 CORE_ADDR addr;
3031
3032 set_current_program_space (pspace);
3033
aa7d318d
TT
3034 ALL_OBJFILES (objfile)
3035 {
3036 struct breakpoint *b;
17450429 3037 struct breakpoint_objfile_data *bp_objfile_data;
aa7d318d 3038
17450429 3039 bp_objfile_data = get_breakpoint_objfile_data (objfile);
aa7d318d 3040
17450429
PP
3041 if (msym_not_found_p (bp_objfile_data->terminate_msym))
3042 continue;
3043
3044 if (bp_objfile_data->terminate_msym == NULL)
3045 {
3046 struct minimal_symbol *m;
3047
3048 m = lookup_minimal_symbol (func_name, NULL, objfile);
3049 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
3050 && MSYMBOL_TYPE (m) != mst_file_text))
3051 {
3052 /* Prevent future lookups in this objfile. */
3053 bp_objfile_data->terminate_msym = &msym_not_found;
3054 continue;
3055 }
3056 bp_objfile_data->terminate_msym = m;
3057 }
aa7d318d 3058
17450429
PP
3059 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3060 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
06edf0c0
PA
3061 bp_std_terminate_master,
3062 &internal_breakpoint_ops);
aa7d318d
TT
3063 b->addr_string = xstrdup (func_name);
3064 b->enable_state = bp_disabled;
3065 }
17450429
PP
3066 }
3067
aa7d318d
TT
3068 update_global_location_list (1);
3069
3070 do_cleanups (old_chain);
3071}
3072
186c406b
TT
3073/* Install a master breakpoint on the unwinder's debug hook. */
3074
70221824 3075static void
186c406b
TT
3076create_exception_master_breakpoint (void)
3077{
3078 struct objfile *objfile;
17450429 3079 const char *const func_name = "_Unwind_DebugHook";
186c406b
TT
3080
3081 ALL_OBJFILES (objfile)
3082 {
17450429
PP
3083 struct breakpoint *b;
3084 struct gdbarch *gdbarch;
3085 struct breakpoint_objfile_data *bp_objfile_data;
3086 CORE_ADDR addr;
3087
3088 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3089
28106bc2
SDJ
3090 /* We prefer the SystemTap probe point if it exists. */
3091 if (!bp_objfile_data->exception_searched)
3092 {
3093 bp_objfile_data->exception_probes
3094 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3095 bp_objfile_data->exception_searched = 1;
3096 }
3097
3098 if (bp_objfile_data->exception_probes != NULL)
3099 {
3100 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3101 int i;
3102 struct probe *probe;
3103
3104 for (i = 0;
3105 VEC_iterate (probe_p,
3106 bp_objfile_data->exception_probes,
3107 i, probe);
3108 ++i)
3109 {
3110 struct breakpoint *b;
3111
3112 b = create_internal_breakpoint (gdbarch, probe->address,
3113 bp_exception_master,
3114 &internal_breakpoint_ops);
3115 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3116 b->enable_state = bp_disabled;
3117 }
3118
3119 continue;
3120 }
3121
3122 /* Otherwise, try the hook function. */
3123
17450429
PP
3124 if (msym_not_found_p (bp_objfile_data->exception_msym))
3125 continue;
3126
3127 gdbarch = get_objfile_arch (objfile);
186c406b 3128
17450429 3129 if (bp_objfile_data->exception_msym == NULL)
186c406b 3130 {
17450429 3131 struct minimal_symbol *debug_hook;
186c406b 3132
17450429
PP
3133 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3134 if (debug_hook == NULL)
3135 {
3136 bp_objfile_data->exception_msym = &msym_not_found;
3137 continue;
3138 }
3139
3140 bp_objfile_data->exception_msym = debug_hook;
186c406b 3141 }
17450429
PP
3142
3143 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3144 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3145 &current_target);
06edf0c0
PA
3146 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3147 &internal_breakpoint_ops);
17450429
PP
3148 b->addr_string = xstrdup (func_name);
3149 b->enable_state = bp_disabled;
186c406b
TT
3150 }
3151
3152 update_global_location_list (1);
3153}
3154
c906108c 3155void
fba45db2 3156update_breakpoints_after_exec (void)
c906108c 3157{
35df4500 3158 struct breakpoint *b, *b_tmp;
876fa593 3159 struct bp_location *bploc, **bplocp_tmp;
c906108c 3160
25b22b0a
PA
3161 /* We're about to delete breakpoints from GDB's lists. If the
3162 INSERTED flag is true, GDB will try to lift the breakpoints by
3163 writing the breakpoints' "shadow contents" back into memory. The
3164 "shadow contents" are NOT valid after an exec, so GDB should not
3165 do that. Instead, the target is responsible from marking
3166 breakpoints out as soon as it detects an exec. We don't do that
3167 here instead, because there may be other attempts to delete
3168 breakpoints after detecting an exec and before reaching here. */
876fa593 3169 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
6c95b8df
PA
3170 if (bploc->pspace == current_program_space)
3171 gdb_assert (!bploc->inserted);
c906108c 3172
35df4500 3173 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3174 {
6c95b8df
PA
3175 if (b->pspace != current_program_space)
3176 continue;
3177
4a64f543 3178 /* Solib breakpoints must be explicitly reset after an exec(). */
c5aa993b
JM
3179 if (b->type == bp_shlib_event)
3180 {
3181 delete_breakpoint (b);
3182 continue;
3183 }
c906108c 3184
4a64f543 3185 /* JIT breakpoints must be explicitly reset after an exec(). */
4efc6507
DE
3186 if (b->type == bp_jit_event)
3187 {
3188 delete_breakpoint (b);
3189 continue;
3190 }
3191
1900040c 3192 /* Thread event breakpoints must be set anew after an exec(),
0fd8e87f
UW
3193 as must overlay event and longjmp master breakpoints. */
3194 if (b->type == bp_thread_event || b->type == bp_overlay_event
186c406b
TT
3195 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3196 || b->type == bp_exception_master)
c4093a6a
JM
3197 {
3198 delete_breakpoint (b);
3199 continue;
3200 }
3201
4a64f543 3202 /* Step-resume breakpoints are meaningless after an exec(). */
2c03e5be 3203 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
c5aa993b
JM
3204 {
3205 delete_breakpoint (b);
3206 continue;
3207 }
3208
611c83ae
PA
3209 /* Longjmp and longjmp-resume breakpoints are also meaningless
3210 after an exec. */
186c406b 3211 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
e2e4d78b 3212 || b->type == bp_longjmp_call_dummy
186c406b 3213 || b->type == bp_exception || b->type == bp_exception_resume)
611c83ae
PA
3214 {
3215 delete_breakpoint (b);
3216 continue;
3217 }
3218
ce78b96d
JB
3219 if (b->type == bp_catchpoint)
3220 {
3221 /* For now, none of the bp_catchpoint breakpoints need to
3222 do anything at this point. In the future, if some of
3223 the catchpoints need to something, we will need to add
3224 a new method, and call this method from here. */
3225 continue;
3226 }
3227
c5aa993b
JM
3228 /* bp_finish is a special case. The only way we ought to be able
3229 to see one of these when an exec() has happened, is if the user
3230 caught a vfork, and then said "finish". Ordinarily a finish just
3231 carries them to the call-site of the current callee, by setting
3232 a temporary bp there and resuming. But in this case, the finish
3233 will carry them entirely through the vfork & exec.
3234
3235 We don't want to allow a bp_finish to remain inserted now. But
3236 we can't safely delete it, 'cause finish_command has a handle to
3237 the bp on a bpstat, and will later want to delete it. There's a
3238 chance (and I've seen it happen) that if we delete the bp_finish
3239 here, that its storage will get reused by the time finish_command
3240 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3241 We really must allow finish_command to delete a bp_finish.
3242
e5dd4106 3243 In the absence of a general solution for the "how do we know
53a5351d
JM
3244 it's safe to delete something others may have handles to?"
3245 problem, what we'll do here is just uninsert the bp_finish, and
3246 let finish_command delete it.
3247
3248 (We know the bp_finish is "doomed" in the sense that it's
3249 momentary, and will be deleted as soon as finish_command sees
3250 the inferior stopped. So it doesn't matter that the bp's
3251 address is probably bogus in the new a.out, unlike e.g., the
3252 solib breakpoints.) */
c5aa993b 3253
c5aa993b
JM
3254 if (b->type == bp_finish)
3255 {
3256 continue;
3257 }
3258
3259 /* Without a symbolic address, we have little hope of the
3260 pre-exec() address meaning the same thing in the post-exec()
4a64f543 3261 a.out. */
c5aa993b
JM
3262 if (b->addr_string == NULL)
3263 {
3264 delete_breakpoint (b);
3265 continue;
3266 }
c5aa993b 3267 }
1900040c 3268 /* FIXME what about longjmp breakpoints? Re-create them here? */
af02033e
PP
3269 create_overlay_event_breakpoint ();
3270 create_longjmp_master_breakpoint ();
3271 create_std_terminate_master_breakpoint ();
186c406b 3272 create_exception_master_breakpoint ();
c906108c
SS
3273}
3274
3275int
fba45db2 3276detach_breakpoints (int pid)
c906108c 3277{
35df4500 3278 struct bp_location *bl, **blp_tmp;
3a1bae8e 3279 int val = 0;
ce696e05 3280 struct cleanup *old_chain = save_inferior_ptid ();
6c95b8df 3281 struct inferior *inf = current_inferior ();
c5aa993b 3282
39f77062 3283 if (pid == PIDGET (inferior_ptid))
8a3fe4f8 3284 error (_("Cannot detach breakpoints of inferior_ptid"));
c5aa993b 3285
6c95b8df 3286 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
ce696e05 3287 inferior_ptid = pid_to_ptid (pid);
35df4500 3288 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3289 {
35df4500 3290 if (bl->pspace != inf->pspace)
6c95b8df
PA
3291 continue;
3292
35df4500
TJB
3293 if (bl->inserted)
3294 val |= remove_breakpoint_1 (bl, mark_inserted);
c5aa993b 3295 }
d03285ec
UW
3296
3297 /* Detach single-step breakpoints as well. */
3298 detach_single_step_breakpoints ();
3299
ce696e05 3300 do_cleanups (old_chain);
3a1bae8e 3301 return val;
c906108c
SS
3302}
3303
35df4500 3304/* Remove the breakpoint location BL from the current address space.
6c95b8df
PA
3305 Note that this is used to detach breakpoints from a child fork.
3306 When we get here, the child isn't in the inferior list, and neither
3307 do we have objects to represent its address space --- we should
35df4500 3308 *not* look at bl->pspace->aspace here. */
6c95b8df 3309
c906108c 3310static int
35df4500 3311remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
c906108c
SS
3312{
3313 int val;
c5aa993b 3314
35df4500
TJB
3315 /* BL is never in moribund_locations by our callers. */
3316 gdb_assert (bl->owner != NULL);
2bdf28a0 3317
35df4500 3318 if (bl->owner->enable_state == bp_permanent)
c2c6d25f
JM
3319 /* Permanent breakpoints cannot be inserted or removed. */
3320 return 0;
3321
74960c60
VP
3322 /* The type of none suggests that owner is actually deleted.
3323 This should not ever happen. */
35df4500 3324 gdb_assert (bl->owner->type != bp_none);
0bde7532 3325
35df4500
TJB
3326 if (bl->loc_type == bp_loc_software_breakpoint
3327 || bl->loc_type == bp_loc_hardware_breakpoint)
c906108c 3328 {
c02f5703
MS
3329 /* "Normal" instruction breakpoint: either the standard
3330 trap-instruction bp (bp_breakpoint), or a
3331 bp_hardware_breakpoint. */
3332
3333 /* First check to see if we have to handle an overlay. */
3334 if (overlay_debugging == ovly_off
35df4500
TJB
3335 || bl->section == NULL
3336 || !(section_is_overlay (bl->section)))
c02f5703
MS
3337 {
3338 /* No overlay handling: just remove the breakpoint. */
348d480f 3339 val = bl->owner->ops->remove_location (bl);
c02f5703 3340 }
c906108c
SS
3341 else
3342 {
4a64f543 3343 /* This breakpoint is in an overlay section.
c02f5703
MS
3344 Did we set a breakpoint at the LMA? */
3345 if (!overlay_events_enabled)
3346 {
3347 /* Yes -- overlay event support is not active, so we
3348 should have set a breakpoint at the LMA. Remove it.
3349 */
c02f5703
MS
3350 /* Ignore any failures: if the LMA is in ROM, we will
3351 have already warned when we failed to insert it. */
35df4500
TJB
3352 if (bl->loc_type == bp_loc_hardware_breakpoint)
3353 target_remove_hw_breakpoint (bl->gdbarch,
3354 &bl->overlay_target_info);
c02f5703 3355 else
35df4500
TJB
3356 target_remove_breakpoint (bl->gdbarch,
3357 &bl->overlay_target_info);
c02f5703
MS
3358 }
3359 /* Did we set a breakpoint at the VMA?
3360 If so, we will have marked the breakpoint 'inserted'. */
35df4500 3361 if (bl->inserted)
c906108c 3362 {
c02f5703
MS
3363 /* Yes -- remove it. Previously we did not bother to
3364 remove the breakpoint if the section had been
3365 unmapped, but let's not rely on that being safe. We
3366 don't know what the overlay manager might do. */
aa67235e
UW
3367
3368 /* However, we should remove *software* breakpoints only
3369 if the section is still mapped, or else we overwrite
3370 wrong code with the saved shadow contents. */
348d480f
PA
3371 if (bl->loc_type == bp_loc_hardware_breakpoint
3372 || section_is_mapped (bl->section))
3373 val = bl->owner->ops->remove_location (bl);
aa67235e
UW
3374 else
3375 val = 0;
c906108c 3376 }
c02f5703
MS
3377 else
3378 {
3379 /* No -- not inserted, so no need to remove. No error. */
3380 val = 0;
3381 }
c906108c 3382 }
879d1e6b
UW
3383
3384 /* In some cases, we might not be able to remove a breakpoint
3385 in a shared library that has already been removed, but we
3386 have not yet processed the shlib unload event. */
35df4500 3387 if (val && solib_name_from_address (bl->pspace, bl->address))
879d1e6b
UW
3388 val = 0;
3389
c906108c
SS
3390 if (val)
3391 return val;
35df4500 3392 bl->inserted = (is == mark_inserted);
c906108c 3393 }
35df4500 3394 else if (bl->loc_type == bp_loc_hardware_watchpoint)
c906108c 3395 {
77b06cd7
TJB
3396 gdb_assert (bl->owner->ops != NULL
3397 && bl->owner->ops->remove_location != NULL);
3398
35df4500 3399 bl->inserted = (is == mark_inserted);
77b06cd7 3400 bl->owner->ops->remove_location (bl);
2e70b7b9 3401
c906108c 3402 /* Failure to remove any of the hardware watchpoints comes here. */
35df4500 3403 if ((is == mark_uninserted) && (bl->inserted))
8a3fe4f8 3404 warning (_("Could not remove hardware watchpoint %d."),
35df4500 3405 bl->owner->number);
c906108c 3406 }
35df4500
TJB
3407 else if (bl->owner->type == bp_catchpoint
3408 && breakpoint_enabled (bl->owner)
3409 && !bl->duplicate)
ce78b96d 3410 {
77b06cd7
TJB
3411 gdb_assert (bl->owner->ops != NULL
3412 && bl->owner->ops->remove_location != NULL);
ce78b96d 3413
77b06cd7 3414 val = bl->owner->ops->remove_location (bl);
ce78b96d
JB
3415 if (val)
3416 return val;
77b06cd7 3417
35df4500 3418 bl->inserted = (is == mark_inserted);
ce78b96d 3419 }
c906108c
SS
3420
3421 return 0;
3422}
3423
6c95b8df 3424static int
35df4500 3425remove_breakpoint (struct bp_location *bl, insertion_state_t is)
6c95b8df
PA
3426{
3427 int ret;
3428 struct cleanup *old_chain;
3429
35df4500
TJB
3430 /* BL is never in moribund_locations by our callers. */
3431 gdb_assert (bl->owner != NULL);
2bdf28a0 3432
35df4500 3433 if (bl->owner->enable_state == bp_permanent)
6c95b8df
PA
3434 /* Permanent breakpoints cannot be inserted or removed. */
3435 return 0;
3436
3437 /* The type of none suggests that owner is actually deleted.
3438 This should not ever happen. */
35df4500 3439 gdb_assert (bl->owner->type != bp_none);
6c95b8df
PA
3440
3441 old_chain = save_current_space_and_thread ();
3442
35df4500 3443 switch_to_program_space_and_thread (bl->pspace);
6c95b8df 3444
35df4500 3445 ret = remove_breakpoint_1 (bl, is);
6c95b8df
PA
3446
3447 do_cleanups (old_chain);
3448 return ret;
3449}
3450
c906108c
SS
3451/* Clear the "inserted" flag in all breakpoints. */
3452
25b22b0a 3453void
fba45db2 3454mark_breakpoints_out (void)
c906108c 3455{
35df4500 3456 struct bp_location *bl, **blp_tmp;
c906108c 3457
35df4500
TJB
3458 ALL_BP_LOCATIONS (bl, blp_tmp)
3459 if (bl->pspace == current_program_space)
3460 bl->inserted = 0;
c906108c
SS
3461}
3462
53a5351d
JM
3463/* Clear the "inserted" flag in all breakpoints and delete any
3464 breakpoints which should go away between runs of the program.
c906108c
SS
3465
3466 Plus other such housekeeping that has to be done for breakpoints
3467 between runs.
3468
53a5351d
JM
3469 Note: this function gets called at the end of a run (by
3470 generic_mourn_inferior) and when a run begins (by
4a64f543 3471 init_wait_for_inferior). */
c906108c
SS
3472
3473
3474
3475void
fba45db2 3476breakpoint_init_inferior (enum inf_context context)
c906108c 3477{
35df4500
TJB
3478 struct breakpoint *b, *b_tmp;
3479 struct bp_location *bl, **blp_tmp;
1c5cfe86 3480 int ix;
6c95b8df 3481 struct program_space *pspace = current_program_space;
c906108c 3482
50c71eaf
PA
3483 /* If breakpoint locations are shared across processes, then there's
3484 nothing to do. */
2567c7d9 3485 if (gdbarch_has_global_breakpoints (target_gdbarch))
50c71eaf
PA
3486 return;
3487
35df4500 3488 ALL_BP_LOCATIONS (bl, blp_tmp)
6c95b8df 3489 {
35df4500
TJB
3490 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3491 if (bl->pspace == pspace
3492 && bl->owner->enable_state != bp_permanent)
3493 bl->inserted = 0;
6c95b8df 3494 }
075f6582 3495
35df4500 3496 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 3497 {
6c95b8df
PA
3498 if (b->loc && b->loc->pspace != pspace)
3499 continue;
3500
c5aa993b
JM
3501 switch (b->type)
3502 {
3503 case bp_call_dummy:
e2e4d78b 3504 case bp_longjmp_call_dummy:
c906108c 3505
c5aa993b 3506 /* If the call dummy breakpoint is at the entry point it will
ab92d69b
PA
3507 cause problems when the inferior is rerun, so we better get
3508 rid of it. */
3509
3510 case bp_watchpoint_scope:
3511
3512 /* Also get rid of scope breakpoints. */
3513
3514 case bp_shlib_event:
3515
3516 /* Also remove solib event breakpoints. Their addresses may
3517 have changed since the last time we ran the program.
3518 Actually we may now be debugging against different target;
3519 and so the solib backend that installed this breakpoint may
3520 not be used in by the target. E.g.,
3521
3522 (gdb) file prog-linux
3523 (gdb) run # native linux target
3524 ...
3525 (gdb) kill
3526 (gdb) file prog-win.exe
3527 (gdb) tar rem :9999 # remote Windows gdbserver.
3528 */
c906108c 3529
f59f708a
PA
3530 case bp_step_resume:
3531
3532 /* Also remove step-resume breakpoints. */
3533
c5aa993b
JM
3534 delete_breakpoint (b);
3535 break;
c906108c 3536
c5aa993b
JM
3537 case bp_watchpoint:
3538 case bp_hardware_watchpoint:
3539 case bp_read_watchpoint:
3540 case bp_access_watchpoint:
3a5c3e22
PA
3541 {
3542 struct watchpoint *w = (struct watchpoint *) b;
c906108c 3543
3a5c3e22
PA
3544 /* Likewise for watchpoints on local expressions. */
3545 if (w->exp_valid_block != NULL)
3546 delete_breakpoint (b);
3547 else if (context == inf_starting)
3548 {
3549 /* Reset val field to force reread of starting value in
3550 insert_breakpoints. */
3551 if (w->val)
3552 value_free (w->val);
3553 w->val = NULL;
3554 w->val_valid = 0;
c860120c 3555 }
3a5c3e22 3556 }
c5aa993b
JM
3557 break;
3558 default:
c5aa993b
JM
3559 break;
3560 }
3561 }
1c5cfe86
PA
3562
3563 /* Get rid of the moribund locations. */
35df4500
TJB
3564 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3565 decref_bp_location (&bl);
1c5cfe86 3566 VEC_free (bp_location_p, moribund_locations);
c906108c
SS
3567}
3568
6c95b8df
PA
3569/* These functions concern about actual breakpoints inserted in the
3570 target --- to e.g. check if we need to do decr_pc adjustment or if
3571 we need to hop over the bkpt --- so we check for address space
3572 match, not program space. */
3573
c2c6d25f
JM
3574/* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3575 exists at PC. It returns ordinary_breakpoint_here if it's an
3576 ordinary breakpoint, or permanent_breakpoint_here if it's a
3577 permanent breakpoint.
3578 - When continuing from a location with an ordinary breakpoint, we
3579 actually single step once before calling insert_breakpoints.
e5dd4106 3580 - When continuing from a location with a permanent breakpoint, we
c2c6d25f
JM
3581 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3582 the target, to advance the PC past the breakpoint. */
c906108c 3583
c2c6d25f 3584enum breakpoint_here
6c95b8df 3585breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
c906108c 3586{
35df4500 3587 struct bp_location *bl, **blp_tmp;
c2c6d25f 3588 int any_breakpoint_here = 0;
c906108c 3589
35df4500 3590 ALL_BP_LOCATIONS (bl, blp_tmp)
075f6582 3591 {
35df4500
TJB
3592 if (bl->loc_type != bp_loc_software_breakpoint
3593 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3594 continue;
3595
f1310107 3596 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
35df4500
TJB
3597 if ((breakpoint_enabled (bl->owner)
3598 || bl->owner->enable_state == bp_permanent)
f1310107 3599 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3600 {
3601 if (overlay_debugging
35df4500
TJB
3602 && section_is_overlay (bl->section)
3603 && !section_is_mapped (bl->section))
075f6582 3604 continue; /* unmapped overlay -- can't be a match */
35df4500 3605 else if (bl->owner->enable_state == bp_permanent)
075f6582
DJ
3606 return permanent_breakpoint_here;
3607 else
3608 any_breakpoint_here = 1;
3609 }
3610 }
c906108c 3611
c2c6d25f 3612 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
c906108c
SS
3613}
3614
1c5cfe86
PA
3615/* Return true if there's a moribund breakpoint at PC. */
3616
3617int
6c95b8df 3618moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
1c5cfe86
PA
3619{
3620 struct bp_location *loc;
3621 int ix;
3622
3623 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
f1310107 3624 if (breakpoint_location_address_match (loc, aspace, pc))
1c5cfe86
PA
3625 return 1;
3626
3627 return 0;
3628}
c2c6d25f 3629
c36b740a 3630/* Returns non-zero if there's a breakpoint inserted at PC, which is
4a64f543
MS
3631 inserted using regular breakpoint_chain / bp_location array
3632 mechanism. This does not check for single-step breakpoints, which
3633 are inserted and removed using direct target manipulation. */
c906108c
SS
3634
3635int
4a64f543
MS
3636regular_breakpoint_inserted_here_p (struct address_space *aspace,
3637 CORE_ADDR pc)
c906108c 3638{
35df4500 3639 struct bp_location *bl, **blp_tmp;
c906108c 3640
35df4500 3641 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3642 {
35df4500
TJB
3643 if (bl->loc_type != bp_loc_software_breakpoint
3644 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3645 continue;
3646
35df4500 3647 if (bl->inserted
f1310107 3648 && breakpoint_location_address_match (bl, aspace, pc))
075f6582
DJ
3649 {
3650 if (overlay_debugging
35df4500
TJB
3651 && section_is_overlay (bl->section)
3652 && !section_is_mapped (bl->section))
075f6582
DJ
3653 continue; /* unmapped overlay -- can't be a match */
3654 else
3655 return 1;
3656 }
c5aa993b 3657 }
c36b740a
VP
3658 return 0;
3659}
3660
3661/* Returns non-zero iff there's either regular breakpoint
3662 or a single step breakpoint inserted at PC. */
3663
3664int
6c95b8df 3665breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
c36b740a 3666{
6c95b8df 3667 if (regular_breakpoint_inserted_here_p (aspace, pc))
c36b740a 3668 return 1;
c906108c 3669
6c95b8df 3670 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3671 return 1;
3672
c906108c
SS
3673 return 0;
3674}
3675
4fa8626c
DJ
3676/* This function returns non-zero iff there is a software breakpoint
3677 inserted at PC. */
3678
3679int
3e43a32a
MS
3680software_breakpoint_inserted_here_p (struct address_space *aspace,
3681 CORE_ADDR pc)
4fa8626c 3682{
35df4500 3683 struct bp_location *bl, **blp_tmp;
4fa8626c 3684
35df4500 3685 ALL_BP_LOCATIONS (bl, blp_tmp)
4fa8626c 3686 {
35df4500 3687 if (bl->loc_type != bp_loc_software_breakpoint)
4fa8626c
DJ
3688 continue;
3689
35df4500
TJB
3690 if (bl->inserted
3691 && breakpoint_address_match (bl->pspace->aspace, bl->address,
6c95b8df 3692 aspace, pc))
4fa8626c
DJ
3693 {
3694 if (overlay_debugging
35df4500
TJB
3695 && section_is_overlay (bl->section)
3696 && !section_is_mapped (bl->section))
4fa8626c
DJ
3697 continue; /* unmapped overlay -- can't be a match */
3698 else
3699 return 1;
3700 }
3701 }
3702
1aafd4da 3703 /* Also check for software single-step breakpoints. */
6c95b8df 3704 if (single_step_breakpoint_inserted_here_p (aspace, pc))
1aafd4da
UW
3705 return 1;
3706
4fa8626c
DJ
3707 return 0;
3708}
3709
9093389c
PA
3710int
3711hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3712 CORE_ADDR addr, ULONGEST len)
3713{
3714 struct breakpoint *bpt;
3715
3716 ALL_BREAKPOINTS (bpt)
3717 {
3718 struct bp_location *loc;
3719
3720 if (bpt->type != bp_hardware_watchpoint
3721 && bpt->type != bp_access_watchpoint)
3722 continue;
3723
3724 if (!breakpoint_enabled (bpt))
3725 continue;
3726
3727 for (loc = bpt->loc; loc; loc = loc->next)
3728 if (loc->pspace->aspace == aspace && loc->inserted)
3729 {
3730 CORE_ADDR l, h;
3731
3732 /* Check for intersection. */
3733 l = max (loc->address, addr);
3734 h = min (loc->address + loc->length, addr + len);
3735 if (l < h)
3736 return 1;
3737 }
3738 }
3739 return 0;
3740}
3741
075f6582
DJ
3742/* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3743 PC is valid for process/thread PTID. */
c906108c
SS
3744
3745int
6c95b8df
PA
3746breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3747 ptid_t ptid)
c906108c 3748{
35df4500 3749 struct bp_location *bl, **blp_tmp;
4a306c9a 3750 /* The thread and task IDs associated to PTID, computed lazily. */
a6f1cd96 3751 int thread = -1;
4a306c9a 3752 int task = 0;
a6f1cd96 3753
35df4500 3754 ALL_BP_LOCATIONS (bl, blp_tmp)
c5aa993b 3755 {
35df4500
TJB
3756 if (bl->loc_type != bp_loc_software_breakpoint
3757 && bl->loc_type != bp_loc_hardware_breakpoint)
075f6582
DJ
3758 continue;
3759
35df4500
TJB
3760 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3761 if (!breakpoint_enabled (bl->owner)
3762 && bl->owner->enable_state != bp_permanent)
a6f1cd96
JB
3763 continue;
3764
f1310107 3765 if (!breakpoint_location_address_match (bl, aspace, pc))
a6f1cd96
JB
3766 continue;
3767
35df4500 3768 if (bl->owner->thread != -1)
075f6582 3769 {
a6f1cd96
JB
3770 /* This is a thread-specific breakpoint. Check that ptid
3771 matches that thread. If thread hasn't been computed yet,
3772 it is now time to do so. */
3773 if (thread == -1)
3774 thread = pid_to_thread_id (ptid);
35df4500 3775 if (bl->owner->thread != thread)
a6f1cd96 3776 continue;
075f6582 3777 }
a6f1cd96 3778
35df4500 3779 if (bl->owner->task != 0)
4a306c9a
JB
3780 {
3781 /* This is a task-specific breakpoint. Check that ptid
3782 matches that task. If task hasn't been computed yet,
3783 it is now time to do so. */
3784 if (task == 0)
3785 task = ada_get_task_number (ptid);
35df4500 3786 if (bl->owner->task != task)
4a306c9a
JB
3787 continue;
3788 }
3789
a6f1cd96 3790 if (overlay_debugging
35df4500
TJB
3791 && section_is_overlay (bl->section)
3792 && !section_is_mapped (bl->section))
a6f1cd96
JB
3793 continue; /* unmapped overlay -- can't be a match */
3794
3795 return 1;
c5aa993b 3796 }
c906108c
SS
3797
3798 return 0;
3799}
c906108c 3800\f
c5aa993b 3801
c906108c
SS
3802/* bpstat stuff. External routines' interfaces are documented
3803 in breakpoint.h. */
3804
3805int
c326b90e 3806is_catchpoint (struct breakpoint *ep)
c906108c 3807{
533be4dd 3808 return (ep->type == bp_catchpoint);
c906108c
SS
3809}
3810
f431efe5
PA
3811/* Frees any storage that is part of a bpstat. Does not walk the
3812 'next' chain. */
3813
3814static void
198757a8
VP
3815bpstat_free (bpstat bs)
3816{
3817 if (bs->old_val != NULL)
3818 value_free (bs->old_val);
9add0f1b 3819 decref_counted_command_line (&bs->commands);
f431efe5 3820 decref_bp_location (&bs->bp_location_at);
198757a8
VP
3821 xfree (bs);
3822}
3823
c906108c
SS
3824/* Clear a bpstat so that it says we are not at any breakpoint.
3825 Also free any storage that is part of a bpstat. */
3826
3827void
fba45db2 3828bpstat_clear (bpstat *bsp)
c906108c
SS
3829{
3830 bpstat p;
3831 bpstat q;
3832
3833 if (bsp == 0)
3834 return;
3835 p = *bsp;
3836 while (p != NULL)
3837 {
3838 q = p->next;
198757a8 3839 bpstat_free (p);
c906108c
SS
3840 p = q;
3841 }
3842 *bsp = NULL;
3843}
3844
3845/* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3846 is part of the bpstat is copied as well. */
3847
3848bpstat
fba45db2 3849bpstat_copy (bpstat bs)
c906108c
SS
3850{
3851 bpstat p = NULL;
3852 bpstat tmp;
3853 bpstat retval = NULL;
3854
3855 if (bs == NULL)
3856 return bs;
3857
3858 for (; bs != NULL; bs = bs->next)
3859 {
3860 tmp = (bpstat) xmalloc (sizeof (*tmp));
3861 memcpy (tmp, bs, sizeof (*tmp));
9add0f1b 3862 incref_counted_command_line (tmp->commands);
f431efe5 3863 incref_bp_location (tmp->bp_location_at);
31cc81e9 3864 if (bs->old_val != NULL)
3c3185ac
JK
3865 {
3866 tmp->old_val = value_copy (bs->old_val);
3867 release_value (tmp->old_val);
3868 }
31cc81e9 3869
c906108c
SS
3870 if (p == NULL)
3871 /* This is the first thing in the chain. */
3872 retval = tmp;
3873 else
3874 p->next = tmp;
3875 p = tmp;
3876 }
3877 p->next = NULL;
3878 return retval;
3879}
3880
4a64f543 3881/* Find the bpstat associated with this breakpoint. */
c906108c
SS
3882
3883bpstat
fba45db2 3884bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
c906108c 3885{
c5aa993b
JM
3886 if (bsp == NULL)
3887 return NULL;
c906108c 3888
c5aa993b
JM
3889 for (; bsp != NULL; bsp = bsp->next)
3890 {
f431efe5 3891 if (bsp->breakpoint_at == breakpoint)
c5aa993b
JM
3892 return bsp;
3893 }
c906108c
SS
3894 return NULL;
3895}
3896
4a64f543
MS
3897/* Put in *NUM the breakpoint number of the first breakpoint we are
3898 stopped at. *BSP upon return is a bpstat which points to the
3899 remaining breakpoints stopped at (but which is not guaranteed to be
3900 good for anything but further calls to bpstat_num).
3901
8671a17b
PA
3902 Return 0 if passed a bpstat which does not indicate any breakpoints.
3903 Return -1 if stopped at a breakpoint that has been deleted since
3904 we set it.
3905 Return 1 otherwise. */
c906108c
SS
3906
3907int
8671a17b 3908bpstat_num (bpstat *bsp, int *num)
c906108c
SS
3909{
3910 struct breakpoint *b;
3911
3912 if ((*bsp) == NULL)
3913 return 0; /* No more breakpoint values */
8671a17b 3914
4a64f543
MS
3915 /* We assume we'll never have several bpstats that correspond to a
3916 single breakpoint -- otherwise, this function might return the
3917 same number more than once and this will look ugly. */
f431efe5 3918 b = (*bsp)->breakpoint_at;
8671a17b
PA
3919 *bsp = (*bsp)->next;
3920 if (b == NULL)
3921 return -1; /* breakpoint that's been deleted since */
3922
3923 *num = b->number; /* We have its number */
3924 return 1;
c906108c
SS
3925}
3926
e93ca019 3927/* See breakpoint.h. */
c906108c
SS
3928
3929void
e93ca019 3930bpstat_clear_actions (void)
c906108c 3931{
e93ca019
JK
3932 struct thread_info *tp;
3933 bpstat bs;
3934
3935 if (ptid_equal (inferior_ptid, null_ptid))
3936 return;
3937
3938 tp = find_thread_ptid (inferior_ptid);
3939 if (tp == NULL)
3940 return;
3941
3942 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
c906108c 3943 {
9add0f1b 3944 decref_counted_command_line (&bs->commands);
abf85f46 3945
c906108c
SS
3946 if (bs->old_val != NULL)
3947 {
3948 value_free (bs->old_val);
3949 bs->old_val = NULL;
3950 }
3951 }
3952}
3953
f3b1572e
PA
3954/* Called when a command is about to proceed the inferior. */
3955
3956static void
3957breakpoint_about_to_proceed (void)
3958{
3959 if (!ptid_equal (inferior_ptid, null_ptid))
3960 {
3961 struct thread_info *tp = inferior_thread ();
3962
3963 /* Allow inferior function calls in breakpoint commands to not
3964 interrupt the command list. When the call finishes
3965 successfully, the inferior will be standing at the same
3966 breakpoint as if nothing happened. */
16c381f0 3967 if (tp->control.in_infcall)
f3b1572e
PA
3968 return;
3969 }
3970
3971 breakpoint_proceeded = 1;
3972}
3973
4a64f543
MS
3974/* Stub for cleaning up our state if we error-out of a breakpoint
3975 command. */
c906108c 3976static void
4efb68b1 3977cleanup_executing_breakpoints (void *ignore)
c906108c
SS
3978{
3979 executing_breakpoint_commands = 0;
3980}
3981
abf85f46
JK
3982/* Return non-zero iff CMD as the first line of a command sequence is `silent'
3983 or its equivalent. */
3984
3985static int
3986command_line_is_silent (struct command_line *cmd)
3987{
3988 return cmd && (strcmp ("silent", cmd->line) == 0
3989 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3990}
3991
4a64f543
MS
3992/* Execute all the commands associated with all the breakpoints at
3993 this location. Any of these commands could cause the process to
3994 proceed beyond this point, etc. We look out for such changes by
3995 checking the global "breakpoint_proceeded" after each command.
c906108c 3996
347bddb7
PA
3997 Returns true if a breakpoint command resumed the inferior. In that
3998 case, it is the caller's responsibility to recall it again with the
3999 bpstat of the current thread. */
4000
4001static int
4002bpstat_do_actions_1 (bpstat *bsp)
c906108c
SS
4003{
4004 bpstat bs;
4005 struct cleanup *old_chain;
347bddb7 4006 int again = 0;
c906108c
SS
4007
4008 /* Avoid endless recursion if a `source' command is contained
4009 in bs->commands. */
4010 if (executing_breakpoint_commands)
347bddb7 4011 return 0;
c906108c
SS
4012
4013 executing_breakpoint_commands = 1;
4014 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4015
cf6c5ffb
TT
4016 prevent_dont_repeat ();
4017
4a64f543 4018 /* This pointer will iterate over the list of bpstat's. */
c906108c
SS
4019 bs = *bsp;
4020
4021 breakpoint_proceeded = 0;
4022 for (; bs != NULL; bs = bs->next)
4023 {
9add0f1b 4024 struct counted_command_line *ccmd;
6c50ab1c
JB
4025 struct command_line *cmd;
4026 struct cleanup *this_cmd_tree_chain;
4027
4028 /* Take ownership of the BSP's command tree, if it has one.
4029
4030 The command tree could legitimately contain commands like
4031 'step' and 'next', which call clear_proceed_status, which
4032 frees stop_bpstat's command tree. To make sure this doesn't
4033 free the tree we're executing out from under us, we need to
4034 take ownership of the tree ourselves. Since a given bpstat's
4035 commands are only executed once, we don't need to copy it; we
4036 can clear the pointer in the bpstat, and make sure we free
4037 the tree when we're done. */
9add0f1b
TT
4038 ccmd = bs->commands;
4039 bs->commands = NULL;
abf85f46
JK
4040 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4041 cmd = ccmd ? ccmd->commands : NULL;
4042 if (command_line_is_silent (cmd))
4043 {
4044 /* The action has been already done by bpstat_stop_status. */
4045 cmd = cmd->next;
4046 }
6c50ab1c 4047
c906108c
SS
4048 while (cmd != NULL)
4049 {
4050 execute_control_command (cmd);
4051
4052 if (breakpoint_proceeded)
4053 break;
4054 else
4055 cmd = cmd->next;
4056 }
6c50ab1c
JB
4057
4058 /* We can free this command tree now. */
4059 do_cleanups (this_cmd_tree_chain);
4060
c906108c 4061 if (breakpoint_proceeded)
32c1e744
VP
4062 {
4063 if (target_can_async_p ())
347bddb7
PA
4064 /* If we are in async mode, then the target might be still
4065 running, not stopped at any breakpoint, so nothing for
4066 us to do here -- just return to the event loop. */
4067 ;
32c1e744
VP
4068 else
4069 /* In sync mode, when execute_control_command returns
4070 we're already standing on the next breakpoint.
347bddb7
PA
4071 Breakpoint commands for that stop were not run, since
4072 execute_command does not run breakpoint commands --
4073 only command_line_handler does, but that one is not
4074 involved in execution of breakpoint commands. So, we
4075 can now execute breakpoint commands. It should be
4076 noted that making execute_command do bpstat actions is
4077 not an option -- in this case we'll have recursive
4078 invocation of bpstat for each breakpoint with a
4079 command, and can easily blow up GDB stack. Instead, we
4080 return true, which will trigger the caller to recall us
4081 with the new stop_bpstat. */
4082 again = 1;
4083 break;
32c1e744 4084 }
c906108c 4085 }
c2b8ed2c 4086 do_cleanups (old_chain);
347bddb7
PA
4087 return again;
4088}
4089
4090void
4091bpstat_do_actions (void)
4092{
353d1d73
JK
4093 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4094
347bddb7
PA
4095 /* Do any commands attached to breakpoint we are stopped at. */
4096 while (!ptid_equal (inferior_ptid, null_ptid)
4097 && target_has_execution
4098 && !is_exited (inferior_ptid)
4099 && !is_executing (inferior_ptid))
4100 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4101 and only return when it is stopped at the next breakpoint, we
4102 keep doing breakpoint actions until it returns false to
4103 indicate the inferior was not resumed. */
16c381f0 4104 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
347bddb7 4105 break;
353d1d73
JK
4106
4107 discard_cleanups (cleanup_if_error);
c906108c
SS
4108}
4109
fa4727a6
DJ
4110/* Print out the (old or new) value associated with a watchpoint. */
4111
4112static void
4113watchpoint_value_print (struct value *val, struct ui_file *stream)
4114{
4115 if (val == NULL)
4116 fprintf_unfiltered (stream, _("<unreadable>"));
4117 else
79a45b7d
TT
4118 {
4119 struct value_print_options opts;
4120 get_user_print_options (&opts);
4121 value_print (val, stream, &opts);
4122 }
fa4727a6
DJ
4123}
4124
e514a9d6 4125/* Generic routine for printing messages indicating why we
4a64f543 4126 stopped. The behavior of this function depends on the value
e514a9d6
JM
4127 'print_it' in the bpstat structure. Under some circumstances we
4128 may decide not to print anything here and delegate the task to
4a64f543 4129 normal_stop(). */
e514a9d6
JM
4130
4131static enum print_stop_action
4132print_bp_stop_message (bpstat bs)
4133{
4134 switch (bs->print_it)
4135 {
4136 case print_it_noop:
4a64f543 4137 /* Nothing should be printed for this bpstat entry. */
e514a9d6
JM
4138 return PRINT_UNKNOWN;
4139 break;
4140
4141 case print_it_done:
4142 /* We still want to print the frame, but we already printed the
4a64f543 4143 relevant messages. */
e514a9d6
JM
4144 return PRINT_SRC_AND_LOC;
4145 break;
4146
4147 case print_it_normal:
4f8d1dc6 4148 {
f431efe5
PA
4149 struct breakpoint *b = bs->breakpoint_at;
4150
1a6a67de
TJB
4151 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4152 which has since been deleted. */
4153 if (b == NULL)
4154 return PRINT_UNKNOWN;
4155
348d480f
PA
4156 /* Normal case. Call the breakpoint's print_it method. */
4157 return b->ops->print_it (bs);
4f8d1dc6 4158 }
348d480f 4159 break;
3086aeae 4160
e514a9d6 4161 default:
8e65ff28 4162 internal_error (__FILE__, __LINE__,
e2e0b3e5 4163 _("print_bp_stop_message: unrecognized enum value"));
e514a9d6 4164 break;
c906108c 4165 }
c906108c
SS
4166}
4167
edcc5120
TT
4168/* A helper function that prints a shared library stopped event. */
4169
4170static void
4171print_solib_event (int is_catchpoint)
4172{
4173 int any_deleted
4174 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4175 int any_added
4176 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4177
4178 if (!is_catchpoint)
4179 {
4180 if (any_added || any_deleted)
4181 ui_out_text (current_uiout,
4182 _("Stopped due to shared library event:\n"));
4183 else
4184 ui_out_text (current_uiout,
4185 _("Stopped due to shared library event (no "
4186 "libraries added or removed)\n"));
4187 }
4188
4189 if (ui_out_is_mi_like_p (current_uiout))
4190 ui_out_field_string (current_uiout, "reason",
4191 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4192
4193 if (any_deleted)
4194 {
4195 struct cleanup *cleanup;
4196 char *name;
4197 int ix;
4198
4199 ui_out_text (current_uiout, _(" Inferior unloaded "));
4200 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4201 "removed");
4202 for (ix = 0;
4203 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4204 ix, name);
4205 ++ix)
4206 {
4207 if (ix > 0)
4208 ui_out_text (current_uiout, " ");
4209 ui_out_field_string (current_uiout, "library", name);
4210 ui_out_text (current_uiout, "\n");
4211 }
4212
4213 do_cleanups (cleanup);
4214 }
4215
4216 if (any_added)
4217 {
4218 struct so_list *iter;
4219 int ix;
4220 struct cleanup *cleanup;
4221
4222 ui_out_text (current_uiout, _(" Inferior loaded "));
4223 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4224 "added");
4225 for (ix = 0;
4226 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4227 ix, iter);
4228 ++ix)
4229 {
4230 if (ix > 0)
4231 ui_out_text (current_uiout, " ");
4232 ui_out_field_string (current_uiout, "library", iter->so_name);
4233 ui_out_text (current_uiout, "\n");
4234 }
4235
4236 do_cleanups (cleanup);
4237 }
4238}
4239
e514a9d6
JM
4240/* Print a message indicating what happened. This is called from
4241 normal_stop(). The input to this routine is the head of the bpstat
36dfb11c
TT
4242 list - a list of the eventpoints that caused this stop. KIND is
4243 the target_waitkind for the stopping event. This
e514a9d6
JM
4244 routine calls the generic print routine for printing a message
4245 about reasons for stopping. This will print (for example) the
4246 "Breakpoint n," part of the output. The return value of this
4247 routine is one of:
c906108c 4248
4a64f543 4249 PRINT_UNKNOWN: Means we printed nothing.
917317f4 4250 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4a64f543 4251 code to print the location. An example is
c5aa993b
JM
4252 "Breakpoint 1, " which should be followed by
4253 the location.
917317f4 4254 PRINT_SRC_ONLY: Means we printed something, but there is no need
c5aa993b
JM
4255 to also print the location part of the message.
4256 An example is the catch/throw messages, which
4a64f543 4257 don't require a location appended to the end.
917317f4 4258 PRINT_NOTHING: We have done some printing and we don't need any
4a64f543 4259 further info to be printed. */
c906108c 4260
917317f4 4261enum print_stop_action
36dfb11c 4262bpstat_print (bpstat bs, int kind)
c906108c
SS
4263{
4264 int val;
c5aa993b 4265
c906108c 4266 /* Maybe another breakpoint in the chain caused us to stop.
53a5351d
JM
4267 (Currently all watchpoints go on the bpstat whether hit or not.
4268 That probably could (should) be changed, provided care is taken
c906108c 4269 with respect to bpstat_explains_signal). */
e514a9d6
JM
4270 for (; bs; bs = bs->next)
4271 {
4272 val = print_bp_stop_message (bs);
4273 if (val == PRINT_SRC_ONLY
4274 || val == PRINT_SRC_AND_LOC
4275 || val == PRINT_NOTHING)
4276 return val;
4277 }
c906108c 4278
36dfb11c
TT
4279 /* If we had hit a shared library event breakpoint,
4280 print_bp_stop_message would print out this message. If we hit an
4281 OS-level shared library event, do the same thing. */
4282 if (kind == TARGET_WAITKIND_LOADED)
4283 {
edcc5120 4284 print_solib_event (0);
36dfb11c
TT
4285 return PRINT_NOTHING;
4286 }
4287
e514a9d6 4288 /* We reached the end of the chain, or we got a null BS to start
4a64f543 4289 with and nothing was printed. */
917317f4 4290 return PRINT_UNKNOWN;
c906108c
SS
4291}
4292
4a64f543
MS
4293/* Evaluate the expression EXP and return 1 if value is zero. This is
4294 used inside a catch_errors to evaluate the breakpoint condition.
4295 The argument is a "struct expression *" that has been cast to a
4296 "char *" to make it pass through catch_errors. */
c906108c
SS
4297
4298static int
4efb68b1 4299breakpoint_cond_eval (void *exp)
c906108c 4300{
278cd55f 4301 struct value *mark = value_mark ();
c5aa993b 4302 int i = !value_true (evaluate_expression ((struct expression *) exp));
cc59ec59 4303
c906108c
SS
4304 value_free_to_mark (mark);
4305 return i;
4306}
4307
5760d0ab 4308/* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
c906108c
SS
4309
4310static bpstat
5760d0ab 4311bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
c906108c
SS
4312{
4313 bpstat bs;
4314
4315 bs = (bpstat) xmalloc (sizeof (*bs));
5760d0ab
JK
4316 bs->next = NULL;
4317 **bs_link_pointer = bs;
4318 *bs_link_pointer = &bs->next;
f431efe5
PA
4319 bs->breakpoint_at = bl->owner;
4320 bs->bp_location_at = bl;
4321 incref_bp_location (bl);
c906108c
SS
4322 /* If the condition is false, etc., don't do the commands. */
4323 bs->commands = NULL;
4324 bs->old_val = NULL;
4325 bs->print_it = print_it_normal;
4326 return bs;
4327}
4328\f
d983da9c
DJ
4329/* The target has stopped with waitstatus WS. Check if any hardware
4330 watchpoints have triggered, according to the target. */
4331
4332int
4333watchpoints_triggered (struct target_waitstatus *ws)
4334{
d92524f1 4335 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
d983da9c
DJ
4336 CORE_ADDR addr;
4337 struct breakpoint *b;
4338
4339 if (!stopped_by_watchpoint)
4340 {
4341 /* We were not stopped by a watchpoint. Mark all watchpoints
4342 as not triggered. */
4343 ALL_BREAKPOINTS (b)
cc60f2e3 4344 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4345 {
4346 struct watchpoint *w = (struct watchpoint *) b;
4347
4348 w->watchpoint_triggered = watch_triggered_no;
4349 }
d983da9c
DJ
4350
4351 return 0;
4352 }
4353
4354 if (!target_stopped_data_address (&current_target, &addr))
4355 {
4356 /* We were stopped by a watchpoint, but we don't know where.
4357 Mark all watchpoints as unknown. */
4358 ALL_BREAKPOINTS (b)
cc60f2e3 4359 if (is_hardware_watchpoint (b))
3a5c3e22
PA
4360 {
4361 struct watchpoint *w = (struct watchpoint *) b;
4362
4363 w->watchpoint_triggered = watch_triggered_unknown;
4364 }
d983da9c
DJ
4365
4366 return stopped_by_watchpoint;
4367 }
4368
4369 /* The target could report the data address. Mark watchpoints
4370 affected by this data address as triggered, and all others as not
4371 triggered. */
4372
4373 ALL_BREAKPOINTS (b)
cc60f2e3 4374 if (is_hardware_watchpoint (b))
d983da9c 4375 {
3a5c3e22 4376 struct watchpoint *w = (struct watchpoint *) b;
a5606eee 4377 struct bp_location *loc;
d983da9c 4378
3a5c3e22 4379 w->watchpoint_triggered = watch_triggered_no;
a5606eee 4380 for (loc = b->loc; loc; loc = loc->next)
9c06b0b4 4381 {
3a5c3e22 4382 if (is_masked_watchpoint (b))
9c06b0b4 4383 {
3a5c3e22
PA
4384 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4385 CORE_ADDR start = loc->address & w->hw_wp_mask;
9c06b0b4
TJB
4386
4387 if (newaddr == start)
4388 {
3a5c3e22 4389 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4390 break;
4391 }
4392 }
4393 /* Exact match not required. Within range is sufficient. */
4394 else if (target_watchpoint_addr_within_range (&current_target,
4395 addr, loc->address,
4396 loc->length))
4397 {
3a5c3e22 4398 w->watchpoint_triggered = watch_triggered_yes;
9c06b0b4
TJB
4399 break;
4400 }
4401 }
d983da9c
DJ
4402 }
4403
4404 return 1;
4405}
4406
c906108c
SS
4407/* Possible return values for watchpoint_check (this can't be an enum
4408 because of check_errors). */
4409/* The watchpoint has been deleted. */
4410#define WP_DELETED 1
4411/* The value has changed. */
4412#define WP_VALUE_CHANGED 2
4413/* The value has not changed. */
4414#define WP_VALUE_NOT_CHANGED 3
60e1c644
PA
4415/* Ignore this watchpoint, no matter if the value changed or not. */
4416#define WP_IGNORE 4
c906108c
SS
4417
4418#define BP_TEMPFLAG 1
4419#define BP_HARDWAREFLAG 2
4420
4a64f543
MS
4421/* Evaluate watchpoint condition expression and check if its value
4422 changed.
553e4c11
JB
4423
4424 P should be a pointer to struct bpstat, but is defined as a void *
4425 in order for this function to be usable with catch_errors. */
c906108c
SS
4426
4427static int
4efb68b1 4428watchpoint_check (void *p)
c906108c
SS
4429{
4430 bpstat bs = (bpstat) p;
3a5c3e22 4431 struct watchpoint *b;
c906108c
SS
4432 struct frame_info *fr;
4433 int within_current_scope;
4434
f431efe5 4435 /* BS is built from an existing struct breakpoint. */
2bdf28a0 4436 gdb_assert (bs->breakpoint_at != NULL);
3a5c3e22 4437 b = (struct watchpoint *) bs->breakpoint_at;
d0fb5eae 4438
f6bc2008
PA
4439 /* If this is a local watchpoint, we only want to check if the
4440 watchpoint frame is in scope if the current thread is the thread
4441 that was used to create the watchpoint. */
4442 if (!watchpoint_in_thread_scope (b))
60e1c644 4443 return WP_IGNORE;
f6bc2008 4444
c906108c
SS
4445 if (b->exp_valid_block == NULL)
4446 within_current_scope = 1;
4447 else
4448 {
edb3359d
DJ
4449 struct frame_info *frame = get_current_frame ();
4450 struct gdbarch *frame_arch = get_frame_arch (frame);
4451 CORE_ADDR frame_pc = get_frame_pc (frame);
4452
4a64f543
MS
4453 /* in_function_epilogue_p() returns a non-zero value if we're
4454 still in the function but the stack frame has already been
4455 invalidated. Since we can't rely on the values of local
4456 variables after the stack has been destroyed, we are treating
4457 the watchpoint in that state as `not changed' without further
4458 checking. Don't mark watchpoints as changed if the current
4459 frame is in an epilogue - even if they are in some other
4460 frame, our view of the stack is likely to be wrong and
4461 frame_find_by_id could error out. */
a0f49112 4462 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
60e1c644 4463 return WP_IGNORE;
a0f49112 4464
101dcfbe 4465 fr = frame_find_by_id (b->watchpoint_frame);
c906108c 4466 within_current_scope = (fr != NULL);
69fbadd5
DJ
4467
4468 /* If we've gotten confused in the unwinder, we might have
4469 returned a frame that can't describe this variable. */
edb3359d
DJ
4470 if (within_current_scope)
4471 {
4472 struct symbol *function;
4473
4474 function = get_frame_function (fr);
4475 if (function == NULL
4476 || !contained_in (b->exp_valid_block,
4477 SYMBOL_BLOCK_VALUE (function)))
4478 within_current_scope = 0;
4479 }
69fbadd5 4480
edb3359d 4481 if (within_current_scope)
c906108c
SS
4482 /* If we end up stopping, the current frame will get selected
4483 in normal_stop. So this call to select_frame won't affect
4484 the user. */
0f7d239c 4485 select_frame (fr);
c906108c 4486 }
c5aa993b 4487
c906108c
SS
4488 if (within_current_scope)
4489 {
4a64f543
MS
4490 /* We use value_{,free_to_}mark because it could be a *long*
4491 time before we return to the command level and call
4492 free_all_values. We can't call free_all_values because we
4493 might be in the middle of evaluating a function call. */
c906108c 4494
0cf6dd15 4495 int pc = 0;
9c06b0b4 4496 struct value *mark;
fa4727a6
DJ
4497 struct value *new_val;
4498
3a5c3e22 4499 if (is_masked_watchpoint (&b->base))
9c06b0b4
TJB
4500 /* Since we don't know the exact trigger address (from
4501 stopped_data_address), just tell the user we've triggered
4502 a mask watchpoint. */
4503 return WP_VALUE_CHANGED;
4504
4505 mark = value_mark ();
0cf6dd15 4506 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
218d2fc6 4507
4a64f543
MS
4508 /* We use value_equal_contents instead of value_equal because
4509 the latter coerces an array to a pointer, thus comparing just
4510 the address of the array instead of its contents. This is
4511 not what we want. */
fa4727a6 4512 if ((b->val != NULL) != (new_val != NULL)
218d2fc6 4513 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
c906108c 4514 {
fa4727a6
DJ
4515 if (new_val != NULL)
4516 {
4517 release_value (new_val);
4518 value_free_to_mark (mark);
4519 }
c906108c
SS
4520 bs->old_val = b->val;
4521 b->val = new_val;
fa4727a6 4522 b->val_valid = 1;
c906108c
SS
4523 return WP_VALUE_CHANGED;
4524 }
4525 else
4526 {
60e1c644 4527 /* Nothing changed. */
c906108c 4528 value_free_to_mark (mark);
c906108c
SS
4529 return WP_VALUE_NOT_CHANGED;
4530 }
4531 }
4532 else
4533 {
79a45e25
PA
4534 struct ui_out *uiout = current_uiout;
4535
c906108c 4536 /* This seems like the only logical thing to do because
c5aa993b
JM
4537 if we temporarily ignored the watchpoint, then when
4538 we reenter the block in which it is valid it contains
4539 garbage (in the case of a function, it may have two
4540 garbage values, one before and one after the prologue).
4541 So we can't even detect the first assignment to it and
4542 watch after that (since the garbage may or may not equal
4543 the first value assigned). */
348d480f
PA
4544 /* We print all the stop information in
4545 breakpoint_ops->print_it, but in this case, by the time we
4546 call breakpoint_ops->print_it this bp will be deleted
4547 already. So we have no choice but print the information
4548 here. */
9dc5e2a9 4549 if (ui_out_is_mi_like_p (uiout))
034dad6f
BR
4550 ui_out_field_string
4551 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
8b93c638 4552 ui_out_text (uiout, "\nWatchpoint ");
3a5c3e22 4553 ui_out_field_int (uiout, "wpnum", b->base.number);
3e43a32a
MS
4554 ui_out_text (uiout,
4555 " deleted because the program has left the block in\n\
8b93c638 4556which its expression is valid.\n");
4ce44c66 4557
cdac0397 4558 /* Make sure the watchpoint's commands aren't executed. */
3a5c3e22 4559 decref_counted_command_line (&b->base.commands);
d0fb5eae 4560 watchpoint_del_at_next_stop (b);
c906108c
SS
4561
4562 return WP_DELETED;
4563 }
4564}
4565
18a18393 4566/* Return true if it looks like target has stopped due to hitting
348d480f
PA
4567 breakpoint location BL. This function does not check if we should
4568 stop, only if BL explains the stop. */
4569
18a18393 4570static int
6c95b8df 4571bpstat_check_location (const struct bp_location *bl,
09ac7c10
TT
4572 struct address_space *aspace, CORE_ADDR bp_addr,
4573 const struct target_waitstatus *ws)
18a18393
VP
4574{
4575 struct breakpoint *b = bl->owner;
4576
348d480f 4577 /* BL is from an existing breakpoint. */
2bdf28a0
JK
4578 gdb_assert (b != NULL);
4579
09ac7c10 4580 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
18a18393
VP
4581}
4582
3a5c3e22
PA
4583/* Determine if the watched values have actually changed, and we
4584 should stop. If not, set BS->stop to 0. */
4585
18a18393
VP
4586static void
4587bpstat_check_watchpoint (bpstat bs)
4588{
2bdf28a0 4589 const struct bp_location *bl;
3a5c3e22 4590 struct watchpoint *b;
2bdf28a0
JK
4591
4592 /* BS is built for existing struct breakpoint. */
f431efe5 4593 bl = bs->bp_location_at;
2bdf28a0 4594 gdb_assert (bl != NULL);
3a5c3e22 4595 b = (struct watchpoint *) bs->breakpoint_at;
2bdf28a0 4596 gdb_assert (b != NULL);
18a18393 4597
18a18393 4598 {
18a18393
VP
4599 int must_check_value = 0;
4600
3a5c3e22 4601 if (b->base.type == bp_watchpoint)
18a18393
VP
4602 /* For a software watchpoint, we must always check the
4603 watched value. */
4604 must_check_value = 1;
4605 else if (b->watchpoint_triggered == watch_triggered_yes)
4606 /* We have a hardware watchpoint (read, write, or access)
4607 and the target earlier reported an address watched by
4608 this watchpoint. */
4609 must_check_value = 1;
4610 else if (b->watchpoint_triggered == watch_triggered_unknown
3a5c3e22 4611 && b->base.type == bp_hardware_watchpoint)
18a18393
VP
4612 /* We were stopped by a hardware watchpoint, but the target could
4613 not report the data address. We must check the watchpoint's
4614 value. Access and read watchpoints are out of luck; without
4615 a data address, we can't figure it out. */
4616 must_check_value = 1;
3a5c3e22 4617
18a18393
VP
4618 if (must_check_value)
4619 {
3e43a32a
MS
4620 char *message
4621 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
3a5c3e22 4622 b->base.number);
18a18393
VP
4623 struct cleanup *cleanups = make_cleanup (xfree, message);
4624 int e = catch_errors (watchpoint_check, bs, message,
4625 RETURN_MASK_ALL);
4626 do_cleanups (cleanups);
4627 switch (e)
4628 {
4629 case WP_DELETED:
4630 /* We've already printed what needs to be printed. */
4631 bs->print_it = print_it_done;
4632 /* Stop. */
4633 break;
60e1c644
PA
4634 case WP_IGNORE:
4635 bs->print_it = print_it_noop;
4636 bs->stop = 0;
4637 break;
18a18393 4638 case WP_VALUE_CHANGED:
3a5c3e22 4639 if (b->base.type == bp_read_watchpoint)
18a18393 4640 {
85d721b8
PA
4641 /* There are two cases to consider here:
4642
4a64f543 4643 1. We're watching the triggered memory for reads.
85d721b8
PA
4644 In that case, trust the target, and always report
4645 the watchpoint hit to the user. Even though
4646 reads don't cause value changes, the value may
4647 have changed since the last time it was read, and
4648 since we're not trapping writes, we will not see
4649 those, and as such we should ignore our notion of
4650 old value.
4651
4a64f543 4652 2. We're watching the triggered memory for both
85d721b8
PA
4653 reads and writes. There are two ways this may
4654 happen:
4655
4a64f543 4656 2.1. This is a target that can't break on data
85d721b8
PA
4657 reads only, but can break on accesses (reads or
4658 writes), such as e.g., x86. We detect this case
4659 at the time we try to insert read watchpoints.
4660
4a64f543 4661 2.2. Otherwise, the target supports read
85d721b8
PA
4662 watchpoints, but, the user set an access or write
4663 watchpoint watching the same memory as this read
4664 watchpoint.
4665
4666 If we're watching memory writes as well as reads,
4667 ignore watchpoint hits when we find that the
4668 value hasn't changed, as reads don't cause
4669 changes. This still gives false positives when
4670 the program writes the same value to memory as
4671 what there was already in memory (we will confuse
4672 it for a read), but it's much better than
4673 nothing. */
4674
4675 int other_write_watchpoint = 0;
4676
4677 if (bl->watchpoint_type == hw_read)
4678 {
4679 struct breakpoint *other_b;
4680
4681 ALL_BREAKPOINTS (other_b)
3a5c3e22
PA
4682 if (other_b->type == bp_hardware_watchpoint
4683 || other_b->type == bp_access_watchpoint)
85d721b8 4684 {
3a5c3e22
PA
4685 struct watchpoint *other_w =
4686 (struct watchpoint *) other_b;
4687
4688 if (other_w->watchpoint_triggered
4689 == watch_triggered_yes)
4690 {
4691 other_write_watchpoint = 1;
4692 break;
4693 }
85d721b8
PA
4694 }
4695 }
4696
4697 if (other_write_watchpoint
4698 || bl->watchpoint_type == hw_access)
4699 {
4700 /* We're watching the same memory for writes,
4701 and the value changed since the last time we
4702 updated it, so this trap must be for a write.
4703 Ignore it. */
4704 bs->print_it = print_it_noop;
4705 bs->stop = 0;
4706 }
18a18393
VP
4707 }
4708 break;
4709 case WP_VALUE_NOT_CHANGED:
3a5c3e22
PA
4710 if (b->base.type == bp_hardware_watchpoint
4711 || b->base.type == bp_watchpoint)
18a18393
VP
4712 {
4713 /* Don't stop: write watchpoints shouldn't fire if
4714 the value hasn't changed. */
4715 bs->print_it = print_it_noop;
4716 bs->stop = 0;
4717 }
4718 /* Stop. */
4719 break;
4720 default:
4721 /* Can't happen. */
4722 case 0:
4723 /* Error from catch_errors. */
3a5c3e22 4724 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
d0fb5eae 4725 watchpoint_del_at_next_stop (b);
18a18393
VP
4726 /* We've already printed what needs to be printed. */
4727 bs->print_it = print_it_done;
4728 break;
4729 }
4730 }
4731 else /* must_check_value == 0 */
4732 {
4733 /* This is a case where some watchpoint(s) triggered, but
4734 not at the address of this watchpoint, or else no
4735 watchpoint triggered after all. So don't print
4736 anything for this watchpoint. */
4737 bs->print_it = print_it_noop;
4738 bs->stop = 0;
4739 }
4740 }
4741}
4742
4743
4744/* Check conditions (condition proper, frame, thread and ignore count)
4745 of breakpoint referred to by BS. If we should not stop for this
4746 breakpoint, set BS->stop to 0. */
f431efe5 4747
18a18393
VP
4748static void
4749bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4750{
4751 int thread_id = pid_to_thread_id (ptid);
2bdf28a0
JK
4752 const struct bp_location *bl;
4753 struct breakpoint *b;
4754
4755 /* BS is built for existing struct breakpoint. */
f431efe5 4756 bl = bs->bp_location_at;
2bdf28a0 4757 gdb_assert (bl != NULL);
f431efe5 4758 b = bs->breakpoint_at;
2bdf28a0 4759 gdb_assert (b != NULL);
18a18393 4760
b775012e
LM
4761 /* Even if the target evaluated the condition on its end and notified GDB, we
4762 need to do so again since GDB does not know if we stopped due to a
4763 breakpoint or a single step breakpoint. */
4764
18a18393 4765 if (frame_id_p (b->frame_id)
edb3359d 4766 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
18a18393
VP
4767 bs->stop = 0;
4768 else if (bs->stop)
4769 {
4770 int value_is_zero = 0;
60e1c644
PA
4771 struct expression *cond;
4772
7371cf6d
PM
4773 /* Evaluate Python breakpoints that have a "stop"
4774 method implemented. */
4775 if (b->py_bp_object)
4776 bs->stop = gdbpy_should_stop (b->py_bp_object);
4777
60e1c644 4778 if (is_watchpoint (b))
3a5c3e22
PA
4779 {
4780 struct watchpoint *w = (struct watchpoint *) b;
4781
4782 cond = w->cond_exp;
4783 }
60e1c644
PA
4784 else
4785 cond = bl->cond;
4786
f431efe5 4787 if (cond && b->disposition != disp_del_at_next_stop)
18a18393 4788 {
60e1c644 4789 int within_current_scope = 1;
3a5c3e22 4790 struct watchpoint * w;
60e1c644 4791
c5bc3a77
DJ
4792 /* We use value_mark and value_free_to_mark because it could
4793 be a long time before we return to the command level and
4794 call free_all_values. We can't call free_all_values
4795 because we might be in the middle of evaluating a
4796 function call. */
4797 struct value *mark = value_mark ();
4798
3a5c3e22
PA
4799 if (is_watchpoint (b))
4800 w = (struct watchpoint *) b;
4801 else
4802 w = NULL;
4803
edb3359d
DJ
4804 /* Need to select the frame, with all that implies so that
4805 the conditions will have the right context. Because we
4806 use the frame, we will not see an inlined function's
4807 variables when we arrive at a breakpoint at the start
4808 of the inlined function; the current frame will be the
4809 call site. */
3a5c3e22 4810 if (w == NULL || w->cond_exp_valid_block == NULL)
60e1c644
PA
4811 select_frame (get_current_frame ());
4812 else
4813 {
4814 struct frame_info *frame;
4815
4816 /* For local watchpoint expressions, which particular
4817 instance of a local is being watched matters, so we
4818 keep track of the frame to evaluate the expression
4819 in. To evaluate the condition however, it doesn't
4820 really matter which instantiation of the function
4821 where the condition makes sense triggers the
4822 watchpoint. This allows an expression like "watch
4823 global if q > 10" set in `func', catch writes to
4824 global on all threads that call `func', or catch
4825 writes on all recursive calls of `func' by a single
4826 thread. We simply always evaluate the condition in
4827 the innermost frame that's executing where it makes
4828 sense to evaluate the condition. It seems
4829 intuitive. */
3a5c3e22 4830 frame = block_innermost_frame (w->cond_exp_valid_block);
60e1c644
PA
4831 if (frame != NULL)
4832 select_frame (frame);
4833 else
4834 within_current_scope = 0;
4835 }
4836 if (within_current_scope)
4837 value_is_zero
4838 = catch_errors (breakpoint_cond_eval, cond,
4839 "Error in testing breakpoint condition:\n",
4840 RETURN_MASK_ALL);
4841 else
4842 {
4843 warning (_("Watchpoint condition cannot be tested "
4844 "in the current scope"));
4845 /* If we failed to set the right context for this
4846 watchpoint, unconditionally report it. */
4847 value_is_zero = 0;
4848 }
4a64f543 4849 /* FIXME-someday, should give breakpoint #. */
c5bc3a77 4850 value_free_to_mark (mark);
18a18393 4851 }
60e1c644
PA
4852
4853 if (cond && value_is_zero)
18a18393
VP
4854 {
4855 bs->stop = 0;
4856 }
4857 else if (b->thread != -1 && b->thread != thread_id)
4858 {
4859 bs->stop = 0;
4860 }
4861 else if (b->ignore_count > 0)
4862 {
4863 b->ignore_count--;
4864 annotate_ignore_count_change ();
4865 bs->stop = 0;
4a64f543 4866 /* Increase the hit count even though we don't stop. */
18a18393 4867 ++(b->hit_count);
8d3788bd 4868 observer_notify_breakpoint_modified (b);
18a18393
VP
4869 }
4870 }
4871}
4872
4873
9709f61c 4874/* Get a bpstat associated with having just stopped at address
d983da9c 4875 BP_ADDR in thread PTID.
c906108c 4876
d983da9c 4877 Determine whether we stopped at a breakpoint, etc, or whether we
4a64f543
MS
4878 don't understand this stop. Result is a chain of bpstat's such
4879 that:
c906108c 4880
c5aa993b 4881 if we don't understand the stop, the result is a null pointer.
c906108c 4882
c5aa993b 4883 if we understand why we stopped, the result is not null.
c906108c 4884
c5aa993b
JM
4885 Each element of the chain refers to a particular breakpoint or
4886 watchpoint at which we have stopped. (We may have stopped for
4887 several reasons concurrently.)
c906108c 4888
c5aa993b
JM
4889 Each element of the chain has valid next, breakpoint_at,
4890 commands, FIXME??? fields. */
c906108c
SS
4891
4892bpstat
6c95b8df 4893bpstat_stop_status (struct address_space *aspace,
09ac7c10
TT
4894 CORE_ADDR bp_addr, ptid_t ptid,
4895 const struct target_waitstatus *ws)
c906108c 4896{
0d381245 4897 struct breakpoint *b = NULL;
afe38095 4898 struct bp_location *bl;
20874c92 4899 struct bp_location *loc;
5760d0ab
JK
4900 /* First item of allocated bpstat's. */
4901 bpstat bs_head = NULL, *bs_link = &bs_head;
c906108c 4902 /* Pointer to the last thing in the chain currently. */
5760d0ab 4903 bpstat bs;
20874c92 4904 int ix;
429374b8 4905 int need_remove_insert;
f431efe5 4906 int removed_any;
c906108c 4907
f431efe5
PA
4908 /* First, build the bpstat chain with locations that explain a
4909 target stop, while being careful to not set the target running,
4910 as that may invalidate locations (in particular watchpoint
4911 locations are recreated). Resuming will happen here with
4912 breakpoint conditions or watchpoint expressions that include
4913 inferior function calls. */
c5aa993b 4914
429374b8
JK
4915 ALL_BREAKPOINTS (b)
4916 {
4917 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4918 continue;
a5606eee 4919
429374b8
JK
4920 for (bl = b->loc; bl != NULL; bl = bl->next)
4921 {
4a64f543
MS
4922 /* For hardware watchpoints, we look only at the first
4923 location. The watchpoint_check function will work on the
4924 entire expression, not the individual locations. For
4925 read watchpoints, the watchpoints_triggered function has
4926 checked all locations already. */
429374b8
JK
4927 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4928 break;
18a18393 4929
429374b8
JK
4930 if (bl->shlib_disabled)
4931 continue;
c5aa993b 4932
09ac7c10 4933 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
429374b8 4934 continue;
c5aa993b 4935
4a64f543
MS
4936 /* Come here if it's a watchpoint, or if the break address
4937 matches. */
c5aa993b 4938
4a64f543
MS
4939 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4940 explain stop. */
c5aa993b 4941
f431efe5
PA
4942 /* Assume we stop. Should we find a watchpoint that is not
4943 actually triggered, or if the condition of the breakpoint
4944 evaluates as false, we'll reset 'stop' to 0. */
429374b8
JK
4945 bs->stop = 1;
4946 bs->print = 1;
d983da9c 4947
f431efe5
PA
4948 /* If this is a scope breakpoint, mark the associated
4949 watchpoint as triggered so that we will handle the
4950 out-of-scope event. We'll get to the watchpoint next
4951 iteration. */
d0fb5eae 4952 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
3a5c3e22
PA
4953 {
4954 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4955
4956 w->watchpoint_triggered = watch_triggered_yes;
4957 }
f431efe5
PA
4958 }
4959 }
4960
4961 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4962 {
f1310107 4963 if (breakpoint_location_address_match (loc, aspace, bp_addr))
f431efe5 4964 {
5760d0ab 4965 bs = bpstat_alloc (loc, &bs_link);
f431efe5
PA
4966 /* For hits of moribund locations, we should just proceed. */
4967 bs->stop = 0;
4968 bs->print = 0;
4969 bs->print_it = print_it_noop;
4970 }
4971 }
4972
edcc5120
TT
4973 /* A bit of special processing for shlib breakpoints. We need to
4974 process solib loading here, so that the lists of loaded and
4975 unloaded libraries are correct before we handle "catch load" and
4976 "catch unload". */
4977 for (bs = bs_head; bs != NULL; bs = bs->next)
4978 {
5d268276 4979 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
edcc5120
TT
4980 {
4981 handle_solib_event ();
4982 break;
4983 }
4984 }
4985
f431efe5
PA
4986 /* Now go through the locations that caused the target to stop, and
4987 check whether we're interested in reporting this stop to higher
4988 layers, or whether we should resume the target transparently. */
4989
4990 removed_any = 0;
4991
5760d0ab 4992 for (bs = bs_head; bs != NULL; bs = bs->next)
f431efe5
PA
4993 {
4994 if (!bs->stop)
4995 continue;
4996
f431efe5 4997 b = bs->breakpoint_at;
348d480f
PA
4998 b->ops->check_status (bs);
4999 if (bs->stop)
28010a5d 5000 {
348d480f 5001 bpstat_check_breakpoint_conditions (bs, ptid);
f431efe5 5002
429374b8
JK
5003 if (bs->stop)
5004 {
5005 ++(b->hit_count);
8d3788bd 5006 observer_notify_breakpoint_modified (b);
c906108c 5007
4a64f543 5008 /* We will stop here. */
429374b8
JK
5009 if (b->disposition == disp_disable)
5010 {
816338b5
SS
5011 --(b->enable_count);
5012 if (b->enable_count <= 0
5013 && b->enable_state != bp_permanent)
429374b8 5014 b->enable_state = bp_disabled;
f431efe5 5015 removed_any = 1;
429374b8
JK
5016 }
5017 if (b->silent)
5018 bs->print = 0;
5019 bs->commands = b->commands;
9add0f1b 5020 incref_counted_command_line (bs->commands);
abf85f46
JK
5021 if (command_line_is_silent (bs->commands
5022 ? bs->commands->commands : NULL))
5023 bs->print = 0;
429374b8
JK
5024 }
5025
348d480f 5026 }
a9b3a50f
PA
5027
5028 /* Print nothing for this entry if we don't stop or don't
5029 print. */
5030 if (!bs->stop || !bs->print)
5031 bs->print_it = print_it_noop;
429374b8 5032 }
876fa593 5033
d983da9c
DJ
5034 /* If we aren't stopping, the value of some hardware watchpoint may
5035 not have changed, but the intermediate memory locations we are
5036 watching may have. Don't bother if we're stopping; this will get
5037 done later. */
d832cb68 5038 need_remove_insert = 0;
5760d0ab
JK
5039 if (! bpstat_causes_stop (bs_head))
5040 for (bs = bs_head; bs != NULL; bs = bs->next)
d983da9c 5041 if (!bs->stop
f431efe5
PA
5042 && bs->breakpoint_at
5043 && is_hardware_watchpoint (bs->breakpoint_at))
d983da9c 5044 {
3a5c3e22
PA
5045 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5046
5047 update_watchpoint (w, 0 /* don't reparse. */);
d832cb68 5048 need_remove_insert = 1;
d983da9c
DJ
5049 }
5050
d832cb68 5051 if (need_remove_insert)
2d134ed3 5052 update_global_location_list (1);
f431efe5
PA
5053 else if (removed_any)
5054 update_global_location_list (0);
d832cb68 5055
5760d0ab 5056 return bs_head;
c906108c 5057}
628fe4e4
JK
5058
5059static void
5060handle_jit_event (void)
5061{
5062 struct frame_info *frame;
5063 struct gdbarch *gdbarch;
5064
5065 /* Switch terminal for any messages produced by
5066 breakpoint_re_set. */
5067 target_terminal_ours_for_output ();
5068
5069 frame = get_current_frame ();
5070 gdbarch = get_frame_arch (frame);
5071
5072 jit_event_handler (gdbarch);
5073
5074 target_terminal_inferior ();
5075}
5076
edcc5120
TT
5077/* Handle an solib event by calling solib_add. */
5078
5079void
5080handle_solib_event (void)
5081{
5082 clear_program_space_solib_cache (current_inferior ()->pspace);
5083
5084 /* Check for any newly added shared libraries if we're supposed to
5085 be adding them automatically. Switch terminal for any messages
5086 produced by breakpoint_re_set. */
5087 target_terminal_ours_for_output ();
5088#ifdef SOLIB_ADD
5089 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
5090#else
5091 solib_add (NULL, 0, &current_target, auto_solib_add);
5092#endif
5093 target_terminal_inferior ();
5094}
5095
628fe4e4
JK
5096/* Prepare WHAT final decision for infrun. */
5097
5098/* Decide what infrun needs to do with this bpstat. */
5099
c906108c 5100struct bpstat_what
0e30163f 5101bpstat_what (bpstat bs_head)
c906108c 5102{
c906108c 5103 struct bpstat_what retval;
628fe4e4 5104 int jit_event = 0;
0e30163f 5105 bpstat bs;
c906108c 5106
628fe4e4 5107 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
aa7d318d 5108 retval.call_dummy = STOP_NONE;
186c406b 5109 retval.is_longjmp = 0;
628fe4e4 5110
0e30163f 5111 for (bs = bs_head; bs != NULL; bs = bs->next)
c906108c 5112 {
628fe4e4
JK
5113 /* Extract this BS's action. After processing each BS, we check
5114 if its action overrides all we've seem so far. */
5115 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5116 enum bptype bptype;
5117
c906108c 5118 if (bs->breakpoint_at == NULL)
628fe4e4
JK
5119 {
5120 /* I suspect this can happen if it was a momentary
5121 breakpoint which has since been deleted. */
5122 bptype = bp_none;
5123 }
20874c92 5124 else
f431efe5 5125 bptype = bs->breakpoint_at->type;
628fe4e4
JK
5126
5127 switch (bptype)
c906108c
SS
5128 {
5129 case bp_none:
628fe4e4 5130 break;
c906108c
SS
5131 case bp_breakpoint:
5132 case bp_hardware_breakpoint:
5133 case bp_until:
5134 case bp_finish:
a9b3a50f 5135 case bp_shlib_event:
c906108c
SS
5136 if (bs->stop)
5137 {
5138 if (bs->print)
628fe4e4 5139 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5140 else
628fe4e4 5141 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5142 }
5143 else
628fe4e4 5144 this_action = BPSTAT_WHAT_SINGLE;
c906108c
SS
5145 break;
5146 case bp_watchpoint:
5147 case bp_hardware_watchpoint:
5148 case bp_read_watchpoint:
5149 case bp_access_watchpoint:
5150 if (bs->stop)
5151 {
5152 if (bs->print)
628fe4e4 5153 this_action = BPSTAT_WHAT_STOP_NOISY;
c906108c 5154 else
628fe4e4 5155 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c
SS
5156 }
5157 else
628fe4e4
JK
5158 {
5159 /* There was a watchpoint, but we're not stopping.
5160 This requires no further action. */
5161 }
c906108c
SS
5162 break;
5163 case bp_longjmp:
e2e4d78b 5164 case bp_longjmp_call_dummy:
186c406b 5165 case bp_exception:
628fe4e4 5166 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
e2e4d78b 5167 retval.is_longjmp = bptype != bp_exception;
c906108c
SS
5168 break;
5169 case bp_longjmp_resume:
186c406b 5170 case bp_exception_resume:
628fe4e4 5171 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
186c406b 5172 retval.is_longjmp = bptype == bp_longjmp_resume;
c906108c
SS
5173 break;
5174 case bp_step_resume:
5175 if (bs->stop)
628fe4e4
JK
5176 this_action = BPSTAT_WHAT_STEP_RESUME;
5177 else
c906108c 5178 {
628fe4e4
JK
5179 /* It is for the wrong frame. */
5180 this_action = BPSTAT_WHAT_SINGLE;
c906108c 5181 }
c906108c 5182 break;
2c03e5be
PA
5183 case bp_hp_step_resume:
5184 if (bs->stop)
5185 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5186 else
5187 {
5188 /* It is for the wrong frame. */
5189 this_action = BPSTAT_WHAT_SINGLE;
5190 }
5191 break;
c906108c 5192 case bp_watchpoint_scope:
c4093a6a 5193 case bp_thread_event:
1900040c 5194 case bp_overlay_event:
0fd8e87f 5195 case bp_longjmp_master:
aa7d318d 5196 case bp_std_terminate_master:
186c406b 5197 case bp_exception_master:
628fe4e4 5198 this_action = BPSTAT_WHAT_SINGLE;
c4093a6a 5199 break;
ce78b96d 5200 case bp_catchpoint:
c5aa993b
JM
5201 if (bs->stop)
5202 {
5203 if (bs->print)
628fe4e4 5204 this_action = BPSTAT_WHAT_STOP_NOISY;
c5aa993b 5205 else
628fe4e4 5206 this_action = BPSTAT_WHAT_STOP_SILENT;
c5aa993b
JM
5207 }
5208 else
628fe4e4
JK
5209 {
5210 /* There was a catchpoint, but we're not stopping.
5211 This requires no further action. */
5212 }
5213 break;
628fe4e4
JK
5214 case bp_jit_event:
5215 jit_event = 1;
5216 this_action = BPSTAT_WHAT_SINGLE;
c5aa993b 5217 break;
c906108c 5218 case bp_call_dummy:
53a5351d
JM
5219 /* Make sure the action is stop (silent or noisy),
5220 so infrun.c pops the dummy frame. */
aa7d318d 5221 retval.call_dummy = STOP_STACK_DUMMY;
628fe4e4 5222 this_action = BPSTAT_WHAT_STOP_SILENT;
aa7d318d
TT
5223 break;
5224 case bp_std_terminate:
5225 /* Make sure the action is stop (silent or noisy),
5226 so infrun.c pops the dummy frame. */
aa7d318d 5227 retval.call_dummy = STOP_STD_TERMINATE;
628fe4e4 5228 this_action = BPSTAT_WHAT_STOP_SILENT;
c906108c 5229 break;
1042e4c0 5230 case bp_tracepoint:
7a697b8d 5231 case bp_fast_tracepoint:
0fb4aa4b 5232 case bp_static_tracepoint:
1042e4c0
SS
5233 /* Tracepoint hits should not be reported back to GDB, and
5234 if one got through somehow, it should have been filtered
5235 out already. */
5236 internal_error (__FILE__, __LINE__,
7a697b8d 5237 _("bpstat_what: tracepoint encountered"));
0e30163f
JK
5238 break;
5239 case bp_gnu_ifunc_resolver:
5240 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5241 this_action = BPSTAT_WHAT_SINGLE;
5242 break;
5243 case bp_gnu_ifunc_resolver_return:
5244 /* The breakpoint will be removed, execution will restart from the
5245 PC of the former breakpoint. */
5246 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5247 break;
e7e0cddf
SS
5248
5249 case bp_dprintf:
5250 this_action = BPSTAT_WHAT_STOP_SILENT;
5251 break;
5252
628fe4e4
JK
5253 default:
5254 internal_error (__FILE__, __LINE__,
5255 _("bpstat_what: unhandled bptype %d"), (int) bptype);
c906108c 5256 }
628fe4e4
JK
5257
5258 retval.main_action = max (retval.main_action, this_action);
c906108c 5259 }
628fe4e4 5260
0e30163f
JK
5261 /* These operations may affect the bs->breakpoint_at state so they are
5262 delayed after MAIN_ACTION is decided above. */
5263
628fe4e4
JK
5264 if (jit_event)
5265 {
5266 if (debug_infrun)
5267 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5268
5269 handle_jit_event ();
5270 }
5271
0e30163f
JK
5272 for (bs = bs_head; bs != NULL; bs = bs->next)
5273 {
5274 struct breakpoint *b = bs->breakpoint_at;
5275
5276 if (b == NULL)
5277 continue;
5278 switch (b->type)
5279 {
5280 case bp_gnu_ifunc_resolver:
5281 gnu_ifunc_resolver_stop (b);
5282 break;
5283 case bp_gnu_ifunc_resolver_return:
5284 gnu_ifunc_resolver_return_stop (b);
5285 break;
5286 }
5287 }
5288
c906108c
SS
5289 return retval;
5290}
5291
5292/* Nonzero if we should step constantly (e.g. watchpoints on machines
5293 without hardware support). This isn't related to a specific bpstat,
5294 just to things like whether watchpoints are set. */
5295
c5aa993b 5296int
fba45db2 5297bpstat_should_step (void)
c906108c
SS
5298{
5299 struct breakpoint *b;
cc59ec59 5300
c906108c 5301 ALL_BREAKPOINTS (b)
717a8278 5302 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
3172dc30 5303 return 1;
c906108c
SS
5304 return 0;
5305}
5306
67822962
PA
5307int
5308bpstat_causes_stop (bpstat bs)
5309{
5310 for (; bs != NULL; bs = bs->next)
5311 if (bs->stop)
5312 return 1;
5313
5314 return 0;
5315}
5316
c906108c 5317\f
c5aa993b 5318
170b53b2
UW
5319/* Compute a string of spaces suitable to indent the next line
5320 so it starts at the position corresponding to the table column
5321 named COL_NAME in the currently active table of UIOUT. */
5322
5323static char *
5324wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5325{
5326 static char wrap_indent[80];
5327 int i, total_width, width, align;
5328 char *text;
5329
5330 total_width = 0;
5331 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5332 {
5333 if (strcmp (text, col_name) == 0)
5334 {
5335 gdb_assert (total_width < sizeof wrap_indent);
5336 memset (wrap_indent, ' ', total_width);
5337 wrap_indent[total_width] = 0;
5338
5339 return wrap_indent;
5340 }
5341
5342 total_width += width + 1;
5343 }
5344
5345 return NULL;
5346}
5347
b775012e
LM
5348/* Determine if the locations of this breakpoint will have their conditions
5349 evaluated by the target, host or a mix of both. Returns the following:
5350
5351 "host": Host evals condition.
5352 "host or target": Host or Target evals condition.
5353 "target": Target evals condition.
5354*/
5355
5356static const char *
5357bp_condition_evaluator (struct breakpoint *b)
5358{
5359 struct bp_location *bl;
5360 char host_evals = 0;
5361 char target_evals = 0;
5362
5363 if (!b)
5364 return NULL;
5365
5366 if (!is_breakpoint (b))
5367 return NULL;
5368
5369 if (gdb_evaluates_breakpoint_condition_p ()
5370 || !target_supports_evaluation_of_breakpoint_conditions ())
5371 return condition_evaluation_host;
5372
5373 for (bl = b->loc; bl; bl = bl->next)
5374 {
5375 if (bl->cond_bytecode)
5376 target_evals++;
5377 else
5378 host_evals++;
5379 }
5380
5381 if (host_evals && target_evals)
5382 return condition_evaluation_both;
5383 else if (target_evals)
5384 return condition_evaluation_target;
5385 else
5386 return condition_evaluation_host;
5387}
5388
5389/* Determine the breakpoint location's condition evaluator. This is
5390 similar to bp_condition_evaluator, but for locations. */
5391
5392static const char *
5393bp_location_condition_evaluator (struct bp_location *bl)
5394{
5395 if (bl && !is_breakpoint (bl->owner))
5396 return NULL;
5397
5398 if (gdb_evaluates_breakpoint_condition_p ()
5399 || !target_supports_evaluation_of_breakpoint_conditions ())
5400 return condition_evaluation_host;
5401
5402 if (bl && bl->cond_bytecode)
5403 return condition_evaluation_target;
5404 else
5405 return condition_evaluation_host;
5406}
5407
859825b8
JK
5408/* Print the LOC location out of the list of B->LOC locations. */
5409
170b53b2
UW
5410static void
5411print_breakpoint_location (struct breakpoint *b,
5412 struct bp_location *loc)
0d381245 5413{
79a45e25 5414 struct ui_out *uiout = current_uiout;
6c95b8df
PA
5415 struct cleanup *old_chain = save_current_program_space ();
5416
859825b8
JK
5417 if (loc != NULL && loc->shlib_disabled)
5418 loc = NULL;
5419
6c95b8df
PA
5420 if (loc != NULL)
5421 set_current_program_space (loc->pspace);
5422
56435ebe
TT
5423 if (b->display_canonical)
5424 ui_out_field_string (uiout, "what", b->addr_string);
f8eba3c6 5425 else if (loc && loc->source_file)
0d381245
VP
5426 {
5427 struct symbol *sym
5428 = find_pc_sect_function (loc->address, loc->section);
5429 if (sym)
5430 {
5431 ui_out_text (uiout, "in ");
5432 ui_out_field_string (uiout, "func",
5433 SYMBOL_PRINT_NAME (sym));
170b53b2
UW
5434 ui_out_text (uiout, " ");
5435 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5436 ui_out_text (uiout, "at ");
0d381245 5437 }
f8eba3c6 5438 ui_out_field_string (uiout, "file", loc->source_file);
0d381245
VP
5439 ui_out_text (uiout, ":");
5440
5441 if (ui_out_is_mi_like_p (uiout))
5442 {
5443 struct symtab_and_line sal = find_pc_line (loc->address, 0);
5444 char *fullname = symtab_to_fullname (sal.symtab);
5445
5446 if (fullname)
5447 ui_out_field_string (uiout, "fullname", fullname);
5448 }
5449
f8eba3c6 5450 ui_out_field_int (uiout, "line", loc->line_number);
0d381245 5451 }
859825b8 5452 else if (loc)
0d381245 5453 {
f99d8bf4
PA
5454 struct ui_file *stb = mem_fileopen ();
5455 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
170b53b2 5456
f99d8bf4 5457 print_address_symbolic (loc->gdbarch, loc->address, stb,
22e722e1 5458 demangle, "");
0d381245 5459 ui_out_field_stream (uiout, "at", stb);
170b53b2
UW
5460
5461 do_cleanups (stb_chain);
0d381245 5462 }
859825b8
JK
5463 else
5464 ui_out_field_string (uiout, "pending", b->addr_string);
6c95b8df 5465
b775012e
LM
5466 if (loc && is_breakpoint (b)
5467 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5468 && bp_condition_evaluator (b) == condition_evaluation_both)
5469 {
5470 ui_out_text (uiout, " (");
5471 ui_out_field_string (uiout, "evaluated-by",
5472 bp_location_condition_evaluator (loc));
5473 ui_out_text (uiout, ")");
5474 }
5475
6c95b8df 5476 do_cleanups (old_chain);
0d381245
VP
5477}
5478
269b11a2
PA
5479static const char *
5480bptype_string (enum bptype type)
c906108c 5481{
c4093a6a
JM
5482 struct ep_type_description
5483 {
5484 enum bptype type;
5485 char *description;
5486 };
5487 static struct ep_type_description bptypes[] =
c906108c 5488 {
c5aa993b
JM
5489 {bp_none, "?deleted?"},
5490 {bp_breakpoint, "breakpoint"},
c906108c 5491 {bp_hardware_breakpoint, "hw breakpoint"},
c5aa993b
JM
5492 {bp_until, "until"},
5493 {bp_finish, "finish"},
5494 {bp_watchpoint, "watchpoint"},
c906108c 5495 {bp_hardware_watchpoint, "hw watchpoint"},
c5aa993b
JM
5496 {bp_read_watchpoint, "read watchpoint"},
5497 {bp_access_watchpoint, "acc watchpoint"},
5498 {bp_longjmp, "longjmp"},
5499 {bp_longjmp_resume, "longjmp resume"},
e2e4d78b 5500 {bp_longjmp_call_dummy, "longjmp for call dummy"},
186c406b
TT
5501 {bp_exception, "exception"},
5502 {bp_exception_resume, "exception resume"},
c5aa993b 5503 {bp_step_resume, "step resume"},
2c03e5be 5504 {bp_hp_step_resume, "high-priority step resume"},
c5aa993b
JM
5505 {bp_watchpoint_scope, "watchpoint scope"},
5506 {bp_call_dummy, "call dummy"},
aa7d318d 5507 {bp_std_terminate, "std::terminate"},
c5aa993b 5508 {bp_shlib_event, "shlib events"},
c4093a6a 5509 {bp_thread_event, "thread events"},
1900040c 5510 {bp_overlay_event, "overlay events"},
0fd8e87f 5511 {bp_longjmp_master, "longjmp master"},
aa7d318d 5512 {bp_std_terminate_master, "std::terminate master"},
186c406b 5513 {bp_exception_master, "exception master"},
ce78b96d 5514 {bp_catchpoint, "catchpoint"},
1042e4c0 5515 {bp_tracepoint, "tracepoint"},
7a697b8d 5516 {bp_fast_tracepoint, "fast tracepoint"},
0fb4aa4b 5517 {bp_static_tracepoint, "static tracepoint"},
e7e0cddf 5518 {bp_dprintf, "dprintf"},
4efc6507 5519 {bp_jit_event, "jit events"},
0e30163f
JK
5520 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5521 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
c5aa993b 5522 };
269b11a2
PA
5523
5524 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5525 || ((int) type != bptypes[(int) type].type))
5526 internal_error (__FILE__, __LINE__,
5527 _("bptypes table does not describe type #%d."),
5528 (int) type);
5529
5530 return bptypes[(int) type].description;
5531}
5532
5533/* Print B to gdb_stdout. */
5534
5535static void
5536print_one_breakpoint_location (struct breakpoint *b,
5537 struct bp_location *loc,
5538 int loc_number,
5539 struct bp_location **last_loc,
269b11a2
PA
5540 int allflag)
5541{
5542 struct command_line *l;
c2c6d25f 5543 static char bpenables[] = "nynny";
c906108c 5544
79a45e25 5545 struct ui_out *uiout = current_uiout;
0d381245
VP
5546 int header_of_multiple = 0;
5547 int part_of_multiple = (loc != NULL);
79a45b7d
TT
5548 struct value_print_options opts;
5549
5550 get_user_print_options (&opts);
0d381245
VP
5551
5552 gdb_assert (!loc || loc_number != 0);
4a64f543
MS
5553 /* See comment in print_one_breakpoint concerning treatment of
5554 breakpoints with single disabled location. */
0d381245
VP
5555 if (loc == NULL
5556 && (b->loc != NULL
5557 && (b->loc->next != NULL || !b->loc->enabled)))
5558 header_of_multiple = 1;
5559 if (loc == NULL)
5560 loc = b->loc;
5561
c4093a6a
JM
5562 annotate_record ();
5563
5564 /* 1 */
5565 annotate_field (0);
0d381245
VP
5566 if (part_of_multiple)
5567 {
5568 char *formatted;
0c6773c1 5569 formatted = xstrprintf ("%d.%d", b->number, loc_number);
0d381245
VP
5570 ui_out_field_string (uiout, "number", formatted);
5571 xfree (formatted);
5572 }
5573 else
5574 {
5575 ui_out_field_int (uiout, "number", b->number);
5576 }
c4093a6a
JM
5577
5578 /* 2 */
5579 annotate_field (1);
0d381245
VP
5580 if (part_of_multiple)
5581 ui_out_field_skip (uiout, "type");
269b11a2
PA
5582 else
5583 ui_out_field_string (uiout, "type", bptype_string (b->type));
c4093a6a
JM
5584
5585 /* 3 */
5586 annotate_field (2);
0d381245
VP
5587 if (part_of_multiple)
5588 ui_out_field_skip (uiout, "disp");
5589 else
2cec12e5 5590 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
0d381245 5591
c4093a6a
JM
5592
5593 /* 4 */
5594 annotate_field (3);
0d381245 5595 if (part_of_multiple)
54e52265 5596 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
0d381245 5597 else
4a64f543
MS
5598 ui_out_field_fmt (uiout, "enabled", "%c",
5599 bpenables[(int) b->enable_state]);
54e52265 5600 ui_out_spaces (uiout, 2);
0d381245 5601
c4093a6a
JM
5602
5603 /* 5 and 6 */
3086aeae 5604 if (b->ops != NULL && b->ops->print_one != NULL)
0d381245 5605 {
4a64f543
MS
5606 /* Although the print_one can possibly print all locations,
5607 calling it here is not likely to get any nice result. So,
5608 make sure there's just one location. */
0d381245 5609 gdb_assert (b->loc == NULL || b->loc->next == NULL);
a6d9a66e 5610 b->ops->print_one (b, last_loc);
0d381245 5611 }
3086aeae
DJ
5612 else
5613 switch (b->type)
5614 {
5615 case bp_none:
5616 internal_error (__FILE__, __LINE__,
e2e0b3e5 5617 _("print_one_breakpoint: bp_none encountered\n"));
3086aeae 5618 break;
c906108c 5619
3086aeae
DJ
5620 case bp_watchpoint:
5621 case bp_hardware_watchpoint:
5622 case bp_read_watchpoint:
5623 case bp_access_watchpoint:
3a5c3e22
PA
5624 {
5625 struct watchpoint *w = (struct watchpoint *) b;
5626
5627 /* Field 4, the address, is omitted (which makes the columns
5628 not line up too nicely with the headers, but the effect
5629 is relatively readable). */
5630 if (opts.addressprint)
5631 ui_out_field_skip (uiout, "addr");
5632 annotate_field (5);
5633 ui_out_field_string (uiout, "what", w->exp_string);
5634 }
3086aeae
DJ
5635 break;
5636
3086aeae
DJ
5637 case bp_breakpoint:
5638 case bp_hardware_breakpoint:
5639 case bp_until:
5640 case bp_finish:
5641 case bp_longjmp:
5642 case bp_longjmp_resume:
e2e4d78b 5643 case bp_longjmp_call_dummy:
186c406b
TT
5644 case bp_exception:
5645 case bp_exception_resume:
3086aeae 5646 case bp_step_resume:
2c03e5be 5647 case bp_hp_step_resume:
3086aeae
DJ
5648 case bp_watchpoint_scope:
5649 case bp_call_dummy:
aa7d318d 5650 case bp_std_terminate:
3086aeae
DJ
5651 case bp_shlib_event:
5652 case bp_thread_event:
5653 case bp_overlay_event:
0fd8e87f 5654 case bp_longjmp_master:
aa7d318d 5655 case bp_std_terminate_master:
186c406b 5656 case bp_exception_master:
1042e4c0 5657 case bp_tracepoint:
7a697b8d 5658 case bp_fast_tracepoint:
0fb4aa4b 5659 case bp_static_tracepoint:
e7e0cddf 5660 case bp_dprintf:
4efc6507 5661 case bp_jit_event:
0e30163f
JK
5662 case bp_gnu_ifunc_resolver:
5663 case bp_gnu_ifunc_resolver_return:
79a45b7d 5664 if (opts.addressprint)
3086aeae
DJ
5665 {
5666 annotate_field (4);
54e52265 5667 if (header_of_multiple)
0d381245 5668 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
e9bbd7c5 5669 else if (b->loc == NULL || loc->shlib_disabled)
54e52265 5670 ui_out_field_string (uiout, "addr", "<PENDING>");
0101ce28 5671 else
5af949e3
UW
5672 ui_out_field_core_addr (uiout, "addr",
5673 loc->gdbarch, loc->address);
3086aeae
DJ
5674 }
5675 annotate_field (5);
0d381245 5676 if (!header_of_multiple)
170b53b2 5677 print_breakpoint_location (b, loc);
0d381245 5678 if (b->loc)
a6d9a66e 5679 *last_loc = b->loc;
3086aeae
DJ
5680 break;
5681 }
c906108c 5682
6c95b8df
PA
5683
5684 /* For backward compatibility, don't display inferiors unless there
5685 are several. */
5686 if (loc != NULL
5687 && !header_of_multiple
5688 && (allflag
5689 || (!gdbarch_has_global_breakpoints (target_gdbarch)
5690 && (number_of_program_spaces () > 1
5691 || number_of_inferiors () > 1)
4a64f543
MS
5692 /* LOC is for existing B, it cannot be in
5693 moribund_locations and thus having NULL OWNER. */
6c95b8df
PA
5694 && loc->owner->type != bp_catchpoint)))
5695 {
5696 struct inferior *inf;
5697 int first = 1;
5698
5699 for (inf = inferior_list; inf != NULL; inf = inf->next)
5700 {
5701 if (inf->pspace == loc->pspace)
5702 {
5703 if (first)
5704 {
5705 first = 0;
5706 ui_out_text (uiout, " inf ");
5707 }
5708 else
5709 ui_out_text (uiout, ", ");
5710 ui_out_text (uiout, plongest (inf->num));
5711 }
5712 }
5713 }
5714
4a306c9a 5715 if (!part_of_multiple)
c4093a6a 5716 {
4a306c9a
JB
5717 if (b->thread != -1)
5718 {
5719 /* FIXME: This seems to be redundant and lost here; see the
4a64f543 5720 "stop only in" line a little further down. */
4a306c9a
JB
5721 ui_out_text (uiout, " thread ");
5722 ui_out_field_int (uiout, "thread", b->thread);
5723 }
5724 else if (b->task != 0)
5725 {
5726 ui_out_text (uiout, " task ");
5727 ui_out_field_int (uiout, "task", b->task);
5728 }
c4093a6a 5729 }
f1310107 5730
8b93c638 5731 ui_out_text (uiout, "\n");
f1310107 5732
348d480f 5733 if (!part_of_multiple)
f1310107
TJB
5734 b->ops->print_one_detail (b, uiout);
5735
0d381245 5736 if (part_of_multiple && frame_id_p (b->frame_id))
c4093a6a
JM
5737 {
5738 annotate_field (6);
8b93c638 5739 ui_out_text (uiout, "\tstop only in stack frame at ");
e5dd4106 5740 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
818dd999 5741 the frame ID. */
5af949e3
UW
5742 ui_out_field_core_addr (uiout, "frame",
5743 b->gdbarch, b->frame_id.stack_addr);
8b93c638 5744 ui_out_text (uiout, "\n");
c4093a6a
JM
5745 }
5746
28010a5d 5747 if (!part_of_multiple && b->cond_string)
c4093a6a
JM
5748 {
5749 annotate_field (7);
d77f58be 5750 if (is_tracepoint (b))
1042e4c0
SS
5751 ui_out_text (uiout, "\ttrace only if ");
5752 else
5753 ui_out_text (uiout, "\tstop only if ");
0101ce28 5754 ui_out_field_string (uiout, "cond", b->cond_string);
b775012e
LM
5755
5756 /* Print whether the target is doing the breakpoint's condition
5757 evaluation. If GDB is doing the evaluation, don't print anything. */
5758 if (is_breakpoint (b)
5759 && breakpoint_condition_evaluation_mode ()
5760 == condition_evaluation_target)
5761 {
5762 ui_out_text (uiout, " (");
5763 ui_out_field_string (uiout, "evaluated-by",
5764 bp_condition_evaluator (b));
5765 ui_out_text (uiout, " evals)");
5766 }
0101ce28
JJ
5767 ui_out_text (uiout, "\n");
5768 }
5769
0d381245 5770 if (!part_of_multiple && b->thread != -1)
c4093a6a 5771 {
4a64f543 5772 /* FIXME should make an annotation for this. */
8b93c638
JM
5773 ui_out_text (uiout, "\tstop only in thread ");
5774 ui_out_field_int (uiout, "thread", b->thread);
5775 ui_out_text (uiout, "\n");
c4093a6a
JM
5776 }
5777
63c715c6 5778 if (!part_of_multiple && b->hit_count)
c4093a6a 5779 {
4a64f543 5780 /* FIXME should make an annotation for this. */
c326b90e 5781 if (is_catchpoint (b))
8b93c638 5782 ui_out_text (uiout, "\tcatchpoint");
f196051f
SS
5783 else if (is_tracepoint (b))
5784 ui_out_text (uiout, "\ttracepoint");
8b93c638
JM
5785 else
5786 ui_out_text (uiout, "\tbreakpoint");
5787 ui_out_text (uiout, " already hit ");
5788 ui_out_field_int (uiout, "times", b->hit_count);
5789 if (b->hit_count == 1)
5790 ui_out_text (uiout, " time\n");
5791 else
5792 ui_out_text (uiout, " times\n");
c4093a6a
JM
5793 }
5794
4a64f543
MS
5795 /* Output the count also if it is zero, but only if this is mi.
5796 FIXME: Should have a better test for this. */
9dc5e2a9 5797 if (ui_out_is_mi_like_p (uiout))
63c715c6 5798 if (!part_of_multiple && b->hit_count == 0)
fb40c209 5799 ui_out_field_int (uiout, "times", b->hit_count);
8b93c638 5800
0d381245 5801 if (!part_of_multiple && b->ignore_count)
c4093a6a
JM
5802 {
5803 annotate_field (8);
8b93c638
JM
5804 ui_out_text (uiout, "\tignore next ");
5805 ui_out_field_int (uiout, "ignore", b->ignore_count);
5806 ui_out_text (uiout, " hits\n");
c4093a6a 5807 }
059fb39f 5808
816338b5
SS
5809 /* Note that an enable count of 1 corresponds to "enable once"
5810 behavior, which is reported by the combination of enablement and
5811 disposition, so we don't need to mention it here. */
5812 if (!part_of_multiple && b->enable_count > 1)
5813 {
5814 annotate_field (8);
5815 ui_out_text (uiout, "\tdisable after ");
5816 /* Tweak the wording to clarify that ignore and enable counts
5817 are distinct, and have additive effect. */
5818 if (b->ignore_count)
5819 ui_out_text (uiout, "additional ");
5820 else
5821 ui_out_text (uiout, "next ");
5822 ui_out_field_int (uiout, "enable", b->enable_count);
5823 ui_out_text (uiout, " hits\n");
5824 }
5825
f196051f
SS
5826 if (!part_of_multiple && is_tracepoint (b))
5827 {
5828 struct tracepoint *tp = (struct tracepoint *) b;
5829
5830 if (tp->traceframe_usage)
5831 {
5832 ui_out_text (uiout, "\ttrace buffer usage ");
5833 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
5834 ui_out_text (uiout, " bytes\n");
5835 }
5836 }
5837
9add0f1b 5838 l = b->commands ? b->commands->commands : NULL;
059fb39f 5839 if (!part_of_multiple && l)
c4093a6a 5840 {
3b31d625
EZ
5841 struct cleanup *script_chain;
5842
c4093a6a 5843 annotate_field (9);
3b31d625 5844 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
8b93c638 5845 print_command_lines (uiout, l, 4);
3b31d625 5846 do_cleanups (script_chain);
c4093a6a 5847 }
d24317b4 5848
d9b3f62e 5849 if (is_tracepoint (b))
1042e4c0 5850 {
d9b3f62e
PA
5851 struct tracepoint *t = (struct tracepoint *) b;
5852
5853 if (!part_of_multiple && t->pass_count)
5854 {
5855 annotate_field (10);
5856 ui_out_text (uiout, "\tpass count ");
5857 ui_out_field_int (uiout, "pass", t->pass_count);
5858 ui_out_text (uiout, " \n");
5859 }
1042e4c0
SS
5860 }
5861
d24317b4
VP
5862 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5863 {
3a5c3e22
PA
5864 if (is_watchpoint (b))
5865 {
5866 struct watchpoint *w = (struct watchpoint *) b;
5867
5868 ui_out_field_string (uiout, "original-location", w->exp_string);
5869 }
5870 else if (b->addr_string)
d24317b4 5871 ui_out_field_string (uiout, "original-location", b->addr_string);
d24317b4 5872 }
c4093a6a 5873}
c5aa993b 5874
0d381245
VP
5875static void
5876print_one_breakpoint (struct breakpoint *b,
4a64f543 5877 struct bp_location **last_loc,
6c95b8df 5878 int allflag)
0d381245 5879{
8d3788bd 5880 struct cleanup *bkpt_chain;
79a45e25 5881 struct ui_out *uiout = current_uiout;
8d3788bd
VP
5882
5883 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
5884
12c5a436 5885 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
8d3788bd 5886 do_cleanups (bkpt_chain);
0d381245
VP
5887
5888 /* If this breakpoint has custom print function,
5889 it's already printed. Otherwise, print individual
5890 locations, if any. */
5891 if (b->ops == NULL || b->ops->print_one == NULL)
5892 {
4a64f543
MS
5893 /* If breakpoint has a single location that is disabled, we
5894 print it as if it had several locations, since otherwise it's
5895 hard to represent "breakpoint enabled, location disabled"
5896 situation.
5897
5898 Note that while hardware watchpoints have several locations
a3be7890 5899 internally, that's not a property exposed to user. */
0d381245 5900 if (b->loc
a5606eee 5901 && !is_hardware_watchpoint (b)
8d3788bd 5902 && (b->loc->next || !b->loc->enabled))
0d381245
VP
5903 {
5904 struct bp_location *loc;
5905 int n = 1;
8d3788bd 5906
0d381245 5907 for (loc = b->loc; loc; loc = loc->next, ++n)
8d3788bd
VP
5908 {
5909 struct cleanup *inner2 =
5910 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5911 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5912 do_cleanups (inner2);
5913 }
0d381245
VP
5914 }
5915 }
5916}
5917
a6d9a66e
UW
5918static int
5919breakpoint_address_bits (struct breakpoint *b)
5920{
5921 int print_address_bits = 0;
5922 struct bp_location *loc;
5923
5924 for (loc = b->loc; loc; loc = loc->next)
5925 {
c7437ca6
PA
5926 int addr_bit;
5927
5928 /* Software watchpoints that aren't watching memory don't have
5929 an address to print. */
5930 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5931 continue;
5932
5933 addr_bit = gdbarch_addr_bit (loc->gdbarch);
a6d9a66e
UW
5934 if (addr_bit > print_address_bits)
5935 print_address_bits = addr_bit;
5936 }
5937
5938 return print_address_bits;
5939}
0d381245 5940
c4093a6a
JM
5941struct captured_breakpoint_query_args
5942 {
5943 int bnum;
5944 };
c5aa993b 5945
c4093a6a 5946static int
2b65245e 5947do_captured_breakpoint_query (struct ui_out *uiout, void *data)
c4093a6a
JM
5948{
5949 struct captured_breakpoint_query_args *args = data;
52f0bd74 5950 struct breakpoint *b;
a6d9a66e 5951 struct bp_location *dummy_loc = NULL;
cc59ec59 5952
c4093a6a
JM
5953 ALL_BREAKPOINTS (b)
5954 {
5955 if (args->bnum == b->number)
c5aa993b 5956 {
12c5a436 5957 print_one_breakpoint (b, &dummy_loc, 0);
c4093a6a 5958 return GDB_RC_OK;
c5aa993b 5959 }
c4093a6a
JM
5960 }
5961 return GDB_RC_NONE;
5962}
c5aa993b 5963
c4093a6a 5964enum gdb_rc
4a64f543
MS
5965gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5966 char **error_message)
c4093a6a
JM
5967{
5968 struct captured_breakpoint_query_args args;
cc59ec59 5969
c4093a6a
JM
5970 args.bnum = bnum;
5971 /* For the moment we don't trust print_one_breakpoint() to not throw
4a64f543 5972 an error. */
b0b13bb4
DJ
5973 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5974 error_message, RETURN_MASK_ALL) < 0)
5975 return GDB_RC_FAIL;
5976 else
5977 return GDB_RC_OK;
c4093a6a 5978}
c5aa993b 5979
09d682a4
TT
5980/* Return true if this breakpoint was set by the user, false if it is
5981 internal or momentary. */
5982
5983int
5984user_breakpoint_p (struct breakpoint *b)
5985{
46c6471b 5986 return b->number > 0;
09d682a4
TT
5987}
5988
7f3b0473 5989/* Print information on user settable breakpoint (watchpoint, etc)
d77f58be
SS
5990 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5991 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5992 FILTER is non-NULL, call it on each breakpoint and only include the
5993 ones for which it returns non-zero. Return the total number of
5994 breakpoints listed. */
c906108c 5995
d77f58be 5996static int
e5a67952 5997breakpoint_1 (char *args, int allflag,
4a64f543 5998 int (*filter) (const struct breakpoint *))
c4093a6a 5999{
52f0bd74 6000 struct breakpoint *b;
a6d9a66e 6001 struct bp_location *last_loc = NULL;
7f3b0473 6002 int nr_printable_breakpoints;
3b31d625 6003 struct cleanup *bkpttbl_chain;
79a45b7d 6004 struct value_print_options opts;
a6d9a66e 6005 int print_address_bits = 0;
269b11a2 6006 int print_type_col_width = 14;
79a45e25 6007 struct ui_out *uiout = current_uiout;
269b11a2 6008
79a45b7d
TT
6009 get_user_print_options (&opts);
6010
4a64f543
MS
6011 /* Compute the number of rows in the table, as well as the size
6012 required for address fields. */
7f3b0473
AC
6013 nr_printable_breakpoints = 0;
6014 ALL_BREAKPOINTS (b)
e5a67952
MS
6015 {
6016 /* If we have a filter, only list the breakpoints it accepts. */
6017 if (filter && !filter (b))
6018 continue;
6019
6020 /* If we have an "args" string, it is a list of breakpoints to
6021 accept. Skip the others. */
6022 if (args != NULL && *args != '\0')
6023 {
6024 if (allflag && parse_and_eval_long (args) != b->number)
6025 continue;
6026 if (!allflag && !number_is_in_list (args, b->number))
6027 continue;
6028 }
269b11a2 6029
e5a67952
MS
6030 if (allflag || user_breakpoint_p (b))
6031 {
6032 int addr_bit, type_len;
a6d9a66e 6033
e5a67952
MS
6034 addr_bit = breakpoint_address_bits (b);
6035 if (addr_bit > print_address_bits)
6036 print_address_bits = addr_bit;
269b11a2 6037
e5a67952
MS
6038 type_len = strlen (bptype_string (b->type));
6039 if (type_len > print_type_col_width)
6040 print_type_col_width = type_len;
6041
6042 nr_printable_breakpoints++;
6043 }
6044 }
7f3b0473 6045
79a45b7d 6046 if (opts.addressprint)
3b31d625 6047 bkpttbl_chain
3e43a32a
MS
6048 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6049 nr_printable_breakpoints,
3b31d625 6050 "BreakpointTable");
8b93c638 6051 else
3b31d625 6052 bkpttbl_chain
3e43a32a
MS
6053 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6054 nr_printable_breakpoints,
3b31d625 6055 "BreakpointTable");
8b93c638 6056
7f3b0473 6057 if (nr_printable_breakpoints > 0)
d7faa9e7
AC
6058 annotate_breakpoints_headers ();
6059 if (nr_printable_breakpoints > 0)
6060 annotate_field (0);
4a64f543 6061 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
d7faa9e7
AC
6062 if (nr_printable_breakpoints > 0)
6063 annotate_field (1);
269b11a2 6064 ui_out_table_header (uiout, print_type_col_width, ui_left,
4a64f543 6065 "type", "Type"); /* 2 */
d7faa9e7
AC
6066 if (nr_printable_breakpoints > 0)
6067 annotate_field (2);
4a64f543 6068 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
d7faa9e7
AC
6069 if (nr_printable_breakpoints > 0)
6070 annotate_field (3);
54e52265 6071 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
79a45b7d 6072 if (opts.addressprint)
e5a67952
MS
6073 {
6074 if (nr_printable_breakpoints > 0)
6075 annotate_field (4);
6076 if (print_address_bits <= 32)
6077 ui_out_table_header (uiout, 10, ui_left,
6078 "addr", "Address"); /* 5 */
6079 else
6080 ui_out_table_header (uiout, 18, ui_left,
6081 "addr", "Address"); /* 5 */
6082 }
d7faa9e7
AC
6083 if (nr_printable_breakpoints > 0)
6084 annotate_field (5);
6085 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6086 ui_out_table_body (uiout);
6087 if (nr_printable_breakpoints > 0)
6088 annotate_breakpoints_table ();
7f3b0473 6089
c4093a6a 6090 ALL_BREAKPOINTS (b)
e5a67952
MS
6091 {
6092 QUIT;
6093 /* If we have a filter, only list the breakpoints it accepts. */
6094 if (filter && !filter (b))
6095 continue;
6096
6097 /* If we have an "args" string, it is a list of breakpoints to
6098 accept. Skip the others. */
6099
6100 if (args != NULL && *args != '\0')
6101 {
6102 if (allflag) /* maintenance info breakpoint */
6103 {
6104 if (parse_and_eval_long (args) != b->number)
6105 continue;
6106 }
6107 else /* all others */
6108 {
6109 if (!number_is_in_list (args, b->number))
6110 continue;
6111 }
6112 }
6113 /* We only print out user settable breakpoints unless the
6114 allflag is set. */
6115 if (allflag || user_breakpoint_p (b))
12c5a436 6116 print_one_breakpoint (b, &last_loc, allflag);
e5a67952
MS
6117 }
6118
3b31d625 6119 do_cleanups (bkpttbl_chain);
698384cd 6120
7f3b0473 6121 if (nr_printable_breakpoints == 0)
c906108c 6122 {
4a64f543
MS
6123 /* If there's a filter, let the caller decide how to report
6124 empty list. */
d77f58be
SS
6125 if (!filter)
6126 {
e5a67952 6127 if (args == NULL || *args == '\0')
d77f58be
SS
6128 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6129 else
4a64f543 6130 ui_out_message (uiout, 0,
e5a67952
MS
6131 "No breakpoint or watchpoint matching '%s'.\n",
6132 args);
d77f58be 6133 }
c906108c
SS
6134 }
6135 else
c4093a6a 6136 {
a6d9a66e
UW
6137 if (last_loc && !server_command)
6138 set_next_address (last_loc->gdbarch, last_loc->address);
c4093a6a 6139 }
c906108c 6140
4a64f543 6141 /* FIXME? Should this be moved up so that it is only called when
c4093a6a 6142 there have been breakpoints? */
c906108c 6143 annotate_breakpoints_table_end ();
d77f58be
SS
6144
6145 return nr_printable_breakpoints;
c906108c
SS
6146}
6147
ad443146
SS
6148/* Display the value of default-collect in a way that is generally
6149 compatible with the breakpoint list. */
6150
6151static void
6152default_collect_info (void)
6153{
79a45e25
PA
6154 struct ui_out *uiout = current_uiout;
6155
ad443146
SS
6156 /* If it has no value (which is frequently the case), say nothing; a
6157 message like "No default-collect." gets in user's face when it's
6158 not wanted. */
6159 if (!*default_collect)
6160 return;
6161
6162 /* The following phrase lines up nicely with per-tracepoint collect
6163 actions. */
6164 ui_out_text (uiout, "default collect ");
6165 ui_out_field_string (uiout, "default-collect", default_collect);
6166 ui_out_text (uiout, " \n");
6167}
6168
c906108c 6169static void
e5a67952 6170breakpoints_info (char *args, int from_tty)
c906108c 6171{
e5a67952 6172 breakpoint_1 (args, 0, NULL);
ad443146
SS
6173
6174 default_collect_info ();
d77f58be
SS
6175}
6176
6177static void
e5a67952 6178watchpoints_info (char *args, int from_tty)
d77f58be 6179{
e5a67952 6180 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
79a45e25 6181 struct ui_out *uiout = current_uiout;
d77f58be
SS
6182
6183 if (num_printed == 0)
6184 {
e5a67952 6185 if (args == NULL || *args == '\0')
d77f58be
SS
6186 ui_out_message (uiout, 0, "No watchpoints.\n");
6187 else
e5a67952 6188 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
d77f58be 6189 }
c906108c
SS
6190}
6191
7a292a7a 6192static void
e5a67952 6193maintenance_info_breakpoints (char *args, int from_tty)
c906108c 6194{
e5a67952 6195 breakpoint_1 (args, 1, NULL);
ad443146
SS
6196
6197 default_collect_info ();
c906108c
SS
6198}
6199
0d381245 6200static int
714835d5 6201breakpoint_has_pc (struct breakpoint *b,
6c95b8df 6202 struct program_space *pspace,
714835d5 6203 CORE_ADDR pc, struct obj_section *section)
0d381245
VP
6204{
6205 struct bp_location *bl = b->loc;
cc59ec59 6206
0d381245
VP
6207 for (; bl; bl = bl->next)
6208 {
6c95b8df
PA
6209 if (bl->pspace == pspace
6210 && bl->address == pc
0d381245
VP
6211 && (!overlay_debugging || bl->section == section))
6212 return 1;
6213 }
6214 return 0;
6215}
6216
672f9b60 6217/* Print a message describing any user-breakpoints set at PC. This
6c95b8df
PA
6218 concerns with logical breakpoints, so we match program spaces, not
6219 address spaces. */
c906108c
SS
6220
6221static void
6c95b8df
PA
6222describe_other_breakpoints (struct gdbarch *gdbarch,
6223 struct program_space *pspace, CORE_ADDR pc,
5af949e3 6224 struct obj_section *section, int thread)
c906108c 6225{
52f0bd74
AC
6226 int others = 0;
6227 struct breakpoint *b;
c906108c
SS
6228
6229 ALL_BREAKPOINTS (b)
672f9b60
KP
6230 others += (user_breakpoint_p (b)
6231 && breakpoint_has_pc (b, pspace, pc, section));
c906108c
SS
6232 if (others > 0)
6233 {
a3f17187
AC
6234 if (others == 1)
6235 printf_filtered (_("Note: breakpoint "));
6236 else /* if (others == ???) */
6237 printf_filtered (_("Note: breakpoints "));
c906108c 6238 ALL_BREAKPOINTS (b)
672f9b60 6239 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
0d381245
VP
6240 {
6241 others--;
6242 printf_filtered ("%d", b->number);
6243 if (b->thread == -1 && thread != -1)
6244 printf_filtered (" (all threads)");
6245 else if (b->thread != -1)
6246 printf_filtered (" (thread %d)", b->thread);
6247 printf_filtered ("%s%s ",
059fb39f 6248 ((b->enable_state == bp_disabled
f8eba3c6 6249 || b->enable_state == bp_call_disabled)
0d381245
VP
6250 ? " (disabled)"
6251 : b->enable_state == bp_permanent
6252 ? " (permanent)"
6253 : ""),
6254 (others > 1) ? ","
6255 : ((others == 1) ? " and" : ""));
6256 }
a3f17187 6257 printf_filtered (_("also set at pc "));
5af949e3 6258 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
c906108c
SS
6259 printf_filtered (".\n");
6260 }
6261}
6262\f
c906108c 6263
e4f237da
KB
6264/* Return true iff it is meaningful to use the address member of
6265 BPT. For some breakpoint types, the address member is irrelevant
6266 and it makes no sense to attempt to compare it to other addresses
6267 (or use it for any other purpose either).
6268
4a64f543
MS
6269 More specifically, each of the following breakpoint types will
6270 always have a zero valued address and we don't want to mark
6271 breakpoints of any of these types to be a duplicate of an actual
6272 breakpoint at address zero:
e4f237da
KB
6273
6274 bp_watchpoint
2d134ed3
PA
6275 bp_catchpoint
6276
6277*/
e4f237da
KB
6278
6279static int
6280breakpoint_address_is_meaningful (struct breakpoint *bpt)
6281{
6282 enum bptype type = bpt->type;
6283
2d134ed3
PA
6284 return (type != bp_watchpoint && type != bp_catchpoint);
6285}
6286
6287/* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6288 true if LOC1 and LOC2 represent the same watchpoint location. */
6289
6290static int
4a64f543
MS
6291watchpoint_locations_match (struct bp_location *loc1,
6292 struct bp_location *loc2)
2d134ed3 6293{
3a5c3e22
PA
6294 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6295 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6296
6297 /* Both of them must exist. */
6298 gdb_assert (w1 != NULL);
6299 gdb_assert (w2 != NULL);
2bdf28a0 6300
4a64f543
MS
6301 /* If the target can evaluate the condition expression in hardware,
6302 then we we need to insert both watchpoints even if they are at
6303 the same place. Otherwise the watchpoint will only trigger when
6304 the condition of whichever watchpoint was inserted evaluates to
6305 true, not giving a chance for GDB to check the condition of the
6306 other watchpoint. */
3a5c3e22 6307 if ((w1->cond_exp
4a64f543
MS
6308 && target_can_accel_watchpoint_condition (loc1->address,
6309 loc1->length,
0cf6dd15 6310 loc1->watchpoint_type,
3a5c3e22
PA
6311 w1->cond_exp))
6312 || (w2->cond_exp
4a64f543
MS
6313 && target_can_accel_watchpoint_condition (loc2->address,
6314 loc2->length,
0cf6dd15 6315 loc2->watchpoint_type,
3a5c3e22 6316 w2->cond_exp)))
0cf6dd15
TJB
6317 return 0;
6318
85d721b8
PA
6319 /* Note that this checks the owner's type, not the location's. In
6320 case the target does not support read watchpoints, but does
6321 support access watchpoints, we'll have bp_read_watchpoint
6322 watchpoints with hw_access locations. Those should be considered
6323 duplicates of hw_read locations. The hw_read locations will
6324 become hw_access locations later. */
2d134ed3
PA
6325 return (loc1->owner->type == loc2->owner->type
6326 && loc1->pspace->aspace == loc2->pspace->aspace
6327 && loc1->address == loc2->address
6328 && loc1->length == loc2->length);
e4f237da
KB
6329}
6330
6c95b8df
PA
6331/* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6332 same breakpoint location. In most targets, this can only be true
6333 if ASPACE1 matches ASPACE2. On targets that have global
6334 breakpoints, the address space doesn't really matter. */
6335
6336static int
6337breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6338 struct address_space *aspace2, CORE_ADDR addr2)
6339{
6340 return ((gdbarch_has_global_breakpoints (target_gdbarch)
6341 || aspace1 == aspace2)
6342 && addr1 == addr2);
6343}
6344
f1310107
TJB
6345/* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6346 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6347 matches ASPACE2. On targets that have global breakpoints, the address
6348 space doesn't really matter. */
6349
6350static int
6351breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6352 int len1, struct address_space *aspace2,
6353 CORE_ADDR addr2)
6354{
6355 return ((gdbarch_has_global_breakpoints (target_gdbarch)
6356 || aspace1 == aspace2)
6357 && addr2 >= addr1 && addr2 < addr1 + len1);
6358}
6359
6360/* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6361 a ranged breakpoint. In most targets, a match happens only if ASPACE
6362 matches the breakpoint's address space. On targets that have global
6363 breakpoints, the address space doesn't really matter. */
6364
6365static int
6366breakpoint_location_address_match (struct bp_location *bl,
6367 struct address_space *aspace,
6368 CORE_ADDR addr)
6369{
6370 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6371 aspace, addr)
6372 || (bl->length
6373 && breakpoint_address_match_range (bl->pspace->aspace,
6374 bl->address, bl->length,
6375 aspace, addr)));
6376}
6377
1e4d1764
YQ
6378/* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6379 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6380 true, otherwise returns false. */
6381
6382static int
6383tracepoint_locations_match (struct bp_location *loc1,
6384 struct bp_location *loc2)
6385{
6386 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6387 /* Since tracepoint locations are never duplicated with others', tracepoint
6388 locations at the same address of different tracepoints are regarded as
6389 different locations. */
6390 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6391 else
6392 return 0;
6393}
6394
2d134ed3
PA
6395/* Assuming LOC1 and LOC2's types' have meaningful target addresses
6396 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6397 represent the same location. */
6398
6399static int
4a64f543
MS
6400breakpoint_locations_match (struct bp_location *loc1,
6401 struct bp_location *loc2)
2d134ed3 6402{
2bdf28a0
JK
6403 int hw_point1, hw_point2;
6404
6405 /* Both of them must not be in moribund_locations. */
6406 gdb_assert (loc1->owner != NULL);
6407 gdb_assert (loc2->owner != NULL);
6408
6409 hw_point1 = is_hardware_watchpoint (loc1->owner);
6410 hw_point2 = is_hardware_watchpoint (loc2->owner);
2d134ed3
PA
6411
6412 if (hw_point1 != hw_point2)
6413 return 0;
6414 else if (hw_point1)
6415 return watchpoint_locations_match (loc1, loc2);
1e4d1764
YQ
6416 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6417 return tracepoint_locations_match (loc1, loc2);
2d134ed3 6418 else
f1310107
TJB
6419 /* We compare bp_location.length in order to cover ranged breakpoints. */
6420 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6421 loc2->pspace->aspace, loc2->address)
6422 && loc1->length == loc2->length);
2d134ed3
PA
6423}
6424
76897487
KB
6425static void
6426breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6427 int bnum, int have_bnum)
6428{
f63fbe86
MS
6429 /* The longest string possibly returned by hex_string_custom
6430 is 50 chars. These must be at least that big for safety. */
6431 char astr1[64];
6432 char astr2[64];
76897487 6433
bb599908
PH
6434 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6435 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
76897487 6436 if (have_bnum)
8a3fe4f8 6437 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
76897487
KB
6438 bnum, astr1, astr2);
6439 else
8a3fe4f8 6440 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
76897487
KB
6441}
6442
4a64f543
MS
6443/* Adjust a breakpoint's address to account for architectural
6444 constraints on breakpoint placement. Return the adjusted address.
6445 Note: Very few targets require this kind of adjustment. For most
6446 targets, this function is simply the identity function. */
76897487
KB
6447
6448static CORE_ADDR
a6d9a66e
UW
6449adjust_breakpoint_address (struct gdbarch *gdbarch,
6450 CORE_ADDR bpaddr, enum bptype bptype)
76897487 6451{
a6d9a66e 6452 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
76897487
KB
6453 {
6454 /* Very few targets need any kind of breakpoint adjustment. */
6455 return bpaddr;
6456 }
88f7da05
KB
6457 else if (bptype == bp_watchpoint
6458 || bptype == bp_hardware_watchpoint
6459 || bptype == bp_read_watchpoint
6460 || bptype == bp_access_watchpoint
fe798b75 6461 || bptype == bp_catchpoint)
88f7da05
KB
6462 {
6463 /* Watchpoints and the various bp_catch_* eventpoints should not
6464 have their addresses modified. */
6465 return bpaddr;
6466 }
76897487
KB
6467 else
6468 {
6469 CORE_ADDR adjusted_bpaddr;
6470
6471 /* Some targets have architectural constraints on the placement
6472 of breakpoint instructions. Obtain the adjusted address. */
a6d9a66e 6473 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
76897487
KB
6474
6475 /* An adjusted breakpoint address can significantly alter
6476 a user's expectations. Print a warning if an adjustment
6477 is required. */
6478 if (adjusted_bpaddr != bpaddr)
6479 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6480
6481 return adjusted_bpaddr;
6482 }
6483}
6484
28010a5d
PA
6485void
6486init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6487 struct breakpoint *owner)
7cc221ef 6488{
7cc221ef
DJ
6489 memset (loc, 0, sizeof (*loc));
6490
348d480f
PA
6491 gdb_assert (ops != NULL);
6492
28010a5d
PA
6493 loc->ops = ops;
6494 loc->owner = owner;
511a6cd4 6495 loc->cond = NULL;
b775012e 6496 loc->cond_bytecode = NULL;
0d381245
VP
6497 loc->shlib_disabled = 0;
6498 loc->enabled = 1;
e049a4b5 6499
28010a5d 6500 switch (owner->type)
e049a4b5
DJ
6501 {
6502 case bp_breakpoint:
6503 case bp_until:
6504 case bp_finish:
6505 case bp_longjmp:
6506 case bp_longjmp_resume:
e2e4d78b 6507 case bp_longjmp_call_dummy:
186c406b
TT
6508 case bp_exception:
6509 case bp_exception_resume:
e049a4b5 6510 case bp_step_resume:
2c03e5be 6511 case bp_hp_step_resume:
e049a4b5
DJ
6512 case bp_watchpoint_scope:
6513 case bp_call_dummy:
aa7d318d 6514 case bp_std_terminate:
e049a4b5
DJ
6515 case bp_shlib_event:
6516 case bp_thread_event:
6517 case bp_overlay_event:
4efc6507 6518 case bp_jit_event:
0fd8e87f 6519 case bp_longjmp_master:
aa7d318d 6520 case bp_std_terminate_master:
186c406b 6521 case bp_exception_master:
0e30163f
JK
6522 case bp_gnu_ifunc_resolver:
6523 case bp_gnu_ifunc_resolver_return:
e7e0cddf 6524 case bp_dprintf:
e049a4b5 6525 loc->loc_type = bp_loc_software_breakpoint;
b775012e 6526 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
6527 break;
6528 case bp_hardware_breakpoint:
6529 loc->loc_type = bp_loc_hardware_breakpoint;
b775012e 6530 mark_breakpoint_location_modified (loc);
e049a4b5
DJ
6531 break;
6532 case bp_hardware_watchpoint:
6533 case bp_read_watchpoint:
6534 case bp_access_watchpoint:
6535 loc->loc_type = bp_loc_hardware_watchpoint;
6536 break;
6537 case bp_watchpoint:
ce78b96d 6538 case bp_catchpoint:
15c3d785
PA
6539 case bp_tracepoint:
6540 case bp_fast_tracepoint:
0fb4aa4b 6541 case bp_static_tracepoint:
e049a4b5
DJ
6542 loc->loc_type = bp_loc_other;
6543 break;
6544 default:
e2e0b3e5 6545 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
e049a4b5
DJ
6546 }
6547
f431efe5 6548 loc->refc = 1;
28010a5d
PA
6549}
6550
6551/* Allocate a struct bp_location. */
6552
6553static struct bp_location *
6554allocate_bp_location (struct breakpoint *bpt)
6555{
348d480f
PA
6556 return bpt->ops->allocate_location (bpt);
6557}
7cc221ef 6558
f431efe5
PA
6559static void
6560free_bp_location (struct bp_location *loc)
fe3f5fa8 6561{
348d480f 6562 loc->ops->dtor (loc);
fe3f5fa8
VP
6563 xfree (loc);
6564}
6565
f431efe5
PA
6566/* Increment reference count. */
6567
6568static void
6569incref_bp_location (struct bp_location *bl)
6570{
6571 ++bl->refc;
6572}
6573
6574/* Decrement reference count. If the reference count reaches 0,
6575 destroy the bp_location. Sets *BLP to NULL. */
6576
6577static void
6578decref_bp_location (struct bp_location **blp)
6579{
0807b50c
PA
6580 gdb_assert ((*blp)->refc > 0);
6581
f431efe5
PA
6582 if (--(*blp)->refc == 0)
6583 free_bp_location (*blp);
6584 *blp = NULL;
6585}
6586
346774a9 6587/* Add breakpoint B at the end of the global breakpoint chain. */
c906108c 6588
346774a9
PA
6589static void
6590add_to_breakpoint_chain (struct breakpoint *b)
c906108c 6591{
346774a9 6592 struct breakpoint *b1;
c906108c 6593
346774a9
PA
6594 /* Add this breakpoint to the end of the chain so that a list of
6595 breakpoints will come out in order of increasing numbers. */
6596
6597 b1 = breakpoint_chain;
6598 if (b1 == 0)
6599 breakpoint_chain = b;
6600 else
6601 {
6602 while (b1->next)
6603 b1 = b1->next;
6604 b1->next = b;
6605 }
6606}
6607
6608/* Initializes breakpoint B with type BPTYPE and no locations yet. */
6609
6610static void
6611init_raw_breakpoint_without_location (struct breakpoint *b,
6612 struct gdbarch *gdbarch,
28010a5d 6613 enum bptype bptype,
c0a91b2b 6614 const struct breakpoint_ops *ops)
346774a9 6615{
c906108c 6616 memset (b, 0, sizeof (*b));
2219d63c 6617
348d480f
PA
6618 gdb_assert (ops != NULL);
6619
28010a5d 6620 b->ops = ops;
4d28f7a8 6621 b->type = bptype;
a6d9a66e 6622 b->gdbarch = gdbarch;
c906108c
SS
6623 b->language = current_language->la_language;
6624 b->input_radix = input_radix;
6625 b->thread = -1;
b5de0fa7 6626 b->enable_state = bp_enabled;
c906108c
SS
6627 b->next = 0;
6628 b->silent = 0;
6629 b->ignore_count = 0;
6630 b->commands = NULL;
818dd999 6631 b->frame_id = null_frame_id;
0d381245 6632 b->condition_not_parsed = 0;
84f4c1fe 6633 b->py_bp_object = NULL;
d0fb5eae 6634 b->related_breakpoint = b;
346774a9
PA
6635}
6636
6637/* Helper to set_raw_breakpoint below. Creates a breakpoint
6638 that has type BPTYPE and has no locations as yet. */
346774a9
PA
6639
6640static struct breakpoint *
6641set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
348d480f 6642 enum bptype bptype,
c0a91b2b 6643 const struct breakpoint_ops *ops)
346774a9
PA
6644{
6645 struct breakpoint *b = XNEW (struct breakpoint);
6646
348d480f 6647 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
c56053d2 6648 add_to_breakpoint_chain (b);
0d381245
VP
6649 return b;
6650}
6651
0e30163f
JK
6652/* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
6653 resolutions should be made as the user specified the location explicitly
6654 enough. */
6655
0d381245 6656static void
0e30163f 6657set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
0d381245 6658{
2bdf28a0
JK
6659 gdb_assert (loc->owner != NULL);
6660
0d381245 6661 if (loc->owner->type == bp_breakpoint
1042e4c0 6662 || loc->owner->type == bp_hardware_breakpoint
d77f58be 6663 || is_tracepoint (loc->owner))
0d381245 6664 {
0e30163f 6665 int is_gnu_ifunc;
2c02bd72 6666 const char *function_name;
6a3a010b 6667 CORE_ADDR func_addr;
0e30163f 6668
2c02bd72 6669 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6a3a010b 6670 &func_addr, NULL, &is_gnu_ifunc);
0e30163f
JK
6671
6672 if (is_gnu_ifunc && !explicit_loc)
6673 {
6674 struct breakpoint *b = loc->owner;
6675
6676 gdb_assert (loc->pspace == current_program_space);
2c02bd72 6677 if (gnu_ifunc_resolve_name (function_name,
0e30163f
JK
6678 &loc->requested_address))
6679 {
6680 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
6681 loc->address = adjust_breakpoint_address (loc->gdbarch,
6682 loc->requested_address,
6683 b->type);
6684 }
6685 else if (b->type == bp_breakpoint && b->loc == loc
6686 && loc->next == NULL && b->related_breakpoint == b)
6687 {
6688 /* Create only the whole new breakpoint of this type but do not
6689 mess more complicated breakpoints with multiple locations. */
6690 b->type = bp_gnu_ifunc_resolver;
6a3a010b
MR
6691 /* Remember the resolver's address for use by the return
6692 breakpoint. */
6693 loc->related_address = func_addr;
0e30163f
JK
6694 }
6695 }
6696
2c02bd72
DE
6697 if (function_name)
6698 loc->function_name = xstrdup (function_name);
0d381245
VP
6699 }
6700}
6701
a6d9a66e 6702/* Attempt to determine architecture of location identified by SAL. */
1bfeeb0f 6703struct gdbarch *
a6d9a66e
UW
6704get_sal_arch (struct symtab_and_line sal)
6705{
6706 if (sal.section)
6707 return get_objfile_arch (sal.section->objfile);
6708 if (sal.symtab)
6709 return get_objfile_arch (sal.symtab->objfile);
6710
6711 return NULL;
6712}
6713
346774a9
PA
6714/* Low level routine for partially initializing a breakpoint of type
6715 BPTYPE. The newly created breakpoint's address, section, source
c56053d2 6716 file name, and line number are provided by SAL.
0d381245
VP
6717
6718 It is expected that the caller will complete the initialization of
6719 the newly created breakpoint struct as well as output any status
c56053d2 6720 information regarding the creation of a new breakpoint. */
0d381245 6721
346774a9
PA
6722static void
6723init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
28010a5d 6724 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 6725 const struct breakpoint_ops *ops)
0d381245 6726{
28010a5d 6727 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
346774a9 6728
3742cc8b 6729 add_location_to_breakpoint (b, &sal);
0d381245 6730
6c95b8df
PA
6731 if (bptype != bp_catchpoint)
6732 gdb_assert (sal.pspace != NULL);
6733
f8eba3c6
TT
6734 /* Store the program space that was used to set the breakpoint,
6735 except for ordinary breakpoints, which are independent of the
6736 program space. */
6737 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
6738 b->pspace = sal.pspace;
0d381245 6739
c906108c 6740 breakpoints_changed ();
346774a9 6741}
c906108c 6742
346774a9
PA
6743/* set_raw_breakpoint is a low level routine for allocating and
6744 partially initializing a breakpoint of type BPTYPE. The newly
6745 created breakpoint's address, section, source file name, and line
6746 number are provided by SAL. The newly created and partially
6747 initialized breakpoint is added to the breakpoint chain and
6748 is also returned as the value of this function.
6749
6750 It is expected that the caller will complete the initialization of
6751 the newly created breakpoint struct as well as output any status
6752 information regarding the creation of a new breakpoint. In
6753 particular, set_raw_breakpoint does NOT set the breakpoint
6754 number! Care should be taken to not allow an error to occur
6755 prior to completing the initialization of the breakpoint. If this
6756 should happen, a bogus breakpoint will be left on the chain. */
6757
6758struct breakpoint *
6759set_raw_breakpoint (struct gdbarch *gdbarch,
348d480f 6760 struct symtab_and_line sal, enum bptype bptype,
c0a91b2b 6761 const struct breakpoint_ops *ops)
346774a9
PA
6762{
6763 struct breakpoint *b = XNEW (struct breakpoint);
6764
348d480f 6765 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
c56053d2 6766 add_to_breakpoint_chain (b);
c906108c
SS
6767 return b;
6768}
6769
c2c6d25f
JM
6770
6771/* Note that the breakpoint object B describes a permanent breakpoint
6772 instruction, hard-wired into the inferior's code. */
6773void
6774make_breakpoint_permanent (struct breakpoint *b)
6775{
0d381245 6776 struct bp_location *bl;
cc59ec59 6777
b5de0fa7 6778 b->enable_state = bp_permanent;
c2c6d25f 6779
4a64f543
MS
6780 /* By definition, permanent breakpoints are already present in the
6781 code. Mark all locations as inserted. For now,
6782 make_breakpoint_permanent is called in just one place, so it's
6783 hard to say if it's reasonable to have permanent breakpoint with
e5dd4106 6784 multiple locations or not, but it's easy to implement. */
0d381245
VP
6785 for (bl = b->loc; bl; bl = bl->next)
6786 bl->inserted = 1;
c2c6d25f
JM
6787}
6788
53a5351d 6789/* Call this routine when stepping and nexting to enable a breakpoint
186c406b
TT
6790 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
6791 initiated the operation. */
c906108c
SS
6792
6793void
186c406b 6794set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
c906108c 6795{
35df4500 6796 struct breakpoint *b, *b_tmp;
186c406b 6797 int thread = tp->num;
0fd8e87f
UW
6798
6799 /* To avoid having to rescan all objfile symbols at every step,
6800 we maintain a list of continually-inserted but always disabled
6801 longjmp "master" breakpoints. Here, we simply create momentary
6802 clones of those and enable them for the requested thread. */
35df4500 6803 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df 6804 if (b->pspace == current_program_space
186c406b
TT
6805 && (b->type == bp_longjmp_master
6806 || b->type == bp_exception_master))
0fd8e87f 6807 {
06edf0c0
PA
6808 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
6809 struct breakpoint *clone;
cc59ec59 6810
e2e4d78b
JK
6811 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
6812 after their removal. */
06edf0c0 6813 clone = momentary_breakpoint_from_master (b, type,
e2e4d78b 6814 &longjmp_breakpoint_ops);
0fd8e87f
UW
6815 clone->thread = thread;
6816 }
186c406b
TT
6817
6818 tp->initiating_frame = frame;
c906108c
SS
6819}
6820
611c83ae 6821/* Delete all longjmp breakpoints from THREAD. */
c906108c 6822void
611c83ae 6823delete_longjmp_breakpoint (int thread)
c906108c 6824{
35df4500 6825 struct breakpoint *b, *b_tmp;
c906108c 6826
35df4500 6827 ALL_BREAKPOINTS_SAFE (b, b_tmp)
186c406b 6828 if (b->type == bp_longjmp || b->type == bp_exception)
611c83ae
PA
6829 {
6830 if (b->thread == thread)
6831 delete_breakpoint (b);
6832 }
c906108c
SS
6833}
6834
f59f708a
PA
6835void
6836delete_longjmp_breakpoint_at_next_stop (int thread)
6837{
6838 struct breakpoint *b, *b_tmp;
6839
6840 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6841 if (b->type == bp_longjmp || b->type == bp_exception)
6842 {
6843 if (b->thread == thread)
6844 b->disposition = disp_del_at_next_stop;
6845 }
6846}
6847
e2e4d78b
JK
6848/* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
6849 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
6850 pointer to any of them. Return NULL if this system cannot place longjmp
6851 breakpoints. */
6852
6853struct breakpoint *
6854set_longjmp_breakpoint_for_call_dummy (void)
6855{
6856 struct breakpoint *b, *retval = NULL;
6857
6858 ALL_BREAKPOINTS (b)
6859 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
6860 {
6861 struct breakpoint *new_b;
6862
6863 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
6864 &momentary_breakpoint_ops);
6865 new_b->thread = pid_to_thread_id (inferior_ptid);
6866
6867 /* Link NEW_B into the chain of RETVAL breakpoints. */
6868
6869 gdb_assert (new_b->related_breakpoint == new_b);
6870 if (retval == NULL)
6871 retval = new_b;
6872 new_b->related_breakpoint = retval;
6873 while (retval->related_breakpoint != new_b->related_breakpoint)
6874 retval = retval->related_breakpoint;
6875 retval->related_breakpoint = new_b;
6876 }
6877
6878 return retval;
6879}
6880
6881/* Verify all existing dummy frames and their associated breakpoints for
6882 THREAD. Remove those which can no longer be found in the current frame
6883 stack.
6884
6885 You should call this function only at places where it is safe to currently
6886 unwind the whole stack. Failed stack unwind would discard live dummy
6887 frames. */
6888
6889void
6890check_longjmp_breakpoint_for_call_dummy (int thread)
6891{
6892 struct breakpoint *b, *b_tmp;
6893
6894 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6895 if (b->type == bp_longjmp_call_dummy && b->thread == thread)
6896 {
6897 struct breakpoint *dummy_b = b->related_breakpoint;
6898
6899 while (dummy_b != b && dummy_b->type != bp_call_dummy)
6900 dummy_b = dummy_b->related_breakpoint;
6901 if (dummy_b->type != bp_call_dummy
6902 || frame_find_by_id (dummy_b->frame_id) != NULL)
6903 continue;
6904
6905 dummy_frame_discard (dummy_b->frame_id);
6906
6907 while (b->related_breakpoint != b)
6908 {
6909 if (b_tmp == b->related_breakpoint)
6910 b_tmp = b->related_breakpoint->next;
6911 delete_breakpoint (b->related_breakpoint);
6912 }
6913 delete_breakpoint (b);
6914 }
6915}
6916
1900040c
MS
6917void
6918enable_overlay_breakpoints (void)
6919{
52f0bd74 6920 struct breakpoint *b;
1900040c
MS
6921
6922 ALL_BREAKPOINTS (b)
6923 if (b->type == bp_overlay_event)
6924 {
6925 b->enable_state = bp_enabled;
b60e7edf 6926 update_global_location_list (1);
c02f5703 6927 overlay_events_enabled = 1;
1900040c
MS
6928 }
6929}
6930
6931void
6932disable_overlay_breakpoints (void)
6933{
52f0bd74 6934 struct breakpoint *b;
1900040c
MS
6935
6936 ALL_BREAKPOINTS (b)
6937 if (b->type == bp_overlay_event)
6938 {
6939 b->enable_state = bp_disabled;
b60e7edf 6940 update_global_location_list (0);
c02f5703 6941 overlay_events_enabled = 0;
1900040c
MS
6942 }
6943}
6944
aa7d318d
TT
6945/* Set an active std::terminate breakpoint for each std::terminate
6946 master breakpoint. */
6947void
6948set_std_terminate_breakpoint (void)
6949{
35df4500 6950 struct breakpoint *b, *b_tmp;
aa7d318d 6951
35df4500 6952 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6953 if (b->pspace == current_program_space
6954 && b->type == bp_std_terminate_master)
6955 {
06edf0c0
PA
6956 momentary_breakpoint_from_master (b, bp_std_terminate,
6957 &momentary_breakpoint_ops);
aa7d318d
TT
6958 }
6959}
6960
6961/* Delete all the std::terminate breakpoints. */
6962void
6963delete_std_terminate_breakpoint (void)
6964{
35df4500 6965 struct breakpoint *b, *b_tmp;
aa7d318d 6966
35df4500 6967 ALL_BREAKPOINTS_SAFE (b, b_tmp)
aa7d318d
TT
6968 if (b->type == bp_std_terminate)
6969 delete_breakpoint (b);
6970}
6971
c4093a6a 6972struct breakpoint *
a6d9a66e 6973create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
c4093a6a
JM
6974{
6975 struct breakpoint *b;
c4093a6a 6976
06edf0c0
PA
6977 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
6978 &internal_breakpoint_ops);
6979
b5de0fa7 6980 b->enable_state = bp_enabled;
c4093a6a 6981 /* addr_string has to be used or breakpoint_re_set will delete me. */
5af949e3
UW
6982 b->addr_string
6983 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
c4093a6a 6984
b60e7edf 6985 update_global_location_list_nothrow (1);
74960c60 6986
c4093a6a
JM
6987 return b;
6988}
6989
6990void
6991remove_thread_event_breakpoints (void)
6992{
35df4500 6993 struct breakpoint *b, *b_tmp;
c4093a6a 6994
35df4500 6995 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
6996 if (b->type == bp_thread_event
6997 && b->loc->pspace == current_program_space)
c4093a6a
JM
6998 delete_breakpoint (b);
6999}
7000
0101ce28
JJ
7001struct lang_and_radix
7002 {
7003 enum language lang;
7004 int radix;
7005 };
7006
4efc6507
DE
7007/* Create a breakpoint for JIT code registration and unregistration. */
7008
7009struct breakpoint *
7010create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7011{
7012 struct breakpoint *b;
7013
06edf0c0
PA
7014 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7015 &internal_breakpoint_ops);
4efc6507
DE
7016 update_global_location_list_nothrow (1);
7017 return b;
7018}
0101ce28 7019
03673fc7
PP
7020/* Remove JIT code registration and unregistration breakpoint(s). */
7021
7022void
7023remove_jit_event_breakpoints (void)
7024{
7025 struct breakpoint *b, *b_tmp;
7026
7027 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7028 if (b->type == bp_jit_event
7029 && b->loc->pspace == current_program_space)
7030 delete_breakpoint (b);
7031}
7032
cae688ec
JJ
7033void
7034remove_solib_event_breakpoints (void)
7035{
35df4500 7036 struct breakpoint *b, *b_tmp;
cae688ec 7037
35df4500 7038 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6c95b8df
PA
7039 if (b->type == bp_shlib_event
7040 && b->loc->pspace == current_program_space)
cae688ec
JJ
7041 delete_breakpoint (b);
7042}
7043
7044struct breakpoint *
a6d9a66e 7045create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
cae688ec
JJ
7046{
7047 struct breakpoint *b;
7048
06edf0c0
PA
7049 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7050 &internal_breakpoint_ops);
b60e7edf 7051 update_global_location_list_nothrow (1);
cae688ec
JJ
7052 return b;
7053}
7054
7055/* Disable any breakpoints that are on code in shared libraries. Only
7056 apply to enabled breakpoints, disabled ones can just stay disabled. */
7057
7058void
cb851954 7059disable_breakpoints_in_shlibs (void)
cae688ec 7060{
876fa593 7061 struct bp_location *loc, **locp_tmp;
cae688ec 7062
876fa593 7063 ALL_BP_LOCATIONS (loc, locp_tmp)
cae688ec 7064 {
2bdf28a0 7065 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7066 struct breakpoint *b = loc->owner;
2bdf28a0 7067
4a64f543
MS
7068 /* We apply the check to all breakpoints, including disabled for
7069 those with loc->duplicate set. This is so that when breakpoint
7070 becomes enabled, or the duplicate is removed, gdb will try to
7071 insert all breakpoints. If we don't set shlib_disabled here,
7072 we'll try to insert those breakpoints and fail. */
1042e4c0 7073 if (((b->type == bp_breakpoint)
508ccb1f 7074 || (b->type == bp_jit_event)
1042e4c0 7075 || (b->type == bp_hardware_breakpoint)
d77f58be 7076 || (is_tracepoint (b)))
6c95b8df 7077 && loc->pspace == current_program_space
0d381245 7078 && !loc->shlib_disabled
a77053c2 7079#ifdef PC_SOLIB
0d381245 7080 && PC_SOLIB (loc->address)
a77053c2 7081#else
6c95b8df 7082 && solib_name_from_address (loc->pspace, loc->address)
a77053c2
MK
7083#endif
7084 )
0d381245
VP
7085 {
7086 loc->shlib_disabled = 1;
7087 }
cae688ec
JJ
7088 }
7089}
7090
1e4d1764
YQ
7091/* Disable any breakpoints and tracepoints that are in an unloaded shared
7092 library. Only apply to enabled breakpoints, disabled ones can just stay
4a64f543 7093 disabled. */
84acb35a 7094
75149521 7095static void
84acb35a
JJ
7096disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7097{
876fa593 7098 struct bp_location *loc, **locp_tmp;
84acb35a
JJ
7099 int disabled_shlib_breaks = 0;
7100
c86cf029
VP
7101 /* SunOS a.out shared libraries are always mapped, so do not
7102 disable breakpoints; they will only be reported as unloaded
7103 through clear_solib when GDB discards its shared library
7104 list. See clear_solib for more information. */
7105 if (exec_bfd != NULL
7106 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7107 return;
7108
876fa593 7109 ALL_BP_LOCATIONS (loc, locp_tmp)
84acb35a 7110 {
2bdf28a0 7111 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
0d381245 7112 struct breakpoint *b = loc->owner;
cc59ec59 7113
1e4d1764 7114 if (solib->pspace == loc->pspace
e2dd7057 7115 && !loc->shlib_disabled
1e4d1764
YQ
7116 && (((b->type == bp_breakpoint
7117 || b->type == bp_jit_event
7118 || b->type == bp_hardware_breakpoint)
7119 && (loc->loc_type == bp_loc_hardware_breakpoint
7120 || loc->loc_type == bp_loc_software_breakpoint))
7121 || is_tracepoint (b))
e2dd7057 7122 && solib_contains_address_p (solib, loc->address))
84acb35a 7123 {
e2dd7057
PP
7124 loc->shlib_disabled = 1;
7125 /* At this point, we cannot rely on remove_breakpoint
7126 succeeding so we must mark the breakpoint as not inserted
7127 to prevent future errors occurring in remove_breakpoints. */
7128 loc->inserted = 0;
8d3788bd
VP
7129
7130 /* This may cause duplicate notifications for the same breakpoint. */
7131 observer_notify_breakpoint_modified (b);
7132
e2dd7057
PP
7133 if (!disabled_shlib_breaks)
7134 {
7135 target_terminal_ours_for_output ();
3e43a32a
MS
7136 warning (_("Temporarily disabling breakpoints "
7137 "for unloaded shared library \"%s\""),
e2dd7057 7138 solib->so_name);
84acb35a 7139 }
e2dd7057 7140 disabled_shlib_breaks = 1;
84acb35a
JJ
7141 }
7142 }
84acb35a
JJ
7143}
7144
ce78b96d
JB
7145/* FORK & VFORK catchpoints. */
7146
e29a4733
PA
7147/* An instance of this type is used to represent a fork or vfork
7148 catchpoint. It includes a "struct breakpoint" as a kind of base
7149 class; users downcast to "struct breakpoint *" when needed. A
7150 breakpoint is really of this type iff its ops pointer points to
7151 CATCH_FORK_BREAKPOINT_OPS. */
7152
7153struct fork_catchpoint
7154{
7155 /* The base class. */
7156 struct breakpoint base;
7157
7158 /* Process id of a child process whose forking triggered this
7159 catchpoint. This field is only valid immediately after this
7160 catchpoint has triggered. */
7161 ptid_t forked_inferior_pid;
7162};
7163
4a64f543
MS
7164/* Implement the "insert" breakpoint_ops method for fork
7165 catchpoints. */
ce78b96d 7166
77b06cd7
TJB
7167static int
7168insert_catch_fork (struct bp_location *bl)
ce78b96d 7169{
77b06cd7 7170 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
7171}
7172
4a64f543
MS
7173/* Implement the "remove" breakpoint_ops method for fork
7174 catchpoints. */
ce78b96d
JB
7175
7176static int
77b06cd7 7177remove_catch_fork (struct bp_location *bl)
ce78b96d
JB
7178{
7179 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
7180}
7181
7182/* Implement the "breakpoint_hit" breakpoint_ops method for fork
7183 catchpoints. */
7184
7185static int
f1310107 7186breakpoint_hit_catch_fork (const struct bp_location *bl,
09ac7c10
TT
7187 struct address_space *aspace, CORE_ADDR bp_addr,
7188 const struct target_waitstatus *ws)
ce78b96d 7189{
e29a4733
PA
7190 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7191
f90263c1
TT
7192 if (ws->kind != TARGET_WAITKIND_FORKED)
7193 return 0;
7194
7195 c->forked_inferior_pid = ws->value.related_pid;
7196 return 1;
ce78b96d
JB
7197}
7198
4a64f543
MS
7199/* Implement the "print_it" breakpoint_ops method for fork
7200 catchpoints. */
ce78b96d
JB
7201
7202static enum print_stop_action
348d480f 7203print_it_catch_fork (bpstat bs)
ce78b96d 7204{
36dfb11c 7205 struct ui_out *uiout = current_uiout;
348d480f
PA
7206 struct breakpoint *b = bs->breakpoint_at;
7207 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
e29a4733 7208
ce78b96d 7209 annotate_catchpoint (b->number);
36dfb11c
TT
7210 if (b->disposition == disp_del)
7211 ui_out_text (uiout, "\nTemporary catchpoint ");
7212 else
7213 ui_out_text (uiout, "\nCatchpoint ");
7214 if (ui_out_is_mi_like_p (uiout))
7215 {
7216 ui_out_field_string (uiout, "reason",
7217 async_reason_lookup (EXEC_ASYNC_FORK));
7218 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7219 }
7220 ui_out_field_int (uiout, "bkptno", b->number);
7221 ui_out_text (uiout, " (forked process ");
7222 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7223 ui_out_text (uiout, "), ");
ce78b96d
JB
7224 return PRINT_SRC_AND_LOC;
7225}
7226
4a64f543
MS
7227/* Implement the "print_one" breakpoint_ops method for fork
7228 catchpoints. */
ce78b96d
JB
7229
7230static void
a6d9a66e 7231print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7232{
e29a4733 7233 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7234 struct value_print_options opts;
79a45e25 7235 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7236
7237 get_user_print_options (&opts);
7238
4a64f543
MS
7239 /* Field 4, the address, is omitted (which makes the columns not
7240 line up too nicely with the headers, but the effect is relatively
7241 readable). */
79a45b7d 7242 if (opts.addressprint)
ce78b96d
JB
7243 ui_out_field_skip (uiout, "addr");
7244 annotate_field (5);
7245 ui_out_text (uiout, "fork");
e29a4733 7246 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7247 {
7248 ui_out_text (uiout, ", process ");
7249 ui_out_field_int (uiout, "what",
e29a4733 7250 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7251 ui_out_spaces (uiout, 1);
7252 }
7253}
7254
7255/* Implement the "print_mention" breakpoint_ops method for fork
7256 catchpoints. */
7257
7258static void
7259print_mention_catch_fork (struct breakpoint *b)
7260{
7261 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7262}
7263
6149aea9
PA
7264/* Implement the "print_recreate" breakpoint_ops method for fork
7265 catchpoints. */
7266
7267static void
7268print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7269{
7270 fprintf_unfiltered (fp, "catch fork");
d9b3f62e 7271 print_recreate_thread (b, fp);
6149aea9
PA
7272}
7273
ce78b96d
JB
7274/* The breakpoint_ops structure to be used in fork catchpoints. */
7275
2060206e 7276static struct breakpoint_ops catch_fork_breakpoint_ops;
ce78b96d 7277
4a64f543
MS
7278/* Implement the "insert" breakpoint_ops method for vfork
7279 catchpoints. */
ce78b96d 7280
77b06cd7
TJB
7281static int
7282insert_catch_vfork (struct bp_location *bl)
ce78b96d 7283{
77b06cd7 7284 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
ce78b96d
JB
7285}
7286
4a64f543
MS
7287/* Implement the "remove" breakpoint_ops method for vfork
7288 catchpoints. */
ce78b96d
JB
7289
7290static int
77b06cd7 7291remove_catch_vfork (struct bp_location *bl)
ce78b96d
JB
7292{
7293 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
7294}
7295
7296/* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7297 catchpoints. */
7298
7299static int
f1310107 7300breakpoint_hit_catch_vfork (const struct bp_location *bl,
09ac7c10
TT
7301 struct address_space *aspace, CORE_ADDR bp_addr,
7302 const struct target_waitstatus *ws)
ce78b96d 7303{
e29a4733
PA
7304 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7305
f90263c1
TT
7306 if (ws->kind != TARGET_WAITKIND_VFORKED)
7307 return 0;
7308
7309 c->forked_inferior_pid = ws->value.related_pid;
7310 return 1;
ce78b96d
JB
7311}
7312
4a64f543
MS
7313/* Implement the "print_it" breakpoint_ops method for vfork
7314 catchpoints. */
ce78b96d
JB
7315
7316static enum print_stop_action
348d480f 7317print_it_catch_vfork (bpstat bs)
ce78b96d 7318{
36dfb11c 7319 struct ui_out *uiout = current_uiout;
348d480f 7320 struct breakpoint *b = bs->breakpoint_at;
e29a4733
PA
7321 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7322
ce78b96d 7323 annotate_catchpoint (b->number);
36dfb11c
TT
7324 if (b->disposition == disp_del)
7325 ui_out_text (uiout, "\nTemporary catchpoint ");
7326 else
7327 ui_out_text (uiout, "\nCatchpoint ");
7328 if (ui_out_is_mi_like_p (uiout))
7329 {
7330 ui_out_field_string (uiout, "reason",
7331 async_reason_lookup (EXEC_ASYNC_VFORK));
7332 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7333 }
7334 ui_out_field_int (uiout, "bkptno", b->number);
7335 ui_out_text (uiout, " (vforked process ");
7336 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7337 ui_out_text (uiout, "), ");
ce78b96d
JB
7338 return PRINT_SRC_AND_LOC;
7339}
7340
4a64f543
MS
7341/* Implement the "print_one" breakpoint_ops method for vfork
7342 catchpoints. */
ce78b96d
JB
7343
7344static void
a6d9a66e 7345print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
ce78b96d 7346{
e29a4733 7347 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
79a45b7d 7348 struct value_print_options opts;
79a45e25 7349 struct ui_out *uiout = current_uiout;
79a45b7d
TT
7350
7351 get_user_print_options (&opts);
4a64f543
MS
7352 /* Field 4, the address, is omitted (which makes the columns not
7353 line up too nicely with the headers, but the effect is relatively
7354 readable). */
79a45b7d 7355 if (opts.addressprint)
ce78b96d
JB
7356 ui_out_field_skip (uiout, "addr");
7357 annotate_field (5);
7358 ui_out_text (uiout, "vfork");
e29a4733 7359 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
ce78b96d
JB
7360 {
7361 ui_out_text (uiout, ", process ");
7362 ui_out_field_int (uiout, "what",
e29a4733 7363 ptid_get_pid (c->forked_inferior_pid));
ce78b96d
JB
7364 ui_out_spaces (uiout, 1);
7365 }
7366}
7367
7368/* Implement the "print_mention" breakpoint_ops method for vfork
7369 catchpoints. */
7370
7371static void
7372print_mention_catch_vfork (struct breakpoint *b)
7373{
7374 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7375}
7376
6149aea9
PA
7377/* Implement the "print_recreate" breakpoint_ops method for vfork
7378 catchpoints. */
7379
7380static void
7381print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7382{
7383 fprintf_unfiltered (fp, "catch vfork");
d9b3f62e 7384 print_recreate_thread (b, fp);
6149aea9
PA
7385}
7386
ce78b96d
JB
7387/* The breakpoint_ops structure to be used in vfork catchpoints. */
7388
2060206e 7389static struct breakpoint_ops catch_vfork_breakpoint_ops;
ce78b96d 7390
edcc5120
TT
7391/* An instance of this type is used to represent an solib catchpoint.
7392 It includes a "struct breakpoint" as a kind of base class; users
7393 downcast to "struct breakpoint *" when needed. A breakpoint is
7394 really of this type iff its ops pointer points to
7395 CATCH_SOLIB_BREAKPOINT_OPS. */
7396
7397struct solib_catchpoint
7398{
7399 /* The base class. */
7400 struct breakpoint base;
7401
7402 /* True for "catch load", false for "catch unload". */
7403 unsigned char is_load;
7404
7405 /* Regular expression to match, if any. COMPILED is only valid when
7406 REGEX is non-NULL. */
7407 char *regex;
7408 regex_t compiled;
7409};
7410
7411static void
7412dtor_catch_solib (struct breakpoint *b)
7413{
7414 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7415
7416 if (self->regex)
7417 regfree (&self->compiled);
7418 xfree (self->regex);
7419
7420 base_breakpoint_ops.dtor (b);
7421}
7422
7423static int
7424insert_catch_solib (struct bp_location *ignore)
7425{
7426 return 0;
7427}
7428
7429static int
7430remove_catch_solib (struct bp_location *ignore)
7431{
7432 return 0;
7433}
7434
7435static int
7436breakpoint_hit_catch_solib (const struct bp_location *bl,
7437 struct address_space *aspace,
7438 CORE_ADDR bp_addr,
7439 const struct target_waitstatus *ws)
7440{
7441 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7442 struct breakpoint *other;
7443
7444 if (ws->kind == TARGET_WAITKIND_LOADED)
7445 return 1;
7446
7447 ALL_BREAKPOINTS (other)
7448 {
7449 struct bp_location *other_bl;
7450
7451 if (other == bl->owner)
7452 continue;
7453
7454 if (other->type != bp_shlib_event)
7455 continue;
7456
7457 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7458 continue;
7459
7460 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7461 {
7462 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7463 return 1;
7464 }
7465 }
7466
7467 return 0;
7468}
7469
7470static void
7471check_status_catch_solib (struct bpstats *bs)
7472{
7473 struct solib_catchpoint *self
7474 = (struct solib_catchpoint *) bs->breakpoint_at;
7475 int ix;
7476
7477 if (self->is_load)
7478 {
7479 struct so_list *iter;
7480
7481 for (ix = 0;
7482 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
7483 ix, iter);
7484 ++ix)
7485 {
7486 if (!self->regex
7487 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
7488 return;
7489 }
7490 }
7491 else
7492 {
7493 char *iter;
7494
7495 for (ix = 0;
7496 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
7497 ix, iter);
7498 ++ix)
7499 {
7500 if (!self->regex
7501 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
7502 return;
7503 }
7504 }
7505
7506 bs->stop = 0;
7507 bs->print_it = print_it_noop;
7508}
7509
7510static enum print_stop_action
7511print_it_catch_solib (bpstat bs)
7512{
7513 struct breakpoint *b = bs->breakpoint_at;
7514 struct ui_out *uiout = current_uiout;
7515
7516 annotate_catchpoint (b->number);
7517 if (b->disposition == disp_del)
7518 ui_out_text (uiout, "\nTemporary catchpoint ");
7519 else
7520 ui_out_text (uiout, "\nCatchpoint ");
7521 ui_out_field_int (uiout, "bkptno", b->number);
7522 ui_out_text (uiout, "\n");
7523 if (ui_out_is_mi_like_p (uiout))
7524 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7525 print_solib_event (1);
7526 return PRINT_SRC_AND_LOC;
7527}
7528
7529static void
7530print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7531{
7532 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7533 struct value_print_options opts;
7534 struct ui_out *uiout = current_uiout;
7535 char *msg;
7536
7537 get_user_print_options (&opts);
7538 /* Field 4, the address, is omitted (which makes the columns not
7539 line up too nicely with the headers, but the effect is relatively
7540 readable). */
7541 if (opts.addressprint)
7542 {
7543 annotate_field (4);
7544 ui_out_field_skip (uiout, "addr");
7545 }
7546
7547 annotate_field (5);
7548 if (self->is_load)
7549 {
7550 if (self->regex)
7551 msg = xstrprintf (_("load of library matching %s"), self->regex);
7552 else
7553 msg = xstrdup (_("load of library"));
7554 }
7555 else
7556 {
7557 if (self->regex)
7558 msg = xstrprintf (_("unload of library matching %s"), self->regex);
7559 else
7560 msg = xstrdup (_("unload of library"));
7561 }
7562 ui_out_field_string (uiout, "what", msg);
7563 xfree (msg);
7564}
7565
7566static void
7567print_mention_catch_solib (struct breakpoint *b)
7568{
7569 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7570
7571 printf_filtered (_("Catchpoint %d (%s)"), b->number,
7572 self->is_load ? "load" : "unload");
7573}
7574
7575static void
7576print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
7577{
7578 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7579
7580 fprintf_unfiltered (fp, "%s %s",
7581 b->disposition == disp_del ? "tcatch" : "catch",
7582 self->is_load ? "load" : "unload");
7583 if (self->regex)
7584 fprintf_unfiltered (fp, " %s", self->regex);
7585 fprintf_unfiltered (fp, "\n");
7586}
7587
7588static struct breakpoint_ops catch_solib_breakpoint_ops;
7589
7590/* A helper function that does all the work for "catch load" and
7591 "catch unload". */
7592
7593static void
7594catch_load_or_unload (char *arg, int from_tty, int is_load,
7595 struct cmd_list_element *command)
7596{
7597 struct solib_catchpoint *c;
7598 struct gdbarch *gdbarch = get_current_arch ();
7599 int tempflag;
edcc5120
TT
7600 struct cleanup *cleanup;
7601
7602 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7603
7604 if (!arg)
7605 arg = "";
7606 arg = skip_spaces (arg);
7607
7608 c = XCNEW (struct solib_catchpoint);
7609 cleanup = make_cleanup (xfree, c);
7610
7611 if (*arg != '\0')
7612 {
7613 int errcode;
7614
7615 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
7616 if (errcode != 0)
7617 {
7618 char *err = get_regcomp_error (errcode, &c->compiled);
7619
7620 make_cleanup (xfree, err);
7621 error (_("Invalid regexp (%s): %s"), err, arg);
7622 }
7623 c->regex = xstrdup (arg);
7624 }
7625
7626 c->is_load = is_load;
7627 init_catchpoint (&c->base, gdbarch, tempflag, NULL,
7628 &catch_solib_breakpoint_ops);
7629
7630 discard_cleanups (cleanup);
7631 install_breakpoint (0, &c->base, 1);
7632}
7633
7634static void
7635catch_load_command_1 (char *arg, int from_tty,
7636 struct cmd_list_element *command)
7637{
7638 catch_load_or_unload (arg, from_tty, 1, command);
7639}
7640
7641static void
7642catch_unload_command_1 (char *arg, int from_tty,
7643 struct cmd_list_element *command)
7644{
7645 catch_load_or_unload (arg, from_tty, 0, command);
7646}
7647
fa3064dd
YQ
7648DEF_VEC_I(int);
7649
be5c67c1
PA
7650/* An instance of this type is used to represent a syscall catchpoint.
7651 It includes a "struct breakpoint" as a kind of base class; users
7652 downcast to "struct breakpoint *" when needed. A breakpoint is
7653 really of this type iff its ops pointer points to
7654 CATCH_SYSCALL_BREAKPOINT_OPS. */
7655
7656struct syscall_catchpoint
7657{
7658 /* The base class. */
7659 struct breakpoint base;
7660
7661 /* Syscall numbers used for the 'catch syscall' feature. If no
7662 syscall has been specified for filtering, its value is NULL.
7663 Otherwise, it holds a list of all syscalls to be caught. The
7664 list elements are allocated with xmalloc. */
7665 VEC(int) *syscalls_to_be_caught;
7666};
7667
7668/* Implement the "dtor" breakpoint_ops method for syscall
7669 catchpoints. */
7670
7671static void
7672dtor_catch_syscall (struct breakpoint *b)
7673{
7674 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7675
7676 VEC_free (int, c->syscalls_to_be_caught);
348d480f 7677
2060206e 7678 base_breakpoint_ops.dtor (b);
be5c67c1
PA
7679}
7680
fa3064dd
YQ
7681static const struct inferior_data *catch_syscall_inferior_data = NULL;
7682
7683struct catch_syscall_inferior_data
7684{
7685 /* We keep a count of the number of times the user has requested a
7686 particular syscall to be tracked, and pass this information to the
7687 target. This lets capable targets implement filtering directly. */
7688
7689 /* Number of times that "any" syscall is requested. */
7690 int any_syscall_count;
7691
7692 /* Count of each system call. */
7693 VEC(int) *syscalls_counts;
7694
7695 /* This counts all syscall catch requests, so we can readily determine
7696 if any catching is necessary. */
7697 int total_syscalls_count;
7698};
7699
7700static struct catch_syscall_inferior_data*
7701get_catch_syscall_inferior_data (struct inferior *inf)
7702{
7703 struct catch_syscall_inferior_data *inf_data;
7704
7705 inf_data = inferior_data (inf, catch_syscall_inferior_data);
7706 if (inf_data == NULL)
7707 {
7708 inf_data = XZALLOC (struct catch_syscall_inferior_data);
7709 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
7710 }
7711
7712 return inf_data;
7713}
7714
7715static void
7716catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
7717{
7718 xfree (arg);
7719}
7720
7721
a96d9b2e
SDJ
7722/* Implement the "insert" breakpoint_ops method for syscall
7723 catchpoints. */
7724
77b06cd7
TJB
7725static int
7726insert_catch_syscall (struct bp_location *bl)
a96d9b2e 7727{
be5c67c1 7728 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 7729 struct inferior *inf = current_inferior ();
fa3064dd
YQ
7730 struct catch_syscall_inferior_data *inf_data
7731 = get_catch_syscall_inferior_data (inf);
a96d9b2e 7732
fa3064dd 7733 ++inf_data->total_syscalls_count;
be5c67c1 7734 if (!c->syscalls_to_be_caught)
fa3064dd 7735 ++inf_data->any_syscall_count;
a96d9b2e
SDJ
7736 else
7737 {
7738 int i, iter;
cc59ec59 7739
a96d9b2e 7740 for (i = 0;
be5c67c1 7741 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7742 i++)
7743 {
7744 int elem;
cc59ec59 7745
fa3064dd 7746 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e 7747 {
fa3064dd 7748 int old_size = VEC_length (int, inf_data->syscalls_counts);
3e43a32a
MS
7749 uintptr_t vec_addr_offset
7750 = old_size * ((uintptr_t) sizeof (int));
a96d9b2e 7751 uintptr_t vec_addr;
fa3064dd
YQ
7752 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
7753 vec_addr = ((uintptr_t) VEC_address (int,
7754 inf_data->syscalls_counts)
7755 + vec_addr_offset);
a96d9b2e
SDJ
7756 memset ((void *) vec_addr, 0,
7757 (iter + 1 - old_size) * sizeof (int));
7758 }
fa3064dd
YQ
7759 elem = VEC_index (int, inf_data->syscalls_counts, iter);
7760 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
a96d9b2e
SDJ
7761 }
7762 }
7763
77b06cd7 7764 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
fa3064dd
YQ
7765 inf_data->total_syscalls_count != 0,
7766 inf_data->any_syscall_count,
7767 VEC_length (int,
7768 inf_data->syscalls_counts),
7769 VEC_address (int,
7770 inf_data->syscalls_counts));
a96d9b2e
SDJ
7771}
7772
7773/* Implement the "remove" breakpoint_ops method for syscall
7774 catchpoints. */
7775
7776static int
77b06cd7 7777remove_catch_syscall (struct bp_location *bl)
a96d9b2e 7778{
be5c67c1 7779 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
a96d9b2e 7780 struct inferior *inf = current_inferior ();
fa3064dd
YQ
7781 struct catch_syscall_inferior_data *inf_data
7782 = get_catch_syscall_inferior_data (inf);
a96d9b2e 7783
fa3064dd 7784 --inf_data->total_syscalls_count;
be5c67c1 7785 if (!c->syscalls_to_be_caught)
fa3064dd 7786 --inf_data->any_syscall_count;
a96d9b2e
SDJ
7787 else
7788 {
7789 int i, iter;
cc59ec59 7790
a96d9b2e 7791 for (i = 0;
be5c67c1 7792 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7793 i++)
7794 {
7795 int elem;
fa3064dd 7796 if (iter >= VEC_length (int, inf_data->syscalls_counts))
a96d9b2e
SDJ
7797 /* Shouldn't happen. */
7798 continue;
fa3064dd
YQ
7799 elem = VEC_index (int, inf_data->syscalls_counts, iter);
7800 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
a96d9b2e
SDJ
7801 }
7802 }
7803
7804 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
fa3064dd
YQ
7805 inf_data->total_syscalls_count != 0,
7806 inf_data->any_syscall_count,
7807 VEC_length (int,
7808 inf_data->syscalls_counts),
3e43a32a 7809 VEC_address (int,
fa3064dd 7810 inf_data->syscalls_counts));
a96d9b2e
SDJ
7811}
7812
7813/* Implement the "breakpoint_hit" breakpoint_ops method for syscall
7814 catchpoints. */
7815
7816static int
f1310107 7817breakpoint_hit_catch_syscall (const struct bp_location *bl,
09ac7c10
TT
7818 struct address_space *aspace, CORE_ADDR bp_addr,
7819 const struct target_waitstatus *ws)
a96d9b2e 7820{
4a64f543
MS
7821 /* We must check if we are catching specific syscalls in this
7822 breakpoint. If we are, then we must guarantee that the called
7823 syscall is the same syscall we are catching. */
a96d9b2e 7824 int syscall_number = 0;
be5c67c1
PA
7825 const struct syscall_catchpoint *c
7826 = (const struct syscall_catchpoint *) bl->owner;
a96d9b2e 7827
f90263c1
TT
7828 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
7829 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
a96d9b2e
SDJ
7830 return 0;
7831
f90263c1
TT
7832 syscall_number = ws->value.syscall_number;
7833
a96d9b2e 7834 /* Now, checking if the syscall is the same. */
be5c67c1 7835 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7836 {
7837 int i, iter;
cc59ec59 7838
a96d9b2e 7839 for (i = 0;
be5c67c1 7840 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7841 i++)
7842 if (syscall_number == iter)
7843 break;
7844 /* Not the same. */
7845 if (!iter)
7846 return 0;
7847 }
7848
7849 return 1;
7850}
7851
7852/* Implement the "print_it" breakpoint_ops method for syscall
7853 catchpoints. */
7854
7855static enum print_stop_action
348d480f 7856print_it_catch_syscall (bpstat bs)
a96d9b2e 7857{
36dfb11c 7858 struct ui_out *uiout = current_uiout;
348d480f 7859 struct breakpoint *b = bs->breakpoint_at;
a96d9b2e
SDJ
7860 /* These are needed because we want to know in which state a
7861 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
7862 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
7863 must print "called syscall" or "returned from syscall". */
7864 ptid_t ptid;
7865 struct target_waitstatus last;
7866 struct syscall s;
a96d9b2e
SDJ
7867
7868 get_last_target_status (&ptid, &last);
7869
7870 get_syscall_by_number (last.value.syscall_number, &s);
7871
7872 annotate_catchpoint (b->number);
7873
36dfb11c
TT
7874 if (b->disposition == disp_del)
7875 ui_out_text (uiout, "\nTemporary catchpoint ");
a96d9b2e 7876 else
36dfb11c
TT
7877 ui_out_text (uiout, "\nCatchpoint ");
7878 if (ui_out_is_mi_like_p (uiout))
7879 {
7880 ui_out_field_string (uiout, "reason",
7881 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
7882 ? EXEC_ASYNC_SYSCALL_ENTRY
7883 : EXEC_ASYNC_SYSCALL_RETURN));
7884 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7885 }
7886 ui_out_field_int (uiout, "bkptno", b->number);
a96d9b2e
SDJ
7887
7888 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
36dfb11c
TT
7889 ui_out_text (uiout, " (call to syscall ");
7890 else
7891 ui_out_text (uiout, " (returned from syscall ");
a96d9b2e 7892
36dfb11c
TT
7893 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
7894 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
7895 if (s.name != NULL)
7896 ui_out_field_string (uiout, "syscall-name", s.name);
7897
7898 ui_out_text (uiout, "), ");
a96d9b2e
SDJ
7899
7900 return PRINT_SRC_AND_LOC;
7901}
7902
7903/* Implement the "print_one" breakpoint_ops method for syscall
7904 catchpoints. */
7905
7906static void
7907print_one_catch_syscall (struct breakpoint *b,
f1310107 7908 struct bp_location **last_loc)
a96d9b2e 7909{
be5c67c1 7910 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
a96d9b2e 7911 struct value_print_options opts;
79a45e25 7912 struct ui_out *uiout = current_uiout;
a96d9b2e
SDJ
7913
7914 get_user_print_options (&opts);
4a64f543
MS
7915 /* Field 4, the address, is omitted (which makes the columns not
7916 line up too nicely with the headers, but the effect is relatively
7917 readable). */
a96d9b2e
SDJ
7918 if (opts.addressprint)
7919 ui_out_field_skip (uiout, "addr");
7920 annotate_field (5);
7921
be5c67c1
PA
7922 if (c->syscalls_to_be_caught
7923 && VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
7924 ui_out_text (uiout, "syscalls \"");
7925 else
7926 ui_out_text (uiout, "syscall \"");
7927
be5c67c1 7928 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7929 {
7930 int i, iter;
7931 char *text = xstrprintf ("%s", "");
cc59ec59 7932
a96d9b2e 7933 for (i = 0;
be5c67c1 7934 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7935 i++)
7936 {
7937 char *x = text;
7938 struct syscall s;
7939 get_syscall_by_number (iter, &s);
7940
7941 if (s.name != NULL)
7942 text = xstrprintf ("%s%s, ", text, s.name);
7943 else
7944 text = xstrprintf ("%s%d, ", text, iter);
7945
7946 /* We have to xfree the last 'text' (now stored at 'x')
e5dd4106 7947 because xstrprintf dynamically allocates new space for it
a96d9b2e
SDJ
7948 on every call. */
7949 xfree (x);
7950 }
7951 /* Remove the last comma. */
7952 text[strlen (text) - 2] = '\0';
7953 ui_out_field_string (uiout, "what", text);
7954 }
7955 else
7956 ui_out_field_string (uiout, "what", "<any syscall>");
7957 ui_out_text (uiout, "\" ");
7958}
7959
7960/* Implement the "print_mention" breakpoint_ops method for syscall
7961 catchpoints. */
7962
7963static void
7964print_mention_catch_syscall (struct breakpoint *b)
7965{
be5c67c1
PA
7966 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7967
7968 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
7969 {
7970 int i, iter;
7971
be5c67c1 7972 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
a96d9b2e
SDJ
7973 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
7974 else
7975 printf_filtered (_("Catchpoint %d (syscall"), b->number);
7976
7977 for (i = 0;
be5c67c1 7978 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
7979 i++)
7980 {
7981 struct syscall s;
7982 get_syscall_by_number (iter, &s);
7983
7984 if (s.name)
7985 printf_filtered (" '%s' [%d]", s.name, s.number);
7986 else
7987 printf_filtered (" %d", s.number);
7988 }
7989 printf_filtered (")");
7990 }
7991 else
7992 printf_filtered (_("Catchpoint %d (any syscall)"),
7993 b->number);
7994}
7995
6149aea9
PA
7996/* Implement the "print_recreate" breakpoint_ops method for syscall
7997 catchpoints. */
7998
7999static void
8000print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8001{
be5c67c1
PA
8002 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8003
6149aea9
PA
8004 fprintf_unfiltered (fp, "catch syscall");
8005
be5c67c1 8006 if (c->syscalls_to_be_caught)
6149aea9
PA
8007 {
8008 int i, iter;
8009
8010 for (i = 0;
be5c67c1 8011 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
6149aea9
PA
8012 i++)
8013 {
8014 struct syscall s;
8015
8016 get_syscall_by_number (iter, &s);
8017 if (s.name)
8018 fprintf_unfiltered (fp, " %s", s.name);
8019 else
8020 fprintf_unfiltered (fp, " %d", s.number);
8021 }
8022 }
d9b3f62e 8023 print_recreate_thread (b, fp);
6149aea9
PA
8024}
8025
a96d9b2e
SDJ
8026/* The breakpoint_ops structure to be used in syscall catchpoints. */
8027
2060206e 8028static struct breakpoint_ops catch_syscall_breakpoint_ops;
a96d9b2e
SDJ
8029
8030/* Returns non-zero if 'b' is a syscall catchpoint. */
8031
8032static int
8033syscall_catchpoint_p (struct breakpoint *b)
8034{
8035 return (b->ops == &catch_syscall_breakpoint_ops);
8036}
8037
346774a9
PA
8038/* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8039 is non-zero, then make the breakpoint temporary. If COND_STRING is
8040 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8041 the breakpoint_ops structure associated to the catchpoint. */
ce78b96d 8042
346774a9
PA
8043static void
8044init_catchpoint (struct breakpoint *b,
8045 struct gdbarch *gdbarch, int tempflag,
8046 char *cond_string,
c0a91b2b 8047 const struct breakpoint_ops *ops)
c906108c 8048{
c5aa993b 8049 struct symtab_and_line sal;
346774a9 8050
fe39c653 8051 init_sal (&sal);
6c95b8df 8052 sal.pspace = current_program_space;
c5aa993b 8053
28010a5d 8054 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
ce78b96d 8055
1b36a34b 8056 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
b5de0fa7 8057 b->disposition = tempflag ? disp_del : disp_donttouch;
346774a9
PA
8058}
8059
28010a5d 8060void
3ea46bff 8061install_breakpoint (int internal, struct breakpoint *b, int update_gll)
c56053d2
PA
8062{
8063 add_to_breakpoint_chain (b);
3a5c3e22
PA
8064 set_breakpoint_number (internal, b);
8065 if (!internal)
8066 mention (b);
c56053d2 8067 observer_notify_breakpoint_created (b);
3ea46bff
YQ
8068
8069 if (update_gll)
8070 update_global_location_list (1);
c56053d2
PA
8071}
8072
9b70b993 8073static void
a6d9a66e
UW
8074create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8075 int tempflag, char *cond_string,
c0a91b2b 8076 const struct breakpoint_ops *ops)
c906108c 8077{
e29a4733 8078 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
ce78b96d 8079
e29a4733
PA
8080 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8081
8082 c->forked_inferior_pid = null_ptid;
8083
3ea46bff 8084 install_breakpoint (0, &c->base, 1);
c906108c
SS
8085}
8086
fe798b75
JB
8087/* Exec catchpoints. */
8088
b4d90040
PA
8089/* An instance of this type is used to represent an exec catchpoint.
8090 It includes a "struct breakpoint" as a kind of base class; users
8091 downcast to "struct breakpoint *" when needed. A breakpoint is
8092 really of this type iff its ops pointer points to
8093 CATCH_EXEC_BREAKPOINT_OPS. */
8094
8095struct exec_catchpoint
8096{
8097 /* The base class. */
8098 struct breakpoint base;
8099
8100 /* Filename of a program whose exec triggered this catchpoint.
8101 This field is only valid immediately after this catchpoint has
8102 triggered. */
8103 char *exec_pathname;
8104};
8105
8106/* Implement the "dtor" breakpoint_ops method for exec
8107 catchpoints. */
8108
8109static void
8110dtor_catch_exec (struct breakpoint *b)
8111{
8112 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8113
8114 xfree (c->exec_pathname);
348d480f 8115
2060206e 8116 base_breakpoint_ops.dtor (b);
b4d90040
PA
8117}
8118
77b06cd7
TJB
8119static int
8120insert_catch_exec (struct bp_location *bl)
c906108c 8121{
77b06cd7 8122 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
fe798b75 8123}
c906108c 8124
fe798b75 8125static int
77b06cd7 8126remove_catch_exec (struct bp_location *bl)
fe798b75
JB
8127{
8128 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
8129}
c906108c 8130
fe798b75 8131static int
f1310107 8132breakpoint_hit_catch_exec (const struct bp_location *bl,
09ac7c10
TT
8133 struct address_space *aspace, CORE_ADDR bp_addr,
8134 const struct target_waitstatus *ws)
fe798b75 8135{
b4d90040
PA
8136 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8137
f90263c1
TT
8138 if (ws->kind != TARGET_WAITKIND_EXECD)
8139 return 0;
8140
8141 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8142 return 1;
fe798b75 8143}
c906108c 8144
fe798b75 8145static enum print_stop_action
348d480f 8146print_it_catch_exec (bpstat bs)
fe798b75 8147{
36dfb11c 8148 struct ui_out *uiout = current_uiout;
348d480f 8149 struct breakpoint *b = bs->breakpoint_at;
b4d90040
PA
8150 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8151
fe798b75 8152 annotate_catchpoint (b->number);
36dfb11c
TT
8153 if (b->disposition == disp_del)
8154 ui_out_text (uiout, "\nTemporary catchpoint ");
8155 else
8156 ui_out_text (uiout, "\nCatchpoint ");
8157 if (ui_out_is_mi_like_p (uiout))
8158 {
8159 ui_out_field_string (uiout, "reason",
8160 async_reason_lookup (EXEC_ASYNC_EXEC));
8161 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8162 }
8163 ui_out_field_int (uiout, "bkptno", b->number);
8164 ui_out_text (uiout, " (exec'd ");
8165 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8166 ui_out_text (uiout, "), ");
8167
fe798b75 8168 return PRINT_SRC_AND_LOC;
c906108c
SS
8169}
8170
fe798b75 8171static void
a6d9a66e 8172print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
fe798b75 8173{
b4d90040 8174 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
fe798b75 8175 struct value_print_options opts;
79a45e25 8176 struct ui_out *uiout = current_uiout;
fe798b75
JB
8177
8178 get_user_print_options (&opts);
8179
8180 /* Field 4, the address, is omitted (which makes the columns
8181 not line up too nicely with the headers, but the effect
8182 is relatively readable). */
8183 if (opts.addressprint)
8184 ui_out_field_skip (uiout, "addr");
8185 annotate_field (5);
8186 ui_out_text (uiout, "exec");
b4d90040 8187 if (c->exec_pathname != NULL)
fe798b75
JB
8188 {
8189 ui_out_text (uiout, ", program \"");
b4d90040 8190 ui_out_field_string (uiout, "what", c->exec_pathname);
fe798b75
JB
8191 ui_out_text (uiout, "\" ");
8192 }
8193}
8194
8195static void
8196print_mention_catch_exec (struct breakpoint *b)
8197{
8198 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8199}
8200
6149aea9
PA
8201/* Implement the "print_recreate" breakpoint_ops method for exec
8202 catchpoints. */
8203
8204static void
8205print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8206{
8207 fprintf_unfiltered (fp, "catch exec");
d9b3f62e 8208 print_recreate_thread (b, fp);
6149aea9
PA
8209}
8210
2060206e 8211static struct breakpoint_ops catch_exec_breakpoint_ops;
fe798b75 8212
a96d9b2e
SDJ
8213static void
8214create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
c0a91b2b 8215 const struct breakpoint_ops *ops)
a96d9b2e 8216{
be5c67c1 8217 struct syscall_catchpoint *c;
a96d9b2e 8218 struct gdbarch *gdbarch = get_current_arch ();
a96d9b2e 8219
be5c67c1
PA
8220 c = XNEW (struct syscall_catchpoint);
8221 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8222 c->syscalls_to_be_caught = filter;
a96d9b2e 8223
3ea46bff 8224 install_breakpoint (0, &c->base, 1);
a96d9b2e
SDJ
8225}
8226
c906108c 8227static int
fba45db2 8228hw_breakpoint_used_count (void)
c906108c 8229{
c906108c 8230 int i = 0;
f1310107
TJB
8231 struct breakpoint *b;
8232 struct bp_location *bl;
c906108c
SS
8233
8234 ALL_BREAKPOINTS (b)
c5aa993b 8235 {
d6b74ac4 8236 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
f1310107
TJB
8237 for (bl = b->loc; bl; bl = bl->next)
8238 {
8239 /* Special types of hardware breakpoints may use more than
8240 one register. */
348d480f 8241 i += b->ops->resources_needed (bl);
f1310107 8242 }
c5aa993b 8243 }
c906108c
SS
8244
8245 return i;
8246}
8247
a1398e0c
PA
8248/* Returns the resources B would use if it were a hardware
8249 watchpoint. */
8250
c906108c 8251static int
a1398e0c 8252hw_watchpoint_use_count (struct breakpoint *b)
c906108c 8253{
c906108c 8254 int i = 0;
e09342b5 8255 struct bp_location *bl;
c906108c 8256
a1398e0c
PA
8257 if (!breakpoint_enabled (b))
8258 return 0;
8259
8260 for (bl = b->loc; bl; bl = bl->next)
8261 {
8262 /* Special types of hardware watchpoints may use more than
8263 one register. */
8264 i += b->ops->resources_needed (bl);
8265 }
8266
8267 return i;
8268}
8269
8270/* Returns the sum the used resources of all hardware watchpoints of
8271 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8272 the sum of the used resources of all hardware watchpoints of other
8273 types _not_ TYPE. */
8274
8275static int
8276hw_watchpoint_used_count_others (struct breakpoint *except,
8277 enum bptype type, int *other_type_used)
8278{
8279 int i = 0;
8280 struct breakpoint *b;
8281
c906108c
SS
8282 *other_type_used = 0;
8283 ALL_BREAKPOINTS (b)
e09342b5 8284 {
a1398e0c
PA
8285 if (b == except)
8286 continue;
e09342b5
TJB
8287 if (!breakpoint_enabled (b))
8288 continue;
8289
a1398e0c
PA
8290 if (b->type == type)
8291 i += hw_watchpoint_use_count (b);
8292 else if (is_hardware_watchpoint (b))
8293 *other_type_used = 1;
e09342b5
TJB
8294 }
8295
c906108c
SS
8296 return i;
8297}
8298
c906108c 8299void
fba45db2 8300disable_watchpoints_before_interactive_call_start (void)
c906108c 8301{
c5aa993b 8302 struct breakpoint *b;
c906108c
SS
8303
8304 ALL_BREAKPOINTS (b)
c5aa993b 8305 {
cc60f2e3 8306 if (is_watchpoint (b) && breakpoint_enabled (b))
c5aa993b 8307 {
b5de0fa7 8308 b->enable_state = bp_call_disabled;
b60e7edf 8309 update_global_location_list (0);
c5aa993b
JM
8310 }
8311 }
c906108c
SS
8312}
8313
8314void
fba45db2 8315enable_watchpoints_after_interactive_call_stop (void)
c906108c 8316{
c5aa993b 8317 struct breakpoint *b;
c906108c
SS
8318
8319 ALL_BREAKPOINTS (b)
c5aa993b 8320 {
cc60f2e3 8321 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
c5aa993b 8322 {
b5de0fa7 8323 b->enable_state = bp_enabled;
b60e7edf 8324 update_global_location_list (1);
c5aa993b
JM
8325 }
8326 }
c906108c
SS
8327}
8328
8bea4e01
UW
8329void
8330disable_breakpoints_before_startup (void)
8331{
6c95b8df 8332 current_program_space->executing_startup = 1;
f8eba3c6 8333 update_global_location_list (0);
8bea4e01
UW
8334}
8335
8336void
8337enable_breakpoints_after_startup (void)
8338{
6c95b8df 8339 current_program_space->executing_startup = 0;
f8eba3c6 8340 breakpoint_re_set ();
8bea4e01
UW
8341}
8342
c906108c
SS
8343
8344/* Set a breakpoint that will evaporate an end of command
8345 at address specified by SAL.
8346 Restrict it to frame FRAME if FRAME is nonzero. */
8347
8348struct breakpoint *
a6d9a66e
UW
8349set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8350 struct frame_id frame_id, enum bptype type)
c906108c 8351{
52f0bd74 8352 struct breakpoint *b;
edb3359d
DJ
8353
8354 /* If FRAME_ID is valid, it should be a real frame, not an inlined
8355 one. */
8356 gdb_assert (!frame_id_inlined_p (frame_id));
8357
06edf0c0 8358 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
b5de0fa7
EZ
8359 b->enable_state = bp_enabled;
8360 b->disposition = disp_donttouch;
818dd999 8361 b->frame_id = frame_id;
c906108c 8362
4a64f543
MS
8363 /* If we're debugging a multi-threaded program, then we want
8364 momentary breakpoints to be active in only a single thread of
8365 control. */
39f77062
KB
8366 if (in_thread_list (inferior_ptid))
8367 b->thread = pid_to_thread_id (inferior_ptid);
c906108c 8368
b60e7edf 8369 update_global_location_list_nothrow (1);
74960c60 8370
c906108c
SS
8371 return b;
8372}
611c83ae 8373
06edf0c0
PA
8374/* Make a momentary breakpoint based on the master breakpoint ORIG.
8375 The new breakpoint will have type TYPE, and use OPS as it
8376 breakpoint_ops. */
e58b0e63 8377
06edf0c0
PA
8378static struct breakpoint *
8379momentary_breakpoint_from_master (struct breakpoint *orig,
8380 enum bptype type,
c0a91b2b 8381 const struct breakpoint_ops *ops)
e58b0e63
PA
8382{
8383 struct breakpoint *copy;
8384
06edf0c0 8385 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
e58b0e63 8386 copy->loc = allocate_bp_location (copy);
0e30163f 8387 set_breakpoint_location_function (copy->loc, 1);
e58b0e63 8388
a6d9a66e 8389 copy->loc->gdbarch = orig->loc->gdbarch;
e58b0e63
PA
8390 copy->loc->requested_address = orig->loc->requested_address;
8391 copy->loc->address = orig->loc->address;
8392 copy->loc->section = orig->loc->section;
6c95b8df 8393 copy->loc->pspace = orig->loc->pspace;
55aa24fb 8394 copy->loc->probe = orig->loc->probe;
e58b0e63 8395
f8eba3c6
TT
8396 if (orig->loc->source_file != NULL)
8397 copy->loc->source_file = xstrdup (orig->loc->source_file);
e58b0e63 8398
f8eba3c6 8399 copy->loc->line_number = orig->loc->line_number;
e58b0e63
PA
8400 copy->frame_id = orig->frame_id;
8401 copy->thread = orig->thread;
6c95b8df 8402 copy->pspace = orig->pspace;
e58b0e63
PA
8403
8404 copy->enable_state = bp_enabled;
8405 copy->disposition = disp_donttouch;
8406 copy->number = internal_breakpoint_number--;
8407
8408 update_global_location_list_nothrow (0);
8409 return copy;
8410}
8411
06edf0c0
PA
8412/* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8413 ORIG is NULL. */
8414
8415struct breakpoint *
8416clone_momentary_breakpoint (struct breakpoint *orig)
8417{
8418 /* If there's nothing to clone, then return nothing. */
8419 if (orig == NULL)
8420 return NULL;
8421
8422 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8423}
8424
611c83ae 8425struct breakpoint *
a6d9a66e
UW
8426set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8427 enum bptype type)
611c83ae
PA
8428{
8429 struct symtab_and_line sal;
8430
8431 sal = find_pc_line (pc, 0);
8432 sal.pc = pc;
8433 sal.section = find_pc_overlay (pc);
8434 sal.explicit_pc = 1;
8435
a6d9a66e 8436 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
611c83ae 8437}
c906108c 8438\f
c5aa993b 8439
c906108c
SS
8440/* Tell the user we have just set a breakpoint B. */
8441
8442static void
fba45db2 8443mention (struct breakpoint *b)
c906108c 8444{
348d480f 8445 b->ops->print_mention (b);
79a45e25 8446 if (ui_out_is_mi_like_p (current_uiout))
fb40c209 8447 return;
c906108c
SS
8448 printf_filtered ("\n");
8449}
c906108c 8450\f
c5aa993b 8451
0d381245 8452static struct bp_location *
39d61571 8453add_location_to_breakpoint (struct breakpoint *b,
0d381245
VP
8454 const struct symtab_and_line *sal)
8455{
8456 struct bp_location *loc, **tmp;
3742cc8b
YQ
8457 CORE_ADDR adjusted_address;
8458 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8459
8460 if (loc_gdbarch == NULL)
8461 loc_gdbarch = b->gdbarch;
8462
8463 /* Adjust the breakpoint's address prior to allocating a location.
8464 Once we call allocate_bp_location(), that mostly uninitialized
8465 location will be placed on the location chain. Adjustment of the
8466 breakpoint may cause target_read_memory() to be called and we do
8467 not want its scan of the location chain to find a breakpoint and
8468 location that's only been partially initialized. */
8469 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8470 sal->pc, b->type);
0d381245 8471
39d61571 8472 loc = allocate_bp_location (b);
0d381245
VP
8473 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
8474 ;
8475 *tmp = loc;
3742cc8b 8476
0d381245 8477 loc->requested_address = sal->pc;
3742cc8b 8478 loc->address = adjusted_address;
6c95b8df 8479 loc->pspace = sal->pspace;
55aa24fb 8480 loc->probe = sal->probe;
6c95b8df 8481 gdb_assert (loc->pspace != NULL);
0d381245 8482 loc->section = sal->section;
3742cc8b 8483 loc->gdbarch = loc_gdbarch;
f8eba3c6
TT
8484
8485 if (sal->symtab != NULL)
8486 loc->source_file = xstrdup (sal->symtab->filename);
8487 loc->line_number = sal->line;
8488
0e30163f
JK
8489 set_breakpoint_location_function (loc,
8490 sal->explicit_pc || sal->explicit_line);
0d381245
VP
8491 return loc;
8492}
514f746b
AR
8493\f
8494
8495/* Return 1 if LOC is pointing to a permanent breakpoint,
8496 return 0 otherwise. */
8497
8498static int
8499bp_loc_is_permanent (struct bp_location *loc)
8500{
8501 int len;
8502 CORE_ADDR addr;
1afeeb75 8503 const gdb_byte *bpoint;
514f746b 8504 gdb_byte *target_mem;
939c61fa
JK
8505 struct cleanup *cleanup;
8506 int retval = 0;
514f746b
AR
8507
8508 gdb_assert (loc != NULL);
8509
8510 addr = loc->address;
1afeeb75 8511 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
514f746b 8512
939c61fa 8513 /* Software breakpoints unsupported? */
1afeeb75 8514 if (bpoint == NULL)
939c61fa
JK
8515 return 0;
8516
514f746b
AR
8517 target_mem = alloca (len);
8518
939c61fa
JK
8519 /* Enable the automatic memory restoration from breakpoints while
8520 we read the memory. Otherwise we could say about our temporary
8521 breakpoints they are permanent. */
6c95b8df
PA
8522 cleanup = save_current_space_and_thread ();
8523
8524 switch_to_program_space_and_thread (loc->pspace);
8525 make_show_memory_breakpoints_cleanup (0);
939c61fa 8526
514f746b 8527 if (target_read_memory (loc->address, target_mem, len) == 0
1afeeb75 8528 && memcmp (target_mem, bpoint, len) == 0)
939c61fa 8529 retval = 1;
514f746b 8530
939c61fa
JK
8531 do_cleanups (cleanup);
8532
8533 return retval;
514f746b
AR
8534}
8535
e7e0cddf
SS
8536/* The style in which to perform a dynamic printf. This is a user
8537 option because different output options have different tradeoffs;
8538 if GDB does the printing, there is better error handling if there
8539 is a problem with any of the arguments, but using an inferior
8540 function lets you have special-purpose printers and sending of
8541 output to the same place as compiled-in print functions. (Future
8542 styles may include the ability to do a target-side printf.) */
8543
8544static const char dprintf_style_gdb[] = "gdb";
8545static const char dprintf_style_call[] = "call";
8546static const char *const dprintf_style_enums[] = {
8547 dprintf_style_gdb,
8548 dprintf_style_call,
8549 NULL
8550};
8551static const char *dprintf_style = dprintf_style_gdb;
8552
8553/* The function to use for dynamic printf if the preferred style is to
8554 call into the inferior. The value is simply a string that is
8555 copied into the command, so it can be anything that GDB can
8556 evaluate to a callable address, not necessarily a function name. */
8557
8558static char *dprintf_function = "";
8559
8560/* The channel to use for dynamic printf if the preferred style is to
8561 call into the inferior; if a nonempty string, it will be passed to
8562 the call as the first argument, with the format string as the
8563 second. As with the dprintf function, this can be anything that
8564 GDB knows how to evaluate, so in addition to common choices like
8565 "stderr", this could be an app-specific expression like
8566 "mystreams[curlogger]". */
514f746b 8567
e7e0cddf
SS
8568static char *dprintf_channel = "";
8569
8570/* Build a command list for the dprintf corresponding to the current
8571 settings of the dprintf style options. */
8572
8573static void
8574update_dprintf_command_list (struct breakpoint *b)
8575{
8576 char *dprintf_args = b->extra_string;
8577 char *printf_line = NULL;
8578
8579 if (!dprintf_args)
8580 return;
8581
8582 dprintf_args = skip_spaces (dprintf_args);
8583
8584 /* Allow a comma, as it may have terminated a location, but don't
8585 insist on it. */
8586 if (*dprintf_args == ',')
8587 ++dprintf_args;
8588 dprintf_args = skip_spaces (dprintf_args);
8589
8590 if (*dprintf_args != '"')
8591 error (_("Bad format string, missing '\"'."));
8592
8593 if (strcmp (dprintf_style, "gdb") == 0)
8594 printf_line = xstrprintf ("printf %s", dprintf_args);
8595 else if (strcmp (dprintf_style, "call") == 0)
8596 {
8597 if (!dprintf_function)
8598 error (_("No function supplied for dprintf call"));
8599
8600 if (dprintf_channel && strlen (dprintf_channel) > 0)
8601 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8602 dprintf_function,
8603 dprintf_channel,
8604 dprintf_args);
8605 else
8606 printf_line = xstrprintf ("call (void) %s (%s)",
8607 dprintf_function,
8608 dprintf_args);
8609 }
8610 else
8611 internal_error (__FILE__, __LINE__,
8612 _("Invalid dprintf style."));
8613
8614 /* Manufacture a printf/continue sequence. */
8615 if (printf_line)
8616 {
8617 struct command_line *printf_cmd_line, *cont_cmd_line = NULL;
8618
8619 cont_cmd_line = xmalloc (sizeof (struct command_line));
8620 cont_cmd_line->control_type = simple_control;
8621 cont_cmd_line->body_count = 0;
8622 cont_cmd_line->body_list = NULL;
8623 cont_cmd_line->next = NULL;
8624 cont_cmd_line->line = xstrdup ("continue");
8625
8626 printf_cmd_line = xmalloc (sizeof (struct command_line));
8627 printf_cmd_line->control_type = simple_control;
8628 printf_cmd_line->body_count = 0;
8629 printf_cmd_line->body_list = NULL;
8630 printf_cmd_line->next = cont_cmd_line;
8631 printf_cmd_line->line = printf_line;
8632
8633 breakpoint_set_commands (b, printf_cmd_line);
8634 }
8635}
8636
8637/* Update all dprintf commands, making their command lists reflect
8638 current style settings. */
8639
8640static void
8641update_dprintf_commands (char *args, int from_tty,
8642 struct cmd_list_element *c)
8643{
8644 struct breakpoint *b;
8645
8646 ALL_BREAKPOINTS (b)
8647 {
8648 if (b->type == bp_dprintf)
8649 update_dprintf_command_list (b);
8650 }
8651}
c3f6f71d 8652
018d34a4
VP
8653/* Create a breakpoint with SAL as location. Use ADDR_STRING
8654 as textual description of the location, and COND_STRING
db107f19 8655 as condition expression. */
018d34a4
VP
8656
8657static void
d9b3f62e
PA
8658init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8659 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 8660 char *filter, char *cond_string,
e7e0cddf 8661 char *extra_string,
d9b3f62e
PA
8662 enum bptype type, enum bpdisp disposition,
8663 int thread, int task, int ignore_count,
c0a91b2b 8664 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8665 int enabled, int internal, unsigned flags,
8666 int display_canonical)
018d34a4 8667{
0d381245 8668 int i;
018d34a4
VP
8669
8670 if (type == bp_hardware_breakpoint)
8671 {
fbbd034e
AS
8672 int target_resources_ok;
8673
8674 i = hw_breakpoint_used_count ();
8675 target_resources_ok =
8676 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
018d34a4
VP
8677 i + 1, 0);
8678 if (target_resources_ok == 0)
8679 error (_("No hardware breakpoint support in the target."));
8680 else if (target_resources_ok < 0)
8681 error (_("Hardware breakpoints used exceeds limit."));
8682 }
8683
6c95b8df
PA
8684 gdb_assert (sals.nelts > 0);
8685
0d381245
VP
8686 for (i = 0; i < sals.nelts; ++i)
8687 {
8688 struct symtab_and_line sal = sals.sals[i];
8689 struct bp_location *loc;
8690
8691 if (from_tty)
5af949e3
UW
8692 {
8693 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8694 if (!loc_gdbarch)
8695 loc_gdbarch = gdbarch;
8696
8697 describe_other_breakpoints (loc_gdbarch,
6c95b8df 8698 sal.pspace, sal.pc, sal.section, thread);
5af949e3 8699 }
0d381245
VP
8700
8701 if (i == 0)
8702 {
d9b3f62e 8703 init_raw_breakpoint (b, gdbarch, sal, type, ops);
0d381245 8704 b->thread = thread;
4a306c9a 8705 b->task = task;
855a6e68 8706
0d381245 8707 b->cond_string = cond_string;
e7e0cddf 8708 b->extra_string = extra_string;
0d381245 8709 b->ignore_count = ignore_count;
41447f92 8710 b->enable_state = enabled ? bp_enabled : bp_disabled;
0d381245 8711 b->disposition = disposition;
6c95b8df 8712
44f238bb
PA
8713 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8714 b->loc->inserted = 1;
8715
0fb4aa4b
PA
8716 if (type == bp_static_tracepoint)
8717 {
d9b3f62e 8718 struct tracepoint *t = (struct tracepoint *) b;
0fb4aa4b
PA
8719 struct static_tracepoint_marker marker;
8720
983af33b 8721 if (strace_marker_p (b))
0fb4aa4b
PA
8722 {
8723 /* We already know the marker exists, otherwise, we
8724 wouldn't see a sal for it. */
8725 char *p = &addr_string[3];
8726 char *endp;
8727 char *marker_str;
0fb4aa4b 8728
e9cafbcc 8729 p = skip_spaces (p);
0fb4aa4b 8730
e9cafbcc 8731 endp = skip_to_space (p);
0fb4aa4b
PA
8732
8733 marker_str = savestring (p, endp - p);
d9b3f62e 8734 t->static_trace_marker_id = marker_str;
0fb4aa4b 8735
3e43a32a
MS
8736 printf_filtered (_("Probed static tracepoint "
8737 "marker \"%s\"\n"),
d9b3f62e 8738 t->static_trace_marker_id);
0fb4aa4b
PA
8739 }
8740 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8741 {
d9b3f62e 8742 t->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
8743 release_static_tracepoint_marker (&marker);
8744
3e43a32a
MS
8745 printf_filtered (_("Probed static tracepoint "
8746 "marker \"%s\"\n"),
d9b3f62e 8747 t->static_trace_marker_id);
0fb4aa4b
PA
8748 }
8749 else
3e43a32a
MS
8750 warning (_("Couldn't determine the static "
8751 "tracepoint marker to probe"));
0fb4aa4b
PA
8752 }
8753
0d381245
VP
8754 loc = b->loc;
8755 }
8756 else
018d34a4 8757 {
39d61571 8758 loc = add_location_to_breakpoint (b, &sal);
44f238bb
PA
8759 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8760 loc->inserted = 1;
0d381245
VP
8761 }
8762
514f746b
AR
8763 if (bp_loc_is_permanent (loc))
8764 make_breakpoint_permanent (b);
8765
0d381245
VP
8766 if (b->cond_string)
8767 {
8768 char *arg = b->cond_string;
1bb9788d
TT
8769 loc->cond = parse_exp_1 (&arg, loc->address,
8770 block_for_pc (loc->address), 0);
0d381245 8771 if (*arg)
588ae58c 8772 error (_("Garbage '%s' follows condition"), arg);
018d34a4 8773 }
e7e0cddf
SS
8774
8775 /* Dynamic printf requires and uses additional arguments on the
8776 command line, otherwise it's an error. */
8777 if (type == bp_dprintf)
8778 {
8779 if (b->extra_string)
8780 update_dprintf_command_list (b);
8781 else
8782 error (_("Format string required"));
8783 }
8784 else if (b->extra_string)
588ae58c 8785 error (_("Garbage '%s' at end of command"), b->extra_string);
855a6e68 8786 }
018d34a4 8787
56435ebe 8788 b->display_canonical = display_canonical;
018d34a4
VP
8789 if (addr_string)
8790 b->addr_string = addr_string;
8791 else
8792 /* addr_string has to be used or breakpoint_re_set will delete
8793 me. */
5af949e3
UW
8794 b->addr_string
8795 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
f8eba3c6 8796 b->filter = filter;
d9b3f62e 8797}
018d34a4 8798
d9b3f62e
PA
8799static void
8800create_breakpoint_sal (struct gdbarch *gdbarch,
8801 struct symtabs_and_lines sals, char *addr_string,
f8eba3c6 8802 char *filter, char *cond_string,
e7e0cddf 8803 char *extra_string,
d9b3f62e
PA
8804 enum bptype type, enum bpdisp disposition,
8805 int thread, int task, int ignore_count,
c0a91b2b 8806 const struct breakpoint_ops *ops, int from_tty,
44f238bb
PA
8807 int enabled, int internal, unsigned flags,
8808 int display_canonical)
d9b3f62e
PA
8809{
8810 struct breakpoint *b;
8811 struct cleanup *old_chain;
8812
8813 if (is_tracepoint_type (type))
8814 {
8815 struct tracepoint *t;
8816
8817 t = XCNEW (struct tracepoint);
8818 b = &t->base;
8819 }
8820 else
8821 b = XNEW (struct breakpoint);
8822
8823 old_chain = make_cleanup (xfree, b);
8824
8825 init_breakpoint_sal (b, gdbarch,
8826 sals, addr_string,
e7e0cddf 8827 filter, cond_string, extra_string,
d9b3f62e
PA
8828 type, disposition,
8829 thread, task, ignore_count,
8830 ops, from_tty,
44f238bb
PA
8831 enabled, internal, flags,
8832 display_canonical);
d9b3f62e
PA
8833 discard_cleanups (old_chain);
8834
3ea46bff 8835 install_breakpoint (internal, b, 0);
018d34a4
VP
8836}
8837
8838/* Add SALS.nelts breakpoints to the breakpoint table. For each
8839 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8840 value. COND_STRING, if not NULL, specified the condition to be
8841 used for all breakpoints. Essentially the only case where
8842 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8843 function. In that case, it's still not possible to specify
8844 separate conditions for different overloaded functions, so
8845 we take just a single condition string.
8846
c3f6f71d 8847 NOTE: If the function succeeds, the caller is expected to cleanup
018d34a4 8848 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
c3f6f71d
JM
8849 array contents). If the function fails (error() is called), the
8850 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
4a64f543 8851 COND and SALS arrays and each of those arrays contents. */
c906108c
SS
8852
8853static void
8cdf0e15 8854create_breakpoints_sal (struct gdbarch *gdbarch,
7efd8fc2 8855 struct linespec_result *canonical,
e7e0cddf 8856 char *cond_string, char *extra_string,
8cdf0e15
VP
8857 enum bptype type, enum bpdisp disposition,
8858 int thread, int task, int ignore_count,
c0a91b2b 8859 const struct breakpoint_ops *ops, int from_tty,
44f238bb 8860 int enabled, int internal, unsigned flags)
c906108c 8861{
018d34a4 8862 int i;
f8eba3c6 8863 struct linespec_sals *lsal;
cc59ec59 8864
f8eba3c6
TT
8865 if (canonical->pre_expanded)
8866 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
8867
8868 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
c3f6f71d 8869 {
f8eba3c6
TT
8870 /* Note that 'addr_string' can be NULL in the case of a plain
8871 'break', without arguments. */
8872 char *addr_string = (canonical->addr_string
8873 ? xstrdup (canonical->addr_string)
8874 : NULL);
8875 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
8876 struct cleanup *inner = make_cleanup (xfree, addr_string);
0d381245 8877
f8eba3c6
TT
8878 make_cleanup (xfree, filter_string);
8879 create_breakpoint_sal (gdbarch, lsal->sals,
8880 addr_string,
8881 filter_string,
e7e0cddf
SS
8882 cond_string, extra_string,
8883 type, disposition,
84f4c1fe 8884 thread, task, ignore_count, ops,
44f238bb 8885 from_tty, enabled, internal, flags,
56435ebe 8886 canonical->special_display);
f8eba3c6 8887 discard_cleanups (inner);
c3f6f71d 8888 }
c3f6f71d 8889}
c906108c 8890
9998af43 8891/* Parse ADDRESS which is assumed to be a SAL specification possibly
c3f6f71d 8892 followed by conditionals. On return, SALS contains an array of SAL
4a64f543 8893 addresses found. ADDR_STRING contains a vector of (canonical)
9998af43
TJB
8894 address strings. ADDRESS points to the end of the SAL.
8895
8896 The array and the line spec strings are allocated on the heap, it is
8897 the caller's responsibility to free them. */
c906108c 8898
b9362cc7 8899static void
c3f6f71d 8900parse_breakpoint_sals (char **address,
58438ac1 8901 struct linespec_result *canonical)
c3f6f71d
JM
8902{
8903 char *addr_start = *address;
cc59ec59 8904
c3f6f71d 8905 /* If no arg given, or if first arg is 'if ', use the default
4a64f543 8906 breakpoint. */
c3f6f71d
JM
8907 if ((*address) == NULL
8908 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
c906108c 8909 {
1bfeeb0f
JL
8910 /* The last displayed codepoint, if it's valid, is our default breakpoint
8911 address. */
8912 if (last_displayed_sal_is_valid ())
c906108c 8913 {
f8eba3c6 8914 struct linespec_sals lsal;
c3f6f71d 8915 struct symtab_and_line sal;
cc59ec59 8916
4a64f543 8917 init_sal (&sal); /* Initialize to zeroes. */
f8eba3c6 8918 lsal.sals.sals = (struct symtab_and_line *)
c906108c 8919 xmalloc (sizeof (struct symtab_and_line));
1bfeeb0f
JL
8920
8921 /* Set sal's pspace, pc, symtab, and line to the values
8922 corresponding to the last call to print_frame_info. */
8923 get_last_displayed_sal (&sal);
8924 sal.section = find_pc_overlay (sal.pc);
00903456 8925
4a64f543 8926 /* "break" without arguments is equivalent to "break *PC"
1bfeeb0f
JL
8927 where PC is the last displayed codepoint's address. So
8928 make sure to set sal.explicit_pc to prevent GDB from
8929 trying to expand the list of sals to include all other
8930 instances with the same symtab and line. */
00903456
JK
8931 sal.explicit_pc = 1;
8932
f8eba3c6
TT
8933 lsal.sals.sals[0] = sal;
8934 lsal.sals.nelts = 1;
8935 lsal.canonical = NULL;
8936
8937 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
c906108c
SS
8938 }
8939 else
8a3fe4f8 8940 error (_("No default breakpoint address now."));
c906108c
SS
8941 }
8942 else
8943 {
cc80f267
JK
8944 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
8945
c906108c 8946 /* Force almost all breakpoints to be in terms of the
4a64f543
MS
8947 current_source_symtab (which is decode_line_1's default).
8948 This should produce the results we want almost all of the
cc80f267
JK
8949 time while leaving default_breakpoint_* alone.
8950
8951 ObjC: However, don't match an Objective-C method name which
8952 may have a '+' or '-' succeeded by a '['. */
8953 if (last_displayed_sal_is_valid ()
8954 && (!cursal.symtab
8955 || ((strchr ("+-", (*address)[0]) != NULL)
8956 && ((*address)[1] != '['))))
f8eba3c6
TT
8957 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
8958 get_last_displayed_symtab (),
8959 get_last_displayed_line (),
8960 canonical, NULL, NULL);
c906108c 8961 else
f8eba3c6 8962 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
cc80f267 8963 cursal.symtab, cursal.line, canonical, NULL, NULL);
c906108c 8964 }
c3f6f71d 8965}
c906108c 8966
c906108c 8967
c3f6f71d 8968/* Convert each SAL into a real PC. Verify that the PC can be
4a64f543 8969 inserted as a breakpoint. If it can't throw an error. */
c906108c 8970
b9362cc7 8971static void
23e7acfb 8972breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
c3f6f71d
JM
8973{
8974 int i;
cc59ec59 8975
c3f6f71d 8976 for (i = 0; i < sals->nelts; i++)
ee53e872 8977 resolve_sal_pc (&sals->sals[i]);
c3f6f71d
JM
8978}
8979
7a697b8d
SS
8980/* Fast tracepoints may have restrictions on valid locations. For
8981 instance, a fast tracepoint using a jump instead of a trap will
8982 likely have to overwrite more bytes than a trap would, and so can
8983 only be placed where the instruction is longer than the jump, or a
8984 multi-instruction sequence does not have a jump into the middle of
8985 it, etc. */
8986
8987static void
8988check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8989 struct symtabs_and_lines *sals)
8990{
8991 int i, rslt;
8992 struct symtab_and_line *sal;
8993 char *msg;
8994 struct cleanup *old_chain;
8995
8996 for (i = 0; i < sals->nelts; i++)
8997 {
f8eba3c6
TT
8998 struct gdbarch *sarch;
8999
7a697b8d
SS
9000 sal = &sals->sals[i];
9001
f8eba3c6
TT
9002 sarch = get_sal_arch (*sal);
9003 /* We fall back to GDBARCH if there is no architecture
9004 associated with SAL. */
9005 if (sarch == NULL)
9006 sarch = gdbarch;
9007 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
7a697b8d
SS
9008 NULL, &msg);
9009 old_chain = make_cleanup (xfree, msg);
9010
9011 if (!rslt)
9012 error (_("May not have a fast tracepoint at 0x%s%s"),
f8eba3c6 9013 paddress (sarch, sal->pc), (msg ? msg : ""));
7a697b8d
SS
9014
9015 do_cleanups (old_chain);
9016 }
9017}
9018
018d34a4
VP
9019/* Given TOK, a string specification of condition and thread, as
9020 accepted by the 'break' command, extract the condition
9021 string and thread number and set *COND_STRING and *THREAD.
4a64f543 9022 PC identifies the context at which the condition should be parsed.
018d34a4
VP
9023 If no condition is found, *COND_STRING is set to NULL.
9024 If no thread is found, *THREAD is set to -1. */
d634f2de
JB
9025
9026static void
9027find_condition_and_thread (char *tok, CORE_ADDR pc,
e7e0cddf
SS
9028 char **cond_string, int *thread, int *task,
9029 char **rest)
018d34a4
VP
9030{
9031 *cond_string = NULL;
9032 *thread = -1;
9033 while (tok && *tok)
9034 {
9035 char *end_tok;
9036 int toklen;
9037 char *cond_start = NULL;
9038 char *cond_end = NULL;
cc59ec59 9039
e9cafbcc 9040 tok = skip_spaces (tok);
e7e0cddf
SS
9041
9042 if ((*tok == '"' || *tok == ',') && rest)
9043 {
9044 *rest = savestring (tok, strlen (tok));
9045 return;
9046 }
9047
e9cafbcc 9048 end_tok = skip_to_space (tok);
d634f2de 9049
018d34a4 9050 toklen = end_tok - tok;
d634f2de 9051
018d34a4
VP
9052 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9053 {
f7545552
TT
9054 struct expression *expr;
9055
018d34a4 9056 tok = cond_start = end_tok + 1;
1bb9788d 9057 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
f7545552 9058 xfree (expr);
018d34a4 9059 cond_end = tok;
d634f2de 9060 *cond_string = savestring (cond_start, cond_end - cond_start);
018d34a4
VP
9061 }
9062 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9063 {
9064 char *tmptok;
d634f2de 9065
018d34a4
VP
9066 tok = end_tok + 1;
9067 tmptok = tok;
9068 *thread = strtol (tok, &tok, 0);
9069 if (tok == tmptok)
9070 error (_("Junk after thread keyword."));
9071 if (!valid_thread_id (*thread))
9072 error (_("Unknown thread %d."), *thread);
9073 }
4a306c9a
JB
9074 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9075 {
9076 char *tmptok;
9077
9078 tok = end_tok + 1;
9079 tmptok = tok;
9080 *task = strtol (tok, &tok, 0);
9081 if (tok == tmptok)
9082 error (_("Junk after task keyword."));
9083 if (!valid_task_id (*task))
b6199126 9084 error (_("Unknown task %d."), *task);
4a306c9a 9085 }
e7e0cddf
SS
9086 else if (rest)
9087 {
9088 *rest = savestring (tok, strlen (tok));
ccab2054 9089 return;
e7e0cddf 9090 }
018d34a4
VP
9091 else
9092 error (_("Junk at end of arguments."));
9093 }
9094}
9095
0fb4aa4b
PA
9096/* Decode a static tracepoint marker spec. */
9097
9098static struct symtabs_and_lines
9099decode_static_tracepoint_spec (char **arg_p)
9100{
9101 VEC(static_tracepoint_marker_p) *markers = NULL;
9102 struct symtabs_and_lines sals;
0fb4aa4b
PA
9103 struct cleanup *old_chain;
9104 char *p = &(*arg_p)[3];
9105 char *endp;
9106 char *marker_str;
9107 int i;
9108
e9cafbcc 9109 p = skip_spaces (p);
0fb4aa4b 9110
e9cafbcc 9111 endp = skip_to_space (p);
0fb4aa4b
PA
9112
9113 marker_str = savestring (p, endp - p);
9114 old_chain = make_cleanup (xfree, marker_str);
9115
9116 markers = target_static_tracepoint_markers_by_strid (marker_str);
9117 if (VEC_empty(static_tracepoint_marker_p, markers))
9118 error (_("No known static tracepoint marker named %s"), marker_str);
9119
9120 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9121 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9122
9123 for (i = 0; i < sals.nelts; i++)
9124 {
9125 struct static_tracepoint_marker *marker;
9126
9127 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9128
9129 init_sal (&sals.sals[i]);
9130
9131 sals.sals[i] = find_pc_line (marker->address, 0);
9132 sals.sals[i].pc = marker->address;
9133
9134 release_static_tracepoint_marker (marker);
9135 }
9136
9137 do_cleanups (old_chain);
9138
9139 *arg_p = endp;
9140 return sals;
9141}
9142
fd9b8c24
PA
9143/* Set a breakpoint. This function is shared between CLI and MI
9144 functions for setting a breakpoint. This function has two major
9145 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
9146 parameter. If non-zero, the function will parse arg, extracting
4a64f543
MS
9147 breakpoint location, address and thread. Otherwise, ARG is just
9148 the location of breakpoint, with condition and thread specified by
9149 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
9150 the breakpoint number will be allocated from the internal
9151 breakpoint count. Returns true if any breakpoint was created;
9152 false otherwise. */
0101ce28 9153
8cdf0e15
VP
9154int
9155create_breakpoint (struct gdbarch *gdbarch,
e7e0cddf
SS
9156 char *arg, char *cond_string,
9157 int thread, char *extra_string,
8cdf0e15 9158 int parse_condition_and_thread,
0fb4aa4b 9159 int tempflag, enum bptype type_wanted,
8cdf0e15
VP
9160 int ignore_count,
9161 enum auto_boolean pending_break_support,
c0a91b2b 9162 const struct breakpoint_ops *ops,
44f238bb
PA
9163 int from_tty, int enabled, int internal,
9164 unsigned flags)
c3f6f71d 9165{
b78a6381 9166 volatile struct gdb_exception e;
f8eba3c6 9167 char *copy_arg = NULL;
c3f6f71d 9168 char *addr_start = arg;
7efd8fc2 9169 struct linespec_result canonical;
c3f6f71d 9170 struct cleanup *old_chain;
80c99de1 9171 struct cleanup *bkpt_chain = NULL;
0101ce28 9172 int pending = 0;
4a306c9a 9173 int task = 0;
86b17b60 9174 int prev_bkpt_count = breakpoint_count;
c3f6f71d 9175
348d480f
PA
9176 gdb_assert (ops != NULL);
9177
7efd8fc2 9178 init_linespec_result (&canonical);
c3f6f71d 9179
b78a6381
TT
9180 TRY_CATCH (e, RETURN_MASK_ALL)
9181 {
983af33b
SDJ
9182 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9183 addr_start, &copy_arg);
b78a6381 9184 }
0101ce28
JJ
9185
9186 /* If caller is interested in rc value from parse, set value. */
05ff989b 9187 switch (e.reason)
0101ce28 9188 {
983af33b
SDJ
9189 case GDB_NO_ERROR:
9190 if (VEC_empty (linespec_sals, canonical.sals))
9191 return 0;
9192 break;
05ff989b
AC
9193 case RETURN_ERROR:
9194 switch (e.error)
0101ce28 9195 {
05ff989b 9196 case NOT_FOUND_ERROR:
0101ce28 9197
05ff989b
AC
9198 /* If pending breakpoint support is turned off, throw
9199 error. */
fa8d40ab
JJ
9200
9201 if (pending_break_support == AUTO_BOOLEAN_FALSE)
723a2275
VP
9202 throw_exception (e);
9203
9204 exception_print (gdb_stderr, e);
fa8d40ab 9205
05ff989b
AC
9206 /* If pending breakpoint support is auto query and the user
9207 selects no, then simply return the error code. */
059fb39f 9208 if (pending_break_support == AUTO_BOOLEAN_AUTO
bfccc43c
YQ
9209 && !nquery (_("Make %s pending on future shared library load? "),
9210 bptype_string (type_wanted)))
fd9b8c24 9211 return 0;
fa8d40ab 9212
05ff989b
AC
9213 /* At this point, either the user was queried about setting
9214 a pending breakpoint and selected yes, or pending
9215 breakpoint behavior is on and thus a pending breakpoint
9216 is defaulted on behalf of the user. */
f8eba3c6
TT
9217 {
9218 struct linespec_sals lsal;
9219
9220 copy_arg = xstrdup (addr_start);
9221 lsal.canonical = xstrdup (copy_arg);
9222 lsal.sals.nelts = 1;
9223 lsal.sals.sals = XNEW (struct symtab_and_line);
9224 init_sal (&lsal.sals.sals[0]);
9225 pending = 1;
9226 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9227 }
05ff989b
AC
9228 break;
9229 default:
98deb0da 9230 throw_exception (e);
0101ce28 9231 }
2abae994 9232 break;
05ff989b 9233 default:
983af33b 9234 throw_exception (e);
0101ce28 9235 }
c3f6f71d 9236
4a64f543 9237 /* Create a chain of things that always need to be cleaned up. */
f8eba3c6 9238 old_chain = make_cleanup_destroy_linespec_result (&canonical);
c3f6f71d 9239
c3f6f71d
JM
9240 /* ----------------------------- SNIP -----------------------------
9241 Anything added to the cleanup chain beyond this point is assumed
9242 to be part of a breakpoint. If the breakpoint create succeeds
80c99de1
PA
9243 then the memory is not reclaimed. */
9244 bkpt_chain = make_cleanup (null_cleanup, 0);
c3f6f71d 9245
c3f6f71d
JM
9246 /* Resolve all line numbers to PC's and verify that the addresses
9247 are ok for the target. */
0101ce28 9248 if (!pending)
f8eba3c6
TT
9249 {
9250 int ix;
9251 struct linespec_sals *iter;
9252
9253 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9254 breakpoint_sals_to_pc (&iter->sals);
9255 }
c3f6f71d 9256
7a697b8d 9257 /* Fast tracepoints may have additional restrictions on location. */
bfccc43c 9258 if (!pending && type_wanted == bp_fast_tracepoint)
f8eba3c6
TT
9259 {
9260 int ix;
9261 struct linespec_sals *iter;
9262
9263 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9264 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9265 }
7a697b8d 9266
c3f6f71d
JM
9267 /* Verify that condition can be parsed, before setting any
9268 breakpoints. Allocate a separate condition expression for each
4a64f543 9269 breakpoint. */
0101ce28 9270 if (!pending)
c3f6f71d 9271 {
f8eba3c6
TT
9272 struct linespec_sals *lsal;
9273
9274 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9275
2f069f6f 9276 if (parse_condition_and_thread)
72b2ff0e 9277 {
e7e0cddf 9278 char *rest;
72b2ff0e
VP
9279 /* Here we only parse 'arg' to separate condition
9280 from thread number, so parsing in context of first
9281 sal is OK. When setting the breakpoint we'll
9282 re-parse it in context of each sal. */
9283 cond_string = NULL;
9284 thread = -1;
e7e0cddf 9285 rest = NULL;
f8eba3c6 9286 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
e7e0cddf 9287 &thread, &task, &rest);
72b2ff0e
VP
9288 if (cond_string)
9289 make_cleanup (xfree, cond_string);
e7e0cddf
SS
9290 if (rest)
9291 make_cleanup (xfree, rest);
9292 if (rest)
9293 extra_string = rest;
72b2ff0e 9294 }
2f069f6f 9295 else
72b2ff0e
VP
9296 {
9297 /* Create a private copy of condition string. */
9298 if (cond_string)
9299 {
9300 cond_string = xstrdup (cond_string);
9301 make_cleanup (xfree, cond_string);
9302 }
e7e0cddf
SS
9303 /* Create a private copy of any extra string. */
9304 if (extra_string)
9305 {
9306 extra_string = xstrdup (extra_string);
9307 make_cleanup (xfree, extra_string);
9308 }
72b2ff0e 9309 }
0fb4aa4b 9310
983af33b 9311 ops->create_breakpoints_sal (gdbarch, &canonical, lsal,
e7e0cddf 9312 cond_string, extra_string, type_wanted,
d9b3f62e
PA
9313 tempflag ? disp_del : disp_donttouch,
9314 thread, task, ignore_count, ops,
44f238bb 9315 from_tty, enabled, internal, flags);
c906108c 9316 }
0101ce28
JJ
9317 else
9318 {
0101ce28
JJ
9319 struct breakpoint *b;
9320
0101ce28
JJ
9321 make_cleanup (xfree, copy_arg);
9322
bfccc43c
YQ
9323 if (is_tracepoint_type (type_wanted))
9324 {
9325 struct tracepoint *t;
9326
9327 t = XCNEW (struct tracepoint);
9328 b = &t->base;
9329 }
9330 else
9331 b = XNEW (struct breakpoint);
9332
9333 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9334
f8eba3c6 9335 b->addr_string = copy_arg;
72b2ff0e 9336 b->cond_string = NULL;
e7e0cddf 9337 b->extra_string = NULL;
0101ce28 9338 b->ignore_count = ignore_count;
0101ce28 9339 b->disposition = tempflag ? disp_del : disp_donttouch;
0d381245 9340 b->condition_not_parsed = 1;
41447f92 9341 b->enable_state = enabled ? bp_enabled : bp_disabled;
cc72b2a2
KP
9342 if ((type_wanted != bp_breakpoint
9343 && type_wanted != bp_hardware_breakpoint) || thread != -1)
f8eba3c6 9344 b->pspace = current_program_space;
8bea4e01 9345
bfccc43c 9346 install_breakpoint (internal, b, 0);
0101ce28
JJ
9347 }
9348
f8eba3c6 9349 if (VEC_length (linespec_sals, canonical.sals) > 1)
95a42b64 9350 {
3e43a32a
MS
9351 warning (_("Multiple breakpoints were set.\nUse the "
9352 "\"delete\" command to delete unwanted breakpoints."));
86b17b60 9353 prev_breakpoint_count = prev_bkpt_count;
95a42b64
TT
9354 }
9355
80c99de1
PA
9356 /* That's it. Discard the cleanups for data inserted into the
9357 breakpoint. */
9358 discard_cleanups (bkpt_chain);
9359 /* But cleanup everything else. */
c3f6f71d 9360 do_cleanups (old_chain);
217dc9e2 9361
80c99de1 9362 /* error call may happen here - have BKPT_CHAIN already discarded. */
217dc9e2 9363 update_global_location_list (1);
fd9b8c24
PA
9364
9365 return 1;
c3f6f71d 9366}
c906108c 9367
348d480f 9368/* Set a breakpoint.
72b2ff0e
VP
9369 ARG is a string describing breakpoint address,
9370 condition, and thread.
9371 FLAG specifies if a breakpoint is hardware on,
9372 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9373 and BP_TEMPFLAG. */
348d480f 9374
98deb0da 9375static void
72b2ff0e 9376break_command_1 (char *arg, int flag, int from_tty)
c3f6f71d 9377{
72b2ff0e 9378 int tempflag = flag & BP_TEMPFLAG;
0fb4aa4b
PA
9379 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9380 ? bp_hardware_breakpoint
9381 : bp_breakpoint);
55aa24fb
SDJ
9382 struct breakpoint_ops *ops;
9383 const char *arg_cp = arg;
9384
9385 /* Matching breakpoints on probes. */
9386 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9387 ops = &bkpt_probe_breakpoint_ops;
9388 else
9389 ops = &bkpt_breakpoint_ops;
c3f6f71d 9390
8cdf0e15
VP
9391 create_breakpoint (get_current_arch (),
9392 arg,
e7e0cddf 9393 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b 9394 tempflag, type_wanted,
8cdf0e15
VP
9395 0 /* Ignore count */,
9396 pending_break_support,
55aa24fb 9397 ops,
8cdf0e15 9398 from_tty,
84f4c1fe 9399 1 /* enabled */,
44f238bb
PA
9400 0 /* internal */,
9401 0);
c906108c
SS
9402}
9403
c906108c
SS
9404/* Helper function for break_command_1 and disassemble_command. */
9405
9406void
fba45db2 9407resolve_sal_pc (struct symtab_and_line *sal)
c906108c
SS
9408{
9409 CORE_ADDR pc;
9410
9411 if (sal->pc == 0 && sal->symtab != NULL)
9412 {
9413 if (!find_line_pc (sal->symtab, sal->line, &pc))
8a3fe4f8 9414 error (_("No line %d in file \"%s\"."),
c906108c
SS
9415 sal->line, sal->symtab->filename);
9416 sal->pc = pc;
6a048695 9417
4a64f543
MS
9418 /* If this SAL corresponds to a breakpoint inserted using a line
9419 number, then skip the function prologue if necessary. */
6a048695 9420 if (sal->explicit_line)
059acae7 9421 skip_prologue_sal (sal);
c906108c
SS
9422 }
9423
9424 if (sal->section == 0 && sal->symtab != NULL)
9425 {
9426 struct blockvector *bv;
c5aa993b
JM
9427 struct block *b;
9428 struct symbol *sym;
c906108c 9429
801e3a5b 9430 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
c906108c
SS
9431 if (bv != NULL)
9432 {
7f0df278 9433 sym = block_linkage_function (b);
c906108c
SS
9434 if (sym != NULL)
9435 {
9436 fixup_symbol_section (sym, sal->symtab->objfile);
714835d5 9437 sal->section = SYMBOL_OBJ_SECTION (sym);
c906108c
SS
9438 }
9439 else
9440 {
4a64f543
MS
9441 /* It really is worthwhile to have the section, so we'll
9442 just have to look harder. This case can be executed
9443 if we have line numbers but no functions (as can
9444 happen in assembly source). */
c906108c 9445
c5aa993b 9446 struct minimal_symbol *msym;
6c95b8df
PA
9447 struct cleanup *old_chain = save_current_space_and_thread ();
9448
9449 switch_to_program_space_and_thread (sal->pspace);
c906108c
SS
9450
9451 msym = lookup_minimal_symbol_by_pc (sal->pc);
9452 if (msym)
714835d5 9453 sal->section = SYMBOL_OBJ_SECTION (msym);
6c95b8df
PA
9454
9455 do_cleanups (old_chain);
c906108c
SS
9456 }
9457 }
9458 }
9459}
9460
9461void
fba45db2 9462break_command (char *arg, int from_tty)
c906108c 9463{
db107f19 9464 break_command_1 (arg, 0, from_tty);
c906108c
SS
9465}
9466
c906108c 9467void
fba45db2 9468tbreak_command (char *arg, int from_tty)
c906108c 9469{
db107f19 9470 break_command_1 (arg, BP_TEMPFLAG, from_tty);
c906108c
SS
9471}
9472
c906108c 9473static void
fba45db2 9474hbreak_command (char *arg, int from_tty)
c906108c 9475{
db107f19 9476 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
c906108c
SS
9477}
9478
9479static void
fba45db2 9480thbreak_command (char *arg, int from_tty)
c906108c 9481{
db107f19 9482 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
c906108c
SS
9483}
9484
9485static void
fba45db2 9486stop_command (char *arg, int from_tty)
c906108c 9487{
a3f17187 9488 printf_filtered (_("Specify the type of breakpoint to set.\n\
c906108c 9489Usage: stop in <function | address>\n\
a3f17187 9490 stop at <line>\n"));
c906108c
SS
9491}
9492
9493static void
fba45db2 9494stopin_command (char *arg, int from_tty)
c906108c
SS
9495{
9496 int badInput = 0;
9497
c5aa993b 9498 if (arg == (char *) NULL)
c906108c
SS
9499 badInput = 1;
9500 else if (*arg != '*')
9501 {
9502 char *argptr = arg;
9503 int hasColon = 0;
9504
4a64f543 9505 /* Look for a ':'. If this is a line number specification, then
53a5351d 9506 say it is bad, otherwise, it should be an address or
4a64f543 9507 function/method name. */
c906108c 9508 while (*argptr && !hasColon)
c5aa993b
JM
9509 {
9510 hasColon = (*argptr == ':');
9511 argptr++;
9512 }
c906108c
SS
9513
9514 if (hasColon)
c5aa993b 9515 badInput = (*argptr != ':'); /* Not a class::method */
c906108c 9516 else
c5aa993b 9517 badInput = isdigit (*arg); /* a simple line number */
c906108c
SS
9518 }
9519
9520 if (badInput)
a3f17187 9521 printf_filtered (_("Usage: stop in <function | address>\n"));
c906108c 9522 else
db107f19 9523 break_command_1 (arg, 0, from_tty);
c906108c
SS
9524}
9525
9526static void
fba45db2 9527stopat_command (char *arg, int from_tty)
c906108c
SS
9528{
9529 int badInput = 0;
9530
c5aa993b 9531 if (arg == (char *) NULL || *arg == '*') /* no line number */
c906108c
SS
9532 badInput = 1;
9533 else
9534 {
9535 char *argptr = arg;
9536 int hasColon = 0;
9537
4a64f543
MS
9538 /* Look for a ':'. If there is a '::' then get out, otherwise
9539 it is probably a line number. */
c906108c 9540 while (*argptr && !hasColon)
c5aa993b
JM
9541 {
9542 hasColon = (*argptr == ':');
9543 argptr++;
9544 }
c906108c
SS
9545
9546 if (hasColon)
c5aa993b 9547 badInput = (*argptr == ':'); /* we have class::method */
c906108c 9548 else
c5aa993b 9549 badInput = !isdigit (*arg); /* not a line number */
c906108c
SS
9550 }
9551
9552 if (badInput)
a3f17187 9553 printf_filtered (_("Usage: stop at <line>\n"));
c906108c 9554 else
db107f19 9555 break_command_1 (arg, 0, from_tty);
c906108c
SS
9556}
9557
e7e0cddf
SS
9558void dprintf_command (char *arg, int from_tty);
9559
9560/* The dynamic printf command is mostly like a regular breakpoint, but
9561 with a prewired command list consisting of a single output command,
9562 built from extra arguments supplied on the dprintf command
9563 line. */
9564
9565void
9566dprintf_command (char *arg, int from_tty)
9567{
9568 create_breakpoint (get_current_arch (),
9569 arg,
9570 NULL, 0, NULL, 1 /* parse arg */,
9571 0, bp_dprintf,
9572 0 /* Ignore count */,
9573 pending_break_support,
9574 &dprintf_breakpoint_ops,
9575 from_tty,
9576 1 /* enabled */,
9577 0 /* internal */,
9578 0);
9579}
9580
f1310107
TJB
9581/* Implement the "breakpoint_hit" breakpoint_ops method for
9582 ranged breakpoints. */
9583
9584static int
9585breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9586 struct address_space *aspace,
09ac7c10
TT
9587 CORE_ADDR bp_addr,
9588 const struct target_waitstatus *ws)
f1310107 9589{
09ac7c10 9590 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 9591 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
9592 return 0;
9593
f1310107
TJB
9594 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9595 bl->length, aspace, bp_addr);
9596}
9597
9598/* Implement the "resources_needed" breakpoint_ops method for
9599 ranged breakpoints. */
9600
9601static int
9602resources_needed_ranged_breakpoint (const struct bp_location *bl)
9603{
9604 return target_ranged_break_num_registers ();
9605}
9606
9607/* Implement the "print_it" breakpoint_ops method for
9608 ranged breakpoints. */
9609
9610static enum print_stop_action
348d480f 9611print_it_ranged_breakpoint (bpstat bs)
f1310107 9612{
348d480f 9613 struct breakpoint *b = bs->breakpoint_at;
f1310107 9614 struct bp_location *bl = b->loc;
79a45e25 9615 struct ui_out *uiout = current_uiout;
f1310107
TJB
9616
9617 gdb_assert (b->type == bp_hardware_breakpoint);
9618
9619 /* Ranged breakpoints have only one location. */
9620 gdb_assert (bl && bl->next == NULL);
9621
9622 annotate_breakpoint (b->number);
9623 if (b->disposition == disp_del)
9624 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
9625 else
9626 ui_out_text (uiout, "\nRanged breakpoint ");
9627 if (ui_out_is_mi_like_p (uiout))
9628 {
9629 ui_out_field_string (uiout, "reason",
9630 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9631 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9632 }
9633 ui_out_field_int (uiout, "bkptno", b->number);
9634 ui_out_text (uiout, ", ");
9635
9636 return PRINT_SRC_AND_LOC;
9637}
9638
9639/* Implement the "print_one" breakpoint_ops method for
9640 ranged breakpoints. */
9641
9642static void
9643print_one_ranged_breakpoint (struct breakpoint *b,
9644 struct bp_location **last_loc)
9645{
9646 struct bp_location *bl = b->loc;
9647 struct value_print_options opts;
79a45e25 9648 struct ui_out *uiout = current_uiout;
f1310107
TJB
9649
9650 /* Ranged breakpoints have only one location. */
9651 gdb_assert (bl && bl->next == NULL);
9652
9653 get_user_print_options (&opts);
9654
9655 if (opts.addressprint)
9656 /* We don't print the address range here, it will be printed later
9657 by print_one_detail_ranged_breakpoint. */
9658 ui_out_field_skip (uiout, "addr");
9659 annotate_field (5);
9660 print_breakpoint_location (b, bl);
9661 *last_loc = bl;
9662}
9663
9664/* Implement the "print_one_detail" breakpoint_ops method for
9665 ranged breakpoints. */
9666
9667static void
9668print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9669 struct ui_out *uiout)
9670{
9671 CORE_ADDR address_start, address_end;
9672 struct bp_location *bl = b->loc;
f99d8bf4
PA
9673 struct ui_file *stb = mem_fileopen ();
9674 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
f1310107
TJB
9675
9676 gdb_assert (bl);
9677
9678 address_start = bl->address;
9679 address_end = address_start + bl->length - 1;
9680
9681 ui_out_text (uiout, "\taddress range: ");
f99d8bf4 9682 fprintf_unfiltered (stb, "[%s, %s]",
f1310107
TJB
9683 print_core_address (bl->gdbarch, address_start),
9684 print_core_address (bl->gdbarch, address_end));
9685 ui_out_field_stream (uiout, "addr", stb);
9686 ui_out_text (uiout, "\n");
9687
9688 do_cleanups (cleanup);
9689}
9690
9691/* Implement the "print_mention" breakpoint_ops method for
9692 ranged breakpoints. */
9693
9694static void
9695print_mention_ranged_breakpoint (struct breakpoint *b)
9696{
9697 struct bp_location *bl = b->loc;
79a45e25 9698 struct ui_out *uiout = current_uiout;
f1310107
TJB
9699
9700 gdb_assert (bl);
9701 gdb_assert (b->type == bp_hardware_breakpoint);
9702
9703 if (ui_out_is_mi_like_p (uiout))
9704 return;
9705
9706 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9707 b->number, paddress (bl->gdbarch, bl->address),
9708 paddress (bl->gdbarch, bl->address + bl->length - 1));
9709}
9710
9711/* Implement the "print_recreate" breakpoint_ops method for
9712 ranged breakpoints. */
9713
9714static void
9715print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9716{
9717 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
9718 b->addr_string_range_end);
d9b3f62e 9719 print_recreate_thread (b, fp);
f1310107
TJB
9720}
9721
9722/* The breakpoint_ops structure to be used in ranged breakpoints. */
9723
2060206e 9724static struct breakpoint_ops ranged_breakpoint_ops;
f1310107
TJB
9725
9726/* Find the address where the end of the breakpoint range should be
9727 placed, given the SAL of the end of the range. This is so that if
9728 the user provides a line number, the end of the range is set to the
9729 last instruction of the given line. */
9730
9731static CORE_ADDR
9732find_breakpoint_range_end (struct symtab_and_line sal)
9733{
9734 CORE_ADDR end;
9735
9736 /* If the user provided a PC value, use it. Otherwise,
9737 find the address of the end of the given location. */
9738 if (sal.explicit_pc)
9739 end = sal.pc;
9740 else
9741 {
9742 int ret;
9743 CORE_ADDR start;
9744
9745 ret = find_line_pc_range (sal, &start, &end);
9746 if (!ret)
9747 error (_("Could not find location of the end of the range."));
9748
9749 /* find_line_pc_range returns the start of the next line. */
9750 end--;
9751 }
9752
9753 return end;
9754}
9755
9756/* Implement the "break-range" CLI command. */
9757
9758static void
9759break_range_command (char *arg, int from_tty)
9760{
9761 char *arg_start, *addr_string_start, *addr_string_end;
9762 struct linespec_result canonical_start, canonical_end;
9763 int bp_count, can_use_bp, length;
9764 CORE_ADDR end;
9765 struct breakpoint *b;
9766 struct symtab_and_line sal_start, sal_end;
f1310107 9767 struct cleanup *cleanup_bkpt;
f8eba3c6 9768 struct linespec_sals *lsal_start, *lsal_end;
f1310107
TJB
9769
9770 /* We don't support software ranged breakpoints. */
9771 if (target_ranged_break_num_registers () < 0)
9772 error (_("This target does not support hardware ranged breakpoints."));
9773
9774 bp_count = hw_breakpoint_used_count ();
9775 bp_count += target_ranged_break_num_registers ();
9776 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9777 bp_count, 0);
9778 if (can_use_bp < 0)
9779 error (_("Hardware breakpoints used exceeds limit."));
9780
f8eba3c6 9781 arg = skip_spaces (arg);
f1310107
TJB
9782 if (arg == NULL || arg[0] == '\0')
9783 error(_("No address range specified."));
9784
f1310107
TJB
9785 init_linespec_result (&canonical_start);
9786
f8eba3c6
TT
9787 arg_start = arg;
9788 parse_breakpoint_sals (&arg, &canonical_start);
f1310107 9789
f8eba3c6 9790 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
f1310107
TJB
9791
9792 if (arg[0] != ',')
9793 error (_("Too few arguments."));
f8eba3c6 9794 else if (VEC_empty (linespec_sals, canonical_start.sals))
f1310107 9795 error (_("Could not find location of the beginning of the range."));
f8eba3c6
TT
9796
9797 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
9798
9799 if (VEC_length (linespec_sals, canonical_start.sals) > 1
9800 || lsal_start->sals.nelts != 1)
f1310107
TJB
9801 error (_("Cannot create a ranged breakpoint with multiple locations."));
9802
f8eba3c6
TT
9803 sal_start = lsal_start->sals.sals[0];
9804 addr_string_start = savestring (arg_start, arg - arg_start);
9805 make_cleanup (xfree, addr_string_start);
f1310107
TJB
9806
9807 arg++; /* Skip the comma. */
f8eba3c6 9808 arg = skip_spaces (arg);
f1310107
TJB
9809
9810 /* Parse the end location. */
9811
f1310107
TJB
9812 init_linespec_result (&canonical_end);
9813 arg_start = arg;
9814
f8eba3c6 9815 /* We call decode_line_full directly here instead of using
f1310107
TJB
9816 parse_breakpoint_sals because we need to specify the start location's
9817 symtab and line as the default symtab and line for the end of the
9818 range. This makes it possible to have ranges like "foo.c:27, +14",
9819 where +14 means 14 lines from the start location. */
f8eba3c6
TT
9820 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
9821 sal_start.symtab, sal_start.line,
9822 &canonical_end, NULL, NULL);
9823
9824 make_cleanup_destroy_linespec_result (&canonical_end);
f1310107 9825
f8eba3c6 9826 if (VEC_empty (linespec_sals, canonical_end.sals))
f1310107 9827 error (_("Could not find location of the end of the range."));
f8eba3c6
TT
9828
9829 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
9830 if (VEC_length (linespec_sals, canonical_end.sals) > 1
9831 || lsal_end->sals.nelts != 1)
f1310107
TJB
9832 error (_("Cannot create a ranged breakpoint with multiple locations."));
9833
f8eba3c6
TT
9834 sal_end = lsal_end->sals.sals[0];
9835 addr_string_end = savestring (arg_start, arg - arg_start);
9836 make_cleanup (xfree, addr_string_end);
f1310107
TJB
9837
9838 end = find_breakpoint_range_end (sal_end);
9839 if (sal_start.pc > end)
177b42fe 9840 error (_("Invalid address range, end precedes start."));
f1310107
TJB
9841
9842 length = end - sal_start.pc + 1;
9843 if (length < 0)
9844 /* Length overflowed. */
9845 error (_("Address range too large."));
9846 else if (length == 1)
9847 {
9848 /* This range is simple enough to be handled by
9849 the `hbreak' command. */
9850 hbreak_command (addr_string_start, 1);
9851
9852 do_cleanups (cleanup_bkpt);
9853
9854 return;
9855 }
9856
9857 /* Now set up the breakpoint. */
9858 b = set_raw_breakpoint (get_current_arch (), sal_start,
348d480f 9859 bp_hardware_breakpoint, &ranged_breakpoint_ops);
f1310107
TJB
9860 set_breakpoint_count (breakpoint_count + 1);
9861 b->number = breakpoint_count;
9862 b->disposition = disp_donttouch;
f8eba3c6
TT
9863 b->addr_string = xstrdup (addr_string_start);
9864 b->addr_string_range_end = xstrdup (addr_string_end);
f1310107
TJB
9865 b->loc->length = length;
9866
f8eba3c6 9867 do_cleanups (cleanup_bkpt);
f1310107
TJB
9868
9869 mention (b);
8d3788bd 9870 observer_notify_breakpoint_created (b);
f1310107
TJB
9871 update_global_location_list (1);
9872}
9873
4a64f543
MS
9874/* Return non-zero if EXP is verified as constant. Returned zero
9875 means EXP is variable. Also the constant detection may fail for
9876 some constant expressions and in such case still falsely return
9877 zero. */
2e6e3d9c 9878
65d79d4b
SDJ
9879static int
9880watchpoint_exp_is_const (const struct expression *exp)
9881{
9882 int i = exp->nelts;
9883
9884 while (i > 0)
9885 {
9886 int oplenp, argsp;
9887
9888 /* We are only interested in the descriptor of each element. */
9889 operator_length (exp, i, &oplenp, &argsp);
9890 i -= oplenp;
9891
9892 switch (exp->elts[i].opcode)
9893 {
9894 case BINOP_ADD:
9895 case BINOP_SUB:
9896 case BINOP_MUL:
9897 case BINOP_DIV:
9898 case BINOP_REM:
9899 case BINOP_MOD:
9900 case BINOP_LSH:
9901 case BINOP_RSH:
9902 case BINOP_LOGICAL_AND:
9903 case BINOP_LOGICAL_OR:
9904 case BINOP_BITWISE_AND:
9905 case BINOP_BITWISE_IOR:
9906 case BINOP_BITWISE_XOR:
9907 case BINOP_EQUAL:
9908 case BINOP_NOTEQUAL:
9909 case BINOP_LESS:
9910 case BINOP_GTR:
9911 case BINOP_LEQ:
9912 case BINOP_GEQ:
9913 case BINOP_REPEAT:
9914 case BINOP_COMMA:
9915 case BINOP_EXP:
9916 case BINOP_MIN:
9917 case BINOP_MAX:
9918 case BINOP_INTDIV:
9919 case BINOP_CONCAT:
9920 case BINOP_IN:
9921 case BINOP_RANGE:
9922 case TERNOP_COND:
9923 case TERNOP_SLICE:
9924 case TERNOP_SLICE_COUNT:
9925
9926 case OP_LONG:
9927 case OP_DOUBLE:
9928 case OP_DECFLOAT:
9929 case OP_LAST:
9930 case OP_COMPLEX:
9931 case OP_STRING:
9932 case OP_BITSTRING:
9933 case OP_ARRAY:
9934 case OP_TYPE:
9935 case OP_NAME:
9936 case OP_OBJC_NSSTRING:
9937
9938 case UNOP_NEG:
9939 case UNOP_LOGICAL_NOT:
9940 case UNOP_COMPLEMENT:
9941 case UNOP_ADDR:
9942 case UNOP_HIGH:
aeaa2474 9943 case UNOP_CAST:
4a64f543
MS
9944 /* Unary, binary and ternary operators: We have to check
9945 their operands. If they are constant, then so is the
9946 result of that operation. For instance, if A and B are
9947 determined to be constants, then so is "A + B".
9948
9949 UNOP_IND is one exception to the rule above, because the
9950 value of *ADDR is not necessarily a constant, even when
9951 ADDR is. */
65d79d4b
SDJ
9952 break;
9953
9954 case OP_VAR_VALUE:
9955 /* Check whether the associated symbol is a constant.
4a64f543 9956
65d79d4b 9957 We use SYMBOL_CLASS rather than TYPE_CONST because it's
4a64f543
MS
9958 possible that a buggy compiler could mark a variable as
9959 constant even when it is not, and TYPE_CONST would return
9960 true in this case, while SYMBOL_CLASS wouldn't.
9961
9962 We also have to check for function symbols because they
9963 are always constant. */
65d79d4b
SDJ
9964 {
9965 struct symbol *s = exp->elts[i + 2].symbol;
9966
9967 if (SYMBOL_CLASS (s) != LOC_BLOCK
9968 && SYMBOL_CLASS (s) != LOC_CONST
9969 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9970 return 0;
9971 break;
9972 }
9973
9974 /* The default action is to return 0 because we are using
9975 the optimistic approach here: If we don't know something,
9976 then it is not a constant. */
9977 default:
9978 return 0;
9979 }
9980 }
9981
9982 return 1;
9983}
9984
3a5c3e22
PA
9985/* Implement the "dtor" breakpoint_ops method for watchpoints. */
9986
9987static void
9988dtor_watchpoint (struct breakpoint *self)
9989{
9990 struct watchpoint *w = (struct watchpoint *) self;
9991
9992 xfree (w->cond_exp);
9993 xfree (w->exp);
9994 xfree (w->exp_string);
9995 xfree (w->exp_string_reparse);
9996 value_free (w->val);
9997
9998 base_breakpoint_ops.dtor (self);
9999}
10000
348d480f
PA
10001/* Implement the "re_set" breakpoint_ops method for watchpoints. */
10002
10003static void
10004re_set_watchpoint (struct breakpoint *b)
10005{
3a5c3e22
PA
10006 struct watchpoint *w = (struct watchpoint *) b;
10007
348d480f
PA
10008 /* Watchpoint can be either on expression using entirely global
10009 variables, or it can be on local variables.
10010
10011 Watchpoints of the first kind are never auto-deleted, and even
10012 persist across program restarts. Since they can use variables
10013 from shared libraries, we need to reparse expression as libraries
10014 are loaded and unloaded.
10015
10016 Watchpoints on local variables can also change meaning as result
10017 of solib event. For example, if a watchpoint uses both a local
10018 and a global variables in expression, it's a local watchpoint,
10019 but unloading of a shared library will make the expression
10020 invalid. This is not a very common use case, but we still
10021 re-evaluate expression, to avoid surprises to the user.
10022
10023 Note that for local watchpoints, we re-evaluate it only if
10024 watchpoints frame id is still valid. If it's not, it means the
10025 watchpoint is out of scope and will be deleted soon. In fact,
10026 I'm not sure we'll ever be called in this case.
10027
10028 If a local watchpoint's frame id is still valid, then
3a5c3e22 10029 w->exp_valid_block is likewise valid, and we can safely use it.
348d480f 10030
3a5c3e22
PA
10031 Don't do anything about disabled watchpoints, since they will be
10032 reevaluated again when enabled. */
10033 update_watchpoint (w, 1 /* reparse */);
348d480f
PA
10034}
10035
77b06cd7
TJB
10036/* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10037
10038static int
10039insert_watchpoint (struct bp_location *bl)
10040{
3a5c3e22
PA
10041 struct watchpoint *w = (struct watchpoint *) bl->owner;
10042 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10043
10044 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10045 w->cond_exp);
77b06cd7
TJB
10046}
10047
10048/* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10049
10050static int
10051remove_watchpoint (struct bp_location *bl)
10052{
3a5c3e22
PA
10053 struct watchpoint *w = (struct watchpoint *) bl->owner;
10054 int length = w->exact ? 1 : bl->length;
e09342b5
TJB
10055
10056 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
3a5c3e22 10057 w->cond_exp);
e09342b5
TJB
10058}
10059
e09342b5 10060static int
348d480f 10061breakpoint_hit_watchpoint (const struct bp_location *bl,
09ac7c10
TT
10062 struct address_space *aspace, CORE_ADDR bp_addr,
10063 const struct target_waitstatus *ws)
e09342b5 10064{
348d480f 10065 struct breakpoint *b = bl->owner;
3a5c3e22 10066 struct watchpoint *w = (struct watchpoint *) b;
77b06cd7 10067
348d480f
PA
10068 /* Continuable hardware watchpoints are treated as non-existent if the
10069 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10070 some data address). Otherwise gdb won't stop on a break instruction
10071 in the code (not from a breakpoint) when a hardware watchpoint has
10072 been defined. Also skip watchpoints which we know did not trigger
10073 (did not match the data address). */
10074 if (is_hardware_watchpoint (b)
3a5c3e22 10075 && w->watchpoint_triggered == watch_triggered_no)
348d480f 10076 return 0;
9c06b0b4 10077
348d480f 10078 return 1;
9c06b0b4
TJB
10079}
10080
348d480f
PA
10081static void
10082check_status_watchpoint (bpstat bs)
9c06b0b4 10083{
348d480f 10084 gdb_assert (is_watchpoint (bs->breakpoint_at));
9c06b0b4 10085
348d480f 10086 bpstat_check_watchpoint (bs);
9c06b0b4
TJB
10087}
10088
10089/* Implement the "resources_needed" breakpoint_ops method for
348d480f 10090 hardware watchpoints. */
9c06b0b4
TJB
10091
10092static int
348d480f 10093resources_needed_watchpoint (const struct bp_location *bl)
9c06b0b4 10094{
3a5c3e22
PA
10095 struct watchpoint *w = (struct watchpoint *) bl->owner;
10096 int length = w->exact? 1 : bl->length;
348d480f
PA
10097
10098 return target_region_ok_for_hw_watchpoint (bl->address, length);
9c06b0b4
TJB
10099}
10100
10101/* Implement the "works_in_software_mode" breakpoint_ops method for
348d480f 10102 hardware watchpoints. */
9c06b0b4
TJB
10103
10104static int
348d480f 10105works_in_software_mode_watchpoint (const struct breakpoint *b)
9c06b0b4 10106{
efa80663
PA
10107 /* Read and access watchpoints only work with hardware support. */
10108 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
9c06b0b4
TJB
10109}
10110
9c06b0b4 10111static enum print_stop_action
348d480f 10112print_it_watchpoint (bpstat bs)
9c06b0b4 10113{
348d480f
PA
10114 struct cleanup *old_chain;
10115 struct breakpoint *b;
10116 const struct bp_location *bl;
f99d8bf4 10117 struct ui_file *stb;
348d480f 10118 enum print_stop_action result;
3a5c3e22 10119 struct watchpoint *w;
79a45e25 10120 struct ui_out *uiout = current_uiout;
348d480f
PA
10121
10122 gdb_assert (bs->bp_location_at != NULL);
10123
10124 bl = bs->bp_location_at;
10125 b = bs->breakpoint_at;
3a5c3e22 10126 w = (struct watchpoint *) b;
348d480f 10127
f99d8bf4
PA
10128 stb = mem_fileopen ();
10129 old_chain = make_cleanup_ui_file_delete (stb);
9c06b0b4
TJB
10130
10131 switch (b->type)
10132 {
348d480f 10133 case bp_watchpoint:
9c06b0b4
TJB
10134 case bp_hardware_watchpoint:
10135 annotate_watchpoint (b->number);
10136 if (ui_out_is_mi_like_p (uiout))
10137 ui_out_field_string
10138 (uiout, "reason",
10139 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
348d480f
PA
10140 mention (b);
10141 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10142 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10143 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10144 ui_out_field_stream (uiout, "old", stb);
10145 ui_out_text (uiout, "\nNew value = ");
f99d8bf4 10146 watchpoint_value_print (w->val, stb);
348d480f
PA
10147 ui_out_field_stream (uiout, "new", stb);
10148 ui_out_text (uiout, "\n");
10149 /* More than one watchpoint may have been triggered. */
10150 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10151 break;
10152
10153 case bp_read_watchpoint:
10154 if (ui_out_is_mi_like_p (uiout))
10155 ui_out_field_string
10156 (uiout, "reason",
10157 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
348d480f
PA
10158 mention (b);
10159 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10160 ui_out_text (uiout, "\nValue = ");
f99d8bf4 10161 watchpoint_value_print (w->val, stb);
348d480f
PA
10162 ui_out_field_stream (uiout, "value", stb);
10163 ui_out_text (uiout, "\n");
10164 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10165 break;
10166
10167 case bp_access_watchpoint:
348d480f
PA
10168 if (bs->old_val != NULL)
10169 {
10170 annotate_watchpoint (b->number);
10171 if (ui_out_is_mi_like_p (uiout))
10172 ui_out_field_string
10173 (uiout, "reason",
10174 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10175 mention (b);
10176 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10177 ui_out_text (uiout, "\nOld value = ");
f99d8bf4 10178 watchpoint_value_print (bs->old_val, stb);
348d480f
PA
10179 ui_out_field_stream (uiout, "old", stb);
10180 ui_out_text (uiout, "\nNew value = ");
10181 }
10182 else
10183 {
10184 mention (b);
10185 if (ui_out_is_mi_like_p (uiout))
10186 ui_out_field_string
10187 (uiout, "reason",
10188 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10189 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10190 ui_out_text (uiout, "\nValue = ");
10191 }
f99d8bf4 10192 watchpoint_value_print (w->val, stb);
348d480f
PA
10193 ui_out_field_stream (uiout, "new", stb);
10194 ui_out_text (uiout, "\n");
10195 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10196 break;
10197 default:
348d480f 10198 result = PRINT_UNKNOWN;
9c06b0b4
TJB
10199 }
10200
348d480f
PA
10201 do_cleanups (old_chain);
10202 return result;
10203}
10204
10205/* Implement the "print_mention" breakpoint_ops method for hardware
10206 watchpoints. */
10207
10208static void
10209print_mention_watchpoint (struct breakpoint *b)
10210{
10211 struct cleanup *ui_out_chain;
3a5c3e22 10212 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10213 struct ui_out *uiout = current_uiout;
348d480f
PA
10214
10215 switch (b->type)
10216 {
10217 case bp_watchpoint:
10218 ui_out_text (uiout, "Watchpoint ");
10219 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10220 break;
10221 case bp_hardware_watchpoint:
10222 ui_out_text (uiout, "Hardware watchpoint ");
10223 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10224 break;
10225 case bp_read_watchpoint:
10226 ui_out_text (uiout, "Hardware read watchpoint ");
10227 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10228 break;
10229 case bp_access_watchpoint:
10230 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10231 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10232 break;
10233 default:
10234 internal_error (__FILE__, __LINE__,
10235 _("Invalid hardware watchpoint type."));
10236 }
10237
10238 ui_out_field_int (uiout, "number", b->number);
10239 ui_out_text (uiout, ": ");
3a5c3e22 10240 ui_out_field_string (uiout, "exp", w->exp_string);
348d480f
PA
10241 do_cleanups (ui_out_chain);
10242}
10243
10244/* Implement the "print_recreate" breakpoint_ops method for
10245 watchpoints. */
10246
10247static void
10248print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10249{
3a5c3e22
PA
10250 struct watchpoint *w = (struct watchpoint *) b;
10251
348d480f
PA
10252 switch (b->type)
10253 {
10254 case bp_watchpoint:
10255 case bp_hardware_watchpoint:
10256 fprintf_unfiltered (fp, "watch");
10257 break;
10258 case bp_read_watchpoint:
10259 fprintf_unfiltered (fp, "rwatch");
10260 break;
10261 case bp_access_watchpoint:
10262 fprintf_unfiltered (fp, "awatch");
10263 break;
10264 default:
10265 internal_error (__FILE__, __LINE__,
10266 _("Invalid watchpoint type."));
10267 }
10268
3a5c3e22 10269 fprintf_unfiltered (fp, " %s", w->exp_string);
d9b3f62e 10270 print_recreate_thread (b, fp);
348d480f
PA
10271}
10272
10273/* The breakpoint_ops structure to be used in hardware watchpoints. */
10274
2060206e 10275static struct breakpoint_ops watchpoint_breakpoint_ops;
348d480f
PA
10276
10277/* Implement the "insert" breakpoint_ops method for
10278 masked hardware watchpoints. */
10279
10280static int
10281insert_masked_watchpoint (struct bp_location *bl)
10282{
3a5c3e22
PA
10283 struct watchpoint *w = (struct watchpoint *) bl->owner;
10284
10285 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10286 bl->watchpoint_type);
10287}
10288
10289/* Implement the "remove" breakpoint_ops method for
10290 masked hardware watchpoints. */
10291
10292static int
10293remove_masked_watchpoint (struct bp_location *bl)
10294{
3a5c3e22
PA
10295 struct watchpoint *w = (struct watchpoint *) bl->owner;
10296
10297 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
348d480f
PA
10298 bl->watchpoint_type);
10299}
10300
10301/* Implement the "resources_needed" breakpoint_ops method for
10302 masked hardware watchpoints. */
10303
10304static int
10305resources_needed_masked_watchpoint (const struct bp_location *bl)
10306{
3a5c3e22
PA
10307 struct watchpoint *w = (struct watchpoint *) bl->owner;
10308
10309 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
348d480f
PA
10310}
10311
10312/* Implement the "works_in_software_mode" breakpoint_ops method for
10313 masked hardware watchpoints. */
10314
10315static int
10316works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10317{
10318 return 0;
10319}
10320
10321/* Implement the "print_it" breakpoint_ops method for
10322 masked hardware watchpoints. */
10323
10324static enum print_stop_action
10325print_it_masked_watchpoint (bpstat bs)
10326{
10327 struct breakpoint *b = bs->breakpoint_at;
79a45e25 10328 struct ui_out *uiout = current_uiout;
348d480f
PA
10329
10330 /* Masked watchpoints have only one location. */
10331 gdb_assert (b->loc && b->loc->next == NULL);
10332
10333 switch (b->type)
10334 {
10335 case bp_hardware_watchpoint:
10336 annotate_watchpoint (b->number);
10337 if (ui_out_is_mi_like_p (uiout))
10338 ui_out_field_string
10339 (uiout, "reason",
10340 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10341 break;
10342
10343 case bp_read_watchpoint:
10344 if (ui_out_is_mi_like_p (uiout))
10345 ui_out_field_string
10346 (uiout, "reason",
10347 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10348 break;
10349
10350 case bp_access_watchpoint:
10351 if (ui_out_is_mi_like_p (uiout))
10352 ui_out_field_string
10353 (uiout, "reason",
10354 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10355 break;
10356 default:
10357 internal_error (__FILE__, __LINE__,
10358 _("Invalid hardware watchpoint type."));
10359 }
10360
10361 mention (b);
9c06b0b4
TJB
10362 ui_out_text (uiout, _("\n\
10363Check the underlying instruction at PC for the memory\n\
10364address and value which triggered this watchpoint.\n"));
10365 ui_out_text (uiout, "\n");
10366
10367 /* More than one watchpoint may have been triggered. */
10368 return PRINT_UNKNOWN;
10369}
10370
10371/* Implement the "print_one_detail" breakpoint_ops method for
10372 masked hardware watchpoints. */
10373
10374static void
10375print_one_detail_masked_watchpoint (const struct breakpoint *b,
10376 struct ui_out *uiout)
10377{
3a5c3e22
PA
10378 struct watchpoint *w = (struct watchpoint *) b;
10379
9c06b0b4
TJB
10380 /* Masked watchpoints have only one location. */
10381 gdb_assert (b->loc && b->loc->next == NULL);
10382
10383 ui_out_text (uiout, "\tmask ");
3a5c3e22 10384 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
9c06b0b4
TJB
10385 ui_out_text (uiout, "\n");
10386}
10387
10388/* Implement the "print_mention" breakpoint_ops method for
10389 masked hardware watchpoints. */
10390
10391static void
10392print_mention_masked_watchpoint (struct breakpoint *b)
10393{
3a5c3e22 10394 struct watchpoint *w = (struct watchpoint *) b;
79a45e25 10395 struct ui_out *uiout = current_uiout;
9c06b0b4
TJB
10396 struct cleanup *ui_out_chain;
10397
10398 switch (b->type)
10399 {
10400 case bp_hardware_watchpoint:
10401 ui_out_text (uiout, "Masked hardware watchpoint ");
10402 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10403 break;
10404 case bp_read_watchpoint:
10405 ui_out_text (uiout, "Masked hardware read watchpoint ");
10406 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10407 break;
10408 case bp_access_watchpoint:
10409 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10410 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10411 break;
10412 default:
10413 internal_error (__FILE__, __LINE__,
10414 _("Invalid hardware watchpoint type."));
10415 }
10416
10417 ui_out_field_int (uiout, "number", b->number);
10418 ui_out_text (uiout, ": ");
3a5c3e22 10419 ui_out_field_string (uiout, "exp", w->exp_string);
9c06b0b4
TJB
10420 do_cleanups (ui_out_chain);
10421}
10422
10423/* Implement the "print_recreate" breakpoint_ops method for
10424 masked hardware watchpoints. */
10425
10426static void
10427print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10428{
3a5c3e22 10429 struct watchpoint *w = (struct watchpoint *) b;
9c06b0b4
TJB
10430 char tmp[40];
10431
10432 switch (b->type)
10433 {
10434 case bp_hardware_watchpoint:
10435 fprintf_unfiltered (fp, "watch");
10436 break;
10437 case bp_read_watchpoint:
10438 fprintf_unfiltered (fp, "rwatch");
10439 break;
10440 case bp_access_watchpoint:
10441 fprintf_unfiltered (fp, "awatch");
10442 break;
10443 default:
10444 internal_error (__FILE__, __LINE__,
10445 _("Invalid hardware watchpoint type."));
10446 }
10447
3a5c3e22
PA
10448 sprintf_vma (tmp, w->hw_wp_mask);
10449 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
d9b3f62e 10450 print_recreate_thread (b, fp);
9c06b0b4
TJB
10451}
10452
10453/* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10454
2060206e 10455static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
9c06b0b4
TJB
10456
10457/* Tell whether the given watchpoint is a masked hardware watchpoint. */
10458
10459static int
10460is_masked_watchpoint (const struct breakpoint *b)
10461{
10462 return b->ops == &masked_watchpoint_breakpoint_ops;
10463}
10464
53a5351d
JM
10465/* accessflag: hw_write: watch write,
10466 hw_read: watch read,
10467 hw_access: watch access (read or write) */
c906108c 10468static void
84f4c1fe
PM
10469watch_command_1 (char *arg, int accessflag, int from_tty,
10470 int just_location, int internal)
c906108c 10471{
a9634178 10472 volatile struct gdb_exception e;
d983da9c 10473 struct breakpoint *b, *scope_breakpoint = NULL;
c906108c 10474 struct expression *exp;
60e1c644 10475 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
a1442452 10476 struct value *val, *mark, *result;
c906108c 10477 struct frame_info *frame;
c906108c
SS
10478 char *exp_start = NULL;
10479 char *exp_end = NULL;
9c06b0b4
TJB
10480 char *tok, *end_tok;
10481 int toklen = -1;
c906108c
SS
10482 char *cond_start = NULL;
10483 char *cond_end = NULL;
c906108c 10484 enum bptype bp_type;
37e4754d 10485 int thread = -1;
0cf6dd15 10486 int pc = 0;
9c06b0b4
TJB
10487 /* Flag to indicate whether we are going to use masks for
10488 the hardware watchpoint. */
10489 int use_mask = 0;
10490 CORE_ADDR mask = 0;
3a5c3e22 10491 struct watchpoint *w;
c906108c 10492
37e4754d
LM
10493 /* Make sure that we actually have parameters to parse. */
10494 if (arg != NULL && arg[0] != '\0')
10495 {
9c06b0b4 10496 char *value_start;
37e4754d 10497
9c06b0b4
TJB
10498 /* Look for "parameter value" pairs at the end
10499 of the arguments string. */
10500 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
10501 {
10502 /* Skip whitespace at the end of the argument list. */
10503 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10504 tok--;
10505
10506 /* Find the beginning of the last token.
10507 This is the value of the parameter. */
10508 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10509 tok--;
10510 value_start = tok + 1;
10511
10512 /* Skip whitespace. */
10513 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10514 tok--;
10515
10516 end_tok = tok;
10517
10518 /* Find the beginning of the second to last token.
10519 This is the parameter itself. */
10520 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10521 tok--;
10522 tok++;
10523 toklen = end_tok - tok + 1;
10524
10525 if (toklen == 6 && !strncmp (tok, "thread", 6))
10526 {
10527 /* At this point we've found a "thread" token, which means
10528 the user is trying to set a watchpoint that triggers
10529 only in a specific thread. */
10530 char *endp;
37e4754d 10531
9c06b0b4
TJB
10532 if (thread != -1)
10533 error(_("You can specify only one thread."));
37e4754d 10534
9c06b0b4
TJB
10535 /* Extract the thread ID from the next token. */
10536 thread = strtol (value_start, &endp, 0);
37e4754d 10537
9c06b0b4
TJB
10538 /* Check if the user provided a valid numeric value for the
10539 thread ID. */
10540 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10541 error (_("Invalid thread ID specification %s."), value_start);
10542
10543 /* Check if the thread actually exists. */
10544 if (!valid_thread_id (thread))
10545 error (_("Unknown thread %d."), thread);
10546 }
10547 else if (toklen == 4 && !strncmp (tok, "mask", 4))
10548 {
10549 /* We've found a "mask" token, which means the user wants to
10550 create a hardware watchpoint that is going to have the mask
10551 facility. */
10552 struct value *mask_value, *mark;
37e4754d 10553
9c06b0b4
TJB
10554 if (use_mask)
10555 error(_("You can specify only one mask."));
37e4754d 10556
9c06b0b4 10557 use_mask = just_location = 1;
37e4754d 10558
9c06b0b4
TJB
10559 mark = value_mark ();
10560 mask_value = parse_to_comma_and_eval (&value_start);
10561 mask = value_as_address (mask_value);
10562 value_free_to_mark (mark);
10563 }
10564 else
10565 /* We didn't recognize what we found. We should stop here. */
10566 break;
37e4754d 10567
9c06b0b4
TJB
10568 /* Truncate the string and get rid of the "parameter value" pair before
10569 the arguments string is parsed by the parse_exp_1 function. */
10570 *tok = '\0';
10571 }
37e4754d
LM
10572 }
10573
10574 /* Parse the rest of the arguments. */
c906108c
SS
10575 innermost_block = NULL;
10576 exp_start = arg;
1bb9788d 10577 exp = parse_exp_1 (&arg, 0, 0, 0);
c906108c 10578 exp_end = arg;
fa8a61dc
TT
10579 /* Remove trailing whitespace from the expression before saving it.
10580 This makes the eventual display of the expression string a bit
10581 prettier. */
10582 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10583 --exp_end;
10584
65d79d4b
SDJ
10585 /* Checking if the expression is not constant. */
10586 if (watchpoint_exp_is_const (exp))
10587 {
10588 int len;
10589
10590 len = exp_end - exp_start;
10591 while (len > 0 && isspace (exp_start[len - 1]))
10592 len--;
10593 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10594 }
10595
c906108c
SS
10596 exp_valid_block = innermost_block;
10597 mark = value_mark ();
a1442452 10598 fetch_subexp_value (exp, &pc, &val, &result, NULL);
06a64a0b
TT
10599
10600 if (just_location)
10601 {
9c06b0b4
TJB
10602 int ret;
10603
06a64a0b 10604 exp_valid_block = NULL;
a1442452 10605 val = value_addr (result);
06a64a0b
TT
10606 release_value (val);
10607 value_free_to_mark (mark);
9c06b0b4
TJB
10608
10609 if (use_mask)
10610 {
10611 ret = target_masked_watch_num_registers (value_as_address (val),
10612 mask);
10613 if (ret == -1)
10614 error (_("This target does not support masked watchpoints."));
10615 else if (ret == -2)
10616 error (_("Invalid mask or memory region."));
10617 }
06a64a0b
TT
10618 }
10619 else if (val != NULL)
fa4727a6 10620 release_value (val);
c906108c 10621
e9cafbcc
TT
10622 tok = skip_spaces (arg);
10623 end_tok = skip_to_space (tok);
c906108c
SS
10624
10625 toklen = end_tok - tok;
10626 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10627 {
2d134ed3
PA
10628 struct expression *cond;
10629
60e1c644 10630 innermost_block = NULL;
c906108c 10631 tok = cond_start = end_tok + 1;
1bb9788d 10632 cond = parse_exp_1 (&tok, 0, 0, 0);
60e1c644
PA
10633
10634 /* The watchpoint expression may not be local, but the condition
10635 may still be. E.g.: `watch global if local > 0'. */
10636 cond_exp_valid_block = innermost_block;
10637
2d134ed3 10638 xfree (cond);
c906108c
SS
10639 cond_end = tok;
10640 }
10641 if (*tok)
8a3fe4f8 10642 error (_("Junk at end of command."));
c906108c 10643
53a5351d 10644 if (accessflag == hw_read)
c5aa993b 10645 bp_type = bp_read_watchpoint;
53a5351d 10646 else if (accessflag == hw_access)
c5aa993b
JM
10647 bp_type = bp_access_watchpoint;
10648 else
10649 bp_type = bp_hardware_watchpoint;
c906108c 10650
d983da9c 10651 frame = block_innermost_frame (exp_valid_block);
d983da9c
DJ
10652
10653 /* If the expression is "local", then set up a "watchpoint scope"
10654 breakpoint at the point where we've left the scope of the watchpoint
10655 expression. Create the scope breakpoint before the watchpoint, so
10656 that we will encounter it first in bpstat_stop_status. */
60e1c644 10657 if (exp_valid_block && frame)
d983da9c 10658 {
edb3359d
DJ
10659 if (frame_id_p (frame_unwind_caller_id (frame)))
10660 {
10661 scope_breakpoint
a6d9a66e
UW
10662 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
10663 frame_unwind_caller_pc (frame),
06edf0c0
PA
10664 bp_watchpoint_scope,
10665 &momentary_breakpoint_ops);
d983da9c 10666
edb3359d 10667 scope_breakpoint->enable_state = bp_enabled;
d983da9c 10668
edb3359d
DJ
10669 /* Automatically delete the breakpoint when it hits. */
10670 scope_breakpoint->disposition = disp_del;
d983da9c 10671
edb3359d
DJ
10672 /* Only break in the proper frame (help with recursion). */
10673 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
d983da9c 10674
edb3359d 10675 /* Set the address at which we will stop. */
a6d9a66e
UW
10676 scope_breakpoint->loc->gdbarch
10677 = frame_unwind_caller_arch (frame);
edb3359d
DJ
10678 scope_breakpoint->loc->requested_address
10679 = frame_unwind_caller_pc (frame);
10680 scope_breakpoint->loc->address
a6d9a66e
UW
10681 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10682 scope_breakpoint->loc->requested_address,
edb3359d
DJ
10683 scope_breakpoint->type);
10684 }
d983da9c
DJ
10685 }
10686
c906108c 10687 /* Now set up the breakpoint. */
3a5c3e22
PA
10688
10689 w = XCNEW (struct watchpoint);
10690 b = &w->base;
348d480f 10691 if (use_mask)
3a5c3e22
PA
10692 init_raw_breakpoint_without_location (b, NULL, bp_type,
10693 &masked_watchpoint_breakpoint_ops);
348d480f 10694 else
3a5c3e22
PA
10695 init_raw_breakpoint_without_location (b, NULL, bp_type,
10696 &watchpoint_breakpoint_ops);
37e4754d 10697 b->thread = thread;
b5de0fa7 10698 b->disposition = disp_donttouch;
348d480f 10699 b->pspace = current_program_space;
3a5c3e22
PA
10700 w->exp = exp;
10701 w->exp_valid_block = exp_valid_block;
10702 w->cond_exp_valid_block = cond_exp_valid_block;
06a64a0b
TT
10703 if (just_location)
10704 {
10705 struct type *t = value_type (val);
10706 CORE_ADDR addr = value_as_address (val);
10707 char *name;
10708
10709 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
10710 name = type_to_string (t);
10711
3a5c3e22 10712 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
d63d0675 10713 core_addr_to_string (addr));
06a64a0b
TT
10714 xfree (name);
10715
3a5c3e22 10716 w->exp_string = xstrprintf ("-location %.*s",
d63d0675
JK
10717 (int) (exp_end - exp_start), exp_start);
10718
06a64a0b
TT
10719 /* The above expression is in C. */
10720 b->language = language_c;
10721 }
10722 else
3a5c3e22 10723 w->exp_string = savestring (exp_start, exp_end - exp_start);
9c06b0b4
TJB
10724
10725 if (use_mask)
10726 {
3a5c3e22 10727 w->hw_wp_mask = mask;
9c06b0b4
TJB
10728 }
10729 else
10730 {
3a5c3e22
PA
10731 w->val = val;
10732 w->val_valid = 1;
9c06b0b4 10733 }
77b06cd7 10734
c906108c
SS
10735 if (cond_start)
10736 b->cond_string = savestring (cond_start, cond_end - cond_start);
10737 else
10738 b->cond_string = 0;
c5aa993b 10739
c906108c 10740 if (frame)
f6bc2008 10741 {
3a5c3e22
PA
10742 w->watchpoint_frame = get_frame_id (frame);
10743 w->watchpoint_thread = inferior_ptid;
f6bc2008 10744 }
c906108c 10745 else
f6bc2008 10746 {
3a5c3e22
PA
10747 w->watchpoint_frame = null_frame_id;
10748 w->watchpoint_thread = null_ptid;
f6bc2008 10749 }
c906108c 10750
d983da9c 10751 if (scope_breakpoint != NULL)
c906108c 10752 {
d983da9c
DJ
10753 /* The scope breakpoint is related to the watchpoint. We will
10754 need to act on them together. */
10755 b->related_breakpoint = scope_breakpoint;
10756 scope_breakpoint->related_breakpoint = b;
c906108c 10757 }
d983da9c 10758
06a64a0b
TT
10759 if (!just_location)
10760 value_free_to_mark (mark);
2d134ed3 10761
a9634178
TJB
10762 TRY_CATCH (e, RETURN_MASK_ALL)
10763 {
10764 /* Finally update the new watchpoint. This creates the locations
10765 that should be inserted. */
3a5c3e22 10766 update_watchpoint (w, 1);
a9634178
TJB
10767 }
10768 if (e.reason < 0)
10769 {
10770 delete_breakpoint (b);
10771 throw_exception (e);
10772 }
10773
3ea46bff 10774 install_breakpoint (internal, b, 1);
c906108c
SS
10775}
10776
e09342b5 10777/* Return count of debug registers needed to watch the given expression.
e09342b5 10778 If the watchpoint cannot be handled in hardware return zero. */
c906108c 10779
c906108c 10780static int
a9634178 10781can_use_hardware_watchpoint (struct value *v)
c906108c
SS
10782{
10783 int found_memory_cnt = 0;
2e70b7b9 10784 struct value *head = v;
c906108c
SS
10785
10786 /* Did the user specifically forbid us to use hardware watchpoints? */
c5aa993b 10787 if (!can_use_hw_watchpoints)
c906108c 10788 return 0;
c5aa993b 10789
5c44784c
JM
10790 /* Make sure that the value of the expression depends only upon
10791 memory contents, and values computed from them within GDB. If we
10792 find any register references or function calls, we can't use a
10793 hardware watchpoint.
10794
10795 The idea here is that evaluating an expression generates a series
10796 of values, one holding the value of every subexpression. (The
10797 expression a*b+c has five subexpressions: a, b, a*b, c, and
10798 a*b+c.) GDB's values hold almost enough information to establish
10799 the criteria given above --- they identify memory lvalues,
10800 register lvalues, computed values, etcetera. So we can evaluate
10801 the expression, and then scan the chain of values that leaves
10802 behind to decide whether we can detect any possible change to the
10803 expression's final value using only hardware watchpoints.
10804
10805 However, I don't think that the values returned by inferior
10806 function calls are special in any way. So this function may not
10807 notice that an expression involving an inferior function call
10808 can't be watched with hardware watchpoints. FIXME. */
17cf0ecd 10809 for (; v; v = value_next (v))
c906108c 10810 {
5c44784c 10811 if (VALUE_LVAL (v) == lval_memory)
c906108c 10812 {
8464be76
DJ
10813 if (v != head && value_lazy (v))
10814 /* A lazy memory lvalue in the chain is one that GDB never
10815 needed to fetch; we either just used its address (e.g.,
10816 `a' in `a.b') or we never needed it at all (e.g., `a'
10817 in `a,b'). This doesn't apply to HEAD; if that is
10818 lazy then it was not readable, but watch it anyway. */
5c44784c 10819 ;
53a5351d 10820 else
5c44784c
JM
10821 {
10822 /* Ahh, memory we actually used! Check if we can cover
10823 it with hardware watchpoints. */
df407dfe 10824 struct type *vtype = check_typedef (value_type (v));
2e70b7b9
MS
10825
10826 /* We only watch structs and arrays if user asked for it
10827 explicitly, never if they just happen to appear in a
10828 middle of some value chain. */
10829 if (v == head
10830 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10831 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10832 {
42ae5230 10833 CORE_ADDR vaddr = value_address (v);
e09342b5
TJB
10834 int len;
10835 int num_regs;
10836
a9634178 10837 len = (target_exact_watchpoints
e09342b5
TJB
10838 && is_scalar_type_recursive (vtype))?
10839 1 : TYPE_LENGTH (value_type (v));
2e70b7b9 10840
e09342b5
TJB
10841 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10842 if (!num_regs)
2e70b7b9
MS
10843 return 0;
10844 else
e09342b5 10845 found_memory_cnt += num_regs;
2e70b7b9 10846 }
5c44784c 10847 }
c5aa993b 10848 }
5086187c
AC
10849 else if (VALUE_LVAL (v) != not_lval
10850 && deprecated_value_modifiable (v) == 0)
38b6c3b3 10851 return 0; /* These are values from the history (e.g., $1). */
5086187c 10852 else if (VALUE_LVAL (v) == lval_register)
38b6c3b3 10853 return 0; /* Cannot watch a register with a HW watchpoint. */
c906108c
SS
10854 }
10855
10856 /* The expression itself looks suitable for using a hardware
10857 watchpoint, but give the target machine a chance to reject it. */
10858 return found_memory_cnt;
10859}
10860
8b93c638 10861void
84f4c1fe 10862watch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 10863{
84f4c1fe 10864 watch_command_1 (arg, hw_write, from_tty, 0, internal);
06a64a0b
TT
10865}
10866
10867/* A helper function that looks for an argument at the start of a
10868 string. The argument must also either be at the end of the string,
10869 or be followed by whitespace. Returns 1 if it finds the argument,
10870 0 otherwise. If the argument is found, it updates *STR. */
10871
10872static int
10873check_for_argument (char **str, char *arg, int arg_len)
10874{
10875 if (strncmp (*str, arg, arg_len) == 0
10876 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
10877 {
10878 *str += arg_len;
10879 return 1;
10880 }
10881 return 0;
10882}
10883
10884/* A helper function that looks for the "-location" argument and then
10885 calls watch_command_1. */
10886
10887static void
10888watch_maybe_just_location (char *arg, int accessflag, int from_tty)
10889{
10890 int just_location = 0;
10891
10892 if (arg
10893 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10894 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10895 {
e9cafbcc 10896 arg = skip_spaces (arg);
06a64a0b
TT
10897 just_location = 1;
10898 }
10899
84f4c1fe 10900 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
8b93c638 10901}
8926118c 10902
c5aa993b 10903static void
fba45db2 10904watch_command (char *arg, int from_tty)
c906108c 10905{
06a64a0b 10906 watch_maybe_just_location (arg, hw_write, from_tty);
c906108c
SS
10907}
10908
8b93c638 10909void
84f4c1fe 10910rwatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 10911{
84f4c1fe 10912 watch_command_1 (arg, hw_read, from_tty, 0, internal);
8b93c638 10913}
8926118c 10914
c5aa993b 10915static void
fba45db2 10916rwatch_command (char *arg, int from_tty)
c906108c 10917{
06a64a0b 10918 watch_maybe_just_location (arg, hw_read, from_tty);
c906108c
SS
10919}
10920
8b93c638 10921void
84f4c1fe 10922awatch_command_wrapper (char *arg, int from_tty, int internal)
8b93c638 10923{
84f4c1fe 10924 watch_command_1 (arg, hw_access, from_tty, 0, internal);
8b93c638 10925}
8926118c 10926
c5aa993b 10927static void
fba45db2 10928awatch_command (char *arg, int from_tty)
c906108c 10929{
06a64a0b 10930 watch_maybe_just_location (arg, hw_access, from_tty);
c906108c 10931}
c906108c 10932\f
c5aa993b 10933
43ff13b4 10934/* Helper routines for the until_command routine in infcmd.c. Here
c906108c
SS
10935 because it uses the mechanisms of breakpoints. */
10936
bfec99b2
PA
10937struct until_break_command_continuation_args
10938{
10939 struct breakpoint *breakpoint;
10940 struct breakpoint *breakpoint2;
186c406b 10941 int thread_num;
bfec99b2
PA
10942};
10943
43ff13b4 10944/* This function is called by fetch_inferior_event via the
4a64f543 10945 cmd_continuation pointer, to complete the until command. It takes
43ff13b4 10946 care of cleaning up the temporary breakpoints set up by the until
4a64f543 10947 command. */
c2c6d25f 10948static void
fa4cd53f 10949until_break_command_continuation (void *arg, int err)
43ff13b4 10950{
bfec99b2
PA
10951 struct until_break_command_continuation_args *a = arg;
10952
10953 delete_breakpoint (a->breakpoint);
10954 if (a->breakpoint2)
10955 delete_breakpoint (a->breakpoint2);
186c406b 10956 delete_longjmp_breakpoint (a->thread_num);
43ff13b4
JM
10957}
10958
c906108c 10959void
ae66c1fc 10960until_break_command (char *arg, int from_tty, int anywhere)
c906108c
SS
10961{
10962 struct symtabs_and_lines sals;
10963 struct symtab_and_line sal;
8556afb4
PA
10964 struct frame_info *frame;
10965 struct gdbarch *frame_gdbarch;
10966 struct frame_id stack_frame_id;
10967 struct frame_id caller_frame_id;
c906108c 10968 struct breakpoint *breakpoint;
f107f563 10969 struct breakpoint *breakpoint2 = NULL;
c906108c 10970 struct cleanup *old_chain;
186c406b
TT
10971 int thread;
10972 struct thread_info *tp;
c906108c
SS
10973
10974 clear_proceed_status ();
10975
10976 /* Set a breakpoint where the user wants it and at return from
4a64f543 10977 this function. */
c5aa993b 10978
1bfeeb0f 10979 if (last_displayed_sal_is_valid ())
f8eba3c6 10980 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
1bfeeb0f 10981 get_last_displayed_symtab (),
f8eba3c6 10982 get_last_displayed_line ());
c906108c 10983 else
f8eba3c6
TT
10984 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
10985 (struct symtab *) NULL, 0);
c5aa993b 10986
c906108c 10987 if (sals.nelts != 1)
8a3fe4f8 10988 error (_("Couldn't get information on specified line."));
c5aa993b 10989
c906108c 10990 sal = sals.sals[0];
4a64f543 10991 xfree (sals.sals); /* malloc'd, so freed. */
c5aa993b 10992
c906108c 10993 if (*arg)
8a3fe4f8 10994 error (_("Junk at end of arguments."));
c5aa993b 10995
c906108c 10996 resolve_sal_pc (&sal);
c5aa993b 10997
186c406b
TT
10998 tp = inferior_thread ();
10999 thread = tp->num;
11000
883bc8d1
PA
11001 old_chain = make_cleanup (null_cleanup, NULL);
11002
8556afb4
PA
11003 /* Note linespec handling above invalidates the frame chain.
11004 Installing a breakpoint also invalidates the frame chain (as it
11005 may need to switch threads), so do any frame handling before
11006 that. */
11007
11008 frame = get_selected_frame (NULL);
11009 frame_gdbarch = get_frame_arch (frame);
11010 stack_frame_id = get_stack_frame_id (frame);
11011 caller_frame_id = frame_unwind_caller_id (frame);
883bc8d1 11012
ae66c1fc
EZ
11013 /* Keep within the current frame, or in frames called by the current
11014 one. */
edb3359d 11015
883bc8d1 11016 if (frame_id_p (caller_frame_id))
c906108c 11017 {
883bc8d1
PA
11018 struct symtab_and_line sal2;
11019
11020 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11021 sal2.pc = frame_unwind_caller_pc (frame);
a6d9a66e 11022 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
883bc8d1
PA
11023 sal2,
11024 caller_frame_id,
f107f563
VP
11025 bp_until);
11026 make_cleanup_delete_breakpoint (breakpoint2);
186c406b 11027
883bc8d1 11028 set_longjmp_breakpoint (tp, caller_frame_id);
186c406b 11029 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
c906108c 11030 }
c5aa993b 11031
c70a6932
JK
11032 /* set_momentary_breakpoint could invalidate FRAME. */
11033 frame = NULL;
11034
883bc8d1
PA
11035 if (anywhere)
11036 /* If the user told us to continue until a specified location,
11037 we don't specify a frame at which we need to stop. */
11038 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11039 null_frame_id, bp_until);
11040 else
11041 /* Otherwise, specify the selected frame, because we want to stop
11042 only at the very same frame. */
11043 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11044 stack_frame_id, bp_until);
11045 make_cleanup_delete_breakpoint (breakpoint);
11046
a493e3e2 11047 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
f107f563 11048
4a64f543
MS
11049 /* If we are running asynchronously, and proceed call above has
11050 actually managed to start the target, arrange for breakpoints to
11051 be deleted when the target stops. Otherwise, we're already
11052 stopped and delete breakpoints via cleanup chain. */
f107f563 11053
8ea051c5 11054 if (target_can_async_p () && is_running (inferior_ptid))
f107f563 11055 {
bfec99b2
PA
11056 struct until_break_command_continuation_args *args;
11057 args = xmalloc (sizeof (*args));
f107f563 11058
bfec99b2
PA
11059 args->breakpoint = breakpoint;
11060 args->breakpoint2 = breakpoint2;
186c406b 11061 args->thread_num = thread;
f107f563
VP
11062
11063 discard_cleanups (old_chain);
95e54da7
PA
11064 add_continuation (inferior_thread (),
11065 until_break_command_continuation, args,
604ead4a 11066 xfree);
f107f563
VP
11067 }
11068 else
c5aa993b 11069 do_cleanups (old_chain);
c906108c 11070}
ae66c1fc 11071
c906108c
SS
11072/* This function attempts to parse an optional "if <cond>" clause
11073 from the arg string. If one is not found, it returns NULL.
c5aa993b 11074
c906108c
SS
11075 Else, it returns a pointer to the condition string. (It does not
11076 attempt to evaluate the string against a particular block.) And,
11077 it updates arg to point to the first character following the parsed
4a64f543 11078 if clause in the arg string. */
53a5351d 11079
c906108c 11080static char *
fba45db2 11081ep_parse_optional_if_clause (char **arg)
c906108c 11082{
c5aa993b
JM
11083 char *cond_string;
11084
11085 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
c906108c 11086 return NULL;
c5aa993b 11087
4a64f543 11088 /* Skip the "if" keyword. */
c906108c 11089 (*arg) += 2;
c5aa993b 11090
c906108c 11091 /* Skip any extra leading whitespace, and record the start of the
4a64f543 11092 condition string. */
e9cafbcc 11093 *arg = skip_spaces (*arg);
c906108c 11094 cond_string = *arg;
c5aa993b 11095
4a64f543
MS
11096 /* Assume that the condition occupies the remainder of the arg
11097 string. */
c906108c 11098 (*arg) += strlen (cond_string);
c5aa993b 11099
c906108c
SS
11100 return cond_string;
11101}
c5aa993b 11102
c906108c
SS
11103/* Commands to deal with catching events, such as signals, exceptions,
11104 process start/exit, etc. */
c5aa993b
JM
11105
11106typedef enum
11107{
44feb3ce
TT
11108 catch_fork_temporary, catch_vfork_temporary,
11109 catch_fork_permanent, catch_vfork_permanent
c5aa993b
JM
11110}
11111catch_fork_kind;
11112
c906108c 11113static void
cc59ec59
MS
11114catch_fork_command_1 (char *arg, int from_tty,
11115 struct cmd_list_element *command)
c906108c 11116{
a6d9a66e 11117 struct gdbarch *gdbarch = get_current_arch ();
c5aa993b 11118 char *cond_string = NULL;
44feb3ce
TT
11119 catch_fork_kind fork_kind;
11120 int tempflag;
11121
11122 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11123 tempflag = (fork_kind == catch_fork_temporary
11124 || fork_kind == catch_vfork_temporary);
c5aa993b 11125
44feb3ce
TT
11126 if (!arg)
11127 arg = "";
e9cafbcc 11128 arg = skip_spaces (arg);
c5aa993b 11129
c906108c 11130 /* The allowed syntax is:
c5aa993b
JM
11131 catch [v]fork
11132 catch [v]fork if <cond>
11133
4a64f543 11134 First, check if there's an if clause. */
c906108c 11135 cond_string = ep_parse_optional_if_clause (&arg);
c5aa993b 11136
c906108c 11137 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11138 error (_("Junk at end of arguments."));
c5aa993b 11139
c906108c 11140 /* If this target supports it, create a fork or vfork catchpoint
4a64f543 11141 and enable reporting of such events. */
c5aa993b
JM
11142 switch (fork_kind)
11143 {
44feb3ce
TT
11144 case catch_fork_temporary:
11145 case catch_fork_permanent:
a6d9a66e 11146 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11147 &catch_fork_breakpoint_ops);
c906108c 11148 break;
44feb3ce
TT
11149 case catch_vfork_temporary:
11150 case catch_vfork_permanent:
a6d9a66e 11151 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
ce78b96d 11152 &catch_vfork_breakpoint_ops);
c906108c 11153 break;
c5aa993b 11154 default:
8a3fe4f8 11155 error (_("unsupported or unknown fork kind; cannot catch it"));
c906108c 11156 break;
c5aa993b 11157 }
c906108c
SS
11158}
11159
11160static void
cc59ec59
MS
11161catch_exec_command_1 (char *arg, int from_tty,
11162 struct cmd_list_element *command)
c906108c 11163{
b4d90040 11164 struct exec_catchpoint *c;
a6d9a66e 11165 struct gdbarch *gdbarch = get_current_arch ();
44feb3ce 11166 int tempflag;
c5aa993b 11167 char *cond_string = NULL;
c906108c 11168
44feb3ce
TT
11169 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11170
11171 if (!arg)
11172 arg = "";
e9cafbcc 11173 arg = skip_spaces (arg);
c906108c
SS
11174
11175 /* The allowed syntax is:
c5aa993b
JM
11176 catch exec
11177 catch exec if <cond>
c906108c 11178
4a64f543 11179 First, check if there's an if clause. */
c906108c
SS
11180 cond_string = ep_parse_optional_if_clause (&arg);
11181
11182 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11183 error (_("Junk at end of arguments."));
c906108c 11184
b4d90040
PA
11185 c = XNEW (struct exec_catchpoint);
11186 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11187 &catch_exec_breakpoint_ops);
11188 c->exec_pathname = NULL;
11189
3ea46bff 11190 install_breakpoint (0, &c->base, 1);
c906108c 11191}
c5aa993b 11192
3086aeae 11193static enum print_stop_action
348d480f 11194print_it_exception_catchpoint (bpstat bs)
3086aeae 11195{
79a45e25 11196 struct ui_out *uiout = current_uiout;
348d480f 11197 struct breakpoint *b = bs->breakpoint_at;
ade92717 11198 int bp_temp, bp_throw;
3086aeae 11199
ade92717 11200 annotate_catchpoint (b->number);
3086aeae 11201
ade92717
AR
11202 bp_throw = strstr (b->addr_string, "throw") != NULL;
11203 if (b->loc->address != b->loc->requested_address)
11204 breakpoint_adjustment_warning (b->loc->requested_address,
11205 b->loc->address,
11206 b->number, 1);
df2b6d2d 11207 bp_temp = b->disposition == disp_del;
ade92717
AR
11208 ui_out_text (uiout,
11209 bp_temp ? "Temporary catchpoint "
11210 : "Catchpoint ");
11211 if (!ui_out_is_mi_like_p (uiout))
11212 ui_out_field_int (uiout, "bkptno", b->number);
11213 ui_out_text (uiout,
c0b37c48
AR
11214 bp_throw ? " (exception thrown), "
11215 : " (exception caught), ");
ade92717
AR
11216 if (ui_out_is_mi_like_p (uiout))
11217 {
11218 ui_out_field_string (uiout, "reason",
11219 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11220 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
11221 ui_out_field_int (uiout, "bkptno", b->number);
11222 }
3086aeae
DJ
11223 return PRINT_SRC_AND_LOC;
11224}
11225
11226static void
cc59ec59
MS
11227print_one_exception_catchpoint (struct breakpoint *b,
11228 struct bp_location **last_loc)
3086aeae 11229{
79a45b7d 11230 struct value_print_options opts;
79a45e25 11231 struct ui_out *uiout = current_uiout;
cc59ec59 11232
79a45b7d
TT
11233 get_user_print_options (&opts);
11234 if (opts.addressprint)
3086aeae
DJ
11235 {
11236 annotate_field (4);
604133b5
AR
11237 if (b->loc == NULL || b->loc->shlib_disabled)
11238 ui_out_field_string (uiout, "addr", "<PENDING>");
11239 else
5af949e3
UW
11240 ui_out_field_core_addr (uiout, "addr",
11241 b->loc->gdbarch, b->loc->address);
3086aeae
DJ
11242 }
11243 annotate_field (5);
604133b5 11244 if (b->loc)
a6d9a66e 11245 *last_loc = b->loc;
3086aeae
DJ
11246 if (strstr (b->addr_string, "throw") != NULL)
11247 ui_out_field_string (uiout, "what", "exception throw");
11248 else
11249 ui_out_field_string (uiout, "what", "exception catch");
11250}
11251
11252static void
11253print_mention_exception_catchpoint (struct breakpoint *b)
11254{
79a45e25 11255 struct ui_out *uiout = current_uiout;
ade92717
AR
11256 int bp_temp;
11257 int bp_throw;
11258
df2b6d2d 11259 bp_temp = b->disposition == disp_del;
ade92717
AR
11260 bp_throw = strstr (b->addr_string, "throw") != NULL;
11261 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
11262 : _("Catchpoint "));
11263 ui_out_field_int (uiout, "bkptno", b->number);
11264 ui_out_text (uiout, bp_throw ? _(" (throw)")
11265 : _(" (catch)"));
3086aeae
DJ
11266}
11267
6149aea9
PA
11268/* Implement the "print_recreate" breakpoint_ops method for throw and
11269 catch catchpoints. */
11270
11271static void
4a64f543
MS
11272print_recreate_exception_catchpoint (struct breakpoint *b,
11273 struct ui_file *fp)
6149aea9
PA
11274{
11275 int bp_temp;
11276 int bp_throw;
11277
11278 bp_temp = b->disposition == disp_del;
11279 bp_throw = strstr (b->addr_string, "throw") != NULL;
11280 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
11281 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
d9b3f62e 11282 print_recreate_thread (b, fp);
6149aea9
PA
11283}
11284
2060206e 11285static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
3086aeae
DJ
11286
11287static int
11288handle_gnu_v3_exceptions (int tempflag, char *cond_string,
11289 enum exception_event_kind ex_event, int from_tty)
11290{
604133b5
AR
11291 char *trigger_func_name;
11292
3086aeae 11293 if (ex_event == EX_EVENT_CATCH)
604133b5 11294 trigger_func_name = "__cxa_begin_catch";
3086aeae 11295 else
604133b5 11296 trigger_func_name = "__cxa_throw";
3086aeae 11297
8cdf0e15 11298 create_breakpoint (get_current_arch (),
e7e0cddf 11299 trigger_func_name, cond_string, -1, NULL,
8cdf0e15 11300 0 /* condition and thread are valid. */,
0fb4aa4b 11301 tempflag, bp_breakpoint,
8cdf0e15
VP
11302 0,
11303 AUTO_BOOLEAN_TRUE /* pending */,
11304 &gnu_v3_exception_catchpoint_ops, from_tty,
84f4c1fe 11305 1 /* enabled */,
44f238bb
PA
11306 0 /* internal */,
11307 0);
3086aeae 11308
3086aeae
DJ
11309 return 1;
11310}
11311
4a64f543 11312/* Deal with "catch catch" and "catch throw" commands. */
c906108c
SS
11313
11314static void
fba45db2
KB
11315catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
11316 int tempflag, int from_tty)
c906108c 11317{
c5aa993b 11318 char *cond_string = NULL;
c5aa993b 11319
44feb3ce
TT
11320 if (!arg)
11321 arg = "";
e9cafbcc 11322 arg = skip_spaces (arg);
c5aa993b 11323
c906108c
SS
11324 cond_string = ep_parse_optional_if_clause (&arg);
11325
11326 if ((*arg != '\0') && !isspace (*arg))
8a3fe4f8 11327 error (_("Junk at end of arguments."));
c906108c 11328
059fb39f
PM
11329 if (ex_event != EX_EVENT_THROW
11330 && ex_event != EX_EVENT_CATCH)
8a3fe4f8 11331 error (_("Unsupported or unknown exception event; cannot catch it"));
c906108c 11332
3086aeae
DJ
11333 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
11334 return;
11335
8a3fe4f8 11336 warning (_("Unsupported with this platform/compiler combination."));
c906108c
SS
11337}
11338
44feb3ce
TT
11339/* Implementation of "catch catch" command. */
11340
11341static void
11342catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
11343{
11344 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 11345
44feb3ce
TT
11346 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
11347}
11348
11349/* Implementation of "catch throw" command. */
11350
11351static void
11352catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
11353{
11354 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
cc59ec59 11355
44feb3ce
TT
11356 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
11357}
11358
9ac4176b 11359void
28010a5d
PA
11360init_ada_exception_breakpoint (struct breakpoint *b,
11361 struct gdbarch *gdbarch,
11362 struct symtab_and_line sal,
11363 char *addr_string,
c0a91b2b 11364 const struct breakpoint_ops *ops,
28010a5d
PA
11365 int tempflag,
11366 int from_tty)
f7f9143b 11367{
f7f9143b
JB
11368 if (from_tty)
11369 {
5af949e3
UW
11370 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11371 if (!loc_gdbarch)
11372 loc_gdbarch = gdbarch;
11373
6c95b8df
PA
11374 describe_other_breakpoints (loc_gdbarch,
11375 sal.pspace, sal.pc, sal.section, -1);
f7f9143b
JB
11376 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11377 version for exception catchpoints, because two catchpoints
11378 used for different exception names will use the same address.
11379 In this case, a "breakpoint ... also set at..." warning is
4a64f543 11380 unproductive. Besides, the warning phrasing is also a bit
e5dd4106 11381 inappropriate, we should use the word catchpoint, and tell
f7f9143b
JB
11382 the user what type of catchpoint it is. The above is good
11383 enough for now, though. */
11384 }
11385
28010a5d 11386 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
f7f9143b
JB
11387
11388 b->enable_state = bp_enabled;
11389 b->disposition = tempflag ? disp_del : disp_donttouch;
f7f9143b
JB
11390 b->addr_string = addr_string;
11391 b->language = language_ada;
f7f9143b
JB
11392}
11393
a96d9b2e
SDJ
11394/* Splits the argument using space as delimiter. Returns an xmalloc'd
11395 filter list, or NULL if no filtering is required. */
11396static VEC(int) *
11397catch_syscall_split_args (char *arg)
11398{
11399 VEC(int) *result = NULL;
29d0bb3d 11400 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
a96d9b2e
SDJ
11401
11402 while (*arg != '\0')
11403 {
11404 int i, syscall_number;
11405 char *endptr;
11406 char cur_name[128];
11407 struct syscall s;
11408
11409 /* Skip whitespace. */
11410 while (isspace (*arg))
11411 arg++;
11412
11413 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11414 cur_name[i] = arg[i];
11415 cur_name[i] = '\0';
11416 arg += i;
11417
11418 /* Check if the user provided a syscall name or a number. */
11419 syscall_number = (int) strtol (cur_name, &endptr, 0);
11420 if (*endptr == '\0')
bccd0dd2 11421 get_syscall_by_number (syscall_number, &s);
a96d9b2e
SDJ
11422 else
11423 {
11424 /* We have a name. Let's check if it's valid and convert it
11425 to a number. */
11426 get_syscall_by_name (cur_name, &s);
11427
11428 if (s.number == UNKNOWN_SYSCALL)
4a64f543
MS
11429 /* Here we have to issue an error instead of a warning,
11430 because GDB cannot do anything useful if there's no
11431 syscall number to be caught. */
a96d9b2e
SDJ
11432 error (_("Unknown syscall name '%s'."), cur_name);
11433 }
11434
11435 /* Ok, it's valid. */
11436 VEC_safe_push (int, result, s.number);
11437 }
11438
11439 discard_cleanups (cleanup);
11440 return result;
11441}
11442
11443/* Implement the "catch syscall" command. */
11444
11445static void
cc59ec59
MS
11446catch_syscall_command_1 (char *arg, int from_tty,
11447 struct cmd_list_element *command)
a96d9b2e
SDJ
11448{
11449 int tempflag;
11450 VEC(int) *filter;
11451 struct syscall s;
11452 struct gdbarch *gdbarch = get_current_arch ();
11453
11454 /* Checking if the feature if supported. */
11455 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11456 error (_("The feature 'catch syscall' is not supported on \
ea666128 11457this architecture yet."));
a96d9b2e
SDJ
11458
11459 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11460
e9cafbcc 11461 arg = skip_spaces (arg);
a96d9b2e
SDJ
11462
11463 /* We need to do this first "dummy" translation in order
11464 to get the syscall XML file loaded or, most important,
11465 to display a warning to the user if there's no XML file
11466 for his/her architecture. */
11467 get_syscall_by_number (0, &s);
11468
11469 /* The allowed syntax is:
11470 catch syscall
11471 catch syscall <name | number> [<name | number> ... <name | number>]
11472
11473 Let's check if there's a syscall name. */
11474
11475 if (arg != NULL)
11476 filter = catch_syscall_split_args (arg);
11477 else
11478 filter = NULL;
11479
11480 create_syscall_event_catchpoint (tempflag, filter,
11481 &catch_syscall_breakpoint_ops);
11482}
11483
c906108c 11484static void
fba45db2 11485catch_command (char *arg, int from_tty)
c906108c 11486{
44feb3ce 11487 error (_("Catch requires an event name."));
c906108c
SS
11488}
11489\f
11490
11491static void
fba45db2 11492tcatch_command (char *arg, int from_tty)
c906108c 11493{
44feb3ce 11494 error (_("Catch requires an event name."));
c906108c
SS
11495}
11496
8a2c437b
TT
11497/* A qsort comparison function that sorts breakpoints in order. */
11498
11499static int
11500compare_breakpoints (const void *a, const void *b)
11501{
11502 const breakpoint_p *ba = a;
11503 uintptr_t ua = (uintptr_t) *ba;
11504 const breakpoint_p *bb = b;
11505 uintptr_t ub = (uintptr_t) *bb;
11506
11507 if ((*ba)->number < (*bb)->number)
11508 return -1;
11509 else if ((*ba)->number > (*bb)->number)
11510 return 1;
11511
11512 /* Now sort by address, in case we see, e..g, two breakpoints with
11513 the number 0. */
11514 if (ua < ub)
11515 return -1;
11516 return ub > ub ? 1 : 0;
11517}
11518
80f8a6eb 11519/* Delete breakpoints by address or line. */
c906108c
SS
11520
11521static void
fba45db2 11522clear_command (char *arg, int from_tty)
c906108c 11523{
8a2c437b 11524 struct breakpoint *b, *prev;
d6e956e5
VP
11525 VEC(breakpoint_p) *found = 0;
11526 int ix;
c906108c
SS
11527 int default_match;
11528 struct symtabs_and_lines sals;
11529 struct symtab_and_line sal;
c906108c 11530 int i;
8a2c437b 11531 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
c906108c
SS
11532
11533 if (arg)
11534 {
39cf75f7
DE
11535 sals = decode_line_with_current_source (arg,
11536 (DECODE_LINE_FUNFIRSTLINE
11537 | DECODE_LINE_LIST_MODE));
c906108c
SS
11538 default_match = 0;
11539 }
11540 else
11541 {
c5aa993b 11542 sals.sals = (struct symtab_and_line *)
c906108c 11543 xmalloc (sizeof (struct symtab_and_line));
80f8a6eb 11544 make_cleanup (xfree, sals.sals);
4a64f543 11545 init_sal (&sal); /* Initialize to zeroes. */
1bfeeb0f
JL
11546
11547 /* Set sal's line, symtab, pc, and pspace to the values
11548 corresponding to the last call to print_frame_info. If the
11549 codepoint is not valid, this will set all the fields to 0. */
11550 get_last_displayed_sal (&sal);
c906108c 11551 if (sal.symtab == 0)
8a3fe4f8 11552 error (_("No source file specified."));
c906108c
SS
11553
11554 sals.sals[0] = sal;
11555 sals.nelts = 1;
11556
11557 default_match = 1;
11558 }
11559
4a64f543
MS
11560 /* We don't call resolve_sal_pc here. That's not as bad as it
11561 seems, because all existing breakpoints typically have both
11562 file/line and pc set. So, if clear is given file/line, we can
11563 match this to existing breakpoint without obtaining pc at all.
ed0616c6
VP
11564
11565 We only support clearing given the address explicitly
11566 present in breakpoint table. Say, we've set breakpoint
4a64f543 11567 at file:line. There were several PC values for that file:line,
ed0616c6 11568 due to optimization, all in one block.
4a64f543
MS
11569
11570 We've picked one PC value. If "clear" is issued with another
ed0616c6
VP
11571 PC corresponding to the same file:line, the breakpoint won't
11572 be cleared. We probably can still clear the breakpoint, but
11573 since the other PC value is never presented to user, user
11574 can only find it by guessing, and it does not seem important
11575 to support that. */
11576
4a64f543
MS
11577 /* For each line spec given, delete bps which correspond to it. Do
11578 it in two passes, solely to preserve the current behavior that
11579 from_tty is forced true if we delete more than one
11580 breakpoint. */
c906108c 11581
80f8a6eb 11582 found = NULL;
8a2c437b 11583 make_cleanup (VEC_cleanup (breakpoint_p), &found);
c906108c
SS
11584 for (i = 0; i < sals.nelts; i++)
11585 {
4aac40c8
TT
11586 int is_abs, sal_name_len;
11587
c906108c 11588 /* If exact pc given, clear bpts at that pc.
c5aa993b
JM
11589 If line given (pc == 0), clear all bpts on specified line.
11590 If defaulting, clear all bpts on default line
c906108c 11591 or at default pc.
c5aa993b
JM
11592
11593 defaulting sal.pc != 0 tests to do
11594
11595 0 1 pc
11596 1 1 pc _and_ line
11597 0 0 line
11598 1 0 <can't happen> */
c906108c
SS
11599
11600 sal = sals.sals[i];
4aac40c8
TT
11601 is_abs = sal.symtab == NULL ? 1 : IS_ABSOLUTE_PATH (sal.symtab->filename);
11602 sal_name_len = is_abs ? 0 : strlen (sal.symtab->filename);
c906108c 11603
4a64f543 11604 /* Find all matching breakpoints and add them to 'found'. */
d6e956e5 11605 ALL_BREAKPOINTS (b)
c5aa993b 11606 {
0d381245 11607 int match = 0;
4a64f543 11608 /* Are we going to delete b? */
cc60f2e3 11609 if (b->type != bp_none && !is_watchpoint (b))
0d381245
VP
11610 {
11611 struct bp_location *loc = b->loc;
11612 for (; loc; loc = loc->next)
11613 {
f8eba3c6
TT
11614 /* If the user specified file:line, don't allow a PC
11615 match. This matches historical gdb behavior. */
11616 int pc_match = (!sal.explicit_line
11617 && sal.pc
11618 && (loc->pspace == sal.pspace)
11619 && (loc->address == sal.pc)
11620 && (!section_is_overlay (loc->section)
11621 || loc->section == sal.section));
4aac40c8
TT
11622 int line_match = 0;
11623
11624 if ((default_match || sal.explicit_line)
11625 && loc->source_file != NULL
11626 && sal.symtab != NULL
11627 && sal.pspace == loc->pspace
11628 && loc->line_number == sal.line)
11629 {
11630 if (filename_cmp (loc->source_file,
11631 sal.symtab->filename) == 0)
11632 line_match = 1;
11633 else if (!IS_ABSOLUTE_PATH (sal.symtab->filename)
11634 && compare_filenames_for_search (loc->source_file,
11635 sal.symtab->filename,
11636 sal_name_len))
11637 line_match = 1;
11638 }
11639
0d381245
VP
11640 if (pc_match || line_match)
11641 {
11642 match = 1;
11643 break;
11644 }
11645 }
11646 }
11647
11648 if (match)
d6e956e5 11649 VEC_safe_push(breakpoint_p, found, b);
c906108c 11650 }
80f8a6eb 11651 }
8a2c437b 11652
80f8a6eb 11653 /* Now go thru the 'found' chain and delete them. */
d6e956e5 11654 if (VEC_empty(breakpoint_p, found))
80f8a6eb
MS
11655 {
11656 if (arg)
8a3fe4f8 11657 error (_("No breakpoint at %s."), arg);
80f8a6eb 11658 else
8a3fe4f8 11659 error (_("No breakpoint at this line."));
80f8a6eb 11660 }
c906108c 11661
8a2c437b
TT
11662 /* Remove duplicates from the vec. */
11663 qsort (VEC_address (breakpoint_p, found),
11664 VEC_length (breakpoint_p, found),
11665 sizeof (breakpoint_p),
11666 compare_breakpoints);
11667 prev = VEC_index (breakpoint_p, found, 0);
11668 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11669 {
11670 if (b == prev)
11671 {
11672 VEC_ordered_remove (breakpoint_p, found, ix);
11673 --ix;
11674 }
11675 }
11676
d6e956e5 11677 if (VEC_length(breakpoint_p, found) > 1)
4a64f543 11678 from_tty = 1; /* Always report if deleted more than one. */
80f8a6eb 11679 if (from_tty)
a3f17187 11680 {
d6e956e5 11681 if (VEC_length(breakpoint_p, found) == 1)
a3f17187
AC
11682 printf_unfiltered (_("Deleted breakpoint "));
11683 else
11684 printf_unfiltered (_("Deleted breakpoints "));
11685 }
80f8a6eb 11686 breakpoints_changed ();
d6e956e5
VP
11687
11688 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
80f8a6eb 11689 {
c5aa993b 11690 if (from_tty)
d6e956e5
VP
11691 printf_unfiltered ("%d ", b->number);
11692 delete_breakpoint (b);
c906108c 11693 }
80f8a6eb
MS
11694 if (from_tty)
11695 putchar_unfiltered ('\n');
8a2c437b
TT
11696
11697 do_cleanups (cleanups);
c906108c
SS
11698}
11699\f
11700/* Delete breakpoint in BS if they are `delete' breakpoints and
11701 all breakpoints that are marked for deletion, whether hit or not.
11702 This is called after any breakpoint is hit, or after errors. */
11703
11704void
fba45db2 11705breakpoint_auto_delete (bpstat bs)
c906108c 11706{
35df4500 11707 struct breakpoint *b, *b_tmp;
c906108c
SS
11708
11709 for (; bs; bs = bs->next)
f431efe5
PA
11710 if (bs->breakpoint_at
11711 && bs->breakpoint_at->disposition == disp_del
c906108c 11712 && bs->stop)
f431efe5 11713 delete_breakpoint (bs->breakpoint_at);
c906108c 11714
35df4500 11715 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 11716 {
b5de0fa7 11717 if (b->disposition == disp_del_at_next_stop)
c5aa993b
JM
11718 delete_breakpoint (b);
11719 }
c906108c
SS
11720}
11721
4a64f543
MS
11722/* A comparison function for bp_location AP and BP being interfaced to
11723 qsort. Sort elements primarily by their ADDRESS (no matter what
11724 does breakpoint_address_is_meaningful say for its OWNER),
11725 secondarily by ordering first bp_permanent OWNERed elements and
11726 terciarily just ensuring the array is sorted stable way despite
e5dd4106 11727 qsort being an unstable algorithm. */
876fa593
JK
11728
11729static int
494cfb0f 11730bp_location_compare (const void *ap, const void *bp)
876fa593 11731{
494cfb0f
JK
11732 struct bp_location *a = *(void **) ap;
11733 struct bp_location *b = *(void **) bp;
2bdf28a0 11734 /* A and B come from existing breakpoints having non-NULL OWNER. */
876fa593
JK
11735 int a_perm = a->owner->enable_state == bp_permanent;
11736 int b_perm = b->owner->enable_state == bp_permanent;
11737
11738 if (a->address != b->address)
11739 return (a->address > b->address) - (a->address < b->address);
11740
dea2aa5f
LM
11741 /* Sort locations at the same address by their pspace number, keeping
11742 locations of the same inferior (in a multi-inferior environment)
11743 grouped. */
11744
11745 if (a->pspace->num != b->pspace->num)
11746 return ((a->pspace->num > b->pspace->num)
11747 - (a->pspace->num < b->pspace->num));
11748
876fa593
JK
11749 /* Sort permanent breakpoints first. */
11750 if (a_perm != b_perm)
11751 return (a_perm < b_perm) - (a_perm > b_perm);
11752
c56a97f9
JK
11753 /* Make the internal GDB representation stable across GDB runs
11754 where A and B memory inside GDB can differ. Breakpoint locations of
11755 the same type at the same address can be sorted in arbitrary order. */
876fa593
JK
11756
11757 if (a->owner->number != b->owner->number)
c56a97f9
JK
11758 return ((a->owner->number > b->owner->number)
11759 - (a->owner->number < b->owner->number));
876fa593
JK
11760
11761 return (a > b) - (a < b);
11762}
11763
876fa593 11764/* Set bp_location_placed_address_before_address_max and
4a64f543
MS
11765 bp_location_shadow_len_after_address_max according to the current
11766 content of the bp_location array. */
f7545552
TT
11767
11768static void
876fa593 11769bp_location_target_extensions_update (void)
f7545552 11770{
876fa593
JK
11771 struct bp_location *bl, **blp_tmp;
11772
11773 bp_location_placed_address_before_address_max = 0;
11774 bp_location_shadow_len_after_address_max = 0;
11775
11776 ALL_BP_LOCATIONS (bl, blp_tmp)
11777 {
11778 CORE_ADDR start, end, addr;
11779
11780 if (!bp_location_has_shadow (bl))
11781 continue;
11782
11783 start = bl->target_info.placed_address;
11784 end = start + bl->target_info.shadow_len;
11785
11786 gdb_assert (bl->address >= start);
11787 addr = bl->address - start;
11788 if (addr > bp_location_placed_address_before_address_max)
11789 bp_location_placed_address_before_address_max = addr;
11790
11791 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11792
11793 gdb_assert (bl->address < end);
11794 addr = end - bl->address;
11795 if (addr > bp_location_shadow_len_after_address_max)
11796 bp_location_shadow_len_after_address_max = addr;
11797 }
f7545552
TT
11798}
11799
1e4d1764
YQ
11800/* Download tracepoint locations if they haven't been. */
11801
11802static void
11803download_tracepoint_locations (void)
11804{
11805 struct bp_location *bl, **blp_tmp;
11806 struct cleanup *old_chain;
11807
11808 if (!target_can_download_tracepoint ())
11809 return;
11810
11811 old_chain = save_current_space_and_thread ();
11812
11813 ALL_BP_LOCATIONS (bl, blp_tmp)
11814 {
11815 struct tracepoint *t;
11816
11817 if (!is_tracepoint (bl->owner))
11818 continue;
11819
11820 if ((bl->owner->type == bp_fast_tracepoint
11821 ? !may_insert_fast_tracepoints
11822 : !may_insert_tracepoints))
11823 continue;
11824
11825 /* In tracepoint, locations are _never_ duplicated, so
11826 should_be_inserted is equivalent to
11827 unduplicated_should_be_inserted. */
11828 if (!should_be_inserted (bl) || bl->inserted)
11829 continue;
11830
11831 switch_to_program_space_and_thread (bl->pspace);
11832
11833 target_download_tracepoint (bl);
11834
11835 bl->inserted = 1;
11836 t = (struct tracepoint *) bl->owner;
11837 t->number_on_target = bl->owner->number;
11838 }
11839
11840 do_cleanups (old_chain);
11841}
11842
934709f0
PW
11843/* Swap the insertion/duplication state between two locations. */
11844
11845static void
11846swap_insertion (struct bp_location *left, struct bp_location *right)
11847{
11848 const int left_inserted = left->inserted;
11849 const int left_duplicate = left->duplicate;
b775012e 11850 const int left_needs_update = left->needs_update;
934709f0
PW
11851 const struct bp_target_info left_target_info = left->target_info;
11852
1e4d1764
YQ
11853 /* Locations of tracepoints can never be duplicated. */
11854 if (is_tracepoint (left->owner))
11855 gdb_assert (!left->duplicate);
11856 if (is_tracepoint (right->owner))
11857 gdb_assert (!right->duplicate);
11858
934709f0
PW
11859 left->inserted = right->inserted;
11860 left->duplicate = right->duplicate;
b775012e 11861 left->needs_update = right->needs_update;
934709f0
PW
11862 left->target_info = right->target_info;
11863 right->inserted = left_inserted;
11864 right->duplicate = left_duplicate;
b775012e 11865 right->needs_update = left_needs_update;
934709f0
PW
11866 right->target_info = left_target_info;
11867}
11868
b775012e
LM
11869/* Force the re-insertion of the locations at ADDRESS. This is called
11870 once a new/deleted/modified duplicate location is found and we are evaluating
11871 conditions on the target's side. Such conditions need to be updated on
11872 the target. */
11873
11874static void
11875force_breakpoint_reinsertion (struct bp_location *bl)
11876{
11877 struct bp_location **locp = NULL, **loc2p;
11878 struct bp_location *loc;
11879 CORE_ADDR address = 0;
11880 int pspace_num;
11881
11882 address = bl->address;
11883 pspace_num = bl->pspace->num;
11884
11885 /* This is only meaningful if the target is
11886 evaluating conditions and if the user has
11887 opted for condition evaluation on the target's
11888 side. */
11889 if (gdb_evaluates_breakpoint_condition_p ()
11890 || !target_supports_evaluation_of_breakpoint_conditions ())
11891 return;
11892
11893 /* Flag all breakpoint locations with this address and
11894 the same program space as the location
11895 as "its condition has changed". We need to
11896 update the conditions on the target's side. */
11897 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11898 {
11899 loc = *loc2p;
11900
11901 if (!is_breakpoint (loc->owner)
11902 || pspace_num != loc->pspace->num)
11903 continue;
11904
11905 /* Flag the location appropriately. We use a different state to
11906 let everyone know that we already updated the set of locations
11907 with addr bl->address and program space bl->pspace. This is so
11908 we don't have to keep calling these functions just to mark locations
11909 that have already been marked. */
11910 loc->condition_changed = condition_updated;
11911
11912 /* Free the agent expression bytecode as well. We will compute
11913 it later on. */
11914 if (loc->cond_bytecode)
11915 {
11916 free_agent_expr (loc->cond_bytecode);
11917 loc->cond_bytecode = NULL;
11918 }
11919 }
11920}
11921
4cd9bd08 11922/* If SHOULD_INSERT is false, do not insert any breakpoint locations
b60e7edf
PA
11923 into the inferior, only remove already-inserted locations that no
11924 longer should be inserted. Functions that delete a breakpoint or
11925 breakpoints should pass false, so that deleting a breakpoint
11926 doesn't have the side effect of inserting the locations of other
11927 breakpoints that are marked not-inserted, but should_be_inserted
11928 returns true on them.
11929
11930 This behaviour is useful is situations close to tear-down -- e.g.,
11931 after an exec, while the target still has execution, but breakpoint
11932 shadows of the previous executable image should *NOT* be restored
11933 to the new image; or before detaching, where the target still has
11934 execution and wants to delete breakpoints from GDB's lists, and all
11935 breakpoints had already been removed from the inferior. */
11936
0d381245 11937static void
b60e7edf 11938update_global_location_list (int should_insert)
0d381245 11939{
74960c60 11940 struct breakpoint *b;
876fa593 11941 struct bp_location **locp, *loc;
f7545552 11942 struct cleanup *cleanups;
b775012e
LM
11943 /* Last breakpoint location address that was marked for update. */
11944 CORE_ADDR last_addr = 0;
11945 /* Last breakpoint location program space that was marked for update. */
11946 int last_pspace_num = -1;
f7545552 11947
2d134ed3
PA
11948 /* Used in the duplicates detection below. When iterating over all
11949 bp_locations, points to the first bp_location of a given address.
11950 Breakpoints and watchpoints of different types are never
11951 duplicates of each other. Keep one pointer for each type of
11952 breakpoint/watchpoint, so we only need to loop over all locations
11953 once. */
11954 struct bp_location *bp_loc_first; /* breakpoint */
11955 struct bp_location *wp_loc_first; /* hardware watchpoint */
11956 struct bp_location *awp_loc_first; /* access watchpoint */
11957 struct bp_location *rwp_loc_first; /* read watchpoint */
876fa593 11958
4a64f543
MS
11959 /* Saved former bp_location array which we compare against the newly
11960 built bp_location from the current state of ALL_BREAKPOINTS. */
876fa593
JK
11961 struct bp_location **old_location, **old_locp;
11962 unsigned old_location_count;
11963
11964 old_location = bp_location;
11965 old_location_count = bp_location_count;
11966 bp_location = NULL;
11967 bp_location_count = 0;
11968 cleanups = make_cleanup (xfree, old_location);
0d381245 11969
74960c60 11970 ALL_BREAKPOINTS (b)
876fa593
JK
11971 for (loc = b->loc; loc; loc = loc->next)
11972 bp_location_count++;
11973
11974 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
11975 locp = bp_location;
11976 ALL_BREAKPOINTS (b)
11977 for (loc = b->loc; loc; loc = loc->next)
11978 *locp++ = loc;
11979 qsort (bp_location, bp_location_count, sizeof (*bp_location),
494cfb0f 11980 bp_location_compare);
876fa593
JK
11981
11982 bp_location_target_extensions_update ();
74960c60 11983
4a64f543
MS
11984 /* Identify bp_location instances that are no longer present in the
11985 new list, and therefore should be freed. Note that it's not
11986 necessary that those locations should be removed from inferior --
11987 if there's another location at the same address (previously
11988 marked as duplicate), we don't need to remove/insert the
11989 location.
876fa593 11990
4a64f543
MS
11991 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11992 and former bp_location array state respectively. */
876fa593
JK
11993
11994 locp = bp_location;
11995 for (old_locp = old_location; old_locp < old_location + old_location_count;
11996 old_locp++)
74960c60 11997 {
876fa593 11998 struct bp_location *old_loc = *old_locp;
c7d46a38 11999 struct bp_location **loc2p;
876fa593 12000
e5dd4106 12001 /* Tells if 'old_loc' is found among the new locations. If
4a64f543 12002 not, we have to free it. */
c7d46a38 12003 int found_object = 0;
20874c92
VP
12004 /* Tells if the location should remain inserted in the target. */
12005 int keep_in_target = 0;
12006 int removed = 0;
876fa593 12007
4a64f543
MS
12008 /* Skip LOCP entries which will definitely never be needed.
12009 Stop either at or being the one matching OLD_LOC. */
876fa593 12010 while (locp < bp_location + bp_location_count
c7d46a38 12011 && (*locp)->address < old_loc->address)
876fa593 12012 locp++;
c7d46a38
PA
12013
12014 for (loc2p = locp;
12015 (loc2p < bp_location + bp_location_count
12016 && (*loc2p)->address == old_loc->address);
12017 loc2p++)
12018 {
b775012e
LM
12019 /* Check if this is a new/duplicated location or a duplicated
12020 location that had its condition modified. If so, we want to send
12021 its condition to the target if evaluation of conditions is taking
12022 place there. */
12023 if ((*loc2p)->condition_changed == condition_modified
12024 && (last_addr != old_loc->address
12025 || last_pspace_num != old_loc->pspace->num))
c7d46a38 12026 {
b775012e
LM
12027 force_breakpoint_reinsertion (*loc2p);
12028 last_pspace_num = old_loc->pspace->num;
c7d46a38 12029 }
b775012e
LM
12030
12031 if (*loc2p == old_loc)
12032 found_object = 1;
c7d46a38 12033 }
74960c60 12034
b775012e
LM
12035 /* We have already handled this address, update it so that we don't
12036 have to go through updates again. */
12037 last_addr = old_loc->address;
12038
12039 /* Target-side condition evaluation: Handle deleted locations. */
12040 if (!found_object)
12041 force_breakpoint_reinsertion (old_loc);
12042
4a64f543
MS
12043 /* If this location is no longer present, and inserted, look if
12044 there's maybe a new location at the same address. If so,
12045 mark that one inserted, and don't remove this one. This is
12046 needed so that we don't have a time window where a breakpoint
12047 at certain location is not inserted. */
74960c60 12048
876fa593 12049 if (old_loc->inserted)
0d381245 12050 {
4a64f543
MS
12051 /* If the location is inserted now, we might have to remove
12052 it. */
74960c60 12053
876fa593 12054 if (found_object && should_be_inserted (old_loc))
74960c60 12055 {
4a64f543
MS
12056 /* The location is still present in the location list,
12057 and still should be inserted. Don't do anything. */
20874c92 12058 keep_in_target = 1;
74960c60
VP
12059 }
12060 else
12061 {
b775012e
LM
12062 /* This location still exists, but it won't be kept in the
12063 target since it may have been disabled. We proceed to
12064 remove its target-side condition. */
12065
4a64f543
MS
12066 /* The location is either no longer present, or got
12067 disabled. See if there's another location at the
12068 same address, in which case we don't need to remove
12069 this one from the target. */
876fa593 12070
2bdf28a0 12071 /* OLD_LOC comes from existing struct breakpoint. */
876fa593
JK
12072 if (breakpoint_address_is_meaningful (old_loc->owner))
12073 {
876fa593 12074 for (loc2p = locp;
c7d46a38
PA
12075 (loc2p < bp_location + bp_location_count
12076 && (*loc2p)->address == old_loc->address);
876fa593
JK
12077 loc2p++)
12078 {
12079 struct bp_location *loc2 = *loc2p;
12080
2d134ed3 12081 if (breakpoint_locations_match (loc2, old_loc))
c7d46a38 12082 {
85d721b8
PA
12083 /* Read watchpoint locations are switched to
12084 access watchpoints, if the former are not
12085 supported, but the latter are. */
12086 if (is_hardware_watchpoint (old_loc->owner))
12087 {
12088 gdb_assert (is_hardware_watchpoint (loc2->owner));
12089 loc2->watchpoint_type = old_loc->watchpoint_type;
12090 }
12091
934709f0
PW
12092 /* loc2 is a duplicated location. We need to check
12093 if it should be inserted in case it will be
12094 unduplicated. */
12095 if (loc2 != old_loc
12096 && unduplicated_should_be_inserted (loc2))
c7d46a38 12097 {
934709f0 12098 swap_insertion (old_loc, loc2);
c7d46a38
PA
12099 keep_in_target = 1;
12100 break;
12101 }
876fa593
JK
12102 }
12103 }
12104 }
74960c60
VP
12105 }
12106
20874c92
VP
12107 if (!keep_in_target)
12108 {
876fa593 12109 if (remove_breakpoint (old_loc, mark_uninserted))
20874c92 12110 {
4a64f543
MS
12111 /* This is just about all we can do. We could keep
12112 this location on the global list, and try to
12113 remove it next time, but there's no particular
12114 reason why we will succeed next time.
20874c92 12115
4a64f543
MS
12116 Note that at this point, old_loc->owner is still
12117 valid, as delete_breakpoint frees the breakpoint
12118 only after calling us. */
3e43a32a
MS
12119 printf_filtered (_("warning: Error removing "
12120 "breakpoint %d\n"),
876fa593 12121 old_loc->owner->number);
20874c92
VP
12122 }
12123 removed = 1;
12124 }
0d381245 12125 }
74960c60
VP
12126
12127 if (!found_object)
1c5cfe86 12128 {
db82e815
PA
12129 if (removed && non_stop
12130 && breakpoint_address_is_meaningful (old_loc->owner)
12131 && !is_hardware_watchpoint (old_loc->owner))
20874c92 12132 {
db82e815
PA
12133 /* This location was removed from the target. In
12134 non-stop mode, a race condition is possible where
12135 we've removed a breakpoint, but stop events for that
12136 breakpoint are already queued and will arrive later.
12137 We apply an heuristic to be able to distinguish such
12138 SIGTRAPs from other random SIGTRAPs: we keep this
12139 breakpoint location for a bit, and will retire it
12140 after we see some number of events. The theory here
12141 is that reporting of events should, "on the average",
12142 be fair, so after a while we'll see events from all
12143 threads that have anything of interest, and no longer
12144 need to keep this breakpoint location around. We
12145 don't hold locations forever so to reduce chances of
12146 mistaking a non-breakpoint SIGTRAP for a breakpoint
12147 SIGTRAP.
12148
12149 The heuristic failing can be disastrous on
12150 decr_pc_after_break targets.
12151
12152 On decr_pc_after_break targets, like e.g., x86-linux,
12153 if we fail to recognize a late breakpoint SIGTRAP,
12154 because events_till_retirement has reached 0 too
12155 soon, we'll fail to do the PC adjustment, and report
12156 a random SIGTRAP to the user. When the user resumes
12157 the inferior, it will most likely immediately crash
2dec564e 12158 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
db82e815
PA
12159 corrupted, because of being resumed e.g., in the
12160 middle of a multi-byte instruction, or skipped a
12161 one-byte instruction. This was actually seen happen
12162 on native x86-linux, and should be less rare on
12163 targets that do not support new thread events, like
12164 remote, due to the heuristic depending on
12165 thread_count.
12166
12167 Mistaking a random SIGTRAP for a breakpoint trap
12168 causes similar symptoms (PC adjustment applied when
12169 it shouldn't), but then again, playing with SIGTRAPs
12170 behind the debugger's back is asking for trouble.
12171
12172 Since hardware watchpoint traps are always
12173 distinguishable from other traps, so we don't need to
12174 apply keep hardware watchpoint moribund locations
12175 around. We simply always ignore hardware watchpoint
12176 traps we can no longer explain. */
12177
876fa593
JK
12178 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12179 old_loc->owner = NULL;
20874c92 12180
876fa593 12181 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
1c5cfe86
PA
12182 }
12183 else
f431efe5
PA
12184 {
12185 old_loc->owner = NULL;
12186 decref_bp_location (&old_loc);
12187 }
20874c92 12188 }
74960c60 12189 }
1c5cfe86 12190
348d480f
PA
12191 /* Rescan breakpoints at the same address and section, marking the
12192 first one as "first" and any others as "duplicates". This is so
12193 that the bpt instruction is only inserted once. If we have a
12194 permanent breakpoint at the same place as BPT, make that one the
12195 official one, and the rest as duplicates. Permanent breakpoints
12196 are sorted first for the same address.
12197
12198 Do the same for hardware watchpoints, but also considering the
12199 watchpoint's type (regular/access/read) and length. */
12200
12201 bp_loc_first = NULL;
12202 wp_loc_first = NULL;
12203 awp_loc_first = NULL;
12204 rwp_loc_first = NULL;
12205 ALL_BP_LOCATIONS (loc, locp)
12206 {
12207 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12208 non-NULL. */
348d480f 12209 struct bp_location **loc_first_p;
d3fbdd86 12210 b = loc->owner;
348d480f 12211
f8eba3c6 12212 if (!should_be_inserted (loc)
348d480f 12213 || !breakpoint_address_is_meaningful (b)
1e4d1764
YQ
12214 /* Don't detect duplicate for tracepoint locations because they are
12215 never duplicated. See the comments in field `duplicate' of
12216 `struct bp_location'. */
348d480f 12217 || is_tracepoint (b))
b775012e
LM
12218 {
12219 /* Clear the condition modification flag. */
12220 loc->condition_changed = condition_unchanged;
12221 continue;
12222 }
348d480f
PA
12223
12224 /* Permanent breakpoint should always be inserted. */
12225 if (b->enable_state == bp_permanent && ! loc->inserted)
12226 internal_error (__FILE__, __LINE__,
12227 _("allegedly permanent breakpoint is not "
12228 "actually inserted"));
12229
12230 if (b->type == bp_hardware_watchpoint)
12231 loc_first_p = &wp_loc_first;
12232 else if (b->type == bp_read_watchpoint)
12233 loc_first_p = &rwp_loc_first;
12234 else if (b->type == bp_access_watchpoint)
12235 loc_first_p = &awp_loc_first;
12236 else
12237 loc_first_p = &bp_loc_first;
12238
12239 if (*loc_first_p == NULL
12240 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12241 || !breakpoint_locations_match (loc, *loc_first_p))
12242 {
12243 *loc_first_p = loc;
12244 loc->duplicate = 0;
b775012e
LM
12245
12246 if (is_breakpoint (loc->owner) && loc->condition_changed)
12247 {
12248 loc->needs_update = 1;
12249 /* Clear the condition modification flag. */
12250 loc->condition_changed = condition_unchanged;
12251 }
348d480f
PA
12252 continue;
12253 }
12254
934709f0
PW
12255
12256 /* This and the above ensure the invariant that the first location
12257 is not duplicated, and is the inserted one.
12258 All following are marked as duplicated, and are not inserted. */
12259 if (loc->inserted)
12260 swap_insertion (loc, *loc_first_p);
348d480f
PA
12261 loc->duplicate = 1;
12262
b775012e
LM
12263 /* Clear the condition modification flag. */
12264 loc->condition_changed = condition_unchanged;
12265
348d480f
PA
12266 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12267 && b->enable_state != bp_permanent)
12268 internal_error (__FILE__, __LINE__,
12269 _("another breakpoint was inserted on top of "
12270 "a permanent breakpoint"));
12271 }
12272
b775012e 12273 if (breakpoints_always_inserted_mode ()
348d480f
PA
12274 && (have_live_inferiors ()
12275 || (gdbarch_has_global_breakpoints (target_gdbarch))))
b775012e
LM
12276 {
12277 if (should_insert)
12278 insert_breakpoint_locations ();
12279 else
12280 {
12281 /* Though should_insert is false, we may need to update conditions
12282 on the target's side if it is evaluating such conditions. We
12283 only update conditions for locations that are marked
12284 "needs_update". */
12285 update_inserted_breakpoint_locations ();
12286 }
12287 }
348d480f 12288
1e4d1764
YQ
12289 if (should_insert)
12290 download_tracepoint_locations ();
12291
348d480f
PA
12292 do_cleanups (cleanups);
12293}
12294
12295void
12296breakpoint_retire_moribund (void)
12297{
12298 struct bp_location *loc;
12299 int ix;
12300
12301 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12302 if (--(loc->events_till_retirement) == 0)
12303 {
12304 decref_bp_location (&loc);
12305 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12306 --ix;
12307 }
12308}
12309
12310static void
12311update_global_location_list_nothrow (int inserting)
12312{
bfd189b1 12313 volatile struct gdb_exception e;
348d480f
PA
12314
12315 TRY_CATCH (e, RETURN_MASK_ERROR)
12316 update_global_location_list (inserting);
12317}
12318
12319/* Clear BKP from a BPS. */
12320
12321static void
12322bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12323{
12324 bpstat bs;
12325
12326 for (bs = bps; bs; bs = bs->next)
12327 if (bs->breakpoint_at == bpt)
12328 {
12329 bs->breakpoint_at = NULL;
12330 bs->old_val = NULL;
12331 /* bs->commands will be freed later. */
12332 }
12333}
12334
12335/* Callback for iterate_over_threads. */
12336static int
12337bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12338{
12339 struct breakpoint *bpt = data;
12340
12341 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12342 return 0;
12343}
12344
12345/* Helper for breakpoint and tracepoint breakpoint_ops->mention
12346 callbacks. */
12347
12348static void
12349say_where (struct breakpoint *b)
12350{
79a45e25 12351 struct ui_out *uiout = current_uiout;
348d480f
PA
12352 struct value_print_options opts;
12353
12354 get_user_print_options (&opts);
12355
12356 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12357 single string. */
12358 if (b->loc == NULL)
12359 {
12360 printf_filtered (_(" (%s) pending."), b->addr_string);
12361 }
12362 else
12363 {
f8eba3c6 12364 if (opts.addressprint || b->loc->source_file == NULL)
348d480f
PA
12365 {
12366 printf_filtered (" at ");
12367 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12368 gdb_stdout);
12369 }
f8eba3c6
TT
12370 if (b->loc->source_file)
12371 {
12372 /* If there is a single location, we can print the location
12373 more nicely. */
12374 if (b->loc->next == NULL)
12375 printf_filtered (": file %s, line %d.",
12376 b->loc->source_file, b->loc->line_number);
12377 else
12378 /* This is not ideal, but each location may have a
12379 different file name, and this at least reflects the
12380 real situation somewhat. */
12381 printf_filtered (": %s.", b->addr_string);
12382 }
348d480f
PA
12383
12384 if (b->loc->next)
12385 {
12386 struct bp_location *loc = b->loc;
12387 int n = 0;
12388 for (; loc; loc = loc->next)
12389 ++n;
12390 printf_filtered (" (%d locations)", n);
12391 }
12392 }
12393}
12394
348d480f
PA
12395/* Default bp_location_ops methods. */
12396
12397static void
12398bp_location_dtor (struct bp_location *self)
12399{
12400 xfree (self->cond);
b775012e
LM
12401 if (self->cond_bytecode)
12402 free_agent_expr (self->cond_bytecode);
348d480f 12403 xfree (self->function_name);
f8eba3c6 12404 xfree (self->source_file);
348d480f
PA
12405}
12406
12407static const struct bp_location_ops bp_location_ops =
12408{
12409 bp_location_dtor
12410};
12411
2060206e
PA
12412/* Default breakpoint_ops methods all breakpoint_ops ultimately
12413 inherit from. */
348d480f 12414
2060206e
PA
12415static void
12416base_breakpoint_dtor (struct breakpoint *self)
348d480f
PA
12417{
12418 decref_counted_command_line (&self->commands);
12419 xfree (self->cond_string);
348d480f 12420 xfree (self->addr_string);
f8eba3c6 12421 xfree (self->filter);
348d480f 12422 xfree (self->addr_string_range_end);
348d480f
PA
12423}
12424
2060206e
PA
12425static struct bp_location *
12426base_breakpoint_allocate_location (struct breakpoint *self)
348d480f
PA
12427{
12428 struct bp_location *loc;
12429
12430 loc = XNEW (struct bp_location);
12431 init_bp_location (loc, &bp_location_ops, self);
12432 return loc;
12433}
12434
2060206e
PA
12435static void
12436base_breakpoint_re_set (struct breakpoint *b)
12437{
12438 /* Nothing to re-set. */
12439}
12440
12441#define internal_error_pure_virtual_called() \
12442 gdb_assert_not_reached ("pure virtual function called")
12443
12444static int
12445base_breakpoint_insert_location (struct bp_location *bl)
12446{
12447 internal_error_pure_virtual_called ();
12448}
12449
12450static int
12451base_breakpoint_remove_location (struct bp_location *bl)
12452{
12453 internal_error_pure_virtual_called ();
12454}
12455
12456static int
12457base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12458 struct address_space *aspace,
09ac7c10
TT
12459 CORE_ADDR bp_addr,
12460 const struct target_waitstatus *ws)
2060206e
PA
12461{
12462 internal_error_pure_virtual_called ();
12463}
12464
12465static void
12466base_breakpoint_check_status (bpstat bs)
12467{
12468 /* Always stop. */
12469}
12470
12471/* A "works_in_software_mode" breakpoint_ops method that just internal
12472 errors. */
12473
12474static int
12475base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12476{
12477 internal_error_pure_virtual_called ();
12478}
12479
12480/* A "resources_needed" breakpoint_ops method that just internal
12481 errors. */
12482
12483static int
12484base_breakpoint_resources_needed (const struct bp_location *bl)
12485{
12486 internal_error_pure_virtual_called ();
12487}
12488
12489static enum print_stop_action
12490base_breakpoint_print_it (bpstat bs)
12491{
12492 internal_error_pure_virtual_called ();
12493}
12494
12495static void
12496base_breakpoint_print_one_detail (const struct breakpoint *self,
12497 struct ui_out *uiout)
12498{
12499 /* nothing */
12500}
12501
12502static void
12503base_breakpoint_print_mention (struct breakpoint *b)
12504{
12505 internal_error_pure_virtual_called ();
12506}
12507
12508static void
12509base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12510{
12511 internal_error_pure_virtual_called ();
12512}
12513
983af33b
SDJ
12514static void
12515base_breakpoint_create_sals_from_address (char **arg,
12516 struct linespec_result *canonical,
12517 enum bptype type_wanted,
12518 char *addr_start,
12519 char **copy_arg)
12520{
12521 internal_error_pure_virtual_called ();
12522}
12523
12524static void
12525base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12526 struct linespec_result *c,
12527 struct linespec_sals *lsal,
12528 char *cond_string,
e7e0cddf 12529 char *extra_string,
983af33b
SDJ
12530 enum bptype type_wanted,
12531 enum bpdisp disposition,
12532 int thread,
12533 int task, int ignore_count,
12534 const struct breakpoint_ops *o,
12535 int from_tty, int enabled,
44f238bb 12536 int internal, unsigned flags)
983af33b
SDJ
12537{
12538 internal_error_pure_virtual_called ();
12539}
12540
12541static void
12542base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12543 struct symtabs_and_lines *sals)
12544{
12545 internal_error_pure_virtual_called ();
12546}
12547
2060206e
PA
12548static struct breakpoint_ops base_breakpoint_ops =
12549{
12550 base_breakpoint_dtor,
12551 base_breakpoint_allocate_location,
12552 base_breakpoint_re_set,
12553 base_breakpoint_insert_location,
12554 base_breakpoint_remove_location,
12555 base_breakpoint_breakpoint_hit,
12556 base_breakpoint_check_status,
12557 base_breakpoint_resources_needed,
12558 base_breakpoint_works_in_software_mode,
12559 base_breakpoint_print_it,
12560 NULL,
12561 base_breakpoint_print_one_detail,
12562 base_breakpoint_print_mention,
983af33b
SDJ
12563 base_breakpoint_print_recreate,
12564 base_breakpoint_create_sals_from_address,
12565 base_breakpoint_create_breakpoints_sal,
12566 base_breakpoint_decode_linespec,
2060206e
PA
12567};
12568
12569/* Default breakpoint_ops methods. */
12570
12571static void
348d480f
PA
12572bkpt_re_set (struct breakpoint *b)
12573{
06edf0c0
PA
12574 /* FIXME: is this still reachable? */
12575 if (b->addr_string == NULL)
12576 {
12577 /* Anything without a string can't be re-set. */
348d480f 12578 delete_breakpoint (b);
06edf0c0 12579 return;
348d480f 12580 }
06edf0c0
PA
12581
12582 breakpoint_re_set_default (b);
348d480f
PA
12583}
12584
2060206e 12585static int
348d480f
PA
12586bkpt_insert_location (struct bp_location *bl)
12587{
12588 if (bl->loc_type == bp_loc_hardware_breakpoint)
12589 return target_insert_hw_breakpoint (bl->gdbarch,
12590 &bl->target_info);
12591 else
12592 return target_insert_breakpoint (bl->gdbarch,
12593 &bl->target_info);
12594}
12595
2060206e 12596static int
348d480f
PA
12597bkpt_remove_location (struct bp_location *bl)
12598{
12599 if (bl->loc_type == bp_loc_hardware_breakpoint)
12600 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12601 else
12602 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
12603}
12604
2060206e 12605static int
348d480f 12606bkpt_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12607 struct address_space *aspace, CORE_ADDR bp_addr,
12608 const struct target_waitstatus *ws)
348d480f
PA
12609{
12610 struct breakpoint *b = bl->owner;
12611
09ac7c10 12612 if (ws->kind != TARGET_WAITKIND_STOPPED
a493e3e2 12613 || ws->value.sig != GDB_SIGNAL_TRAP)
09ac7c10
TT
12614 return 0;
12615
348d480f
PA
12616 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12617 aspace, bp_addr))
12618 return 0;
12619
12620 if (overlay_debugging /* unmapped overlay section */
12621 && section_is_overlay (bl->section)
12622 && !section_is_mapped (bl->section))
12623 return 0;
12624
12625 return 1;
12626}
12627
2060206e 12628static int
348d480f
PA
12629bkpt_resources_needed (const struct bp_location *bl)
12630{
12631 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12632
12633 return 1;
12634}
12635
2060206e 12636static enum print_stop_action
348d480f
PA
12637bkpt_print_it (bpstat bs)
12638{
348d480f
PA
12639 struct breakpoint *b;
12640 const struct bp_location *bl;
001c8c33 12641 int bp_temp;
79a45e25 12642 struct ui_out *uiout = current_uiout;
348d480f
PA
12643
12644 gdb_assert (bs->bp_location_at != NULL);
12645
12646 bl = bs->bp_location_at;
12647 b = bs->breakpoint_at;
12648
001c8c33
PA
12649 bp_temp = b->disposition == disp_del;
12650 if (bl->address != bl->requested_address)
12651 breakpoint_adjustment_warning (bl->requested_address,
12652 bl->address,
12653 b->number, 1);
12654 annotate_breakpoint (b->number);
12655 if (bp_temp)
12656 ui_out_text (uiout, "\nTemporary breakpoint ");
12657 else
12658 ui_out_text (uiout, "\nBreakpoint ");
12659 if (ui_out_is_mi_like_p (uiout))
348d480f 12660 {
001c8c33
PA
12661 ui_out_field_string (uiout, "reason",
12662 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12663 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
06edf0c0 12664 }
001c8c33
PA
12665 ui_out_field_int (uiout, "bkptno", b->number);
12666 ui_out_text (uiout, ", ");
06edf0c0 12667
001c8c33 12668 return PRINT_SRC_AND_LOC;
06edf0c0
PA
12669}
12670
2060206e 12671static void
06edf0c0
PA
12672bkpt_print_mention (struct breakpoint *b)
12673{
79a45e25 12674 if (ui_out_is_mi_like_p (current_uiout))
06edf0c0
PA
12675 return;
12676
12677 switch (b->type)
12678 {
12679 case bp_breakpoint:
12680 case bp_gnu_ifunc_resolver:
12681 if (b->disposition == disp_del)
12682 printf_filtered (_("Temporary breakpoint"));
12683 else
12684 printf_filtered (_("Breakpoint"));
12685 printf_filtered (_(" %d"), b->number);
12686 if (b->type == bp_gnu_ifunc_resolver)
12687 printf_filtered (_(" at gnu-indirect-function resolver"));
12688 break;
12689 case bp_hardware_breakpoint:
12690 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12691 break;
e7e0cddf
SS
12692 case bp_dprintf:
12693 printf_filtered (_("Dprintf %d"), b->number);
12694 break;
06edf0c0
PA
12695 }
12696
12697 say_where (b);
12698}
12699
2060206e 12700static void
06edf0c0
PA
12701bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12702{
12703 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12704 fprintf_unfiltered (fp, "tbreak");
12705 else if (tp->type == bp_breakpoint)
12706 fprintf_unfiltered (fp, "break");
12707 else if (tp->type == bp_hardware_breakpoint
12708 && tp->disposition == disp_del)
12709 fprintf_unfiltered (fp, "thbreak");
12710 else if (tp->type == bp_hardware_breakpoint)
12711 fprintf_unfiltered (fp, "hbreak");
12712 else
12713 internal_error (__FILE__, __LINE__,
12714 _("unhandled breakpoint type %d"), (int) tp->type);
12715
2060206e 12716 fprintf_unfiltered (fp, " %s", tp->addr_string);
dd11a36c 12717 print_recreate_thread (tp, fp);
06edf0c0
PA
12718}
12719
983af33b
SDJ
12720static void
12721bkpt_create_sals_from_address (char **arg,
12722 struct linespec_result *canonical,
12723 enum bptype type_wanted,
12724 char *addr_start, char **copy_arg)
12725{
12726 create_sals_from_address_default (arg, canonical, type_wanted,
12727 addr_start, copy_arg);
12728}
12729
12730static void
12731bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12732 struct linespec_result *canonical,
12733 struct linespec_sals *lsal,
12734 char *cond_string,
e7e0cddf 12735 char *extra_string,
983af33b
SDJ
12736 enum bptype type_wanted,
12737 enum bpdisp disposition,
12738 int thread,
12739 int task, int ignore_count,
12740 const struct breakpoint_ops *ops,
12741 int from_tty, int enabled,
44f238bb 12742 int internal, unsigned flags)
983af33b
SDJ
12743{
12744 create_breakpoints_sal_default (gdbarch, canonical, lsal,
e7e0cddf
SS
12745 cond_string, extra_string,
12746 type_wanted,
983af33b
SDJ
12747 disposition, thread, task,
12748 ignore_count, ops, from_tty,
44f238bb 12749 enabled, internal, flags);
983af33b
SDJ
12750}
12751
12752static void
12753bkpt_decode_linespec (struct breakpoint *b, char **s,
12754 struct symtabs_and_lines *sals)
12755{
12756 decode_linespec_default (b, s, sals);
12757}
12758
06edf0c0
PA
12759/* Virtual table for internal breakpoints. */
12760
12761static void
12762internal_bkpt_re_set (struct breakpoint *b)
12763{
12764 switch (b->type)
12765 {
12766 /* Delete overlay event and longjmp master breakpoints; they
12767 will be reset later by breakpoint_re_set. */
12768 case bp_overlay_event:
12769 case bp_longjmp_master:
12770 case bp_std_terminate_master:
12771 case bp_exception_master:
12772 delete_breakpoint (b);
12773 break;
12774
12775 /* This breakpoint is special, it's set up when the inferior
12776 starts and we really don't want to touch it. */
12777 case bp_shlib_event:
12778
12779 /* Like bp_shlib_event, this breakpoint type is special. Once
12780 it is set up, we do not want to touch it. */
12781 case bp_thread_event:
12782 break;
12783 }
12784}
12785
12786static void
12787internal_bkpt_check_status (bpstat bs)
12788{
a9b3a50f
PA
12789 if (bs->breakpoint_at->type == bp_shlib_event)
12790 {
12791 /* If requested, stop when the dynamic linker notifies GDB of
12792 events. This allows the user to get control and place
12793 breakpoints in initializer routines for dynamically loaded
12794 objects (among other things). */
12795 bs->stop = stop_on_solib_events;
12796 bs->print = stop_on_solib_events;
12797 }
12798 else
12799 bs->stop = 0;
06edf0c0
PA
12800}
12801
12802static enum print_stop_action
12803internal_bkpt_print_it (bpstat bs)
12804{
36dfb11c 12805 struct ui_out *uiout = current_uiout;
06edf0c0 12806 struct breakpoint *b;
06edf0c0 12807
06edf0c0
PA
12808 b = bs->breakpoint_at;
12809
06edf0c0
PA
12810 switch (b->type)
12811 {
348d480f
PA
12812 case bp_shlib_event:
12813 /* Did we stop because the user set the stop_on_solib_events
12814 variable? (If so, we report this as a generic, "Stopped due
12815 to shlib event" message.) */
edcc5120 12816 print_solib_event (0);
348d480f
PA
12817 break;
12818
12819 case bp_thread_event:
12820 /* Not sure how we will get here.
12821 GDB should not stop for these breakpoints. */
12822 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12823 break;
12824
12825 case bp_overlay_event:
12826 /* By analogy with the thread event, GDB should not stop for these. */
12827 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
348d480f
PA
12828 break;
12829
12830 case bp_longjmp_master:
12831 /* These should never be enabled. */
12832 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
348d480f
PA
12833 break;
12834
12835 case bp_std_terminate_master:
12836 /* These should never be enabled. */
12837 printf_filtered (_("std::terminate Master Breakpoint: "
12838 "gdb should not stop!\n"));
348d480f
PA
12839 break;
12840
12841 case bp_exception_master:
12842 /* These should never be enabled. */
12843 printf_filtered (_("Exception Master Breakpoint: "
12844 "gdb should not stop!\n"));
06edf0c0
PA
12845 break;
12846 }
12847
001c8c33 12848 return PRINT_NOTHING;
06edf0c0
PA
12849}
12850
12851static void
12852internal_bkpt_print_mention (struct breakpoint *b)
12853{
12854 /* Nothing to mention. These breakpoints are internal. */
12855}
12856
06edf0c0
PA
12857/* Virtual table for momentary breakpoints */
12858
12859static void
12860momentary_bkpt_re_set (struct breakpoint *b)
12861{
12862 /* Keep temporary breakpoints, which can be encountered when we step
12863 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
12864 Otherwise these should have been blown away via the cleanup chain
12865 or by breakpoint_init_inferior when we rerun the executable. */
12866}
12867
12868static void
12869momentary_bkpt_check_status (bpstat bs)
12870{
12871 /* Nothing. The point of these breakpoints is causing a stop. */
12872}
12873
12874static enum print_stop_action
12875momentary_bkpt_print_it (bpstat bs)
12876{
79a45e25
PA
12877 struct ui_out *uiout = current_uiout;
12878
001c8c33 12879 if (ui_out_is_mi_like_p (uiout))
06edf0c0 12880 {
001c8c33 12881 struct breakpoint *b = bs->breakpoint_at;
348d480f 12882
001c8c33
PA
12883 switch (b->type)
12884 {
12885 case bp_finish:
12886 ui_out_field_string
12887 (uiout, "reason",
12888 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
12889 break;
348d480f 12890
001c8c33
PA
12891 case bp_until:
12892 ui_out_field_string
12893 (uiout, "reason",
12894 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
12895 break;
12896 }
348d480f
PA
12897 }
12898
001c8c33 12899 return PRINT_UNKNOWN;
348d480f
PA
12900}
12901
06edf0c0
PA
12902static void
12903momentary_bkpt_print_mention (struct breakpoint *b)
348d480f 12904{
06edf0c0 12905 /* Nothing to mention. These breakpoints are internal. */
348d480f
PA
12906}
12907
e2e4d78b
JK
12908/* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
12909
12910 It gets cleared already on the removal of the first one of such placed
12911 breakpoints. This is OK as they get all removed altogether. */
12912
12913static void
12914longjmp_bkpt_dtor (struct breakpoint *self)
12915{
12916 struct thread_info *tp = find_thread_id (self->thread);
12917
12918 if (tp)
12919 tp->initiating_frame = null_frame_id;
12920
12921 momentary_breakpoint_ops.dtor (self);
12922}
12923
55aa24fb
SDJ
12924/* Specific methods for probe breakpoints. */
12925
12926static int
12927bkpt_probe_insert_location (struct bp_location *bl)
12928{
12929 int v = bkpt_insert_location (bl);
12930
12931 if (v == 0)
12932 {
12933 /* The insertion was successful, now let's set the probe's semaphore
12934 if needed. */
12935 bl->probe->pops->set_semaphore (bl->probe, bl->gdbarch);
12936 }
12937
12938 return v;
12939}
12940
12941static int
12942bkpt_probe_remove_location (struct bp_location *bl)
12943{
12944 /* Let's clear the semaphore before removing the location. */
12945 bl->probe->pops->clear_semaphore (bl->probe, bl->gdbarch);
12946
12947 return bkpt_remove_location (bl);
12948}
12949
12950static void
12951bkpt_probe_create_sals_from_address (char **arg,
12952 struct linespec_result *canonical,
12953 enum bptype type_wanted,
12954 char *addr_start, char **copy_arg)
12955{
12956 struct linespec_sals lsal;
12957
12958 lsal.sals = parse_probes (arg, canonical);
12959
12960 *copy_arg = xstrdup (canonical->addr_string);
12961 lsal.canonical = xstrdup (*copy_arg);
12962
12963 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
12964}
12965
12966static void
12967bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
12968 struct symtabs_and_lines *sals)
12969{
12970 *sals = parse_probes (s, NULL);
12971 if (!sals->sals)
12972 error (_("probe not found"));
12973}
12974
348d480f 12975/* The breakpoint_ops structure to be used in tracepoints. */
876fa593 12976
348d480f
PA
12977static void
12978tracepoint_re_set (struct breakpoint *b)
12979{
12980 breakpoint_re_set_default (b);
12981}
876fa593 12982
348d480f
PA
12983static int
12984tracepoint_breakpoint_hit (const struct bp_location *bl,
09ac7c10
TT
12985 struct address_space *aspace, CORE_ADDR bp_addr,
12986 const struct target_waitstatus *ws)
348d480f
PA
12987{
12988 /* By definition, the inferior does not report stops at
12989 tracepoints. */
12990 return 0;
74960c60
VP
12991}
12992
12993static void
348d480f
PA
12994tracepoint_print_one_detail (const struct breakpoint *self,
12995 struct ui_out *uiout)
74960c60 12996{
d9b3f62e
PA
12997 struct tracepoint *tp = (struct tracepoint *) self;
12998 if (tp->static_trace_marker_id)
348d480f
PA
12999 {
13000 gdb_assert (self->type == bp_static_tracepoint);
cc59ec59 13001
348d480f
PA
13002 ui_out_text (uiout, "\tmarker id is ");
13003 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
d9b3f62e 13004 tp->static_trace_marker_id);
348d480f
PA
13005 ui_out_text (uiout, "\n");
13006 }
0d381245
VP
13007}
13008
a474d7c2 13009static void
348d480f 13010tracepoint_print_mention (struct breakpoint *b)
a474d7c2 13011{
79a45e25 13012 if (ui_out_is_mi_like_p (current_uiout))
348d480f 13013 return;
cc59ec59 13014
348d480f
PA
13015 switch (b->type)
13016 {
13017 case bp_tracepoint:
13018 printf_filtered (_("Tracepoint"));
13019 printf_filtered (_(" %d"), b->number);
13020 break;
13021 case bp_fast_tracepoint:
13022 printf_filtered (_("Fast tracepoint"));
13023 printf_filtered (_(" %d"), b->number);
13024 break;
13025 case bp_static_tracepoint:
13026 printf_filtered (_("Static tracepoint"));
13027 printf_filtered (_(" %d"), b->number);
13028 break;
13029 default:
13030 internal_error (__FILE__, __LINE__,
13031 _("unhandled tracepoint type %d"), (int) b->type);
13032 }
13033
13034 say_where (b);
a474d7c2
PA
13035}
13036
348d480f 13037static void
d9b3f62e 13038tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
a474d7c2 13039{
d9b3f62e
PA
13040 struct tracepoint *tp = (struct tracepoint *) self;
13041
13042 if (self->type == bp_fast_tracepoint)
348d480f 13043 fprintf_unfiltered (fp, "ftrace");
d9b3f62e 13044 if (self->type == bp_static_tracepoint)
348d480f 13045 fprintf_unfiltered (fp, "strace");
d9b3f62e 13046 else if (self->type == bp_tracepoint)
348d480f
PA
13047 fprintf_unfiltered (fp, "trace");
13048 else
13049 internal_error (__FILE__, __LINE__,
d9b3f62e 13050 _("unhandled tracepoint type %d"), (int) self->type);
cc59ec59 13051
d9b3f62e
PA
13052 fprintf_unfiltered (fp, " %s", self->addr_string);
13053 print_recreate_thread (self, fp);
13054
13055 if (tp->pass_count)
13056 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
a474d7c2
PA
13057}
13058
983af33b
SDJ
13059static void
13060tracepoint_create_sals_from_address (char **arg,
13061 struct linespec_result *canonical,
13062 enum bptype type_wanted,
13063 char *addr_start, char **copy_arg)
13064{
13065 create_sals_from_address_default (arg, canonical, type_wanted,
13066 addr_start, copy_arg);
13067}
13068
13069static void
13070tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13071 struct linespec_result *canonical,
13072 struct linespec_sals *lsal,
13073 char *cond_string,
e7e0cddf 13074 char *extra_string,
983af33b
SDJ
13075 enum bptype type_wanted,
13076 enum bpdisp disposition,
13077 int thread,
13078 int task, int ignore_count,
13079 const struct breakpoint_ops *ops,
13080 int from_tty, int enabled,
44f238bb 13081 int internal, unsigned flags)
983af33b
SDJ
13082{
13083 create_breakpoints_sal_default (gdbarch, canonical, lsal,
e7e0cddf
SS
13084 cond_string, extra_string,
13085 type_wanted,
983af33b
SDJ
13086 disposition, thread, task,
13087 ignore_count, ops, from_tty,
44f238bb 13088 enabled, internal, flags);
983af33b
SDJ
13089}
13090
13091static void
13092tracepoint_decode_linespec (struct breakpoint *b, char **s,
13093 struct symtabs_and_lines *sals)
13094{
13095 decode_linespec_default (b, s, sals);
13096}
13097
2060206e 13098struct breakpoint_ops tracepoint_breakpoint_ops;
348d480f 13099
55aa24fb
SDJ
13100/* The breakpoint_ops structure to be use on tracepoints placed in a
13101 static probe. */
13102
13103static void
13104tracepoint_probe_create_sals_from_address (char **arg,
13105 struct linespec_result *canonical,
13106 enum bptype type_wanted,
13107 char *addr_start, char **copy_arg)
13108{
13109 /* We use the same method for breakpoint on probes. */
13110 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13111 addr_start, copy_arg);
13112}
13113
13114static void
13115tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13116 struct symtabs_and_lines *sals)
13117{
13118 /* We use the same method for breakpoint on probes. */
13119 bkpt_probe_decode_linespec (b, s, sals);
13120}
13121
13122static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13123
983af33b
SDJ
13124/* The breakpoint_ops structure to be used on static tracepoints with
13125 markers (`-m'). */
13126
13127static void
13128strace_marker_create_sals_from_address (char **arg,
13129 struct linespec_result *canonical,
13130 enum bptype type_wanted,
13131 char *addr_start, char **copy_arg)
13132{
13133 struct linespec_sals lsal;
13134
13135 lsal.sals = decode_static_tracepoint_spec (arg);
13136
13137 *copy_arg = savestring (addr_start, *arg - addr_start);
13138
13139 canonical->addr_string = xstrdup (*copy_arg);
13140 lsal.canonical = xstrdup (*copy_arg);
13141 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13142}
13143
13144static void
13145strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13146 struct linespec_result *canonical,
13147 struct linespec_sals *lsal,
13148 char *cond_string,
e7e0cddf 13149 char *extra_string,
983af33b
SDJ
13150 enum bptype type_wanted,
13151 enum bpdisp disposition,
13152 int thread,
13153 int task, int ignore_count,
13154 const struct breakpoint_ops *ops,
13155 int from_tty, int enabled,
44f238bb 13156 int internal, unsigned flags)
983af33b
SDJ
13157{
13158 int i;
13159
13160 /* If the user is creating a static tracepoint by marker id
13161 (strace -m MARKER_ID), then store the sals index, so that
13162 breakpoint_re_set can try to match up which of the newly
13163 found markers corresponds to this one, and, don't try to
13164 expand multiple locations for each sal, given than SALS
13165 already should contain all sals for MARKER_ID. */
13166
13167 for (i = 0; i < lsal->sals.nelts; ++i)
13168 {
13169 struct symtabs_and_lines expanded;
13170 struct tracepoint *tp;
13171 struct cleanup *old_chain;
13172 char *addr_string;
13173
13174 expanded.nelts = 1;
13175 expanded.sals = &lsal->sals.sals[i];
13176
13177 addr_string = xstrdup (canonical->addr_string);
13178 old_chain = make_cleanup (xfree, addr_string);
13179
13180 tp = XCNEW (struct tracepoint);
13181 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13182 addr_string, NULL,
e7e0cddf
SS
13183 cond_string, extra_string,
13184 type_wanted, disposition,
983af33b 13185 thread, task, ignore_count, ops,
44f238bb 13186 from_tty, enabled, internal, flags,
983af33b
SDJ
13187 canonical->special_display);
13188 /* Given that its possible to have multiple markers with
13189 the same string id, if the user is creating a static
13190 tracepoint by marker id ("strace -m MARKER_ID"), then
13191 store the sals index, so that breakpoint_re_set can
13192 try to match up which of the newly found markers
13193 corresponds to this one */
13194 tp->static_trace_marker_id_idx = i;
13195
13196 install_breakpoint (internal, &tp->base, 0);
13197
13198 discard_cleanups (old_chain);
13199 }
13200}
13201
13202static void
13203strace_marker_decode_linespec (struct breakpoint *b, char **s,
13204 struct symtabs_and_lines *sals)
13205{
13206 struct tracepoint *tp = (struct tracepoint *) b;
13207
13208 *sals = decode_static_tracepoint_spec (s);
13209 if (sals->nelts > tp->static_trace_marker_id_idx)
13210 {
13211 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13212 sals->nelts = 1;
13213 }
13214 else
13215 error (_("marker %s not found"), tp->static_trace_marker_id);
13216}
13217
13218static struct breakpoint_ops strace_marker_breakpoint_ops;
13219
13220static int
13221strace_marker_p (struct breakpoint *b)
13222{
13223 return b->ops == &strace_marker_breakpoint_ops;
13224}
13225
53a5351d 13226/* Delete a breakpoint and clean up all traces of it in the data
f431efe5 13227 structures. */
c906108c
SS
13228
13229void
fba45db2 13230delete_breakpoint (struct breakpoint *bpt)
c906108c 13231{
52f0bd74 13232 struct breakpoint *b;
c906108c 13233
8a3fe4f8 13234 gdb_assert (bpt != NULL);
c906108c 13235
4a64f543
MS
13236 /* Has this bp already been deleted? This can happen because
13237 multiple lists can hold pointers to bp's. bpstat lists are
13238 especial culprits.
13239
13240 One example of this happening is a watchpoint's scope bp. When
13241 the scope bp triggers, we notice that the watchpoint is out of
13242 scope, and delete it. We also delete its scope bp. But the
13243 scope bp is marked "auto-deleting", and is already on a bpstat.
13244 That bpstat is then checked for auto-deleting bp's, which are
13245 deleted.
13246
13247 A real solution to this problem might involve reference counts in
13248 bp's, and/or giving them pointers back to their referencing
13249 bpstat's, and teaching delete_breakpoint to only free a bp's
13250 storage when no more references were extent. A cheaper bandaid
13251 was chosen. */
c906108c
SS
13252 if (bpt->type == bp_none)
13253 return;
13254
4a64f543
MS
13255 /* At least avoid this stale reference until the reference counting
13256 of breakpoints gets resolved. */
d0fb5eae 13257 if (bpt->related_breakpoint != bpt)
e5a0a904 13258 {
d0fb5eae 13259 struct breakpoint *related;
3a5c3e22 13260 struct watchpoint *w;
d0fb5eae
JK
13261
13262 if (bpt->type == bp_watchpoint_scope)
3a5c3e22 13263 w = (struct watchpoint *) bpt->related_breakpoint;
d0fb5eae 13264 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
3a5c3e22
PA
13265 w = (struct watchpoint *) bpt;
13266 else
13267 w = NULL;
13268 if (w != NULL)
13269 watchpoint_del_at_next_stop (w);
d0fb5eae
JK
13270
13271 /* Unlink bpt from the bpt->related_breakpoint ring. */
13272 for (related = bpt; related->related_breakpoint != bpt;
13273 related = related->related_breakpoint);
13274 related->related_breakpoint = bpt->related_breakpoint;
13275 bpt->related_breakpoint = bpt;
e5a0a904
JK
13276 }
13277
a9634178
TJB
13278 /* watch_command_1 creates a watchpoint but only sets its number if
13279 update_watchpoint succeeds in creating its bp_locations. If there's
13280 a problem in that process, we'll be asked to delete the half-created
13281 watchpoint. In that case, don't announce the deletion. */
13282 if (bpt->number)
13283 observer_notify_breakpoint_deleted (bpt);
c906108c 13284
c906108c
SS
13285 if (breakpoint_chain == bpt)
13286 breakpoint_chain = bpt->next;
13287
c906108c
SS
13288 ALL_BREAKPOINTS (b)
13289 if (b->next == bpt)
c5aa993b
JM
13290 {
13291 b->next = bpt->next;
13292 break;
13293 }
c906108c 13294
f431efe5
PA
13295 /* Be sure no bpstat's are pointing at the breakpoint after it's
13296 been freed. */
13297 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
e5dd4106 13298 in all threads for now. Note that we cannot just remove bpstats
f431efe5
PA
13299 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13300 commands are associated with the bpstat; if we remove it here,
13301 then the later call to bpstat_do_actions (&stop_bpstat); in
13302 event-top.c won't do anything, and temporary breakpoints with
13303 commands won't work. */
13304
13305 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13306
4a64f543
MS
13307 /* Now that breakpoint is removed from breakpoint list, update the
13308 global location list. This will remove locations that used to
13309 belong to this breakpoint. Do this before freeing the breakpoint
13310 itself, since remove_breakpoint looks at location's owner. It
13311 might be better design to have location completely
13312 self-contained, but it's not the case now. */
b60e7edf 13313 update_global_location_list (0);
74960c60 13314
348d480f 13315 bpt->ops->dtor (bpt);
4a64f543
MS
13316 /* On the chance that someone will soon try again to delete this
13317 same bp, we mark it as deleted before freeing its storage. */
c906108c 13318 bpt->type = bp_none;
b8c9b27d 13319 xfree (bpt);
c906108c
SS
13320}
13321
4d6140d9
AC
13322static void
13323do_delete_breakpoint_cleanup (void *b)
13324{
13325 delete_breakpoint (b);
13326}
13327
13328struct cleanup *
13329make_cleanup_delete_breakpoint (struct breakpoint *b)
13330{
13331 return make_cleanup (do_delete_breakpoint_cleanup, b);
13332}
13333
51be5b68
PA
13334/* Iterator function to call a user-provided callback function once
13335 for each of B and its related breakpoints. */
13336
13337static void
13338iterate_over_related_breakpoints (struct breakpoint *b,
13339 void (*function) (struct breakpoint *,
13340 void *),
13341 void *data)
13342{
13343 struct breakpoint *related;
13344
13345 related = b;
13346 do
13347 {
13348 struct breakpoint *next;
13349
13350 /* FUNCTION may delete RELATED. */
13351 next = related->related_breakpoint;
13352
13353 if (next == related)
13354 {
13355 /* RELATED is the last ring entry. */
13356 function (related, data);
13357
13358 /* FUNCTION may have deleted it, so we'd never reach back to
13359 B. There's nothing left to do anyway, so just break
13360 out. */
13361 break;
13362 }
13363 else
13364 function (related, data);
13365
13366 related = next;
13367 }
13368 while (related != b);
13369}
95a42b64
TT
13370
13371static void
13372do_delete_breakpoint (struct breakpoint *b, void *ignore)
13373{
13374 delete_breakpoint (b);
13375}
13376
51be5b68
PA
13377/* A callback for map_breakpoint_numbers that calls
13378 delete_breakpoint. */
13379
13380static void
13381do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13382{
13383 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13384}
13385
c906108c 13386void
fba45db2 13387delete_command (char *arg, int from_tty)
c906108c 13388{
35df4500 13389 struct breakpoint *b, *b_tmp;
c906108c 13390
ea9365bb
TT
13391 dont_repeat ();
13392
c906108c
SS
13393 if (arg == 0)
13394 {
13395 int breaks_to_delete = 0;
13396
46c6471b
PA
13397 /* Delete all breakpoints if no argument. Do not delete
13398 internal breakpoints, these have to be deleted with an
13399 explicit breakpoint number argument. */
c5aa993b 13400 ALL_BREAKPOINTS (b)
46c6471b 13401 if (user_breakpoint_p (b))
973d738b
DJ
13402 {
13403 breaks_to_delete = 1;
13404 break;
13405 }
c906108c
SS
13406
13407 /* Ask user only if there are some breakpoints to delete. */
13408 if (!from_tty
e2e0b3e5 13409 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
c906108c 13410 {
35df4500 13411 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 13412 if (user_breakpoint_p (b))
c5aa993b 13413 delete_breakpoint (b);
c906108c
SS
13414 }
13415 }
13416 else
51be5b68 13417 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
c906108c
SS
13418}
13419
0d381245
VP
13420static int
13421all_locations_are_pending (struct bp_location *loc)
fe3f5fa8 13422{
0d381245 13423 for (; loc; loc = loc->next)
8645ff69
UW
13424 if (!loc->shlib_disabled
13425 && !loc->pspace->executing_startup)
0d381245
VP
13426 return 0;
13427 return 1;
fe3f5fa8
VP
13428}
13429
776592bf
DE
13430/* Subroutine of update_breakpoint_locations to simplify it.
13431 Return non-zero if multiple fns in list LOC have the same name.
13432 Null names are ignored. */
13433
13434static int
13435ambiguous_names_p (struct bp_location *loc)
13436{
13437 struct bp_location *l;
13438 htab_t htab = htab_create_alloc (13, htab_hash_string,
cc59ec59
MS
13439 (int (*) (const void *,
13440 const void *)) streq,
776592bf
DE
13441 NULL, xcalloc, xfree);
13442
13443 for (l = loc; l != NULL; l = l->next)
13444 {
13445 const char **slot;
13446 const char *name = l->function_name;
13447
13448 /* Allow for some names to be NULL, ignore them. */
13449 if (name == NULL)
13450 continue;
13451
13452 slot = (const char **) htab_find_slot (htab, (const void *) name,
13453 INSERT);
4a64f543
MS
13454 /* NOTE: We can assume slot != NULL here because xcalloc never
13455 returns NULL. */
776592bf
DE
13456 if (*slot != NULL)
13457 {
13458 htab_delete (htab);
13459 return 1;
13460 }
13461 *slot = name;
13462 }
13463
13464 htab_delete (htab);
13465 return 0;
13466}
13467
0fb4aa4b
PA
13468/* When symbols change, it probably means the sources changed as well,
13469 and it might mean the static tracepoint markers are no longer at
13470 the same address or line numbers they used to be at last we
13471 checked. Losing your static tracepoints whenever you rebuild is
13472 undesirable. This function tries to resync/rematch gdb static
13473 tracepoints with the markers on the target, for static tracepoints
13474 that have not been set by marker id. Static tracepoint that have
13475 been set by marker id are reset by marker id in breakpoint_re_set.
13476 The heuristic is:
13477
13478 1) For a tracepoint set at a specific address, look for a marker at
13479 the old PC. If one is found there, assume to be the same marker.
13480 If the name / string id of the marker found is different from the
13481 previous known name, assume that means the user renamed the marker
13482 in the sources, and output a warning.
13483
13484 2) For a tracepoint set at a given line number, look for a marker
13485 at the new address of the old line number. If one is found there,
13486 assume to be the same marker. If the name / string id of the
13487 marker found is different from the previous known name, assume that
13488 means the user renamed the marker in the sources, and output a
13489 warning.
13490
13491 3) If a marker is no longer found at the same address or line, it
13492 may mean the marker no longer exists. But it may also just mean
13493 the code changed a bit. Maybe the user added a few lines of code
13494 that made the marker move up or down (in line number terms). Ask
13495 the target for info about the marker with the string id as we knew
13496 it. If found, update line number and address in the matching
13497 static tracepoint. This will get confused if there's more than one
13498 marker with the same ID (possible in UST, although unadvised
13499 precisely because it confuses tools). */
13500
13501static struct symtab_and_line
13502update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13503{
d9b3f62e 13504 struct tracepoint *tp = (struct tracepoint *) b;
0fb4aa4b
PA
13505 struct static_tracepoint_marker marker;
13506 CORE_ADDR pc;
0fb4aa4b
PA
13507
13508 pc = sal.pc;
13509 if (sal.line)
13510 find_line_pc (sal.symtab, sal.line, &pc);
13511
13512 if (target_static_tracepoint_marker_at (pc, &marker))
13513 {
d9b3f62e 13514 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
0fb4aa4b
PA
13515 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13516 b->number,
d9b3f62e 13517 tp->static_trace_marker_id, marker.str_id);
0fb4aa4b 13518
d9b3f62e
PA
13519 xfree (tp->static_trace_marker_id);
13520 tp->static_trace_marker_id = xstrdup (marker.str_id);
0fb4aa4b
PA
13521 release_static_tracepoint_marker (&marker);
13522
13523 return sal;
13524 }
13525
13526 /* Old marker wasn't found on target at lineno. Try looking it up
13527 by string ID. */
13528 if (!sal.explicit_pc
13529 && sal.line != 0
13530 && sal.symtab != NULL
d9b3f62e 13531 && tp->static_trace_marker_id != NULL)
0fb4aa4b
PA
13532 {
13533 VEC(static_tracepoint_marker_p) *markers;
13534
13535 markers
d9b3f62e 13536 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
0fb4aa4b
PA
13537
13538 if (!VEC_empty(static_tracepoint_marker_p, markers))
13539 {
80e1d417 13540 struct symtab_and_line sal2;
0fb4aa4b 13541 struct symbol *sym;
80e1d417 13542 struct static_tracepoint_marker *tpmarker;
79a45e25 13543 struct ui_out *uiout = current_uiout;
0fb4aa4b 13544
80e1d417 13545 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
0fb4aa4b 13546
d9b3f62e 13547 xfree (tp->static_trace_marker_id);
80e1d417 13548 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
0fb4aa4b
PA
13549
13550 warning (_("marker for static tracepoint %d (%s) not "
13551 "found at previous line number"),
d9b3f62e 13552 b->number, tp->static_trace_marker_id);
0fb4aa4b 13553
80e1d417 13554 init_sal (&sal2);
0fb4aa4b 13555
80e1d417 13556 sal2.pc = tpmarker->address;
0fb4aa4b 13557
80e1d417
AS
13558 sal2 = find_pc_line (tpmarker->address, 0);
13559 sym = find_pc_sect_function (tpmarker->address, NULL);
0fb4aa4b
PA
13560 ui_out_text (uiout, "Now in ");
13561 if (sym)
13562 {
13563 ui_out_field_string (uiout, "func",
13564 SYMBOL_PRINT_NAME (sym));
13565 ui_out_text (uiout, " at ");
13566 }
80e1d417 13567 ui_out_field_string (uiout, "file", sal2.symtab->filename);
0fb4aa4b
PA
13568 ui_out_text (uiout, ":");
13569
13570 if (ui_out_is_mi_like_p (uiout))
13571 {
80e1d417 13572 char *fullname = symtab_to_fullname (sal2.symtab);
0fb4aa4b
PA
13573
13574 if (fullname)
13575 ui_out_field_string (uiout, "fullname", fullname);
13576 }
13577
80e1d417 13578 ui_out_field_int (uiout, "line", sal2.line);
0fb4aa4b
PA
13579 ui_out_text (uiout, "\n");
13580
80e1d417 13581 b->loc->line_number = sal2.line;
0fb4aa4b 13582
f8eba3c6 13583 xfree (b->loc->source_file);
0fb4aa4b 13584 if (sym)
80e1d417 13585 b->loc->source_file = xstrdup (sal2.symtab->filename);
0fb4aa4b 13586 else
f8eba3c6 13587 b->loc->source_file = NULL;
0fb4aa4b
PA
13588
13589 xfree (b->addr_string);
13590 b->addr_string = xstrprintf ("%s:%d",
80e1d417 13591 sal2.symtab->filename,
f8eba3c6 13592 b->loc->line_number);
0fb4aa4b
PA
13593
13594 /* Might be nice to check if function changed, and warn if
13595 so. */
13596
80e1d417 13597 release_static_tracepoint_marker (tpmarker);
0fb4aa4b
PA
13598 }
13599 }
13600 return sal;
13601}
13602
8d3788bd
VP
13603/* Returns 1 iff locations A and B are sufficiently same that
13604 we don't need to report breakpoint as changed. */
13605
13606static int
13607locations_are_equal (struct bp_location *a, struct bp_location *b)
13608{
13609 while (a && b)
13610 {
13611 if (a->address != b->address)
13612 return 0;
13613
13614 if (a->shlib_disabled != b->shlib_disabled)
13615 return 0;
13616
13617 if (a->enabled != b->enabled)
13618 return 0;
13619
13620 a = a->next;
13621 b = b->next;
13622 }
13623
13624 if ((a == NULL) != (b == NULL))
13625 return 0;
13626
13627 return 1;
13628}
13629
f1310107
TJB
13630/* Create new breakpoint locations for B (a hardware or software breakpoint)
13631 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
13632 a ranged breakpoint. */
13633
0e30163f 13634void
0d381245 13635update_breakpoint_locations (struct breakpoint *b,
f1310107
TJB
13636 struct symtabs_and_lines sals,
13637 struct symtabs_and_lines sals_end)
fe3f5fa8
VP
13638{
13639 int i;
0d381245
VP
13640 struct bp_location *existing_locations = b->loc;
13641
f8eba3c6
TT
13642 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
13643 {
13644 /* Ranged breakpoints have only one start location and one end
13645 location. */
13646 b->enable_state = bp_disabled;
13647 update_global_location_list (1);
13648 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13649 "multiple locations found\n"),
13650 b->number);
13651 return;
13652 }
f1310107 13653
4a64f543
MS
13654 /* If there's no new locations, and all existing locations are
13655 pending, don't do anything. This optimizes the common case where
13656 all locations are in the same shared library, that was unloaded.
13657 We'd like to retain the location, so that when the library is
13658 loaded again, we don't loose the enabled/disabled status of the
13659 individual locations. */
0d381245 13660 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
fe3f5fa8
VP
13661 return;
13662
fe3f5fa8
VP
13663 b->loc = NULL;
13664
0d381245 13665 for (i = 0; i < sals.nelts; ++i)
fe3f5fa8 13666 {
f8eba3c6
TT
13667 struct bp_location *new_loc;
13668
13669 switch_to_program_space_and_thread (sals.sals[i].pspace);
13670
13671 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
fe3f5fa8 13672
0d381245
VP
13673 /* Reparse conditions, they might contain references to the
13674 old symtab. */
13675 if (b->cond_string != NULL)
13676 {
f1310107 13677 char *s;
bfd189b1 13678 volatile struct gdb_exception e;
fe3f5fa8 13679
0d381245
VP
13680 s = b->cond_string;
13681 TRY_CATCH (e, RETURN_MASK_ERROR)
13682 {
1bb9788d
TT
13683 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
13684 block_for_pc (sals.sals[i].pc),
0d381245
VP
13685 0);
13686 }
13687 if (e.reason < 0)
13688 {
3e43a32a
MS
13689 warning (_("failed to reevaluate condition "
13690 "for breakpoint %d: %s"),
0d381245
VP
13691 b->number, e.message);
13692 new_loc->enabled = 0;
13693 }
13694 }
fe3f5fa8 13695
f1310107
TJB
13696 if (sals_end.nelts)
13697 {
13698 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
13699
13700 new_loc->length = end - sals.sals[0].pc + 1;
13701 }
0d381245 13702 }
fe3f5fa8 13703
514f746b
AR
13704 /* Update locations of permanent breakpoints. */
13705 if (b->enable_state == bp_permanent)
13706 make_breakpoint_permanent (b);
13707
4a64f543
MS
13708 /* If possible, carry over 'disable' status from existing
13709 breakpoints. */
0d381245
VP
13710 {
13711 struct bp_location *e = existing_locations;
776592bf
DE
13712 /* If there are multiple breakpoints with the same function name,
13713 e.g. for inline functions, comparing function names won't work.
13714 Instead compare pc addresses; this is just a heuristic as things
13715 may have moved, but in practice it gives the correct answer
13716 often enough until a better solution is found. */
13717 int have_ambiguous_names = ambiguous_names_p (b->loc);
13718
0d381245
VP
13719 for (; e; e = e->next)
13720 {
13721 if (!e->enabled && e->function_name)
13722 {
13723 struct bp_location *l = b->loc;
776592bf
DE
13724 if (have_ambiguous_names)
13725 {
13726 for (; l; l = l->next)
f1310107 13727 if (breakpoint_locations_match (e, l))
776592bf
DE
13728 {
13729 l->enabled = 0;
13730 break;
13731 }
13732 }
13733 else
13734 {
13735 for (; l; l = l->next)
13736 if (l->function_name
13737 && strcmp (e->function_name, l->function_name) == 0)
13738 {
13739 l->enabled = 0;
13740 break;
13741 }
13742 }
0d381245
VP
13743 }
13744 }
13745 }
fe3f5fa8 13746
8d3788bd
VP
13747 if (!locations_are_equal (existing_locations, b->loc))
13748 observer_notify_breakpoint_modified (b);
13749
b60e7edf 13750 update_global_location_list (1);
fe3f5fa8
VP
13751}
13752
ef23e705
TJB
13753/* Find the SaL locations corresponding to the given ADDR_STRING.
13754 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13755
13756static struct symtabs_and_lines
13757addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
13758{
13759 char *s;
02d20e4a 13760 struct symtabs_and_lines sals = {0};
f8eba3c6 13761 volatile struct gdb_exception e;
ef23e705 13762
983af33b 13763 gdb_assert (b->ops != NULL);
ef23e705 13764 s = addr_string;
ef23e705
TJB
13765
13766 TRY_CATCH (e, RETURN_MASK_ERROR)
13767 {
983af33b 13768 b->ops->decode_linespec (b, &s, &sals);
ef23e705
TJB
13769 }
13770 if (e.reason < 0)
13771 {
13772 int not_found_and_ok = 0;
13773 /* For pending breakpoints, it's expected that parsing will
13774 fail until the right shared library is loaded. User has
13775 already told to create pending breakpoints and don't need
13776 extra messages. If breakpoint is in bp_shlib_disabled
13777 state, then user already saw the message about that
13778 breakpoint being disabled, and don't want to see more
13779 errors. */
58438ac1 13780 if (e.error == NOT_FOUND_ERROR
ef23e705
TJB
13781 && (b->condition_not_parsed
13782 || (b->loc && b->loc->shlib_disabled)
f8eba3c6 13783 || (b->loc && b->loc->pspace->executing_startup)
ef23e705
TJB
13784 || b->enable_state == bp_disabled))
13785 not_found_and_ok = 1;
13786
13787 if (!not_found_and_ok)
13788 {
13789 /* We surely don't want to warn about the same breakpoint
13790 10 times. One solution, implemented here, is disable
13791 the breakpoint on error. Another solution would be to
13792 have separate 'warning emitted' flag. Since this
13793 happens only when a binary has changed, I don't know
13794 which approach is better. */
13795 b->enable_state = bp_disabled;
13796 throw_exception (e);
13797 }
13798 }
13799
58438ac1 13800 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
ef23e705 13801 {
f8eba3c6 13802 int i;
ef23e705 13803
f8eba3c6
TT
13804 for (i = 0; i < sals.nelts; ++i)
13805 resolve_sal_pc (&sals.sals[i]);
ef23e705
TJB
13806 if (b->condition_not_parsed && s && s[0])
13807 {
13808 char *cond_string = 0;
13809 int thread = -1;
13810 int task = 0;
e7e0cddf 13811 char *extra_string = NULL;
ef23e705
TJB
13812
13813 find_condition_and_thread (s, sals.sals[0].pc,
e7e0cddf
SS
13814 &cond_string, &thread, &task,
13815 &extra_string);
ef23e705
TJB
13816 if (cond_string)
13817 b->cond_string = cond_string;
13818 b->thread = thread;
13819 b->task = task;
e7e0cddf
SS
13820 if (extra_string)
13821 b->extra_string = extra_string;
ef23e705
TJB
13822 b->condition_not_parsed = 0;
13823 }
13824
983af33b 13825 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
ef23e705 13826 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
ef23e705 13827
58438ac1
TT
13828 *found = 1;
13829 }
13830 else
13831 *found = 0;
ef23e705
TJB
13832
13833 return sals;
13834}
13835
348d480f
PA
13836/* The default re_set method, for typical hardware or software
13837 breakpoints. Reevaluate the breakpoint and recreate its
13838 locations. */
13839
13840static void
28010a5d 13841breakpoint_re_set_default (struct breakpoint *b)
ef23e705
TJB
13842{
13843 int found;
f1310107 13844 struct symtabs_and_lines sals, sals_end;
ef23e705 13845 struct symtabs_and_lines expanded = {0};
f1310107 13846 struct symtabs_and_lines expanded_end = {0};
ef23e705
TJB
13847
13848 sals = addr_string_to_sals (b, b->addr_string, &found);
13849 if (found)
13850 {
13851 make_cleanup (xfree, sals.sals);
f8eba3c6 13852 expanded = sals;
ef23e705
TJB
13853 }
13854
f1310107
TJB
13855 if (b->addr_string_range_end)
13856 {
13857 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
13858 if (found)
13859 {
13860 make_cleanup (xfree, sals_end.sals);
f8eba3c6 13861 expanded_end = sals_end;
f1310107
TJB
13862 }
13863 }
13864
13865 update_breakpoint_locations (b, expanded, expanded_end);
28010a5d
PA
13866}
13867
983af33b
SDJ
13868/* Default method for creating SALs from an address string. It basically
13869 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13870
13871static void
13872create_sals_from_address_default (char **arg,
13873 struct linespec_result *canonical,
13874 enum bptype type_wanted,
13875 char *addr_start, char **copy_arg)
13876{
13877 parse_breakpoint_sals (arg, canonical);
13878}
13879
13880/* Call create_breakpoints_sal for the given arguments. This is the default
13881 function for the `create_breakpoints_sal' method of
13882 breakpoint_ops. */
13883
13884static void
13885create_breakpoints_sal_default (struct gdbarch *gdbarch,
13886 struct linespec_result *canonical,
13887 struct linespec_sals *lsal,
13888 char *cond_string,
e7e0cddf 13889 char *extra_string,
983af33b
SDJ
13890 enum bptype type_wanted,
13891 enum bpdisp disposition,
13892 int thread,
13893 int task, int ignore_count,
13894 const struct breakpoint_ops *ops,
13895 int from_tty, int enabled,
44f238bb 13896 int internal, unsigned flags)
983af33b
SDJ
13897{
13898 create_breakpoints_sal (gdbarch, canonical, cond_string,
e7e0cddf 13899 extra_string,
983af33b
SDJ
13900 type_wanted, disposition,
13901 thread, task, ignore_count, ops, from_tty,
44f238bb 13902 enabled, internal, flags);
983af33b
SDJ
13903}
13904
13905/* Decode the line represented by S by calling decode_line_full. This is the
13906 default function for the `decode_linespec' method of breakpoint_ops. */
13907
13908static void
13909decode_linespec_default (struct breakpoint *b, char **s,
13910 struct symtabs_and_lines *sals)
13911{
13912 struct linespec_result canonical;
13913
13914 init_linespec_result (&canonical);
13915 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
13916 (struct symtab *) NULL, 0,
13917 &canonical, multiple_symbols_all,
13918 b->filter);
13919
13920 /* We should get 0 or 1 resulting SALs. */
13921 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
13922
13923 if (VEC_length (linespec_sals, canonical.sals) > 0)
13924 {
13925 struct linespec_sals *lsal;
13926
13927 lsal = VEC_index (linespec_sals, canonical.sals, 0);
13928 *sals = lsal->sals;
13929 /* Arrange it so the destructor does not free the
13930 contents. */
13931 lsal->sals.sals = NULL;
13932 }
13933
13934 destroy_linespec_result (&canonical);
13935}
13936
28010a5d
PA
13937/* Prepare the global context for a re-set of breakpoint B. */
13938
13939static struct cleanup *
13940prepare_re_set_context (struct breakpoint *b)
13941{
13942 struct cleanup *cleanups;
13943
13944 input_radix = b->input_radix;
13945 cleanups = save_current_space_and_thread ();
f8eba3c6
TT
13946 if (b->pspace != NULL)
13947 switch_to_program_space_and_thread (b->pspace);
28010a5d
PA
13948 set_language (b->language);
13949
13950 return cleanups;
ef23e705
TJB
13951}
13952
c906108c
SS
13953/* Reset a breakpoint given it's struct breakpoint * BINT.
13954 The value we return ends up being the return value from catch_errors.
13955 Unused in this case. */
13956
13957static int
4efb68b1 13958breakpoint_re_set_one (void *bint)
c906108c 13959{
4a64f543 13960 /* Get past catch_errs. */
53a5351d 13961 struct breakpoint *b = (struct breakpoint *) bint;
348d480f 13962 struct cleanup *cleanups;
c906108c 13963
348d480f
PA
13964 cleanups = prepare_re_set_context (b);
13965 b->ops->re_set (b);
13966 do_cleanups (cleanups);
c906108c
SS
13967 return 0;
13968}
13969
69de3c6a 13970/* Re-set all breakpoints after symbols have been re-loaded. */
c906108c 13971void
69de3c6a 13972breakpoint_re_set (void)
c906108c 13973{
35df4500 13974 struct breakpoint *b, *b_tmp;
c906108c
SS
13975 enum language save_language;
13976 int save_input_radix;
6c95b8df 13977 struct cleanup *old_chain;
c5aa993b 13978
c906108c
SS
13979 save_language = current_language->la_language;
13980 save_input_radix = input_radix;
6c95b8df
PA
13981 old_chain = save_current_program_space ();
13982
35df4500 13983 ALL_BREAKPOINTS_SAFE (b, b_tmp)
c5aa993b 13984 {
4a64f543 13985 /* Format possible error msg. */
fe3f5fa8 13986 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
9ebf4acf
AC
13987 b->number);
13988 struct cleanup *cleanups = make_cleanup (xfree, message);
c5aa993b 13989 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
9ebf4acf 13990 do_cleanups (cleanups);
c5aa993b 13991 }
c906108c
SS
13992 set_language (save_language);
13993 input_radix = save_input_radix;
e62c965a 13994
0756c555 13995 jit_breakpoint_re_set ();
4efc6507 13996
6c95b8df
PA
13997 do_cleanups (old_chain);
13998
af02033e
PP
13999 create_overlay_event_breakpoint ();
14000 create_longjmp_master_breakpoint ();
14001 create_std_terminate_master_breakpoint ();
186c406b 14002 create_exception_master_breakpoint ();
1bfeeb0f
JL
14003
14004 /* While we're at it, reset the skip list too. */
14005 skip_re_set ();
c906108c
SS
14006}
14007\f
c906108c
SS
14008/* Reset the thread number of this breakpoint:
14009
14010 - If the breakpoint is for all threads, leave it as-is.
4a64f543 14011 - Else, reset it to the current thread for inferior_ptid. */
c906108c 14012void
fba45db2 14013breakpoint_re_set_thread (struct breakpoint *b)
c906108c
SS
14014{
14015 if (b->thread != -1)
14016 {
39f77062
KB
14017 if (in_thread_list (inferior_ptid))
14018 b->thread = pid_to_thread_id (inferior_ptid);
6c95b8df
PA
14019
14020 /* We're being called after following a fork. The new fork is
14021 selected as current, and unless this was a vfork will have a
14022 different program space from the original thread. Reset that
14023 as well. */
14024 b->loc->pspace = current_program_space;
c906108c
SS
14025 }
14026}
14027
03ac34d5
MS
14028/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14029 If from_tty is nonzero, it prints a message to that effect,
14030 which ends with a period (no newline). */
14031
c906108c 14032void
fba45db2 14033set_ignore_count (int bptnum, int count, int from_tty)
c906108c 14034{
52f0bd74 14035 struct breakpoint *b;
c906108c
SS
14036
14037 if (count < 0)
14038 count = 0;
14039
14040 ALL_BREAKPOINTS (b)
14041 if (b->number == bptnum)
c5aa993b 14042 {
d77f58be
SS
14043 if (is_tracepoint (b))
14044 {
14045 if (from_tty && count != 0)
14046 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14047 bptnum);
14048 return;
14049 }
14050
c5aa993b 14051 b->ignore_count = count;
221ea385
KS
14052 if (from_tty)
14053 {
14054 if (count == 0)
3e43a32a
MS
14055 printf_filtered (_("Will stop next time "
14056 "breakpoint %d is reached."),
221ea385
KS
14057 bptnum);
14058 else if (count == 1)
a3f17187 14059 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
221ea385
KS
14060 bptnum);
14061 else
3e43a32a
MS
14062 printf_filtered (_("Will ignore next %d "
14063 "crossings of breakpoint %d."),
221ea385
KS
14064 count, bptnum);
14065 }
c5aa993b 14066 breakpoints_changed ();
8d3788bd 14067 observer_notify_breakpoint_modified (b);
c5aa993b
JM
14068 return;
14069 }
c906108c 14070
8a3fe4f8 14071 error (_("No breakpoint number %d."), bptnum);
c906108c
SS
14072}
14073
c906108c
SS
14074/* Command to set ignore-count of breakpoint N to COUNT. */
14075
14076static void
fba45db2 14077ignore_command (char *args, int from_tty)
c906108c
SS
14078{
14079 char *p = args;
52f0bd74 14080 int num;
c906108c
SS
14081
14082 if (p == 0)
e2e0b3e5 14083 error_no_arg (_("a breakpoint number"));
c5aa993b 14084
c906108c 14085 num = get_number (&p);
5c44784c 14086 if (num == 0)
8a3fe4f8 14087 error (_("bad breakpoint number: '%s'"), args);
c906108c 14088 if (*p == 0)
8a3fe4f8 14089 error (_("Second argument (specified ignore-count) is missing."));
c906108c
SS
14090
14091 set_ignore_count (num,
14092 longest_to_int (value_as_long (parse_and_eval (p))),
14093 from_tty);
221ea385
KS
14094 if (from_tty)
14095 printf_filtered ("\n");
c906108c
SS
14096}
14097\f
14098/* Call FUNCTION on each of the breakpoints
14099 whose numbers are given in ARGS. */
14100
14101static void
95a42b64
TT
14102map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14103 void *),
14104 void *data)
c906108c 14105{
52f0bd74
AC
14106 int num;
14107 struct breakpoint *b, *tmp;
11cf8741 14108 int match;
197f0a60 14109 struct get_number_or_range_state state;
c906108c 14110
197f0a60 14111 if (args == 0)
e2e0b3e5 14112 error_no_arg (_("one or more breakpoint numbers"));
c906108c 14113
197f0a60
TT
14114 init_number_or_range (&state, args);
14115
14116 while (!state.finished)
c906108c 14117 {
197f0a60
TT
14118 char *p = state.string;
14119
11cf8741 14120 match = 0;
c5aa993b 14121
197f0a60 14122 num = get_number_or_range (&state);
5c44784c 14123 if (num == 0)
c5aa993b 14124 {
8a3fe4f8 14125 warning (_("bad breakpoint number at or near '%s'"), p);
5c44784c
JM
14126 }
14127 else
14128 {
14129 ALL_BREAKPOINTS_SAFE (b, tmp)
14130 if (b->number == num)
14131 {
11cf8741 14132 match = 1;
cdac0397 14133 function (b, data);
11cf8741 14134 break;
5c44784c 14135 }
11cf8741 14136 if (match == 0)
a3f17187 14137 printf_unfiltered (_("No breakpoint number %d.\n"), num);
c5aa993b 14138 }
c906108c
SS
14139 }
14140}
14141
0d381245
VP
14142static struct bp_location *
14143find_location_by_number (char *number)
14144{
14145 char *dot = strchr (number, '.');
14146 char *p1;
14147 int bp_num;
14148 int loc_num;
14149 struct breakpoint *b;
14150 struct bp_location *loc;
14151
14152 *dot = '\0';
14153
14154 p1 = number;
197f0a60 14155 bp_num = get_number (&p1);
0d381245
VP
14156 if (bp_num == 0)
14157 error (_("Bad breakpoint number '%s'"), number);
14158
14159 ALL_BREAKPOINTS (b)
14160 if (b->number == bp_num)
14161 {
14162 break;
14163 }
14164
14165 if (!b || b->number != bp_num)
14166 error (_("Bad breakpoint number '%s'"), number);
14167
14168 p1 = dot+1;
197f0a60 14169 loc_num = get_number (&p1);
0d381245
VP
14170 if (loc_num == 0)
14171 error (_("Bad breakpoint location number '%s'"), number);
14172
14173 --loc_num;
14174 loc = b->loc;
14175 for (;loc_num && loc; --loc_num, loc = loc->next)
14176 ;
14177 if (!loc)
14178 error (_("Bad breakpoint location number '%s'"), dot+1);
14179
14180 return loc;
14181}
14182
14183
1900040c
MS
14184/* Set ignore-count of breakpoint number BPTNUM to COUNT.
14185 If from_tty is nonzero, it prints a message to that effect,
14186 which ends with a period (no newline). */
14187
c906108c 14188void
fba45db2 14189disable_breakpoint (struct breakpoint *bpt)
c906108c
SS
14190{
14191 /* Never disable a watchpoint scope breakpoint; we want to
14192 hit them when we leave scope so we can delete both the
14193 watchpoint and its scope breakpoint at that time. */
14194 if (bpt->type == bp_watchpoint_scope)
14195 return;
14196
c2c6d25f 14197 /* You can't disable permanent breakpoints. */
b5de0fa7 14198 if (bpt->enable_state == bp_permanent)
c2c6d25f
JM
14199 return;
14200
b5de0fa7 14201 bpt->enable_state = bp_disabled;
c906108c 14202
b775012e
LM
14203 /* Mark breakpoint locations modified. */
14204 mark_breakpoint_modified (bpt);
14205
d248b706
KY
14206 if (target_supports_enable_disable_tracepoint ()
14207 && current_trace_status ()->running && is_tracepoint (bpt))
14208 {
14209 struct bp_location *location;
14210
14211 for (location = bpt->loc; location; location = location->next)
14212 target_disable_tracepoint (location);
14213 }
14214
b60e7edf 14215 update_global_location_list (0);
c906108c 14216
8d3788bd 14217 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14218}
14219
51be5b68
PA
14220/* A callback for iterate_over_related_breakpoints. */
14221
14222static void
14223do_disable_breakpoint (struct breakpoint *b, void *ignore)
14224{
14225 disable_breakpoint (b);
14226}
14227
95a42b64
TT
14228/* A callback for map_breakpoint_numbers that calls
14229 disable_breakpoint. */
14230
14231static void
14232do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14233{
51be5b68 14234 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
95a42b64
TT
14235}
14236
c906108c 14237static void
fba45db2 14238disable_command (char *args, int from_tty)
c906108c 14239{
c906108c 14240 if (args == 0)
46c6471b
PA
14241 {
14242 struct breakpoint *bpt;
14243
14244 ALL_BREAKPOINTS (bpt)
14245 if (user_breakpoint_p (bpt))
14246 disable_breakpoint (bpt);
14247 }
0d381245
VP
14248 else if (strchr (args, '.'))
14249 {
14250 struct bp_location *loc = find_location_by_number (args);
14251 if (loc)
d248b706 14252 {
b775012e
LM
14253 if (loc->enabled)
14254 {
14255 loc->enabled = 0;
14256 mark_breakpoint_location_modified (loc);
14257 }
d248b706
KY
14258 if (target_supports_enable_disable_tracepoint ()
14259 && current_trace_status ()->running && loc->owner
14260 && is_tracepoint (loc->owner))
14261 target_disable_tracepoint (loc);
14262 }
b60e7edf 14263 update_global_location_list (0);
0d381245 14264 }
c906108c 14265 else
95a42b64 14266 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
c906108c
SS
14267}
14268
14269static void
816338b5
SS
14270enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14271 int count)
c906108c 14272{
afe38095 14273 int target_resources_ok;
c906108c
SS
14274
14275 if (bpt->type == bp_hardware_breakpoint)
14276 {
14277 int i;
c5aa993b 14278 i = hw_breakpoint_used_count ();
53a5351d 14279 target_resources_ok =
d92524f1 14280 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
53a5351d 14281 i + 1, 0);
c906108c 14282 if (target_resources_ok == 0)
8a3fe4f8 14283 error (_("No hardware breakpoint support in the target."));
c906108c 14284 else if (target_resources_ok < 0)
8a3fe4f8 14285 error (_("Hardware breakpoints used exceeds limit."));
c906108c
SS
14286 }
14287
cc60f2e3 14288 if (is_watchpoint (bpt))
c906108c 14289 {
d07205c2
JK
14290 /* Initialize it just to avoid a GCC false warning. */
14291 enum enable_state orig_enable_state = 0;
bfd189b1 14292 volatile struct gdb_exception e;
dde02812
ES
14293
14294 TRY_CATCH (e, RETURN_MASK_ALL)
c906108c 14295 {
3a5c3e22
PA
14296 struct watchpoint *w = (struct watchpoint *) bpt;
14297
1e718ff1
TJB
14298 orig_enable_state = bpt->enable_state;
14299 bpt->enable_state = bp_enabled;
3a5c3e22 14300 update_watchpoint (w, 1 /* reparse */);
c906108c 14301 }
dde02812 14302 if (e.reason < 0)
c5aa993b 14303 {
1e718ff1 14304 bpt->enable_state = orig_enable_state;
dde02812
ES
14305 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14306 bpt->number);
14307 return;
c5aa993b 14308 }
c906108c 14309 }
0101ce28 14310
b4c291bb
KH
14311 if (bpt->enable_state != bp_permanent)
14312 bpt->enable_state = bp_enabled;
d248b706 14313
b775012e
LM
14314 bpt->enable_state = bp_enabled;
14315
14316 /* Mark breakpoint locations modified. */
14317 mark_breakpoint_modified (bpt);
14318
d248b706
KY
14319 if (target_supports_enable_disable_tracepoint ()
14320 && current_trace_status ()->running && is_tracepoint (bpt))
14321 {
14322 struct bp_location *location;
14323
14324 for (location = bpt->loc; location; location = location->next)
14325 target_enable_tracepoint (location);
14326 }
14327
b4c291bb 14328 bpt->disposition = disposition;
816338b5 14329 bpt->enable_count = count;
b60e7edf 14330 update_global_location_list (1);
b4c291bb
KH
14331 breakpoints_changed ();
14332
8d3788bd 14333 observer_notify_breakpoint_modified (bpt);
c906108c
SS
14334}
14335
fe3f5fa8 14336
c906108c 14337void
fba45db2 14338enable_breakpoint (struct breakpoint *bpt)
c906108c 14339{
816338b5 14340 enable_breakpoint_disp (bpt, bpt->disposition, 0);
51be5b68
PA
14341}
14342
14343static void
14344do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14345{
14346 enable_breakpoint (bpt);
c906108c
SS
14347}
14348
95a42b64
TT
14349/* A callback for map_breakpoint_numbers that calls
14350 enable_breakpoint. */
14351
14352static void
14353do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14354{
51be5b68 14355 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
95a42b64
TT
14356}
14357
c906108c
SS
14358/* The enable command enables the specified breakpoints (or all defined
14359 breakpoints) so they once again become (or continue to be) effective
1272ad14 14360 in stopping the inferior. */
c906108c 14361
c906108c 14362static void
fba45db2 14363enable_command (char *args, int from_tty)
c906108c 14364{
c906108c 14365 if (args == 0)
46c6471b
PA
14366 {
14367 struct breakpoint *bpt;
14368
14369 ALL_BREAKPOINTS (bpt)
14370 if (user_breakpoint_p (bpt))
14371 enable_breakpoint (bpt);
14372 }
0d381245
VP
14373 else if (strchr (args, '.'))
14374 {
14375 struct bp_location *loc = find_location_by_number (args);
14376 if (loc)
d248b706 14377 {
b775012e
LM
14378 if (!loc->enabled)
14379 {
14380 loc->enabled = 1;
14381 mark_breakpoint_location_modified (loc);
14382 }
d248b706
KY
14383 if (target_supports_enable_disable_tracepoint ()
14384 && current_trace_status ()->running && loc->owner
14385 && is_tracepoint (loc->owner))
14386 target_enable_tracepoint (loc);
14387 }
b60e7edf 14388 update_global_location_list (1);
0d381245 14389 }
c906108c 14390 else
95a42b64 14391 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
c906108c
SS
14392}
14393
816338b5
SS
14394/* This struct packages up disposition data for application to multiple
14395 breakpoints. */
14396
14397struct disp_data
14398{
14399 enum bpdisp disp;
14400 int count;
14401};
14402
c906108c 14403static void
51be5b68
PA
14404do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14405{
816338b5 14406 struct disp_data disp_data = *(struct disp_data *) arg;
51be5b68 14407
816338b5 14408 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
51be5b68
PA
14409}
14410
14411static void
14412do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14413{
816338b5 14414 struct disp_data disp = { disp_disable, 1 };
51be5b68
PA
14415
14416 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14417}
14418
c906108c 14419static void
fba45db2 14420enable_once_command (char *args, int from_tty)
c906108c 14421{
51be5b68 14422 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
c906108c
SS
14423}
14424
816338b5
SS
14425static void
14426do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14427{
14428 struct disp_data disp = { disp_disable, *(int *) countptr };
14429
14430 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14431}
14432
14433static void
14434enable_count_command (char *args, int from_tty)
14435{
14436 int count = get_number (&args);
14437
14438 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14439}
14440
c906108c 14441static void
51be5b68 14442do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
c906108c 14443{
816338b5 14444 struct disp_data disp = { disp_del, 1 };
51be5b68
PA
14445
14446 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
c906108c
SS
14447}
14448
c906108c 14449static void
fba45db2 14450enable_delete_command (char *args, int from_tty)
c906108c 14451{
51be5b68 14452 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
c906108c
SS
14453}
14454\f
fa8d40ab
JJ
14455static void
14456set_breakpoint_cmd (char *args, int from_tty)
14457{
14458}
14459
14460static void
14461show_breakpoint_cmd (char *args, int from_tty)
14462{
14463}
14464
1f3b5d1b
PP
14465/* Invalidate last known value of any hardware watchpoint if
14466 the memory which that value represents has been written to by
14467 GDB itself. */
14468
14469static void
14470invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
14471 const bfd_byte *data)
14472{
14473 struct breakpoint *bp;
14474
14475 ALL_BREAKPOINTS (bp)
14476 if (bp->enable_state == bp_enabled
3a5c3e22 14477 && bp->type == bp_hardware_watchpoint)
1f3b5d1b 14478 {
3a5c3e22 14479 struct watchpoint *wp = (struct watchpoint *) bp;
1f3b5d1b 14480
3a5c3e22
PA
14481 if (wp->val_valid && wp->val)
14482 {
14483 struct bp_location *loc;
14484
14485 for (loc = bp->loc; loc != NULL; loc = loc->next)
14486 if (loc->loc_type == bp_loc_hardware_watchpoint
14487 && loc->address + loc->length > addr
14488 && addr + len > loc->address)
14489 {
14490 value_free (wp->val);
14491 wp->val = NULL;
14492 wp->val_valid = 0;
14493 }
14494 }
1f3b5d1b
PP
14495 }
14496}
14497
8181d85f
DJ
14498/* Create and insert a raw software breakpoint at PC. Return an
14499 identifier, which should be used to remove the breakpoint later.
14500 In general, places which call this should be using something on the
14501 breakpoint chain instead; this function should be eliminated
14502 someday. */
14503
14504void *
6c95b8df
PA
14505deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
14506 struct address_space *aspace, CORE_ADDR pc)
8181d85f
DJ
14507{
14508 struct bp_target_info *bp_tgt;
14509
6c95b8df 14510 bp_tgt = XZALLOC (struct bp_target_info);
8181d85f 14511
6c95b8df 14512 bp_tgt->placed_address_space = aspace;
8181d85f 14513 bp_tgt->placed_address = pc;
6c95b8df 14514
a6d9a66e 14515 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
8181d85f
DJ
14516 {
14517 /* Could not insert the breakpoint. */
14518 xfree (bp_tgt);
14519 return NULL;
14520 }
14521
14522 return bp_tgt;
14523}
14524
4a64f543
MS
14525/* Remove a breakpoint BP inserted by
14526 deprecated_insert_raw_breakpoint. */
8181d85f
DJ
14527
14528int
a6d9a66e 14529deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
8181d85f
DJ
14530{
14531 struct bp_target_info *bp_tgt = bp;
14532 int ret;
14533
a6d9a66e 14534 ret = target_remove_breakpoint (gdbarch, bp_tgt);
8181d85f
DJ
14535 xfree (bp_tgt);
14536
14537 return ret;
14538}
14539
4a64f543
MS
14540/* One (or perhaps two) breakpoints used for software single
14541 stepping. */
8181d85f
DJ
14542
14543static void *single_step_breakpoints[2];
a6d9a66e 14544static struct gdbarch *single_step_gdbarch[2];
8181d85f
DJ
14545
14546/* Create and insert a breakpoint for software single step. */
14547
14548void
6c95b8df 14549insert_single_step_breakpoint (struct gdbarch *gdbarch,
4a64f543
MS
14550 struct address_space *aspace,
14551 CORE_ADDR next_pc)
8181d85f
DJ
14552{
14553 void **bpt_p;
14554
14555 if (single_step_breakpoints[0] == NULL)
a6d9a66e
UW
14556 {
14557 bpt_p = &single_step_breakpoints[0];
14558 single_step_gdbarch[0] = gdbarch;
14559 }
8181d85f
DJ
14560 else
14561 {
14562 gdb_assert (single_step_breakpoints[1] == NULL);
14563 bpt_p = &single_step_breakpoints[1];
a6d9a66e 14564 single_step_gdbarch[1] = gdbarch;
8181d85f
DJ
14565 }
14566
4a64f543
MS
14567 /* NOTE drow/2006-04-11: A future improvement to this function would
14568 be to only create the breakpoints once, and actually put them on
14569 the breakpoint chain. That would let us use set_raw_breakpoint.
14570 We could adjust the addresses each time they were needed. Doing
14571 this requires corresponding changes elsewhere where single step
14572 breakpoints are handled, however. So, for now, we use this. */
8181d85f 14573
6c95b8df 14574 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
8181d85f 14575 if (*bpt_p == NULL)
5af949e3
UW
14576 error (_("Could not insert single-step breakpoint at %s"),
14577 paddress (gdbarch, next_pc));
8181d85f
DJ
14578}
14579
f02253f1
HZ
14580/* Check if the breakpoints used for software single stepping
14581 were inserted or not. */
14582
14583int
14584single_step_breakpoints_inserted (void)
14585{
14586 return (single_step_breakpoints[0] != NULL
14587 || single_step_breakpoints[1] != NULL);
14588}
14589
8181d85f
DJ
14590/* Remove and delete any breakpoints used for software single step. */
14591
14592void
14593remove_single_step_breakpoints (void)
14594{
14595 gdb_assert (single_step_breakpoints[0] != NULL);
14596
14597 /* See insert_single_step_breakpoint for more about this deprecated
14598 call. */
a6d9a66e
UW
14599 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
14600 single_step_breakpoints[0]);
14601 single_step_gdbarch[0] = NULL;
8181d85f
DJ
14602 single_step_breakpoints[0] = NULL;
14603
14604 if (single_step_breakpoints[1] != NULL)
14605 {
a6d9a66e
UW
14606 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
14607 single_step_breakpoints[1]);
14608 single_step_gdbarch[1] = NULL;
8181d85f
DJ
14609 single_step_breakpoints[1] = NULL;
14610 }
14611}
14612
d03285ec
UW
14613/* Delete software single step breakpoints without removing them from
14614 the inferior. This is intended to be used if the inferior's address
14615 space where they were inserted is already gone, e.g. after exit or
14616 exec. */
14617
14618void
14619cancel_single_step_breakpoints (void)
14620{
14621 int i;
14622
14623 for (i = 0; i < 2; i++)
14624 if (single_step_breakpoints[i])
14625 {
14626 xfree (single_step_breakpoints[i]);
14627 single_step_breakpoints[i] = NULL;
14628 single_step_gdbarch[i] = NULL;
14629 }
14630}
14631
14632/* Detach software single-step breakpoints from INFERIOR_PTID without
14633 removing them. */
14634
14635static void
14636detach_single_step_breakpoints (void)
14637{
14638 int i;
14639
14640 for (i = 0; i < 2; i++)
14641 if (single_step_breakpoints[i])
14642 target_remove_breakpoint (single_step_gdbarch[i],
14643 single_step_breakpoints[i]);
14644}
14645
4a64f543
MS
14646/* Check whether a software single-step breakpoint is inserted at
14647 PC. */
1aafd4da
UW
14648
14649static int
cc59ec59
MS
14650single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14651 CORE_ADDR pc)
1aafd4da
UW
14652{
14653 int i;
14654
14655 for (i = 0; i < 2; i++)
14656 {
14657 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
6c95b8df
PA
14658 if (bp_tgt
14659 && breakpoint_address_match (bp_tgt->placed_address_space,
14660 bp_tgt->placed_address,
14661 aspace, pc))
1aafd4da
UW
14662 return 1;
14663 }
14664
14665 return 0;
14666}
14667
a96d9b2e
SDJ
14668/* Returns 0 if 'bp' is NOT a syscall catchpoint,
14669 non-zero otherwise. */
14670static int
14671is_syscall_catchpoint_enabled (struct breakpoint *bp)
14672{
14673 if (syscall_catchpoint_p (bp)
14674 && bp->enable_state != bp_disabled
14675 && bp->enable_state != bp_call_disabled)
14676 return 1;
14677 else
14678 return 0;
14679}
14680
14681int
14682catch_syscall_enabled (void)
14683{
fa3064dd
YQ
14684 struct catch_syscall_inferior_data *inf_data
14685 = get_catch_syscall_inferior_data (current_inferior ());
a96d9b2e 14686
fa3064dd 14687 return inf_data->total_syscalls_count != 0;
a96d9b2e
SDJ
14688}
14689
14690int
14691catching_syscall_number (int syscall_number)
14692{
14693 struct breakpoint *bp;
14694
14695 ALL_BREAKPOINTS (bp)
14696 if (is_syscall_catchpoint_enabled (bp))
14697 {
be5c67c1
PA
14698 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
14699
14700 if (c->syscalls_to_be_caught)
a96d9b2e
SDJ
14701 {
14702 int i, iter;
14703 for (i = 0;
be5c67c1 14704 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
a96d9b2e
SDJ
14705 i++)
14706 if (syscall_number == iter)
14707 return 1;
14708 }
14709 else
14710 return 1;
14711 }
14712
14713 return 0;
14714}
14715
14716/* Complete syscall names. Used by "catch syscall". */
49c4e619 14717static VEC (char_ptr) *
a96d9b2e
SDJ
14718catch_syscall_completer (struct cmd_list_element *cmd,
14719 char *text, char *word)
14720{
14721 const char **list = get_syscall_names ();
49c4e619 14722 VEC (char_ptr) *retlist
c38eea1a 14723 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
cc59ec59 14724
c38eea1a
MS
14725 xfree (list);
14726 return retlist;
a96d9b2e
SDJ
14727}
14728
1042e4c0
SS
14729/* Tracepoint-specific operations. */
14730
14731/* Set tracepoint count to NUM. */
14732static void
14733set_tracepoint_count (int num)
14734{
14735 tracepoint_count = num;
4fa62494 14736 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
1042e4c0
SS
14737}
14738
70221824 14739static void
1042e4c0
SS
14740trace_command (char *arg, int from_tty)
14741{
55aa24fb
SDJ
14742 struct breakpoint_ops *ops;
14743 const char *arg_cp = arg;
14744
14745 if (arg && probe_linespec_to_ops (&arg_cp))
14746 ops = &tracepoint_probe_breakpoint_ops;
14747 else
14748 ops = &tracepoint_breakpoint_ops;
14749
8cdf0e15
VP
14750 if (create_breakpoint (get_current_arch (),
14751 arg,
e7e0cddf 14752 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b
PA
14753 0 /* tempflag */,
14754 bp_tracepoint /* type_wanted */,
8cdf0e15
VP
14755 0 /* Ignore count */,
14756 pending_break_support,
55aa24fb 14757 ops,
8cdf0e15 14758 from_tty,
84f4c1fe 14759 1 /* enabled */,
44f238bb 14760 0 /* internal */, 0))
fd9b8c24 14761 set_tracepoint_count (breakpoint_count);
1042e4c0
SS
14762}
14763
70221824 14764static void
7a697b8d
SS
14765ftrace_command (char *arg, int from_tty)
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_fast_tracepoint /* type_wanted */,
14772 0 /* Ignore count */,
14773 pending_break_support,
348d480f 14774 &tracepoint_breakpoint_ops,
0fb4aa4b 14775 from_tty,
84f4c1fe 14776 1 /* enabled */,
44f238bb 14777 0 /* internal */, 0))
0fb4aa4b
PA
14778 set_tracepoint_count (breakpoint_count);
14779}
14780
14781/* strace command implementation. Creates a static tracepoint. */
14782
70221824 14783static void
0fb4aa4b
PA
14784strace_command (char *arg, int from_tty)
14785{
983af33b
SDJ
14786 struct breakpoint_ops *ops;
14787
14788 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14789 or with a normal static tracepoint. */
14790 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
14791 ops = &strace_marker_breakpoint_ops;
14792 else
14793 ops = &tracepoint_breakpoint_ops;
14794
0fb4aa4b
PA
14795 if (create_breakpoint (get_current_arch (),
14796 arg,
e7e0cddf 14797 NULL, 0, NULL, 1 /* parse arg */,
0fb4aa4b
PA
14798 0 /* tempflag */,
14799 bp_static_tracepoint /* type_wanted */,
8cdf0e15
VP
14800 0 /* Ignore count */,
14801 pending_break_support,
983af33b 14802 ops,
8cdf0e15 14803 from_tty,
84f4c1fe 14804 1 /* enabled */,
44f238bb 14805 0 /* internal */, 0))
fd9b8c24 14806 set_tracepoint_count (breakpoint_count);
7a697b8d
SS
14807}
14808
409873ef
SS
14809/* Set up a fake reader function that gets command lines from a linked
14810 list that was acquired during tracepoint uploading. */
14811
14812static struct uploaded_tp *this_utp;
3149d8c1 14813static int next_cmd;
409873ef
SS
14814
14815static char *
14816read_uploaded_action (void)
14817{
14818 char *rslt;
14819
3149d8c1 14820 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
409873ef 14821
3149d8c1 14822 next_cmd++;
409873ef
SS
14823
14824 return rslt;
14825}
14826
00bf0b85
SS
14827/* Given information about a tracepoint as recorded on a target (which
14828 can be either a live system or a trace file), attempt to create an
14829 equivalent GDB tracepoint. This is not a reliable process, since
14830 the target does not necessarily have all the information used when
14831 the tracepoint was originally defined. */
14832
d9b3f62e 14833struct tracepoint *
00bf0b85 14834create_tracepoint_from_upload (struct uploaded_tp *utp)
d5551862 14835{
409873ef 14836 char *addr_str, small_buf[100];
d9b3f62e 14837 struct tracepoint *tp;
fd9b8c24 14838
409873ef
SS
14839 if (utp->at_string)
14840 addr_str = utp->at_string;
14841 else
14842 {
14843 /* In the absence of a source location, fall back to raw
14844 address. Since there is no way to confirm that the address
14845 means the same thing as when the trace was started, warn the
14846 user. */
3e43a32a
MS
14847 warning (_("Uploaded tracepoint %d has no "
14848 "source location, using raw address"),
409873ef
SS
14849 utp->number);
14850 sprintf (small_buf, "*%s", hex_string (utp->addr));
14851 addr_str = small_buf;
14852 }
14853
14854 /* There's not much we can do with a sequence of bytecodes. */
14855 if (utp->cond && !utp->cond_string)
3e43a32a
MS
14856 warning (_("Uploaded tracepoint %d condition "
14857 "has no source form, ignoring it"),
409873ef 14858 utp->number);
d5551862 14859
8cdf0e15 14860 if (!create_breakpoint (get_current_arch (),
409873ef 14861 addr_str,
e7e0cddf
SS
14862 utp->cond_string, -1, NULL,
14863 0 /* parse cond/thread */,
8cdf0e15 14864 0 /* tempflag */,
0fb4aa4b 14865 utp->type /* type_wanted */,
8cdf0e15
VP
14866 0 /* Ignore count */,
14867 pending_break_support,
348d480f 14868 &tracepoint_breakpoint_ops,
8cdf0e15 14869 0 /* from_tty */,
84f4c1fe 14870 utp->enabled /* enabled */,
44f238bb
PA
14871 0 /* internal */,
14872 CREATE_BREAKPOINT_FLAGS_INSERTED))
fd9b8c24
PA
14873 return NULL;
14874
00bf0b85
SS
14875 set_tracepoint_count (breakpoint_count);
14876
409873ef 14877 /* Get the tracepoint we just created. */
fd9b8c24
PA
14878 tp = get_tracepoint (tracepoint_count);
14879 gdb_assert (tp != NULL);
d5551862 14880
00bf0b85
SS
14881 if (utp->pass > 0)
14882 {
d9b3f62e 14883 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
00bf0b85 14884
409873ef 14885 trace_pass_command (small_buf, 0);
00bf0b85
SS
14886 }
14887
409873ef
SS
14888 /* If we have uploaded versions of the original commands, set up a
14889 special-purpose "reader" function and call the usual command line
14890 reader, then pass the result to the breakpoint command-setting
14891 function. */
3149d8c1 14892 if (!VEC_empty (char_ptr, utp->cmd_strings))
00bf0b85 14893 {
409873ef 14894 struct command_line *cmd_list;
00bf0b85 14895
409873ef 14896 this_utp = utp;
3149d8c1 14897 next_cmd = 0;
d5551862 14898
409873ef
SS
14899 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14900
d9b3f62e 14901 breakpoint_set_commands (&tp->base, cmd_list);
00bf0b85 14902 }
3149d8c1
SS
14903 else if (!VEC_empty (char_ptr, utp->actions)
14904 || !VEC_empty (char_ptr, utp->step_actions))
3e43a32a
MS
14905 warning (_("Uploaded tracepoint %d actions "
14906 "have no source form, ignoring them"),
409873ef 14907 utp->number);
00bf0b85 14908
f196051f
SS
14909 /* Copy any status information that might be available. */
14910 tp->base.hit_count = utp->hit_count;
14911 tp->traceframe_usage = utp->traceframe_usage;
14912
00bf0b85 14913 return tp;
d9b3f62e 14914}
00bf0b85 14915
1042e4c0
SS
14916/* Print information on tracepoint number TPNUM_EXP, or all if
14917 omitted. */
14918
14919static void
e5a67952 14920tracepoints_info (char *args, int from_tty)
1042e4c0 14921{
79a45e25 14922 struct ui_out *uiout = current_uiout;
e5a67952 14923 int num_printed;
1042e4c0 14924
e5a67952 14925 num_printed = breakpoint_1 (args, 0, is_tracepoint);
d77f58be
SS
14926
14927 if (num_printed == 0)
1042e4c0 14928 {
e5a67952 14929 if (args == NULL || *args == '\0')
d77f58be
SS
14930 ui_out_message (uiout, 0, "No tracepoints.\n");
14931 else
e5a67952 14932 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
1042e4c0 14933 }
ad443146
SS
14934
14935 default_collect_info ();
1042e4c0
SS
14936}
14937
4a64f543 14938/* The 'enable trace' command enables tracepoints.
1042e4c0
SS
14939 Not supported by all targets. */
14940static void
14941enable_trace_command (char *args, int from_tty)
14942{
14943 enable_command (args, from_tty);
14944}
14945
4a64f543 14946/* The 'disable trace' command disables tracepoints.
1042e4c0
SS
14947 Not supported by all targets. */
14948static void
14949disable_trace_command (char *args, int from_tty)
14950{
14951 disable_command (args, from_tty);
14952}
14953
4a64f543 14954/* Remove a tracepoint (or all if no argument). */
1042e4c0
SS
14955static void
14956delete_trace_command (char *arg, int from_tty)
14957{
35df4500 14958 struct breakpoint *b, *b_tmp;
1042e4c0
SS
14959
14960 dont_repeat ();
14961
14962 if (arg == 0)
14963 {
14964 int breaks_to_delete = 0;
14965
14966 /* Delete all breakpoints if no argument.
14967 Do not delete internal or call-dummy breakpoints, these
4a64f543
MS
14968 have to be deleted with an explicit breakpoint number
14969 argument. */
1042e4c0 14970 ALL_TRACEPOINTS (b)
46c6471b 14971 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0
SS
14972 {
14973 breaks_to_delete = 1;
14974 break;
14975 }
1042e4c0
SS
14976
14977 /* Ask user only if there are some breakpoints to delete. */
14978 if (!from_tty
14979 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14980 {
35df4500 14981 ALL_BREAKPOINTS_SAFE (b, b_tmp)
46c6471b 14982 if (is_tracepoint (b) && user_breakpoint_p (b))
1042e4c0 14983 delete_breakpoint (b);
1042e4c0
SS
14984 }
14985 }
14986 else
51be5b68 14987 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
1042e4c0
SS
14988}
14989
197f0a60
TT
14990/* Helper function for trace_pass_command. */
14991
14992static void
d9b3f62e 14993trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
197f0a60 14994{
d9b3f62e
PA
14995 tp->pass_count = count;
14996 observer_notify_tracepoint_modified (tp->base.number);
197f0a60
TT
14997 if (from_tty)
14998 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
d9b3f62e 14999 tp->base.number, count);
197f0a60
TT
15000}
15001
1042e4c0
SS
15002/* Set passcount for tracepoint.
15003
15004 First command argument is passcount, second is tracepoint number.
15005 If tracepoint number omitted, apply to most recently defined.
15006 Also accepts special argument "all". */
15007
15008static void
15009trace_pass_command (char *args, int from_tty)
15010{
d9b3f62e 15011 struct tracepoint *t1;
1042e4c0 15012 unsigned int count;
1042e4c0
SS
15013
15014 if (args == 0 || *args == 0)
3e43a32a
MS
15015 error (_("passcount command requires an "
15016 "argument (count + optional TP num)"));
1042e4c0 15017
4a64f543 15018 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
1042e4c0
SS
15019
15020 while (*args && isspace ((int) *args))
15021 args++;
15022
15023 if (*args && strncasecmp (args, "all", 3) == 0)
15024 {
d9b3f62e
PA
15025 struct breakpoint *b;
15026
1042e4c0 15027 args += 3; /* Skip special argument "all". */
1042e4c0
SS
15028 if (*args)
15029 error (_("Junk at end of arguments."));
1042e4c0 15030
d9b3f62e 15031 ALL_TRACEPOINTS (b)
197f0a60 15032 {
d9b3f62e 15033 t1 = (struct tracepoint *) b;
197f0a60
TT
15034 trace_pass_set_count (t1, count, from_tty);
15035 }
15036 }
15037 else if (*args == '\0')
1042e4c0 15038 {
197f0a60 15039 t1 = get_tracepoint_by_number (&args, NULL, 1);
1042e4c0 15040 if (t1)
197f0a60
TT
15041 trace_pass_set_count (t1, count, from_tty);
15042 }
15043 else
15044 {
15045 struct get_number_or_range_state state;
15046
15047 init_number_or_range (&state, args);
15048 while (!state.finished)
1042e4c0 15049 {
197f0a60
TT
15050 t1 = get_tracepoint_by_number (&args, &state, 1);
15051 if (t1)
15052 trace_pass_set_count (t1, count, from_tty);
1042e4c0
SS
15053 }
15054 }
1042e4c0
SS
15055}
15056
d9b3f62e 15057struct tracepoint *
1042e4c0
SS
15058get_tracepoint (int num)
15059{
15060 struct breakpoint *t;
15061
15062 ALL_TRACEPOINTS (t)
15063 if (t->number == num)
d9b3f62e 15064 return (struct tracepoint *) t;
1042e4c0
SS
15065
15066 return NULL;
15067}
15068
d5551862
SS
15069/* Find the tracepoint with the given target-side number (which may be
15070 different from the tracepoint number after disconnecting and
15071 reconnecting). */
15072
d9b3f62e 15073struct tracepoint *
d5551862
SS
15074get_tracepoint_by_number_on_target (int num)
15075{
d9b3f62e 15076 struct breakpoint *b;
d5551862 15077
d9b3f62e
PA
15078 ALL_TRACEPOINTS (b)
15079 {
15080 struct tracepoint *t = (struct tracepoint *) b;
15081
15082 if (t->number_on_target == num)
15083 return t;
15084 }
d5551862
SS
15085
15086 return NULL;
15087}
15088
1042e4c0 15089/* Utility: parse a tracepoint number and look it up in the list.
197f0a60
TT
15090 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15091 If OPTIONAL_P is true, then if the argument is missing, the most
1042e4c0 15092 recent tracepoint (tracepoint_count) is returned. */
d9b3f62e 15093struct tracepoint *
197f0a60
TT
15094get_tracepoint_by_number (char **arg,
15095 struct get_number_or_range_state *state,
15096 int optional_p)
1042e4c0
SS
15097{
15098 extern int tracepoint_count;
15099 struct breakpoint *t;
15100 int tpnum;
15101 char *instring = arg == NULL ? NULL : *arg;
15102
197f0a60
TT
15103 if (state)
15104 {
15105 gdb_assert (!state->finished);
15106 tpnum = get_number_or_range (state);
15107 }
15108 else if (arg == NULL || *arg == NULL || ! **arg)
1042e4c0
SS
15109 {
15110 if (optional_p)
15111 tpnum = tracepoint_count;
15112 else
15113 error_no_arg (_("tracepoint number"));
15114 }
15115 else
197f0a60 15116 tpnum = get_number (arg);
1042e4c0
SS
15117
15118 if (tpnum <= 0)
15119 {
15120 if (instring && *instring)
15121 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15122 instring);
15123 else
3e43a32a
MS
15124 printf_filtered (_("Tracepoint argument missing "
15125 "and no previous tracepoint\n"));
1042e4c0
SS
15126 return NULL;
15127 }
15128
15129 ALL_TRACEPOINTS (t)
15130 if (t->number == tpnum)
15131 {
d9b3f62e 15132 return (struct tracepoint *) t;
1042e4c0
SS
15133 }
15134
1042e4c0
SS
15135 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15136 return NULL;
15137}
15138
d9b3f62e
PA
15139void
15140print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15141{
15142 if (b->thread != -1)
15143 fprintf_unfiltered (fp, " thread %d", b->thread);
15144
15145 if (b->task != 0)
15146 fprintf_unfiltered (fp, " task %d", b->task);
15147
15148 fprintf_unfiltered (fp, "\n");
15149}
15150
6149aea9
PA
15151/* Save information on user settable breakpoints (watchpoints, etc) to
15152 a new script file named FILENAME. If FILTER is non-NULL, call it
15153 on each breakpoint and only include the ones for which it returns
15154 non-zero. */
15155
1042e4c0 15156static void
6149aea9
PA
15157save_breakpoints (char *filename, int from_tty,
15158 int (*filter) (const struct breakpoint *))
1042e4c0
SS
15159{
15160 struct breakpoint *tp;
6149aea9 15161 int any = 0;
a7bdde9e 15162 char *pathname;
1042e4c0 15163 struct cleanup *cleanup;
a7bdde9e 15164 struct ui_file *fp;
6149aea9 15165 int extra_trace_bits = 0;
1042e4c0 15166
6149aea9
PA
15167 if (filename == 0 || *filename == 0)
15168 error (_("Argument required (file name in which to save)"));
1042e4c0
SS
15169
15170 /* See if we have anything to save. */
6149aea9 15171 ALL_BREAKPOINTS (tp)
1042e4c0 15172 {
6149aea9 15173 /* Skip internal and momentary breakpoints. */
09d682a4 15174 if (!user_breakpoint_p (tp))
6149aea9
PA
15175 continue;
15176
15177 /* If we have a filter, only save the breakpoints it accepts. */
15178 if (filter && !filter (tp))
15179 continue;
15180
15181 any = 1;
15182
15183 if (is_tracepoint (tp))
15184 {
15185 extra_trace_bits = 1;
15186
15187 /* We can stop searching. */
15188 break;
15189 }
1042e4c0 15190 }
6149aea9
PA
15191
15192 if (!any)
1042e4c0 15193 {
6149aea9 15194 warning (_("Nothing to save."));
1042e4c0
SS
15195 return;
15196 }
15197
6149aea9 15198 pathname = tilde_expand (filename);
1042e4c0 15199 cleanup = make_cleanup (xfree, pathname);
a7bdde9e 15200 fp = gdb_fopen (pathname, "w");
059fb39f 15201 if (!fp)
6149aea9
PA
15202 error (_("Unable to open file '%s' for saving (%s)"),
15203 filename, safe_strerror (errno));
a7bdde9e 15204 make_cleanup_ui_file_delete (fp);
8bf6485c 15205
6149aea9
PA
15206 if (extra_trace_bits)
15207 save_trace_state_variables (fp);
8bf6485c 15208
6149aea9 15209 ALL_BREAKPOINTS (tp)
1042e4c0 15210 {
6149aea9 15211 /* Skip internal and momentary breakpoints. */
09d682a4 15212 if (!user_breakpoint_p (tp))
6149aea9 15213 continue;
8bf6485c 15214
6149aea9
PA
15215 /* If we have a filter, only save the breakpoints it accepts. */
15216 if (filter && !filter (tp))
15217 continue;
15218
348d480f 15219 tp->ops->print_recreate (tp, fp);
1042e4c0 15220
6149aea9
PA
15221 /* Note, we can't rely on tp->number for anything, as we can't
15222 assume the recreated breakpoint numbers will match. Use $bpnum
15223 instead. */
15224
15225 if (tp->cond_string)
15226 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15227
15228 if (tp->ignore_count)
15229 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15230
a7bdde9e 15231 if (tp->commands)
1042e4c0 15232 {
a7bdde9e
VP
15233 volatile struct gdb_exception ex;
15234
6149aea9 15235 fprintf_unfiltered (fp, " commands\n");
a7bdde9e 15236
79a45e25 15237 ui_out_redirect (current_uiout, fp);
14dba4b4 15238 TRY_CATCH (ex, RETURN_MASK_ALL)
1042e4c0 15239 {
79a45e25 15240 print_command_lines (current_uiout, tp->commands->commands, 2);
a7bdde9e 15241 }
79a45e25 15242 ui_out_redirect (current_uiout, NULL);
1042e4c0 15243
a7bdde9e
VP
15244 if (ex.reason < 0)
15245 throw_exception (ex);
1042e4c0 15246
a7bdde9e 15247 fprintf_unfiltered (fp, " end\n");
1042e4c0 15248 }
6149aea9
PA
15249
15250 if (tp->enable_state == bp_disabled)
15251 fprintf_unfiltered (fp, "disable\n");
15252
15253 /* If this is a multi-location breakpoint, check if the locations
15254 should be individually disabled. Watchpoint locations are
15255 special, and not user visible. */
15256 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15257 {
15258 struct bp_location *loc;
15259 int n = 1;
15260
15261 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15262 if (!loc->enabled)
15263 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15264 }
1042e4c0 15265 }
8bf6485c 15266
6149aea9 15267 if (extra_trace_bits && *default_collect)
8bf6485c
SS
15268 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15269
1042e4c0
SS
15270 do_cleanups (cleanup);
15271 if (from_tty)
6149aea9
PA
15272 printf_filtered (_("Saved to file '%s'.\n"), filename);
15273}
15274
15275/* The `save breakpoints' command. */
15276
15277static void
15278save_breakpoints_command (char *args, int from_tty)
15279{
15280 save_breakpoints (args, from_tty, NULL);
15281}
15282
15283/* The `save tracepoints' command. */
15284
15285static void
15286save_tracepoints_command (char *args, int from_tty)
15287{
15288 save_breakpoints (args, from_tty, is_tracepoint);
1042e4c0
SS
15289}
15290
15291/* Create a vector of all tracepoints. */
15292
15293VEC(breakpoint_p) *
eeae04df 15294all_tracepoints (void)
1042e4c0
SS
15295{
15296 VEC(breakpoint_p) *tp_vec = 0;
15297 struct breakpoint *tp;
15298
15299 ALL_TRACEPOINTS (tp)
15300 {
15301 VEC_safe_push (breakpoint_p, tp_vec, tp);
15302 }
15303
15304 return tp_vec;
15305}
15306
c906108c 15307\f
4a64f543
MS
15308/* This help string is used for the break, hbreak, tbreak and thbreak
15309 commands. It is defined as a macro to prevent duplication.
15310 COMMAND should be a string constant containing the name of the
15311 command. */
31e2b00f
AS
15312#define BREAK_ARGS_HELP(command) \
15313command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15314LOCATION may be a line number, function name, or \"*\" and an address.\n\
15315If a line number is specified, break at start of code for that line.\n\
15316If a function is specified, break at start of code for that function.\n\
15317If an address is specified, break at that exact address.\n\
dc10affe
PA
15318With no LOCATION, uses current execution address of the selected\n\
15319stack frame. This is useful for breaking on return to a stack frame.\n\
31e2b00f
AS
15320\n\
15321THREADNUM is the number from \"info threads\".\n\
15322CONDITION is a boolean expression.\n\
15323\n\
d41c0fc8
PA
15324Multiple breakpoints at one place are permitted, and useful if their\n\
15325conditions are different.\n\
31e2b00f
AS
15326\n\
15327Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15328
44feb3ce
TT
15329/* List of subcommands for "catch". */
15330static struct cmd_list_element *catch_cmdlist;
15331
15332/* List of subcommands for "tcatch". */
15333static struct cmd_list_element *tcatch_cmdlist;
15334
9ac4176b 15335void
44feb3ce
TT
15336add_catch_command (char *name, char *docstring,
15337 void (*sfunc) (char *args, int from_tty,
15338 struct cmd_list_element *command),
625e8578 15339 completer_ftype *completer,
44feb3ce
TT
15340 void *user_data_catch,
15341 void *user_data_tcatch)
15342{
15343 struct cmd_list_element *command;
15344
15345 command = add_cmd (name, class_breakpoint, NULL, docstring,
15346 &catch_cmdlist);
15347 set_cmd_sfunc (command, sfunc);
15348 set_cmd_context (command, user_data_catch);
a96d9b2e 15349 set_cmd_completer (command, completer);
44feb3ce
TT
15350
15351 command = add_cmd (name, class_breakpoint, NULL, docstring,
15352 &tcatch_cmdlist);
15353 set_cmd_sfunc (command, sfunc);
15354 set_cmd_context (command, user_data_tcatch);
a96d9b2e 15355 set_cmd_completer (command, completer);
44feb3ce
TT
15356}
15357
6c95b8df 15358static void
a79b8f6e 15359clear_syscall_counts (struct inferior *inf)
6c95b8df 15360{
fa3064dd
YQ
15361 struct catch_syscall_inferior_data *inf_data
15362 = get_catch_syscall_inferior_data (inf);
15363
15364 inf_data->total_syscalls_count = 0;
15365 inf_data->any_syscall_count = 0;
15366 VEC_free (int, inf_data->syscalls_counts);
6c95b8df
PA
15367}
15368
6149aea9
PA
15369static void
15370save_command (char *arg, int from_tty)
15371{
3e43a32a
MS
15372 printf_unfiltered (_("\"save\" must be followed by "
15373 "the name of a save subcommand.\n"));
6149aea9
PA
15374 help_list (save_cmdlist, "save ", -1, gdb_stdout);
15375}
15376
84f4c1fe
PM
15377struct breakpoint *
15378iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15379 void *data)
15380{
35df4500 15381 struct breakpoint *b, *b_tmp;
84f4c1fe 15382
35df4500 15383 ALL_BREAKPOINTS_SAFE (b, b_tmp)
84f4c1fe
PM
15384 {
15385 if ((*callback) (b, data))
15386 return b;
15387 }
15388
15389 return NULL;
15390}
15391
0574c78f
GB
15392/* Zero if any of the breakpoint's locations could be a location where
15393 functions have been inlined, nonzero otherwise. */
15394
15395static int
15396is_non_inline_function (struct breakpoint *b)
15397{
15398 /* The shared library event breakpoint is set on the address of a
15399 non-inline function. */
15400 if (b->type == bp_shlib_event)
15401 return 1;
15402
15403 return 0;
15404}
15405
15406/* Nonzero if the specified PC cannot be a location where functions
15407 have been inlined. */
15408
15409int
09ac7c10
TT
15410pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15411 const struct target_waitstatus *ws)
0574c78f
GB
15412{
15413 struct breakpoint *b;
15414 struct bp_location *bl;
15415
15416 ALL_BREAKPOINTS (b)
15417 {
15418 if (!is_non_inline_function (b))
15419 continue;
15420
15421 for (bl = b->loc; bl != NULL; bl = bl->next)
15422 {
15423 if (!bl->shlib_disabled
09ac7c10 15424 && bpstat_check_location (bl, aspace, pc, ws))
0574c78f
GB
15425 return 1;
15426 }
15427 }
15428
15429 return 0;
15430}
15431
2060206e
PA
15432void
15433initialize_breakpoint_ops (void)
15434{
15435 static int initialized = 0;
15436
15437 struct breakpoint_ops *ops;
15438
15439 if (initialized)
15440 return;
15441 initialized = 1;
15442
15443 /* The breakpoint_ops structure to be inherit by all kinds of
15444 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15445 internal and momentary breakpoints, etc.). */
15446 ops = &bkpt_base_breakpoint_ops;
15447 *ops = base_breakpoint_ops;
15448 ops->re_set = bkpt_re_set;
15449 ops->insert_location = bkpt_insert_location;
15450 ops->remove_location = bkpt_remove_location;
15451 ops->breakpoint_hit = bkpt_breakpoint_hit;
983af33b
SDJ
15452 ops->create_sals_from_address = bkpt_create_sals_from_address;
15453 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15454 ops->decode_linespec = bkpt_decode_linespec;
2060206e
PA
15455
15456 /* The breakpoint_ops structure to be used in regular breakpoints. */
15457 ops = &bkpt_breakpoint_ops;
15458 *ops = bkpt_base_breakpoint_ops;
15459 ops->re_set = bkpt_re_set;
15460 ops->resources_needed = bkpt_resources_needed;
15461 ops->print_it = bkpt_print_it;
15462 ops->print_mention = bkpt_print_mention;
15463 ops->print_recreate = bkpt_print_recreate;
15464
15465 /* Ranged breakpoints. */
15466 ops = &ranged_breakpoint_ops;
15467 *ops = bkpt_breakpoint_ops;
15468 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15469 ops->resources_needed = resources_needed_ranged_breakpoint;
15470 ops->print_it = print_it_ranged_breakpoint;
15471 ops->print_one = print_one_ranged_breakpoint;
15472 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15473 ops->print_mention = print_mention_ranged_breakpoint;
15474 ops->print_recreate = print_recreate_ranged_breakpoint;
15475
15476 /* Internal breakpoints. */
15477 ops = &internal_breakpoint_ops;
15478 *ops = bkpt_base_breakpoint_ops;
15479 ops->re_set = internal_bkpt_re_set;
15480 ops->check_status = internal_bkpt_check_status;
15481 ops->print_it = internal_bkpt_print_it;
15482 ops->print_mention = internal_bkpt_print_mention;
15483
15484 /* Momentary breakpoints. */
15485 ops = &momentary_breakpoint_ops;
15486 *ops = bkpt_base_breakpoint_ops;
15487 ops->re_set = momentary_bkpt_re_set;
15488 ops->check_status = momentary_bkpt_check_status;
15489 ops->print_it = momentary_bkpt_print_it;
15490 ops->print_mention = momentary_bkpt_print_mention;
15491
e2e4d78b
JK
15492 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15493 ops = &longjmp_breakpoint_ops;
15494 *ops = momentary_breakpoint_ops;
15495 ops->dtor = longjmp_bkpt_dtor;
15496
55aa24fb
SDJ
15497 /* Probe breakpoints. */
15498 ops = &bkpt_probe_breakpoint_ops;
15499 *ops = bkpt_breakpoint_ops;
15500 ops->insert_location = bkpt_probe_insert_location;
15501 ops->remove_location = bkpt_probe_remove_location;
15502 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15503 ops->decode_linespec = bkpt_probe_decode_linespec;
15504
2060206e
PA
15505 /* GNU v3 exception catchpoints. */
15506 ops = &gnu_v3_exception_catchpoint_ops;
15507 *ops = bkpt_breakpoint_ops;
15508 ops->print_it = print_it_exception_catchpoint;
15509 ops->print_one = print_one_exception_catchpoint;
15510 ops->print_mention = print_mention_exception_catchpoint;
15511 ops->print_recreate = print_recreate_exception_catchpoint;
15512
15513 /* Watchpoints. */
15514 ops = &watchpoint_breakpoint_ops;
15515 *ops = base_breakpoint_ops;
3a5c3e22 15516 ops->dtor = dtor_watchpoint;
2060206e
PA
15517 ops->re_set = re_set_watchpoint;
15518 ops->insert_location = insert_watchpoint;
15519 ops->remove_location = remove_watchpoint;
15520 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15521 ops->check_status = check_status_watchpoint;
15522 ops->resources_needed = resources_needed_watchpoint;
15523 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15524 ops->print_it = print_it_watchpoint;
15525 ops->print_mention = print_mention_watchpoint;
15526 ops->print_recreate = print_recreate_watchpoint;
15527
15528 /* Masked watchpoints. */
15529 ops = &masked_watchpoint_breakpoint_ops;
15530 *ops = watchpoint_breakpoint_ops;
15531 ops->insert_location = insert_masked_watchpoint;
15532 ops->remove_location = remove_masked_watchpoint;
15533 ops->resources_needed = resources_needed_masked_watchpoint;
15534 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15535 ops->print_it = print_it_masked_watchpoint;
15536 ops->print_one_detail = print_one_detail_masked_watchpoint;
15537 ops->print_mention = print_mention_masked_watchpoint;
15538 ops->print_recreate = print_recreate_masked_watchpoint;
15539
15540 /* Tracepoints. */
15541 ops = &tracepoint_breakpoint_ops;
15542 *ops = base_breakpoint_ops;
15543 ops->re_set = tracepoint_re_set;
15544 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15545 ops->print_one_detail = tracepoint_print_one_detail;
15546 ops->print_mention = tracepoint_print_mention;
15547 ops->print_recreate = tracepoint_print_recreate;
983af33b
SDJ
15548 ops->create_sals_from_address = tracepoint_create_sals_from_address;
15549 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15550 ops->decode_linespec = tracepoint_decode_linespec;
15551
55aa24fb
SDJ
15552 /* Probe tracepoints. */
15553 ops = &tracepoint_probe_breakpoint_ops;
15554 *ops = tracepoint_breakpoint_ops;
15555 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15556 ops->decode_linespec = tracepoint_probe_decode_linespec;
15557
983af33b
SDJ
15558 /* Static tracepoints with marker (`-m'). */
15559 ops = &strace_marker_breakpoint_ops;
15560 *ops = tracepoint_breakpoint_ops;
15561 ops->create_sals_from_address = strace_marker_create_sals_from_address;
15562 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15563 ops->decode_linespec = strace_marker_decode_linespec;
2060206e
PA
15564
15565 /* Fork catchpoints. */
15566 ops = &catch_fork_breakpoint_ops;
15567 *ops = base_breakpoint_ops;
15568 ops->insert_location = insert_catch_fork;
15569 ops->remove_location = remove_catch_fork;
15570 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15571 ops->print_it = print_it_catch_fork;
15572 ops->print_one = print_one_catch_fork;
15573 ops->print_mention = print_mention_catch_fork;
15574 ops->print_recreate = print_recreate_catch_fork;
15575
15576 /* Vfork catchpoints. */
15577 ops = &catch_vfork_breakpoint_ops;
15578 *ops = base_breakpoint_ops;
15579 ops->insert_location = insert_catch_vfork;
15580 ops->remove_location = remove_catch_vfork;
15581 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15582 ops->print_it = print_it_catch_vfork;
15583 ops->print_one = print_one_catch_vfork;
15584 ops->print_mention = print_mention_catch_vfork;
15585 ops->print_recreate = print_recreate_catch_vfork;
15586
15587 /* Exec catchpoints. */
15588 ops = &catch_exec_breakpoint_ops;
15589 *ops = base_breakpoint_ops;
15590 ops->dtor = dtor_catch_exec;
15591 ops->insert_location = insert_catch_exec;
15592 ops->remove_location = remove_catch_exec;
15593 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15594 ops->print_it = print_it_catch_exec;
15595 ops->print_one = print_one_catch_exec;
15596 ops->print_mention = print_mention_catch_exec;
15597 ops->print_recreate = print_recreate_catch_exec;
15598
15599 /* Syscall catchpoints. */
15600 ops = &catch_syscall_breakpoint_ops;
15601 *ops = base_breakpoint_ops;
15602 ops->dtor = dtor_catch_syscall;
15603 ops->insert_location = insert_catch_syscall;
15604 ops->remove_location = remove_catch_syscall;
15605 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
15606 ops->print_it = print_it_catch_syscall;
15607 ops->print_one = print_one_catch_syscall;
15608 ops->print_mention = print_mention_catch_syscall;
15609 ops->print_recreate = print_recreate_catch_syscall;
edcc5120
TT
15610
15611 /* Solib-related catchpoints. */
15612 ops = &catch_solib_breakpoint_ops;
15613 *ops = base_breakpoint_ops;
15614 ops->dtor = dtor_catch_solib;
15615 ops->insert_location = insert_catch_solib;
15616 ops->remove_location = remove_catch_solib;
15617 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15618 ops->check_status = check_status_catch_solib;
15619 ops->print_it = print_it_catch_solib;
15620 ops->print_one = print_one_catch_solib;
15621 ops->print_mention = print_mention_catch_solib;
15622 ops->print_recreate = print_recreate_catch_solib;
e7e0cddf
SS
15623
15624 ops = &dprintf_breakpoint_ops;
15625 *ops = bkpt_base_breakpoint_ops;
15626 ops->re_set = bkpt_re_set;
15627 ops->resources_needed = bkpt_resources_needed;
15628 ops->print_it = bkpt_print_it;
15629 ops->print_mention = bkpt_print_mention;
15630 ops->print_recreate = bkpt_print_recreate;
2060206e
PA
15631}
15632
c906108c 15633void
fba45db2 15634_initialize_breakpoint (void)
c906108c
SS
15635{
15636 struct cmd_list_element *c;
15637
2060206e
PA
15638 initialize_breakpoint_ops ();
15639
84acb35a 15640 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
6c95b8df 15641 observer_attach_inferior_exit (clear_syscall_counts);
1f3b5d1b 15642 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
84acb35a 15643
55aa24fb
SDJ
15644 breakpoint_objfile_key
15645 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
17450429 15646
fa3064dd
YQ
15647 catch_syscall_inferior_data
15648 = register_inferior_data_with_cleanup (catch_syscall_inferior_data_cleanup);
15649
c906108c
SS
15650 breakpoint_chain = 0;
15651 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15652 before a breakpoint is set. */
15653 breakpoint_count = 0;
15654
1042e4c0
SS
15655 tracepoint_count = 0;
15656
1bedd215
AC
15657 add_com ("ignore", class_breakpoint, ignore_command, _("\
15658Set ignore-count of breakpoint number N to COUNT.\n\
15659Usage is `ignore N COUNT'."));
c906108c 15660 if (xdb_commands)
c5aa993b 15661 add_com_alias ("bc", "ignore", class_breakpoint, 1);
c906108c 15662
1bedd215
AC
15663 add_com ("commands", class_breakpoint, commands_command, _("\
15664Set commands to be executed when a breakpoint is hit.\n\
c906108c
SS
15665Give breakpoint number as argument after \"commands\".\n\
15666With no argument, the targeted breakpoint is the last one set.\n\
15667The commands themselves follow starting on the next line.\n\
15668Type a line containing \"end\" to indicate the end of them.\n\
15669Give \"silent\" as the first line to make the breakpoint silent;\n\
1bedd215 15670then no output is printed when it is hit, except what the commands print."));
c906108c 15671
d55637df 15672 c = add_com ("condition", class_breakpoint, condition_command, _("\
1bedd215 15673Specify breakpoint number N to break only if COND is true.\n\
c906108c 15674Usage is `condition N COND', where N is an integer and COND is an\n\
1bedd215 15675expression to be evaluated whenever breakpoint N is reached."));
d55637df 15676 set_cmd_completer (c, condition_completer);
c906108c 15677
1bedd215 15678 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
31e2b00f 15679Set a temporary breakpoint.\n\
c906108c
SS
15680Like \"break\" except the breakpoint is only temporary,\n\
15681so it will be deleted when hit. Equivalent to \"break\" followed\n\
31e2b00f
AS
15682by using \"enable delete\" on the breakpoint number.\n\
15683\n"
15684BREAK_ARGS_HELP ("tbreak")));
5ba2abeb 15685 set_cmd_completer (c, location_completer);
c94fdfd0 15686
1bedd215 15687 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
a3be7890 15688Set a hardware assisted breakpoint.\n\
c906108c 15689Like \"break\" except the breakpoint requires hardware support,\n\
31e2b00f
AS
15690some target hardware may not have this support.\n\
15691\n"
15692BREAK_ARGS_HELP ("hbreak")));
5ba2abeb 15693 set_cmd_completer (c, location_completer);
c906108c 15694
1bedd215 15695 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
31e2b00f 15696Set a temporary hardware assisted breakpoint.\n\
c906108c 15697Like \"hbreak\" except the breakpoint is only temporary,\n\
31e2b00f
AS
15698so it will be deleted when hit.\n\
15699\n"
15700BREAK_ARGS_HELP ("thbreak")));
5ba2abeb 15701 set_cmd_completer (c, location_completer);
c906108c 15702
1bedd215
AC
15703 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15704Enable some breakpoints.\n\
c906108c
SS
15705Give breakpoint numbers (separated by spaces) as arguments.\n\
15706With no subcommand, breakpoints are enabled until you command otherwise.\n\
15707This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15708With a subcommand you can enable temporarily."),
c906108c
SS
15709 &enablelist, "enable ", 1, &cmdlist);
15710 if (xdb_commands)
1bedd215
AC
15711 add_com ("ab", class_breakpoint, enable_command, _("\
15712Enable some breakpoints.\n\
c906108c
SS
15713Give breakpoint numbers (separated by spaces) as arguments.\n\
15714With no subcommand, breakpoints are enabled until you command otherwise.\n\
15715This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15716With a subcommand you can enable temporarily."));
c906108c
SS
15717
15718 add_com_alias ("en", "enable", class_breakpoint, 1);
15719
84951ab5 15720 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
1bedd215 15721Enable some breakpoints.\n\
c906108c
SS
15722Give breakpoint numbers (separated by spaces) as arguments.\n\
15723This is used to cancel the effect of the \"disable\" command.\n\
1bedd215 15724May be abbreviated to simply \"enable\".\n"),
c5aa993b 15725 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
c906108c 15726
1a966eab
AC
15727 add_cmd ("once", no_class, enable_once_command, _("\
15728Enable breakpoints for one hit. Give breakpoint numbers.\n\
15729If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
c906108c
SS
15730 &enablebreaklist);
15731
1a966eab
AC
15732 add_cmd ("delete", no_class, enable_delete_command, _("\
15733Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15734If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15735 &enablebreaklist);
15736
816338b5
SS
15737 add_cmd ("count", no_class, enable_count_command, _("\
15738Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15739If a breakpoint is hit while enabled in this fashion,\n\
15740the count is decremented; when it reaches zero, the breakpoint is disabled."),
15741 &enablebreaklist);
15742
1a966eab
AC
15743 add_cmd ("delete", no_class, enable_delete_command, _("\
15744Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15745If a breakpoint is hit while enabled in this fashion, it is deleted."),
c906108c
SS
15746 &enablelist);
15747
1a966eab
AC
15748 add_cmd ("once", no_class, enable_once_command, _("\
15749Enable breakpoints for one hit. Give breakpoint numbers.\n\
15750If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
816338b5
SS
15751 &enablelist);
15752
15753 add_cmd ("count", no_class, enable_count_command, _("\
15754Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15755If a breakpoint is hit while enabled in this fashion,\n\
15756the count is decremented; when it reaches zero, the breakpoint is disabled."),
c906108c
SS
15757 &enablelist);
15758
1bedd215
AC
15759 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15760Disable some breakpoints.\n\
c906108c
SS
15761Arguments are breakpoint numbers with spaces in between.\n\
15762To disable all breakpoints, give no argument.\n\
64b9b334 15763A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
c906108c
SS
15764 &disablelist, "disable ", 1, &cmdlist);
15765 add_com_alias ("dis", "disable", class_breakpoint, 1);
15766 add_com_alias ("disa", "disable", class_breakpoint, 1);
15767 if (xdb_commands)
1bedd215
AC
15768 add_com ("sb", class_breakpoint, disable_command, _("\
15769Disable some breakpoints.\n\
c906108c
SS
15770Arguments are breakpoint numbers with spaces in between.\n\
15771To disable all breakpoints, give no argument.\n\
64b9b334 15772A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
c906108c 15773
1a966eab
AC
15774 add_cmd ("breakpoints", class_alias, disable_command, _("\
15775Disable some breakpoints.\n\
c906108c
SS
15776Arguments are breakpoint numbers with spaces in between.\n\
15777To disable all breakpoints, give no argument.\n\
64b9b334 15778A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
1a966eab 15779This command may be abbreviated \"disable\"."),
c906108c
SS
15780 &disablelist);
15781
1bedd215
AC
15782 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15783Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15784Arguments are breakpoint numbers with spaces in between.\n\
15785To delete all breakpoints, give no argument.\n\
15786\n\
15787Also a prefix command for deletion of other GDB objects.\n\
1bedd215 15788The \"unset\" command is also an alias for \"delete\"."),
c906108c
SS
15789 &deletelist, "delete ", 1, &cmdlist);
15790 add_com_alias ("d", "delete", class_breakpoint, 1);
7f198e01 15791 add_com_alias ("del", "delete", class_breakpoint, 1);
c906108c 15792 if (xdb_commands)
1bedd215
AC
15793 add_com ("db", class_breakpoint, delete_command, _("\
15794Delete some breakpoints.\n\
c906108c 15795Arguments are breakpoint numbers with spaces in between.\n\
1bedd215 15796To delete all breakpoints, give no argument.\n"));
c906108c 15797
1a966eab
AC
15798 add_cmd ("breakpoints", class_alias, delete_command, _("\
15799Delete some breakpoints or auto-display expressions.\n\
c906108c
SS
15800Arguments are breakpoint numbers with spaces in between.\n\
15801To delete all breakpoints, give no argument.\n\
1a966eab 15802This command may be abbreviated \"delete\"."),
c906108c
SS
15803 &deletelist);
15804
1bedd215
AC
15805 add_com ("clear", class_breakpoint, clear_command, _("\
15806Clear breakpoint at specified line or function.\n\
c906108c
SS
15807Argument may be line number, function name, or \"*\" and an address.\n\
15808If line number is specified, all breakpoints in that line are cleared.\n\
15809If function is specified, breakpoints at beginning of function are cleared.\n\
1bedd215
AC
15810If an address is specified, breakpoints at that address are cleared.\n\
15811\n\
15812With no argument, clears all breakpoints in the line that the selected frame\n\
c906108c
SS
15813is executing in.\n\
15814\n\
1bedd215 15815See also the \"delete\" command which clears breakpoints by number."));
a6cc4789 15816 add_com_alias ("cl", "clear", class_breakpoint, 1);
c906108c 15817
1bedd215 15818 c = add_com ("break", class_breakpoint, break_command, _("\
31e2b00f
AS
15819Set breakpoint at specified line or function.\n"
15820BREAK_ARGS_HELP ("break")));
5ba2abeb 15821 set_cmd_completer (c, location_completer);
c94fdfd0 15822
c906108c
SS
15823 add_com_alias ("b", "break", class_run, 1);
15824 add_com_alias ("br", "break", class_run, 1);
15825 add_com_alias ("bre", "break", class_run, 1);
15826 add_com_alias ("brea", "break", class_run, 1);
15827
7681d515
PM
15828 if (xdb_commands)
15829 add_com_alias ("ba", "break", class_breakpoint, 1);
c906108c
SS
15830
15831 if (dbx_commands)
15832 {
1bedd215
AC
15833 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15834Break in function/address or break at a line in the current file."),
c5aa993b
JM
15835 &stoplist, "stop ", 1, &cmdlist);
15836 add_cmd ("in", class_breakpoint, stopin_command,
1a966eab 15837 _("Break in function or address."), &stoplist);
c5aa993b 15838 add_cmd ("at", class_breakpoint, stopat_command,
1a966eab 15839 _("Break at a line in the current file."), &stoplist);
1bedd215
AC
15840 add_com ("status", class_info, breakpoints_info, _("\
15841Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15842The \"Type\" column indicates one of:\n\
15843\tbreakpoint - normal breakpoint\n\
15844\twatchpoint - watchpoint\n\
15845The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15846the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15847breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15848address and file/line number respectively.\n\
15849\n\
15850Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15851are set to the address of the last breakpoint listed unless the command\n\
15852is prefixed with \"server \".\n\n\
c906108c 15853Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15854breakpoint set."));
c906108c
SS
15855 }
15856
1bedd215 15857 add_info ("breakpoints", breakpoints_info, _("\
e5a67952 15858Status of specified breakpoints (all user-settable breakpoints if no argument).\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 15872
6b04bdb7
MS
15873 add_info_alias ("b", "breakpoints", 1);
15874
c906108c 15875 if (xdb_commands)
1bedd215
AC
15876 add_com ("lb", class_breakpoint, breakpoints_info, _("\
15877Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15878The \"Type\" column indicates one of:\n\
15879\tbreakpoint - normal breakpoint\n\
15880\twatchpoint - watchpoint\n\
15881The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15882the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15883breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1bedd215
AC
15884address and file/line number respectively.\n\
15885\n\
15886Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15887are set to the address of the last breakpoint listed unless the command\n\
15888is prefixed with \"server \".\n\n\
c906108c 15889Convenience variable \"$bpnum\" contains the number of the last\n\
1bedd215 15890breakpoint set."));
c906108c 15891
1a966eab
AC
15892 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15893Status of all breakpoints, or breakpoint number NUMBER.\n\
c906108c
SS
15894The \"Type\" column indicates one of:\n\
15895\tbreakpoint - normal breakpoint\n\
15896\twatchpoint - watchpoint\n\
15897\tlongjmp - internal breakpoint used to step through longjmp()\n\
15898\tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15899\tuntil - internal breakpoint used by the \"until\" command\n\
1a966eab
AC
15900\tfinish - internal breakpoint used by the \"finish\" command\n\
15901The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
c906108c
SS
15902the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15903breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
1a966eab
AC
15904address and file/line number respectively.\n\
15905\n\
15906Convenience variable \"$_\" and default examine address for \"x\"\n\
d1aa2f50
NR
15907are set to the address of the last breakpoint listed unless the command\n\
15908is prefixed with \"server \".\n\n\
c906108c 15909Convenience variable \"$bpnum\" contains the number of the last\n\
1a966eab 15910breakpoint set."),
c906108c
SS
15911 &maintenanceinfolist);
15912
44feb3ce
TT
15913 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15914Set catchpoints to catch events."),
15915 &catch_cmdlist, "catch ",
15916 0/*allow-unknown*/, &cmdlist);
15917
15918 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15919Set temporary catchpoints to catch events."),
15920 &tcatch_cmdlist, "tcatch ",
15921 0/*allow-unknown*/, &cmdlist);
15922
15923 /* Add catch and tcatch sub-commands. */
15924 add_catch_command ("catch", _("\
88e7d25d 15925Catch an exception, when caught."),
44feb3ce 15926 catch_catch_command,
a96d9b2e 15927 NULL,
44feb3ce
TT
15928 CATCH_PERMANENT,
15929 CATCH_TEMPORARY);
15930 add_catch_command ("throw", _("\
88e7d25d 15931Catch an exception, when thrown."),
44feb3ce 15932 catch_throw_command,
a96d9b2e 15933 NULL,
44feb3ce
TT
15934 CATCH_PERMANENT,
15935 CATCH_TEMPORARY);
15936 add_catch_command ("fork", _("Catch calls to fork."),
15937 catch_fork_command_1,
a96d9b2e 15938 NULL,
44feb3ce
TT
15939 (void *) (uintptr_t) catch_fork_permanent,
15940 (void *) (uintptr_t) catch_fork_temporary);
15941 add_catch_command ("vfork", _("Catch calls to vfork."),
15942 catch_fork_command_1,
a96d9b2e 15943 NULL,
44feb3ce
TT
15944 (void *) (uintptr_t) catch_vfork_permanent,
15945 (void *) (uintptr_t) catch_vfork_temporary);
15946 add_catch_command ("exec", _("Catch calls to exec."),
15947 catch_exec_command_1,
a96d9b2e
SDJ
15948 NULL,
15949 CATCH_PERMANENT,
15950 CATCH_TEMPORARY);
edcc5120
TT
15951 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15952Usage: catch load [REGEX]\n\
15953If REGEX is given, only stop for libraries matching the regular expression."),
15954 catch_load_command_1,
15955 NULL,
15956 CATCH_PERMANENT,
15957 CATCH_TEMPORARY);
15958 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15959Usage: catch unload [REGEX]\n\
15960If REGEX is given, only stop for libraries matching the regular expression."),
15961 catch_unload_command_1,
15962 NULL,
15963 CATCH_PERMANENT,
15964 CATCH_TEMPORARY);
a96d9b2e
SDJ
15965 add_catch_command ("syscall", _("\
15966Catch system calls by their names and/or numbers.\n\
15967Arguments say which system calls to catch. If no arguments\n\
15968are given, every system call will be caught.\n\
15969Arguments, if given, should be one or more system call names\n\
15970(if your system supports that), or system call numbers."),
15971 catch_syscall_command_1,
15972 catch_syscall_completer,
44feb3ce
TT
15973 CATCH_PERMANENT,
15974 CATCH_TEMPORARY);
c5aa993b 15975
1bedd215
AC
15976 c = add_com ("watch", class_breakpoint, watch_command, _("\
15977Set a watchpoint for an expression.\n\
06a64a0b 15978Usage: watch [-l|-location] EXPRESSION\n\
c906108c 15979A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15980an expression changes.\n\
15981If -l or -location is given, this evaluates EXPRESSION and watches\n\
15982the memory to which it refers."));
65d12d83 15983 set_cmd_completer (c, expression_completer);
c906108c 15984
1bedd215
AC
15985 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15986Set a read watchpoint for an expression.\n\
06a64a0b 15987Usage: rwatch [-l|-location] EXPRESSION\n\
c906108c 15988A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15989an expression is read.\n\
15990If -l or -location is given, this evaluates EXPRESSION and watches\n\
15991the memory to which it refers."));
65d12d83 15992 set_cmd_completer (c, expression_completer);
c906108c 15993
1bedd215
AC
15994 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15995Set a watchpoint for an expression.\n\
06a64a0b 15996Usage: awatch [-l|-location] EXPRESSION\n\
c906108c 15997A watchpoint stops execution of your program whenever the value of\n\
06a64a0b
TT
15998an expression is either read or written.\n\
15999If -l or -location is given, this evaluates EXPRESSION and watches\n\
16000the memory to which it refers."));
65d12d83 16001 set_cmd_completer (c, expression_completer);
c906108c 16002
d77f58be 16003 add_info ("watchpoints", watchpoints_info, _("\
e5a67952 16004Status of specified watchpoints (all watchpoints if no argument)."));
c906108c 16005
920d2a44
AC
16006 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16007 respond to changes - contrary to the description. */
85c07804
AC
16008 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16009 &can_use_hw_watchpoints, _("\
16010Set debugger's willingness to use watchpoint hardware."), _("\
16011Show debugger's willingness to use watchpoint hardware."), _("\
c906108c
SS
16012If zero, gdb will not use hardware for new watchpoints, even if\n\
16013such is available. (However, any hardware watchpoints that were\n\
16014created before setting this to nonzero, will continue to use watchpoint\n\
85c07804
AC
16015hardware.)"),
16016 NULL,
920d2a44 16017 show_can_use_hw_watchpoints,
85c07804 16018 &setlist, &showlist);
c906108c
SS
16019
16020 can_use_hw_watchpoints = 1;
fa8d40ab 16021
1042e4c0
SS
16022 /* Tracepoint manipulation commands. */
16023
16024 c = add_com ("trace", class_breakpoint, trace_command, _("\
16025Set a tracepoint at specified line or function.\n\
16026\n"
16027BREAK_ARGS_HELP ("trace") "\n\
16028Do \"help tracepoints\" for info on other tracepoint commands."));
16029 set_cmd_completer (c, location_completer);
16030
16031 add_com_alias ("tp", "trace", class_alias, 0);
16032 add_com_alias ("tr", "trace", class_alias, 1);
16033 add_com_alias ("tra", "trace", class_alias, 1);
16034 add_com_alias ("trac", "trace", class_alias, 1);
16035
7a697b8d
SS
16036 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16037Set a fast tracepoint at specified line or function.\n\
16038\n"
16039BREAK_ARGS_HELP ("ftrace") "\n\
16040Do \"help tracepoints\" for info on other tracepoint commands."));
16041 set_cmd_completer (c, location_completer);
16042
0fb4aa4b
PA
16043 c = add_com ("strace", class_breakpoint, strace_command, _("\
16044Set a static tracepoint at specified line, function or marker.\n\
16045\n\
16046strace [LOCATION] [if CONDITION]\n\
16047LOCATION may be a line number, function name, \"*\" and an address,\n\
16048or -m MARKER_ID.\n\
16049If a line number is specified, probe the marker at start of code\n\
16050for that line. If a function is specified, probe the marker at start\n\
16051of code for that function. If an address is specified, probe the marker\n\
16052at that exact address. If a marker id is specified, probe the marker\n\
16053with that name. With no LOCATION, uses current execution address of\n\
16054the selected stack frame.\n\
16055Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16056This collects arbitrary user data passed in the probe point call to the\n\
16057tracing library. You can inspect it when analyzing the trace buffer,\n\
16058by printing the $_sdata variable like any other convenience variable.\n\
16059\n\
16060CONDITION is a boolean expression.\n\
16061\n\
d41c0fc8
PA
16062Multiple tracepoints at one place are permitted, and useful if their\n\
16063conditions are different.\n\
0fb4aa4b
PA
16064\n\
16065Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16066Do \"help tracepoints\" for info on other tracepoint commands."));
16067 set_cmd_completer (c, location_completer);
16068
1042e4c0 16069 add_info ("tracepoints", tracepoints_info, _("\
e5a67952 16070Status of specified tracepoints (all tracepoints if no argument).\n\
1042e4c0
SS
16071Convenience variable \"$tpnum\" contains the number of the\n\
16072last tracepoint set."));
16073
16074 add_info_alias ("tp", "tracepoints", 1);
16075
16076 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16077Delete specified tracepoints.\n\
16078Arguments are tracepoint numbers, separated by spaces.\n\
16079No argument means delete all tracepoints."),
16080 &deletelist);
16081
16082 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16083Disable specified tracepoints.\n\
16084Arguments are tracepoint numbers, separated by spaces.\n\
16085No argument means disable all tracepoints."),
16086 &disablelist);
16087 deprecate_cmd (c, "disable");
16088
16089 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16090Enable specified tracepoints.\n\
16091Arguments are tracepoint numbers, separated by spaces.\n\
16092No argument means enable all tracepoints."),
16093 &enablelist);
16094 deprecate_cmd (c, "enable");
16095
16096 add_com ("passcount", class_trace, trace_pass_command, _("\
16097Set the passcount for a tracepoint.\n\
16098The trace will end when the tracepoint has been passed 'count' times.\n\
16099Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16100if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16101
6149aea9
PA
16102 add_prefix_cmd ("save", class_breakpoint, save_command,
16103 _("Save breakpoint definitions as a script."),
16104 &save_cmdlist, "save ",
16105 0/*allow-unknown*/, &cmdlist);
16106
16107 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16108Save current breakpoint definitions as a script.\n\
cce7e648 16109This includes all types of breakpoints (breakpoints, watchpoints,\n\
6149aea9
PA
16110catchpoints, tracepoints). Use the 'source' command in another debug\n\
16111session to restore them."),
16112 &save_cmdlist);
16113 set_cmd_completer (c, filename_completer);
16114
16115 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
1042e4c0 16116Save current tracepoint definitions as a script.\n\
6149aea9
PA
16117Use the 'source' command in another debug session to restore them."),
16118 &save_cmdlist);
1042e4c0
SS
16119 set_cmd_completer (c, filename_completer);
16120
6149aea9
PA
16121 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16122 deprecate_cmd (c, "save tracepoints");
16123
1bedd215 16124 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
fa8d40ab
JJ
16125Breakpoint specific settings\n\
16126Configure various breakpoint-specific variables such as\n\
1bedd215 16127pending breakpoint behavior"),
fa8d40ab
JJ
16128 &breakpoint_set_cmdlist, "set breakpoint ",
16129 0/*allow-unknown*/, &setlist);
1bedd215 16130 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
fa8d40ab
JJ
16131Breakpoint specific settings\n\
16132Configure various breakpoint-specific variables such as\n\
1bedd215 16133pending breakpoint behavior"),
fa8d40ab
JJ
16134 &breakpoint_show_cmdlist, "show breakpoint ",
16135 0/*allow-unknown*/, &showlist);
16136
7915a72c
AC
16137 add_setshow_auto_boolean_cmd ("pending", no_class,
16138 &pending_break_support, _("\
16139Set debugger's behavior regarding pending breakpoints."), _("\
16140Show debugger's behavior regarding pending breakpoints."), _("\
6e1d7d6c
AC
16141If on, an unrecognized breakpoint location will cause gdb to create a\n\
16142pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16143an error. If auto, an unrecognized breakpoint location results in a\n\
7915a72c 16144user-query to see if a pending breakpoint should be created."),
2c5b56ce 16145 NULL,
920d2a44 16146 show_pending_break_support,
6e1d7d6c
AC
16147 &breakpoint_set_cmdlist,
16148 &breakpoint_show_cmdlist);
fa8d40ab
JJ
16149
16150 pending_break_support = AUTO_BOOLEAN_AUTO;
765dc015
VP
16151
16152 add_setshow_boolean_cmd ("auto-hw", no_class,
16153 &automatic_hardware_breakpoints, _("\
16154Set automatic usage of hardware breakpoints."), _("\
16155Show automatic usage of hardware breakpoints."), _("\
16156If set, the debugger will automatically use hardware breakpoints for\n\
16157breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16158a warning will be emitted for such breakpoints."),
16159 NULL,
16160 show_automatic_hardware_breakpoints,
16161 &breakpoint_set_cmdlist,
16162 &breakpoint_show_cmdlist);
74960c60 16163
33e5cbd6
PA
16164 add_setshow_enum_cmd ("always-inserted", class_support,
16165 always_inserted_enums, &always_inserted_mode, _("\
74960c60
VP
16166Set mode for inserting breakpoints."), _("\
16167Show mode for inserting breakpoints."), _("\
33e5cbd6
PA
16168When this mode is off, breakpoints are inserted in inferior when it is\n\
16169resumed, and removed when execution stops. When this mode is on,\n\
16170breakpoints are inserted immediately and removed only when the user\n\
16171deletes the breakpoint. When this mode is auto (which is the default),\n\
16172the behaviour depends on the non-stop setting (see help set non-stop).\n\
16173In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16174behaves as if always-inserted mode is on; if gdb is controlling the\n\
16175inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
74960c60
VP
16176 NULL,
16177 &show_always_inserted_mode,
16178 &breakpoint_set_cmdlist,
16179 &breakpoint_show_cmdlist);
f1310107 16180
b775012e
LM
16181 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16182 condition_evaluation_enums,
16183 &condition_evaluation_mode_1, _("\
16184Set mode of breakpoint condition evaluation."), _("\
16185Show mode of breakpoint condition evaluation."), _("\
d1cda5d9 16186When this is set to \"host\", breakpoint conditions will be\n\
b775012e
LM
16187evaluated on the host's side by GDB. When it is set to \"target\",\n\
16188breakpoint conditions will be downloaded to the target (if the target\n\
16189supports such feature) and conditions will be evaluated on the target's side.\n\
16190If this is set to \"auto\" (default), this will be automatically set to\n\
16191\"target\" if it supports condition evaluation, otherwise it will\n\
16192be set to \"gdb\""),
16193 &set_condition_evaluation_mode,
16194 &show_condition_evaluation_mode,
16195 &breakpoint_set_cmdlist,
16196 &breakpoint_show_cmdlist);
16197
f1310107
TJB
16198 add_com ("break-range", class_breakpoint, break_range_command, _("\
16199Set a breakpoint for an address range.\n\
16200break-range START-LOCATION, END-LOCATION\n\
16201where START-LOCATION and END-LOCATION can be one of the following:\n\
16202 LINENUM, for that line in the current file,\n\
16203 FILE:LINENUM, for that line in that file,\n\
16204 +OFFSET, for that number of lines after the current line\n\
16205 or the start of the range\n\
16206 FUNCTION, for the first line in that function,\n\
16207 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16208 *ADDRESS, for the instruction at that address.\n\
16209\n\
16210The breakpoint will stop execution of the inferior whenever it executes\n\
16211an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16212range (including START-LOCATION and END-LOCATION)."));
16213
e7e0cddf
SS
16214 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16215Set a dynamic printf at specified line or function.\n\
16216dprintf location,format string,arg1,arg2,...\n\
16217location may be a line number, function name, or \"*\" and an address.\n\
16218If a line number is specified, break at start of code for that line.\n\
16219If a function is specified, break at start of code for that function.\n\
16220"));
16221 set_cmd_completer (c, location_completer);
16222
16223 add_setshow_enum_cmd ("dprintf-style", class_support,
16224 dprintf_style_enums, &dprintf_style, _("\
16225Set the style of usage for dynamic printf."), _("\
16226Show the style of usage for dynamic printf."), _("\
16227This setting chooses how GDB will do a dynamic printf.\n\
16228If the value is \"gdb\", then the printing is done by GDB to its own\n\
16229console, as with the \"printf\" command.\n\
16230If the value is \"call\", the print is done by calling a function in your\n\
16231program; by default printf(), but you can choose a different function or\n\
16232output stream by setting dprintf-function and dprintf-channel."),
16233 update_dprintf_commands, NULL,
16234 &setlist, &showlist);
16235
16236 dprintf_function = xstrdup ("printf");
16237 add_setshow_string_cmd ("dprintf-function", class_support,
16238 &dprintf_function, _("\
16239Set the function to use for dynamic printf"), _("\
16240Show the function to use for dynamic printf"), NULL,
16241 update_dprintf_commands, NULL,
16242 &setlist, &showlist);
16243
16244 dprintf_channel = xstrdup ("");
16245 add_setshow_string_cmd ("dprintf-channel", class_support,
16246 &dprintf_channel, _("\
16247Set the channel to use for dynamic printf"), _("\
16248Show the channel to use for dynamic printf"), NULL,
16249 update_dprintf_commands, NULL,
16250 &setlist, &showlist);
16251
765dc015 16252 automatic_hardware_breakpoints = 1;
f3b1572e
PA
16253
16254 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
c906108c 16255}
This page took 2.362714 seconds and 4 git commands to generate.